John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 21 | import android.app.ActivityManager; |
John Reck | b8802b1 | 2014-06-16 15:28:50 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Alan Viverette | 58c42c3 | 2014-07-12 20:33:45 -0700 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 25 | import android.graphics.Point; |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 26 | import android.graphics.Rect; |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.AnimatedVectorDrawable; |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 28 | import android.os.IBinder; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 29 | import android.os.ParcelFileDescriptor; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 30 | import android.os.RemoteException; |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 31 | import android.os.ServiceManager; |
Jaesung Chung | 9a89d83 | 2017-05-16 01:54:17 +0900 | [diff] [blame] | 32 | import android.os.SystemProperties; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 33 | import android.os.Trace; |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 34 | import android.util.Log; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 35 | import android.view.Surface.OutOfResourcesException; |
| 36 | import android.view.View.AttachInfo; |
| 37 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 38 | import com.android.internal.R; |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 39 | import com.android.internal.util.VirtualRefBasePtr; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 40 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 41 | import java.io.File; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 42 | import java.io.FileDescriptor; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 43 | import java.io.PrintWriter; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 44 | import java.lang.annotation.Retention; |
| 45 | import java.lang.annotation.RetentionPolicy; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 48 | * Threaded renderer that proxies the rendering to a render thread. Most calls |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 49 | * are currently synchronous. |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 50 | * |
| 51 | * The UI thread can block on the RenderThread, but RenderThread must never |
| 52 | * block on the UI thread. |
| 53 | * |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 54 | * ThreadedRenderer creates an instance of RenderProxy. RenderProxy in turn creates |
| 55 | * and manages a CanvasContext on the RenderThread. The CanvasContext is fully managed |
| 56 | * by the lifecycle of the RenderProxy. |
| 57 | * |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 58 | * Note that although currently the EGL context & surfaces are created & managed |
| 59 | * by the render thread, the goal is to move that into a shared structure that can |
| 60 | * be managed by both threads. EGLSurface creation & deletion should ideally be |
| 61 | * done on the UI thread and not the RenderThread to avoid stalling the |
| 62 | * RenderThread with surface buffer allocation. |
| 63 | * |
| 64 | * @hide |
| 65 | */ |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 66 | public final class ThreadedRenderer { |
| 67 | private static final String LOG_TAG = "ThreadedRenderer"; |
| 68 | |
| 69 | /** |
| 70 | * Name of the file that holds the shaders cache. |
| 71 | */ |
| 72 | private static final String CACHE_PATH_SHADERS = "com.android.opengl.shaders_cache"; |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 73 | private static final String CACHE_PATH_SKIASHADERS = "com.android.skia.shaders_cache"; |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 76 | * System property used to enable or disable threaded rendering profiling. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 77 | * The default value of this property is assumed to be false. |
| 78 | * |
| 79 | * When profiling is enabled, the adb shell dumpsys gfxinfo command will |
| 80 | * output extra information about the time taken to execute by the last |
| 81 | * frames. |
| 82 | * |
| 83 | * Possible values: |
| 84 | * "true", to enable profiling |
| 85 | * "visual_bars", to enable profiling and visualize the results on screen |
| 86 | * "false", to disable profiling |
| 87 | * |
| 88 | * @see #PROFILE_PROPERTY_VISUALIZE_BARS |
| 89 | * |
| 90 | * @hide |
| 91 | */ |
| 92 | public static final String PROFILE_PROPERTY = "debug.hwui.profile"; |
| 93 | |
| 94 | /** |
| 95 | * Value for {@link #PROFILE_PROPERTY}. When the property is set to this |
| 96 | * value, profiling data will be visualized on screen as a bar chart. |
| 97 | * |
| 98 | * @hide |
| 99 | */ |
| 100 | public static final String PROFILE_PROPERTY_VISUALIZE_BARS = "visual_bars"; |
| 101 | |
| 102 | /** |
| 103 | * System property used to specify the number of frames to be used |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 104 | * when doing threaded rendering profiling. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 105 | * The default value of this property is #PROFILE_MAX_FRAMES. |
| 106 | * |
| 107 | * When profiling is enabled, the adb shell dumpsys gfxinfo command will |
| 108 | * output extra information about the time taken to execute by the last |
| 109 | * frames. |
| 110 | * |
| 111 | * Possible values: |
| 112 | * "60", to set the limit of frames to 60 |
| 113 | */ |
| 114 | static final String PROFILE_MAXFRAMES_PROPERTY = "debug.hwui.profile.maxframes"; |
| 115 | |
| 116 | /** |
| 117 | * System property used to debug EGL configuration choice. |
| 118 | * |
| 119 | * Possible values: |
| 120 | * "choice", print the chosen configuration only |
| 121 | * "all", print all possible configurations |
| 122 | */ |
| 123 | static final String PRINT_CONFIG_PROPERTY = "debug.hwui.print_config"; |
| 124 | |
| 125 | /** |
| 126 | * Turn on to draw dirty regions every other frame. |
| 127 | * |
| 128 | * Possible values: |
| 129 | * "true", to enable dirty regions debugging |
| 130 | * "false", to disable dirty regions debugging |
| 131 | * |
| 132 | * @hide |
| 133 | */ |
| 134 | public static final String DEBUG_DIRTY_REGIONS_PROPERTY = "debug.hwui.show_dirty_regions"; |
| 135 | |
| 136 | /** |
| 137 | * Turn on to flash hardware layers when they update. |
| 138 | * |
| 139 | * Possible values: |
| 140 | * "true", to enable hardware layers updates debugging |
| 141 | * "false", to disable hardware layers updates debugging |
| 142 | * |
| 143 | * @hide |
| 144 | */ |
| 145 | public static final String DEBUG_SHOW_LAYERS_UPDATES_PROPERTY = |
| 146 | "debug.hwui.show_layers_updates"; |
| 147 | |
| 148 | /** |
| 149 | * Controls overdraw debugging. |
| 150 | * |
| 151 | * Possible values: |
| 152 | * "false", to disable overdraw debugging |
| 153 | * "show", to show overdraw areas on screen |
| 154 | * "count", to display an overdraw counter |
| 155 | * |
| 156 | * @hide |
| 157 | */ |
| 158 | public static final String DEBUG_OVERDRAW_PROPERTY = "debug.hwui.overdraw"; |
| 159 | |
| 160 | /** |
| 161 | * Value for {@link #DEBUG_OVERDRAW_PROPERTY}. When the property is set to this |
| 162 | * value, overdraw will be shown on screen by coloring pixels. |
| 163 | * |
| 164 | * @hide |
| 165 | */ |
| 166 | public static final String OVERDRAW_PROPERTY_SHOW = "show"; |
| 167 | |
| 168 | /** |
| 169 | * Turn on to debug non-rectangular clip operations. |
| 170 | * |
| 171 | * Possible values: |
| 172 | * "hide", to disable this debug mode |
| 173 | * "highlight", highlight drawing commands tested against a non-rectangular clip |
| 174 | * "stencil", renders the clip region on screen when set |
| 175 | * |
| 176 | * @hide |
| 177 | */ |
| 178 | public static final String DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY = |
| 179 | "debug.hwui.show_non_rect_clip"; |
| 180 | |
John Reck | 9f51644 | 2017-09-25 10:27:21 -0700 | [diff] [blame] | 181 | /** |
| 182 | * Sets the FPS devisor to lower the FPS. |
| 183 | * |
| 184 | * Sets a positive integer as a divisor. 1 (the default value) menas the full FPS, and 2 |
| 185 | * means half the full FPS. |
| 186 | * |
| 187 | * |
| 188 | * @hide |
| 189 | */ |
| 190 | public static final String DEBUG_FPS_DIVISOR = "debug.hwui.fps_divisor"; |
| 191 | |
Jaesung Chung | 9a89d83 | 2017-05-16 01:54:17 +0900 | [diff] [blame] | 192 | static { |
| 193 | // Try to check OpenGL support early if possible. |
| 194 | isAvailable(); |
| 195 | } |
| 196 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 197 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 198 | * A process can set this flag to false to prevent the use of threaded |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 199 | * rendering. |
| 200 | * |
| 201 | * @hide |
| 202 | */ |
| 203 | public static boolean sRendererDisabled = false; |
| 204 | |
| 205 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 206 | * Further threaded renderer disabling for the system process. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 207 | * |
| 208 | * @hide |
| 209 | */ |
| 210 | public static boolean sSystemRendererDisabled = false; |
| 211 | |
| 212 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 213 | * Invoke this method to disable threaded rendering in the current process. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 214 | * |
| 215 | * @hide |
| 216 | */ |
| 217 | public static void disable(boolean system) { |
| 218 | sRendererDisabled = true; |
| 219 | if (system) { |
| 220 | sSystemRendererDisabled = true; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | public static boolean sTrimForeground = false; |
| 225 | |
| 226 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 227 | * Controls whether or not the renderer should aggressively trim |
| 228 | * memory. Note that this must not be set for any process that uses |
| 229 | * WebView! This should be only used by system_process or similar |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 230 | * that do not go into the background. |
| 231 | */ |
| 232 | public static void enableForegroundTrimming() { |
| 233 | sTrimForeground = true; |
| 234 | } |
| 235 | |
Jaesung Chung | 9a89d83 | 2017-05-16 01:54:17 +0900 | [diff] [blame] | 236 | private static Boolean sSupportsOpenGL; |
Derek Sollenberger | f64c34e | 2016-06-28 16:39:13 -0400 | [diff] [blame] | 237 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 238 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 239 | * Indicates whether threaded rendering is available under any form for |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 240 | * the view hierarchy. |
| 241 | * |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 242 | * @return True if the view hierarchy can potentially be defer rendered, |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 243 | * false otherwise |
| 244 | */ |
| 245 | public static boolean isAvailable() { |
Jaesung Chung | 9a89d83 | 2017-05-16 01:54:17 +0900 | [diff] [blame] | 246 | if (sSupportsOpenGL != null) { |
| 247 | return sSupportsOpenGL.booleanValue(); |
| 248 | } |
| 249 | if (SystemProperties.getInt("ro.kernel.qemu", 0) == 0) { |
| 250 | // Device is not an emulator. |
| 251 | sSupportsOpenGL = true; |
| 252 | return true; |
| 253 | } |
| 254 | int qemu_gles = SystemProperties.getInt("qemu.gles", -1); |
| 255 | if (qemu_gles == -1) { |
| 256 | // In this case, the value of the qemu.gles property is not ready |
| 257 | // because the SurfaceFlinger service may not start at this point. |
| 258 | return false; |
| 259 | } |
| 260 | // In the emulator this property will be set > 0 when OpenGL ES 2.0 is |
| 261 | // enabled, 0 otherwise. On old emulator versions it will be undefined. |
| 262 | sSupportsOpenGL = qemu_gles > 0; |
| 263 | return sSupportsOpenGL.booleanValue(); |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 267 | * Sets the directory to use as a persistent storage for threaded rendering |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 268 | * resources. |
| 269 | * |
| 270 | * @param cacheDir A directory the current process can write to |
| 271 | * |
| 272 | * @hide |
| 273 | */ |
| 274 | public static void setupDiskCache(File cacheDir) { |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 275 | ThreadedRenderer.setupShadersDiskCache( |
| 276 | new File(cacheDir, CACHE_PATH_SHADERS).getAbsolutePath(), |
| 277 | new File(cacheDir, CACHE_PATH_SKIASHADERS).getAbsolutePath()); |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 281 | * Creates a threaded renderer using OpenGL. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 282 | * |
| 283 | * @param translucent True if the surface is translucent, false otherwise |
| 284 | * |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 285 | * @return A threaded renderer backed by OpenGL. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 286 | */ |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 287 | public static ThreadedRenderer create(Context context, boolean translucent, String name) { |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 288 | ThreadedRenderer renderer = null; |
Derek Sollenberger | f64c34e | 2016-06-28 16:39:13 -0400 | [diff] [blame] | 289 | if (isAvailable()) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 290 | renderer = new ThreadedRenderer(context, translucent, name); |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 291 | } |
| 292 | return renderer; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Invoke this method when the system is running out of memory. This |
| 297 | * method will attempt to recover as much memory as possible, based on |
| 298 | * the specified hint. |
| 299 | * |
| 300 | * @param level Hint about the amount of memory that should be trimmed, |
| 301 | * see {@link android.content.ComponentCallbacks} |
| 302 | */ |
| 303 | public static void trimMemory(int level) { |
| 304 | nTrimMemory(level); |
| 305 | } |
| 306 | |
| 307 | public static void overrideProperty(@NonNull String name, @NonNull String value) { |
| 308 | if (name == null || value == null) { |
| 309 | throw new IllegalArgumentException("name and value must be non-null"); |
| 310 | } |
| 311 | nOverrideProperty(name, value); |
| 312 | } |
| 313 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 314 | // Keep in sync with DrawFrameTask.h SYNC_* flags |
| 315 | // Nothing interesting to report |
John Reck | cd028f3 | 2014-06-24 08:44:29 -0700 | [diff] [blame] | 316 | private static final int SYNC_OK = 0; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 317 | // Needs a ViewRoot invalidate |
John Reck | cd028f3 | 2014-06-24 08:44:29 -0700 | [diff] [blame] | 318 | private static final int SYNC_INVALIDATE_REQUIRED = 1 << 0; |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 319 | // Spoiler: the reward is GPU-accelerated drawing, better find that Surface! |
| 320 | private static final int SYNC_LOST_SURFACE_REWARD_IF_FOUND = 1 << 1; |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 321 | // setStopped is true, drawing is false |
| 322 | // TODO: Remove this and SYNC_LOST_SURFACE_REWARD_IF_FOUND? |
| 323 | // This flag isn't really used as there's nothing that we care to do |
| 324 | // in response, so it really just exists to differentiate from LOST_SURFACE |
| 325 | // but possibly both can just be deleted. |
| 326 | private static final int SYNC_CONTEXT_IS_STOPPED = 1 << 2; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 327 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 328 | private static final String[] VISUALIZERS = { |
| 329 | PROFILE_PROPERTY_VISUALIZE_BARS, |
| 330 | }; |
| 331 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 332 | private static final int FLAG_DUMP_FRAMESTATS = 1 << 0; |
| 333 | private static final int FLAG_DUMP_RESET = 1 << 1; |
John Reck | 184264d | 2018-02-12 16:21:21 -0800 | [diff] [blame] | 334 | private static final int FLAG_DUMP_ALL = FLAG_DUMP_FRAMESTATS; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 335 | |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 336 | @IntDef(flag = true, prefix = { "FLAG_DUMP_" }, value = { |
| 337 | FLAG_DUMP_FRAMESTATS, |
| 338 | FLAG_DUMP_RESET |
| 339 | }) |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 340 | @Retention(RetentionPolicy.SOURCE) |
| 341 | public @interface DumpFlags {} |
| 342 | |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 343 | // Size of the rendered content. |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 344 | private int mWidth, mHeight; |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 345 | |
| 346 | // Actual size of the drawing surface. |
| 347 | private int mSurfaceWidth, mSurfaceHeight; |
| 348 | |
| 349 | // Insets between the drawing surface and rendered content. These are |
| 350 | // applied as translation when updating the root render node. |
| 351 | private int mInsetTop, mInsetLeft; |
| 352 | |
Alan Viverette | 57774a8 | 2014-07-15 15:49:55 -0700 | [diff] [blame] | 353 | // Whether the surface has insets. Used to protect opacity. |
| 354 | private boolean mHasInsets; |
| 355 | |
Alan Viverette | 58c42c3 | 2014-07-12 20:33:45 -0700 | [diff] [blame] | 356 | // Light and shadow properties specified by the theme. |
| 357 | private final float mLightY; |
| 358 | private final float mLightZ; |
| 359 | private final float mLightRadius; |
Chris Craik | 058fc64 | 2014-07-23 18:19:28 -0700 | [diff] [blame] | 360 | private final int mAmbientShadowAlpha; |
| 361 | private final int mSpotShadowAlpha; |
Alan Viverette | 58c42c3 | 2014-07-12 20:33:45 -0700 | [diff] [blame] | 362 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 363 | private long mNativeProxy; |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 364 | private boolean mInitialized = false; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 365 | private RenderNode mRootNode; |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 366 | private boolean mRootNodeNeedsUpdate; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 367 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 368 | private boolean mEnabled; |
| 369 | private boolean mRequested = true; |
John Reck | 3e04f09 | 2017-06-02 15:50:09 -0700 | [diff] [blame] | 370 | private boolean mIsOpaque = false; |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 371 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 372 | ThreadedRenderer(Context context, boolean translucent, String name) { |
Alan Viverette | ed6f14a | 2014-08-26 14:53:28 -0700 | [diff] [blame] | 373 | final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Lighting, 0, 0); |
Alan Viverette | 58c42c3 | 2014-07-12 20:33:45 -0700 | [diff] [blame] | 374 | mLightY = a.getDimension(R.styleable.Lighting_lightY, 0); |
| 375 | mLightZ = a.getDimension(R.styleable.Lighting_lightZ, 0); |
| 376 | mLightRadius = a.getDimension(R.styleable.Lighting_lightRadius, 0); |
Alan Viverette | ed6f14a | 2014-08-26 14:53:28 -0700 | [diff] [blame] | 377 | mAmbientShadowAlpha = |
| 378 | (int) (255 * a.getFloat(R.styleable.Lighting_ambientShadowAlpha, 0) + 0.5f); |
| 379 | mSpotShadowAlpha = (int) (255 * a.getFloat(R.styleable.Lighting_spotShadowAlpha, 0) + 0.5f); |
Alan Viverette | 58c42c3 | 2014-07-12 20:33:45 -0700 | [diff] [blame] | 380 | a.recycle(); |
| 381 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 382 | long rootNodePtr = nCreateRootRenderNode(); |
| 383 | mRootNode = RenderNode.adopt(rootNodePtr); |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 384 | mRootNode.setClipToBounds(false); |
John Reck | 3e04f09 | 2017-06-02 15:50:09 -0700 | [diff] [blame] | 385 | mIsOpaque = !translucent; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 386 | mNativeProxy = nCreateProxy(translucent, rootNodePtr); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 387 | nSetName(mNativeProxy, name); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 388 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 389 | ProcessInitializer.sInstance.init(context, mNativeProxy); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 390 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 391 | loadSystemProperties(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 392 | } |
| 393 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 394 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 395 | * Destroys the threaded rendering context. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 396 | */ |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 397 | void destroy() { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 398 | mInitialized = false; |
| 399 | updateEnabledState(null); |
Doris Liu | 350e652 | 2016-02-19 14:20:37 -0800 | [diff] [blame] | 400 | nDestroy(mNativeProxy, mRootNode.mNativeRenderNode); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 401 | } |
| 402 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 403 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 404 | * Indicates whether threaded rendering is currently enabled. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 405 | * |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 406 | * @return True if threaded rendering is in use, false otherwise. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 407 | */ |
| 408 | boolean isEnabled() { |
| 409 | return mEnabled; |
| 410 | } |
| 411 | |
| 412 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 413 | * Indicates whether threaded rendering is currently enabled. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 414 | * |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 415 | * @param enabled True if the threaded renderer is in use, false otherwise. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 416 | */ |
| 417 | void setEnabled(boolean enabled) { |
| 418 | mEnabled = enabled; |
| 419 | } |
| 420 | |
| 421 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 422 | * Indicates whether threaded rendering is currently request but not |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 423 | * necessarily enabled yet. |
| 424 | * |
| 425 | * @return True if requested, false otherwise. |
| 426 | */ |
| 427 | boolean isRequested() { |
| 428 | return mRequested; |
| 429 | } |
| 430 | |
| 431 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 432 | * Indicates whether threaded rendering is currently requested but not |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 433 | * necessarily enabled yet. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 434 | */ |
| 435 | void setRequested(boolean requested) { |
| 436 | mRequested = requested; |
| 437 | } |
| 438 | |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 439 | private void updateEnabledState(Surface surface) { |
| 440 | if (surface == null || !surface.isValid()) { |
| 441 | setEnabled(false); |
| 442 | } else { |
| 443 | setEnabled(mInitialized); |
| 444 | } |
| 445 | } |
| 446 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 447 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 448 | * Initializes the threaded renderer for the specified surface. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 449 | * |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 450 | * @param surface The surface to render |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 451 | * |
| 452 | * @return True if the initialization was successful, false otherwise. |
| 453 | */ |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 454 | boolean initialize(Surface surface) throws OutOfResourcesException { |
Thomas Buhot | 0bcd0cb | 2015-12-04 12:18:03 +0100 | [diff] [blame] | 455 | boolean status = !mInitialized; |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 456 | mInitialized = true; |
| 457 | updateEnabledState(surface); |
Thomas Buhot | 0bcd0cb | 2015-12-04 12:18:03 +0100 | [diff] [blame] | 458 | nInitialize(mNativeProxy, surface); |
Dan Stoza | 5795d64 | 2014-06-20 13:01:36 -0700 | [diff] [blame] | 459 | return status; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 460 | } |
| 461 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 462 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 463 | * Initializes the threaded renderer for the specified surface and setup the |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 464 | * renderer for drawing, if needed. This is invoked when the ViewAncestor has |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 465 | * potentially lost the threaded renderer. The threaded renderer should be |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 466 | * reinitialized and setup when the render {@link #isRequested()} and |
| 467 | * {@link #isEnabled()}. |
| 468 | * |
| 469 | * @param width The width of the drawing surface. |
| 470 | * @param height The height of the drawing surface. |
| 471 | * @param attachInfo Information about the window. |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 472 | * @param surface The surface to render |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 473 | * @param surfaceInsets The drawing surface insets to apply |
| 474 | * |
| 475 | * @return true if the surface was initialized, false otherwise. Returning |
| 476 | * false might mean that the surface was already initialized. |
| 477 | */ |
| 478 | boolean initializeIfNeeded(int width, int height, View.AttachInfo attachInfo, |
| 479 | Surface surface, Rect surfaceInsets) throws OutOfResourcesException { |
| 480 | if (isRequested()) { |
| 481 | // We lost the gl context, so recreate it. |
| 482 | if (!isEnabled()) { |
| 483 | if (initialize(surface)) { |
| 484 | setup(width, height, attachInfo, surfaceInsets); |
| 485 | return true; |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 493 | * Updates the threaded renderer for the specified surface. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 494 | * |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 495 | * @param surface The surface to render |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 496 | */ |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 497 | void updateSurface(Surface surface) throws OutOfResourcesException { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 498 | updateEnabledState(surface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 499 | nUpdateSurface(mNativeProxy, surface); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 500 | } |
| 501 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 502 | /** |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 503 | * Halts any current rendering into the surface. Use this if it is unclear whether |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 504 | * or not the surface used by the ThreadedRenderer will be changing. It |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 505 | * Suspends any rendering into the surface, but will not do any destruction. |
| 506 | * |
| 507 | * Any subsequent draws will override the pause, resuming normal operation. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 508 | */ |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 509 | boolean pauseSurface(Surface surface) { |
| 510 | return nPauseSurface(mNativeProxy, surface); |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 511 | } |
| 512 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 513 | /** |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 514 | * Hard stops or resumes rendering into the surface. This flag is used to |
| 515 | * determine whether or not it is safe to use the given surface *at all* |
| 516 | */ |
| 517 | void setStopped(boolean stopped) { |
| 518 | nSetStopped(mNativeProxy, stopped); |
| 519 | } |
| 520 | |
| 521 | /** |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 522 | * Destroys all hardware rendering resources associated with the specified |
| 523 | * view hierarchy. |
| 524 | * |
| 525 | * @param view The root of the view hierarchy |
| 526 | */ |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 527 | void destroyHardwareResources(View view) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 528 | destroyResources(view); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 529 | nDestroyHardwareResources(mNativeProxy); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | private static void destroyResources(View view) { |
| 533 | view.destroyHardwareResources(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 534 | } |
| 535 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 536 | /** |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 537 | * Detaches the layer's surface texture from the GL context and releases |
| 538 | * the texture id |
| 539 | */ |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 540 | void detachSurfaceTexture(long hardwareLayer) { |
| 541 | nDetachSurfaceTexture(mNativeProxy, hardwareLayer); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 542 | } |
| 543 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 544 | /** |
| 545 | * Sets up the renderer for drawing. |
| 546 | * |
| 547 | * @param width The width of the drawing surface. |
| 548 | * @param height The height of the drawing surface. |
| 549 | * @param attachInfo Information about the window. |
| 550 | * @param surfaceInsets The drawing surface insets to apply |
| 551 | */ |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 552 | void setup(int width, int height, AttachInfo attachInfo, Rect surfaceInsets) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 553 | mWidth = width; |
| 554 | mHeight = height; |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 555 | |
Alan Viverette | 3aa1ffb | 2014-10-30 12:22:08 -0700 | [diff] [blame] | 556 | if (surfaceInsets != null && (surfaceInsets.left != 0 || surfaceInsets.right != 0 |
| 557 | || surfaceInsets.top != 0 || surfaceInsets.bottom != 0)) { |
Alan Viverette | 57774a8 | 2014-07-15 15:49:55 -0700 | [diff] [blame] | 558 | mHasInsets = true; |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 559 | mInsetLeft = surfaceInsets.left; |
| 560 | mInsetTop = surfaceInsets.top; |
| 561 | mSurfaceWidth = width + mInsetLeft + surfaceInsets.right; |
| 562 | mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom; |
Alan Viverette | 57774a8 | 2014-07-15 15:49:55 -0700 | [diff] [blame] | 563 | |
| 564 | // If the surface has insets, it can't be opaque. |
| 565 | setOpaque(false); |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 566 | } else { |
Alan Viverette | 57774a8 | 2014-07-15 15:49:55 -0700 | [diff] [blame] | 567 | mHasInsets = false; |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 568 | mInsetLeft = 0; |
| 569 | mInsetTop = 0; |
| 570 | mSurfaceWidth = width; |
| 571 | mSurfaceHeight = height; |
| 572 | } |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 573 | |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 574 | mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight); |
John Reck | ab1080c | 2016-06-21 16:24:20 -0700 | [diff] [blame] | 575 | nSetup(mNativeProxy, mLightRadius, |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 576 | mAmbientShadowAlpha, mSpotShadowAlpha); |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 577 | |
| 578 | setLightCenter(attachInfo); |
| 579 | } |
| 580 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 581 | /** |
| 582 | * Updates the light position based on the position of the window. |
| 583 | * |
| 584 | * @param attachInfo Information about the window. |
| 585 | */ |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 586 | void setLightCenter(AttachInfo attachInfo) { |
| 587 | // Adjust light position for window offsets. |
| 588 | final Point displaySize = attachInfo.mPoint; |
| 589 | attachInfo.mDisplay.getRealSize(displaySize); |
| 590 | final float lightX = displaySize.x / 2f - attachInfo.mWindowLeft; |
| 591 | final float lightY = mLightY - attachInfo.mWindowTop; |
| 592 | |
| 593 | nSetLightCenter(mNativeProxy, lightX, lightY, mLightZ); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 594 | } |
| 595 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 596 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 597 | * Change the ThreadedRenderer's opacity |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 598 | */ |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 599 | void setOpaque(boolean opaque) { |
John Reck | 3e04f09 | 2017-06-02 15:50:09 -0700 | [diff] [blame] | 600 | mIsOpaque = opaque && !mHasInsets; |
| 601 | nSetOpaque(mNativeProxy, mIsOpaque); |
| 602 | } |
| 603 | |
| 604 | boolean isOpaque() { |
| 605 | return mIsOpaque; |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 606 | } |
| 607 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 608 | /** |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 609 | * Enable/disable wide gamut rendering on this renderer. |
| 610 | */ |
| 611 | void setWideGamut(boolean wideGamut) { |
| 612 | nSetWideGamut(mNativeProxy, wideGamut); |
| 613 | } |
| 614 | |
| 615 | /** |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 616 | * Gets the current width of the surface. This is the width that the surface |
| 617 | * was last set to in a call to {@link #setup(int, int, View.AttachInfo, Rect)}. |
| 618 | * |
| 619 | * @return the current width of the surface |
| 620 | */ |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 621 | int getWidth() { |
| 622 | return mWidth; |
| 623 | } |
| 624 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 625 | /** |
| 626 | * Gets the current height of the surface. This is the height that the surface |
| 627 | * was last set to in a call to {@link #setup(int, int, View.AttachInfo, Rect)}. |
| 628 | * |
| 629 | * @return the current width of the surface |
| 630 | */ |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 631 | int getHeight() { |
| 632 | return mHeight; |
| 633 | } |
| 634 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 635 | /** |
| 636 | * Outputs extra debugging information in the specified file descriptor. |
| 637 | */ |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 638 | void dumpGfxInfo(PrintWriter pw, FileDescriptor fd, String[] args) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 639 | pw.flush(); |
John Reck | 184264d | 2018-02-12 16:21:21 -0800 | [diff] [blame] | 640 | // If there's no arguments, eg 'dumpsys gfxinfo', then dump everything. |
| 641 | // If there's a targetted package, eg 'dumpsys gfxinfo com.android.systemui', then only |
| 642 | // dump the summary information |
| 643 | int flags = (args == null || args.length == 0) ? FLAG_DUMP_ALL : 0; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 644 | for (int i = 0; i < args.length; i++) { |
| 645 | switch (args[i]) { |
| 646 | case "framestats": |
| 647 | flags |= FLAG_DUMP_FRAMESTATS; |
| 648 | break; |
| 649 | case "reset": |
| 650 | flags |= FLAG_DUMP_RESET; |
| 651 | break; |
John Reck | 184264d | 2018-02-12 16:21:21 -0800 | [diff] [blame] | 652 | case "-a": // magic option passed when dumping a bugreport. |
| 653 | flags = FLAG_DUMP_ALL; |
| 654 | break; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 655 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 656 | } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 657 | nDumpProfileInfo(mNativeProxy, fd, flags); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 658 | } |
| 659 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 660 | /** |
| 661 | * Loads system properties used by the renderer. This method is invoked |
| 662 | * whenever system properties are modified. Implementations can use this |
| 663 | * to trigger live updates of the renderer based on properties. |
| 664 | * |
| 665 | * @return True if a property has changed. |
| 666 | */ |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 667 | boolean loadSystemProperties() { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 668 | boolean changed = nLoadSystemProperties(mNativeProxy); |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 669 | if (changed) { |
| 670 | invalidateRoot(); |
| 671 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 672 | return changed; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 673 | } |
| 674 | |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 675 | private void updateViewTreeDisplayList(View view) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 676 | view.mPrivateFlags |= View.PFLAG_DRAWN; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 677 | view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED) |
| 678 | == View.PFLAG_INVALIDATED; |
| 679 | view.mPrivateFlags &= ~View.PFLAG_INVALIDATED; |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 680 | view.updateDisplayListIfDirty(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 681 | view.mRecreateDisplayList = false; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 684 | private void updateRootDisplayList(View view, DrawCallbacks callbacks) { |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 685 | Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Record View#draw()"); |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 686 | updateViewTreeDisplayList(view); |
| 687 | |
| 688 | if (mRootNodeNeedsUpdate || !mRootNode.isValid()) { |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 689 | DisplayListCanvas canvas = mRootNode.start(mSurfaceWidth, mSurfaceHeight); |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 690 | try { |
Alan Viverette | dbed893 | 2014-08-06 17:54:52 -0700 | [diff] [blame] | 691 | final int saveCount = canvas.save(); |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 692 | canvas.translate(mInsetLeft, mInsetTop); |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 693 | callbacks.onPreDraw(canvas); |
Chris Craik | abedca3 | 2014-08-28 15:03:55 -0700 | [diff] [blame] | 694 | |
| 695 | canvas.insertReorderBarrier(); |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 696 | canvas.drawRenderNode(view.updateDisplayListIfDirty()); |
Chris Craik | abedca3 | 2014-08-28 15:03:55 -0700 | [diff] [blame] | 697 | canvas.insertInorderBarrier(); |
| 698 | |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 699 | callbacks.onPostDraw(canvas); |
Alan Viverette | dbed893 | 2014-08-06 17:54:52 -0700 | [diff] [blame] | 700 | canvas.restoreToCount(saveCount); |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 701 | mRootNodeNeedsUpdate = false; |
| 702 | } finally { |
| 703 | mRootNode.end(canvas); |
| 704 | } |
| 705 | } |
| 706 | Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 707 | } |
| 708 | |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 709 | /** |
| 710 | * Adds a rendernode to the renderer which can be drawn and changed asynchronously to the |
| 711 | * rendernode of the UI thread. |
| 712 | * @param node The node to add. |
| 713 | * @param placeFront If true, the render node will be placed in front of the content node, |
| 714 | * otherwise behind the content node. |
| 715 | */ |
| 716 | public void addRenderNode(RenderNode node, boolean placeFront) { |
| 717 | nAddRenderNode(mNativeProxy, node.mNativeRenderNode, placeFront); |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * Only especially added render nodes can be removed. |
| 722 | * @param node The node which was added via addRenderNode which should get removed again. |
| 723 | */ |
| 724 | public void removeRenderNode(RenderNode node) { |
| 725 | nRemoveRenderNode(mNativeProxy, node.mNativeRenderNode); |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Draws a particular render node. If the node is not the content node, only the additional |
| 730 | * nodes will get drawn and the content remains untouched. |
| 731 | * @param node The node to be drawn. |
| 732 | */ |
| 733 | public void drawRenderNode(RenderNode node) { |
| 734 | nDrawRenderNode(mNativeProxy, node.mNativeRenderNode); |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * To avoid unnecessary overdrawing of the main content all additionally passed render nodes |
| 739 | * will be prevented to overdraw this area. It will be synchronized with the draw call. |
| 740 | * This should be updated in the content view's draw call. |
| 741 | * @param left The left side of the protected bounds. |
| 742 | * @param top The top side of the protected bounds. |
| 743 | * @param right The right side of the protected bounds. |
| 744 | * @param bottom The bottom side of the protected bounds. |
| 745 | */ |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 746 | public void setContentDrawBounds(int left, int top, int right, int bottom) { |
Jorim Jaggi | e85ce99 | 2016-05-09 19:05:22 -0700 | [diff] [blame] | 747 | nSetContentDrawBounds(mNativeProxy, left, top, right, bottom); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 748 | } |
| 749 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 750 | /** |
| 751 | * Interface used to receive callbacks whenever a view is drawn by |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 752 | * a threaded renderer instance. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 753 | */ |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 754 | interface DrawCallbacks { |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 755 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 756 | * Invoked before a view is drawn by a threaded renderer. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 757 | * This method can be used to apply transformations to the |
| 758 | * canvas but no drawing command should be issued. |
| 759 | * |
| 760 | * @param canvas The Canvas used to render the view. |
| 761 | */ |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 762 | void onPreDraw(DisplayListCanvas canvas); |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 763 | |
| 764 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 765 | * Invoked after a view is drawn by a threaded renderer. |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 766 | * It is safe to invoke drawing commands from this method. |
| 767 | * |
| 768 | * @param canvas The Canvas used to render the view. |
| 769 | */ |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 770 | void onPostDraw(DisplayListCanvas canvas); |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /** |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 774 | * Indicates that the content drawn by DrawCallbacks needs to |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 775 | * be updated, which will be done by the next call to draw() |
| 776 | */ |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 777 | void invalidateRoot() { |
| 778 | mRootNodeNeedsUpdate = true; |
| 779 | } |
| 780 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 781 | /** |
| 782 | * Draws the specified view. |
| 783 | * |
| 784 | * @param view The view to draw. |
| 785 | * @param attachInfo AttachInfo tied to the specified view. |
| 786 | * @param callbacks Callbacks invoked when drawing happens. |
| 787 | */ |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 788 | void draw(View view, AttachInfo attachInfo, DrawCallbacks callbacks) { |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 789 | attachInfo.mIgnoreDirtyState = true; |
John Reck | bc0cc02 | 2014-04-11 16:08:14 -0700 | [diff] [blame] | 790 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 791 | final Choreographer choreographer = attachInfo.mViewRootImpl.mChoreographer; |
| 792 | choreographer.mFrameInfo.markDrawStart(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 793 | |
John Reck | 61375a8 | 2014-09-18 19:27:48 +0000 | [diff] [blame] | 794 | updateRootDisplayList(view, callbacks); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 795 | |
John Reck | 6313b92 | 2014-04-16 18:59:21 -0700 | [diff] [blame] | 796 | attachInfo.mIgnoreDirtyState = false; |
| 797 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 798 | // register animating rendernodes which started animating prior to renderer |
| 799 | // creation, which is typical for animators started prior to first draw |
| 800 | if (attachInfo.mPendingAnimatingRenderNodes != null) { |
| 801 | final int count = attachInfo.mPendingAnimatingRenderNodes.size(); |
| 802 | for (int i = 0; i < count; i++) { |
| 803 | registerAnimatingRenderNode( |
| 804 | attachInfo.mPendingAnimatingRenderNodes.get(i)); |
| 805 | } |
| 806 | attachInfo.mPendingAnimatingRenderNodes.clear(); |
| 807 | // We don't need this anymore as subsequent calls to |
| 808 | // ViewRootImpl#attachRenderNodeAnimator will go directly to us. |
| 809 | attachInfo.mPendingAnimatingRenderNodes = null; |
| 810 | } |
| 811 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 812 | final long[] frameInfo = choreographer.mFrameInfo.mFrameInfo; |
John Reck | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 813 | int syncResult = nSyncAndDrawFrame(mNativeProxy, frameInfo, frameInfo.length); |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 814 | if ((syncResult & SYNC_LOST_SURFACE_REWARD_IF_FOUND) != 0) { |
| 815 | setEnabled(false); |
John Reck | b13de07 | 2014-11-19 16:33:47 -0800 | [diff] [blame] | 816 | attachInfo.mViewRootImpl.mSurface.release(); |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 817 | // Invalidate since we failed to draw. This should fetch a Surface |
| 818 | // if it is still needed or do nothing if we are no longer drawing |
| 819 | attachInfo.mViewRootImpl.invalidate(); |
| 820 | } |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 821 | if ((syncResult & SYNC_INVALIDATE_REQUIRED) != 0) { |
| 822 | attachInfo.mViewRootImpl.invalidate(); |
| 823 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 824 | } |
| 825 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 826 | static void invokeFunctor(long functor, boolean waitForCompletion) { |
| 827 | nInvokeFunctor(functor, waitForCompletion); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 828 | } |
| 829 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 830 | /** |
| 831 | * Creates a new hardware layer. A hardware layer built by calling this |
| 832 | * method will be treated as a texture layer, instead of as a render target. |
| 833 | * |
| 834 | * @return A hardware layer |
| 835 | */ |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 836 | HardwareLayer createTextureLayer() { |
| 837 | long layer = nCreateTextureLayer(mNativeProxy); |
| 838 | return HardwareLayer.adoptTextureLayer(this, layer); |
| 839 | } |
| 840 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 841 | |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 842 | void buildLayer(RenderNode node) { |
| 843 | nBuildLayer(mNativeProxy, node.getNativeDisplayList()); |
| 844 | } |
| 845 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 846 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 847 | boolean copyLayerInto(final HardwareLayer layer, final Bitmap bitmap) { |
| 848 | return nCopyLayerInto(mNativeProxy, |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 849 | layer.getDeferredLayerUpdater(), bitmap); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 850 | } |
| 851 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 852 | /** |
| 853 | * Indicates that the specified hardware layer needs to be updated |
| 854 | * as soon as possible. |
| 855 | * |
| 856 | * @param layer The hardware layer that needs an update |
| 857 | */ |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 858 | void pushLayerUpdate(HardwareLayer layer) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 859 | nPushLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater()); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 860 | } |
| 861 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 862 | /** |
| 863 | * Tells the HardwareRenderer that the layer is destroyed. The renderer |
| 864 | * should remove the layer from any update queues. |
| 865 | */ |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 866 | void onLayerDestroyed(HardwareLayer layer) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 867 | nCancelLayerUpdate(mNativeProxy, layer.getDeferredLayerUpdater()); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 868 | } |
| 869 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 870 | /** |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 871 | * Blocks until all previously queued work has completed. |
| 872 | */ |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 873 | void fence() { |
| 874 | nFence(mNativeProxy); |
| 875 | } |
| 876 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 877 | /** |
| 878 | * Prevents any further drawing until draw() is called. This is a signal |
| 879 | * that the contents of the RenderNode tree are no longer safe to play back. |
| 880 | * In practice this usually means that there are Functor pointers in the |
| 881 | * display list that are no longer valid. |
| 882 | */ |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 883 | void stopDrawing() { |
| 884 | nStopDrawing(mNativeProxy); |
| 885 | } |
| 886 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 887 | /** |
| 888 | * Called by {@link ViewRootImpl} when a new performTraverals is scheduled. |
| 889 | */ |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 890 | public void notifyFramePending() { |
| 891 | nNotifyFramePending(mNativeProxy); |
| 892 | } |
| 893 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 894 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 895 | void registerAnimatingRenderNode(RenderNode animator) { |
| 896 | nRegisterAnimatingRenderNode(mRootNode.mNativeRenderNode, animator.mNativeRenderNode); |
| 897 | } |
| 898 | |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 899 | void registerVectorDrawableAnimator( |
| 900 | AnimatedVectorDrawable.VectorDrawableAnimatorRT animator) { |
| 901 | nRegisterVectorDrawableAnimator(mRootNode.mNativeRenderNode, |
| 902 | animator.getAnimatorNativePtr()); |
| 903 | } |
| 904 | |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 905 | public void serializeDisplayListTree() { |
| 906 | nSerializeDisplayListTree(mNativeProxy); |
| 907 | } |
| 908 | |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 909 | public static int copySurfaceInto(Surface surface, Rect srcRect, Bitmap bitmap) { |
| 910 | if (srcRect == null) { |
| 911 | // Empty rect means entire surface |
| 912 | return nCopySurfaceInto(surface, 0, 0, 0, 0, bitmap); |
| 913 | } else { |
| 914 | return nCopySurfaceInto(surface, srcRect.left, srcRect.top, |
| 915 | srcRect.right, srcRect.bottom, bitmap); |
| 916 | } |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 917 | } |
| 918 | |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 919 | /** |
| 920 | * Creates a {@link android.graphics.Bitmap.Config#HARDWARE} bitmap from the given |
| 921 | * RenderNode. Note that the RenderNode should be created as a root node (so x/y of 0,0), and |
| 922 | * not the RenderNode from a View. |
| 923 | **/ |
| 924 | public static Bitmap createHardwareBitmap(RenderNode node, int width, int height) { |
| 925 | return nCreateHardwareBitmap(node.getNativeDisplayList(), width, height); |
| 926 | } |
| 927 | |
John Reck | 938e884 | 2017-08-24 13:41:59 -0700 | [diff] [blame] | 928 | /** |
| 929 | * Sets whether or not high contrast text rendering is enabled. The setting is global |
| 930 | * but only affects content rendered after the change is made. |
| 931 | */ |
| 932 | public static void setHighContrastText(boolean highContrastText) { |
| 933 | nSetHighContrastText(highContrastText); |
| 934 | } |
| 935 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 936 | @Override |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 937 | protected void finalize() throws Throwable { |
| 938 | try { |
| 939 | nDeleteProxy(mNativeProxy); |
John Reck | 0ed751d | 2014-04-08 14:10:17 -0700 | [diff] [blame] | 940 | mNativeProxy = 0; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 941 | } finally { |
| 942 | super.finalize(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 946 | private static class ProcessInitializer { |
| 947 | static ProcessInitializer sInstance = new ProcessInitializer(); |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 948 | |
| 949 | private boolean mInitialized = false; |
| 950 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 951 | private Context mAppContext; |
| 952 | private IGraphicsStats mGraphicsStatsService; |
| 953 | private IGraphicsStatsCallback mGraphicsStatsCallback = new IGraphicsStatsCallback.Stub() { |
| 954 | @Override |
| 955 | public void onRotateGraphicsStatsBuffer() throws RemoteException { |
| 956 | rotateBuffer(); |
| 957 | } |
| 958 | }; |
| 959 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 960 | private ProcessInitializer() {} |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 961 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 962 | synchronized void init(Context context, long renderProxy) { |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 963 | if (mInitialized) return; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 964 | mInitialized = true; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 965 | mAppContext = context.getApplicationContext(); |
John Reck | 9f51644 | 2017-09-25 10:27:21 -0700 | [diff] [blame] | 966 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 967 | initSched(renderProxy); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 968 | initGraphicsStats(); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 971 | private void initSched(long renderProxy) { |
Tim Murray | 33eb07f | 2016-06-10 10:03:20 -0700 | [diff] [blame] | 972 | try { |
| 973 | int tid = nGetRenderThreadTid(renderProxy); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 974 | ActivityManager.getService().setRenderThread(tid); |
Tim Murray | 33eb07f | 2016-06-10 10:03:20 -0700 | [diff] [blame] | 975 | } catch (Throwable t) { |
| 976 | Log.w(LOG_TAG, "Failed to set scheduler for RenderThread", t); |
| 977 | } |
| 978 | } |
| 979 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 980 | private void initGraphicsStats() { |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 981 | try { |
John Reck | 828698b | 2015-06-30 12:56:03 -0700 | [diff] [blame] | 982 | IBinder binder = ServiceManager.getService("graphicsstats"); |
| 983 | if (binder == null) return; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 984 | mGraphicsStatsService = IGraphicsStats.Stub.asInterface(binder); |
| 985 | requestBuffer(); |
| 986 | } catch (Throwable t) { |
| 987 | Log.w(LOG_TAG, "Could not acquire gfx stats buffer", t); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | private void rotateBuffer() { |
| 992 | nRotateProcessStatsBuffer(); |
| 993 | requestBuffer(); |
| 994 | } |
| 995 | |
| 996 | private void requestBuffer() { |
| 997 | try { |
| 998 | final String pkg = mAppContext.getApplicationInfo().packageName; |
| 999 | ParcelFileDescriptor pfd = mGraphicsStatsService |
| 1000 | .requestBufferForProcess(pkg, mGraphicsStatsCallback); |
| 1001 | nSetProcessStatsBuffer(pfd.getFd()); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 1002 | pfd.close(); |
John Reck | 828698b | 2015-06-30 12:56:03 -0700 | [diff] [blame] | 1003 | } catch (Throwable t) { |
| 1004 | Log.w(LOG_TAG, "Could not acquire gfx stats buffer", t); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 1005 | } |
| 1006 | } |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1009 | void addFrameMetricsObserver(FrameMetricsObserver observer) { |
| 1010 | long nativeObserver = nAddFrameMetricsObserver(mNativeProxy, observer); |
| 1011 | observer.mNative = new VirtualRefBasePtr(nativeObserver); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1014 | void removeFrameMetricsObserver(FrameMetricsObserver observer) { |
| 1015 | nRemoveFrameMetricsObserver(mNativeProxy, observer.mNative.get()); |
| 1016 | observer.mNative = null; |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
John Reck | 9f51644 | 2017-09-25 10:27:21 -0700 | [diff] [blame] | 1019 | /** b/68769804: For low FPS experiments. */ |
| 1020 | public static void setFPSDivisor(int divisor) { |
Makoto Onuki | b708cbe | 2018-01-09 12:35:06 -0800 | [diff] [blame] | 1021 | nHackySetRTAnimationsEnabled(divisor <= 1); |
John Reck | 9f51644 | 2017-09-25 10:27:21 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
John Reck | a896306 | 2017-06-14 10:47:50 -0700 | [diff] [blame] | 1024 | /** Not actually public - internal use only. This doc to make lint happy */ |
| 1025 | public static native void disableVsync(); |
| 1026 | |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 1027 | static native void setupShadersDiskCache(String cacheFile, String skiaCacheFile); |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 1028 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 1029 | private static native void nRotateProcessStatsBuffer(); |
| 1030 | private static native void nSetProcessStatsBuffer(int fd); |
Tim Murray | 33eb07f | 2016-06-10 10:03:20 -0700 | [diff] [blame] | 1031 | private static native int nGetRenderThreadTid(long nativeProxy); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 1032 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 1033 | private static native long nCreateRootRenderNode(); |
| 1034 | private static native long nCreateProxy(boolean translucent, long rootRenderNode); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 1035 | private static native void nDeleteProxy(long nativeProxy); |
| 1036 | |
John Reck | e4280ba | 2014-05-05 16:39:37 -0700 | [diff] [blame] | 1037 | private static native boolean nLoadSystemProperties(long nativeProxy); |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 1038 | private static native void nSetName(long nativeProxy, String name); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 1039 | |
Thomas Buhot | 0bcd0cb | 2015-12-04 12:18:03 +0100 | [diff] [blame] | 1040 | private static native void nInitialize(long nativeProxy, Surface window); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 1041 | private static native void nUpdateSurface(long nativeProxy, Surface window); |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 1042 | private static native boolean nPauseSurface(long nativeProxy, Surface window); |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 1043 | private static native void nSetStopped(long nativeProxy, boolean stopped); |
John Reck | ab1080c | 2016-06-21 16:24:20 -0700 | [diff] [blame] | 1044 | private static native void nSetup(long nativeProxy, |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 1045 | float lightRadius, int ambientShadowAlpha, int spotShadowAlpha); |
| 1046 | private static native void nSetLightCenter(long nativeProxy, |
| 1047 | float lightX, float lightY, float lightZ); |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 1048 | private static native void nSetOpaque(long nativeProxy, boolean opaque); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 1049 | private static native void nSetWideGamut(long nativeProxy, boolean wideGamut); |
John Reck | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 1050 | private static native int nSyncAndDrawFrame(long nativeProxy, long[] frameInfo, int size); |
Doris Liu | 350e652 | 2016-02-19 14:20:37 -0800 | [diff] [blame] | 1051 | private static native void nDestroy(long nativeProxy, long rootRenderNode); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 1052 | private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode); |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 1053 | private static native void nRegisterVectorDrawableAnimator(long rootRenderNode, long animator); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 1054 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 1055 | private static native void nInvokeFunctor(long functor, boolean waitForCompletion); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 1056 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 1057 | private static native long nCreateTextureLayer(long nativeProxy); |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 1058 | private static native void nBuildLayer(long nativeProxy, long node); |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 1059 | private static native boolean nCopyLayerInto(long nativeProxy, long layer, Bitmap bitmap); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 1060 | private static native void nPushLayerUpdate(long nativeProxy, long layer); |
| 1061 | private static native void nCancelLayerUpdate(long nativeProxy, long layer); |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 1062 | private static native void nDetachSurfaceTexture(long nativeProxy, long layer); |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 1063 | |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 1064 | private static native void nDestroyHardwareResources(long nativeProxy); |
| 1065 | private static native void nTrimMemory(int level); |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 1066 | private static native void nOverrideProperty(String name, String value); |
John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 1067 | |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 1068 | private static native void nFence(long nativeProxy); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 1069 | private static native void nStopDrawing(long nativeProxy); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 1070 | private static native void nNotifyFramePending(long nativeProxy); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 1071 | |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 1072 | private static native void nSerializeDisplayListTree(long nativeProxy); |
| 1073 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 1074 | private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd, |
| 1075 | @DumpFlags int dumpFlags); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 1076 | |
| 1077 | private static native void nAddRenderNode(long nativeProxy, long rootRenderNode, |
| 1078 | boolean placeFront); |
| 1079 | private static native void nRemoveRenderNode(long nativeProxy, long rootRenderNode); |
| 1080 | private static native void nDrawRenderNode(long nativeProxy, long rootRenderNode); |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 1081 | private static native void nSetContentDrawBounds(long nativeProxy, int left, |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 1082 | int top, int right, int bottom); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1083 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1084 | private static native long nAddFrameMetricsObserver(long nativeProxy, FrameMetricsObserver observer); |
| 1085 | private static native void nRemoveFrameMetricsObserver(long nativeProxy, long nativeObserver); |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 1086 | |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 1087 | private static native int nCopySurfaceInto(Surface surface, |
| 1088 | int srcLeft, int srcTop, int srcRight, int srcBottom, Bitmap bitmap); |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 1089 | |
| 1090 | private static native Bitmap nCreateHardwareBitmap(long renderNode, int width, int height); |
John Reck | 938e884 | 2017-08-24 13:41:59 -0700 | [diff] [blame] | 1091 | private static native void nSetHighContrastText(boolean enabled); |
John Reck | 9f51644 | 2017-09-25 10:27:21 -0700 | [diff] [blame] | 1092 | // For temporary experimentation b/66945974 |
| 1093 | private static native void nHackySetRTAnimationsEnabled(boolean enabled); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1094 | } |