• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 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 package com.skia;
9 
10 import javax.microedition.khronos.egl.EGL10;
11 import javax.microedition.khronos.egl.EGLConfig;
12 import javax.microedition.khronos.egl.EGLDisplay;
13 import javax.microedition.khronos.opengles.GL10;
14 
15 import android.content.Context;
16 import android.opengl.EGL14;
17 import android.opengl.GLSurfaceView;
18 import android.os.Build;
19 import android.util.Log;
20 import android.view.MotionEvent;
21 import android.widget.Toast;
22 
23 public class SkiaSampleView extends GLSurfaceView {
24 
25     private final SkiaSampleRenderer mSampleRenderer;
26     private boolean mRequestedOpenGLAPI; // true == use (desktop) OpenGL. false == use OpenGL ES.
27     private int mRequestedMSAASampleCount;
28 
SkiaSampleView(Context ctx, String cmdLineFlags, boolean useOpenGL, int msaaSampleCount)29     public SkiaSampleView(Context ctx, String cmdLineFlags, boolean useOpenGL, int msaaSampleCount) {
30         super(ctx);
31 
32         mSampleRenderer = new SkiaSampleRenderer(this, cmdLineFlags);
33         mRequestedMSAASampleCount = msaaSampleCount;
34 
35         setEGLContextClientVersion(2);
36         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
37             setEGLConfigChooser(8, 8, 8, 8, 0, 8);
38         } else {
39             mRequestedOpenGLAPI = useOpenGL;
40             setEGLConfigChooser(new SampleViewEGLConfigChooser());
41         }
42         setRenderer(mSampleRenderer);
43         setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
44     }
45 
46     @Override
onTouchEvent(MotionEvent event)47     public boolean onTouchEvent(MotionEvent event) {
48         int count = event.getPointerCount();
49         for (int i = 0; i < count; i++) {
50             final float x = event.getX(i);
51             final float y = event.getY(i);
52             final int owner = event.getPointerId(i);
53             int action = event.getAction() & MotionEvent.ACTION_MASK;
54             switch (action) {
55             case MotionEvent.ACTION_POINTER_UP:
56                 action = MotionEvent.ACTION_UP;
57                 break;
58             case MotionEvent.ACTION_POINTER_DOWN:
59                 action = MotionEvent.ACTION_DOWN;
60                 break;
61             default:
62                 break;
63             }
64             final int finalAction = action;
65             queueEvent(new Runnable() {
66                 @Override
67                 public void run() {
68                     mSampleRenderer.handleClick(owner, x, y, finalAction);
69                 }
70             });
71         }
72         return true;
73     }
74 
inval()75     public void inval() {
76         queueEvent(new Runnable() {
77             @Override
78             public void run() {
79                 mSampleRenderer.postInval();
80             }
81         });
82     }
83 
terminate()84     public void terminate() {
85         queueEvent(new Runnable() {
86             @Override
87             public void run() {
88                 mSampleRenderer.term();
89             }
90         });
91     }
92 
showOverview()93     public void showOverview() {
94         queueEvent(new Runnable() {
95             @Override
96             public void run() {
97                 mSampleRenderer.showOverview();
98             }
99         });
100     }
101 
nextSample()102     public void nextSample() {
103         queueEvent(new Runnable() {
104             @Override
105             public void run() {
106                 mSampleRenderer.nextSample();
107             }
108         });
109     }
110 
previousSample()111     public void previousSample() {
112         queueEvent(new Runnable() {
113             @Override
114             public void run() {
115                 mSampleRenderer.previousSample();
116             }
117         });
118     }
119 
goToSample(final int position)120     public void goToSample(final int position) {
121         queueEvent(new Runnable() {
122             @Override
123             public void run() {
124                 mSampleRenderer.goToSample(position);
125             }
126         });
127     }
128 
toggleRenderingMode()129     public void toggleRenderingMode() {
130         queueEvent(new Runnable() {
131             @Override
132             public void run() {
133                 mSampleRenderer.toggleRenderingMode();
134             }
135         });
136     }
137 
toggleSlideshow()138     public void toggleSlideshow() {
139         queueEvent(new Runnable() {
140             @Override
141             public void run() {
142                 mSampleRenderer.toggleSlideshow();
143             }
144         });
145     }
146 
toggleFPS()147     public void toggleFPS() {
148         queueEvent(new Runnable() {
149             @Override
150             public void run() {
151                 mSampleRenderer.toggleFPS();
152             }
153         });
154     }
155 
toggleTiling()156     public void toggleTiling() {
157         queueEvent(new Runnable() {
158             @Override
159             public void run() {
160                 mSampleRenderer.toggleTiling();
161             }
162         });
163     }
164 
toggleBBox()165     public void toggleBBox() {
166         queueEvent(new Runnable() {
167             @Override
168             public void run() {
169                 mSampleRenderer.toggleBBox();
170             }
171         });
172     }
173 
saveToPDF()174     public void saveToPDF() {
175         queueEvent(new Runnable() {
176             @Override
177             public void run() {
178                 mSampleRenderer.saveToPDF();
179             }
180         });
181 
182 	String msg = getContext().getString(R.string.pdf_save_info);
183 	Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
184     }
185 
getUsesOpenGLAPI()186     public boolean getUsesOpenGLAPI() {
187         return mRequestedOpenGLAPI;
188     }
189 
getMSAASampleCount()190     public int getMSAASampleCount() {
191         return mSampleRenderer.getMSAASampleCount();
192     }
193 
194     private class SampleViewEGLConfigChooser implements GLSurfaceView.EGLConfigChooser {
195 
196         @Override
chooseConfig(EGL10 egl, EGLDisplay display)197         public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
198             int numConfigs = 0;
199             int[] configSpec = null;
200             int[] value = new int[1];
201 
202             int[] validAPIs = new int[] {
203                 EGL14.EGL_OPENGL_API,
204                 EGL14.EGL_OPENGL_ES_API
205             };
206             int initialAPI = mRequestedOpenGLAPI ? 0 : 1;
207 
208             for (int i = initialAPI; i < validAPIs.length && numConfigs == 0; i++) {
209                 int currentAPI = validAPIs[i];
210                 EGL14.eglBindAPI(currentAPI);
211 
212                 // setup the renderableType which will only be included in the
213                 // spec if we are attempting to get access to the OpenGL APIs.
214                 int renderableType = EGL14.EGL_OPENGL_BIT;
215                 if (currentAPI == EGL14.EGL_OPENGL_API) {
216                     renderableType = EGL14.EGL_OPENGL_ES2_BIT;
217                 }
218 
219                 if (mRequestedMSAASampleCount > 0) {
220                     configSpec = new int[] {
221                         EGL10.EGL_RED_SIZE, 8,
222                         EGL10.EGL_GREEN_SIZE, 8,
223                         EGL10.EGL_BLUE_SIZE, 8,
224                         EGL10.EGL_ALPHA_SIZE, 8,
225                         EGL10.EGL_DEPTH_SIZE, 0,
226                         EGL10.EGL_STENCIL_SIZE, 8,
227                         EGL10.EGL_SAMPLE_BUFFERS, 1,
228                         EGL10.EGL_SAMPLES, mRequestedMSAASampleCount,
229                         EGL10.EGL_RENDERABLE_TYPE, renderableType,
230                         EGL10.EGL_NONE
231                     };
232 
233                     // EGL_RENDERABLE_TYPE is only needed when attempting to use
234                     // the OpenGL API (not ES) and causes many EGL drivers to fail
235                     // with a BAD_ATTRIBUTE error.
236                     if (!mRequestedOpenGLAPI) {
237                       configSpec[16] = EGL10.EGL_NONE;
238                       Log.i("Skia", "spec: " + configSpec);
239                     }
240 
241                     if (!egl.eglChooseConfig(display, configSpec, null, 0, value)) {
242                         Log.i("Skia", "Could not get MSAA context count: " + mRequestedMSAASampleCount);
243                     }
244 
245                     numConfigs = value[0];
246                 }
247 
248                 if (numConfigs <= 0) {
249                     // Try without multisampling.
250                     configSpec = new int[] {
251                         EGL10.EGL_RED_SIZE, 8,
252                         EGL10.EGL_GREEN_SIZE, 8,
253                         EGL10.EGL_BLUE_SIZE, 8,
254                         EGL10.EGL_ALPHA_SIZE, 8,
255                         EGL10.EGL_DEPTH_SIZE, 0,
256                         EGL10.EGL_STENCIL_SIZE, 8,
257                         EGL10.EGL_RENDERABLE_TYPE, renderableType,
258                         EGL10.EGL_NONE
259                     };
260 
261                     // EGL_RENDERABLE_TYPE is only needed when attempting to use
262                     // the OpenGL API (not ES) and causes many EGL drivers to fail
263                     // with a BAD_ATTRIBUTE error.
264                     if (!mRequestedOpenGLAPI) {
265                       configSpec[12] = EGL10.EGL_NONE;
266                       Log.i("Skia", "spec: " + configSpec);
267                     }
268 
269                     if (!egl.eglChooseConfig(display, configSpec, null, 0, value)) {
270                       Log.i("Skia", "Could not get non-MSAA context count");
271                     }
272                     numConfigs = value[0];
273                 }
274             }
275 
276             if (numConfigs <= 0) {
277                 throw new IllegalArgumentException("No configs match configSpec");
278             }
279 
280             // Get all matching configurations.
281             EGLConfig[] configs = new EGLConfig[numConfigs];
282             if (!egl.eglChooseConfig(display, configSpec, configs, numConfigs, value)) {
283                 throw new IllegalArgumentException("Could not get config data");
284             }
285 
286             for (int i = 0; i < configs.length; ++i) {
287                 EGLConfig config = configs[i];
288                 if (findConfigAttrib(egl, display, config , EGL10.EGL_RED_SIZE, 0) == 8 &&
289                         findConfigAttrib(egl, display, config, EGL10.EGL_BLUE_SIZE, 0) == 8 &&
290                         findConfigAttrib(egl, display, config, EGL10.EGL_GREEN_SIZE, 0) == 8 &&
291                         findConfigAttrib(egl, display, config, EGL10.EGL_ALPHA_SIZE, 0) == 8 &&
292                         findConfigAttrib(egl, display, config, EGL10.EGL_STENCIL_SIZE, 0) == 8) {
293                     return config;
294                 }
295             }
296 
297             throw new IllegalArgumentException("Could not find suitable EGL config");
298         }
299 
findConfigAttrib(EGL10 egl, EGLDisplay display, EGLConfig config, int attribute, int defaultValue)300         private int findConfigAttrib(EGL10 egl, EGLDisplay display,
301                 EGLConfig config, int attribute, int defaultValue) {
302             int[] value = new int[1];
303             if (egl.eglGetConfigAttrib(display, config, attribute, value)) {
304                 return value[0];
305             }
306             return defaultValue;
307         }
308 
309     }
310 }
311