OLD | NEW |
---|---|
(Empty) | |
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.j ava | |
11 // javah -classpath $AJAR:$SRC/java -d $SRC/jni org.skia.canvasproof.GaneshPictu reRenderer | |
12 | |
13 package org.skia.canvasproof; | |
14 | |
15 import android.graphics.Rect; | |
16 import android.opengl.GLSurfaceView; | |
17 import android.util.Log; | |
18 import java.io.InputStream; | |
19 import java.io.IOException; | |
20 import java.lang.Throwable; | |
21 import java.lang.UnsatisfiedLinkError; | |
22 import javax.microedition.khronos.egl.EGLConfig; | |
23 import javax.microedition.khronos.opengles.GL10; | |
24 | |
25 public class GaneshPictureRenderer implements GLSurfaceView.Renderer { | |
26 private static final String TAG = "GaneshPictureRenderer"; | |
27 GaneshPictureRenderer() { | |
28 try { | |
29 System.loadLibrary("skia_android"); | |
30 System.loadLibrary("canvasproof"); | |
31 } catch (java.lang.Error e) { | |
32 Log.e(TAG, "System.loadLibrary error", e); | |
33 return; | |
34 } | |
35 this.scale = 1; | |
36 } | |
37 public android.opengl.GLSurfaceView makeView( | |
tomhudson
2015/08/24 20:10:25
Nit: You've imported GLSurfaceView; do you really
hal.canary
2015/08/31 21:16:56
Done.
For consistency, I'm prefer import statemen
| |
38 android.app.Activity activity) { | |
39 this.view = new GLSurfaceView(activity); | |
40 this.view.setEGLConfigChooser(8, 8, 8, 8, 0, 8); | |
41 this.view.setEGLContextClientVersion(2); | |
42 this.view.setRenderer(this); | |
43 this.view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); | |
44 return this.view; | |
45 } | |
46 static public Rect cullRect(long picturePtr) { | |
47 Rect rect = new Rect(); | |
48 try { | |
49 GaneshPictureRenderer.GetCullRect(rect, picturePtr); | |
50 } catch (UnsatisfiedLinkError e) { | |
51 Log.e(TAG, "GetCullRect failed", e); | |
52 } | |
53 return rect; | |
54 } | |
55 public void setPicture(long picturePtr) { | |
56 this.picturePtr = picturePtr; | |
57 this.view.requestRender(); | |
58 } | |
59 public void setScale(float s) { this.scale = s; } | |
60 | |
61 public void releaseResources() { | |
62 if (this.contextPtr != 0) { | |
63 try { | |
64 GaneshPictureRenderer.CleanUp(this.contextPtr); | |
65 } catch (UnsatisfiedLinkError e) { | |
66 Log.e(TAG, "CleanUp failed", e); | |
67 } | |
68 } | |
69 this.contextPtr = 0; | |
70 } | |
71 | |
72 private void createContext() { | |
73 try { | |
74 this.contextPtr = GaneshPictureRenderer.Ctor(); | |
75 } catch (UnsatisfiedLinkError e) { | |
76 Log.e(TAG, "Ctor failed", e); | |
77 } | |
78 } | |
79 | |
80 @Override | |
81 public void onSurfaceCreated(GL10 gl, EGLConfig c) { | |
82 this.releaseResources(); | |
83 this.createContext(); | |
84 } | |
85 @Override | |
86 public void onDrawFrame(GL10 gl) { | |
87 if (this.contextPtr == 0) { | |
88 this.createContext(); | |
89 } | |
90 if (this.width > 0 && this.height > 0 && | |
91 this.contextPtr != 0 && this.picturePtr != 0) { | |
92 try { | |
93 GaneshPictureRenderer.DrawThisFrame( | |
94 this.width, this.height, this.scale, | |
95 this.contextPtr, this.picturePtr); | |
96 } catch (UnsatisfiedLinkError e) { | |
97 Log.e(TAG, "DrawThisFrame failed", e); | |
98 } | |
99 } | |
100 } | |
101 @Override | |
102 public void onSurfaceChanged(GL10 gl, int w, int h) { | |
103 this.width = w; | |
104 this.height = h; | |
105 } | |
106 @Override | |
107 public void finalize() throws Throwable { | |
108 super.finalize(); | |
109 this.releaseResources(); | |
110 } | |
111 private long picturePtr; | |
112 private long contextPtr; | |
113 private float scale; | |
114 private int width; | |
115 private int height; | |
116 private GLSurfaceView view; | |
117 | |
118 // Make the native functions static to simplify JNI interaction. | |
119 private static native void DrawThisFrame(int w, int h, float s, long pr, lon g pc); | |
120 private static native long Ctor(); | |
121 private static native void CleanUp(long p); | |
122 private static native void GetCullRect(Rect r, long picture); | |
123 } | |
OLD | NEW |