diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2017-12-18 00:16:25 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2017-12-18 00:16:25 +0000 |
| commit | 9615db1e25e3b1ed12a71bff0384072c0c473ae8 (patch) | |
| tree | 53a83a5388468fae3d9fd9664378b035f2d6b668 /unityplugin/Android/Common | |
| parent | One final update to appveyor script (diff) | |
| download | DiligentEngine-9615db1e25e3b1ed12a71bff0384072c0c473ae8.tar.gz DiligentEngine-9615db1e25e3b1ed12a71bff0384072c0c473ae8.zip | |
Added gradle build for Unity plugin; updated submodules
Diffstat (limited to 'unityplugin/Android/Common')
3 files changed, 570 insertions, 0 deletions
diff --git a/unityplugin/Android/Common/Java/UnityEmulator/UnityEmulatorApplication.java b/unityplugin/Android/Common/Java/UnityEmulator/UnityEmulatorApplication.java new file mode 100644 index 0000000..b393e51 --- /dev/null +++ b/unityplugin/Android/Common/Java/UnityEmulator/UnityEmulatorApplication.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.DiligentGraphics.UnityEmulator; + +import javax.microedition.khronos.opengles.GL10; + +import android.app.Application; +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.BitmapFactory; +import android.graphics.Matrix; +import android.opengl.GLUtils; +import android.util.Log; +import android.widget.Toast; + +public class UnityEmulatorApplication extends Application { + public void onCreate(){ + Log.w("native-activity", "onCreate"); + + final PackageManager pm = getApplicationContext().getPackageManager(); + ApplicationInfo ai; + try { + ai = pm.getApplicationInfo( this.getPackageName(), 0); + } catch (final NameNotFoundException e) { + ai = null; + } + final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)"); + Toast.makeText(this, applicationName, Toast.LENGTH_SHORT).show(); + } +} diff --git a/unityplugin/Android/Common/Java/UnityEmulator/UnityEmulatorNativeActivity.java b/unityplugin/Android/Common/Java/UnityEmulator/UnityEmulatorNativeActivity.java new file mode 100644 index 0000000..f49a282 --- /dev/null +++ b/unityplugin/Android/Common/Java/UnityEmulator/UnityEmulatorNativeActivity.java @@ -0,0 +1,164 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.DiligentGraphics.UnityEmulator; + +import android.app.NativeActivity; +import android.os.Bundle; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup.MarginLayoutParams; +import android.view.WindowManager.LayoutParams; +import android.widget.LinearLayout; +import android.widget.PopupWindow; +import android.widget.TextView; +import android.util.Log; + +public class UnityEmulatorNativeActivity extends NativeActivity { + + static + { + try{ + System.loadLibrary("GraphicsEngineOpenGL"); + Log.i("native-activity", "Loaded GraphicsEngineOpenGL library\n"); + } catch (UnsatisfiedLinkError e) { + Log.e("native-activity", "Failed to load GraphicsEngineOpenGL library\n" + e); + } + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + String pluginName = getString(R.string.plugin_name); + try{ + System.loadLibrary(pluginName); + Log.i("native-activity", "Loaded " + pluginName + " plugin\n"); + } catch (UnsatisfiedLinkError e) { + Log.e("native-activity", "Failed to load " + pluginName + " plugin\n" + e); + } + + //Hide toolbar + int SDK_INT = android.os.Build.VERSION.SDK_INT; + if(SDK_INT >= 19) + { + setImmersiveSticky(); + + View decorView = getWindow().getDecorView(); + decorView.setOnSystemUiVisibilityChangeListener + (new View.OnSystemUiVisibilityChangeListener() { + @Override + public void onSystemUiVisibilityChange(int visibility) { + setImmersiveSticky(); + } + }); + } + } + + protected void onResume() { + super.onResume(); + + //Hide toolbar + int SDK_INT = android.os.Build.VERSION.SDK_INT; + if(SDK_INT >= 11 && SDK_INT < 14) + { + getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN); + } + else if(SDK_INT >= 14 && SDK_INT < 19) + { + getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LOW_PROFILE); + } + else if(SDK_INT >= 19) + { + setImmersiveSticky(); + } + + } + // Our popup window, you will call it from your C/C++ code later + + void setImmersiveSticky() { + View decorView = getWindow().getDecorView(); + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN + | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); + } + + UnityEmulatorNativeActivity _activity; + PopupWindow _popupWindow; + TextView _label; + + public void showUI() + { + if( _popupWindow != null ) + return; + + _activity = this; + + this.runOnUiThread(new Runnable() { + @Override + public void run() { + LayoutInflater layoutInflater + = (LayoutInflater)getBaseContext() + .getSystemService(LAYOUT_INFLATER_SERVICE); + View popupView = layoutInflater.inflate(R.layout.widgets, null); + _popupWindow = new PopupWindow( + popupView, + LayoutParams.WRAP_CONTENT, + LayoutParams.WRAP_CONTENT); + + LinearLayout mainLayout = new LinearLayout(_activity); + MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); + params.setMargins(0, 0, 0, 0); + _activity.setContentView(mainLayout, params); + + // Show our UI over NativeActivity window + _popupWindow.showAtLocation(mainLayout, Gravity.TOP | Gravity.LEFT, 10, 10); + _popupWindow.update(); + + _label = (TextView)popupView.findViewById(R.id.textViewFPS); + + }}); + } + + protected void onPause() + { + super.onPause(); + if (_popupWindow != null) { + _popupWindow.dismiss(); + _popupWindow = null; + } + } + + public void updateFPS(final float fFPS) + { + if( _label == null ) + return; + + _activity = this; + this.runOnUiThread(new Runnable() { + @Override + public void run() { + _label.setText(String.format("%2.2f FPS", fFPS)); + + }}); + } +} + + diff --git a/unityplugin/Android/Common/Java/helper/NDKHelper.java b/unityplugin/Android/Common/Java/helper/NDKHelper.java new file mode 100644 index 0000000..b7f3a06 --- /dev/null +++ b/unityplugin/Android/Common/Java/helper/NDKHelper.java @@ -0,0 +1,359 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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; + +@TargetApi(Build.VERSION_CODES.GINGERBREAD) +public class NDKHelper { + + 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) { + int pot = 1; + while (pot < i) + pot <<= 1; + return pot; + } + + private Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, + float newHeight) { + if (bitmapToScale == null) + return null; + // get the original width and height + int width = bitmapToScale.getWidth(); + int height = bitmapToScale.getHeight(); + // create a matrix for the manipulation + Matrix matrix = new Matrix(); + + // resize the bit map + 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); + } + + public class TextureInformation { + boolean ret; + boolean alphaChannel; + int originalWidth; + int originalHeight; + Object image; + } + + public Object loadTexture(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)); + } + // Matrix matrix = new Matrix(); + // // resize the bit map + // matrix.postScale(-1F, 1F); + // + // // recreate the new Bitmap and set it back + // bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), + // bitmap.getHeight(), matrix, true); + + } 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); + + return info; + } + + public Object loadCubemapTexture(String path, int face, int miplevel, boolean sRGB) { + 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) { + 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) { + // Scale it + bitmap = scaleBitmap(bitmap, width, height); + } + } + + } catch (Exception e) { + Log.w("NDKHelper", "Coundn't load a file:" + path); + } + + return bitmap; + } + + public int getBitmapWidth(Bitmap bmp) { + return bmp.getWidth(); + } + + public int getBitmapHeight(Bitmap bmp) { + return bmp.getHeight(); + } + + 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) { + bmp.recycle(); + } + + 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) { + return ai.nativeLibraryDir; + } + return "/system/lib/"; + } + + 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) activity + .getSystemService(Context.AUDIO_SERVICE); + String framesPerBuffer = am + .getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER); + return Integer.parseInt(framesPerBuffer); + } else { + return 0; + } + } + + 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 |
