From c7677936ece8e80f3456ced6170f49c1f8af59b2 Mon Sep 17 00:00:00 2001 From: ctiao Date: Mon, 15 Jun 2015 12:31:46 +0800 Subject: [PATCH] add NativeBitmapFactoryTest.java for testing libndkbitmap --- DanmakuFlameMaster/build.gradle | 10 +- .../tv/cjump/jni/NativeBitmapFactoryTest.java | 161 ++++++++++++++++++ 2 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 DanmakuFlameMaster/src/androidTest/java/tv/cjump/jni/NativeBitmapFactoryTest.java diff --git a/DanmakuFlameMaster/build.gradle b/DanmakuFlameMaster/build.gradle index 6ab00803..ac8c4da8 100644 --- a/DanmakuFlameMaster/build.gradle +++ b/DanmakuFlameMaster/build.gradle @@ -16,6 +16,8 @@ apply plugin: 'com.android.library' +def SourcePath = 'src/main/' + android { compileSdkVersion 22 buildToolsVersion "22.0.1" @@ -26,10 +28,14 @@ android { versionName VERSION_NAME versionCode Integer.parseInt(VERSION_CODE) } - sourceSets { + + sourceSets { main { - jniLibs.srcDirs = ['src/main/libs'] + manifest.srcFile "${SourcePath}AndroidManifest.xml" + java.srcDirs = ["${SourcePath}java"] + jniLibs.srcDirs = ["${SourcePath}libs"] } + androidTest.setRoot("${SourcePath}../androidTest") } } diff --git a/DanmakuFlameMaster/src/androidTest/java/tv/cjump/jni/NativeBitmapFactoryTest.java b/DanmakuFlameMaster/src/androidTest/java/tv/cjump/jni/NativeBitmapFactoryTest.java new file mode 100644 index 00000000..7fb510e6 --- /dev/null +++ b/DanmakuFlameMaster/src/androidTest/java/tv/cjump/jni/NativeBitmapFactoryTest.java @@ -0,0 +1,161 @@ +package tv.cjump.jni; + +import android.app.ActivityManager; +import android.app.Instrumentation; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.os.Debug; +import android.os.SystemClock; +import android.test.InstrumentationTestCase; +import android.util.Log; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import java.util.ArrayList; +import java.util.Locale; + +/** + * Created by ch on 15-6-12. + */ +public class NativeBitmapFactoryTest extends InstrumentationTestCase { + + private static final int DEFAULT_MESSAGE_SIZE = 1024; + private static final int BYTES_IN_MEGABYTE = 1024 * 1024; + + private static final int BITMAP_WIDTH = 200; + private static final int BITMAP_HEIGHT = 200; + private static final String TAG = NativeBitmapFactoryTest.class.getSimpleName(); + + public void testLoadLibs() { + NativeBitmapFactory.loadLibs(); + boolean isInNativeAlloc = NativeBitmapFactory.isInNativeAlloc(); + Assert.assertTrue("NativeBitmapFactory is not supported on your OS", isInNativeAlloc); + } + + public void testNativeBitmap() { + Bitmap bitmap = NativeBitmapFactory.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ARGB_8888); + accessBitmap(bitmap); + bitmap.recycle(); + gcAndWait(); + } + + public void testDalvikBitmap() { + Bitmap bitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ARGB_8888); + accessBitmap(bitmap); + bitmap.recycle(); + gcAndWait(); + } + + public void testNativeBitmaps() { + StringBuilder sb = new StringBuilder(DEFAULT_MESSAGE_SIZE); + appendValue(sb, "\n\n", "===== before create 50 NativeBitmap", "\n\n"); + updateHeapValue(sb); + final String message = sb.toString(); + Log.i(TAG, message); + sb = new StringBuilder(); + ArrayList bitmaps = new ArrayList<>(); + for (int i = 0; i < 150; i++) { + Bitmap bitmap = NativeBitmapFactory.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ARGB_8888); + accessBitmap(bitmap); + bitmaps.add(bitmap); + } + updateHeapValue(sb); + Log.d(TAG, sb.toString()); + for (Bitmap bitmap : bitmaps) { + bitmap.recycle(); + } + gcAndWait(); + } + + public Context getContext(){ + return getInstrumentation().getTargetContext(); + } + + private void updateHeapValue(StringBuilder sb) { + + ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); + ActivityManager activityManager = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); + activityManager.getMemoryInfo(mi); + long availableMegs = mi.availMem; + final Runtime runtime = Runtime.getRuntime(); + final long heapMemory = runtime.totalMemory() - runtime.freeMemory(); + // When changing format of output below, make sure to sync "scripts/test_runner.py" as well. + appendSize(sb, "System availMem: ", availableMegs, "\n"); + appendSize(sb, "Java heap size: ", heapMemory, "\n"); + appendSize(sb, "Native heap size: ", Debug.getNativeHeapSize(), "\n"); + } + + public void testDalvikBitmaps() { + StringBuilder sb = new StringBuilder(DEFAULT_MESSAGE_SIZE); + appendValue(sb, "\n\n", "===== before create 50 DalvikBitmap", "\n\n"); + updateHeapValue(sb); + final String message = sb.toString(); + Log.i(TAG, message); + sb = new StringBuilder(); + ArrayList bitmaps = new ArrayList<>(); + for (int i = 0; i < 150; i++) { + Bitmap bitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ARGB_8888); + accessBitmap(bitmap); + bitmaps.add(bitmap); + } + updateHeapValue(sb); + Log.d(TAG, sb.toString()); + for (Bitmap bitmap : bitmaps) { + bitmap.recycle(); + } + gcAndWait(); + } + + public void testReleaseLibs() { + NativeBitmapFactory.releaseLibs(); + } + + + private void accessBitmap(Bitmap bitmap) { + boolean result = (bitmap != null && bitmap.getWidth() == BITMAP_WIDTH && bitmap.getHeight() == BITMAP_HEIGHT); + if (result) { + if (android.os.Build.VERSION.SDK_INT >= 17 && !bitmap.isPremultiplied()) { + bitmap.setPremultiplied(true); + } + Canvas canvas = new Canvas(bitmap); + Paint paint = new Paint(); + paint.setColor(Color.RED); + paint.setTextSize(20f); + canvas.drawRect(0f, 0f, (float) bitmap.getWidth(), (float) bitmap.getHeight(), + paint); + canvas.drawText("TestLib", 0, 0, paint); + } + } + + private void gcAndWait() { + System.gc(); + SystemClock.sleep(10000); +// try { +// Debug.dumpHprofData("/sdcard/nbf_test_dump.hprof"); +// } catch (IOException e) { +// e.printStackTrace(); +// } + } + + private static void appendSize(StringBuilder sb, String prefix, long bytes, String suffix) { + String value = String.format(Locale.getDefault(), "%.2f", (float) bytes / BYTES_IN_MEGABYTE); + appendValue(sb, prefix, value + " MB", suffix); + } + + private static void appendTime(StringBuilder sb, String prefix, long timeMs, String suffix) { + appendValue(sb, prefix, timeMs + " ms", suffix); + } + + private static void appendNumber(StringBuilder sb, String prefix, long number, String suffix) { + appendValue(sb, prefix, number + "", suffix); + } + + private static void appendValue(StringBuilder sb, String prefix, String value, String suffix) { + sb.append(prefix).append(value).append(suffix); + } + +} \ No newline at end of file