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