refactor texture decode and some test

This commit is contained in:
alexiscatnip
2022-01-22 02:45:02 +08:00
parent 1ac09071aa
commit 7711722a98
8 changed files with 89 additions and 48 deletions
@@ -27,6 +27,7 @@
using System;
using OpenMetaverse.Imaging;
using UnityEngine;
namespace OpenMetaverse.Assets
{
@@ -97,9 +98,10 @@ namespace OpenMetaverse.Assets
this.Components = 0;
Texture2D texture = new Texture2D(1,1);
// using (var reader = new OpenJpegDotNet.IO.Reader(AssetData))
var texture = Imaging.T2D.LoadT2DWithoutMipMaps(AssetData);
Image = new ManagedImage(texture);
T2D.LoadT2DWithoutMipMaps(AssetData, texture); //blocking.
Image = new ManagedImage(texture); //blocking.
// using (var reader = new OpenJpegDotNet.IO.Reader(AssetData))
// {
// // *hack: decode from ManagedImage directly or better yet, get rid of ManagedImage entirely!
+7 -3
View File
@@ -1,12 +1,16 @@
using UnityEngine;
using System;
using UnityEngine;
using UnityEngine.PlayerLoop;
namespace OpenMetaverse.Imaging
{
public class T2D
{
public static Texture2D LoadT2DWithoutMipMaps(byte[] thebytes)
// Create a NEW T2D, by providing a bitmap in byte-array form.
public static Texture2D LoadT2DWithoutMipMaps(byte[] thebytes, Texture2D texture)
{
var texture = new Texture2D(1, 1, TextureFormat.ARGB32, false); // impt there is a jobs bug here
// texture = new Texture2D(1, 1, TextureFormat.ARGB32, false); // impt there is a jobs bug here
//also, new Texture2d can't be called outside the main thread!
var loaderSettings = AsyncImageLoader.LoaderSettings.Default;
loaderSettings.generateMipmap = false; // impt there is a bug here
+42 -33
View File
@@ -2,50 +2,59 @@
namespace Raindrop.Disk
{
public class DirectoryHelpers
public static class DirectoryHelpers
{
// returns SD card if the bool is true OR the sd card is not available.
// otherwise i will return internal-but-shared storage
public static string GetAndroidExternalFilesDir(bool preferSDcard)
// returns SD card if the bool is true OR the sd card is not available.
// otherwise i will return internal-but-shared storage
private static string GetAndroidExternalFilesDir(bool preferSDcard)
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
using (AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
// Get all available external file directories (emulated and sdCards)
AndroidJavaObject[] externalFilesDirectories = context.Call<AndroidJavaObject[],AndroidJavaObject[]>("getExternalFilesDirs", null);
AndroidJavaObject emulated = null;
AndroidJavaObject sdCard = null;
// Get all available external file directories (emulated and sdCards)
AndroidJavaObject[] externalFilesDirectories = context.Call<AndroidJavaObject[],AndroidJavaObject[]>("getExternalFilesDirs", null);
AndroidJavaObject emulated = null;
AndroidJavaObject sdCard = null;
for (int i = 0; i < externalFilesDirectories.Length; i++)
for (int i = 0; i < externalFilesDirectories.Length; i++)
{
AndroidJavaObject directory = externalFilesDirectories[i];
using (AndroidJavaClass environment = new AndroidJavaClass("android.os.Environment"))
{
AndroidJavaObject directory = externalFilesDirectories[i];
using (AndroidJavaClass environment = new AndroidJavaClass("android.os.Environment"))
{
// Check which one is the emulated and which the sdCard.
bool isRemovable = environment.CallStatic<bool> ("isExternalStorageRemovable", directory);
bool isEmulated = environment.CallStatic<bool> ("isExternalStorageEmulated", directory);
if (isEmulated)
emulated = directory;
else if (isRemovable && isEmulated == false)
sdCard = directory;
}
// Check which one is the emulated and which the sdCard.
bool isRemovable = environment.CallStatic<bool> ("isExternalStorageRemovable", directory);
bool isEmulated = environment.CallStatic<bool> ("isExternalStorageEmulated", directory);
if (isEmulated)
emulated = directory;
else if (isRemovable && isEmulated == false)
sdCard = directory;
}
// Return the sdCard if available
if (sdCard != null && preferSDcard)
return sdCard.Call<string>("getAbsolutePath");
else
return emulated.Call<string>("getAbsolutePath");
}
// Return the sdCard if available
if (sdCard != null && preferSDcard)
return sdCard.Call<string>("getAbsolutePath");
else
return emulated.Call<string>("getAbsolutePath");
}
}
}
//gives us the base directory where we should be storing the cache files
public static string GetCacheDir()
{
return Application.persistentDataPath; //todo : correctly implement this.
//gives us the base directory where we should be storing the cache files
public static string GetCacheDir()
{
#if UNITY_EDITOR
return Application.persistentDataPath; //todo : correctly implement this.
#endif
#if UNITY_ANDROID
return GetAndroidExternalFilesDir(true); //todo : correctly implement this.
#endif
}
}
}
}
+2 -1
View File
@@ -38,6 +38,7 @@ using Raindrop.Netcom;
using Raindrop.Media;
using Raindrop.UI.Notification;
using OpenMetaverse;
using Raindrop.Disk;
using Raindrop.ServiceLocator;
using UnityEngine;
using Logger = OpenMetaverse.Logger;
@@ -268,7 +269,7 @@ namespace Raindrop
public RaindropInstance(GridClient client0)
{
_appDataDir = Application.persistentDataPath;
_appDataDir = DirectoryHelpers.GetCacheDir(); //Application.persistentDataPath;
_streamingAssetsDir = Application.streamingAssetsPath;
InitializeLoggingAndConfig();
@@ -0,0 +1,21 @@
using Raindrop.Services.Bootstrap;
using UnityEngine;
namespace Raindrop.Unity
{
public class SceneBootstrapperGenerator
{
public static GameObject mtd;
public static void Init()
{
mtd = GameObject.CreatePrimitive(PrimitiveType.Cube);
mtd.AddComponent<RaindropBootstrapper>();
}
public static void AddMainThreadDispatcher()
{
mtd.AddComponent<UnityMainThreadDispatcher>();
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6d694132a38d459ba7e1da4f79021833
timeCreated: 1642647269
+9 -8
View File
@@ -67,9 +67,11 @@ namespace Raindrop.Tests.ImagingTests
byte[] thebytes = BetterStreamingAssets.ReadAllBytes(_inputImageSubPath);
Assert.True(thebytes.Length > 0);
var texture = T2D.LoadT2DWithoutMipMaps(thebytes);
Texture2D texture
= new Texture2D(1, 1, TextureFormat.ARGB32, false);
T2D.LoadT2DWithoutMipMaps(thebytes, texture);
Assert.True(texture.height > 5); //todo so arbitrary
@@ -98,10 +100,9 @@ namespace Raindrop.Tests.ImagingTests
}
// fixme: after implementing on-sd/on-device caching preferences
[Test]
[UnityPlatform (RuntimePlatform.Android)]
public void printImportantPaths_Android()
public void printImportantPaths_Platforms()
{
//get from SA
// var j2p_bytes = BetterStreamingAssets.ReadAllBytes(_largeImageRelativePath);
@@ -121,10 +122,10 @@ namespace Raindrop.Tests.ImagingTests
// should be /storage/emulated/0/Android/data/com.UnityTestRunner.UnityTestRunner/files/Pictures/
Debug.Log("Application.sataPath" + Application.dataPath);
Debug.Log("GetAndroidExternalFilesDir internal"+ Disk.DirectoryHelpers.GetAndroidExternalFilesDir(true));
// Debug.Log("GetAndroidExternalFilesDir internal"+ Disk.DirectoryHelpers.GetAndroidExternalFilesDir(true));
// should be /storage/6106-8710/Android/data/com.UnityTestRunner.UnityTestRunner/files
Debug.Log("GetAndroidExternalFilesDir prefersdcard"+ Disk.DirectoryHelpers.GetAndroidExternalFilesDir(false));
// Debug.Log("GetAndroidExternalFilesDir prefersdcard"+ Disk.DirectoryHelpers.GetAndroidExternalFilesDir(false));
//should be /storage/emulated/0/Android/data/com.UnityTestRunner.UnityTestRunner/files/Pictures/
}
+1 -1
View File
@@ -214,7 +214,7 @@ namespace Raindrop.Tests
Assert.True(instance.Client.Network.Connected == false, "check API that we are logged out");
LoginPresenterIsAvailable(vm);
TypeUserAndPassIntoLoginPanel();
yield return new WaitForSeconds(2);
yield return new WaitForSeconds(5);
Utils.UIHelpers.ClickButtonByUnityName("LoginBtn");
//assert the backend API; that we are logged in.