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