blob: a52152ec04767f562b3fbf0dcf7abe4b1fcced43 [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;
25import android.os.SystemProperties;
26import android.util.Log;
Igor Murashkina86ab6402013-08-30 12:58:36 -070027import android.view.Surface.OutOfResourcesException;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080028
29/**
30 * SurfaceControl
31 * @hide
32 */
33public class SurfaceControl {
34 private static final String TAG = "SurfaceControl";
Mathias Agopian29479eb2013-02-14 14:36:04 -080035
Ashok Bhata0398432014-01-20 20:08:01 +000036 private static native long nativeCreate(SurfaceSession session, String name,
Mathias Agopian29479eb2013-02-14 14:36:04 -080037 int w, int h, int format, int flags)
38 throws OutOfResourcesException;
Ashok Bhata0398432014-01-20 20:08:01 +000039 private static native void nativeRelease(long nativeObject);
40 private static native void nativeDestroy(long nativeObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080041
42 private static native Bitmap nativeScreenshot(IBinder displayToken,
43 int width, int height, int minLayer, int maxLayer, boolean allLayers);
Mathias Agopian0449a402013-03-01 23:01:51 -080044 private static native void nativeScreenshot(IBinder displayToken, Surface consumer,
45 int width, int height, int minLayer, int maxLayer, boolean allLayers);
Mathias Agopian29479eb2013-02-14 14:36:04 -080046
47 private static native void nativeOpenTransaction();
48 private static native void nativeCloseTransaction();
49 private static native void nativeSetAnimationTransaction();
50
Ashok Bhata0398432014-01-20 20:08:01 +000051 private static native void nativeSetLayer(long nativeObject, int zorder);
52 private static native void nativeSetPosition(long nativeObject, float x, float y);
53 private static native void nativeSetSize(long nativeObject, int w, int h);
54 private static native void nativeSetTransparentRegionHint(long nativeObject, Region region);
55 private static native void nativeSetAlpha(long nativeObject, float alpha);
56 private static native void nativeSetMatrix(long nativeObject, float dsdx, float dtdx, float dsdy, float dtdy);
57 private static native void nativeSetFlags(long nativeObject, int flags, int mask);
58 private static native void nativeSetWindowCrop(long nativeObject, int l, int t, int r, int b);
59 private static native void nativeSetLayerStack(long nativeObject, int layerStack);
Mathias Agopian29479eb2013-02-14 14:36:04 -080060
61 private static native IBinder nativeGetBuiltInDisplay(int physicalDisplayId);
62 private static native IBinder nativeCreateDisplay(String name, boolean secure);
Jesse Hall6a6bc212013-08-08 12:15:03 -070063 private static native void nativeDestroyDisplay(IBinder displayToken);
Mathias Agopian29479eb2013-02-14 14:36:04 -080064 private static native void nativeSetDisplaySurface(
Ashok Bhata0398432014-01-20 20:08:01 +000065 IBinder displayToken, long nativeSurfaceObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080066 private static native void nativeSetDisplayLayerStack(
67 IBinder displayToken, int layerStack);
68 private static native void nativeSetDisplayProjection(
69 IBinder displayToken, int orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -070070 int l, int t, int r, int b,
Mathias Agopian29479eb2013-02-14 14:36:04 -080071 int L, int T, int R, int B);
72 private static native boolean nativeGetDisplayInfo(
73 IBinder displayToken, SurfaceControl.PhysicalDisplayInfo outInfo);
74 private static native void nativeBlankDisplay(IBinder displayToken);
75 private static native void nativeUnblankDisplay(IBinder displayToken);
76
77
Mathias Agopian3866f0d2013-02-11 22:08:48 -080078 private final CloseGuard mCloseGuard = CloseGuard.get();
Igor Murashkina86ab6402013-08-30 12:58:36 -070079 private final String mName;
Ashok Bhata0398432014-01-20 20:08:01 +000080 long mNativeObject; // package visibility only for Surface.java access
Mathias Agopian3866f0d2013-02-11 22:08:48 -080081
Mathias Agopian3866f0d2013-02-11 22:08:48 -080082 /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */
83
84 /**
85 * Surface creation flag: Surface is created hidden
86 */
87 public static final int HIDDEN = 0x00000004;
88
89 /**
90 * Surface creation flag: The surface contains secure content, special
91 * measures will be taken to disallow the surface's content to be copied
92 * from another process. In particular, screenshots and VNC servers will
93 * be disabled, but other measures can take place, for instance the
Jesse Hall6a6bc212013-08-08 12:15:03 -070094 * surface might not be hardware accelerated.
Mathias Agopian3866f0d2013-02-11 22:08:48 -080095 *
96 */
97 public static final int SECURE = 0x00000080;
98
99 /**
100 * Surface creation flag: Creates a surface where color components are interpreted
101 * as "non pre-multiplied" by their alpha channel. Of course this flag is
102 * meaningless for surfaces without an alpha channel. By default
103 * surfaces are pre-multiplied, which means that each color component is
104 * already multiplied by its alpha value. In this case the blending
105 * equation used is:
106 *
107 * DEST = SRC + DEST * (1-SRC_ALPHA)
108 *
109 * By contrast, non pre-multiplied surfaces use the following equation:
110 *
111 * DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)
112 *
113 * pre-multiplied surfaces must always be used if transparent pixels are
114 * composited on top of each-other into the surface. A pre-multiplied
115 * surface can never lower the value of the alpha component of a given
116 * pixel.
117 *
118 * In some rare situations, a non pre-multiplied surface is preferable.
119 *
120 */
121 public static final int NON_PREMULTIPLIED = 0x00000100;
122
123 /**
124 * Surface creation flag: Indicates that the surface must be considered opaque,
125 * even if its pixel format is set to translucent. This can be useful if an
126 * application needs full RGBA 8888 support for instance but will
127 * still draw every pixel opaque.
128 *
129 */
130 public static final int OPAQUE = 0x00000400;
131
132 /**
133 * Surface creation flag: Application requires a hardware-protected path to an
134 * external display sink. If a hardware-protected path is not available,
135 * then this surface will not be displayed on the external sink.
136 *
137 */
138 public static final int PROTECTED_APP = 0x00000800;
139
140 // 0x1000 is reserved for an independent DRM protected flag in framework
141
142 /**
143 * Surface creation flag: Creates a normal surface.
144 * This is the default.
145 *
146 */
147 public static final int FX_SURFACE_NORMAL = 0x00000000;
148
149 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800150 * Surface creation flag: Creates a Dim surface.
151 * Everything behind this surface is dimmed by the amount specified
152 * in {@link #setAlpha}. It is an error to lock a Dim surface, since it
153 * doesn't have a backing store.
154 *
155 */
156 public static final int FX_SURFACE_DIM = 0x00020000;
157
158 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800159 * Mask used for FX values above.
160 *
161 */
162 public static final int FX_SURFACE_MASK = 0x000F0000;
163
164 /* flags used with setFlags() (keep in sync with ISurfaceComposer.h) */
165
166 /**
167 * Surface flag: Hide the surface.
168 * Equivalent to calling hide().
169 */
170 public static final int SURFACE_HIDDEN = 0x01;
171
172
173 /* built-in physical display ids (keep in sync with ISurfaceComposer.h)
174 * these are different from the logical display ids used elsewhere in the framework */
175
176 /**
177 * Built-in physical display id: Main display.
178 * Use only with {@link SurfaceControl#getBuiltInDisplay()}.
179 */
180 public static final int BUILT_IN_DISPLAY_ID_MAIN = 0;
181
182 /**
183 * Built-in physical display id: Attached HDMI display.
184 * Use only with {@link SurfaceControl#getBuiltInDisplay()}.
185 */
186 public static final int BUILT_IN_DISPLAY_ID_HDMI = 1;
187
188
189
190 /**
191 * Create a surface with a name.
192 *
193 * The surface creation flags specify what kind of surface to create and
194 * certain options such as whether the surface can be assumed to be opaque
195 * and whether it should be initially hidden. Surfaces should always be
196 * created with the {@link #HIDDEN} flag set to ensure that they are not
197 * made visible prematurely before all of the surface's properties have been
198 * configured.
199 *
200 * Good practice is to first create the surface with the {@link #HIDDEN} flag
201 * specified, open a transaction, set the surface layer, layer stack, alpha,
202 * and position, call {@link #show} if appropriate, and close the transaction.
203 *
204 * @param session The surface session, must not be null.
205 * @param name The surface name, must not be null.
206 * @param w The surface initial width.
207 * @param h The surface initial height.
208 * @param flags The surface creation flags. Should always include {@link #HIDDEN}
209 * in the creation flags.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700210 *
211 * @throws throws OutOfResourcesException If the SurfaceControl cannot be created.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800212 */
213 public SurfaceControl(SurfaceSession session,
214 String name, int w, int h, int format, int flags)
215 throws OutOfResourcesException {
216 if (session == null) {
217 throw new IllegalArgumentException("session must not be null");
218 }
219 if (name == null) {
220 throw new IllegalArgumentException("name must not be null");
221 }
222
223 if ((flags & SurfaceControl.HIDDEN) == 0) {
224 Log.w(TAG, "Surfaces should always be created with the HIDDEN flag set "
225 + "to ensure that they are not made visible prematurely before "
226 + "all of the surface's properties have been configured. "
227 + "Set the other properties and make the surface visible within "
228 + "a transaction. New surface name: " + name,
229 new Throwable());
230 }
231
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800232 mName = name;
233 mNativeObject = nativeCreate(session, name, w, h, format, flags);
234 if (mNativeObject == 0) {
235 throw new OutOfResourcesException(
236 "Couldn't allocate SurfaceControl native object");
237 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700238
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800239 mCloseGuard.open("release");
240 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700241
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800242 @Override
243 protected void finalize() throws Throwable {
244 try {
245 if (mCloseGuard != null) {
246 mCloseGuard.warnIfOpen();
247 }
248 if (mNativeObject != 0) {
249 nativeRelease(mNativeObject);
250 }
251 } finally {
252 super.finalize();
253 }
254 }
255
256 @Override
257 public String toString() {
258 return "Surface(name=" + mName + ")";
259 }
260
261 /**
262 * Release the local reference to the server-side surface.
263 * Always call release() when you're done with a Surface.
264 * This will make the surface invalid.
265 */
266 public void release() {
267 if (mNativeObject != 0) {
268 nativeRelease(mNativeObject);
269 mNativeObject = 0;
270 }
271 mCloseGuard.close();
272 }
273
274 /**
275 * Free all server-side state associated with this surface and
276 * release this object's reference. This method can only be
277 * called from the process that created the service.
278 */
279 public void destroy() {
280 if (mNativeObject != 0) {
281 nativeDestroy(mNativeObject);
282 mNativeObject = 0;
283 }
284 mCloseGuard.close();
285 }
286
287 private void checkNotReleased() {
288 if (mNativeObject == 0) throw new NullPointerException(
289 "mNativeObject is null. Have you called release() already?");
290 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700291
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800292 /*
293 * set surface parameters.
294 * needs to be inside open/closeTransaction block
295 */
296
297 /** start a transaction */
298 public static void openTransaction() {
299 nativeOpenTransaction();
300 }
301
302 /** end a transaction */
303 public static void closeTransaction() {
304 nativeCloseTransaction();
305 }
306
307 /** flag the transaction as an animation */
308 public static void setAnimationTransaction() {
309 nativeSetAnimationTransaction();
310 }
311
312 public void setLayer(int zorder) {
313 checkNotReleased();
314 nativeSetLayer(mNativeObject, zorder);
315 }
316
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800317 public void setPosition(float x, float y) {
318 checkNotReleased();
319 nativeSetPosition(mNativeObject, x, y);
320 }
321
322 public void setSize(int w, int h) {
323 checkNotReleased();
324 nativeSetSize(mNativeObject, w, h);
325 }
326
327 public void hide() {
328 checkNotReleased();
329 nativeSetFlags(mNativeObject, SURFACE_HIDDEN, SURFACE_HIDDEN);
330 }
331
332 public void show() {
333 checkNotReleased();
334 nativeSetFlags(mNativeObject, 0, SURFACE_HIDDEN);
335 }
336
337 public void setTransparentRegionHint(Region region) {
338 checkNotReleased();
339 nativeSetTransparentRegionHint(mNativeObject, region);
340 }
341
342 public void setAlpha(float alpha) {
343 checkNotReleased();
344 nativeSetAlpha(mNativeObject, alpha);
345 }
346
347 public void setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
348 checkNotReleased();
349 nativeSetMatrix(mNativeObject, dsdx, dtdx, dsdy, dtdy);
350 }
351
352 public void setFlags(int flags, int mask) {
353 checkNotReleased();
354 nativeSetFlags(mNativeObject, flags, mask);
355 }
356
357 public void setWindowCrop(Rect crop) {
358 checkNotReleased();
359 if (crop != null) {
Jesse Hall6a6bc212013-08-08 12:15:03 -0700360 nativeSetWindowCrop(mNativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800361 crop.left, crop.top, crop.right, crop.bottom);
362 } else {
363 nativeSetWindowCrop(mNativeObject, 0, 0, 0, 0);
364 }
365 }
366
367 public void setLayerStack(int layerStack) {
368 checkNotReleased();
369 nativeSetLayerStack(mNativeObject, layerStack);
370 }
371
372 /*
373 * set display parameters.
374 * needs to be inside open/closeTransaction block
375 */
376
377 /**
378 * Describes the properties of a physical display known to surface flinger.
379 */
380 public static final class PhysicalDisplayInfo {
381 public int width;
382 public int height;
383 public float refreshRate;
384 public float density;
385 public float xDpi;
386 public float yDpi;
387 public boolean secure;
Jesse Hall6a6bc212013-08-08 12:15:03 -0700388
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800389 public PhysicalDisplayInfo() {
390 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700391
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800392 public PhysicalDisplayInfo(PhysicalDisplayInfo other) {
393 copyFrom(other);
394 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700395
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800396 @Override
397 public boolean equals(Object o) {
398 return o instanceof PhysicalDisplayInfo && equals((PhysicalDisplayInfo)o);
399 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700400
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800401 public boolean equals(PhysicalDisplayInfo other) {
402 return other != null
403 && width == other.width
404 && height == other.height
405 && refreshRate == other.refreshRate
406 && density == other.density
407 && xDpi == other.xDpi
408 && yDpi == other.yDpi
409 && secure == other.secure;
410 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700411
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800412 @Override
413 public int hashCode() {
414 return 0; // don't care
415 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700416
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800417 public void copyFrom(PhysicalDisplayInfo other) {
418 width = other.width;
419 height = other.height;
420 refreshRate = other.refreshRate;
421 density = other.density;
422 xDpi = other.xDpi;
423 yDpi = other.yDpi;
424 secure = other.secure;
425 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700426
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800427 // For debugging purposes
428 @Override
429 public String toString() {
430 return "PhysicalDisplayInfo{" + width + " x " + height + ", " + refreshRate + " fps, "
431 + "density " + density + ", " + xDpi + " x " + yDpi + " dpi, secure " + secure
432 + "}";
433 }
434 }
435
436 public static void unblankDisplay(IBinder displayToken) {
437 if (displayToken == null) {
438 throw new IllegalArgumentException("displayToken must not be null");
439 }
440 nativeUnblankDisplay(displayToken);
441 }
442
443 public static void blankDisplay(IBinder displayToken) {
444 if (displayToken == null) {
445 throw new IllegalArgumentException("displayToken must not be null");
446 }
447 nativeBlankDisplay(displayToken);
448 }
449
450 public static boolean getDisplayInfo(IBinder displayToken, SurfaceControl.PhysicalDisplayInfo outInfo) {
451 if (displayToken == null) {
452 throw new IllegalArgumentException("displayToken must not be null");
453 }
454 if (outInfo == null) {
455 throw new IllegalArgumentException("outInfo must not be null");
456 }
457 return nativeGetDisplayInfo(displayToken, outInfo);
458 }
459
460 public static void setDisplayProjection(IBinder displayToken,
461 int orientation, Rect layerStackRect, Rect displayRect) {
462 if (displayToken == null) {
463 throw new IllegalArgumentException("displayToken must not be null");
464 }
465 if (layerStackRect == null) {
466 throw new IllegalArgumentException("layerStackRect must not be null");
467 }
468 if (displayRect == null) {
469 throw new IllegalArgumentException("displayRect must not be null");
470 }
471 nativeSetDisplayProjection(displayToken, orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -0700472 layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800473 displayRect.left, displayRect.top, displayRect.right, displayRect.bottom);
474 }
475
476 public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
477 if (displayToken == null) {
478 throw new IllegalArgumentException("displayToken must not be null");
479 }
480 nativeSetDisplayLayerStack(displayToken, layerStack);
481 }
482
483 public static void setDisplaySurface(IBinder displayToken, Surface surface) {
484 if (displayToken == null) {
485 throw new IllegalArgumentException("displayToken must not be null");
486 }
Jeff Brownfc0ebd72013-04-30 16:33:00 -0700487
488 if (surface != null) {
489 synchronized (surface.mLock) {
490 nativeSetDisplaySurface(displayToken, surface.mNativeObject);
491 }
492 } else {
493 nativeSetDisplaySurface(displayToken, 0);
494 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800495 }
496
497 public static IBinder createDisplay(String name, boolean secure) {
498 if (name == null) {
499 throw new IllegalArgumentException("name must not be null");
500 }
501 return nativeCreateDisplay(name, secure);
502 }
503
Jesse Hall6a6bc212013-08-08 12:15:03 -0700504 public static void destroyDisplay(IBinder displayToken) {
505 if (displayToken == null) {
506 throw new IllegalArgumentException("displayToken must not be null");
507 }
508 nativeDestroyDisplay(displayToken);
509 }
510
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800511 public static IBinder getBuiltInDisplay(int builtInDisplayId) {
512 return nativeGetBuiltInDisplay(builtInDisplayId);
513 }
514
Mathias Agopian0449a402013-03-01 23:01:51 -0800515
516 /**
517 * Copy the current screen contents into the provided {@link Surface}
518 *
519 * @param display The display to take the screenshot of.
520 * @param consumer The {@link Surface} to take the screenshot into.
521 * @param width The desired width of the returned bitmap; the raw
522 * screen will be scaled down to this size.
523 * @param height The desired height of the returned bitmap; the raw
524 * screen will be scaled down to this size.
525 * @param minLayer The lowest (bottom-most Z order) surface layer to
526 * include in the screenshot.
527 * @param maxLayer The highest (top-most Z order) surface layer to
528 * include in the screenshot.
529 */
530 public static void screenshot(IBinder display, Surface consumer,
531 int width, int height, int minLayer, int maxLayer) {
532 screenshot(display, consumer, width, height, minLayer, maxLayer, false);
533 }
534
535 /**
536 * Copy the current screen contents into the provided {@link Surface}
537 *
538 * @param display The display to take the screenshot of.
539 * @param consumer The {@link Surface} to take the screenshot into.
540 * @param width The desired width of the returned bitmap; the raw
541 * screen will be scaled down to this size.
542 * @param height The desired height of the returned bitmap; the raw
543 * screen will be scaled down to this size.
544 */
545 public static void screenshot(IBinder display, Surface consumer,
546 int width, int height) {
547 screenshot(display, consumer, width, height, 0, 0, true);
548 }
549
550 /**
551 * Copy the current screen contents into the provided {@link Surface}
552 *
553 * @param display The display to take the screenshot of.
554 * @param consumer The {@link Surface} to take the screenshot into.
555 */
556 public static void screenshot(IBinder display, Surface consumer) {
557 screenshot(display, consumer, 0, 0, 0, 0, true);
558 }
559
560
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800561 /**
562 * Copy the current screen contents into a bitmap and return it.
563 *
Mathias Agopian0449a402013-03-01 23:01:51 -0800564 * CAVEAT: Versions of screenshot that return a {@link Bitmap} can
565 * be extremely slow; avoid use unless absolutely necessary; prefer
566 * the versions that use a {@link Surface} instead, such as
567 * {@link SurfaceControl#screenshot(IBinder, Surface)}.
568 *
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800569 * @param width The desired width of the returned bitmap; the raw
570 * screen will be scaled down to this size.
571 * @param height The desired height of the returned bitmap; the raw
572 * screen will be scaled down to this size.
573 * @param minLayer The lowest (bottom-most Z order) surface layer to
574 * include in the screenshot.
575 * @param maxLayer The highest (top-most Z order) surface layer to
576 * include in the screenshot.
577 * @return Returns a Bitmap containing the screen contents, or null
Mathias Agopian49ff2c62013-03-17 01:05:21 -0700578 * if an error occurs. Make sure to call Bitmap.recycle() as soon as
579 * possible, once its content is not needed anymore.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800580 */
581 public static Bitmap screenshot(int width, int height, int minLayer, int maxLayer) {
582 // TODO: should take the display as a parameter
Mathias Agopian0449a402013-03-01 23:01:51 -0800583 IBinder displayToken = SurfaceControl.getBuiltInDisplay(
584 SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800585 return nativeScreenshot(displayToken, width, height, minLayer, maxLayer, false);
586 }
587
588 /**
589 * Like {@link SurfaceControl#screenshot(int, int, int, int)} but includes all
590 * Surfaces in the screenshot.
Mathias Agopian49ff2c62013-03-17 01:05:21 -0700591 *
592 * @param width The desired width of the returned bitmap; the raw
593 * screen will be scaled down to this size.
594 * @param height The desired height of the returned bitmap; the raw
595 * screen will be scaled down to this size.
596 * @return Returns a Bitmap containing the screen contents, or null
597 * if an error occurs. Make sure to call Bitmap.recycle() as soon as
598 * possible, once its content is not needed anymore.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800599 */
600 public static Bitmap screenshot(int width, int height) {
601 // TODO: should take the display as a parameter
Mathias Agopian0449a402013-03-01 23:01:51 -0800602 IBinder displayToken = SurfaceControl.getBuiltInDisplay(
603 SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800604 return nativeScreenshot(displayToken, width, height, 0, 0, true);
605 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700606
Mathias Agopian0449a402013-03-01 23:01:51 -0800607 private static void screenshot(IBinder display, Surface consumer,
608 int width, int height, int minLayer, int maxLayer, boolean allLayers) {
609 if (display == null) {
610 throw new IllegalArgumentException("displayToken must not be null");
611 }
612 if (consumer == null) {
613 throw new IllegalArgumentException("consumer must not be null");
614 }
615 nativeScreenshot(display, consumer, width, height, minLayer, maxLayer, allLayers);
616 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800617}