Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1 | /* |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 2 | * Copyright (C) 2014 The Android Open Source Project |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Brown | ad9ef19 | 2014-04-08 17:26:30 -0700 | [diff] [blame] | 17 | package com.android.server.display; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 18 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.io.InputStream; |
| 21 | import java.io.InputStreamReader; |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 22 | import java.io.PrintWriter; |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.nio.ByteOrder; |
| 25 | import java.nio.FloatBuffer; |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 26 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 28 | import android.graphics.PixelFormat; |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 29 | import android.graphics.SurfaceTexture; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 30 | import android.hardware.display.DisplayManagerInternal; |
| 31 | import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 32 | import android.opengl.EGL14; |
| 33 | import android.opengl.EGLConfig; |
| 34 | import android.opengl.EGLContext; |
| 35 | import android.opengl.EGLDisplay; |
| 36 | import android.opengl.EGLSurface; |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 37 | import android.opengl.GLES20; |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 38 | import android.opengl.GLES11Ext; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 39 | import android.util.Slog; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 40 | import android.view.DisplayInfo; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 41 | import android.view.Surface.OutOfResourcesException; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 42 | import android.view.Surface; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 43 | import android.view.SurfaceControl; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 44 | import android.view.SurfaceSession; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 45 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 46 | import libcore.io.Streams; |
| 47 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 48 | import com.android.server.LocalServices; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 51 | * <p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 52 | * Animates a screen transition from on to off or off to on by applying |
| 53 | * some GL transformations to a screenshot. |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 54 | * </p><p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 55 | * This component must only be created or accessed by the {@link Looper} thread |
| 56 | * that belongs to the {@link DisplayPowerController}. |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 57 | * </p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 58 | */ |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 59 | final class ColorFade { |
| 60 | private static final String TAG = "ColorFade"; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 61 | |
| 62 | private static final boolean DEBUG = false; |
| 63 | |
| 64 | // The layer for the electron beam surface. |
| 65 | // This is currently hardcoded to be one layer above the boot animation. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 66 | private static final int COLOR_FADE_LAYER = 0x40000001; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 67 | |
Jeff Brown | 78eb122 | 2012-10-10 18:27:44 -0700 | [diff] [blame] | 68 | // The number of frames to draw when preparing the animation so that it will |
| 69 | // be ready to run smoothly. We use 3 frames because we are triple-buffered. |
| 70 | // See code for details. |
| 71 | private static final int DEJANK_FRAMES = 3; |
| 72 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 73 | private final int mDisplayId; |
| 74 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 75 | // Set to true when the animation context has been fully prepared. |
| 76 | private boolean mPrepared; |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 77 | private int mMode; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 78 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 79 | private final DisplayManagerInternal mDisplayManagerInternal; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 80 | private int mDisplayLayerStack; // layer stack associated with primary display |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 81 | private int mDisplayWidth; // real width, not rotated |
| 82 | private int mDisplayHeight; // real height, not rotated |
| 83 | private SurfaceSession mSurfaceSession; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 84 | private SurfaceControl mSurfaceControl; |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 85 | private Surface mSurface; |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 86 | private NaturalSurfaceLayout mSurfaceLayout; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 87 | private EGLDisplay mEglDisplay; |
| 88 | private EGLConfig mEglConfig; |
| 89 | private EGLContext mEglContext; |
| 90 | private EGLSurface mEglSurface; |
| 91 | private boolean mSurfaceVisible; |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 92 | private float mSurfaceAlpha; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 93 | |
| 94 | // Texture names. We only use one texture, which contains the screenshot. |
| 95 | private final int[] mTexNames = new int[1]; |
| 96 | private boolean mTexNamesGenerated; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 97 | private final float mTexMatrix[] = new float[16]; |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 98 | private final float mProjMatrix[] = new float[16]; |
| 99 | private final int[] mGLBuffers = new int[2]; |
| 100 | private int mTexCoordLoc, mVertexLoc, mTexUnitLoc, mProjMatrixLoc, mTexMatrixLoc; |
| 101 | private int mOpacityLoc, mScaleLoc, mGammaLoc, mSaturationLoc; |
| 102 | private int mProgram; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 103 | |
| 104 | // Vertex and corresponding texture coordinates. |
| 105 | // We have 4 2D vertices, so 8 elements. The vertices form a quad. |
| 106 | private final FloatBuffer mVertexBuffer = createNativeFloatBuffer(8); |
| 107 | private final FloatBuffer mTexCoordBuffer = createNativeFloatBuffer(8); |
| 108 | |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 109 | /** |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 110 | * Animates an color fade warming up. |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 111 | */ |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 112 | public static final int MODE_WARM_UP = 0; |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 115 | * Animates an color fade shutting off. |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 116 | */ |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 117 | public static final int MODE_COOL_DOWN = 1; |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Animates a simple dim layer to fade the contents of the screen in or out progressively. |
| 121 | */ |
| 122 | public static final int MODE_FADE = 2; |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 123 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 124 | public ColorFade(int displayId) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 125 | mDisplayId = displayId; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 126 | mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 130 | * Warms up the color fade in preparation for turning on or off. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 131 | * This method prepares a GL context, and captures a screen shot. |
| 132 | * |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 133 | * @param mode The desired mode for the upcoming animation. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 134 | * @return True if the color fade is ready, false if it is uncontrollable. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 135 | */ |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 136 | public boolean prepare(Context context, int mode) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 137 | if (DEBUG) { |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 138 | Slog.d(TAG, "prepare: mode=" + mode); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 141 | mMode = mode; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 142 | |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 143 | // Get the display size and layer stack. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 144 | // This is not expected to change while the color fade surface is showing. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 145 | DisplayInfo displayInfo = mDisplayManagerInternal.getDisplayInfo(mDisplayId); |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 146 | mDisplayLayerStack = displayInfo.layerStack; |
| 147 | mDisplayWidth = displayInfo.getNaturalWidth(); |
| 148 | mDisplayHeight = displayInfo.getNaturalHeight(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 149 | |
| 150 | // Prepare the surface for drawing. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 151 | if (!(createSurface() && createEglContext() && createEglSurface() && |
| 152 | captureScreenshotTextureAndSetViewport())) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 153 | dismiss(); |
| 154 | return false; |
| 155 | } |
| 156 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 157 | // Init GL |
| 158 | if (!attachEglContext()) { |
| 159 | return false; |
| 160 | } |
| 161 | try { |
| 162 | if(!initGLShaders(context) || !initGLBuffers() || checkGlErrors("prepare")) { |
| 163 | detachEglContext(); |
| 164 | dismiss(); |
| 165 | return false; |
| 166 | } |
| 167 | } finally { |
| 168 | detachEglContext(); |
| 169 | } |
| 170 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 171 | // Done. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 172 | mPrepared = true; |
Jeff Brown | 78eb122 | 2012-10-10 18:27:44 -0700 | [diff] [blame] | 173 | |
| 174 | // Dejanking optimization. |
| 175 | // Some GL drivers can introduce a lot of lag in the first few frames as they |
| 176 | // initialize their state and allocate graphics buffers for rendering. |
| 177 | // Work around this problem by rendering the first frame of the animation a few |
| 178 | // times. The rest of the animation should run smoothly thereafter. |
| 179 | // The frames we draw here aren't visible because we are essentially just |
| 180 | // painting the screenshot as-is. |
| 181 | if (mode == MODE_COOL_DOWN) { |
| 182 | for (int i = 0; i < DEJANK_FRAMES; i++) { |
| 183 | draw(1.0f); |
| 184 | } |
| 185 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 186 | return true; |
| 187 | } |
| 188 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 189 | private String readFile(Context context, int resourceId) { |
| 190 | try{ |
| 191 | InputStream stream = context.getResources().openRawResource(resourceId); |
| 192 | return new String(Streams.readFully(new InputStreamReader(stream))); |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 193 | } |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 194 | catch (IOException e) { |
| 195 | Slog.e(TAG, "Unrecognized shader " + Integer.toString(resourceId)); |
| 196 | throw new RuntimeException(e); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private int loadShader(Context context, int resourceId, int type) { |
| 201 | String source = readFile(context, resourceId); |
| 202 | |
| 203 | int shader = GLES20.glCreateShader(type); |
| 204 | |
| 205 | GLES20.glShaderSource(shader, source); |
| 206 | GLES20.glCompileShader(shader); |
| 207 | |
| 208 | int[] compiled = new int[1]; |
| 209 | GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); |
| 210 | if (compiled[0] == 0) { |
| 211 | Slog.e(TAG, "Could not compile shader " + shader + ", " + type + ":"); |
| 212 | Slog.e(TAG, GLES20.glGetShaderSource(shader)); |
| 213 | Slog.e(TAG, GLES20.glGetShaderInfoLog(shader)); |
| 214 | GLES20.glDeleteShader(shader); |
| 215 | shader = 0; |
| 216 | } |
| 217 | |
| 218 | return shader; |
| 219 | } |
| 220 | |
| 221 | private boolean initGLShaders(Context context) { |
| 222 | int vshader = loadShader(context, com.android.internal.R.raw.color_fade_vert, |
| 223 | GLES20.GL_VERTEX_SHADER); |
| 224 | int fshader = loadShader(context, com.android.internal.R.raw.color_fade_frag, |
| 225 | GLES20.GL_FRAGMENT_SHADER); |
Michael Wright | 5018df7 | 2014-10-08 17:46:09 -0700 | [diff] [blame] | 226 | GLES20.glReleaseShaderCompiler(); |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 227 | if (vshader == 0 || fshader == 0) return false; |
| 228 | |
| 229 | mProgram = GLES20.glCreateProgram(); |
| 230 | |
| 231 | GLES20.glAttachShader(mProgram, vshader); |
| 232 | GLES20.glAttachShader(mProgram, fshader); |
Michael Wright | 5018df7 | 2014-10-08 17:46:09 -0700 | [diff] [blame] | 233 | GLES20.glDeleteShader(vshader); |
| 234 | GLES20.glDeleteShader(fshader); |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 235 | |
| 236 | GLES20.glLinkProgram(mProgram); |
| 237 | |
| 238 | mVertexLoc = GLES20.glGetAttribLocation(mProgram, "position"); |
| 239 | mTexCoordLoc = GLES20.glGetAttribLocation(mProgram, "uv"); |
| 240 | |
| 241 | mProjMatrixLoc = GLES20.glGetUniformLocation(mProgram, "proj_matrix"); |
| 242 | mTexMatrixLoc = GLES20.glGetUniformLocation(mProgram, "tex_matrix"); |
| 243 | |
| 244 | mOpacityLoc = GLES20.glGetUniformLocation(mProgram, "opacity"); |
| 245 | mGammaLoc = GLES20.glGetUniformLocation(mProgram, "gamma"); |
| 246 | mSaturationLoc = GLES20.glGetUniformLocation(mProgram, "saturation"); |
| 247 | mScaleLoc = GLES20.glGetUniformLocation(mProgram, "scale"); |
| 248 | mTexUnitLoc = GLES20.glGetUniformLocation(mProgram, "texUnit"); |
| 249 | |
| 250 | GLES20.glUseProgram(mProgram); |
| 251 | GLES20.glUniform1i(mTexUnitLoc, 0); |
| 252 | GLES20.glUseProgram(0); |
| 253 | |
| 254 | return true; |
| 255 | } |
| 256 | |
Michael Wright | 5018df7 | 2014-10-08 17:46:09 -0700 | [diff] [blame] | 257 | private void destroyGLShaders() { |
| 258 | GLES20.glDeleteProgram(mProgram); |
| 259 | checkGlErrors("glDeleteProgram"); |
| 260 | } |
| 261 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 262 | private boolean initGLBuffers() { |
| 263 | //Fill vertices |
| 264 | setQuad(mVertexBuffer, 0, 0, mDisplayWidth, mDisplayHeight); |
| 265 | |
| 266 | // Setup GL Textures |
| 267 | GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTexNames[0]); |
| 268 | GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, |
| 269 | GLES20.GL_NEAREST); |
| 270 | GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, |
| 271 | GLES20.GL_NEAREST); |
| 272 | GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, |
| 273 | GLES20.GL_CLAMP_TO_EDGE); |
| 274 | GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, |
| 275 | GLES20.GL_CLAMP_TO_EDGE); |
| 276 | GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0); |
| 277 | |
| 278 | // Setup GL Buffers |
| 279 | GLES20.glGenBuffers(2, mGLBuffers, 0); |
| 280 | |
| 281 | // fill vertex buffer |
| 282 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mGLBuffers[0]); |
| 283 | GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mVertexBuffer.capacity() * 4, |
| 284 | mVertexBuffer, GLES20.GL_STATIC_DRAW); |
| 285 | |
| 286 | // fill tex buffer |
| 287 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mGLBuffers[1]); |
| 288 | GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mTexCoordBuffer.capacity() * 4, |
| 289 | mTexCoordBuffer, GLES20.GL_STATIC_DRAW); |
| 290 | |
| 291 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
Michael Wright | 5018df7 | 2014-10-08 17:46:09 -0700 | [diff] [blame] | 296 | private void destroyGLBuffers() { |
| 297 | GLES20.glDeleteBuffers(2, mGLBuffers, 0); |
| 298 | checkGlErrors("glDeleteBuffers"); |
| 299 | } |
| 300 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 301 | private static void setQuad(FloatBuffer vtx, float x, float y, float w, float h) { |
| 302 | if (DEBUG) { |
| 303 | Slog.d(TAG, "setQuad: x=" + x + ", y=" + y + ", w=" + w + ", h=" + h); |
| 304 | } |
| 305 | vtx.put(0, x); |
| 306 | vtx.put(1, y); |
| 307 | vtx.put(2, x); |
| 308 | vtx.put(3, y + h); |
| 309 | vtx.put(4, x + w); |
| 310 | vtx.put(5, y + h); |
| 311 | vtx.put(6, x + w); |
| 312 | vtx.put(7, y); |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 315 | /** |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 316 | * Dismisses the color fade animation surface and cleans up. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 317 | * |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 318 | * To prevent stray photons from leaking out after the color fade has been |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 319 | * turned off, it is a good idea to defer dismissing the animation until the |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 320 | * color fade has been turned back on fully. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 321 | */ |
| 322 | public void dismiss() { |
| 323 | if (DEBUG) { |
| 324 | Slog.d(TAG, "dismiss"); |
| 325 | } |
| 326 | |
Michael Wright | 5018df7 | 2014-10-08 17:46:09 -0700 | [diff] [blame] | 327 | if (mPrepared) { |
| 328 | attachEglContext(); |
| 329 | try { |
| 330 | destroyScreenshotTexture(); |
| 331 | destroyGLShaders(); |
| 332 | destroyGLBuffers(); |
| 333 | destroyEglSurface(); |
| 334 | } finally { |
| 335 | detachEglContext(); |
| 336 | } |
| 337 | destroySurface(); |
| 338 | GLES20.glFlush(); |
| 339 | mPrepared = false; |
| 340 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 344 | * Draws an animation frame showing the color fade activated at the |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 345 | * specified level. |
| 346 | * |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 347 | * @param level The color fade level. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 348 | * @return True if successful. |
| 349 | */ |
| 350 | public boolean draw(float level) { |
| 351 | if (DEBUG) { |
| 352 | Slog.d(TAG, "drawFrame: level=" + level); |
| 353 | } |
| 354 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 355 | if (!mPrepared) { |
| 356 | return false; |
| 357 | } |
| 358 | |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 359 | if (mMode == MODE_FADE) { |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 360 | return showSurface(1.0f - level); |
| 361 | } |
| 362 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 363 | if (!attachEglContext()) { |
| 364 | return false; |
| 365 | } |
| 366 | try { |
| 367 | // Clear frame to solid black. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 368 | GLES20.glClearColor(0f, 0f, 0f, 1f); |
| 369 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 370 | |
| 371 | // Draw the frame. |
Neil Fuller | e573aa9 | 2015-02-11 15:49:47 +0000 | [diff] [blame] | 372 | double one_minus_level = 1 - level; |
| 373 | double cos = Math.cos(Math.PI * one_minus_level); |
| 374 | double sign = cos < 0 ? -1 : 1; |
| 375 | float opacity = (float) -Math.pow(one_minus_level, 2) + 1; |
| 376 | float saturation = (float) Math.pow(level, 4); |
| 377 | float scale = (float) ((-Math.pow(one_minus_level, 2) + 1) * 0.1d + 0.9d); |
| 378 | float gamma = (float) ((0.5d * sign * Math.pow(cos, 2) + 0.5d) * 0.9d + 0.1d); |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 379 | drawFaded(opacity, 1.f / gamma, saturation, scale); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 380 | if (checkGlErrors("drawFrame")) { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | EGL14.eglSwapBuffers(mEglDisplay, mEglSurface); |
| 385 | } finally { |
| 386 | detachEglContext(); |
| 387 | } |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 388 | return showSurface(1.0f); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 391 | private void drawFaded(float opacity, float gamma, float saturation, float scale) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 392 | if (DEBUG) { |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 393 | Slog.d(TAG, "drawFaded: opacity=" + opacity + ", gamma=" + gamma + |
| 394 | ", saturation=" + saturation + ", scale=" + scale); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 395 | } |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 396 | // Use shaders |
| 397 | GLES20.glUseProgram(mProgram); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 398 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 399 | // Set Uniforms |
| 400 | GLES20.glUniformMatrix4fv(mProjMatrixLoc, 1, false, mProjMatrix, 0); |
| 401 | GLES20.glUniformMatrix4fv(mTexMatrixLoc, 1, false, mTexMatrix, 0); |
| 402 | GLES20.glUniform1f(mOpacityLoc, opacity); |
| 403 | GLES20.glUniform1f(mGammaLoc, gamma); |
| 404 | GLES20.glUniform1f(mSaturationLoc, saturation); |
| 405 | GLES20.glUniform1f(mScaleLoc, scale); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 406 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 407 | // Use textures |
| 408 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0); |
| 409 | GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTexNames[0]); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 410 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 411 | // draw the plane |
| 412 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mGLBuffers[0]); |
| 413 | GLES20.glEnableVertexAttribArray(mVertexLoc); |
| 414 | GLES20.glVertexAttribPointer(mVertexLoc, 2, GLES20.GL_FLOAT, false, 0, 0); |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 415 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 416 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mGLBuffers[1]); |
| 417 | GLES20.glEnableVertexAttribArray(mTexCoordLoc); |
| 418 | GLES20.glVertexAttribPointer(mTexCoordLoc, 2, GLES20.GL_FLOAT, false, 0, 0); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 419 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 420 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 421 | |
| 422 | // clean up |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 423 | GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0); |
| 424 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 427 | private void ortho(float left, float right, float bottom, float top, float znear, float zfar) { |
| 428 | mProjMatrix[0] = 2f / (right - left); |
| 429 | mProjMatrix[1] = 0; |
| 430 | mProjMatrix[2] = 0; |
| 431 | mProjMatrix[3] = 0; |
| 432 | mProjMatrix[4] = 0; |
| 433 | mProjMatrix[5] = 2f / (top - bottom); |
| 434 | mProjMatrix[6] = 0; |
| 435 | mProjMatrix[7] = 0; |
| 436 | mProjMatrix[8] = 0; |
| 437 | mProjMatrix[9] = 0; |
| 438 | mProjMatrix[10] = -2f / (zfar - znear); |
| 439 | mProjMatrix[11] = 0; |
| 440 | mProjMatrix[12] = -(right + left) / (right - left); |
| 441 | mProjMatrix[13] = -(top + bottom) / (top - bottom); |
| 442 | mProjMatrix[14] = -(zfar + znear) / (zfar - znear); |
| 443 | mProjMatrix[15] = 1f; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | private boolean captureScreenshotTextureAndSetViewport() { |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 447 | if (!attachEglContext()) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 448 | return false; |
| 449 | } |
| 450 | try { |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 451 | if (!mTexNamesGenerated) { |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 452 | GLES20.glGenTextures(1, mTexNames, 0); |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 453 | if (checkGlErrors("glGenTextures")) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 454 | return false; |
| 455 | } |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 456 | mTexNamesGenerated = true; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 457 | } |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 458 | |
Jeff Sharkey | 21ef964 | 2013-04-25 11:34:23 -0700 | [diff] [blame] | 459 | final SurfaceTexture st = new SurfaceTexture(mTexNames[0]); |
| 460 | final Surface s = new Surface(st); |
| 461 | try { |
| 462 | SurfaceControl.screenshot(SurfaceControl.getBuiltInDisplay( |
| 463 | SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN), s); |
Michael Wright | 2ed0513 | 2015-01-08 14:30:47 -0800 | [diff] [blame] | 464 | st.updateTexImage(); |
| 465 | st.getTransformMatrix(mTexMatrix); |
Jeff Sharkey | 21ef964 | 2013-04-25 11:34:23 -0700 | [diff] [blame] | 466 | } finally { |
| 467 | s.release(); |
Michael Wright | 2ed0513 | 2015-01-08 14:30:47 -0800 | [diff] [blame] | 468 | st.release(); |
Jeff Sharkey | 21ef964 | 2013-04-25 11:34:23 -0700 | [diff] [blame] | 469 | } |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 470 | |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 471 | // Set up texture coordinates for a quad. |
| 472 | // We might need to change this if the texture ends up being |
| 473 | // a different size from the display for some reason. |
| 474 | mTexCoordBuffer.put(0, 0f); mTexCoordBuffer.put(1, 0f); |
| 475 | mTexCoordBuffer.put(2, 0f); mTexCoordBuffer.put(3, 1f); |
| 476 | mTexCoordBuffer.put(4, 1f); mTexCoordBuffer.put(5, 1f); |
| 477 | mTexCoordBuffer.put(6, 1f); mTexCoordBuffer.put(7, 0f); |
| 478 | |
| 479 | // Set up our viewport. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 480 | GLES20.glViewport(0, 0, mDisplayWidth, mDisplayHeight); |
| 481 | ortho(0, mDisplayWidth, 0, mDisplayHeight, -1, 1); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 482 | } finally { |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 483 | detachEglContext(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 484 | } |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | private void destroyScreenshotTexture() { |
| 489 | if (mTexNamesGenerated) { |
| 490 | mTexNamesGenerated = false; |
Michael Wright | 5018df7 | 2014-10-08 17:46:09 -0700 | [diff] [blame] | 491 | GLES20.glDeleteTextures(1, mTexNames, 0); |
| 492 | checkGlErrors("glDeleteTextures"); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
| 496 | private boolean createEglContext() { |
| 497 | if (mEglDisplay == null) { |
| 498 | mEglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); |
| 499 | if (mEglDisplay == EGL14.EGL_NO_DISPLAY) { |
| 500 | logEglError("eglGetDisplay"); |
| 501 | return false; |
| 502 | } |
| 503 | |
| 504 | int[] version = new int[2]; |
| 505 | if (!EGL14.eglInitialize(mEglDisplay, version, 0, version, 1)) { |
| 506 | mEglDisplay = null; |
| 507 | logEglError("eglInitialize"); |
| 508 | return false; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | if (mEglConfig == null) { |
| 513 | int[] eglConfigAttribList = new int[] { |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 514 | EGL14.EGL_RENDERABLE_TYPE, |
| 515 | EGL14.EGL_OPENGL_ES2_BIT, |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 516 | EGL14.EGL_RED_SIZE, 8, |
| 517 | EGL14.EGL_GREEN_SIZE, 8, |
| 518 | EGL14.EGL_BLUE_SIZE, 8, |
| 519 | EGL14.EGL_ALPHA_SIZE, 8, |
| 520 | EGL14.EGL_NONE |
| 521 | }; |
| 522 | int[] numEglConfigs = new int[1]; |
| 523 | EGLConfig[] eglConfigs = new EGLConfig[1]; |
| 524 | if (!EGL14.eglChooseConfig(mEglDisplay, eglConfigAttribList, 0, |
| 525 | eglConfigs, 0, eglConfigs.length, numEglConfigs, 0)) { |
| 526 | logEglError("eglChooseConfig"); |
| 527 | return false; |
| 528 | } |
| 529 | mEglConfig = eglConfigs[0]; |
| 530 | } |
| 531 | |
| 532 | if (mEglContext == null) { |
| 533 | int[] eglContextAttribList = new int[] { |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 534 | EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 535 | EGL14.EGL_NONE |
| 536 | }; |
| 537 | mEglContext = EGL14.eglCreateContext(mEglDisplay, mEglConfig, |
| 538 | EGL14.EGL_NO_CONTEXT, eglContextAttribList, 0); |
| 539 | if (mEglContext == null) { |
| 540 | logEglError("eglCreateContext"); |
| 541 | return false; |
| 542 | } |
| 543 | } |
| 544 | return true; |
| 545 | } |
| 546 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 547 | private boolean createSurface() { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 548 | if (mSurfaceSession == null) { |
| 549 | mSurfaceSession = new SurfaceSession(); |
| 550 | } |
| 551 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 552 | SurfaceControl.openTransaction(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 553 | try { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 554 | if (mSurfaceControl == null) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 555 | try { |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 556 | int flags; |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 557 | if (mMode == MODE_FADE) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 558 | flags = SurfaceControl.FX_SURFACE_DIM | SurfaceControl.HIDDEN; |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 559 | } else { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 560 | flags = SurfaceControl.OPAQUE | SurfaceControl.HIDDEN; |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 561 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 562 | mSurfaceControl = new SurfaceControl(mSurfaceSession, |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 563 | "ColorFade", mDisplayWidth, mDisplayHeight, |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 564 | PixelFormat.OPAQUE, flags); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 565 | } catch (OutOfResourcesException ex) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 566 | Slog.e(TAG, "Unable to create surface.", ex); |
| 567 | return false; |
| 568 | } |
| 569 | } |
| 570 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 571 | mSurfaceControl.setLayerStack(mDisplayLayerStack); |
| 572 | mSurfaceControl.setSize(mDisplayWidth, mDisplayHeight); |
Mathias Agopian | 86e1bc7 | 2013-03-13 17:48:22 -0700 | [diff] [blame] | 573 | mSurface = new Surface(); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 574 | mSurface.copyFrom(mSurfaceControl); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 575 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 576 | mSurfaceLayout = new NaturalSurfaceLayout(mDisplayManagerInternal, |
| 577 | mDisplayId, mSurfaceControl); |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 578 | mSurfaceLayout.onDisplayTransaction(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 579 | } finally { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 580 | SurfaceControl.closeTransaction(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 581 | } |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 582 | return true; |
| 583 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 584 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 585 | private boolean createEglSurface() { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 586 | if (mEglSurface == null) { |
| 587 | int[] eglSurfaceAttribList = new int[] { |
| 588 | EGL14.EGL_NONE |
| 589 | }; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 590 | // turn our SurfaceControl into a Surface |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 591 | mEglSurface = EGL14.eglCreateWindowSurface(mEglDisplay, mEglConfig, mSurface, |
| 592 | eglSurfaceAttribList, 0); |
| 593 | if (mEglSurface == null) { |
| 594 | logEglError("eglCreateWindowSurface"); |
| 595 | return false; |
| 596 | } |
| 597 | } |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | private void destroyEglSurface() { |
| 602 | if (mEglSurface != null) { |
| 603 | if (!EGL14.eglDestroySurface(mEglDisplay, mEglSurface)) { |
| 604 | logEglError("eglDestroySurface"); |
| 605 | } |
| 606 | mEglSurface = null; |
| 607 | } |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 608 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 609 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 610 | private void destroySurface() { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 611 | if (mSurfaceControl != null) { |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 612 | mSurfaceLayout.dispose(); |
| 613 | mSurfaceLayout = null; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 614 | SurfaceControl.openTransaction(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 615 | try { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 616 | mSurfaceControl.destroy(); |
| 617 | mSurface.release(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 618 | } finally { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 619 | SurfaceControl.closeTransaction(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 620 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 621 | mSurfaceControl = null; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 622 | mSurfaceVisible = false; |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 623 | mSurfaceAlpha = 0f; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 627 | private boolean showSurface(float alpha) { |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 628 | if (!mSurfaceVisible || mSurfaceAlpha != alpha) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 629 | SurfaceControl.openTransaction(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 630 | try { |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 631 | mSurfaceControl.setLayer(COLOR_FADE_LAYER); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 632 | mSurfaceControl.setAlpha(alpha); |
| 633 | mSurfaceControl.show(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 634 | } finally { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 635 | SurfaceControl.closeTransaction(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 636 | } |
| 637 | mSurfaceVisible = true; |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 638 | mSurfaceAlpha = alpha; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 639 | } |
| 640 | return true; |
| 641 | } |
| 642 | |
| 643 | private boolean attachEglContext() { |
| 644 | if (mEglSurface == null) { |
| 645 | return false; |
| 646 | } |
| 647 | if (!EGL14.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) { |
| 648 | logEglError("eglMakeCurrent"); |
| 649 | return false; |
| 650 | } |
| 651 | return true; |
| 652 | } |
| 653 | |
| 654 | private void detachEglContext() { |
| 655 | if (mEglDisplay != null) { |
| 656 | EGL14.eglMakeCurrent(mEglDisplay, |
| 657 | EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT); |
| 658 | } |
| 659 | } |
| 660 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 661 | private static FloatBuffer createNativeFloatBuffer(int size) { |
| 662 | ByteBuffer bb = ByteBuffer.allocateDirect(size * 4); |
| 663 | bb.order(ByteOrder.nativeOrder()); |
| 664 | return bb.asFloatBuffer(); |
| 665 | } |
| 666 | |
| 667 | private static void logEglError(String func) { |
| 668 | Slog.e(TAG, func + " failed: error " + EGL14.eglGetError(), new Throwable()); |
| 669 | } |
| 670 | |
| 671 | private static boolean checkGlErrors(String func) { |
| 672 | return checkGlErrors(func, true); |
| 673 | } |
| 674 | |
| 675 | private static boolean checkGlErrors(String func, boolean log) { |
| 676 | boolean hadError = false; |
| 677 | int error; |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 678 | while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 679 | if (log) { |
| 680 | Slog.e(TAG, func + " failed: error " + error, new Throwable()); |
| 681 | } |
| 682 | hadError = true; |
| 683 | } |
| 684 | return hadError; |
| 685 | } |
| 686 | |
| 687 | public void dump(PrintWriter pw) { |
| 688 | pw.println(); |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 689 | pw.println("Color Fade State:"); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 690 | pw.println(" mPrepared=" + mPrepared); |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 691 | pw.println(" mMode=" + mMode); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 692 | pw.println(" mDisplayLayerStack=" + mDisplayLayerStack); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 693 | pw.println(" mDisplayWidth=" + mDisplayWidth); |
| 694 | pw.println(" mDisplayHeight=" + mDisplayHeight); |
| 695 | pw.println(" mSurfaceVisible=" + mSurfaceVisible); |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 696 | pw.println(" mSurfaceAlpha=" + mSurfaceAlpha); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 697 | } |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 698 | |
| 699 | /** |
| 700 | * Keeps a surface aligned with the natural orientation of the device. |
| 701 | * Updates the position and transformation of the matrix whenever the display |
| 702 | * is rotated. This is a little tricky because the display transaction |
| 703 | * callback can be invoked on any thread, not necessarily the thread that |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 704 | * owns the color fade. |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 705 | */ |
| 706 | private static final class NaturalSurfaceLayout implements DisplayTransactionListener { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 707 | private final DisplayManagerInternal mDisplayManagerInternal; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 708 | private final int mDisplayId; |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 709 | private SurfaceControl mSurfaceControl; |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 710 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 711 | public NaturalSurfaceLayout(DisplayManagerInternal displayManagerInternal, |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 712 | int displayId, SurfaceControl surfaceControl) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 713 | mDisplayManagerInternal = displayManagerInternal; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 714 | mDisplayId = displayId; |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 715 | mSurfaceControl = surfaceControl; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 716 | mDisplayManagerInternal.registerDisplayTransactionListener(this); |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | public void dispose() { |
| 720 | synchronized (this) { |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 721 | mSurfaceControl = null; |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 722 | } |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 723 | mDisplayManagerInternal.unregisterDisplayTransactionListener(this); |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | @Override |
| 727 | public void onDisplayTransaction() { |
| 728 | synchronized (this) { |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 729 | if (mSurfaceControl == null) { |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 730 | return; |
| 731 | } |
| 732 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 733 | DisplayInfo displayInfo = mDisplayManagerInternal.getDisplayInfo(mDisplayId); |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 734 | switch (displayInfo.rotation) { |
| 735 | case Surface.ROTATION_0: |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 736 | mSurfaceControl.setPosition(0, 0); |
| 737 | mSurfaceControl.setMatrix(1, 0, 0, 1); |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 738 | break; |
| 739 | case Surface.ROTATION_90: |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 740 | mSurfaceControl.setPosition(0, displayInfo.logicalHeight); |
| 741 | mSurfaceControl.setMatrix(0, -1, 1, 0); |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 742 | break; |
| 743 | case Surface.ROTATION_180: |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 744 | mSurfaceControl.setPosition(displayInfo.logicalWidth, |
| 745 | displayInfo.logicalHeight); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 746 | mSurfaceControl.setMatrix(-1, 0, 0, -1); |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 747 | break; |
| 748 | case Surface.ROTATION_270: |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 749 | mSurfaceControl.setPosition(displayInfo.logicalWidth, 0); |
| 750 | mSurfaceControl.setMatrix(0, 1, -1, 0); |
Jeff Brown | 7f3994e | 2012-12-04 14:04:28 -0800 | [diff] [blame] | 751 | break; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 756 | } |