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