diff options
Diffstat (limited to 'platform_tools/android/apps/canvasproof/src/main/java/org/skia')
4 files changed, 455 insertions, 0 deletions
diff --git a/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/CanvasProofActivity.java b/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/CanvasProofActivity.java new file mode 100644 index 0000000000..9363585bbf --- /dev/null +++ b/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/CanvasProofActivity.java @@ -0,0 +1,205 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +package org.skia.canvasproof; + +import android.app.Activity; +import android.content.res.AssetManager; +import android.graphics.Picture; +import android.opengl.GLSurfaceView; +import android.os.Bundle; +import android.util.Log; +import android.view.Gravity; +import android.view.MotionEvent; +import android.view.View; +import android.widget.LinearLayout.LayoutParams; +import android.widget.LinearLayout; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +public class CanvasProofActivity extends Activity { + private static final String TAG = "CanvasProofActivity"; + private GaneshPictureRenderer ganeshPictureRenderer; + private HwuiPictureView hwuiPictureView; + private GLSurfaceView ganeshPictureView; + private LinearLayout splitPaneView; + private View currentView; + private float x, y; + private int resourcesIndex; + private class PictureAsset { + public String path; + public long ptr; + public Picture picture; + }; + private PictureAsset[] assets; + + @SuppressWarnings("deprecation") // purposely using this + private static Picture ReadPicture(InputStream inputStream) + throws IOException { + Picture p = null; + try { + p = Picture.createFromStream(inputStream); + } catch (java.lang.Exception e) { + Log.e(TAG, "Exception in Picture.createFromStream", e); + } + inputStream.close(); + return p; + } + + private void getAssetPaths() { + String directory = "skps"; + AssetManager mgr = this.getAssets(); + assert (mgr != null); + String[] resources; + try { + resources = mgr.list(directory); + } catch (IOException e) { + Log.e(TAG, "IOException in getAssetPaths", e); + return; + } + if (resources == null || resources.length == 0) { + Log.e(TAG, "SKP assets should be packaged in " + + ".../apps/canvasproof/src/main/assets/skps/" + + ", but none were found."); + return; + } + CreateSkiaPicture.init(); + this.assets = new PictureAsset[resources.length]; + for (int i = 0; i < resources.length; ++i) { + String path = directory + File.separator + resources[i]; + this.assets[i] = new PictureAsset(); + this.assets[i].path = path; + try { + this.assets[i].ptr = CreateSkiaPicture.create(mgr.open(path)); + if (0 == this.assets[i].ptr) { + Log.e(TAG, "CreateSkiaPicture.create returned 0 " + path); + } + Picture p = CanvasProofActivity.ReadPicture(mgr.open(path)); + if (null == p) { + Log.e(TAG, "CanvasProofActivity.ReadPicture.create " + + "returned null " + path); + } else if (0 == p.getHeight() || 0 == p.getWidth()) { + Log.e(TAG, "CanvasProofActivity.ReadPicture.create " + + "empty picture" + path); + p = null; + } + this.assets[i].picture = p; + } catch (IOException e) { + Log.e(TAG, "IOException in getAssetPaths " + path + e); + return; + } + } + } + + private void nextView() { + if (this.currentView == this.hwuiPictureView) { + this.currentView = this.ganeshPictureView; + this.ganeshPictureView.setRenderMode( + GLSurfaceView.RENDERMODE_CONTINUOUSLY); + } else if (this.currentView == null || + this.currentView == this.ganeshPictureView) { + this.setContentView(new View(this)); + LayoutParams layoutParams = + new LayoutParams( + LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.5f); + this.ganeshPictureView.setLayoutParams(layoutParams); + this.ganeshPictureView.setRenderMode( + GLSurfaceView.RENDERMODE_WHEN_DIRTY); + this.splitPaneView.addView(this.ganeshPictureView); + this.hwuiPictureView.setLayoutParams(layoutParams); + this.splitPaneView.addView(this.hwuiPictureView); + this.currentView = this.splitPaneView; + this.hwuiPictureView.fullTime = false; + } else if (this.currentView == this.splitPaneView) { + this.splitPaneView.removeAllViews(); + this.currentView = this.hwuiPictureView; + this.hwuiPictureView.fullTime = true; + } else { + Log.e(TAG, "unexpected value"); + this.setContentView(null); + return; + } + this.setContentView(this.currentView); + this.currentView.invalidate(); + } + + private void nextPicture(int d) { + if (this.assets == null) { + Log.w(TAG, "this.assets == null"); + return; + } + assert (this.assets.length > 0); + resourcesIndex = (resourcesIndex + d) % this.assets.length; + while (resourcesIndex < 0) { + resourcesIndex += this.assets.length; + } + while (resourcesIndex >= this.assets.length) { + resourcesIndex -= this.assets.length; + } + this.ganeshPictureRenderer.setPicture(assets[resourcesIndex].ptr); + this.hwuiPictureView.setPicture(assets[resourcesIndex].picture); + this.currentView.invalidate(); + } + + @Override + protected void onStop() { + this.ganeshPictureRenderer.releaseResources(); + super.onStop(); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + this.getAssetPaths(); + this.ganeshPictureRenderer = new GaneshPictureRenderer(); + this.hwuiPictureView = new HwuiPictureView(this); + + this.ganeshPictureRenderer.setScale(2.0f); + this.hwuiPictureView.setScale(2.0f); + this.ganeshPictureView = ganeshPictureRenderer.makeView(this); + this.splitPaneView = new LinearLayout(this); + this.splitPaneView.setOrientation(LinearLayout.VERTICAL); + this.splitPaneView.setGravity(Gravity.FILL); + + LayoutParams layoutParams = + new LayoutParams( + LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.5f); + this.ganeshPictureView.setLayoutParams(layoutParams); + this.hwuiPictureView.setLayoutParams(layoutParams); + + this.nextView(); + this.nextPicture(0); + } + + // TODO: replace this funtion with onTouchEvent(). + // @Override public boolean onTouchEvent(MotionEvent event)... + @Override + public boolean dispatchTouchEvent (MotionEvent event) { + switch(event.getAction()) { + case MotionEvent.ACTION_DOWN: + this.x = event.getX(); + this.y = event.getY(); + break; + case MotionEvent.ACTION_UP: + float dx = event.getX() - this.x; + float dy = event.getY() - this.y; + float dx2 = dx * dx; + float dy2 = dy * dy; + if (dx2 + dy2 > 22500.0) { + if (dy2 < dx2) { + this.nextPicture(dx > 0 ? 1 : -1); + } else if (dy > 0) { + this.nextView(); + } + } + break; + } + return super.onTouchEvent(event); + } +} diff --git a/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/CreateSkiaPicture.java b/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/CreateSkiaPicture.java new file mode 100644 index 0000000000..61aa14a7b7 --- /dev/null +++ b/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/CreateSkiaPicture.java @@ -0,0 +1,58 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +/* +AJAR=$ANDROID_SDK_ROOT/platforms/android-19/android.jar +CLASS=CreateSkiaPicture +SRC=platform_tools/android/apps/canvasproof/src/main +javac -classpath $AJAR $SRC/java/org/skia/canvasproof/$CLASS.java +javah -classpath $AJAR:$SRC/java -d $SRC/jni org.skia.canvasproof.$CLASS +*/ + +package org.skia.canvasproof; + +import android.util.Log; +import java.io.IOException; +import java.io.InputStream; +import java.lang.UnsatisfiedLinkError; + +public class CreateSkiaPicture { + private static final String TAG = "CreateSkiaPicture"; + + public static void init() { + try { + System.loadLibrary("skia_android"); + System.loadLibrary("canvasproof"); + } catch (java.lang.Error e) { + Log.v(TAG, "System.loadLibrary error", e); + } + } + + public static long create(InputStream inputStream) throws IOException { + byte[] buffer = new byte[16 * (1 << 10)]; // 16 KByte + long p = 0; + try { + p = CreateSkiaPicture.createImpl(inputStream, buffer); + } catch (UnsatisfiedLinkError e) { + Log.e(TAG, "UnsatisfiedLinkError createImpl"); + } + inputStream.close(); + return p; + } + + public static void delete(long ptr) { + try { + if (ptr != 0) { + CreateSkiaPicture.deleteImpl(ptr); + } + } catch (UnsatisfiedLinkError e) { + Log.e(TAG, "UnsatisfiedLinkError deleteImpl"); + } + + } + private static native void deleteImpl(long ptr); + private static native long createImpl(InputStream s, byte[] b); +} diff --git a/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/GaneshPictureRenderer.java b/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/GaneshPictureRenderer.java new file mode 100644 index 0000000000..01c6dc3f25 --- /dev/null +++ b/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/GaneshPictureRenderer.java @@ -0,0 +1,120 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +// AJAR=$ANDROID_SDK_ROOT/platforms/android-19/android.jar +// SRC=platform_tools/android/apps/canvasproof/src/main +// javac -classpath $AJAR $SRC/java/org/skia/canvasproof/GaneshPictureRenderer.java +// javah -classpath $AJAR:$SRC/java -d $SRC/jni org.skia.canvasproof.GaneshPictureRenderer + +package org.skia.canvasproof; + +import android.app.Activity; +import android.graphics.Rect; +import android.opengl.GLSurfaceView; +import android.util.Log; +import javax.microedition.khronos.egl.EGLConfig; +import javax.microedition.khronos.opengles.GL10; + +public class GaneshPictureRenderer implements GLSurfaceView.Renderer { + private static final String TAG = "GaneshPictureRenderer"; + private long picturePtr; + private long contextPtr; + private float scale; + private int width; + private int height; + private GLSurfaceView view; + + GaneshPictureRenderer() { + try { + System.loadLibrary("skia_android"); + System.loadLibrary("canvasproof"); + } catch (java.lang.Error e) { + Log.e(TAG, "System.loadLibrary error", e); + return; + } + this.scale = 1; + } + public GLSurfaceView makeView(Activity activity) { + this.view = new GLSurfaceView(activity); + this.view.setEGLConfigChooser(8, 8, 8, 8, 0, 8); + this.view.setEGLContextClientVersion(2); + this.view.setRenderer(this); + this.view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); + return this.view; + } + static public Rect cullRect(long picturePtr) { + Rect rect = new Rect(); + try { + GaneshPictureRenderer.GetCullRect(rect, picturePtr); + } catch (UnsatisfiedLinkError e) { + Log.e(TAG, "GetCullRect failed", e); + } + return rect; + } + public void setPicture(long picturePtr) { + this.picturePtr = picturePtr; + this.view.requestRender(); + } + public void setScale(float s) { this.scale = s; } + + public void releaseResources() { + if (this.contextPtr != 0) { + try { + GaneshPictureRenderer.CleanUp(this.contextPtr); + } catch (UnsatisfiedLinkError e) { + Log.e(TAG, "CleanUp failed", e); + } + } + this.contextPtr = 0; + } + + private void createContext() { + try { + this.contextPtr = GaneshPictureRenderer.Ctor(); + } catch (UnsatisfiedLinkError e) { + Log.e(TAG, "Ctor failed", e); + } + } + + @Override + public void onSurfaceCreated(GL10 gl, EGLConfig c) { + this.releaseResources(); + this.createContext(); + } + @Override + public void onDrawFrame(GL10 gl) { + if (this.contextPtr == 0) { + this.createContext(); + } + if (this.width > 0 && this.height > 0 && + this.contextPtr != 0 && this.picturePtr != 0) { + try { + GaneshPictureRenderer.DrawThisFrame( + this.width, this.height, this.scale, + this.contextPtr, this.picturePtr); + } catch (UnsatisfiedLinkError e) { + Log.e(TAG, "DrawThisFrame failed", e); + } + } + } + @Override + public void onSurfaceChanged(GL10 gl, int w, int h) { + this.width = w; + this.height = h; + } + @Override + public void finalize() throws Throwable { + super.finalize(); + this.releaseResources(); + } + + // Make the native functions static to simplify JNI interaction. + private static native void DrawThisFrame(int w, int h, float s, long pr, long pc); + private static native long Ctor(); + private static native void CleanUp(long p); + private static native void GetCullRect(Rect r, long picture); +} diff --git a/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/HwuiPictureView.java b/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/HwuiPictureView.java new file mode 100644 index 0000000000..872089c9d3 --- /dev/null +++ b/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/HwuiPictureView.java @@ -0,0 +1,72 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +package org.skia.canvasproof; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Picture; +import android.util.Log; +import android.view.View; +import java.io.IOException; +import java.io.InputStream; + +public class HwuiPictureView extends View { + private static final String TAG = "HwuiPictureView"; + private Picture picture; + private float scale; + private Picture defaultPicture; + + public boolean fullTime; + + HwuiPictureView(Context context) { + super(context); + this.scale = 1.0f; + } + public void setScale(float s) { + this.scale = s; + } + public void setPicture(Picture p) { + this.picture = p; + this.invalidate(); + } + + @Override + protected void onDraw(Canvas canvas) { + if (this.picture != null) { + canvas.save(); + canvas.scale(scale, scale); + HwuiPictureView.draw(canvas, this.picture); + canvas.restore(); + if (fullTime) { + this.invalidate(); + } + } + } + + static private void draw(Canvas canvas, Picture p) { + if (android.os.Build.VERSION.SDK_INT > 22) { + try { + canvas.drawPicture(p); + return; + } catch (java.lang.Exception e) { + Log.e(TAG, "Exception while drawing picture in Hwui"); + } + } + if (p.getWidth() > 0 && p.getHeight() > 0) { + // Fallback to software rendering. + Bitmap bm = Bitmap.createBitmap(p.getWidth(), p.getHeight(), + Bitmap.Config.ARGB_8888); + (new Canvas(bm)).drawPicture(p); + canvas.drawBitmap(bm, 0.0f, 0.0f, null); + bm.recycle(); + } + } +} |