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 static android.graphics.Matrix.MSCALE_X; 20 import static android.graphics.Matrix.MSCALE_Y; 21 import static android.graphics.Matrix.MSKEW_X; 22 import static android.graphics.Matrix.MSKEW_Y; 23 import static android.graphics.Matrix.MTRANS_X; 24 import static android.graphics.Matrix.MTRANS_Y; 25 import static android.view.Surface.ROTATION_270; 26 import static android.view.Surface.ROTATION_90; 27 import static android.view.SurfaceControlProto.HASH_CODE; 28 import static android.view.SurfaceControlProto.NAME; 29 30 import android.annotation.FloatRange; 31 import android.annotation.IntRange; 32 import android.annotation.NonNull; 33 import android.annotation.Nullable; 34 import android.annotation.Size; 35 import android.annotation.UnsupportedAppUsage; 36 import android.graphics.Bitmap; 37 import android.graphics.ColorSpace; 38 import android.graphics.GraphicBuffer; 39 import android.graphics.Matrix; 40 import android.graphics.PixelFormat; 41 import android.graphics.Point; 42 import android.graphics.Rect; 43 import android.graphics.Region; 44 import android.hardware.display.DisplayedContentSample; 45 import android.hardware.display.DisplayedContentSamplingAttributes; 46 import android.os.Build; 47 import android.os.IBinder; 48 import android.os.Parcel; 49 import android.os.Parcelable; 50 import android.util.ArrayMap; 51 import android.util.Log; 52 import android.util.SparseIntArray; 53 import android.util.proto.ProtoOutputStream; 54 import android.view.Surface.OutOfResourcesException; 55 56 import com.android.internal.annotations.GuardedBy; 57 58 import dalvik.system.CloseGuard; 59 60 import libcore.util.NativeAllocationRegistry; 61 62 import java.io.Closeable; 63 import java.nio.ByteBuffer; 64 import java.nio.ByteOrder; 65 import java.util.Objects; 66 67 /** 68 * Handle to an on-screen Surface managed by the system compositor. The SurfaceControl is 69 * a combination of a buffer source, and metadata about how to display the buffers. 70 * By constructing a {@link Surface} from this SurfaceControl you can submit buffers to be 71 * composited. Using {@link SurfaceControl.Transaction} you can manipulate various 72 * properties of how the buffer will be displayed on-screen. SurfaceControl's are 73 * arranged into a scene-graph like hierarchy, and as such any SurfaceControl may have 74 * a parent. Geometric properties like transform, crop, and Z-ordering will be inherited 75 * from the parent, as if the child were content in the parents buffer stream. 76 */ 77 public final class SurfaceControl implements Parcelable { 78 private static final String TAG = "SurfaceControl"; 79 nativeCreate(SurfaceSession session, String name, int w, int h, int format, int flags, long parentObject, Parcel metadata)80 private static native long nativeCreate(SurfaceSession session, String name, 81 int w, int h, int format, int flags, long parentObject, Parcel metadata) 82 throws OutOfResourcesException; nativeReadFromParcel(Parcel in)83 private static native long nativeReadFromParcel(Parcel in); nativeCopyFromSurfaceControl(long nativeObject)84 private static native long nativeCopyFromSurfaceControl(long nativeObject); nativeWriteToParcel(long nativeObject, Parcel out)85 private static native void nativeWriteToParcel(long nativeObject, Parcel out); nativeRelease(long nativeObject)86 private static native void nativeRelease(long nativeObject); nativeDestroy(long nativeObject)87 private static native void nativeDestroy(long nativeObject); nativeDisconnect(long nativeObject)88 private static native void nativeDisconnect(long nativeObject); 89 nativeScreenshot(IBinder displayToken, Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation, boolean captureSecureLayers)90 private static native ScreenshotGraphicBuffer nativeScreenshot(IBinder displayToken, 91 Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation, 92 boolean captureSecureLayers); nativeCaptureLayers(IBinder displayToken, IBinder layerHandleToken, Rect sourceCrop, float frameScale, IBinder[] excludeLayers)93 private static native ScreenshotGraphicBuffer nativeCaptureLayers(IBinder displayToken, 94 IBinder layerHandleToken, Rect sourceCrop, float frameScale, IBinder[] excludeLayers); 95 nativeCreateTransaction()96 private static native long nativeCreateTransaction(); nativeGetNativeTransactionFinalizer()97 private static native long nativeGetNativeTransactionFinalizer(); nativeApplyTransaction(long transactionObj, boolean sync)98 private static native void nativeApplyTransaction(long transactionObj, boolean sync); nativeMergeTransaction(long transactionObj, long otherTransactionObj)99 private static native void nativeMergeTransaction(long transactionObj, 100 long otherTransactionObj); nativeSetAnimationTransaction(long transactionObj)101 private static native void nativeSetAnimationTransaction(long transactionObj); nativeSetEarlyWakeup(long transactionObj)102 private static native void nativeSetEarlyWakeup(long transactionObj); 103 nativeSetLayer(long transactionObj, long nativeObject, int zorder)104 private static native void nativeSetLayer(long transactionObj, long nativeObject, int zorder); nativeSetRelativeLayer(long transactionObj, long nativeObject, IBinder relativeTo, int zorder)105 private static native void nativeSetRelativeLayer(long transactionObj, long nativeObject, 106 IBinder relativeTo, int zorder); nativeSetPosition(long transactionObj, long nativeObject, float x, float y)107 private static native void nativeSetPosition(long transactionObj, long nativeObject, 108 float x, float y); nativeSetGeometryAppliesWithResize(long transactionObj, long nativeObject)109 private static native void nativeSetGeometryAppliesWithResize(long transactionObj, 110 long nativeObject); nativeSetSize(long transactionObj, long nativeObject, int w, int h)111 private static native void nativeSetSize(long transactionObj, long nativeObject, int w, int h); nativeSetTransparentRegionHint(long transactionObj, long nativeObject, Region region)112 private static native void nativeSetTransparentRegionHint(long transactionObj, 113 long nativeObject, Region region); nativeSetAlpha(long transactionObj, long nativeObject, float alpha)114 private static native void nativeSetAlpha(long transactionObj, long nativeObject, float alpha); nativeSetMatrix(long transactionObj, long nativeObject, float dsdx, float dtdx, float dtdy, float dsdy)115 private static native void nativeSetMatrix(long transactionObj, long nativeObject, 116 float dsdx, float dtdx, 117 float dtdy, float dsdy); nativeSetColorTransform(long transactionObj, long nativeObject, float[] matrix, float[] translation)118 private static native void nativeSetColorTransform(long transactionObj, long nativeObject, 119 float[] matrix, float[] translation); nativeSetColorSpaceAgnostic(long transactionObj, long nativeObject, boolean agnostic)120 private static native void nativeSetColorSpaceAgnostic(long transactionObj, long nativeObject, 121 boolean agnostic); nativeSetGeometry(long transactionObj, long nativeObject, Rect sourceCrop, Rect dest, long orientation)122 private static native void nativeSetGeometry(long transactionObj, long nativeObject, 123 Rect sourceCrop, Rect dest, long orientation); nativeSetColor(long transactionObj, long nativeObject, float[] color)124 private static native void nativeSetColor(long transactionObj, long nativeObject, float[] color); nativeSetFlags(long transactionObj, long nativeObject, int flags, int mask)125 private static native void nativeSetFlags(long transactionObj, long nativeObject, 126 int flags, int mask); nativeSetWindowCrop(long transactionObj, long nativeObject, int l, int t, int r, int b)127 private static native void nativeSetWindowCrop(long transactionObj, long nativeObject, 128 int l, int t, int r, int b); nativeSetCornerRadius(long transactionObj, long nativeObject, float cornerRadius)129 private static native void nativeSetCornerRadius(long transactionObj, long nativeObject, 130 float cornerRadius); nativeSetLayerStack(long transactionObj, long nativeObject, int layerStack)131 private static native void nativeSetLayerStack(long transactionObj, long nativeObject, 132 int layerStack); 133 nativeClearContentFrameStats(long nativeObject)134 private static native boolean nativeClearContentFrameStats(long nativeObject); nativeGetContentFrameStats(long nativeObject, WindowContentFrameStats outStats)135 private static native boolean nativeGetContentFrameStats(long nativeObject, WindowContentFrameStats outStats); nativeClearAnimationFrameStats()136 private static native boolean nativeClearAnimationFrameStats(); nativeGetAnimationFrameStats(WindowAnimationFrameStats outStats)137 private static native boolean nativeGetAnimationFrameStats(WindowAnimationFrameStats outStats); 138 nativeGetPhysicalDisplayIds()139 private static native long[] nativeGetPhysicalDisplayIds(); nativeGetPhysicalDisplayToken(long physicalDisplayId)140 private static native IBinder nativeGetPhysicalDisplayToken(long physicalDisplayId); nativeCreateDisplay(String name, boolean secure)141 private static native IBinder nativeCreateDisplay(String name, boolean secure); nativeDestroyDisplay(IBinder displayToken)142 private static native void nativeDestroyDisplay(IBinder displayToken); nativeSetDisplaySurface(long transactionObj, IBinder displayToken, long nativeSurfaceObject)143 private static native void nativeSetDisplaySurface(long transactionObj, 144 IBinder displayToken, long nativeSurfaceObject); nativeSetDisplayLayerStack(long transactionObj, IBinder displayToken, int layerStack)145 private static native void nativeSetDisplayLayerStack(long transactionObj, 146 IBinder displayToken, int layerStack); nativeSetDisplayProjection(long transactionObj, IBinder displayToken, int orientation, int l, int t, int r, int b, int L, int T, int R, int B)147 private static native void nativeSetDisplayProjection(long transactionObj, 148 IBinder displayToken, int orientation, 149 int l, int t, int r, int b, 150 int L, int T, int R, int B); nativeSetDisplaySize(long transactionObj, IBinder displayToken, int width, int height)151 private static native void nativeSetDisplaySize(long transactionObj, IBinder displayToken, 152 int width, int height); nativeGetDisplayConfigs( IBinder displayToken)153 private static native SurfaceControl.PhysicalDisplayInfo[] nativeGetDisplayConfigs( 154 IBinder displayToken); 155 private static native DisplayedContentSamplingAttributes nativeGetDisplayedContentSamplingAttributes(IBinder displayToken)156 nativeGetDisplayedContentSamplingAttributes(IBinder displayToken); nativeSetDisplayedContentSamplingEnabled(IBinder displayToken, boolean enable, int componentMask, int maxFrames)157 private static native boolean nativeSetDisplayedContentSamplingEnabled(IBinder displayToken, 158 boolean enable, int componentMask, int maxFrames); nativeGetDisplayedContentSample( IBinder displayToken, long numFrames, long timestamp)159 private static native DisplayedContentSample nativeGetDisplayedContentSample( 160 IBinder displayToken, long numFrames, long timestamp); nativeGetActiveConfig(IBinder displayToken)161 private static native int nativeGetActiveConfig(IBinder displayToken); nativeSetActiveConfig(IBinder displayToken, int id)162 private static native boolean nativeSetActiveConfig(IBinder displayToken, int id); nativeSetAllowedDisplayConfigs(IBinder displayToken, int[] allowedConfigs)163 private static native boolean nativeSetAllowedDisplayConfigs(IBinder displayToken, 164 int[] allowedConfigs); nativeGetAllowedDisplayConfigs(IBinder displayToken)165 private static native int[] nativeGetAllowedDisplayConfigs(IBinder displayToken); nativeGetDisplayColorModes(IBinder displayToken)166 private static native int[] nativeGetDisplayColorModes(IBinder displayToken); nativeGetDisplayNativePrimaries( IBinder displayToken)167 private static native SurfaceControl.DisplayPrimaries nativeGetDisplayNativePrimaries( 168 IBinder displayToken); nativeGetCompositionDataspaces()169 private static native int[] nativeGetCompositionDataspaces(); nativeGetActiveColorMode(IBinder displayToken)170 private static native int nativeGetActiveColorMode(IBinder displayToken); nativeSetActiveColorMode(IBinder displayToken, int colorMode)171 private static native boolean nativeSetActiveColorMode(IBinder displayToken, 172 int colorMode); nativeSetDisplayPowerMode( IBinder displayToken, int mode)173 private static native void nativeSetDisplayPowerMode( 174 IBinder displayToken, int mode); nativeDeferTransactionUntil(long transactionObj, long nativeObject, IBinder handle, long frame)175 private static native void nativeDeferTransactionUntil(long transactionObj, long nativeObject, 176 IBinder handle, long frame); nativeDeferTransactionUntilSurface(long transactionObj, long nativeObject, long surfaceObject, long frame)177 private static native void nativeDeferTransactionUntilSurface(long transactionObj, 178 long nativeObject, 179 long surfaceObject, long frame); nativeReparentChildren(long transactionObj, long nativeObject, IBinder handle)180 private static native void nativeReparentChildren(long transactionObj, long nativeObject, 181 IBinder handle); nativeReparent(long transactionObj, long nativeObject, long newParentNativeObject)182 private static native void nativeReparent(long transactionObj, long nativeObject, 183 long newParentNativeObject); nativeSeverChildren(long transactionObj, long nativeObject)184 private static native void nativeSeverChildren(long transactionObj, long nativeObject); nativeSetOverrideScalingMode(long transactionObj, long nativeObject, int scalingMode)185 private static native void nativeSetOverrideScalingMode(long transactionObj, long nativeObject, 186 int scalingMode); nativeGetHandle(long nativeObject)187 private static native IBinder nativeGetHandle(long nativeObject); nativeGetTransformToDisplayInverse(long nativeObject)188 private static native boolean nativeGetTransformToDisplayInverse(long nativeObject); 189 nativeGetHdrCapabilities(IBinder displayToken)190 private static native Display.HdrCapabilities nativeGetHdrCapabilities(IBinder displayToken); 191 nativeSetInputWindowInfo(long transactionObj, long nativeObject, InputWindowHandle handle)192 private static native void nativeSetInputWindowInfo(long transactionObj, long nativeObject, 193 InputWindowHandle handle); nativeTransferTouchFocus(long transactionObj, IBinder fromToken, IBinder toToken)194 private static native void nativeTransferTouchFocus(long transactionObj, IBinder fromToken, 195 IBinder toToken); nativeGetProtectedContentSupport()196 private static native boolean nativeGetProtectedContentSupport(); nativeSetMetadata(long transactionObj, long nativeObject, int key, Parcel data)197 private static native void nativeSetMetadata(long transactionObj, long nativeObject, int key, 198 Parcel data); nativeSyncInputWindows(long transactionObj)199 private static native void nativeSyncInputWindows(long transactionObj); nativeGetDisplayBrightnessSupport(IBinder displayToken)200 private static native boolean nativeGetDisplayBrightnessSupport(IBinder displayToken); nativeSetDisplayBrightness(IBinder displayToken, float brightness)201 private static native boolean nativeSetDisplayBrightness(IBinder displayToken, 202 float brightness); 203 204 private final CloseGuard mCloseGuard = CloseGuard.get(); 205 private String mName; 206 long mNativeObject; // package visibility only for Surface.java access 207 208 // TODO: Move this to native. 209 private final Object mSizeLock = new Object(); 210 @GuardedBy("mSizeLock") 211 private int mWidth; 212 @GuardedBy("mSizeLock") 213 private int mHeight; 214 215 static Transaction sGlobalTransaction; 216 static long sTransactionNestCount = 0; 217 218 /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */ 219 220 /** 221 * Surface creation flag: Surface is created hidden 222 * @hide 223 */ 224 @UnsupportedAppUsage 225 public static final int HIDDEN = 0x00000004; 226 227 /** 228 * Surface creation flag: The surface contains secure content, special 229 * measures will be taken to disallow the surface's content to be copied 230 * from another process. In particular, screenshots and VNC servers will 231 * be disabled, but other measures can take place, for instance the 232 * surface might not be hardware accelerated. 233 * @hide 234 */ 235 public static final int SECURE = 0x00000080; 236 237 /** 238 * Surface creation flag: Creates a surface where color components are interpreted 239 * as "non pre-multiplied" by their alpha channel. Of course this flag is 240 * meaningless for surfaces without an alpha channel. By default 241 * surfaces are pre-multiplied, which means that each color component is 242 * already multiplied by its alpha value. In this case the blending 243 * equation used is: 244 * <p> 245 * <code>DEST = SRC + DEST * (1-SRC_ALPHA)</code> 246 * <p> 247 * By contrast, non pre-multiplied surfaces use the following equation: 248 * <p> 249 * <code>DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)</code> 250 * <p> 251 * pre-multiplied surfaces must always be used if transparent pixels are 252 * composited on top of each-other into the surface. A pre-multiplied 253 * surface can never lower the value of the alpha component of a given 254 * pixel. 255 * <p> 256 * In some rare situations, a non pre-multiplied surface is preferable. 257 * @hide 258 */ 259 public static final int NON_PREMULTIPLIED = 0x00000100; 260 261 /** 262 * Surface creation flag: Indicates that the surface must be considered opaque, 263 * even if its pixel format contains an alpha channel. This can be useful if an 264 * application needs full RGBA 8888 support for instance but will 265 * still draw every pixel opaque. 266 * <p> 267 * This flag is ignored if setAlpha() is used to make the surface non-opaque. 268 * Combined effects are (assuming a buffer format with an alpha channel): 269 * <ul> 270 * <li>OPAQUE + alpha(1.0) == opaque composition 271 * <li>OPAQUE + alpha(0.x) == blended composition 272 * <li>!OPAQUE + alpha(1.0) == blended composition 273 * <li>!OPAQUE + alpha(0.x) == blended composition 274 * </ul> 275 * If the underlying buffer lacks an alpha channel, the OPAQUE flag is effectively 276 * set automatically. 277 * @hide 278 */ 279 public static final int OPAQUE = 0x00000400; 280 281 /** 282 * Surface creation flag: Application requires a hardware-protected path to an 283 * external display sink. If a hardware-protected path is not available, 284 * then this surface will not be displayed on the external sink. 285 * 286 * @hide 287 */ 288 public static final int PROTECTED_APP = 0x00000800; 289 290 // 0x1000 is reserved for an independent DRM protected flag in framework 291 292 /** 293 * Surface creation flag: Window represents a cursor glyph. 294 * @hide 295 */ 296 public static final int CURSOR_WINDOW = 0x00002000; 297 298 /** 299 * Surface creation flag: Creates a normal surface. 300 * This is the default. 301 * 302 * @hide 303 */ 304 public static final int FX_SURFACE_NORMAL = 0x00000000; 305 306 /** 307 * Surface creation flag: Creates a Dim surface. 308 * Everything behind this surface is dimmed by the amount specified 309 * in {@link #setAlpha}. It is an error to lock a Dim surface, since it 310 * doesn't have a backing store. 311 * 312 * @hide 313 */ 314 public static final int FX_SURFACE_DIM = 0x00020000; 315 316 /** 317 * Surface creation flag: Creates a container surface. 318 * This surface will have no buffers and will only be used 319 * as a container for other surfaces, or for its InputInfo. 320 * @hide 321 */ 322 public static final int FX_SURFACE_CONTAINER = 0x00080000; 323 324 /** 325 * Mask used for FX values above. 326 * 327 * @hide 328 */ 329 public static final int FX_SURFACE_MASK = 0x000F0000; 330 331 /* flags used with setFlags() (keep in sync with ISurfaceComposer.h) */ 332 333 /** 334 * Surface flag: Hide the surface. 335 * Equivalent to calling hide(). 336 * Updates the value set during Surface creation (see {@link #HIDDEN}). 337 */ 338 private static final int SURFACE_HIDDEN = 0x01; 339 340 /** 341 * Surface flag: composite without blending when possible. 342 * Updates the value set during Surface creation (see {@link #OPAQUE}). 343 */ 344 private static final int SURFACE_OPAQUE = 0x02; 345 346 // Display power modes. 347 /** 348 * Display power mode off: used while blanking the screen. 349 * Use only with {@link SurfaceControl#setDisplayPowerMode}. 350 * @hide 351 */ 352 public static final int POWER_MODE_OFF = 0; 353 354 /** 355 * Display power mode doze: used while putting the screen into low power mode. 356 * Use only with {@link SurfaceControl#setDisplayPowerMode}. 357 * @hide 358 */ 359 public static final int POWER_MODE_DOZE = 1; 360 361 /** 362 * Display power mode normal: used while unblanking the screen. 363 * Use only with {@link SurfaceControl#setDisplayPowerMode}. 364 * @hide 365 */ 366 public static final int POWER_MODE_NORMAL = 2; 367 368 /** 369 * Display power mode doze: used while putting the screen into a suspended 370 * low power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}. 371 * @hide 372 */ 373 public static final int POWER_MODE_DOZE_SUSPEND = 3; 374 375 /** 376 * Display power mode on: used while putting the screen into a suspended 377 * full power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}. 378 * @hide 379 */ 380 public static final int POWER_MODE_ON_SUSPEND = 4; 381 382 /** 383 * A value for windowType used to indicate that the window should be omitted from screenshots 384 * and display mirroring. A temporary workaround until we express such things with 385 * the hierarchy. 386 * TODO: b/64227542 387 * @hide 388 */ 389 public static final int WINDOW_TYPE_DONT_SCREENSHOT = 441731; 390 391 /** 392 * internal representation of how to interpret pixel value, used only to convert to ColorSpace. 393 */ 394 private static final int INTERNAL_DATASPACE_SRGB = 142671872; 395 private static final int INTERNAL_DATASPACE_DISPLAY_P3 = 143261696; 396 private static final int INTERNAL_DATASPACE_SCRGB = 411107328; 397 assignNativeObject(long nativeObject)398 private void assignNativeObject(long nativeObject) { 399 if (mNativeObject != 0) { 400 release(); 401 } 402 mNativeObject = nativeObject; 403 } 404 405 /** 406 * @hide 407 */ copyFrom(SurfaceControl other)408 public void copyFrom(SurfaceControl other) { 409 mName = other.mName; 410 mWidth = other.mWidth; 411 mHeight = other.mHeight; 412 assignNativeObject(nativeCopyFromSurfaceControl(other.mNativeObject)); 413 } 414 415 /** 416 * owner UID. 417 * @hide 418 */ 419 public static final int METADATA_OWNER_UID = 1; 420 421 /** 422 * Window type as per {@link WindowManager.LayoutParams}. 423 * @hide 424 */ 425 public static final int METADATA_WINDOW_TYPE = 2; 426 427 /** 428 * Task id to allow association between surfaces and task. 429 * @hide 430 */ 431 public static final int METADATA_TASK_ID = 3; 432 433 /** 434 * A wrapper around GraphicBuffer that contains extra information about how to 435 * interpret the screenshot GraphicBuffer. 436 * @hide 437 */ 438 public static class ScreenshotGraphicBuffer { 439 private final GraphicBuffer mGraphicBuffer; 440 private final ColorSpace mColorSpace; 441 private final boolean mContainsSecureLayers; 442 ScreenshotGraphicBuffer(GraphicBuffer graphicBuffer, ColorSpace colorSpace, boolean containsSecureLayers)443 public ScreenshotGraphicBuffer(GraphicBuffer graphicBuffer, ColorSpace colorSpace, 444 boolean containsSecureLayers) { 445 mGraphicBuffer = graphicBuffer; 446 mColorSpace = colorSpace; 447 mContainsSecureLayers = containsSecureLayers; 448 } 449 450 /** 451 * Create ScreenshotGraphicBuffer from existing native GraphicBuffer object. 452 * @param width The width in pixels of the buffer 453 * @param height The height in pixels of the buffer 454 * @param format The format of each pixel as specified in {@link PixelFormat} 455 * @param usage Hint indicating how the buffer will be used 456 * @param unwrappedNativeObject The native object of GraphicBuffer 457 * @param namedColorSpace Integer value of a named color space {@link ColorSpace.Named} 458 * @param containsSecureLayer Indicates whether this graphic buffer contains captured contents 459 * of secure layers, in which case the screenshot should not be persisted. 460 */ createFromNative(int width, int height, int format, int usage, long unwrappedNativeObject, int namedColorSpace, boolean containsSecureLayers)461 private static ScreenshotGraphicBuffer createFromNative(int width, int height, int format, 462 int usage, long unwrappedNativeObject, int namedColorSpace, 463 boolean containsSecureLayers) { 464 GraphicBuffer graphicBuffer = GraphicBuffer.createFromExisting(width, height, format, 465 usage, unwrappedNativeObject); 466 ColorSpace colorSpace = ColorSpace.get(ColorSpace.Named.values()[namedColorSpace]); 467 return new ScreenshotGraphicBuffer(graphicBuffer, colorSpace, containsSecureLayers); 468 } 469 getColorSpace()470 public ColorSpace getColorSpace() { 471 return mColorSpace; 472 } 473 getGraphicBuffer()474 public GraphicBuffer getGraphicBuffer() { 475 return mGraphicBuffer; 476 } 477 containsSecureLayers()478 public boolean containsSecureLayers() { 479 return mContainsSecureLayers; 480 } 481 } 482 483 /** 484 * Builder class for {@link SurfaceControl} objects. 485 * 486 * By default the surface will be hidden, and have "unset" bounds, meaning it can 487 * be as large as the bounds of its parent if a buffer or child so requires. 488 * 489 * It is necessary to set at least a name via {@link Builder#setName} 490 */ 491 public static class Builder { 492 private SurfaceSession mSession; 493 private int mFlags = HIDDEN; 494 private int mWidth; 495 private int mHeight; 496 private int mFormat = PixelFormat.OPAQUE; 497 private String mName; 498 private SurfaceControl mParent; 499 private SparseIntArray mMetadata; 500 501 /** 502 * Begin building a SurfaceControl with a given {@link SurfaceSession}. 503 * 504 * @param session The {@link SurfaceSession} with which to eventually construct the surface. 505 * @hide 506 */ Builder(SurfaceSession session)507 public Builder(SurfaceSession session) { 508 mSession = session; 509 } 510 511 /** 512 * Begin building a SurfaceControl. 513 */ Builder()514 public Builder() { 515 } 516 517 /** 518 * Construct a new {@link SurfaceControl} with the set parameters. The builder 519 * remains valid. 520 */ 521 @NonNull build()522 public SurfaceControl build() { 523 if (mWidth < 0 || mHeight < 0) { 524 throw new IllegalStateException( 525 "width and height must be positive or unset"); 526 } 527 if ((mWidth > 0 || mHeight > 0) && (isColorLayerSet() || isContainerLayerSet())) { 528 throw new IllegalStateException( 529 "Only buffer layers can set a valid buffer size."); 530 } 531 return new SurfaceControl( 532 mSession, mName, mWidth, mHeight, mFormat, mFlags, mParent, mMetadata); 533 } 534 535 /** 536 * Set a debugging-name for the SurfaceControl. 537 * 538 * @param name A name to identify the Surface in debugging. 539 */ 540 @NonNull setName(@onNull String name)541 public Builder setName(@NonNull String name) { 542 mName = name; 543 return this; 544 } 545 546 /** 547 * Set the initial size of the controlled surface's buffers in pixels. 548 * 549 * @param width The buffer width in pixels. 550 * @param height The buffer height in pixels. 551 */ 552 @NonNull setBufferSize(@ntRangefrom = 0) int width, @IntRange(from = 0) int height)553 public Builder setBufferSize(@IntRange(from = 0) int width, 554 @IntRange(from = 0) int height) { 555 if (width < 0 || height < 0) { 556 throw new IllegalArgumentException( 557 "width and height must be positive"); 558 } 559 mWidth = width; 560 mHeight = height; 561 // set this as a buffer layer since we are specifying a buffer size. 562 return setFlags(FX_SURFACE_NORMAL, FX_SURFACE_MASK); 563 } 564 565 /** 566 * Set the initial size of the controlled surface's buffers in pixels. 567 */ unsetBufferSize()568 private void unsetBufferSize() { 569 mWidth = 0; 570 mHeight = 0; 571 } 572 573 /** 574 * Set the pixel format of the controlled surface's buffers, using constants from 575 * {@link android.graphics.PixelFormat}. 576 */ 577 @NonNull setFormat(@ixelFormat.Format int format)578 public Builder setFormat(@PixelFormat.Format int format) { 579 mFormat = format; 580 return this; 581 } 582 583 /** 584 * Specify if the app requires a hardware-protected path to 585 * an external display sync. If protected content is enabled, but 586 * such a path is not available, then the controlled Surface will 587 * not be displayed. 588 * 589 * @param protectedContent Whether to require a protected sink. 590 * @hide 591 */ 592 @NonNull setProtected(boolean protectedContent)593 public Builder setProtected(boolean protectedContent) { 594 if (protectedContent) { 595 mFlags |= PROTECTED_APP; 596 } else { 597 mFlags &= ~PROTECTED_APP; 598 } 599 return this; 600 } 601 602 /** 603 * Specify whether the Surface contains secure content. If true, the system 604 * will prevent the surfaces content from being copied by another process. In 605 * particular screenshots and VNC servers will be disabled. This is however 606 * not a complete prevention of readback as {@link #setProtected}. 607 * @hide 608 */ 609 @NonNull setSecure(boolean secure)610 public Builder setSecure(boolean secure) { 611 if (secure) { 612 mFlags |= SECURE; 613 } else { 614 mFlags &= ~SECURE; 615 } 616 return this; 617 } 618 619 /** 620 * Indicates whether the surface must be considered opaque, 621 * even if its pixel format is set to translucent. This can be useful if an 622 * application needs full RGBA 8888 support for instance but will 623 * still draw every pixel opaque. 624 * <p> 625 * This flag only determines whether opacity will be sampled from the alpha channel. 626 * Plane-alpha from calls to setAlpha() can still result in blended composition 627 * regardless of the opaque setting. 628 * 629 * Combined effects are (assuming a buffer format with an alpha channel): 630 * <ul> 631 * <li>OPAQUE + alpha(1.0) == opaque composition 632 * <li>OPAQUE + alpha(0.x) == blended composition 633 * <li>OPAQUE + alpha(0.0) == no composition 634 * <li>!OPAQUE + alpha(1.0) == blended composition 635 * <li>!OPAQUE + alpha(0.x) == blended composition 636 * <li>!OPAQUE + alpha(0.0) == no composition 637 * </ul> 638 * If the underlying buffer lacks an alpha channel, it is as if setOpaque(true) 639 * were set automatically. 640 * @param opaque Whether the Surface is OPAQUE. 641 */ 642 @NonNull setOpaque(boolean opaque)643 public Builder setOpaque(boolean opaque) { 644 if (opaque) { 645 mFlags |= OPAQUE; 646 } else { 647 mFlags &= ~OPAQUE; 648 } 649 return this; 650 } 651 652 /** 653 * Set a parent surface for our new SurfaceControl. 654 * 655 * Child surfaces are constrained to the onscreen region of their parent. 656 * Furthermore they stack relatively in Z order, and inherit the transformation 657 * of the parent. 658 * 659 * @param parent The parent control. 660 */ 661 @NonNull setParent(@ullable SurfaceControl parent)662 public Builder setParent(@Nullable SurfaceControl parent) { 663 mParent = parent; 664 return this; 665 } 666 667 /** 668 * Sets a metadata int. 669 * 670 * @param key metadata key 671 * @param data associated data 672 * @hide 673 */ setMetadata(int key, int data)674 public Builder setMetadata(int key, int data) { 675 if (mMetadata == null) { 676 mMetadata = new SparseIntArray(); 677 } 678 mMetadata.put(key, data); 679 return this; 680 } 681 682 /** 683 * Indicate whether a 'ColorLayer' is to be constructed. 684 * 685 * Color layers will not have an associated BufferQueue and will instead always render a 686 * solid color (that is, solid before plane alpha). Currently that color is black. 687 * 688 * @hide 689 */ setColorLayer()690 public Builder setColorLayer() { 691 unsetBufferSize(); 692 return setFlags(FX_SURFACE_DIM, FX_SURFACE_MASK); 693 } 694 isColorLayerSet()695 private boolean isColorLayerSet() { 696 return (mFlags & FX_SURFACE_DIM) == FX_SURFACE_DIM; 697 } 698 699 /** 700 * Indicates whether a 'ContainerLayer' is to be constructed. 701 * 702 * Container layers will not be rendered in any fashion and instead are used 703 * as a parent of renderable layers. 704 * 705 * @hide 706 */ setContainerLayer()707 public Builder setContainerLayer() { 708 unsetBufferSize(); 709 return setFlags(FX_SURFACE_CONTAINER, FX_SURFACE_MASK); 710 } 711 isContainerLayerSet()712 private boolean isContainerLayerSet() { 713 return (mFlags & FX_SURFACE_CONTAINER) == FX_SURFACE_CONTAINER; 714 } 715 716 /** 717 * Set 'Surface creation flags' such as {@link #HIDDEN}, {@link #SECURE}. 718 * 719 * TODO: Finish conversion to individual builder methods? 720 * @param flags The combined flags 721 * @hide 722 */ setFlags(int flags)723 public Builder setFlags(int flags) { 724 mFlags = flags; 725 return this; 726 } 727 setFlags(int flags, int mask)728 private Builder setFlags(int flags, int mask) { 729 mFlags = (mFlags & ~mask) | flags; 730 return this; 731 } 732 } 733 734 /** 735 * Create a surface with a name. 736 * <p> 737 * The surface creation flags specify what kind of surface to create and 738 * certain options such as whether the surface can be assumed to be opaque 739 * and whether it should be initially hidden. Surfaces should always be 740 * created with the {@link #HIDDEN} flag set to ensure that they are not 741 * made visible prematurely before all of the surface's properties have been 742 * configured. 743 * <p> 744 * Good practice is to first create the surface with the {@link #HIDDEN} flag 745 * specified, open a transaction, set the surface layer, layer stack, alpha, 746 * and position, call {@link #show} if appropriate, and close the transaction. 747 * <p> 748 * Bounds of the surface is determined by its crop and its buffer size. If the 749 * surface has no buffer or crop, the surface is boundless and only constrained 750 * by the size of its parent bounds. 751 * 752 * @param session The surface session, must not be null. 753 * @param name The surface name, must not be null. 754 * @param w The surface initial width. 755 * @param h The surface initial height. 756 * @param flags The surface creation flags. Should always include {@link #HIDDEN} 757 * in the creation flags. 758 * @param metadata Initial metadata. 759 * 760 * @throws throws OutOfResourcesException If the SurfaceControl cannot be created. 761 */ SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags, SurfaceControl parent, SparseIntArray metadata)762 private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags, 763 SurfaceControl parent, SparseIntArray metadata) 764 throws OutOfResourcesException, IllegalArgumentException { 765 if (name == null) { 766 throw new IllegalArgumentException("name must not be null"); 767 } 768 769 if ((flags & SurfaceControl.HIDDEN) == 0) { 770 Log.w(TAG, "Surfaces should always be created with the HIDDEN flag set " 771 + "to ensure that they are not made visible prematurely before " 772 + "all of the surface's properties have been configured. " 773 + "Set the other properties and make the surface visible within " 774 + "a transaction. New surface name: " + name, 775 new Throwable()); 776 } 777 778 mName = name; 779 mWidth = w; 780 mHeight = h; 781 Parcel metaParcel = Parcel.obtain(); 782 try { 783 if (metadata != null && metadata.size() > 0) { 784 metaParcel.writeInt(metadata.size()); 785 for (int i = 0; i < metadata.size(); ++i) { 786 metaParcel.writeInt(metadata.keyAt(i)); 787 metaParcel.writeByteArray( 788 ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()) 789 .putInt(metadata.valueAt(i)).array()); 790 } 791 metaParcel.setDataPosition(0); 792 } 793 mNativeObject = nativeCreate(session, name, w, h, format, flags, 794 parent != null ? parent.mNativeObject : 0, metaParcel); 795 } finally { 796 metaParcel.recycle(); 797 } 798 if (mNativeObject == 0) { 799 throw new OutOfResourcesException( 800 "Couldn't allocate SurfaceControl native object"); 801 } 802 803 mCloseGuard.open("release"); 804 } 805 806 /** This is a transfer constructor, useful for transferring a live SurfaceControl native 807 * object to another Java wrapper which could have some different behavior, e.g. 808 * event logging. 809 * @hide 810 */ SurfaceControl(SurfaceControl other)811 public SurfaceControl(SurfaceControl other) { 812 mName = other.mName; 813 mWidth = other.mWidth; 814 mHeight = other.mHeight; 815 mNativeObject = other.mNativeObject; 816 other.mCloseGuard.close(); 817 other.mNativeObject = 0; 818 mCloseGuard.open("release"); 819 } 820 SurfaceControl(Parcel in)821 private SurfaceControl(Parcel in) { 822 readFromParcel(in); 823 mCloseGuard.open("release"); 824 } 825 826 /** 827 * @hide 828 */ SurfaceControl()829 public SurfaceControl() { 830 mCloseGuard.open("release"); 831 } 832 readFromParcel(Parcel in)833 public void readFromParcel(Parcel in) { 834 if (in == null) { 835 throw new IllegalArgumentException("source must not be null"); 836 } 837 838 mName = in.readString(); 839 mWidth = in.readInt(); 840 mHeight = in.readInt(); 841 842 long object = 0; 843 if (in.readInt() != 0) { 844 object = nativeReadFromParcel(in); 845 } 846 assignNativeObject(object); 847 } 848 849 @Override describeContents()850 public int describeContents() { 851 return 0; 852 } 853 854 @Override writeToParcel(Parcel dest, int flags)855 public void writeToParcel(Parcel dest, int flags) { 856 dest.writeString(mName); 857 dest.writeInt(mWidth); 858 dest.writeInt(mHeight); 859 if (mNativeObject == 0) { 860 dest.writeInt(0); 861 } else { 862 dest.writeInt(1); 863 } 864 nativeWriteToParcel(mNativeObject, dest); 865 866 if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) { 867 release(); 868 } 869 } 870 871 /** 872 * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link 873 * android.view.SurfaceControlProto}. 874 * 875 * @param proto Stream to write the SurfaceControl object to. 876 * @param fieldId Field Id of the SurfaceControl as defined in the parent message. 877 * @hide 878 */ writeToProto(ProtoOutputStream proto, long fieldId)879 public void writeToProto(ProtoOutputStream proto, long fieldId) { 880 final long token = proto.start(fieldId); 881 proto.write(HASH_CODE, System.identityHashCode(this)); 882 proto.write(NAME, mName); 883 proto.end(token); 884 } 885 886 public static final @android.annotation.NonNull Creator<SurfaceControl> CREATOR 887 = new Creator<SurfaceControl>() { 888 public SurfaceControl createFromParcel(Parcel in) { 889 return new SurfaceControl(in); 890 } 891 892 public SurfaceControl[] newArray(int size) { 893 return new SurfaceControl[size]; 894 } 895 }; 896 897 /** 898 * @hide 899 */ 900 @Override finalize()901 protected void finalize() throws Throwable { 902 try { 903 if (mCloseGuard != null) { 904 mCloseGuard.warnIfOpen(); 905 } 906 if (mNativeObject != 0) { 907 nativeRelease(mNativeObject); 908 } 909 } finally { 910 super.finalize(); 911 } 912 } 913 914 /** 915 * Release the local reference to the server-side surface. The surface 916 * may continue to exist on-screen as long as its parent continues 917 * to exist. To explicitly remove a surface from the screen use 918 * {@link Transaction#reparent} with a null-parent. After release, 919 * {@link #isValid} will return false and other methods will throw 920 * an exception. 921 * 922 * Always call release() when you're done with a SurfaceControl. 923 */ release()924 public void release() { 925 if (mNativeObject != 0) { 926 nativeRelease(mNativeObject); 927 mNativeObject = 0; 928 } 929 mCloseGuard.close(); 930 } 931 932 /** 933 * Release the local resources like {@link #release} but also 934 * remove the Surface from the screen. 935 * @hide 936 */ remove()937 public void remove() { 938 if (mNativeObject != 0) { 939 nativeDestroy(mNativeObject); 940 mNativeObject = 0; 941 } 942 mCloseGuard.close(); 943 } 944 945 /** 946 * Disconnect any client still connected to the surface. 947 * @hide 948 */ disconnect()949 public void disconnect() { 950 if (mNativeObject != 0) { 951 nativeDisconnect(mNativeObject); 952 } 953 } 954 checkNotReleased()955 private void checkNotReleased() { 956 if (mNativeObject == 0) throw new NullPointerException( 957 "mNativeObject is null. Have you called release() already?"); 958 } 959 960 /** 961 * Check whether this instance points to a valid layer with the system-compositor. For 962 * example this may be false if construction failed, or the layer was released 963 * ({@link #release}). 964 * 965 * @return Whether this SurfaceControl is valid. 966 */ isValid()967 public boolean isValid() { 968 return mNativeObject != 0; 969 } 970 971 /* 972 * set surface parameters. 973 * needs to be inside open/closeTransaction block 974 */ 975 976 /** start a transaction 977 * @hide 978 */ 979 @UnsupportedAppUsage openTransaction()980 public static void openTransaction() { 981 synchronized (SurfaceControl.class) { 982 if (sGlobalTransaction == null) { 983 sGlobalTransaction = new Transaction(); 984 } 985 synchronized(SurfaceControl.class) { 986 sTransactionNestCount++; 987 } 988 } 989 } 990 991 /** 992 * Merge the supplied transaction in to the deprecated "global" transaction. 993 * This clears the supplied transaction in an identical fashion to {@link Transaction#merge}. 994 * <p> 995 * This is a utility for interop with legacy-code and will go away with the Global Transaction. 996 * @hide 997 */ 998 @Deprecated mergeToGlobalTransaction(Transaction t)999 public static void mergeToGlobalTransaction(Transaction t) { 1000 synchronized(SurfaceControl.class) { 1001 sGlobalTransaction.merge(t); 1002 } 1003 } 1004 1005 /** end a transaction 1006 * @hide 1007 */ 1008 @UnsupportedAppUsage closeTransaction()1009 public static void closeTransaction() { 1010 synchronized(SurfaceControl.class) { 1011 if (sTransactionNestCount == 0) { 1012 Log.e(TAG, 1013 "Call to SurfaceControl.closeTransaction without matching openTransaction"); 1014 } else if (--sTransactionNestCount > 0) { 1015 return; 1016 } 1017 sGlobalTransaction.apply(); 1018 } 1019 } 1020 1021 /** 1022 * @hide 1023 */ deferTransactionUntil(IBinder handle, long frame)1024 public void deferTransactionUntil(IBinder handle, long frame) { 1025 synchronized(SurfaceControl.class) { 1026 sGlobalTransaction.deferTransactionUntil(this, handle, frame); 1027 } 1028 } 1029 1030 /** 1031 * @hide 1032 */ deferTransactionUntil(Surface barrier, long frame)1033 public void deferTransactionUntil(Surface barrier, long frame) { 1034 synchronized(SurfaceControl.class) { 1035 sGlobalTransaction.deferTransactionUntilSurface(this, barrier, frame); 1036 } 1037 } 1038 1039 /** 1040 * @hide 1041 */ reparentChildren(IBinder newParentHandle)1042 public void reparentChildren(IBinder newParentHandle) { 1043 synchronized(SurfaceControl.class) { 1044 sGlobalTransaction.reparentChildren(this, newParentHandle); 1045 } 1046 } 1047 1048 /** 1049 * @hide 1050 */ reparent(SurfaceControl newParent)1051 public void reparent(SurfaceControl newParent) { 1052 synchronized(SurfaceControl.class) { 1053 sGlobalTransaction.reparent(this, newParent); 1054 } 1055 } 1056 1057 /** 1058 * @hide 1059 */ detachChildren()1060 public void detachChildren() { 1061 synchronized(SurfaceControl.class) { 1062 sGlobalTransaction.detachChildren(this); 1063 } 1064 } 1065 1066 /** 1067 * @hide 1068 */ setOverrideScalingMode(int scalingMode)1069 public void setOverrideScalingMode(int scalingMode) { 1070 checkNotReleased(); 1071 synchronized(SurfaceControl.class) { 1072 sGlobalTransaction.setOverrideScalingMode(this, scalingMode); 1073 } 1074 } 1075 1076 /** 1077 * @hide 1078 */ getHandle()1079 public IBinder getHandle() { 1080 return nativeGetHandle(mNativeObject); 1081 } 1082 1083 /** 1084 * @hide 1085 */ setAnimationTransaction()1086 public static void setAnimationTransaction() { 1087 synchronized (SurfaceControl.class) { 1088 sGlobalTransaction.setAnimationTransaction(); 1089 } 1090 } 1091 1092 /** 1093 * @hide 1094 */ 1095 @UnsupportedAppUsage setLayer(int zorder)1096 public void setLayer(int zorder) { 1097 checkNotReleased(); 1098 synchronized(SurfaceControl.class) { 1099 sGlobalTransaction.setLayer(this, zorder); 1100 } 1101 } 1102 1103 /** 1104 * @hide 1105 */ setRelativeLayer(SurfaceControl relativeTo, int zorder)1106 public void setRelativeLayer(SurfaceControl relativeTo, int zorder) { 1107 checkNotReleased(); 1108 synchronized(SurfaceControl.class) { 1109 sGlobalTransaction.setRelativeLayer(this, relativeTo, zorder); 1110 } 1111 } 1112 1113 /** 1114 * @hide 1115 */ 1116 @UnsupportedAppUsage setPosition(float x, float y)1117 public void setPosition(float x, float y) { 1118 checkNotReleased(); 1119 synchronized(SurfaceControl.class) { 1120 sGlobalTransaction.setPosition(this, x, y); 1121 } 1122 } 1123 1124 /** 1125 * @hide 1126 */ setGeometryAppliesWithResize()1127 public void setGeometryAppliesWithResize() { 1128 checkNotReleased(); 1129 synchronized(SurfaceControl.class) { 1130 sGlobalTransaction.setGeometryAppliesWithResize(this); 1131 } 1132 } 1133 1134 /** 1135 * @hide 1136 */ setBufferSize(int w, int h)1137 public void setBufferSize(int w, int h) { 1138 checkNotReleased(); 1139 synchronized(SurfaceControl.class) { 1140 sGlobalTransaction.setBufferSize(this, w, h); 1141 } 1142 } 1143 1144 /** 1145 * @hide 1146 */ 1147 @UnsupportedAppUsage hide()1148 public void hide() { 1149 checkNotReleased(); 1150 synchronized(SurfaceControl.class) { 1151 sGlobalTransaction.hide(this); 1152 } 1153 } 1154 1155 /** 1156 * @hide 1157 */ 1158 @UnsupportedAppUsage show()1159 public void show() { 1160 checkNotReleased(); 1161 synchronized(SurfaceControl.class) { 1162 sGlobalTransaction.show(this); 1163 } 1164 } 1165 1166 /** 1167 * @hide 1168 */ setTransparentRegionHint(Region region)1169 public void setTransparentRegionHint(Region region) { 1170 checkNotReleased(); 1171 synchronized(SurfaceControl.class) { 1172 sGlobalTransaction.setTransparentRegionHint(this, region); 1173 } 1174 } 1175 1176 /** 1177 * @hide 1178 */ clearContentFrameStats()1179 public boolean clearContentFrameStats() { 1180 checkNotReleased(); 1181 return nativeClearContentFrameStats(mNativeObject); 1182 } 1183 1184 /** 1185 * @hide 1186 */ getContentFrameStats(WindowContentFrameStats outStats)1187 public boolean getContentFrameStats(WindowContentFrameStats outStats) { 1188 checkNotReleased(); 1189 return nativeGetContentFrameStats(mNativeObject, outStats); 1190 } 1191 1192 /** 1193 * @hide 1194 */ clearAnimationFrameStats()1195 public static boolean clearAnimationFrameStats() { 1196 return nativeClearAnimationFrameStats(); 1197 } 1198 1199 /** 1200 * @hide 1201 */ getAnimationFrameStats(WindowAnimationFrameStats outStats)1202 public static boolean getAnimationFrameStats(WindowAnimationFrameStats outStats) { 1203 return nativeGetAnimationFrameStats(outStats); 1204 } 1205 1206 /** 1207 * @hide 1208 */ setAlpha(float alpha)1209 public void setAlpha(float alpha) { 1210 checkNotReleased(); 1211 synchronized(SurfaceControl.class) { 1212 sGlobalTransaction.setAlpha(this, alpha); 1213 } 1214 } 1215 1216 /** 1217 * @hide 1218 */ setColor(@ize3) float[] color)1219 public void setColor(@Size(3) float[] color) { 1220 checkNotReleased(); 1221 synchronized (SurfaceControl.class) { 1222 sGlobalTransaction.setColor(this, color); 1223 } 1224 } 1225 1226 /** 1227 * @hide 1228 */ setMatrix(float dsdx, float dtdx, float dtdy, float dsdy)1229 public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) { 1230 checkNotReleased(); 1231 synchronized(SurfaceControl.class) { 1232 sGlobalTransaction.setMatrix(this, dsdx, dtdx, dtdy, dsdy); 1233 } 1234 } 1235 1236 /** 1237 * Sets the transform and position of a {@link SurfaceControl} from a 3x3 transformation matrix. 1238 * 1239 * @param matrix The matrix to apply. 1240 * @param float9 An array of 9 floats to be used to extract the values from the matrix. 1241 * @hide 1242 */ setMatrix(Matrix matrix, float[] float9)1243 public void setMatrix(Matrix matrix, float[] float9) { 1244 checkNotReleased(); 1245 matrix.getValues(float9); 1246 synchronized (SurfaceControl.class) { 1247 sGlobalTransaction.setMatrix(this, float9[MSCALE_X], float9[MSKEW_Y], 1248 float9[MSKEW_X], float9[MSCALE_Y]); 1249 sGlobalTransaction.setPosition(this, float9[MTRANS_X], float9[MTRANS_Y]); 1250 } 1251 } 1252 1253 /** 1254 * Sets the color transform for the Surface. 1255 * @param matrix A float array with 9 values represents a 3x3 transform matrix 1256 * @param translation A float array with 3 values represents a translation vector 1257 * @hide 1258 */ setColorTransform(@ize9) float[] matrix, @Size(3) float[] translation)1259 public void setColorTransform(@Size(9) float[] matrix, @Size(3) float[] translation) { 1260 checkNotReleased(); 1261 synchronized (SurfaceControl.class) { 1262 sGlobalTransaction.setColorTransform(this, matrix, translation); 1263 } 1264 } 1265 1266 /** 1267 * Sets the Surface to be color space agnostic. If a surface is color space agnostic, 1268 * the color can be interpreted in any color space. 1269 * @param agnostic A boolean to indicate whether the surface is color space agnostic 1270 * @hide 1271 */ setColorSpaceAgnostic(boolean agnostic)1272 public void setColorSpaceAgnostic(boolean agnostic) { 1273 checkNotReleased(); 1274 synchronized (SurfaceControl.class) { 1275 sGlobalTransaction.setColorSpaceAgnostic(this, agnostic); 1276 } 1277 } 1278 1279 /** 1280 * Bounds the surface and its children to the bounds specified. Size of the surface will be 1281 * ignored and only the crop and buffer size will be used to determine the bounds of the 1282 * surface. If no crop is specified and the surface has no buffer, the surface bounds is only 1283 * constrained by the size of its parent bounds. 1284 * 1285 * @param crop Bounds of the crop to apply. 1286 * @hide 1287 */ setWindowCrop(Rect crop)1288 public void setWindowCrop(Rect crop) { 1289 checkNotReleased(); 1290 synchronized (SurfaceControl.class) { 1291 sGlobalTransaction.setWindowCrop(this, crop); 1292 } 1293 } 1294 1295 /** 1296 * Same as {@link SurfaceControl#setWindowCrop(Rect)} but sets the crop rect top left at 0, 0. 1297 * 1298 * @param width width of crop rect 1299 * @param height height of crop rect 1300 * @hide 1301 */ setWindowCrop(int width, int height)1302 public void setWindowCrop(int width, int height) { 1303 checkNotReleased(); 1304 synchronized (SurfaceControl.class) { 1305 sGlobalTransaction.setWindowCrop(this, width, height); 1306 } 1307 } 1308 1309 /** 1310 * Sets the corner radius of a {@link SurfaceControl}. 1311 * 1312 * @param cornerRadius Corner radius in pixels. 1313 * @hide 1314 */ setCornerRadius(float cornerRadius)1315 public void setCornerRadius(float cornerRadius) { 1316 checkNotReleased(); 1317 synchronized (SurfaceControl.class) { 1318 sGlobalTransaction.setCornerRadius(this, cornerRadius); 1319 } 1320 } 1321 1322 /** 1323 * @hide 1324 */ setLayerStack(int layerStack)1325 public void setLayerStack(int layerStack) { 1326 checkNotReleased(); 1327 synchronized(SurfaceControl.class) { 1328 sGlobalTransaction.setLayerStack(this, layerStack); 1329 } 1330 } 1331 1332 /** 1333 * @hide 1334 */ setOpaque(boolean isOpaque)1335 public void setOpaque(boolean isOpaque) { 1336 checkNotReleased(); 1337 1338 synchronized (SurfaceControl.class) { 1339 sGlobalTransaction.setOpaque(this, isOpaque); 1340 } 1341 } 1342 1343 /** 1344 * @hide 1345 */ setSecure(boolean isSecure)1346 public void setSecure(boolean isSecure) { 1347 checkNotReleased(); 1348 1349 synchronized (SurfaceControl.class) { 1350 sGlobalTransaction.setSecure(this, isSecure); 1351 } 1352 } 1353 1354 /** 1355 * @hide 1356 */ getWidth()1357 public int getWidth() { 1358 synchronized (mSizeLock) { 1359 return mWidth; 1360 } 1361 } 1362 1363 /** 1364 * @hide 1365 */ getHeight()1366 public int getHeight() { 1367 synchronized (mSizeLock) { 1368 return mHeight; 1369 } 1370 } 1371 1372 @Override toString()1373 public String toString() { 1374 return "Surface(name=" + mName + ")/@0x" + 1375 Integer.toHexString(System.identityHashCode(this)); 1376 } 1377 1378 /* 1379 * set display parameters. 1380 * needs to be inside open/closeTransaction block 1381 */ 1382 1383 /** 1384 * Describes the properties of a physical display known to surface flinger. 1385 * @hide 1386 */ 1387 public static final class PhysicalDisplayInfo { 1388 /** 1389 * @hide 1390 */ 1391 @UnsupportedAppUsage 1392 public int width; 1393 1394 /** 1395 * @hide 1396 */ 1397 @UnsupportedAppUsage 1398 public int height; 1399 1400 /** 1401 * @hide 1402 */ 1403 @UnsupportedAppUsage 1404 public float refreshRate; 1405 1406 /** 1407 * @hide 1408 */ 1409 @UnsupportedAppUsage 1410 public float density; 1411 1412 /** 1413 * @hide 1414 */ 1415 @UnsupportedAppUsage 1416 public float xDpi; 1417 1418 /** 1419 * @hide 1420 */ 1421 @UnsupportedAppUsage 1422 public float yDpi; 1423 1424 /** 1425 * @hide 1426 */ 1427 @UnsupportedAppUsage 1428 public boolean secure; 1429 1430 /** 1431 * @hide 1432 */ 1433 @UnsupportedAppUsage 1434 public long appVsyncOffsetNanos; 1435 1436 /** 1437 * @hide 1438 */ 1439 @UnsupportedAppUsage 1440 public long presentationDeadlineNanos; 1441 1442 /** 1443 * @hide 1444 */ 1445 @UnsupportedAppUsage PhysicalDisplayInfo()1446 public PhysicalDisplayInfo() { 1447 } 1448 1449 /** 1450 * @hide 1451 */ PhysicalDisplayInfo(PhysicalDisplayInfo other)1452 public PhysicalDisplayInfo(PhysicalDisplayInfo other) { 1453 copyFrom(other); 1454 } 1455 1456 /** 1457 * @hide 1458 */ 1459 @Override equals(Object o)1460 public boolean equals(Object o) { 1461 return o instanceof PhysicalDisplayInfo && equals((PhysicalDisplayInfo)o); 1462 } 1463 1464 /** 1465 * @hide 1466 */ equals(PhysicalDisplayInfo other)1467 public boolean equals(PhysicalDisplayInfo other) { 1468 return other != null 1469 && width == other.width 1470 && height == other.height 1471 && refreshRate == other.refreshRate 1472 && density == other.density 1473 && xDpi == other.xDpi 1474 && yDpi == other.yDpi 1475 && secure == other.secure 1476 && appVsyncOffsetNanos == other.appVsyncOffsetNanos 1477 && presentationDeadlineNanos == other.presentationDeadlineNanos; 1478 } 1479 1480 /** 1481 * @hide 1482 */ 1483 @Override hashCode()1484 public int hashCode() { 1485 return 0; // don't care 1486 } 1487 1488 /** 1489 * @hide 1490 */ copyFrom(PhysicalDisplayInfo other)1491 public void copyFrom(PhysicalDisplayInfo other) { 1492 width = other.width; 1493 height = other.height; 1494 refreshRate = other.refreshRate; 1495 density = other.density; 1496 xDpi = other.xDpi; 1497 yDpi = other.yDpi; 1498 secure = other.secure; 1499 appVsyncOffsetNanos = other.appVsyncOffsetNanos; 1500 presentationDeadlineNanos = other.presentationDeadlineNanos; 1501 } 1502 1503 /** 1504 * @hide 1505 */ 1506 @Override toString()1507 public String toString() { 1508 return "PhysicalDisplayInfo{" + width + " x " + height + ", " + refreshRate + " fps, " 1509 + "density " + density + ", " + xDpi + " x " + yDpi + " dpi, secure " + secure 1510 + ", appVsyncOffset " + appVsyncOffsetNanos 1511 + ", bufferDeadline " + presentationDeadlineNanos + "}"; 1512 } 1513 } 1514 1515 /** 1516 * @hide 1517 */ setDisplayPowerMode(IBinder displayToken, int mode)1518 public static void setDisplayPowerMode(IBinder displayToken, int mode) { 1519 if (displayToken == null) { 1520 throw new IllegalArgumentException("displayToken must not be null"); 1521 } 1522 nativeSetDisplayPowerMode(displayToken, mode); 1523 } 1524 1525 /** 1526 * @hide 1527 */ 1528 @UnsupportedAppUsage getDisplayConfigs(IBinder displayToken)1529 public static SurfaceControl.PhysicalDisplayInfo[] getDisplayConfigs(IBinder displayToken) { 1530 if (displayToken == null) { 1531 throw new IllegalArgumentException("displayToken must not be null"); 1532 } 1533 return nativeGetDisplayConfigs(displayToken); 1534 } 1535 1536 /** 1537 * @hide 1538 */ getActiveConfig(IBinder displayToken)1539 public static int getActiveConfig(IBinder displayToken) { 1540 if (displayToken == null) { 1541 throw new IllegalArgumentException("displayToken must not be null"); 1542 } 1543 return nativeGetActiveConfig(displayToken); 1544 } 1545 1546 /** 1547 * @hide 1548 */ getDisplayedContentSamplingAttributes( IBinder displayToken)1549 public static DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes( 1550 IBinder displayToken) { 1551 if (displayToken == null) { 1552 throw new IllegalArgumentException("displayToken must not be null"); 1553 } 1554 return nativeGetDisplayedContentSamplingAttributes(displayToken); 1555 } 1556 1557 /** 1558 * @hide 1559 */ setDisplayedContentSamplingEnabled( IBinder displayToken, boolean enable, int componentMask, int maxFrames)1560 public static boolean setDisplayedContentSamplingEnabled( 1561 IBinder displayToken, boolean enable, int componentMask, int maxFrames) { 1562 if (displayToken == null) { 1563 throw new IllegalArgumentException("displayToken must not be null"); 1564 } 1565 final int maxColorComponents = 4; 1566 if ((componentMask >> maxColorComponents) != 0) { 1567 throw new IllegalArgumentException("invalid componentMask when enabling sampling"); 1568 } 1569 return nativeSetDisplayedContentSamplingEnabled( 1570 displayToken, enable, componentMask, maxFrames); 1571 } 1572 1573 /** 1574 * @hide 1575 */ getDisplayedContentSample( IBinder displayToken, long maxFrames, long timestamp)1576 public static DisplayedContentSample getDisplayedContentSample( 1577 IBinder displayToken, long maxFrames, long timestamp) { 1578 if (displayToken == null) { 1579 throw new IllegalArgumentException("displayToken must not be null"); 1580 } 1581 return nativeGetDisplayedContentSample(displayToken, maxFrames, timestamp); 1582 } 1583 1584 1585 /** 1586 * @hide 1587 */ setActiveConfig(IBinder displayToken, int id)1588 public static boolean setActiveConfig(IBinder displayToken, int id) { 1589 if (displayToken == null) { 1590 throw new IllegalArgumentException("displayToken must not be null"); 1591 } 1592 return nativeSetActiveConfig(displayToken, id); 1593 } 1594 1595 /** 1596 * @hide 1597 */ setAllowedDisplayConfigs(IBinder displayToken, int[] allowedConfigs)1598 public static boolean setAllowedDisplayConfigs(IBinder displayToken, int[] allowedConfigs) { 1599 if (displayToken == null) { 1600 throw new IllegalArgumentException("displayToken must not be null"); 1601 } 1602 if (allowedConfigs == null) { 1603 throw new IllegalArgumentException("allowedConfigs must not be null"); 1604 } 1605 1606 return nativeSetAllowedDisplayConfigs(displayToken, allowedConfigs); 1607 } 1608 1609 /** 1610 * @hide 1611 */ getAllowedDisplayConfigs(IBinder displayToken)1612 public static int[] getAllowedDisplayConfigs(IBinder displayToken) { 1613 if (displayToken == null) { 1614 throw new IllegalArgumentException("displayToken must not be null"); 1615 } 1616 return nativeGetAllowedDisplayConfigs(displayToken); 1617 } 1618 1619 /** 1620 * @hide 1621 */ getDisplayColorModes(IBinder displayToken)1622 public static int[] getDisplayColorModes(IBinder displayToken) { 1623 if (displayToken == null) { 1624 throw new IllegalArgumentException("displayToken must not be null"); 1625 } 1626 return nativeGetDisplayColorModes(displayToken); 1627 } 1628 1629 /** 1630 * Color coordinates in CIE1931 XYZ color space 1631 * 1632 * @hide 1633 */ 1634 public static final class CieXyz { 1635 /** 1636 * @hide 1637 */ 1638 public float X; 1639 1640 /** 1641 * @hide 1642 */ 1643 public float Y; 1644 1645 /** 1646 * @hide 1647 */ 1648 public float Z; 1649 } 1650 1651 /** 1652 * Contains a display's color primaries 1653 * 1654 * @hide 1655 */ 1656 public static final class DisplayPrimaries { 1657 /** 1658 * @hide 1659 */ 1660 public CieXyz red; 1661 1662 /** 1663 * @hide 1664 */ 1665 public CieXyz green; 1666 1667 /** 1668 * @hide 1669 */ 1670 public CieXyz blue; 1671 1672 /** 1673 * @hide 1674 */ 1675 public CieXyz white; 1676 1677 /** 1678 * @hide 1679 */ DisplayPrimaries()1680 public DisplayPrimaries() { 1681 } 1682 } 1683 1684 /** 1685 * @hide 1686 */ getDisplayNativePrimaries( IBinder displayToken)1687 public static SurfaceControl.DisplayPrimaries getDisplayNativePrimaries( 1688 IBinder displayToken) { 1689 if (displayToken == null) { 1690 throw new IllegalArgumentException("displayToken must not be null"); 1691 } 1692 1693 return nativeGetDisplayNativePrimaries(displayToken); 1694 } 1695 1696 /** 1697 * @hide 1698 */ getActiveColorMode(IBinder displayToken)1699 public static int getActiveColorMode(IBinder displayToken) { 1700 if (displayToken == null) { 1701 throw new IllegalArgumentException("displayToken must not be null"); 1702 } 1703 return nativeGetActiveColorMode(displayToken); 1704 } 1705 1706 /** 1707 * @hide 1708 */ setActiveColorMode(IBinder displayToken, int colorMode)1709 public static boolean setActiveColorMode(IBinder displayToken, int colorMode) { 1710 if (displayToken == null) { 1711 throw new IllegalArgumentException("displayToken must not be null"); 1712 } 1713 return nativeSetActiveColorMode(displayToken, colorMode); 1714 } 1715 1716 /** 1717 * Returns an array of color spaces with 2 elements. The first color space is the 1718 * default color space and second one is wide color gamut color space. 1719 * @hide 1720 */ getCompositionColorSpaces()1721 public static ColorSpace[] getCompositionColorSpaces() { 1722 int[] dataspaces = nativeGetCompositionDataspaces(); 1723 ColorSpace srgb = ColorSpace.get(ColorSpace.Named.SRGB); 1724 ColorSpace[] colorSpaces = { srgb, srgb }; 1725 if (dataspaces.length == 2) { 1726 for (int i = 0; i < 2; ++i) { 1727 switch(dataspaces[i]) { 1728 case INTERNAL_DATASPACE_DISPLAY_P3: 1729 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.DISPLAY_P3); 1730 break; 1731 case INTERNAL_DATASPACE_SCRGB: 1732 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.EXTENDED_SRGB); 1733 break; 1734 case INTERNAL_DATASPACE_SRGB: 1735 // Other dataspace is not recognized, use SRGB color space instead, 1736 // the default value of the array is already SRGB, thus do nothing. 1737 default: 1738 break; 1739 } 1740 } 1741 } 1742 return colorSpaces; 1743 } 1744 1745 /** 1746 * @hide 1747 */ 1748 @UnsupportedAppUsage setDisplayProjection(IBinder displayToken, int orientation, Rect layerStackRect, Rect displayRect)1749 public static void setDisplayProjection(IBinder displayToken, 1750 int orientation, Rect layerStackRect, Rect displayRect) { 1751 synchronized (SurfaceControl.class) { 1752 sGlobalTransaction.setDisplayProjection(displayToken, orientation, 1753 layerStackRect, displayRect); 1754 } 1755 } 1756 1757 /** 1758 * @hide 1759 */ 1760 @UnsupportedAppUsage setDisplayLayerStack(IBinder displayToken, int layerStack)1761 public static void setDisplayLayerStack(IBinder displayToken, int layerStack) { 1762 synchronized (SurfaceControl.class) { 1763 sGlobalTransaction.setDisplayLayerStack(displayToken, layerStack); 1764 } 1765 } 1766 1767 /** 1768 * @hide 1769 */ 1770 @UnsupportedAppUsage setDisplaySurface(IBinder displayToken, Surface surface)1771 public static void setDisplaySurface(IBinder displayToken, Surface surface) { 1772 synchronized (SurfaceControl.class) { 1773 sGlobalTransaction.setDisplaySurface(displayToken, surface); 1774 } 1775 } 1776 1777 /** 1778 * @hide 1779 */ setDisplaySize(IBinder displayToken, int width, int height)1780 public static void setDisplaySize(IBinder displayToken, int width, int height) { 1781 synchronized (SurfaceControl.class) { 1782 sGlobalTransaction.setDisplaySize(displayToken, width, height); 1783 } 1784 } 1785 1786 /** 1787 * @hide 1788 */ getHdrCapabilities(IBinder displayToken)1789 public static Display.HdrCapabilities getHdrCapabilities(IBinder displayToken) { 1790 if (displayToken == null) { 1791 throw new IllegalArgumentException("displayToken must not be null"); 1792 } 1793 return nativeGetHdrCapabilities(displayToken); 1794 } 1795 1796 /** 1797 * @hide 1798 */ 1799 @UnsupportedAppUsage createDisplay(String name, boolean secure)1800 public static IBinder createDisplay(String name, boolean secure) { 1801 if (name == null) { 1802 throw new IllegalArgumentException("name must not be null"); 1803 } 1804 return nativeCreateDisplay(name, secure); 1805 } 1806 1807 /** 1808 * @hide 1809 */ 1810 @UnsupportedAppUsage destroyDisplay(IBinder displayToken)1811 public static void destroyDisplay(IBinder displayToken) { 1812 if (displayToken == null) { 1813 throw new IllegalArgumentException("displayToken must not be null"); 1814 } 1815 nativeDestroyDisplay(displayToken); 1816 } 1817 1818 /** 1819 * @hide 1820 */ getPhysicalDisplayIds()1821 public static long[] getPhysicalDisplayIds() { 1822 return nativeGetPhysicalDisplayIds(); 1823 } 1824 1825 /** 1826 * @hide 1827 */ getPhysicalDisplayToken(long physicalDisplayId)1828 public static IBinder getPhysicalDisplayToken(long physicalDisplayId) { 1829 return nativeGetPhysicalDisplayToken(physicalDisplayId); 1830 } 1831 1832 /** 1833 * TODO(116025192): Remove this stopgap once framework is display-agnostic. 1834 * 1835 * @hide 1836 */ getInternalDisplayToken()1837 public static IBinder getInternalDisplayToken() { 1838 final long[] physicalDisplayIds = getPhysicalDisplayIds(); 1839 if (physicalDisplayIds.length == 0) { 1840 return null; 1841 } 1842 return getPhysicalDisplayToken(physicalDisplayIds[0]); 1843 } 1844 1845 /** 1846 * @see SurfaceControl#screenshot(IBinder, Surface, Rect, int, int, boolean, int) 1847 * @hide 1848 */ screenshot(IBinder display, Surface consumer)1849 public static void screenshot(IBinder display, Surface consumer) { 1850 screenshot(display, consumer, new Rect(), 0, 0, false, 0); 1851 } 1852 1853 /** 1854 * Copy the current screen contents into the provided {@link Surface} 1855 * 1856 * @param consumer The {@link Surface} to take the screenshot into. 1857 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int) 1858 * @hide 1859 */ screenshot(IBinder display, Surface consumer, Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation)1860 public static void screenshot(IBinder display, Surface consumer, Rect sourceCrop, int width, 1861 int height, boolean useIdentityTransform, int rotation) { 1862 if (consumer == null) { 1863 throw new IllegalArgumentException("consumer must not be null"); 1864 } 1865 1866 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(display, sourceCrop, width, 1867 height, useIdentityTransform, rotation); 1868 try { 1869 consumer.attachAndQueueBuffer(buffer.getGraphicBuffer()); 1870 } catch (RuntimeException e) { 1871 Log.w(TAG, "Failed to take screenshot - " + e.getMessage()); 1872 } 1873 } 1874 1875 /** 1876 * @see SurfaceControl#screenshot(Rect, int, int, boolean, int)} 1877 * @hide 1878 */ 1879 @UnsupportedAppUsage screenshot(Rect sourceCrop, int width, int height, int rotation)1880 public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) { 1881 return screenshot(sourceCrop, width, height, false, rotation); 1882 } 1883 1884 /** 1885 * Copy the current screen contents into a hardware bitmap and return it. 1886 * Note: If you want to modify the Bitmap in software, you will need to copy the Bitmap into 1887 * a software Bitmap using {@link Bitmap#copy(Bitmap.Config, boolean)} 1888 * 1889 * CAVEAT: Versions of screenshot that return a {@link Bitmap} can be extremely slow; avoid use 1890 * unless absolutely necessary; prefer the versions that use a {@link Surface} such as 1891 * {@link SurfaceControl#screenshot(IBinder, Surface)} or {@link GraphicBuffer} such as 1892 * {@link SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}. 1893 * 1894 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)} 1895 * @hide 1896 */ 1897 @UnsupportedAppUsage screenshot(Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation)1898 public static Bitmap screenshot(Rect sourceCrop, int width, int height, 1899 boolean useIdentityTransform, int rotation) { 1900 // TODO: should take the display as a parameter 1901 final IBinder displayToken = SurfaceControl.getInternalDisplayToken(); 1902 if (displayToken == null) { 1903 Log.w(TAG, "Failed to take screenshot because internal display is disconnected"); 1904 return null; 1905 } 1906 1907 if (rotation == ROTATION_90 || rotation == ROTATION_270) { 1908 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90; 1909 } 1910 1911 SurfaceControl.rotateCropForSF(sourceCrop, rotation); 1912 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(displayToken, sourceCrop, width, 1913 height, useIdentityTransform, rotation); 1914 1915 if (buffer == null) { 1916 Log.w(TAG, "Failed to take screenshot"); 1917 return null; 1918 } 1919 return Bitmap.wrapHardwareBuffer(buffer.getGraphicBuffer(), buffer.getColorSpace()); 1920 } 1921 1922 /** 1923 * Captures all the surfaces in a display and returns a {@link GraphicBuffer} with the content. 1924 * 1925 * @param display The display to take the screenshot of. 1926 * @param sourceCrop The portion of the screen to capture into the Bitmap; caller may 1927 * pass in 'new Rect()' if no cropping is desired. 1928 * @param width The desired width of the returned bitmap; the raw screen will be 1929 * scaled down to this size; caller may pass in 0 if no scaling is 1930 * desired. 1931 * @param height The desired height of the returned bitmap; the raw screen will 1932 * be scaled down to this size; caller may pass in 0 if no scaling 1933 * is desired. 1934 * @param useIdentityTransform Replace whatever transformation (rotation, scaling, translation) 1935 * the surface layers are currently using with the identity 1936 * transformation while taking the screenshot. 1937 * @param rotation Apply a custom clockwise rotation to the screenshot, i.e. 1938 * Surface.ROTATION_0,90,180,270. SurfaceFlinger will always take 1939 * screenshots in its native portrait orientation by default, so 1940 * this is useful for returning screenshots that are independent of 1941 * device orientation. 1942 * @return Returns a GraphicBuffer that contains the captured content. 1943 * @hide 1944 */ screenshotToBuffer(IBinder display, Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation)1945 public static ScreenshotGraphicBuffer screenshotToBuffer(IBinder display, Rect sourceCrop, 1946 int width, int height, boolean useIdentityTransform, int rotation) { 1947 if (display == null) { 1948 throw new IllegalArgumentException("displayToken must not be null"); 1949 } 1950 1951 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation, 1952 false /* captureSecureLayers */); 1953 } 1954 1955 /** 1956 * Like screenshotToBuffer, but if the caller is AID_SYSTEM, allows 1957 * for the capture of secure layers. This is used for the screen rotation 1958 * animation where the system server takes screenshots but does 1959 * not persist them or allow them to leave the server. However in other 1960 * cases in the system server, we mostly want to omit secure layers 1961 * like when we take a screenshot on behalf of the assistant. 1962 * 1963 * @hide 1964 */ screenshotToBufferWithSecureLayersUnsafe(IBinder display, Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation)1965 public static ScreenshotGraphicBuffer screenshotToBufferWithSecureLayersUnsafe(IBinder display, 1966 Rect sourceCrop, int width, int height, boolean useIdentityTransform, 1967 int rotation) { 1968 if (display == null) { 1969 throw new IllegalArgumentException("displayToken must not be null"); 1970 } 1971 1972 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation, 1973 true /* captureSecureLayers */); 1974 } 1975 rotateCropForSF(Rect crop, int rot)1976 private static void rotateCropForSF(Rect crop, int rot) { 1977 if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270) { 1978 int tmp = crop.top; 1979 crop.top = crop.left; 1980 crop.left = tmp; 1981 tmp = crop.right; 1982 crop.right = crop.bottom; 1983 crop.bottom = tmp; 1984 } 1985 } 1986 1987 /** 1988 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content. 1989 * 1990 * @param layerHandleToken The root layer to capture. 1991 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new 1992 * Rect()' or null if no cropping is desired. 1993 * @param frameScale The desired scale of the returned buffer; the raw 1994 * screen will be scaled up/down. 1995 * 1996 * @return Returns a GraphicBuffer that contains the layer capture. 1997 * @hide 1998 */ captureLayers(IBinder layerHandleToken, Rect sourceCrop, float frameScale)1999 public static ScreenshotGraphicBuffer captureLayers(IBinder layerHandleToken, Rect sourceCrop, 2000 float frameScale) { 2001 final IBinder displayToken = SurfaceControl.getInternalDisplayToken(); 2002 return nativeCaptureLayers(displayToken, layerHandleToken, sourceCrop, frameScale, null); 2003 } 2004 2005 /** 2006 * Like {@link captureLayers} but with an array of layer handles to exclude. 2007 * @hide 2008 */ captureLayersExcluding(IBinder layerHandleToken, Rect sourceCrop, float frameScale, IBinder[] exclude)2009 public static ScreenshotGraphicBuffer captureLayersExcluding(IBinder layerHandleToken, 2010 Rect sourceCrop, float frameScale, IBinder[] exclude) { 2011 final IBinder displayToken = SurfaceControl.getInternalDisplayToken(); 2012 return nativeCaptureLayers(displayToken, layerHandleToken, sourceCrop, frameScale, exclude); 2013 } 2014 2015 /** 2016 * Returns whether protected content is supported in GPU composition. 2017 * @hide 2018 */ getProtectedContentSupport()2019 public static boolean getProtectedContentSupport() { 2020 return nativeGetProtectedContentSupport(); 2021 } 2022 2023 /** 2024 * Returns whether brightness operations are supported on a display. 2025 * 2026 * @param displayToken 2027 * The token for the display. 2028 * 2029 * @return Whether brightness operations are supported on the display. 2030 * 2031 * @hide 2032 */ getDisplayBrightnessSupport(IBinder displayToken)2033 public static boolean getDisplayBrightnessSupport(IBinder displayToken) { 2034 return nativeGetDisplayBrightnessSupport(displayToken); 2035 } 2036 2037 /** 2038 * Sets the brightness of a display. 2039 * 2040 * @param displayToken 2041 * The token for the display whose brightness is set. 2042 * @param brightness 2043 * A number between 0.0f (minimum brightness) and 1.0f (maximum brightness), or -1.0f to 2044 * turn the backlight off. 2045 * 2046 * @return Whether the method succeeded or not. 2047 * 2048 * @throws IllegalArgumentException if: 2049 * - displayToken is null; 2050 * - brightness is NaN or greater than 1.0f. 2051 * 2052 * @hide 2053 */ setDisplayBrightness(IBinder displayToken, float brightness)2054 public static boolean setDisplayBrightness(IBinder displayToken, float brightness) { 2055 Objects.requireNonNull(displayToken); 2056 if (Float.isNaN(brightness) || brightness > 1.0f 2057 || (brightness < 0.0f && brightness != -1.0f)) { 2058 throw new IllegalArgumentException("brightness must be a number between 0.0f and 1.0f," 2059 + " or -1 to turn the backlight off."); 2060 } 2061 return nativeSetDisplayBrightness(displayToken, brightness); 2062 } 2063 2064 /** 2065 * An atomic set of changes to a set of SurfaceControl. 2066 */ 2067 public static class Transaction implements Closeable { 2068 /** 2069 * @hide 2070 */ 2071 public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry( 2072 Transaction.class.getClassLoader(), 2073 nativeGetNativeTransactionFinalizer(), 512); 2074 private long mNativeObject; 2075 2076 private final ArrayMap<SurfaceControl, Point> mResizedSurfaces = new ArrayMap<>(); 2077 Runnable mFreeNativeResources; 2078 2079 /** 2080 * Open a new transaction object. The transaction may be filed with commands to 2081 * manipulate {@link SurfaceControl} instances, and then applied atomically with 2082 * {@link #apply}. Eventually the user should invoke {@link #close}, when the object 2083 * is no longer required. Note however that re-using a transaction after a call to apply 2084 * is allowed as a convenience. 2085 */ Transaction()2086 public Transaction() { 2087 mNativeObject = nativeCreateTransaction(); 2088 mFreeNativeResources 2089 = sRegistry.registerNativeAllocation(this, mNativeObject); 2090 } 2091 2092 /** 2093 * Apply the transaction, clearing it's state, and making it usable 2094 * as a new transaction. 2095 */ apply()2096 public void apply() { 2097 apply(false); 2098 } 2099 2100 /** 2101 * Release the native transaction object, without applying it. 2102 */ 2103 @Override close()2104 public void close() { 2105 mFreeNativeResources.run(); 2106 mNativeObject = 0; 2107 } 2108 2109 /** 2110 * Jankier version of apply. Avoid use (b/28068298). 2111 * @hide 2112 */ apply(boolean sync)2113 public void apply(boolean sync) { 2114 applyResizedSurfaces(); 2115 nativeApplyTransaction(mNativeObject, sync); 2116 } 2117 applyResizedSurfaces()2118 private void applyResizedSurfaces() { 2119 for (int i = mResizedSurfaces.size() - 1; i >= 0; i--) { 2120 final Point size = mResizedSurfaces.valueAt(i); 2121 final SurfaceControl surfaceControl = mResizedSurfaces.keyAt(i); 2122 synchronized (surfaceControl.mSizeLock) { 2123 surfaceControl.mWidth = size.x; 2124 surfaceControl.mHeight = size.y; 2125 } 2126 } 2127 mResizedSurfaces.clear(); 2128 } 2129 2130 /** 2131 * Toggle the visibility of a given Layer and it's sub-tree. 2132 * 2133 * @param sc The SurfaceControl for which to set the visibility 2134 * @param visible The new visibility 2135 * @return This transaction object. 2136 */ 2137 @NonNull setVisibility(@onNull SurfaceControl sc, boolean visible)2138 public Transaction setVisibility(@NonNull SurfaceControl sc, boolean visible) { 2139 sc.checkNotReleased(); 2140 if (visible) { 2141 return show(sc); 2142 } else { 2143 return hide(sc); 2144 } 2145 } 2146 2147 /** 2148 * Request that a given surface and it's sub-tree be shown. 2149 * 2150 * @param sc The surface to show. 2151 * @return This transaction. 2152 * @hide 2153 */ 2154 @UnsupportedAppUsage show(SurfaceControl sc)2155 public Transaction show(SurfaceControl sc) { 2156 sc.checkNotReleased(); 2157 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_HIDDEN); 2158 return this; 2159 } 2160 2161 /** 2162 * Request that a given surface and it's sub-tree be hidden. 2163 * 2164 * @param sc The surface to hidden. 2165 * @return This transaction. 2166 * @hide 2167 */ 2168 @UnsupportedAppUsage hide(SurfaceControl sc)2169 public Transaction hide(SurfaceControl sc) { 2170 sc.checkNotReleased(); 2171 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_HIDDEN, SURFACE_HIDDEN); 2172 return this; 2173 } 2174 2175 /** 2176 * @hide 2177 */ 2178 @UnsupportedAppUsage setPosition(SurfaceControl sc, float x, float y)2179 public Transaction setPosition(SurfaceControl sc, float x, float y) { 2180 sc.checkNotReleased(); 2181 nativeSetPosition(mNativeObject, sc.mNativeObject, x, y); 2182 return this; 2183 } 2184 2185 /** 2186 * Set the default buffer size for the SurfaceControl, if there is a 2187 * {@link Surface} associated with the control, then 2188 * this will be the default size for buffers dequeued from it. 2189 * @param sc The surface to set the buffer size for. 2190 * @param w The default width 2191 * @param h The default height 2192 * @return This Transaction 2193 */ 2194 @NonNull setBufferSize(@onNull SurfaceControl sc, @IntRange(from = 0) int w, @IntRange(from = 0) int h)2195 public Transaction setBufferSize(@NonNull SurfaceControl sc, 2196 @IntRange(from = 0) int w, @IntRange(from = 0) int h) { 2197 sc.checkNotReleased(); 2198 mResizedSurfaces.put(sc, new Point(w, h)); 2199 nativeSetSize(mNativeObject, sc.mNativeObject, w, h); 2200 return this; 2201 } 2202 2203 /** 2204 * Set the Z-order for a given SurfaceControl, relative to it's siblings. 2205 * If two siblings share the same Z order the ordering is undefined. Surfaces 2206 * with a negative Z will be placed below the parent surface. 2207 * 2208 * @param sc The SurfaceControl to set the Z order on 2209 * @param z The Z-order 2210 * @return This Transaction. 2211 */ 2212 @NonNull setLayer(@onNull SurfaceControl sc, @IntRange(from = Integer.MIN_VALUE, to = Integer.MAX_VALUE) int z)2213 public Transaction setLayer(@NonNull SurfaceControl sc, 2214 @IntRange(from = Integer.MIN_VALUE, to = Integer.MAX_VALUE) int z) { 2215 sc.checkNotReleased(); 2216 nativeSetLayer(mNativeObject, sc.mNativeObject, z); 2217 return this; 2218 } 2219 2220 /** 2221 * @hide 2222 */ setRelativeLayer(SurfaceControl sc, SurfaceControl relativeTo, int z)2223 public Transaction setRelativeLayer(SurfaceControl sc, SurfaceControl relativeTo, int z) { 2224 sc.checkNotReleased(); 2225 nativeSetRelativeLayer(mNativeObject, sc.mNativeObject, 2226 relativeTo.getHandle(), z); 2227 return this; 2228 } 2229 2230 /** 2231 * @hide 2232 */ setTransparentRegionHint(SurfaceControl sc, Region transparentRegion)2233 public Transaction setTransparentRegionHint(SurfaceControl sc, Region transparentRegion) { 2234 sc.checkNotReleased(); 2235 nativeSetTransparentRegionHint(mNativeObject, 2236 sc.mNativeObject, transparentRegion); 2237 return this; 2238 } 2239 2240 /** 2241 * Set the alpha for a given surface. If the alpha is non-zero the SurfaceControl 2242 * will be blended with the Surfaces under it according to the specified ratio. 2243 * 2244 * @param sc The given SurfaceControl. 2245 * @param alpha The alpha to set. 2246 */ 2247 @NonNull setAlpha(@onNull SurfaceControl sc, @FloatRange(from = 0.0, to = 1.0) float alpha)2248 public Transaction setAlpha(@NonNull SurfaceControl sc, 2249 @FloatRange(from = 0.0, to = 1.0) float alpha) { 2250 sc.checkNotReleased(); 2251 nativeSetAlpha(mNativeObject, sc.mNativeObject, alpha); 2252 return this; 2253 } 2254 2255 /** 2256 * @hide 2257 */ setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle)2258 public Transaction setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle) { 2259 sc.checkNotReleased(); 2260 nativeSetInputWindowInfo(mNativeObject, sc.mNativeObject, handle); 2261 return this; 2262 } 2263 2264 /** 2265 * Transfers touch focus from one window to another. It is possible for multiple windows to 2266 * have touch focus if they support split touch dispatch 2267 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this 2268 * method only transfers touch focus of the specified window without affecting 2269 * other windows that may also have touch focus at the same time. 2270 * @param fromToken The token of a window that currently has touch focus. 2271 * @param toToken The token of the window that should receive touch focus in 2272 * place of the first. 2273 * @hide 2274 */ transferTouchFocus(IBinder fromToken, IBinder toToken)2275 public Transaction transferTouchFocus(IBinder fromToken, IBinder toToken) { 2276 nativeTransferTouchFocus(mNativeObject, fromToken, toToken); 2277 return this; 2278 } 2279 2280 /** 2281 * Waits until any changes to input windows have been sent from SurfaceFlinger to 2282 * InputFlinger before returning. 2283 * 2284 * @hide 2285 */ syncInputWindows()2286 public Transaction syncInputWindows() { 2287 nativeSyncInputWindows(mNativeObject); 2288 return this; 2289 } 2290 2291 /** 2292 * Specify how the buffer assosciated with this Surface is mapped in to the 2293 * parent coordinate space. The source frame will be scaled to fit the destination 2294 * frame, after being rotated according to the orientation parameter. 2295 * 2296 * @param sc The SurfaceControl to specify the geometry of 2297 * @param sourceCrop The source rectangle in buffer space. Or null for the entire buffer. 2298 * @param destFrame The destination rectangle in parent space. Or null for the source frame. 2299 * @param orientation The buffer rotation 2300 * @return This transaction object. 2301 */ 2302 @NonNull setGeometry(@onNull SurfaceControl sc, @Nullable Rect sourceCrop, @Nullable Rect destFrame, @Surface.Rotation int orientation)2303 public Transaction setGeometry(@NonNull SurfaceControl sc, @Nullable Rect sourceCrop, 2304 @Nullable Rect destFrame, @Surface.Rotation int orientation) { 2305 sc.checkNotReleased(); 2306 nativeSetGeometry(mNativeObject, sc.mNativeObject, sourceCrop, destFrame, orientation); 2307 return this; 2308 } 2309 2310 /** 2311 * @hide 2312 */ 2313 @UnsupportedAppUsage setMatrix(SurfaceControl sc, float dsdx, float dtdx, float dtdy, float dsdy)2314 public Transaction setMatrix(SurfaceControl sc, 2315 float dsdx, float dtdx, float dtdy, float dsdy) { 2316 sc.checkNotReleased(); 2317 nativeSetMatrix(mNativeObject, sc.mNativeObject, 2318 dsdx, dtdx, dtdy, dsdy); 2319 return this; 2320 } 2321 2322 /** 2323 * @hide 2324 */ 2325 @UnsupportedAppUsage setMatrix(SurfaceControl sc, Matrix matrix, float[] float9)2326 public Transaction setMatrix(SurfaceControl sc, Matrix matrix, float[] float9) { 2327 matrix.getValues(float9); 2328 setMatrix(sc, float9[MSCALE_X], float9[MSKEW_Y], 2329 float9[MSKEW_X], float9[MSCALE_Y]); 2330 setPosition(sc, float9[MTRANS_X], float9[MTRANS_Y]); 2331 return this; 2332 } 2333 2334 /** 2335 * Sets the color transform for the Surface. 2336 * @param matrix A float array with 9 values represents a 3x3 transform matrix 2337 * @param translation A float array with 3 values represents a translation vector 2338 * @hide 2339 */ setColorTransform(SurfaceControl sc, @Size(9) float[] matrix, @Size(3) float[] translation)2340 public Transaction setColorTransform(SurfaceControl sc, @Size(9) float[] matrix, 2341 @Size(3) float[] translation) { 2342 sc.checkNotReleased(); 2343 nativeSetColorTransform(mNativeObject, sc.mNativeObject, matrix, translation); 2344 return this; 2345 } 2346 2347 /** 2348 * Sets the Surface to be color space agnostic. If a surface is color space agnostic, 2349 * the color can be interpreted in any color space. 2350 * @param agnostic A boolean to indicate whether the surface is color space agnostic 2351 * @hide 2352 */ setColorSpaceAgnostic(SurfaceControl sc, boolean agnostic)2353 public Transaction setColorSpaceAgnostic(SurfaceControl sc, boolean agnostic) { 2354 sc.checkNotReleased(); 2355 nativeSetColorSpaceAgnostic(mNativeObject, sc.mNativeObject, agnostic); 2356 return this; 2357 } 2358 2359 /** 2360 * @hide 2361 */ 2362 @UnsupportedAppUsage setWindowCrop(SurfaceControl sc, Rect crop)2363 public Transaction setWindowCrop(SurfaceControl sc, Rect crop) { 2364 sc.checkNotReleased(); 2365 if (crop != null) { 2366 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 2367 crop.left, crop.top, crop.right, crop.bottom); 2368 } else { 2369 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, 0, 0); 2370 } 2371 2372 return this; 2373 } 2374 2375 /** 2376 * @hide 2377 */ setWindowCrop(SurfaceControl sc, int width, int height)2378 public Transaction setWindowCrop(SurfaceControl sc, int width, int height) { 2379 sc.checkNotReleased(); 2380 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, width, height); 2381 return this; 2382 } 2383 2384 /** 2385 * Sets the corner radius of a {@link SurfaceControl}. 2386 * @param sc SurfaceControl 2387 * @param cornerRadius Corner radius in pixels. 2388 * @return Itself. 2389 * @hide 2390 */ 2391 @UnsupportedAppUsage setCornerRadius(SurfaceControl sc, float cornerRadius)2392 public Transaction setCornerRadius(SurfaceControl sc, float cornerRadius) { 2393 sc.checkNotReleased(); 2394 nativeSetCornerRadius(mNativeObject, sc.mNativeObject, cornerRadius); 2395 2396 return this; 2397 } 2398 2399 /** 2400 * @hide 2401 */ 2402 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.O) setLayerStack(SurfaceControl sc, int layerStack)2403 public Transaction setLayerStack(SurfaceControl sc, int layerStack) { 2404 sc.checkNotReleased(); 2405 nativeSetLayerStack(mNativeObject, sc.mNativeObject, layerStack); 2406 return this; 2407 } 2408 2409 /** 2410 * @hide 2411 */ 2412 @UnsupportedAppUsage deferTransactionUntil(SurfaceControl sc, IBinder handle, long frameNumber)2413 public Transaction deferTransactionUntil(SurfaceControl sc, IBinder handle, 2414 long frameNumber) { 2415 if (frameNumber < 0) { 2416 return this; 2417 } 2418 sc.checkNotReleased(); 2419 nativeDeferTransactionUntil(mNativeObject, sc.mNativeObject, handle, frameNumber); 2420 return this; 2421 } 2422 2423 /** 2424 * @hide 2425 */ 2426 @UnsupportedAppUsage deferTransactionUntilSurface(SurfaceControl sc, Surface barrierSurface, long frameNumber)2427 public Transaction deferTransactionUntilSurface(SurfaceControl sc, Surface barrierSurface, 2428 long frameNumber) { 2429 if (frameNumber < 0) { 2430 return this; 2431 } 2432 sc.checkNotReleased(); 2433 nativeDeferTransactionUntilSurface(mNativeObject, sc.mNativeObject, 2434 barrierSurface.mNativeObject, frameNumber); 2435 return this; 2436 } 2437 2438 /** 2439 * @hide 2440 */ reparentChildren(SurfaceControl sc, IBinder newParentHandle)2441 public Transaction reparentChildren(SurfaceControl sc, IBinder newParentHandle) { 2442 sc.checkNotReleased(); 2443 nativeReparentChildren(mNativeObject, sc.mNativeObject, newParentHandle); 2444 return this; 2445 } 2446 2447 /** 2448 * Re-parents a given layer to a new parent. Children inherit transform (position, scaling) 2449 * crop, visibility, and Z-ordering from their parents, as if the children were pixels within the 2450 * parent Surface. 2451 * 2452 * @param sc The SurfaceControl to reparent 2453 * @param newParent The new parent for the given control. 2454 * @return This Transaction 2455 */ 2456 @NonNull reparent(@onNull SurfaceControl sc, @Nullable SurfaceControl newParent)2457 public Transaction reparent(@NonNull SurfaceControl sc, 2458 @Nullable SurfaceControl newParent) { 2459 sc.checkNotReleased(); 2460 long otherObject = 0; 2461 if (newParent != null) { 2462 newParent.checkNotReleased(); 2463 otherObject = newParent.mNativeObject; 2464 } 2465 nativeReparent(mNativeObject, sc.mNativeObject, otherObject); 2466 return this; 2467 } 2468 2469 /** 2470 * @hide 2471 */ detachChildren(SurfaceControl sc)2472 public Transaction detachChildren(SurfaceControl sc) { 2473 sc.checkNotReleased(); 2474 nativeSeverChildren(mNativeObject, sc.mNativeObject); 2475 return this; 2476 } 2477 2478 /** 2479 * @hide 2480 */ setOverrideScalingMode(SurfaceControl sc, int overrideScalingMode)2481 public Transaction setOverrideScalingMode(SurfaceControl sc, int overrideScalingMode) { 2482 sc.checkNotReleased(); 2483 nativeSetOverrideScalingMode(mNativeObject, sc.mNativeObject, 2484 overrideScalingMode); 2485 return this; 2486 } 2487 2488 /** 2489 * Sets a color for the Surface. 2490 * @param color A float array with three values to represent r, g, b in range [0..1] 2491 * @hide 2492 */ 2493 @UnsupportedAppUsage setColor(SurfaceControl sc, @Size(3) float[] color)2494 public Transaction setColor(SurfaceControl sc, @Size(3) float[] color) { 2495 sc.checkNotReleased(); 2496 nativeSetColor(mNativeObject, sc.mNativeObject, color); 2497 return this; 2498 } 2499 2500 /** 2501 * If the buffer size changes in this transaction, position and crop updates specified 2502 * in this transaction will not complete until a buffer of the new size 2503 * arrives. As transform matrix and size are already frozen in this fashion, 2504 * this enables totally freezing the surface until the resize has completed 2505 * (at which point the geometry influencing aspects of this transaction will then occur) 2506 * @hide 2507 */ setGeometryAppliesWithResize(SurfaceControl sc)2508 public Transaction setGeometryAppliesWithResize(SurfaceControl sc) { 2509 sc.checkNotReleased(); 2510 nativeSetGeometryAppliesWithResize(mNativeObject, sc.mNativeObject); 2511 return this; 2512 } 2513 2514 /** 2515 * Sets the security of the surface. Setting the flag is equivalent to creating the 2516 * Surface with the {@link #SECURE} flag. 2517 * @hide 2518 */ setSecure(SurfaceControl sc, boolean isSecure)2519 public Transaction setSecure(SurfaceControl sc, boolean isSecure) { 2520 sc.checkNotReleased(); 2521 if (isSecure) { 2522 nativeSetFlags(mNativeObject, sc.mNativeObject, SECURE, SECURE); 2523 } else { 2524 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SECURE); 2525 } 2526 return this; 2527 } 2528 2529 /** 2530 * Sets the opacity of the surface. Setting the flag is equivalent to creating the 2531 * Surface with the {@link #OPAQUE} flag. 2532 * @hide 2533 */ setOpaque(SurfaceControl sc, boolean isOpaque)2534 public Transaction setOpaque(SurfaceControl sc, boolean isOpaque) { 2535 sc.checkNotReleased(); 2536 if (isOpaque) { 2537 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_OPAQUE, SURFACE_OPAQUE); 2538 } else { 2539 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_OPAQUE); 2540 } 2541 return this; 2542 } 2543 2544 /** 2545 * @hide 2546 */ setDisplaySurface(IBinder displayToken, Surface surface)2547 public Transaction setDisplaySurface(IBinder displayToken, Surface surface) { 2548 if (displayToken == null) { 2549 throw new IllegalArgumentException("displayToken must not be null"); 2550 } 2551 2552 if (surface != null) { 2553 synchronized (surface.mLock) { 2554 nativeSetDisplaySurface(mNativeObject, displayToken, surface.mNativeObject); 2555 } 2556 } else { 2557 nativeSetDisplaySurface(mNativeObject, displayToken, 0); 2558 } 2559 return this; 2560 } 2561 2562 /** 2563 * @hide 2564 */ setDisplayLayerStack(IBinder displayToken, int layerStack)2565 public Transaction setDisplayLayerStack(IBinder displayToken, int layerStack) { 2566 if (displayToken == null) { 2567 throw new IllegalArgumentException("displayToken must not be null"); 2568 } 2569 nativeSetDisplayLayerStack(mNativeObject, displayToken, layerStack); 2570 return this; 2571 } 2572 2573 /** 2574 * @hide 2575 */ setDisplayProjection(IBinder displayToken, int orientation, Rect layerStackRect, Rect displayRect)2576 public Transaction setDisplayProjection(IBinder displayToken, 2577 int orientation, Rect layerStackRect, Rect displayRect) { 2578 if (displayToken == null) { 2579 throw new IllegalArgumentException("displayToken must not be null"); 2580 } 2581 if (layerStackRect == null) { 2582 throw new IllegalArgumentException("layerStackRect must not be null"); 2583 } 2584 if (displayRect == null) { 2585 throw new IllegalArgumentException("displayRect must not be null"); 2586 } 2587 nativeSetDisplayProjection(mNativeObject, displayToken, orientation, 2588 layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom, 2589 displayRect.left, displayRect.top, displayRect.right, displayRect.bottom); 2590 return this; 2591 } 2592 2593 /** 2594 * @hide 2595 */ setDisplaySize(IBinder displayToken, int width, int height)2596 public Transaction setDisplaySize(IBinder displayToken, int width, int height) { 2597 if (displayToken == null) { 2598 throw new IllegalArgumentException("displayToken must not be null"); 2599 } 2600 if (width <= 0 || height <= 0) { 2601 throw new IllegalArgumentException("width and height must be positive"); 2602 } 2603 2604 nativeSetDisplaySize(mNativeObject, displayToken, width, height); 2605 return this; 2606 } 2607 2608 /** flag the transaction as an animation 2609 * @hide 2610 */ setAnimationTransaction()2611 public Transaction setAnimationTransaction() { 2612 nativeSetAnimationTransaction(mNativeObject); 2613 return this; 2614 } 2615 2616 /** 2617 * Indicate that SurfaceFlinger should wake up earlier than usual as a result of this 2618 * transaction. This should be used when the caller thinks that the scene is complex enough 2619 * that it's likely to hit GL composition, and thus, SurfaceFlinger needs to more time in 2620 * order not to miss frame deadlines. 2621 * <p> 2622 * Corresponds to setting ISurfaceComposer::eEarlyWakeup 2623 * @hide 2624 */ setEarlyWakeup()2625 public Transaction setEarlyWakeup() { 2626 nativeSetEarlyWakeup(mNativeObject); 2627 return this; 2628 } 2629 2630 /** 2631 * Sets an arbitrary piece of metadata on the surface. This is a helper for int data. 2632 * @hide 2633 */ setMetadata(SurfaceControl sc, int key, int data)2634 public Transaction setMetadata(SurfaceControl sc, int key, int data) { 2635 Parcel parcel = Parcel.obtain(); 2636 parcel.writeInt(data); 2637 try { 2638 setMetadata(sc, key, parcel); 2639 } finally { 2640 parcel.recycle(); 2641 } 2642 return this; 2643 } 2644 2645 /** 2646 * Sets an arbitrary piece of metadata on the surface. 2647 * @hide 2648 */ setMetadata(SurfaceControl sc, int key, Parcel data)2649 public Transaction setMetadata(SurfaceControl sc, int key, Parcel data) { 2650 nativeSetMetadata(mNativeObject, sc.mNativeObject, key, data); 2651 return this; 2652 } 2653 2654 /** 2655 * Merge the other transaction into this transaction, clearing the 2656 * other transaction as if it had been applied. 2657 * 2658 * @param other The transaction to merge in to this one. 2659 * @return This transaction. 2660 */ 2661 @NonNull merge(@onNull Transaction other)2662 public Transaction merge(@NonNull Transaction other) { 2663 if (this == other) { 2664 return this; 2665 } 2666 mResizedSurfaces.putAll(other.mResizedSurfaces); 2667 other.mResizedSurfaces.clear(); 2668 nativeMergeTransaction(mNativeObject, other.mNativeObject); 2669 return this; 2670 } 2671 2672 /** 2673 * Equivalent to reparent with a null parent, in that it removes 2674 * the SurfaceControl from the scene, but it also releases 2675 * the local resources (by calling {@link SurfaceControl#release}) 2676 * after this method returns, {@link SurfaceControl#isValid} will return 2677 * false for the argument. 2678 * 2679 * @param sc The surface to remove and release. 2680 * @return This transaction 2681 * @hide 2682 */ 2683 @NonNull remove(@onNull SurfaceControl sc)2684 public Transaction remove(@NonNull SurfaceControl sc) { 2685 reparent(sc, null); 2686 sc.release(); 2687 return this; 2688 } 2689 } 2690 } 2691