blob: 97a1f215529768dc126b0caa55529a74d7d42491 [file] [log] [blame]
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001/*
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
17package android.view;
18
19import dalvik.system.CloseGuard;
20import android.graphics.Bitmap;
21import android.graphics.Rect;
22import android.graphics.Region;
Mathias Agopian0449a402013-03-01 23:01:51 -080023import android.view.Surface;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080024import android.os.IBinder;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080025import android.util.Log;
Igor Murashkina86ab6402013-08-30 12:58:36 -070026import android.view.Surface.OutOfResourcesException;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080027
28/**
29 * SurfaceControl
30 * @hide
31 */
32public class SurfaceControl {
33 private static final String TAG = "SurfaceControl";
Mathias Agopian29479eb2013-02-14 14:36:04 -080034
Ashok Bhat36bef0b2014-01-20 20:08:01 +000035 private static native long nativeCreate(SurfaceSession session, String name,
Mathias Agopian29479eb2013-02-14 14:36:04 -080036 int w, int h, int format, int flags)
37 throws OutOfResourcesException;
Ashok Bhat36bef0b2014-01-20 20:08:01 +000038 private static native void nativeRelease(long nativeObject);
39 private static native void nativeDestroy(long nativeObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080040
41 private static native Bitmap nativeScreenshot(IBinder displayToken,
42 int width, int height, int minLayer, int maxLayer, boolean allLayers);
Mathias Agopian0449a402013-03-01 23:01:51 -080043 private static native void nativeScreenshot(IBinder displayToken, Surface consumer,
44 int width, int height, int minLayer, int maxLayer, boolean allLayers);
Mathias Agopian29479eb2013-02-14 14:36:04 -080045
46 private static native void nativeOpenTransaction();
47 private static native void nativeCloseTransaction();
48 private static native void nativeSetAnimationTransaction();
49
Ashok Bhat36bef0b2014-01-20 20:08:01 +000050 private static native void nativeSetLayer(long nativeObject, int zorder);
51 private static native void nativeSetPosition(long nativeObject, float x, float y);
52 private static native void nativeSetSize(long nativeObject, int w, int h);
53 private static native void nativeSetTransparentRegionHint(long nativeObject, Region region);
54 private static native void nativeSetAlpha(long nativeObject, float alpha);
55 private static native void nativeSetMatrix(long nativeObject, float dsdx, float dtdx, float dsdy, float dtdy);
56 private static native void nativeSetFlags(long nativeObject, int flags, int mask);
57 private static native void nativeSetWindowCrop(long nativeObject, int l, int t, int r, int b);
58 private static native void nativeSetLayerStack(long nativeObject, int layerStack);
Mathias Agopian29479eb2013-02-14 14:36:04 -080059
60 private static native IBinder nativeGetBuiltInDisplay(int physicalDisplayId);
61 private static native IBinder nativeCreateDisplay(String name, boolean secure);
Jesse Hall6a6bc212013-08-08 12:15:03 -070062 private static native void nativeDestroyDisplay(IBinder displayToken);
Mathias Agopian29479eb2013-02-14 14:36:04 -080063 private static native void nativeSetDisplaySurface(
Ashok Bhat36bef0b2014-01-20 20:08:01 +000064 IBinder displayToken, long nativeSurfaceObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080065 private static native void nativeSetDisplayLayerStack(
66 IBinder displayToken, int layerStack);
67 private static native void nativeSetDisplayProjection(
68 IBinder displayToken, int orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -070069 int l, int t, int r, int b,
Mathias Agopian29479eb2013-02-14 14:36:04 -080070 int L, int T, int R, int B);
71 private static native boolean nativeGetDisplayInfo(
72 IBinder displayToken, SurfaceControl.PhysicalDisplayInfo outInfo);
73 private static native void nativeBlankDisplay(IBinder displayToken);
74 private static native void nativeUnblankDisplay(IBinder displayToken);
75
76
Mathias Agopian3866f0d2013-02-11 22:08:48 -080077 private final CloseGuard mCloseGuard = CloseGuard.get();
Igor Murashkina86ab6402013-08-30 12:58:36 -070078 private final String mName;
Ashok Bhat36bef0b2014-01-20 20:08:01 +000079 long mNativeObject; // package visibility only for Surface.java access
Mathias Agopian3866f0d2013-02-11 22:08:48 -080080
Mathias Agopian3866f0d2013-02-11 22:08:48 -080081 /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */
82
83 /**
84 * Surface creation flag: Surface is created hidden
85 */
86 public static final int HIDDEN = 0x00000004;
87
88 /**
89 * Surface creation flag: The surface contains secure content, special
90 * measures will be taken to disallow the surface's content to be copied
91 * from another process. In particular, screenshots and VNC servers will
92 * be disabled, but other measures can take place, for instance the
Jesse Hall6a6bc212013-08-08 12:15:03 -070093 * surface might not be hardware accelerated.
Mathias Agopian3866f0d2013-02-11 22:08:48 -080094 *
95 */
96 public static final int SECURE = 0x00000080;
97
98 /**
99 * Surface creation flag: Creates a surface where color components are interpreted
100 * as "non pre-multiplied" by their alpha channel. Of course this flag is
101 * meaningless for surfaces without an alpha channel. By default
102 * surfaces are pre-multiplied, which means that each color component is
103 * already multiplied by its alpha value. In this case the blending
104 * equation used is:
Andy McFadden314405b2014-01-29 17:18:05 -0800105 * <p>
106 * <code>DEST = SRC + DEST * (1-SRC_ALPHA)</code>
107 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800108 * By contrast, non pre-multiplied surfaces use the following equation:
Andy McFadden314405b2014-01-29 17:18:05 -0800109 * <p>
110 * <code>DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)</code>
111 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800112 * pre-multiplied surfaces must always be used if transparent pixels are
113 * composited on top of each-other into the surface. A pre-multiplied
114 * surface can never lower the value of the alpha component of a given
115 * pixel.
Andy McFadden314405b2014-01-29 17:18:05 -0800116 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800117 * In some rare situations, a non pre-multiplied surface is preferable.
118 *
119 */
120 public static final int NON_PREMULTIPLIED = 0x00000100;
121
122 /**
123 * Surface creation flag: Indicates that the surface must be considered opaque,
124 * even if its pixel format is set to translucent. This can be useful if an
125 * application needs full RGBA 8888 support for instance but will
126 * still draw every pixel opaque.
Andy McFadden314405b2014-01-29 17:18:05 -0800127 * <p>
128 * This flag is ignored if setAlpha() is used to make the surface non-opaque.
129 * Combined effects are (assuming a buffer format with an alpha channel):
130 * <ul>
131 * <li>OPAQUE + alpha(1.0) == opaque composition
132 * <li>OPAQUE + alpha(0.x) == blended composition
133 * <li>!OPAQUE + alpha(1.0) == blended composition
134 * <li>!OPAQUE + alpha(0.x) == blended composition
135 * </ul>
136 * If the underlying buffer lacks an alpha channel, the OPAQUE flag is effectively
137 * set automatically.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800138 */
139 public static final int OPAQUE = 0x00000400;
140
141 /**
142 * Surface creation flag: Application requires a hardware-protected path to an
143 * external display sink. If a hardware-protected path is not available,
144 * then this surface will not be displayed on the external sink.
145 *
146 */
147 public static final int PROTECTED_APP = 0x00000800;
148
149 // 0x1000 is reserved for an independent DRM protected flag in framework
150
151 /**
152 * Surface creation flag: Creates a normal surface.
153 * This is the default.
154 *
155 */
156 public static final int FX_SURFACE_NORMAL = 0x00000000;
157
158 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800159 * Surface creation flag: Creates a Dim surface.
160 * Everything behind this surface is dimmed by the amount specified
161 * in {@link #setAlpha}. It is an error to lock a Dim surface, since it
162 * doesn't have a backing store.
163 *
164 */
165 public static final int FX_SURFACE_DIM = 0x00020000;
166
167 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800168 * Mask used for FX values above.
169 *
170 */
171 public static final int FX_SURFACE_MASK = 0x000F0000;
172
173 /* flags used with setFlags() (keep in sync with ISurfaceComposer.h) */
174
175 /**
176 * Surface flag: Hide the surface.
177 * Equivalent to calling hide().
Andy McFadden314405b2014-01-29 17:18:05 -0800178 * Updates the value set during Surface creation (see {@link #HIDDEN}).
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800179 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800180 private static final int SURFACE_HIDDEN = 0x01;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800181
Andy McFadden314405b2014-01-29 17:18:05 -0800182 /**
183 * Surface flag: composite without blending when possible.
184 * Updates the value set during Surface creation (see {@link #OPAQUE}).
185 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800186 private static final int SURFACE_OPAQUE = 0x02;
Andy McFadden314405b2014-01-29 17:18:05 -0800187
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800188
189 /* built-in physical display ids (keep in sync with ISurfaceComposer.h)
190 * these are different from the logical display ids used elsewhere in the framework */
191
192 /**
193 * Built-in physical display id: Main display.
Andy McFadden40b9ef12014-01-30 13:44:47 -0800194 * Use only with {@link SurfaceControl#getBuiltInDisplay(int)}.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800195 */
196 public static final int BUILT_IN_DISPLAY_ID_MAIN = 0;
197
198 /**
199 * Built-in physical display id: Attached HDMI display.
Andy McFadden40b9ef12014-01-30 13:44:47 -0800200 * Use only with {@link SurfaceControl#getBuiltInDisplay(int)}.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800201 */
202 public static final int BUILT_IN_DISPLAY_ID_HDMI = 1;
203
204
205
206 /**
207 * Create a surface with a name.
Andy McFadden314405b2014-01-29 17:18:05 -0800208 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800209 * The surface creation flags specify what kind of surface to create and
210 * certain options such as whether the surface can be assumed to be opaque
211 * and whether it should be initially hidden. Surfaces should always be
212 * created with the {@link #HIDDEN} flag set to ensure that they are not
213 * made visible prematurely before all of the surface's properties have been
214 * configured.
Andy McFadden314405b2014-01-29 17:18:05 -0800215 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800216 * Good practice is to first create the surface with the {@link #HIDDEN} flag
217 * specified, open a transaction, set the surface layer, layer stack, alpha,
218 * and position, call {@link #show} if appropriate, and close the transaction.
219 *
220 * @param session The surface session, must not be null.
221 * @param name The surface name, must not be null.
222 * @param w The surface initial width.
223 * @param h The surface initial height.
224 * @param flags The surface creation flags. Should always include {@link #HIDDEN}
225 * in the creation flags.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700226 *
227 * @throws throws OutOfResourcesException If the SurfaceControl cannot be created.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800228 */
229 public SurfaceControl(SurfaceSession session,
230 String name, int w, int h, int format, int flags)
231 throws OutOfResourcesException {
232 if (session == null) {
233 throw new IllegalArgumentException("session must not be null");
234 }
235 if (name == null) {
236 throw new IllegalArgumentException("name must not be null");
237 }
238
239 if ((flags & SurfaceControl.HIDDEN) == 0) {
240 Log.w(TAG, "Surfaces should always be created with the HIDDEN flag set "
241 + "to ensure that they are not made visible prematurely before "
242 + "all of the surface's properties have been configured. "
243 + "Set the other properties and make the surface visible within "
244 + "a transaction. New surface name: " + name,
245 new Throwable());
246 }
247
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800248 mName = name;
249 mNativeObject = nativeCreate(session, name, w, h, format, flags);
250 if (mNativeObject == 0) {
251 throw new OutOfResourcesException(
252 "Couldn't allocate SurfaceControl native object");
253 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700254
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800255 mCloseGuard.open("release");
256 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700257
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800258 @Override
259 protected void finalize() throws Throwable {
260 try {
261 if (mCloseGuard != null) {
262 mCloseGuard.warnIfOpen();
263 }
264 if (mNativeObject != 0) {
265 nativeRelease(mNativeObject);
266 }
267 } finally {
268 super.finalize();
269 }
270 }
271
272 @Override
273 public String toString() {
274 return "Surface(name=" + mName + ")";
275 }
276
277 /**
278 * Release the local reference to the server-side surface.
279 * Always call release() when you're done with a Surface.
280 * This will make the surface invalid.
281 */
282 public void release() {
283 if (mNativeObject != 0) {
284 nativeRelease(mNativeObject);
285 mNativeObject = 0;
286 }
287 mCloseGuard.close();
288 }
289
290 /**
291 * Free all server-side state associated with this surface and
292 * release this object's reference. This method can only be
293 * called from the process that created the service.
294 */
295 public void destroy() {
296 if (mNativeObject != 0) {
297 nativeDestroy(mNativeObject);
298 mNativeObject = 0;
299 }
300 mCloseGuard.close();
301 }
302
303 private void checkNotReleased() {
304 if (mNativeObject == 0) throw new NullPointerException(
305 "mNativeObject is null. Have you called release() already?");
306 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700307
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800308 /*
309 * set surface parameters.
310 * needs to be inside open/closeTransaction block
311 */
312
313 /** start a transaction */
314 public static void openTransaction() {
315 nativeOpenTransaction();
316 }
317
318 /** end a transaction */
319 public static void closeTransaction() {
320 nativeCloseTransaction();
321 }
322
323 /** flag the transaction as an animation */
324 public static void setAnimationTransaction() {
325 nativeSetAnimationTransaction();
326 }
327
328 public void setLayer(int zorder) {
329 checkNotReleased();
330 nativeSetLayer(mNativeObject, zorder);
331 }
332
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800333 public void setPosition(float x, float y) {
334 checkNotReleased();
335 nativeSetPosition(mNativeObject, x, y);
336 }
337
338 public void setSize(int w, int h) {
339 checkNotReleased();
340 nativeSetSize(mNativeObject, w, h);
341 }
342
343 public void hide() {
344 checkNotReleased();
345 nativeSetFlags(mNativeObject, SURFACE_HIDDEN, SURFACE_HIDDEN);
346 }
347
348 public void show() {
349 checkNotReleased();
350 nativeSetFlags(mNativeObject, 0, SURFACE_HIDDEN);
351 }
352
353 public void setTransparentRegionHint(Region region) {
354 checkNotReleased();
355 nativeSetTransparentRegionHint(mNativeObject, region);
356 }
357
Andy McFadden314405b2014-01-29 17:18:05 -0800358 /**
359 * Sets an alpha value for the entire Surface. This value is combined with the
360 * per-pixel alpha. It may be used with opaque Surfaces.
361 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800362 public void setAlpha(float alpha) {
363 checkNotReleased();
364 nativeSetAlpha(mNativeObject, alpha);
365 }
366
367 public void setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
368 checkNotReleased();
369 nativeSetMatrix(mNativeObject, dsdx, dtdx, dsdy, dtdy);
370 }
371
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800372 public void setWindowCrop(Rect crop) {
373 checkNotReleased();
374 if (crop != null) {
Jesse Hall6a6bc212013-08-08 12:15:03 -0700375 nativeSetWindowCrop(mNativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800376 crop.left, crop.top, crop.right, crop.bottom);
377 } else {
378 nativeSetWindowCrop(mNativeObject, 0, 0, 0, 0);
379 }
380 }
381
382 public void setLayerStack(int layerStack) {
383 checkNotReleased();
384 nativeSetLayerStack(mNativeObject, layerStack);
385 }
386
Andy McFadden314405b2014-01-29 17:18:05 -0800387 /**
388 * Sets the opacity of the surface. Setting the flag is equivalent to creating the
389 * Surface with the {@link #OPAQUE} flag.
390 */
391 public void setOpaque(boolean isOpaque) {
392 checkNotReleased();
393 if (isOpaque) {
394 nativeSetFlags(mNativeObject, SURFACE_OPAQUE, SURFACE_OPAQUE);
395 } else {
396 nativeSetFlags(mNativeObject, 0, SURFACE_OPAQUE);
397 }
398 }
399
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800400 /*
401 * set display parameters.
402 * needs to be inside open/closeTransaction block
403 */
404
405 /**
406 * Describes the properties of a physical display known to surface flinger.
407 */
408 public static final class PhysicalDisplayInfo {
409 public int width;
410 public int height;
411 public float refreshRate;
412 public float density;
413 public float xDpi;
414 public float yDpi;
415 public boolean secure;
Jesse Hall6a6bc212013-08-08 12:15:03 -0700416
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800417 public PhysicalDisplayInfo() {
418 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700419
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800420 public PhysicalDisplayInfo(PhysicalDisplayInfo other) {
421 copyFrom(other);
422 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700423
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800424 @Override
425 public boolean equals(Object o) {
426 return o instanceof PhysicalDisplayInfo && equals((PhysicalDisplayInfo)o);
427 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700428
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800429 public boolean equals(PhysicalDisplayInfo other) {
430 return other != null
431 && width == other.width
432 && height == other.height
433 && refreshRate == other.refreshRate
434 && density == other.density
435 && xDpi == other.xDpi
436 && yDpi == other.yDpi
437 && secure == other.secure;
438 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700439
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800440 @Override
441 public int hashCode() {
442 return 0; // don't care
443 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700444
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800445 public void copyFrom(PhysicalDisplayInfo other) {
446 width = other.width;
447 height = other.height;
448 refreshRate = other.refreshRate;
449 density = other.density;
450 xDpi = other.xDpi;
451 yDpi = other.yDpi;
452 secure = other.secure;
453 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700454
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800455 // For debugging purposes
456 @Override
457 public String toString() {
458 return "PhysicalDisplayInfo{" + width + " x " + height + ", " + refreshRate + " fps, "
459 + "density " + density + ", " + xDpi + " x " + yDpi + " dpi, secure " + secure
460 + "}";
461 }
462 }
463
464 public static void unblankDisplay(IBinder displayToken) {
465 if (displayToken == null) {
466 throw new IllegalArgumentException("displayToken must not be null");
467 }
468 nativeUnblankDisplay(displayToken);
469 }
470
471 public static void blankDisplay(IBinder displayToken) {
472 if (displayToken == null) {
473 throw new IllegalArgumentException("displayToken must not be null");
474 }
475 nativeBlankDisplay(displayToken);
476 }
477
478 public static boolean getDisplayInfo(IBinder displayToken, SurfaceControl.PhysicalDisplayInfo outInfo) {
479 if (displayToken == null) {
480 throw new IllegalArgumentException("displayToken must not be null");
481 }
482 if (outInfo == null) {
483 throw new IllegalArgumentException("outInfo must not be null");
484 }
485 return nativeGetDisplayInfo(displayToken, outInfo);
486 }
487
488 public static void setDisplayProjection(IBinder displayToken,
489 int orientation, Rect layerStackRect, Rect displayRect) {
490 if (displayToken == null) {
491 throw new IllegalArgumentException("displayToken must not be null");
492 }
493 if (layerStackRect == null) {
494 throw new IllegalArgumentException("layerStackRect must not be null");
495 }
496 if (displayRect == null) {
497 throw new IllegalArgumentException("displayRect must not be null");
498 }
499 nativeSetDisplayProjection(displayToken, orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -0700500 layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800501 displayRect.left, displayRect.top, displayRect.right, displayRect.bottom);
502 }
503
504 public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
505 if (displayToken == null) {
506 throw new IllegalArgumentException("displayToken must not be null");
507 }
508 nativeSetDisplayLayerStack(displayToken, layerStack);
509 }
510
511 public static void setDisplaySurface(IBinder displayToken, Surface surface) {
512 if (displayToken == null) {
513 throw new IllegalArgumentException("displayToken must not be null");
514 }
Jeff Brownfc0ebd72013-04-30 16:33:00 -0700515
516 if (surface != null) {
517 synchronized (surface.mLock) {
518 nativeSetDisplaySurface(displayToken, surface.mNativeObject);
519 }
520 } else {
521 nativeSetDisplaySurface(displayToken, 0);
522 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800523 }
524
525 public static IBinder createDisplay(String name, boolean secure) {
526 if (name == null) {
527 throw new IllegalArgumentException("name must not be null");
528 }
529 return nativeCreateDisplay(name, secure);
530 }
531
Jesse Hall6a6bc212013-08-08 12:15:03 -0700532 public static void destroyDisplay(IBinder displayToken) {
533 if (displayToken == null) {
534 throw new IllegalArgumentException("displayToken must not be null");
535 }
536 nativeDestroyDisplay(displayToken);
537 }
538
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800539 public static IBinder getBuiltInDisplay(int builtInDisplayId) {
540 return nativeGetBuiltInDisplay(builtInDisplayId);
541 }
542
Mathias Agopian0449a402013-03-01 23:01:51 -0800543
544 /**
545 * Copy the current screen contents into the provided {@link Surface}
546 *
547 * @param display The display to take the screenshot of.
548 * @param consumer The {@link Surface} to take the screenshot into.
549 * @param width The desired width of the returned bitmap; the raw
550 * screen will be scaled down to this size.
551 * @param height The desired height of the returned bitmap; the raw
552 * screen will be scaled down to this size.
553 * @param minLayer The lowest (bottom-most Z order) surface layer to
554 * include in the screenshot.
555 * @param maxLayer The highest (top-most Z order) surface layer to
556 * include in the screenshot.
557 */
558 public static void screenshot(IBinder display, Surface consumer,
559 int width, int height, int minLayer, int maxLayer) {
560 screenshot(display, consumer, width, height, minLayer, maxLayer, false);
561 }
562
563 /**
564 * Copy the current screen contents into the provided {@link Surface}
565 *
566 * @param display The display to take the screenshot of.
567 * @param consumer The {@link Surface} to take the screenshot into.
568 * @param width The desired width of the returned bitmap; the raw
569 * screen will be scaled down to this size.
570 * @param height The desired height of the returned bitmap; the raw
571 * screen will be scaled down to this size.
572 */
573 public static void screenshot(IBinder display, Surface consumer,
574 int width, int height) {
575 screenshot(display, consumer, width, height, 0, 0, true);
576 }
577
578 /**
579 * Copy the current screen contents into the provided {@link Surface}
580 *
581 * @param display The display to take the screenshot of.
582 * @param consumer The {@link Surface} to take the screenshot into.
583 */
584 public static void screenshot(IBinder display, Surface consumer) {
585 screenshot(display, consumer, 0, 0, 0, 0, true);
586 }
587
588
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800589 /**
590 * Copy the current screen contents into a bitmap and return it.
591 *
Mathias Agopian0449a402013-03-01 23:01:51 -0800592 * CAVEAT: Versions of screenshot that return a {@link Bitmap} can
593 * be extremely slow; avoid use unless absolutely necessary; prefer
594 * the versions that use a {@link Surface} instead, such as
595 * {@link SurfaceControl#screenshot(IBinder, Surface)}.
596 *
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800597 * @param width The desired width of the returned bitmap; the raw
598 * screen will be scaled down to this size.
599 * @param height The desired height of the returned bitmap; the raw
600 * screen will be scaled down to this size.
601 * @param minLayer The lowest (bottom-most Z order) surface layer to
602 * include in the screenshot.
603 * @param maxLayer The highest (top-most Z order) surface layer to
604 * include in the screenshot.
605 * @return Returns a Bitmap containing the screen contents, or null
Mathias Agopian49ff2c62013-03-17 01:05:21 -0700606 * if an error occurs. Make sure to call Bitmap.recycle() as soon as
607 * possible, once its content is not needed anymore.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800608 */
609 public static Bitmap screenshot(int width, int height, int minLayer, int maxLayer) {
610 // TODO: should take the display as a parameter
Mathias Agopian0449a402013-03-01 23:01:51 -0800611 IBinder displayToken = SurfaceControl.getBuiltInDisplay(
612 SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800613 return nativeScreenshot(displayToken, width, height, minLayer, maxLayer, false);
614 }
615
616 /**
617 * Like {@link SurfaceControl#screenshot(int, int, int, int)} but includes all
618 * Surfaces in the screenshot.
Mathias Agopian49ff2c62013-03-17 01:05:21 -0700619 *
620 * @param width The desired width of the returned bitmap; the raw
621 * screen will be scaled down to this size.
622 * @param height The desired height of the returned bitmap; the raw
623 * screen will be scaled down to this size.
624 * @return Returns a Bitmap containing the screen contents, or null
625 * if an error occurs. Make sure to call Bitmap.recycle() as soon as
626 * possible, once its content is not needed anymore.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800627 */
628 public static Bitmap screenshot(int width, int height) {
629 // TODO: should take the display as a parameter
Mathias Agopian0449a402013-03-01 23:01:51 -0800630 IBinder displayToken = SurfaceControl.getBuiltInDisplay(
631 SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800632 return nativeScreenshot(displayToken, width, height, 0, 0, true);
633 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700634
Mathias Agopian0449a402013-03-01 23:01:51 -0800635 private static void screenshot(IBinder display, Surface consumer,
636 int width, int height, int minLayer, int maxLayer, boolean allLayers) {
637 if (display == null) {
638 throw new IllegalArgumentException("displayToken must not be null");
639 }
640 if (consumer == null) {
641 throw new IllegalArgumentException("consumer must not be null");
642 }
643 nativeScreenshot(display, consumer, width, height, minLayer, maxLayer, allLayers);
644 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800645}