1 /* 2 * Copyright 2015 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 // AJAR=$ANDROID_SDK_ROOT/platforms/android-19/android.jar 9 // SRC=platform_tools/android/apps/canvasproof/src/main 10 // javac -classpath $AJAR $SRC/java/org/skia/canvasproof/GaneshPictureRenderer.java 11 // javah -classpath $AJAR:$SRC/java -d $SRC/jni org.skia.canvasproof.GaneshPictureRenderer 12 13 package org.skia.canvasproof; 14 15 import android.app.Activity; 16 import android.graphics.Rect; 17 import android.opengl.GLSurfaceView; 18 import android.util.Log; 19 import javax.microedition.khronos.egl.EGLConfig; 20 import javax.microedition.khronos.opengles.GL10; 21 22 public class GaneshPictureRenderer implements GLSurfaceView.Renderer { 23 private static final String TAG = "GaneshPictureRenderer"; 24 private long picturePtr; 25 private long contextPtr; 26 private float scale; 27 private int width; 28 private int height; 29 private GLSurfaceView view; 30 GaneshPictureRenderer()31 GaneshPictureRenderer() { 32 try { 33 System.loadLibrary("skia_android"); 34 System.loadLibrary("canvasproof"); 35 } catch (java.lang.Error e) { 36 Log.e(TAG, "System.loadLibrary error", e); 37 return; 38 } 39 this.scale = 1; 40 } makeView(Activity activity)41 public GLSurfaceView makeView(Activity activity) { 42 this.view = new GLSurfaceView(activity); 43 this.view.setEGLConfigChooser(8, 8, 8, 8, 0, 8); 44 this.view.setEGLContextClientVersion(2); 45 this.view.setRenderer(this); 46 this.view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 47 return this.view; 48 } cullRect(long picturePtr)49 static public Rect cullRect(long picturePtr) { 50 Rect rect = new Rect(); 51 try { 52 GaneshPictureRenderer.GetCullRect(rect, picturePtr); 53 } catch (UnsatisfiedLinkError e) { 54 Log.e(TAG, "GetCullRect failed", e); 55 } 56 return rect; 57 } setPicture(long picturePtr)58 public void setPicture(long picturePtr) { 59 this.picturePtr = picturePtr; 60 this.view.requestRender(); 61 } setScale(float s)62 public void setScale(float s) { this.scale = s; } 63 releaseResources()64 public void releaseResources() { 65 if (this.contextPtr != 0) { 66 try { 67 GaneshPictureRenderer.CleanUp(this.contextPtr); 68 } catch (UnsatisfiedLinkError e) { 69 Log.e(TAG, "CleanUp failed", e); 70 } 71 } 72 this.contextPtr = 0; 73 } 74 createContext()75 private void createContext() { 76 try { 77 this.contextPtr = GaneshPictureRenderer.Ctor(); 78 } catch (UnsatisfiedLinkError e) { 79 Log.e(TAG, "Ctor failed", e); 80 } 81 } 82 83 @Override onSurfaceCreated(GL10 gl, EGLConfig c)84 public void onSurfaceCreated(GL10 gl, EGLConfig c) { 85 this.releaseResources(); 86 this.createContext(); 87 } 88 @Override onDrawFrame(GL10 gl)89 public void onDrawFrame(GL10 gl) { 90 if (this.contextPtr == 0) { 91 this.createContext(); 92 } 93 if (this.width > 0 && this.height > 0 && 94 this.contextPtr != 0 && this.picturePtr != 0) { 95 try { 96 GaneshPictureRenderer.DrawThisFrame( 97 this.width, this.height, this.scale, 98 this.contextPtr, this.picturePtr); 99 } catch (UnsatisfiedLinkError e) { 100 Log.e(TAG, "DrawThisFrame failed", e); 101 } 102 } 103 } 104 @Override onSurfaceChanged(GL10 gl, int w, int h)105 public void onSurfaceChanged(GL10 gl, int w, int h) { 106 this.width = w; 107 this.height = h; 108 } 109 @Override finalize()110 public void finalize() throws Throwable { 111 super.finalize(); 112 this.releaseResources(); 113 } 114 115 // Make the native functions static to simplify JNI interaction. DrawThisFrame(int w, int h, float s, long pr, long pc)116 private static native void DrawThisFrame(int w, int h, float s, long pr, long pc); Ctor()117 private static native long Ctor(); CleanUp(long p)118 private static native void CleanUp(long p); GetCullRect(Rect r, long picture)119 private static native void GetCullRect(Rect r, long picture); 120 } 121