blob: 5e2aaae44cb10d6684b4c32d322aee910b65a46c [file] [log] [blame]
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Jorim Jaggi21c39a72017-10-20 15:47:51 +020019import static android.graphics.Matrix.MSCALE_X;
20import static android.graphics.Matrix.MSCALE_Y;
21import static android.graphics.Matrix.MSKEW_X;
22import static android.graphics.Matrix.MSKEW_Y;
23import static android.graphics.Matrix.MTRANS_X;
24import static android.graphics.Matrix.MTRANS_Y;
Vishnu Nair04ab4392018-01-10 11:00:06 -080025import static android.view.Surface.ROTATION_270;
26import static android.view.Surface.ROTATION_90;
27import static android.view.SurfaceControlProto.HASH_CODE;
28import static android.view.SurfaceControlProto.NAME;
chaviwa69e0a72017-11-29 17:55:12 -080029
Robert Carr76907ee2019-01-11 13:38:19 -080030import android.annotation.FloatRange;
31import android.annotation.IntRange;
32import android.annotation.NonNull;
33import android.annotation.Nullable;
chaviw0dd03f52017-08-25 12:15:26 -070034import android.annotation.Size;
Mathew Inwooda570dee2018-08-17 14:56:00 +010035import android.annotation.UnsupportedAppUsage;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080036import android.graphics.Bitmap;
Peiyong Lin5f4a5682019-01-17 11:37:07 -080037import android.graphics.ColorSpace;
Robert Carr6486d312017-01-09 19:48:29 -080038import android.graphics.GraphicBuffer;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020039import android.graphics.Matrix;
Vishnu Nair04ab4392018-01-10 11:00:06 -080040import android.graphics.PixelFormat;
Jorim Jaggia5e10572017-11-15 14:36:26 +010041import android.graphics.Point;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080042import android.graphics.Rect;
43import android.graphics.Region;
Peiyong Lin9d427402019-01-23 18:39:06 -080044import android.hardware.HardwareBuffer;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070045import android.hardware.display.DisplayedContentSample;
46import android.hardware.display.DisplayedContentSamplingAttributes;
Mathew Inwood679c15e2019-02-06 15:36:04 +000047import android.os.Build;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080048import android.os.IBinder;
Jorim Jaggi06975df2017-12-01 14:52:13 +010049import android.os.Parcel;
50import android.os.Parcelable;
Jorim Jaggia5e10572017-11-15 14:36:26 +010051import android.util.ArrayMap;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080052import android.util.Log;
Evan Rosky485df202018-12-06 14:11:12 -080053import android.util.SparseIntArray;
Vishnu Nair04ab4392018-01-10 11:00:06 -080054import android.util.proto.ProtoOutputStream;
Igor Murashkina86ab6402013-08-30 12:58:36 -070055import android.view.Surface.OutOfResourcesException;
Jorim Jaggia5e10572017-11-15 14:36:26 +010056
57import com.android.internal.annotations.GuardedBy;
58
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070059import dalvik.system.CloseGuard;
Vishnu Nair04ab4392018-01-10 11:00:06 -080060
Robert Carre625fcf2017-09-01 12:36:28 -070061import libcore.util.NativeAllocationRegistry;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070062
Robert Carre13b58e2017-08-31 14:50:44 -070063import java.io.Closeable;
Evan Rosky485df202018-12-06 14:11:12 -080064import java.nio.ByteBuffer;
Evan Roskyc12c9a32019-02-06 13:38:03 -080065import java.nio.ByteOrder;
Robert Carre13b58e2017-08-31 14:50:44 -070066
Mathias Agopian3866f0d2013-02-11 22:08:48 -080067/**
Robert Carr76907ee2019-01-11 13:38:19 -080068 * 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.
Mathias Agopian3866f0d2013-02-11 22:08:48 -080076 */
Robert Carr76907ee2019-01-11 13:38:19 -080077public final class SurfaceControl implements Parcelable {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080078 private static final String TAG = "SurfaceControl";
Mathias Agopian29479eb2013-02-14 14:36:04 -080079
Ashok Bhat36bef0b2014-01-20 20:08:01 +000080 private static native long nativeCreate(SurfaceSession session, String name,
Evan Rosky485df202018-12-06 14:11:12 -080081 int w, int h, int format, int flags, long parentObject, Parcel metadata)
Mathias Agopian29479eb2013-02-14 14:36:04 -080082 throws OutOfResourcesException;
Jorim Jaggi06975df2017-12-01 14:52:13 +010083 private static native long nativeReadFromParcel(Parcel in);
chaviwbeb7a0c2018-12-05 13:49:54 -080084 private static native long nativeCopyFromSurfaceControl(long nativeObject);
Jorim Jaggi06975df2017-12-01 14:52:13 +010085 private static native void nativeWriteToParcel(long nativeObject, Parcel out);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000086 private static native void nativeRelease(long nativeObject);
87 private static native void nativeDestroy(long nativeObject);
Chong Zhang47e36a32016-02-29 16:44:33 -080088 private static native void nativeDisconnect(long nativeObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080089
chaviw08520a02018-09-10 16:44:56 -070090 private static native GraphicBuffer nativeScreenshot(IBinder displayToken,
Robert Carr5c52b132019-02-15 15:48:11 -080091 Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation,
92 boolean captureSecureLayers);
chaviwfbe47df2017-11-10 16:14:49 -080093 private static native GraphicBuffer nativeCaptureLayers(IBinder layerHandleToken,
94 Rect sourceCrop, float frameScale);
Mathias Agopian29479eb2013-02-14 14:36:04 -080095
Robert Carre13b58e2017-08-31 14:50:44 -070096 private static native long nativeCreateTransaction();
97 private static native long nativeGetNativeTransactionFinalizer();
98 private static native void nativeApplyTransaction(long transactionObj, boolean sync);
Robert Carrb1579c82017-09-05 14:54:47 -070099 private static native void nativeMergeTransaction(long transactionObj,
100 long otherTransactionObj);
Robert Carre13b58e2017-08-31 14:50:44 -0700101 private static native void nativeSetAnimationTransaction(long transactionObj);
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100102 private static native void nativeSetEarlyWakeup(long transactionObj);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800103
Robert Carre13b58e2017-08-31 14:50:44 -0700104 private static native void nativeSetLayer(long transactionObj, long nativeObject, int zorder);
105 private static native void nativeSetRelativeLayer(long transactionObj, long nativeObject,
106 IBinder relativeTo, int zorder);
107 private static native void nativeSetPosition(long transactionObj, long nativeObject,
108 float x, float y);
109 private static native void nativeSetGeometryAppliesWithResize(long transactionObj,
110 long nativeObject);
111 private static native void nativeSetSize(long transactionObj, long nativeObject, int w, int h);
112 private static native void nativeSetTransparentRegionHint(long transactionObj,
113 long nativeObject, Region region);
114 private static native void nativeSetAlpha(long transactionObj, long nativeObject, float alpha);
115 private static native void nativeSetMatrix(long transactionObj, long nativeObject,
116 float dsdx, float dtdx,
Andrii Kulian283acd22017-08-03 04:03:51 -0700117 float dtdy, float dsdy);
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700118 private static native void nativeSetColorTransform(long transactionObj, long nativeObject,
119 float[] matrix, float[] translation);
Peiyong Linf4f0f642019-03-01 14:36:05 -0800120 private static native void nativeSetColorSpaceAgnostic(long transactionObj, long nativeObject,
121 boolean agnostic);
Robert Carr76907ee2019-01-11 13:38:19 -0800122 private static native void nativeSetGeometry(long transactionObj, long nativeObject,
123 Rect sourceCrop, Rect dest, long orientation);
Robert Carre13b58e2017-08-31 14:50:44 -0700124 private static native void nativeSetColor(long transactionObj, long nativeObject, float[] color);
125 private static native void nativeSetFlags(long transactionObj, long nativeObject,
126 int flags, int mask);
127 private static native void nativeSetWindowCrop(long transactionObj, long nativeObject,
128 int l, int t, int r, int b);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -0700129 private static native void nativeSetCornerRadius(long transactionObj, long nativeObject,
130 float cornerRadius);
Robert Carre13b58e2017-08-31 14:50:44 -0700131 private static native void nativeSetLayerStack(long transactionObj, long nativeObject,
132 int layerStack);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800133
Svetoslav1376d602014-03-13 11:17:26 -0700134 private static native boolean nativeClearContentFrameStats(long nativeObject);
135 private static native boolean nativeGetContentFrameStats(long nativeObject, WindowContentFrameStats outStats);
136 private static native boolean nativeClearAnimationFrameStats();
137 private static native boolean nativeGetAnimationFrameStats(WindowAnimationFrameStats outStats);
138
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800139 private static native long[] nativeGetPhysicalDisplayIds();
140 private static native IBinder nativeGetPhysicalDisplayToken(long physicalDisplayId);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800141 private static native IBinder nativeCreateDisplay(String name, boolean secure);
Jesse Hall6a6bc212013-08-08 12:15:03 -0700142 private static native void nativeDestroyDisplay(IBinder displayToken);
Robert Carre13b58e2017-08-31 14:50:44 -0700143 private static native void nativeSetDisplaySurface(long transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000144 IBinder displayToken, long nativeSurfaceObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700145 private static native void nativeSetDisplayLayerStack(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800146 IBinder displayToken, int layerStack);
Robert Carre13b58e2017-08-31 14:50:44 -0700147 private static native void nativeSetDisplayProjection(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800148 IBinder displayToken, int orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -0700149 int l, int t, int r, int b,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800150 int L, int T, int R, int B);
Robert Carre13b58e2017-08-31 14:50:44 -0700151 private static native void nativeSetDisplaySize(long transactionObj, IBinder displayToken,
152 int width, int height);
Dan Stoza00101052014-05-02 15:23:40 -0700153 private static native SurfaceControl.PhysicalDisplayInfo[] nativeGetDisplayConfigs(
154 IBinder displayToken);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -0700155 private static native DisplayedContentSamplingAttributes
156 nativeGetDisplayedContentSamplingAttributes(IBinder displayToken);
157 private static native boolean nativeSetDisplayedContentSamplingEnabled(IBinder displayToken,
158 boolean enable, int componentMask, int maxFrames);
159 private static native DisplayedContentSample nativeGetDisplayedContentSample(
160 IBinder displayToken, long numFrames, long timestamp);
Dan Stoza00101052014-05-02 15:23:40 -0700161 private static native int nativeGetActiveConfig(IBinder displayToken);
162 private static native boolean nativeSetActiveConfig(IBinder displayToken, int id);
Ady Abraham6070ce12019-01-24 18:48:58 -0800163 private static native boolean nativeSetAllowedDisplayConfigs(IBinder displayToken,
164 int[] allowedConfigs);
Ady Abraham42f9a2fb2019-02-26 14:13:39 -0800165 private static native int[] nativeGetAllowedDisplayConfigs(IBinder displayToken);
Michael Wright1c9977b2016-07-12 13:30:10 -0700166 private static native int[] nativeGetDisplayColorModes(IBinder displayToken);
Daniel Solomon10e3b332019-01-20 21:09:11 -0800167 private static native SurfaceControl.DisplayPrimaries nativeGetDisplayNativePrimaries(
168 IBinder displayToken);
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800169 private static native int[] nativeGetCompositionDataspaces();
Michael Wright1c9977b2016-07-12 13:30:10 -0700170 private static native int nativeGetActiveColorMode(IBinder displayToken);
171 private static native boolean nativeSetActiveColorMode(IBinder displayToken,
172 int colorMode);
Prashant Malanic55929a2014-05-25 01:59:21 -0700173 private static native void nativeSetDisplayPowerMode(
174 IBinder displayToken, int mode);
Robert Carre13b58e2017-08-31 14:50:44 -0700175 private static native void nativeDeferTransactionUntil(long transactionObj, long nativeObject,
Rob Carr64e516f2015-10-29 00:20:45 +0000176 IBinder handle, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700177 private static native void nativeDeferTransactionUntilSurface(long transactionObj,
178 long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800179 long surfaceObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700180 private static native void nativeReparentChildren(long transactionObj, long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800181 IBinder handle);
Robert Carre13b58e2017-08-31 14:50:44 -0700182 private static native void nativeReparent(long transactionObj, long nativeObject,
Robert Carr10584fa2019-01-14 15:55:19 -0800183 long newParentNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700184 private static native void nativeSeverChildren(long transactionObj, long nativeObject);
185 private static native void nativeSetOverrideScalingMode(long transactionObj, long nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700186 int scalingMode);
Rob Carr64e516f2015-10-29 00:20:45 +0000187 private static native IBinder nativeGetHandle(long nativeObject);
Robert Carr6da3cc02016-06-16 15:17:07 -0700188 private static native boolean nativeGetTransformToDisplayInverse(long nativeObject);
189
Michael Wright9ff94c02016-03-30 18:05:40 -0700190 private static native Display.HdrCapabilities nativeGetHdrCapabilities(IBinder displayToken);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800191
Robert Carr788f5742018-07-30 17:46:45 -0700192 private static native void nativeSetInputWindowInfo(long transactionObj, long nativeObject,
193 InputWindowHandle handle);
chaviw59f532e2018-12-26 15:34:59 -0800194 private static native void nativeTransferTouchFocus(long transactionObj, IBinder fromToken,
195 IBinder toToken);
Peiyong Lin0ddb7d42019-01-16 13:25:09 -0800196 private static native boolean nativeGetProtectedContentSupport();
Evan Rosky485df202018-12-06 14:11:12 -0800197 private static native void nativeSetMetadata(long transactionObj, int key, Parcel data);
chaviw319cd0782019-02-14 11:00:23 -0800198 private static native void nativeSyncInputWindows(long transactionObj);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800199
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800200 private final CloseGuard mCloseGuard = CloseGuard.get();
chaviwbeb7a0c2018-12-05 13:49:54 -0800201 private String mName;
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000202 long mNativeObject; // package visibility only for Surface.java access
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800203
Jorim Jaggia5e10572017-11-15 14:36:26 +0100204 // TODO: Move this to native.
205 private final Object mSizeLock = new Object();
206 @GuardedBy("mSizeLock")
207 private int mWidth;
208 @GuardedBy("mSizeLock")
209 private int mHeight;
210
Robert Carre13b58e2017-08-31 14:50:44 -0700211 static Transaction sGlobalTransaction;
212 static long sTransactionNestCount = 0;
213
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800214 /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */
215
216 /**
217 * Surface creation flag: Surface is created hidden
Robert Carra7827f72019-01-11 12:53:37 -0800218 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800219 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100220 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800221 public static final int HIDDEN = 0x00000004;
222
223 /**
224 * Surface creation flag: The surface contains secure content, special
225 * measures will be taken to disallow the surface's content to be copied
226 * from another process. In particular, screenshots and VNC servers will
227 * be disabled, but other measures can take place, for instance the
Jesse Hall6a6bc212013-08-08 12:15:03 -0700228 * surface might not be hardware accelerated.
Robert Carra7827f72019-01-11 12:53:37 -0800229 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800230 */
231 public static final int SECURE = 0x00000080;
232
233 /**
234 * Surface creation flag: Creates a surface where color components are interpreted
235 * as "non pre-multiplied" by their alpha channel. Of course this flag is
236 * meaningless for surfaces without an alpha channel. By default
237 * surfaces are pre-multiplied, which means that each color component is
238 * already multiplied by its alpha value. In this case the blending
239 * equation used is:
Andy McFadden314405b2014-01-29 17:18:05 -0800240 * <p>
241 * <code>DEST = SRC + DEST * (1-SRC_ALPHA)</code>
242 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800243 * By contrast, non pre-multiplied surfaces use the following equation:
Andy McFadden314405b2014-01-29 17:18:05 -0800244 * <p>
245 * <code>DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)</code>
246 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800247 * pre-multiplied surfaces must always be used if transparent pixels are
248 * composited on top of each-other into the surface. A pre-multiplied
249 * surface can never lower the value of the alpha component of a given
250 * pixel.
Andy McFadden314405b2014-01-29 17:18:05 -0800251 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800252 * In some rare situations, a non pre-multiplied surface is preferable.
Robert Carra7827f72019-01-11 12:53:37 -0800253 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800254 */
255 public static final int NON_PREMULTIPLIED = 0x00000100;
256
257 /**
258 * Surface creation flag: Indicates that the surface must be considered opaque,
Robert Carre625fcf2017-09-01 12:36:28 -0700259 * even if its pixel format contains an alpha channel. This can be useful if an
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800260 * application needs full RGBA 8888 support for instance but will
261 * still draw every pixel opaque.
Andy McFadden314405b2014-01-29 17:18:05 -0800262 * <p>
263 * This flag is ignored if setAlpha() is used to make the surface non-opaque.
264 * Combined effects are (assuming a buffer format with an alpha channel):
265 * <ul>
266 * <li>OPAQUE + alpha(1.0) == opaque composition
267 * <li>OPAQUE + alpha(0.x) == blended composition
268 * <li>!OPAQUE + alpha(1.0) == blended composition
269 * <li>!OPAQUE + alpha(0.x) == blended composition
270 * </ul>
271 * If the underlying buffer lacks an alpha channel, the OPAQUE flag is effectively
272 * set automatically.
Robert Carra7827f72019-01-11 12:53:37 -0800273 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800274 */
275 public static final int OPAQUE = 0x00000400;
276
277 /**
278 * Surface creation flag: Application requires a hardware-protected path to an
279 * external display sink. If a hardware-protected path is not available,
280 * then this surface will not be displayed on the external sink.
281 *
Robert Carra7827f72019-01-11 12:53:37 -0800282 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800283 */
284 public static final int PROTECTED_APP = 0x00000800;
285
286 // 0x1000 is reserved for an independent DRM protected flag in framework
287
288 /**
Riley Andrews68eccda2014-07-07 11:47:35 -0700289 * Surface creation flag: Window represents a cursor glyph.
Robert Carra7827f72019-01-11 12:53:37 -0800290 * @hide
Riley Andrews68eccda2014-07-07 11:47:35 -0700291 */
292 public static final int CURSOR_WINDOW = 0x00002000;
293
294 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800295 * Surface creation flag: Creates a normal surface.
296 * This is the default.
297 *
Robert Carra7827f72019-01-11 12:53:37 -0800298 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800299 */
300 public static final int FX_SURFACE_NORMAL = 0x00000000;
301
302 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800303 * Surface creation flag: Creates a Dim surface.
304 * Everything behind this surface is dimmed by the amount specified
305 * in {@link #setAlpha}. It is an error to lock a Dim surface, since it
306 * doesn't have a backing store.
307 *
Robert Carra7827f72019-01-11 12:53:37 -0800308 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800309 */
310 public static final int FX_SURFACE_DIM = 0x00020000;
311
312 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700313 * Surface creation flag: Creates a container surface.
314 * This surface will have no buffers and will only be used
315 * as a container for other surfaces, or for its InputInfo.
Robert Carra7827f72019-01-11 12:53:37 -0800316 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700317 */
318 public static final int FX_SURFACE_CONTAINER = 0x00080000;
319
320 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800321 * Mask used for FX values above.
322 *
Robert Carra7827f72019-01-11 12:53:37 -0800323 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800324 */
325 public static final int FX_SURFACE_MASK = 0x000F0000;
326
327 /* flags used with setFlags() (keep in sync with ISurfaceComposer.h) */
328
329 /**
330 * Surface flag: Hide the surface.
331 * Equivalent to calling hide().
Andy McFadden314405b2014-01-29 17:18:05 -0800332 * Updates the value set during Surface creation (see {@link #HIDDEN}).
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800333 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800334 private static final int SURFACE_HIDDEN = 0x01;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800335
Andy McFadden314405b2014-01-29 17:18:05 -0800336 /**
337 * Surface flag: composite without blending when possible.
338 * Updates the value set during Surface creation (see {@link #OPAQUE}).
339 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800340 private static final int SURFACE_OPAQUE = 0x02;
Andy McFadden314405b2014-01-29 17:18:05 -0800341
Robert Carr76907ee2019-01-11 13:38:19 -0800342 // Display power modes.
Prashant Malanic55929a2014-05-25 01:59:21 -0700343 /**
344 * Display power mode off: used while blanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700345 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800346 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700347 */
348 public static final int POWER_MODE_OFF = 0;
349
350 /**
351 * Display power mode doze: used while putting the screen into low power mode.
Jeff Brown5dc21912014-07-17 18:50:18 -0700352 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800353 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700354 */
355 public static final int POWER_MODE_DOZE = 1;
356
357 /**
358 * Display power mode normal: used while unblanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700359 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800360 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700361 */
362 public static final int POWER_MODE_NORMAL = 2;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800363
Jeff Brown5dc21912014-07-17 18:50:18 -0700364 /**
365 * Display power mode doze: used while putting the screen into a suspended
366 * low power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800367 * @hide
Jeff Brown5dc21912014-07-17 18:50:18 -0700368 */
369 public static final int POWER_MODE_DOZE_SUSPEND = 3;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800370
371 /**
Chris Phoenix10a4a642017-09-25 13:21:00 -0700372 * Display power mode on: used while putting the screen into a suspended
373 * full power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800374 * @hide
Chris Phoenix10a4a642017-09-25 13:21:00 -0700375 */
376 public static final int POWER_MODE_ON_SUSPEND = 4;
377
378 /**
Robert Carr132c9f52017-07-31 17:02:30 -0700379 * A value for windowType used to indicate that the window should be omitted from screenshots
380 * and display mirroring. A temporary workaround until we express such things with
381 * the hierarchy.
382 * TODO: b/64227542
383 * @hide
384 */
385 public static final int WINDOW_TYPE_DONT_SCREENSHOT = 441731;
386
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800387 /**
388 * internal representation of how to interpret pixel value, used only to convert to ColorSpace.
389 */
390 private static final int INTERNAL_DATASPACE_SRGB = 142671872;
391 private static final int INTERNAL_DATASPACE_DISPLAY_P3 = 143261696;
392 private static final int INTERNAL_DATASPACE_SCRGB = 411107328;
393
Robert Carreb344c72019-01-07 18:35:30 -0800394 private void assignNativeObject(long nativeObject) {
395 if (mNativeObject != 0) {
396 release();
397 }
398 mNativeObject = nativeObject;
399 }
400
Robert Carra7827f72019-01-11 12:53:37 -0800401 /**
402 * @hide
403 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800404 public void copyFrom(SurfaceControl other) {
405 mName = other.mName;
406 mWidth = other.mWidth;
407 mHeight = other.mHeight;
Robert Carreb344c72019-01-07 18:35:30 -0800408 assignNativeObject(nativeCopyFromSurfaceControl(other.mNativeObject));
chaviwbeb7a0c2018-12-05 13:49:54 -0800409 }
410
Robert Carr132c9f52017-07-31 17:02:30 -0700411 /**
Evan Rosky485df202018-12-06 14:11:12 -0800412 * owner UID.
413 * @hide
414 */
415 public static final int METADATA_OWNER_UID = 1;
416
417 /**
418 * Window type as per {@link WindowManager.LayoutParams}.
419 * @hide
420 */
421 public static final int METADATA_WINDOW_TYPE = 2;
422
423 /**
Evan Rosky9020c072018-12-06 14:11:12 -0800424 * Task id to allow association between surfaces and task.
425 * @hide
426 */
427 public static final int METADATA_TASK_ID = 3;
428
429 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700430 * Builder class for {@link SurfaceControl} objects.
431 */
432 public static class Builder {
433 private SurfaceSession mSession;
434 private int mFlags = HIDDEN;
435 private int mWidth;
436 private int mHeight;
437 private int mFormat = PixelFormat.OPAQUE;
438 private String mName;
439 private SurfaceControl mParent;
Evan Rosky485df202018-12-06 14:11:12 -0800440 private SparseIntArray mMetadata;
Robert Carre625fcf2017-09-01 12:36:28 -0700441
442 /**
443 * Begin building a SurfaceControl with a given {@link SurfaceSession}.
444 *
445 * @param session The {@link SurfaceSession} with which to eventually construct the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800446 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700447 */
448 public Builder(SurfaceSession session) {
449 mSession = session;
450 }
451
452 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800453 * Begin building a SurfaceControl.
454 */
455 public Builder() {
456 }
457
458 /**
459 * Construct a new {@link SurfaceControl} with the set parameters. The builder
460 * remains valid.
Robert Carre625fcf2017-09-01 12:36:28 -0700461 */
462 public SurfaceControl build() {
Vishnu Naire86bd982018-11-28 13:23:17 -0800463 if (mWidth < 0 || mHeight < 0) {
Robert Carre625fcf2017-09-01 12:36:28 -0700464 throw new IllegalArgumentException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800465 "width and height must be positive or unset");
466 }
467 if ((mWidth > 0 || mHeight > 0) && (isColorLayerSet() || isContainerLayerSet())) {
468 throw new IllegalArgumentException(
469 "Only buffer layers can set a valid buffer size.");
Robert Carre625fcf2017-09-01 12:36:28 -0700470 }
Evan Rosky485df202018-12-06 14:11:12 -0800471 return new SurfaceControl(
472 mSession, mName, mWidth, mHeight, mFormat, mFlags, mParent, mMetadata);
Robert Carre625fcf2017-09-01 12:36:28 -0700473 }
474
475 /**
476 * Set a debugging-name for the SurfaceControl.
477 *
478 * @param name A name to identify the Surface in debugging.
479 */
480 public Builder setName(String name) {
481 mName = name;
482 return this;
483 }
484
485 /**
486 * Set the initial size of the controlled surface's buffers in pixels.
487 *
488 * @param width The buffer width in pixels.
489 * @param height The buffer height in pixels.
490 */
Robert Carr76907ee2019-01-11 13:38:19 -0800491 public Builder setBufferSize(@IntRange(from = 0) int width,
492 @IntRange(from = 0) int height) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800493 if (width < 0 || height < 0) {
Robert Carre625fcf2017-09-01 12:36:28 -0700494 throw new IllegalArgumentException(
495 "width and height must be positive");
496 }
497 mWidth = width;
498 mHeight = height;
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000499 // set this as a buffer layer since we are specifying a buffer size.
500 return setFlags(FX_SURFACE_NORMAL, FX_SURFACE_MASK);
501 }
502
503 /**
504 * Set the initial size of the controlled surface's buffers in pixels.
505 */
506 private void unsetBufferSize() {
507 mWidth = 0;
508 mHeight = 0;
Robert Carre625fcf2017-09-01 12:36:28 -0700509 }
510
511 /**
512 * Set the pixel format of the controlled surface's buffers, using constants from
513 * {@link android.graphics.PixelFormat}.
514 */
Robert Carr76907ee2019-01-11 13:38:19 -0800515 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700516 public Builder setFormat(@PixelFormat.Format int format) {
517 mFormat = format;
518 return this;
519 }
520
521 /**
522 * Specify if the app requires a hardware-protected path to
523 * an external display sync. If protected content is enabled, but
524 * such a path is not available, then the controlled Surface will
525 * not be displayed.
526 *
527 * @param protectedContent Whether to require a protected sink.
Robert Carra7827f72019-01-11 12:53:37 -0800528 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700529 */
Robert Carr76907ee2019-01-11 13:38:19 -0800530 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700531 public Builder setProtected(boolean protectedContent) {
532 if (protectedContent) {
533 mFlags |= PROTECTED_APP;
534 } else {
535 mFlags &= ~PROTECTED_APP;
536 }
537 return this;
538 }
539
540 /**
541 * Specify whether the Surface contains secure content. If true, the system
542 * will prevent the surfaces content from being copied by another process. In
543 * particular screenshots and VNC servers will be disabled. This is however
544 * not a complete prevention of readback as {@link #setProtected}.
Robert Carra7827f72019-01-11 12:53:37 -0800545 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700546 */
Robert Carr76907ee2019-01-11 13:38:19 -0800547 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700548 public Builder setSecure(boolean secure) {
549 if (secure) {
550 mFlags |= SECURE;
551 } else {
552 mFlags &= ~SECURE;
553 }
554 return this;
555 }
556
557 /**
558 * Indicates whether the surface must be considered opaque,
559 * even if its pixel format is set to translucent. This can be useful if an
560 * application needs full RGBA 8888 support for instance but will
561 * still draw every pixel opaque.
562 * <p>
563 * This flag only determines whether opacity will be sampled from the alpha channel.
564 * Plane-alpha from calls to setAlpha() can still result in blended composition
565 * regardless of the opaque setting.
566 *
567 * Combined effects are (assuming a buffer format with an alpha channel):
568 * <ul>
569 * <li>OPAQUE + alpha(1.0) == opaque composition
570 * <li>OPAQUE + alpha(0.x) == blended composition
571 * <li>OPAQUE + alpha(0.0) == no composition
572 * <li>!OPAQUE + alpha(1.0) == blended composition
573 * <li>!OPAQUE + alpha(0.x) == blended composition
574 * <li>!OPAQUE + alpha(0.0) == no composition
575 * </ul>
576 * If the underlying buffer lacks an alpha channel, it is as if setOpaque(true)
577 * were set automatically.
578 * @param opaque Whether the Surface is OPAQUE.
579 */
Robert Carr76907ee2019-01-11 13:38:19 -0800580 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700581 public Builder setOpaque(boolean opaque) {
582 if (opaque) {
583 mFlags |= OPAQUE;
584 } else {
585 mFlags &= ~OPAQUE;
586 }
587 return this;
588 }
589
590 /**
591 * Set a parent surface for our new SurfaceControl.
592 *
593 * Child surfaces are constrained to the onscreen region of their parent.
594 * Furthermore they stack relatively in Z order, and inherit the transformation
595 * of the parent.
596 *
597 * @param parent The parent control.
598 */
Robert Carr76907ee2019-01-11 13:38:19 -0800599 @NonNull
600 public Builder setParent(@Nullable SurfaceControl parent) {
Robert Carre625fcf2017-09-01 12:36:28 -0700601 mParent = parent;
602 return this;
603 }
604
605 /**
Evan Rosky485df202018-12-06 14:11:12 -0800606 * Sets a metadata int.
Robert Carre625fcf2017-09-01 12:36:28 -0700607 *
Evan Rosky485df202018-12-06 14:11:12 -0800608 * @param key metadata key
609 * @param data associated data
Robert Carra7827f72019-01-11 12:53:37 -0800610 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700611 */
Evan Rosky485df202018-12-06 14:11:12 -0800612 public Builder setMetadata(int key, int data) {
613 if (mMetadata == null) {
614 mMetadata = new SparseIntArray();
Robert Carre625fcf2017-09-01 12:36:28 -0700615 }
Evan Rosky485df202018-12-06 14:11:12 -0800616 mMetadata.put(key, data);
Robert Carre625fcf2017-09-01 12:36:28 -0700617 return this;
618 }
619
620 /**
621 * Indicate whether a 'ColorLayer' is to be constructed.
622 *
623 * Color layers will not have an associated BufferQueue and will instead always render a
624 * solid color (that is, solid before plane alpha). Currently that color is black.
625 *
Robert Carra7827f72019-01-11 12:53:37 -0800626 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700627 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000628 public Builder setColorLayer() {
629 unsetBufferSize();
630 return setFlags(FX_SURFACE_DIM, FX_SURFACE_MASK);
Robert Carre625fcf2017-09-01 12:36:28 -0700631 }
632
Vishnu Naire86bd982018-11-28 13:23:17 -0800633 private boolean isColorLayerSet() {
634 return (mFlags & FX_SURFACE_DIM) == FX_SURFACE_DIM;
635 }
636
Robert Carre625fcf2017-09-01 12:36:28 -0700637 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700638 * Indicates whether a 'ContainerLayer' is to be constructed.
639 *
640 * Container layers will not be rendered in any fashion and instead are used
641 * as a parent of renderable layers.
642 *
Robert Carra7827f72019-01-11 12:53:37 -0800643 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700644 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000645 public Builder setContainerLayer() {
646 unsetBufferSize();
647 return setFlags(FX_SURFACE_CONTAINER, FX_SURFACE_MASK);
Robert Carrb6cd6432018-08-13 13:01:47 -0700648 }
649
Vishnu Naire86bd982018-11-28 13:23:17 -0800650 private boolean isContainerLayerSet() {
651 return (mFlags & FX_SURFACE_CONTAINER) == FX_SURFACE_CONTAINER;
652 }
653
Robert Carrb6cd6432018-08-13 13:01:47 -0700654 /**
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000655 * Set 'Surface creation flags' such as {@link #HIDDEN}, {@link #SECURE}.
Robert Carre625fcf2017-09-01 12:36:28 -0700656 *
657 * TODO: Finish conversion to individual builder methods?
658 * @param flags The combined flags
Robert Carra7827f72019-01-11 12:53:37 -0800659 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700660 */
661 public Builder setFlags(int flags) {
662 mFlags = flags;
663 return this;
664 }
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000665
666 private Builder setFlags(int flags, int mask) {
667 mFlags = (mFlags & ~mask) | flags;
668 return this;
669 }
Robert Carre625fcf2017-09-01 12:36:28 -0700670 }
671
672 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800673 * Create a surface with a name.
Andy McFadden314405b2014-01-29 17:18:05 -0800674 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800675 * The surface creation flags specify what kind of surface to create and
676 * certain options such as whether the surface can be assumed to be opaque
677 * and whether it should be initially hidden. Surfaces should always be
678 * created with the {@link #HIDDEN} flag set to ensure that they are not
679 * made visible prematurely before all of the surface's properties have been
680 * configured.
Andy McFadden314405b2014-01-29 17:18:05 -0800681 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800682 * Good practice is to first create the surface with the {@link #HIDDEN} flag
683 * specified, open a transaction, set the surface layer, layer stack, alpha,
684 * and position, call {@link #show} if appropriate, and close the transaction.
Vishnu Naird454442d2018-11-13 13:51:01 -0800685 * <p>
686 * Bounds of the surface is determined by its crop and its buffer size. If the
687 * surface has no buffer or crop, the surface is boundless and only constrained
688 * by the size of its parent bounds.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800689 *
690 * @param session The surface session, must not be null.
691 * @param name The surface name, must not be null.
692 * @param w The surface initial width.
693 * @param h The surface initial height.
694 * @param flags The surface creation flags. Should always include {@link #HIDDEN}
695 * in the creation flags.
Evan Rosky485df202018-12-06 14:11:12 -0800696 * @param metadata Initial metadata.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700697 *
698 * @throws throws OutOfResourcesException If the SurfaceControl cannot be created.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800699 */
Robert Carre625fcf2017-09-01 12:36:28 -0700700 private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags,
Evan Rosky485df202018-12-06 14:11:12 -0800701 SurfaceControl parent, SparseIntArray metadata)
Robert Carrb0f39362018-03-14 13:52:25 -0700702 throws OutOfResourcesException, IllegalArgumentException {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800703 if (name == null) {
704 throw new IllegalArgumentException("name must not be null");
705 }
706
707 if ((flags & SurfaceControl.HIDDEN) == 0) {
708 Log.w(TAG, "Surfaces should always be created with the HIDDEN flag set "
709 + "to ensure that they are not made visible prematurely before "
710 + "all of the surface's properties have been configured. "
711 + "Set the other properties and make the surface visible within "
712 + "a transaction. New surface name: " + name,
713 new Throwable());
714 }
715
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800716 mName = name;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100717 mWidth = w;
718 mHeight = h;
Evan Rosky485df202018-12-06 14:11:12 -0800719 Parcel metaParcel = Parcel.obtain();
720 try {
721 if (metadata != null && metadata.size() > 0) {
722 metaParcel.writeInt(metadata.size());
723 for (int i = 0; i < metadata.size(); ++i) {
724 metaParcel.writeInt(metadata.keyAt(i));
725 metaParcel.writeByteArray(
Evan Roskyc12c9a32019-02-06 13:38:03 -0800726 ByteBuffer.allocate(4).order(ByteOrder.nativeOrder())
727 .putInt(metadata.valueAt(i)).array());
Evan Rosky485df202018-12-06 14:11:12 -0800728 }
Evan Roskyc12c9a32019-02-06 13:38:03 -0800729 metaParcel.setDataPosition(0);
Evan Rosky485df202018-12-06 14:11:12 -0800730 }
731 mNativeObject = nativeCreate(session, name, w, h, format, flags,
732 parent != null ? parent.mNativeObject : 0, metaParcel);
733 } finally {
734 metaParcel.recycle();
735 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800736 if (mNativeObject == 0) {
737 throw new OutOfResourcesException(
738 "Couldn't allocate SurfaceControl native object");
739 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700740
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800741 mCloseGuard.open("release");
742 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700743
Robert Carra7827f72019-01-11 12:53:37 -0800744 /** This is a transfer constructor, useful for transferring a live SurfaceControl native
745 * object to another Java wrapper which could have some different behavior, e.g.
746 * event logging.
747 * @hide
748 */
Robert Carr3b716242016-08-16 16:02:21 -0700749 public SurfaceControl(SurfaceControl other) {
750 mName = other.mName;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100751 mWidth = other.mWidth;
752 mHeight = other.mHeight;
Robert Carr3b716242016-08-16 16:02:21 -0700753 mNativeObject = other.mNativeObject;
754 other.mCloseGuard.close();
755 other.mNativeObject = 0;
756 mCloseGuard.open("release");
757 }
758
Jorim Jaggi06975df2017-12-01 14:52:13 +0100759 private SurfaceControl(Parcel in) {
chaviwbeb7a0c2018-12-05 13:49:54 -0800760 readFromParcel(in);
761 mCloseGuard.open("release");
762 }
763
Robert Carra7827f72019-01-11 12:53:37 -0800764 /**
765 * @hide
766 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800767 public SurfaceControl() {
768 mCloseGuard.open("release");
769 }
770
771 public void readFromParcel(Parcel in) {
772 if (in == null) {
773 throw new IllegalArgumentException("source must not be null");
774 }
775
Jorim Jaggi06975df2017-12-01 14:52:13 +0100776 mName = in.readString();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100777 mWidth = in.readInt();
778 mHeight = in.readInt();
Robert Carr5fea55b2018-12-10 13:05:52 -0800779
Robert Carreb344c72019-01-07 18:35:30 -0800780 long object = 0;
Robert Carr5fea55b2018-12-10 13:05:52 -0800781 if (in.readInt() != 0) {
Robert Carreb344c72019-01-07 18:35:30 -0800782 object = nativeReadFromParcel(in);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100783 }
Robert Carreb344c72019-01-07 18:35:30 -0800784 assignNativeObject(object);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100785 }
786
787 @Override
788 public int describeContents() {
789 return 0;
790 }
791
792 @Override
793 public void writeToParcel(Parcel dest, int flags) {
794 dest.writeString(mName);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100795 dest.writeInt(mWidth);
796 dest.writeInt(mHeight);
Robert Carr5fea55b2018-12-10 13:05:52 -0800797 if (mNativeObject == 0) {
798 dest.writeInt(0);
799 } else {
800 dest.writeInt(1);
801 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100802 nativeWriteToParcel(mNativeObject, dest);
Robert Carr5fea55b2018-12-10 13:05:52 -0800803
804 if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) {
805 release();
806 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100807 }
808
Vishnu Nair04ab4392018-01-10 11:00:06 -0800809 /**
810 * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link
811 * android.view.SurfaceControlProto}.
812 *
813 * @param proto Stream to write the SurfaceControl object to.
814 * @param fieldId Field Id of the SurfaceControl as defined in the parent message.
815 * @hide
816 */
817 public void writeToProto(ProtoOutputStream proto, long fieldId) {
818 final long token = proto.start(fieldId);
819 proto.write(HASH_CODE, System.identityHashCode(this));
820 proto.write(NAME, mName);
821 proto.end(token);
822 }
823
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700824 public static final @android.annotation.NonNull Creator<SurfaceControl> CREATOR
Jorim Jaggi06975df2017-12-01 14:52:13 +0100825 = new Creator<SurfaceControl>() {
826 public SurfaceControl createFromParcel(Parcel in) {
827 return new SurfaceControl(in);
828 }
829
830 public SurfaceControl[] newArray(int size) {
831 return new SurfaceControl[size];
832 }
833 };
834
Robert Carra7827f72019-01-11 12:53:37 -0800835 /**
836 * @hide
837 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800838 @Override
839 protected void finalize() throws Throwable {
840 try {
841 if (mCloseGuard != null) {
842 mCloseGuard.warnIfOpen();
843 }
844 if (mNativeObject != 0) {
845 nativeRelease(mNativeObject);
846 }
847 } finally {
848 super.finalize();
849 }
850 }
851
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800852 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800853 * Release the local reference to the server-side surface. The surface
854 * may continue to exist on-screen as long as its parent continues
855 * to exist. To explicitly remove a surface from the screen use
856 * {@link Transaction#reparent} with a null-parent.
857 *
858 * Always call release() when you're done with a SurfaceControl.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800859 */
860 public void release() {
861 if (mNativeObject != 0) {
862 nativeRelease(mNativeObject);
863 mNativeObject = 0;
864 }
865 mCloseGuard.close();
866 }
867
868 /**
Robert Carr5ea304d2019-02-04 16:04:55 -0800869 * Release the local resources like {@link #release} but also
870 * remove the Surface from the screen.
Robert Carra7827f72019-01-11 12:53:37 -0800871 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800872 */
Robert Carr5ea304d2019-02-04 16:04:55 -0800873 public void remove() {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800874 if (mNativeObject != 0) {
875 nativeDestroy(mNativeObject);
876 mNativeObject = 0;
877 }
878 mCloseGuard.close();
879 }
880
Chong Zhang47e36a32016-02-29 16:44:33 -0800881 /**
882 * Disconnect any client still connected to the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800883 * @hide
Chong Zhang47e36a32016-02-29 16:44:33 -0800884 */
885 public void disconnect() {
886 if (mNativeObject != 0) {
887 nativeDisconnect(mNativeObject);
888 }
889 }
890
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800891 private void checkNotReleased() {
892 if (mNativeObject == 0) throw new NullPointerException(
893 "mNativeObject is null. Have you called release() already?");
894 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700895
Robert Carra7827f72019-01-11 12:53:37 -0800896 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800897 * Check whether this instance points to a valid layer with the system-compositor. For
898 * example this may be false if construction failed, or the layer was released.
899 *
900 * @return Whether this SurfaceControl is valid.
Robert Carra7827f72019-01-11 12:53:37 -0800901 */
Robert Carr5fea55b2018-12-10 13:05:52 -0800902 public boolean isValid() {
903 return mNativeObject != 0;
904 }
905
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800906 /*
907 * set surface parameters.
908 * needs to be inside open/closeTransaction block
909 */
910
Robert Carra7827f72019-01-11 12:53:37 -0800911 /** start a transaction
912 * @hide
913 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100914 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800915 public static void openTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -0700916 synchronized (SurfaceControl.class) {
917 if (sGlobalTransaction == null) {
918 sGlobalTransaction = new Transaction();
919 }
920 synchronized(SurfaceControl.class) {
921 sTransactionNestCount++;
922 }
923 }
924 }
925
926 private static void closeTransaction(boolean sync) {
927 synchronized(SurfaceControl.class) {
928 if (sTransactionNestCount == 0) {
929 Log.e(TAG, "Call to SurfaceControl.closeTransaction without matching openTransaction");
930 } else if (--sTransactionNestCount > 0) {
931 return;
932 }
933 sGlobalTransaction.apply(sync);
934 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800935 }
936
Robert Carrb1579c82017-09-05 14:54:47 -0700937 /**
938 * Merge the supplied transaction in to the deprecated "global" transaction.
939 * This clears the supplied transaction in an identical fashion to {@link Transaction#merge}.
940 * <p>
941 * This is a utility for interop with legacy-code and will go away with the Global Transaction.
Robert Carra7827f72019-01-11 12:53:37 -0800942 * @hide
Robert Carrb1579c82017-09-05 14:54:47 -0700943 */
944 @Deprecated
945 public static void mergeToGlobalTransaction(Transaction t) {
Robert Carrc5fc4612017-12-05 11:55:40 -0800946 synchronized(SurfaceControl.class) {
Robert Carrb1579c82017-09-05 14:54:47 -0700947 sGlobalTransaction.merge(t);
948 }
949 }
950
Robert Carra7827f72019-01-11 12:53:37 -0800951 /** end a transaction
952 * @hide
953 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100954 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800955 public static void closeTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -0700956 closeTransaction(false);
Robert Carre9953b12016-05-23 20:52:04 -0700957 }
958
Robert Carra7827f72019-01-11 12:53:37 -0800959 /**
960 * @hide
961 */
Robert Carre9953b12016-05-23 20:52:04 -0700962 public static void closeTransactionSync() {
Robert Carre13b58e2017-08-31 14:50:44 -0700963 closeTransaction(true);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800964 }
965
Robert Carra7827f72019-01-11 12:53:37 -0800966 /**
967 * @hide
968 */
Rob Carr64e516f2015-10-29 00:20:45 +0000969 public void deferTransactionUntil(IBinder handle, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -0800970 synchronized(SurfaceControl.class) {
971 sGlobalTransaction.deferTransactionUntil(this, handle, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800972 }
973 }
974
Robert Carra7827f72019-01-11 12:53:37 -0800975 /**
976 * @hide
977 */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800978 public void deferTransactionUntil(Surface barrier, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -0800979 synchronized(SurfaceControl.class) {
980 sGlobalTransaction.deferTransactionUntilSurface(this, barrier, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800981 }
982 }
983
Robert Carra7827f72019-01-11 12:53:37 -0800984 /**
985 * @hide
986 */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800987 public void reparentChildren(IBinder newParentHandle) {
Robert Carre13b58e2017-08-31 14:50:44 -0700988 synchronized(SurfaceControl.class) {
989 sGlobalTransaction.reparentChildren(this, newParentHandle);
990 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800991 }
992
Robert Carra7827f72019-01-11 12:53:37 -0800993 /**
994 * @hide
995 */
Robert Carr10584fa2019-01-14 15:55:19 -0800996 public void reparent(SurfaceControl newParent) {
Robert Carre13b58e2017-08-31 14:50:44 -0700997 synchronized(SurfaceControl.class) {
Robert Carr10584fa2019-01-14 15:55:19 -0800998 sGlobalTransaction.reparent(this, newParent);
Robert Carre13b58e2017-08-31 14:50:44 -0700999 }
chaviw63542382017-08-17 17:39:29 -07001000 }
1001
Robert Carra7827f72019-01-11 12:53:37 -08001002 /**
1003 * @hide
1004 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001005 public void detachChildren() {
Robert Carre13b58e2017-08-31 14:50:44 -07001006 synchronized(SurfaceControl.class) {
1007 sGlobalTransaction.detachChildren(this);
1008 }
Rob Carr64e516f2015-10-29 00:20:45 +00001009 }
1010
Robert Carra7827f72019-01-11 12:53:37 -08001011 /**
1012 * @hide
1013 */
Robert Carr1ca6a332016-04-11 18:00:43 -07001014 public void setOverrideScalingMode(int scalingMode) {
1015 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001016 synchronized(SurfaceControl.class) {
1017 sGlobalTransaction.setOverrideScalingMode(this, scalingMode);
1018 }
Robert Carr1ca6a332016-04-11 18:00:43 -07001019 }
1020
Robert Carra7827f72019-01-11 12:53:37 -08001021 /**
1022 * @hide
1023 */
Rob Carr64e516f2015-10-29 00:20:45 +00001024 public IBinder getHandle() {
1025 return nativeGetHandle(mNativeObject);
1026 }
1027
Robert Carra7827f72019-01-11 12:53:37 -08001028 /**
1029 * @hide
1030 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001031 public static void setAnimationTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -07001032 synchronized (SurfaceControl.class) {
1033 sGlobalTransaction.setAnimationTransaction();
1034 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001035 }
1036
Robert Carra7827f72019-01-11 12:53:37 -08001037 /**
1038 * @hide
1039 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001040 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001041 public void setLayer(int zorder) {
1042 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001043 synchronized(SurfaceControl.class) {
1044 sGlobalTransaction.setLayer(this, zorder);
1045 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001046 }
1047
Robert Carra7827f72019-01-11 12:53:37 -08001048 /**
1049 * @hide
1050 */
Robert Carr77e34942017-10-18 19:13:56 -07001051 public void setRelativeLayer(SurfaceControl relativeTo, int zorder) {
Robert Carraf422a82017-04-10 18:34:33 -07001052 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001053 synchronized(SurfaceControl.class) {
1054 sGlobalTransaction.setRelativeLayer(this, relativeTo, zorder);
1055 }
Robert Carraf422a82017-04-10 18:34:33 -07001056 }
1057
Robert Carra7827f72019-01-11 12:53:37 -08001058 /**
1059 * @hide
1060 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001061 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001062 public void setPosition(float x, float y) {
1063 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001064 synchronized(SurfaceControl.class) {
1065 sGlobalTransaction.setPosition(this, x, y);
1066 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001067 }
1068
Robert Carra7827f72019-01-11 12:53:37 -08001069 /**
1070 * @hide
1071 */
Robert Carr6da3cc02016-06-16 15:17:07 -07001072 public void setGeometryAppliesWithResize() {
Robert Carra9408d42016-06-03 13:28:48 -07001073 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001074 synchronized(SurfaceControl.class) {
1075 sGlobalTransaction.setGeometryAppliesWithResize(this);
1076 }
Robert Carra9408d42016-06-03 13:28:48 -07001077 }
1078
Robert Carra7827f72019-01-11 12:53:37 -08001079 /**
1080 * @hide
1081 */
Vishnu Naire86bd982018-11-28 13:23:17 -08001082 public void setBufferSize(int w, int h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001083 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001084 synchronized(SurfaceControl.class) {
Vishnu Naire86bd982018-11-28 13:23:17 -08001085 sGlobalTransaction.setBufferSize(this, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07001086 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001087 }
1088
Robert Carra7827f72019-01-11 12:53:37 -08001089 /**
1090 * @hide
1091 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001092 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001093 public void hide() {
1094 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001095 synchronized(SurfaceControl.class) {
1096 sGlobalTransaction.hide(this);
1097 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001098 }
1099
Robert Carra7827f72019-01-11 12:53:37 -08001100 /**
1101 * @hide
1102 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001103 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001104 public void show() {
1105 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001106 synchronized(SurfaceControl.class) {
1107 sGlobalTransaction.show(this);
1108 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001109 }
1110
Robert Carra7827f72019-01-11 12:53:37 -08001111 /**
1112 * @hide
1113 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001114 public void setTransparentRegionHint(Region region) {
1115 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001116 synchronized(SurfaceControl.class) {
1117 sGlobalTransaction.setTransparentRegionHint(this, region);
1118 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001119 }
1120
Robert Carra7827f72019-01-11 12:53:37 -08001121 /**
1122 * @hide
1123 */
Svetoslav1376d602014-03-13 11:17:26 -07001124 public boolean clearContentFrameStats() {
1125 checkNotReleased();
1126 return nativeClearContentFrameStats(mNativeObject);
1127 }
1128
Robert Carra7827f72019-01-11 12:53:37 -08001129 /**
1130 * @hide
1131 */
Svetoslav1376d602014-03-13 11:17:26 -07001132 public boolean getContentFrameStats(WindowContentFrameStats outStats) {
1133 checkNotReleased();
1134 return nativeGetContentFrameStats(mNativeObject, outStats);
1135 }
1136
Robert Carra7827f72019-01-11 12:53:37 -08001137 /**
1138 * @hide
1139 */
Svetoslav1376d602014-03-13 11:17:26 -07001140 public static boolean clearAnimationFrameStats() {
1141 return nativeClearAnimationFrameStats();
1142 }
1143
Robert Carra7827f72019-01-11 12:53:37 -08001144 /**
1145 * @hide
1146 */
Svetoslav1376d602014-03-13 11:17:26 -07001147 public static boolean getAnimationFrameStats(WindowAnimationFrameStats outStats) {
1148 return nativeGetAnimationFrameStats(outStats);
1149 }
1150
Robert Carra7827f72019-01-11 12:53:37 -08001151 /**
1152 * @hide
1153 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001154 public void setAlpha(float alpha) {
1155 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001156 synchronized(SurfaceControl.class) {
1157 sGlobalTransaction.setAlpha(this, alpha);
1158 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001159 }
1160
Robert Carra7827f72019-01-11 12:53:37 -08001161 /**
1162 * @hide
1163 */
chaviw0dd03f52017-08-25 12:15:26 -07001164 public void setColor(@Size(3) float[] color) {
1165 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001166 synchronized (SurfaceControl.class) {
1167 sGlobalTransaction.setColor(this, color);
1168 }
chaviw0dd03f52017-08-25 12:15:26 -07001169 }
1170
Robert Carra7827f72019-01-11 12:53:37 -08001171 /**
1172 * @hide
1173 */
Robert Carr0edf18f2017-02-21 20:01:47 -08001174 public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001175 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001176 synchronized(SurfaceControl.class) {
1177 sGlobalTransaction.setMatrix(this, dsdx, dtdx, dtdy, dsdy);
1178 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001179 }
1180
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001181 /**
1182 * Sets the transform and position of a {@link SurfaceControl} from a 3x3 transformation matrix.
1183 *
1184 * @param matrix The matrix to apply.
1185 * @param float9 An array of 9 floats to be used to extract the values from the matrix.
Robert Carra7827f72019-01-11 12:53:37 -08001186 * @hide
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001187 */
1188 public void setMatrix(Matrix matrix, float[] float9) {
1189 checkNotReleased();
1190 matrix.getValues(float9);
1191 synchronized (SurfaceControl.class) {
1192 sGlobalTransaction.setMatrix(this, float9[MSCALE_X], float9[MSKEW_Y],
1193 float9[MSKEW_X], float9[MSCALE_Y]);
1194 sGlobalTransaction.setPosition(this, float9[MTRANS_X], float9[MTRANS_Y]);
1195 }
1196 }
1197
Peiyong Lin52bb6b42018-10-01 11:40:50 -07001198 /**
1199 * Sets the color transform for the Surface.
1200 * @param matrix A float array with 9 values represents a 3x3 transform matrix
1201 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08001202 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07001203 */
1204 public void setColorTransform(@Size(9) float[] matrix, @Size(3) float[] translation) {
1205 checkNotReleased();
1206 synchronized (SurfaceControl.class) {
1207 sGlobalTransaction.setColorTransform(this, matrix, translation);
1208 }
1209 }
1210
Vishnu Naird454442d2018-11-13 13:51:01 -08001211 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08001212 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
1213 * the color can be interpreted in any color space.
1214 * @param agnostic A boolean to indicate whether the surface is color space agnostic
1215 * @hide
1216 */
1217 public void setColorSpaceAgnostic(boolean agnostic) {
1218 checkNotReleased();
1219 synchronized (SurfaceControl.class) {
1220 sGlobalTransaction.setColorSpaceAgnostic(this, agnostic);
1221 }
1222 }
1223
1224 /**
Vishnu Naird454442d2018-11-13 13:51:01 -08001225 * Bounds the surface and its children to the bounds specified. Size of the surface will be
1226 * ignored and only the crop and buffer size will be used to determine the bounds of the
1227 * surface. If no crop is specified and the surface has no buffer, the surface bounds is only
1228 * constrained by the size of its parent bounds.
1229 *
1230 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08001231 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001232 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001233 public void setWindowCrop(Rect crop) {
1234 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001235 synchronized (SurfaceControl.class) {
1236 sGlobalTransaction.setWindowCrop(this, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001237 }
1238 }
1239
Vishnu Naird454442d2018-11-13 13:51:01 -08001240 /**
1241 * Same as {@link SurfaceControl#setWindowCrop(Rect)} but sets the crop rect top left at 0, 0.
1242 *
1243 * @param width width of crop rect
1244 * @param height height of crop rect
Robert Carra7827f72019-01-11 12:53:37 -08001245 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001246 */
1247 public void setWindowCrop(int width, int height) {
1248 checkNotReleased();
1249 synchronized (SurfaceControl.class) {
1250 sGlobalTransaction.setWindowCrop(this, width, height);
1251 }
1252 }
1253
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07001254 /**
1255 * Sets the corner radius of a {@link SurfaceControl}.
1256 *
1257 * @param cornerRadius Corner radius in pixels.
Robert Carra7827f72019-01-11 12:53:37 -08001258 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07001259 */
1260 public void setCornerRadius(float cornerRadius) {
1261 checkNotReleased();
1262 synchronized (SurfaceControl.class) {
1263 sGlobalTransaction.setCornerRadius(this, cornerRadius);
1264 }
1265 }
1266
Robert Carra7827f72019-01-11 12:53:37 -08001267 /**
1268 * @hide
1269 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001270 public void setLayerStack(int layerStack) {
1271 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001272 synchronized(SurfaceControl.class) {
1273 sGlobalTransaction.setLayerStack(this, layerStack);
1274 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001275 }
1276
Robert Carra7827f72019-01-11 12:53:37 -08001277 /**
1278 * @hide
1279 */
Andy McFadden314405b2014-01-29 17:18:05 -08001280 public void setOpaque(boolean isOpaque) {
1281 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001282
1283 synchronized (SurfaceControl.class) {
1284 sGlobalTransaction.setOpaque(this, isOpaque);
Andy McFadden314405b2014-01-29 17:18:05 -08001285 }
1286 }
1287
Robert Carra7827f72019-01-11 12:53:37 -08001288 /**
1289 * @hide
1290 */
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001291 public void setSecure(boolean isSecure) {
1292 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001293
1294 synchronized (SurfaceControl.class) {
1295 sGlobalTransaction.setSecure(this, isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001296 }
1297 }
1298
Robert Carra7827f72019-01-11 12:53:37 -08001299 /**
1300 * @hide
1301 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001302 public int getWidth() {
1303 synchronized (mSizeLock) {
1304 return mWidth;
1305 }
1306 }
1307
Robert Carra7827f72019-01-11 12:53:37 -08001308 /**
1309 * @hide
1310 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001311 public int getHeight() {
1312 synchronized (mSizeLock) {
1313 return mHeight;
1314 }
1315 }
1316
Robert Carre13b58e2017-08-31 14:50:44 -07001317 @Override
1318 public String toString() {
1319 return "Surface(name=" + mName + ")/@0x" +
1320 Integer.toHexString(System.identityHashCode(this));
1321 }
1322
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001323 /*
1324 * set display parameters.
1325 * needs to be inside open/closeTransaction block
1326 */
1327
1328 /**
1329 * Describes the properties of a physical display known to surface flinger.
Robert Carr76907ee2019-01-11 13:38:19 -08001330 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001331 */
1332 public static final class PhysicalDisplayInfo {
Robert Carra7827f72019-01-11 12:53:37 -08001333 /**
1334 * @hide
1335 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001336 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001337 public int width;
Robert Carra7827f72019-01-11 12:53:37 -08001338
1339 /**
1340 * @hide
1341 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001342 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001343 public int height;
Robert Carra7827f72019-01-11 12:53:37 -08001344
1345 /**
1346 * @hide
1347 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001348 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001349 public float refreshRate;
Robert Carra7827f72019-01-11 12:53:37 -08001350
1351 /**
1352 * @hide
1353 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001354 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001355 public float density;
Robert Carra7827f72019-01-11 12:53:37 -08001356
1357 /**
1358 * @hide
1359 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001360 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001361 public float xDpi;
Robert Carra7827f72019-01-11 12:53:37 -08001362
1363 /**
1364 * @hide
1365 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001366 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001367 public float yDpi;
Robert Carra7827f72019-01-11 12:53:37 -08001368
1369 /**
1370 * @hide
1371 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001372 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001373 public boolean secure;
Robert Carra7827f72019-01-11 12:53:37 -08001374
1375 /**
1376 * @hide
1377 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001378 @UnsupportedAppUsage
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001379 public long appVsyncOffsetNanos;
Robert Carra7827f72019-01-11 12:53:37 -08001380
1381 /**
1382 * @hide
1383 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001384 @UnsupportedAppUsage
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001385 public long presentationDeadlineNanos;
Jesse Hall6a6bc212013-08-08 12:15:03 -07001386
Robert Carra7827f72019-01-11 12:53:37 -08001387 /**
1388 * @hide
1389 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001390 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001391 public PhysicalDisplayInfo() {
1392 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001393
Robert Carra7827f72019-01-11 12:53:37 -08001394 /**
1395 * @hide
1396 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001397 public PhysicalDisplayInfo(PhysicalDisplayInfo other) {
1398 copyFrom(other);
1399 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001400
Robert Carra7827f72019-01-11 12:53:37 -08001401 /**
1402 * @hide
1403 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001404 @Override
1405 public boolean equals(Object o) {
1406 return o instanceof PhysicalDisplayInfo && equals((PhysicalDisplayInfo)o);
1407 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001408
Robert Carra7827f72019-01-11 12:53:37 -08001409 /**
1410 * @hide
1411 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001412 public boolean equals(PhysicalDisplayInfo other) {
1413 return other != null
1414 && width == other.width
1415 && height == other.height
1416 && refreshRate == other.refreshRate
1417 && density == other.density
1418 && xDpi == other.xDpi
1419 && yDpi == other.yDpi
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001420 && secure == other.secure
1421 && appVsyncOffsetNanos == other.appVsyncOffsetNanos
Michael Wright1c9977b2016-07-12 13:30:10 -07001422 && presentationDeadlineNanos == other.presentationDeadlineNanos;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001423 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001424
Robert Carra7827f72019-01-11 12:53:37 -08001425 /**
1426 * @hide
1427 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001428 @Override
1429 public int hashCode() {
1430 return 0; // don't care
1431 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001432
Robert Carra7827f72019-01-11 12:53:37 -08001433 /**
1434 * @hide
1435 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001436 public void copyFrom(PhysicalDisplayInfo other) {
1437 width = other.width;
1438 height = other.height;
1439 refreshRate = other.refreshRate;
1440 density = other.density;
1441 xDpi = other.xDpi;
1442 yDpi = other.yDpi;
1443 secure = other.secure;
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001444 appVsyncOffsetNanos = other.appVsyncOffsetNanos;
1445 presentationDeadlineNanos = other.presentationDeadlineNanos;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001446 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001447
Robert Carra7827f72019-01-11 12:53:37 -08001448 /**
1449 * @hide
1450 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001451 @Override
1452 public String toString() {
1453 return "PhysicalDisplayInfo{" + width + " x " + height + ", " + refreshRate + " fps, "
1454 + "density " + density + ", " + xDpi + " x " + yDpi + " dpi, secure " + secure
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001455 + ", appVsyncOffset " + appVsyncOffsetNanos
Michael Wright1c9977b2016-07-12 13:30:10 -07001456 + ", bufferDeadline " + presentationDeadlineNanos + "}";
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001457 }
1458 }
1459
Robert Carra7827f72019-01-11 12:53:37 -08001460 /**
1461 * @hide
1462 */
Prashant Malanic55929a2014-05-25 01:59:21 -07001463 public static void setDisplayPowerMode(IBinder displayToken, int mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001464 if (displayToken == null) {
1465 throw new IllegalArgumentException("displayToken must not be null");
1466 }
Prashant Malanic55929a2014-05-25 01:59:21 -07001467 nativeSetDisplayPowerMode(displayToken, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001468 }
1469
Robert Carra7827f72019-01-11 12:53:37 -08001470 /**
1471 * @hide
1472 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001473 @UnsupportedAppUsage
Dan Stoza00101052014-05-02 15:23:40 -07001474 public static SurfaceControl.PhysicalDisplayInfo[] getDisplayConfigs(IBinder displayToken) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001475 if (displayToken == null) {
1476 throw new IllegalArgumentException("displayToken must not be null");
1477 }
Dan Stoza00101052014-05-02 15:23:40 -07001478 return nativeGetDisplayConfigs(displayToken);
1479 }
1480
Robert Carra7827f72019-01-11 12:53:37 -08001481 /**
1482 * @hide
1483 */
Dan Stoza00101052014-05-02 15:23:40 -07001484 public static int getActiveConfig(IBinder displayToken) {
1485 if (displayToken == null) {
1486 throw new IllegalArgumentException("displayToken must not be null");
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001487 }
Dan Stoza00101052014-05-02 15:23:40 -07001488 return nativeGetActiveConfig(displayToken);
1489 }
1490
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001491 /**
1492 * @hide
1493 */
1494 public static DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
1495 IBinder displayToken) {
1496 if (displayToken == null) {
1497 throw new IllegalArgumentException("displayToken must not be null");
1498 }
1499 return nativeGetDisplayedContentSamplingAttributes(displayToken);
1500 }
1501
1502 /**
1503 * @hide
1504 */
1505 public static boolean setDisplayedContentSamplingEnabled(
1506 IBinder displayToken, boolean enable, int componentMask, int maxFrames) {
1507 if (displayToken == null) {
1508 throw new IllegalArgumentException("displayToken must not be null");
1509 }
1510 final int maxColorComponents = 4;
1511 if ((componentMask >> maxColorComponents) != 0) {
1512 throw new IllegalArgumentException("invalid componentMask when enabling sampling");
1513 }
1514 return nativeSetDisplayedContentSamplingEnabled(
1515 displayToken, enable, componentMask, maxFrames);
1516 }
1517
1518 /**
1519 * @hide
1520 */
1521 public static DisplayedContentSample getDisplayedContentSample(
1522 IBinder displayToken, long maxFrames, long timestamp) {
1523 if (displayToken == null) {
1524 throw new IllegalArgumentException("displayToken must not be null");
1525 }
1526 return nativeGetDisplayedContentSample(displayToken, maxFrames, timestamp);
1527 }
1528
1529
Robert Carra7827f72019-01-11 12:53:37 -08001530 /**
1531 * @hide
1532 */
Dan Stoza00101052014-05-02 15:23:40 -07001533 public static boolean setActiveConfig(IBinder displayToken, int id) {
1534 if (displayToken == null) {
1535 throw new IllegalArgumentException("displayToken must not be null");
1536 }
1537 return nativeSetActiveConfig(displayToken, id);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001538 }
1539
Robert Carra7827f72019-01-11 12:53:37 -08001540 /**
1541 * @hide
1542 */
Ady Abraham6070ce12019-01-24 18:48:58 -08001543 public static boolean setAllowedDisplayConfigs(IBinder displayToken, int[] allowedConfigs) {
1544 if (displayToken == null) {
1545 throw new IllegalArgumentException("displayToken must not be null");
1546 }
1547 if (allowedConfigs == null) {
1548 throw new IllegalArgumentException("allowedConfigs must not be null");
1549 }
1550
1551 return nativeSetAllowedDisplayConfigs(displayToken, allowedConfigs);
1552 }
1553
1554 /**
1555 * @hide
1556 */
Ady Abraham42f9a2fb2019-02-26 14:13:39 -08001557 public static int[] getAllowedDisplayConfigs(IBinder displayToken) {
1558 if (displayToken == null) {
1559 throw new IllegalArgumentException("displayToken must not be null");
1560 }
1561 return nativeGetAllowedDisplayConfigs(displayToken);
1562 }
1563
1564 /**
1565 * @hide
1566 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001567 public static int[] getDisplayColorModes(IBinder displayToken) {
1568 if (displayToken == null) {
1569 throw new IllegalArgumentException("displayToken must not be null");
1570 }
1571 return nativeGetDisplayColorModes(displayToken);
1572 }
1573
Robert Carra7827f72019-01-11 12:53:37 -08001574 /**
Daniel Solomon10e3b332019-01-20 21:09:11 -08001575 * Color coordinates in CIE1931 XYZ color space
1576 *
1577 * @hide
1578 */
1579 public static final class CieXyz {
1580 /**
1581 * @hide
1582 */
1583 public float X;
1584
1585 /**
1586 * @hide
1587 */
1588 public float Y;
1589
1590 /**
1591 * @hide
1592 */
1593 public float Z;
1594 }
1595
1596 /**
1597 * Contains a display's color primaries
1598 *
1599 * @hide
1600 */
1601 public static final class DisplayPrimaries {
1602 /**
1603 * @hide
1604 */
1605 public CieXyz red;
1606
1607 /**
1608 * @hide
1609 */
1610 public CieXyz green;
1611
1612 /**
1613 * @hide
1614 */
1615 public CieXyz blue;
1616
1617 /**
1618 * @hide
1619 */
1620 public CieXyz white;
1621
1622 /**
1623 * @hide
1624 */
1625 public DisplayPrimaries() {
1626 }
1627 }
1628
1629 /**
1630 * @hide
1631 */
1632 public static SurfaceControl.DisplayPrimaries getDisplayNativePrimaries(
1633 IBinder displayToken) {
1634 if (displayToken == null) {
1635 throw new IllegalArgumentException("displayToken must not be null");
1636 }
1637
1638 return nativeGetDisplayNativePrimaries(displayToken);
1639 }
1640
1641 /**
Robert Carra7827f72019-01-11 12:53:37 -08001642 * @hide
1643 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001644 public static int getActiveColorMode(IBinder displayToken) {
1645 if (displayToken == null) {
1646 throw new IllegalArgumentException("displayToken must not be null");
1647 }
1648 return nativeGetActiveColorMode(displayToken);
1649 }
1650
Robert Carra7827f72019-01-11 12:53:37 -08001651 /**
1652 * @hide
1653 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001654 public static boolean setActiveColorMode(IBinder displayToken, int colorMode) {
1655 if (displayToken == null) {
1656 throw new IllegalArgumentException("displayToken must not be null");
1657 }
1658 return nativeSetActiveColorMode(displayToken, colorMode);
1659 }
1660
Robert Carra7827f72019-01-11 12:53:37 -08001661 /**
Peiyong Lin5f4a5682019-01-17 11:37:07 -08001662 * Returns an array of color spaces with 2 elements. The first color space is the
1663 * default color space and second one is wide color gamut color space.
1664 * @hide
1665 */
1666 public static ColorSpace[] getCompositionColorSpaces() {
1667 int[] dataspaces = nativeGetCompositionDataspaces();
1668 ColorSpace srgb = ColorSpace.get(ColorSpace.Named.SRGB);
1669 ColorSpace[] colorSpaces = { srgb, srgb };
1670 if (dataspaces.length == 2) {
1671 for (int i = 0; i < 2; ++i) {
1672 switch(dataspaces[i]) {
1673 case INTERNAL_DATASPACE_DISPLAY_P3:
1674 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.DISPLAY_P3);
1675 break;
1676 case INTERNAL_DATASPACE_SCRGB:
1677 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.EXTENDED_SRGB);
1678 break;
1679 case INTERNAL_DATASPACE_SRGB:
1680 // Other dataspace is not recognized, use SRGB color space instead,
1681 // the default value of the array is already SRGB, thus do nothing.
1682 default:
1683 break;
1684 }
1685 }
1686 }
1687 return colorSpaces;
1688 }
1689
1690 /**
Robert Carra7827f72019-01-11 12:53:37 -08001691 * @hide
1692 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001693 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001694 public static void setDisplayProjection(IBinder displayToken,
1695 int orientation, Rect layerStackRect, Rect displayRect) {
Robert Carre13b58e2017-08-31 14:50:44 -07001696 synchronized (SurfaceControl.class) {
1697 sGlobalTransaction.setDisplayProjection(displayToken, orientation,
1698 layerStackRect, displayRect);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001699 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001700 }
1701
Robert Carra7827f72019-01-11 12:53:37 -08001702 /**
1703 * @hide
1704 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001705 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001706 public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -07001707 synchronized (SurfaceControl.class) {
1708 sGlobalTransaction.setDisplayLayerStack(displayToken, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001709 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001710 }
1711
Robert Carra7827f72019-01-11 12:53:37 -08001712 /**
1713 * @hide
1714 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001715 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001716 public static void setDisplaySurface(IBinder displayToken, Surface surface) {
Robert Carre13b58e2017-08-31 14:50:44 -07001717 synchronized (SurfaceControl.class) {
1718 sGlobalTransaction.setDisplaySurface(displayToken, surface);
Jeff Brownfc0ebd72013-04-30 16:33:00 -07001719 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001720 }
1721
Robert Carra7827f72019-01-11 12:53:37 -08001722 /**
1723 * @hide
1724 */
Michael Wright01e840f2014-06-26 16:03:25 -07001725 public static void setDisplaySize(IBinder displayToken, int width, int height) {
Robert Carre13b58e2017-08-31 14:50:44 -07001726 synchronized (SurfaceControl.class) {
1727 sGlobalTransaction.setDisplaySize(displayToken, width, height);
Michael Wright01e840f2014-06-26 16:03:25 -07001728 }
Michael Wright01e840f2014-06-26 16:03:25 -07001729 }
1730
Robert Carra7827f72019-01-11 12:53:37 -08001731 /**
1732 * @hide
1733 */
Michael Wright9ff94c02016-03-30 18:05:40 -07001734 public static Display.HdrCapabilities getHdrCapabilities(IBinder displayToken) {
1735 if (displayToken == null) {
1736 throw new IllegalArgumentException("displayToken must not be null");
1737 }
1738 return nativeGetHdrCapabilities(displayToken);
1739 }
1740
Robert Carra7827f72019-01-11 12:53:37 -08001741 /**
1742 * @hide
1743 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001744 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001745 public static IBinder createDisplay(String name, boolean secure) {
1746 if (name == null) {
1747 throw new IllegalArgumentException("name must not be null");
1748 }
1749 return nativeCreateDisplay(name, secure);
1750 }
1751
Robert Carra7827f72019-01-11 12:53:37 -08001752 /**
1753 * @hide
1754 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001755 @UnsupportedAppUsage
Jesse Hall6a6bc212013-08-08 12:15:03 -07001756 public static void destroyDisplay(IBinder displayToken) {
1757 if (displayToken == null) {
1758 throw new IllegalArgumentException("displayToken must not be null");
1759 }
1760 nativeDestroyDisplay(displayToken);
1761 }
1762
Robert Carra7827f72019-01-11 12:53:37 -08001763 /**
1764 * @hide
1765 */
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001766 public static long[] getPhysicalDisplayIds() {
1767 return nativeGetPhysicalDisplayIds();
1768 }
1769
1770 /**
1771 * @hide
1772 */
1773 public static IBinder getPhysicalDisplayToken(long physicalDisplayId) {
1774 return nativeGetPhysicalDisplayToken(physicalDisplayId);
1775 }
1776
1777 /**
1778 * TODO(116025192): Remove this stopgap once framework is display-agnostic.
1779 *
1780 * @hide
1781 */
1782 public static IBinder getInternalDisplayToken() {
1783 final long[] physicalDisplayIds = getPhysicalDisplayIds();
1784 if (physicalDisplayIds.length == 0) {
1785 return null;
1786 }
1787 return getPhysicalDisplayToken(physicalDisplayIds[0]);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001788 }
1789
Mathias Agopian0449a402013-03-01 23:01:51 -08001790 /**
chaviw08520a02018-09-10 16:44:56 -07001791 * @see SurfaceControl#screenshot(IBinder, Surface, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001792 * @hide
Mathias Agopian0449a402013-03-01 23:01:51 -08001793 */
1794 public static void screenshot(IBinder display, Surface consumer) {
chaviw08520a02018-09-10 16:44:56 -07001795 screenshot(display, consumer, new Rect(), 0, 0, false, 0);
1796 }
1797
1798 /**
1799 * Copy the current screen contents into the provided {@link Surface}
1800 *
1801 * @param consumer The {@link Surface} to take the screenshot into.
1802 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001803 * @hide
chaviw08520a02018-09-10 16:44:56 -07001804 */
1805 public static void screenshot(IBinder display, Surface consumer, Rect sourceCrop, int width,
1806 int height, boolean useIdentityTransform, int rotation) {
1807 if (consumer == null) {
1808 throw new IllegalArgumentException("consumer must not be null");
1809 }
1810
1811 final GraphicBuffer buffer = screenshotToBuffer(display, sourceCrop, width, height,
1812 useIdentityTransform, rotation);
1813 try {
1814 consumer.attachAndQueueBuffer(buffer);
1815 } catch (RuntimeException e) {
1816 Log.w(TAG, "Failed to take screenshot - " + e.getMessage());
1817 }
1818 }
1819
1820 /**
1821 * @see SurfaceControl#screenshot(Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001822 * @hide
chaviw08520a02018-09-10 16:44:56 -07001823 */
1824 @UnsupportedAppUsage
1825 public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) {
1826 return screenshot(sourceCrop, width, height, false, rotation);
Mathias Agopian0449a402013-03-01 23:01:51 -08001827 }
1828
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001829 /**
chaviw1da9cd92017-12-06 10:48:11 -08001830 * Copy the current screen contents into a hardware bitmap and return it.
1831 * Note: If you want to modify the Bitmap in software, you will need to copy the Bitmap into
1832 * a software Bitmap using {@link Bitmap#copy(Bitmap.Config, boolean)}
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001833 *
chaviw08520a02018-09-10 16:44:56 -07001834 * CAVEAT: Versions of screenshot that return a {@link Bitmap} can be extremely slow; avoid use
1835 * unless absolutely necessary; prefer the versions that use a {@link Surface} such as
1836 * {@link SurfaceControl#screenshot(IBinder, Surface)} or {@link GraphicBuffer} such as
1837 * {@link SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}.
Mathias Agopian0449a402013-03-01 23:01:51 -08001838 *
chaviw08520a02018-09-10 16:44:56 -07001839 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001840 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001841 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001842 @UnsupportedAppUsage
Dan Stoza9890e3412014-05-22 16:12:54 -07001843 public static Bitmap screenshot(Rect sourceCrop, int width, int height,
chaviw08520a02018-09-10 16:44:56 -07001844 boolean useIdentityTransform, int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001845 // TODO: should take the display as a parameter
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001846 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
1847 if (displayToken == null) {
1848 Log.w(TAG, "Failed to take screenshot because internal display is disconnected");
1849 return null;
1850 }
1851
chaviwa69e0a72017-11-29 17:55:12 -08001852 if (rotation == ROTATION_90 || rotation == ROTATION_270) {
1853 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
1854 }
1855
1856 SurfaceControl.rotateCropForSF(sourceCrop, rotation);
chaviw08520a02018-09-10 16:44:56 -07001857 final GraphicBuffer buffer = screenshotToBuffer(displayToken, sourceCrop, width, height,
1858 useIdentityTransform, rotation);
1859
1860 if (buffer == null) {
1861 Log.w(TAG, "Failed to take screenshot");
1862 return null;
1863 }
Peiyong Lin9d427402019-01-23 18:39:06 -08001864 // TODO(b/116112787) Now that hardware bitmap creation can take color space, we
1865 // should continue to fix screenshot.
1866 return Bitmap.wrapHardwareBuffer(HardwareBuffer.createFromGraphicBuffer(buffer),
1867 ColorSpace.get(ColorSpace.Named.SRGB));
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001868 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001869
chaviw08520a02018-09-10 16:44:56 -07001870 /**
1871 * Captures all the surfaces in a display and returns a {@link GraphicBuffer} with the content.
1872 *
1873 * @param display The display to take the screenshot of.
1874 * @param sourceCrop The portion of the screen to capture into the Bitmap; caller may
1875 * pass in 'new Rect()' if no cropping is desired.
1876 * @param width The desired width of the returned bitmap; the raw screen will be
1877 * scaled down to this size; caller may pass in 0 if no scaling is
1878 * desired.
1879 * @param height The desired height of the returned bitmap; the raw screen will
1880 * be scaled down to this size; caller may pass in 0 if no scaling
1881 * is desired.
1882 * @param useIdentityTransform Replace whatever transformation (rotation, scaling, translation)
1883 * the surface layers are currently using with the identity
1884 * transformation while taking the screenshot.
1885 * @param rotation Apply a custom clockwise rotation to the screenshot, i.e.
1886 * Surface.ROTATION_0,90,180,270. SurfaceFlinger will always take
1887 * screenshots in its native portrait orientation by default, so
1888 * this is useful for returning screenshots that are independent of
1889 * device orientation.
1890 * @return Returns a GraphicBuffer that contains the captured content.
Robert Carra7827f72019-01-11 12:53:37 -08001891 * @hide
chaviw08520a02018-09-10 16:44:56 -07001892 */
1893 public static GraphicBuffer screenshotToBuffer(IBinder display, Rect sourceCrop, int width,
1894 int height, boolean useIdentityTransform, int rotation) {
Mathias Agopian0449a402013-03-01 23:01:51 -08001895 if (display == null) {
1896 throw new IllegalArgumentException("displayToken must not be null");
1897 }
chaviw08520a02018-09-10 16:44:56 -07001898
Robert Carr5c52b132019-02-15 15:48:11 -08001899 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1900 false /* captureSecureLayers */);
1901 }
1902
1903 /**
1904 * Like screenshotToBuffer, but if the caller is AID_SYSTEM, allows
1905 * for the capture of secure layers. This is used for the screen rotation
1906 * animation where the system server takes screenshots but does
1907 * not persist them or allow them to leave the server. However in other
1908 * cases in the system server, we mostly want to omit secure layers
1909 * like when we take a screenshot on behalf of the assistant.
1910 *
1911 * @hide
1912 */
1913 public static GraphicBuffer screenshotToBufferWithSecureLayersUnsafe(IBinder display,
1914 Rect sourceCrop, int width, int height, boolean useIdentityTransform,
1915 int rotation) {
1916 if (display == null) {
1917 throw new IllegalArgumentException("displayToken must not be null");
1918 }
1919
1920 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1921 true /* captureSecureLayers */);
Mathias Agopian0449a402013-03-01 23:01:51 -08001922 }
Robert Carre13b58e2017-08-31 14:50:44 -07001923
chaviwa69e0a72017-11-29 17:55:12 -08001924 private static void rotateCropForSF(Rect crop, int rot) {
1925 if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270) {
1926 int tmp = crop.top;
1927 crop.top = crop.left;
1928 crop.left = tmp;
1929 tmp = crop.right;
1930 crop.right = crop.bottom;
1931 crop.bottom = tmp;
1932 }
1933 }
1934
chaviw1cda84c2017-10-23 16:47:10 -07001935 /**
chaviw1da9cd92017-12-06 10:48:11 -08001936 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
chaviw1cda84c2017-10-23 16:47:10 -07001937 *
1938 * @param layerHandleToken The root layer to capture.
chaviwfbe47df2017-11-10 16:14:49 -08001939 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
1940 * Rect()' or null if no cropping is desired.
1941 * @param frameScale The desired scale of the returned buffer; the raw
1942 * screen will be scaled up/down.
Chavi Weingartend7ec64c2017-11-30 01:52:01 +00001943 *
1944 * @return Returns a GraphicBuffer that contains the layer capture.
Robert Carra7827f72019-01-11 12:53:37 -08001945 * @hide
chaviw1cda84c2017-10-23 16:47:10 -07001946 */
Chavi Weingartend7ec64c2017-11-30 01:52:01 +00001947 public static GraphicBuffer captureLayers(IBinder layerHandleToken, Rect sourceCrop,
chaviwfbe47df2017-11-10 16:14:49 -08001948 float frameScale) {
1949 return nativeCaptureLayers(layerHandleToken, sourceCrop, frameScale);
chaviw1cda84c2017-10-23 16:47:10 -07001950 }
1951
Robert Carra7827f72019-01-11 12:53:37 -08001952 /**
Peiyong Lin0ddb7d42019-01-16 13:25:09 -08001953 * Returns whether protected content is supported in GPU composition.
1954 * @hide
1955 */
1956 public static boolean getProtectedContentSupport() {
1957 return nativeGetProtectedContentSupport();
1958 }
1959
1960 /**
Robert Carr76907ee2019-01-11 13:38:19 -08001961 * An atomic set of changes to a set of SurfaceControl.
Robert Carra7827f72019-01-11 12:53:37 -08001962 */
Robert Carre13b58e2017-08-31 14:50:44 -07001963 public static class Transaction implements Closeable {
Robert Carr76907ee2019-01-11 13:38:19 -08001964 /**
1965 * @hide
1966 */
Robert Carre13b58e2017-08-31 14:50:44 -07001967 public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
1968 Transaction.class.getClassLoader(),
1969 nativeGetNativeTransactionFinalizer(), 512);
1970 private long mNativeObject;
1971
Jorim Jaggia5e10572017-11-15 14:36:26 +01001972 private final ArrayMap<SurfaceControl, Point> mResizedSurfaces = new ArrayMap<>();
Robert Carre13b58e2017-08-31 14:50:44 -07001973 Runnable mFreeNativeResources;
1974
Robert Carra7827f72019-01-11 12:53:37 -08001975 /**
Robert Carr76907ee2019-01-11 13:38:19 -08001976 * Open a new transaction object. The transaction may be filed with commands to
1977 * manipulate {@link SurfaceControl} instances, and then applied atomically with
1978 * {@link #apply}. Eventually the user should invoke {@link #close}, when the object
1979 * is no longer required. Note however that re-using a transaction after a call to apply
1980 * is allowed as a convenience.
Robert Carra7827f72019-01-11 12:53:37 -08001981 */
Robert Carre13b58e2017-08-31 14:50:44 -07001982 public Transaction() {
1983 mNativeObject = nativeCreateTransaction();
1984 mFreeNativeResources
1985 = sRegistry.registerNativeAllocation(this, mNativeObject);
1986 }
1987
1988 /**
1989 * Apply the transaction, clearing it's state, and making it usable
1990 * as a new transaction.
1991 */
1992 public void apply() {
1993 apply(false);
1994 }
1995
1996 /**
1997 * Close the transaction, if the transaction was not already applied this will cancel the
1998 * transaction.
1999 */
2000 @Override
2001 public void close() {
2002 mFreeNativeResources.run();
2003 mNativeObject = 0;
2004 }
2005
2006 /**
2007 * Jankier version of apply. Avoid use (b/28068298).
Robert Carra7827f72019-01-11 12:53:37 -08002008 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002009 */
2010 public void apply(boolean sync) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002011 applyResizedSurfaces();
Robert Carre13b58e2017-08-31 14:50:44 -07002012 nativeApplyTransaction(mNativeObject, sync);
2013 }
2014
Jorim Jaggia5e10572017-11-15 14:36:26 +01002015 private void applyResizedSurfaces() {
2016 for (int i = mResizedSurfaces.size() - 1; i >= 0; i--) {
2017 final Point size = mResizedSurfaces.valueAt(i);
2018 final SurfaceControl surfaceControl = mResizedSurfaces.keyAt(i);
2019 synchronized (surfaceControl.mSizeLock) {
2020 surfaceControl.mWidth = size.x;
2021 surfaceControl.mHeight = size.y;
2022 }
2023 }
2024 mResizedSurfaces.clear();
2025 }
2026
Robert Carra7827f72019-01-11 12:53:37 -08002027 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002028 * Toggle the visibility of a given Layer and it's sub-tree.
2029 *
2030 * @param sc The SurfaceControl for which to set the visibility
2031 * @param visible The new visibility
2032 * @return This transaction object.
2033 */
2034 @NonNull
2035 public Transaction setVisibility(@NonNull SurfaceControl sc, boolean visible) {
2036 sc.checkNotReleased();
2037 if (visible) {
2038 return show(sc);
2039 } else {
2040 return hide(sc);
2041 }
2042 }
2043
2044 /**
2045 * Request that a given surface and it's sub-tree be shown.
2046 *
2047 * @param sc The surface to show.
2048 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002049 * @hide
2050 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002051 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002052 public Transaction show(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002053 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002054 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_HIDDEN);
2055 return this;
2056 }
2057
Robert Carra7827f72019-01-11 12:53:37 -08002058 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002059 * Request that a given surface and it's sub-tree be hidden.
2060 *
2061 * @param sc The surface to hidden.
2062 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002063 * @hide
2064 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002065 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002066 public Transaction hide(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002067 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002068 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_HIDDEN, SURFACE_HIDDEN);
2069 return this;
2070 }
2071
Robert Carra7827f72019-01-11 12:53:37 -08002072 /**
2073 * @hide
2074 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002075 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002076 public Transaction setPosition(SurfaceControl sc, float x, float y) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002077 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002078 nativeSetPosition(mNativeObject, sc.mNativeObject, x, y);
2079 return this;
2080 }
2081
Robert Carra7827f72019-01-11 12:53:37 -08002082 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002083 * Set the default buffer size for the SurfaceControl, if there is an
2084 * {@link Surface} assosciated with the control, then
2085 * this will be the default size for buffers dequeued from it.
2086 * @param sc The surface to set the buffer size for.
2087 * @param w The default width
2088 * @param h The default height
2089 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002090 */
Robert Carr76907ee2019-01-11 13:38:19 -08002091 @NonNull
2092 public Transaction setBufferSize(@NonNull SurfaceControl sc,
2093 @IntRange(from = 0) int w, @IntRange(from = 0) int h) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002094 sc.checkNotReleased();
Jorim Jaggia5e10572017-11-15 14:36:26 +01002095 mResizedSurfaces.put(sc, new Point(w, h));
Jorim Jaggidfc27372017-10-27 17:47:49 +02002096 nativeSetSize(mNativeObject, sc.mNativeObject, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07002097 return this;
2098 }
2099
Robert Carra7827f72019-01-11 12:53:37 -08002100 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002101 * Set the Z-order for a given SurfaceControl, relative to it's siblings.
2102 * If two siblings share the same Z order the ordering is undefined. Surfaces
2103 * with a negative Z will be placed below the parent surface.
2104 *
2105 * @param sc The SurfaceControl to set the Z order on
2106 * @param z The Z-order
2107 * @return This Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002108 */
Robert Carr76907ee2019-01-11 13:38:19 -08002109 @NonNull
2110 public Transaction setLayer(@NonNull SurfaceControl sc,
2111 @IntRange(from = Integer.MIN_VALUE, to = Integer.MAX_VALUE) int z) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002112 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002113 nativeSetLayer(mNativeObject, sc.mNativeObject, z);
2114 return this;
2115 }
2116
Robert Carra7827f72019-01-11 12:53:37 -08002117 /**
2118 * @hide
2119 */
Robert Carr77e34942017-10-18 19:13:56 -07002120 public Transaction setRelativeLayer(SurfaceControl sc, SurfaceControl relativeTo, int z) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002121 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002122 nativeSetRelativeLayer(mNativeObject, sc.mNativeObject,
Robert Carr77e34942017-10-18 19:13:56 -07002123 relativeTo.getHandle(), z);
Robert Carre13b58e2017-08-31 14:50:44 -07002124 return this;
2125 }
2126
Robert Carra7827f72019-01-11 12:53:37 -08002127 /**
2128 * @hide
2129 */
Robert Carre13b58e2017-08-31 14:50:44 -07002130 public Transaction setTransparentRegionHint(SurfaceControl sc, Region transparentRegion) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002131 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002132 nativeSetTransparentRegionHint(mNativeObject,
2133 sc.mNativeObject, transparentRegion);
2134 return this;
2135 }
2136
Robert Carra7827f72019-01-11 12:53:37 -08002137 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002138 * Set the alpha for a given surface. If the alpha is non-zero the SurfaceControl
2139 * will be blended with the Surfaces under it according to the specified ratio.
2140 *
2141 * @param sc The given SurfaceControl.
2142 * @param alpha The alpha to set.
Robert Carra7827f72019-01-11 12:53:37 -08002143 */
Robert Carr76907ee2019-01-11 13:38:19 -08002144 @NonNull
2145 public Transaction setAlpha(@NonNull SurfaceControl sc,
2146 @FloatRange(from = 0.0, to = 1.0) float alpha) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002147 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002148 nativeSetAlpha(mNativeObject, sc.mNativeObject, alpha);
2149 return this;
2150 }
2151
Robert Carra7827f72019-01-11 12:53:37 -08002152 /**
2153 * @hide
2154 */
Robert Carr788f5742018-07-30 17:46:45 -07002155 public Transaction setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle) {
2156 sc.checkNotReleased();
2157 nativeSetInputWindowInfo(mNativeObject, sc.mNativeObject, handle);
2158 return this;
2159 }
2160
chaviw59f532e2018-12-26 15:34:59 -08002161 /**
2162 * Transfers touch focus from one window to another. It is possible for multiple windows to
2163 * have touch focus if they support split touch dispatch
2164 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this
2165 * method only transfers touch focus of the specified window without affecting
2166 * other windows that may also have touch focus at the same time.
2167 * @param fromToken The token of a window that currently has touch focus.
2168 * @param toToken The token of the window that should receive touch focus in
2169 * place of the first.
Robert Carra7827f72019-01-11 12:53:37 -08002170 * @hide
chaviw59f532e2018-12-26 15:34:59 -08002171 */
2172 public Transaction transferTouchFocus(IBinder fromToken, IBinder toToken) {
2173 nativeTransferTouchFocus(mNativeObject, fromToken, toToken);
2174 return this;
2175 }
2176
Robert Carra7827f72019-01-11 12:53:37 -08002177 /**
chaviw319cd0782019-02-14 11:00:23 -08002178 * Waits until any changes to input windows have been sent from SurfaceFlinger to
2179 * InputFlinger before returning.
2180 *
2181 * @hide
2182 */
2183 public Transaction syncInputWindows() {
2184 nativeSyncInputWindows(mNativeObject);
2185 return this;
2186 }
2187
2188 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002189 * Specify how the buffer assosciated with this Surface is mapped in to the
2190 * parent coordinate space. The source frame will be scaled to fit the destination
2191 * frame, after being rotated according to the orientation parameter.
2192 *
2193 * @param sc The SurfaceControl to specify the geometry of
2194 * @param sourceCrop The source rectangle in buffer space. Or null for the entire buffer.
2195 * @param destFrame The destination rectangle in parent space. Or null for the source frame.
2196 * @param orientation The buffer rotation
2197 * @return This transaction object.
2198 */
2199 @NonNull
2200 public Transaction setGeometry(@NonNull SurfaceControl sc, @Nullable Rect sourceCrop,
2201 @Nullable Rect destFrame, @Surface.Rotation int orientation) {
2202 sc.checkNotReleased();
2203 nativeSetGeometry(mNativeObject, sc.mNativeObject, sourceCrop, destFrame, orientation);
2204 return this;
2205 }
2206
2207 /**
Robert Carra7827f72019-01-11 12:53:37 -08002208 * @hide
2209 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002210 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002211 public Transaction setMatrix(SurfaceControl sc,
2212 float dsdx, float dtdx, float dtdy, float dsdy) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002213 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002214 nativeSetMatrix(mNativeObject, sc.mNativeObject,
2215 dsdx, dtdx, dtdy, dsdy);
2216 return this;
2217 }
2218
Robert Carra7827f72019-01-11 12:53:37 -08002219 /**
2220 * @hide
2221 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002222 @UnsupportedAppUsage
Jorim Jaggi21c39a72017-10-20 15:47:51 +02002223 public Transaction setMatrix(SurfaceControl sc, Matrix matrix, float[] float9) {
2224 matrix.getValues(float9);
2225 setMatrix(sc, float9[MSCALE_X], float9[MSKEW_Y],
2226 float9[MSKEW_X], float9[MSCALE_Y]);
2227 setPosition(sc, float9[MTRANS_X], float9[MTRANS_Y]);
2228 return this;
2229 }
2230
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002231 /**
2232 * Sets the color transform for the Surface.
2233 * @param matrix A float array with 9 values represents a 3x3 transform matrix
2234 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08002235 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002236 */
2237 public Transaction setColorTransform(SurfaceControl sc, @Size(9) float[] matrix,
2238 @Size(3) float[] translation) {
2239 sc.checkNotReleased();
2240 nativeSetColorTransform(mNativeObject, sc.mNativeObject, matrix, translation);
2241 return this;
2242 }
2243
Robert Carra7827f72019-01-11 12:53:37 -08002244 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08002245 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
2246 * the color can be interpreted in any color space.
2247 * @param agnostic A boolean to indicate whether the surface is color space agnostic
2248 * @hide
2249 */
2250 public Transaction setColorSpaceAgnostic(SurfaceControl sc, boolean agnostic) {
2251 sc.checkNotReleased();
2252 nativeSetColorSpaceAgnostic(mNativeObject, sc.mNativeObject, agnostic);
2253 return this;
2254 }
2255
2256 /**
Robert Carra7827f72019-01-11 12:53:37 -08002257 * @hide
2258 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002259 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002260 public Transaction setWindowCrop(SurfaceControl sc, Rect crop) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002261 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002262 if (crop != null) {
2263 nativeSetWindowCrop(mNativeObject, sc.mNativeObject,
2264 crop.left, crop.top, crop.right, crop.bottom);
2265 } else {
2266 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, 0, 0);
2267 }
2268
2269 return this;
2270 }
2271
Robert Carra7827f72019-01-11 12:53:37 -08002272 /**
2273 * @hide
2274 */
Vishnu Naird454442d2018-11-13 13:51:01 -08002275 public Transaction setWindowCrop(SurfaceControl sc, int width, int height) {
2276 sc.checkNotReleased();
2277 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, width, height);
2278 return this;
2279 }
2280
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002281 /**
2282 * Sets the corner radius of a {@link SurfaceControl}.
2283 * @param sc SurfaceControl
2284 * @param cornerRadius Corner radius in pixels.
2285 * @return Itself.
Robert Carra7827f72019-01-11 12:53:37 -08002286 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002287 */
2288 @UnsupportedAppUsage
2289 public Transaction setCornerRadius(SurfaceControl sc, float cornerRadius) {
2290 sc.checkNotReleased();
2291 nativeSetCornerRadius(mNativeObject, sc.mNativeObject, cornerRadius);
2292
2293 return this;
2294 }
2295
Robert Carra7827f72019-01-11 12:53:37 -08002296 /**
2297 * @hide
2298 */
Mathew Inwood679c15e2019-02-06 15:36:04 +00002299 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.O)
Robert Carre13b58e2017-08-31 14:50:44 -07002300 public Transaction setLayerStack(SurfaceControl sc, int layerStack) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002301 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002302 nativeSetLayerStack(mNativeObject, sc.mNativeObject, layerStack);
2303 return this;
2304 }
2305
Robert Carra7827f72019-01-11 12:53:37 -08002306 /**
2307 * @hide
2308 */
Robert Carr76907ee2019-01-11 13:38:19 -08002309 @UnsupportedAppUsage
Jorim Jaggidfc27372017-10-27 17:47:49 +02002310 public Transaction deferTransactionUntil(SurfaceControl sc, IBinder handle,
2311 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002312 if (frameNumber < 0) {
2313 return this;
2314 }
Jorim Jaggidfc27372017-10-27 17:47:49 +02002315 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002316 nativeDeferTransactionUntil(mNativeObject, sc.mNativeObject, handle, frameNumber);
2317 return this;
2318 }
2319
Robert Carra7827f72019-01-11 12:53:37 -08002320 /**
2321 * @hide
2322 */
Robert Carr76907ee2019-01-11 13:38:19 -08002323 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002324 public Transaction deferTransactionUntilSurface(SurfaceControl sc, Surface barrierSurface,
2325 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002326 if (frameNumber < 0) {
2327 return this;
2328 }
Jorim Jaggidfc27372017-10-27 17:47:49 +02002329 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002330 nativeDeferTransactionUntilSurface(mNativeObject, sc.mNativeObject,
2331 barrierSurface.mNativeObject, frameNumber);
2332 return this;
2333 }
2334
Robert Carra7827f72019-01-11 12:53:37 -08002335 /**
2336 * @hide
2337 */
Robert Carre13b58e2017-08-31 14:50:44 -07002338 public Transaction reparentChildren(SurfaceControl sc, IBinder newParentHandle) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002339 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002340 nativeReparentChildren(mNativeObject, sc.mNativeObject, newParentHandle);
2341 return this;
2342 }
2343
Robert Carr10584fa2019-01-14 15:55:19 -08002344 /**
2345 * Re-parents a given layer to a new parent. Children inherit transform (position, scaling)
2346 * crop, visibility, and Z-ordering from their parents, as if the children were pixels within the
2347 * parent Surface.
2348 *
2349 * @param sc The SurfaceControl to reparent
2350 * @param newParent The new parent for the given control.
2351 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002352 */
Robert Carr76907ee2019-01-11 13:38:19 -08002353 @NonNull
2354 public Transaction reparent(@NonNull SurfaceControl sc,
2355 @Nullable SurfaceControl newParent) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002356 sc.checkNotReleased();
Robert Carr10584fa2019-01-14 15:55:19 -08002357 long otherObject = 0;
2358 if (newParent != null) {
2359 newParent.checkNotReleased();
2360 otherObject = newParent.mNativeObject;
2361 }
2362 nativeReparent(mNativeObject, sc.mNativeObject, otherObject);
Robert Carre13b58e2017-08-31 14:50:44 -07002363 return this;
2364 }
2365
Robert Carra7827f72019-01-11 12:53:37 -08002366 /**
2367 * @hide
2368 */
Robert Carre13b58e2017-08-31 14:50:44 -07002369 public Transaction detachChildren(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002370 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002371 nativeSeverChildren(mNativeObject, sc.mNativeObject);
2372 return this;
2373 }
2374
Robert Carra7827f72019-01-11 12:53:37 -08002375 /**
2376 * @hide
2377 */
Robert Carre13b58e2017-08-31 14:50:44 -07002378 public Transaction setOverrideScalingMode(SurfaceControl sc, int overrideScalingMode) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002379 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002380 nativeSetOverrideScalingMode(mNativeObject, sc.mNativeObject,
2381 overrideScalingMode);
2382 return this;
2383 }
2384
2385 /**
2386 * Sets a color for the Surface.
2387 * @param color A float array with three values to represent r, g, b in range [0..1]
Robert Carra7827f72019-01-11 12:53:37 -08002388 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002389 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002390 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002391 public Transaction setColor(SurfaceControl sc, @Size(3) float[] color) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002392 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002393 nativeSetColor(mNativeObject, sc.mNativeObject, color);
2394 return this;
2395 }
2396
2397 /**
2398 * If the buffer size changes in this transaction, position and crop updates specified
2399 * in this transaction will not complete until a buffer of the new size
2400 * arrives. As transform matrix and size are already frozen in this fashion,
2401 * this enables totally freezing the surface until the resize has completed
2402 * (at which point the geometry influencing aspects of this transaction will then occur)
Robert Carra7827f72019-01-11 12:53:37 -08002403 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002404 */
2405 public Transaction setGeometryAppliesWithResize(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002406 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002407 nativeSetGeometryAppliesWithResize(mNativeObject, sc.mNativeObject);
2408 return this;
2409 }
2410
2411 /**
2412 * Sets the security of the surface. Setting the flag is equivalent to creating the
2413 * Surface with the {@link #SECURE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002414 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002415 */
Robert Carrb1579c82017-09-05 14:54:47 -07002416 public Transaction setSecure(SurfaceControl sc, boolean isSecure) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002417 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002418 if (isSecure) {
2419 nativeSetFlags(mNativeObject, sc.mNativeObject, SECURE, SECURE);
2420 } else {
2421 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SECURE);
2422 }
2423 return this;
2424 }
2425
2426 /**
2427 * Sets the opacity of the surface. Setting the flag is equivalent to creating the
2428 * Surface with the {@link #OPAQUE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002429 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002430 */
2431 public Transaction setOpaque(SurfaceControl sc, boolean isOpaque) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002432 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002433 if (isOpaque) {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002434 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_OPAQUE, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002435 } else {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002436 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002437 }
2438 return this;
2439 }
2440
Robert Carra7827f72019-01-11 12:53:37 -08002441 /**
2442 * @hide
2443 */
Robert Carre13b58e2017-08-31 14:50:44 -07002444 public Transaction setDisplaySurface(IBinder displayToken, Surface surface) {
2445 if (displayToken == null) {
2446 throw new IllegalArgumentException("displayToken must not be null");
2447 }
2448
2449 if (surface != null) {
2450 synchronized (surface.mLock) {
2451 nativeSetDisplaySurface(mNativeObject, displayToken, surface.mNativeObject);
2452 }
2453 } else {
2454 nativeSetDisplaySurface(mNativeObject, displayToken, 0);
2455 }
2456 return this;
2457 }
2458
Robert Carra7827f72019-01-11 12:53:37 -08002459 /**
2460 * @hide
2461 */
Robert Carre13b58e2017-08-31 14:50:44 -07002462 public Transaction setDisplayLayerStack(IBinder displayToken, int layerStack) {
2463 if (displayToken == null) {
2464 throw new IllegalArgumentException("displayToken must not be null");
2465 }
2466 nativeSetDisplayLayerStack(mNativeObject, displayToken, layerStack);
2467 return this;
2468 }
2469
Robert Carra7827f72019-01-11 12:53:37 -08002470 /**
2471 * @hide
2472 */
Robert Carre13b58e2017-08-31 14:50:44 -07002473 public Transaction setDisplayProjection(IBinder displayToken,
2474 int orientation, Rect layerStackRect, Rect displayRect) {
2475 if (displayToken == null) {
2476 throw new IllegalArgumentException("displayToken must not be null");
2477 }
2478 if (layerStackRect == null) {
2479 throw new IllegalArgumentException("layerStackRect must not be null");
2480 }
2481 if (displayRect == null) {
2482 throw new IllegalArgumentException("displayRect must not be null");
2483 }
2484 nativeSetDisplayProjection(mNativeObject, displayToken, orientation,
2485 layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom,
2486 displayRect.left, displayRect.top, displayRect.right, displayRect.bottom);
2487 return this;
2488 }
2489
Robert Carra7827f72019-01-11 12:53:37 -08002490 /**
2491 * @hide
2492 */
Robert Carre13b58e2017-08-31 14:50:44 -07002493 public Transaction setDisplaySize(IBinder displayToken, int width, int height) {
2494 if (displayToken == null) {
2495 throw new IllegalArgumentException("displayToken must not be null");
2496 }
2497 if (width <= 0 || height <= 0) {
2498 throw new IllegalArgumentException("width and height must be positive");
2499 }
2500
2501 nativeSetDisplaySize(mNativeObject, displayToken, width, height);
2502 return this;
2503 }
2504
Robert Carra7827f72019-01-11 12:53:37 -08002505 /** flag the transaction as an animation
2506 * @hide
2507 */
Robert Carre13b58e2017-08-31 14:50:44 -07002508 public Transaction setAnimationTransaction() {
2509 nativeSetAnimationTransaction(mNativeObject);
2510 return this;
2511 }
Robert Carrb1579c82017-09-05 14:54:47 -07002512
2513 /**
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002514 * Indicate that SurfaceFlinger should wake up earlier than usual as a result of this
2515 * transaction. This should be used when the caller thinks that the scene is complex enough
2516 * that it's likely to hit GL composition, and thus, SurfaceFlinger needs to more time in
2517 * order not to miss frame deadlines.
2518 * <p>
2519 * Corresponds to setting ISurfaceComposer::eEarlyWakeup
Robert Carra7827f72019-01-11 12:53:37 -08002520 * @hide
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002521 */
2522 public Transaction setEarlyWakeup() {
2523 nativeSetEarlyWakeup(mNativeObject);
2524 return this;
2525 }
2526
2527 /**
Evan Rosky485df202018-12-06 14:11:12 -08002528 * Sets an arbitrary piece of metadata on the surface. This is a helper for int data.
2529 * @hide
2530 */
2531 public Transaction setMetadata(int key, int data) {
2532 Parcel parcel = Parcel.obtain();
2533 parcel.writeInt(data);
2534 try {
2535 setMetadata(key, parcel);
2536 } finally {
2537 parcel.recycle();
2538 }
2539 return this;
2540 }
2541
2542 /**
2543 * Sets an arbitrary piece of metadata on the surface.
2544 * @hide
2545 */
2546 public Transaction setMetadata(int key, Parcel data) {
2547 nativeSetMetadata(mNativeObject, key, data);
2548 return this;
2549 }
2550
2551 /**
Robert Carrb1579c82017-09-05 14:54:47 -07002552 * Merge the other transaction into this transaction, clearing the
2553 * other transaction as if it had been applied.
Robert Carr76907ee2019-01-11 13:38:19 -08002554 *
2555 * @param other The transaction to merge in to this one.
2556 * @return This transaction.
Robert Carrb1579c82017-09-05 14:54:47 -07002557 */
Robert Carr76907ee2019-01-11 13:38:19 -08002558 @NonNull
2559 public Transaction merge(@NonNull Transaction other) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002560 mResizedSurfaces.putAll(other.mResizedSurfaces);
2561 other.mResizedSurfaces.clear();
Robert Carrb1579c82017-09-05 14:54:47 -07002562 nativeMergeTransaction(mNativeObject, other.mNativeObject);
2563 return this;
2564 }
Robert Carr71200f22019-02-05 09:44:53 -08002565
2566 /**
2567 * Equivalent to reparent with a null parent, in that it removes
2568 * the SurfaceControl from the scene, but it also releases
2569 * the local resources (by calling {@link SurfaceControl#release})
2570 * after this method returns, {@link SurfaceControl#isValid} will return
2571 * false for the argument.
2572 *
2573 * @param sc The surface to remove and release.
2574 * @return This transaction
2575 * @hide
2576 */
2577 @NonNull
2578 public Transaction remove(@NonNull SurfaceControl sc) {
2579 reparent(sc, null);
2580 sc.release();
2581 return this;
2582 }
Robert Carre13b58e2017-08-31 14:50:44 -07002583 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08002584}