blob: 3b3836582b16587f9e324808288fd13e8c8444de [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 Jaggibfa95a72020-06-18 22:51:49 +020052import android.os.Trace;
Jorim Jaggia5e10572017-11-15 14:36:26 +010053import android.util.ArrayMap;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080054import android.util.Log;
Evan Rosky485df202018-12-06 14:11:12 -080055import android.util.SparseIntArray;
Vishnu Nair04ab4392018-01-10 11:00:06 -080056import android.util.proto.ProtoOutputStream;
Igor Murashkina86ab6402013-08-30 12:58:36 -070057import android.view.Surface.OutOfResourcesException;
Jorim Jaggia5e10572017-11-15 14:36:26 +010058
59import com.android.internal.annotations.GuardedBy;
60
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070061import dalvik.system.CloseGuard;
Vishnu Nair04ab4392018-01-10 11:00:06 -080062
Robert Carre625fcf2017-09-01 12:36:28 -070063import libcore.util.NativeAllocationRegistry;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070064
Robert Carre13b58e2017-08-31 14:50:44 -070065import java.io.Closeable;
Evan Rosky485df202018-12-06 14:11:12 -080066import java.nio.ByteBuffer;
Evan Roskyc12c9a32019-02-06 13:38:03 -080067import java.nio.ByteOrder;
Dan Gittik832b4972019-02-13 18:17:47 +000068import java.util.Objects;
Robert Carre13b58e2017-08-31 14:50:44 -070069
Mathias Agopian3866f0d2013-02-11 22:08:48 -080070/**
Robert Carr76907ee2019-01-11 13:38:19 -080071 * Handle to an on-screen Surface managed by the system compositor. The SurfaceControl is
72 * a combination of a buffer source, and metadata about how to display the buffers.
73 * By constructing a {@link Surface} from this SurfaceControl you can submit buffers to be
74 * composited. Using {@link SurfaceControl.Transaction} you can manipulate various
75 * properties of how the buffer will be displayed on-screen. SurfaceControl's are
76 * arranged into a scene-graph like hierarchy, and as such any SurfaceControl may have
77 * a parent. Geometric properties like transform, crop, and Z-ordering will be inherited
78 * from the parent, as if the child were content in the parents buffer stream.
Mathias Agopian3866f0d2013-02-11 22:08:48 -080079 */
Robert Carr76907ee2019-01-11 13:38:19 -080080public final class SurfaceControl implements Parcelable {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080081 private static final String TAG = "SurfaceControl";
Mathias Agopian29479eb2013-02-14 14:36:04 -080082
Ashok Bhat36bef0b2014-01-20 20:08:01 +000083 private static native long nativeCreate(SurfaceSession session, String name,
Evan Rosky485df202018-12-06 14:11:12 -080084 int w, int h, int format, int flags, long parentObject, Parcel metadata)
Mathias Agopian29479eb2013-02-14 14:36:04 -080085 throws OutOfResourcesException;
Jorim Jaggi06975df2017-12-01 14:52:13 +010086 private static native long nativeReadFromParcel(Parcel in);
chaviwbeb7a0c2018-12-05 13:49:54 -080087 private static native long nativeCopyFromSurfaceControl(long nativeObject);
Jorim Jaggi06975df2017-12-01 14:52:13 +010088 private static native void nativeWriteToParcel(long nativeObject, Parcel out);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000089 private static native void nativeRelease(long nativeObject);
Chong Zhang47e36a32016-02-29 16:44:33 -080090 private static native void nativeDisconnect(long nativeObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080091
Peiyong Line3e5efd2019-03-21 20:59:47 +000092 private static native ScreenshotGraphicBuffer nativeScreenshot(IBinder displayToken,
Robert Carr5c52b132019-02-15 15:48:11 -080093 Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation,
94 boolean captureSecureLayers);
Peiyong Lin21e499a2019-04-03 16:37:46 -070095 private static native ScreenshotGraphicBuffer nativeCaptureLayers(IBinder displayToken,
Chiawei Wang02202d12019-01-03 18:12:13 +080096 long layerObject, Rect sourceCrop, float frameScale, long[] excludeLayerObjects,
97 int format);
chaviwa51724f2019-09-19 09:50:11 -070098 private static native long nativeMirrorSurface(long mirrorOfObject);
Robert Carre13b58e2017-08-31 14:50:44 -070099 private static native long nativeCreateTransaction();
100 private static native long nativeGetNativeTransactionFinalizer();
101 private static native void nativeApplyTransaction(long transactionObj, boolean sync);
Robert Carrb1579c82017-09-05 14:54:47 -0700102 private static native void nativeMergeTransaction(long transactionObj,
103 long otherTransactionObj);
Robert Carre13b58e2017-08-31 14:50:44 -0700104 private static native void nativeSetAnimationTransaction(long transactionObj);
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100105 private static native void nativeSetEarlyWakeup(long transactionObj);
Vishnu Nair2ed39d82020-06-17 15:43:13 -0700106 private static native void nativeSetEarlyWakeupStart(long transactionObj);
107 private static native void nativeSetEarlyWakeupEnd(long transactionObj);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800108
Robert Carre13b58e2017-08-31 14:50:44 -0700109 private static native void nativeSetLayer(long transactionObj, long nativeObject, int zorder);
110 private static native void nativeSetRelativeLayer(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700111 long relativeToObject, int zorder);
Robert Carre13b58e2017-08-31 14:50:44 -0700112 private static native void nativeSetPosition(long transactionObj, long nativeObject,
113 float x, float y);
Robert Carre13b58e2017-08-31 14:50:44 -0700114 private static native void nativeSetSize(long transactionObj, long nativeObject, int w, int h);
115 private static native void nativeSetTransparentRegionHint(long transactionObj,
116 long nativeObject, Region region);
117 private static native void nativeSetAlpha(long transactionObj, long nativeObject, float alpha);
118 private static native void nativeSetMatrix(long transactionObj, long nativeObject,
119 float dsdx, float dtdx,
Andrii Kulian283acd22017-08-03 04:03:51 -0700120 float dtdy, float dsdy);
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700121 private static native void nativeSetColorTransform(long transactionObj, long nativeObject,
122 float[] matrix, float[] translation);
Peiyong Linf4f0f642019-03-01 14:36:05 -0800123 private static native void nativeSetColorSpaceAgnostic(long transactionObj, long nativeObject,
124 boolean agnostic);
Robert Carr76907ee2019-01-11 13:38:19 -0800125 private static native void nativeSetGeometry(long transactionObj, long nativeObject,
126 Rect sourceCrop, Rect dest, long orientation);
Robert Carre13b58e2017-08-31 14:50:44 -0700127 private static native void nativeSetColor(long transactionObj, long nativeObject, float[] color);
128 private static native void nativeSetFlags(long transactionObj, long nativeObject,
129 int flags, int mask);
Ana Krulecfea97172019-11-02 23:11:02 +0100130 private static native void nativeSetFrameRateSelectionPriority(long transactionObj,
131 long nativeObject, int priority);
Robert Carre13b58e2017-08-31 14:50:44 -0700132 private static native void nativeSetWindowCrop(long transactionObj, long nativeObject,
133 int l, int t, int r, int b);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -0700134 private static native void nativeSetCornerRadius(long transactionObj, long nativeObject,
135 float cornerRadius);
Lucas Dupin991415e2019-11-25 17:48:58 -0800136 private static native void nativeSetBackgroundBlurRadius(long transactionObj, long nativeObject,
137 int blurRadius);
Robert Carre13b58e2017-08-31 14:50:44 -0700138 private static native void nativeSetLayerStack(long transactionObj, long nativeObject,
139 int layerStack);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800140
Svetoslav1376d602014-03-13 11:17:26 -0700141 private static native boolean nativeClearContentFrameStats(long nativeObject);
142 private static native boolean nativeGetContentFrameStats(long nativeObject, WindowContentFrameStats outStats);
143 private static native boolean nativeClearAnimationFrameStats();
144 private static native boolean nativeGetAnimationFrameStats(WindowAnimationFrameStats outStats);
145
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800146 private static native long[] nativeGetPhysicalDisplayIds();
147 private static native IBinder nativeGetPhysicalDisplayToken(long physicalDisplayId);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800148 private static native IBinder nativeCreateDisplay(String name, boolean secure);
Jesse Hall6a6bc212013-08-08 12:15:03 -0700149 private static native void nativeDestroyDisplay(IBinder displayToken);
Robert Carre13b58e2017-08-31 14:50:44 -0700150 private static native void nativeSetDisplaySurface(long transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000151 IBinder displayToken, long nativeSurfaceObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700152 private static native void nativeSetDisplayLayerStack(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800153 IBinder displayToken, int layerStack);
Robert Carre13b58e2017-08-31 14:50:44 -0700154 private static native void nativeSetDisplayProjection(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800155 IBinder displayToken, int orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -0700156 int l, int t, int r, int b,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800157 int L, int T, int R, int B);
Robert Carre13b58e2017-08-31 14:50:44 -0700158 private static native void nativeSetDisplaySize(long transactionObj, IBinder displayToken,
159 int width, int height);
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800160 private static native SurfaceControl.DisplayInfo nativeGetDisplayInfo(IBinder displayToken);
161 private static native SurfaceControl.DisplayConfig[] nativeGetDisplayConfigs(
Dan Stoza00101052014-05-02 15:23:40 -0700162 IBinder displayToken);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -0700163 private static native DisplayedContentSamplingAttributes
164 nativeGetDisplayedContentSamplingAttributes(IBinder displayToken);
165 private static native boolean nativeSetDisplayedContentSamplingEnabled(IBinder displayToken,
166 boolean enable, int componentMask, int maxFrames);
167 private static native DisplayedContentSample nativeGetDisplayedContentSample(
168 IBinder displayToken, long numFrames, long timestamp);
Dan Stoza00101052014-05-02 15:23:40 -0700169 private static native int nativeGetActiveConfig(IBinder displayToken);
Ana Krulec4f753aa2019-11-14 00:49:39 +0100170 private static native boolean nativeSetDesiredDisplayConfigSpecs(IBinder displayToken,
Ana Krulec52f12892019-11-18 03:57:20 -0800171 SurfaceControl.DesiredDisplayConfigSpecs desiredDisplayConfigSpecs);
Ana Kruleca74a8642019-11-14 00:51:00 +0100172 private static native SurfaceControl.DesiredDisplayConfigSpecs
173 nativeGetDesiredDisplayConfigSpecs(IBinder displayToken);
Michael Wright1c9977b2016-07-12 13:30:10 -0700174 private static native int[] nativeGetDisplayColorModes(IBinder displayToken);
Daniel Solomon10e3b332019-01-20 21:09:11 -0800175 private static native SurfaceControl.DisplayPrimaries nativeGetDisplayNativePrimaries(
176 IBinder displayToken);
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800177 private static native int[] nativeGetCompositionDataspaces();
Michael Wright1c9977b2016-07-12 13:30:10 -0700178 private static native int nativeGetActiveColorMode(IBinder displayToken);
179 private static native boolean nativeSetActiveColorMode(IBinder displayToken,
180 int colorMode);
Galia Peycheva056b3ee2019-06-26 14:05:12 +0200181 private static native void nativeSetAutoLowLatencyMode(IBinder displayToken, boolean on);
182 private static native void nativeSetGameContentType(IBinder displayToken, boolean on);
Prashant Malanic55929a2014-05-25 01:59:21 -0700183 private static native void nativeSetDisplayPowerMode(
184 IBinder displayToken, int mode);
Robert Carre13b58e2017-08-31 14:50:44 -0700185 private static native void nativeDeferTransactionUntil(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700186 long barrierObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700187 private static native void nativeDeferTransactionUntilSurface(long transactionObj,
188 long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800189 long surfaceObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700190 private static native void nativeReparentChildren(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700191 long newParentObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700192 private static native void nativeReparent(long transactionObj, long nativeObject,
Robert Carr10584fa2019-01-14 15:55:19 -0800193 long newParentNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700194 private static native void nativeSeverChildren(long transactionObj, long nativeObject);
195 private static native void nativeSetOverrideScalingMode(long transactionObj, long nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700196 int scalingMode);
Robert Carr6da3cc02016-06-16 15:17:07 -0700197
Michael Wright9ff94c02016-03-30 18:05:40 -0700198 private static native Display.HdrCapabilities nativeGetHdrCapabilities(IBinder displayToken);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800199
Galia Peycheva056b3ee2019-06-26 14:05:12 +0200200 private static native boolean nativeGetAutoLowLatencyModeSupport(IBinder displayToken);
201 private static native boolean nativeGetGameContentTypeSupport(IBinder displayToken);
202
Robert Carr788f5742018-07-30 17:46:45 -0700203 private static native void nativeSetInputWindowInfo(long transactionObj, long nativeObject,
204 InputWindowHandle handle);
Arthur Hungc499ad32019-09-05 15:51:14 +0800205
Peiyong Lin0ddb7d42019-01-16 13:25:09 -0800206 private static native boolean nativeGetProtectedContentSupport();
Evan Roskyb51e2462019-04-03 19:27:18 -0700207 private static native void nativeSetMetadata(long transactionObj, long nativeObject, int key,
208 Parcel data);
chaviw319cd0782019-02-14 11:00:23 -0800209 private static native void nativeSyncInputWindows(long transactionObj);
Dan Gittik832b4972019-02-13 18:17:47 +0000210 private static native boolean nativeGetDisplayBrightnessSupport(IBinder displayToken);
211 private static native boolean nativeSetDisplayBrightness(IBinder displayToken,
212 float brightness);
Vishnu Nair629df2b2019-06-11 16:03:38 -0700213 private static native long nativeReadTransactionFromParcel(Parcel in);
214 private static native void nativeWriteTransactionToParcel(long nativeObject, Parcel out);
Vishnu Naird87984d2019-11-06 14:43:22 -0800215 private static native void nativeSetShadowRadius(long transactionObj, long nativeObject,
216 float shadowRadius);
Vishnu Nair4a067c52019-11-19 14:25:56 -0800217 private static native void nativeSetGlobalShadowSettings(@Size(4) float[] ambientColor,
218 @Size(4) float[] spotColor, float lightPosY, float lightPosZ, float lightRadius);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800219
Steven Thomas6cf051e2020-01-14 11:37:21 -0800220 private static native void nativeSetFrameRate(
Steven Thomasdd7bf2f2020-01-31 18:50:02 -0800221 long transactionObj, long nativeObject, float frameRate, int compatibility);
Jorim Jaggiee540702020-04-02 21:40:52 +0200222 private static native long nativeGetHandle(long nativeObject);
Steven Thomas6cf051e2020-01-14 11:37:21 -0800223
Steven Thomas6ec6fbc2020-03-24 16:06:33 -0700224 private static native long nativeAcquireFrameRateFlexibilityToken();
225 private static native void nativeReleaseFrameRateFlexibilityToken(long token);
Vishnu Naireb53e522020-05-08 18:03:12 -0700226 private static native void nativeSetFixedTransformHint(long transactionObj, long nativeObject,
227 int transformHint);
Steven Thomas6ec6fbc2020-03-24 16:06:33 -0700228
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800229 private final CloseGuard mCloseGuard = CloseGuard.get();
chaviwbeb7a0c2018-12-05 13:49:54 -0800230 private String mName;
Robert Carr48ec4e02019-07-16 14:28:47 -0700231 /**
232 * @hide
233 */
234 public long mNativeObject;
Jorim Jaggiee540702020-04-02 21:40:52 +0200235 private long mNativeHandle;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800236
Jorim Jaggia5e10572017-11-15 14:36:26 +0100237 // TODO: Move this to native.
238 private final Object mSizeLock = new Object();
239 @GuardedBy("mSizeLock")
240 private int mWidth;
241 @GuardedBy("mSizeLock")
242 private int mHeight;
243
Robert Carre13b58e2017-08-31 14:50:44 -0700244 static Transaction sGlobalTransaction;
245 static long sTransactionNestCount = 0;
246
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800247 /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */
248
249 /**
250 * Surface creation flag: Surface is created hidden
Robert Carra7827f72019-01-11 12:53:37 -0800251 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800252 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100253 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800254 public static final int HIDDEN = 0x00000004;
255
256 /**
257 * Surface creation flag: The surface contains secure content, special
258 * measures will be taken to disallow the surface's content to be copied
259 * from another process. In particular, screenshots and VNC servers will
260 * be disabled, but other measures can take place, for instance the
Jesse Hall6a6bc212013-08-08 12:15:03 -0700261 * surface might not be hardware accelerated.
Robert Carra7827f72019-01-11 12:53:37 -0800262 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800263 */
264 public static final int SECURE = 0x00000080;
265
266 /**
267 * Surface creation flag: Creates a surface where color components are interpreted
268 * as "non pre-multiplied" by their alpha channel. Of course this flag is
269 * meaningless for surfaces without an alpha channel. By default
270 * surfaces are pre-multiplied, which means that each color component is
271 * already multiplied by its alpha value. In this case the blending
272 * equation used is:
Andy McFadden314405b2014-01-29 17:18:05 -0800273 * <p>
274 * <code>DEST = SRC + DEST * (1-SRC_ALPHA)</code>
275 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800276 * By contrast, non pre-multiplied surfaces use the following equation:
Andy McFadden314405b2014-01-29 17:18:05 -0800277 * <p>
278 * <code>DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)</code>
279 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800280 * pre-multiplied surfaces must always be used if transparent pixels are
281 * composited on top of each-other into the surface. A pre-multiplied
282 * surface can never lower the value of the alpha component of a given
283 * pixel.
Andy McFadden314405b2014-01-29 17:18:05 -0800284 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800285 * In some rare situations, a non pre-multiplied surface is preferable.
Robert Carra7827f72019-01-11 12:53:37 -0800286 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800287 */
288 public static final int NON_PREMULTIPLIED = 0x00000100;
289
290 /**
291 * Surface creation flag: Indicates that the surface must be considered opaque,
Robert Carre625fcf2017-09-01 12:36:28 -0700292 * even if its pixel format contains an alpha channel. This can be useful if an
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800293 * application needs full RGBA 8888 support for instance but will
294 * still draw every pixel opaque.
Andy McFadden314405b2014-01-29 17:18:05 -0800295 * <p>
296 * This flag is ignored if setAlpha() is used to make the surface non-opaque.
297 * Combined effects are (assuming a buffer format with an alpha channel):
298 * <ul>
299 * <li>OPAQUE + alpha(1.0) == opaque composition
300 * <li>OPAQUE + alpha(0.x) == blended composition
301 * <li>!OPAQUE + alpha(1.0) == blended composition
302 * <li>!OPAQUE + alpha(0.x) == blended composition
303 * </ul>
304 * If the underlying buffer lacks an alpha channel, the OPAQUE flag is effectively
305 * set automatically.
Robert Carra7827f72019-01-11 12:53:37 -0800306 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800307 */
308 public static final int OPAQUE = 0x00000400;
309
310 /**
311 * Surface creation flag: Application requires a hardware-protected path to an
312 * external display sink. If a hardware-protected path is not available,
313 * then this surface will not be displayed on the external sink.
314 *
Robert Carra7827f72019-01-11 12:53:37 -0800315 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800316 */
317 public static final int PROTECTED_APP = 0x00000800;
318
319 // 0x1000 is reserved for an independent DRM protected flag in framework
320
321 /**
Riley Andrews68eccda2014-07-07 11:47:35 -0700322 * Surface creation flag: Window represents a cursor glyph.
Robert Carra7827f72019-01-11 12:53:37 -0800323 * @hide
Riley Andrews68eccda2014-07-07 11:47:35 -0700324 */
325 public static final int CURSOR_WINDOW = 0x00002000;
326
327 /**
Vishnu Nair06cff302020-05-29 14:39:20 -0700328 * Surface creation flag: Indicates the effect layer will not have a color fill on
329 * creation.
330 *
331 * @hide
332 */
333 public static final int NO_COLOR_FILL = 0x00004000;
334
335 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800336 * Surface creation flag: Creates a normal surface.
337 * This is the default.
338 *
Robert Carra7827f72019-01-11 12:53:37 -0800339 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800340 */
341 public static final int FX_SURFACE_NORMAL = 0x00000000;
342
343 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800344 * Surface creation flag: Creates a effect surface which
345 * represents a solid color and or shadows.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800346 *
Robert Carra7827f72019-01-11 12:53:37 -0800347 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800348 */
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800349 public static final int FX_SURFACE_EFFECT = 0x00020000;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800350
351 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700352 * Surface creation flag: Creates a container surface.
353 * This surface will have no buffers and will only be used
354 * as a container for other surfaces, or for its InputInfo.
Robert Carra7827f72019-01-11 12:53:37 -0800355 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700356 */
357 public static final int FX_SURFACE_CONTAINER = 0x00080000;
358
359 /**
Robert Carr48ec4e02019-07-16 14:28:47 -0700360 * @hide
361 */
362 public static final int FX_SURFACE_BLAST = 0x00040000;
363
364 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800365 * Mask used for FX values above.
366 *
Robert Carra7827f72019-01-11 12:53:37 -0800367 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800368 */
369 public static final int FX_SURFACE_MASK = 0x000F0000;
370
371 /* flags used with setFlags() (keep in sync with ISurfaceComposer.h) */
372
373 /**
374 * Surface flag: Hide the surface.
375 * Equivalent to calling hide().
Andy McFadden314405b2014-01-29 17:18:05 -0800376 * Updates the value set during Surface creation (see {@link #HIDDEN}).
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800377 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800378 private static final int SURFACE_HIDDEN = 0x01;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800379
Andy McFadden314405b2014-01-29 17:18:05 -0800380 /**
381 * Surface flag: composite without blending when possible.
382 * Updates the value set during Surface creation (see {@link #OPAQUE}).
383 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800384 private static final int SURFACE_OPAQUE = 0x02;
Andy McFadden314405b2014-01-29 17:18:05 -0800385
Robert Carr76907ee2019-01-11 13:38:19 -0800386 // Display power modes.
Prashant Malanic55929a2014-05-25 01:59:21 -0700387 /**
388 * Display power mode off: used while blanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700389 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800390 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700391 */
392 public static final int POWER_MODE_OFF = 0;
393
394 /**
395 * Display power mode doze: used while putting the screen into low power mode.
Jeff Brown5dc21912014-07-17 18:50:18 -0700396 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800397 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700398 */
399 public static final int POWER_MODE_DOZE = 1;
400
401 /**
402 * Display power mode normal: used while unblanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700403 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800404 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700405 */
406 public static final int POWER_MODE_NORMAL = 2;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800407
Jeff Brown5dc21912014-07-17 18:50:18 -0700408 /**
409 * Display power mode doze: used while putting the screen into a suspended
410 * low power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800411 * @hide
Jeff Brown5dc21912014-07-17 18:50:18 -0700412 */
413 public static final int POWER_MODE_DOZE_SUSPEND = 3;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800414
415 /**
Chris Phoenix10a4a642017-09-25 13:21:00 -0700416 * Display power mode on: used while putting the screen into a suspended
417 * full power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800418 * @hide
Chris Phoenix10a4a642017-09-25 13:21:00 -0700419 */
420 public static final int POWER_MODE_ON_SUSPEND = 4;
421
422 /**
Robert Carr132c9f52017-07-31 17:02:30 -0700423 * A value for windowType used to indicate that the window should be omitted from screenshots
424 * and display mirroring. A temporary workaround until we express such things with
425 * the hierarchy.
426 * TODO: b/64227542
427 * @hide
428 */
429 public static final int WINDOW_TYPE_DONT_SCREENSHOT = 441731;
430
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800431 /**
432 * internal representation of how to interpret pixel value, used only to convert to ColorSpace.
433 */
434 private static final int INTERNAL_DATASPACE_SRGB = 142671872;
435 private static final int INTERNAL_DATASPACE_DISPLAY_P3 = 143261696;
436 private static final int INTERNAL_DATASPACE_SCRGB = 411107328;
437
Robert Carreb344c72019-01-07 18:35:30 -0800438 private void assignNativeObject(long nativeObject) {
439 if (mNativeObject != 0) {
440 release();
441 }
Tiger Huang0fd67482020-04-15 22:16:13 +0800442 if (nativeObject != 0) {
Jorim Jaggibfa95a72020-06-18 22:51:49 +0200443 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "closeGuard");
Robert Carr01b0dd52020-02-27 13:25:17 -0800444 mCloseGuard.open("release");
Jorim Jaggibfa95a72020-06-18 22:51:49 +0200445 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Robert Carr01b0dd52020-02-27 13:25:17 -0800446 }
Robert Carreb344c72019-01-07 18:35:30 -0800447 mNativeObject = nativeObject;
Jorim Jaggiee540702020-04-02 21:40:52 +0200448 mNativeHandle = mNativeObject != 0 ? nativeGetHandle(nativeObject) : 0;
Robert Carreb344c72019-01-07 18:35:30 -0800449 }
450
Robert Carra7827f72019-01-11 12:53:37 -0800451 /**
452 * @hide
453 */
Jorim Jaggiee540702020-04-02 21:40:52 +0200454 public void copyFrom(@NonNull SurfaceControl other) {
chaviwbeb7a0c2018-12-05 13:49:54 -0800455 mName = other.mName;
456 mWidth = other.mWidth;
457 mHeight = other.mHeight;
Robert Carreb344c72019-01-07 18:35:30 -0800458 assignNativeObject(nativeCopyFromSurfaceControl(other.mNativeObject));
chaviwbeb7a0c2018-12-05 13:49:54 -0800459 }
460
Robert Carr132c9f52017-07-31 17:02:30 -0700461 /**
Evan Rosky485df202018-12-06 14:11:12 -0800462 * owner UID.
463 * @hide
464 */
465 public static final int METADATA_OWNER_UID = 1;
466
467 /**
468 * Window type as per {@link WindowManager.LayoutParams}.
469 * @hide
470 */
471 public static final int METADATA_WINDOW_TYPE = 2;
472
473 /**
Evan Rosky9020c072018-12-06 14:11:12 -0800474 * Task id to allow association between surfaces and task.
475 * @hide
476 */
477 public static final int METADATA_TASK_ID = 3;
478
479 /**
Daichi Hirono638ae6d2019-12-18 13:59:58 +0900480 * The style of mouse cursor and hotspot.
481 * @hide
482 */
483 public static final int METADATA_MOUSE_CURSOR = 4;
484
485 /**
Daichi Hirono1d56ce32019-11-01 14:15:10 +0900486 * Accessibility ID to allow association between surfaces and accessibility tree.
487 * @hide
488 */
Daichi Hirono638ae6d2019-12-18 13:59:58 +0900489 public static final int METADATA_ACCESSIBILITY_ID = 5;
Daichi Hirono1d56ce32019-11-01 14:15:10 +0900490
491 /**
Peiyong Line3e5efd2019-03-21 20:59:47 +0000492 * A wrapper around GraphicBuffer that contains extra information about how to
493 * interpret the screenshot GraphicBuffer.
494 * @hide
495 */
496 public static class ScreenshotGraphicBuffer {
497 private final GraphicBuffer mGraphicBuffer;
498 private final ColorSpace mColorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700499 private final boolean mContainsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000500
Robert Carr66b5664f2019-04-02 14:18:56 -0700501 public ScreenshotGraphicBuffer(GraphicBuffer graphicBuffer, ColorSpace colorSpace,
502 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000503 mGraphicBuffer = graphicBuffer;
504 mColorSpace = colorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700505 mContainsSecureLayers = containsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000506 }
507
508 /**
509 * Create ScreenshotGraphicBuffer from existing native GraphicBuffer object.
510 * @param width The width in pixels of the buffer
511 * @param height The height in pixels of the buffer
512 * @param format The format of each pixel as specified in {@link PixelFormat}
513 * @param usage Hint indicating how the buffer will be used
514 * @param unwrappedNativeObject The native object of GraphicBuffer
515 * @param namedColorSpace Integer value of a named color space {@link ColorSpace.Named}
Robert Carr66b5664f2019-04-02 14:18:56 -0700516 * @param containsSecureLayer Indicates whether this graphic buffer contains captured contents
517 * of secure layers, in which case the screenshot should not be persisted.
Peiyong Line3e5efd2019-03-21 20:59:47 +0000518 */
519 private static ScreenshotGraphicBuffer createFromNative(int width, int height, int format,
Robert Carr66b5664f2019-04-02 14:18:56 -0700520 int usage, long unwrappedNativeObject, int namedColorSpace,
521 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000522 GraphicBuffer graphicBuffer = GraphicBuffer.createFromExisting(width, height, format,
523 usage, unwrappedNativeObject);
524 ColorSpace colorSpace = ColorSpace.get(ColorSpace.Named.values()[namedColorSpace]);
Robert Carr66b5664f2019-04-02 14:18:56 -0700525 return new ScreenshotGraphicBuffer(graphicBuffer, colorSpace, containsSecureLayers);
Peiyong Line3e5efd2019-03-21 20:59:47 +0000526 }
527
528 public ColorSpace getColorSpace() {
529 return mColorSpace;
530 }
531
532 public GraphicBuffer getGraphicBuffer() {
533 return mGraphicBuffer;
534 }
Robert Carr66b5664f2019-04-02 14:18:56 -0700535
536 public boolean containsSecureLayers() {
537 return mContainsSecureLayers;
538 }
Peiyong Line3e5efd2019-03-21 20:59:47 +0000539 }
540
541 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700542 * Builder class for {@link SurfaceControl} objects.
Robert Carr29fe58a2019-03-11 15:25:16 -0700543 *
544 * By default the surface will be hidden, and have "unset" bounds, meaning it can
545 * be as large as the bounds of its parent if a buffer or child so requires.
546 *
547 * It is necessary to set at least a name via {@link Builder#setName}
Robert Carre625fcf2017-09-01 12:36:28 -0700548 */
549 public static class Builder {
550 private SurfaceSession mSession;
551 private int mFlags = HIDDEN;
552 private int mWidth;
553 private int mHeight;
554 private int mFormat = PixelFormat.OPAQUE;
555 private String mName;
556 private SurfaceControl mParent;
Evan Rosky485df202018-12-06 14:11:12 -0800557 private SparseIntArray mMetadata;
Robert Carre625fcf2017-09-01 12:36:28 -0700558
559 /**
560 * Begin building a SurfaceControl with a given {@link SurfaceSession}.
561 *
562 * @param session The {@link SurfaceSession} with which to eventually construct the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800563 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700564 */
565 public Builder(SurfaceSession session) {
566 mSession = session;
567 }
568
569 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800570 * Begin building a SurfaceControl.
571 */
572 public Builder() {
573 }
574
575 /**
576 * Construct a new {@link SurfaceControl} with the set parameters. The builder
577 * remains valid.
Robert Carre625fcf2017-09-01 12:36:28 -0700578 */
Robert Carrda1d2422019-03-07 15:54:37 -0800579 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700580 public SurfaceControl build() {
Vishnu Naire86bd982018-11-28 13:23:17 -0800581 if (mWidth < 0 || mHeight < 0) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700582 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800583 "width and height must be positive or unset");
584 }
Vishnu Nair06cff302020-05-29 14:39:20 -0700585 if ((mWidth > 0 || mHeight > 0) && (isEffectLayer() || isContainerLayer())) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700586 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800587 "Only buffer layers can set a valid buffer size.");
Robert Carre625fcf2017-09-01 12:36:28 -0700588 }
Evan Rosky485df202018-12-06 14:11:12 -0800589 return new SurfaceControl(
590 mSession, mName, mWidth, mHeight, mFormat, mFlags, mParent, mMetadata);
Robert Carre625fcf2017-09-01 12:36:28 -0700591 }
592
593 /**
594 * Set a debugging-name for the SurfaceControl.
595 *
596 * @param name A name to identify the Surface in debugging.
597 */
Robert Carrda1d2422019-03-07 15:54:37 -0800598 @NonNull
599 public Builder setName(@NonNull String name) {
Robert Carre625fcf2017-09-01 12:36:28 -0700600 mName = name;
601 return this;
602 }
603
604 /**
605 * Set the initial size of the controlled surface's buffers in pixels.
606 *
607 * @param width The buffer width in pixels.
608 * @param height The buffer height in pixels.
609 */
Robert Carrda1d2422019-03-07 15:54:37 -0800610 @NonNull
Robert Carr76907ee2019-01-11 13:38:19 -0800611 public Builder setBufferSize(@IntRange(from = 0) int width,
612 @IntRange(from = 0) int height) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800613 if (width < 0 || height < 0) {
Robert Carre625fcf2017-09-01 12:36:28 -0700614 throw new IllegalArgumentException(
615 "width and height must be positive");
616 }
617 mWidth = width;
618 mHeight = height;
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000619 // set this as a buffer layer since we are specifying a buffer size.
620 return setFlags(FX_SURFACE_NORMAL, FX_SURFACE_MASK);
621 }
622
623 /**
624 * Set the initial size of the controlled surface's buffers in pixels.
625 */
626 private void unsetBufferSize() {
627 mWidth = 0;
628 mHeight = 0;
Robert Carre625fcf2017-09-01 12:36:28 -0700629 }
630
631 /**
632 * Set the pixel format of the controlled surface's buffers, using constants from
633 * {@link android.graphics.PixelFormat}.
634 */
Robert Carr76907ee2019-01-11 13:38:19 -0800635 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700636 public Builder setFormat(@PixelFormat.Format int format) {
637 mFormat = format;
638 return this;
639 }
640
641 /**
642 * Specify if the app requires a hardware-protected path to
643 * an external display sync. If protected content is enabled, but
644 * such a path is not available, then the controlled Surface will
645 * not be displayed.
646 *
647 * @param protectedContent Whether to require a protected sink.
Robert Carra7827f72019-01-11 12:53:37 -0800648 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700649 */
Robert Carr76907ee2019-01-11 13:38:19 -0800650 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700651 public Builder setProtected(boolean protectedContent) {
652 if (protectedContent) {
653 mFlags |= PROTECTED_APP;
654 } else {
655 mFlags &= ~PROTECTED_APP;
656 }
657 return this;
658 }
659
660 /**
661 * Specify whether the Surface contains secure content. If true, the system
662 * will prevent the surfaces content from being copied by another process. In
663 * particular screenshots and VNC servers will be disabled. This is however
664 * not a complete prevention of readback as {@link #setProtected}.
Robert Carra7827f72019-01-11 12:53:37 -0800665 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700666 */
Robert Carr76907ee2019-01-11 13:38:19 -0800667 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700668 public Builder setSecure(boolean secure) {
669 if (secure) {
670 mFlags |= SECURE;
671 } else {
672 mFlags &= ~SECURE;
673 }
674 return this;
675 }
676
677 /**
678 * Indicates whether the surface must be considered opaque,
679 * even if its pixel format is set to translucent. This can be useful if an
680 * application needs full RGBA 8888 support for instance but will
681 * still draw every pixel opaque.
682 * <p>
683 * This flag only determines whether opacity will be sampled from the alpha channel.
684 * Plane-alpha from calls to setAlpha() can still result in blended composition
685 * regardless of the opaque setting.
686 *
687 * Combined effects are (assuming a buffer format with an alpha channel):
688 * <ul>
689 * <li>OPAQUE + alpha(1.0) == opaque composition
690 * <li>OPAQUE + alpha(0.x) == blended composition
691 * <li>OPAQUE + alpha(0.0) == no composition
692 * <li>!OPAQUE + alpha(1.0) == blended composition
693 * <li>!OPAQUE + alpha(0.x) == blended composition
694 * <li>!OPAQUE + alpha(0.0) == no composition
695 * </ul>
696 * If the underlying buffer lacks an alpha channel, it is as if setOpaque(true)
697 * were set automatically.
698 * @param opaque Whether the Surface is OPAQUE.
699 */
Robert Carr76907ee2019-01-11 13:38:19 -0800700 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700701 public Builder setOpaque(boolean opaque) {
702 if (opaque) {
703 mFlags |= OPAQUE;
704 } else {
705 mFlags &= ~OPAQUE;
706 }
707 return this;
708 }
709
710 /**
Tiger Huang969c6082019-12-24 20:08:57 +0800711 * Set the initial visibility for the SurfaceControl.
712 *
713 * @param hidden Whether the Surface is initially HIDDEN.
714 * @hide
715 */
716 @NonNull
717 public Builder setHidden(boolean hidden) {
718 if (hidden) {
719 mFlags |= HIDDEN;
720 } else {
721 mFlags &= ~HIDDEN;
722 }
723 return this;
724 }
725
726 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700727 * Set a parent surface for our new SurfaceControl.
728 *
729 * Child surfaces are constrained to the onscreen region of their parent.
730 * Furthermore they stack relatively in Z order, and inherit the transformation
731 * of the parent.
732 *
733 * @param parent The parent control.
734 */
Robert Carr76907ee2019-01-11 13:38:19 -0800735 @NonNull
736 public Builder setParent(@Nullable SurfaceControl parent) {
Robert Carre625fcf2017-09-01 12:36:28 -0700737 mParent = parent;
738 return this;
739 }
740
741 /**
Evan Rosky485df202018-12-06 14:11:12 -0800742 * Sets a metadata int.
Robert Carre625fcf2017-09-01 12:36:28 -0700743 *
Evan Rosky485df202018-12-06 14:11:12 -0800744 * @param key metadata key
745 * @param data associated data
Robert Carra7827f72019-01-11 12:53:37 -0800746 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700747 */
Evan Rosky485df202018-12-06 14:11:12 -0800748 public Builder setMetadata(int key, int data) {
749 if (mMetadata == null) {
750 mMetadata = new SparseIntArray();
Robert Carre625fcf2017-09-01 12:36:28 -0700751 }
Evan Rosky485df202018-12-06 14:11:12 -0800752 mMetadata.put(key, data);
Robert Carre625fcf2017-09-01 12:36:28 -0700753 return this;
754 }
755
756 /**
Vishnu Nair06cff302020-05-29 14:39:20 -0700757 * Indicate whether an 'EffectLayer' is to be constructed.
Robert Carre625fcf2017-09-01 12:36:28 -0700758 *
Vishnu Nair06cff302020-05-29 14:39:20 -0700759 * An effect layer behaves like a container layer by default but it can support
760 * color fill, shadows and/or blur. These layers will not have an associated buffer.
761 * When created, this layer has no effects set and will be transparent but the caller
762 * can render an effect by calling:
763 * - {@link Transaction#setColor(SurfaceControl, float[])}
764 * - {@link Transaction#setBackgroundBlurRadius(SurfaceControl, int)}
765 * - {@link Transaction#setShadowRadius(SurfaceControl, float)}
766 *
767 * @hide
768 */
769 public Builder setEffectLayer() {
770 mFlags |= NO_COLOR_FILL;
771 unsetBufferSize();
772 return setFlags(FX_SURFACE_EFFECT, FX_SURFACE_MASK);
773 }
774
775 /**
776 * A convenience function to create an effect layer with a default color fill
777 * applied to it. Currently that color is black.
Robert Carre625fcf2017-09-01 12:36:28 -0700778 *
Robert Carra7827f72019-01-11 12:53:37 -0800779 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700780 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000781 public Builder setColorLayer() {
782 unsetBufferSize();
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800783 return setFlags(FX_SURFACE_EFFECT, FX_SURFACE_MASK);
Robert Carre625fcf2017-09-01 12:36:28 -0700784 }
785
Vishnu Nair06cff302020-05-29 14:39:20 -0700786 private boolean isEffectLayer() {
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800787 return (mFlags & FX_SURFACE_EFFECT) == FX_SURFACE_EFFECT;
Vishnu Naire86bd982018-11-28 13:23:17 -0800788 }
789
Robert Carre625fcf2017-09-01 12:36:28 -0700790 /**
Robert Carr48ec4e02019-07-16 14:28:47 -0700791 * @hide
792 */
793 public Builder setBLASTLayer() {
794 unsetBufferSize();
795 return setFlags(FX_SURFACE_BLAST, FX_SURFACE_MASK);
796 }
797
798 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700799 * Indicates whether a 'ContainerLayer' is to be constructed.
800 *
801 * Container layers will not be rendered in any fashion and instead are used
802 * as a parent of renderable layers.
803 *
Robert Carra7827f72019-01-11 12:53:37 -0800804 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700805 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000806 public Builder setContainerLayer() {
807 unsetBufferSize();
808 return setFlags(FX_SURFACE_CONTAINER, FX_SURFACE_MASK);
Robert Carrb6cd6432018-08-13 13:01:47 -0700809 }
810
Vishnu Nair06cff302020-05-29 14:39:20 -0700811 private boolean isContainerLayer() {
Vishnu Naire86bd982018-11-28 13:23:17 -0800812 return (mFlags & FX_SURFACE_CONTAINER) == FX_SURFACE_CONTAINER;
813 }
814
Robert Carrb6cd6432018-08-13 13:01:47 -0700815 /**
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000816 * Set 'Surface creation flags' such as {@link #HIDDEN}, {@link #SECURE}.
Robert Carre625fcf2017-09-01 12:36:28 -0700817 *
818 * TODO: Finish conversion to individual builder methods?
819 * @param flags The combined flags
Robert Carra7827f72019-01-11 12:53:37 -0800820 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700821 */
822 public Builder setFlags(int flags) {
823 mFlags = flags;
824 return this;
825 }
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000826
827 private Builder setFlags(int flags, int mask) {
828 mFlags = (mFlags & ~mask) | flags;
829 return this;
830 }
Robert Carre625fcf2017-09-01 12:36:28 -0700831 }
832
833 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800834 * Create a surface with a name.
Andy McFadden314405b2014-01-29 17:18:05 -0800835 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800836 * The surface creation flags specify what kind of surface to create and
837 * certain options such as whether the surface can be assumed to be opaque
838 * and whether it should be initially hidden. Surfaces should always be
839 * created with the {@link #HIDDEN} flag set to ensure that they are not
840 * made visible prematurely before all of the surface's properties have been
841 * configured.
Andy McFadden314405b2014-01-29 17:18:05 -0800842 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800843 * Good practice is to first create the surface with the {@link #HIDDEN} flag
844 * specified, open a transaction, set the surface layer, layer stack, alpha,
chaviw619da692019-06-10 15:39:40 -0700845 * and position, call {@link Transaction#show(SurfaceControl)} if appropriate, and close the
846 * transaction.
Vishnu Naird454442d2018-11-13 13:51:01 -0800847 * <p>
848 * Bounds of the surface is determined by its crop and its buffer size. If the
849 * surface has no buffer or crop, the surface is boundless and only constrained
850 * by the size of its parent bounds.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800851 *
chaviw619da692019-06-10 15:39:40 -0700852 * @param session The surface session, must not be null.
853 * @param name The surface name, must not be null.
854 * @param w The surface initial width.
855 * @param h The surface initial height.
Tiger Huang969c6082019-12-24 20:08:57 +0800856 * @param flags The surface creation flags.
Evan Rosky485df202018-12-06 14:11:12 -0800857 * @param metadata Initial metadata.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700858 * @throws throws OutOfResourcesException If the SurfaceControl cannot be created.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800859 */
Robert Carre625fcf2017-09-01 12:36:28 -0700860 private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags,
Evan Rosky485df202018-12-06 14:11:12 -0800861 SurfaceControl parent, SparseIntArray metadata)
Robert Carrb0f39362018-03-14 13:52:25 -0700862 throws OutOfResourcesException, IllegalArgumentException {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800863 if (name == null) {
864 throw new IllegalArgumentException("name must not be null");
865 }
866
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800867 mName = name;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100868 mWidth = w;
869 mHeight = h;
Evan Rosky485df202018-12-06 14:11:12 -0800870 Parcel metaParcel = Parcel.obtain();
871 try {
872 if (metadata != null && metadata.size() > 0) {
873 metaParcel.writeInt(metadata.size());
874 for (int i = 0; i < metadata.size(); ++i) {
875 metaParcel.writeInt(metadata.keyAt(i));
876 metaParcel.writeByteArray(
Evan Roskyc12c9a32019-02-06 13:38:03 -0800877 ByteBuffer.allocate(4).order(ByteOrder.nativeOrder())
878 .putInt(metadata.valueAt(i)).array());
Evan Rosky485df202018-12-06 14:11:12 -0800879 }
Evan Roskyc12c9a32019-02-06 13:38:03 -0800880 metaParcel.setDataPosition(0);
Evan Rosky485df202018-12-06 14:11:12 -0800881 }
882 mNativeObject = nativeCreate(session, name, w, h, format, flags,
883 parent != null ? parent.mNativeObject : 0, metaParcel);
884 } finally {
885 metaParcel.recycle();
886 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800887 if (mNativeObject == 0) {
888 throw new OutOfResourcesException(
889 "Couldn't allocate SurfaceControl native object");
890 }
Jorim Jaggiee540702020-04-02 21:40:52 +0200891 mNativeHandle = nativeGetHandle(mNativeObject);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800892 mCloseGuard.open("release");
893 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700894
Jorim Jaggiee540702020-04-02 21:40:52 +0200895 /**
896 * Copy constructor. Creates a new native object pointing to the same surface as {@code other}.
897 *
898 * @param other The object to copy the surface from.
Robert Carra7827f72019-01-11 12:53:37 -0800899 * @hide
900 */
Jorim Jaggiee540702020-04-02 21:40:52 +0200901 @TestApi
902 public SurfaceControl(@NonNull SurfaceControl other) {
903 copyFrom(other);
Robert Carr3b716242016-08-16 16:02:21 -0700904 }
905
Jorim Jaggi06975df2017-12-01 14:52:13 +0100906 private SurfaceControl(Parcel in) {
chaviwbeb7a0c2018-12-05 13:49:54 -0800907 readFromParcel(in);
chaviwbeb7a0c2018-12-05 13:49:54 -0800908 }
909
Robert Carra7827f72019-01-11 12:53:37 -0800910 /**
911 * @hide
912 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800913 public SurfaceControl() {
chaviwbeb7a0c2018-12-05 13:49:54 -0800914 }
915
916 public void readFromParcel(Parcel in) {
917 if (in == null) {
918 throw new IllegalArgumentException("source must not be null");
919 }
920
Jeff Sharkey1639e6b2020-04-22 17:36:53 -0600921 mName = in.readString8();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100922 mWidth = in.readInt();
923 mHeight = in.readInt();
Robert Carr5fea55b2018-12-10 13:05:52 -0800924
Robert Carreb344c72019-01-07 18:35:30 -0800925 long object = 0;
Robert Carr5fea55b2018-12-10 13:05:52 -0800926 if (in.readInt() != 0) {
Robert Carreb344c72019-01-07 18:35:30 -0800927 object = nativeReadFromParcel(in);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100928 }
Robert Carreb344c72019-01-07 18:35:30 -0800929 assignNativeObject(object);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100930 }
931
932 @Override
933 public int describeContents() {
934 return 0;
935 }
936
937 @Override
938 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey1639e6b2020-04-22 17:36:53 -0600939 dest.writeString8(mName);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100940 dest.writeInt(mWidth);
941 dest.writeInt(mHeight);
Robert Carr5fea55b2018-12-10 13:05:52 -0800942 if (mNativeObject == 0) {
943 dest.writeInt(0);
944 } else {
945 dest.writeInt(1);
946 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100947 nativeWriteToParcel(mNativeObject, dest);
Robert Carr5fea55b2018-12-10 13:05:52 -0800948
949 if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) {
950 release();
951 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100952 }
953
Vishnu Nair04ab4392018-01-10 11:00:06 -0800954 /**
Jorim Jaggiee540702020-04-02 21:40:52 +0200955 * Checks whether two {@link SurfaceControl} objects represent the same surface.
956 *
957 * @param other The other object to check
958 * @return {@code true} if these two {@link SurfaceControl} objects represent the same surface.
959 * @hide
960 */
961 @TestApi
962 public boolean isSameSurface(@NonNull SurfaceControl other) {
963 return other.mNativeHandle == mNativeHandle;
964 }
965
966 /**
Vishnu Nair04ab4392018-01-10 11:00:06 -0800967 * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link
968 * android.view.SurfaceControlProto}.
969 *
970 * @param proto Stream to write the SurfaceControl object to.
971 * @param fieldId Field Id of the SurfaceControl as defined in the parent message.
972 * @hide
973 */
Jeffrey Huangcb782852019-12-05 11:28:11 -0800974 public void dumpDebug(ProtoOutputStream proto, long fieldId) {
Vishnu Nair04ab4392018-01-10 11:00:06 -0800975 final long token = proto.start(fieldId);
976 proto.write(HASH_CODE, System.identityHashCode(this));
977 proto.write(NAME, mName);
978 proto.end(token);
979 }
980
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700981 public static final @android.annotation.NonNull Creator<SurfaceControl> CREATOR
Jorim Jaggi06975df2017-12-01 14:52:13 +0100982 = new Creator<SurfaceControl>() {
983 public SurfaceControl createFromParcel(Parcel in) {
984 return new SurfaceControl(in);
985 }
986
987 public SurfaceControl[] newArray(int size) {
988 return new SurfaceControl[size];
989 }
990 };
991
Robert Carra7827f72019-01-11 12:53:37 -0800992 /**
993 * @hide
994 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800995 @Override
996 protected void finalize() throws Throwable {
997 try {
998 if (mCloseGuard != null) {
999 mCloseGuard.warnIfOpen();
1000 }
1001 if (mNativeObject != 0) {
1002 nativeRelease(mNativeObject);
1003 }
1004 } finally {
1005 super.finalize();
1006 }
1007 }
1008
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001009 /**
Robert Carr76907ee2019-01-11 13:38:19 -08001010 * Release the local reference to the server-side surface. The surface
1011 * may continue to exist on-screen as long as its parent continues
1012 * to exist. To explicitly remove a surface from the screen use
Robert Carr29fe58a2019-03-11 15:25:16 -07001013 * {@link Transaction#reparent} with a null-parent. After release,
1014 * {@link #isValid} will return false and other methods will throw
1015 * an exception.
Robert Carr76907ee2019-01-11 13:38:19 -08001016 *
1017 * Always call release() when you're done with a SurfaceControl.
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001018 */
1019 public void release() {
1020 if (mNativeObject != 0) {
1021 nativeRelease(mNativeObject);
1022 mNativeObject = 0;
Jorim Jaggiee540702020-04-02 21:40:52 +02001023 mNativeHandle = 0;
Robert Carr01b0dd52020-02-27 13:25:17 -08001024 mCloseGuard.close();
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001025 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001026 }
1027
1028 /**
Chong Zhang47e36a32016-02-29 16:44:33 -08001029 * Disconnect any client still connected to the surface.
Robert Carra7827f72019-01-11 12:53:37 -08001030 * @hide
Chong Zhang47e36a32016-02-29 16:44:33 -08001031 */
1032 public void disconnect() {
1033 if (mNativeObject != 0) {
1034 nativeDisconnect(mNativeObject);
1035 }
1036 }
1037
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001038 private void checkNotReleased() {
Jorim Jaggie1f741b2020-06-16 22:40:12 +02001039 if (mNativeObject == 0) throw new NullPointerException(
1040 "Invalid " + this + ", mNativeObject is null. Have you called release() already?");
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001041 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001042
Robert Carra7827f72019-01-11 12:53:37 -08001043 /**
Robert Carr76907ee2019-01-11 13:38:19 -08001044 * Check whether this instance points to a valid layer with the system-compositor. For
Robert Carr29fe58a2019-03-11 15:25:16 -07001045 * example this may be false if construction failed, or the layer was released
1046 * ({@link #release}).
Robert Carr76907ee2019-01-11 13:38:19 -08001047 *
1048 * @return Whether this SurfaceControl is valid.
Robert Carra7827f72019-01-11 12:53:37 -08001049 */
Robert Carr5fea55b2018-12-10 13:05:52 -08001050 public boolean isValid() {
1051 return mNativeObject != 0;
1052 }
1053
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001054 /*
1055 * set surface parameters.
1056 * needs to be inside open/closeTransaction block
1057 */
1058
Robert Carra7827f72019-01-11 12:53:37 -08001059 /** start a transaction
1060 * @hide
1061 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001062 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001063 public static void openTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -07001064 synchronized (SurfaceControl.class) {
1065 if (sGlobalTransaction == null) {
1066 sGlobalTransaction = new Transaction();
1067 }
1068 synchronized(SurfaceControl.class) {
1069 sTransactionNestCount++;
1070 }
1071 }
1072 }
1073
Robert Carrb1579c82017-09-05 14:54:47 -07001074 /**
1075 * Merge the supplied transaction in to the deprecated "global" transaction.
1076 * This clears the supplied transaction in an identical fashion to {@link Transaction#merge}.
1077 * <p>
1078 * This is a utility for interop with legacy-code and will go away with the Global Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08001079 * @hide
Robert Carrb1579c82017-09-05 14:54:47 -07001080 */
1081 @Deprecated
1082 public static void mergeToGlobalTransaction(Transaction t) {
Robert Carrc5fc4612017-12-05 11:55:40 -08001083 synchronized(SurfaceControl.class) {
Robert Carrb1579c82017-09-05 14:54:47 -07001084 sGlobalTransaction.merge(t);
1085 }
1086 }
1087
chaviwdaaeab02019-03-20 12:25:37 -07001088 /** end a transaction
1089 * @hide
Robert Carra7827f72019-01-11 12:53:37 -08001090 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001091 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001092 public static void closeTransaction() {
chaviwdaaeab02019-03-20 12:25:37 -07001093 synchronized(SurfaceControl.class) {
1094 if (sTransactionNestCount == 0) {
1095 Log.e(TAG,
1096 "Call to SurfaceControl.closeTransaction without matching openTransaction");
1097 } else if (--sTransactionNestCount > 0) {
1098 return;
1099 }
1100 sGlobalTransaction.apply();
1101 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001102 }
1103
Robert Carra7827f72019-01-11 12:53:37 -08001104 /**
1105 * @hide
1106 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001107 public void deferTransactionUntil(SurfaceControl barrier, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -08001108 synchronized(SurfaceControl.class) {
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001109 sGlobalTransaction.deferTransactionUntil(this, barrier, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -08001110 }
1111 }
1112
Robert Carra7827f72019-01-11 12:53:37 -08001113 /**
1114 * @hide
1115 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001116 public void reparentChildren(SurfaceControl newParent) {
Robert Carre13b58e2017-08-31 14:50:44 -07001117 synchronized(SurfaceControl.class) {
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001118 sGlobalTransaction.reparentChildren(this, newParent);
Robert Carre13b58e2017-08-31 14:50:44 -07001119 }
Robert Carrd5c7dd62017-03-08 10:39:30 -08001120 }
1121
Robert Carra7827f72019-01-11 12:53:37 -08001122 /**
1123 * @hide
1124 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001125 public void detachChildren() {
Robert Carre13b58e2017-08-31 14:50:44 -07001126 synchronized(SurfaceControl.class) {
1127 sGlobalTransaction.detachChildren(this);
1128 }
Rob Carr64e516f2015-10-29 00:20:45 +00001129 }
1130
Robert Carra7827f72019-01-11 12:53:37 -08001131 /**
1132 * @hide
1133 */
Robert Carr1ca6a332016-04-11 18:00:43 -07001134 public void setOverrideScalingMode(int scalingMode) {
1135 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001136 synchronized(SurfaceControl.class) {
1137 sGlobalTransaction.setOverrideScalingMode(this, scalingMode);
1138 }
Robert Carr1ca6a332016-04-11 18:00:43 -07001139 }
1140
Robert Carra7827f72019-01-11 12:53:37 -08001141 /**
1142 * @hide
1143 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001144 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001145 public void setLayer(int zorder) {
1146 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001147 synchronized(SurfaceControl.class) {
1148 sGlobalTransaction.setLayer(this, zorder);
1149 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001150 }
1151
Robert Carra7827f72019-01-11 12:53:37 -08001152 /**
1153 * @hide
1154 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001155 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001156 public void setPosition(float x, float y) {
1157 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001158 synchronized(SurfaceControl.class) {
1159 sGlobalTransaction.setPosition(this, x, y);
1160 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001161 }
1162
Robert Carra7827f72019-01-11 12:53:37 -08001163 /**
1164 * @hide
1165 */
Vishnu Naire86bd982018-11-28 13:23:17 -08001166 public void setBufferSize(int w, int h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001167 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001168 synchronized(SurfaceControl.class) {
Vishnu Naire86bd982018-11-28 13:23:17 -08001169 sGlobalTransaction.setBufferSize(this, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07001170 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001171 }
1172
Robert Carra7827f72019-01-11 12:53:37 -08001173 /**
1174 * @hide
1175 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001176 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001177 public void hide() {
1178 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001179 synchronized(SurfaceControl.class) {
1180 sGlobalTransaction.hide(this);
1181 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001182 }
1183
Robert Carra7827f72019-01-11 12:53:37 -08001184 /**
1185 * @hide
1186 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001187 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001188 public void show() {
1189 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001190 synchronized(SurfaceControl.class) {
1191 sGlobalTransaction.show(this);
1192 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001193 }
1194
Robert Carra7827f72019-01-11 12:53:37 -08001195 /**
1196 * @hide
1197 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001198 public void setTransparentRegionHint(Region region) {
1199 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001200 synchronized(SurfaceControl.class) {
1201 sGlobalTransaction.setTransparentRegionHint(this, region);
1202 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001203 }
1204
Robert Carra7827f72019-01-11 12:53:37 -08001205 /**
1206 * @hide
1207 */
Svetoslav1376d602014-03-13 11:17:26 -07001208 public boolean clearContentFrameStats() {
1209 checkNotReleased();
1210 return nativeClearContentFrameStats(mNativeObject);
1211 }
1212
Robert Carra7827f72019-01-11 12:53:37 -08001213 /**
1214 * @hide
1215 */
Svetoslav1376d602014-03-13 11:17:26 -07001216 public boolean getContentFrameStats(WindowContentFrameStats outStats) {
1217 checkNotReleased();
1218 return nativeGetContentFrameStats(mNativeObject, outStats);
1219 }
1220
Robert Carra7827f72019-01-11 12:53:37 -08001221 /**
1222 * @hide
1223 */
Svetoslav1376d602014-03-13 11:17:26 -07001224 public static boolean clearAnimationFrameStats() {
1225 return nativeClearAnimationFrameStats();
1226 }
1227
Robert Carra7827f72019-01-11 12:53:37 -08001228 /**
1229 * @hide
1230 */
Svetoslav1376d602014-03-13 11:17:26 -07001231 public static boolean getAnimationFrameStats(WindowAnimationFrameStats outStats) {
1232 return nativeGetAnimationFrameStats(outStats);
1233 }
1234
Robert Carra7827f72019-01-11 12:53:37 -08001235 /**
1236 * @hide
1237 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001238 public void setAlpha(float alpha) {
1239 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001240 synchronized(SurfaceControl.class) {
1241 sGlobalTransaction.setAlpha(this, alpha);
1242 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001243 }
1244
Robert Carra7827f72019-01-11 12:53:37 -08001245 /**
1246 * @hide
1247 */
Robert Carr0edf18f2017-02-21 20:01:47 -08001248 public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001249 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001250 synchronized(SurfaceControl.class) {
1251 sGlobalTransaction.setMatrix(this, dsdx, dtdx, dtdy, dsdy);
1252 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001253 }
1254
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001255 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08001256 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
1257 * the color can be interpreted in any color space.
1258 * @param agnostic A boolean to indicate whether the surface is color space agnostic
1259 * @hide
1260 */
1261 public void setColorSpaceAgnostic(boolean agnostic) {
1262 checkNotReleased();
1263 synchronized (SurfaceControl.class) {
1264 sGlobalTransaction.setColorSpaceAgnostic(this, agnostic);
1265 }
1266 }
1267
1268 /**
Vishnu Naird454442d2018-11-13 13:51:01 -08001269 * Bounds the surface and its children to the bounds specified. Size of the surface will be
1270 * ignored and only the crop and buffer size will be used to determine the bounds of the
1271 * surface. If no crop is specified and the surface has no buffer, the surface bounds is only
1272 * constrained by the size of its parent bounds.
1273 *
1274 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08001275 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001276 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001277 public void setWindowCrop(Rect crop) {
1278 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001279 synchronized (SurfaceControl.class) {
1280 sGlobalTransaction.setWindowCrop(this, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001281 }
1282 }
1283
Vishnu Naird454442d2018-11-13 13:51:01 -08001284 /**
Robert Carra7827f72019-01-11 12:53:37 -08001285 * @hide
1286 */
Andy McFadden314405b2014-01-29 17:18:05 -08001287 public void setOpaque(boolean isOpaque) {
1288 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001289
1290 synchronized (SurfaceControl.class) {
1291 sGlobalTransaction.setOpaque(this, isOpaque);
Andy McFadden314405b2014-01-29 17:18:05 -08001292 }
1293 }
1294
Robert Carra7827f72019-01-11 12:53:37 -08001295 /**
1296 * @hide
1297 */
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001298 public void setSecure(boolean isSecure) {
1299 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001300
1301 synchronized (SurfaceControl.class) {
1302 sGlobalTransaction.setSecure(this, isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001303 }
1304 }
1305
Robert Carra7827f72019-01-11 12:53:37 -08001306 /**
1307 * @hide
1308 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001309 public int getWidth() {
1310 synchronized (mSizeLock) {
1311 return mWidth;
1312 }
1313 }
1314
Robert Carra7827f72019-01-11 12:53:37 -08001315 /**
1316 * @hide
1317 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001318 public int getHeight() {
1319 synchronized (mSizeLock) {
1320 return mHeight;
1321 }
1322 }
1323
Robert Carre13b58e2017-08-31 14:50:44 -07001324 @Override
1325 public String toString() {
1326 return "Surface(name=" + mName + ")/@0x" +
1327 Integer.toHexString(System.identityHashCode(this));
1328 }
1329
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001330 /**
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001331 * Immutable information about physical display.
1332 *
Robert Carr76907ee2019-01-11 13:38:19 -08001333 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001334 */
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001335 public static final class DisplayInfo {
Dominik Laskowski26290bb2020-01-15 16:09:55 -08001336 public boolean isInternal;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001337 public float density;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001338 public boolean secure;
Marin Shalamanov98af1592019-10-08 10:48:08 +02001339 public DeviceProductInfo deviceProductInfo;
Robert Carra7827f72019-01-11 12:53:37 -08001340
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001341 @Override
1342 public String toString() {
Dominik Laskowski26290bb2020-01-15 16:09:55 -08001343 return "DisplayInfo{isInternal=" + isInternal
1344 + ", density=" + density
Marin Shalamanov98af1592019-10-08 10:48:08 +02001345 + ", secure=" + secure
1346 + ", deviceProductInfo=" + deviceProductInfo + "}";
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001347 }
1348 }
1349
1350 /**
1351 * Configuration supported by physical display.
1352 *
1353 * @hide
1354 */
1355 public static final class DisplayConfig {
Ady Abraham8a5e3912020-02-18 17:28:26 -08001356 /**
1357 * Invalid display config id.
1358 */
1359 public static final int INVALID_DISPLAY_CONFIG_ID = -1;
1360
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001361 public int width;
1362 public int height;
1363 public float xDpi;
1364 public float yDpi;
1365
1366 public float refreshRate;
1367 public long appVsyncOffsetNanos;
1368 public long presentationDeadlineNanos;
1369
Ady Abraham8a5e3912020-02-18 17:28:26 -08001370 /**
1371 * The config group ID this config is associated to.
1372 * Configs in the same group are similar from vendor's perspective and switching between
1373 * configs within the same group can be done seamlessly in most cases.
1374 * @see: android.hardware.graphics.composer@2.4::IComposerClient::Attribute::CONFIG_GROUP
1375 */
1376 public int configGroup;
1377
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001378 @Override
1379 public String toString() {
1380 return "DisplayConfig{width=" + width
1381 + ", height=" + height
1382 + ", xDpi=" + xDpi
1383 + ", yDpi=" + yDpi
1384 + ", refreshRate=" + refreshRate
1385 + ", appVsyncOffsetNanos=" + appVsyncOffsetNanos
Ady Abraham8a5e3912020-02-18 17:28:26 -08001386 + ", presentationDeadlineNanos=" + presentationDeadlineNanos
1387 + ", configGroup=" + configGroup + "}";
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001388 }
1389 }
1390
Robert Carra7827f72019-01-11 12:53:37 -08001391 /**
1392 * @hide
1393 */
Prashant Malanic55929a2014-05-25 01:59:21 -07001394 public static void setDisplayPowerMode(IBinder displayToken, int mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001395 if (displayToken == null) {
1396 throw new IllegalArgumentException("displayToken must not be null");
1397 }
Prashant Malanic55929a2014-05-25 01:59:21 -07001398 nativeSetDisplayPowerMode(displayToken, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001399 }
1400
Robert Carra7827f72019-01-11 12:53:37 -08001401 /**
1402 * @hide
1403 */
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001404 public static SurfaceControl.DisplayInfo getDisplayInfo(IBinder displayToken) {
1405 if (displayToken == null) {
1406 throw new IllegalArgumentException("displayToken must not be null");
1407 }
1408 return nativeGetDisplayInfo(displayToken);
1409 }
1410
1411 /**
1412 * @hide
1413 */
1414 public static SurfaceControl.DisplayConfig[] getDisplayConfigs(IBinder displayToken) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001415 if (displayToken == null) {
1416 throw new IllegalArgumentException("displayToken must not be null");
1417 }
Dan Stoza00101052014-05-02 15:23:40 -07001418 return nativeGetDisplayConfigs(displayToken);
1419 }
1420
Robert Carra7827f72019-01-11 12:53:37 -08001421 /**
1422 * @hide
1423 */
Dan Stoza00101052014-05-02 15:23:40 -07001424 public static int getActiveConfig(IBinder displayToken) {
1425 if (displayToken == null) {
1426 throw new IllegalArgumentException("displayToken must not be null");
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001427 }
Dan Stoza00101052014-05-02 15:23:40 -07001428 return nativeGetActiveConfig(displayToken);
1429 }
1430
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001431 /**
1432 * @hide
1433 */
1434 public static DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
1435 IBinder displayToken) {
1436 if (displayToken == null) {
1437 throw new IllegalArgumentException("displayToken must not be null");
1438 }
1439 return nativeGetDisplayedContentSamplingAttributes(displayToken);
1440 }
1441
1442 /**
1443 * @hide
1444 */
1445 public static boolean setDisplayedContentSamplingEnabled(
1446 IBinder displayToken, boolean enable, int componentMask, int maxFrames) {
1447 if (displayToken == null) {
1448 throw new IllegalArgumentException("displayToken must not be null");
1449 }
1450 final int maxColorComponents = 4;
1451 if ((componentMask >> maxColorComponents) != 0) {
1452 throw new IllegalArgumentException("invalid componentMask when enabling sampling");
1453 }
1454 return nativeSetDisplayedContentSamplingEnabled(
1455 displayToken, enable, componentMask, maxFrames);
1456 }
1457
1458 /**
1459 * @hide
1460 */
1461 public static DisplayedContentSample getDisplayedContentSample(
1462 IBinder displayToken, long maxFrames, long timestamp) {
1463 if (displayToken == null) {
1464 throw new IllegalArgumentException("displayToken must not be null");
1465 }
1466 return nativeGetDisplayedContentSample(displayToken, maxFrames, timestamp);
1467 }
1468
1469
Robert Carra7827f72019-01-11 12:53:37 -08001470 /**
Ana Krulec52f12892019-11-18 03:57:20 -08001471 * Contains information about desired display configuration.
1472 *
1473 * @hide
1474 */
1475 public static final class DesiredDisplayConfigSpecs {
Ana Kruleca74a8642019-11-14 00:51:00 +01001476 public int defaultConfig;
Steven Thomas305a57c2020-04-17 12:28:36 -07001477 /**
1478 * The primary refresh rate range represents display manager's general guidance on the
1479 * display configs surface flinger will consider when switching refresh rates. Unless
1480 * surface flinger has a specific reason to do otherwise, it will stay within this range.
1481 */
1482 public float primaryRefreshRateMin;
1483 public float primaryRefreshRateMax;
1484 /**
1485 * The app request refresh rate range allows surface flinger to consider more display
1486 * configs when switching refresh rates. Although surface flinger will generally stay within
1487 * the primary range, specific considerations, such as layer frame rate settings specified
1488 * via the setFrameRate() api, may cause surface flinger to go outside the primary
1489 * range. Surface flinger never goes outside the app request range. The app request range
1490 * will be greater than or equal to the primary refresh rate range, never smaller.
1491 */
1492 public float appRequestRefreshRateMin;
1493 public float appRequestRefreshRateMax;
Ana Krulec52f12892019-11-18 03:57:20 -08001494
Ana Kruleca74a8642019-11-14 00:51:00 +01001495 public DesiredDisplayConfigSpecs() {}
Ana Krulec52f12892019-11-18 03:57:20 -08001496
Ana Kruleca74a8642019-11-14 00:51:00 +01001497 public DesiredDisplayConfigSpecs(DesiredDisplayConfigSpecs other) {
1498 copyFrom(other);
1499 }
Ana Krulec52f12892019-11-18 03:57:20 -08001500
Steven Thomas305a57c2020-04-17 12:28:36 -07001501 public DesiredDisplayConfigSpecs(int defaultConfig, float primaryRefreshRateMin,
1502 float primaryRefreshRateMax, float appRequestRefreshRateMin,
1503 float appRequestRefreshRateMax) {
Ana Kruleca74a8642019-11-14 00:51:00 +01001504 this.defaultConfig = defaultConfig;
Steven Thomas305a57c2020-04-17 12:28:36 -07001505 this.primaryRefreshRateMin = primaryRefreshRateMin;
1506 this.primaryRefreshRateMax = primaryRefreshRateMax;
1507 this.appRequestRefreshRateMin = appRequestRefreshRateMin;
1508 this.appRequestRefreshRateMax = appRequestRefreshRateMax;
Ana Kruleca74a8642019-11-14 00:51:00 +01001509 }
1510
1511 @Override
1512 public boolean equals(Object o) {
1513 return o instanceof DesiredDisplayConfigSpecs && equals((DesiredDisplayConfigSpecs) o);
1514 }
1515
1516 /**
1517 * Tests for equality.
1518 */
1519 public boolean equals(DesiredDisplayConfigSpecs other) {
1520 return other != null && defaultConfig == other.defaultConfig
Steven Thomas305a57c2020-04-17 12:28:36 -07001521 && primaryRefreshRateMin == other.primaryRefreshRateMin
1522 && primaryRefreshRateMax == other.primaryRefreshRateMax
1523 && appRequestRefreshRateMin == other.appRequestRefreshRateMin
1524 && appRequestRefreshRateMax == other.appRequestRefreshRateMax;
Ana Kruleca74a8642019-11-14 00:51:00 +01001525 }
1526
1527 @Override
1528 public int hashCode() {
1529 return 0; // don't care
1530 }
1531
1532 /**
1533 * Copies the supplied object's values to this object.
1534 */
1535 public void copyFrom(DesiredDisplayConfigSpecs other) {
1536 defaultConfig = other.defaultConfig;
Steven Thomas305a57c2020-04-17 12:28:36 -07001537 primaryRefreshRateMin = other.primaryRefreshRateMin;
1538 primaryRefreshRateMax = other.primaryRefreshRateMax;
1539 appRequestRefreshRateMin = other.appRequestRefreshRateMin;
1540 appRequestRefreshRateMax = other.appRequestRefreshRateMax;
Ana Kruleca74a8642019-11-14 00:51:00 +01001541 }
1542
1543 @Override
1544 public String toString() {
Steven Thomas305a57c2020-04-17 12:28:36 -07001545 return String.format("defaultConfig=%d primaryRefreshRateRange=[%.0f %.0f]"
1546 + " appRequestRefreshRateRange=[%.0f %.0f]",
1547 defaultConfig, primaryRefreshRateMin, primaryRefreshRateMax,
1548 appRequestRefreshRateMin, appRequestRefreshRateMax);
Ana Krulec52f12892019-11-18 03:57:20 -08001549 }
1550 }
1551
1552 /**
Ady Abraham42f9a2fb2019-02-26 14:13:39 -08001553 * @hide
1554 */
Ana Krulec4f753aa2019-11-14 00:49:39 +01001555 public static boolean setDesiredDisplayConfigSpecs(IBinder displayToken,
Ana Krulec52f12892019-11-18 03:57:20 -08001556 SurfaceControl.DesiredDisplayConfigSpecs desiredDisplayConfigSpecs) {
Ana Krulec4f753aa2019-11-14 00:49:39 +01001557 if (displayToken == null) {
1558 throw new IllegalArgumentException("displayToken must not be null");
1559 }
1560
Ana Krulec52f12892019-11-18 03:57:20 -08001561 return nativeSetDesiredDisplayConfigSpecs(displayToken, desiredDisplayConfigSpecs);
Ana Krulec4f753aa2019-11-14 00:49:39 +01001562 }
1563
1564 /**
1565 * @hide
1566 */
Ana Kruleca74a8642019-11-14 00:51:00 +01001567 public static SurfaceControl.DesiredDisplayConfigSpecs getDesiredDisplayConfigSpecs(
1568 IBinder displayToken) {
1569 if (displayToken == null) {
1570 throw new IllegalArgumentException("displayToken must not be null");
1571 }
1572
1573 return nativeGetDesiredDisplayConfigSpecs(displayToken);
1574 }
1575
1576 /**
1577 * @hide
1578 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001579 public static int[] getDisplayColorModes(IBinder displayToken) {
1580 if (displayToken == null) {
1581 throw new IllegalArgumentException("displayToken must not be null");
1582 }
1583 return nativeGetDisplayColorModes(displayToken);
1584 }
1585
Robert Carra7827f72019-01-11 12:53:37 -08001586 /**
Daniel Solomon10e3b332019-01-20 21:09:11 -08001587 * Color coordinates in CIE1931 XYZ color space
1588 *
1589 * @hide
1590 */
1591 public static final class CieXyz {
1592 /**
1593 * @hide
1594 */
1595 public float X;
1596
1597 /**
1598 * @hide
1599 */
1600 public float Y;
1601
1602 /**
1603 * @hide
1604 */
1605 public float Z;
1606 }
1607
1608 /**
1609 * Contains a display's color primaries
1610 *
1611 * @hide
1612 */
1613 public static final class DisplayPrimaries {
1614 /**
1615 * @hide
1616 */
1617 public CieXyz red;
1618
1619 /**
1620 * @hide
1621 */
1622 public CieXyz green;
1623
1624 /**
1625 * @hide
1626 */
1627 public CieXyz blue;
1628
1629 /**
1630 * @hide
1631 */
1632 public CieXyz white;
1633
1634 /**
1635 * @hide
1636 */
1637 public DisplayPrimaries() {
1638 }
1639 }
1640
1641 /**
1642 * @hide
1643 */
1644 public static SurfaceControl.DisplayPrimaries getDisplayNativePrimaries(
1645 IBinder displayToken) {
1646 if (displayToken == null) {
1647 throw new IllegalArgumentException("displayToken must not be null");
1648 }
1649
1650 return nativeGetDisplayNativePrimaries(displayToken);
1651 }
1652
1653 /**
Robert Carra7827f72019-01-11 12:53:37 -08001654 * @hide
1655 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001656 public static int getActiveColorMode(IBinder displayToken) {
1657 if (displayToken == null) {
1658 throw new IllegalArgumentException("displayToken must not be null");
1659 }
1660 return nativeGetActiveColorMode(displayToken);
1661 }
1662
Robert Carra7827f72019-01-11 12:53:37 -08001663 /**
1664 * @hide
1665 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001666 public static boolean setActiveColorMode(IBinder displayToken, int colorMode) {
1667 if (displayToken == null) {
1668 throw new IllegalArgumentException("displayToken must not be null");
1669 }
1670 return nativeSetActiveColorMode(displayToken, colorMode);
1671 }
1672
Robert Carra7827f72019-01-11 12:53:37 -08001673 /**
Peiyong Lin5f4a5682019-01-17 11:37:07 -08001674 * Returns an array of color spaces with 2 elements. The first color space is the
1675 * default color space and second one is wide color gamut color space.
1676 * @hide
1677 */
1678 public static ColorSpace[] getCompositionColorSpaces() {
1679 int[] dataspaces = nativeGetCompositionDataspaces();
1680 ColorSpace srgb = ColorSpace.get(ColorSpace.Named.SRGB);
1681 ColorSpace[] colorSpaces = { srgb, srgb };
1682 if (dataspaces.length == 2) {
1683 for (int i = 0; i < 2; ++i) {
1684 switch(dataspaces[i]) {
1685 case INTERNAL_DATASPACE_DISPLAY_P3:
1686 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.DISPLAY_P3);
1687 break;
1688 case INTERNAL_DATASPACE_SCRGB:
1689 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.EXTENDED_SRGB);
1690 break;
1691 case INTERNAL_DATASPACE_SRGB:
1692 // Other dataspace is not recognized, use SRGB color space instead,
1693 // the default value of the array is already SRGB, thus do nothing.
1694 default:
1695 break;
1696 }
1697 }
1698 }
1699 return colorSpaces;
1700 }
1701
1702 /**
Robert Carra7827f72019-01-11 12:53:37 -08001703 * @hide
1704 */
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001705 public static void setAutoLowLatencyMode(IBinder displayToken, boolean on) {
1706 if (displayToken == null) {
1707 throw new IllegalArgumentException("displayToken must not be null");
1708 }
1709
1710 nativeSetAutoLowLatencyMode(displayToken, on);
1711 }
1712
1713 /**
1714 * @hide
1715 */
1716 public static void setGameContentType(IBinder displayToken, boolean on) {
1717 if (displayToken == null) {
1718 throw new IllegalArgumentException("displayToken must not be null");
1719 }
1720
1721 nativeSetGameContentType(displayToken, on);
1722 }
1723
1724 /**
1725 * @hide
1726 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001727 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001728 public static void setDisplayProjection(IBinder displayToken,
1729 int orientation, Rect layerStackRect, Rect displayRect) {
Robert Carre13b58e2017-08-31 14:50:44 -07001730 synchronized (SurfaceControl.class) {
1731 sGlobalTransaction.setDisplayProjection(displayToken, orientation,
1732 layerStackRect, displayRect);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001733 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001734 }
1735
Robert Carra7827f72019-01-11 12:53:37 -08001736 /**
1737 * @hide
1738 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001739 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001740 public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -07001741 synchronized (SurfaceControl.class) {
1742 sGlobalTransaction.setDisplayLayerStack(displayToken, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001743 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001744 }
1745
Robert Carra7827f72019-01-11 12:53:37 -08001746 /**
1747 * @hide
1748 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001749 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001750 public static void setDisplaySurface(IBinder displayToken, Surface surface) {
Robert Carre13b58e2017-08-31 14:50:44 -07001751 synchronized (SurfaceControl.class) {
1752 sGlobalTransaction.setDisplaySurface(displayToken, surface);
Jeff Brownfc0ebd72013-04-30 16:33:00 -07001753 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001754 }
1755
Robert Carra7827f72019-01-11 12:53:37 -08001756 /**
1757 * @hide
1758 */
Michael Wright01e840f2014-06-26 16:03:25 -07001759 public static void setDisplaySize(IBinder displayToken, int width, int height) {
Robert Carre13b58e2017-08-31 14:50:44 -07001760 synchronized (SurfaceControl.class) {
1761 sGlobalTransaction.setDisplaySize(displayToken, width, height);
Michael Wright01e840f2014-06-26 16:03:25 -07001762 }
Michael Wright01e840f2014-06-26 16:03:25 -07001763 }
1764
Robert Carra7827f72019-01-11 12:53:37 -08001765 /**
1766 * @hide
1767 */
Michael Wright9ff94c02016-03-30 18:05:40 -07001768 public static Display.HdrCapabilities getHdrCapabilities(IBinder displayToken) {
1769 if (displayToken == null) {
1770 throw new IllegalArgumentException("displayToken must not be null");
1771 }
1772 return nativeGetHdrCapabilities(displayToken);
1773 }
1774
Robert Carra7827f72019-01-11 12:53:37 -08001775 /**
1776 * @hide
1777 */
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001778 public static boolean getAutoLowLatencyModeSupport(IBinder displayToken) {
1779 if (displayToken == null) {
1780 throw new IllegalArgumentException("displayToken must not be null");
1781 }
1782
1783 return nativeGetAutoLowLatencyModeSupport(displayToken);
1784 }
1785
1786 /**
1787 * @hide
1788 */
1789 public static boolean getGameContentTypeSupport(IBinder displayToken) {
1790 if (displayToken == null) {
1791 throw new IllegalArgumentException("displayToken must not be null");
1792 }
1793
1794 return nativeGetGameContentTypeSupport(displayToken);
1795 }
1796
1797 /**
1798 * @hide
1799 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001800 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001801 public static IBinder createDisplay(String name, boolean secure) {
1802 if (name == null) {
1803 throw new IllegalArgumentException("name must not be null");
1804 }
1805 return nativeCreateDisplay(name, secure);
1806 }
1807
Robert Carra7827f72019-01-11 12:53:37 -08001808 /**
1809 * @hide
1810 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001811 @UnsupportedAppUsage
Jesse Hall6a6bc212013-08-08 12:15:03 -07001812 public static void destroyDisplay(IBinder displayToken) {
1813 if (displayToken == null) {
1814 throw new IllegalArgumentException("displayToken must not be null");
1815 }
1816 nativeDestroyDisplay(displayToken);
1817 }
1818
Robert Carra7827f72019-01-11 12:53:37 -08001819 /**
1820 * @hide
1821 */
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001822 public static long[] getPhysicalDisplayIds() {
1823 return nativeGetPhysicalDisplayIds();
1824 }
1825
1826 /**
1827 * @hide
1828 */
1829 public static IBinder getPhysicalDisplayToken(long physicalDisplayId) {
1830 return nativeGetPhysicalDisplayToken(physicalDisplayId);
1831 }
1832
1833 /**
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001834 * TODO(b/116025192): Remove this stopgap once framework is display-agnostic.
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001835 *
1836 * @hide
1837 */
1838 public static IBinder getInternalDisplayToken() {
1839 final long[] physicalDisplayIds = getPhysicalDisplayIds();
1840 if (physicalDisplayIds.length == 0) {
1841 return null;
1842 }
1843 return getPhysicalDisplayToken(physicalDisplayIds[0]);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001844 }
1845
Mathias Agopian0449a402013-03-01 23:01:51 -08001846 /**
chaviw08520a02018-09-10 16:44:56 -07001847 * @see SurfaceControl#screenshot(IBinder, Surface, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001848 * @hide
Mathias Agopian0449a402013-03-01 23:01:51 -08001849 */
1850 public static void screenshot(IBinder display, Surface consumer) {
chaviw08520a02018-09-10 16:44:56 -07001851 screenshot(display, consumer, new Rect(), 0, 0, false, 0);
1852 }
1853
1854 /**
1855 * Copy the current screen contents into the provided {@link Surface}
1856 *
1857 * @param consumer The {@link Surface} to take the screenshot into.
1858 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001859 * @hide
chaviw08520a02018-09-10 16:44:56 -07001860 */
1861 public static void screenshot(IBinder display, Surface consumer, Rect sourceCrop, int width,
1862 int height, boolean useIdentityTransform, int rotation) {
1863 if (consumer == null) {
1864 throw new IllegalArgumentException("consumer must not be null");
1865 }
1866
Peiyong Line3e5efd2019-03-21 20:59:47 +00001867 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(display, sourceCrop, width,
1868 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07001869 try {
Peiyong Linccc06b62019-06-25 17:31:09 -07001870 consumer.attachAndQueueBufferWithColorSpace(buffer.getGraphicBuffer(),
1871 buffer.getColorSpace());
chaviw08520a02018-09-10 16:44:56 -07001872 } catch (RuntimeException e) {
1873 Log.w(TAG, "Failed to take screenshot - " + e.getMessage());
1874 }
1875 }
1876
1877 /**
1878 * @see SurfaceControl#screenshot(Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001879 * @hide
chaviw08520a02018-09-10 16:44:56 -07001880 */
1881 @UnsupportedAppUsage
1882 public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) {
1883 return screenshot(sourceCrop, width, height, false, rotation);
Mathias Agopian0449a402013-03-01 23:01:51 -08001884 }
1885
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001886 /**
chaviw1da9cd92017-12-06 10:48:11 -08001887 * Copy the current screen contents into a hardware bitmap and return it.
1888 * Note: If you want to modify the Bitmap in software, you will need to copy the Bitmap into
1889 * a software Bitmap using {@link Bitmap#copy(Bitmap.Config, boolean)}
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001890 *
chaviw08520a02018-09-10 16:44:56 -07001891 * CAVEAT: Versions of screenshot that return a {@link Bitmap} can be extremely slow; avoid use
1892 * unless absolutely necessary; prefer the versions that use a {@link Surface} such as
1893 * {@link SurfaceControl#screenshot(IBinder, Surface)} or {@link GraphicBuffer} such as
1894 * {@link SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}.
Mathias Agopian0449a402013-03-01 23:01:51 -08001895 *
chaviw08520a02018-09-10 16:44:56 -07001896 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001897 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001898 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001899 @UnsupportedAppUsage
Dan Stoza9890e3412014-05-22 16:12:54 -07001900 public static Bitmap screenshot(Rect sourceCrop, int width, int height,
chaviw08520a02018-09-10 16:44:56 -07001901 boolean useIdentityTransform, int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001902 // TODO: should take the display as a parameter
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001903 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
1904 if (displayToken == null) {
1905 Log.w(TAG, "Failed to take screenshot because internal display is disconnected");
1906 return null;
1907 }
1908
chaviwa69e0a72017-11-29 17:55:12 -08001909 if (rotation == ROTATION_90 || rotation == ROTATION_270) {
1910 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
1911 }
1912
1913 SurfaceControl.rotateCropForSF(sourceCrop, rotation);
Peiyong Line3e5efd2019-03-21 20:59:47 +00001914 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(displayToken, sourceCrop, width,
1915 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07001916
1917 if (buffer == null) {
1918 Log.w(TAG, "Failed to take screenshot");
1919 return null;
1920 }
Sunny Goyal62915b22019-04-10 12:28:47 -07001921 return Bitmap.wrapHardwareBuffer(buffer.getGraphicBuffer(), buffer.getColorSpace());
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001922 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001923
chaviw08520a02018-09-10 16:44:56 -07001924 /**
1925 * Captures all the surfaces in a display and returns a {@link GraphicBuffer} with the content.
1926 *
1927 * @param display The display to take the screenshot of.
1928 * @param sourceCrop The portion of the screen to capture into the Bitmap; caller may
1929 * pass in 'new Rect()' if no cropping is desired.
1930 * @param width The desired width of the returned bitmap; the raw screen will be
1931 * scaled down to this size; caller may pass in 0 if no scaling is
1932 * desired.
1933 * @param height The desired height of the returned bitmap; the raw screen will
1934 * be scaled down to this size; caller may pass in 0 if no scaling
1935 * is desired.
1936 * @param useIdentityTransform Replace whatever transformation (rotation, scaling, translation)
1937 * the surface layers are currently using with the identity
1938 * transformation while taking the screenshot.
1939 * @param rotation Apply a custom clockwise rotation to the screenshot, i.e.
1940 * Surface.ROTATION_0,90,180,270. SurfaceFlinger will always take
1941 * screenshots in its native portrait orientation by default, so
1942 * this is useful for returning screenshots that are independent of
1943 * device orientation.
1944 * @return Returns a GraphicBuffer that contains the captured content.
Robert Carra7827f72019-01-11 12:53:37 -08001945 * @hide
chaviw08520a02018-09-10 16:44:56 -07001946 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00001947 public static ScreenshotGraphicBuffer screenshotToBuffer(IBinder display, Rect sourceCrop,
1948 int width, int height, boolean useIdentityTransform, int rotation) {
Mathias Agopian0449a402013-03-01 23:01:51 -08001949 if (display == null) {
1950 throw new IllegalArgumentException("displayToken must not be null");
1951 }
chaviw08520a02018-09-10 16:44:56 -07001952
Robert Carr5c52b132019-02-15 15:48:11 -08001953 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1954 false /* captureSecureLayers */);
1955 }
1956
1957 /**
1958 * Like screenshotToBuffer, but if the caller is AID_SYSTEM, allows
1959 * for the capture of secure layers. This is used for the screen rotation
1960 * animation where the system server takes screenshots but does
1961 * not persist them or allow them to leave the server. However in other
1962 * cases in the system server, we mostly want to omit secure layers
1963 * like when we take a screenshot on behalf of the assistant.
1964 *
1965 * @hide
1966 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00001967 public static ScreenshotGraphicBuffer screenshotToBufferWithSecureLayersUnsafe(IBinder display,
Robert Carr5c52b132019-02-15 15:48:11 -08001968 Rect sourceCrop, int width, int height, boolean useIdentityTransform,
1969 int rotation) {
1970 if (display == null) {
1971 throw new IllegalArgumentException("displayToken must not be null");
1972 }
1973
1974 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1975 true /* captureSecureLayers */);
Mathias Agopian0449a402013-03-01 23:01:51 -08001976 }
Robert Carre13b58e2017-08-31 14:50:44 -07001977
chaviwa69e0a72017-11-29 17:55:12 -08001978 private static void rotateCropForSF(Rect crop, int rot) {
1979 if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270) {
1980 int tmp = crop.top;
1981 crop.top = crop.left;
1982 crop.left = tmp;
1983 tmp = crop.right;
1984 crop.right = crop.bottom;
1985 crop.bottom = tmp;
1986 }
1987 }
1988
chaviw1cda84c2017-10-23 16:47:10 -07001989 /**
chaviw1da9cd92017-12-06 10:48:11 -08001990 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
chaviw1cda84c2017-10-23 16:47:10 -07001991 *
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001992 * @param layer The root layer to capture.
chaviwfbe47df2017-11-10 16:14:49 -08001993 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
Vishnu Naira8bff972019-12-03 10:18:10 -08001994 * Rect()' or null if no cropping is desired. If the root layer does not
1995 * have a buffer or a crop set, then a non-empty source crop must be
1996 * specified.
chaviwfbe47df2017-11-10 16:14:49 -08001997 * @param frameScale The desired scale of the returned buffer; the raw
1998 * screen will be scaled up/down.
Chavi Weingartend7ec64c2017-11-30 01:52:01 +00001999 *
2000 * @return Returns a GraphicBuffer that contains the layer capture.
Robert Carra7827f72019-01-11 12:53:37 -08002001 * @hide
chaviw1cda84c2017-10-23 16:47:10 -07002002 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002003 public static ScreenshotGraphicBuffer captureLayers(SurfaceControl layer, Rect sourceCrop,
chaviwfbe47df2017-11-10 16:14:49 -08002004 float frameScale) {
Chiawei Wang02202d12019-01-03 18:12:13 +08002005 return captureLayers(layer, sourceCrop, frameScale, PixelFormat.RGBA_8888);
2006 }
2007
2008 /**
2009 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
2010 *
2011 * @param layer The root layer to capture.
2012 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
Vishnu Naira8bff972019-12-03 10:18:10 -08002013 * Rect()' or null if no cropping is desired. If the root layer does not
2014 * have a buffer or a crop set, then a non-empty source crop must be
2015 * specified.
Chiawei Wang02202d12019-01-03 18:12:13 +08002016 * @param frameScale The desired scale of the returned buffer; the raw
2017 * screen will be scaled up/down.
2018 * @param format The desired pixel format of the returned buffer.
2019 *
2020 * @return Returns a GraphicBuffer that contains the layer capture.
2021 * @hide
2022 */
2023 public static ScreenshotGraphicBuffer captureLayers(SurfaceControl layer, Rect sourceCrop,
2024 float frameScale, int format) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07002025 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
Chiawei Wang02202d12019-01-03 18:12:13 +08002026 return nativeCaptureLayers(displayToken, layer.mNativeObject, sourceCrop, frameScale, null,
2027 format);
Robert Carrffcdc512019-04-02 11:51:11 -07002028 }
2029
2030 /**
2031 * Like {@link captureLayers} but with an array of layer handles to exclude.
2032 * @hide
2033 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002034 public static ScreenshotGraphicBuffer captureLayersExcluding(SurfaceControl layer,
Robert Carr689a1b02020-03-12 15:33:51 -07002035 Rect sourceCrop, float frameScale, int format, SurfaceControl[] exclude) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07002036 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002037 long[] nativeExcludeObjects = new long[exclude.length];
2038 for (int i = 0; i < exclude.length; i++) {
2039 nativeExcludeObjects[i] = exclude[i].mNativeObject;
2040 }
2041 return nativeCaptureLayers(displayToken, layer.mNativeObject, sourceCrop, frameScale,
Chiawei Wang02202d12019-01-03 18:12:13 +08002042 nativeExcludeObjects, PixelFormat.RGBA_8888);
chaviw1cda84c2017-10-23 16:47:10 -07002043 }
2044
Robert Carra7827f72019-01-11 12:53:37 -08002045 /**
Peiyong Lin0ddb7d42019-01-16 13:25:09 -08002046 * Returns whether protected content is supported in GPU composition.
2047 * @hide
2048 */
2049 public static boolean getProtectedContentSupport() {
2050 return nativeGetProtectedContentSupport();
2051 }
2052
2053 /**
Dan Gittik832b4972019-02-13 18:17:47 +00002054 * Returns whether brightness operations are supported on a display.
2055 *
2056 * @param displayToken
2057 * The token for the display.
2058 *
2059 * @return Whether brightness operations are supported on the display.
2060 *
2061 * @hide
2062 */
2063 public static boolean getDisplayBrightnessSupport(IBinder displayToken) {
2064 return nativeGetDisplayBrightnessSupport(displayToken);
2065 }
2066
2067 /**
2068 * Sets the brightness of a display.
2069 *
2070 * @param displayToken
2071 * The token for the display whose brightness is set.
2072 * @param brightness
2073 * A number between 0.0f (minimum brightness) and 1.0f (maximum brightness), or -1.0f to
2074 * turn the backlight off.
2075 *
2076 * @return Whether the method succeeded or not.
2077 *
2078 * @throws IllegalArgumentException if:
2079 * - displayToken is null;
2080 * - brightness is NaN or greater than 1.0f.
2081 *
2082 * @hide
2083 */
2084 public static boolean setDisplayBrightness(IBinder displayToken, float brightness) {
2085 Objects.requireNonNull(displayToken);
2086 if (Float.isNaN(brightness) || brightness > 1.0f
2087 || (brightness < 0.0f && brightness != -1.0f)) {
2088 throw new IllegalArgumentException("brightness must be a number between 0.0f and 1.0f,"
2089 + " or -1 to turn the backlight off.");
2090 }
2091 return nativeSetDisplayBrightness(displayToken, brightness);
2092 }
2093
chaviwa51724f2019-09-19 09:50:11 -07002094 /**
2095 * Creates a mirrored hierarchy for the mirrorOf {@link SurfaceControl}
2096 *
2097 * Real Hierarchy Mirror
2098 * SC (value that's returned)
2099 * |
2100 * A A'
2101 * | |
2102 * B B'
2103 *
2104 * @param mirrorOf The root of the hierarchy that should be mirrored.
2105 * @return A SurfaceControl that's the parent of the root of the mirrored hierarchy.
2106 *
2107 * @hide
2108 */
2109 public static SurfaceControl mirrorSurface(SurfaceControl mirrorOf) {
2110 long nativeObj = nativeMirrorSurface(mirrorOf.mNativeObject);
2111 SurfaceControl sc = new SurfaceControl();
2112 sc.assignNativeObject(nativeObj);
2113 return sc;
2114 }
2115
Vishnu Nair4a067c52019-11-19 14:25:56 -08002116 private static void validateColorArg(@Size(4) float[] color) {
2117 final String msg = "Color must be specified as a float array with"
2118 + " four values to represent r, g, b, a in range [0..1]";
2119 if (color.length != 4) {
2120 throw new IllegalArgumentException(msg);
2121 }
2122 for (float c:color) {
2123 if ((c < 0.f) || (c > 1.f)) {
2124 throw new IllegalArgumentException(msg);
2125 }
2126 }
2127 }
2128
2129 /**
2130 * Sets the global configuration for all the shadows drawn by SurfaceFlinger. Shadow follows
2131 * material design guidelines.
2132 *
2133 * @param ambientColor Color applied to the ambient shadow. The alpha is premultiplied. A
2134 * float array with four values to represent r, g, b, a in range [0..1]
2135 * @param spotColor Color applied to the spot shadow. The alpha is premultiplied. The position
2136 * of the spot shadow depends on the light position. A float array with
2137 * four values to represent r, g, b, a in range [0..1]
2138 * @param lightPosY Y axis position of the light used to cast the spot shadow in pixels.
2139 * @param lightPosZ Z axis position of the light used to cast the spot shadow in pixels. The X
2140 * axis position is set to the display width / 2.
2141 * @param lightRadius Radius of the light casting the shadow in pixels.
2142 *[
2143 * @hide
2144 */
2145 public static void setGlobalShadowSettings(@Size(4) float[] ambientColor,
2146 @Size(4) float[] spotColor, float lightPosY, float lightPosZ, float lightRadius) {
2147 validateColorArg(ambientColor);
2148 validateColorArg(spotColor);
2149 nativeSetGlobalShadowSettings(ambientColor, spotColor, lightPosY, lightPosZ, lightRadius);
2150 }
2151
Vishnu Nair629df2b2019-06-11 16:03:38 -07002152 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002153 * An atomic set of changes to a set of SurfaceControl.
Robert Carra7827f72019-01-11 12:53:37 -08002154 */
Vishnu Nair629df2b2019-06-11 16:03:38 -07002155 public static class Transaction implements Closeable, Parcelable {
Robert Carr76907ee2019-01-11 13:38:19 -08002156 /**
2157 * @hide
2158 */
Robert Carre13b58e2017-08-31 14:50:44 -07002159 public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
2160 Transaction.class.getClassLoader(),
2161 nativeGetNativeTransactionFinalizer(), 512);
Robert Carr48ec4e02019-07-16 14:28:47 -07002162 /**
2163 * @hide
2164 */
2165 public long mNativeObject;
Robert Carre13b58e2017-08-31 14:50:44 -07002166
Jorim Jaggia5e10572017-11-15 14:36:26 +01002167 private final ArrayMap<SurfaceControl, Point> mResizedSurfaces = new ArrayMap<>();
Robert Carre13b58e2017-08-31 14:50:44 -07002168 Runnable mFreeNativeResources;
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002169 private static final float[] INVALID_COLOR = {-1, -1, -1};
Robert Carre13b58e2017-08-31 14:50:44 -07002170
Robert Carra7827f72019-01-11 12:53:37 -08002171 /**
Robert Carrfee0b822019-12-17 23:56:44 -08002172 * @hide
2173 */
2174 protected void checkPreconditions(SurfaceControl sc) {
2175 sc.checkNotReleased();
2176 }
2177
2178 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002179 * Open a new transaction object. The transaction may be filed with commands to
2180 * manipulate {@link SurfaceControl} instances, and then applied atomically with
2181 * {@link #apply}. Eventually the user should invoke {@link #close}, when the object
2182 * is no longer required. Note however that re-using a transaction after a call to apply
2183 * is allowed as a convenience.
Robert Carra7827f72019-01-11 12:53:37 -08002184 */
Robert Carre13b58e2017-08-31 14:50:44 -07002185 public Transaction() {
2186 mNativeObject = nativeCreateTransaction();
2187 mFreeNativeResources
2188 = sRegistry.registerNativeAllocation(this, mNativeObject);
2189 }
2190
Vishnu Nair629df2b2019-06-11 16:03:38 -07002191 private Transaction(Parcel in) {
2192 readFromParcel(in);
2193 }
2194
Robert Carre13b58e2017-08-31 14:50:44 -07002195 /**
2196 * Apply the transaction, clearing it's state, and making it usable
2197 * as a new transaction.
2198 */
2199 public void apply() {
2200 apply(false);
2201 }
2202
2203 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002204 * Release the native transaction object, without applying it.
Robert Carre13b58e2017-08-31 14:50:44 -07002205 */
2206 @Override
2207 public void close() {
2208 mFreeNativeResources.run();
2209 mNativeObject = 0;
2210 }
2211
2212 /**
2213 * Jankier version of apply. Avoid use (b/28068298).
Robert Carra7827f72019-01-11 12:53:37 -08002214 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002215 */
2216 public void apply(boolean sync) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002217 applyResizedSurfaces();
Robert Carre13b58e2017-08-31 14:50:44 -07002218 nativeApplyTransaction(mNativeObject, sync);
2219 }
2220
Jorim Jaggia5e10572017-11-15 14:36:26 +01002221 private void applyResizedSurfaces() {
2222 for (int i = mResizedSurfaces.size() - 1; i >= 0; i--) {
2223 final Point size = mResizedSurfaces.valueAt(i);
2224 final SurfaceControl surfaceControl = mResizedSurfaces.keyAt(i);
2225 synchronized (surfaceControl.mSizeLock) {
2226 surfaceControl.mWidth = size.x;
2227 surfaceControl.mHeight = size.y;
2228 }
2229 }
2230 mResizedSurfaces.clear();
2231 }
2232
Robert Carra7827f72019-01-11 12:53:37 -08002233 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002234 * Toggle the visibility of a given Layer and it's sub-tree.
2235 *
2236 * @param sc The SurfaceControl for which to set the visibility
2237 * @param visible The new visibility
2238 * @return This transaction object.
2239 */
2240 @NonNull
2241 public Transaction setVisibility(@NonNull SurfaceControl sc, boolean visible) {
Robert Carrfee0b822019-12-17 23:56:44 -08002242 checkPreconditions(sc);
Robert Carr76907ee2019-01-11 13:38:19 -08002243 if (visible) {
2244 return show(sc);
2245 } else {
2246 return hide(sc);
2247 }
2248 }
2249
2250 /**
Ana Krulecfea97172019-11-02 23:11:02 +01002251 * This information is passed to SurfaceFlinger to decide which window should have a
2252 * priority when deciding about the refresh rate of the display. All windows have the
2253 * lowest priority by default.
2254 * @hide
2255 */
2256 @NonNull
2257 public Transaction setFrameRateSelectionPriority(@NonNull SurfaceControl sc, int priority) {
2258 sc.checkNotReleased();
2259 nativeSetFrameRateSelectionPriority(mNativeObject, sc.mNativeObject, priority);
2260 return this;
2261 }
2262
2263 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002264 * Request that a given surface and it's sub-tree be shown.
2265 *
2266 * @param sc The surface to show.
2267 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002268 * @hide
2269 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002270 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002271 public Transaction show(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002272 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002273 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_HIDDEN);
2274 return this;
2275 }
2276
Robert Carra7827f72019-01-11 12:53:37 -08002277 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002278 * Request that a given surface and it's sub-tree be hidden.
2279 *
2280 * @param sc The surface to hidden.
2281 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002282 * @hide
2283 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002284 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002285 public Transaction hide(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002286 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002287 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_HIDDEN, SURFACE_HIDDEN);
2288 return this;
2289 }
2290
Robert Carra7827f72019-01-11 12:53:37 -08002291 /**
2292 * @hide
2293 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002294 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002295 public Transaction setPosition(SurfaceControl sc, float x, float y) {
Robert Carrfee0b822019-12-17 23:56:44 -08002296 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002297 nativeSetPosition(mNativeObject, sc.mNativeObject, x, y);
2298 return this;
2299 }
2300
Robert Carra7827f72019-01-11 12:53:37 -08002301 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002302 * Set the default buffer size for the SurfaceControl, if there is a
2303 * {@link Surface} associated with the control, then
Robert Carr76907ee2019-01-11 13:38:19 -08002304 * this will be the default size for buffers dequeued from it.
2305 * @param sc The surface to set the buffer size for.
2306 * @param w The default width
2307 * @param h The default height
2308 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002309 */
Robert Carr76907ee2019-01-11 13:38:19 -08002310 @NonNull
2311 public Transaction setBufferSize(@NonNull SurfaceControl sc,
2312 @IntRange(from = 0) int w, @IntRange(from = 0) int h) {
Robert Carrfee0b822019-12-17 23:56:44 -08002313 checkPreconditions(sc);
Jorim Jaggia5e10572017-11-15 14:36:26 +01002314 mResizedSurfaces.put(sc, new Point(w, h));
Jorim Jaggidfc27372017-10-27 17:47:49 +02002315 nativeSetSize(mNativeObject, sc.mNativeObject, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07002316 return this;
2317 }
2318
Robert Carra7827f72019-01-11 12:53:37 -08002319 /**
Vishnu Naireb53e522020-05-08 18:03:12 -07002320 * Provide the graphic producer a transform hint if the layer and its children are
2321 * in an orientation different from the display's orientation. The caller is responsible
2322 * for clearing this transform hint if the layer is no longer in a fixed orientation.
2323 *
2324 * The transform hint is used to prevent allocating a buffer of different size when a
2325 * layer is rotated. The producer can choose to consume the hint and allocate the buffer
2326 * with the same size.
2327 *
2328 * @return This Transaction.
2329 * @hide
2330 */
2331 @NonNull
2332 public Transaction setFixedTransformHint(@NonNull SurfaceControl sc,
2333 @Surface.Rotation int transformHint) {
2334 checkPreconditions(sc);
2335 nativeSetFixedTransformHint(mNativeObject, sc.mNativeObject, transformHint);
2336 return this;
2337 }
2338
2339 /**
2340 * Clearing any transform hint if set on this layer.
2341 *
2342 * @return This Transaction.
2343 * @hide
2344 */
2345 @NonNull
2346 public Transaction unsetFixedTransformHint(@NonNull SurfaceControl sc) {
2347 checkPreconditions(sc);
2348 nativeSetFixedTransformHint(mNativeObject, sc.mNativeObject, -1/* INVALID_ROTATION */);
2349 return this;
2350 }
2351
2352 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002353 * Set the Z-order for a given SurfaceControl, relative to it's siblings.
2354 * If two siblings share the same Z order the ordering is undefined. Surfaces
2355 * with a negative Z will be placed below the parent surface.
2356 *
2357 * @param sc The SurfaceControl to set the Z order on
2358 * @param z The Z-order
2359 * @return This Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002360 */
Robert Carr76907ee2019-01-11 13:38:19 -08002361 @NonNull
2362 public Transaction setLayer(@NonNull SurfaceControl sc,
2363 @IntRange(from = Integer.MIN_VALUE, to = Integer.MAX_VALUE) int z) {
Robert Carrfee0b822019-12-17 23:56:44 -08002364 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002365 nativeSetLayer(mNativeObject, sc.mNativeObject, z);
2366 return this;
2367 }
2368
Robert Carra7827f72019-01-11 12:53:37 -08002369 /**
2370 * @hide
2371 */
Robert Carr77e34942017-10-18 19:13:56 -07002372 public Transaction setRelativeLayer(SurfaceControl sc, SurfaceControl relativeTo, int z) {
Robert Carrfee0b822019-12-17 23:56:44 -08002373 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002374 nativeSetRelativeLayer(mNativeObject, sc.mNativeObject, relativeTo.mNativeObject, z);
Robert Carre13b58e2017-08-31 14:50:44 -07002375 return this;
2376 }
2377
Robert Carra7827f72019-01-11 12:53:37 -08002378 /**
2379 * @hide
2380 */
Robert Carre13b58e2017-08-31 14:50:44 -07002381 public Transaction setTransparentRegionHint(SurfaceControl sc, Region transparentRegion) {
Robert Carrfee0b822019-12-17 23:56:44 -08002382 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002383 nativeSetTransparentRegionHint(mNativeObject,
2384 sc.mNativeObject, transparentRegion);
2385 return this;
2386 }
2387
Robert Carra7827f72019-01-11 12:53:37 -08002388 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002389 * Set the alpha for a given surface. If the alpha is non-zero the SurfaceControl
2390 * will be blended with the Surfaces under it according to the specified ratio.
2391 *
2392 * @param sc The given SurfaceControl.
2393 * @param alpha The alpha to set.
Robert Carra7827f72019-01-11 12:53:37 -08002394 */
Robert Carr76907ee2019-01-11 13:38:19 -08002395 @NonNull
2396 public Transaction setAlpha(@NonNull SurfaceControl sc,
2397 @FloatRange(from = 0.0, to = 1.0) float alpha) {
Robert Carrfee0b822019-12-17 23:56:44 -08002398 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002399 nativeSetAlpha(mNativeObject, sc.mNativeObject, alpha);
2400 return this;
2401 }
2402
Robert Carra7827f72019-01-11 12:53:37 -08002403 /**
2404 * @hide
2405 */
Robert Carr788f5742018-07-30 17:46:45 -07002406 public Transaction setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle) {
Robert Carrfee0b822019-12-17 23:56:44 -08002407 checkPreconditions(sc);
Robert Carr788f5742018-07-30 17:46:45 -07002408 nativeSetInputWindowInfo(mNativeObject, sc.mNativeObject, handle);
2409 return this;
2410 }
2411
chaviw59f532e2018-12-26 15:34:59 -08002412 /**
chaviw319cd0782019-02-14 11:00:23 -08002413 * Waits until any changes to input windows have been sent from SurfaceFlinger to
2414 * InputFlinger before returning.
2415 *
2416 * @hide
2417 */
2418 public Transaction syncInputWindows() {
2419 nativeSyncInputWindows(mNativeObject);
2420 return this;
2421 }
2422
2423 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002424 * Specify how the buffer assosciated with this Surface is mapped in to the
2425 * parent coordinate space. The source frame will be scaled to fit the destination
2426 * frame, after being rotated according to the orientation parameter.
2427 *
2428 * @param sc The SurfaceControl to specify the geometry of
2429 * @param sourceCrop The source rectangle in buffer space. Or null for the entire buffer.
2430 * @param destFrame The destination rectangle in parent space. Or null for the source frame.
2431 * @param orientation The buffer rotation
2432 * @return This transaction object.
2433 */
2434 @NonNull
2435 public Transaction setGeometry(@NonNull SurfaceControl sc, @Nullable Rect sourceCrop,
2436 @Nullable Rect destFrame, @Surface.Rotation int orientation) {
Robert Carrfee0b822019-12-17 23:56:44 -08002437 checkPreconditions(sc);
Robert Carr76907ee2019-01-11 13:38:19 -08002438 nativeSetGeometry(mNativeObject, sc.mNativeObject, sourceCrop, destFrame, orientation);
2439 return this;
2440 }
2441
2442 /**
Robert Carra7827f72019-01-11 12:53:37 -08002443 * @hide
2444 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002445 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002446 public Transaction setMatrix(SurfaceControl sc,
2447 float dsdx, float dtdx, float dtdy, float dsdy) {
Robert Carrfee0b822019-12-17 23:56:44 -08002448 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002449 nativeSetMatrix(mNativeObject, sc.mNativeObject,
2450 dsdx, dtdx, dtdy, dsdy);
2451 return this;
2452 }
2453
Robert Carra7827f72019-01-11 12:53:37 -08002454 /**
chaviw619da692019-06-10 15:39:40 -07002455 * Sets the transform and position of a {@link SurfaceControl} from a 3x3 transformation
2456 * matrix.
2457 *
2458 * @param sc SurfaceControl to set matrix of
2459 * @param matrix The matrix to apply.
2460 * @param float9 An array of 9 floats to be used to extract the values from the matrix.
Robert Carra7827f72019-01-11 12:53:37 -08002461 * @hide
2462 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002463 @UnsupportedAppUsage
Jorim Jaggi21c39a72017-10-20 15:47:51 +02002464 public Transaction setMatrix(SurfaceControl sc, Matrix matrix, float[] float9) {
2465 matrix.getValues(float9);
2466 setMatrix(sc, float9[MSCALE_X], float9[MSKEW_Y],
2467 float9[MSKEW_X], float9[MSCALE_Y]);
2468 setPosition(sc, float9[MTRANS_X], float9[MTRANS_Y]);
2469 return this;
2470 }
2471
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002472 /**
2473 * Sets the color transform for the Surface.
chaviw619da692019-06-10 15:39:40 -07002474 *
2475 * @param sc SurfaceControl to set color transform of
2476 * @param matrix A float array with 9 values represents a 3x3 transform matrix
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002477 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08002478 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002479 */
2480 public Transaction setColorTransform(SurfaceControl sc, @Size(9) float[] matrix,
2481 @Size(3) float[] translation) {
Robert Carrfee0b822019-12-17 23:56:44 -08002482 checkPreconditions(sc);
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002483 nativeSetColorTransform(mNativeObject, sc.mNativeObject, matrix, translation);
2484 return this;
2485 }
2486
Robert Carra7827f72019-01-11 12:53:37 -08002487 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08002488 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
2489 * the color can be interpreted in any color space.
2490 * @param agnostic A boolean to indicate whether the surface is color space agnostic
2491 * @hide
2492 */
2493 public Transaction setColorSpaceAgnostic(SurfaceControl sc, boolean agnostic) {
Robert Carrfee0b822019-12-17 23:56:44 -08002494 checkPreconditions(sc);
Peiyong Linf4f0f642019-03-01 14:36:05 -08002495 nativeSetColorSpaceAgnostic(mNativeObject, sc.mNativeObject, agnostic);
2496 return this;
2497 }
2498
2499 /**
chaviw619da692019-06-10 15:39:40 -07002500 * Bounds the surface and its children to the bounds specified. Size of the surface will be
2501 * ignored and only the crop and buffer size will be used to determine the bounds of the
2502 * surface. If no crop is specified and the surface has no buffer, the surface bounds is
2503 * only constrained by the size of its parent bounds.
2504 *
2505 * @param sc SurfaceControl to set crop of.
2506 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08002507 * @hide
2508 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002509 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002510 public Transaction setWindowCrop(SurfaceControl sc, Rect crop) {
Robert Carrfee0b822019-12-17 23:56:44 -08002511 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002512 if (crop != null) {
2513 nativeSetWindowCrop(mNativeObject, sc.mNativeObject,
2514 crop.left, crop.top, crop.right, crop.bottom);
2515 } else {
2516 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, 0, 0);
2517 }
2518
2519 return this;
2520 }
2521
Robert Carra7827f72019-01-11 12:53:37 -08002522 /**
chaviw619da692019-06-10 15:39:40 -07002523 * Same as {@link Transaction#setWindowCrop(SurfaceControl, Rect)} but sets the crop rect
2524 * top left at 0, 0.
2525 *
2526 * @param sc SurfaceControl to set crop of.
2527 * @param width width of crop rect
2528 * @param height height of crop rect
Robert Carra7827f72019-01-11 12:53:37 -08002529 * @hide
2530 */
Vishnu Naird454442d2018-11-13 13:51:01 -08002531 public Transaction setWindowCrop(SurfaceControl sc, int width, int height) {
Robert Carrfee0b822019-12-17 23:56:44 -08002532 checkPreconditions(sc);
Vishnu Naird454442d2018-11-13 13:51:01 -08002533 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, width, height);
2534 return this;
2535 }
2536
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002537 /**
2538 * Sets the corner radius of a {@link SurfaceControl}.
2539 * @param sc SurfaceControl
2540 * @param cornerRadius Corner radius in pixels.
2541 * @return Itself.
Robert Carra7827f72019-01-11 12:53:37 -08002542 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002543 */
2544 @UnsupportedAppUsage
2545 public Transaction setCornerRadius(SurfaceControl sc, float cornerRadius) {
Robert Carrfee0b822019-12-17 23:56:44 -08002546 checkPreconditions(sc);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002547 nativeSetCornerRadius(mNativeObject, sc.mNativeObject, cornerRadius);
2548
2549 return this;
2550 }
2551
Robert Carra7827f72019-01-11 12:53:37 -08002552 /**
Lucas Dupin991415e2019-11-25 17:48:58 -08002553 * Sets the background blur radius of the {@link SurfaceControl}.
2554 *
2555 * @param sc SurfaceControl.
2556 * @param radius Blur radius in pixels.
2557 * @return itself.
2558 * @hide
2559 */
2560 public Transaction setBackgroundBlurRadius(SurfaceControl sc, int radius) {
2561 checkPreconditions(sc);
2562 nativeSetBackgroundBlurRadius(mNativeObject, sc.mNativeObject, radius);
2563 return this;
2564 }
2565
2566 /**
Robert Carra7827f72019-01-11 12:53:37 -08002567 * @hide
2568 */
Mathew Inwood679c15e2019-02-06 15:36:04 +00002569 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.O)
Robert Carre13b58e2017-08-31 14:50:44 -07002570 public Transaction setLayerStack(SurfaceControl sc, int layerStack) {
Robert Carrfee0b822019-12-17 23:56:44 -08002571 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002572 nativeSetLayerStack(mNativeObject, sc.mNativeObject, layerStack);
2573 return this;
2574 }
2575
Robert Carra7827f72019-01-11 12:53:37 -08002576 /**
2577 * @hide
2578 */
Robert Carr76907ee2019-01-11 13:38:19 -08002579 @UnsupportedAppUsage
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002580 public Transaction deferTransactionUntil(SurfaceControl sc, SurfaceControl barrier,
Jorim Jaggidfc27372017-10-27 17:47:49 +02002581 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002582 if (frameNumber < 0) {
2583 return this;
2584 }
Robert Carrfee0b822019-12-17 23:56:44 -08002585 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002586 nativeDeferTransactionUntil(mNativeObject, sc.mNativeObject, barrier.mNativeObject,
2587 frameNumber);
Robert Carre13b58e2017-08-31 14:50:44 -07002588 return this;
2589 }
2590
Robert Carra7827f72019-01-11 12:53:37 -08002591 /**
2592 * @hide
2593 */
Robert Carrcef9a282020-01-14 09:08:05 -08002594 @Deprecated
Robert Carr76907ee2019-01-11 13:38:19 -08002595 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002596 public Transaction deferTransactionUntilSurface(SurfaceControl sc, Surface barrierSurface,
2597 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002598 if (frameNumber < 0) {
2599 return this;
2600 }
Robert Carrfee0b822019-12-17 23:56:44 -08002601 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002602 nativeDeferTransactionUntilSurface(mNativeObject, sc.mNativeObject,
2603 barrierSurface.mNativeObject, frameNumber);
2604 return this;
2605 }
2606
Robert Carra7827f72019-01-11 12:53:37 -08002607 /**
2608 * @hide
2609 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002610 public Transaction reparentChildren(SurfaceControl sc, SurfaceControl newParent) {
Robert Carrfee0b822019-12-17 23:56:44 -08002611 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002612 nativeReparentChildren(mNativeObject, sc.mNativeObject, newParent.mNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -07002613 return this;
2614 }
2615
Robert Carr10584fa2019-01-14 15:55:19 -08002616 /**
2617 * Re-parents a given layer to a new parent. Children inherit transform (position, scaling)
2618 * crop, visibility, and Z-ordering from their parents, as if the children were pixels within the
2619 * parent Surface.
2620 *
2621 * @param sc The SurfaceControl to reparent
2622 * @param newParent The new parent for the given control.
2623 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002624 */
Robert Carr76907ee2019-01-11 13:38:19 -08002625 @NonNull
2626 public Transaction reparent(@NonNull SurfaceControl sc,
2627 @Nullable SurfaceControl newParent) {
Robert Carrfee0b822019-12-17 23:56:44 -08002628 checkPreconditions(sc);
Robert Carr10584fa2019-01-14 15:55:19 -08002629 long otherObject = 0;
2630 if (newParent != null) {
2631 newParent.checkNotReleased();
2632 otherObject = newParent.mNativeObject;
2633 }
2634 nativeReparent(mNativeObject, sc.mNativeObject, otherObject);
Robert Carre13b58e2017-08-31 14:50:44 -07002635 return this;
2636 }
2637
Robert Carra7827f72019-01-11 12:53:37 -08002638 /**
2639 * @hide
2640 */
Robert Carre13b58e2017-08-31 14:50:44 -07002641 public Transaction detachChildren(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002642 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002643 nativeSeverChildren(mNativeObject, sc.mNativeObject);
2644 return this;
2645 }
2646
Robert Carra7827f72019-01-11 12:53:37 -08002647 /**
2648 * @hide
2649 */
Robert Carre13b58e2017-08-31 14:50:44 -07002650 public Transaction setOverrideScalingMode(SurfaceControl sc, int overrideScalingMode) {
Robert Carrfee0b822019-12-17 23:56:44 -08002651 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002652 nativeSetOverrideScalingMode(mNativeObject, sc.mNativeObject,
2653 overrideScalingMode);
2654 return this;
2655 }
2656
2657 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002658 * Fills the surface with the specified color.
2659 * @param color A float array with three values to represent r, g, b in range [0..1]. An
2660 * invalid color will remove the color fill.
Robert Carra7827f72019-01-11 12:53:37 -08002661 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002662 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002663 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002664 public Transaction setColor(SurfaceControl sc, @Size(3) float[] color) {
Robert Carrfee0b822019-12-17 23:56:44 -08002665 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002666 nativeSetColor(mNativeObject, sc.mNativeObject, color);
2667 return this;
2668 }
2669
2670 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002671 * Removes color fill.
2672 * @hide
2673 */
2674 public Transaction unsetColor(SurfaceControl sc) {
2675 checkPreconditions(sc);
2676 nativeSetColor(mNativeObject, sc.mNativeObject, INVALID_COLOR);
2677 return this;
2678 }
2679
2680 /**
Robert Carre13b58e2017-08-31 14:50:44 -07002681 * Sets the security of the surface. Setting the flag is equivalent to creating the
2682 * Surface with the {@link #SECURE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002683 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002684 */
Robert Carrb1579c82017-09-05 14:54:47 -07002685 public Transaction setSecure(SurfaceControl sc, boolean isSecure) {
Robert Carrfee0b822019-12-17 23:56:44 -08002686 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002687 if (isSecure) {
2688 nativeSetFlags(mNativeObject, sc.mNativeObject, SECURE, SECURE);
2689 } else {
2690 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SECURE);
2691 }
2692 return this;
2693 }
2694
2695 /**
2696 * Sets the opacity of the surface. Setting the flag is equivalent to creating the
2697 * Surface with the {@link #OPAQUE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002698 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002699 */
2700 public Transaction setOpaque(SurfaceControl sc, boolean isOpaque) {
Robert Carrfee0b822019-12-17 23:56:44 -08002701 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002702 if (isOpaque) {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002703 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_OPAQUE, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002704 } else {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002705 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002706 }
2707 return this;
2708 }
2709
Robert Carra7827f72019-01-11 12:53:37 -08002710 /**
2711 * @hide
2712 */
Robert Carre13b58e2017-08-31 14:50:44 -07002713 public Transaction setDisplaySurface(IBinder displayToken, Surface surface) {
2714 if (displayToken == null) {
2715 throw new IllegalArgumentException("displayToken must not be null");
2716 }
2717
2718 if (surface != null) {
2719 synchronized (surface.mLock) {
2720 nativeSetDisplaySurface(mNativeObject, displayToken, surface.mNativeObject);
2721 }
2722 } else {
2723 nativeSetDisplaySurface(mNativeObject, displayToken, 0);
2724 }
2725 return this;
2726 }
2727
Robert Carra7827f72019-01-11 12:53:37 -08002728 /**
2729 * @hide
2730 */
Robert Carre13b58e2017-08-31 14:50:44 -07002731 public Transaction setDisplayLayerStack(IBinder displayToken, int layerStack) {
2732 if (displayToken == null) {
2733 throw new IllegalArgumentException("displayToken must not be null");
2734 }
2735 nativeSetDisplayLayerStack(mNativeObject, displayToken, layerStack);
2736 return this;
2737 }
2738
Robert Carra7827f72019-01-11 12:53:37 -08002739 /**
2740 * @hide
2741 */
Robert Carre13b58e2017-08-31 14:50:44 -07002742 public Transaction setDisplayProjection(IBinder displayToken,
2743 int orientation, Rect layerStackRect, Rect displayRect) {
2744 if (displayToken == null) {
2745 throw new IllegalArgumentException("displayToken must not be null");
2746 }
2747 if (layerStackRect == null) {
2748 throw new IllegalArgumentException("layerStackRect must not be null");
2749 }
2750 if (displayRect == null) {
2751 throw new IllegalArgumentException("displayRect must not be null");
2752 }
2753 nativeSetDisplayProjection(mNativeObject, displayToken, orientation,
2754 layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom,
2755 displayRect.left, displayRect.top, displayRect.right, displayRect.bottom);
2756 return this;
2757 }
2758
Robert Carra7827f72019-01-11 12:53:37 -08002759 /**
2760 * @hide
2761 */
Robert Carre13b58e2017-08-31 14:50:44 -07002762 public Transaction setDisplaySize(IBinder displayToken, int width, int height) {
2763 if (displayToken == null) {
2764 throw new IllegalArgumentException("displayToken must not be null");
2765 }
2766 if (width <= 0 || height <= 0) {
2767 throw new IllegalArgumentException("width and height must be positive");
2768 }
2769
2770 nativeSetDisplaySize(mNativeObject, displayToken, width, height);
2771 return this;
2772 }
2773
Vishnu Nair629df2b2019-06-11 16:03:38 -07002774 /** flag the transaction as an animation
Robert Carra7827f72019-01-11 12:53:37 -08002775 * @hide
2776 */
Robert Carre13b58e2017-08-31 14:50:44 -07002777 public Transaction setAnimationTransaction() {
2778 nativeSetAnimationTransaction(mNativeObject);
2779 return this;
2780 }
Robert Carrb1579c82017-09-05 14:54:47 -07002781
2782 /**
Vishnu Nair2ed39d82020-06-17 15:43:13 -07002783 * @deprecated use {@link Transaction#setEarlyWakeupStart()}
2784 *
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002785 * Indicate that SurfaceFlinger should wake up earlier than usual as a result of this
2786 * transaction. This should be used when the caller thinks that the scene is complex enough
2787 * that it's likely to hit GL composition, and thus, SurfaceFlinger needs to more time in
2788 * order not to miss frame deadlines.
2789 * <p>
2790 * Corresponds to setting ISurfaceComposer::eEarlyWakeup
Robert Carra7827f72019-01-11 12:53:37 -08002791 * @hide
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002792 */
Vishnu Nair2ed39d82020-06-17 15:43:13 -07002793 @Deprecated
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002794 public Transaction setEarlyWakeup() {
2795 nativeSetEarlyWakeup(mNativeObject);
2796 return this;
2797 }
2798
Vishnu Nair2ed39d82020-06-17 15:43:13 -07002799 /**
2800 * Provides a hint to SurfaceFlinger to change its offset so that SurfaceFlinger wakes up
2801 * earlier to compose surfaces. The caller should use this as a hint to SurfaceFlinger
2802 * when the scene is complex enough to use GPU composition. The hint will remain active
2803 * until until the client calls {@link Transaction#setEarlyWakeupEnd}.
2804 *
2805 * @hide
2806 */
2807 public Transaction setEarlyWakeupStart() {
2808 nativeSetEarlyWakeupStart(mNativeObject);
2809 return this;
2810 }
2811
2812 /**
2813 * Removes the early wake up hint set by {@link Transaction#setEarlyWakeupStart}.
2814 *
2815 * @hide
2816 */
2817 public Transaction setEarlyWakeupEnd() {
2818 nativeSetEarlyWakeupEnd(mNativeObject);
2819 return this;
2820 }
2821
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002822 /**
Evan Rosky485df202018-12-06 14:11:12 -08002823 * Sets an arbitrary piece of metadata on the surface. This is a helper for int data.
2824 * @hide
2825 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002826 public Transaction setMetadata(SurfaceControl sc, int key, int data) {
Evan Rosky485df202018-12-06 14:11:12 -08002827 Parcel parcel = Parcel.obtain();
2828 parcel.writeInt(data);
2829 try {
Evan Roskyb51e2462019-04-03 19:27:18 -07002830 setMetadata(sc, key, parcel);
Evan Rosky485df202018-12-06 14:11:12 -08002831 } finally {
2832 parcel.recycle();
2833 }
2834 return this;
2835 }
2836
2837 /**
2838 * Sets an arbitrary piece of metadata on the surface.
2839 * @hide
2840 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002841 public Transaction setMetadata(SurfaceControl sc, int key, Parcel data) {
Robert Carrfee0b822019-12-17 23:56:44 -08002842 checkPreconditions(sc);
Evan Roskyb51e2462019-04-03 19:27:18 -07002843 nativeSetMetadata(mNativeObject, sc.mNativeObject, key, data);
Evan Rosky485df202018-12-06 14:11:12 -08002844 return this;
2845 }
2846
Vishnu Naird87984d2019-11-06 14:43:22 -08002847 /**
2848 * Draws shadows of length {@code shadowRadius} around the surface {@link SurfaceControl}.
2849 * If the length is 0.0f then the shadows will not be drawn.
2850 *
2851 * Shadows are drawn around the screen bounds, these are the post transformed cropped
2852 * bounds. They can draw over their parent bounds and will be occluded by layers with a
2853 * higher z-order. The shadows will respect the surface's corner radius if the
2854 * rounded corner bounds (transformed source bounds) are within the screen bounds.
2855 *
2856 * A shadow will only be drawn on buffer and color layers. If the radius is applied on a
2857 * container layer, it will be passed down the hierarchy to be applied on buffer and color
2858 * layers but not its children. A scenario where this is useful is when SystemUI animates
2859 * a task by controlling a leash to it, can draw a shadow around the app surface by
2860 * setting a shadow on the leash. This is similar to how rounded corners are set.
2861 *
2862 * @hide
2863 */
2864 public Transaction setShadowRadius(SurfaceControl sc, float shadowRadius) {
Robert Carrfee0b822019-12-17 23:56:44 -08002865 checkPreconditions(sc);
Vishnu Naird87984d2019-11-06 14:43:22 -08002866 nativeSetShadowRadius(mNativeObject, sc.mNativeObject, shadowRadius);
2867 return this;
2868 }
2869
Evan Rosky485df202018-12-06 14:11:12 -08002870 /**
Steven Thomas6cf051e2020-01-14 11:37:21 -08002871 * Sets the intended frame rate for the surface {@link SurfaceControl}.
Steven Thomas4528b332020-03-26 21:27:54 -07002872 * <p>
Steven Thomas6cf051e2020-01-14 11:37:21 -08002873 * On devices that are capable of running the display at different refresh rates, the system
2874 * may choose a display refresh rate to better match this surface's frame rate. Usage of
2875 * this API won't directly affect the application's frame production pipeline. However,
2876 * because the system may change the display refresh rate, calls to this function may result
2877 * in changes to Choreographer callback timings, and changes to the time interval at which
2878 * the system releases buffers back to the application.
2879 *
2880 * @param sc The SurfaceControl to specify the frame rate of.
Steven Thomasadd6be12020-01-23 16:35:32 -08002881 * @param frameRate The intended frame rate for this surface, in frames per second. 0 is a
2882 * special value that indicates the app will accept the system's choice for
2883 * the display frame rate, which is the default behavior if this function
Steven Thomas4528b332020-03-26 21:27:54 -07002884 * isn't called. The frameRate param does <em>not</em> need to be a valid
2885 * refresh rate for this device's display - e.g., it's fine to pass 30fps
2886 * to a device that can only run the display at 60fps.
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08002887 * @param compatibility The frame rate compatibility of this surface. The compatibility
2888 * value may influence the system's choice of display frame rate. See
2889 * the Surface.FRAME_RATE_COMPATIBILITY_* values for more info.
Steven Thomas6cf051e2020-01-14 11:37:21 -08002890 * @return This transaction object.
2891 */
2892 @NonNull
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08002893 public Transaction setFrameRate(@NonNull SurfaceControl sc,
2894 @FloatRange(from = 0.0) float frameRate,
2895 @Surface.FrameRateCompatibility int compatibility) {
Steven Thomas6cf051e2020-01-14 11:37:21 -08002896 checkPreconditions(sc);
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08002897 nativeSetFrameRate(mNativeObject, sc.mNativeObject, frameRate, compatibility);
Steven Thomas6cf051e2020-01-14 11:37:21 -08002898 return this;
2899 }
2900
2901 /**
Robert Carrb1579c82017-09-05 14:54:47 -07002902 * Merge the other transaction into this transaction, clearing the
2903 * other transaction as if it had been applied.
Robert Carr76907ee2019-01-11 13:38:19 -08002904 *
2905 * @param other The transaction to merge in to this one.
2906 * @return This transaction.
Robert Carrb1579c82017-09-05 14:54:47 -07002907 */
Robert Carr76907ee2019-01-11 13:38:19 -08002908 @NonNull
2909 public Transaction merge(@NonNull Transaction other) {
Tiger Huanged6794e2019-05-07 20:07:59 +08002910 if (this == other) {
2911 return this;
2912 }
Jorim Jaggia5e10572017-11-15 14:36:26 +01002913 mResizedSurfaces.putAll(other.mResizedSurfaces);
2914 other.mResizedSurfaces.clear();
Robert Carrb1579c82017-09-05 14:54:47 -07002915 nativeMergeTransaction(mNativeObject, other.mNativeObject);
2916 return this;
2917 }
Robert Carr71200f22019-02-05 09:44:53 -08002918
2919 /**
2920 * Equivalent to reparent with a null parent, in that it removes
2921 * the SurfaceControl from the scene, but it also releases
2922 * the local resources (by calling {@link SurfaceControl#release})
2923 * after this method returns, {@link SurfaceControl#isValid} will return
2924 * false for the argument.
2925 *
2926 * @param sc The surface to remove and release.
2927 * @return This transaction
2928 * @hide
2929 */
2930 @NonNull
2931 public Transaction remove(@NonNull SurfaceControl sc) {
2932 reparent(sc, null);
2933 sc.release();
2934 return this;
2935 }
Vishnu Nair629df2b2019-06-11 16:03:38 -07002936
Vishnu Nairf7645aa2019-06-18 11:14:01 -07002937 /**
2938 * Writes the transaction to parcel, clearing the transaction as if it had been applied so
2939 * it can be used to store future transactions. It's the responsibility of the parcel
2940 * reader to apply the original transaction.
2941 *
2942 * @param dest parcel to write the transaction to
2943 * @param flags
2944 */
Vishnu Nair629df2b2019-06-11 16:03:38 -07002945 @Override
2946 public void writeToParcel(@NonNull Parcel dest, @WriteFlags int flags) {
2947 if (mNativeObject == 0) {
2948 dest.writeInt(0);
2949 } else {
2950 dest.writeInt(1);
2951 }
2952 nativeWriteTransactionToParcel(mNativeObject, dest);
2953 }
2954
2955 private void readFromParcel(Parcel in) {
2956 mNativeObject = 0;
2957 if (in.readInt() != 0) {
2958 mNativeObject = nativeReadTransactionFromParcel(in);
2959 mFreeNativeResources = sRegistry.registerNativeAllocation(this, mNativeObject);
2960 }
2961 }
2962
2963 @Override
2964 public int describeContents() {
2965 return 0;
2966 }
2967
2968 public static final @NonNull Creator<Transaction> CREATOR = new Creator<Transaction>() {
2969 @Override
2970 public Transaction createFromParcel(Parcel in) {
2971 return new Transaction(in);
2972 }
2973 @Override
2974 public Transaction[] newArray(int size) {
2975 return new Transaction[size];
2976 }
2977 };
Robert Carre13b58e2017-08-31 14:50:44 -07002978 }
Robert Carrfee0b822019-12-17 23:56:44 -08002979
2980 /**
2981 * A debugging utility subclass of SurfaceControl.Transaction. At construction
2982 * you can pass in a monitor object, and all the other methods will throw an exception
2983 * if the monitor is not held when they are called.
2984 * @hide
2985 */
2986 public static class LockDebuggingTransaction extends SurfaceControl.Transaction {
2987 Object mMonitor;
2988
2989 public LockDebuggingTransaction(Object o) {
2990 mMonitor = o;
2991 }
2992
2993 @Override
2994 protected void checkPreconditions(SurfaceControl sc) {
2995 super.checkPreconditions(sc);
2996 if (!Thread.holdsLock(mMonitor)) {
2997 throw new RuntimeException(
2998 "Unlocked access to synchronized SurfaceControl.Transaction");
2999 }
3000 }
3001 }
Steven Thomas6ec6fbc2020-03-24 16:06:33 -07003002
3003 /**
3004 * Acquire a frame rate flexibility token, which allows surface flinger to freely switch display
3005 * frame rates. This is used by CTS tests to put the device in a consistent state. See
Steven Thomas6faeaa22020-04-07 21:26:38 -07003006 * ISurfaceComposer::acquireFrameRateFlexibilityToken(). The caller must have the
3007 * ACCESS_SURFACE_FLINGER permission, or else the call will fail, returning 0.
Steven Thomas6ec6fbc2020-03-24 16:06:33 -07003008 * @hide
3009 */
3010 @TestApi
3011 public static long acquireFrameRateFlexibilityToken() {
3012 return nativeAcquireFrameRateFlexibilityToken();
3013 }
3014
3015 /**
3016 * Release a frame rate flexibility token.
3017 * @hide
3018 */
3019 @TestApi
3020 public static void releaseFrameRateFlexibilityToken(long token) {
3021 nativeReleaseFrameRateFlexibilityToken(token);
3022 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08003023}