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