blob: 352ab837e1ac22f6620ab24cdef802647e7c4e97 [file] [log] [blame]
Romain Guy2d614592010-06-09 18:21:37 -07001/*
Romain Guy735738c2012-12-03 12:34:51 -08002 * Copyright (C) 2013 The Android Open Source Project
Romain Guy2d614592010-06-09 18:21:37 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy2d614592010-06-09 18:21:37 -070017package android.view;
18
Romain Guy7d7b5492011-01-24 16:33:45 -080019import android.graphics.Paint;
20import android.graphics.Rect;
Romain Guyaa6c24c2011-04-28 18:40:04 -070021import android.graphics.SurfaceTexture;
Romain Guy98e4a522013-01-07 10:58:34 -080022import android.util.DisplayMetrics;
Igor Murashkina86ab6402013-08-30 12:58:36 -070023import android.view.Surface.OutOfResourcesException;
24
Romain Guya9582652011-11-10 14:20:10 -080025import java.io.File;
Romain Guya676ad72012-02-13 17:47:10 -080026import java.io.PrintWriter;
Romain Guy484c7192011-07-25 11:56:33 -070027
Romain Guy2d614592010-06-09 18:21:37 -070028/**
Romain Guy8d4aeb72013-02-12 16:08:55 -080029 * Interface for rendering a view hierarchy using hardware acceleration.
Romain Guy52036b12013-02-14 18:03:37 -080030 *
Romain Guy2d614592010-06-09 18:21:37 -070031 * @hide
32 */
Romain Guy61c8c9c2010-08-09 20:48:09 -070033public abstract class HardwareRenderer {
Romain Guy4f6aff32011-01-12 16:21:41 -080034 static final String LOG_TAG = "HardwareRenderer";
Romain Guyfb8b7632010-08-23 21:05:08 -070035
Romain Guy52339202010-09-03 16:04:46 -070036 /**
Romain Guya9582652011-11-10 14:20:10 -080037 * Name of the file that holds the shaders cache.
38 */
39 private static final String CACHE_PATH_SHADERS = "com.android.opengl.shaders_cache";
40
41 /**
Romain Guy069ea0e2011-02-08 12:24:52 -080042 * System property used to enable or disable dirty regions invalidation.
43 * This property is only queried if {@link #RENDER_DIRTY_REGIONS} is true.
44 * The default value of this property is assumed to be true.
Igor Murashkina86ab6402013-08-30 12:58:36 -070045 *
Romain Guy069ea0e2011-02-08 12:24:52 -080046 * Possible values:
47 * "true", to enable partial invalidates
48 * "false", to disable partial invalidates
49 */
Romain Guy4b8c4f82012-04-27 15:48:35 -070050 static final String RENDER_DIRTY_REGIONS_PROPERTY = "debug.hwui.render_dirty_regions";
Romain Guy069ea0e2011-02-08 12:24:52 -080051
52 /**
Romain Guya676ad72012-02-13 17:47:10 -080053 * System property used to enable or disable hardware rendering profiling.
54 * The default value of this property is assumed to be false.
Chet Haase09280602012-04-03 16:15:34 -070055 *
Romain Guya676ad72012-02-13 17:47:10 -080056 * When profiling is enabled, the adb shell dumpsys gfxinfo command will
57 * output extra information about the time taken to execute by the last
58 * frames.
59 *
60 * Possible values:
61 * "true", to enable profiling
Romain Guy48ef4a92013-01-10 18:38:46 -080062 * "visual_bars", to enable profiling and visualize the results on screen
63 * "visual_lines", to enable profiling and visualize the results on screen
Romain Guya676ad72012-02-13 17:47:10 -080064 * "false", to disable profiling
Romain Guy672433d2013-01-04 19:05:13 -080065 *
Romain Guy48ef4a92013-01-10 18:38:46 -080066 * @see #PROFILE_PROPERTY_VISUALIZE_BARS
67 * @see #PROFILE_PROPERTY_VISUALIZE_LINES
Romain Guya4fef022013-01-07 11:18:38 -080068 *
Romain Guy4b8c4f82012-04-27 15:48:35 -070069 * @hide
Romain Guya676ad72012-02-13 17:47:10 -080070 */
Romain Guy4b8c4f82012-04-27 15:48:35 -070071 public static final String PROFILE_PROPERTY = "debug.hwui.profile";
Romain Guya676ad72012-02-13 17:47:10 -080072
73 /**
Romain Guya4fef022013-01-07 11:18:38 -080074 * Value for {@link #PROFILE_PROPERTY}. When the property is set to this
Romain Guy48ef4a92013-01-10 18:38:46 -080075 * value, profiling data will be visualized on screen as a bar chart.
Romain Guy672433d2013-01-04 19:05:13 -080076 *
77 * @hide
78 */
Romain Guy48ef4a92013-01-10 18:38:46 -080079 public static final String PROFILE_PROPERTY_VISUALIZE_BARS = "visual_bars";
80
81 /**
82 * Value for {@link #PROFILE_PROPERTY}. When the property is set to this
83 * value, profiling data will be visualized on screen as a line chart.
84 *
85 * @hide
86 */
87 public static final String PROFILE_PROPERTY_VISUALIZE_LINES = "visual_lines";
Romain Guy672433d2013-01-04 19:05:13 -080088
89 /**
Chet Haase09280602012-04-03 16:15:34 -070090 * System property used to specify the number of frames to be used
91 * when doing hardware rendering profiling.
92 * The default value of this property is #PROFILE_MAX_FRAMES.
93 *
94 * When profiling is enabled, the adb shell dumpsys gfxinfo command will
95 * output extra information about the time taken to execute by the last
96 * frames.
97 *
98 * Possible values:
99 * "60", to set the limit of frames to 60
100 */
Romain Guy4b8c4f82012-04-27 15:48:35 -0700101 static final String PROFILE_MAXFRAMES_PROPERTY = "debug.hwui.profile.maxframes";
Chet Haase09280602012-04-03 16:15:34 -0700102
103 /**
Romain Guy484c7192011-07-25 11:56:33 -0700104 * System property used to debug EGL configuration choice.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700105 *
Romain Guy484c7192011-07-25 11:56:33 -0700106 * Possible values:
Romain Guy484c7192011-07-25 11:56:33 -0700107 * "choice", print the chosen configuration only
108 * "all", print all possible configurations
109 */
Romain Guy4b8c4f82012-04-27 15:48:35 -0700110 static final String PRINT_CONFIG_PROPERTY = "debug.hwui.print_config";
Romain Guy484c7192011-07-25 11:56:33 -0700111
112 /**
Romain Guy7d7b5492011-01-24 16:33:45 -0800113 * Turn on to draw dirty regions every other frame.
Romain Guyb04f7e92012-02-15 12:36:54 -0800114 *
115 * Possible values:
116 * "true", to enable dirty regions debugging
117 * "false", to disable dirty regions debugging
Igor Murashkina86ab6402013-08-30 12:58:36 -0700118 *
Romain Guy4b8c4f82012-04-27 15:48:35 -0700119 * @hide
Romain Guy7d7b5492011-01-24 16:33:45 -0800120 */
Romain Guy4b8c4f82012-04-27 15:48:35 -0700121 public static final String DEBUG_DIRTY_REGIONS_PROPERTY = "debug.hwui.show_dirty_regions";
Romain Guy4ff0cf42012-08-06 14:51:10 -0700122
123 /**
124 * Turn on to flash hardware layers when they update.
125 *
126 * Possible values:
127 * "true", to enable hardware layers updates debugging
128 * "false", to disable hardware layers updates debugging
129 *
130 * @hide
131 */
132 public static final String DEBUG_SHOW_LAYERS_UPDATES_PROPERTY =
133 "debug.hwui.show_layers_updates";
134
Romain Guy7d7b5492011-01-24 16:33:45 -0800135 /**
Romain Guy78dd96d2013-05-03 14:24:16 -0700136 * Controls overdraw debugging.
Romain Guy7c450aa2012-09-21 19:15:00 -0700137 *
138 * Possible values:
Romain Guy7c450aa2012-09-21 19:15:00 -0700139 * "false", to disable overdraw debugging
Romain Guy78dd96d2013-05-03 14:24:16 -0700140 * "show", to show overdraw areas on screen
141 * "count", to display an overdraw counter
Romain Guy7c450aa2012-09-21 19:15:00 -0700142 *
143 * @hide
144 */
Romain Guy78dd96d2013-05-03 14:24:16 -0700145 public static final String DEBUG_OVERDRAW_PROPERTY = "debug.hwui.overdraw";
146
147 /**
148 * Value for {@link #DEBUG_OVERDRAW_PROPERTY}. When the property is set to this
149 * value, overdraw will be shown on screen by coloring pixels.
150 *
151 * @hide
152 */
153 public static final String OVERDRAW_PROPERTY_SHOW = "show";
154
155 /**
Romain Guy08bca882013-02-25 16:21:30 -0800156 * Turn on to debug non-rectangular clip operations.
157 *
158 * Possible values:
159 * "hide", to disable this debug mode
160 * "highlight", highlight drawing commands tested against a non-rectangular clip
161 * "stencil", renders the clip region on screen when set
162 *
163 * @hide
164 */
165 public static final String DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY =
166 "debug.hwui.show_non_rect_clip";
167
168 /**
Romain Guy52339202010-09-03 16:04:46 -0700169 * A process can set this flag to false to prevent the use of hardware
170 * rendering.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700171 *
Romain Guy52339202010-09-03 16:04:46 -0700172 * @hide
173 */
174 public static boolean sRendererDisabled = false;
175
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700176 /**
177 * Further hardware renderer disabling for the system process.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700178 *
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700179 * @hide
180 */
181 public static boolean sSystemRendererDisabled = false;
182
John Reckcec24ae2013-11-05 13:27:50 -0800183 /** @hide */
184 public static boolean sUseRenderThread = false;
185
Romain Guy2d614592010-06-09 18:21:37 -0700186 private boolean mEnabled;
187 private boolean mRequested = true;
188
189 /**
Romain Guy67f27952010-12-07 20:09:23 -0800190 * Invoke this method to disable hardware rendering in the current process.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700191 *
Romain Guy52339202010-09-03 16:04:46 -0700192 * @hide
193 */
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700194 public static void disable(boolean system) {
Romain Guy52339202010-09-03 16:04:46 -0700195 sRendererDisabled = true;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700196 if (system) {
197 sSystemRendererDisabled = true;
198 }
Romain Guy52339202010-09-03 16:04:46 -0700199 }
200
201 /**
Romain Guy61c8c9c2010-08-09 20:48:09 -0700202 * Indicates whether hardware acceleration is available under any form for
203 * the view hierarchy.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700204 *
Romain Guy61c8c9c2010-08-09 20:48:09 -0700205 * @return True if the view hierarchy can potentially be hardware accelerated,
206 * false otherwise
207 */
208 public static boolean isAvailable() {
209 return GLES20Canvas.isAvailable();
210 }
211
212 /**
Romain Guy2d614592010-06-09 18:21:37 -0700213 * Destroys the hardware rendering context.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700214 *
Romain Guy4caa4ed2010-08-25 14:46:24 -0700215 * @param full If true, destroys all associated resources.
Romain Guy2d614592010-06-09 18:21:37 -0700216 */
Romain Guy4caa4ed2010-08-25 14:46:24 -0700217 abstract void destroy(boolean full);
Romain Guy2d614592010-06-09 18:21:37 -0700218
219 /**
220 * Initializes the hardware renderer for the specified surface.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700221 *
Romain Guy786fc932012-07-24 16:24:56 -0700222 * @param surface The surface to hardware accelerate
Igor Murashkina86ab6402013-08-30 12:58:36 -0700223 *
Romain Guy2d614592010-06-09 18:21:37 -0700224 * @return True if the initialization was successful, false otherwise.
225 */
Igor Murashkina86ab6402013-08-30 12:58:36 -0700226 abstract boolean initialize(Surface surface) throws OutOfResourcesException;
227
Romain Guy2a83f002011-01-18 18:28:21 -0800228 /**
229 * Updates the hardware renderer for the specified surface.
Romain Guy786fc932012-07-24 16:24:56 -0700230 *
231 * @param surface The surface to hardware accelerate
Romain Guy2a83f002011-01-18 18:28:21 -0800232 */
Igor Murashkina86ab6402013-08-30 12:58:36 -0700233 abstract void updateSurface(Surface surface) throws OutOfResourcesException;
Romain Guy2d614592010-06-09 18:21:37 -0700234
235 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -0800236 * Destroys the layers used by the specified view hierarchy.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700237 *
Romain Guy6d7475d2011-07-27 16:28:21 -0700238 * @param view The root of the view hierarchy
239 */
240 abstract void destroyLayers(View view);
241
242 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -0800243 * Destroys all hardware rendering resources associated with the specified
244 * view hierarchy.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700245 *
Romain Guy31f2c2e2011-11-21 10:55:41 -0800246 * @param view The root of the view hierarchy
247 */
248 abstract void destroyHardwareResources(View view);
Igor Murashkina86ab6402013-08-30 12:58:36 -0700249
Romain Guy31f2c2e2011-11-21 10:55:41 -0800250 /**
Romain Guy03985752011-07-11 15:33:51 -0700251 * This method should be invoked whenever the current hardware renderer
Romain Guycf15efb2011-08-02 12:59:32 -0700252 * context should be reset.
Romain Guy786fc932012-07-24 16:24:56 -0700253 *
254 * @param surface The surface to hardware accelerate
Romain Guy7e1160e2011-07-08 15:49:50 -0700255 */
Romain Guy786fc932012-07-24 16:24:56 -0700256 abstract void invalidate(Surface surface);
Romain Guy03985752011-07-11 15:33:51 -0700257
258 /**
259 * This method should be invoked to ensure the hardware renderer is in
260 * valid state (for instance, to ensure the correct EGL context is bound
261 * to the current thread.)
Igor Murashkina86ab6402013-08-30 12:58:36 -0700262 *
Romain Guy03985752011-07-11 15:33:51 -0700263 * @return true if the renderer is now valid, false otherwise
264 */
265 abstract boolean validate();
Romain Guy7e1160e2011-07-08 15:49:50 -0700266
267 /**
Romain Guy1ac47652012-04-11 18:15:20 -0700268 * This method ensures the hardware renderer is in a valid state
269 * before executing the specified action.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700270 *
Romain Guy1ac47652012-04-11 18:15:20 -0700271 * This method will attempt to set a valid state even if the window
272 * the renderer is attached to was destroyed.
273 *
274 * @return true if the action was run
275 */
276 abstract boolean safelyRun(Runnable action);
277
278 /**
Romain Guy7e1160e2011-07-08 15:49:50 -0700279 * Setup the hardware renderer for drawing. This is called whenever the
280 * size of the target surface changes or when the surface is first created.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700281 *
Romain Guy2d614592010-06-09 18:21:37 -0700282 * @param width Width of the drawing surface.
283 * @param height Height of the drawing surface.
Romain Guy2d614592010-06-09 18:21:37 -0700284 */
Romain Guyfb8b7632010-08-23 21:05:08 -0700285 abstract void setup(int width, int height);
Romain Guy2d614592010-06-09 18:21:37 -0700286
Romain Guy069ea0e2011-02-08 12:24:52 -0800287 /**
Chet Haase40e03832011-10-06 08:34:13 -0700288 * Gets the current width of the surface. This is the width that the surface
289 * was last set to in a call to {@link #setup(int, int)}.
290 *
291 * @return the current width of the surface
292 */
293 abstract int getWidth();
294
295 /**
296 * Gets the current height of the surface. This is the height that the surface
297 * was last set to in a call to {@link #setup(int, int)}.
298 *
299 * @return the current width of the surface
300 */
301 abstract int getHeight();
302
303 /**
Romain Guya676ad72012-02-13 17:47:10 -0800304 * Outputs extra debugging information in the specified file descriptor.
305 * @param pw
306 */
307 abstract void dumpGfxInfo(PrintWriter pw);
Michael Jurkaa3fabff2012-03-28 17:22:29 +0200308
309 /**
310 * Outputs the total number of frames rendered (used for fps calculations)
311 *
312 * @return the number of frames rendered
313 */
314 abstract long getFrameCount();
315
Romain Guya676ad72012-02-13 17:47:10 -0800316 /**
Romain Guy5bb3c732012-11-29 17:52:58 -0800317 * Loads system properties used by the renderer. This method is invoked
318 * whenever system properties are modified. Implementations can use this
319 * to trigger live updates of the renderer based on properties.
320 *
321 * @param surface The surface to update with the new properties.
322 * Can be null.
323 *
324 * @return True if a property has changed.
325 */
John Reckcec24ae2013-11-05 13:27:50 -0800326 abstract boolean loadSystemProperties();
Romain Guy5bb3c732012-11-29 17:52:58 -0800327
Romain Guy5bb3c732012-11-29 17:52:58 -0800328 /**
Romain Guya9582652011-11-10 14:20:10 -0800329 * Sets the directory to use as a persistent storage for hardware rendering
330 * resources.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700331 *
Romain Guya9582652011-11-10 14:20:10 -0800332 * @param cacheDir A directory the current process can write to
Romain Guy52036b12013-02-14 18:03:37 -0800333 *
334 * @hide
Romain Guya9582652011-11-10 14:20:10 -0800335 */
336 public static void setupDiskCache(File cacheDir) {
John Reck30835792013-11-07 14:21:24 -0800337 GLRenderer.setupShadersDiskCache(new File(cacheDir, CACHE_PATH_SHADERS).getAbsolutePath());
Romain Guya9582652011-11-10 14:20:10 -0800338 }
339
Romain Guy244ada12012-03-28 16:41:26 -0700340 /**
Romain Guy11cb6422012-09-21 00:39:43 -0700341 * Indicates that the specified hardware layer needs to be updated
342 * as soon as possible.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700343 *
Romain Guy11cb6422012-09-21 00:39:43 -0700344 * @param layer The hardware layer that needs an update
Romain Guy40543602013-06-12 15:31:28 -0700345 *
346 * @see #flushLayerUpdates()
Romain Guye93482f2013-06-17 13:14:51 -0700347 * @see #cancelLayerUpdate(HardwareLayer)
Romain Guy11cb6422012-09-21 00:39:43 -0700348 */
349 abstract void pushLayerUpdate(HardwareLayer layer);
350
351 /**
Romain Guye93482f2013-06-17 13:14:51 -0700352 * Cancels a queued layer update. If the specified layer was not
353 * queued for update, this method has no effect.
354 *
355 * @param layer The layer whose update to cancel
356 *
357 * @see #pushLayerUpdate(HardwareLayer)
358 */
359 abstract void cancelLayerUpdate(HardwareLayer layer);
360
361 /**
Romain Guy40543602013-06-12 15:31:28 -0700362 * Forces all enqueued layer updates to be executed immediately.
363 *
364 * @see #pushLayerUpdate(HardwareLayer)
365 */
366 abstract void flushLayerUpdates();
367
368 /**
Romain Guy069ea0e2011-02-08 12:24:52 -0800369 * Interface used to receive callbacks whenever a view is drawn by
370 * a hardware renderer instance.
371 */
Dianne Hackborn0f761d62010-11-30 22:06:10 -0800372 interface HardwareDrawCallbacks {
Romain Guy069ea0e2011-02-08 12:24:52 -0800373 /**
374 * Invoked before a view is drawn by a hardware renderer.
Romain Guy96885eb2013-03-26 15:05:58 -0700375 * This method can be used to apply transformations to the
376 * canvas but no drawing command should be issued.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700377 *
Romain Guy069ea0e2011-02-08 12:24:52 -0800378 * @param canvas The Canvas used to render the view.
379 */
Romain Guy7d70fbf2011-05-24 17:40:25 -0700380 void onHardwarePreDraw(HardwareCanvas canvas);
Romain Guy069ea0e2011-02-08 12:24:52 -0800381
382 /**
383 * Invoked after a view is drawn by a hardware renderer.
Romain Guy96885eb2013-03-26 15:05:58 -0700384 * It is safe to invoke drawing commands from this method.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700385 *
Romain Guy069ea0e2011-02-08 12:24:52 -0800386 * @param canvas The Canvas used to render the view.
387 */
Romain Guy7d70fbf2011-05-24 17:40:25 -0700388 void onHardwarePostDraw(HardwareCanvas canvas);
Dianne Hackborn0f761d62010-11-30 22:06:10 -0800389 }
390
Romain Guy2d614592010-06-09 18:21:37 -0700391 /**
392 * Draws the specified view.
Romain Guy7d7b5492011-01-24 16:33:45 -0800393 *
Romain Guy2d614592010-06-09 18:21:37 -0700394 * @param view The view to draw.
395 * @param attachInfo AttachInfo tied to the specified view.
Romain Guy7d7b5492011-01-24 16:33:45 -0800396 * @param callbacks Callbacks invoked when drawing happens.
397 * @param dirty The dirty rectangle to update, can be null.
Romain Guy2d614592010-06-09 18:21:37 -0700398 */
Romain Guye55945e2013-04-04 15:26:04 -0700399 abstract void draw(View view, View.AttachInfo attachInfo, HardwareDrawCallbacks callbacks,
Romain Guy7d7b5492011-01-24 16:33:45 -0800400 Rect dirty);
Romain Guy2d614592010-06-09 18:21:37 -0700401
402 /**
Romain Guyaa6c24c2011-04-28 18:40:04 -0700403 * Creates a new hardware layer. A hardware layer built by calling this
404 * method will be treated as a texture layer, instead of as a render target.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700405 *
Romain Guya9489272011-06-22 20:58:11 -0700406 * @param isOpaque Whether the layer should be opaque or not
Igor Murashkina86ab6402013-08-30 12:58:36 -0700407 *
Romain Guyaa6c24c2011-04-28 18:40:04 -0700408 * @return A hardware layer
Jamie Gennis2af35242012-04-05 11:44:30 -0700409 */
Romain Guya9489272011-06-22 20:58:11 -0700410 abstract HardwareLayer createHardwareLayer(boolean isOpaque);
Jamie Gennis2af35242012-04-05 11:44:30 -0700411
Romain Guyaa6c24c2011-04-28 18:40:04 -0700412 /**
Romain Guy6c319ca2011-01-11 14:29:25 -0800413 * Creates a new hardware layer.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700414 *
Romain Guy6c319ca2011-01-11 14:29:25 -0800415 * @param width The minimum width of the layer
416 * @param height The minimum height of the layer
417 * @param isOpaque Whether the layer should be opaque or not
Igor Murashkina86ab6402013-08-30 12:58:36 -0700418 *
Romain Guy6c319ca2011-01-11 14:29:25 -0800419 * @return A hardware layer
420 */
421 abstract HardwareLayer createHardwareLayer(int width, int height, boolean isOpaque);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700422
423 /**
424 * Creates a new {@link SurfaceTexture} that can be used to render into the
425 * specified hardware layer.
Romain Guyaa6c24c2011-04-28 18:40:04 -0700426 *
427 * @param layer The layer to render into using a {@link android.graphics.SurfaceTexture}
Igor Murashkina86ab6402013-08-30 12:58:36 -0700428 *
Romain Guyaa6c24c2011-04-28 18:40:04 -0700429 * @return A {@link SurfaceTexture}
430 */
Romain Guye5e0c502011-06-15 15:18:31 -0700431 abstract SurfaceTexture createSurfaceTexture(HardwareLayer layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700432
433 /**
Jamie Gennis2af35242012-04-05 11:44:30 -0700434 * Sets the {@link android.graphics.SurfaceTexture} that will be used to
435 * render into the specified hardware layer.
436 *
437 * @param layer The layer to render into using a {@link android.graphics.SurfaceTexture}
438 * @param surfaceTexture The {@link android.graphics.SurfaceTexture} to use for the layer
439 */
440 abstract void setSurfaceTexture(HardwareLayer layer, SurfaceTexture surfaceTexture);
441
442 /**
Romain Guyba6be8a2012-04-23 18:22:09 -0700443 * Detaches the specified functor from the current functor execution queue.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700444 *
Romain Guyba6be8a2012-04-23 18:22:09 -0700445 * @param functor The native functor to remove from the execution queue.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700446 *
447 * @see HardwareCanvas#callDrawGLFunction(int)
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000448 * @see #attachFunctor(android.view.View.AttachInfo, long)
Romain Guyba6be8a2012-04-23 18:22:09 -0700449 */
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000450 abstract void detachFunctor(long functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700451
452 /**
453 * Schedules the specified functor in the functors execution queue.
454 *
455 * @param attachInfo AttachInfo tied to this renderer.
456 * @param functor The native functor to insert in the execution queue.
457 *
458 * @see HardwareCanvas#callDrawGLFunction(int)
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000459 * @see #detachFunctor(long)
Chris Craik41ee4652012-05-31 15:05:57 -0700460 *
Romain Guyba6be8a2012-04-23 18:22:09 -0700461 */
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000462 abstract void attachFunctor(View.AttachInfo attachInfo, long functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700463
464 /**
Romain Guy2d614592010-06-09 18:21:37 -0700465 * Initializes the hardware renderer for the specified surface and setup the
Joe Onoratoc6cc0f82011-04-12 11:53:13 -0700466 * renderer for drawing, if needed. This is invoked when the ViewAncestor has
Romain Guy2d614592010-06-09 18:21:37 -0700467 * potentially lost the hardware renderer. The hardware renderer should be
468 * reinitialized and setup when the render {@link #isRequested()} and
469 * {@link #isEnabled()}.
Romain Guy44d79742012-01-13 12:12:09 -0800470 *
Romain Guy2d614592010-06-09 18:21:37 -0700471 * @param width The width of the drawing surface.
472 * @param height The height of the drawing surface.
Romain Guy786fc932012-07-24 16:24:56 -0700473 * @param surface The surface to hardware accelerate
Igor Murashkina86ab6402013-08-30 12:58:36 -0700474 *
Romain Guydfab3632012-10-03 14:53:25 -0700475 * @return true if the surface was initialized, false otherwise. Returning
476 * false might mean that the surface was already initialized.
Romain Guy2d614592010-06-09 18:21:37 -0700477 */
Romain Guydfab3632012-10-03 14:53:25 -0700478 boolean initializeIfNeeded(int width, int height, Surface surface)
Igor Murashkina86ab6402013-08-30 12:58:36 -0700479 throws OutOfResourcesException {
Romain Guy2d614592010-06-09 18:21:37 -0700480 if (isRequested()) {
481 // We lost the gl context, so recreate it.
482 if (!isEnabled()) {
Romain Guy786fc932012-07-24 16:24:56 -0700483 if (initialize(surface)) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700484 setup(width, height);
Romain Guydfab3632012-10-03 14:53:25 -0700485 return true;
Romain Guy2d614592010-06-09 18:21:37 -0700486 }
487 }
Romain Guydfab3632012-10-03 14:53:25 -0700488 }
489 return false;
Romain Guy2d614592010-06-09 18:21:37 -0700490 }
491
492 /**
Romain Guyef359272013-01-31 19:07:29 -0800493 * Optional, sets the name of the renderer. Useful for debugging purposes.
494 *
495 * @param name The name of this renderer, can be null
496 */
497 abstract void setName(String name);
498
499 /**
Romain Guy2d614592010-06-09 18:21:37 -0700500 * Creates a hardware renderer using OpenGL.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700501 *
Romain Guye4d01122010-06-16 18:44:05 -0700502 * @param translucent True if the surface is translucent, false otherwise
Igor Murashkina86ab6402013-08-30 12:58:36 -0700503 *
Romain Guy2d614592010-06-09 18:21:37 -0700504 * @return A hardware renderer backed by OpenGL.
505 */
John Reck30835792013-11-07 14:21:24 -0800506 static HardwareRenderer create(boolean translucent) {
John Reckcec24ae2013-11-05 13:27:50 -0800507 HardwareRenderer renderer = null;
John Reck30835792013-11-07 14:21:24 -0800508 if (GLES20Canvas.isAvailable()) {
John Reck3dfe19f2013-12-13 14:25:19 -0800509 if (sUseRenderThread) {
510 renderer = new ThreadedRenderer(translucent);
511 } else {
512 renderer = new GLRenderer(translucent);
513 }
John Reckcec24ae2013-11-05 13:27:50 -0800514 }
515 return renderer;
Romain Guy2d614592010-06-09 18:21:37 -0700516 }
517
518 /**
Romain Guybdf76092011-07-18 15:00:43 -0700519 * Invoke this method when the system is running out of memory. This
520 * method will attempt to recover as much memory as possible, based on
521 * the specified hint.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700522 *
Romain Guybdf76092011-07-18 15:00:43 -0700523 * @param level Hint about the amount of memory that should be trimmed,
524 * see {@link android.content.ComponentCallbacks}
525 */
526 static void trimMemory(int level) {
Romain Guy19f86e82012-04-23 15:19:07 -0700527 startTrimMemory(level);
528 endTrimMemory();
529 }
530
531 /**
532 * Starts the process of trimming memory. Usually this call will setup
533 * hardware rendering context and reclaim memory.Extra cleanup might
534 * be required by calling {@link #endTrimMemory()}.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700535 *
Romain Guy19f86e82012-04-23 15:19:07 -0700536 * @param level Hint about the amount of memory that should be trimmed,
537 * see {@link android.content.ComponentCallbacks}
538 */
539 static void startTrimMemory(int level) {
John Reck30835792013-11-07 14:21:24 -0800540 GLRenderer.startTrimMemory(level);
Romain Guy19f86e82012-04-23 15:19:07 -0700541 }
542
543 /**
544 * Finishes the process of trimming memory. This method will usually
545 * cleanup special resources used by the memory trimming process.
546 */
547 static void endTrimMemory() {
John Reck30835792013-11-07 14:21:24 -0800548 GLRenderer.endTrimMemory();
Romain Guybdf76092011-07-18 15:00:43 -0700549 }
550
551 /**
Romain Guy2d614592010-06-09 18:21:37 -0700552 * Indicates whether hardware acceleration is currently enabled.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700553 *
Romain Guy2d614592010-06-09 18:21:37 -0700554 * @return True if hardware acceleration is in use, false otherwise.
555 */
556 boolean isEnabled() {
557 return mEnabled;
558 }
559
560 /**
561 * Indicates whether hardware acceleration is currently enabled.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700562 *
Romain Guy2d614592010-06-09 18:21:37 -0700563 * @param enabled True if the hardware renderer is in use, false otherwise.
564 */
565 void setEnabled(boolean enabled) {
566 mEnabled = enabled;
567 }
568
569 /**
570 * Indicates whether hardware acceleration is currently request but not
571 * necessarily enabled yet.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700572 *
Romain Guy2d614592010-06-09 18:21:37 -0700573 * @return True if requested, false otherwise.
574 */
575 boolean isRequested() {
576 return mRequested;
577 }
578
579 /**
Romain Guy9745fae2010-12-08 11:39:15 -0800580 * Indicates whether hardware acceleration is currently requested but not
Romain Guy2d614592010-06-09 18:21:37 -0700581 * necessarily enabled yet.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700582 *
Romain Guy2d614592010-06-09 18:21:37 -0700583 * @return True to request hardware acceleration, false otherwise.
584 */
585 void setRequested(boolean requested) {
586 mRequested = requested;
587 }
588
Romain Guy48ef4a92013-01-10 18:38:46 -0800589 /**
590 * Describes a series of frames that should be drawn on screen as a graph.
591 * Each frame is composed of 1 or more elements.
592 */
593 abstract class GraphDataProvider {
594 /**
595 * Draws the graph as bars. Frame elements are stacked on top of
596 * each other.
597 */
598 public static final int GRAPH_TYPE_BARS = 0;
599 /**
600 * Draws the graph as lines. The number of series drawn corresponds
601 * to the number of elements.
602 */
603 public static final int GRAPH_TYPE_LINES = 1;
604
605 /**
606 * Returns the type of graph to render.
607 *
608 * @return {@link #GRAPH_TYPE_BARS} or {@link #GRAPH_TYPE_LINES}
609 */
610 abstract int getGraphType();
611
612 /**
613 * This method is invoked before the graph is drawn. This method
614 * can be used to compute sizes, etc.
615 *
616 * @param metrics The display metrics
617 */
618 abstract void prepare(DisplayMetrics metrics);
619
620 /**
621 * @return The size in pixels of a vertical unit.
622 */
623 abstract int getVerticalUnitSize();
624
625 /**
626 * @return The size in pixels of a horizontal unit.
627 */
628 abstract int getHorizontalUnitSize();
629
630 /**
631 * @return The size in pixels of the margin between horizontal units.
632 */
633 abstract int getHorizontaUnitMargin();
634
635 /**
636 * An optional threshold value.
637 *
638 * @return A value >= 0 to draw the threshold, a negative value
639 * to ignore it.
640 */
641 abstract float getThreshold();
642
643 /**
644 * The data to draw in the graph. The number of elements in the
645 * array must be at least {@link #getFrameCount()} * {@link #getElementCount()}.
646 * If a value is negative the following values will be ignored.
647 */
648 abstract float[] getData();
649
650 /**
651 * Returns the number of frames to render in the graph.
652 */
653 abstract int getFrameCount();
654
655 /**
656 * Returns the number of elements in each frame. This directly affects
657 * the number of series drawn in the graph.
658 */
659 abstract int getElementCount();
660
661 /**
662 * Returns the current frame, if any. If the returned value is negative
663 * the current frame is ignored.
664 */
665 abstract int getCurrentFrame();
666
667 /**
668 * Prepares the paint to draw the specified element (or series.)
669 */
670 abstract void setupGraphPaint(Paint paint, int elementIndex);
671
672 /**
673 * Prepares the paint to draw the threshold.
674 */
675 abstract void setupThresholdPaint(Paint paint);
676
677 /**
678 * Prepares the paint to draw the current frame indicator.
679 */
680 abstract void setupCurrentFramePaint(Paint paint);
681 }
Romain Guy2d614592010-06-09 18:21:37 -0700682}