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; | 10 import javax.microedition.khronos.egl.EGL10; |
11 import javax.microedition.khronos.egl.EGLConfig; | 11 import javax.microedition.khronos.egl.EGLConfig; |
12 import javax.microedition.khronos.egl.EGLDisplay; | 12 import javax.microedition.khronos.egl.EGLDisplay; |
13 import javax.microedition.khronos.opengles.GL10; | 13 import javax.microedition.khronos.opengles.GL10; |
14 | 14 |
15 import android.content.Context; | 15 import android.content.Context; |
16 import android.opengl.EGL14; | 16 import android.opengl.EGL14; |
17 import android.opengl.GLSurfaceView; | 17 import android.opengl.GLSurfaceView; |
18 import android.os.Build; | 18 import android.os.Build; |
| 19 import android.util.Log; |
19 import android.view.MotionEvent; | 20 import android.view.MotionEvent; |
20 | 21 |
21 public class SkiaSampleView extends GLSurfaceView { | 22 public class SkiaSampleView extends GLSurfaceView { |
22 | 23 |
23 private final SkiaSampleRenderer mSampleRenderer; | 24 private final SkiaSampleRenderer mSampleRenderer; |
24 private boolean mRequestedOpenGLAPI; // true == use (desktop) OpenGL. false
== use OpenGL ES. | 25 private boolean mRequestedOpenGLAPI; // true == use (desktop) OpenGL. false
== use OpenGL ES. |
25 private int mRequestedMSAASampleCount; | 26 private int mRequestedMSAASampleCount; |
26 | 27 |
27 public SkiaSampleView(Context ctx, boolean useOpenGL, int msaaSampleCount) { | 28 public SkiaSampleView(Context ctx, boolean useOpenGL, int msaaSampleCount) { |
28 super(ctx); | 29 super(ctx); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 | 181 |
181 public boolean getUsesOpenGLAPI() { | 182 public boolean getUsesOpenGLAPI() { |
182 return mRequestedOpenGLAPI; | 183 return mRequestedOpenGLAPI; |
183 } | 184 } |
184 | 185 |
185 public int getMSAASampleCount() { | 186 public int getMSAASampleCount() { |
186 return mSampleRenderer.getMSAASampleCount(); | 187 return mSampleRenderer.getMSAASampleCount(); |
187 } | 188 } |
188 | 189 |
189 private class SampleViewEGLConfigChooser implements GLSurfaceView.EGLConfigC
hooser { | 190 private class SampleViewEGLConfigChooser implements GLSurfaceView.EGLConfigC
hooser { |
190 private int[] mValue; | |
191 | 191 |
192 @Override | 192 @Override |
193 public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { | 193 public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { |
194 mValue = new int[1]; | |
195 | |
196 int glAPIToTry; | |
197 | |
198 if (mRequestedOpenGLAPI) { | |
199 glAPIToTry = EGL14.EGL_OPENGL_API; | |
200 } else { | |
201 glAPIToTry = EGL14.EGL_OPENGL_ES_API; | |
202 } | |
203 | |
204 int numConfigs = 0; | 194 int numConfigs = 0; |
205 int[] configSpec = null; | 195 int[] configSpec = null; |
| 196 int[] value = new int[1]; |
| 197 |
| 198 int[] validAPIs = new int[] { |
| 199 EGL14.EGL_OPENGL_API, |
| 200 EGL14.EGL_OPENGL_ES_API |
| 201 }; |
| 202 int initialAPI = mRequestedOpenGLAPI ? 0 : 1; |
| 203 |
| 204 for (int i = initialAPI; i < validAPIs.length && numConfigs == 0; i+
+) { |
| 205 int currentAPI = validAPIs[i]; |
| 206 EGL14.eglBindAPI(currentAPI); |
206 | 207 |
207 do { | 208 // setup the renderableType which will only be included in the |
208 EGL14.eglBindAPI(glAPIToTry); | 209 // spec if we are attempting to get access to the OpenGL APIs. |
209 | 210 int renderableType = EGL14.EGL_OPENGL_BIT; |
210 int renderableType; | 211 if (currentAPI == EGL14.EGL_OPENGL_API) { |
211 if (glAPIToTry == EGL14.EGL_OPENGL_API) { | |
212 renderableType = EGL14.EGL_OPENGL_ES2_BIT; | 212 renderableType = EGL14.EGL_OPENGL_ES2_BIT; |
213 | |
214 // If this API does not work, try ES next. | |
215 glAPIToTry = EGL14.EGL_OPENGL_ES_API; | |
216 } else { | |
217 renderableType = EGL14.EGL_OPENGL_BIT; | |
218 } | 213 } |
219 | 214 |
220 | |
221 if (mRequestedMSAASampleCount > 0) { | 215 if (mRequestedMSAASampleCount > 0) { |
222 configSpec = new int[] { | 216 configSpec = new int[] { |
223 EGL10.EGL_RED_SIZE, 8, | 217 EGL10.EGL_RED_SIZE, 8, |
224 EGL10.EGL_GREEN_SIZE, 8, | 218 EGL10.EGL_GREEN_SIZE, 8, |
225 EGL10.EGL_BLUE_SIZE, 8, | 219 EGL10.EGL_BLUE_SIZE, 8, |
226 EGL10.EGL_ALPHA_SIZE, 8, | 220 EGL10.EGL_ALPHA_SIZE, 8, |
227 EGL10.EGL_DEPTH_SIZE, 0, | 221 EGL10.EGL_DEPTH_SIZE, 0, |
228 EGL10.EGL_STENCIL_SIZE, 8, | 222 EGL10.EGL_STENCIL_SIZE, 8, |
229 EGL10.EGL_RENDERABLE_TYPE, renderableType, | |
230 EGL10.EGL_SAMPLE_BUFFERS, 1, | 223 EGL10.EGL_SAMPLE_BUFFERS, 1, |
231 EGL10.EGL_SAMPLES, mRequestedMSAASampleCount, | 224 EGL10.EGL_SAMPLES, mRequestedMSAASampleCount, |
| 225 EGL10.EGL_RENDERABLE_TYPE, renderableType, |
232 EGL10.EGL_NONE | 226 EGL10.EGL_NONE |
233 }; | 227 }; |
234 | 228 |
235 if (!egl.eglChooseConfig(display, configSpec, null, 0, mValu
e)) { | 229 // EGL_RENDERABLE_TYPE is only needed when attempting to use |
236 throw new IllegalArgumentException("Could not get MSAA c
ontext count"); | 230 // the OpenGL API (not ES) and causes many EGL drivers to fa
il |
| 231 // with a BAD_ATTRIBUTE error. |
| 232 if (!mRequestedOpenGLAPI) { |
| 233 configSpec[16] = EGL10.EGL_NONE; |
| 234 Log.i("Skia", "spec: " + configSpec); |
| 235 } |
| 236 |
| 237 if (!egl.eglChooseConfig(display, configSpec, null, 0, value
)) { |
| 238 Log.i("Skia", "Could not get MSAA context count: " + mRe
questedMSAASampleCount); |
237 } | 239 } |
238 | 240 |
239 numConfigs = mValue[0]; | 241 numConfigs = value[0]; |
240 } | 242 } |
241 | 243 |
242 if (numConfigs <= 0) { | 244 if (numConfigs <= 0) { |
243 // Try without multisampling. | 245 // Try without multisampling. |
244 configSpec = new int[] { | 246 configSpec = new int[] { |
245 EGL10.EGL_RED_SIZE, 8, | 247 EGL10.EGL_RED_SIZE, 8, |
246 EGL10.EGL_GREEN_SIZE, 8, | 248 EGL10.EGL_GREEN_SIZE, 8, |
247 EGL10.EGL_BLUE_SIZE, 8, | 249 EGL10.EGL_BLUE_SIZE, 8, |
248 EGL10.EGL_ALPHA_SIZE, 8, | 250 EGL10.EGL_ALPHA_SIZE, 8, |
249 EGL10.EGL_DEPTH_SIZE, 0, | 251 EGL10.EGL_DEPTH_SIZE, 0, |
250 EGL10.EGL_STENCIL_SIZE, 8, | 252 EGL10.EGL_STENCIL_SIZE, 8, |
251 EGL10.EGL_RENDERABLE_TYPE, renderableType, | 253 EGL10.EGL_RENDERABLE_TYPE, renderableType, |
252 EGL10.EGL_NONE | 254 EGL10.EGL_NONE |
253 }; | 255 }; |
| 256 |
| 257 // EGL_RENDERABLE_TYPE is only needed when attempting to use |
| 258 // the OpenGL API (not ES) and causes many EGL drivers to fa
il |
| 259 // with a BAD_ATTRIBUTE error. |
| 260 if (!mRequestedOpenGLAPI) { |
| 261 configSpec[12] = EGL10.EGL_NONE; |
| 262 Log.i("Skia", "spec: " + configSpec); |
| 263 } |
254 | 264 |
255 if (!egl.eglChooseConfig(display, configSpec, null, 0, mValu
e)) { | 265 if (!egl.eglChooseConfig(display, configSpec, null, 0, value
)) { |
256 throw new IllegalArgumentException("Could not get non-MS
AA context count"); | 266 Log.i("Skia", "Could not get non-MSAA context count"); |
257 } | 267 } |
258 numConfigs = mValue[0]; | 268 numConfigs = value[0]; |
259 } | 269 } |
260 | 270 } |
261 } while (glAPIToTry != EGL14.EGL_OPENGL_ES_API && numConfigs == 0); | |
262 | 271 |
263 if (numConfigs <= 0) { | 272 if (numConfigs <= 0) { |
264 throw new IllegalArgumentException("No configs match configSpec"
); | 273 throw new IllegalArgumentException("No configs match configSpec"
); |
265 } | 274 } |
266 | 275 |
267 // Get all matching configurations. | 276 // Get all matching configurations. |
268 EGLConfig[] configs = new EGLConfig[numConfigs]; | 277 EGLConfig[] configs = new EGLConfig[numConfigs]; |
269 if (!egl.eglChooseConfig(display, configSpec, configs, numConfigs, m
Value)) { | 278 if (!egl.eglChooseConfig(display, configSpec, configs, numConfigs, v
alue)) { |
270 throw new IllegalArgumentException("Could not get config data"); | 279 throw new IllegalArgumentException("Could not get config data"); |
271 } | 280 } |
272 | 281 |
273 for (int i = 0; i < configs.length; ++i) { | 282 for (int i = 0; i < configs.length; ++i) { |
274 EGLConfig config = configs[i]; | 283 EGLConfig config = configs[i]; |
275 if (findConfigAttrib(egl, display, config , EGL10.EGL_RED_SIZE,
0) == 8 && | 284 if (findConfigAttrib(egl, display, config , EGL10.EGL_RED_SIZE,
0) == 8 && |
276 findConfigAttrib(egl, display, config, EGL10.EGL_BLUE_SI
ZE, 0) == 8 && | 285 findConfigAttrib(egl, display, config, EGL10.EGL_BLUE_SI
ZE, 0) == 8 && |
277 findConfigAttrib(egl, display, config, EGL10.EGL_GREEN_S
IZE, 0) == 8 && | 286 findConfigAttrib(egl, display, config, EGL10.EGL_GREEN_S
IZE, 0) == 8 && |
278 findConfigAttrib(egl, display, config, EGL10.EGL_ALPHA_S
IZE, 0) == 8 && | 287 findConfigAttrib(egl, display, config, EGL10.EGL_ALPHA_S
IZE, 0) == 8 && |
279 findConfigAttrib(egl, display, config, EGL10.EGL_STENCIL
_SIZE, 0) == 8) { | 288 findConfigAttrib(egl, display, config, EGL10.EGL_STENCIL
_SIZE, 0) == 8) { |
280 return config; | 289 return config; |
281 } | 290 } |
282 } | 291 } |
283 | 292 |
284 throw new IllegalArgumentException("Could not find suitable EGL conf
ig"); | 293 throw new IllegalArgumentException("Could not find suitable EGL conf
ig"); |
285 } | 294 } |
286 | 295 |
287 private int findConfigAttrib(EGL10 egl, EGLDisplay display, | 296 private int findConfigAttrib(EGL10 egl, EGLDisplay display, |
288 EGLConfig config, int attribute, int defaultValue) { | 297 EGLConfig config, int attribute, int defaultValue) { |
289 if (egl.eglGetConfigAttrib(display, config, attribute, mValue)) { | 298 int[] value = new int[1]; |
290 return mValue[0]; | 299 if (egl.eglGetConfigAttrib(display, config, attribute, value)) { |
| 300 return value[0]; |
291 } | 301 } |
292 return defaultValue; | 302 return defaultValue; |
293 } | 303 } |
294 | 304 |
295 } | 305 } |
296 } | 306 } |
OLD | NEW |