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 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 20 | import android.content.ComponentCallbacks2; |
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 | bd43152 | 2012-09-26 13:31:25 -0700 | [diff] [blame] | 24 | import android.opengl.EGL14; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 25 | import android.opengl.GLUtils; |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 26 | import android.opengl.ManagedEGLContext; |
| 27 | import android.os.Handler; |
| 28 | import android.os.Looper; |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 29 | import android.os.SystemClock; |
| 30 | import android.os.SystemProperties; |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 31 | import android.os.Trace; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 32 | import android.util.Log; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 33 | import com.google.android.gles_jni.EGLImpl; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 34 | |
| 35 | import javax.microedition.khronos.egl.EGL10; |
| 36 | import javax.microedition.khronos.egl.EGL11; |
| 37 | import javax.microedition.khronos.egl.EGLConfig; |
| 38 | import javax.microedition.khronos.egl.EGLContext; |
| 39 | import javax.microedition.khronos.egl.EGLDisplay; |
| 40 | import javax.microedition.khronos.egl.EGLSurface; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 41 | import javax.microedition.khronos.opengles.GL; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 42 | |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 43 | import java.io.File; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 44 | import java.io.PrintWriter; |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 45 | import java.util.concurrent.locks.ReentrantLock; |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 46 | |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 47 | import static javax.microedition.khronos.egl.EGL10.*; |
| 48 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 49 | /** |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 50 | * Interface for rendering a ViewAncestor using hardware acceleration. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 51 | * |
| 52 | * @hide |
| 53 | */ |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 54 | public abstract class HardwareRenderer { |
Romain Guy | 4f6aff3 | 2011-01-12 16:21:41 -0800 | [diff] [blame] | 55 | static final String LOG_TAG = "HardwareRenderer"; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 56 | |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 57 | /** |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 58 | * Name of the file that holds the shaders cache. |
| 59 | */ |
| 60 | private static final String CACHE_PATH_SHADERS = "com.android.opengl.shaders_cache"; |
| 61 | |
| 62 | /** |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 63 | * 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] | 64 | * When turned on the property defined by {@link #RENDER_DIRTY_REGIONS_PROPERTY} |
| 65 | * must also have the value "true". |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 66 | */ |
| 67 | public static final boolean RENDER_DIRTY_REGIONS = true; |
| 68 | |
| 69 | /** |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 70 | * System property used to enable or disable dirty regions invalidation. |
| 71 | * This property is only queried if {@link #RENDER_DIRTY_REGIONS} is true. |
| 72 | * The default value of this property is assumed to be true. |
| 73 | * |
| 74 | * Possible values: |
| 75 | * "true", to enable partial invalidates |
| 76 | * "false", to disable partial invalidates |
| 77 | */ |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 78 | static final String RENDER_DIRTY_REGIONS_PROPERTY = "debug.hwui.render_dirty_regions"; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * System property used to enable or disable vsync. |
| 82 | * The default value of this property is assumed to be false. |
| 83 | * |
| 84 | * Possible values: |
| 85 | * "true", to disable vsync |
| 86 | * "false", to enable vsync |
| 87 | */ |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 88 | static final String DISABLE_VSYNC_PROPERTY = "debug.hwui.disable_vsync"; |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 91 | * System property used to enable or disable hardware rendering profiling. |
| 92 | * The default value of this property is assumed to be false. |
Chet Haase | 0928060 | 2012-04-03 16:15:34 -0700 | [diff] [blame] | 93 | * |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 94 | * When profiling is enabled, the adb shell dumpsys gfxinfo command will |
| 95 | * output extra information about the time taken to execute by the last |
| 96 | * frames. |
| 97 | * |
| 98 | * Possible values: |
| 99 | * "true", to enable profiling |
| 100 | * "false", to disable profiling |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 101 | * |
| 102 | * @hide |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 103 | */ |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 104 | public static final String PROFILE_PROPERTY = "debug.hwui.profile"; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 105 | |
| 106 | /** |
Chet Haase | 0928060 | 2012-04-03 16:15:34 -0700 | [diff] [blame] | 107 | * System property used to specify the number of frames to be used |
| 108 | * when doing hardware rendering profiling. |
| 109 | * The default value of this property is #PROFILE_MAX_FRAMES. |
| 110 | * |
| 111 | * When profiling is enabled, the adb shell dumpsys gfxinfo command will |
| 112 | * output extra information about the time taken to execute by the last |
| 113 | * frames. |
| 114 | * |
| 115 | * Possible values: |
| 116 | * "60", to set the limit of frames to 60 |
| 117 | */ |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 118 | static final String PROFILE_MAXFRAMES_PROPERTY = "debug.hwui.profile.maxframes"; |
Chet Haase | 0928060 | 2012-04-03 16:15:34 -0700 | [diff] [blame] | 119 | |
| 120 | /** |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 121 | * System property used to debug EGL configuration choice. |
| 122 | * |
| 123 | * Possible values: |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 124 | * "choice", print the chosen configuration only |
| 125 | * "all", print all possible configurations |
| 126 | */ |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 127 | static final String PRINT_CONFIG_PROPERTY = "debug.hwui.print_config"; |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 128 | |
| 129 | /** |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 130 | * Turn on to draw dirty regions every other frame. |
Romain Guy | b04f7e9 | 2012-02-15 12:36:54 -0800 | [diff] [blame] | 131 | * |
| 132 | * Possible values: |
| 133 | * "true", to enable dirty regions debugging |
| 134 | * "false", to disable dirty regions debugging |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 135 | * |
| 136 | * @hide |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 137 | */ |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 138 | public static final String DEBUG_DIRTY_REGIONS_PROPERTY = "debug.hwui.show_dirty_regions"; |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Turn on to flash hardware layers when they update. |
| 142 | * |
| 143 | * Possible values: |
| 144 | * "true", to enable hardware layers updates debugging |
| 145 | * "false", to disable hardware layers updates debugging |
| 146 | * |
| 147 | * @hide |
| 148 | */ |
| 149 | public static final String DEBUG_SHOW_LAYERS_UPDATES_PROPERTY = |
| 150 | "debug.hwui.show_layers_updates"; |
| 151 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 152 | /** |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 153 | * Turn on to show overdraw level. |
| 154 | * |
| 155 | * Possible values: |
| 156 | * "true", to enable overdraw debugging |
| 157 | * "false", to disable overdraw debugging |
| 158 | * |
| 159 | * @hide |
| 160 | */ |
| 161 | public static final String DEBUG_SHOW_OVERDRAW_PROPERTY = "debug.hwui.show_overdraw"; |
| 162 | |
| 163 | /** |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 164 | * A process can set this flag to false to prevent the use of hardware |
| 165 | * rendering. |
| 166 | * |
| 167 | * @hide |
| 168 | */ |
| 169 | public static boolean sRendererDisabled = false; |
| 170 | |
Dianne Hackborn | 5d927c2 | 2011-09-02 12:22:18 -0700 | [diff] [blame] | 171 | /** |
| 172 | * Further hardware renderer disabling for the system process. |
| 173 | * |
| 174 | * @hide |
| 175 | */ |
| 176 | public static boolean sSystemRendererDisabled = false; |
| 177 | |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 178 | /** |
| 179 | * Number of frames to profile. |
| 180 | */ |
Romain Guy | a21f877 | 2012-05-07 10:20:52 -0700 | [diff] [blame] | 181 | private static final int PROFILE_MAX_FRAMES = 128; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Number of floats per profiled frame. |
| 185 | */ |
| 186 | private static final int PROFILE_FRAME_DATA_COUNT = 3; |
| 187 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 188 | private boolean mEnabled; |
| 189 | private boolean mRequested = true; |
| 190 | |
| 191 | /** |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 192 | * Invoke this method to disable hardware rendering in the current process. |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 193 | * |
| 194 | * @hide |
| 195 | */ |
Dianne Hackborn | 5d927c2 | 2011-09-02 12:22:18 -0700 | [diff] [blame] | 196 | public static void disable(boolean system) { |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 197 | sRendererDisabled = true; |
Dianne Hackborn | 5d927c2 | 2011-09-02 12:22:18 -0700 | [diff] [blame] | 198 | if (system) { |
| 199 | sSystemRendererDisabled = true; |
| 200 | } |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 204 | * Indicates whether hardware acceleration is available under any form for |
| 205 | * the view hierarchy. |
| 206 | * |
| 207 | * @return True if the view hierarchy can potentially be hardware accelerated, |
| 208 | * false otherwise |
| 209 | */ |
| 210 | public static boolean isAvailable() { |
| 211 | return GLES20Canvas.isAvailable(); |
| 212 | } |
| 213 | |
| 214 | /** |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 215 | * Destroys the hardware rendering context. |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 216 | * |
| 217 | * @param full If true, destroys all associated resources. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 218 | */ |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 219 | abstract void destroy(boolean full); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Initializes the hardware renderer for the specified surface. |
| 223 | * |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 224 | * @param surface The surface to hardware accelerate |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 225 | * |
| 226 | * @return True if the initialization was successful, false otherwise. |
| 227 | */ |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 228 | abstract boolean initialize(Surface surface) throws Surface.OutOfResourcesException; |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * Updates the hardware renderer for the specified surface. |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 232 | * |
| 233 | * @param surface The surface to hardware accelerate |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 234 | */ |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 235 | abstract void updateSurface(Surface surface) throws Surface.OutOfResourcesException; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 236 | |
| 237 | /** |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 238 | * Destroys the layers used by the specified view hierarchy. |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 239 | * |
| 240 | * @param view The root of the view hierarchy |
| 241 | */ |
| 242 | abstract void destroyLayers(View view); |
| 243 | |
| 244 | /** |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 245 | * Destroys all hardware rendering resources associated with the specified |
| 246 | * view hierarchy. |
| 247 | * |
| 248 | * @param view The root of the view hierarchy |
| 249 | */ |
| 250 | abstract void destroyHardwareResources(View view); |
| 251 | |
| 252 | /** |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 253 | * This method should be invoked whenever the current hardware renderer |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 254 | * context should be reset. |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 255 | * |
| 256 | * @param surface The surface to hardware accelerate |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 257 | */ |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 258 | abstract void invalidate(Surface surface); |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 259 | |
| 260 | /** |
| 261 | * This method should be invoked to ensure the hardware renderer is in |
| 262 | * valid state (for instance, to ensure the correct EGL context is bound |
| 263 | * to the current thread.) |
| 264 | * |
| 265 | * @return true if the renderer is now valid, false otherwise |
| 266 | */ |
| 267 | abstract boolean validate(); |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 268 | |
| 269 | /** |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 270 | * This method ensures the hardware renderer is in a valid state |
| 271 | * before executing the specified action. |
| 272 | * |
| 273 | * This method will attempt to set a valid state even if the window |
| 274 | * the renderer is attached to was destroyed. |
| 275 | * |
| 276 | * @return true if the action was run |
| 277 | */ |
| 278 | abstract boolean safelyRun(Runnable action); |
| 279 | |
| 280 | /** |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 281 | * Setup the hardware renderer for drawing. This is called whenever the |
| 282 | * size of the target surface changes or when the surface is first created. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 283 | * |
| 284 | * @param width Width of the drawing surface. |
| 285 | * @param height Height of the drawing surface. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 286 | */ |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 287 | abstract void setup(int width, int height); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 288 | |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 289 | /** |
Chet Haase | 40e0383 | 2011-10-06 08:34:13 -0700 | [diff] [blame] | 290 | * Gets the current width of the surface. This is the width that the surface |
| 291 | * was last set to in a call to {@link #setup(int, int)}. |
| 292 | * |
| 293 | * @return the current width of the surface |
| 294 | */ |
| 295 | abstract int getWidth(); |
| 296 | |
| 297 | /** |
| 298 | * Gets the current height of the surface. This is the height that the surface |
| 299 | * was last set to in a call to {@link #setup(int, int)}. |
| 300 | * |
| 301 | * @return the current width of the surface |
| 302 | */ |
| 303 | abstract int getHeight(); |
| 304 | |
| 305 | /** |
Chet Haase | 08837c2 | 2011-11-28 11:53:21 -0800 | [diff] [blame] | 306 | * Gets the current canvas associated with this HardwareRenderer. |
| 307 | * |
| 308 | * @return the current HardwareCanvas |
| 309 | */ |
| 310 | abstract HardwareCanvas getCanvas(); |
| 311 | |
| 312 | /** |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 313 | * Outputs extra debugging information in the specified file descriptor. |
| 314 | * @param pw |
| 315 | */ |
| 316 | abstract void dumpGfxInfo(PrintWriter pw); |
Michael Jurka | a3fabff | 2012-03-28 17:22:29 +0200 | [diff] [blame] | 317 | |
| 318 | /** |
| 319 | * Outputs the total number of frames rendered (used for fps calculations) |
| 320 | * |
| 321 | * @return the number of frames rendered |
| 322 | */ |
| 323 | abstract long getFrameCount(); |
| 324 | |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 325 | /** |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 326 | * Sets the directory to use as a persistent storage for hardware rendering |
| 327 | * resources. |
| 328 | * |
| 329 | * @param cacheDir A directory the current process can write to |
| 330 | */ |
| 331 | public static void setupDiskCache(File cacheDir) { |
| 332 | nSetupShadersDiskCache(new File(cacheDir, CACHE_PATH_SHADERS).getAbsolutePath()); |
| 333 | } |
| 334 | |
| 335 | private static native void nSetupShadersDiskCache(String cacheFile); |
| 336 | |
| 337 | /** |
Jamie Gennis | b335fad | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 338 | * Notifies EGL that the frame is about to be rendered. |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 339 | * @param size |
Jamie Gennis | b335fad | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 340 | */ |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 341 | private static void beginFrame(int[] size) { |
| 342 | nBeginFrame(size); |
Jamie Gennis | b335fad | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 345 | private static native void nBeginFrame(int[] size); |
Jamie Gennis | b335fad | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 346 | |
| 347 | /** |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 348 | * Preserves the back buffer of the current surface after a buffer swap. |
| 349 | * Calling this method sets the EGL_SWAP_BEHAVIOR attribute of the current |
| 350 | * surface to EGL_BUFFER_PRESERVED. Calling this method requires an EGL |
| 351 | * config that supports EGL_SWAP_BEHAVIOR_PRESERVED_BIT. |
| 352 | * |
| 353 | * @return True if the swap behavior was successfully changed, |
| 354 | * false otherwise. |
| 355 | */ |
| 356 | static boolean preserveBackBuffer() { |
| 357 | return nPreserveBackBuffer(); |
| 358 | } |
| 359 | |
| 360 | private static native boolean nPreserveBackBuffer(); |
| 361 | |
| 362 | /** |
| 363 | * Indicates whether the current surface preserves its back buffer |
| 364 | * after a buffer swap. |
| 365 | * |
| 366 | * @return True, if the surface's EGL_SWAP_BEHAVIOR is EGL_BUFFER_PRESERVED, |
| 367 | * false otherwise |
| 368 | */ |
| 369 | static boolean isBackBufferPreserved() { |
| 370 | return nIsBackBufferPreserved(); |
| 371 | } |
| 372 | |
| 373 | private static native boolean nIsBackBufferPreserved(); |
| 374 | |
| 375 | /** |
| 376 | * Disables v-sync. For performance testing only. |
| 377 | */ |
| 378 | static void disableVsync() { |
| 379 | nDisableVsync(); |
| 380 | } |
| 381 | |
| 382 | private static native void nDisableVsync(); |
| 383 | |
| 384 | /** |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 385 | * Indicates that the specified hardware layer needs to be updated |
| 386 | * as soon as possible. |
| 387 | * |
| 388 | * @param layer The hardware layer that needs an update |
| 389 | */ |
| 390 | abstract void pushLayerUpdate(HardwareLayer layer); |
| 391 | |
| 392 | /** |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 393 | * Interface used to receive callbacks whenever a view is drawn by |
| 394 | * a hardware renderer instance. |
| 395 | */ |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 396 | interface HardwareDrawCallbacks { |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 397 | /** |
| 398 | * Invoked before a view is drawn by a hardware renderer. |
| 399 | * |
| 400 | * @param canvas The Canvas used to render the view. |
| 401 | */ |
Romain Guy | 7d70fbf | 2011-05-24 17:40:25 -0700 | [diff] [blame] | 402 | void onHardwarePreDraw(HardwareCanvas canvas); |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 403 | |
| 404 | /** |
| 405 | * Invoked after a view is drawn by a hardware renderer. |
| 406 | * |
| 407 | * @param canvas The Canvas used to render the view. |
| 408 | */ |
Romain Guy | 7d70fbf | 2011-05-24 17:40:25 -0700 | [diff] [blame] | 409 | void onHardwarePostDraw(HardwareCanvas canvas); |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 412 | /** |
| 413 | * Draws the specified view. |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 414 | * |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 415 | * @param view The view to draw. |
| 416 | * @param attachInfo AttachInfo tied to the specified view. |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 417 | * @param callbacks Callbacks invoked when drawing happens. |
| 418 | * @param dirty The dirty rectangle to update, can be null. |
Romain Guy | 50d133e | 2011-08-12 15:25:28 -0700 | [diff] [blame] | 419 | * |
| 420 | * @return true if the dirty rect was ignored, false otherwise |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 421 | */ |
Romain Guy | 50d133e | 2011-08-12 15:25:28 -0700 | [diff] [blame] | 422 | abstract boolean draw(View view, View.AttachInfo attachInfo, HardwareDrawCallbacks callbacks, |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 423 | Rect dirty); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 424 | |
| 425 | /** |
Romain Guy | 53ca03d | 2010-10-08 18:55:27 -0700 | [diff] [blame] | 426 | * Creates a new display list that can be used to record batches of |
| 427 | * drawing operations. |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 428 | * |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 429 | * @param name The name of the display list, used for debugging purpose. |
| 430 | * May be null |
| 431 | * |
Romain Guy | 53ca03d | 2010-10-08 18:55:27 -0700 | [diff] [blame] | 432 | * @return A new display list. |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 433 | */ |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 434 | public abstract DisplayList createDisplayList(String name); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 435 | |
| 436 | /** |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 437 | * Creates a new hardware layer. A hardware layer built by calling this |
| 438 | * method will be treated as a texture layer, instead of as a render target. |
| 439 | * |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 440 | * @param isOpaque Whether the layer should be opaque or not |
| 441 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 442 | * @return A hardware layer |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 443 | */ |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 444 | abstract HardwareLayer createHardwareLayer(boolean isOpaque); |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 445 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 446 | /** |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 447 | * Creates a new hardware layer. |
| 448 | * |
| 449 | * @param width The minimum width of the layer |
| 450 | * @param height The minimum height of the layer |
| 451 | * @param isOpaque Whether the layer should be opaque or not |
| 452 | * |
| 453 | * @return A hardware layer |
| 454 | */ |
| 455 | abstract HardwareLayer createHardwareLayer(int width, int height, boolean isOpaque); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * Creates a new {@link SurfaceTexture} that can be used to render into the |
| 459 | * specified hardware layer. |
| 460 | * |
| 461 | * |
| 462 | * @param layer The layer to render into using a {@link android.graphics.SurfaceTexture} |
| 463 | * |
| 464 | * @return A {@link SurfaceTexture} |
| 465 | */ |
Romain Guy | e5e0c50 | 2011-06-15 15:18:31 -0700 | [diff] [blame] | 466 | abstract SurfaceTexture createSurfaceTexture(HardwareLayer layer); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 467 | |
| 468 | /** |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 469 | * Sets the {@link android.graphics.SurfaceTexture} that will be used to |
| 470 | * render into the specified hardware layer. |
| 471 | * |
| 472 | * @param layer The layer to render into using a {@link android.graphics.SurfaceTexture} |
| 473 | * @param surfaceTexture The {@link android.graphics.SurfaceTexture} to use for the layer |
| 474 | */ |
| 475 | abstract void setSurfaceTexture(HardwareLayer layer, SurfaceTexture surfaceTexture); |
| 476 | |
| 477 | /** |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 478 | * Detaches the specified functor from the current functor execution queue. |
| 479 | * |
| 480 | * @param functor The native functor to remove from the execution queue. |
| 481 | * |
| 482 | * @see HardwareCanvas#callDrawGLFunction(int) |
| 483 | * @see #attachFunctor(android.view.View.AttachInfo, int) |
| 484 | */ |
| 485 | abstract void detachFunctor(int functor); |
| 486 | |
| 487 | /** |
| 488 | * Schedules the specified functor in the functors execution queue. |
| 489 | * |
| 490 | * @param attachInfo AttachInfo tied to this renderer. |
| 491 | * @param functor The native functor to insert in the execution queue. |
| 492 | * |
| 493 | * @see HardwareCanvas#callDrawGLFunction(int) |
Chris Craik | 41ee465 | 2012-05-31 15:05:57 -0700 | [diff] [blame] | 494 | * @see #detachFunctor(int) |
| 495 | * |
| 496 | * @return true if the functor was attached successfully |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 497 | */ |
Chris Craik | 41ee465 | 2012-05-31 15:05:57 -0700 | [diff] [blame] | 498 | abstract boolean attachFunctor(View.AttachInfo attachInfo, int functor); |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 499 | |
| 500 | /** |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 501 | * Initializes the hardware renderer for the specified surface and setup the |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 502 | * renderer for drawing, if needed. This is invoked when the ViewAncestor has |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 503 | * potentially lost the hardware renderer. The hardware renderer should be |
| 504 | * reinitialized and setup when the render {@link #isRequested()} and |
| 505 | * {@link #isEnabled()}. |
Romain Guy | 44d7974 | 2012-01-13 12:12:09 -0800 | [diff] [blame] | 506 | * |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 507 | * @param width The width of the drawing surface. |
| 508 | * @param height The height of the drawing surface. |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 509 | * @param surface The surface to hardware accelerate |
Romain Guy | dfab363 | 2012-10-03 14:53:25 -0700 | [diff] [blame] | 510 | * |
| 511 | * @return true if the surface was initialized, false otherwise. Returning |
| 512 | * false might mean that the surface was already initialized. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 513 | */ |
Romain Guy | dfab363 | 2012-10-03 14:53:25 -0700 | [diff] [blame] | 514 | boolean initializeIfNeeded(int width, int height, Surface surface) |
Romain Guy | 44d7974 | 2012-01-13 12:12:09 -0800 | [diff] [blame] | 515 | throws Surface.OutOfResourcesException { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 516 | if (isRequested()) { |
| 517 | // We lost the gl context, so recreate it. |
| 518 | if (!isEnabled()) { |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 519 | if (initialize(surface)) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 520 | setup(width, height); |
Romain Guy | dfab363 | 2012-10-03 14:53:25 -0700 | [diff] [blame] | 521 | return true; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
Romain Guy | dfab363 | 2012-10-03 14:53:25 -0700 | [diff] [blame] | 524 | } |
| 525 | return false; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Creates a hardware renderer using OpenGL. |
| 530 | * |
| 531 | * @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] | 532 | * @param translucent True if the surface is translucent, false otherwise |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 533 | * |
| 534 | * @return A hardware renderer backed by OpenGL. |
| 535 | */ |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 536 | static HardwareRenderer createGlRenderer(int glVersion, boolean translucent) { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 537 | switch (glVersion) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 538 | case 2: |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 539 | return Gl20Renderer.create(translucent); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 540 | } |
| 541 | throw new IllegalArgumentException("Unknown GL version: " + glVersion); |
| 542 | } |
| 543 | |
| 544 | /** |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 545 | * Invoke this method when the system is running out of memory. This |
| 546 | * method will attempt to recover as much memory as possible, based on |
| 547 | * the specified hint. |
| 548 | * |
| 549 | * @param level Hint about the amount of memory that should be trimmed, |
| 550 | * see {@link android.content.ComponentCallbacks} |
| 551 | */ |
| 552 | static void trimMemory(int level) { |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 553 | startTrimMemory(level); |
| 554 | endTrimMemory(); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Starts the process of trimming memory. Usually this call will setup |
| 559 | * hardware rendering context and reclaim memory.Extra cleanup might |
| 560 | * be required by calling {@link #endTrimMemory()}. |
| 561 | * |
| 562 | * @param level Hint about the amount of memory that should be trimmed, |
| 563 | * see {@link android.content.ComponentCallbacks} |
| 564 | */ |
| 565 | static void startTrimMemory(int level) { |
| 566 | Gl20Renderer.startTrimMemory(level); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Finishes the process of trimming memory. This method will usually |
| 571 | * cleanup special resources used by the memory trimming process. |
| 572 | */ |
| 573 | static void endTrimMemory() { |
| 574 | Gl20Renderer.endTrimMemory(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /** |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 578 | * Indicates whether hardware acceleration is currently enabled. |
| 579 | * |
| 580 | * @return True if hardware acceleration is in use, false otherwise. |
| 581 | */ |
| 582 | boolean isEnabled() { |
| 583 | return mEnabled; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Indicates whether hardware acceleration is currently enabled. |
| 588 | * |
| 589 | * @param enabled True if the hardware renderer is in use, false otherwise. |
| 590 | */ |
| 591 | void setEnabled(boolean enabled) { |
| 592 | mEnabled = enabled; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Indicates whether hardware acceleration is currently request but not |
| 597 | * necessarily enabled yet. |
| 598 | * |
| 599 | * @return True if requested, false otherwise. |
| 600 | */ |
| 601 | boolean isRequested() { |
| 602 | return mRequested; |
| 603 | } |
| 604 | |
| 605 | /** |
Romain Guy | 9745fae | 2010-12-08 11:39:15 -0800 | [diff] [blame] | 606 | * Indicates whether hardware acceleration is currently requested but not |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 607 | * necessarily enabled yet. |
| 608 | * |
| 609 | * @return True to request hardware acceleration, false otherwise. |
| 610 | */ |
| 611 | void setRequested(boolean requested) { |
| 612 | mRequested = requested; |
| 613 | } |
| 614 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 615 | @SuppressWarnings({"deprecation"}) |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 616 | static abstract class GlRenderer extends HardwareRenderer { |
Romain Guy | 16260e7 | 2011-09-01 14:26:11 -0700 | [diff] [blame] | 617 | static final int SURFACE_STATE_ERROR = 0; |
| 618 | static final int SURFACE_STATE_SUCCESS = 1; |
| 619 | static final int SURFACE_STATE_UPDATED = 2; |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 620 | |
Chris Craik | 65924a3 | 2012-04-05 17:52:11 -0700 | [diff] [blame] | 621 | static final int FUNCTOR_PROCESS_DELAY = 4; |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 622 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 623 | static EGL10 sEgl; |
| 624 | static EGLDisplay sEglDisplay; |
| 625 | static EGLConfig sEglConfig; |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 626 | static final Object[] sEglLock = new Object[0]; |
Chet Haase | 40e0383 | 2011-10-06 08:34:13 -0700 | [diff] [blame] | 627 | int mWidth = -1, mHeight = -1; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 628 | |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 629 | static final ThreadLocal<ManagedEGLContext> sEglContextStorage |
| 630 | = new ThreadLocal<ManagedEGLContext>(); |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 631 | |
| 632 | EGLContext mEglContext; |
| 633 | Thread mEglThread; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 634 | |
| 635 | EGLSurface mEglSurface; |
| 636 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 637 | GL mGl; |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 638 | HardwareCanvas mCanvas; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 639 | |
Michael Jurka | a3fabff | 2012-03-28 17:22:29 +0200 | [diff] [blame] | 640 | long mFrameCount; |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 641 | Paint mDebugPaint; |
| 642 | |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 643 | static boolean sDirtyRegions; |
| 644 | static final boolean sDirtyRegionsRequested; |
| 645 | static { |
| 646 | String dirtyProperty = SystemProperties.get(RENDER_DIRTY_REGIONS_PROPERTY, "true"); |
| 647 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 648 | sDirtyRegions = RENDER_DIRTY_REGIONS && "true".equalsIgnoreCase(dirtyProperty); |
| 649 | sDirtyRegionsRequested = sDirtyRegions; |
| 650 | } |
| 651 | |
| 652 | boolean mDirtyRegionsEnabled; |
Romain Guy | 9477c6e | 2011-12-09 12:27:21 -0800 | [diff] [blame] | 653 | boolean mUpdateDirtyRegions; |
| 654 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 655 | final boolean mVsyncDisabled; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 656 | |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 657 | final boolean mProfileEnabled; |
| 658 | final float[] mProfileData; |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 659 | final ReentrantLock mProfileLock; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 660 | int mProfileCurrentFrame = -PROFILE_FRAME_DATA_COUNT; |
Romain Guy | b04f7e9 | 2012-02-15 12:36:54 -0800 | [diff] [blame] | 661 | |
| 662 | final boolean mDebugDirtyRegions; |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 663 | final boolean mShowOverdraw; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 664 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 665 | final int mGlVersion; |
| 666 | final boolean mTranslucent; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 667 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 668 | private boolean mDestroyed; |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 669 | |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 670 | private final Rect mRedrawClip = new Rect(); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 671 | |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 672 | private final int[] mSurfaceSize = new int[2]; |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 673 | private final FunctorsRunnable mFunctorsRunnable = new FunctorsRunnable(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 674 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 675 | GlRenderer(int glVersion, boolean translucent) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 676 | mGlVersion = glVersion; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 677 | mTranslucent = translucent; |
Romain Guy | b04f7e9 | 2012-02-15 12:36:54 -0800 | [diff] [blame] | 678 | |
| 679 | String property; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 680 | |
Romain Guy | b04f7e9 | 2012-02-15 12:36:54 -0800 | [diff] [blame] | 681 | property = SystemProperties.get(DISABLE_VSYNC_PROPERTY, "false"); |
| 682 | mVsyncDisabled = "true".equalsIgnoreCase(property); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 683 | if (mVsyncDisabled) { |
| 684 | Log.d(LOG_TAG, "Disabling v-sync"); |
| 685 | } |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 686 | |
Romain Guy | 13b9073 | 2012-05-21 12:13:31 -0700 | [diff] [blame] | 687 | property = SystemProperties.get(PROFILE_PROPERTY, "false"); |
| 688 | mProfileEnabled = "true".equalsIgnoreCase(property); |
| 689 | if (mProfileEnabled) { |
| 690 | Log.d(LOG_TAG, "Profiling hardware renderer"); |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | if (mProfileEnabled) { |
Chet Haase | 0928060 | 2012-04-03 16:15:34 -0700 | [diff] [blame] | 694 | property = SystemProperties.get(PROFILE_MAXFRAMES_PROPERTY, |
| 695 | Integer.toString(PROFILE_MAX_FRAMES)); |
| 696 | int maxProfileFrames = Integer.valueOf(property); |
| 697 | mProfileData = new float[maxProfileFrames * PROFILE_FRAME_DATA_COUNT]; |
| 698 | for (int i = 0; i < mProfileData.length; i += PROFILE_FRAME_DATA_COUNT) { |
| 699 | mProfileData[i] = mProfileData[i + 1] = mProfileData[i + 2] = -1; |
| 700 | } |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 701 | |
| 702 | mProfileLock = new ReentrantLock(); |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 703 | } else { |
| 704 | mProfileData = null; |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 705 | mProfileLock = null; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 706 | } |
Romain Guy | b04f7e9 | 2012-02-15 12:36:54 -0800 | [diff] [blame] | 707 | |
| 708 | property = SystemProperties.get(DEBUG_DIRTY_REGIONS_PROPERTY, "false"); |
| 709 | mDebugDirtyRegions = "true".equalsIgnoreCase(property); |
| 710 | if (mDebugDirtyRegions) { |
| 711 | Log.d(LOG_TAG, "Debugging dirty regions"); |
| 712 | } |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 713 | |
| 714 | mShowOverdraw = SystemProperties.getBoolean( |
| 715 | HardwareRenderer.DEBUG_SHOW_OVERDRAW_PROPERTY, false); |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | @Override |
| 719 | void dumpGfxInfo(PrintWriter pw) { |
| 720 | if (mProfileEnabled) { |
| 721 | pw.printf("\n\tDraw\tProcess\tExecute\n"); |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 722 | |
| 723 | mProfileLock.lock(); |
| 724 | try { |
| 725 | for (int i = 0; i < mProfileData.length; i += PROFILE_FRAME_DATA_COUNT) { |
| 726 | if (mProfileData[i] < 0) { |
| 727 | break; |
| 728 | } |
| 729 | pw.printf("\t%3.2f\t%3.2f\t%3.2f\n", mProfileData[i], mProfileData[i + 1], |
| 730 | mProfileData[i + 2]); |
| 731 | mProfileData[i] = mProfileData[i + 1] = mProfileData[i + 2] = -1; |
Chet Haase | 0928060 | 2012-04-03 16:15:34 -0700 | [diff] [blame] | 732 | } |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 733 | mProfileCurrentFrame = mProfileData.length; |
| 734 | } finally { |
| 735 | mProfileLock.unlock(); |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 736 | } |
| 737 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Michael Jurka | a3fabff | 2012-03-28 17:22:29 +0200 | [diff] [blame] | 740 | @Override |
| 741 | long getFrameCount() { |
| 742 | return mFrameCount; |
| 743 | } |
| 744 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 745 | /** |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 746 | * Indicates whether this renderer instance can track and update dirty regions. |
| 747 | */ |
| 748 | boolean hasDirtyRegions() { |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 749 | return mDirtyRegionsEnabled; |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /** |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 753 | * Checks for OpenGL errors. If an error has occured, {@link #destroy(boolean)} |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 754 | * is invoked and the requested flag is turned off. The error code is |
| 755 | * also logged as a warning. |
| 756 | */ |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 757 | void checkEglErrors() { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 758 | if (isEnabled()) { |
Romain Guy | 740ee65 | 2012-09-17 18:11:40 -0700 | [diff] [blame] | 759 | checkEglErrorsForced(); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | private void checkEglErrorsForced() { |
| 764 | int error = sEgl.eglGetError(); |
| 765 | if (error != EGL_SUCCESS) { |
| 766 | // something bad has happened revert to |
| 767 | // normal rendering. |
| 768 | Log.w(LOG_TAG, "EGL error: " + GLUtils.getEGLErrorString(error)); |
| 769 | fallback(error != EGL11.EGL_CONTEXT_LOST); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 772 | |
Romain Guy | 9745fae | 2010-12-08 11:39:15 -0800 | [diff] [blame] | 773 | private void fallback(boolean fallback) { |
| 774 | destroy(true); |
| 775 | if (fallback) { |
| 776 | // we'll try again if it was context lost |
| 777 | setRequested(false); |
| 778 | Log.w(LOG_TAG, "Mountain View, we've had a problem here. " |
| 779 | + "Switching back to software rendering."); |
| 780 | } |
| 781 | } |
| 782 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 783 | @Override |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 784 | boolean initialize(Surface surface) throws Surface.OutOfResourcesException { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 785 | if (isRequested() && !isEnabled()) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 786 | initializeEgl(); |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 787 | mGl = createEglSurface(surface); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 788 | mDestroyed = false; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 789 | |
| 790 | if (mGl != null) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 791 | int err = sEgl.eglGetError(); |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 792 | if (err != EGL_SUCCESS) { |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 793 | destroy(true); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 794 | setRequested(false); |
| 795 | } else { |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 796 | if (mCanvas == null) { |
| 797 | mCanvas = createCanvas(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 798 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 799 | if (mCanvas != null) { |
| 800 | setEnabled(true); |
| 801 | } else { |
| 802 | Log.w(LOG_TAG, "Hardware accelerated Canvas could not be created"); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | return mCanvas != null; |
| 807 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 808 | } |
| 809 | return false; |
| 810 | } |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 811 | |
| 812 | @Override |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 813 | void updateSurface(Surface surface) throws Surface.OutOfResourcesException { |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 814 | if (isRequested() && isEnabled()) { |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 815 | createEglSurface(surface); |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 816 | } |
| 817 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 818 | |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 819 | abstract HardwareCanvas createCanvas(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 820 | |
Romain Guy | 29d23ec | 2011-07-25 14:42:24 -0700 | [diff] [blame] | 821 | abstract int[] getConfig(boolean dirtyRegions); |
| 822 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 823 | void initializeEgl() { |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 824 | synchronized (sEglLock) { |
| 825 | if (sEgl == null && sEglConfig == null) { |
| 826 | sEgl = (EGL10) EGLContext.getEGL(); |
| 827 | |
| 828 | // Get to the default display. |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 829 | sEglDisplay = sEgl.eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 830 | |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 831 | if (sEglDisplay == EGL_NO_DISPLAY) { |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 832 | throw new RuntimeException("eglGetDisplay failed " |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 833 | + GLUtils.getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | // We can now initialize EGL for that display |
| 837 | int[] version = new int[2]; |
| 838 | if (!sEgl.eglInitialize(sEglDisplay, version)) { |
| 839 | throw new RuntimeException("eglInitialize failed " + |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 840 | GLUtils.getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 841 | } |
Romain Guy | 740ee65 | 2012-09-17 18:11:40 -0700 | [diff] [blame] | 842 | |
| 843 | checkEglErrorsForced(); |
| 844 | |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 845 | sEglConfig = chooseEglConfig(); |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 846 | if (sEglConfig == null) { |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 847 | // We tried to use EGL_SWAP_BEHAVIOR_PRESERVED_BIT, try again without |
| 848 | if (sDirtyRegions) { |
| 849 | sDirtyRegions = false; |
| 850 | sEglConfig = chooseEglConfig(); |
| 851 | if (sEglConfig == null) { |
| 852 | throw new RuntimeException("eglConfig not initialized"); |
| 853 | } |
| 854 | } else { |
| 855 | throw new RuntimeException("eglConfig not initialized"); |
| 856 | } |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 857 | } |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 858 | } |
| 859 | } |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 860 | |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 861 | ManagedEGLContext managedContext = sEglContextStorage.get(); |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 862 | mEglContext = managedContext != null ? managedContext.getContext() : null; |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 863 | mEglThread = Thread.currentThread(); |
| 864 | |
| 865 | if (mEglContext == null) { |
| 866 | mEglContext = createContext(sEgl, sEglDisplay, sEglConfig); |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 867 | sEglContextStorage.set(createManagedContext(mEglContext)); |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 868 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 871 | abstract ManagedEGLContext createManagedContext(EGLContext eglContext); |
| 872 | |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 873 | private EGLConfig chooseEglConfig() { |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 874 | EGLConfig[] configs = new EGLConfig[1]; |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 875 | int[] configsCount = new int[1]; |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 876 | int[] configSpec = getConfig(sDirtyRegions); |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 877 | |
| 878 | // Debug |
Romain Guy | 29d23ec | 2011-07-25 14:42:24 -0700 | [diff] [blame] | 879 | final String debug = SystemProperties.get(PRINT_CONFIG_PROPERTY, ""); |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 880 | if ("all".equalsIgnoreCase(debug)) { |
| 881 | sEgl.eglChooseConfig(sEglDisplay, configSpec, null, 0, configsCount); |
| 882 | |
| 883 | EGLConfig[] debugConfigs = new EGLConfig[configsCount[0]]; |
| 884 | sEgl.eglChooseConfig(sEglDisplay, configSpec, debugConfigs, |
| 885 | configsCount[0], configsCount); |
| 886 | |
| 887 | for (EGLConfig config : debugConfigs) { |
| 888 | printConfig(config); |
| 889 | } |
| 890 | } |
| 891 | |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 892 | if (!sEgl.eglChooseConfig(sEglDisplay, configSpec, configs, 1, configsCount)) { |
| 893 | throw new IllegalArgumentException("eglChooseConfig failed " + |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 894 | GLUtils.getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 895 | } else if (configsCount[0] > 0) { |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 896 | if ("choice".equalsIgnoreCase(debug)) { |
| 897 | printConfig(configs[0]); |
| 898 | } |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 899 | return configs[0]; |
| 900 | } |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 901 | |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 902 | return null; |
| 903 | } |
| 904 | |
Romain Guy | e979e62 | 2012-03-20 13:50:27 -0700 | [diff] [blame] | 905 | private static void printConfig(EGLConfig config) { |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 906 | int[] value = new int[1]; |
| 907 | |
| 908 | Log.d(LOG_TAG, "EGL configuration " + config + ":"); |
| 909 | |
| 910 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_RED_SIZE, value); |
| 911 | Log.d(LOG_TAG, " RED_SIZE = " + value[0]); |
| 912 | |
| 913 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_GREEN_SIZE, value); |
| 914 | Log.d(LOG_TAG, " GREEN_SIZE = " + value[0]); |
| 915 | |
| 916 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_BLUE_SIZE, value); |
| 917 | Log.d(LOG_TAG, " BLUE_SIZE = " + value[0]); |
| 918 | |
| 919 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_ALPHA_SIZE, value); |
| 920 | Log.d(LOG_TAG, " ALPHA_SIZE = " + value[0]); |
| 921 | |
| 922 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_DEPTH_SIZE, value); |
| 923 | Log.d(LOG_TAG, " DEPTH_SIZE = " + value[0]); |
| 924 | |
| 925 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_STENCIL_SIZE, value); |
| 926 | Log.d(LOG_TAG, " STENCIL_SIZE = " + value[0]); |
| 927 | |
Romain Guy | 8efca54 | 2012-10-15 18:09:49 -0700 | [diff] [blame] | 928 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_SAMPLE_BUFFERS, value); |
| 929 | Log.d(LOG_TAG, " SAMPLE_BUFFERS = " + value[0]); |
| 930 | |
| 931 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_SAMPLES, value); |
| 932 | Log.d(LOG_TAG, " SAMPLES = " + value[0]); |
| 933 | |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 934 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_SURFACE_TYPE, value); |
Romain Guy | 29d23ec | 2011-07-25 14:42:24 -0700 | [diff] [blame] | 935 | Log.d(LOG_TAG, " SURFACE_TYPE = 0x" + Integer.toHexString(value[0])); |
Romain Guy | 8efca54 | 2012-10-15 18:09:49 -0700 | [diff] [blame] | 936 | |
| 937 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_CONFIG_CAVEAT, value); |
| 938 | Log.d(LOG_TAG, " CONFIG_CAVEAT = 0x" + Integer.toHexString(value[0])); |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 941 | GL createEglSurface(Surface surface) throws Surface.OutOfResourcesException { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 942 | // Check preconditions. |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 943 | if (sEgl == null) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 944 | throw new RuntimeException("egl not initialized"); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 945 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 946 | if (sEglDisplay == null) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 947 | throw new RuntimeException("eglDisplay not initialized"); |
| 948 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 949 | if (sEglConfig == null) { |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 950 | throw new RuntimeException("eglConfig not initialized"); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 951 | } |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 952 | if (Thread.currentThread() != mEglThread) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 953 | throw new IllegalStateException("HardwareRenderer cannot be used " |
| 954 | + "from multiple threads"); |
| 955 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 956 | |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 957 | // In case we need to destroy an existing surface |
| 958 | destroySurface(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 959 | |
| 960 | // Create an EGL surface we can render into. |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 961 | if (!createSurface(surface)) { |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 962 | return null; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 965 | initCaches(); |
Romain Guy | 6f7d939 | 2011-06-02 14:17:28 -0700 | [diff] [blame] | 966 | |
Romain Guy | 9477c6e | 2011-12-09 12:27:21 -0800 | [diff] [blame] | 967 | return mEglContext.getGL(); |
| 968 | } |
| 969 | |
| 970 | private void enableDirtyRegions() { |
Romain Guy | 6f7d939 | 2011-06-02 14:17:28 -0700 | [diff] [blame] | 971 | // If mDirtyRegions is set, this means we have an EGL configuration |
| 972 | // with EGL_SWAP_BEHAVIOR_PRESERVED_BIT set |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 973 | if (sDirtyRegions) { |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 974 | if (!(mDirtyRegionsEnabled = preserveBackBuffer())) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 975 | Log.w(LOG_TAG, "Backbuffer cannot be preserved"); |
| 976 | } |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 977 | } else if (sDirtyRegionsRequested) { |
Romain Guy | 6f7d939 | 2011-06-02 14:17:28 -0700 | [diff] [blame] | 978 | // If mDirtyRegions is not set, our EGL configuration does not |
| 979 | // have EGL_SWAP_BEHAVIOR_PRESERVED_BIT; however, the default |
| 980 | // swap behavior might be EGL_BUFFER_PRESERVED, which means we |
| 981 | // want to set mDirtyRegions. We try to do this only if dirty |
| 982 | // regions were initially requested as part of the device |
| 983 | // configuration (see RENDER_DIRTY_REGIONS) |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 984 | mDirtyRegionsEnabled = isBackBufferPreserved(); |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 985 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 988 | abstract void initCaches(); |
| 989 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 990 | EGLContext createContext(EGL10 egl, EGLDisplay eglDisplay, EGLConfig eglConfig) { |
Romain Guy | bd43152 | 2012-09-26 13:31:25 -0700 | [diff] [blame] | 991 | int[] attribs = { EGL14.EGL_CONTEXT_CLIENT_VERSION, mGlVersion, EGL_NONE }; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 992 | |
Romain Guy | c002936 | 2012-09-24 20:18:13 -0700 | [diff] [blame] | 993 | EGLContext context = egl.eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, |
| 994 | mGlVersion != 0 ? attribs : null); |
Romain Guy | ed1b6f4 | 2012-09-24 21:42:18 -0700 | [diff] [blame] | 995 | if (context == null || context == EGL_NO_CONTEXT) { |
Romain Guy | c002936 | 2012-09-24 20:18:13 -0700 | [diff] [blame] | 996 | //noinspection ConstantConditions |
| 997 | throw new IllegalStateException( |
| 998 | "Could not create an EGL context. eglCreateContext failed with error: " + |
| 999 | GLUtils.getEGLErrorString(sEgl.eglGetError())); |
| 1000 | } |
| 1001 | return context; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | @Override |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 1005 | void destroy(boolean full) { |
| 1006 | if (full && mCanvas != null) { |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 1007 | mCanvas = null; |
| 1008 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1009 | |
Romain Guy | 357c942 | 2011-08-02 11:32:49 -0700 | [diff] [blame] | 1010 | if (!isEnabled() || mDestroyed) { |
| 1011 | setEnabled(false); |
| 1012 | return; |
| 1013 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1014 | |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 1015 | destroySurface(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1016 | setEnabled(false); |
Romain Guy | 357c942 | 2011-08-02 11:32:49 -0700 | [diff] [blame] | 1017 | |
| 1018 | mDestroyed = true; |
| 1019 | mGl = null; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 1022 | void destroySurface() { |
| 1023 | if (mEglSurface != null && mEglSurface != EGL_NO_SURFACE) { |
| 1024 | sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 1025 | sEgl.eglDestroySurface(sEglDisplay, mEglSurface); |
| 1026 | mEglSurface = null; |
| 1027 | } |
| 1028 | } |
| 1029 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1030 | @Override |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1031 | void invalidate(Surface surface) { |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 1032 | // Cancels any existing buffer to ensure we'll get a buffer |
| 1033 | // of the right size before we call eglSwapBuffers |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 1034 | sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1035 | |
Romain Guy | d3facf3 | 2011-08-03 11:14:38 -0700 | [diff] [blame] | 1036 | if (mEglSurface != null && mEglSurface != EGL_NO_SURFACE) { |
| 1037 | sEgl.eglDestroySurface(sEglDisplay, mEglSurface); |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1038 | mEglSurface = null; |
| 1039 | setEnabled(false); |
Romain Guy | d3facf3 | 2011-08-03 11:14:38 -0700 | [diff] [blame] | 1040 | } |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 1041 | |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1042 | if (surface.isValid()) { |
| 1043 | if (!createSurface(surface)) { |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1044 | return; |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 1045 | } |
Romain Guy | 9477c6e | 2011-12-09 12:27:21 -0800 | [diff] [blame] | 1046 | |
| 1047 | mUpdateDirtyRegions = true; |
| 1048 | |
Romain Guy | 86e3e22 | 2011-08-16 14:30:08 -0700 | [diff] [blame] | 1049 | if (mCanvas != null) { |
| 1050 | setEnabled(true); |
| 1051 | } |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 1052 | } |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1053 | } |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 1054 | |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1055 | private boolean createSurface(Surface surface) { |
| 1056 | mEglSurface = sEgl.eglCreateWindowSurface(sEglDisplay, sEglConfig, surface, null); |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1057 | |
| 1058 | if (mEglSurface == null || mEglSurface == EGL_NO_SURFACE) { |
| 1059 | int error = sEgl.eglGetError(); |
| 1060 | if (error == EGL_BAD_NATIVE_WINDOW) { |
| 1061 | Log.e(LOG_TAG, "createWindowSurface returned EGL_BAD_NATIVE_WINDOW."); |
| 1062 | return false; |
| 1063 | } |
| 1064 | throw new RuntimeException("createWindowSurface failed " |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 1065 | + GLUtils.getEGLErrorString(error)); |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1066 | } |
Romain Guy | bd43152 | 2012-09-26 13:31:25 -0700 | [diff] [blame] | 1067 | |
| 1068 | if (!sEgl.eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, mEglContext)) { |
| 1069 | throw new IllegalStateException("eglMakeCurrent failed " + |
| 1070 | GLUtils.getEGLErrorString(sEgl.eglGetError())); |
| 1071 | } |
| 1072 | |
| 1073 | enableDirtyRegions(); |
| 1074 | |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1075 | return true; |
| 1076 | } |
| 1077 | |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1078 | @Override |
| 1079 | boolean validate() { |
| 1080 | return checkCurrent() != SURFACE_STATE_ERROR; |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | @Override |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1084 | void setup(int width, int height) { |
Romain Guy | eca9b1f | 2011-08-26 13:36:37 -0700 | [diff] [blame] | 1085 | if (validate()) { |
| 1086 | mCanvas.setViewport(width, height); |
Chet Haase | 40e0383 | 2011-10-06 08:34:13 -0700 | [diff] [blame] | 1087 | mWidth = width; |
| 1088 | mHeight = height; |
Romain Guy | eca9b1f | 2011-08-26 13:36:37 -0700 | [diff] [blame] | 1089 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1090 | } |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1091 | |
Chet Haase | 40e0383 | 2011-10-06 08:34:13 -0700 | [diff] [blame] | 1092 | @Override |
| 1093 | int getWidth() { |
| 1094 | return mWidth; |
| 1095 | } |
| 1096 | |
| 1097 | @Override |
| 1098 | int getHeight() { |
| 1099 | return mHeight; |
| 1100 | } |
| 1101 | |
Chet Haase | 08837c2 | 2011-11-28 11:53:21 -0800 | [diff] [blame] | 1102 | @Override |
| 1103 | HardwareCanvas getCanvas() { |
| 1104 | return mCanvas; |
| 1105 | } |
| 1106 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1107 | boolean canDraw() { |
| 1108 | return mGl != null && mCanvas != null; |
| 1109 | } |
| 1110 | |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 1111 | int onPreDraw(Rect dirty) { |
| 1112 | return DisplayList.STATUS_DONE; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 1115 | void onPostDraw() { |
| 1116 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1117 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 1118 | class FunctorsRunnable implements Runnable { |
| 1119 | View.AttachInfo attachInfo; |
| 1120 | |
| 1121 | @Override |
| 1122 | public void run() { |
| 1123 | final HardwareRenderer renderer = attachInfo.mHardwareRenderer; |
| 1124 | if (renderer == null || !renderer.isEnabled() || renderer != GlRenderer.this) { |
| 1125 | return; |
| 1126 | } |
| 1127 | |
| 1128 | final int surfaceState = checkCurrent(); |
| 1129 | if (surfaceState != SURFACE_STATE_ERROR) { |
| 1130 | int status = mCanvas.invokeFunctors(mRedrawClip); |
| 1131 | handleFunctorStatus(attachInfo, status); |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1136 | @Override |
Romain Guy | 50d133e | 2011-08-12 15:25:28 -0700 | [diff] [blame] | 1137 | boolean draw(View view, View.AttachInfo attachInfo, HardwareDrawCallbacks callbacks, |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1138 | Rect dirty) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1139 | if (canDraw()) { |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 1140 | if (!hasDirtyRegions()) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1141 | dirty = null; |
| 1142 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1143 | attachInfo.mIgnoreDirtyState = true; |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 1144 | attachInfo.mDrawingTime = SystemClock.uptimeMillis(); |
| 1145 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1146 | view.mPrivateFlags |= View.PFLAG_DRAWN; |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1147 | |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 1148 | final int surfaceState = checkCurrent(); |
| 1149 | if (surfaceState != SURFACE_STATE_ERROR) { |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 1150 | HardwareCanvas canvas = mCanvas; |
| 1151 | attachInfo.mHardwareCanvas = canvas; |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 1152 | |
| 1153 | if (mProfileEnabled) { |
| 1154 | mProfileLock.lock(); |
| 1155 | } |
| 1156 | |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 1157 | // We had to change the current surface and/or context, redraw everything |
| 1158 | if (surfaceState == SURFACE_STATE_UPDATED) { |
| 1159 | dirty = null; |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 1160 | beginFrame(null); |
| 1161 | } else { |
| 1162 | int[] size = mSurfaceSize; |
| 1163 | beginFrame(size); |
| 1164 | |
| 1165 | if (size[1] != mHeight || size[0] != mWidth) { |
| 1166 | mWidth = size[0]; |
| 1167 | mHeight = size[1]; |
| 1168 | |
| 1169 | canvas.setViewport(mWidth, mHeight); |
| 1170 | |
| 1171 | dirty = null; |
| 1172 | } |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 1175 | int saveCount = 0; |
| 1176 | int status = DisplayList.STATUS_DONE; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1177 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 1178 | try { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1179 | view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED) |
| 1180 | == View.PFLAG_INVALIDATED; |
| 1181 | view.mPrivateFlags &= ~View.PFLAG_INVALIDATED; |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1182 | |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 1183 | long getDisplayListStartTime = 0; |
| 1184 | if (mProfileEnabled) { |
| 1185 | mProfileCurrentFrame += PROFILE_FRAME_DATA_COUNT; |
| 1186 | if (mProfileCurrentFrame >= mProfileData.length) { |
| 1187 | mProfileCurrentFrame = 0; |
| 1188 | } |
| 1189 | |
Jeff Brown | 95db2b2 | 2011-11-30 19:54:41 -0800 | [diff] [blame] | 1190 | getDisplayListStartTime = System.nanoTime(); |
| 1191 | } |
| 1192 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 1193 | canvas.clearLayerUpdates(); |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 1194 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 1195 | DisplayList displayList; |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 1196 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "getDisplayList"); |
| 1197 | try { |
| 1198 | displayList = view.getDisplayList(); |
| 1199 | } finally { |
| 1200 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 1201 | } |
Jeff Brown | 95db2b2 | 2011-11-30 19:54:41 -0800 | [diff] [blame] | 1202 | |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 1203 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "prepareFrame"); |
| 1204 | try { |
| 1205 | status = onPreDraw(dirty); |
| 1206 | } finally { |
| 1207 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 1208 | } |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 1209 | saveCount = canvas.save(); |
| 1210 | callbacks.onHardwarePreDraw(canvas); |
| 1211 | |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 1212 | if (mProfileEnabled) { |
Jeff Brown | 95db2b2 | 2011-11-30 19:54:41 -0800 | [diff] [blame] | 1213 | long now = System.nanoTime(); |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 1214 | float total = (now - getDisplayListStartTime) * 0.000001f; |
| 1215 | //noinspection PointlessArithmeticExpression |
| 1216 | mProfileData[mProfileCurrentFrame] = total; |
Jeff Brown | 95db2b2 | 2011-11-30 19:54:41 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1219 | if (displayList != null) { |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 1220 | long drawDisplayListStartTime = 0; |
| 1221 | if (mProfileEnabled) { |
Jeff Brown | 95db2b2 | 2011-11-30 19:54:41 -0800 | [diff] [blame] | 1222 | drawDisplayListStartTime = System.nanoTime(); |
| 1223 | } |
| 1224 | |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 1225 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "drawDisplayList"); |
| 1226 | try { |
| 1227 | status |= canvas.drawDisplayList(displayList, mRedrawClip, |
| 1228 | DisplayList.FLAG_CLIP_CHILDREN); |
| 1229 | } finally { |
| 1230 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 1231 | } |
Jeff Brown | 95db2b2 | 2011-11-30 19:54:41 -0800 | [diff] [blame] | 1232 | |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 1233 | if (mProfileEnabled) { |
Jeff Brown | 95db2b2 | 2011-11-30 19:54:41 -0800 | [diff] [blame] | 1234 | long now = System.nanoTime(); |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 1235 | float total = (now - drawDisplayListStartTime) * 0.000001f; |
| 1236 | mProfileData[mProfileCurrentFrame + 1] = total; |
Jeff Brown | 95db2b2 | 2011-11-30 19:54:41 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 1239 | handleFunctorStatus(attachInfo, status); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1240 | } else { |
| 1241 | // Shouldn't reach here |
| 1242 | view.draw(canvas); |
| 1243 | } |
Romain Guy | b04f7e9 | 2012-02-15 12:36:54 -0800 | [diff] [blame] | 1244 | } finally { |
| 1245 | callbacks.onHardwarePostDraw(canvas); |
| 1246 | canvas.restoreToCount(saveCount); |
| 1247 | view.mRecreateDisplayList = false; |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 1248 | |
Michael Jurka | a3fabff | 2012-03-28 17:22:29 +0200 | [diff] [blame] | 1249 | mFrameCount++; |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 1250 | |
Romain Guy | b04f7e9 | 2012-02-15 12:36:54 -0800 | [diff] [blame] | 1251 | if (mDebugDirtyRegions) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1252 | if (mDebugPaint == null) { |
| 1253 | mDebugPaint = new Paint(); |
| 1254 | mDebugPaint.setColor(0x7fff0000); |
| 1255 | } |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 1256 | |
Michael Jurka | a3fabff | 2012-03-28 17:22:29 +0200 | [diff] [blame] | 1257 | if (dirty != null && (mFrameCount & 1) == 0) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1258 | canvas.drawRect(dirty, mDebugPaint); |
| 1259 | } |
| 1260 | } |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 1261 | } |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1262 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 1263 | onPostDraw(); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1264 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 1265 | attachInfo.mIgnoreDirtyState = false; |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1266 | |
| 1267 | if ((status & DisplayList.STATUS_DREW) == DisplayList.STATUS_DREW) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1268 | long eglSwapBuffersStartTime = 0; |
| 1269 | if (mProfileEnabled) { |
| 1270 | eglSwapBuffersStartTime = System.nanoTime(); |
| 1271 | } |
| 1272 | |
| 1273 | sEgl.eglSwapBuffers(sEglDisplay, mEglSurface); |
| 1274 | |
| 1275 | if (mProfileEnabled) { |
| 1276 | long now = System.nanoTime(); |
| 1277 | float total = (now - eglSwapBuffersStartTime) * 0.000001f; |
| 1278 | mProfileData[mProfileCurrentFrame + 2] = total; |
| 1279 | } |
| 1280 | |
| 1281 | checkEglErrors(); |
Jeff Brown | 95db2b2 | 2011-11-30 19:54:41 -0800 | [diff] [blame] | 1282 | } |
| 1283 | |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 1284 | if (mProfileEnabled) { |
| 1285 | mProfileLock.unlock(); |
| 1286 | } |
| 1287 | |
Romain Guy | 50d133e | 2011-08-12 15:25:28 -0700 | [diff] [blame] | 1288 | return dirty == null; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1289 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1290 | } |
Romain Guy | 50d133e | 2011-08-12 15:25:28 -0700 | [diff] [blame] | 1291 | |
| 1292 | return false; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1293 | } |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1294 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 1295 | private void handleFunctorStatus(View.AttachInfo attachInfo, int status) { |
| 1296 | // If the draw flag is set, functors will be invoked while executing |
| 1297 | // the tree of display lists |
| 1298 | if ((status & DisplayList.STATUS_DRAW) != 0) { |
| 1299 | if (mRedrawClip.isEmpty()) { |
| 1300 | attachInfo.mViewRootImpl.invalidate(); |
| 1301 | } else { |
| 1302 | attachInfo.mViewRootImpl.invalidateChildInParent(null, mRedrawClip); |
| 1303 | mRedrawClip.setEmpty(); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | if ((status & DisplayList.STATUS_INVOKE) != 0) { |
Chris Craik | cb82b94 | 2012-06-12 17:23:28 -0700 | [diff] [blame] | 1308 | scheduleFunctors(attachInfo, true); |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | |
Chris Craik | cb82b94 | 2012-06-12 17:23:28 -0700 | [diff] [blame] | 1312 | private void scheduleFunctors(View.AttachInfo attachInfo, boolean delayed) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1313 | mFunctorsRunnable.attachInfo = attachInfo; |
| 1314 | if (!attachInfo.mHandler.hasCallbacks(mFunctorsRunnable)) { |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 1315 | // delay the functor callback by a few ms so it isn't polled constantly |
Chris Craik | cb82b94 | 2012-06-12 17:23:28 -0700 | [diff] [blame] | 1316 | attachInfo.mHandler.postDelayed(mFunctorsRunnable, |
| 1317 | delayed ? FUNCTOR_PROCESS_DELAY : 0); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 1318 | } |
| 1319 | } |
| 1320 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1321 | @Override |
| 1322 | void detachFunctor(int functor) { |
| 1323 | if (mCanvas != null) { |
| 1324 | mCanvas.detachFunctor(functor); |
Chris Craik | 932b7f6 | 2012-06-06 13:59:33 -0700 | [diff] [blame] | 1325 | } |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | @Override |
Chris Craik | 41ee465 | 2012-05-31 15:05:57 -0700 | [diff] [blame] | 1329 | boolean attachFunctor(View.AttachInfo attachInfo, int functor) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1330 | if (mCanvas != null) { |
| 1331 | mCanvas.attachFunctor(functor); |
Chris Craik | cb82b94 | 2012-06-12 17:23:28 -0700 | [diff] [blame] | 1332 | scheduleFunctors(attachInfo, false); |
Chris Craik | 41ee465 | 2012-05-31 15:05:57 -0700 | [diff] [blame] | 1333 | return true; |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1334 | } |
Chris Craik | 41ee465 | 2012-05-31 15:05:57 -0700 | [diff] [blame] | 1335 | return false; |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1338 | /** |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1339 | * Ensures the current EGL context is the one we expect. |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1340 | * |
| 1341 | * @return {@link #SURFACE_STATE_ERROR} if the correct EGL context cannot be made current, |
| 1342 | * {@link #SURFACE_STATE_UPDATED} if the EGL context was changed or |
| 1343 | * {@link #SURFACE_STATE_SUCCESS} if the EGL context was the correct one |
| 1344 | */ |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 1345 | int checkCurrent() { |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1346 | if (mEglThread != Thread.currentThread()) { |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1347 | throw new IllegalStateException("Hardware acceleration can only be used with a " + |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1348 | "single UI thread.\nOriginal thread: " + mEglThread + "\n" + |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1349 | "Current thread: " + Thread.currentThread()); |
| 1350 | } |
| 1351 | |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1352 | if (!mEglContext.equals(sEgl.eglGetCurrentContext()) || |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1353 | !mEglSurface.equals(sEgl.eglGetCurrentSurface(EGL_DRAW))) { |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1354 | if (!sEgl.eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, mEglContext)) { |
Romain Guy | 9745fae | 2010-12-08 11:39:15 -0800 | [diff] [blame] | 1355 | Log.e(LOG_TAG, "eglMakeCurrent failed " + |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 1356 | GLUtils.getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | eca9b1f | 2011-08-26 13:36:37 -0700 | [diff] [blame] | 1357 | fallback(true); |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 1358 | return SURFACE_STATE_ERROR; |
| 1359 | } else { |
Romain Guy | 9477c6e | 2011-12-09 12:27:21 -0800 | [diff] [blame] | 1360 | if (mUpdateDirtyRegions) { |
| 1361 | enableDirtyRegions(); |
| 1362 | mUpdateDirtyRegions = false; |
| 1363 | } |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 1364 | return SURFACE_STATE_UPDATED; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1365 | } |
| 1366 | } |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 1367 | return SURFACE_STATE_SUCCESS; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1368 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1369 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1370 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1371 | /** |
| 1372 | * Hardware renderer using OpenGL ES 2.0. |
| 1373 | */ |
| 1374 | static class Gl20Renderer extends GlRenderer { |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1375 | private GLES20Canvas mGlCanvas; |
| 1376 | |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 1377 | private static EGLSurface sPbuffer; |
| 1378 | private static final Object[] sPbufferLock = new Object[0]; |
| 1379 | |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 1380 | static class Gl20RendererEglContext extends ManagedEGLContext { |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1381 | final Handler mHandler = new Handler(); |
| 1382 | |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 1383 | public Gl20RendererEglContext(EGLContext context) { |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1384 | super(context); |
| 1385 | } |
| 1386 | |
| 1387 | @Override |
| 1388 | public void onTerminate(final EGLContext eglContext) { |
| 1389 | // Make sure we do this on the correct thread. |
| 1390 | if (mHandler.getLooper() != Looper.myLooper()) { |
| 1391 | mHandler.post(new Runnable() { |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 1392 | @Override |
| 1393 | public void run() { |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1394 | onTerminate(eglContext); |
| 1395 | } |
| 1396 | }); |
| 1397 | return; |
| 1398 | } |
| 1399 | |
| 1400 | synchronized (sEglLock) { |
| 1401 | if (sEgl == null) return; |
| 1402 | |
| 1403 | if (EGLImpl.getInitCount(sEglDisplay) == 1) { |
| 1404 | usePbufferSurface(eglContext); |
| 1405 | GLES20Canvas.terminateCaches(); |
| 1406 | |
| 1407 | sEgl.eglDestroyContext(sEglDisplay, eglContext); |
Romain Guy | a998dff | 2012-03-23 18:58:36 -0700 | [diff] [blame] | 1408 | sEglContextStorage.set(null); |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1409 | sEglContextStorage.remove(); |
| 1410 | |
| 1411 | sEgl.eglDestroySurface(sEglDisplay, sPbuffer); |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 1412 | sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, |
| 1413 | EGL_NO_SURFACE, EGL_NO_CONTEXT); |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1414 | |
| 1415 | sEgl.eglReleaseThread(); |
| 1416 | sEgl.eglTerminate(sEglDisplay); |
| 1417 | |
| 1418 | sEgl = null; |
| 1419 | sEglDisplay = null; |
| 1420 | sEglConfig = null; |
| 1421 | sPbuffer = null; |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | } |
| 1426 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1427 | Gl20Renderer(boolean translucent) { |
| 1428 | super(2, translucent); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | @Override |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 1432 | HardwareCanvas createCanvas() { |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 1433 | return mGlCanvas = new GLES20Canvas(mTranslucent); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1434 | } |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1435 | |
| 1436 | @Override |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 1437 | ManagedEGLContext createManagedContext(EGLContext eglContext) { |
| 1438 | return new Gl20Renderer.Gl20RendererEglContext(mEglContext); |
| 1439 | } |
| 1440 | |
| 1441 | @Override |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1442 | int[] getConfig(boolean dirtyRegions) { |
| 1443 | return new int[] { |
Romain Guy | bd43152 | 2012-09-26 13:31:25 -0700 | [diff] [blame] | 1444 | EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT, |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1445 | EGL_RED_SIZE, 8, |
| 1446 | EGL_GREEN_SIZE, 8, |
| 1447 | EGL_BLUE_SIZE, 8, |
| 1448 | EGL_ALPHA_SIZE, 8, |
| 1449 | EGL_DEPTH_SIZE, 0, |
Romain Guy | 8efca54 | 2012-10-15 18:09:49 -0700 | [diff] [blame] | 1450 | EGL_CONFIG_CAVEAT, EGL_NONE, |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 1451 | // TODO: Find a better way to choose the stencil size |
| 1452 | EGL_STENCIL_SIZE, mShowOverdraw ? GLES20Canvas.getStencilSize() : 0, |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1453 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT | |
Romain Guy | bd43152 | 2012-09-26 13:31:25 -0700 | [diff] [blame] | 1454 | (dirtyRegions ? EGL14.EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0), |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1455 | EGL_NONE |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1456 | }; |
| 1457 | } |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 1458 | |
| 1459 | @Override |
| 1460 | void initCaches() { |
| 1461 | GLES20Canvas.initCaches(); |
| 1462 | } |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1463 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 1464 | @Override |
| 1465 | boolean canDraw() { |
| 1466 | return super.canDraw() && mGlCanvas != null; |
| 1467 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1468 | |
| 1469 | @Override |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 1470 | int onPreDraw(Rect dirty) { |
| 1471 | return mGlCanvas.onPreDraw(dirty); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 1474 | @Override |
| 1475 | void onPostDraw() { |
| 1476 | mGlCanvas.onPostDraw(); |
| 1477 | } |
| 1478 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1479 | @Override |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 1480 | void destroy(boolean full) { |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 1481 | try { |
| 1482 | super.destroy(full); |
| 1483 | } finally { |
| 1484 | if (full && mGlCanvas != null) { |
| 1485 | mGlCanvas = null; |
| 1486 | } |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | @Override |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1491 | void setup(int width, int height) { |
| 1492 | super.setup(width, height); |
| 1493 | if (mVsyncDisabled) { |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 1494 | disableVsync(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | @Override |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 1499 | void pushLayerUpdate(HardwareLayer layer) { |
| 1500 | mGlCanvas.pushLayerUpdate(layer); |
| 1501 | } |
| 1502 | |
| 1503 | @Override |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 1504 | public DisplayList createDisplayList(String name) { |
| 1505 | return new GLES20DisplayList(name); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1506 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1507 | |
| 1508 | @Override |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 1509 | HardwareLayer createHardwareLayer(boolean isOpaque) { |
| 1510 | return new GLES20TextureLayer(isOpaque); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 1513 | @Override |
| 1514 | HardwareLayer createHardwareLayer(int width, int height, boolean isOpaque) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1515 | return new GLES20RenderLayer(width, height, isOpaque); |
| 1516 | } |
| 1517 | |
| 1518 | @Override |
Romain Guy | e5e0c50 | 2011-06-15 15:18:31 -0700 | [diff] [blame] | 1519 | SurfaceTexture createSurfaceTexture(HardwareLayer layer) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1520 | return ((GLES20TextureLayer) layer).getSurfaceTexture(); |
| 1521 | } |
| 1522 | |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 1523 | @Override |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 1524 | void setSurfaceTexture(HardwareLayer layer, SurfaceTexture surfaceTexture) { |
| 1525 | ((GLES20TextureLayer) layer).setSurfaceTexture(surfaceTexture); |
| 1526 | } |
| 1527 | |
| 1528 | @Override |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 1529 | boolean safelyRun(Runnable action) { |
| 1530 | boolean needsContext = true; |
| 1531 | if (isEnabled() && checkCurrent() != SURFACE_STATE_ERROR) needsContext = false; |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 1532 | |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 1533 | if (needsContext) { |
| 1534 | Gl20RendererEglContext managedContext = |
| 1535 | (Gl20RendererEglContext) sEglContextStorage.get(); |
| 1536 | if (managedContext == null) return false; |
| 1537 | usePbufferSurface(managedContext.getContext()); |
| 1538 | } |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 1539 | |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 1540 | try { |
| 1541 | action.run(); |
| 1542 | } finally { |
Jesse Hall | 0872b37 | 2012-04-02 12:22:51 -0700 | [diff] [blame] | 1543 | if (needsContext) { |
| 1544 | sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, |
| 1545 | EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 1546 | } |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 1547 | } |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 1548 | |
| 1549 | return true; |
| 1550 | } |
| 1551 | |
| 1552 | @Override |
Romain Guy | bd17bd3 | 2012-10-23 19:17:15 -0700 | [diff] [blame] | 1553 | void destroyLayers(final View view) { |
| 1554 | if (view != null) { |
| 1555 | safelyRun(new Runnable() { |
| 1556 | @Override |
| 1557 | public void run() { |
| 1558 | if (mCanvas != null) { |
| 1559 | mCanvas.clearLayerUpdates(); |
| 1560 | } |
| 1561 | destroyHardwareLayer(view); |
| 1562 | GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_LAYERS); |
| 1563 | } |
| 1564 | }); |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | private static void destroyHardwareLayer(View view) { |
| 1569 | view.destroyLayer(true); |
| 1570 | |
| 1571 | if (view instanceof ViewGroup) { |
| 1572 | ViewGroup group = (ViewGroup) view; |
| 1573 | |
| 1574 | int count = group.getChildCount(); |
| 1575 | for (int i = 0; i < count; i++) { |
| 1576 | destroyHardwareLayer(group.getChildAt(i)); |
| 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | @Override |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 1582 | void destroyHardwareResources(final View view) { |
| 1583 | if (view != null) { |
| 1584 | safelyRun(new Runnable() { |
| 1585 | @Override |
| 1586 | public void run() { |
Chet Haase | 6a2d17f | 2012-09-30 12:14:13 -0700 | [diff] [blame] | 1587 | if (mCanvas != null) { |
| 1588 | mCanvas.clearLayerUpdates(); |
| 1589 | } |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 1590 | destroyResources(view); |
| 1591 | GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_LAYERS); |
| 1592 | } |
| 1593 | }); |
| 1594 | } |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 1595 | } |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 1596 | |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 1597 | private static void destroyResources(View view) { |
| 1598 | view.destroyHardwareResources(); |
| 1599 | |
| 1600 | if (view instanceof ViewGroup) { |
| 1601 | ViewGroup group = (ViewGroup) view; |
| 1602 | |
| 1603 | int count = group.getChildCount(); |
| 1604 | for (int i = 0; i < count; i++) { |
| 1605 | destroyResources(group.getChildAt(i)); |
| 1606 | } |
| 1607 | } |
| 1608 | } |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 1609 | |
| 1610 | static HardwareRenderer create(boolean translucent) { |
| 1611 | if (GLES20Canvas.isAvailable()) { |
| 1612 | return new Gl20Renderer(translucent); |
| 1613 | } |
| 1614 | return null; |
| 1615 | } |
| 1616 | |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 1617 | static void startTrimMemory(int level) { |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 1618 | if (sEgl == null || sEglConfig == null) return; |
| 1619 | |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 1620 | Gl20RendererEglContext managedContext = |
| 1621 | (Gl20RendererEglContext) sEglContextStorage.get(); |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 1622 | // We do not have OpenGL objects |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1623 | if (managedContext == null) { |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 1624 | return; |
| 1625 | } else { |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1626 | usePbufferSurface(managedContext.getContext()); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1627 | } |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 1628 | |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 1629 | if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) { |
| 1630 | GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_FULL); |
| 1631 | } else if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { |
| 1632 | GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_MODERATE); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 1633 | } |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 1634 | } |
Jesse Hall | 0872b37 | 2012-04-02 12:22:51 -0700 | [diff] [blame] | 1635 | |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 1636 | static void endTrimMemory() { |
| 1637 | if (sEgl != null && sEglDisplay != null) { |
| 1638 | sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 1639 | } |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 1640 | } |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 1641 | |
| 1642 | private static void usePbufferSurface(EGLContext eglContext) { |
| 1643 | synchronized (sPbufferLock) { |
| 1644 | // Create a temporary 1x1 pbuffer so we have a context |
| 1645 | // to clear our OpenGL objects |
| 1646 | if (sPbuffer == null) { |
| 1647 | sPbuffer = sEgl.eglCreatePbufferSurface(sEglDisplay, sEglConfig, new int[] { |
| 1648 | EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE |
| 1649 | }); |
| 1650 | } |
| 1651 | } |
| 1652 | sEgl.eglMakeCurrent(sEglDisplay, sPbuffer, sPbuffer, eglContext); |
| 1653 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1654 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1655 | } |