|
| 1 | +package rajawali; |
| 2 | + |
| 3 | +import javax.microedition.khronos.egl.EGL10; |
| 4 | +import javax.microedition.khronos.egl.EGLConfig; |
| 5 | +import javax.microedition.khronos.egl.EGLDisplay; |
| 6 | + |
| 7 | +import rajawali.renderer.RajawaliRenderer; |
| 8 | +import rajawali.util.RajLog; |
| 9 | +import android.annotation.TargetApi; |
| 10 | +import android.opengl.GLSurfaceView; |
| 11 | +import android.os.Build; |
| 12 | +import android.service.dreams.DreamService; |
| 13 | +import android.view.View; |
| 14 | +import android.view.ViewGroup; |
| 15 | +import android.widget.AdapterView; |
| 16 | +import android.widget.FrameLayout; |
| 17 | + |
| 18 | + |
| 19 | +@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
| 20 | +public abstract class RajawaliDaydream extends DreamService { |
| 21 | + |
| 22 | + protected boolean mUsesCoverageAa; |
| 23 | + protected GLSurfaceView mSurfaceView; |
| 24 | + protected FrameLayout mLayout; |
| 25 | + |
| 26 | + private RajawaliRenderer mRajRenderer; |
| 27 | + |
| 28 | + @Override |
| 29 | + public void onAttachedToWindow() { |
| 30 | + super.onAttachedToWindow(); |
| 31 | + |
| 32 | + mSurfaceView = new GLSurfaceView(this); |
| 33 | + mSurfaceView.setEGLContextClientVersion(2); |
| 34 | + |
| 35 | + setInteractive(false); |
| 36 | + setFullscreen(true); |
| 37 | + |
| 38 | + mLayout = new FrameLayout(this); |
| 39 | + mLayout.addView(mSurfaceView); |
| 40 | + |
| 41 | + setContentView(mLayout); |
| 42 | + |
| 43 | + setRenderer(createRenderer()); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onDreamingStarted() { |
| 49 | + super.onDreamingStarted(); |
| 50 | + mSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); |
| 51 | + mSurfaceView.onResume(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onDreamingStopped() { |
| 56 | + super.onDreamingStopped(); |
| 57 | + mSurfaceView.onPause(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void onDetachedFromWindow() { |
| 62 | + super.onDetachedFromWindow(); |
| 63 | + mRajRenderer.onSurfaceDestroyed(); |
| 64 | + unbindDrawables(mLayout); |
| 65 | + System.gc(); |
| 66 | + } |
| 67 | + |
| 68 | + protected abstract RajawaliRenderer createRenderer(); |
| 69 | + |
| 70 | + protected void createMultisampleConfig() { |
| 71 | + final int EGL_COVERAGE_BUFFERS_NV = 0x30E0; |
| 72 | + final int EGL_COVERAGE_SAMPLES_NV = 0x30E1; |
| 73 | + |
| 74 | + mSurfaceView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() { |
| 75 | + public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { |
| 76 | + int[] configSpec = new int[] { |
| 77 | + //@formatter:off: |
| 78 | + EGL10.EGL_RED_SIZE, 5, |
| 79 | + EGL10.EGL_GREEN_SIZE, 6, |
| 80 | + EGL10.EGL_BLUE_SIZE, 5, |
| 81 | + EGL10.EGL_DEPTH_SIZE, 16, |
| 82 | + EGL10.EGL_RENDERABLE_TYPE, 4, |
| 83 | + EGL10.EGL_SAMPLE_BUFFERS, 1, |
| 84 | + EGL10.EGL_SAMPLES, 2, |
| 85 | + EGL10.EGL_NONE |
| 86 | + //@formatter:on |
| 87 | + }; |
| 88 | + |
| 89 | + int[] result = new int[1]; |
| 90 | + if (!egl.eglChooseConfig(display, configSpec, null, 0, result)) { |
| 91 | + RajLog.e("Multisampling configuration 1 failed."); |
| 92 | + } |
| 93 | + |
| 94 | + if (result[0] <= 0) { |
| 95 | + // no multisampling, check for coverage multisampling |
| 96 | + configSpec = new int[] { |
| 97 | + //@formatter:off |
| 98 | + EGL10.EGL_RED_SIZE, 5, |
| 99 | + EGL10.EGL_GREEN_SIZE, 6, |
| 100 | + EGL10.EGL_BLUE_SIZE, 5, |
| 101 | + EGL10.EGL_DEPTH_SIZE, 16, |
| 102 | + EGL10.EGL_RENDERABLE_TYPE, 4, |
| 103 | + EGL_COVERAGE_BUFFERS_NV, 1, |
| 104 | + EGL_COVERAGE_SAMPLES_NV, 2, |
| 105 | + EGL10.EGL_NONE |
| 106 | + //@formatter:on |
| 107 | + }; |
| 108 | + |
| 109 | + if (!egl.eglChooseConfig(display, configSpec, null, 0, |
| 110 | + result)) { |
| 111 | + RajLog.e("Multisampling configuration 2 failed. Multisampling is not possible on your device."); |
| 112 | + } |
| 113 | + |
| 114 | + if (result[0] <= 0) { |
| 115 | + configSpec = new int[] { |
| 116 | + //@formatter:off |
| 117 | + EGL10.EGL_RED_SIZE, 5, |
| 118 | + EGL10.EGL_GREEN_SIZE, 6, |
| 119 | + EGL10.EGL_BLUE_SIZE, 5, |
| 120 | + EGL10.EGL_DEPTH_SIZE, 16, |
| 121 | + EGL10.EGL_RENDERABLE_TYPE, 4, |
| 122 | + EGL10.EGL_NONE |
| 123 | + //@formatter:on |
| 124 | + }; |
| 125 | + |
| 126 | + if (!egl.eglChooseConfig(display, configSpec, null, 0, |
| 127 | + result)) { |
| 128 | + RajLog.e("Multisampling configuration 3 failed. Multisampling is not possible on your device."); |
| 129 | + } |
| 130 | + |
| 131 | + if (result[0] <= 0) { |
| 132 | + throw new RuntimeException( |
| 133 | + "Couldn't create OpenGL config."); |
| 134 | + } |
| 135 | + } else { |
| 136 | + mUsesCoverageAa = true; |
| 137 | + } |
| 138 | + } |
| 139 | + EGLConfig[] configs = new EGLConfig[result[0]]; |
| 140 | + if (!egl.eglChooseConfig(display, configSpec, configs, |
| 141 | + result[0], result)) { |
| 142 | + throw new RuntimeException("Couldn't create OpenGL config."); |
| 143 | + } |
| 144 | + |
| 145 | + int index = -1; |
| 146 | + int[] value = new int[1]; |
| 147 | + for (int i = 0; i < configs.length; ++i) { |
| 148 | + egl.eglGetConfigAttrib(display, configs[i], |
| 149 | + EGL10.EGL_RED_SIZE, value); |
| 150 | + if (value[0] == 5) { |
| 151 | + index = i; |
| 152 | + break; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + EGLConfig config = configs.length > 0 ? configs[index] : null; |
| 157 | + if (config == null) { |
| 158 | + throw new RuntimeException("No config chosen"); |
| 159 | + } |
| 160 | + |
| 161 | + return config; |
| 162 | + } |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + protected void setRenderer(RajawaliRenderer renderer) { |
| 167 | + mRajRenderer = renderer; |
| 168 | + mSurfaceView.setRenderer(renderer); |
| 169 | + } |
| 170 | + |
| 171 | + private void unbindDrawables(View view) { |
| 172 | + if (view.getBackground() != null) { |
| 173 | + view.getBackground().setCallback(null); |
| 174 | + } |
| 175 | + if (view instanceof ViewGroup && !(view instanceof AdapterView)) { |
| 176 | + for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { |
| 177 | + unbindDrawables(((ViewGroup) view).getChildAt(i)); |
| 178 | + } |
| 179 | + ((ViewGroup) view).removeAllViews(); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments