blob: 76ed37c51bfe762ccd1253b9288aeea6e6b89a1e [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;
Steven Thomas6ec6fbc2020-03-24 16:06:33 -070035import android.annotation.TestApi;
Artur Satayevad9254c2019-12-10 17:47:54 +000036import android.compat.annotation.UnsupportedAppUsage;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080037import android.graphics.Bitmap;
Peiyong Lin5f4a5682019-01-17 11:37:07 -080038import android.graphics.ColorSpace;
Robert Carr6486d312017-01-09 19:48:29 -080039import android.graphics.GraphicBuffer;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020040import android.graphics.Matrix;
Vishnu Nair04ab4392018-01-10 11:00:06 -080041import android.graphics.PixelFormat;
Jorim Jaggia5e10572017-11-15 14:36:26 +010042import android.graphics.Point;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080043import android.graphics.Rect;
44import android.graphics.Region;
Marin Shalamanov98af1592019-10-08 10:48:08 +020045import android.hardware.display.DeviceProductInfo;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070046import android.hardware.display.DisplayedContentSample;
47import android.hardware.display.DisplayedContentSamplingAttributes;
Mathew Inwood679c15e2019-02-06 15:36:04 +000048import android.os.Build;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080049import android.os.IBinder;
Jorim Jaggi06975df2017-12-01 14:52:13 +010050import android.os.Parcel;
51import android.os.Parcelable;
Jorim Jaggia5e10572017-11-15 14:36:26 +010052import android.util.ArrayMap;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080053import android.util.Log;
Evan Rosky485df202018-12-06 14:11:12 -080054import android.util.SparseIntArray;
Vishnu Nair04ab4392018-01-10 11:00:06 -080055import android.util.proto.ProtoOutputStream;
Igor Murashkina86ab6402013-08-30 12:58:36 -070056import android.view.Surface.OutOfResourcesException;
Jorim Jaggia5e10572017-11-15 14:36:26 +010057
58import com.android.internal.annotations.GuardedBy;
59
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070060import dalvik.system.CloseGuard;
Vishnu Nair04ab4392018-01-10 11:00:06 -080061
Robert Carre625fcf2017-09-01 12:36:28 -070062import libcore.util.NativeAllocationRegistry;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070063
Robert Carre13b58e2017-08-31 14:50:44 -070064import java.io.Closeable;
Evan Rosky485df202018-12-06 14:11:12 -080065import java.nio.ByteBuffer;
Evan Roskyc12c9a32019-02-06 13:38:03 -080066import java.nio.ByteOrder;
Dan Gittik832b4972019-02-13 18:17:47 +000067import java.util.Objects;
Robert Carre13b58e2017-08-31 14:50:44 -070068
Mathias Agopian3866f0d2013-02-11 22:08:48 -080069/**
Robert Carr76907ee2019-01-11 13:38:19 -080070 * Handle to an on-screen Surface managed by the system compositor. The SurfaceControl is
71 * a combination of a buffer source, and metadata about how to display the buffers.
72 * By constructing a {@link Surface} from this SurfaceControl you can submit buffers to be
73 * composited. Using {@link SurfaceControl.Transaction} you can manipulate various
74 * properties of how the buffer will be displayed on-screen. SurfaceControl's are
75 * arranged into a scene-graph like hierarchy, and as such any SurfaceControl may have
76 * a parent. Geometric properties like transform, crop, and Z-ordering will be inherited
77 * from the parent, as if the child were content in the parents buffer stream.
Mathias Agopian3866f0d2013-02-11 22:08:48 -080078 */
Robert Carr76907ee2019-01-11 13:38:19 -080079public final class SurfaceControl implements Parcelable {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080080 private static final String TAG = "SurfaceControl";
Mathias Agopian29479eb2013-02-14 14:36:04 -080081
Ashok Bhat36bef0b2014-01-20 20:08:01 +000082 private static native long nativeCreate(SurfaceSession session, String name,
Evan Rosky485df202018-12-06 14:11:12 -080083 int w, int h, int format, int flags, long parentObject, Parcel metadata)
Mathias Agopian29479eb2013-02-14 14:36:04 -080084 throws OutOfResourcesException;
Jorim Jaggi06975df2017-12-01 14:52:13 +010085 private static native long nativeReadFromParcel(Parcel in);
chaviwbeb7a0c2018-12-05 13:49:54 -080086 private static native long nativeCopyFromSurfaceControl(long nativeObject);
Jorim Jaggi06975df2017-12-01 14:52:13 +010087 private static native void nativeWriteToParcel(long nativeObject, Parcel out);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000088 private static native void nativeRelease(long nativeObject);
Chong Zhang47e36a32016-02-29 16:44:33 -080089 private static native void nativeDisconnect(long nativeObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080090
Peiyong Line3e5efd2019-03-21 20:59:47 +000091 private static native ScreenshotGraphicBuffer nativeScreenshot(IBinder displayToken,
Robert Carr5c52b132019-02-15 15:48:11 -080092 Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation,
93 boolean captureSecureLayers);
Peiyong Lin21e499a2019-04-03 16:37:46 -070094 private static native ScreenshotGraphicBuffer nativeCaptureLayers(IBinder displayToken,
Chiawei Wang02202d12019-01-03 18:12:13 +080095 long layerObject, Rect sourceCrop, float frameScale, long[] excludeLayerObjects,
96 int format);
chaviwa51724f2019-09-19 09:50:11 -070097 private static native long nativeMirrorSurface(long mirrorOfObject);
Robert Carre13b58e2017-08-31 14:50:44 -070098 private static native long nativeCreateTransaction();
99 private static native long nativeGetNativeTransactionFinalizer();
100 private static native void nativeApplyTransaction(long transactionObj, boolean sync);
Robert Carrb1579c82017-09-05 14:54:47 -0700101 private static native void nativeMergeTransaction(long transactionObj,
102 long otherTransactionObj);
Robert Carre13b58e2017-08-31 14:50:44 -0700103 private static native void nativeSetAnimationTransaction(long transactionObj);
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100104 private static native void nativeSetEarlyWakeup(long transactionObj);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800105
Robert Carre13b58e2017-08-31 14:50:44 -0700106 private static native void nativeSetLayer(long transactionObj, long nativeObject, int zorder);
107 private static native void nativeSetRelativeLayer(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700108 long relativeToObject, int zorder);
Robert Carre13b58e2017-08-31 14:50:44 -0700109 private static native void nativeSetPosition(long transactionObj, long nativeObject,
110 float x, float y);
Robert Carre13b58e2017-08-31 14:50:44 -0700111 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);
Ana Krulecfea97172019-11-02 23:11:02 +0100127 private static native void nativeSetFrameRateSelectionPriority(long transactionObj,
128 long nativeObject, int priority);
Robert Carre13b58e2017-08-31 14:50:44 -0700129 private static native void nativeSetWindowCrop(long transactionObj, long nativeObject,
130 int l, int t, int r, int b);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -0700131 private static native void nativeSetCornerRadius(long transactionObj, long nativeObject,
132 float cornerRadius);
Lucas Dupin991415e2019-11-25 17:48:58 -0800133 private static native void nativeSetBackgroundBlurRadius(long transactionObj, long nativeObject,
134 int blurRadius);
Robert Carre13b58e2017-08-31 14:50:44 -0700135 private static native void nativeSetLayerStack(long transactionObj, long nativeObject,
136 int layerStack);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800137
Svetoslav1376d602014-03-13 11:17:26 -0700138 private static native boolean nativeClearContentFrameStats(long nativeObject);
139 private static native boolean nativeGetContentFrameStats(long nativeObject, WindowContentFrameStats outStats);
140 private static native boolean nativeClearAnimationFrameStats();
141 private static native boolean nativeGetAnimationFrameStats(WindowAnimationFrameStats outStats);
142
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800143 private static native long[] nativeGetPhysicalDisplayIds();
144 private static native IBinder nativeGetPhysicalDisplayToken(long physicalDisplayId);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800145 private static native IBinder nativeCreateDisplay(String name, boolean secure);
Jesse Hall6a6bc212013-08-08 12:15:03 -0700146 private static native void nativeDestroyDisplay(IBinder displayToken);
Robert Carre13b58e2017-08-31 14:50:44 -0700147 private static native void nativeSetDisplaySurface(long transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000148 IBinder displayToken, long nativeSurfaceObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700149 private static native void nativeSetDisplayLayerStack(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800150 IBinder displayToken, int layerStack);
Robert Carre13b58e2017-08-31 14:50:44 -0700151 private static native void nativeSetDisplayProjection(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800152 IBinder displayToken, int orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -0700153 int l, int t, int r, int b,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800154 int L, int T, int R, int B);
Robert Carre13b58e2017-08-31 14:50:44 -0700155 private static native void nativeSetDisplaySize(long transactionObj, IBinder displayToken,
156 int width, int height);
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800157 private static native SurfaceControl.DisplayInfo nativeGetDisplayInfo(IBinder displayToken);
158 private static native SurfaceControl.DisplayConfig[] nativeGetDisplayConfigs(
Dan Stoza00101052014-05-02 15:23:40 -0700159 IBinder displayToken);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -0700160 private static native DisplayedContentSamplingAttributes
161 nativeGetDisplayedContentSamplingAttributes(IBinder displayToken);
162 private static native boolean nativeSetDisplayedContentSamplingEnabled(IBinder displayToken,
163 boolean enable, int componentMask, int maxFrames);
164 private static native DisplayedContentSample nativeGetDisplayedContentSample(
165 IBinder displayToken, long numFrames, long timestamp);
Dan Stoza00101052014-05-02 15:23:40 -0700166 private static native int nativeGetActiveConfig(IBinder displayToken);
Ana Krulec4f753aa2019-11-14 00:49:39 +0100167 private static native boolean nativeSetDesiredDisplayConfigSpecs(IBinder displayToken,
Ana Krulec52f12892019-11-18 03:57:20 -0800168 SurfaceControl.DesiredDisplayConfigSpecs desiredDisplayConfigSpecs);
Ana Kruleca74a8642019-11-14 00:51:00 +0100169 private static native SurfaceControl.DesiredDisplayConfigSpecs
170 nativeGetDesiredDisplayConfigSpecs(IBinder displayToken);
Michael Wright1c9977b2016-07-12 13:30:10 -0700171 private static native int[] nativeGetDisplayColorModes(IBinder displayToken);
Daniel Solomon10e3b332019-01-20 21:09:11 -0800172 private static native SurfaceControl.DisplayPrimaries nativeGetDisplayNativePrimaries(
173 IBinder displayToken);
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800174 private static native int[] nativeGetCompositionDataspaces();
Michael Wright1c9977b2016-07-12 13:30:10 -0700175 private static native int nativeGetActiveColorMode(IBinder displayToken);
176 private static native boolean nativeSetActiveColorMode(IBinder displayToken,
177 int colorMode);
Galia Peycheva056b3ee2019-06-26 14:05:12 +0200178 private static native void nativeSetAutoLowLatencyMode(IBinder displayToken, boolean on);
179 private static native void nativeSetGameContentType(IBinder displayToken, boolean on);
Prashant Malanic55929a2014-05-25 01:59:21 -0700180 private static native void nativeSetDisplayPowerMode(
181 IBinder displayToken, int mode);
Robert Carre13b58e2017-08-31 14:50:44 -0700182 private static native void nativeDeferTransactionUntil(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700183 long barrierObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700184 private static native void nativeDeferTransactionUntilSurface(long transactionObj,
185 long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800186 long surfaceObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700187 private static native void nativeReparentChildren(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700188 long newParentObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700189 private static native void nativeReparent(long transactionObj, long nativeObject,
Robert Carr10584fa2019-01-14 15:55:19 -0800190 long newParentNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700191 private static native void nativeSeverChildren(long transactionObj, long nativeObject);
192 private static native void nativeSetOverrideScalingMode(long transactionObj, long nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700193 int scalingMode);
Robert Carr6da3cc02016-06-16 15:17:07 -0700194
Michael Wright9ff94c02016-03-30 18:05:40 -0700195 private static native Display.HdrCapabilities nativeGetHdrCapabilities(IBinder displayToken);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800196
Galia Peycheva056b3ee2019-06-26 14:05:12 +0200197 private static native boolean nativeGetAutoLowLatencyModeSupport(IBinder displayToken);
198 private static native boolean nativeGetGameContentTypeSupport(IBinder displayToken);
199
Robert Carr788f5742018-07-30 17:46:45 -0700200 private static native void nativeSetInputWindowInfo(long transactionObj, long nativeObject,
201 InputWindowHandle handle);
Arthur Hungc499ad32019-09-05 15:51:14 +0800202
Peiyong Lin0ddb7d42019-01-16 13:25:09 -0800203 private static native boolean nativeGetProtectedContentSupport();
Evan Roskyb51e2462019-04-03 19:27:18 -0700204 private static native void nativeSetMetadata(long transactionObj, long nativeObject, int key,
205 Parcel data);
chaviw319cd0782019-02-14 11:00:23 -0800206 private static native void nativeSyncInputWindows(long transactionObj);
Dan Gittik832b4972019-02-13 18:17:47 +0000207 private static native boolean nativeGetDisplayBrightnessSupport(IBinder displayToken);
208 private static native boolean nativeSetDisplayBrightness(IBinder displayToken,
209 float brightness);
Vishnu Nair629df2b2019-06-11 16:03:38 -0700210 private static native long nativeReadTransactionFromParcel(Parcel in);
211 private static native void nativeWriteTransactionToParcel(long nativeObject, Parcel out);
Vishnu Naird87984d2019-11-06 14:43:22 -0800212 private static native void nativeSetShadowRadius(long transactionObj, long nativeObject,
213 float shadowRadius);
Vishnu Nair4a067c52019-11-19 14:25:56 -0800214 private static native void nativeSetGlobalShadowSettings(@Size(4) float[] ambientColor,
215 @Size(4) float[] spotColor, float lightPosY, float lightPosZ, float lightRadius);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800216
Steven Thomas6cf051e2020-01-14 11:37:21 -0800217 private static native void nativeSetFrameRate(
Steven Thomasdd7bf2f2020-01-31 18:50:02 -0800218 long transactionObj, long nativeObject, float frameRate, int compatibility);
Jorim Jaggiee540702020-04-02 21:40:52 +0200219 private static native long nativeGetHandle(long nativeObject);
Steven Thomas6cf051e2020-01-14 11:37:21 -0800220
Steven Thomas6ec6fbc2020-03-24 16:06:33 -0700221 private static native long nativeAcquireFrameRateFlexibilityToken();
222 private static native void nativeReleaseFrameRateFlexibilityToken(long token);
223
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800224 private final CloseGuard mCloseGuard = CloseGuard.get();
chaviwbeb7a0c2018-12-05 13:49:54 -0800225 private String mName;
Robert Carr48ec4e02019-07-16 14:28:47 -0700226 /**
227 * @hide
228 */
229 public long mNativeObject;
Jorim Jaggiee540702020-04-02 21:40:52 +0200230 private long mNativeHandle;
Tiger Huang0fd67482020-04-15 22:16:13 +0800231 private Throwable mReleaseStack = null;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800232
Jorim Jaggia5e10572017-11-15 14:36:26 +0100233 // TODO: Move this to native.
234 private final Object mSizeLock = new Object();
235 @GuardedBy("mSizeLock")
236 private int mWidth;
237 @GuardedBy("mSizeLock")
238 private int mHeight;
239
Robert Carre13b58e2017-08-31 14:50:44 -0700240 static Transaction sGlobalTransaction;
241 static long sTransactionNestCount = 0;
242
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800243 /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */
244
245 /**
246 * Surface creation flag: Surface is created hidden
Robert Carra7827f72019-01-11 12:53:37 -0800247 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800248 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100249 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800250 public static final int HIDDEN = 0x00000004;
251
252 /**
253 * Surface creation flag: The surface contains secure content, special
254 * measures will be taken to disallow the surface's content to be copied
255 * from another process. In particular, screenshots and VNC servers will
256 * be disabled, but other measures can take place, for instance the
Jesse Hall6a6bc212013-08-08 12:15:03 -0700257 * surface might not be hardware accelerated.
Robert Carra7827f72019-01-11 12:53:37 -0800258 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800259 */
260 public static final int SECURE = 0x00000080;
261
262 /**
263 * Surface creation flag: Creates a surface where color components are interpreted
264 * as "non pre-multiplied" by their alpha channel. Of course this flag is
265 * meaningless for surfaces without an alpha channel. By default
266 * surfaces are pre-multiplied, which means that each color component is
267 * already multiplied by its alpha value. In this case the blending
268 * equation used is:
Andy McFadden314405b2014-01-29 17:18:05 -0800269 * <p>
270 * <code>DEST = SRC + DEST * (1-SRC_ALPHA)</code>
271 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800272 * By contrast, non pre-multiplied surfaces use the following equation:
Andy McFadden314405b2014-01-29 17:18:05 -0800273 * <p>
274 * <code>DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)</code>
275 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800276 * pre-multiplied surfaces must always be used if transparent pixels are
277 * composited on top of each-other into the surface. A pre-multiplied
278 * surface can never lower the value of the alpha component of a given
279 * pixel.
Andy McFadden314405b2014-01-29 17:18:05 -0800280 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800281 * In some rare situations, a non pre-multiplied surface is preferable.
Robert Carra7827f72019-01-11 12:53:37 -0800282 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800283 */
284 public static final int NON_PREMULTIPLIED = 0x00000100;
285
286 /**
287 * Surface creation flag: Indicates that the surface must be considered opaque,
Robert Carre625fcf2017-09-01 12:36:28 -0700288 * even if its pixel format contains an alpha channel. This can be useful if an
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800289 * application needs full RGBA 8888 support for instance but will
290 * still draw every pixel opaque.
Andy McFadden314405b2014-01-29 17:18:05 -0800291 * <p>
292 * This flag is ignored if setAlpha() is used to make the surface non-opaque.
293 * Combined effects are (assuming a buffer format with an alpha channel):
294 * <ul>
295 * <li>OPAQUE + alpha(1.0) == opaque composition
296 * <li>OPAQUE + alpha(0.x) == blended composition
297 * <li>!OPAQUE + alpha(1.0) == blended composition
298 * <li>!OPAQUE + alpha(0.x) == blended composition
299 * </ul>
300 * If the underlying buffer lacks an alpha channel, the OPAQUE flag is effectively
301 * set automatically.
Robert Carra7827f72019-01-11 12:53:37 -0800302 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800303 */
304 public static final int OPAQUE = 0x00000400;
305
306 /**
307 * Surface creation flag: Application requires a hardware-protected path to an
308 * external display sink. If a hardware-protected path is not available,
309 * then this surface will not be displayed on the external sink.
310 *
Robert Carra7827f72019-01-11 12:53:37 -0800311 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800312 */
313 public static final int PROTECTED_APP = 0x00000800;
314
315 // 0x1000 is reserved for an independent DRM protected flag in framework
316
317 /**
Riley Andrews68eccda2014-07-07 11:47:35 -0700318 * Surface creation flag: Window represents a cursor glyph.
Robert Carra7827f72019-01-11 12:53:37 -0800319 * @hide
Riley Andrews68eccda2014-07-07 11:47:35 -0700320 */
321 public static final int CURSOR_WINDOW = 0x00002000;
322
323 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800324 * Surface creation flag: Creates a normal surface.
325 * This is the default.
326 *
Robert Carra7827f72019-01-11 12:53:37 -0800327 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800328 */
329 public static final int FX_SURFACE_NORMAL = 0x00000000;
330
331 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800332 * Surface creation flag: Creates a effect surface which
333 * represents a solid color and or shadows.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800334 *
Robert Carra7827f72019-01-11 12:53:37 -0800335 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800336 */
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800337 public static final int FX_SURFACE_EFFECT = 0x00020000;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800338
339 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700340 * Surface creation flag: Creates a container surface.
341 * This surface will have no buffers and will only be used
342 * as a container for other surfaces, or for its InputInfo.
Robert Carra7827f72019-01-11 12:53:37 -0800343 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700344 */
345 public static final int FX_SURFACE_CONTAINER = 0x00080000;
346
347 /**
Robert Carr48ec4e02019-07-16 14:28:47 -0700348 * @hide
349 */
350 public static final int FX_SURFACE_BLAST = 0x00040000;
351
352 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800353 * Mask used for FX values above.
354 *
Robert Carra7827f72019-01-11 12:53:37 -0800355 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800356 */
357 public static final int FX_SURFACE_MASK = 0x000F0000;
358
359 /* flags used with setFlags() (keep in sync with ISurfaceComposer.h) */
360
361 /**
362 * Surface flag: Hide the surface.
363 * Equivalent to calling hide().
Andy McFadden314405b2014-01-29 17:18:05 -0800364 * Updates the value set during Surface creation (see {@link #HIDDEN}).
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800365 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800366 private static final int SURFACE_HIDDEN = 0x01;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800367
Andy McFadden314405b2014-01-29 17:18:05 -0800368 /**
369 * Surface flag: composite without blending when possible.
370 * Updates the value set during Surface creation (see {@link #OPAQUE}).
371 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800372 private static final int SURFACE_OPAQUE = 0x02;
Andy McFadden314405b2014-01-29 17:18:05 -0800373
Robert Carr76907ee2019-01-11 13:38:19 -0800374 // Display power modes.
Prashant Malanic55929a2014-05-25 01:59:21 -0700375 /**
376 * Display power mode off: used while blanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700377 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800378 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700379 */
380 public static final int POWER_MODE_OFF = 0;
381
382 /**
383 * Display power mode doze: used while putting the screen into low power mode.
Jeff Brown5dc21912014-07-17 18:50:18 -0700384 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800385 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700386 */
387 public static final int POWER_MODE_DOZE = 1;
388
389 /**
390 * Display power mode normal: used while unblanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700391 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800392 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700393 */
394 public static final int POWER_MODE_NORMAL = 2;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800395
Jeff Brown5dc21912014-07-17 18:50:18 -0700396 /**
397 * Display power mode doze: used while putting the screen into a suspended
398 * low power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800399 * @hide
Jeff Brown5dc21912014-07-17 18:50:18 -0700400 */
401 public static final int POWER_MODE_DOZE_SUSPEND = 3;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800402
403 /**
Chris Phoenix10a4a642017-09-25 13:21:00 -0700404 * Display power mode on: used while putting the screen into a suspended
405 * full power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800406 * @hide
Chris Phoenix10a4a642017-09-25 13:21:00 -0700407 */
408 public static final int POWER_MODE_ON_SUSPEND = 4;
409
410 /**
Robert Carr132c9f52017-07-31 17:02:30 -0700411 * A value for windowType used to indicate that the window should be omitted from screenshots
412 * and display mirroring. A temporary workaround until we express such things with
413 * the hierarchy.
414 * TODO: b/64227542
415 * @hide
416 */
417 public static final int WINDOW_TYPE_DONT_SCREENSHOT = 441731;
418
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800419 /**
420 * internal representation of how to interpret pixel value, used only to convert to ColorSpace.
421 */
422 private static final int INTERNAL_DATASPACE_SRGB = 142671872;
423 private static final int INTERNAL_DATASPACE_DISPLAY_P3 = 143261696;
424 private static final int INTERNAL_DATASPACE_SCRGB = 411107328;
425
Robert Carreb344c72019-01-07 18:35:30 -0800426 private void assignNativeObject(long nativeObject) {
427 if (mNativeObject != 0) {
428 release();
429 }
Tiger Huang0fd67482020-04-15 22:16:13 +0800430 if (nativeObject != 0) {
Robert Carr01b0dd52020-02-27 13:25:17 -0800431 mCloseGuard.open("release");
432 }
Robert Carreb344c72019-01-07 18:35:30 -0800433 mNativeObject = nativeObject;
Jorim Jaggiee540702020-04-02 21:40:52 +0200434 mNativeHandle = mNativeObject != 0 ? nativeGetHandle(nativeObject) : 0;
Tiger Huang0fd67482020-04-15 22:16:13 +0800435 if (mNativeObject == 0) {
436 if (Build.IS_DEBUGGABLE) {
437 mReleaseStack = new Throwable("assigned zero nativeObject here");
438 }
439 } else {
440 mReleaseStack = null;
441 }
Robert Carreb344c72019-01-07 18:35:30 -0800442 }
443
Robert Carra7827f72019-01-11 12:53:37 -0800444 /**
445 * @hide
446 */
Jorim Jaggiee540702020-04-02 21:40:52 +0200447 public void copyFrom(@NonNull SurfaceControl other) {
chaviwbeb7a0c2018-12-05 13:49:54 -0800448 mName = other.mName;
449 mWidth = other.mWidth;
450 mHeight = other.mHeight;
Robert Carreb344c72019-01-07 18:35:30 -0800451 assignNativeObject(nativeCopyFromSurfaceControl(other.mNativeObject));
chaviwbeb7a0c2018-12-05 13:49:54 -0800452 }
453
Robert Carr132c9f52017-07-31 17:02:30 -0700454 /**
Evan Rosky485df202018-12-06 14:11:12 -0800455 * owner UID.
456 * @hide
457 */
458 public static final int METADATA_OWNER_UID = 1;
459
460 /**
461 * Window type as per {@link WindowManager.LayoutParams}.
462 * @hide
463 */
464 public static final int METADATA_WINDOW_TYPE = 2;
465
466 /**
Evan Rosky9020c072018-12-06 14:11:12 -0800467 * Task id to allow association between surfaces and task.
468 * @hide
469 */
470 public static final int METADATA_TASK_ID = 3;
471
472 /**
Daichi Hirono638ae6d2019-12-18 13:59:58 +0900473 * The style of mouse cursor and hotspot.
474 * @hide
475 */
476 public static final int METADATA_MOUSE_CURSOR = 4;
477
478 /**
Daichi Hirono1d56ce32019-11-01 14:15:10 +0900479 * Accessibility ID to allow association between surfaces and accessibility tree.
480 * @hide
481 */
Daichi Hirono638ae6d2019-12-18 13:59:58 +0900482 public static final int METADATA_ACCESSIBILITY_ID = 5;
Daichi Hirono1d56ce32019-11-01 14:15:10 +0900483
484 /**
Peiyong Line3e5efd2019-03-21 20:59:47 +0000485 * A wrapper around GraphicBuffer that contains extra information about how to
486 * interpret the screenshot GraphicBuffer.
487 * @hide
488 */
489 public static class ScreenshotGraphicBuffer {
490 private final GraphicBuffer mGraphicBuffer;
491 private final ColorSpace mColorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700492 private final boolean mContainsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000493
Robert Carr66b5664f2019-04-02 14:18:56 -0700494 public ScreenshotGraphicBuffer(GraphicBuffer graphicBuffer, ColorSpace colorSpace,
495 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000496 mGraphicBuffer = graphicBuffer;
497 mColorSpace = colorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700498 mContainsSecureLayers = containsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000499 }
500
501 /**
502 * Create ScreenshotGraphicBuffer from existing native GraphicBuffer object.
503 * @param width The width in pixels of the buffer
504 * @param height The height in pixels of the buffer
505 * @param format The format of each pixel as specified in {@link PixelFormat}
506 * @param usage Hint indicating how the buffer will be used
507 * @param unwrappedNativeObject The native object of GraphicBuffer
508 * @param namedColorSpace Integer value of a named color space {@link ColorSpace.Named}
Robert Carr66b5664f2019-04-02 14:18:56 -0700509 * @param containsSecureLayer Indicates whether this graphic buffer contains captured contents
510 * of secure layers, in which case the screenshot should not be persisted.
Peiyong Line3e5efd2019-03-21 20:59:47 +0000511 */
512 private static ScreenshotGraphicBuffer createFromNative(int width, int height, int format,
Robert Carr66b5664f2019-04-02 14:18:56 -0700513 int usage, long unwrappedNativeObject, int namedColorSpace,
514 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000515 GraphicBuffer graphicBuffer = GraphicBuffer.createFromExisting(width, height, format,
516 usage, unwrappedNativeObject);
517 ColorSpace colorSpace = ColorSpace.get(ColorSpace.Named.values()[namedColorSpace]);
Robert Carr66b5664f2019-04-02 14:18:56 -0700518 return new ScreenshotGraphicBuffer(graphicBuffer, colorSpace, containsSecureLayers);
Peiyong Line3e5efd2019-03-21 20:59:47 +0000519 }
520
521 public ColorSpace getColorSpace() {
522 return mColorSpace;
523 }
524
525 public GraphicBuffer getGraphicBuffer() {
526 return mGraphicBuffer;
527 }
Robert Carr66b5664f2019-04-02 14:18:56 -0700528
529 public boolean containsSecureLayers() {
530 return mContainsSecureLayers;
531 }
Peiyong Line3e5efd2019-03-21 20:59:47 +0000532 }
533
534 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700535 * Builder class for {@link SurfaceControl} objects.
Robert Carr29fe58a2019-03-11 15:25:16 -0700536 *
537 * By default the surface will be hidden, and have "unset" bounds, meaning it can
538 * be as large as the bounds of its parent if a buffer or child so requires.
539 *
540 * It is necessary to set at least a name via {@link Builder#setName}
Robert Carre625fcf2017-09-01 12:36:28 -0700541 */
542 public static class Builder {
543 private SurfaceSession mSession;
544 private int mFlags = HIDDEN;
545 private int mWidth;
546 private int mHeight;
547 private int mFormat = PixelFormat.OPAQUE;
548 private String mName;
549 private SurfaceControl mParent;
Evan Rosky485df202018-12-06 14:11:12 -0800550 private SparseIntArray mMetadata;
Robert Carre625fcf2017-09-01 12:36:28 -0700551
552 /**
553 * Begin building a SurfaceControl with a given {@link SurfaceSession}.
554 *
555 * @param session The {@link SurfaceSession} with which to eventually construct the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800556 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700557 */
558 public Builder(SurfaceSession session) {
559 mSession = session;
560 }
561
562 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800563 * Begin building a SurfaceControl.
564 */
565 public Builder() {
566 }
567
568 /**
569 * Construct a new {@link SurfaceControl} with the set parameters. The builder
570 * remains valid.
Robert Carre625fcf2017-09-01 12:36:28 -0700571 */
Robert Carrda1d2422019-03-07 15:54:37 -0800572 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700573 public SurfaceControl build() {
Vishnu Naire86bd982018-11-28 13:23:17 -0800574 if (mWidth < 0 || mHeight < 0) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700575 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800576 "width and height must be positive or unset");
577 }
578 if ((mWidth > 0 || mHeight > 0) && (isColorLayerSet() || isContainerLayerSet())) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700579 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800580 "Only buffer layers can set a valid buffer size.");
Robert Carre625fcf2017-09-01 12:36:28 -0700581 }
Evan Rosky485df202018-12-06 14:11:12 -0800582 return new SurfaceControl(
583 mSession, mName, mWidth, mHeight, mFormat, mFlags, mParent, mMetadata);
Robert Carre625fcf2017-09-01 12:36:28 -0700584 }
585
586 /**
587 * Set a debugging-name for the SurfaceControl.
588 *
589 * @param name A name to identify the Surface in debugging.
590 */
Robert Carrda1d2422019-03-07 15:54:37 -0800591 @NonNull
592 public Builder setName(@NonNull String name) {
Robert Carre625fcf2017-09-01 12:36:28 -0700593 mName = name;
594 return this;
595 }
596
597 /**
598 * Set the initial size of the controlled surface's buffers in pixels.
599 *
600 * @param width The buffer width in pixels.
601 * @param height The buffer height in pixels.
602 */
Robert Carrda1d2422019-03-07 15:54:37 -0800603 @NonNull
Robert Carr76907ee2019-01-11 13:38:19 -0800604 public Builder setBufferSize(@IntRange(from = 0) int width,
605 @IntRange(from = 0) int height) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800606 if (width < 0 || height < 0) {
Robert Carre625fcf2017-09-01 12:36:28 -0700607 throw new IllegalArgumentException(
608 "width and height must be positive");
609 }
610 mWidth = width;
611 mHeight = height;
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000612 // set this as a buffer layer since we are specifying a buffer size.
613 return setFlags(FX_SURFACE_NORMAL, FX_SURFACE_MASK);
614 }
615
616 /**
617 * Set the initial size of the controlled surface's buffers in pixels.
618 */
619 private void unsetBufferSize() {
620 mWidth = 0;
621 mHeight = 0;
Robert Carre625fcf2017-09-01 12:36:28 -0700622 }
623
624 /**
625 * Set the pixel format of the controlled surface's buffers, using constants from
626 * {@link android.graphics.PixelFormat}.
627 */
Robert Carr76907ee2019-01-11 13:38:19 -0800628 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700629 public Builder setFormat(@PixelFormat.Format int format) {
630 mFormat = format;
631 return this;
632 }
633
634 /**
635 * Specify if the app requires a hardware-protected path to
636 * an external display sync. If protected content is enabled, but
637 * such a path is not available, then the controlled Surface will
638 * not be displayed.
639 *
640 * @param protectedContent Whether to require a protected sink.
Robert Carra7827f72019-01-11 12:53:37 -0800641 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700642 */
Robert Carr76907ee2019-01-11 13:38:19 -0800643 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700644 public Builder setProtected(boolean protectedContent) {
645 if (protectedContent) {
646 mFlags |= PROTECTED_APP;
647 } else {
648 mFlags &= ~PROTECTED_APP;
649 }
650 return this;
651 }
652
653 /**
654 * Specify whether the Surface contains secure content. If true, the system
655 * will prevent the surfaces content from being copied by another process. In
656 * particular screenshots and VNC servers will be disabled. This is however
657 * not a complete prevention of readback as {@link #setProtected}.
Robert Carra7827f72019-01-11 12:53:37 -0800658 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700659 */
Robert Carr76907ee2019-01-11 13:38:19 -0800660 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700661 public Builder setSecure(boolean secure) {
662 if (secure) {
663 mFlags |= SECURE;
664 } else {
665 mFlags &= ~SECURE;
666 }
667 return this;
668 }
669
670 /**
671 * Indicates whether the surface must be considered opaque,
672 * even if its pixel format is set to translucent. This can be useful if an
673 * application needs full RGBA 8888 support for instance but will
674 * still draw every pixel opaque.
675 * <p>
676 * This flag only determines whether opacity will be sampled from the alpha channel.
677 * Plane-alpha from calls to setAlpha() can still result in blended composition
678 * regardless of the opaque setting.
679 *
680 * Combined effects are (assuming a buffer format with an alpha channel):
681 * <ul>
682 * <li>OPAQUE + alpha(1.0) == opaque composition
683 * <li>OPAQUE + alpha(0.x) == blended composition
684 * <li>OPAQUE + alpha(0.0) == no composition
685 * <li>!OPAQUE + alpha(1.0) == blended composition
686 * <li>!OPAQUE + alpha(0.x) == blended composition
687 * <li>!OPAQUE + alpha(0.0) == no composition
688 * </ul>
689 * If the underlying buffer lacks an alpha channel, it is as if setOpaque(true)
690 * were set automatically.
691 * @param opaque Whether the Surface is OPAQUE.
692 */
Robert Carr76907ee2019-01-11 13:38:19 -0800693 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700694 public Builder setOpaque(boolean opaque) {
695 if (opaque) {
696 mFlags |= OPAQUE;
697 } else {
698 mFlags &= ~OPAQUE;
699 }
700 return this;
701 }
702
703 /**
Tiger Huang969c6082019-12-24 20:08:57 +0800704 * Set the initial visibility for the SurfaceControl.
705 *
706 * @param hidden Whether the Surface is initially HIDDEN.
707 * @hide
708 */
709 @NonNull
710 public Builder setHidden(boolean hidden) {
711 if (hidden) {
712 mFlags |= HIDDEN;
713 } else {
714 mFlags &= ~HIDDEN;
715 }
716 return this;
717 }
718
719 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700720 * Set a parent surface for our new SurfaceControl.
721 *
722 * Child surfaces are constrained to the onscreen region of their parent.
723 * Furthermore they stack relatively in Z order, and inherit the transformation
724 * of the parent.
725 *
726 * @param parent The parent control.
727 */
Robert Carr76907ee2019-01-11 13:38:19 -0800728 @NonNull
729 public Builder setParent(@Nullable SurfaceControl parent) {
Robert Carre625fcf2017-09-01 12:36:28 -0700730 mParent = parent;
731 return this;
732 }
733
734 /**
Evan Rosky485df202018-12-06 14:11:12 -0800735 * Sets a metadata int.
Robert Carre625fcf2017-09-01 12:36:28 -0700736 *
Evan Rosky485df202018-12-06 14:11:12 -0800737 * @param key metadata key
738 * @param data associated data
Robert Carra7827f72019-01-11 12:53:37 -0800739 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700740 */
Evan Rosky485df202018-12-06 14:11:12 -0800741 public Builder setMetadata(int key, int data) {
742 if (mMetadata == null) {
743 mMetadata = new SparseIntArray();
Robert Carre625fcf2017-09-01 12:36:28 -0700744 }
Evan Rosky485df202018-12-06 14:11:12 -0800745 mMetadata.put(key, data);
Robert Carre625fcf2017-09-01 12:36:28 -0700746 return this;
747 }
748
749 /**
750 * Indicate whether a 'ColorLayer' is to be constructed.
751 *
752 * Color layers will not have an associated BufferQueue and will instead always render a
753 * solid color (that is, solid before plane alpha). Currently that color is black.
754 *
Robert Carra7827f72019-01-11 12:53:37 -0800755 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700756 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000757 public Builder setColorLayer() {
758 unsetBufferSize();
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800759 return setFlags(FX_SURFACE_EFFECT, FX_SURFACE_MASK);
Robert Carre625fcf2017-09-01 12:36:28 -0700760 }
761
Vishnu Naire86bd982018-11-28 13:23:17 -0800762 private boolean isColorLayerSet() {
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800763 return (mFlags & FX_SURFACE_EFFECT) == FX_SURFACE_EFFECT;
Vishnu Naire86bd982018-11-28 13:23:17 -0800764 }
765
Robert Carre625fcf2017-09-01 12:36:28 -0700766 /**
Robert Carr48ec4e02019-07-16 14:28:47 -0700767 * @hide
768 */
769 public Builder setBLASTLayer() {
770 unsetBufferSize();
771 return setFlags(FX_SURFACE_BLAST, FX_SURFACE_MASK);
772 }
773
774 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700775 * Indicates whether a 'ContainerLayer' is to be constructed.
776 *
777 * Container layers will not be rendered in any fashion and instead are used
778 * as a parent of renderable layers.
779 *
Robert Carra7827f72019-01-11 12:53:37 -0800780 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700781 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000782 public Builder setContainerLayer() {
783 unsetBufferSize();
784 return setFlags(FX_SURFACE_CONTAINER, FX_SURFACE_MASK);
Robert Carrb6cd6432018-08-13 13:01:47 -0700785 }
786
Vishnu Naire86bd982018-11-28 13:23:17 -0800787 private boolean isContainerLayerSet() {
788 return (mFlags & FX_SURFACE_CONTAINER) == FX_SURFACE_CONTAINER;
789 }
790
Robert Carrb6cd6432018-08-13 13:01:47 -0700791 /**
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000792 * Set 'Surface creation flags' such as {@link #HIDDEN}, {@link #SECURE}.
Robert Carre625fcf2017-09-01 12:36:28 -0700793 *
794 * TODO: Finish conversion to individual builder methods?
795 * @param flags The combined flags
Robert Carra7827f72019-01-11 12:53:37 -0800796 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700797 */
798 public Builder setFlags(int flags) {
799 mFlags = flags;
800 return this;
801 }
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000802
803 private Builder setFlags(int flags, int mask) {
804 mFlags = (mFlags & ~mask) | flags;
805 return this;
806 }
Robert Carre625fcf2017-09-01 12:36:28 -0700807 }
808
809 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800810 * Create a surface with a name.
Andy McFadden314405b2014-01-29 17:18:05 -0800811 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800812 * The surface creation flags specify what kind of surface to create and
813 * certain options such as whether the surface can be assumed to be opaque
814 * and whether it should be initially hidden. Surfaces should always be
815 * created with the {@link #HIDDEN} flag set to ensure that they are not
816 * made visible prematurely before all of the surface's properties have been
817 * configured.
Andy McFadden314405b2014-01-29 17:18:05 -0800818 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800819 * Good practice is to first create the surface with the {@link #HIDDEN} flag
820 * specified, open a transaction, set the surface layer, layer stack, alpha,
chaviw619da692019-06-10 15:39:40 -0700821 * and position, call {@link Transaction#show(SurfaceControl)} if appropriate, and close the
822 * transaction.
Vishnu Naird454442d2018-11-13 13:51:01 -0800823 * <p>
824 * Bounds of the surface is determined by its crop and its buffer size. If the
825 * surface has no buffer or crop, the surface is boundless and only constrained
826 * by the size of its parent bounds.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800827 *
chaviw619da692019-06-10 15:39:40 -0700828 * @param session The surface session, must not be null.
829 * @param name The surface name, must not be null.
830 * @param w The surface initial width.
831 * @param h The surface initial height.
Tiger Huang969c6082019-12-24 20:08:57 +0800832 * @param flags The surface creation flags.
Evan Rosky485df202018-12-06 14:11:12 -0800833 * @param metadata Initial metadata.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700834 * @throws throws OutOfResourcesException If the SurfaceControl cannot be created.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800835 */
Robert Carre625fcf2017-09-01 12:36:28 -0700836 private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags,
Evan Rosky485df202018-12-06 14:11:12 -0800837 SurfaceControl parent, SparseIntArray metadata)
Robert Carrb0f39362018-03-14 13:52:25 -0700838 throws OutOfResourcesException, IllegalArgumentException {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800839 if (name == null) {
840 throw new IllegalArgumentException("name must not be null");
841 }
842
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800843 mName = name;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100844 mWidth = w;
845 mHeight = h;
Evan Rosky485df202018-12-06 14:11:12 -0800846 Parcel metaParcel = Parcel.obtain();
847 try {
848 if (metadata != null && metadata.size() > 0) {
849 metaParcel.writeInt(metadata.size());
850 for (int i = 0; i < metadata.size(); ++i) {
851 metaParcel.writeInt(metadata.keyAt(i));
852 metaParcel.writeByteArray(
Evan Roskyc12c9a32019-02-06 13:38:03 -0800853 ByteBuffer.allocate(4).order(ByteOrder.nativeOrder())
854 .putInt(metadata.valueAt(i)).array());
Evan Rosky485df202018-12-06 14:11:12 -0800855 }
Evan Roskyc12c9a32019-02-06 13:38:03 -0800856 metaParcel.setDataPosition(0);
Evan Rosky485df202018-12-06 14:11:12 -0800857 }
858 mNativeObject = nativeCreate(session, name, w, h, format, flags,
859 parent != null ? parent.mNativeObject : 0, metaParcel);
860 } finally {
861 metaParcel.recycle();
862 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800863 if (mNativeObject == 0) {
864 throw new OutOfResourcesException(
865 "Couldn't allocate SurfaceControl native object");
866 }
Jorim Jaggiee540702020-04-02 21:40:52 +0200867 mNativeHandle = nativeGetHandle(mNativeObject);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800868 mCloseGuard.open("release");
869 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700870
Jorim Jaggiee540702020-04-02 21:40:52 +0200871 /**
872 * Copy constructor. Creates a new native object pointing to the same surface as {@code other}.
873 *
874 * @param other The object to copy the surface from.
Robert Carra7827f72019-01-11 12:53:37 -0800875 * @hide
876 */
Jorim Jaggiee540702020-04-02 21:40:52 +0200877 @TestApi
878 public SurfaceControl(@NonNull SurfaceControl other) {
879 copyFrom(other);
Robert Carr3b716242016-08-16 16:02:21 -0700880 }
881
Jorim Jaggi06975df2017-12-01 14:52:13 +0100882 private SurfaceControl(Parcel in) {
chaviwbeb7a0c2018-12-05 13:49:54 -0800883 readFromParcel(in);
chaviwbeb7a0c2018-12-05 13:49:54 -0800884 }
885
Robert Carra7827f72019-01-11 12:53:37 -0800886 /**
887 * @hide
888 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800889 public SurfaceControl() {
chaviwbeb7a0c2018-12-05 13:49:54 -0800890 }
891
892 public void readFromParcel(Parcel in) {
893 if (in == null) {
894 throw new IllegalArgumentException("source must not be null");
895 }
896
Jorim Jaggi06975df2017-12-01 14:52:13 +0100897 mName = in.readString();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100898 mWidth = in.readInt();
899 mHeight = in.readInt();
Robert Carr5fea55b2018-12-10 13:05:52 -0800900
Robert Carreb344c72019-01-07 18:35:30 -0800901 long object = 0;
Robert Carr5fea55b2018-12-10 13:05:52 -0800902 if (in.readInt() != 0) {
Robert Carreb344c72019-01-07 18:35:30 -0800903 object = nativeReadFromParcel(in);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100904 }
Robert Carreb344c72019-01-07 18:35:30 -0800905 assignNativeObject(object);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100906 }
907
908 @Override
909 public int describeContents() {
910 return 0;
911 }
912
913 @Override
914 public void writeToParcel(Parcel dest, int flags) {
915 dest.writeString(mName);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100916 dest.writeInt(mWidth);
917 dest.writeInt(mHeight);
Robert Carr5fea55b2018-12-10 13:05:52 -0800918 if (mNativeObject == 0) {
919 dest.writeInt(0);
920 } else {
921 dest.writeInt(1);
922 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100923 nativeWriteToParcel(mNativeObject, dest);
Robert Carr5fea55b2018-12-10 13:05:52 -0800924
925 if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) {
926 release();
927 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100928 }
929
Vishnu Nair04ab4392018-01-10 11:00:06 -0800930 /**
Jorim Jaggiee540702020-04-02 21:40:52 +0200931 * Checks whether two {@link SurfaceControl} objects represent the same surface.
932 *
933 * @param other The other object to check
934 * @return {@code true} if these two {@link SurfaceControl} objects represent the same surface.
935 * @hide
936 */
937 @TestApi
938 public boolean isSameSurface(@NonNull SurfaceControl other) {
939 return other.mNativeHandle == mNativeHandle;
940 }
941
942 /**
Vishnu Nair04ab4392018-01-10 11:00:06 -0800943 * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link
944 * android.view.SurfaceControlProto}.
945 *
946 * @param proto Stream to write the SurfaceControl object to.
947 * @param fieldId Field Id of the SurfaceControl as defined in the parent message.
948 * @hide
949 */
Jeffrey Huangcb782852019-12-05 11:28:11 -0800950 public void dumpDebug(ProtoOutputStream proto, long fieldId) {
Vishnu Nair04ab4392018-01-10 11:00:06 -0800951 final long token = proto.start(fieldId);
952 proto.write(HASH_CODE, System.identityHashCode(this));
953 proto.write(NAME, mName);
954 proto.end(token);
955 }
956
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700957 public static final @android.annotation.NonNull Creator<SurfaceControl> CREATOR
Jorim Jaggi06975df2017-12-01 14:52:13 +0100958 = new Creator<SurfaceControl>() {
959 public SurfaceControl createFromParcel(Parcel in) {
960 return new SurfaceControl(in);
961 }
962
963 public SurfaceControl[] newArray(int size) {
964 return new SurfaceControl[size];
965 }
966 };
967
Robert Carra7827f72019-01-11 12:53:37 -0800968 /**
969 * @hide
970 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800971 @Override
972 protected void finalize() throws Throwable {
973 try {
974 if (mCloseGuard != null) {
975 mCloseGuard.warnIfOpen();
976 }
977 if (mNativeObject != 0) {
978 nativeRelease(mNativeObject);
979 }
980 } finally {
981 super.finalize();
982 }
983 }
984
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800985 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800986 * Release the local reference to the server-side surface. The surface
987 * may continue to exist on-screen as long as its parent continues
988 * to exist. To explicitly remove a surface from the screen use
Robert Carr29fe58a2019-03-11 15:25:16 -0700989 * {@link Transaction#reparent} with a null-parent. After release,
990 * {@link #isValid} will return false and other methods will throw
991 * an exception.
Robert Carr76907ee2019-01-11 13:38:19 -0800992 *
993 * Always call release() when you're done with a SurfaceControl.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800994 */
995 public void release() {
996 if (mNativeObject != 0) {
997 nativeRelease(mNativeObject);
998 mNativeObject = 0;
Jorim Jaggiee540702020-04-02 21:40:52 +0200999 mNativeHandle = 0;
Tiger Huang0fd67482020-04-15 22:16:13 +08001000 if (Build.IS_DEBUGGABLE) {
1001 mReleaseStack = new Throwable("released here");
1002 }
Robert Carr01b0dd52020-02-27 13:25:17 -08001003 mCloseGuard.close();
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001004 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001005 }
1006
1007 /**
Tiger Huang0fd67482020-04-15 22:16:13 +08001008 * Returns the call stack that assigned mNativeObject to zero.
1009 * @hide
1010 */
1011 public Throwable getReleaseStack() {
1012 return mReleaseStack;
1013 }
1014
1015 /**
Chong Zhang47e36a32016-02-29 16:44:33 -08001016 * Disconnect any client still connected to the surface.
Robert Carra7827f72019-01-11 12:53:37 -08001017 * @hide
Chong Zhang47e36a32016-02-29 16:44:33 -08001018 */
1019 public void disconnect() {
1020 if (mNativeObject != 0) {
1021 nativeDisconnect(mNativeObject);
1022 }
1023 }
1024
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001025 private void checkNotReleased() {
Tiger Huang0fd67482020-04-15 22:16:13 +08001026 if (mNativeObject == 0) {
1027 Log.wtf(TAG, "Invalid " + this + " caused by:", mReleaseStack);
1028 throw new NullPointerException(
1029 "mNativeObject of " + this + " is null. Have you called release() already?");
1030 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001031 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001032
Robert Carra7827f72019-01-11 12:53:37 -08001033 /**
Robert Carr76907ee2019-01-11 13:38:19 -08001034 * Check whether this instance points to a valid layer with the system-compositor. For
Robert Carr29fe58a2019-03-11 15:25:16 -07001035 * example this may be false if construction failed, or the layer was released
1036 * ({@link #release}).
Robert Carr76907ee2019-01-11 13:38:19 -08001037 *
1038 * @return Whether this SurfaceControl is valid.
Robert Carra7827f72019-01-11 12:53:37 -08001039 */
Robert Carr5fea55b2018-12-10 13:05:52 -08001040 public boolean isValid() {
1041 return mNativeObject != 0;
1042 }
1043
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001044 /*
1045 * set surface parameters.
1046 * needs to be inside open/closeTransaction block
1047 */
1048
Robert Carra7827f72019-01-11 12:53:37 -08001049 /** start a transaction
1050 * @hide
1051 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001052 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001053 public static void openTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -07001054 synchronized (SurfaceControl.class) {
1055 if (sGlobalTransaction == null) {
1056 sGlobalTransaction = new Transaction();
1057 }
1058 synchronized(SurfaceControl.class) {
1059 sTransactionNestCount++;
1060 }
1061 }
1062 }
1063
Robert Carrb1579c82017-09-05 14:54:47 -07001064 /**
1065 * Merge the supplied transaction in to the deprecated "global" transaction.
1066 * This clears the supplied transaction in an identical fashion to {@link Transaction#merge}.
1067 * <p>
1068 * This is a utility for interop with legacy-code and will go away with the Global Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08001069 * @hide
Robert Carrb1579c82017-09-05 14:54:47 -07001070 */
1071 @Deprecated
1072 public static void mergeToGlobalTransaction(Transaction t) {
Robert Carrc5fc4612017-12-05 11:55:40 -08001073 synchronized(SurfaceControl.class) {
Robert Carrb1579c82017-09-05 14:54:47 -07001074 sGlobalTransaction.merge(t);
1075 }
1076 }
1077
chaviwdaaeab02019-03-20 12:25:37 -07001078 /** end a transaction
1079 * @hide
Robert Carra7827f72019-01-11 12:53:37 -08001080 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001081 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001082 public static void closeTransaction() {
chaviwdaaeab02019-03-20 12:25:37 -07001083 synchronized(SurfaceControl.class) {
1084 if (sTransactionNestCount == 0) {
1085 Log.e(TAG,
1086 "Call to SurfaceControl.closeTransaction without matching openTransaction");
1087 } else if (--sTransactionNestCount > 0) {
1088 return;
1089 }
1090 sGlobalTransaction.apply();
1091 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001092 }
1093
Robert Carra7827f72019-01-11 12:53:37 -08001094 /**
1095 * @hide
1096 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001097 public void deferTransactionUntil(SurfaceControl barrier, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -08001098 synchronized(SurfaceControl.class) {
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001099 sGlobalTransaction.deferTransactionUntil(this, barrier, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -08001100 }
1101 }
1102
Robert Carra7827f72019-01-11 12:53:37 -08001103 /**
1104 * @hide
1105 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001106 public void reparentChildren(SurfaceControl newParent) {
Robert Carre13b58e2017-08-31 14:50:44 -07001107 synchronized(SurfaceControl.class) {
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001108 sGlobalTransaction.reparentChildren(this, newParent);
Robert Carre13b58e2017-08-31 14:50:44 -07001109 }
Robert Carrd5c7dd62017-03-08 10:39:30 -08001110 }
1111
Robert Carra7827f72019-01-11 12:53:37 -08001112 /**
1113 * @hide
1114 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001115 public void detachChildren() {
Robert Carre13b58e2017-08-31 14:50:44 -07001116 synchronized(SurfaceControl.class) {
1117 sGlobalTransaction.detachChildren(this);
1118 }
Rob Carr64e516f2015-10-29 00:20:45 +00001119 }
1120
Robert Carra7827f72019-01-11 12:53:37 -08001121 /**
1122 * @hide
1123 */
Robert Carr1ca6a332016-04-11 18:00:43 -07001124 public void setOverrideScalingMode(int scalingMode) {
1125 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001126 synchronized(SurfaceControl.class) {
1127 sGlobalTransaction.setOverrideScalingMode(this, scalingMode);
1128 }
Robert Carr1ca6a332016-04-11 18:00:43 -07001129 }
1130
Robert Carra7827f72019-01-11 12:53:37 -08001131 /**
1132 * @hide
1133 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001134 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001135 public void setLayer(int zorder) {
1136 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001137 synchronized(SurfaceControl.class) {
1138 sGlobalTransaction.setLayer(this, zorder);
1139 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001140 }
1141
Robert Carra7827f72019-01-11 12:53:37 -08001142 /**
1143 * @hide
1144 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001145 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001146 public void setPosition(float x, float y) {
1147 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001148 synchronized(SurfaceControl.class) {
1149 sGlobalTransaction.setPosition(this, x, y);
1150 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001151 }
1152
Robert Carra7827f72019-01-11 12:53:37 -08001153 /**
1154 * @hide
1155 */
Vishnu Naire86bd982018-11-28 13:23:17 -08001156 public void setBufferSize(int w, int h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001157 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001158 synchronized(SurfaceControl.class) {
Vishnu Naire86bd982018-11-28 13:23:17 -08001159 sGlobalTransaction.setBufferSize(this, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07001160 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001161 }
1162
Robert Carra7827f72019-01-11 12:53:37 -08001163 /**
1164 * @hide
1165 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001166 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001167 public void hide() {
1168 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001169 synchronized(SurfaceControl.class) {
1170 sGlobalTransaction.hide(this);
1171 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001172 }
1173
Robert Carra7827f72019-01-11 12:53:37 -08001174 /**
1175 * @hide
1176 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001177 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001178 public void show() {
1179 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001180 synchronized(SurfaceControl.class) {
1181 sGlobalTransaction.show(this);
1182 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001183 }
1184
Robert Carra7827f72019-01-11 12:53:37 -08001185 /**
1186 * @hide
1187 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001188 public void setTransparentRegionHint(Region region) {
1189 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001190 synchronized(SurfaceControl.class) {
1191 sGlobalTransaction.setTransparentRegionHint(this, region);
1192 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001193 }
1194
Robert Carra7827f72019-01-11 12:53:37 -08001195 /**
1196 * @hide
1197 */
Svetoslav1376d602014-03-13 11:17:26 -07001198 public boolean clearContentFrameStats() {
1199 checkNotReleased();
1200 return nativeClearContentFrameStats(mNativeObject);
1201 }
1202
Robert Carra7827f72019-01-11 12:53:37 -08001203 /**
1204 * @hide
1205 */
Svetoslav1376d602014-03-13 11:17:26 -07001206 public boolean getContentFrameStats(WindowContentFrameStats outStats) {
1207 checkNotReleased();
1208 return nativeGetContentFrameStats(mNativeObject, outStats);
1209 }
1210
Robert Carra7827f72019-01-11 12:53:37 -08001211 /**
1212 * @hide
1213 */
Svetoslav1376d602014-03-13 11:17:26 -07001214 public static boolean clearAnimationFrameStats() {
1215 return nativeClearAnimationFrameStats();
1216 }
1217
Robert Carra7827f72019-01-11 12:53:37 -08001218 /**
1219 * @hide
1220 */
Svetoslav1376d602014-03-13 11:17:26 -07001221 public static boolean getAnimationFrameStats(WindowAnimationFrameStats outStats) {
1222 return nativeGetAnimationFrameStats(outStats);
1223 }
1224
Robert Carra7827f72019-01-11 12:53:37 -08001225 /**
1226 * @hide
1227 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001228 public void setAlpha(float alpha) {
1229 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001230 synchronized(SurfaceControl.class) {
1231 sGlobalTransaction.setAlpha(this, alpha);
1232 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001233 }
1234
Robert Carra7827f72019-01-11 12:53:37 -08001235 /**
1236 * @hide
1237 */
Robert Carr0edf18f2017-02-21 20:01:47 -08001238 public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001239 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001240 synchronized(SurfaceControl.class) {
1241 sGlobalTransaction.setMatrix(this, dsdx, dtdx, dtdy, dsdy);
1242 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001243 }
1244
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001245 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08001246 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
1247 * the color can be interpreted in any color space.
1248 * @param agnostic A boolean to indicate whether the surface is color space agnostic
1249 * @hide
1250 */
1251 public void setColorSpaceAgnostic(boolean agnostic) {
1252 checkNotReleased();
1253 synchronized (SurfaceControl.class) {
1254 sGlobalTransaction.setColorSpaceAgnostic(this, agnostic);
1255 }
1256 }
1257
1258 /**
Vishnu Naird454442d2018-11-13 13:51:01 -08001259 * Bounds the surface and its children to the bounds specified. Size of the surface will be
1260 * ignored and only the crop and buffer size will be used to determine the bounds of the
1261 * surface. If no crop is specified and the surface has no buffer, the surface bounds is only
1262 * constrained by the size of its parent bounds.
1263 *
1264 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08001265 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001266 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001267 public void setWindowCrop(Rect crop) {
1268 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001269 synchronized (SurfaceControl.class) {
1270 sGlobalTransaction.setWindowCrop(this, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001271 }
1272 }
1273
Vishnu Naird454442d2018-11-13 13:51:01 -08001274 /**
Robert Carra7827f72019-01-11 12:53:37 -08001275 * @hide
1276 */
Andy McFadden314405b2014-01-29 17:18:05 -08001277 public void setOpaque(boolean isOpaque) {
1278 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001279
1280 synchronized (SurfaceControl.class) {
1281 sGlobalTransaction.setOpaque(this, isOpaque);
Andy McFadden314405b2014-01-29 17:18:05 -08001282 }
1283 }
1284
Robert Carra7827f72019-01-11 12:53:37 -08001285 /**
1286 * @hide
1287 */
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001288 public void setSecure(boolean isSecure) {
1289 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001290
1291 synchronized (SurfaceControl.class) {
1292 sGlobalTransaction.setSecure(this, isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001293 }
1294 }
1295
Robert Carra7827f72019-01-11 12:53:37 -08001296 /**
1297 * @hide
1298 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001299 public int getWidth() {
1300 synchronized (mSizeLock) {
1301 return mWidth;
1302 }
1303 }
1304
Robert Carra7827f72019-01-11 12:53:37 -08001305 /**
1306 * @hide
1307 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001308 public int getHeight() {
1309 synchronized (mSizeLock) {
1310 return mHeight;
1311 }
1312 }
1313
Robert Carre13b58e2017-08-31 14:50:44 -07001314 @Override
1315 public String toString() {
1316 return "Surface(name=" + mName + ")/@0x" +
1317 Integer.toHexString(System.identityHashCode(this));
1318 }
1319
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001320 /**
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001321 * Immutable information about physical display.
1322 *
Robert Carr76907ee2019-01-11 13:38:19 -08001323 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001324 */
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001325 public static final class DisplayInfo {
Dominik Laskowski26290bb2020-01-15 16:09:55 -08001326 public boolean isInternal;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001327 public float density;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001328 public boolean secure;
Marin Shalamanov98af1592019-10-08 10:48:08 +02001329 public DeviceProductInfo deviceProductInfo;
Robert Carra7827f72019-01-11 12:53:37 -08001330
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001331 @Override
1332 public String toString() {
Dominik Laskowski26290bb2020-01-15 16:09:55 -08001333 return "DisplayInfo{isInternal=" + isInternal
1334 + ", density=" + density
Marin Shalamanov98af1592019-10-08 10:48:08 +02001335 + ", secure=" + secure
1336 + ", deviceProductInfo=" + deviceProductInfo + "}";
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001337 }
1338 }
1339
1340 /**
1341 * Configuration supported by physical display.
1342 *
1343 * @hide
1344 */
1345 public static final class DisplayConfig {
Ady Abraham8a5e3912020-02-18 17:28:26 -08001346 /**
1347 * Invalid display config id.
1348 */
1349 public static final int INVALID_DISPLAY_CONFIG_ID = -1;
1350
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001351 public int width;
1352 public int height;
1353 public float xDpi;
1354 public float yDpi;
1355
1356 public float refreshRate;
1357 public long appVsyncOffsetNanos;
1358 public long presentationDeadlineNanos;
1359
Ady Abraham8a5e3912020-02-18 17:28:26 -08001360 /**
1361 * The config group ID this config is associated to.
1362 * Configs in the same group are similar from vendor's perspective and switching between
1363 * configs within the same group can be done seamlessly in most cases.
1364 * @see: android.hardware.graphics.composer@2.4::IComposerClient::Attribute::CONFIG_GROUP
1365 */
1366 public int configGroup;
1367
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001368 @Override
1369 public String toString() {
1370 return "DisplayConfig{width=" + width
1371 + ", height=" + height
1372 + ", xDpi=" + xDpi
1373 + ", yDpi=" + yDpi
1374 + ", refreshRate=" + refreshRate
1375 + ", appVsyncOffsetNanos=" + appVsyncOffsetNanos
Ady Abraham8a5e3912020-02-18 17:28:26 -08001376 + ", presentationDeadlineNanos=" + presentationDeadlineNanos
1377 + ", configGroup=" + configGroup + "}";
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001378 }
1379 }
1380
Robert Carra7827f72019-01-11 12:53:37 -08001381 /**
1382 * @hide
1383 */
Prashant Malanic55929a2014-05-25 01:59:21 -07001384 public static void setDisplayPowerMode(IBinder displayToken, int mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001385 if (displayToken == null) {
1386 throw new IllegalArgumentException("displayToken must not be null");
1387 }
Prashant Malanic55929a2014-05-25 01:59:21 -07001388 nativeSetDisplayPowerMode(displayToken, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001389 }
1390
Robert Carra7827f72019-01-11 12:53:37 -08001391 /**
1392 * @hide
1393 */
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001394 public static SurfaceControl.DisplayInfo getDisplayInfo(IBinder displayToken) {
1395 if (displayToken == null) {
1396 throw new IllegalArgumentException("displayToken must not be null");
1397 }
1398 return nativeGetDisplayInfo(displayToken);
1399 }
1400
1401 /**
1402 * @hide
1403 */
1404 public static SurfaceControl.DisplayConfig[] getDisplayConfigs(IBinder displayToken) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001405 if (displayToken == null) {
1406 throw new IllegalArgumentException("displayToken must not be null");
1407 }
Dan Stoza00101052014-05-02 15:23:40 -07001408 return nativeGetDisplayConfigs(displayToken);
1409 }
1410
Robert Carra7827f72019-01-11 12:53:37 -08001411 /**
1412 * @hide
1413 */
Dan Stoza00101052014-05-02 15:23:40 -07001414 public static int getActiveConfig(IBinder displayToken) {
1415 if (displayToken == null) {
1416 throw new IllegalArgumentException("displayToken must not be null");
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001417 }
Dan Stoza00101052014-05-02 15:23:40 -07001418 return nativeGetActiveConfig(displayToken);
1419 }
1420
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001421 /**
1422 * @hide
1423 */
1424 public static DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
1425 IBinder displayToken) {
1426 if (displayToken == null) {
1427 throw new IllegalArgumentException("displayToken must not be null");
1428 }
1429 return nativeGetDisplayedContentSamplingAttributes(displayToken);
1430 }
1431
1432 /**
1433 * @hide
1434 */
1435 public static boolean setDisplayedContentSamplingEnabled(
1436 IBinder displayToken, boolean enable, int componentMask, int maxFrames) {
1437 if (displayToken == null) {
1438 throw new IllegalArgumentException("displayToken must not be null");
1439 }
1440 final int maxColorComponents = 4;
1441 if ((componentMask >> maxColorComponents) != 0) {
1442 throw new IllegalArgumentException("invalid componentMask when enabling sampling");
1443 }
1444 return nativeSetDisplayedContentSamplingEnabled(
1445 displayToken, enable, componentMask, maxFrames);
1446 }
1447
1448 /**
1449 * @hide
1450 */
1451 public static DisplayedContentSample getDisplayedContentSample(
1452 IBinder displayToken, long maxFrames, long timestamp) {
1453 if (displayToken == null) {
1454 throw new IllegalArgumentException("displayToken must not be null");
1455 }
1456 return nativeGetDisplayedContentSample(displayToken, maxFrames, timestamp);
1457 }
1458
1459
Robert Carra7827f72019-01-11 12:53:37 -08001460 /**
Ana Krulec52f12892019-11-18 03:57:20 -08001461 * Contains information about desired display configuration.
1462 *
1463 * @hide
1464 */
1465 public static final class DesiredDisplayConfigSpecs {
Ana Kruleca74a8642019-11-14 00:51:00 +01001466 public int defaultConfig;
1467 public float minRefreshRate;
1468 public float maxRefreshRate;
Ana Krulec52f12892019-11-18 03:57:20 -08001469
Ana Kruleca74a8642019-11-14 00:51:00 +01001470 public DesiredDisplayConfigSpecs() {}
Ana Krulec52f12892019-11-18 03:57:20 -08001471
Ana Kruleca74a8642019-11-14 00:51:00 +01001472 public DesiredDisplayConfigSpecs(DesiredDisplayConfigSpecs other) {
1473 copyFrom(other);
1474 }
Ana Krulec52f12892019-11-18 03:57:20 -08001475
Ana Krulec52f12892019-11-18 03:57:20 -08001476 public DesiredDisplayConfigSpecs(
Ana Kruleca74a8642019-11-14 00:51:00 +01001477 int defaultConfig, float minRefreshRate, float maxRefreshRate) {
1478 this.defaultConfig = defaultConfig;
1479 this.minRefreshRate = minRefreshRate;
1480 this.maxRefreshRate = maxRefreshRate;
1481 }
1482
1483 @Override
1484 public boolean equals(Object o) {
1485 return o instanceof DesiredDisplayConfigSpecs && equals((DesiredDisplayConfigSpecs) o);
1486 }
1487
1488 /**
1489 * Tests for equality.
1490 */
1491 public boolean equals(DesiredDisplayConfigSpecs other) {
1492 return other != null && defaultConfig == other.defaultConfig
1493 && minRefreshRate == other.minRefreshRate
1494 && maxRefreshRate == other.maxRefreshRate;
1495 }
1496
1497 @Override
1498 public int hashCode() {
1499 return 0; // don't care
1500 }
1501
1502 /**
1503 * Copies the supplied object's values to this object.
1504 */
1505 public void copyFrom(DesiredDisplayConfigSpecs other) {
1506 defaultConfig = other.defaultConfig;
1507 minRefreshRate = other.minRefreshRate;
1508 maxRefreshRate = other.maxRefreshRate;
1509 }
1510
1511 @Override
1512 public String toString() {
1513 return String.format("defaultConfig=%d min=%.0f max=%.0f", defaultConfig,
1514 minRefreshRate, maxRefreshRate);
Ana Krulec52f12892019-11-18 03:57:20 -08001515 }
1516 }
1517
1518 /**
Ady Abraham42f9a2fb2019-02-26 14:13:39 -08001519 * @hide
1520 */
Ana Krulec4f753aa2019-11-14 00:49:39 +01001521 public static boolean setDesiredDisplayConfigSpecs(IBinder displayToken,
Ana Krulec52f12892019-11-18 03:57:20 -08001522 SurfaceControl.DesiredDisplayConfigSpecs desiredDisplayConfigSpecs) {
Ana Krulec4f753aa2019-11-14 00:49:39 +01001523 if (displayToken == null) {
1524 throw new IllegalArgumentException("displayToken must not be null");
1525 }
1526
Ana Krulec52f12892019-11-18 03:57:20 -08001527 return nativeSetDesiredDisplayConfigSpecs(displayToken, desiredDisplayConfigSpecs);
Ana Krulec4f753aa2019-11-14 00:49:39 +01001528 }
1529
1530 /**
1531 * @hide
1532 */
Ana Kruleca74a8642019-11-14 00:51:00 +01001533 public static SurfaceControl.DesiredDisplayConfigSpecs getDesiredDisplayConfigSpecs(
1534 IBinder displayToken) {
1535 if (displayToken == null) {
1536 throw new IllegalArgumentException("displayToken must not be null");
1537 }
1538
1539 return nativeGetDesiredDisplayConfigSpecs(displayToken);
1540 }
1541
1542 /**
1543 * @hide
1544 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001545 public static int[] getDisplayColorModes(IBinder displayToken) {
1546 if (displayToken == null) {
1547 throw new IllegalArgumentException("displayToken must not be null");
1548 }
1549 return nativeGetDisplayColorModes(displayToken);
1550 }
1551
Robert Carra7827f72019-01-11 12:53:37 -08001552 /**
Daniel Solomon10e3b332019-01-20 21:09:11 -08001553 * Color coordinates in CIE1931 XYZ color space
1554 *
1555 * @hide
1556 */
1557 public static final class CieXyz {
1558 /**
1559 * @hide
1560 */
1561 public float X;
1562
1563 /**
1564 * @hide
1565 */
1566 public float Y;
1567
1568 /**
1569 * @hide
1570 */
1571 public float Z;
1572 }
1573
1574 /**
1575 * Contains a display's color primaries
1576 *
1577 * @hide
1578 */
1579 public static final class DisplayPrimaries {
1580 /**
1581 * @hide
1582 */
1583 public CieXyz red;
1584
1585 /**
1586 * @hide
1587 */
1588 public CieXyz green;
1589
1590 /**
1591 * @hide
1592 */
1593 public CieXyz blue;
1594
1595 /**
1596 * @hide
1597 */
1598 public CieXyz white;
1599
1600 /**
1601 * @hide
1602 */
1603 public DisplayPrimaries() {
1604 }
1605 }
1606
1607 /**
1608 * @hide
1609 */
1610 public static SurfaceControl.DisplayPrimaries getDisplayNativePrimaries(
1611 IBinder displayToken) {
1612 if (displayToken == null) {
1613 throw new IllegalArgumentException("displayToken must not be null");
1614 }
1615
1616 return nativeGetDisplayNativePrimaries(displayToken);
1617 }
1618
1619 /**
Robert Carra7827f72019-01-11 12:53:37 -08001620 * @hide
1621 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001622 public static int getActiveColorMode(IBinder displayToken) {
1623 if (displayToken == null) {
1624 throw new IllegalArgumentException("displayToken must not be null");
1625 }
1626 return nativeGetActiveColorMode(displayToken);
1627 }
1628
Robert Carra7827f72019-01-11 12:53:37 -08001629 /**
1630 * @hide
1631 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001632 public static boolean setActiveColorMode(IBinder displayToken, int colorMode) {
1633 if (displayToken == null) {
1634 throw new IllegalArgumentException("displayToken must not be null");
1635 }
1636 return nativeSetActiveColorMode(displayToken, colorMode);
1637 }
1638
Robert Carra7827f72019-01-11 12:53:37 -08001639 /**
Peiyong Lin5f4a5682019-01-17 11:37:07 -08001640 * Returns an array of color spaces with 2 elements. The first color space is the
1641 * default color space and second one is wide color gamut color space.
1642 * @hide
1643 */
1644 public static ColorSpace[] getCompositionColorSpaces() {
1645 int[] dataspaces = nativeGetCompositionDataspaces();
1646 ColorSpace srgb = ColorSpace.get(ColorSpace.Named.SRGB);
1647 ColorSpace[] colorSpaces = { srgb, srgb };
1648 if (dataspaces.length == 2) {
1649 for (int i = 0; i < 2; ++i) {
1650 switch(dataspaces[i]) {
1651 case INTERNAL_DATASPACE_DISPLAY_P3:
1652 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.DISPLAY_P3);
1653 break;
1654 case INTERNAL_DATASPACE_SCRGB:
1655 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.EXTENDED_SRGB);
1656 break;
1657 case INTERNAL_DATASPACE_SRGB:
1658 // Other dataspace is not recognized, use SRGB color space instead,
1659 // the default value of the array is already SRGB, thus do nothing.
1660 default:
1661 break;
1662 }
1663 }
1664 }
1665 return colorSpaces;
1666 }
1667
1668 /**
Robert Carra7827f72019-01-11 12:53:37 -08001669 * @hide
1670 */
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001671 public static void setAutoLowLatencyMode(IBinder displayToken, boolean on) {
1672 if (displayToken == null) {
1673 throw new IllegalArgumentException("displayToken must not be null");
1674 }
1675
1676 nativeSetAutoLowLatencyMode(displayToken, on);
1677 }
1678
1679 /**
1680 * @hide
1681 */
1682 public static void setGameContentType(IBinder displayToken, boolean on) {
1683 if (displayToken == null) {
1684 throw new IllegalArgumentException("displayToken must not be null");
1685 }
1686
1687 nativeSetGameContentType(displayToken, on);
1688 }
1689
1690 /**
1691 * @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 */
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001744 public static boolean getAutoLowLatencyModeSupport(IBinder displayToken) {
1745 if (displayToken == null) {
1746 throw new IllegalArgumentException("displayToken must not be null");
1747 }
1748
1749 return nativeGetAutoLowLatencyModeSupport(displayToken);
1750 }
1751
1752 /**
1753 * @hide
1754 */
1755 public static boolean getGameContentTypeSupport(IBinder displayToken) {
1756 if (displayToken == null) {
1757 throw new IllegalArgumentException("displayToken must not be null");
1758 }
1759
1760 return nativeGetGameContentTypeSupport(displayToken);
1761 }
1762
1763 /**
1764 * @hide
1765 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001766 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001767 public static IBinder createDisplay(String name, boolean secure) {
1768 if (name == null) {
1769 throw new IllegalArgumentException("name must not be null");
1770 }
1771 return nativeCreateDisplay(name, secure);
1772 }
1773
Robert Carra7827f72019-01-11 12:53:37 -08001774 /**
1775 * @hide
1776 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001777 @UnsupportedAppUsage
Jesse Hall6a6bc212013-08-08 12:15:03 -07001778 public static void destroyDisplay(IBinder displayToken) {
1779 if (displayToken == null) {
1780 throw new IllegalArgumentException("displayToken must not be null");
1781 }
1782 nativeDestroyDisplay(displayToken);
1783 }
1784
Robert Carra7827f72019-01-11 12:53:37 -08001785 /**
1786 * @hide
1787 */
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001788 public static long[] getPhysicalDisplayIds() {
1789 return nativeGetPhysicalDisplayIds();
1790 }
1791
1792 /**
1793 * @hide
1794 */
1795 public static IBinder getPhysicalDisplayToken(long physicalDisplayId) {
1796 return nativeGetPhysicalDisplayToken(physicalDisplayId);
1797 }
1798
1799 /**
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001800 * TODO(b/116025192): Remove this stopgap once framework is display-agnostic.
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001801 *
1802 * @hide
1803 */
1804 public static IBinder getInternalDisplayToken() {
1805 final long[] physicalDisplayIds = getPhysicalDisplayIds();
1806 if (physicalDisplayIds.length == 0) {
1807 return null;
1808 }
1809 return getPhysicalDisplayToken(physicalDisplayIds[0]);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001810 }
1811
Mathias Agopian0449a402013-03-01 23:01:51 -08001812 /**
chaviw08520a02018-09-10 16:44:56 -07001813 * @see SurfaceControl#screenshot(IBinder, Surface, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001814 * @hide
Mathias Agopian0449a402013-03-01 23:01:51 -08001815 */
1816 public static void screenshot(IBinder display, Surface consumer) {
chaviw08520a02018-09-10 16:44:56 -07001817 screenshot(display, consumer, new Rect(), 0, 0, false, 0);
1818 }
1819
1820 /**
1821 * Copy the current screen contents into the provided {@link Surface}
1822 *
1823 * @param consumer The {@link Surface} to take the screenshot into.
1824 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001825 * @hide
chaviw08520a02018-09-10 16:44:56 -07001826 */
1827 public static void screenshot(IBinder display, Surface consumer, Rect sourceCrop, int width,
1828 int height, boolean useIdentityTransform, int rotation) {
1829 if (consumer == null) {
1830 throw new IllegalArgumentException("consumer must not be null");
1831 }
1832
Peiyong Line3e5efd2019-03-21 20:59:47 +00001833 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(display, sourceCrop, width,
1834 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07001835 try {
Peiyong Linccc06b62019-06-25 17:31:09 -07001836 consumer.attachAndQueueBufferWithColorSpace(buffer.getGraphicBuffer(),
1837 buffer.getColorSpace());
chaviw08520a02018-09-10 16:44:56 -07001838 } catch (RuntimeException e) {
1839 Log.w(TAG, "Failed to take screenshot - " + e.getMessage());
1840 }
1841 }
1842
1843 /**
1844 * @see SurfaceControl#screenshot(Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001845 * @hide
chaviw08520a02018-09-10 16:44:56 -07001846 */
1847 @UnsupportedAppUsage
1848 public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) {
1849 return screenshot(sourceCrop, width, height, false, rotation);
Mathias Agopian0449a402013-03-01 23:01:51 -08001850 }
1851
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001852 /**
chaviw1da9cd92017-12-06 10:48:11 -08001853 * Copy the current screen contents into a hardware bitmap and return it.
1854 * Note: If you want to modify the Bitmap in software, you will need to copy the Bitmap into
1855 * a software Bitmap using {@link Bitmap#copy(Bitmap.Config, boolean)}
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001856 *
chaviw08520a02018-09-10 16:44:56 -07001857 * CAVEAT: Versions of screenshot that return a {@link Bitmap} can be extremely slow; avoid use
1858 * unless absolutely necessary; prefer the versions that use a {@link Surface} such as
1859 * {@link SurfaceControl#screenshot(IBinder, Surface)} or {@link GraphicBuffer} such as
1860 * {@link SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}.
Mathias Agopian0449a402013-03-01 23:01:51 -08001861 *
chaviw08520a02018-09-10 16:44:56 -07001862 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001863 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001864 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001865 @UnsupportedAppUsage
Dan Stoza9890e3412014-05-22 16:12:54 -07001866 public static Bitmap screenshot(Rect sourceCrop, int width, int height,
chaviw08520a02018-09-10 16:44:56 -07001867 boolean useIdentityTransform, int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001868 // TODO: should take the display as a parameter
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001869 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
1870 if (displayToken == null) {
1871 Log.w(TAG, "Failed to take screenshot because internal display is disconnected");
1872 return null;
1873 }
1874
chaviwa69e0a72017-11-29 17:55:12 -08001875 if (rotation == ROTATION_90 || rotation == ROTATION_270) {
1876 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
1877 }
1878
1879 SurfaceControl.rotateCropForSF(sourceCrop, rotation);
Peiyong Line3e5efd2019-03-21 20:59:47 +00001880 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(displayToken, sourceCrop, width,
1881 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07001882
1883 if (buffer == null) {
1884 Log.w(TAG, "Failed to take screenshot");
1885 return null;
1886 }
Sunny Goyal62915b22019-04-10 12:28:47 -07001887 return Bitmap.wrapHardwareBuffer(buffer.getGraphicBuffer(), buffer.getColorSpace());
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001888 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001889
chaviw08520a02018-09-10 16:44:56 -07001890 /**
1891 * Captures all the surfaces in a display and returns a {@link GraphicBuffer} with the content.
1892 *
1893 * @param display The display to take the screenshot of.
1894 * @param sourceCrop The portion of the screen to capture into the Bitmap; caller may
1895 * pass in 'new Rect()' if no cropping is desired.
1896 * @param width The desired width of the returned bitmap; the raw screen will be
1897 * scaled down to this size; caller may pass in 0 if no scaling is
1898 * desired.
1899 * @param height The desired height of the returned bitmap; the raw screen will
1900 * be scaled down to this size; caller may pass in 0 if no scaling
1901 * is desired.
1902 * @param useIdentityTransform Replace whatever transformation (rotation, scaling, translation)
1903 * the surface layers are currently using with the identity
1904 * transformation while taking the screenshot.
1905 * @param rotation Apply a custom clockwise rotation to the screenshot, i.e.
1906 * Surface.ROTATION_0,90,180,270. SurfaceFlinger will always take
1907 * screenshots in its native portrait orientation by default, so
1908 * this is useful for returning screenshots that are independent of
1909 * device orientation.
1910 * @return Returns a GraphicBuffer that contains the captured content.
Robert Carra7827f72019-01-11 12:53:37 -08001911 * @hide
chaviw08520a02018-09-10 16:44:56 -07001912 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00001913 public static ScreenshotGraphicBuffer screenshotToBuffer(IBinder display, Rect sourceCrop,
1914 int width, int height, boolean useIdentityTransform, int rotation) {
Mathias Agopian0449a402013-03-01 23:01:51 -08001915 if (display == null) {
1916 throw new IllegalArgumentException("displayToken must not be null");
1917 }
chaviw08520a02018-09-10 16:44:56 -07001918
Robert Carr5c52b132019-02-15 15:48:11 -08001919 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1920 false /* captureSecureLayers */);
1921 }
1922
1923 /**
1924 * Like screenshotToBuffer, but if the caller is AID_SYSTEM, allows
1925 * for the capture of secure layers. This is used for the screen rotation
1926 * animation where the system server takes screenshots but does
1927 * not persist them or allow them to leave the server. However in other
1928 * cases in the system server, we mostly want to omit secure layers
1929 * like when we take a screenshot on behalf of the assistant.
1930 *
1931 * @hide
1932 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00001933 public static ScreenshotGraphicBuffer screenshotToBufferWithSecureLayersUnsafe(IBinder display,
Robert Carr5c52b132019-02-15 15:48:11 -08001934 Rect sourceCrop, int width, int height, boolean useIdentityTransform,
1935 int rotation) {
1936 if (display == null) {
1937 throw new IllegalArgumentException("displayToken must not be null");
1938 }
1939
1940 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1941 true /* captureSecureLayers */);
Mathias Agopian0449a402013-03-01 23:01:51 -08001942 }
Robert Carre13b58e2017-08-31 14:50:44 -07001943
chaviwa69e0a72017-11-29 17:55:12 -08001944 private static void rotateCropForSF(Rect crop, int rot) {
1945 if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270) {
1946 int tmp = crop.top;
1947 crop.top = crop.left;
1948 crop.left = tmp;
1949 tmp = crop.right;
1950 crop.right = crop.bottom;
1951 crop.bottom = tmp;
1952 }
1953 }
1954
chaviw1cda84c2017-10-23 16:47:10 -07001955 /**
chaviw1da9cd92017-12-06 10:48:11 -08001956 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
chaviw1cda84c2017-10-23 16:47:10 -07001957 *
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001958 * @param layer The root layer to capture.
chaviwfbe47df2017-11-10 16:14:49 -08001959 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
Vishnu Naira8bff972019-12-03 10:18:10 -08001960 * Rect()' or null if no cropping is desired. If the root layer does not
1961 * have a buffer or a crop set, then a non-empty source crop must be
1962 * specified.
chaviwfbe47df2017-11-10 16:14:49 -08001963 * @param frameScale The desired scale of the returned buffer; the raw
1964 * screen will be scaled up/down.
Chavi Weingartend7ec64c2017-11-30 01:52:01 +00001965 *
1966 * @return Returns a GraphicBuffer that contains the layer capture.
Robert Carra7827f72019-01-11 12:53:37 -08001967 * @hide
chaviw1cda84c2017-10-23 16:47:10 -07001968 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001969 public static ScreenshotGraphicBuffer captureLayers(SurfaceControl layer, Rect sourceCrop,
chaviwfbe47df2017-11-10 16:14:49 -08001970 float frameScale) {
Chiawei Wang02202d12019-01-03 18:12:13 +08001971 return captureLayers(layer, sourceCrop, frameScale, PixelFormat.RGBA_8888);
1972 }
1973
1974 /**
1975 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
1976 *
1977 * @param layer The root layer to capture.
1978 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
Vishnu Naira8bff972019-12-03 10:18:10 -08001979 * Rect()' or null if no cropping is desired. If the root layer does not
1980 * have a buffer or a crop set, then a non-empty source crop must be
1981 * specified.
Chiawei Wang02202d12019-01-03 18:12:13 +08001982 * @param frameScale The desired scale of the returned buffer; the raw
1983 * screen will be scaled up/down.
1984 * @param format The desired pixel format of the returned buffer.
1985 *
1986 * @return Returns a GraphicBuffer that contains the layer capture.
1987 * @hide
1988 */
1989 public static ScreenshotGraphicBuffer captureLayers(SurfaceControl layer, Rect sourceCrop,
1990 float frameScale, int format) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07001991 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
Chiawei Wang02202d12019-01-03 18:12:13 +08001992 return nativeCaptureLayers(displayToken, layer.mNativeObject, sourceCrop, frameScale, null,
1993 format);
Robert Carrffcdc512019-04-02 11:51:11 -07001994 }
1995
1996 /**
1997 * Like {@link captureLayers} but with an array of layer handles to exclude.
1998 * @hide
1999 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002000 public static ScreenshotGraphicBuffer captureLayersExcluding(SurfaceControl layer,
Robert Carr689a1b02020-03-12 15:33:51 -07002001 Rect sourceCrop, float frameScale, int format, SurfaceControl[] exclude) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07002002 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002003 long[] nativeExcludeObjects = new long[exclude.length];
2004 for (int i = 0; i < exclude.length; i++) {
2005 nativeExcludeObjects[i] = exclude[i].mNativeObject;
2006 }
2007 return nativeCaptureLayers(displayToken, layer.mNativeObject, sourceCrop, frameScale,
Chiawei Wang02202d12019-01-03 18:12:13 +08002008 nativeExcludeObjects, PixelFormat.RGBA_8888);
chaviw1cda84c2017-10-23 16:47:10 -07002009 }
2010
Robert Carra7827f72019-01-11 12:53:37 -08002011 /**
Peiyong Lin0ddb7d42019-01-16 13:25:09 -08002012 * Returns whether protected content is supported in GPU composition.
2013 * @hide
2014 */
2015 public static boolean getProtectedContentSupport() {
2016 return nativeGetProtectedContentSupport();
2017 }
2018
2019 /**
Dan Gittik832b4972019-02-13 18:17:47 +00002020 * Returns whether brightness operations are supported on a display.
2021 *
2022 * @param displayToken
2023 * The token for the display.
2024 *
2025 * @return Whether brightness operations are supported on the display.
2026 *
2027 * @hide
2028 */
2029 public static boolean getDisplayBrightnessSupport(IBinder displayToken) {
2030 return nativeGetDisplayBrightnessSupport(displayToken);
2031 }
2032
2033 /**
2034 * Sets the brightness of a display.
2035 *
2036 * @param displayToken
2037 * The token for the display whose brightness is set.
2038 * @param brightness
2039 * A number between 0.0f (minimum brightness) and 1.0f (maximum brightness), or -1.0f to
2040 * turn the backlight off.
2041 *
2042 * @return Whether the method succeeded or not.
2043 *
2044 * @throws IllegalArgumentException if:
2045 * - displayToken is null;
2046 * - brightness is NaN or greater than 1.0f.
2047 *
2048 * @hide
2049 */
2050 public static boolean setDisplayBrightness(IBinder displayToken, float brightness) {
2051 Objects.requireNonNull(displayToken);
2052 if (Float.isNaN(brightness) || brightness > 1.0f
2053 || (brightness < 0.0f && brightness != -1.0f)) {
2054 throw new IllegalArgumentException("brightness must be a number between 0.0f and 1.0f,"
2055 + " or -1 to turn the backlight off.");
2056 }
2057 return nativeSetDisplayBrightness(displayToken, brightness);
2058 }
2059
chaviwa51724f2019-09-19 09:50:11 -07002060 /**
2061 * Creates a mirrored hierarchy for the mirrorOf {@link SurfaceControl}
2062 *
2063 * Real Hierarchy Mirror
2064 * SC (value that's returned)
2065 * |
2066 * A A'
2067 * | |
2068 * B B'
2069 *
2070 * @param mirrorOf The root of the hierarchy that should be mirrored.
2071 * @return A SurfaceControl that's the parent of the root of the mirrored hierarchy.
2072 *
2073 * @hide
2074 */
2075 public static SurfaceControl mirrorSurface(SurfaceControl mirrorOf) {
2076 long nativeObj = nativeMirrorSurface(mirrorOf.mNativeObject);
2077 SurfaceControl sc = new SurfaceControl();
2078 sc.assignNativeObject(nativeObj);
2079 return sc;
2080 }
2081
Vishnu Nair4a067c52019-11-19 14:25:56 -08002082 private static void validateColorArg(@Size(4) float[] color) {
2083 final String msg = "Color must be specified as a float array with"
2084 + " four values to represent r, g, b, a in range [0..1]";
2085 if (color.length != 4) {
2086 throw new IllegalArgumentException(msg);
2087 }
2088 for (float c:color) {
2089 if ((c < 0.f) || (c > 1.f)) {
2090 throw new IllegalArgumentException(msg);
2091 }
2092 }
2093 }
2094
2095 /**
2096 * Sets the global configuration for all the shadows drawn by SurfaceFlinger. Shadow follows
2097 * material design guidelines.
2098 *
2099 * @param ambientColor Color applied to the ambient shadow. The alpha is premultiplied. A
2100 * float array with four values to represent r, g, b, a in range [0..1]
2101 * @param spotColor Color applied to the spot shadow. The alpha is premultiplied. The position
2102 * of the spot shadow depends on the light position. A float array with
2103 * four values to represent r, g, b, a in range [0..1]
2104 * @param lightPosY Y axis position of the light used to cast the spot shadow in pixels.
2105 * @param lightPosZ Z axis position of the light used to cast the spot shadow in pixels. The X
2106 * axis position is set to the display width / 2.
2107 * @param lightRadius Radius of the light casting the shadow in pixels.
2108 *[
2109 * @hide
2110 */
2111 public static void setGlobalShadowSettings(@Size(4) float[] ambientColor,
2112 @Size(4) float[] spotColor, float lightPosY, float lightPosZ, float lightRadius) {
2113 validateColorArg(ambientColor);
2114 validateColorArg(spotColor);
2115 nativeSetGlobalShadowSettings(ambientColor, spotColor, lightPosY, lightPosZ, lightRadius);
2116 }
2117
Vishnu Nair629df2b2019-06-11 16:03:38 -07002118 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002119 * An atomic set of changes to a set of SurfaceControl.
Robert Carra7827f72019-01-11 12:53:37 -08002120 */
Vishnu Nair629df2b2019-06-11 16:03:38 -07002121 public static class Transaction implements Closeable, Parcelable {
Robert Carr76907ee2019-01-11 13:38:19 -08002122 /**
2123 * @hide
2124 */
Robert Carre13b58e2017-08-31 14:50:44 -07002125 public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
2126 Transaction.class.getClassLoader(),
2127 nativeGetNativeTransactionFinalizer(), 512);
Robert Carr48ec4e02019-07-16 14:28:47 -07002128 /**
2129 * @hide
2130 */
2131 public long mNativeObject;
Robert Carre13b58e2017-08-31 14:50:44 -07002132
Jorim Jaggia5e10572017-11-15 14:36:26 +01002133 private final ArrayMap<SurfaceControl, Point> mResizedSurfaces = new ArrayMap<>();
Robert Carre13b58e2017-08-31 14:50:44 -07002134 Runnable mFreeNativeResources;
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002135 private static final float[] INVALID_COLOR = {-1, -1, -1};
Robert Carre13b58e2017-08-31 14:50:44 -07002136
Robert Carra7827f72019-01-11 12:53:37 -08002137 /**
Robert Carrfee0b822019-12-17 23:56:44 -08002138 * @hide
2139 */
2140 protected void checkPreconditions(SurfaceControl sc) {
2141 sc.checkNotReleased();
2142 }
2143
2144 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002145 * Open a new transaction object. The transaction may be filed with commands to
2146 * manipulate {@link SurfaceControl} instances, and then applied atomically with
2147 * {@link #apply}. Eventually the user should invoke {@link #close}, when the object
2148 * is no longer required. Note however that re-using a transaction after a call to apply
2149 * is allowed as a convenience.
Robert Carra7827f72019-01-11 12:53:37 -08002150 */
Robert Carre13b58e2017-08-31 14:50:44 -07002151 public Transaction() {
2152 mNativeObject = nativeCreateTransaction();
2153 mFreeNativeResources
2154 = sRegistry.registerNativeAllocation(this, mNativeObject);
2155 }
2156
Vishnu Nair629df2b2019-06-11 16:03:38 -07002157 private Transaction(Parcel in) {
2158 readFromParcel(in);
2159 }
2160
Robert Carre13b58e2017-08-31 14:50:44 -07002161 /**
2162 * Apply the transaction, clearing it's state, and making it usable
2163 * as a new transaction.
2164 */
2165 public void apply() {
2166 apply(false);
2167 }
2168
2169 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002170 * Release the native transaction object, without applying it.
Robert Carre13b58e2017-08-31 14:50:44 -07002171 */
2172 @Override
2173 public void close() {
2174 mFreeNativeResources.run();
2175 mNativeObject = 0;
2176 }
2177
2178 /**
2179 * Jankier version of apply. Avoid use (b/28068298).
Robert Carra7827f72019-01-11 12:53:37 -08002180 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002181 */
2182 public void apply(boolean sync) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002183 applyResizedSurfaces();
Robert Carre13b58e2017-08-31 14:50:44 -07002184 nativeApplyTransaction(mNativeObject, sync);
2185 }
2186
Jorim Jaggia5e10572017-11-15 14:36:26 +01002187 private void applyResizedSurfaces() {
2188 for (int i = mResizedSurfaces.size() - 1; i >= 0; i--) {
2189 final Point size = mResizedSurfaces.valueAt(i);
2190 final SurfaceControl surfaceControl = mResizedSurfaces.keyAt(i);
2191 synchronized (surfaceControl.mSizeLock) {
2192 surfaceControl.mWidth = size.x;
2193 surfaceControl.mHeight = size.y;
2194 }
2195 }
2196 mResizedSurfaces.clear();
2197 }
2198
Robert Carra7827f72019-01-11 12:53:37 -08002199 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002200 * Toggle the visibility of a given Layer and it's sub-tree.
2201 *
2202 * @param sc The SurfaceControl for which to set the visibility
2203 * @param visible The new visibility
2204 * @return This transaction object.
2205 */
2206 @NonNull
2207 public Transaction setVisibility(@NonNull SurfaceControl sc, boolean visible) {
Robert Carrfee0b822019-12-17 23:56:44 -08002208 checkPreconditions(sc);
Robert Carr76907ee2019-01-11 13:38:19 -08002209 if (visible) {
2210 return show(sc);
2211 } else {
2212 return hide(sc);
2213 }
2214 }
2215
2216 /**
Ana Krulecfea97172019-11-02 23:11:02 +01002217 * This information is passed to SurfaceFlinger to decide which window should have a
2218 * priority when deciding about the refresh rate of the display. All windows have the
2219 * lowest priority by default.
2220 * @hide
2221 */
2222 @NonNull
2223 public Transaction setFrameRateSelectionPriority(@NonNull SurfaceControl sc, int priority) {
2224 sc.checkNotReleased();
2225 nativeSetFrameRateSelectionPriority(mNativeObject, sc.mNativeObject, priority);
2226 return this;
2227 }
2228
2229 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002230 * Request that a given surface and it's sub-tree be shown.
2231 *
2232 * @param sc The surface to show.
2233 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002234 * @hide
2235 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002236 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002237 public Transaction show(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002238 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002239 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_HIDDEN);
2240 return this;
2241 }
2242
Robert Carra7827f72019-01-11 12:53:37 -08002243 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002244 * Request that a given surface and it's sub-tree be hidden.
2245 *
2246 * @param sc The surface to hidden.
2247 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002248 * @hide
2249 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002250 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002251 public Transaction hide(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002252 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002253 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_HIDDEN, SURFACE_HIDDEN);
2254 return this;
2255 }
2256
Robert Carra7827f72019-01-11 12:53:37 -08002257 /**
2258 * @hide
2259 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002260 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002261 public Transaction setPosition(SurfaceControl sc, float x, float y) {
Robert Carrfee0b822019-12-17 23:56:44 -08002262 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002263 nativeSetPosition(mNativeObject, sc.mNativeObject, x, y);
2264 return this;
2265 }
2266
Robert Carra7827f72019-01-11 12:53:37 -08002267 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002268 * Set the default buffer size for the SurfaceControl, if there is a
2269 * {@link Surface} associated with the control, then
Robert Carr76907ee2019-01-11 13:38:19 -08002270 * this will be the default size for buffers dequeued from it.
2271 * @param sc The surface to set the buffer size for.
2272 * @param w The default width
2273 * @param h The default height
2274 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002275 */
Robert Carr76907ee2019-01-11 13:38:19 -08002276 @NonNull
2277 public Transaction setBufferSize(@NonNull SurfaceControl sc,
2278 @IntRange(from = 0) int w, @IntRange(from = 0) int h) {
Robert Carrfee0b822019-12-17 23:56:44 -08002279 checkPreconditions(sc);
Jorim Jaggia5e10572017-11-15 14:36:26 +01002280 mResizedSurfaces.put(sc, new Point(w, h));
Jorim Jaggidfc27372017-10-27 17:47:49 +02002281 nativeSetSize(mNativeObject, sc.mNativeObject, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07002282 return this;
2283 }
2284
Robert Carra7827f72019-01-11 12:53:37 -08002285 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002286 * Set the Z-order for a given SurfaceControl, relative to it's siblings.
2287 * If two siblings share the same Z order the ordering is undefined. Surfaces
2288 * with a negative Z will be placed below the parent surface.
2289 *
2290 * @param sc The SurfaceControl to set the Z order on
2291 * @param z The Z-order
2292 * @return This Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002293 */
Robert Carr76907ee2019-01-11 13:38:19 -08002294 @NonNull
2295 public Transaction setLayer(@NonNull SurfaceControl sc,
2296 @IntRange(from = Integer.MIN_VALUE, to = Integer.MAX_VALUE) int z) {
Robert Carrfee0b822019-12-17 23:56:44 -08002297 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002298 nativeSetLayer(mNativeObject, sc.mNativeObject, z);
2299 return this;
2300 }
2301
Robert Carra7827f72019-01-11 12:53:37 -08002302 /**
2303 * @hide
2304 */
Robert Carr77e34942017-10-18 19:13:56 -07002305 public Transaction setRelativeLayer(SurfaceControl sc, SurfaceControl relativeTo, int z) {
Robert Carrfee0b822019-12-17 23:56:44 -08002306 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002307 nativeSetRelativeLayer(mNativeObject, sc.mNativeObject, relativeTo.mNativeObject, z);
Robert Carre13b58e2017-08-31 14:50:44 -07002308 return this;
2309 }
2310
Robert Carra7827f72019-01-11 12:53:37 -08002311 /**
2312 * @hide
2313 */
Robert Carre13b58e2017-08-31 14:50:44 -07002314 public Transaction setTransparentRegionHint(SurfaceControl sc, Region transparentRegion) {
Robert Carrfee0b822019-12-17 23:56:44 -08002315 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002316 nativeSetTransparentRegionHint(mNativeObject,
2317 sc.mNativeObject, transparentRegion);
2318 return this;
2319 }
2320
Robert Carra7827f72019-01-11 12:53:37 -08002321 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002322 * Set the alpha for a given surface. If the alpha is non-zero the SurfaceControl
2323 * will be blended with the Surfaces under it according to the specified ratio.
2324 *
2325 * @param sc The given SurfaceControl.
2326 * @param alpha The alpha to set.
Robert Carra7827f72019-01-11 12:53:37 -08002327 */
Robert Carr76907ee2019-01-11 13:38:19 -08002328 @NonNull
2329 public Transaction setAlpha(@NonNull SurfaceControl sc,
2330 @FloatRange(from = 0.0, to = 1.0) float alpha) {
Robert Carrfee0b822019-12-17 23:56:44 -08002331 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002332 nativeSetAlpha(mNativeObject, sc.mNativeObject, alpha);
2333 return this;
2334 }
2335
Robert Carra7827f72019-01-11 12:53:37 -08002336 /**
2337 * @hide
2338 */
Robert Carr788f5742018-07-30 17:46:45 -07002339 public Transaction setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle) {
Robert Carrfee0b822019-12-17 23:56:44 -08002340 checkPreconditions(sc);
Robert Carr788f5742018-07-30 17:46:45 -07002341 nativeSetInputWindowInfo(mNativeObject, sc.mNativeObject, handle);
2342 return this;
2343 }
2344
chaviw59f532e2018-12-26 15:34:59 -08002345 /**
chaviw319cd0782019-02-14 11:00:23 -08002346 * Waits until any changes to input windows have been sent from SurfaceFlinger to
2347 * InputFlinger before returning.
2348 *
2349 * @hide
2350 */
2351 public Transaction syncInputWindows() {
2352 nativeSyncInputWindows(mNativeObject);
2353 return this;
2354 }
2355
2356 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002357 * Specify how the buffer assosciated with this Surface is mapped in to the
2358 * parent coordinate space. The source frame will be scaled to fit the destination
2359 * frame, after being rotated according to the orientation parameter.
2360 *
2361 * @param sc The SurfaceControl to specify the geometry of
2362 * @param sourceCrop The source rectangle in buffer space. Or null for the entire buffer.
2363 * @param destFrame The destination rectangle in parent space. Or null for the source frame.
2364 * @param orientation The buffer rotation
2365 * @return This transaction object.
2366 */
2367 @NonNull
2368 public Transaction setGeometry(@NonNull SurfaceControl sc, @Nullable Rect sourceCrop,
2369 @Nullable Rect destFrame, @Surface.Rotation int orientation) {
Robert Carrfee0b822019-12-17 23:56:44 -08002370 checkPreconditions(sc);
Robert Carr76907ee2019-01-11 13:38:19 -08002371 nativeSetGeometry(mNativeObject, sc.mNativeObject, sourceCrop, destFrame, orientation);
2372 return this;
2373 }
2374
2375 /**
Robert Carra7827f72019-01-11 12:53:37 -08002376 * @hide
2377 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002378 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002379 public Transaction setMatrix(SurfaceControl sc,
2380 float dsdx, float dtdx, float dtdy, float dsdy) {
Robert Carrfee0b822019-12-17 23:56:44 -08002381 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002382 nativeSetMatrix(mNativeObject, sc.mNativeObject,
2383 dsdx, dtdx, dtdy, dsdy);
2384 return this;
2385 }
2386
Robert Carra7827f72019-01-11 12:53:37 -08002387 /**
chaviw619da692019-06-10 15:39:40 -07002388 * Sets the transform and position of a {@link SurfaceControl} from a 3x3 transformation
2389 * matrix.
2390 *
2391 * @param sc SurfaceControl to set matrix of
2392 * @param matrix The matrix to apply.
2393 * @param float9 An array of 9 floats to be used to extract the values from the matrix.
Robert Carra7827f72019-01-11 12:53:37 -08002394 * @hide
2395 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002396 @UnsupportedAppUsage
Jorim Jaggi21c39a72017-10-20 15:47:51 +02002397 public Transaction setMatrix(SurfaceControl sc, Matrix matrix, float[] float9) {
2398 matrix.getValues(float9);
2399 setMatrix(sc, float9[MSCALE_X], float9[MSKEW_Y],
2400 float9[MSKEW_X], float9[MSCALE_Y]);
2401 setPosition(sc, float9[MTRANS_X], float9[MTRANS_Y]);
2402 return this;
2403 }
2404
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002405 /**
2406 * Sets the color transform for the Surface.
chaviw619da692019-06-10 15:39:40 -07002407 *
2408 * @param sc SurfaceControl to set color transform of
2409 * @param matrix A float array with 9 values represents a 3x3 transform matrix
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002410 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08002411 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002412 */
2413 public Transaction setColorTransform(SurfaceControl sc, @Size(9) float[] matrix,
2414 @Size(3) float[] translation) {
Robert Carrfee0b822019-12-17 23:56:44 -08002415 checkPreconditions(sc);
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002416 nativeSetColorTransform(mNativeObject, sc.mNativeObject, matrix, translation);
2417 return this;
2418 }
2419
Robert Carra7827f72019-01-11 12:53:37 -08002420 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08002421 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
2422 * the color can be interpreted in any color space.
2423 * @param agnostic A boolean to indicate whether the surface is color space agnostic
2424 * @hide
2425 */
2426 public Transaction setColorSpaceAgnostic(SurfaceControl sc, boolean agnostic) {
Robert Carrfee0b822019-12-17 23:56:44 -08002427 checkPreconditions(sc);
Peiyong Linf4f0f642019-03-01 14:36:05 -08002428 nativeSetColorSpaceAgnostic(mNativeObject, sc.mNativeObject, agnostic);
2429 return this;
2430 }
2431
2432 /**
chaviw619da692019-06-10 15:39:40 -07002433 * Bounds the surface and its children to the bounds specified. Size of the surface will be
2434 * ignored and only the crop and buffer size will be used to determine the bounds of the
2435 * surface. If no crop is specified and the surface has no buffer, the surface bounds is
2436 * only constrained by the size of its parent bounds.
2437 *
2438 * @param sc SurfaceControl to set crop of.
2439 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08002440 * @hide
2441 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002442 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002443 public Transaction setWindowCrop(SurfaceControl sc, Rect crop) {
Robert Carrfee0b822019-12-17 23:56:44 -08002444 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002445 if (crop != null) {
2446 nativeSetWindowCrop(mNativeObject, sc.mNativeObject,
2447 crop.left, crop.top, crop.right, crop.bottom);
2448 } else {
2449 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, 0, 0);
2450 }
2451
2452 return this;
2453 }
2454
Robert Carra7827f72019-01-11 12:53:37 -08002455 /**
chaviw619da692019-06-10 15:39:40 -07002456 * Same as {@link Transaction#setWindowCrop(SurfaceControl, Rect)} but sets the crop rect
2457 * top left at 0, 0.
2458 *
2459 * @param sc SurfaceControl to set crop of.
2460 * @param width width of crop rect
2461 * @param height height of crop rect
Robert Carra7827f72019-01-11 12:53:37 -08002462 * @hide
2463 */
Vishnu Naird454442d2018-11-13 13:51:01 -08002464 public Transaction setWindowCrop(SurfaceControl sc, int width, int height) {
Robert Carrfee0b822019-12-17 23:56:44 -08002465 checkPreconditions(sc);
Vishnu Naird454442d2018-11-13 13:51:01 -08002466 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, width, height);
2467 return this;
2468 }
2469
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002470 /**
2471 * Sets the corner radius of a {@link SurfaceControl}.
2472 * @param sc SurfaceControl
2473 * @param cornerRadius Corner radius in pixels.
2474 * @return Itself.
Robert Carra7827f72019-01-11 12:53:37 -08002475 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002476 */
2477 @UnsupportedAppUsage
2478 public Transaction setCornerRadius(SurfaceControl sc, float cornerRadius) {
Robert Carrfee0b822019-12-17 23:56:44 -08002479 checkPreconditions(sc);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002480 nativeSetCornerRadius(mNativeObject, sc.mNativeObject, cornerRadius);
2481
2482 return this;
2483 }
2484
Robert Carra7827f72019-01-11 12:53:37 -08002485 /**
Lucas Dupin991415e2019-11-25 17:48:58 -08002486 * Sets the background blur radius of the {@link SurfaceControl}.
2487 *
2488 * @param sc SurfaceControl.
2489 * @param radius Blur radius in pixels.
2490 * @return itself.
2491 * @hide
2492 */
2493 public Transaction setBackgroundBlurRadius(SurfaceControl sc, int radius) {
2494 checkPreconditions(sc);
2495 nativeSetBackgroundBlurRadius(mNativeObject, sc.mNativeObject, radius);
2496 return this;
2497 }
2498
2499 /**
Robert Carra7827f72019-01-11 12:53:37 -08002500 * @hide
2501 */
Mathew Inwood679c15e2019-02-06 15:36:04 +00002502 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.O)
Robert Carre13b58e2017-08-31 14:50:44 -07002503 public Transaction setLayerStack(SurfaceControl sc, int layerStack) {
Robert Carrfee0b822019-12-17 23:56:44 -08002504 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002505 nativeSetLayerStack(mNativeObject, sc.mNativeObject, layerStack);
2506 return this;
2507 }
2508
Robert Carra7827f72019-01-11 12:53:37 -08002509 /**
2510 * @hide
2511 */
Robert Carr76907ee2019-01-11 13:38:19 -08002512 @UnsupportedAppUsage
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002513 public Transaction deferTransactionUntil(SurfaceControl sc, SurfaceControl barrier,
Jorim Jaggidfc27372017-10-27 17:47:49 +02002514 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002515 if (frameNumber < 0) {
2516 return this;
2517 }
Robert Carrfee0b822019-12-17 23:56:44 -08002518 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002519 nativeDeferTransactionUntil(mNativeObject, sc.mNativeObject, barrier.mNativeObject,
2520 frameNumber);
Robert Carre13b58e2017-08-31 14:50:44 -07002521 return this;
2522 }
2523
Robert Carra7827f72019-01-11 12:53:37 -08002524 /**
2525 * @hide
2526 */
Robert Carrcef9a282020-01-14 09:08:05 -08002527 @Deprecated
Robert Carr76907ee2019-01-11 13:38:19 -08002528 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002529 public Transaction deferTransactionUntilSurface(SurfaceControl sc, Surface barrierSurface,
2530 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002531 if (frameNumber < 0) {
2532 return this;
2533 }
Robert Carrfee0b822019-12-17 23:56:44 -08002534 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002535 nativeDeferTransactionUntilSurface(mNativeObject, sc.mNativeObject,
2536 barrierSurface.mNativeObject, frameNumber);
2537 return this;
2538 }
2539
Robert Carra7827f72019-01-11 12:53:37 -08002540 /**
2541 * @hide
2542 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002543 public Transaction reparentChildren(SurfaceControl sc, SurfaceControl newParent) {
Robert Carrfee0b822019-12-17 23:56:44 -08002544 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002545 nativeReparentChildren(mNativeObject, sc.mNativeObject, newParent.mNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -07002546 return this;
2547 }
2548
Robert Carr10584fa2019-01-14 15:55:19 -08002549 /**
2550 * Re-parents a given layer to a new parent. Children inherit transform (position, scaling)
2551 * crop, visibility, and Z-ordering from their parents, as if the children were pixels within the
2552 * parent Surface.
2553 *
2554 * @param sc The SurfaceControl to reparent
2555 * @param newParent The new parent for the given control.
2556 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002557 */
Robert Carr76907ee2019-01-11 13:38:19 -08002558 @NonNull
2559 public Transaction reparent(@NonNull SurfaceControl sc,
2560 @Nullable SurfaceControl newParent) {
Robert Carrfee0b822019-12-17 23:56:44 -08002561 checkPreconditions(sc);
Robert Carr10584fa2019-01-14 15:55:19 -08002562 long otherObject = 0;
2563 if (newParent != null) {
2564 newParent.checkNotReleased();
2565 otherObject = newParent.mNativeObject;
2566 }
2567 nativeReparent(mNativeObject, sc.mNativeObject, otherObject);
Robert Carre13b58e2017-08-31 14:50:44 -07002568 return this;
2569 }
2570
Robert Carra7827f72019-01-11 12:53:37 -08002571 /**
2572 * @hide
2573 */
Robert Carre13b58e2017-08-31 14:50:44 -07002574 public Transaction detachChildren(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002575 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002576 nativeSeverChildren(mNativeObject, sc.mNativeObject);
2577 return this;
2578 }
2579
Robert Carra7827f72019-01-11 12:53:37 -08002580 /**
2581 * @hide
2582 */
Robert Carre13b58e2017-08-31 14:50:44 -07002583 public Transaction setOverrideScalingMode(SurfaceControl sc, int overrideScalingMode) {
Robert Carrfee0b822019-12-17 23:56:44 -08002584 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002585 nativeSetOverrideScalingMode(mNativeObject, sc.mNativeObject,
2586 overrideScalingMode);
2587 return this;
2588 }
2589
2590 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002591 * Fills the surface with the specified color.
2592 * @param color A float array with three values to represent r, g, b in range [0..1]. An
2593 * invalid color will remove the color fill.
Robert Carra7827f72019-01-11 12:53:37 -08002594 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002595 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002596 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002597 public Transaction setColor(SurfaceControl sc, @Size(3) float[] color) {
Robert Carrfee0b822019-12-17 23:56:44 -08002598 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002599 nativeSetColor(mNativeObject, sc.mNativeObject, color);
2600 return this;
2601 }
2602
2603 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002604 * Removes color fill.
2605 * @hide
2606 */
2607 public Transaction unsetColor(SurfaceControl sc) {
2608 checkPreconditions(sc);
2609 nativeSetColor(mNativeObject, sc.mNativeObject, INVALID_COLOR);
2610 return this;
2611 }
2612
2613 /**
Robert Carre13b58e2017-08-31 14:50:44 -07002614 * Sets the security of the surface. Setting the flag is equivalent to creating the
2615 * Surface with the {@link #SECURE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002616 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002617 */
Robert Carrb1579c82017-09-05 14:54:47 -07002618 public Transaction setSecure(SurfaceControl sc, boolean isSecure) {
Robert Carrfee0b822019-12-17 23:56:44 -08002619 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002620 if (isSecure) {
2621 nativeSetFlags(mNativeObject, sc.mNativeObject, SECURE, SECURE);
2622 } else {
2623 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SECURE);
2624 }
2625 return this;
2626 }
2627
2628 /**
2629 * Sets the opacity of the surface. Setting the flag is equivalent to creating the
2630 * Surface with the {@link #OPAQUE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002631 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002632 */
2633 public Transaction setOpaque(SurfaceControl sc, boolean isOpaque) {
Robert Carrfee0b822019-12-17 23:56:44 -08002634 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002635 if (isOpaque) {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002636 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_OPAQUE, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002637 } else {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002638 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002639 }
2640 return this;
2641 }
2642
Robert Carra7827f72019-01-11 12:53:37 -08002643 /**
2644 * @hide
2645 */
Robert Carre13b58e2017-08-31 14:50:44 -07002646 public Transaction setDisplaySurface(IBinder displayToken, Surface surface) {
2647 if (displayToken == null) {
2648 throw new IllegalArgumentException("displayToken must not be null");
2649 }
2650
2651 if (surface != null) {
2652 synchronized (surface.mLock) {
2653 nativeSetDisplaySurface(mNativeObject, displayToken, surface.mNativeObject);
2654 }
2655 } else {
2656 nativeSetDisplaySurface(mNativeObject, displayToken, 0);
2657 }
2658 return this;
2659 }
2660
Robert Carra7827f72019-01-11 12:53:37 -08002661 /**
2662 * @hide
2663 */
Robert Carre13b58e2017-08-31 14:50:44 -07002664 public Transaction setDisplayLayerStack(IBinder displayToken, int layerStack) {
2665 if (displayToken == null) {
2666 throw new IllegalArgumentException("displayToken must not be null");
2667 }
2668 nativeSetDisplayLayerStack(mNativeObject, displayToken, layerStack);
2669 return this;
2670 }
2671
Robert Carra7827f72019-01-11 12:53:37 -08002672 /**
2673 * @hide
2674 */
Robert Carre13b58e2017-08-31 14:50:44 -07002675 public Transaction setDisplayProjection(IBinder displayToken,
2676 int orientation, Rect layerStackRect, Rect displayRect) {
2677 if (displayToken == null) {
2678 throw new IllegalArgumentException("displayToken must not be null");
2679 }
2680 if (layerStackRect == null) {
2681 throw new IllegalArgumentException("layerStackRect must not be null");
2682 }
2683 if (displayRect == null) {
2684 throw new IllegalArgumentException("displayRect must not be null");
2685 }
2686 nativeSetDisplayProjection(mNativeObject, displayToken, orientation,
2687 layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom,
2688 displayRect.left, displayRect.top, displayRect.right, displayRect.bottom);
2689 return this;
2690 }
2691
Robert Carra7827f72019-01-11 12:53:37 -08002692 /**
2693 * @hide
2694 */
Robert Carre13b58e2017-08-31 14:50:44 -07002695 public Transaction setDisplaySize(IBinder displayToken, int width, int height) {
2696 if (displayToken == null) {
2697 throw new IllegalArgumentException("displayToken must not be null");
2698 }
2699 if (width <= 0 || height <= 0) {
2700 throw new IllegalArgumentException("width and height must be positive");
2701 }
2702
2703 nativeSetDisplaySize(mNativeObject, displayToken, width, height);
2704 return this;
2705 }
2706
Vishnu Nair629df2b2019-06-11 16:03:38 -07002707 /** flag the transaction as an animation
Robert Carra7827f72019-01-11 12:53:37 -08002708 * @hide
2709 */
Robert Carre13b58e2017-08-31 14:50:44 -07002710 public Transaction setAnimationTransaction() {
2711 nativeSetAnimationTransaction(mNativeObject);
2712 return this;
2713 }
Robert Carrb1579c82017-09-05 14:54:47 -07002714
2715 /**
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002716 * Indicate that SurfaceFlinger should wake up earlier than usual as a result of this
2717 * transaction. This should be used when the caller thinks that the scene is complex enough
2718 * that it's likely to hit GL composition, and thus, SurfaceFlinger needs to more time in
2719 * order not to miss frame deadlines.
2720 * <p>
2721 * Corresponds to setting ISurfaceComposer::eEarlyWakeup
Robert Carra7827f72019-01-11 12:53:37 -08002722 * @hide
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002723 */
2724 public Transaction setEarlyWakeup() {
2725 nativeSetEarlyWakeup(mNativeObject);
2726 return this;
2727 }
2728
2729 /**
Evan Rosky485df202018-12-06 14:11:12 -08002730 * Sets an arbitrary piece of metadata on the surface. This is a helper for int data.
2731 * @hide
2732 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002733 public Transaction setMetadata(SurfaceControl sc, int key, int data) {
Evan Rosky485df202018-12-06 14:11:12 -08002734 Parcel parcel = Parcel.obtain();
2735 parcel.writeInt(data);
2736 try {
Evan Roskyb51e2462019-04-03 19:27:18 -07002737 setMetadata(sc, key, parcel);
Evan Rosky485df202018-12-06 14:11:12 -08002738 } finally {
2739 parcel.recycle();
2740 }
2741 return this;
2742 }
2743
2744 /**
2745 * Sets an arbitrary piece of metadata on the surface.
2746 * @hide
2747 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002748 public Transaction setMetadata(SurfaceControl sc, int key, Parcel data) {
Robert Carrfee0b822019-12-17 23:56:44 -08002749 checkPreconditions(sc);
Evan Roskyb51e2462019-04-03 19:27:18 -07002750 nativeSetMetadata(mNativeObject, sc.mNativeObject, key, data);
Evan Rosky485df202018-12-06 14:11:12 -08002751 return this;
2752 }
2753
Vishnu Naird87984d2019-11-06 14:43:22 -08002754 /**
2755 * Draws shadows of length {@code shadowRadius} around the surface {@link SurfaceControl}.
2756 * If the length is 0.0f then the shadows will not be drawn.
2757 *
2758 * Shadows are drawn around the screen bounds, these are the post transformed cropped
2759 * bounds. They can draw over their parent bounds and will be occluded by layers with a
2760 * higher z-order. The shadows will respect the surface's corner radius if the
2761 * rounded corner bounds (transformed source bounds) are within the screen bounds.
2762 *
2763 * A shadow will only be drawn on buffer and color layers. If the radius is applied on a
2764 * container layer, it will be passed down the hierarchy to be applied on buffer and color
2765 * layers but not its children. A scenario where this is useful is when SystemUI animates
2766 * a task by controlling a leash to it, can draw a shadow around the app surface by
2767 * setting a shadow on the leash. This is similar to how rounded corners are set.
2768 *
2769 * @hide
2770 */
2771 public Transaction setShadowRadius(SurfaceControl sc, float shadowRadius) {
Robert Carrfee0b822019-12-17 23:56:44 -08002772 checkPreconditions(sc);
Vishnu Naird87984d2019-11-06 14:43:22 -08002773 nativeSetShadowRadius(mNativeObject, sc.mNativeObject, shadowRadius);
2774 return this;
2775 }
2776
Evan Rosky485df202018-12-06 14:11:12 -08002777 /**
Steven Thomas6cf051e2020-01-14 11:37:21 -08002778 * Sets the intended frame rate for the surface {@link SurfaceControl}.
Steven Thomas4528b332020-03-26 21:27:54 -07002779 * <p>
Steven Thomas6cf051e2020-01-14 11:37:21 -08002780 * On devices that are capable of running the display at different refresh rates, the system
2781 * may choose a display refresh rate to better match this surface's frame rate. Usage of
2782 * this API won't directly affect the application's frame production pipeline. However,
2783 * because the system may change the display refresh rate, calls to this function may result
2784 * in changes to Choreographer callback timings, and changes to the time interval at which
2785 * the system releases buffers back to the application.
2786 *
2787 * @param sc The SurfaceControl to specify the frame rate of.
Steven Thomasadd6be12020-01-23 16:35:32 -08002788 * @param frameRate The intended frame rate for this surface, in frames per second. 0 is a
2789 * special value that indicates the app will accept the system's choice for
2790 * the display frame rate, which is the default behavior if this function
Steven Thomas4528b332020-03-26 21:27:54 -07002791 * isn't called. The frameRate param does <em>not</em> need to be a valid
2792 * refresh rate for this device's display - e.g., it's fine to pass 30fps
2793 * to a device that can only run the display at 60fps.
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08002794 * @param compatibility The frame rate compatibility of this surface. The compatibility
2795 * value may influence the system's choice of display frame rate. See
2796 * the Surface.FRAME_RATE_COMPATIBILITY_* values for more info.
Steven Thomas6cf051e2020-01-14 11:37:21 -08002797 * @return This transaction object.
2798 */
2799 @NonNull
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08002800 public Transaction setFrameRate(@NonNull SurfaceControl sc,
2801 @FloatRange(from = 0.0) float frameRate,
2802 @Surface.FrameRateCompatibility int compatibility) {
Steven Thomas6cf051e2020-01-14 11:37:21 -08002803 checkPreconditions(sc);
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08002804 nativeSetFrameRate(mNativeObject, sc.mNativeObject, frameRate, compatibility);
Steven Thomas6cf051e2020-01-14 11:37:21 -08002805 return this;
2806 }
2807
2808 /**
Robert Carrb1579c82017-09-05 14:54:47 -07002809 * Merge the other transaction into this transaction, clearing the
2810 * other transaction as if it had been applied.
Robert Carr76907ee2019-01-11 13:38:19 -08002811 *
2812 * @param other The transaction to merge in to this one.
2813 * @return This transaction.
Robert Carrb1579c82017-09-05 14:54:47 -07002814 */
Robert Carr76907ee2019-01-11 13:38:19 -08002815 @NonNull
2816 public Transaction merge(@NonNull Transaction other) {
Tiger Huanged6794e2019-05-07 20:07:59 +08002817 if (this == other) {
2818 return this;
2819 }
Jorim Jaggia5e10572017-11-15 14:36:26 +01002820 mResizedSurfaces.putAll(other.mResizedSurfaces);
2821 other.mResizedSurfaces.clear();
Robert Carrb1579c82017-09-05 14:54:47 -07002822 nativeMergeTransaction(mNativeObject, other.mNativeObject);
2823 return this;
2824 }
Robert Carr71200f22019-02-05 09:44:53 -08002825
2826 /**
2827 * Equivalent to reparent with a null parent, in that it removes
2828 * the SurfaceControl from the scene, but it also releases
2829 * the local resources (by calling {@link SurfaceControl#release})
2830 * after this method returns, {@link SurfaceControl#isValid} will return
2831 * false for the argument.
2832 *
2833 * @param sc The surface to remove and release.
2834 * @return This transaction
2835 * @hide
2836 */
2837 @NonNull
2838 public Transaction remove(@NonNull SurfaceControl sc) {
2839 reparent(sc, null);
2840 sc.release();
2841 return this;
2842 }
Vishnu Nair629df2b2019-06-11 16:03:38 -07002843
Vishnu Nairf7645aa2019-06-18 11:14:01 -07002844 /**
2845 * Writes the transaction to parcel, clearing the transaction as if it had been applied so
2846 * it can be used to store future transactions. It's the responsibility of the parcel
2847 * reader to apply the original transaction.
2848 *
2849 * @param dest parcel to write the transaction to
2850 * @param flags
2851 */
Vishnu Nair629df2b2019-06-11 16:03:38 -07002852 @Override
2853 public void writeToParcel(@NonNull Parcel dest, @WriteFlags int flags) {
2854 if (mNativeObject == 0) {
2855 dest.writeInt(0);
2856 } else {
2857 dest.writeInt(1);
2858 }
2859 nativeWriteTransactionToParcel(mNativeObject, dest);
2860 }
2861
2862 private void readFromParcel(Parcel in) {
2863 mNativeObject = 0;
2864 if (in.readInt() != 0) {
2865 mNativeObject = nativeReadTransactionFromParcel(in);
2866 mFreeNativeResources = sRegistry.registerNativeAllocation(this, mNativeObject);
2867 }
2868 }
2869
2870 @Override
2871 public int describeContents() {
2872 return 0;
2873 }
2874
2875 public static final @NonNull Creator<Transaction> CREATOR = new Creator<Transaction>() {
2876 @Override
2877 public Transaction createFromParcel(Parcel in) {
2878 return new Transaction(in);
2879 }
2880 @Override
2881 public Transaction[] newArray(int size) {
2882 return new Transaction[size];
2883 }
2884 };
Robert Carre13b58e2017-08-31 14:50:44 -07002885 }
Robert Carrfee0b822019-12-17 23:56:44 -08002886
2887 /**
2888 * A debugging utility subclass of SurfaceControl.Transaction. At construction
2889 * you can pass in a monitor object, and all the other methods will throw an exception
2890 * if the monitor is not held when they are called.
2891 * @hide
2892 */
2893 public static class LockDebuggingTransaction extends SurfaceControl.Transaction {
2894 Object mMonitor;
2895
2896 public LockDebuggingTransaction(Object o) {
2897 mMonitor = o;
2898 }
2899
2900 @Override
2901 protected void checkPreconditions(SurfaceControl sc) {
2902 super.checkPreconditions(sc);
2903 if (!Thread.holdsLock(mMonitor)) {
2904 throw new RuntimeException(
2905 "Unlocked access to synchronized SurfaceControl.Transaction");
2906 }
2907 }
2908 }
Steven Thomas6ec6fbc2020-03-24 16:06:33 -07002909
2910 /**
2911 * Acquire a frame rate flexibility token, which allows surface flinger to freely switch display
2912 * frame rates. This is used by CTS tests to put the device in a consistent state. See
Steven Thomas6faeaa22020-04-07 21:26:38 -07002913 * ISurfaceComposer::acquireFrameRateFlexibilityToken(). The caller must have the
2914 * ACCESS_SURFACE_FLINGER permission, or else the call will fail, returning 0.
Steven Thomas6ec6fbc2020-03-24 16:06:33 -07002915 * @hide
2916 */
2917 @TestApi
2918 public static long acquireFrameRateFlexibilityToken() {
2919 return nativeAcquireFrameRateFlexibilityToken();
2920 }
2921
2922 /**
2923 * Release a frame rate flexibility token.
2924 * @hide
2925 */
2926 @TestApi
2927 public static void releaseFrameRateFlexibilityToken(long token) {
2928 nativeReleaseFrameRateFlexibilityToken(token);
2929 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08002930}