From 26af70588daca8ca16159a49fcd795bafb1cee7d Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Thu, 16 Nov 2017 19:49:58 -0800 Subject: Fixed Android build --- .../UnityEmulator/build/Win32/jni/Android.mk | 3 +- .../UnityEmulator/UnityEmulatorApplication.java | 2 +- .../UnityEmulator/UnityEmulatorNativeActivity.java | 2 +- .../src/Android/Java/helper/NDKHelper.java | 286 ++++++++++++++++----- .../src/Android/UnityGraphicsGLESAndroid_Impl.h | 2 +- 5 files changed, 226 insertions(+), 69 deletions(-) (limited to 'unityplugin/UnityEmulator') diff --git a/unityplugin/UnityEmulator/build/Win32/jni/Android.mk b/unityplugin/UnityEmulator/build/Win32/jni/Android.mk index 45466e4..44ece36 100644 --- a/unityplugin/UnityEmulator/build/Win32/jni/Android.mk +++ b/unityplugin/UnityEmulator/build/Win32/jni/Android.mk @@ -8,7 +8,7 @@ LOCAL_MODULE := UnityEmulator LOCAL_CFLAGS := -std=c++11 -DENGINE_DLL LOCAL_CPP_FEATURES := exceptions -LOCAL_STATIC_LIBRARIES := cpufeatures android_native_app_glue ndk_helper +LOCAL_STATIC_LIBRARIES := cpufeatures android_native_app_glue # Include paths PROJECT_ROOT := $(LOCAL_PATH)/../../.. @@ -27,6 +27,7 @@ LOCAL_C_INCLUDES += $(CORE_ROOT)/Graphics/GraphicsEngine/include LOCAL_C_INCLUDES += $(CORE_ROOT)/Graphics/GraphicsEngineD3DBase/include LOCAL_C_INCLUDES += $(CORE_ROOT)/Graphics/GraphicsEngineOpenGL/interface LOCAL_C_INCLUDES += $(CORE_ROOT)/Graphics/HLSL2GLSLConverterLib/interface +LOCAL_C_INCLUDES += $(CORE_ROOT)/External/Android/ndk_helper/include LOCAL_C_INCLUDES += $(TOOLS_ROOT)/Graphics/GraphicsTools/include # Source files diff --git a/unityplugin/UnityEmulator/src/Android/Java/UnityEmulator/UnityEmulatorApplication.java b/unityplugin/UnityEmulator/src/Android/Java/UnityEmulator/UnityEmulatorApplication.java index 249f291..b393e51 100644 --- a/unityplugin/UnityEmulator/src/Android/Java/UnityEmulator/UnityEmulatorApplication.java +++ b/unityplugin/UnityEmulator/src/Android/Java/UnityEmulator/UnityEmulatorApplication.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.DiligentGrpahics.UnityEmulator; +package com.DiligentGraphics.UnityEmulator; import javax.microedition.khronos.opengles.GL10; diff --git a/unityplugin/UnityEmulator/src/Android/Java/UnityEmulator/UnityEmulatorNativeActivity.java b/unityplugin/UnityEmulator/src/Android/Java/UnityEmulator/UnityEmulatorNativeActivity.java index e30e106..f49a282 100644 --- a/unityplugin/UnityEmulator/src/Android/Java/UnityEmulator/UnityEmulatorNativeActivity.java +++ b/unityplugin/UnityEmulator/src/Android/Java/UnityEmulator/UnityEmulatorNativeActivity.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.DiligentGrpahics.UnityEmulator; +package com.DiligentGraphics.UnityEmulator; import android.app.NativeActivity; import android.os.Bundle; diff --git a/unityplugin/UnityEmulator/src/Android/Java/helper/NDKHelper.java b/unityplugin/UnityEmulator/src/Android/Java/helper/NDKHelper.java index 3385a5d..b7f3a06 100644 --- a/unityplugin/UnityEmulator/src/Android/Java/helper/NDKHelper.java +++ b/unityplugin/UnityEmulator/src/Android/Java/helper/NDKHelper.java @@ -16,46 +16,80 @@ package com.sample.helper; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; +import java.nio.ByteBuffer; import javax.microedition.khronos.opengles.GL10; +import android.R.bool; +import android.opengl.GLES30; + +import android.annotation.TargetApi; +import android.app.Activity; +import android.app.NativeActivity; import android.content.Context; import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; import android.graphics.Bitmap; +import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.media.AudioManager; import android.media.AudioTrack; import android.opengl.GLUtils; +import android.os.Build; import android.util.Log; +import android.view.View; +import android.view.View.MeasureSpec; -public class NDKHelper -{ - private static Context context; +@TargetApi(Build.VERSION_CODES.GINGERBREAD) +public class NDKHelper { - public static void setContext(Context c) - { - Log.i("NDKHelper", "setContext:" + c); - context = c; + public NDKHelper(NativeActivity act) { + activity = act; + } + + public void loadLibrary(String soname) { + if (soname.isEmpty() == false) { + System.loadLibrary(soname); + loadedSO = true; + } + } + + public static Boolean checkSOLoaded() { + if (loadedSO == false) { + Log.e("NDKHelper", + "--------------------------------------------\n" + + ".so has not been loaded. To use JUI helper, please initialize with \n" + + "NDKHelper::Init( ANativeActivity* activity, const char* helper_class_name, const char* native_soname);\n" + + "--------------------------------------------\n"); + return false; + } else + return true; } + private static boolean loadedSO = false; + NativeActivity activity; + + + // // Load Bitmap // Java helper is useful decoding PNG, TIFF etc rather than linking libPng // etc separately // - private int nextPOT(int i) - { + private int nextPOT(int i) { int pot = 1; while (pot < i) pot <<= 1; return pot; } - private Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) - { + private Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, + float newHeight) { if (bitmapToScale == null) return null; // get the original width and height @@ -68,29 +102,34 @@ public class NDKHelper matrix.postScale(newWidth / width, newHeight / height); // recreate the new Bitmap and set it back - return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), - bitmapToScale.getHeight(), matrix, true); + return Bitmap.createBitmap(bitmapToScale, 0, 0, + bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, + true); } - public boolean loadTexture(String path) - { + public class TextureInformation { + boolean ret; + boolean alphaChannel; + int originalWidth; + int originalHeight; + Object image; + } + + public Object loadTexture(String path) { Bitmap bitmap = null; - try - { + TextureInformation info = new TextureInformation(); + try { String str = path; - if (!path.startsWith("/")) - { + if (!path.startsWith("/")) { str = "/" + path; } - File file = new File(context.getExternalFilesDir(null), str); - if (file.canRead()) - { + File file = new File(activity.getExternalFilesDir(null), str); + if (file.canRead()) { bitmap = BitmapFactory.decodeStream(new FileInputStream(file)); - } else - { - bitmap = BitmapFactory.decodeStream(context.getResources().getAssets() - .open(path)); + } else { + bitmap = BitmapFactory.decodeStream(activity.getResources() + .getAssets().open(path)); } // Matrix matrix = new Matrix(); // // resize the bit map @@ -100,104 +139,221 @@ public class NDKHelper // bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), // bitmap.getHeight(), matrix, true); - } catch (Exception e) - { + } catch (Exception e) { Log.w("NDKHelper", "Coundn't load a file:" + path); - return false; + info.ret = false; + return info; } - if (bitmap != null) - { + if (bitmap != null) { GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); } - return true; + info.ret = true; + info.alphaChannel = bitmap.hasAlpha(); + info.originalWidth = getBitmapWidth(bitmap); + info.originalHeight = getBitmapHeight(bitmap); + return info; } - public Bitmap openBitmap(String path, boolean iScalePOT) - { + public Object loadCubemapTexture(String path, int face, int miplevel, boolean sRGB) { Bitmap bitmap = null; - try - { - bitmap = BitmapFactory.decodeStream(context.getResources().getAssets() - .open(path)); - if (iScalePOT) + TextureInformation info = new TextureInformation(); + try { + String str = path; + if (!path.startsWith("/")) { + str = "/" + path; + } + + File file = new File(activity.getExternalFilesDir(null), str); + if (file.canRead()) { + bitmap = BitmapFactory.decodeStream(new FileInputStream(file)); + } else { + bitmap = BitmapFactory.decodeStream(activity.getResources() + .getAssets().open(path)); + } + } catch (Exception e) { + Log.w("NDKHelper", "Coundn't load a file:" + path); + info.ret = false; + return info; + } + + if (bitmap != null) { + if (sRGB) { +// GLUtils.texImage2D(face, miplevel, bitmap, 0); +// GLUtils.texImage2D(face, miplevel, +// GLUtils.getInternalFormat(bitmap), bitmap, 0); +// int i = GLUtils.getInternalFormat(bitmap); +// if( i == GL10.GL_RGBA) +// { +// GLUtils.texImage2D(face, miplevel, +// GLES30.GL_SRGB, bitmap, 0); +// } + //Leave them for now + GLUtils.texImage2D(face, miplevel, bitmap, 0); + } + else + GLUtils.texImage2D(face, miplevel, bitmap, 0); + } + info.ret = true; + info.alphaChannel = bitmap.hasAlpha(); + info.originalWidth = getBitmapWidth(bitmap); + info.originalHeight = getBitmapHeight(bitmap); + + return info; + + } + + public Object loadImage(String path) { + Bitmap bitmap = null; + TextureInformation info = new TextureInformation(); + try { + String str = path; + if (!path.startsWith("/")) { + str = "/" + path; + } + + File file = new File(activity.getExternalFilesDir(null), str); + if (file.canRead()) { + bitmap = BitmapFactory.decodeStream(new FileInputStream(file)); + } else { + bitmap = BitmapFactory.decodeStream(activity.getResources() + .getAssets().open(path)); + } + } catch (Exception e) { + Log.w("NDKHelper", "Coundn't load a file:" + path); + info.ret = false; + return info; + } + + if (bitmap != null) { + GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); + } + info.ret = true; + info.alphaChannel = bitmap.hasAlpha(); + info.originalWidth = getBitmapWidth(bitmap); + info.originalHeight = getBitmapHeight(bitmap); + + int iBytes = bitmap.getWidth() * bitmap.getHeight() * 4; + ByteBuffer buffer = ByteBuffer.allocateDirect(iBytes); + + bitmap.copyPixelsToBuffer(buffer); + info.image = buffer; + return info; + } + + public Bitmap openBitmap(String path, boolean iScalePOT) { + Bitmap bitmap = null; + try { + bitmap = BitmapFactory.decodeStream(activity.getResources() + .getAssets().open(path)); + if (iScalePOT) { int originalWidth = getBitmapWidth(bitmap); int originalHeight = getBitmapHeight(bitmap); int width = nextPOT(originalWidth); int height = nextPOT(originalHeight); - if (originalWidth != width || originalHeight != height) - { + if (originalWidth != width || originalHeight != height) { // Scale it bitmap = scaleBitmap(bitmap, width, height); } } - } catch (Exception e) - { + } catch (Exception e) { Log.w("NDKHelper", "Coundn't load a file:" + path); } return bitmap; } - public int getBitmapWidth(Bitmap bmp) - { + public int getBitmapWidth(Bitmap bmp) { return bmp.getWidth(); } - public int getBitmapHeight(Bitmap bmp) - { + public int getBitmapHeight(Bitmap bmp) { return bmp.getHeight(); } - public void getBitmapPixels(Bitmap bmp, int[] pixels) - { + public void getBitmapPixels(Bitmap bmp, int[] pixels) { int w = bmp.getWidth(); int h = bmp.getHeight(); bmp.getPixels(pixels, 0, w, 0, 0, w, h); } - public void closeBitmap(Bitmap bmp) - { + public void closeBitmap(Bitmap bmp) { bmp.recycle(); } - public static String getNativeLibraryDirectory(Context appContext) - { - ApplicationInfo ai = context.getApplicationInfo(); + public String getNativeLibraryDirectory(Context appContext) { + ApplicationInfo ai = activity.getApplicationInfo(); Log.w("NDKHelper", "ai.nativeLibraryDir:" + ai.nativeLibraryDir); if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 - || (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) - { + || (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { return ai.nativeLibraryDir; } return "/system/lib/"; } - public int getNativeAudioBufferSize() + public String getApplicationName() { + final PackageManager pm = activity.getPackageManager(); + ApplicationInfo ai; + try { + ai = pm.getApplicationInfo(activity.getPackageName(), 0); + } catch (final NameNotFoundException e) { + ai = null; + } + String applicationName = (String) (ai != null ? pm + .getApplicationLabel(ai) : "(unknown)"); + return applicationName; + } + + public String getStringResource(String resourceName) { + int id = activity.getResources().getIdentifier(resourceName, "string", activity.getPackageName()); + String value = id == 0 ? "" : (String)activity.getResources().getText(id); + return value; + } + + // + // Audio related helpers + // + @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) + public int getNativeAudioBufferSize() { int SDK_INT = android.os.Build.VERSION.SDK_INT; - if (SDK_INT >= 17) - { - AudioManager am = (AudioManager) context + if (SDK_INT >= 17) { + AudioManager am = (AudioManager) activity .getSystemService(Context.AUDIO_SERVICE); String framesPerBuffer = am .getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER); return Integer.parseInt(framesPerBuffer); - } else - { + } else { return 0; } } - public int getNativeAudioSampleRate() - { + public int getNativeAudioSampleRate() { return AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM); + } + /* + * Helper to execute function in UIThread + */ + public void runOnUIThread(final long p) { + if (checkSOLoaded()) { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + RunOnUiThreadHandler(p); + } + }); + } + return; } -} + /* + * Native code helper for RunOnUiThread + */ + native public void RunOnUiThreadHandler(long pointer); +} \ No newline at end of file diff --git a/unityplugin/UnityEmulator/src/Android/UnityGraphicsGLESAndroid_Impl.h b/unityplugin/UnityEmulator/src/Android/UnityGraphicsGLESAndroid_Impl.h index 1ea7df1..5f949b0 100644 --- a/unityplugin/UnityEmulator/src/Android/UnityGraphicsGLESAndroid_Impl.h +++ b/unityplugin/UnityEmulator/src/Android/UnityGraphicsGLESAndroid_Impl.h @@ -45,7 +45,7 @@ public: GLenum GetDepthBufferFormat()const { if (depth_size_ == 32) - GL_DEPTH_COMPONENT32F; + return GL_DEPTH_COMPONENT32F; else if (depth_size_ == 24) return GL_DEPTH_COMPONENT24; else if(depth_size_ == 16) -- cgit v1.2.3