OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 package com.skia; | 8 package com.skia; |
9 | 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 | |
10 import android.content.Context; | 15 import android.content.Context; |
16 import android.opengl.EGL14; | |
11 import android.opengl.GLSurfaceView; | 17 import android.opengl.GLSurfaceView; |
12 import android.view.MotionEvent; | 18 import android.view.MotionEvent; |
13 | 19 |
14 public class SkiaSampleView extends GLSurfaceView { | 20 public class SkiaSampleView extends GLSurfaceView { |
15 | 21 |
16 private final SkiaSampleRenderer mSampleRenderer; | 22 private final SkiaSampleRenderer mSampleRenderer; |
23 private boolean mRequestedOpenGLAPI; // true == use (desktop) OpenGL. false == use OpenGL ES. | |
24 private int mRequestedMSAASampleCount; | |
17 | 25 |
18 public SkiaSampleView(Context ctx) { | 26 public SkiaSampleView(Context ctx, boolean useOpenGL, int msaaSampleCount) { |
19 super(ctx); | 27 super(ctx); |
20 | 28 |
21 mSampleRenderer = new SkiaSampleRenderer(this); | 29 mSampleRenderer = new SkiaSampleRenderer(this); |
30 mRequestedOpenGLAPI = useOpenGL; | |
31 mRequestedMSAASampleCount = msaaSampleCount; | |
22 | 32 |
23 setEGLContextClientVersion(2); | 33 setEGLContextClientVersion(2); |
24 setEGLConfigChooser(8,8,8,8,0,8); | 34 setEGLConfigChooser(new SampleViewEGLConfigChooser()); |
djsollen
2013/11/07 16:57:43
you may want do a runtime check of the android ver
Kimmo Kinnunen
2013/11/20 13:45:00
Done.
| |
25 setRenderer(mSampleRenderer); | 35 setRenderer(mSampleRenderer); |
26 setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); | 36 setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); |
27 } | 37 } |
28 | 38 |
29 @Override | 39 @Override |
30 public boolean onTouchEvent(MotionEvent event) { | 40 public boolean onTouchEvent(MotionEvent event) { |
31 int count = event.getPointerCount(); | 41 int count = event.getPointerCount(); |
32 for (int i = 0; i < count; i++) { | 42 for (int i = 0; i < count; i++) { |
33 final float x = event.getX(i); | 43 final float x = event.getX(i); |
34 final float y = event.getY(i); | 44 final float y = event.getY(i); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 } | 165 } |
156 | 166 |
157 public void saveToPDF() { | 167 public void saveToPDF() { |
158 queueEvent(new Runnable() { | 168 queueEvent(new Runnable() { |
159 @Override | 169 @Override |
160 public void run() { | 170 public void run() { |
161 mSampleRenderer.saveToPDF(); | 171 mSampleRenderer.saveToPDF(); |
162 } | 172 } |
163 }); | 173 }); |
164 } | 174 } |
175 | |
176 public boolean getUsesOpenGLAPI() { | |
177 return mRequestedOpenGLAPI; | |
178 } | |
179 | |
180 public int getMSAASampleCount() { | |
181 // Currently we do not check if the request succeeded, because there is | |
182 // no good API to identify which EGLConfig was selected and used by the | |
183 // GLSurfaceView. | |
184 return mRequestedMSAASampleCount; | |
185 } | |
186 | |
187 private class SampleViewEGLConfigChooser implements GLSurfaceView.EGLConfigC hooser { | |
djsollen
2013/11/07 16:57:43
this class should only be instantiated for devices
Kimmo Kinnunen
2013/11/20 13:45:00
Done.
| |
188 private int[] mValue; | |
189 | |
190 @Override | |
191 public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { | |
192 mValue = new int[1]; | |
193 | |
194 int glAPIToTry; | |
195 | |
196 if (mRequestedOpenGLAPI) { | |
197 glAPIToTry = EGL14.EGL_OPENGL_API; | |
198 } else { | |
199 glAPIToTry = EGL14.EGL_OPENGL_ES_API; | |
200 } | |
201 | |
202 int numConfigs = 0; | |
203 int[] configSpec = null; | |
204 | |
205 do { | |
206 EGL14.eglBindAPI(glAPIToTry); | |
207 | |
208 int renderableType; | |
209 if (glAPIToTry == EGL14.EGL_OPENGL_API) { | |
210 renderableType = EGL14.EGL_OPENGL_ES2_BIT; | |
211 | |
212 // If this API does not work, try ES next. | |
213 glAPIToTry = EGL14.EGL_OPENGL_ES_API; | |
214 } else { | |
215 renderableType = EGL14.EGL_OPENGL_BIT; | |
216 } | |
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_RENDERABLE_TYPE, renderableType, | |
228 EGL10.EGL_SAMPLE_BUFFERS, 1, | |
229 EGL10.EGL_SAMPLES, mRequestedMSAASampleCount, | |
230 EGL10.EGL_NONE | |
231 }; | |
232 | |
233 if (!egl.eglChooseConfig(display, configSpec, null, 0, mValu e)) { | |
234 throw new IllegalArgumentException("Could not get MSAA c ontext count"); | |
235 } | |
236 | |
237 numConfigs = mValue[0]; | |
238 } | |
239 | |
240 if (numConfigs <= 0) { | |
241 // Try without multisampling. | |
242 configSpec = new int[] { | |
243 EGL10.EGL_RED_SIZE, 8, | |
244 EGL10.EGL_GREEN_SIZE, 8, | |
245 EGL10.EGL_BLUE_SIZE, 8, | |
246 EGL10.EGL_ALPHA_SIZE, 8, | |
247 EGL10.EGL_DEPTH_SIZE, 0, | |
248 EGL10.EGL_STENCIL_SIZE, 8, | |
249 EGL10.EGL_RENDERABLE_TYPE, renderableType, | |
250 EGL10.EGL_NONE | |
251 }; | |
252 | |
253 if (!egl.eglChooseConfig(display, configSpec, null, 0, mValu e)) { | |
254 throw new IllegalArgumentException("Could not get non-MS AA context count"); | |
255 } | |
256 numConfigs = mValue[0]; | |
257 } | |
258 | |
259 } while (glAPIToTry != EGL14.EGL_OPENGL_ES_API && numConfigs == 0); | |
260 | |
261 if (numConfigs <= 0) { | |
262 throw new IllegalArgumentException("No configs match configSpec" ); | |
263 } | |
264 | |
265 // Get all matching configurations. | |
266 EGLConfig[] configs = new EGLConfig[numConfigs]; | |
267 if (!egl.eglChooseConfig(display, configSpec, configs, numConfigs, m Value)) { | |
268 throw new IllegalArgumentException("Could not get config data"); | |
269 } | |
270 | |
271 for (int i = 0; i < configs.length; ++i) { | |
272 EGLConfig config = configs[i]; | |
273 if (findConfigAttrib(egl, display, config , EGL10.EGL_RED_SIZE, 0) == 8 && | |
274 findConfigAttrib(egl, display, config, EGL10.EGL_BLUE_SI ZE, 0) == 8 && | |
275 findConfigAttrib(egl, display, config, EGL10.EGL_GREEN_S IZE, 0) == 8 && | |
276 findConfigAttrib(egl, display, config, EGL10.EGL_ALPHA_S IZE, 0) == 8 && | |
277 findConfigAttrib(egl, display, config, EGL10.EGL_STENCIL _SIZE, 0) == 8) { | |
278 return config; | |
279 } | |
280 } | |
281 | |
282 throw new IllegalArgumentException("Could not find suitable EGL conf ig"); | |
283 } | |
284 | |
285 private int findConfigAttrib(EGL10 egl, EGLDisplay display, | |
286 EGLConfig config, int attribute, int defaultValue) { | |
287 if (egl.eglGetConfigAttrib(display, config, attribute, mValue)) { | |
288 return mValue[0]; | |
289 } | |
290 return defaultValue; | |
291 } | |
292 | |
293 } | |
165 } | 294 } |
OLD | NEW |