Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 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 | |
| 17 | |
| 18 | package android.view; |
| 19 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 20 | import android.graphics.Canvas; |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 21 | import android.graphics.Paint; |
| 22 | import android.graphics.Rect; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 23 | import android.graphics.SurfaceTexture; |
Romain Guy | aaceeb0 | 2011-03-23 19:56:13 -0700 | [diff] [blame] | 24 | import android.os.*; |
Romain Guy | 9a40bab | 2010-09-23 16:32:47 -0700 | [diff] [blame] | 25 | import android.util.EventLog; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 26 | import android.util.Log; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 27 | |
| 28 | import javax.microedition.khronos.egl.EGL10; |
| 29 | import javax.microedition.khronos.egl.EGL11; |
| 30 | import javax.microedition.khronos.egl.EGLConfig; |
| 31 | import javax.microedition.khronos.egl.EGLContext; |
| 32 | import javax.microedition.khronos.egl.EGLDisplay; |
| 33 | import javax.microedition.khronos.egl.EGLSurface; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 34 | import javax.microedition.khronos.opengles.GL; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 37 | * Interface for rendering a ViewAncestor using hardware acceleration. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 38 | * |
| 39 | * @hide |
| 40 | */ |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 41 | public abstract class HardwareRenderer { |
Romain Guy | 4f6aff3 | 2011-01-12 16:21:41 -0800 | [diff] [blame] | 42 | static final String LOG_TAG = "HardwareRenderer"; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 43 | |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 44 | /** |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 45 | * Turn on to only refresh the parts of the screen that need updating. |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 46 | * When turned on the property defined by {@link #RENDER_DIRTY_REGIONS_PROPERTY} |
| 47 | * must also have the value "true". |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 48 | */ |
| 49 | public static final boolean RENDER_DIRTY_REGIONS = true; |
| 50 | |
| 51 | /** |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 52 | * System property used to enable or disable dirty regions invalidation. |
| 53 | * This property is only queried if {@link #RENDER_DIRTY_REGIONS} is true. |
| 54 | * The default value of this property is assumed to be true. |
| 55 | * |
| 56 | * Possible values: |
| 57 | * "true", to enable partial invalidates |
| 58 | * "false", to disable partial invalidates |
| 59 | */ |
| 60 | static final String RENDER_DIRTY_REGIONS_PROPERTY = "hwui.render_dirty_regions"; |
| 61 | |
| 62 | /** |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 63 | * Turn on to draw dirty regions every other frame. |
| 64 | */ |
| 65 | private static final boolean DEBUG_DIRTY_REGION = false; |
| 66 | |
| 67 | /** |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 68 | * A process can set this flag to false to prevent the use of hardware |
| 69 | * rendering. |
| 70 | * |
| 71 | * @hide |
| 72 | */ |
| 73 | public static boolean sRendererDisabled = false; |
| 74 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 75 | private boolean mEnabled; |
| 76 | private boolean mRequested = true; |
| 77 | |
| 78 | /** |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 79 | * Invoke this method to disable hardware rendering in the current process. |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 80 | * |
| 81 | * @hide |
| 82 | */ |
| 83 | public static void disable() { |
| 84 | sRendererDisabled = true; |
| 85 | } |
| 86 | |
| 87 | /** |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 88 | * Indicates whether hardware acceleration is available under any form for |
| 89 | * the view hierarchy. |
| 90 | * |
| 91 | * @return True if the view hierarchy can potentially be hardware accelerated, |
| 92 | * false otherwise |
| 93 | */ |
| 94 | public static boolean isAvailable() { |
| 95 | return GLES20Canvas.isAvailable(); |
| 96 | } |
| 97 | |
| 98 | /** |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 99 | * Destroys the hardware rendering context. |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 100 | * |
| 101 | * @param full If true, destroys all associated resources. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 102 | */ |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 103 | abstract void destroy(boolean full); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Initializes the hardware renderer for the specified surface. |
| 107 | * |
| 108 | * @param holder The holder for the surface to hardware accelerate. |
| 109 | * |
| 110 | * @return True if the initialization was successful, false otherwise. |
| 111 | */ |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 112 | abstract boolean initialize(SurfaceHolder holder) throws Surface.OutOfResourcesException; |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Updates the hardware renderer for the specified surface. |
| 116 | * |
| 117 | * @param holder The holder for the surface to hardware accelerate. |
| 118 | */ |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 119 | abstract void updateSurface(SurfaceHolder holder) throws Surface.OutOfResourcesException; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Setup the hardware renderer for drawing. This is called for every |
| 123 | * frame to draw. |
| 124 | * |
| 125 | * @param width Width of the drawing surface. |
| 126 | * @param height Height of the drawing surface. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 127 | */ |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 128 | abstract void setup(int width, int height); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 129 | |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 130 | /** |
| 131 | * Interface used to receive callbacks whenever a view is drawn by |
| 132 | * a hardware renderer instance. |
| 133 | */ |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 134 | interface HardwareDrawCallbacks { |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Invoked before a view is drawn by a hardware renderer. |
| 137 | * |
| 138 | * @param canvas The Canvas used to render the view. |
| 139 | */ |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 140 | void onHardwarePreDraw(Canvas canvas); |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Invoked after a view is drawn by a hardware renderer. |
| 144 | * |
| 145 | * @param canvas The Canvas used to render the view. |
| 146 | */ |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 147 | void onHardwarePostDraw(Canvas canvas); |
| 148 | } |
| 149 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Draws the specified view. |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 152 | * |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 153 | * @param view The view to draw. |
| 154 | * @param attachInfo AttachInfo tied to the specified view. |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 155 | * @param callbacks Callbacks invoked when drawing happens. |
| 156 | * @param dirty The dirty rectangle to update, can be null. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 157 | */ |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 158 | abstract void draw(View view, View.AttachInfo attachInfo, HardwareDrawCallbacks callbacks, |
| 159 | Rect dirty); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
Romain Guy | 53ca03d | 2010-10-08 18:55:27 -0700 | [diff] [blame] | 162 | * Creates a new display list that can be used to record batches of |
| 163 | * drawing operations. |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 164 | * |
Romain Guy | 53ca03d | 2010-10-08 18:55:27 -0700 | [diff] [blame] | 165 | * @return A new display list. |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 166 | */ |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 167 | abstract DisplayList createDisplayList(View v); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 170 | * Creates a new hardware layer. A hardware layer built by calling this |
| 171 | * method will be treated as a texture layer, instead of as a render target. |
| 172 | * |
| 173 | * @return A hardware layer |
| 174 | */ |
| 175 | abstract HardwareLayer createHardwareLayer(); |
| 176 | |
| 177 | /** |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 178 | * Creates a new hardware layer. |
| 179 | * |
| 180 | * @param width The minimum width of the layer |
| 181 | * @param height The minimum height of the layer |
| 182 | * @param isOpaque Whether the layer should be opaque or not |
| 183 | * |
| 184 | * @return A hardware layer |
| 185 | */ |
| 186 | abstract HardwareLayer createHardwareLayer(int width, int height, boolean isOpaque); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * Creates a new {@link SurfaceTexture} that can be used to render into the |
| 190 | * specified hardware layer. |
| 191 | * |
| 192 | * |
| 193 | * @param layer The layer to render into using a {@link android.graphics.SurfaceTexture} |
| 194 | * |
| 195 | * @return A {@link SurfaceTexture} |
| 196 | */ |
| 197 | abstract SurfaceTexture createSuraceTexture(HardwareLayer layer); |
| 198 | |
| 199 | /** |
| 200 | * Updates the specified layer. |
| 201 | * |
| 202 | * @param layer The hardware layer to update |
| 203 | * @param width The layer's width |
| 204 | * @param height The layer's height |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 205 | * @param surface The surface to update |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 206 | */ |
| 207 | abstract void updateTextureLayer(HardwareLayer layer, int width, int height, |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 208 | SurfaceTexture surface); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 209 | |
| 210 | /** |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 211 | * Initializes the hardware renderer for the specified surface and setup the |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 212 | * renderer for drawing, if needed. This is invoked when the ViewAncestor has |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 213 | * potentially lost the hardware renderer. The hardware renderer should be |
| 214 | * reinitialized and setup when the render {@link #isRequested()} and |
| 215 | * {@link #isEnabled()}. |
| 216 | * |
| 217 | * @param width The width of the drawing surface. |
| 218 | * @param height The height of the drawing surface. |
| 219 | * @param attachInfo The |
| 220 | * @param holder |
| 221 | */ |
| 222 | void initializeIfNeeded(int width, int height, View.AttachInfo attachInfo, |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 223 | SurfaceHolder holder) throws Surface.OutOfResourcesException { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 224 | if (isRequested()) { |
| 225 | // We lost the gl context, so recreate it. |
| 226 | if (!isEnabled()) { |
| 227 | if (initialize(holder)) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 228 | setup(width, height); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Creates a hardware renderer using OpenGL. |
| 236 | * |
| 237 | * @param glVersion The version of OpenGL to use (1 for OpenGL 1, 11 for OpenGL 1.1, etc.) |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 238 | * @param translucent True if the surface is translucent, false otherwise |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 239 | * |
| 240 | * @return A hardware renderer backed by OpenGL. |
| 241 | */ |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 242 | static HardwareRenderer createGlRenderer(int glVersion, boolean translucent) { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 243 | switch (glVersion) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 244 | case 2: |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 245 | return Gl20Renderer.create(translucent); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 246 | } |
| 247 | throw new IllegalArgumentException("Unknown GL version: " + glVersion); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Indicates whether hardware acceleration is currently enabled. |
| 252 | * |
| 253 | * @return True if hardware acceleration is in use, false otherwise. |
| 254 | */ |
| 255 | boolean isEnabled() { |
| 256 | return mEnabled; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Indicates whether hardware acceleration is currently enabled. |
| 261 | * |
| 262 | * @param enabled True if the hardware renderer is in use, false otherwise. |
| 263 | */ |
| 264 | void setEnabled(boolean enabled) { |
| 265 | mEnabled = enabled; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Indicates whether hardware acceleration is currently request but not |
| 270 | * necessarily enabled yet. |
| 271 | * |
| 272 | * @return True if requested, false otherwise. |
| 273 | */ |
| 274 | boolean isRequested() { |
| 275 | return mRequested; |
| 276 | } |
| 277 | |
| 278 | /** |
Romain Guy | 9745fae | 2010-12-08 11:39:15 -0800 | [diff] [blame] | 279 | * Indicates whether hardware acceleration is currently requested but not |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 280 | * necessarily enabled yet. |
| 281 | * |
| 282 | * @return True to request hardware acceleration, false otherwise. |
| 283 | */ |
| 284 | void setRequested(boolean requested) { |
| 285 | mRequested = requested; |
| 286 | } |
| 287 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 288 | @SuppressWarnings({"deprecation"}) |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 289 | static abstract class GlRenderer extends HardwareRenderer { |
Romain Guy | aaceeb0 | 2011-03-23 19:56:13 -0700 | [diff] [blame] | 290 | // These values are not exposed in our EGL APIs |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 291 | static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098; |
| 292 | static final int EGL_SURFACE_TYPE = 0x3033; |
| 293 | static final int EGL_SWAP_BEHAVIOR_PRESERVED_BIT = 0x0400; |
| 294 | static final int EGL_OPENGL_ES2_BIT = 4; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 295 | |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 296 | private static final int SURFACE_STATE_ERROR = 0; |
| 297 | private static final int SURFACE_STATE_SUCCESS = 1; |
| 298 | private static final int SURFACE_STATE_UPDATED = 2; |
| 299 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 300 | static EGLContext sEglContext; |
| 301 | static EGL10 sEgl; |
| 302 | static EGLDisplay sEglDisplay; |
| 303 | static EGLConfig sEglConfig; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 304 | |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 305 | private static Thread sEglThread; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 306 | |
| 307 | EGLSurface mEglSurface; |
| 308 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 309 | GL mGl; |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 310 | HardwareCanvas mCanvas; |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 311 | int mFrameCount; |
| 312 | Paint mDebugPaint; |
| 313 | |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 314 | boolean mDirtyRegions; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 315 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 316 | final int mGlVersion; |
| 317 | final boolean mTranslucent; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 318 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 319 | private boolean mDestroyed; |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 320 | |
| 321 | private final Rect mRedrawClip = new Rect(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 322 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 323 | GlRenderer(int glVersion, boolean translucent) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 324 | mGlVersion = glVersion; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 325 | mTranslucent = translucent; |
Romain Guy | aaceeb0 | 2011-03-23 19:56:13 -0700 | [diff] [blame] | 326 | final String dirtyProperty = SystemProperties.get(RENDER_DIRTY_REGIONS_PROPERTY, "true"); |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 327 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 328 | mDirtyRegions = RENDER_DIRTY_REGIONS && "true".equalsIgnoreCase(dirtyProperty); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 331 | /** |
Mike Dodd | 5d3e2ea | 2010-10-07 18:02:47 -0700 | [diff] [blame] | 332 | * Return a string for the EGL error code, or the hex representation |
Romain Guy | d10cd57 | 2010-10-10 13:33:22 -0700 | [diff] [blame] | 333 | * if the error is unknown. |
| 334 | * |
| 335 | * @param error The EGL error to convert into a String. |
| 336 | * |
| 337 | * @return An error string correponding to the EGL error code. |
Mike Dodd | 5d3e2ea | 2010-10-07 18:02:47 -0700 | [diff] [blame] | 338 | */ |
| 339 | static String getEGLErrorString(int error) { |
| 340 | switch (error) { |
| 341 | case EGL10.EGL_SUCCESS: |
| 342 | return "EGL_SUCCESS"; |
| 343 | case EGL10.EGL_NOT_INITIALIZED: |
| 344 | return "EGL_NOT_INITIALIZED"; |
| 345 | case EGL10.EGL_BAD_ACCESS: |
| 346 | return "EGL_BAD_ACCESS"; |
| 347 | case EGL10.EGL_BAD_ALLOC: |
| 348 | return "EGL_BAD_ALLOC"; |
| 349 | case EGL10.EGL_BAD_ATTRIBUTE: |
| 350 | return "EGL_BAD_ATTRIBUTE"; |
| 351 | case EGL10.EGL_BAD_CONFIG: |
| 352 | return "EGL_BAD_CONFIG"; |
| 353 | case EGL10.EGL_BAD_CONTEXT: |
| 354 | return "EGL_BAD_CONTEXT"; |
| 355 | case EGL10.EGL_BAD_CURRENT_SURFACE: |
| 356 | return "EGL_BAD_CURRENT_SURFACE"; |
| 357 | case EGL10.EGL_BAD_DISPLAY: |
| 358 | return "EGL_BAD_DISPLAY"; |
| 359 | case EGL10.EGL_BAD_MATCH: |
| 360 | return "EGL_BAD_MATCH"; |
| 361 | case EGL10.EGL_BAD_NATIVE_PIXMAP: |
| 362 | return "EGL_BAD_NATIVE_PIXMAP"; |
| 363 | case EGL10.EGL_BAD_NATIVE_WINDOW: |
| 364 | return "EGL_BAD_NATIVE_WINDOW"; |
| 365 | case EGL10.EGL_BAD_PARAMETER: |
| 366 | return "EGL_BAD_PARAMETER"; |
| 367 | case EGL10.EGL_BAD_SURFACE: |
| 368 | return "EGL_BAD_SURFACE"; |
| 369 | case EGL11.EGL_CONTEXT_LOST: |
| 370 | return "EGL_CONTEXT_LOST"; |
| 371 | default: |
| 372 | return "0x" + Integer.toHexString(error); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 377 | * Checks for OpenGL errors. If an error has occured, {@link #destroy(boolean)} |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 378 | * is invoked and the requested flag is turned off. The error code is |
| 379 | * also logged as a warning. |
| 380 | */ |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 381 | void checkEglErrors() { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 382 | if (isEnabled()) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 383 | int error = sEgl.eglGetError(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 384 | if (error != EGL10.EGL_SUCCESS) { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 385 | // something bad has happened revert to |
| 386 | // normal rendering. |
Romain Guy | 9745fae | 2010-12-08 11:39:15 -0800 | [diff] [blame] | 387 | fallback(error != EGL11.EGL_CONTEXT_LOST); |
Mike Dodd | 5d3e2ea | 2010-10-07 18:02:47 -0700 | [diff] [blame] | 388 | Log.w(LOG_TAG, "EGL error: " + getEGLErrorString(error)); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | } |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 392 | |
Romain Guy | 9745fae | 2010-12-08 11:39:15 -0800 | [diff] [blame] | 393 | private void fallback(boolean fallback) { |
| 394 | destroy(true); |
| 395 | if (fallback) { |
| 396 | // we'll try again if it was context lost |
| 397 | setRequested(false); |
| 398 | Log.w(LOG_TAG, "Mountain View, we've had a problem here. " |
| 399 | + "Switching back to software rendering."); |
| 400 | } |
| 401 | } |
| 402 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 403 | @Override |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 404 | boolean initialize(SurfaceHolder holder) throws Surface.OutOfResourcesException { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 405 | if (isRequested() && !isEnabled()) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 406 | initializeEgl(); |
| 407 | mGl = createEglSurface(holder); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 408 | mDestroyed = false; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 409 | |
| 410 | if (mGl != null) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 411 | int err = sEgl.eglGetError(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 412 | if (err != EGL10.EGL_SUCCESS) { |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 413 | destroy(true); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 414 | setRequested(false); |
| 415 | } else { |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 416 | if (mCanvas == null) { |
| 417 | mCanvas = createCanvas(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 418 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 419 | if (mCanvas != null) { |
| 420 | setEnabled(true); |
| 421 | } else { |
| 422 | Log.w(LOG_TAG, "Hardware accelerated Canvas could not be created"); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return mCanvas != null; |
| 427 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 428 | } |
| 429 | return false; |
| 430 | } |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 431 | |
| 432 | @Override |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 433 | void updateSurface(SurfaceHolder holder) throws Surface.OutOfResourcesException { |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 434 | if (isRequested() && isEnabled()) { |
| 435 | createEglSurface(holder); |
| 436 | } |
| 437 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 438 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 439 | abstract GLES20Canvas createCanvas(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 440 | |
| 441 | void initializeEgl() { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 442 | if (sEglContext != null) return; |
| 443 | |
| 444 | sEglThread = Thread.currentThread(); |
| 445 | sEgl = (EGL10) EGLContext.getEGL(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 446 | |
| 447 | // Get to the default display. |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 448 | sEglDisplay = sEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 449 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 450 | if (sEglDisplay == EGL10.EGL_NO_DISPLAY) { |
Mike Dodd | 5d3e2ea | 2010-10-07 18:02:47 -0700 | [diff] [blame] | 451 | throw new RuntimeException("eglGetDisplay failed " |
| 452 | + getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // We can now initialize EGL for that display |
| 456 | int[] version = new int[2]; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 457 | if (!sEgl.eglInitialize(sEglDisplay, version)) { |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 458 | throw new RuntimeException("eglInitialize failed " + |
| 459 | getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 460 | } |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 461 | |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 462 | sEglConfig = chooseEglConfig(); |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 463 | if (sEglConfig == null) { |
| 464 | // We tried to use EGL_SWAP_BEHAVIOR_PRESERVED_BIT, try again without |
| 465 | if (mDirtyRegions) { |
| 466 | mDirtyRegions = false; |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 467 | sEglConfig = chooseEglConfig(); |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 468 | if (sEglConfig == null) { |
| 469 | throw new RuntimeException("eglConfig not initialized"); |
| 470 | } |
| 471 | } else { |
| 472 | throw new RuntimeException("eglConfig not initialized"); |
| 473 | } |
| 474 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 475 | |
| 476 | /* |
| 477 | * Create an EGL context. We want to do this as rarely as we can, because an |
| 478 | * EGL context is a somewhat heavy object. |
| 479 | */ |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 480 | sEglContext = createContext(sEgl, sEglDisplay, sEglConfig); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 483 | private EGLConfig chooseEglConfig() { |
| 484 | int[] configsCount = new int[1]; |
| 485 | EGLConfig[] configs = new EGLConfig[1]; |
| 486 | int[] configSpec = getConfig(mDirtyRegions); |
| 487 | if (!sEgl.eglChooseConfig(sEglDisplay, configSpec, configs, 1, configsCount)) { |
| 488 | throw new IllegalArgumentException("eglChooseConfig failed " + |
| 489 | getEGLErrorString(sEgl.eglGetError())); |
| 490 | } else if (configsCount[0] > 0) { |
| 491 | return configs[0]; |
| 492 | } |
| 493 | return null; |
| 494 | } |
| 495 | |
| 496 | abstract int[] getConfig(boolean dirtyRegions); |
| 497 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 498 | GL createEglSurface(SurfaceHolder holder) throws Surface.OutOfResourcesException { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 499 | // Check preconditions. |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 500 | if (sEgl == null) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 501 | throw new RuntimeException("egl not initialized"); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 502 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 503 | if (sEglDisplay == null) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 504 | throw new RuntimeException("eglDisplay not initialized"); |
| 505 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 506 | if (sEglConfig == null) { |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 507 | throw new RuntimeException("eglConfig not initialized"); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 508 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 509 | if (Thread.currentThread() != sEglThread) { |
| 510 | throw new IllegalStateException("HardwareRenderer cannot be used " |
| 511 | + "from multiple threads"); |
| 512 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 513 | |
| 514 | /* |
| 515 | * The window size has changed, so we need to create a new |
| 516 | * surface. |
| 517 | */ |
| 518 | if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 519 | /* |
| 520 | * Unbind and destroy the old EGL surface, if |
| 521 | * there is one. |
| 522 | */ |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 523 | sEgl.eglMakeCurrent(sEglDisplay, EGL10.EGL_NO_SURFACE, |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 524 | EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 525 | sEgl.eglDestroySurface(sEglDisplay, mEglSurface); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // Create an EGL surface we can render into. |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 529 | mEglSurface = sEgl.eglCreateWindowSurface(sEglDisplay, sEglConfig, holder, null); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 530 | |
| 531 | if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 532 | int error = sEgl.eglGetError(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 533 | if (error == EGL10.EGL_BAD_NATIVE_WINDOW) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 534 | Log.e(LOG_TAG, "createWindowSurface returned EGL_BAD_NATIVE_WINDOW."); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 535 | return null; |
| 536 | } |
Mike Dodd | 5d3e2ea | 2010-10-07 18:02:47 -0700 | [diff] [blame] | 537 | throw new RuntimeException("createWindowSurface failed " |
| 538 | + getEGLErrorString(error)); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Before we can issue GL commands, we need to make sure |
| 543 | * the context is current and bound to a surface. |
| 544 | */ |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 545 | if (!sEgl.eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, sEglContext)) { |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 546 | throw new Surface.OutOfResourcesException("eglMakeCurrent failed " |
Mike Dodd | 5d3e2ea | 2010-10-07 18:02:47 -0700 | [diff] [blame] | 547 | + getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 548 | } |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 549 | |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 550 | if (mDirtyRegions) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 551 | if (!GLES20Canvas.preserveBackBuffer()) { |
| 552 | Log.w(LOG_TAG, "Backbuffer cannot be preserved"); |
| 553 | } |
| 554 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 555 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 556 | return sEglContext.getGL(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | EGLContext createContext(EGL10 egl, EGLDisplay eglDisplay, EGLConfig eglConfig) { |
| 560 | int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, mGlVersion, EGL10.EGL_NONE }; |
| 561 | |
| 562 | return egl.eglCreateContext(eglDisplay, eglConfig, EGL10.EGL_NO_CONTEXT, |
| 563 | mGlVersion != 0 ? attrib_list : null); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | @Override |
| 567 | void initializeIfNeeded(int width, int height, View.AttachInfo attachInfo, |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 568 | SurfaceHolder holder) throws Surface.OutOfResourcesException { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 569 | if (isRequested()) { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 570 | checkEglErrors(); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 571 | super.initializeIfNeeded(width, height, attachInfo, holder); |
| 572 | } |
| 573 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 574 | |
| 575 | @Override |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 576 | void destroy(boolean full) { |
| 577 | if (full && mCanvas != null) { |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 578 | mCanvas = null; |
| 579 | } |
| 580 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 581 | if (!isEnabled() || mDestroyed) return; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 582 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 583 | mDestroyed = true; |
| 584 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 585 | sEgl.eglMakeCurrent(sEglDisplay, EGL10.EGL_NO_SURFACE, |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 586 | EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 587 | sEgl.eglDestroySurface(sEglDisplay, mEglSurface); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 588 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 589 | mEglSurface = null; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 590 | mGl = null; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 591 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 592 | setEnabled(false); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 595 | @Override |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 596 | void setup(int width, int height) { |
| 597 | mCanvas.setViewport(width, height); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 598 | } |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 599 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 600 | boolean canDraw() { |
| 601 | return mGl != null && mCanvas != null; |
| 602 | } |
| 603 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 604 | void onPreDraw(Rect dirty) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 607 | void onPostDraw() { |
| 608 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 609 | |
| 610 | @Override |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 611 | void draw(View view, View.AttachInfo attachInfo, HardwareDrawCallbacks callbacks, |
| 612 | Rect dirty) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 613 | if (canDraw()) { |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 614 | if (!mDirtyRegions) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 615 | dirty = null; |
| 616 | } |
| 617 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 618 | attachInfo.mDrawingTime = SystemClock.uptimeMillis(); |
| 619 | attachInfo.mIgnoreDirtyState = true; |
| 620 | view.mPrivateFlags |= View.DRAWN; |
Romain Guy | 9a40bab | 2010-09-23 16:32:47 -0700 | [diff] [blame] | 621 | |
| 622 | long startTime; |
| 623 | if (ViewDebug.DEBUG_PROFILE_DRAWING) { |
| 624 | startTime = SystemClock.elapsedRealtime(); |
| 625 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 626 | |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 627 | final int surfaceState = checkCurrent(); |
| 628 | if (surfaceState != SURFACE_STATE_ERROR) { |
| 629 | // We had to change the current surface and/or context, redraw everything |
| 630 | if (surfaceState == SURFACE_STATE_UPDATED) { |
| 631 | dirty = null; |
| 632 | } |
| 633 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 634 | onPreDraw(dirty); |
Romain Guy | 62687ec | 2011-02-02 15:44:19 -0800 | [diff] [blame] | 635 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 636 | HardwareCanvas canvas = mCanvas; |
| 637 | attachInfo.mHardwareCanvas = canvas; |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 638 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 639 | int saveCount = canvas.save(); |
| 640 | callbacks.onHardwarePreDraw(canvas); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 641 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 642 | try { |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 643 | view.mRecreateDisplayList = |
| 644 | (view.mPrivateFlags & View.INVALIDATED) == View.INVALIDATED; |
| 645 | view.mPrivateFlags &= ~View.INVALIDATED; |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 646 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 647 | DisplayList displayList = view.getDisplayList(); |
| 648 | if (displayList != null) { |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 649 | if (canvas.drawDisplayList(displayList, view.getWidth(), |
| 650 | view.getHeight(), mRedrawClip)) { |
Romain Guy | e294d41 | 2011-03-08 17:35:10 -0800 | [diff] [blame] | 651 | if (mRedrawClip.isEmpty() || view.getParent() == null) { |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 652 | view.invalidate(); |
| 653 | } else { |
| 654 | view.getParent().invalidateChild(view, mRedrawClip); |
| 655 | } |
| 656 | mRedrawClip.setEmpty(); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 657 | } |
| 658 | } else { |
| 659 | // Shouldn't reach here |
| 660 | view.draw(canvas); |
| 661 | } |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 662 | |
| 663 | if (DEBUG_DIRTY_REGION) { |
| 664 | if (mDebugPaint == null) { |
| 665 | mDebugPaint = new Paint(); |
| 666 | mDebugPaint.setColor(0x7fff0000); |
| 667 | } |
| 668 | if (dirty != null && (mFrameCount++ & 1) == 0) { |
| 669 | canvas.drawRect(dirty, mDebugPaint); |
| 670 | } |
| 671 | } |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 672 | } finally { |
| 673 | callbacks.onHardwarePostDraw(canvas); |
| 674 | canvas.restoreToCount(saveCount); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 675 | view.mRecreateDisplayList = false; |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 676 | } |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 677 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 678 | onPostDraw(); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 679 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 680 | if (ViewDebug.DEBUG_PROFILE_DRAWING) { |
| 681 | EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime); |
| 682 | } |
| 683 | |
| 684 | attachInfo.mIgnoreDirtyState = false; |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 685 | |
| 686 | final long swapBuffersStartTime; |
| 687 | if (ViewDebug.DEBUG_LATENCY) { |
| 688 | swapBuffersStartTime = System.nanoTime(); |
| 689 | } |
| 690 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 691 | sEgl.eglSwapBuffers(sEglDisplay, mEglSurface); |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 692 | |
| 693 | if (ViewDebug.DEBUG_LATENCY) { |
| 694 | long now = System.nanoTime(); |
| 695 | Log.d(LOG_TAG, "Latency: Spent " |
| 696 | + ((now - swapBuffersStartTime) * 0.000001f) |
| 697 | + "ms waiting for eglSwapBuffers()"); |
| 698 | } |
| 699 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 700 | checkEglErrors(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 701 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 702 | } |
| 703 | } |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 704 | |
| 705 | private int checkCurrent() { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 706 | // TODO: Don't check the current context when we have one per UI thread |
| 707 | // TODO: Use a threadlocal flag to know whether the surface has changed |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 708 | if (!sEglContext.equals(sEgl.eglGetCurrentContext()) || |
| 709 | !mEglSurface.equals(sEgl.eglGetCurrentSurface(EGL10.EGL_DRAW))) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 710 | if (!sEgl.eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, sEglContext)) { |
Romain Guy | 9745fae | 2010-12-08 11:39:15 -0800 | [diff] [blame] | 711 | fallback(true); |
| 712 | Log.e(LOG_TAG, "eglMakeCurrent failed " + |
| 713 | getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 714 | return SURFACE_STATE_ERROR; |
| 715 | } else { |
| 716 | return SURFACE_STATE_UPDATED; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 717 | } |
| 718 | } |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 719 | return SURFACE_STATE_SUCCESS; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 720 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 721 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 722 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 723 | /** |
| 724 | * Hardware renderer using OpenGL ES 2.0. |
| 725 | */ |
| 726 | static class Gl20Renderer extends GlRenderer { |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 727 | private GLES20Canvas mGlCanvas; |
| 728 | |
| 729 | Gl20Renderer(boolean translucent) { |
| 730 | super(2, translucent); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | @Override |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 734 | GLES20Canvas createCanvas() { |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 735 | return mGlCanvas = new GLES20Canvas(mTranslucent); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 736 | } |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 737 | |
| 738 | @Override |
| 739 | int[] getConfig(boolean dirtyRegions) { |
| 740 | return new int[] { |
| 741 | EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 742 | EGL10.EGL_RED_SIZE, 8, |
| 743 | EGL10.EGL_GREEN_SIZE, 8, |
| 744 | EGL10.EGL_BLUE_SIZE, 8, |
| 745 | EGL10.EGL_ALPHA_SIZE, 8, |
| 746 | EGL10.EGL_DEPTH_SIZE, 0, |
| 747 | EGL10.EGL_STENCIL_SIZE, 0, |
| 748 | EGL_SURFACE_TYPE, EGL10.EGL_WINDOW_BIT | |
| 749 | (dirtyRegions ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0), |
| 750 | EGL10.EGL_NONE |
| 751 | }; |
| 752 | } |
| 753 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 754 | @Override |
| 755 | boolean canDraw() { |
| 756 | return super.canDraw() && mGlCanvas != null; |
| 757 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 758 | |
| 759 | @Override |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 760 | void onPreDraw(Rect dirty) { |
| 761 | mGlCanvas.onPreDraw(dirty); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 764 | @Override |
| 765 | void onPostDraw() { |
| 766 | mGlCanvas.onPostDraw(); |
| 767 | } |
| 768 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 769 | @Override |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 770 | void destroy(boolean full) { |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 771 | try { |
| 772 | super.destroy(full); |
| 773 | } finally { |
| 774 | if (full && mGlCanvas != null) { |
| 775 | mGlCanvas = null; |
| 776 | } |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
| 780 | @Override |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 781 | DisplayList createDisplayList(View v) { |
| 782 | return new GLES20DisplayList(v); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 783 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 784 | |
| 785 | @Override |
| 786 | HardwareLayer createHardwareLayer() { |
| 787 | return new GLES20TextureLayer(); |
| 788 | } |
| 789 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 790 | @Override |
| 791 | HardwareLayer createHardwareLayer(int width, int height, boolean isOpaque) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 792 | return new GLES20RenderLayer(width, height, isOpaque); |
| 793 | } |
| 794 | |
| 795 | @Override |
| 796 | SurfaceTexture createSuraceTexture(HardwareLayer layer) { |
| 797 | return ((GLES20TextureLayer) layer).getSurfaceTexture(); |
| 798 | } |
| 799 | |
| 800 | @Override |
| 801 | void updateTextureLayer(HardwareLayer layer, int width, int height, |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 802 | SurfaceTexture surface) { |
| 803 | ((GLES20TextureLayer) layer).update(width, height, surface.mSurfaceTexture); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 804 | } |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 805 | |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 806 | static HardwareRenderer create(boolean translucent) { |
| 807 | if (GLES20Canvas.isAvailable()) { |
| 808 | return new Gl20Renderer(translucent); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 809 | } |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 810 | return null; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 813 | } |