Chromium Code Reviews| Index: platform_tools/android/app/src/com/skia/SkiaSampleView.java |
| diff --git a/platform_tools/android/app/src/com/skia/SkiaSampleView.java b/platform_tools/android/app/src/com/skia/SkiaSampleView.java |
| index 0d493c96df6886b8220dcec62ef509f6ea6e027e..a04d45f8ef94e5ca6e7d17b3d92cbe3e0fa48d56 100644 |
| --- a/platform_tools/android/app/src/com/skia/SkiaSampleView.java |
| +++ b/platform_tools/android/app/src/com/skia/SkiaSampleView.java |
| @@ -7,21 +7,31 @@ |
| package com.skia; |
| +import javax.microedition.khronos.egl.EGL10; |
| +import javax.microedition.khronos.egl.EGLConfig; |
| +import javax.microedition.khronos.egl.EGLDisplay; |
| +import javax.microedition.khronos.opengles.GL10; |
| + |
| import android.content.Context; |
| +import android.opengl.EGL14; |
| import android.opengl.GLSurfaceView; |
| import android.view.MotionEvent; |
| public class SkiaSampleView extends GLSurfaceView { |
| private final SkiaSampleRenderer mSampleRenderer; |
| + private boolean mRequestedOpenGLAPI; // true == use (desktop) OpenGL. false == use OpenGL ES. |
| + private int mRequestedMSAASampleCount; |
| - public SkiaSampleView(Context ctx) { |
| + public SkiaSampleView(Context ctx, boolean useOpenGL, int msaaSampleCount) { |
| super(ctx); |
| mSampleRenderer = new SkiaSampleRenderer(this); |
| + mRequestedOpenGLAPI = useOpenGL; |
| + mRequestedMSAASampleCount = msaaSampleCount; |
| setEGLContextClientVersion(2); |
| - setEGLConfigChooser(8,8,8,8,0,8); |
| + 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.
|
| setRenderer(mSampleRenderer); |
| setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); |
| } |
| @@ -162,4 +172,123 @@ public class SkiaSampleView extends GLSurfaceView { |
| } |
| }); |
| } |
| + |
| + public boolean getUsesOpenGLAPI() { |
| + return mRequestedOpenGLAPI; |
| + } |
| + |
| + public int getMSAASampleCount() { |
| + // Currently we do not check if the request succeeded, because there is |
| + // no good API to identify which EGLConfig was selected and used by the |
| + // GLSurfaceView. |
| + return mRequestedMSAASampleCount; |
| + } |
| + |
| + private class SampleViewEGLConfigChooser implements GLSurfaceView.EGLConfigChooser { |
|
djsollen
2013/11/07 16:57:43
this class should only be instantiated for devices
Kimmo Kinnunen
2013/11/20 13:45:00
Done.
|
| + private int[] mValue; |
| + |
| + @Override |
| + public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { |
| + mValue = new int[1]; |
| + |
| + int glAPIToTry; |
| + |
| + if (mRequestedOpenGLAPI) { |
| + glAPIToTry = EGL14.EGL_OPENGL_API; |
| + } else { |
| + glAPIToTry = EGL14.EGL_OPENGL_ES_API; |
| + } |
| + |
| + int numConfigs = 0; |
| + int[] configSpec = null; |
| + |
| + do { |
| + EGL14.eglBindAPI(glAPIToTry); |
| + |
| + int renderableType; |
| + if (glAPIToTry == EGL14.EGL_OPENGL_API) { |
| + renderableType = EGL14.EGL_OPENGL_ES2_BIT; |
| + |
| + // If this API does not work, try ES next. |
| + glAPIToTry = EGL14.EGL_OPENGL_ES_API; |
| + } else { |
| + renderableType = EGL14.EGL_OPENGL_BIT; |
| + } |
| + |
| + |
| + if (mRequestedMSAASampleCount > 0) { |
| + configSpec = new int[] { |
| + EGL10.EGL_RED_SIZE, 8, |
| + EGL10.EGL_GREEN_SIZE, 8, |
| + EGL10.EGL_BLUE_SIZE, 8, |
| + EGL10.EGL_ALPHA_SIZE, 8, |
| + EGL10.EGL_DEPTH_SIZE, 0, |
| + EGL10.EGL_STENCIL_SIZE, 8, |
| + EGL10.EGL_RENDERABLE_TYPE, renderableType, |
| + EGL10.EGL_SAMPLE_BUFFERS, 1, |
| + EGL10.EGL_SAMPLES, mRequestedMSAASampleCount, |
| + EGL10.EGL_NONE |
| + }; |
| + |
| + if (!egl.eglChooseConfig(display, configSpec, null, 0, mValue)) { |
| + throw new IllegalArgumentException("Could not get MSAA context count"); |
| + } |
| + |
| + numConfigs = mValue[0]; |
| + } |
| + |
| + if (numConfigs <= 0) { |
| + // Try without multisampling. |
| + configSpec = new int[] { |
| + EGL10.EGL_RED_SIZE, 8, |
| + EGL10.EGL_GREEN_SIZE, 8, |
| + EGL10.EGL_BLUE_SIZE, 8, |
| + EGL10.EGL_ALPHA_SIZE, 8, |
| + EGL10.EGL_DEPTH_SIZE, 0, |
| + EGL10.EGL_STENCIL_SIZE, 8, |
| + EGL10.EGL_RENDERABLE_TYPE, renderableType, |
| + EGL10.EGL_NONE |
| + }; |
| + |
| + if (!egl.eglChooseConfig(display, configSpec, null, 0, mValue)) { |
| + throw new IllegalArgumentException("Could not get non-MSAA context count"); |
| + } |
| + numConfigs = mValue[0]; |
| + } |
| + |
| + } while (glAPIToTry != EGL14.EGL_OPENGL_ES_API && numConfigs == 0); |
| + |
| + if (numConfigs <= 0) { |
| + throw new IllegalArgumentException("No configs match configSpec"); |
| + } |
| + |
| + // Get all matching configurations. |
| + EGLConfig[] configs = new EGLConfig[numConfigs]; |
| + if (!egl.eglChooseConfig(display, configSpec, configs, numConfigs, mValue)) { |
| + throw new IllegalArgumentException("Could not get config data"); |
| + } |
| + |
| + for (int i = 0; i < configs.length; ++i) { |
| + EGLConfig config = configs[i]; |
| + if (findConfigAttrib(egl, display, config , EGL10.EGL_RED_SIZE, 0) == 8 && |
| + findConfigAttrib(egl, display, config, EGL10.EGL_BLUE_SIZE, 0) == 8 && |
| + findConfigAttrib(egl, display, config, EGL10.EGL_GREEN_SIZE, 0) == 8 && |
| + findConfigAttrib(egl, display, config, EGL10.EGL_ALPHA_SIZE, 0) == 8 && |
| + findConfigAttrib(egl, display, config, EGL10.EGL_STENCIL_SIZE, 0) == 8) { |
| + return config; |
| + } |
| + } |
| + |
| + throw new IllegalArgumentException("Could not find suitable EGL config"); |
| + } |
| + |
| + private int findConfigAttrib(EGL10 egl, EGLDisplay display, |
| + EGLConfig config, int attribute, int defaultValue) { |
| + if (egl.eglGetConfigAttrib(display, config, attribute, mValue)) { |
| + return mValue[0]; |
| + } |
| + return defaultValue; |
| + } |
| + |
| + } |
| } |