Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -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 | |
| 19 | import dalvik.system.CloseGuard; |
| 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.Rect; |
| 22 | import android.graphics.Region; |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 23 | import android.view.Surface; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 24 | import android.os.IBinder; |
| 25 | import android.os.SystemProperties; |
| 26 | import android.util.Log; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 27 | import android.view.Surface.OutOfResourcesException; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * SurfaceControl |
| 31 | * @hide |
| 32 | */ |
| 33 | public class SurfaceControl { |
| 34 | private static final String TAG = "SurfaceControl"; |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 35 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 36 | private static native long nativeCreate(SurfaceSession session, String name, |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 37 | int w, int h, int format, int flags) |
| 38 | throws OutOfResourcesException; |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 39 | private static native void nativeRelease(long nativeObject); |
| 40 | private static native void nativeDestroy(long nativeObject); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 41 | |
| 42 | private static native Bitmap nativeScreenshot(IBinder displayToken, |
| 43 | int width, int height, int minLayer, int maxLayer, boolean allLayers); |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 44 | private static native void nativeScreenshot(IBinder displayToken, Surface consumer, |
| 45 | int width, int height, int minLayer, int maxLayer, boolean allLayers); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 46 | |
| 47 | private static native void nativeOpenTransaction(); |
| 48 | private static native void nativeCloseTransaction(); |
| 49 | private static native void nativeSetAnimationTransaction(); |
| 50 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 51 | 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 Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 60 | |
| 61 | private static native IBinder nativeGetBuiltInDisplay(int physicalDisplayId); |
| 62 | private static native IBinder nativeCreateDisplay(String name, boolean secure); |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 63 | private static native void nativeDestroyDisplay(IBinder displayToken); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 64 | private static native void nativeSetDisplaySurface( |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 65 | IBinder displayToken, long nativeSurfaceObject); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 66 | private static native void nativeSetDisplayLayerStack( |
| 67 | IBinder displayToken, int layerStack); |
| 68 | private static native void nativeSetDisplayProjection( |
| 69 | IBinder displayToken, int orientation, |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 70 | int l, int t, int r, int b, |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 71 | 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 Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 78 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 79 | private final String mName; |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 80 | long mNativeObject; // package visibility only for Surface.java access |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 81 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 82 | /* 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 Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 94 | * surface might not be hardware accelerated. |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 95 | * |
| 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 Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 150 | * 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 Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 159 | * 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 Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 210 | * |
| 211 | * @throws throws OutOfResourcesException If the SurfaceControl cannot be created. |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 212 | */ |
| 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 Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 232 | 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 Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 238 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 239 | mCloseGuard.open("release"); |
| 240 | } |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 241 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 242 | @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 Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 291 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 292 | /* |
| 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 Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 317 | 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 Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 360 | nativeSetWindowCrop(mNativeObject, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 361 | 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 Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 388 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 389 | public PhysicalDisplayInfo() { |
| 390 | } |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 391 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 392 | public PhysicalDisplayInfo(PhysicalDisplayInfo other) { |
| 393 | copyFrom(other); |
| 394 | } |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 395 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 396 | @Override |
| 397 | public boolean equals(Object o) { |
| 398 | return o instanceof PhysicalDisplayInfo && equals((PhysicalDisplayInfo)o); |
| 399 | } |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 400 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 401 | 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 Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 411 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 412 | @Override |
| 413 | public int hashCode() { |
| 414 | return 0; // don't care |
| 415 | } |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 416 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 417 | 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 Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 426 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 427 | // 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 Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 472 | layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 473 | 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 Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 487 | |
| 488 | if (surface != null) { |
| 489 | synchronized (surface.mLock) { |
| 490 | nativeSetDisplaySurface(displayToken, surface.mNativeObject); |
| 491 | } |
| 492 | } else { |
| 493 | nativeSetDisplaySurface(displayToken, 0); |
| 494 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 495 | } |
| 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 Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 504 | 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 Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 511 | public static IBinder getBuiltInDisplay(int builtInDisplayId) { |
| 512 | return nativeGetBuiltInDisplay(builtInDisplayId); |
| 513 | } |
| 514 | |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 515 | |
| 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 Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 561 | /** |
| 562 | * Copy the current screen contents into a bitmap and return it. |
| 563 | * |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 564 | * 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 Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 569 | * @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 Agopian | 49ff2c6 | 2013-03-17 01:05:21 -0700 | [diff] [blame] | 578 | * if an error occurs. Make sure to call Bitmap.recycle() as soon as |
| 579 | * possible, once its content is not needed anymore. |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 580 | */ |
| 581 | public static Bitmap screenshot(int width, int height, int minLayer, int maxLayer) { |
| 582 | // TODO: should take the display as a parameter |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 583 | IBinder displayToken = SurfaceControl.getBuiltInDisplay( |
| 584 | SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 585 | 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 Agopian | 49ff2c6 | 2013-03-17 01:05:21 -0700 | [diff] [blame] | 591 | * |
| 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 Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 599 | */ |
| 600 | public static Bitmap screenshot(int width, int height) { |
| 601 | // TODO: should take the display as a parameter |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 602 | IBinder displayToken = SurfaceControl.getBuiltInDisplay( |
| 603 | SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 604 | return nativeScreenshot(displayToken, width, height, 0, 0, true); |
| 605 | } |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 606 | |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 607 | 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 Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 617 | } |