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