Chromium Code Reviews| Index: platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/CanvasProofActivity.java |
| 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 0000000000000000000000000000000000000000..522414ec3e7db4867377448ae3e54cd86f870421 |
| --- /dev/null |
| +++ b/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/CanvasProofActivity.java |
| @@ -0,0 +1,187 @@ |
| +/* |
| + * 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.content.res.Resources; |
| +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; |
| +import javax.microedition.khronos.egl.EGLConfig; |
| +import javax.microedition.khronos.opengles.GL10; |
| + |
| +public class CanvasProofActivity extends Activity { |
| + private class PictureAsset { |
| + public String path; |
| + public long ptr; |
| + public Picture picture; |
| + }; |
| + private static final String TAG = "CanvasProofActivity"; |
| + private PictureAsset[] assets; |
| + |
| + @SuppressWarnings("deprecation") // purposely using this |
|
tomhudson
2015/08/22 19:15:52
Nit: given that we're using a deprecated feature,
|
| + 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); |
|
tomhudson
2015/08/22 19:15:52
Can you more explicitly list what failed here? Not
hal.canary
2015/08/31 21:16:55
Done.
|
| + return; |
| + } |
| + if (resources == null) { |
| + Log.e(TAG, "this.getAssets().getAssetPaths returned null"); |
|
tomhudson
2015/08/22 19:15:52
How about "found no resources in path <directory>"
hal.canary
2015/08/31 21:16:55
Done.
|
| + return; |
| + } |
| + if (resources != null && resources.length > 0) { |
| + CreateSkiaPicture.init(); |
| + this.assets = new PictureAsset[resources.length]; |
| + for (int i = 0; i < resources.length; ++i) { |
| + String path = directory + File.separator + resources[i]; |
| + Log.v(TAG, "loading " + path); |
|
tomhudson
2015/08/22 19:15:52
If we don't need these logs any more, please strip
hal.canary
2015/08/31 21:16:55
Done. I've removed all Log.v();
|
| + this.assets[i] = new PictureAsset(); |
| + this.assets[i].path = path; |
| + try { |
| + Log.v(TAG, "calling CreateSkiaPicture.create..."); |
| + this.assets[i].ptr = CreateSkiaPicture.create(mgr.open(path)); |
| + if (0 == this.assets[i].ptr) { |
| + Log.e(TAG, "CreateSkiaPicture.create returned 0 " + path); |
| + } |
| + Log.v(TAG, "calling CanvasProofActivity.ReadPicture..."); |
| + 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 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; |
| + } |
| + //String path = assets[resourcesIndex].path; |
| + this.ganeshPictureRenderer.setPicture(assets[resourcesIndex].ptr); |
| + this.hwuiPictureView.setPicture(assets[resourcesIndex].picture); |
| + this.hwuiPictureView.invalidate(); |
| + |
| + this.ganeshPictureView.requestRender(); |
| + this.ganeshPictureView.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.ganeshPictureView = ganeshPictureRenderer.makeView(this); |
| + |
| + this.ganeshPictureRenderer.setScale(2.0f); |
| + this.hwuiPictureView.setScale(2.0f); |
| + |
| + this.nextPicture(0); |
| + LinearLayout layout = new LinearLayout(this); |
| + layout.setOrientation(LinearLayout.VERTICAL); |
| + layout.setGravity(Gravity.FILL); |
| + LayoutParams layoutParams = |
| + new LayoutParams( |
| + LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.5f); |
| + for (View v : new View[]{ |
| + this.ganeshPictureView, |
| + this.hwuiPictureView |
|
tomhudson
2015/08/22 19:15:52
If HwuiPictureView extends View, why have another
hal.canary
2015/08/31 21:16:55
Done.
|
| + }) { |
| + v.setLayoutParams(layoutParams); |
| + layout.addView(v); |
| + } |
| + this.setContentView(layout); |
| + } |
| + @Override |
| + public boolean dispatchTouchEvent (MotionEvent event) { |
| + switch(event.getAction()) { |
| + case MotionEvent.ACTION_DOWN: |
| + x = event.getX(); |
| + y = event.getY(); |
| + break; |
| + case MotionEvent.ACTION_UP: |
| + float dx = event.getX() - x; |
| + float dy = event.getY() - y; |
| + float dx2 = dx * dx; |
| + float dy2 = dy * dy; |
| + if (dx2 + dy2 > 22500.0) { |
| + if (dy2 < dx2) { |
| + if (dx > 0) { |
| + this.nextPicture(1); |
| + } else { |
| + this.nextPicture(-1); |
| + } |
| + } |
| + } |
| + break; |
| + } |
| + return super.onTouchEvent(event); |
| + } |
| + |
| + private GaneshPictureRenderer ganeshPictureRenderer; |
|
djsollen
2015/08/24 12:42:56
java style is to put the member variables at the t
hal.canary
2015/08/31 21:16:55
Done.
|
| + private GLSurfaceView ganeshPictureView; |
| + private HwuiPictureView hwuiPictureView; |
| + private float x, y; |
| + private int resourcesIndex; |
| +} |