Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1 | /* |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 17 | package android.view; |
| 18 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 19 | import android.content.ComponentCallbacks2; |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 20 | import android.graphics.Color; |
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; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 28 | import android.os.IBinder; |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 29 | import android.os.Looper; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 30 | import android.os.RemoteException; |
| 31 | import android.os.ServiceManager; |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 32 | import android.os.SystemClock; |
| 33 | import android.os.SystemProperties; |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 34 | import android.os.Trace; |
Romain Guy | 98e4a52 | 2013-01-07 10:58:34 -0800 | [diff] [blame] | 35 | import android.util.DisplayMetrics; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 36 | import android.util.Log; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 37 | import com.google.android.gles_jni.EGLImpl; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 38 | |
| 39 | import javax.microedition.khronos.egl.EGL10; |
| 40 | import javax.microedition.khronos.egl.EGL11; |
| 41 | import javax.microedition.khronos.egl.EGLConfig; |
| 42 | import javax.microedition.khronos.egl.EGLContext; |
| 43 | import javax.microedition.khronos.egl.EGLDisplay; |
| 44 | import javax.microedition.khronos.egl.EGLSurface; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 45 | import javax.microedition.khronos.opengles.GL; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 46 | |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 47 | import java.io.File; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 48 | import java.io.PrintWriter; |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 49 | import java.util.concurrent.locks.ReentrantLock; |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 50 | |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 51 | import static javax.microedition.khronos.egl.EGL10.*; |
| 52 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 53 | /** |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 54 | * Interface for rendering a view hierarchy using hardware acceleration. |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 55 | * |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 56 | * @hide |
| 57 | */ |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 58 | public abstract class HardwareRenderer { |
Romain Guy | 4f6aff3 | 2011-01-12 16:21:41 -0800 | [diff] [blame] | 59 | static final String LOG_TAG = "HardwareRenderer"; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 60 | |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 61 | /** |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 62 | * Name of the file that holds the shaders cache. |
| 63 | */ |
| 64 | private static final String CACHE_PATH_SHADERS = "com.android.opengl.shaders_cache"; |
| 65 | |
| 66 | /** |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 67 | * 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] | 68 | * When turned on the property defined by {@link #RENDER_DIRTY_REGIONS_PROPERTY} |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 69 | * must also have the value "true". |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 70 | */ |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 71 | static final boolean RENDER_DIRTY_REGIONS = true; |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 74 | * System property used to enable or disable dirty regions invalidation. |
| 75 | * This property is only queried if {@link #RENDER_DIRTY_REGIONS} is true. |
| 76 | * The default value of this property is assumed to be true. |
| 77 | * |
| 78 | * Possible values: |
| 79 | * "true", to enable partial invalidates |
| 80 | * "false", to disable partial invalidates |
| 81 | */ |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 82 | static final String RENDER_DIRTY_REGIONS_PROPERTY = "debug.hwui.render_dirty_regions"; |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 85 | * System property used to enable or disable hardware rendering profiling. |
| 86 | * The default value of this property is assumed to be false. |
Chet Haase | 0928060 | 2012-04-03 16:15:34 -0700 | [diff] [blame] | 87 | * |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 88 | * When profiling is enabled, the adb shell dumpsys gfxinfo command will |
| 89 | * output extra information about the time taken to execute by the last |
| 90 | * frames. |
| 91 | * |
| 92 | * Possible values: |
| 93 | * "true", to enable profiling |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 94 | * "visual_bars", to enable profiling and visualize the results on screen |
| 95 | * "visual_lines", to enable profiling and visualize the results on screen |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 96 | * "false", to disable profiling |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 97 | * |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 98 | * @see #PROFILE_PROPERTY_VISUALIZE_BARS |
| 99 | * @see #PROFILE_PROPERTY_VISUALIZE_LINES |
Romain Guy | a4fef02 | 2013-01-07 11:18:38 -0800 | [diff] [blame] | 100 | * |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 101 | * @hide |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 102 | */ |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 103 | public static final String PROFILE_PROPERTY = "debug.hwui.profile"; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 104 | |
| 105 | /** |
Romain Guy | a4fef02 | 2013-01-07 11:18:38 -0800 | [diff] [blame] | 106 | * Value for {@link #PROFILE_PROPERTY}. When the property is set to this |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 107 | * value, profiling data will be visualized on screen as a bar chart. |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 108 | * |
| 109 | * @hide |
| 110 | */ |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 111 | public static final String PROFILE_PROPERTY_VISUALIZE_BARS = "visual_bars"; |
| 112 | |
| 113 | /** |
| 114 | * Value for {@link #PROFILE_PROPERTY}. When the property is set to this |
| 115 | * value, profiling data will be visualized on screen as a line chart. |
| 116 | * |
| 117 | * @hide |
| 118 | */ |
| 119 | public static final String PROFILE_PROPERTY_VISUALIZE_LINES = "visual_lines"; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 120 | |
| 121 | /** |
Chet Haase | 0928060 | 2012-04-03 16:15:34 -0700 | [diff] [blame] | 122 | * System property used to specify the number of frames to be used |
| 123 | * when doing hardware rendering profiling. |
| 124 | * The default value of this property is #PROFILE_MAX_FRAMES. |
| 125 | * |
| 126 | * When profiling is enabled, the adb shell dumpsys gfxinfo command will |
| 127 | * output extra information about the time taken to execute by the last |
| 128 | * frames. |
| 129 | * |
| 130 | * Possible values: |
| 131 | * "60", to set the limit of frames to 60 |
| 132 | */ |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 133 | static final String PROFILE_MAXFRAMES_PROPERTY = "debug.hwui.profile.maxframes"; |
Chet Haase | 0928060 | 2012-04-03 16:15:34 -0700 | [diff] [blame] | 134 | |
| 135 | /** |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 136 | * System property used to debug EGL configuration choice. |
| 137 | * |
| 138 | * Possible values: |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 139 | * "choice", print the chosen configuration only |
| 140 | * "all", print all possible configurations |
| 141 | */ |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 142 | static final String PRINT_CONFIG_PROPERTY = "debug.hwui.print_config"; |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 145 | * Turn on to draw dirty regions every other frame. |
Romain Guy | b04f7e9 | 2012-02-15 12:36:54 -0800 | [diff] [blame] | 146 | * |
| 147 | * Possible values: |
| 148 | * "true", to enable dirty regions debugging |
| 149 | * "false", to disable dirty regions debugging |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 150 | * |
| 151 | * @hide |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 152 | */ |
Romain Guy | 4b8c4f8 | 2012-04-27 15:48:35 -0700 | [diff] [blame] | 153 | 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] | 154 | |
| 155 | /** |
| 156 | * Turn on to flash hardware layers when they update. |
| 157 | * |
| 158 | * Possible values: |
| 159 | * "true", to enable hardware layers updates debugging |
| 160 | * "false", to disable hardware layers updates debugging |
| 161 | * |
| 162 | * @hide |
| 163 | */ |
| 164 | public static final String DEBUG_SHOW_LAYERS_UPDATES_PROPERTY = |
| 165 | "debug.hwui.show_layers_updates"; |
| 166 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 167 | /** |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 168 | * Controls overdraw debugging. |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 169 | * |
| 170 | * Possible values: |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 171 | * "false", to disable overdraw debugging |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 172 | * "show", to show overdraw areas on screen |
| 173 | * "count", to display an overdraw counter |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 174 | * |
| 175 | * @hide |
| 176 | */ |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 177 | public static final String DEBUG_OVERDRAW_PROPERTY = "debug.hwui.overdraw"; |
| 178 | |
| 179 | /** |
| 180 | * Value for {@link #DEBUG_OVERDRAW_PROPERTY}. When the property is set to this |
| 181 | * value, overdraw will be shown on screen by coloring pixels. |
| 182 | * |
| 183 | * @hide |
| 184 | */ |
| 185 | public static final String OVERDRAW_PROPERTY_SHOW = "show"; |
| 186 | |
| 187 | /** |
| 188 | * Value for {@link #DEBUG_OVERDRAW_PROPERTY}. When the property is set to this |
| 189 | * value, an overdraw counter will be shown on screen. |
| 190 | * |
| 191 | * @hide |
| 192 | */ |
| 193 | public static final String OVERDRAW_PROPERTY_COUNT = "count"; |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 194 | |
| 195 | /** |
Romain Guy | 08bca88 | 2013-02-25 16:21:30 -0800 | [diff] [blame] | 196 | * Turn on to debug non-rectangular clip operations. |
| 197 | * |
| 198 | * Possible values: |
| 199 | * "hide", to disable this debug mode |
| 200 | * "highlight", highlight drawing commands tested against a non-rectangular clip |
| 201 | * "stencil", renders the clip region on screen when set |
| 202 | * |
| 203 | * @hide |
| 204 | */ |
| 205 | public static final String DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY = |
| 206 | "debug.hwui.show_non_rect_clip"; |
| 207 | |
| 208 | /** |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 209 | * A process can set this flag to false to prevent the use of hardware |
| 210 | * rendering. |
| 211 | * |
| 212 | * @hide |
| 213 | */ |
| 214 | public static boolean sRendererDisabled = false; |
| 215 | |
Dianne Hackborn | 5d927c2 | 2011-09-02 12:22:18 -0700 | [diff] [blame] | 216 | /** |
| 217 | * Further hardware renderer disabling for the system process. |
| 218 | * |
| 219 | * @hide |
| 220 | */ |
| 221 | public static boolean sSystemRendererDisabled = false; |
| 222 | |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 223 | /** |
| 224 | * Number of frames to profile. |
| 225 | */ |
Romain Guy | a21f877 | 2012-05-07 10:20:52 -0700 | [diff] [blame] | 226 | private static final int PROFILE_MAX_FRAMES = 128; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 227 | |
| 228 | /** |
| 229 | * Number of floats per profiled frame. |
| 230 | */ |
| 231 | private static final int PROFILE_FRAME_DATA_COUNT = 3; |
| 232 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 233 | private boolean mEnabled; |
| 234 | private boolean mRequested = true; |
| 235 | |
| 236 | /** |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 237 | * Invoke this method to disable hardware rendering in the current process. |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 238 | * |
| 239 | * @hide |
| 240 | */ |
Dianne Hackborn | 5d927c2 | 2011-09-02 12:22:18 -0700 | [diff] [blame] | 241 | public static void disable(boolean system) { |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 242 | sRendererDisabled = true; |
Dianne Hackborn | 5d927c2 | 2011-09-02 12:22:18 -0700 | [diff] [blame] | 243 | if (system) { |
| 244 | sSystemRendererDisabled = true; |
| 245 | } |
Romain Guy | 5233920 | 2010-09-03 16:04:46 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 249 | * Indicates whether hardware acceleration is available under any form for |
| 250 | * the view hierarchy. |
| 251 | * |
| 252 | * @return True if the view hierarchy can potentially be hardware accelerated, |
| 253 | * false otherwise |
| 254 | */ |
| 255 | public static boolean isAvailable() { |
| 256 | return GLES20Canvas.isAvailable(); |
| 257 | } |
| 258 | |
| 259 | /** |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 260 | * Destroys the hardware rendering context. |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 261 | * |
| 262 | * @param full If true, destroys all associated resources. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 263 | */ |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 264 | abstract void destroy(boolean full); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * Initializes the hardware renderer for the specified surface. |
| 268 | * |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 269 | * @param surface The surface to hardware accelerate |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 270 | * |
| 271 | * @return True if the initialization was successful, false otherwise. |
| 272 | */ |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 273 | abstract boolean initialize(Surface surface) throws Surface.OutOfResourcesException; |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 274 | |
| 275 | /** |
| 276 | * Updates the hardware renderer for the specified surface. |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 277 | * |
| 278 | * @param surface The surface to hardware accelerate |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 279 | */ |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 280 | abstract void updateSurface(Surface surface) throws Surface.OutOfResourcesException; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 281 | |
| 282 | /** |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 283 | * Destroys the layers used by the specified view hierarchy. |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 284 | * |
| 285 | * @param view The root of the view hierarchy |
| 286 | */ |
| 287 | abstract void destroyLayers(View view); |
| 288 | |
| 289 | /** |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 290 | * Destroys all hardware rendering resources associated with the specified |
| 291 | * view hierarchy. |
| 292 | * |
| 293 | * @param view The root of the view hierarchy |
| 294 | */ |
| 295 | abstract void destroyHardwareResources(View view); |
| 296 | |
| 297 | /** |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 298 | * This method should be invoked whenever the current hardware renderer |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 299 | * context should be reset. |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 300 | * |
| 301 | * @param surface The surface to hardware accelerate |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 302 | */ |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 303 | abstract void invalidate(Surface surface); |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 304 | |
| 305 | /** |
| 306 | * This method should be invoked to ensure the hardware renderer is in |
| 307 | * valid state (for instance, to ensure the correct EGL context is bound |
| 308 | * to the current thread.) |
| 309 | * |
| 310 | * @return true if the renderer is now valid, false otherwise |
| 311 | */ |
| 312 | abstract boolean validate(); |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 313 | |
| 314 | /** |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 315 | * This method ensures the hardware renderer is in a valid state |
| 316 | * before executing the specified action. |
| 317 | * |
| 318 | * This method will attempt to set a valid state even if the window |
| 319 | * the renderer is attached to was destroyed. |
| 320 | * |
| 321 | * @return true if the action was run |
| 322 | */ |
| 323 | abstract boolean safelyRun(Runnable action); |
| 324 | |
| 325 | /** |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 326 | * Setup the hardware renderer for drawing. This is called whenever the |
| 327 | * 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] | 328 | * |
| 329 | * @param width Width of the drawing surface. |
| 330 | * @param height Height of the drawing surface. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 331 | */ |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 332 | abstract void setup(int width, int height); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 333 | |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 334 | /** |
Chet Haase | 40e0383 | 2011-10-06 08:34:13 -0700 | [diff] [blame] | 335 | * Gets the current width of the surface. This is the width that the surface |
| 336 | * was last set to in a call to {@link #setup(int, int)}. |
| 337 | * |
| 338 | * @return the current width of the surface |
| 339 | */ |
| 340 | abstract int getWidth(); |
| 341 | |
| 342 | /** |
| 343 | * Gets the current height of the surface. This is the height that the surface |
| 344 | * was last set to in a call to {@link #setup(int, int)}. |
| 345 | * |
| 346 | * @return the current width of the surface |
| 347 | */ |
| 348 | abstract int getHeight(); |
| 349 | |
| 350 | /** |
Chet Haase | 08837c2 | 2011-11-28 11:53:21 -0800 | [diff] [blame] | 351 | * Gets the current canvas associated with this HardwareRenderer. |
| 352 | * |
| 353 | * @return the current HardwareCanvas |
| 354 | */ |
| 355 | abstract HardwareCanvas getCanvas(); |
| 356 | |
| 357 | /** |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 358 | * Outputs extra debugging information in the specified file descriptor. |
| 359 | * @param pw |
| 360 | */ |
| 361 | abstract void dumpGfxInfo(PrintWriter pw); |
Michael Jurka | a3fabff | 2012-03-28 17:22:29 +0200 | [diff] [blame] | 362 | |
| 363 | /** |
| 364 | * Outputs the total number of frames rendered (used for fps calculations) |
| 365 | * |
| 366 | * @return the number of frames rendered |
| 367 | */ |
| 368 | abstract long getFrameCount(); |
| 369 | |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 370 | /** |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 371 | * Loads system properties used by the renderer. This method is invoked |
| 372 | * whenever system properties are modified. Implementations can use this |
| 373 | * to trigger live updates of the renderer based on properties. |
| 374 | * |
| 375 | * @param surface The surface to update with the new properties. |
| 376 | * Can be null. |
| 377 | * |
| 378 | * @return True if a property has changed. |
| 379 | */ |
| 380 | abstract boolean loadSystemProperties(Surface surface); |
| 381 | |
| 382 | private static native boolean nLoadProperties(); |
| 383 | |
| 384 | /** |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 385 | * Sets the directory to use as a persistent storage for hardware rendering |
| 386 | * resources. |
| 387 | * |
| 388 | * @param cacheDir A directory the current process can write to |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 389 | * |
| 390 | * @hide |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 391 | */ |
| 392 | public static void setupDiskCache(File cacheDir) { |
| 393 | nSetupShadersDiskCache(new File(cacheDir, CACHE_PATH_SHADERS).getAbsolutePath()); |
| 394 | } |
| 395 | |
| 396 | private static native void nSetupShadersDiskCache(String cacheFile); |
| 397 | |
| 398 | /** |
Jamie Gennis | b335fad | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 399 | * Notifies EGL that the frame is about to be rendered. |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 400 | * @param size |
Jamie Gennis | b335fad | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 401 | */ |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 402 | static void beginFrame(int[] size) { |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 403 | nBeginFrame(size); |
Jamie Gennis | b335fad | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 406 | private static native void nBeginFrame(int[] size); |
Jamie Gennis | b335fad | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 407 | |
| 408 | /** |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 409 | * Returns the current system time according to the renderer. |
| 410 | * This method is used for debugging only and should not be used |
| 411 | * as a clock. |
| 412 | */ |
| 413 | static long getSystemTime() { |
| 414 | return nGetSystemTime(); |
| 415 | } |
| 416 | |
| 417 | private static native long nGetSystemTime(); |
| 418 | |
| 419 | /** |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 420 | * Preserves the back buffer of the current surface after a buffer swap. |
| 421 | * Calling this method sets the EGL_SWAP_BEHAVIOR attribute of the current |
| 422 | * surface to EGL_BUFFER_PRESERVED. Calling this method requires an EGL |
| 423 | * config that supports EGL_SWAP_BEHAVIOR_PRESERVED_BIT. |
| 424 | * |
| 425 | * @return True if the swap behavior was successfully changed, |
| 426 | * false otherwise. |
| 427 | */ |
| 428 | static boolean preserveBackBuffer() { |
| 429 | return nPreserveBackBuffer(); |
| 430 | } |
| 431 | |
| 432 | private static native boolean nPreserveBackBuffer(); |
| 433 | |
| 434 | /** |
| 435 | * Indicates whether the current surface preserves its back buffer |
| 436 | * after a buffer swap. |
| 437 | * |
| 438 | * @return True, if the surface's EGL_SWAP_BEHAVIOR is EGL_BUFFER_PRESERVED, |
| 439 | * false otherwise |
| 440 | */ |
| 441 | static boolean isBackBufferPreserved() { |
| 442 | return nIsBackBufferPreserved(); |
| 443 | } |
| 444 | |
| 445 | private static native boolean nIsBackBufferPreserved(); |
| 446 | |
| 447 | /** |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 448 | * Indicates that the specified hardware layer needs to be updated |
| 449 | * as soon as possible. |
| 450 | * |
| 451 | * @param layer The hardware layer that needs an update |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 452 | * |
| 453 | * @see #flushLayerUpdates() |
Romain Guy | e93482f | 2013-06-17 13:14:51 -0700 | [diff] [blame] | 454 | * @see #cancelLayerUpdate(HardwareLayer) |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 455 | */ |
| 456 | abstract void pushLayerUpdate(HardwareLayer layer); |
| 457 | |
| 458 | /** |
Romain Guy | e93482f | 2013-06-17 13:14:51 -0700 | [diff] [blame] | 459 | * Cancels a queued layer update. If the specified layer was not |
| 460 | * queued for update, this method has no effect. |
| 461 | * |
| 462 | * @param layer The layer whose update to cancel |
| 463 | * |
| 464 | * @see #pushLayerUpdate(HardwareLayer) |
| 465 | */ |
| 466 | abstract void cancelLayerUpdate(HardwareLayer layer); |
| 467 | |
| 468 | /** |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 469 | * Forces all enqueued layer updates to be executed immediately. |
| 470 | * |
| 471 | * @see #pushLayerUpdate(HardwareLayer) |
| 472 | */ |
| 473 | abstract void flushLayerUpdates(); |
| 474 | |
| 475 | /** |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 476 | * Interface used to receive callbacks whenever a view is drawn by |
| 477 | * a hardware renderer instance. |
| 478 | */ |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 479 | interface HardwareDrawCallbacks { |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 480 | /** |
| 481 | * Invoked before a view is drawn by a hardware renderer. |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 482 | * This method can be used to apply transformations to the |
| 483 | * canvas but no drawing command should be issued. |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 484 | * |
| 485 | * @param canvas The Canvas used to render the view. |
| 486 | */ |
Romain Guy | 7d70fbf | 2011-05-24 17:40:25 -0700 | [diff] [blame] | 487 | void onHardwarePreDraw(HardwareCanvas canvas); |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 488 | |
| 489 | /** |
| 490 | * Invoked after a view is drawn by a hardware renderer. |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 491 | * It is safe to invoke drawing commands from this method. |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 492 | * |
| 493 | * @param canvas The Canvas used to render the view. |
| 494 | */ |
Romain Guy | 7d70fbf | 2011-05-24 17:40:25 -0700 | [diff] [blame] | 495 | void onHardwarePostDraw(HardwareCanvas canvas); |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 498 | /** |
| 499 | * Draws the specified view. |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 500 | * |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 501 | * @param view The view to draw. |
| 502 | * @param attachInfo AttachInfo tied to the specified view. |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 503 | * @param callbacks Callbacks invoked when drawing happens. |
| 504 | * @param dirty The dirty rectangle to update, can be null. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 505 | */ |
Romain Guy | e55945e | 2013-04-04 15:26:04 -0700 | [diff] [blame] | 506 | abstract void draw(View view, View.AttachInfo attachInfo, HardwareDrawCallbacks callbacks, |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 507 | Rect dirty); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 508 | |
| 509 | /** |
Romain Guy | 53ca03d | 2010-10-08 18:55:27 -0700 | [diff] [blame] | 510 | * Creates a new display list that can be used to record batches of |
| 511 | * drawing operations. |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 512 | * |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 513 | * @param name The name of the display list, used for debugging purpose. May be null. |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 514 | * |
Romain Guy | 53ca03d | 2010-10-08 18:55:27 -0700 | [diff] [blame] | 515 | * @return A new display list. |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 516 | * |
| 517 | * @hide |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 518 | */ |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 519 | public abstract DisplayList createDisplayList(String name); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 520 | |
| 521 | /** |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 522 | * Creates a new hardware layer. A hardware layer built by calling this |
| 523 | * method will be treated as a texture layer, instead of as a render target. |
| 524 | * |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 525 | * @param isOpaque Whether the layer should be opaque or not |
| 526 | * |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 527 | * @return A hardware layer |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 528 | */ |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 529 | abstract HardwareLayer createHardwareLayer(boolean isOpaque); |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 530 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 531 | /** |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 532 | * Creates a new hardware layer. |
| 533 | * |
| 534 | * @param width The minimum width of the layer |
| 535 | * @param height The minimum height of the layer |
| 536 | * @param isOpaque Whether the layer should be opaque or not |
| 537 | * |
| 538 | * @return A hardware layer |
| 539 | */ |
| 540 | abstract HardwareLayer createHardwareLayer(int width, int height, boolean isOpaque); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 541 | |
| 542 | /** |
| 543 | * Creates a new {@link SurfaceTexture} that can be used to render into the |
| 544 | * specified hardware layer. |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 545 | * |
| 546 | * @param layer The layer to render into using a {@link android.graphics.SurfaceTexture} |
| 547 | * |
| 548 | * @return A {@link SurfaceTexture} |
| 549 | */ |
Romain Guy | e5e0c50 | 2011-06-15 15:18:31 -0700 | [diff] [blame] | 550 | abstract SurfaceTexture createSurfaceTexture(HardwareLayer layer); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 551 | |
| 552 | /** |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 553 | * Sets the {@link android.graphics.SurfaceTexture} that will be used to |
| 554 | * render into the specified hardware layer. |
| 555 | * |
| 556 | * @param layer The layer to render into using a {@link android.graphics.SurfaceTexture} |
| 557 | * @param surfaceTexture The {@link android.graphics.SurfaceTexture} to use for the layer |
| 558 | */ |
| 559 | abstract void setSurfaceTexture(HardwareLayer layer, SurfaceTexture surfaceTexture); |
| 560 | |
| 561 | /** |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 562 | * Detaches the specified functor from the current functor execution queue. |
| 563 | * |
| 564 | * @param functor The native functor to remove from the execution queue. |
| 565 | * |
| 566 | * @see HardwareCanvas#callDrawGLFunction(int) |
| 567 | * @see #attachFunctor(android.view.View.AttachInfo, int) |
| 568 | */ |
| 569 | abstract void detachFunctor(int functor); |
| 570 | |
| 571 | /** |
| 572 | * Schedules the specified functor in the functors execution queue. |
| 573 | * |
| 574 | * @param attachInfo AttachInfo tied to this renderer. |
| 575 | * @param functor The native functor to insert in the execution queue. |
| 576 | * |
| 577 | * @see HardwareCanvas#callDrawGLFunction(int) |
Chris Craik | 41ee465 | 2012-05-31 15:05:57 -0700 | [diff] [blame] | 578 | * @see #detachFunctor(int) |
| 579 | * |
| 580 | * @return true if the functor was attached successfully |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 581 | */ |
Chris Craik | 41ee465 | 2012-05-31 15:05:57 -0700 | [diff] [blame] | 582 | abstract boolean attachFunctor(View.AttachInfo attachInfo, int functor); |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 583 | |
| 584 | /** |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 585 | * Initializes the hardware renderer for the specified surface and setup the |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 586 | * renderer for drawing, if needed. This is invoked when the ViewAncestor has |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 587 | * potentially lost the hardware renderer. The hardware renderer should be |
| 588 | * reinitialized and setup when the render {@link #isRequested()} and |
| 589 | * {@link #isEnabled()}. |
Romain Guy | 44d7974 | 2012-01-13 12:12:09 -0800 | [diff] [blame] | 590 | * |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 591 | * @param width The width of the drawing surface. |
| 592 | * @param height The height of the drawing surface. |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 593 | * @param surface The surface to hardware accelerate |
Romain Guy | dfab363 | 2012-10-03 14:53:25 -0700 | [diff] [blame] | 594 | * |
| 595 | * @return true if the surface was initialized, false otherwise. Returning |
| 596 | * false might mean that the surface was already initialized. |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 597 | */ |
Romain Guy | dfab363 | 2012-10-03 14:53:25 -0700 | [diff] [blame] | 598 | boolean initializeIfNeeded(int width, int height, Surface surface) |
Romain Guy | 44d7974 | 2012-01-13 12:12:09 -0800 | [diff] [blame] | 599 | throws Surface.OutOfResourcesException { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 600 | if (isRequested()) { |
| 601 | // We lost the gl context, so recreate it. |
| 602 | if (!isEnabled()) { |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 603 | if (initialize(surface)) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 604 | setup(width, height); |
Romain Guy | dfab363 | 2012-10-03 14:53:25 -0700 | [diff] [blame] | 605 | return true; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
Romain Guy | dfab363 | 2012-10-03 14:53:25 -0700 | [diff] [blame] | 608 | } |
| 609 | return false; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /** |
Romain Guy | ef35927 | 2013-01-31 19:07:29 -0800 | [diff] [blame] | 613 | * Optional, sets the name of the renderer. Useful for debugging purposes. |
| 614 | * |
| 615 | * @param name The name of this renderer, can be null |
| 616 | */ |
| 617 | abstract void setName(String name); |
| 618 | |
| 619 | /** |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 620 | * Creates a hardware renderer using OpenGL. |
| 621 | * |
| 622 | * @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] | 623 | * @param translucent True if the surface is translucent, false otherwise |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 624 | * |
| 625 | * @return A hardware renderer backed by OpenGL. |
| 626 | */ |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 627 | static HardwareRenderer createGlRenderer(int glVersion, boolean translucent) { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 628 | switch (glVersion) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 629 | case 2: |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 630 | return Gl20Renderer.create(translucent); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 631 | } |
| 632 | throw new IllegalArgumentException("Unknown GL version: " + glVersion); |
| 633 | } |
| 634 | |
| 635 | /** |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 636 | * Invoke this method when the system is running out of memory. This |
| 637 | * method will attempt to recover as much memory as possible, based on |
| 638 | * the specified hint. |
| 639 | * |
| 640 | * @param level Hint about the amount of memory that should be trimmed, |
| 641 | * see {@link android.content.ComponentCallbacks} |
| 642 | */ |
| 643 | static void trimMemory(int level) { |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 644 | startTrimMemory(level); |
| 645 | endTrimMemory(); |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Starts the process of trimming memory. Usually this call will setup |
| 650 | * hardware rendering context and reclaim memory.Extra cleanup might |
| 651 | * be required by calling {@link #endTrimMemory()}. |
| 652 | * |
| 653 | * @param level Hint about the amount of memory that should be trimmed, |
| 654 | * see {@link android.content.ComponentCallbacks} |
| 655 | */ |
| 656 | static void startTrimMemory(int level) { |
| 657 | Gl20Renderer.startTrimMemory(level); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Finishes the process of trimming memory. This method will usually |
| 662 | * cleanup special resources used by the memory trimming process. |
| 663 | */ |
| 664 | static void endTrimMemory() { |
| 665 | Gl20Renderer.endTrimMemory(); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /** |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 669 | * Indicates whether hardware acceleration is currently enabled. |
| 670 | * |
| 671 | * @return True if hardware acceleration is in use, false otherwise. |
| 672 | */ |
| 673 | boolean isEnabled() { |
| 674 | return mEnabled; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Indicates whether hardware acceleration is currently enabled. |
| 679 | * |
| 680 | * @param enabled True if the hardware renderer is in use, false otherwise. |
| 681 | */ |
| 682 | void setEnabled(boolean enabled) { |
| 683 | mEnabled = enabled; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Indicates whether hardware acceleration is currently request but not |
| 688 | * necessarily enabled yet. |
| 689 | * |
| 690 | * @return True if requested, false otherwise. |
| 691 | */ |
| 692 | boolean isRequested() { |
| 693 | return mRequested; |
| 694 | } |
| 695 | |
| 696 | /** |
Romain Guy | 9745fae | 2010-12-08 11:39:15 -0800 | [diff] [blame] | 697 | * Indicates whether hardware acceleration is currently requested but not |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 698 | * necessarily enabled yet. |
| 699 | * |
| 700 | * @return True to request hardware acceleration, false otherwise. |
| 701 | */ |
| 702 | void setRequested(boolean requested) { |
| 703 | mRequested = requested; |
| 704 | } |
| 705 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 706 | /** |
| 707 | * Describes a series of frames that should be drawn on screen as a graph. |
| 708 | * Each frame is composed of 1 or more elements. |
| 709 | */ |
| 710 | abstract class GraphDataProvider { |
| 711 | /** |
| 712 | * Draws the graph as bars. Frame elements are stacked on top of |
| 713 | * each other. |
| 714 | */ |
| 715 | public static final int GRAPH_TYPE_BARS = 0; |
| 716 | /** |
| 717 | * Draws the graph as lines. The number of series drawn corresponds |
| 718 | * to the number of elements. |
| 719 | */ |
| 720 | public static final int GRAPH_TYPE_LINES = 1; |
| 721 | |
| 722 | /** |
| 723 | * Returns the type of graph to render. |
| 724 | * |
| 725 | * @return {@link #GRAPH_TYPE_BARS} or {@link #GRAPH_TYPE_LINES} |
| 726 | */ |
| 727 | abstract int getGraphType(); |
| 728 | |
| 729 | /** |
| 730 | * This method is invoked before the graph is drawn. This method |
| 731 | * can be used to compute sizes, etc. |
| 732 | * |
| 733 | * @param metrics The display metrics |
| 734 | */ |
| 735 | abstract void prepare(DisplayMetrics metrics); |
| 736 | |
| 737 | /** |
| 738 | * @return The size in pixels of a vertical unit. |
| 739 | */ |
| 740 | abstract int getVerticalUnitSize(); |
| 741 | |
| 742 | /** |
| 743 | * @return The size in pixels of a horizontal unit. |
| 744 | */ |
| 745 | abstract int getHorizontalUnitSize(); |
| 746 | |
| 747 | /** |
| 748 | * @return The size in pixels of the margin between horizontal units. |
| 749 | */ |
| 750 | abstract int getHorizontaUnitMargin(); |
| 751 | |
| 752 | /** |
| 753 | * An optional threshold value. |
| 754 | * |
| 755 | * @return A value >= 0 to draw the threshold, a negative value |
| 756 | * to ignore it. |
| 757 | */ |
| 758 | abstract float getThreshold(); |
| 759 | |
| 760 | /** |
| 761 | * The data to draw in the graph. The number of elements in the |
| 762 | * array must be at least {@link #getFrameCount()} * {@link #getElementCount()}. |
| 763 | * If a value is negative the following values will be ignored. |
| 764 | */ |
| 765 | abstract float[] getData(); |
| 766 | |
| 767 | /** |
| 768 | * Returns the number of frames to render in the graph. |
| 769 | */ |
| 770 | abstract int getFrameCount(); |
| 771 | |
| 772 | /** |
| 773 | * Returns the number of elements in each frame. This directly affects |
| 774 | * the number of series drawn in the graph. |
| 775 | */ |
| 776 | abstract int getElementCount(); |
| 777 | |
| 778 | /** |
| 779 | * Returns the current frame, if any. If the returned value is negative |
| 780 | * the current frame is ignored. |
| 781 | */ |
| 782 | abstract int getCurrentFrame(); |
| 783 | |
| 784 | /** |
| 785 | * Prepares the paint to draw the specified element (or series.) |
| 786 | */ |
| 787 | abstract void setupGraphPaint(Paint paint, int elementIndex); |
| 788 | |
| 789 | /** |
| 790 | * Prepares the paint to draw the threshold. |
| 791 | */ |
| 792 | abstract void setupThresholdPaint(Paint paint); |
| 793 | |
| 794 | /** |
| 795 | * Prepares the paint to draw the current frame indicator. |
| 796 | */ |
| 797 | abstract void setupCurrentFramePaint(Paint paint); |
| 798 | } |
| 799 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 800 | @SuppressWarnings({"deprecation"}) |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 801 | static abstract class GlRenderer extends HardwareRenderer { |
Romain Guy | 16260e7 | 2011-09-01 14:26:11 -0700 | [diff] [blame] | 802 | static final int SURFACE_STATE_ERROR = 0; |
| 803 | static final int SURFACE_STATE_SUCCESS = 1; |
| 804 | static final int SURFACE_STATE_UPDATED = 2; |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 805 | |
Chris Craik | 65924a3 | 2012-04-05 17:52:11 -0700 | [diff] [blame] | 806 | static final int FUNCTOR_PROCESS_DELAY = 4; |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 807 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 808 | private static final int PROFILE_DRAW_MARGIN = 0; |
| 809 | private static final int PROFILE_DRAW_WIDTH = 3; |
| 810 | private static final int[] PROFILE_DRAW_COLORS = { 0xcf3e66cc, 0xcfdc3912, 0xcfe69800 }; |
| 811 | private static final int PROFILE_DRAW_CURRENT_FRAME_COLOR = 0xcf5faa4d; |
| 812 | private static final int PROFILE_DRAW_THRESHOLD_COLOR = 0xff5faa4d; |
| 813 | private static final int PROFILE_DRAW_THRESHOLD_STROKE_WIDTH = 2; |
| 814 | private static final int PROFILE_DRAW_DP_PER_MS = 7; |
| 815 | |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 816 | private static final String[] VISUALIZERS = { |
| 817 | PROFILE_PROPERTY_VISUALIZE_BARS, |
| 818 | PROFILE_PROPERTY_VISUALIZE_LINES |
| 819 | }; |
| 820 | |
| 821 | private static final String[] OVERDRAW = { |
| 822 | OVERDRAW_PROPERTY_SHOW, |
| 823 | OVERDRAW_PROPERTY_COUNT |
| 824 | }; |
| 825 | private static final int OVERDRAW_TYPE_COUNT = 1; |
| 826 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 827 | static EGL10 sEgl; |
| 828 | static EGLDisplay sEglDisplay; |
| 829 | static EGLConfig sEglConfig; |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 830 | static final Object[] sEglLock = new Object[0]; |
Chet Haase | 40e0383 | 2011-10-06 08:34:13 -0700 | [diff] [blame] | 831 | int mWidth = -1, mHeight = -1; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 832 | |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 833 | static final ThreadLocal<ManagedEGLContext> sEglContextStorage |
| 834 | = new ThreadLocal<ManagedEGLContext>(); |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 835 | |
| 836 | EGLContext mEglContext; |
| 837 | Thread mEglThread; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 838 | |
| 839 | EGLSurface mEglSurface; |
| 840 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 841 | GL mGl; |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 842 | HardwareCanvas mCanvas; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 843 | |
Romain Guy | ef35927 | 2013-01-31 19:07:29 -0800 | [diff] [blame] | 844 | String mName; |
| 845 | |
Michael Jurka | a3fabff | 2012-03-28 17:22:29 +0200 | [diff] [blame] | 846 | long mFrameCount; |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 847 | Paint mDebugPaint; |
| 848 | |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 849 | static boolean sDirtyRegions; |
| 850 | static final boolean sDirtyRegionsRequested; |
| 851 | static { |
| 852 | String dirtyProperty = SystemProperties.get(RENDER_DIRTY_REGIONS_PROPERTY, "true"); |
| 853 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 854 | sDirtyRegions = RENDER_DIRTY_REGIONS && "true".equalsIgnoreCase(dirtyProperty); |
| 855 | sDirtyRegionsRequested = sDirtyRegions; |
| 856 | } |
| 857 | |
| 858 | boolean mDirtyRegionsEnabled; |
Romain Guy | 9477c6e | 2011-12-09 12:27:21 -0800 | [diff] [blame] | 859 | boolean mUpdateDirtyRegions; |
| 860 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 861 | boolean mProfileEnabled; |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 862 | int mProfileVisualizerType = -1; |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 863 | float[] mProfileData; |
| 864 | ReentrantLock mProfileLock; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 865 | int mProfileCurrentFrame = -PROFILE_FRAME_DATA_COUNT; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 866 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 867 | GraphDataProvider mDebugDataProvider; |
| 868 | float[][] mProfileShapes; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 869 | Paint mProfilePaint; |
| 870 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 871 | boolean mDebugDirtyRegions; |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 872 | int mDebugOverdraw = -1; |
| 873 | HardwareLayer mDebugOverdrawLayer; |
| 874 | Paint mDebugOverdrawPaint; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 875 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 876 | final int mGlVersion; |
| 877 | final boolean mTranslucent; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 878 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 879 | private boolean mDestroyed; |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 880 | |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 881 | private final Rect mRedrawClip = new Rect(); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 882 | |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 883 | private final int[] mSurfaceSize = new int[2]; |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 884 | private final FunctorsRunnable mFunctorsRunnable = new FunctorsRunnable(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 885 | |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 886 | private long mDrawDelta = Long.MAX_VALUE; |
| 887 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 888 | GlRenderer(int glVersion, boolean translucent) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 889 | mGlVersion = glVersion; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 890 | mTranslucent = translucent; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 891 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 892 | loadSystemProperties(null); |
| 893 | } |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 894 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 895 | @Override |
| 896 | boolean loadSystemProperties(Surface surface) { |
Romain Guy | a4fef02 | 2013-01-07 11:18:38 -0800 | [diff] [blame] | 897 | boolean value; |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 898 | boolean changed = false; |
| 899 | |
Romain Guy | a4fef02 | 2013-01-07 11:18:38 -0800 | [diff] [blame] | 900 | String profiling = SystemProperties.get(PROFILE_PROPERTY); |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 901 | int graphType = search(VISUALIZERS, profiling); |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 902 | value = graphType >= 0; |
Romain Guy | a4fef02 | 2013-01-07 11:18:38 -0800 | [diff] [blame] | 903 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 904 | if (graphType != mProfileVisualizerType) { |
Romain Guy | a4fef02 | 2013-01-07 11:18:38 -0800 | [diff] [blame] | 905 | changed = true; |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 906 | mProfileVisualizerType = graphType; |
Romain Guy | a4fef02 | 2013-01-07 11:18:38 -0800 | [diff] [blame] | 907 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 908 | mProfileShapes = null; |
Romain Guy | a4fef02 | 2013-01-07 11:18:38 -0800 | [diff] [blame] | 909 | mProfilePaint = null; |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 910 | |
| 911 | if (value) { |
| 912 | mDebugDataProvider = new DrawPerformanceDataProvider(graphType); |
| 913 | } else { |
| 914 | mDebugDataProvider = null; |
| 915 | } |
Romain Guy | a4fef02 | 2013-01-07 11:18:38 -0800 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | // If on-screen profiling is not enabled, we need to check whether |
| 919 | // console profiling only is enabled |
| 920 | if (!value) { |
| 921 | value = Boolean.parseBoolean(profiling); |
| 922 | } |
| 923 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 924 | if (value != mProfileEnabled) { |
| 925 | changed = true; |
| 926 | mProfileEnabled = value; |
| 927 | |
| 928 | if (mProfileEnabled) { |
| 929 | Log.d(LOG_TAG, "Profiling hardware renderer"); |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 930 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 931 | int maxProfileFrames = SystemProperties.getInt(PROFILE_MAXFRAMES_PROPERTY, |
| 932 | PROFILE_MAX_FRAMES); |
| 933 | mProfileData = new float[maxProfileFrames * PROFILE_FRAME_DATA_COUNT]; |
| 934 | for (int i = 0; i < mProfileData.length; i += PROFILE_FRAME_DATA_COUNT) { |
| 935 | mProfileData[i] = mProfileData[i + 1] = mProfileData[i + 2] = -1; |
| 936 | } |
| 937 | |
| 938 | mProfileLock = new ReentrantLock(); |
| 939 | } else { |
| 940 | mProfileData = null; |
| 941 | mProfileLock = null; |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 942 | mProfileVisualizerType = -1; |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 943 | } |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 944 | |
Romain Guy | 666d5da | 2013-01-07 11:29:14 -0800 | [diff] [blame] | 945 | mProfileCurrentFrame = -PROFILE_FRAME_DATA_COUNT; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 948 | value = SystemProperties.getBoolean(DEBUG_DIRTY_REGIONS_PROPERTY, false); |
| 949 | if (value != mDebugDirtyRegions) { |
| 950 | changed = true; |
| 951 | mDebugDirtyRegions = value; |
| 952 | |
| 953 | if (mDebugDirtyRegions) { |
| 954 | Log.d(LOG_TAG, "Debugging dirty regions"); |
| 955 | } |
Romain Guy | b04f7e9 | 2012-02-15 12:36:54 -0800 | [diff] [blame] | 956 | } |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 957 | |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 958 | String overdraw = SystemProperties.get(HardwareRenderer.DEBUG_OVERDRAW_PROPERTY); |
| 959 | int debugOverdraw = search(OVERDRAW, overdraw); |
| 960 | if (debugOverdraw != mDebugOverdraw) { |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 961 | changed = true; |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 962 | mDebugOverdraw = debugOverdraw; |
| 963 | |
| 964 | if (mDebugOverdraw != OVERDRAW_TYPE_COUNT) { |
| 965 | if (mDebugOverdrawLayer != null) { |
| 966 | mDebugOverdrawLayer.destroy(); |
| 967 | mDebugOverdrawLayer = null; |
| 968 | mDebugOverdrawPaint = null; |
| 969 | } |
| 970 | } |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | if (nLoadProperties()) { |
| 974 | changed = true; |
| 975 | } |
| 976 | |
| 977 | return changed; |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 978 | } |
| 979 | |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 980 | private static int search(String[] values, String value) { |
| 981 | for (int i = 0; i < values.length; i++) { |
| 982 | if (values[i].equals(value)) return i; |
| 983 | } |
| 984 | return -1; |
| 985 | } |
| 986 | |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 987 | @Override |
| 988 | void dumpGfxInfo(PrintWriter pw) { |
| 989 | if (mProfileEnabled) { |
| 990 | pw.printf("\n\tDraw\tProcess\tExecute\n"); |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 991 | |
| 992 | mProfileLock.lock(); |
| 993 | try { |
| 994 | for (int i = 0; i < mProfileData.length; i += PROFILE_FRAME_DATA_COUNT) { |
| 995 | if (mProfileData[i] < 0) { |
| 996 | break; |
| 997 | } |
| 998 | pw.printf("\t%3.2f\t%3.2f\t%3.2f\n", mProfileData[i], mProfileData[i + 1], |
| 999 | mProfileData[i + 2]); |
| 1000 | mProfileData[i] = mProfileData[i + 1] = mProfileData[i + 2] = -1; |
Chet Haase | 0928060 | 2012-04-03 16:15:34 -0700 | [diff] [blame] | 1001 | } |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 1002 | mProfileCurrentFrame = mProfileData.length; |
| 1003 | } finally { |
| 1004 | mProfileLock.unlock(); |
Romain Guy | a676ad7 | 2012-02-13 17:47:10 -0800 | [diff] [blame] | 1005 | } |
| 1006 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Michael Jurka | a3fabff | 2012-03-28 17:22:29 +0200 | [diff] [blame] | 1009 | @Override |
| 1010 | long getFrameCount() { |
| 1011 | return mFrameCount; |
| 1012 | } |
| 1013 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1014 | /** |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 1015 | * Indicates whether this renderer instance can track and update dirty regions. |
| 1016 | */ |
| 1017 | boolean hasDirtyRegions() { |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 1018 | return mDirtyRegionsEnabled; |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /** |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 1022 | * Checks for OpenGL errors. If an error has occured, {@link #destroy(boolean)} |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1023 | * is invoked and the requested flag is turned off. The error code is |
| 1024 | * also logged as a warning. |
| 1025 | */ |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 1026 | void checkEglErrors() { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1027 | if (isEnabled()) { |
Romain Guy | 740ee65 | 2012-09-17 18:11:40 -0700 | [diff] [blame] | 1028 | checkEglErrorsForced(); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | private void checkEglErrorsForced() { |
| 1033 | int error = sEgl.eglGetError(); |
| 1034 | if (error != EGL_SUCCESS) { |
| 1035 | // something bad has happened revert to |
| 1036 | // normal rendering. |
| 1037 | Log.w(LOG_TAG, "EGL error: " + GLUtils.getEGLErrorString(error)); |
| 1038 | fallback(error != EGL11.EGL_CONTEXT_LOST); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1039 | } |
| 1040 | } |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 1041 | |
Romain Guy | 9745fae | 2010-12-08 11:39:15 -0800 | [diff] [blame] | 1042 | private void fallback(boolean fallback) { |
| 1043 | destroy(true); |
| 1044 | if (fallback) { |
| 1045 | // we'll try again if it was context lost |
| 1046 | setRequested(false); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1047 | Log.w(LOG_TAG, "Mountain View, we've had a problem here. " |
Romain Guy | 9745fae | 2010-12-08 11:39:15 -0800 | [diff] [blame] | 1048 | + "Switching back to software rendering."); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1052 | @Override |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1053 | boolean initialize(Surface surface) throws Surface.OutOfResourcesException { |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1054 | if (isRequested() && !isEnabled()) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1055 | boolean contextCreated = initializeEgl(); |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1056 | mGl = createEglSurface(surface); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1057 | mDestroyed = false; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1058 | |
| 1059 | if (mGl != null) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1060 | int err = sEgl.eglGetError(); |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1061 | if (err != EGL_SUCCESS) { |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 1062 | destroy(true); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1063 | setRequested(false); |
| 1064 | } else { |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 1065 | if (mCanvas == null) { |
| 1066 | mCanvas = createCanvas(); |
Romain Guy | ef35927 | 2013-01-31 19:07:29 -0800 | [diff] [blame] | 1067 | mCanvas.setName(mName); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1068 | } |
Romain Guy | e55945e | 2013-04-04 15:26:04 -0700 | [diff] [blame] | 1069 | setEnabled(true); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1070 | |
| 1071 | if (contextCreated) { |
| 1072 | initAtlas(); |
| 1073 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | return mCanvas != null; |
| 1077 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1078 | } |
| 1079 | return false; |
| 1080 | } |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 1081 | |
| 1082 | @Override |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1083 | void updateSurface(Surface surface) throws Surface.OutOfResourcesException { |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 1084 | if (isRequested() && isEnabled()) { |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1085 | createEglSurface(surface); |
Romain Guy | 2a83f00 | 2011-01-18 18:28:21 -0800 | [diff] [blame] | 1086 | } |
| 1087 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1088 | |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 1089 | abstract HardwareCanvas createCanvas(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1090 | |
Romain Guy | 29d23ec | 2011-07-25 14:42:24 -0700 | [diff] [blame] | 1091 | abstract int[] getConfig(boolean dirtyRegions); |
| 1092 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1093 | boolean initializeEgl() { |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1094 | synchronized (sEglLock) { |
| 1095 | if (sEgl == null && sEglConfig == null) { |
| 1096 | sEgl = (EGL10) EGLContext.getEGL(); |
| 1097 | |
| 1098 | // Get to the default display. |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1099 | sEglDisplay = sEgl.eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1100 | |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1101 | if (sEglDisplay == EGL_NO_DISPLAY) { |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1102 | throw new RuntimeException("eglGetDisplay failed " |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 1103 | + GLUtils.getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | // We can now initialize EGL for that display |
| 1107 | int[] version = new int[2]; |
| 1108 | if (!sEgl.eglInitialize(sEglDisplay, version)) { |
| 1109 | throw new RuntimeException("eglInitialize failed " + |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 1110 | GLUtils.getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1111 | } |
Romain Guy | 740ee65 | 2012-09-17 18:11:40 -0700 | [diff] [blame] | 1112 | |
| 1113 | checkEglErrorsForced(); |
| 1114 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 1115 | sEglConfig = loadEglConfig(); |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 1116 | } |
| 1117 | } |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1118 | |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 1119 | ManagedEGLContext managedContext = sEglContextStorage.get(); |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1120 | mEglContext = managedContext != null ? managedContext.getContext() : null; |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1121 | mEglThread = Thread.currentThread(); |
| 1122 | |
| 1123 | if (mEglContext == null) { |
| 1124 | mEglContext = createContext(sEgl, sEglDisplay, sEglConfig); |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 1125 | sEglContextStorage.set(createManagedContext(mEglContext)); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1126 | return true; |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1127 | } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1128 | |
| 1129 | return false; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 1132 | private EGLConfig loadEglConfig() { |
| 1133 | EGLConfig eglConfig = chooseEglConfig(); |
| 1134 | if (eglConfig == null) { |
| 1135 | // We tried to use EGL_SWAP_BEHAVIOR_PRESERVED_BIT, try again without |
| 1136 | if (sDirtyRegions) { |
| 1137 | sDirtyRegions = false; |
| 1138 | eglConfig = chooseEglConfig(); |
| 1139 | if (eglConfig == null) { |
| 1140 | throw new RuntimeException("eglConfig not initialized"); |
| 1141 | } |
| 1142 | } else { |
| 1143 | throw new RuntimeException("eglConfig not initialized"); |
| 1144 | } |
| 1145 | } |
| 1146 | return eglConfig; |
| 1147 | } |
| 1148 | |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 1149 | abstract ManagedEGLContext createManagedContext(EGLContext eglContext); |
| 1150 | |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1151 | private EGLConfig chooseEglConfig() { |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1152 | EGLConfig[] configs = new EGLConfig[1]; |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1153 | int[] configsCount = new int[1]; |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 1154 | int[] configSpec = getConfig(sDirtyRegions); |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1155 | |
| 1156 | // Debug |
Romain Guy | 29d23ec | 2011-07-25 14:42:24 -0700 | [diff] [blame] | 1157 | final String debug = SystemProperties.get(PRINT_CONFIG_PROPERTY, ""); |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1158 | if ("all".equalsIgnoreCase(debug)) { |
| 1159 | sEgl.eglChooseConfig(sEglDisplay, configSpec, null, 0, configsCount); |
| 1160 | |
| 1161 | EGLConfig[] debugConfigs = new EGLConfig[configsCount[0]]; |
| 1162 | sEgl.eglChooseConfig(sEglDisplay, configSpec, debugConfigs, |
| 1163 | configsCount[0], configsCount); |
| 1164 | |
| 1165 | for (EGLConfig config : debugConfigs) { |
| 1166 | printConfig(config); |
| 1167 | } |
| 1168 | } |
| 1169 | |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1170 | if (!sEgl.eglChooseConfig(sEglDisplay, configSpec, configs, 1, configsCount)) { |
| 1171 | throw new IllegalArgumentException("eglChooseConfig failed " + |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 1172 | GLUtils.getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1173 | } else if (configsCount[0] > 0) { |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1174 | if ("choice".equalsIgnoreCase(debug)) { |
| 1175 | printConfig(configs[0]); |
| 1176 | } |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1177 | return configs[0]; |
| 1178 | } |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1179 | |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1180 | return null; |
| 1181 | } |
| 1182 | |
Romain Guy | e979e62 | 2012-03-20 13:50:27 -0700 | [diff] [blame] | 1183 | private static void printConfig(EGLConfig config) { |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1184 | int[] value = new int[1]; |
| 1185 | |
| 1186 | Log.d(LOG_TAG, "EGL configuration " + config + ":"); |
| 1187 | |
| 1188 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_RED_SIZE, value); |
| 1189 | Log.d(LOG_TAG, " RED_SIZE = " + value[0]); |
| 1190 | |
| 1191 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_GREEN_SIZE, value); |
| 1192 | Log.d(LOG_TAG, " GREEN_SIZE = " + value[0]); |
| 1193 | |
| 1194 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_BLUE_SIZE, value); |
| 1195 | Log.d(LOG_TAG, " BLUE_SIZE = " + value[0]); |
| 1196 | |
| 1197 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_ALPHA_SIZE, value); |
| 1198 | Log.d(LOG_TAG, " ALPHA_SIZE = " + value[0]); |
| 1199 | |
| 1200 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_DEPTH_SIZE, value); |
| 1201 | Log.d(LOG_TAG, " DEPTH_SIZE = " + value[0]); |
| 1202 | |
| 1203 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_STENCIL_SIZE, value); |
| 1204 | Log.d(LOG_TAG, " STENCIL_SIZE = " + value[0]); |
| 1205 | |
Romain Guy | 8efca54 | 2012-10-15 18:09:49 -0700 | [diff] [blame] | 1206 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_SAMPLE_BUFFERS, value); |
| 1207 | Log.d(LOG_TAG, " SAMPLE_BUFFERS = " + value[0]); |
| 1208 | |
| 1209 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_SAMPLES, value); |
| 1210 | Log.d(LOG_TAG, " SAMPLES = " + value[0]); |
| 1211 | |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1212 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_SURFACE_TYPE, value); |
Romain Guy | 29d23ec | 2011-07-25 14:42:24 -0700 | [diff] [blame] | 1213 | Log.d(LOG_TAG, " SURFACE_TYPE = 0x" + Integer.toHexString(value[0])); |
Romain Guy | 8efca54 | 2012-10-15 18:09:49 -0700 | [diff] [blame] | 1214 | |
| 1215 | sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_CONFIG_CAVEAT, value); |
| 1216 | Log.d(LOG_TAG, " CONFIG_CAVEAT = 0x" + Integer.toHexString(value[0])); |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1219 | GL createEglSurface(Surface surface) throws Surface.OutOfResourcesException { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1220 | // Check preconditions. |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1221 | if (sEgl == null) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1222 | throw new RuntimeException("egl not initialized"); |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1223 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1224 | if (sEglDisplay == null) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1225 | throw new RuntimeException("eglDisplay not initialized"); |
| 1226 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1227 | if (sEglConfig == null) { |
Romain Guy | 069ea0e | 2011-02-08 12:24:52 -0800 | [diff] [blame] | 1228 | throw new RuntimeException("eglConfig not initialized"); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1229 | } |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1230 | if (Thread.currentThread() != mEglThread) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1231 | throw new IllegalStateException("HardwareRenderer cannot be used " |
| 1232 | + "from multiple threads"); |
| 1233 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1234 | |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 1235 | // In case we need to destroy an existing surface |
| 1236 | destroySurface(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1237 | |
| 1238 | // Create an EGL surface we can render into. |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1239 | if (!createSurface(surface)) { |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1240 | return null; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 1243 | initCaches(); |
Romain Guy | 6f7d939 | 2011-06-02 14:17:28 -0700 | [diff] [blame] | 1244 | |
Romain Guy | 9477c6e | 2011-12-09 12:27:21 -0800 | [diff] [blame] | 1245 | return mEglContext.getGL(); |
| 1246 | } |
| 1247 | |
| 1248 | private void enableDirtyRegions() { |
Romain Guy | 6f7d939 | 2011-06-02 14:17:28 -0700 | [diff] [blame] | 1249 | // If mDirtyRegions is set, this means we have an EGL configuration |
| 1250 | // with EGL_SWAP_BEHAVIOR_PRESERVED_BIT set |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 1251 | if (sDirtyRegions) { |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 1252 | if (!(mDirtyRegionsEnabled = preserveBackBuffer())) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1253 | Log.w(LOG_TAG, "Backbuffer cannot be preserved"); |
| 1254 | } |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 1255 | } else if (sDirtyRegionsRequested) { |
Romain Guy | 6f7d939 | 2011-06-02 14:17:28 -0700 | [diff] [blame] | 1256 | // If mDirtyRegions is not set, our EGL configuration does not |
| 1257 | // have EGL_SWAP_BEHAVIOR_PRESERVED_BIT; however, the default |
| 1258 | // swap behavior might be EGL_BUFFER_PRESERVED, which means we |
| 1259 | // want to set mDirtyRegions. We try to do this only if dirty |
| 1260 | // regions were initially requested as part of the device |
| 1261 | // configuration (see RENDER_DIRTY_REGIONS) |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 1262 | mDirtyRegionsEnabled = isBackBufferPreserved(); |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1263 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 1266 | abstract void initCaches(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1267 | abstract void initAtlas(); |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 1268 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1269 | EGLContext createContext(EGL10 egl, EGLDisplay eglDisplay, EGLConfig eglConfig) { |
Romain Guy | bd43152 | 2012-09-26 13:31:25 -0700 | [diff] [blame] | 1270 | int[] attribs = { EGL14.EGL_CONTEXT_CLIENT_VERSION, mGlVersion, EGL_NONE }; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1271 | |
Romain Guy | c002936 | 2012-09-24 20:18:13 -0700 | [diff] [blame] | 1272 | EGLContext context = egl.eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, |
| 1273 | mGlVersion != 0 ? attribs : null); |
Romain Guy | ed1b6f4 | 2012-09-24 21:42:18 -0700 | [diff] [blame] | 1274 | if (context == null || context == EGL_NO_CONTEXT) { |
Romain Guy | c002936 | 2012-09-24 20:18:13 -0700 | [diff] [blame] | 1275 | //noinspection ConstantConditions |
| 1276 | throw new IllegalStateException( |
| 1277 | "Could not create an EGL context. eglCreateContext failed with error: " + |
| 1278 | GLUtils.getEGLErrorString(sEgl.eglGetError())); |
| 1279 | } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1280 | |
Romain Guy | c002936 | 2012-09-24 20:18:13 -0700 | [diff] [blame] | 1281 | return context; |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | @Override |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 1285 | void destroy(boolean full) { |
| 1286 | if (full && mCanvas != null) { |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 1287 | mCanvas = null; |
| 1288 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1289 | |
Romain Guy | 357c942 | 2011-08-02 11:32:49 -0700 | [diff] [blame] | 1290 | if (!isEnabled() || mDestroyed) { |
| 1291 | setEnabled(false); |
| 1292 | return; |
| 1293 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1294 | |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 1295 | destroySurface(); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1296 | setEnabled(false); |
Romain Guy | 357c942 | 2011-08-02 11:32:49 -0700 | [diff] [blame] | 1297 | |
| 1298 | mDestroyed = true; |
| 1299 | mGl = null; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 1302 | void destroySurface() { |
| 1303 | if (mEglSurface != null && mEglSurface != EGL_NO_SURFACE) { |
Romain Guy | c8ba0b4 | 2013-06-21 16:40:30 -0700 | [diff] [blame] | 1304 | if (mEglSurface.equals(sEgl.eglGetCurrentSurface(EGL_DRAW))) { |
| 1305 | sEgl.eglMakeCurrent(sEglDisplay, |
| 1306 | EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 1307 | } |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 1308 | sEgl.eglDestroySurface(sEglDisplay, mEglSurface); |
| 1309 | mEglSurface = null; |
| 1310 | } |
| 1311 | } |
| 1312 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1313 | @Override |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1314 | void invalidate(Surface surface) { |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 1315 | // Cancels any existing buffer to ensure we'll get a buffer |
| 1316 | // of the right size before we call eglSwapBuffers |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 1317 | sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1318 | |
Romain Guy | d3facf3 | 2011-08-03 11:14:38 -0700 | [diff] [blame] | 1319 | if (mEglSurface != null && mEglSurface != EGL_NO_SURFACE) { |
| 1320 | sEgl.eglDestroySurface(sEglDisplay, mEglSurface); |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1321 | mEglSurface = null; |
| 1322 | setEnabled(false); |
Romain Guy | d3facf3 | 2011-08-03 11:14:38 -0700 | [diff] [blame] | 1323 | } |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 1324 | |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1325 | if (surface.isValid()) { |
| 1326 | if (!createSurface(surface)) { |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1327 | return; |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 1328 | } |
Romain Guy | 9477c6e | 2011-12-09 12:27:21 -0800 | [diff] [blame] | 1329 | |
| 1330 | mUpdateDirtyRegions = true; |
| 1331 | |
Romain Guy | 86e3e22 | 2011-08-16 14:30:08 -0700 | [diff] [blame] | 1332 | if (mCanvas != null) { |
| 1333 | setEnabled(true); |
| 1334 | } |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 1335 | } |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1336 | } |
Romain Guy | cf15efb | 2011-08-02 12:59:32 -0700 | [diff] [blame] | 1337 | |
Romain Guy | 786fc93 | 2012-07-24 16:24:56 -0700 | [diff] [blame] | 1338 | private boolean createSurface(Surface surface) { |
| 1339 | mEglSurface = sEgl.eglCreateWindowSurface(sEglDisplay, sEglConfig, surface, null); |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1340 | |
| 1341 | if (mEglSurface == null || mEglSurface == EGL_NO_SURFACE) { |
| 1342 | int error = sEgl.eglGetError(); |
| 1343 | if (error == EGL_BAD_NATIVE_WINDOW) { |
| 1344 | Log.e(LOG_TAG, "createWindowSurface returned EGL_BAD_NATIVE_WINDOW."); |
| 1345 | return false; |
| 1346 | } |
| 1347 | throw new RuntimeException("createWindowSurface failed " |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 1348 | + GLUtils.getEGLErrorString(error)); |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1349 | } |
Romain Guy | bd43152 | 2012-09-26 13:31:25 -0700 | [diff] [blame] | 1350 | |
| 1351 | if (!sEgl.eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, mEglContext)) { |
| 1352 | throw new IllegalStateException("eglMakeCurrent failed " + |
| 1353 | GLUtils.getEGLErrorString(sEgl.eglGetError())); |
| 1354 | } |
| 1355 | |
| 1356 | enableDirtyRegions(); |
| 1357 | |
Romain Guy | 1d0c708 | 2011-08-03 16:22:24 -0700 | [diff] [blame] | 1358 | return true; |
| 1359 | } |
| 1360 | |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1361 | @Override |
| 1362 | boolean validate() { |
Romain Guy | cc6b7caf | 2013-06-24 19:19:21 -0700 | [diff] [blame] | 1363 | return checkRenderContext() != SURFACE_STATE_ERROR; |
Romain Guy | 7e1160e | 2011-07-08 15:49:50 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | @Override |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1367 | void setup(int width, int height) { |
Romain Guy | eca9b1f | 2011-08-26 13:36:37 -0700 | [diff] [blame] | 1368 | if (validate()) { |
| 1369 | mCanvas.setViewport(width, height); |
Chet Haase | 40e0383 | 2011-10-06 08:34:13 -0700 | [diff] [blame] | 1370 | mWidth = width; |
| 1371 | mHeight = height; |
Romain Guy | eca9b1f | 2011-08-26 13:36:37 -0700 | [diff] [blame] | 1372 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1373 | } |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1374 | |
Chet Haase | 40e0383 | 2011-10-06 08:34:13 -0700 | [diff] [blame] | 1375 | @Override |
| 1376 | int getWidth() { |
| 1377 | return mWidth; |
| 1378 | } |
| 1379 | |
| 1380 | @Override |
| 1381 | int getHeight() { |
| 1382 | return mHeight; |
| 1383 | } |
| 1384 | |
Chet Haase | 08837c2 | 2011-11-28 11:53:21 -0800 | [diff] [blame] | 1385 | @Override |
| 1386 | HardwareCanvas getCanvas() { |
| 1387 | return mCanvas; |
| 1388 | } |
| 1389 | |
Romain Guy | ef35927 | 2013-01-31 19:07:29 -0800 | [diff] [blame] | 1390 | @Override |
| 1391 | void setName(String name) { |
| 1392 | mName = name; |
| 1393 | } |
| 1394 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1395 | boolean canDraw() { |
| 1396 | return mGl != null && mCanvas != null; |
| 1397 | } |
| 1398 | |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 1399 | int onPreDraw(Rect dirty) { |
| 1400 | return DisplayList.STATUS_DONE; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 1403 | void onPostDraw() { |
| 1404 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1405 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 1406 | class FunctorsRunnable implements Runnable { |
| 1407 | View.AttachInfo attachInfo; |
| 1408 | |
| 1409 | @Override |
| 1410 | public void run() { |
| 1411 | final HardwareRenderer renderer = attachInfo.mHardwareRenderer; |
| 1412 | if (renderer == null || !renderer.isEnabled() || renderer != GlRenderer.this) { |
| 1413 | return; |
| 1414 | } |
| 1415 | |
Romain Guy | cc6b7caf | 2013-06-24 19:19:21 -0700 | [diff] [blame] | 1416 | if (checkRenderContext() != SURFACE_STATE_ERROR) { |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 1417 | int status = mCanvas.invokeFunctors(mRedrawClip); |
| 1418 | handleFunctorStatus(attachInfo, status); |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1423 | @Override |
Romain Guy | e55945e | 2013-04-04 15:26:04 -0700 | [diff] [blame] | 1424 | void draw(View view, View.AttachInfo attachInfo, HardwareDrawCallbacks callbacks, |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1425 | Rect dirty) { |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1426 | if (canDraw()) { |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 1427 | if (!hasDirtyRegions()) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1428 | dirty = null; |
| 1429 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1430 | attachInfo.mIgnoreDirtyState = true; |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 1431 | attachInfo.mDrawingTime = SystemClock.uptimeMillis(); |
| 1432 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1433 | view.mPrivateFlags |= View.PFLAG_DRAWN; |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1434 | |
Romain Guy | cc6b7caf | 2013-06-24 19:19:21 -0700 | [diff] [blame] | 1435 | // We are already on the correct thread |
| 1436 | final int surfaceState = checkRenderContextUnsafe(); |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 1437 | if (surfaceState != SURFACE_STATE_ERROR) { |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 1438 | HardwareCanvas canvas = mCanvas; |
| 1439 | attachInfo.mHardwareCanvas = canvas; |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 1440 | |
| 1441 | if (mProfileEnabled) { |
| 1442 | mProfileLock.lock(); |
| 1443 | } |
| 1444 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 1445 | dirty = beginFrame(canvas, dirty, surfaceState); |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 1446 | |
Romain Guy | d17043d | 2013-02-19 14:12:55 -0800 | [diff] [blame] | 1447 | DisplayList displayList = buildDisplayList(view, canvas); |
| 1448 | |
Romain Guy | cc6b7caf | 2013-06-24 19:19:21 -0700 | [diff] [blame] | 1449 | // buildDisplayList() calls into user code which can cause |
| 1450 | // an eglMakeCurrent to happen with a different surface/context. |
| 1451 | // We must therefore check again here. |
| 1452 | if (checkRenderContextUnsafe() == SURFACE_STATE_ERROR) { |
| 1453 | return; |
| 1454 | } |
| 1455 | |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 1456 | int saveCount = 0; |
| 1457 | int status = DisplayList.STATUS_DONE; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1458 | |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 1459 | long start = getSystemTime(); |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 1460 | try { |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 1461 | status = prepareFrame(dirty); |
Jeff Brown | 95db2b2 | 2011-11-30 19:54:41 -0800 | [diff] [blame] | 1462 | |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 1463 | saveCount = canvas.save(); |
| 1464 | callbacks.onHardwarePreDraw(canvas); |
| 1465 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1466 | if (displayList != null) { |
Chet Haase | 58d110a | 2013-04-10 07:43:29 -0700 | [diff] [blame] | 1467 | status |= drawDisplayList(attachInfo, canvas, displayList, status); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1468 | } else { |
| 1469 | // Shouldn't reach here |
| 1470 | view.draw(canvas); |
| 1471 | } |
Romain Guy | d17043d | 2013-02-19 14:12:55 -0800 | [diff] [blame] | 1472 | } catch (Exception e) { |
| 1473 | Log.e(LOG_TAG, "An error has occurred while drawing:", e); |
Romain Guy | b04f7e9 | 2012-02-15 12:36:54 -0800 | [diff] [blame] | 1474 | } finally { |
| 1475 | callbacks.onHardwarePostDraw(canvas); |
| 1476 | canvas.restoreToCount(saveCount); |
| 1477 | view.mRecreateDisplayList = false; |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 1478 | |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 1479 | mDrawDelta = getSystemTime() - start; |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 1480 | |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 1481 | if (mDrawDelta > 0) { |
| 1482 | mFrameCount++; |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 1483 | |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 1484 | debugOverdraw(attachInfo, dirty, canvas, displayList); |
| 1485 | debugDirtyRegions(dirty, canvas); |
| 1486 | drawProfileData(attachInfo); |
| 1487 | } |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 1488 | } |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1489 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 1490 | onPostDraw(); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1491 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 1492 | swapBuffers(status); |
Jeff Brown | 95db2b2 | 2011-11-30 19:54:41 -0800 | [diff] [blame] | 1493 | |
Romain Guy | 77e67cf | 2012-06-19 16:38:50 -0700 | [diff] [blame] | 1494 | if (mProfileEnabled) { |
| 1495 | mProfileLock.unlock(); |
| 1496 | } |
| 1497 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 1498 | attachInfo.mIgnoreDirtyState = false; |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1499 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1500 | } |
| 1501 | } |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1502 | |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 1503 | abstract void countOverdraw(HardwareCanvas canvas); |
| 1504 | abstract float getOverdraw(HardwareCanvas canvas); |
| 1505 | |
| 1506 | private void debugOverdraw(View.AttachInfo attachInfo, Rect dirty, |
| 1507 | HardwareCanvas canvas, DisplayList displayList) { |
| 1508 | |
| 1509 | if (mDebugOverdraw == OVERDRAW_TYPE_COUNT) { |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 1510 | if (mDebugOverdrawLayer == null) { |
| 1511 | mDebugOverdrawLayer = createHardwareLayer(mWidth, mHeight, true); |
| 1512 | } else if (mDebugOverdrawLayer.getWidth() != mWidth || |
| 1513 | mDebugOverdrawLayer.getHeight() != mHeight) { |
| 1514 | mDebugOverdrawLayer.resize(mWidth, mHeight); |
| 1515 | } |
| 1516 | |
| 1517 | if (!mDebugOverdrawLayer.isValid()) { |
| 1518 | mDebugOverdraw = -1; |
| 1519 | return; |
| 1520 | } |
| 1521 | |
| 1522 | HardwareCanvas layerCanvas = mDebugOverdrawLayer.start(canvas, dirty); |
| 1523 | countOverdraw(layerCanvas); |
| 1524 | final int restoreCount = layerCanvas.save(); |
| 1525 | layerCanvas.drawDisplayList(displayList, null, DisplayList.FLAG_CLIP_CHILDREN); |
| 1526 | layerCanvas.restoreToCount(restoreCount); |
| 1527 | mDebugOverdrawLayer.end(canvas); |
| 1528 | |
| 1529 | float overdraw = getOverdraw(layerCanvas); |
| 1530 | DisplayMetrics metrics = attachInfo.mRootView.getResources().getDisplayMetrics(); |
| 1531 | |
| 1532 | drawOverdrawCounter(canvas, overdraw, metrics.density); |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | private void drawOverdrawCounter(HardwareCanvas canvas, float overdraw, float density) { |
| 1537 | final String text = String.format("%.2fx", overdraw); |
| 1538 | final Paint paint = setupPaint(density); |
| 1539 | // HSBtoColor will clamp the values in the 0..1 range |
| 1540 | paint.setColor(Color.HSBtoColor(0.28f - 0.28f * overdraw / 3.5f, 0.8f, 1.0f)); |
| 1541 | |
| 1542 | canvas.drawText(text, density * 4.0f, mHeight - paint.getFontMetrics().bottom, paint); |
| 1543 | } |
| 1544 | |
| 1545 | private Paint setupPaint(float density) { |
| 1546 | if (mDebugOverdrawPaint == null) { |
| 1547 | mDebugOverdrawPaint = new Paint(); |
| 1548 | mDebugOverdrawPaint.setAntiAlias(true); |
| 1549 | mDebugOverdrawPaint.setShadowLayer(density * 3.0f, 0.0f, 0.0f, 0xff000000); |
| 1550 | mDebugOverdrawPaint.setTextSize(density * 20.0f); |
| 1551 | } |
| 1552 | return mDebugOverdrawPaint; |
| 1553 | } |
| 1554 | |
Romain Guy | d17043d | 2013-02-19 14:12:55 -0800 | [diff] [blame] | 1555 | private DisplayList buildDisplayList(View view, HardwareCanvas canvas) { |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 1556 | if (mDrawDelta <= 0) { |
| 1557 | return view.mDisplayList; |
| 1558 | } |
| 1559 | |
Romain Guy | d17043d | 2013-02-19 14:12:55 -0800 | [diff] [blame] | 1560 | view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED) |
| 1561 | == View.PFLAG_INVALIDATED; |
| 1562 | view.mPrivateFlags &= ~View.PFLAG_INVALIDATED; |
| 1563 | |
| 1564 | long buildDisplayListStartTime = startBuildDisplayListProfiling(); |
| 1565 | canvas.clearLayerUpdates(); |
| 1566 | |
| 1567 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "getDisplayList"); |
| 1568 | DisplayList displayList = view.getDisplayList(); |
| 1569 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 1570 | |
| 1571 | endBuildDisplayListProfiling(buildDisplayListStartTime); |
| 1572 | |
| 1573 | return displayList; |
| 1574 | } |
| 1575 | |
Romain Guy | 98e4a52 | 2013-01-07 10:58:34 -0800 | [diff] [blame] | 1576 | abstract void drawProfileData(View.AttachInfo attachInfo); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 1577 | |
| 1578 | private Rect beginFrame(HardwareCanvas canvas, Rect dirty, int surfaceState) { |
| 1579 | // We had to change the current surface and/or context, redraw everything |
| 1580 | if (surfaceState == SURFACE_STATE_UPDATED) { |
| 1581 | dirty = null; |
| 1582 | beginFrame(null); |
| 1583 | } else { |
| 1584 | int[] size = mSurfaceSize; |
| 1585 | beginFrame(size); |
| 1586 | |
| 1587 | if (size[1] != mHeight || size[0] != mWidth) { |
| 1588 | mWidth = size[0]; |
| 1589 | mHeight = size[1]; |
| 1590 | |
| 1591 | canvas.setViewport(mWidth, mHeight); |
| 1592 | |
| 1593 | dirty = null; |
| 1594 | } |
| 1595 | } |
| 1596 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 1597 | if (mDebugDataProvider != null) dirty = null; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 1598 | |
| 1599 | return dirty; |
| 1600 | } |
| 1601 | |
| 1602 | private long startBuildDisplayListProfiling() { |
| 1603 | if (mProfileEnabled) { |
| 1604 | mProfileCurrentFrame += PROFILE_FRAME_DATA_COUNT; |
| 1605 | if (mProfileCurrentFrame >= mProfileData.length) { |
| 1606 | mProfileCurrentFrame = 0; |
| 1607 | } |
| 1608 | |
| 1609 | return System.nanoTime(); |
| 1610 | } |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
| 1614 | private void endBuildDisplayListProfiling(long getDisplayListStartTime) { |
| 1615 | if (mProfileEnabled) { |
| 1616 | long now = System.nanoTime(); |
| 1617 | float total = (now - getDisplayListStartTime) * 0.000001f; |
| 1618 | //noinspection PointlessArithmeticExpression |
| 1619 | mProfileData[mProfileCurrentFrame] = total; |
| 1620 | } |
| 1621 | } |
| 1622 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 1623 | private int prepareFrame(Rect dirty) { |
| 1624 | int status; |
| 1625 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "prepareFrame"); |
| 1626 | try { |
| 1627 | status = onPreDraw(dirty); |
| 1628 | } finally { |
| 1629 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 1630 | } |
| 1631 | return status; |
| 1632 | } |
| 1633 | |
| 1634 | private int drawDisplayList(View.AttachInfo attachInfo, HardwareCanvas canvas, |
| 1635 | DisplayList displayList, int status) { |
| 1636 | |
| 1637 | long drawDisplayListStartTime = 0; |
| 1638 | if (mProfileEnabled) { |
| 1639 | drawDisplayListStartTime = System.nanoTime(); |
| 1640 | } |
| 1641 | |
| 1642 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "drawDisplayList"); |
| 1643 | try { |
| 1644 | status |= canvas.drawDisplayList(displayList, mRedrawClip, |
| 1645 | DisplayList.FLAG_CLIP_CHILDREN); |
| 1646 | } finally { |
| 1647 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 1648 | } |
| 1649 | |
| 1650 | if (mProfileEnabled) { |
| 1651 | long now = System.nanoTime(); |
| 1652 | float total = (now - drawDisplayListStartTime) * 0.000001f; |
| 1653 | mProfileData[mProfileCurrentFrame + 1] = total; |
| 1654 | } |
| 1655 | |
| 1656 | handleFunctorStatus(attachInfo, status); |
| 1657 | return status; |
| 1658 | } |
| 1659 | |
| 1660 | private void swapBuffers(int status) { |
| 1661 | if ((status & DisplayList.STATUS_DREW) == DisplayList.STATUS_DREW) { |
| 1662 | long eglSwapBuffersStartTime = 0; |
| 1663 | if (mProfileEnabled) { |
| 1664 | eglSwapBuffersStartTime = System.nanoTime(); |
| 1665 | } |
| 1666 | |
| 1667 | sEgl.eglSwapBuffers(sEglDisplay, mEglSurface); |
| 1668 | |
| 1669 | if (mProfileEnabled) { |
| 1670 | long now = System.nanoTime(); |
| 1671 | float total = (now - eglSwapBuffersStartTime) * 0.000001f; |
| 1672 | mProfileData[mProfileCurrentFrame + 2] = total; |
| 1673 | } |
| 1674 | |
| 1675 | checkEglErrors(); |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | private void debugDirtyRegions(Rect dirty, HardwareCanvas canvas) { |
| 1680 | if (mDebugDirtyRegions) { |
| 1681 | if (mDebugPaint == null) { |
| 1682 | mDebugPaint = new Paint(); |
| 1683 | mDebugPaint.setColor(0x7fff0000); |
| 1684 | } |
| 1685 | |
| 1686 | if (dirty != null && (mFrameCount & 1) == 0) { |
| 1687 | canvas.drawRect(dirty, mDebugPaint); |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 1692 | private void handleFunctorStatus(View.AttachInfo attachInfo, int status) { |
| 1693 | // If the draw flag is set, functors will be invoked while executing |
| 1694 | // the tree of display lists |
| 1695 | if ((status & DisplayList.STATUS_DRAW) != 0) { |
| 1696 | if (mRedrawClip.isEmpty()) { |
| 1697 | attachInfo.mViewRootImpl.invalidate(); |
| 1698 | } else { |
| 1699 | attachInfo.mViewRootImpl.invalidateChildInParent(null, mRedrawClip); |
| 1700 | mRedrawClip.setEmpty(); |
| 1701 | } |
| 1702 | } |
| 1703 | |
Chris Craik | 9efa222 | 2012-12-04 15:15:31 -0800 | [diff] [blame] | 1704 | if ((status & DisplayList.STATUS_INVOKE) != 0 || |
| 1705 | attachInfo.mHandler.hasCallbacks(mFunctorsRunnable)) { |
| 1706 | attachInfo.mHandler.removeCallbacks(mFunctorsRunnable); |
| 1707 | mFunctorsRunnable.attachInfo = attachInfo; |
| 1708 | attachInfo.mHandler.postDelayed(mFunctorsRunnable, FUNCTOR_PROCESS_DELAY); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1712 | @Override |
| 1713 | void detachFunctor(int functor) { |
| 1714 | if (mCanvas != null) { |
| 1715 | mCanvas.detachFunctor(functor); |
Chris Craik | 932b7f6 | 2012-06-06 13:59:33 -0700 | [diff] [blame] | 1716 | } |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | @Override |
Chris Craik | 41ee465 | 2012-05-31 15:05:57 -0700 | [diff] [blame] | 1720 | boolean attachFunctor(View.AttachInfo attachInfo, int functor) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1721 | if (mCanvas != null) { |
| 1722 | mCanvas.attachFunctor(functor); |
Chris Craik | 9efa222 | 2012-12-04 15:15:31 -0800 | [diff] [blame] | 1723 | mFunctorsRunnable.attachInfo = attachInfo; |
| 1724 | attachInfo.mHandler.removeCallbacks(mFunctorsRunnable); |
| 1725 | attachInfo.mHandler.postDelayed(mFunctorsRunnable, 0); |
Chris Craik | 41ee465 | 2012-05-31 15:05:57 -0700 | [diff] [blame] | 1726 | return true; |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1727 | } |
Chris Craik | 41ee465 | 2012-05-31 15:05:57 -0700 | [diff] [blame] | 1728 | return false; |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 1729 | } |
| 1730 | |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1731 | /** |
Romain Guy | cc6b7caf | 2013-06-24 19:19:21 -0700 | [diff] [blame] | 1732 | * Ensures the current EGL context and surface are the ones we expect. |
| 1733 | * This method throws an IllegalStateException if invoked from a thread |
| 1734 | * that did not initialize EGL. |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1735 | * |
| 1736 | * @return {@link #SURFACE_STATE_ERROR} if the correct EGL context cannot be made current, |
| 1737 | * {@link #SURFACE_STATE_UPDATED} if the EGL context was changed or |
| 1738 | * {@link #SURFACE_STATE_SUCCESS} if the EGL context was the correct one |
Romain Guy | cc6b7caf | 2013-06-24 19:19:21 -0700 | [diff] [blame] | 1739 | * |
| 1740 | * @see #checkRenderContextUnsafe() |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1741 | */ |
Romain Guy | cc6b7caf | 2013-06-24 19:19:21 -0700 | [diff] [blame] | 1742 | int checkRenderContext() { |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1743 | if (mEglThread != Thread.currentThread()) { |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1744 | throw new IllegalStateException("Hardware acceleration can only be used with a " + |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1745 | "single UI thread.\nOriginal thread: " + mEglThread + "\n" + |
Romain Guy | 0398575 | 2011-07-11 15:33:51 -0700 | [diff] [blame] | 1746 | "Current thread: " + Thread.currentThread()); |
| 1747 | } |
| 1748 | |
Romain Guy | cc6b7caf | 2013-06-24 19:19:21 -0700 | [diff] [blame] | 1749 | return checkRenderContextUnsafe(); |
| 1750 | } |
| 1751 | |
| 1752 | /** |
| 1753 | * Ensures the current EGL context and surface are the ones we expect. |
| 1754 | * This method does not check the current thread. |
| 1755 | * |
| 1756 | * @return {@link #SURFACE_STATE_ERROR} if the correct EGL context cannot be made current, |
| 1757 | * {@link #SURFACE_STATE_UPDATED} if the EGL context was changed or |
| 1758 | * {@link #SURFACE_STATE_SUCCESS} if the EGL context was the correct one |
| 1759 | * |
| 1760 | * @see #checkRenderContext() |
| 1761 | */ |
| 1762 | private int checkRenderContextUnsafe() { |
| 1763 | if (!mEglSurface.equals(sEgl.eglGetCurrentSurface(EGL_DRAW)) || |
| 1764 | !mEglContext.equals(sEgl.eglGetCurrentContext())) { |
Romain Guy | 566b3ef | 2011-07-18 18:38:12 -0700 | [diff] [blame] | 1765 | if (!sEgl.eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, mEglContext)) { |
Romain Guy | 9745fae | 2010-12-08 11:39:15 -0800 | [diff] [blame] | 1766 | Log.e(LOG_TAG, "eglMakeCurrent failed " + |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 1767 | GLUtils.getEGLErrorString(sEgl.eglGetError())); |
Romain Guy | eca9b1f | 2011-08-26 13:36:37 -0700 | [diff] [blame] | 1768 | fallback(true); |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 1769 | return SURFACE_STATE_ERROR; |
| 1770 | } else { |
Romain Guy | 9477c6e | 2011-12-09 12:27:21 -0800 | [diff] [blame] | 1771 | if (mUpdateDirtyRegions) { |
| 1772 | enableDirtyRegions(); |
| 1773 | mUpdateDirtyRegions = false; |
| 1774 | } |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 1775 | return SURFACE_STATE_UPDATED; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1776 | } |
| 1777 | } |
Romain Guy | d88f54c | 2011-01-24 20:22:49 -0800 | [diff] [blame] | 1778 | return SURFACE_STATE_SUCCESS; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1779 | } |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 1780 | |
| 1781 | private static int dpToPx(int dp, float density) { |
| 1782 | return (int) (dp * density + 0.5f); |
| 1783 | } |
| 1784 | |
| 1785 | class DrawPerformanceDataProvider extends GraphDataProvider { |
| 1786 | private final int mGraphType; |
| 1787 | |
| 1788 | private int mVerticalUnit; |
| 1789 | private int mHorizontalUnit; |
| 1790 | private int mHorizontalMargin; |
| 1791 | private int mThresholdStroke; |
| 1792 | |
| 1793 | DrawPerformanceDataProvider(int graphType) { |
| 1794 | mGraphType = graphType; |
| 1795 | } |
| 1796 | |
| 1797 | @Override |
| 1798 | void prepare(DisplayMetrics metrics) { |
| 1799 | final float density = metrics.density; |
| 1800 | |
| 1801 | mVerticalUnit = dpToPx(PROFILE_DRAW_DP_PER_MS, density); |
| 1802 | mHorizontalUnit = dpToPx(PROFILE_DRAW_WIDTH, density); |
| 1803 | mHorizontalMargin = dpToPx(PROFILE_DRAW_MARGIN, density); |
| 1804 | mThresholdStroke = dpToPx(PROFILE_DRAW_THRESHOLD_STROKE_WIDTH, density); |
| 1805 | } |
| 1806 | |
| 1807 | @Override |
| 1808 | int getGraphType() { |
| 1809 | return mGraphType; |
| 1810 | } |
| 1811 | |
| 1812 | @Override |
| 1813 | int getVerticalUnitSize() { |
| 1814 | return mVerticalUnit; |
| 1815 | } |
| 1816 | |
| 1817 | @Override |
| 1818 | int getHorizontalUnitSize() { |
| 1819 | return mHorizontalUnit; |
| 1820 | } |
| 1821 | |
| 1822 | @Override |
| 1823 | int getHorizontaUnitMargin() { |
| 1824 | return mHorizontalMargin; |
| 1825 | } |
| 1826 | |
| 1827 | @Override |
| 1828 | float[] getData() { |
| 1829 | return mProfileData; |
| 1830 | } |
| 1831 | |
| 1832 | @Override |
| 1833 | float getThreshold() { |
| 1834 | return 16; |
| 1835 | } |
| 1836 | |
| 1837 | @Override |
| 1838 | int getFrameCount() { |
| 1839 | return mProfileData.length / PROFILE_FRAME_DATA_COUNT; |
| 1840 | } |
| 1841 | |
| 1842 | @Override |
| 1843 | int getElementCount() { |
| 1844 | return PROFILE_FRAME_DATA_COUNT; |
| 1845 | } |
| 1846 | |
| 1847 | @Override |
| 1848 | int getCurrentFrame() { |
| 1849 | return mProfileCurrentFrame / PROFILE_FRAME_DATA_COUNT; |
| 1850 | } |
| 1851 | |
| 1852 | @Override |
| 1853 | void setupGraphPaint(Paint paint, int elementIndex) { |
| 1854 | paint.setColor(PROFILE_DRAW_COLORS[elementIndex]); |
| 1855 | if (mGraphType == GRAPH_TYPE_LINES) paint.setStrokeWidth(mThresholdStroke); |
| 1856 | } |
| 1857 | |
| 1858 | @Override |
| 1859 | void setupThresholdPaint(Paint paint) { |
| 1860 | paint.setColor(PROFILE_DRAW_THRESHOLD_COLOR); |
| 1861 | paint.setStrokeWidth(mThresholdStroke); |
| 1862 | } |
| 1863 | |
| 1864 | @Override |
| 1865 | void setupCurrentFramePaint(Paint paint) { |
| 1866 | paint.setColor(PROFILE_DRAW_CURRENT_FRAME_COLOR); |
| 1867 | if (mGraphType == GRAPH_TYPE_LINES) paint.setStrokeWidth(mThresholdStroke); |
| 1868 | } |
| 1869 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1870 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1871 | |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1872 | /** |
| 1873 | * Hardware renderer using OpenGL ES 2.0. |
| 1874 | */ |
| 1875 | static class Gl20Renderer extends GlRenderer { |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1876 | private GLES20Canvas mGlCanvas; |
| 1877 | |
Romain Guy | 98e4a52 | 2013-01-07 10:58:34 -0800 | [diff] [blame] | 1878 | private DisplayMetrics mDisplayMetrics; |
| 1879 | |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 1880 | private static EGLSurface sPbuffer; |
| 1881 | private static final Object[] sPbufferLock = new Object[0]; |
| 1882 | |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 1883 | static class Gl20RendererEglContext extends ManagedEGLContext { |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1884 | final Handler mHandler = new Handler(); |
| 1885 | |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 1886 | public Gl20RendererEglContext(EGLContext context) { |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1887 | super(context); |
| 1888 | } |
| 1889 | |
| 1890 | @Override |
| 1891 | public void onTerminate(final EGLContext eglContext) { |
| 1892 | // Make sure we do this on the correct thread. |
| 1893 | if (mHandler.getLooper() != Looper.myLooper()) { |
| 1894 | mHandler.post(new Runnable() { |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 1895 | @Override |
| 1896 | public void run() { |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1897 | onTerminate(eglContext); |
| 1898 | } |
| 1899 | }); |
| 1900 | return; |
| 1901 | } |
| 1902 | |
| 1903 | synchronized (sEglLock) { |
| 1904 | if (sEgl == null) return; |
| 1905 | |
| 1906 | if (EGLImpl.getInitCount(sEglDisplay) == 1) { |
| 1907 | usePbufferSurface(eglContext); |
| 1908 | GLES20Canvas.terminateCaches(); |
| 1909 | |
| 1910 | sEgl.eglDestroyContext(sEglDisplay, eglContext); |
Romain Guy | a998dff | 2012-03-23 18:58:36 -0700 | [diff] [blame] | 1911 | sEglContextStorage.set(null); |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1912 | sEglContextStorage.remove(); |
| 1913 | |
| 1914 | sEgl.eglDestroySurface(sEglDisplay, sPbuffer); |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 1915 | sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, |
| 1916 | EGL_NO_SURFACE, EGL_NO_CONTEXT); |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1917 | |
| 1918 | sEgl.eglReleaseThread(); |
| 1919 | sEgl.eglTerminate(sEglDisplay); |
| 1920 | |
| 1921 | sEgl = null; |
| 1922 | sEglDisplay = null; |
| 1923 | sEglConfig = null; |
| 1924 | sPbuffer = null; |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 1925 | } |
| 1926 | } |
| 1927 | } |
| 1928 | } |
| 1929 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1930 | Gl20Renderer(boolean translucent) { |
| 1931 | super(2, translucent); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | @Override |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 1935 | HardwareCanvas createCanvas() { |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 1936 | return mGlCanvas = new GLES20Canvas(mTranslucent); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1937 | } |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1938 | |
| 1939 | @Override |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 1940 | ManagedEGLContext createManagedContext(EGLContext eglContext) { |
| 1941 | return new Gl20Renderer.Gl20RendererEglContext(mEglContext); |
| 1942 | } |
| 1943 | |
| 1944 | @Override |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1945 | int[] getConfig(boolean dirtyRegions) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1946 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 1947 | final int stencilSize = GLES20Canvas.getStencilSize(); |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 1948 | final int swapBehavior = dirtyRegions ? EGL14.EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
| 1949 | |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1950 | return new int[] { |
Romain Guy | bd43152 | 2012-09-26 13:31:25 -0700 | [diff] [blame] | 1951 | EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT, |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1952 | EGL_RED_SIZE, 8, |
| 1953 | EGL_GREEN_SIZE, 8, |
| 1954 | EGL_BLUE_SIZE, 8, |
| 1955 | EGL_ALPHA_SIZE, 8, |
| 1956 | EGL_DEPTH_SIZE, 0, |
Romain Guy | 8efca54 | 2012-10-15 18:09:49 -0700 | [diff] [blame] | 1957 | EGL_CONFIG_CAVEAT, EGL_NONE, |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 1958 | EGL_STENCIL_SIZE, stencilSize, |
| 1959 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior, |
Romain Guy | 484c719 | 2011-07-25 11:56:33 -0700 | [diff] [blame] | 1960 | EGL_NONE |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1961 | }; |
| 1962 | } |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 1963 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 1964 | @Override |
| 1965 | void initCaches() { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1966 | if (GLES20Canvas.initCaches()) { |
| 1967 | // Caches were (re)initialized, rebind atlas |
| 1968 | initAtlas(); |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | @Override |
| 1973 | void initAtlas() { |
| 1974 | IBinder binder = ServiceManager.getService("assetatlas"); |
Romain Guy | 927bc7d | 2013-05-03 11:32:09 -0700 | [diff] [blame] | 1975 | if (binder == null) return; |
| 1976 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1977 | IAssetAtlas atlas = IAssetAtlas.Stub.asInterface(binder); |
| 1978 | try { |
Romain Guy | 80b12fc | 2013-05-29 15:54:25 -0700 | [diff] [blame] | 1979 | if (atlas.isCompatible(android.os.Process.myPpid())) { |
| 1980 | GraphicBuffer buffer = atlas.getBuffer(); |
| 1981 | if (buffer != null) { |
| 1982 | int[] map = atlas.getMap(); |
| 1983 | if (map != null) { |
| 1984 | GLES20Canvas.initAtlas(buffer, map); |
| 1985 | } |
Romain Guy | b2ed04a | 2013-07-09 18:56:57 -0700 | [diff] [blame] | 1986 | buffer.destroy(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1987 | } |
| 1988 | } |
| 1989 | } catch (RemoteException e) { |
| 1990 | Log.w(LOG_TAG, "Could not acquire atlas", e); |
| 1991 | } |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 1992 | } |
Romain Guy | e91a9c7 | 2011-05-02 14:53:30 -0700 | [diff] [blame] | 1993 | |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 1994 | @Override |
| 1995 | boolean canDraw() { |
| 1996 | return super.canDraw() && mGlCanvas != null; |
| 1997 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1998 | |
| 1999 | @Override |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 2000 | int onPreDraw(Rect dirty) { |
| 2001 | return mGlCanvas.onPreDraw(dirty); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 2004 | @Override |
| 2005 | void onPostDraw() { |
| 2006 | mGlCanvas.onPostDraw(); |
| 2007 | } |
| 2008 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 2009 | @Override |
Romain Guy | 98e4a52 | 2013-01-07 10:58:34 -0800 | [diff] [blame] | 2010 | void drawProfileData(View.AttachInfo attachInfo) { |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2011 | if (mDebugDataProvider != null) { |
| 2012 | final GraphDataProvider provider = mDebugDataProvider; |
| 2013 | initProfileDrawData(attachInfo, provider); |
Romain Guy | 98e4a52 | 2013-01-07 10:58:34 -0800 | [diff] [blame] | 2014 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2015 | final int height = provider.getVerticalUnitSize(); |
| 2016 | final int margin = provider.getHorizontaUnitMargin(); |
| 2017 | final int width = provider.getHorizontalUnitSize(); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2018 | |
| 2019 | int x = 0; |
| 2020 | int count = 0; |
| 2021 | int current = 0; |
| 2022 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2023 | final float[] data = provider.getData(); |
| 2024 | final int elementCount = provider.getElementCount(); |
| 2025 | final int graphType = provider.getGraphType(); |
| 2026 | |
| 2027 | int totalCount = provider.getFrameCount() * elementCount; |
| 2028 | if (graphType == GraphDataProvider.GRAPH_TYPE_LINES) { |
| 2029 | totalCount -= elementCount; |
| 2030 | } |
| 2031 | |
| 2032 | for (int i = 0; i < totalCount; i += elementCount) { |
| 2033 | if (data[i] < 0.0f) break; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2034 | |
| 2035 | int index = count * 4; |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2036 | if (i == provider.getCurrentFrame() * elementCount) current = index; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2037 | |
Romain Guy | 98e4a52 | 2013-01-07 10:58:34 -0800 | [diff] [blame] | 2038 | x += margin; |
| 2039 | int x2 = x + width; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2040 | |
| 2041 | int y2 = mHeight; |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2042 | int y1 = (int) (y2 - data[i] * height); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2043 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2044 | switch (graphType) { |
| 2045 | case GraphDataProvider.GRAPH_TYPE_BARS: { |
| 2046 | for (int j = 0; j < elementCount; j++) { |
| 2047 | //noinspection MismatchedReadAndWriteOfArray |
| 2048 | final float[] r = mProfileShapes[j]; |
| 2049 | r[index] = x; |
| 2050 | r[index + 1] = y1; |
| 2051 | r[index + 2] = x2; |
| 2052 | r[index + 3] = y2; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2053 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2054 | y2 = y1; |
| 2055 | if (j < elementCount - 1) { |
| 2056 | y1 = (int) (y2 - data[i + j + 1] * height); |
| 2057 | } |
| 2058 | } |
| 2059 | } break; |
| 2060 | case GraphDataProvider.GRAPH_TYPE_LINES: { |
| 2061 | for (int j = 0; j < elementCount; j++) { |
| 2062 | //noinspection MismatchedReadAndWriteOfArray |
| 2063 | final float[] r = mProfileShapes[j]; |
| 2064 | r[index] = (x + x2) * 0.5f; |
| 2065 | r[index + 1] = index == 0 ? y1 : r[index - 1]; |
| 2066 | r[index + 2] = r[index] + width; |
| 2067 | r[index + 3] = y1; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2068 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2069 | y2 = y1; |
| 2070 | if (j < elementCount - 1) { |
| 2071 | y1 = (int) (y2 - data[i + j + 1] * height); |
| 2072 | } |
| 2073 | } |
| 2074 | } break; |
| 2075 | } |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2076 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2077 | |
Romain Guy | 98e4a52 | 2013-01-07 10:58:34 -0800 | [diff] [blame] | 2078 | x += width; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2079 | count++; |
| 2080 | } |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2081 | |
Romain Guy | 98e4a52 | 2013-01-07 10:58:34 -0800 | [diff] [blame] | 2082 | x += margin; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2083 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2084 | drawGraph(graphType, count); |
| 2085 | drawCurrentFrame(graphType, current); |
| 2086 | drawThreshold(x, height); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2090 | private void drawGraph(int graphType, int count) { |
| 2091 | for (int i = 0; i < mProfileShapes.length; i++) { |
| 2092 | mDebugDataProvider.setupGraphPaint(mProfilePaint, i); |
| 2093 | switch (graphType) { |
| 2094 | case GraphDataProvider.GRAPH_TYPE_BARS: |
Chris Craik | 2af4635 | 2012-11-26 18:30:17 -0800 | [diff] [blame] | 2095 | mGlCanvas.drawRects(mProfileShapes[i], count * 4, mProfilePaint); |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2096 | break; |
| 2097 | case GraphDataProvider.GRAPH_TYPE_LINES: |
| 2098 | mGlCanvas.drawLines(mProfileShapes[i], 0, count * 4, mProfilePaint); |
| 2099 | break; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2100 | } |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | private void drawCurrentFrame(int graphType, int index) { |
| 2105 | if (index >= 0) { |
| 2106 | mDebugDataProvider.setupCurrentFramePaint(mProfilePaint); |
| 2107 | switch (graphType) { |
| 2108 | case GraphDataProvider.GRAPH_TYPE_BARS: |
| 2109 | mGlCanvas.drawRect(mProfileShapes[2][index], mProfileShapes[2][index + 1], |
| 2110 | mProfileShapes[2][index + 2], mProfileShapes[0][index + 3], |
| 2111 | mProfilePaint); |
| 2112 | break; |
| 2113 | case GraphDataProvider.GRAPH_TYPE_LINES: |
| 2114 | mGlCanvas.drawLine(mProfileShapes[2][index], mProfileShapes[2][index + 1], |
| 2115 | mProfileShapes[2][index], mHeight, mProfilePaint); |
| 2116 | break; |
| 2117 | } |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | private void drawThreshold(int x, int height) { |
| 2122 | float threshold = mDebugDataProvider.getThreshold(); |
| 2123 | if (threshold > 0.0f) { |
| 2124 | mDebugDataProvider.setupThresholdPaint(mProfilePaint); |
| 2125 | int y = (int) (mHeight - threshold * height); |
| 2126 | mGlCanvas.drawLine(0.0f, y, x, y, mProfilePaint); |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | private void initProfileDrawData(View.AttachInfo attachInfo, GraphDataProvider provider) { |
| 2131 | if (mProfileShapes == null) { |
| 2132 | final int elementCount = provider.getElementCount(); |
| 2133 | final int frameCount = provider.getFrameCount(); |
| 2134 | |
| 2135 | mProfileShapes = new float[elementCount][]; |
| 2136 | for (int i = 0; i < elementCount; i++) { |
| 2137 | mProfileShapes[i] = new float[frameCount * 4]; |
| 2138 | } |
| 2139 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2140 | mProfilePaint = new Paint(); |
| 2141 | } |
Romain Guy | 98e4a52 | 2013-01-07 10:58:34 -0800 | [diff] [blame] | 2142 | |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2143 | mProfilePaint.reset(); |
| 2144 | if (provider.getGraphType() == GraphDataProvider.GRAPH_TYPE_LINES) { |
| 2145 | mProfilePaint.setAntiAlias(true); |
| 2146 | } |
| 2147 | |
Romain Guy | 98e4a52 | 2013-01-07 10:58:34 -0800 | [diff] [blame] | 2148 | if (mDisplayMetrics == null) { |
| 2149 | mDisplayMetrics = new DisplayMetrics(); |
| 2150 | } |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2151 | |
Romain Guy | 98e4a52 | 2013-01-07 10:58:34 -0800 | [diff] [blame] | 2152 | attachInfo.mDisplay.getMetrics(mDisplayMetrics); |
Romain Guy | 48ef4a9 | 2013-01-10 18:38:46 -0800 | [diff] [blame] | 2153 | provider.prepare(mDisplayMetrics); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | @Override |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 2157 | void destroy(boolean full) { |
Romain Guy | b8c0de2 | 2010-12-13 14:42:34 -0800 | [diff] [blame] | 2158 | try { |
| 2159 | super.destroy(full); |
| 2160 | } finally { |
| 2161 | if (full && mGlCanvas != null) { |
| 2162 | mGlCanvas = null; |
| 2163 | } |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | @Override |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 2168 | void pushLayerUpdate(HardwareLayer layer) { |
| 2169 | mGlCanvas.pushLayerUpdate(layer); |
| 2170 | } |
| 2171 | |
| 2172 | @Override |
Romain Guy | e93482f | 2013-06-17 13:14:51 -0700 | [diff] [blame] | 2173 | void cancelLayerUpdate(HardwareLayer layer) { |
| 2174 | mGlCanvas.cancelLayerUpdate(layer); |
| 2175 | } |
| 2176 | |
| 2177 | @Override |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 2178 | void flushLayerUpdates() { |
| 2179 | mGlCanvas.flushLayerUpdates(); |
| 2180 | } |
| 2181 | |
| 2182 | @Override |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 2183 | public DisplayList createDisplayList(String name) { |
| 2184 | return new GLES20DisplayList(name); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 2185 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 2186 | |
| 2187 | @Override |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 2188 | HardwareLayer createHardwareLayer(boolean isOpaque) { |
| 2189 | return new GLES20TextureLayer(isOpaque); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 2192 | @Override |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 2193 | public HardwareLayer createHardwareLayer(int width, int height, boolean isOpaque) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 2194 | return new GLES20RenderLayer(width, height, isOpaque); |
| 2195 | } |
| 2196 | |
| 2197 | @Override |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 2198 | void countOverdraw(HardwareCanvas canvas) { |
| 2199 | ((GLES20Canvas) canvas).setCountOverdrawEnabled(true); |
| 2200 | } |
| 2201 | |
| 2202 | @Override |
| 2203 | float getOverdraw(HardwareCanvas canvas) { |
| 2204 | return ((GLES20Canvas) canvas).getOverdraw(); |
| 2205 | } |
| 2206 | |
| 2207 | @Override |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 2208 | public SurfaceTexture createSurfaceTexture(HardwareLayer layer) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 2209 | return ((GLES20TextureLayer) layer).getSurfaceTexture(); |
| 2210 | } |
| 2211 | |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 2212 | @Override |
Jamie Gennis | 2af3524 | 2012-04-05 11:44:30 -0700 | [diff] [blame] | 2213 | void setSurfaceTexture(HardwareLayer layer, SurfaceTexture surfaceTexture) { |
| 2214 | ((GLES20TextureLayer) layer).setSurfaceTexture(surfaceTexture); |
| 2215 | } |
| 2216 | |
| 2217 | @Override |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 2218 | boolean safelyRun(Runnable action) { |
Romain Guy | cc6b7caf | 2013-06-24 19:19:21 -0700 | [diff] [blame] | 2219 | boolean needsContext = !isEnabled() || checkRenderContext() == SURFACE_STATE_ERROR; |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 2220 | |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 2221 | if (needsContext) { |
| 2222 | Gl20RendererEglContext managedContext = |
| 2223 | (Gl20RendererEglContext) sEglContextStorage.get(); |
| 2224 | if (managedContext == null) return false; |
| 2225 | usePbufferSurface(managedContext.getContext()); |
| 2226 | } |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 2227 | |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 2228 | try { |
| 2229 | action.run(); |
| 2230 | } finally { |
Jesse Hall | 0872b37 | 2012-04-02 12:22:51 -0700 | [diff] [blame] | 2231 | if (needsContext) { |
| 2232 | sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, |
| 2233 | EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 2234 | } |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 2235 | } |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 2236 | |
| 2237 | return true; |
| 2238 | } |
| 2239 | |
| 2240 | @Override |
Romain Guy | bd17bd3 | 2012-10-23 19:17:15 -0700 | [diff] [blame] | 2241 | void destroyLayers(final View view) { |
| 2242 | if (view != null) { |
| 2243 | safelyRun(new Runnable() { |
| 2244 | @Override |
| 2245 | public void run() { |
| 2246 | if (mCanvas != null) { |
| 2247 | mCanvas.clearLayerUpdates(); |
| 2248 | } |
| 2249 | destroyHardwareLayer(view); |
| 2250 | GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_LAYERS); |
| 2251 | } |
| 2252 | }); |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | private static void destroyHardwareLayer(View view) { |
| 2257 | view.destroyLayer(true); |
| 2258 | |
| 2259 | if (view instanceof ViewGroup) { |
| 2260 | ViewGroup group = (ViewGroup) view; |
| 2261 | |
| 2262 | int count = group.getChildCount(); |
| 2263 | for (int i = 0; i < count; i++) { |
| 2264 | destroyHardwareLayer(group.getChildAt(i)); |
| 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | @Override |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 2270 | void destroyHardwareResources(final View view) { |
| 2271 | if (view != null) { |
| 2272 | safelyRun(new Runnable() { |
| 2273 | @Override |
| 2274 | public void run() { |
Chet Haase | 6a2d17f | 2012-09-30 12:14:13 -0700 | [diff] [blame] | 2275 | if (mCanvas != null) { |
| 2276 | mCanvas.clearLayerUpdates(); |
| 2277 | } |
Romain Guy | 1ac4765 | 2012-04-11 18:15:20 -0700 | [diff] [blame] | 2278 | destroyResources(view); |
| 2279 | GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_LAYERS); |
| 2280 | } |
| 2281 | }); |
| 2282 | } |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 2283 | } |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 2284 | |
Romain Guy | 31f2c2e | 2011-11-21 10:55:41 -0800 | [diff] [blame] | 2285 | private static void destroyResources(View view) { |
| 2286 | view.destroyHardwareResources(); |
| 2287 | |
| 2288 | if (view instanceof ViewGroup) { |
| 2289 | ViewGroup group = (ViewGroup) view; |
| 2290 | |
| 2291 | int count = group.getChildCount(); |
| 2292 | for (int i = 0; i < count; i++) { |
| 2293 | destroyResources(group.getChildAt(i)); |
| 2294 | } |
| 2295 | } |
| 2296 | } |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 2297 | |
| 2298 | static HardwareRenderer create(boolean translucent) { |
| 2299 | if (GLES20Canvas.isAvailable()) { |
| 2300 | return new Gl20Renderer(translucent); |
| 2301 | } |
| 2302 | return null; |
| 2303 | } |
| 2304 | |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 2305 | static void startTrimMemory(int level) { |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 2306 | if (sEgl == null || sEglConfig == null) return; |
| 2307 | |
Romain Guy | 5d6999e | 2012-03-22 19:15:04 -0700 | [diff] [blame] | 2308 | Gl20RendererEglContext managedContext = |
| 2309 | (Gl20RendererEglContext) sEglContextStorage.get(); |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 2310 | // We do not have OpenGL objects |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 2311 | if (managedContext == null) { |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 2312 | return; |
| 2313 | } else { |
Dianne Hackborn | 717a25d | 2011-11-15 18:59:59 -0800 | [diff] [blame] | 2314 | usePbufferSurface(managedContext.getContext()); |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 2315 | } |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 2316 | |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 2317 | if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) { |
| 2318 | GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_FULL); |
| 2319 | } else if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { |
| 2320 | GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_MODERATE); |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 2321 | } |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 2322 | } |
Jesse Hall | 0872b37 | 2012-04-02 12:22:51 -0700 | [diff] [blame] | 2323 | |
Romain Guy | 19f86e8 | 2012-04-23 15:19:07 -0700 | [diff] [blame] | 2324 | static void endTrimMemory() { |
| 2325 | if (sEgl != null && sEglDisplay != null) { |
| 2326 | sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 2327 | } |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 2328 | } |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 2329 | |
| 2330 | private static void usePbufferSurface(EGLContext eglContext) { |
| 2331 | synchronized (sPbufferLock) { |
| 2332 | // Create a temporary 1x1 pbuffer so we have a context |
| 2333 | // to clear our OpenGL objects |
| 2334 | if (sPbuffer == null) { |
| 2335 | sPbuffer = sEgl.eglCreatePbufferSurface(sEglDisplay, sEglConfig, new int[] { |
| 2336 | EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE |
| 2337 | }); |
| 2338 | } |
| 2339 | } |
| 2340 | sEgl.eglMakeCurrent(sEglDisplay, sPbuffer, sPbuffer, eglContext); |
| 2341 | } |
Romain Guy | e392499 | 2010-06-10 18:51:21 -0700 | [diff] [blame] | 2342 | } |
Romain Guy | 2d61459 | 2010-06-09 18:21:37 -0700 | [diff] [blame] | 2343 | } |