blob: c87808b113f67a170cb5f44f78702d73d64260f0 [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;
Artur Satayevad9254c2019-12-10 17:47:54 +000035import android.compat.annotation.UnsupportedAppUsage;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080036import android.graphics.Bitmap;
Peiyong Lin5f4a5682019-01-17 11:37:07 -080037import android.graphics.ColorSpace;
Robert Carr6486d312017-01-09 19:48:29 -080038import android.graphics.GraphicBuffer;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020039import android.graphics.Matrix;
Vishnu Nair04ab4392018-01-10 11:00:06 -080040import android.graphics.PixelFormat;
Jorim Jaggia5e10572017-11-15 14:36:26 +010041import android.graphics.Point;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080042import android.graphics.Rect;
43import android.graphics.Region;
Marin Shalamanov98af1592019-10-08 10:48:08 +020044import android.hardware.display.DeviceProductInfo;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070045import android.hardware.display.DisplayedContentSample;
46import android.hardware.display.DisplayedContentSamplingAttributes;
Mathew Inwood679c15e2019-02-06 15:36:04 +000047import android.os.Build;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080048import android.os.IBinder;
Jorim Jaggi06975df2017-12-01 14:52:13 +010049import android.os.Parcel;
50import android.os.Parcelable;
Jorim Jaggia5e10572017-11-15 14:36:26 +010051import android.util.ArrayMap;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080052import android.util.Log;
Evan Rosky485df202018-12-06 14:11:12 -080053import android.util.SparseIntArray;
Vishnu Nair04ab4392018-01-10 11:00:06 -080054import android.util.proto.ProtoOutputStream;
Igor Murashkina86ab6402013-08-30 12:58:36 -070055import android.view.Surface.OutOfResourcesException;
Jorim Jaggia5e10572017-11-15 14:36:26 +010056
57import com.android.internal.annotations.GuardedBy;
58
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070059import dalvik.system.CloseGuard;
Vishnu Nair04ab4392018-01-10 11:00:06 -080060
Robert Carre625fcf2017-09-01 12:36:28 -070061import libcore.util.NativeAllocationRegistry;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070062
Robert Carre13b58e2017-08-31 14:50:44 -070063import java.io.Closeable;
Evan Rosky485df202018-12-06 14:11:12 -080064import java.nio.ByteBuffer;
Evan Roskyc12c9a32019-02-06 13:38:03 -080065import java.nio.ByteOrder;
Dan Gittik832b4972019-02-13 18:17:47 +000066import java.util.Objects;
Robert Carre13b58e2017-08-31 14:50:44 -070067
Mathias Agopian3866f0d2013-02-11 22:08:48 -080068/**
Robert Carr76907ee2019-01-11 13:38:19 -080069 * Handle to an on-screen Surface managed by the system compositor. The SurfaceControl is
70 * a combination of a buffer source, and metadata about how to display the buffers.
71 * By constructing a {@link Surface} from this SurfaceControl you can submit buffers to be
72 * composited. Using {@link SurfaceControl.Transaction} you can manipulate various
73 * properties of how the buffer will be displayed on-screen. SurfaceControl's are
74 * arranged into a scene-graph like hierarchy, and as such any SurfaceControl may have
75 * a parent. Geometric properties like transform, crop, and Z-ordering will be inherited
76 * from the parent, as if the child were content in the parents buffer stream.
Mathias Agopian3866f0d2013-02-11 22:08:48 -080077 */
Robert Carr76907ee2019-01-11 13:38:19 -080078public final class SurfaceControl implements Parcelable {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080079 private static final String TAG = "SurfaceControl";
Mathias Agopian29479eb2013-02-14 14:36:04 -080080
Ashok Bhat36bef0b2014-01-20 20:08:01 +000081 private static native long nativeCreate(SurfaceSession session, String name,
Evan Rosky485df202018-12-06 14:11:12 -080082 int w, int h, int format, int flags, long parentObject, Parcel metadata)
Mathias Agopian29479eb2013-02-14 14:36:04 -080083 throws OutOfResourcesException;
Jorim Jaggi06975df2017-12-01 14:52:13 +010084 private static native long nativeReadFromParcel(Parcel in);
chaviwbeb7a0c2018-12-05 13:49:54 -080085 private static native long nativeCopyFromSurfaceControl(long nativeObject);
Jorim Jaggi06975df2017-12-01 14:52:13 +010086 private static native void nativeWriteToParcel(long nativeObject, Parcel out);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000087 private static native void nativeRelease(long nativeObject);
Chong Zhang47e36a32016-02-29 16:44:33 -080088 private static native void nativeDisconnect(long nativeObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080089
Peiyong Line3e5efd2019-03-21 20:59:47 +000090 private static native ScreenshotGraphicBuffer nativeScreenshot(IBinder displayToken,
Robert Carr5c52b132019-02-15 15:48:11 -080091 Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation,
92 boolean captureSecureLayers);
Peiyong Lin21e499a2019-04-03 16:37:46 -070093 private static native ScreenshotGraphicBuffer nativeCaptureLayers(IBinder displayToken,
Chiawei Wang02202d12019-01-03 18:12:13 +080094 long layerObject, Rect sourceCrop, float frameScale, long[] excludeLayerObjects,
95 int format);
chaviwa51724f2019-09-19 09:50:11 -070096 private static native long nativeMirrorSurface(long mirrorOfObject);
Robert Carre13b58e2017-08-31 14:50:44 -070097 private static native long nativeCreateTransaction();
98 private static native long nativeGetNativeTransactionFinalizer();
99 private static native void nativeApplyTransaction(long transactionObj, boolean sync);
Robert Carrb1579c82017-09-05 14:54:47 -0700100 private static native void nativeMergeTransaction(long transactionObj,
101 long otherTransactionObj);
Robert Carre13b58e2017-08-31 14:50:44 -0700102 private static native void nativeSetAnimationTransaction(long transactionObj);
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100103 private static native void nativeSetEarlyWakeup(long transactionObj);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800104
Robert Carre13b58e2017-08-31 14:50:44 -0700105 private static native void nativeSetLayer(long transactionObj, long nativeObject, int zorder);
106 private static native void nativeSetRelativeLayer(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700107 long relativeToObject, int zorder);
Robert Carre13b58e2017-08-31 14:50:44 -0700108 private static native void nativeSetPosition(long transactionObj, long nativeObject,
109 float x, float y);
Robert Carre13b58e2017-08-31 14:50:44 -0700110 private static native void nativeSetSize(long transactionObj, long nativeObject, int w, int h);
111 private static native void nativeSetTransparentRegionHint(long transactionObj,
112 long nativeObject, Region region);
113 private static native void nativeSetAlpha(long transactionObj, long nativeObject, float alpha);
114 private static native void nativeSetMatrix(long transactionObj, long nativeObject,
115 float dsdx, float dtdx,
Andrii Kulian283acd22017-08-03 04:03:51 -0700116 float dtdy, float dsdy);
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700117 private static native void nativeSetColorTransform(long transactionObj, long nativeObject,
118 float[] matrix, float[] translation);
Peiyong Linf4f0f642019-03-01 14:36:05 -0800119 private static native void nativeSetColorSpaceAgnostic(long transactionObj, long nativeObject,
120 boolean agnostic);
Robert Carr76907ee2019-01-11 13:38:19 -0800121 private static native void nativeSetGeometry(long transactionObj, long nativeObject,
122 Rect sourceCrop, Rect dest, long orientation);
Robert Carre13b58e2017-08-31 14:50:44 -0700123 private static native void nativeSetColor(long transactionObj, long nativeObject, float[] color);
124 private static native void nativeSetFlags(long transactionObj, long nativeObject,
125 int flags, int mask);
Ana Krulecfea97172019-11-02 23:11:02 +0100126 private static native void nativeSetFrameRateSelectionPriority(long transactionObj,
127 long nativeObject, int priority);
Robert Carre13b58e2017-08-31 14:50:44 -0700128 private static native void nativeSetWindowCrop(long transactionObj, long nativeObject,
129 int l, int t, int r, int b);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -0700130 private static native void nativeSetCornerRadius(long transactionObj, long nativeObject,
131 float cornerRadius);
Lucas Dupin991415e2019-11-25 17:48:58 -0800132 private static native void nativeSetBackgroundBlurRadius(long transactionObj, long nativeObject,
133 int blurRadius);
Robert Carre13b58e2017-08-31 14:50:44 -0700134 private static native void nativeSetLayerStack(long transactionObj, long nativeObject,
135 int layerStack);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800136
Svetoslav1376d602014-03-13 11:17:26 -0700137 private static native boolean nativeClearContentFrameStats(long nativeObject);
138 private static native boolean nativeGetContentFrameStats(long nativeObject, WindowContentFrameStats outStats);
139 private static native boolean nativeClearAnimationFrameStats();
140 private static native boolean nativeGetAnimationFrameStats(WindowAnimationFrameStats outStats);
141
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800142 private static native long[] nativeGetPhysicalDisplayIds();
143 private static native IBinder nativeGetPhysicalDisplayToken(long physicalDisplayId);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800144 private static native IBinder nativeCreateDisplay(String name, boolean secure);
Jesse Hall6a6bc212013-08-08 12:15:03 -0700145 private static native void nativeDestroyDisplay(IBinder displayToken);
Robert Carre13b58e2017-08-31 14:50:44 -0700146 private static native void nativeSetDisplaySurface(long transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000147 IBinder displayToken, long nativeSurfaceObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700148 private static native void nativeSetDisplayLayerStack(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800149 IBinder displayToken, int layerStack);
Robert Carre13b58e2017-08-31 14:50:44 -0700150 private static native void nativeSetDisplayProjection(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800151 IBinder displayToken, int orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -0700152 int l, int t, int r, int b,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800153 int L, int T, int R, int B);
Robert Carre13b58e2017-08-31 14:50:44 -0700154 private static native void nativeSetDisplaySize(long transactionObj, IBinder displayToken,
155 int width, int height);
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800156 private static native SurfaceControl.DisplayInfo nativeGetDisplayInfo(IBinder displayToken);
157 private static native SurfaceControl.DisplayConfig[] nativeGetDisplayConfigs(
Dan Stoza00101052014-05-02 15:23:40 -0700158 IBinder displayToken);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -0700159 private static native DisplayedContentSamplingAttributes
160 nativeGetDisplayedContentSamplingAttributes(IBinder displayToken);
161 private static native boolean nativeSetDisplayedContentSamplingEnabled(IBinder displayToken,
162 boolean enable, int componentMask, int maxFrames);
163 private static native DisplayedContentSample nativeGetDisplayedContentSample(
164 IBinder displayToken, long numFrames, long timestamp);
Dan Stoza00101052014-05-02 15:23:40 -0700165 private static native int nativeGetActiveConfig(IBinder displayToken);
Ana Krulec4f753aa2019-11-14 00:49:39 +0100166 private static native boolean nativeSetDesiredDisplayConfigSpecs(IBinder displayToken,
Ana Krulec52f12892019-11-18 03:57:20 -0800167 SurfaceControl.DesiredDisplayConfigSpecs desiredDisplayConfigSpecs);
Ana Kruleca74a8642019-11-14 00:51:00 +0100168 private static native SurfaceControl.DesiredDisplayConfigSpecs
169 nativeGetDesiredDisplayConfigSpecs(IBinder displayToken);
Michael Wright1c9977b2016-07-12 13:30:10 -0700170 private static native int[] nativeGetDisplayColorModes(IBinder displayToken);
Daniel Solomon10e3b332019-01-20 21:09:11 -0800171 private static native SurfaceControl.DisplayPrimaries nativeGetDisplayNativePrimaries(
172 IBinder displayToken);
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800173 private static native int[] nativeGetCompositionDataspaces();
Michael Wright1c9977b2016-07-12 13:30:10 -0700174 private static native int nativeGetActiveColorMode(IBinder displayToken);
175 private static native boolean nativeSetActiveColorMode(IBinder displayToken,
176 int colorMode);
Galia Peycheva056b3ee2019-06-26 14:05:12 +0200177 private static native void nativeSetAutoLowLatencyMode(IBinder displayToken, boolean on);
178 private static native void nativeSetGameContentType(IBinder displayToken, boolean on);
Prashant Malanic55929a2014-05-25 01:59:21 -0700179 private static native void nativeSetDisplayPowerMode(
180 IBinder displayToken, int mode);
Robert Carre13b58e2017-08-31 14:50:44 -0700181 private static native void nativeDeferTransactionUntil(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700182 long barrierObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700183 private static native void nativeDeferTransactionUntilSurface(long transactionObj,
184 long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800185 long surfaceObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700186 private static native void nativeReparentChildren(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700187 long newParentObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700188 private static native void nativeReparent(long transactionObj, long nativeObject,
Robert Carr10584fa2019-01-14 15:55:19 -0800189 long newParentNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700190 private static native void nativeSeverChildren(long transactionObj, long nativeObject);
191 private static native void nativeSetOverrideScalingMode(long transactionObj, long nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700192 int scalingMode);
Robert Carr6da3cc02016-06-16 15:17:07 -0700193
Michael Wright9ff94c02016-03-30 18:05:40 -0700194 private static native Display.HdrCapabilities nativeGetHdrCapabilities(IBinder displayToken);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800195
Galia Peycheva056b3ee2019-06-26 14:05:12 +0200196 private static native boolean nativeGetAutoLowLatencyModeSupport(IBinder displayToken);
197 private static native boolean nativeGetGameContentTypeSupport(IBinder displayToken);
198
Robert Carr788f5742018-07-30 17:46:45 -0700199 private static native void nativeSetInputWindowInfo(long transactionObj, long nativeObject,
200 InputWindowHandle handle);
Arthur Hungc499ad32019-09-05 15:51:14 +0800201
Peiyong Lin0ddb7d42019-01-16 13:25:09 -0800202 private static native boolean nativeGetProtectedContentSupport();
Evan Roskyb51e2462019-04-03 19:27:18 -0700203 private static native void nativeSetMetadata(long transactionObj, long nativeObject, int key,
204 Parcel data);
chaviw319cd0782019-02-14 11:00:23 -0800205 private static native void nativeSyncInputWindows(long transactionObj);
Dan Gittik832b4972019-02-13 18:17:47 +0000206 private static native boolean nativeGetDisplayBrightnessSupport(IBinder displayToken);
207 private static native boolean nativeSetDisplayBrightness(IBinder displayToken,
208 float brightness);
Vishnu Nair629df2b2019-06-11 16:03:38 -0700209 private static native long nativeReadTransactionFromParcel(Parcel in);
210 private static native void nativeWriteTransactionToParcel(long nativeObject, Parcel out);
Vishnu Naird87984d2019-11-06 14:43:22 -0800211 private static native void nativeSetShadowRadius(long transactionObj, long nativeObject,
212 float shadowRadius);
Vishnu Nair4a067c52019-11-19 14:25:56 -0800213 private static native void nativeSetGlobalShadowSettings(@Size(4) float[] ambientColor,
214 @Size(4) float[] spotColor, float lightPosY, float lightPosZ, float lightRadius);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800215
Steven Thomas6cf051e2020-01-14 11:37:21 -0800216 private static native void nativeSetFrameRate(
Steven Thomasdd7bf2f2020-01-31 18:50:02 -0800217 long transactionObj, long nativeObject, float frameRate, int compatibility);
Steven Thomas6cf051e2020-01-14 11:37:21 -0800218
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800219 private final CloseGuard mCloseGuard = CloseGuard.get();
chaviwbeb7a0c2018-12-05 13:49:54 -0800220 private String mName;
Robert Carr48ec4e02019-07-16 14:28:47 -0700221 /**
222 * @hide
223 */
224 public long mNativeObject;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800225
Jorim Jaggia5e10572017-11-15 14:36:26 +0100226 // TODO: Move this to native.
227 private final Object mSizeLock = new Object();
228 @GuardedBy("mSizeLock")
229 private int mWidth;
230 @GuardedBy("mSizeLock")
231 private int mHeight;
232
Robert Carre13b58e2017-08-31 14:50:44 -0700233 static Transaction sGlobalTransaction;
234 static long sTransactionNestCount = 0;
235
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800236 /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */
237
238 /**
239 * Surface creation flag: Surface is created hidden
Robert Carra7827f72019-01-11 12:53:37 -0800240 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800241 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100242 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800243 public static final int HIDDEN = 0x00000004;
244
245 /**
246 * Surface creation flag: The surface contains secure content, special
247 * measures will be taken to disallow the surface's content to be copied
248 * from another process. In particular, screenshots and VNC servers will
249 * be disabled, but other measures can take place, for instance the
Jesse Hall6a6bc212013-08-08 12:15:03 -0700250 * surface might not be hardware accelerated.
Robert Carra7827f72019-01-11 12:53:37 -0800251 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800252 */
253 public static final int SECURE = 0x00000080;
254
255 /**
256 * Surface creation flag: Creates a surface where color components are interpreted
257 * as "non pre-multiplied" by their alpha channel. Of course this flag is
258 * meaningless for surfaces without an alpha channel. By default
259 * surfaces are pre-multiplied, which means that each color component is
260 * already multiplied by its alpha value. In this case the blending
261 * equation used is:
Andy McFadden314405b2014-01-29 17:18:05 -0800262 * <p>
263 * <code>DEST = SRC + DEST * (1-SRC_ALPHA)</code>
264 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800265 * By contrast, non pre-multiplied surfaces use the following equation:
Andy McFadden314405b2014-01-29 17:18:05 -0800266 * <p>
267 * <code>DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)</code>
268 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800269 * pre-multiplied surfaces must always be used if transparent pixels are
270 * composited on top of each-other into the surface. A pre-multiplied
271 * surface can never lower the value of the alpha component of a given
272 * pixel.
Andy McFadden314405b2014-01-29 17:18:05 -0800273 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800274 * In some rare situations, a non pre-multiplied surface is preferable.
Robert Carra7827f72019-01-11 12:53:37 -0800275 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800276 */
277 public static final int NON_PREMULTIPLIED = 0x00000100;
278
279 /**
280 * Surface creation flag: Indicates that the surface must be considered opaque,
Robert Carre625fcf2017-09-01 12:36:28 -0700281 * even if its pixel format contains an alpha channel. This can be useful if an
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800282 * application needs full RGBA 8888 support for instance but will
283 * still draw every pixel opaque.
Andy McFadden314405b2014-01-29 17:18:05 -0800284 * <p>
285 * This flag is ignored if setAlpha() is used to make the surface non-opaque.
286 * Combined effects are (assuming a buffer format with an alpha channel):
287 * <ul>
288 * <li>OPAQUE + alpha(1.0) == opaque composition
289 * <li>OPAQUE + alpha(0.x) == blended composition
290 * <li>!OPAQUE + alpha(1.0) == blended composition
291 * <li>!OPAQUE + alpha(0.x) == blended composition
292 * </ul>
293 * If the underlying buffer lacks an alpha channel, the OPAQUE flag is effectively
294 * set automatically.
Robert Carra7827f72019-01-11 12:53:37 -0800295 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800296 */
297 public static final int OPAQUE = 0x00000400;
298
299 /**
300 * Surface creation flag: Application requires a hardware-protected path to an
301 * external display sink. If a hardware-protected path is not available,
302 * then this surface will not be displayed on the external sink.
303 *
Robert Carra7827f72019-01-11 12:53:37 -0800304 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800305 */
306 public static final int PROTECTED_APP = 0x00000800;
307
308 // 0x1000 is reserved for an independent DRM protected flag in framework
309
310 /**
Riley Andrews68eccda2014-07-07 11:47:35 -0700311 * Surface creation flag: Window represents a cursor glyph.
Robert Carra7827f72019-01-11 12:53:37 -0800312 * @hide
Riley Andrews68eccda2014-07-07 11:47:35 -0700313 */
314 public static final int CURSOR_WINDOW = 0x00002000;
315
316 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800317 * Surface creation flag: Creates a normal surface.
318 * This is the default.
319 *
Robert Carra7827f72019-01-11 12:53:37 -0800320 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800321 */
322 public static final int FX_SURFACE_NORMAL = 0x00000000;
323
324 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800325 * Surface creation flag: Creates a effect surface which
326 * represents a solid color and or shadows.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800327 *
Robert Carra7827f72019-01-11 12:53:37 -0800328 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800329 */
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800330 public static final int FX_SURFACE_EFFECT = 0x00020000;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800331
332 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700333 * Surface creation flag: Creates a container surface.
334 * This surface will have no buffers and will only be used
335 * as a container for other surfaces, or for its InputInfo.
Robert Carra7827f72019-01-11 12:53:37 -0800336 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700337 */
338 public static final int FX_SURFACE_CONTAINER = 0x00080000;
339
340 /**
Robert Carr48ec4e02019-07-16 14:28:47 -0700341 * @hide
342 */
343 public static final int FX_SURFACE_BLAST = 0x00040000;
344
345 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800346 * Mask used for FX values above.
347 *
Robert Carra7827f72019-01-11 12:53:37 -0800348 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800349 */
350 public static final int FX_SURFACE_MASK = 0x000F0000;
351
352 /* flags used with setFlags() (keep in sync with ISurfaceComposer.h) */
353
354 /**
355 * Surface flag: Hide the surface.
356 * Equivalent to calling hide().
Andy McFadden314405b2014-01-29 17:18:05 -0800357 * Updates the value set during Surface creation (see {@link #HIDDEN}).
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800358 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800359 private static final int SURFACE_HIDDEN = 0x01;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800360
Andy McFadden314405b2014-01-29 17:18:05 -0800361 /**
362 * Surface flag: composite without blending when possible.
363 * Updates the value set during Surface creation (see {@link #OPAQUE}).
364 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800365 private static final int SURFACE_OPAQUE = 0x02;
Andy McFadden314405b2014-01-29 17:18:05 -0800366
Robert Carr76907ee2019-01-11 13:38:19 -0800367 // Display power modes.
Prashant Malanic55929a2014-05-25 01:59:21 -0700368 /**
369 * Display power mode off: used while blanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700370 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800371 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700372 */
373 public static final int POWER_MODE_OFF = 0;
374
375 /**
376 * Display power mode doze: used while putting the screen into low power mode.
Jeff Brown5dc21912014-07-17 18:50:18 -0700377 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800378 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700379 */
380 public static final int POWER_MODE_DOZE = 1;
381
382 /**
383 * Display power mode normal: used while unblanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700384 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800385 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700386 */
387 public static final int POWER_MODE_NORMAL = 2;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800388
Jeff Brown5dc21912014-07-17 18:50:18 -0700389 /**
390 * Display power mode doze: used while putting the screen into a suspended
391 * low power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800392 * @hide
Jeff Brown5dc21912014-07-17 18:50:18 -0700393 */
394 public static final int POWER_MODE_DOZE_SUSPEND = 3;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800395
396 /**
Chris Phoenix10a4a642017-09-25 13:21:00 -0700397 * Display power mode on: used while putting the screen into a suspended
398 * full power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800399 * @hide
Chris Phoenix10a4a642017-09-25 13:21:00 -0700400 */
401 public static final int POWER_MODE_ON_SUSPEND = 4;
402
403 /**
Robert Carr132c9f52017-07-31 17:02:30 -0700404 * A value for windowType used to indicate that the window should be omitted from screenshots
405 * and display mirroring. A temporary workaround until we express such things with
406 * the hierarchy.
407 * TODO: b/64227542
408 * @hide
409 */
410 public static final int WINDOW_TYPE_DONT_SCREENSHOT = 441731;
411
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800412 /**
413 * internal representation of how to interpret pixel value, used only to convert to ColorSpace.
414 */
415 private static final int INTERNAL_DATASPACE_SRGB = 142671872;
416 private static final int INTERNAL_DATASPACE_DISPLAY_P3 = 143261696;
417 private static final int INTERNAL_DATASPACE_SCRGB = 411107328;
418
Robert Carreb344c72019-01-07 18:35:30 -0800419 private void assignNativeObject(long nativeObject) {
420 if (mNativeObject != 0) {
421 release();
422 }
Robert Carr01b0dd52020-02-27 13:25:17 -0800423 if (nativeObject != 0) {
424 mCloseGuard.open("release");
425 }
Robert Carreb344c72019-01-07 18:35:30 -0800426 mNativeObject = nativeObject;
427 }
428
Robert Carra7827f72019-01-11 12:53:37 -0800429 /**
430 * @hide
431 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800432 public void copyFrom(SurfaceControl other) {
433 mName = other.mName;
434 mWidth = other.mWidth;
435 mHeight = other.mHeight;
Robert Carreb344c72019-01-07 18:35:30 -0800436 assignNativeObject(nativeCopyFromSurfaceControl(other.mNativeObject));
chaviwbeb7a0c2018-12-05 13:49:54 -0800437 }
438
Robert Carr132c9f52017-07-31 17:02:30 -0700439 /**
Evan Rosky485df202018-12-06 14:11:12 -0800440 * owner UID.
441 * @hide
442 */
443 public static final int METADATA_OWNER_UID = 1;
444
445 /**
446 * Window type as per {@link WindowManager.LayoutParams}.
447 * @hide
448 */
449 public static final int METADATA_WINDOW_TYPE = 2;
450
451 /**
Evan Rosky9020c072018-12-06 14:11:12 -0800452 * Task id to allow association between surfaces and task.
453 * @hide
454 */
455 public static final int METADATA_TASK_ID = 3;
456
457 /**
Daichi Hirono638ae6d2019-12-18 13:59:58 +0900458 * The style of mouse cursor and hotspot.
459 * @hide
460 */
461 public static final int METADATA_MOUSE_CURSOR = 4;
462
463 /**
Daichi Hirono1d56ce32019-11-01 14:15:10 +0900464 * Accessibility ID to allow association between surfaces and accessibility tree.
465 * @hide
466 */
Daichi Hirono638ae6d2019-12-18 13:59:58 +0900467 public static final int METADATA_ACCESSIBILITY_ID = 5;
Daichi Hirono1d56ce32019-11-01 14:15:10 +0900468
469 /**
Peiyong Line3e5efd2019-03-21 20:59:47 +0000470 * A wrapper around GraphicBuffer that contains extra information about how to
471 * interpret the screenshot GraphicBuffer.
472 * @hide
473 */
474 public static class ScreenshotGraphicBuffer {
475 private final GraphicBuffer mGraphicBuffer;
476 private final ColorSpace mColorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700477 private final boolean mContainsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000478
Robert Carr66b5664f2019-04-02 14:18:56 -0700479 public ScreenshotGraphicBuffer(GraphicBuffer graphicBuffer, ColorSpace colorSpace,
480 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000481 mGraphicBuffer = graphicBuffer;
482 mColorSpace = colorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700483 mContainsSecureLayers = containsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000484 }
485
486 /**
487 * Create ScreenshotGraphicBuffer from existing native GraphicBuffer object.
488 * @param width The width in pixels of the buffer
489 * @param height The height in pixels of the buffer
490 * @param format The format of each pixel as specified in {@link PixelFormat}
491 * @param usage Hint indicating how the buffer will be used
492 * @param unwrappedNativeObject The native object of GraphicBuffer
493 * @param namedColorSpace Integer value of a named color space {@link ColorSpace.Named}
Robert Carr66b5664f2019-04-02 14:18:56 -0700494 * @param containsSecureLayer Indicates whether this graphic buffer contains captured contents
495 * of secure layers, in which case the screenshot should not be persisted.
Peiyong Line3e5efd2019-03-21 20:59:47 +0000496 */
497 private static ScreenshotGraphicBuffer createFromNative(int width, int height, int format,
Robert Carr66b5664f2019-04-02 14:18:56 -0700498 int usage, long unwrappedNativeObject, int namedColorSpace,
499 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000500 GraphicBuffer graphicBuffer = GraphicBuffer.createFromExisting(width, height, format,
501 usage, unwrappedNativeObject);
502 ColorSpace colorSpace = ColorSpace.get(ColorSpace.Named.values()[namedColorSpace]);
Robert Carr66b5664f2019-04-02 14:18:56 -0700503 return new ScreenshotGraphicBuffer(graphicBuffer, colorSpace, containsSecureLayers);
Peiyong Line3e5efd2019-03-21 20:59:47 +0000504 }
505
506 public ColorSpace getColorSpace() {
507 return mColorSpace;
508 }
509
510 public GraphicBuffer getGraphicBuffer() {
511 return mGraphicBuffer;
512 }
Robert Carr66b5664f2019-04-02 14:18:56 -0700513
514 public boolean containsSecureLayers() {
515 return mContainsSecureLayers;
516 }
Peiyong Line3e5efd2019-03-21 20:59:47 +0000517 }
518
519 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700520 * Builder class for {@link SurfaceControl} objects.
Robert Carr29fe58a2019-03-11 15:25:16 -0700521 *
522 * By default the surface will be hidden, and have "unset" bounds, meaning it can
523 * be as large as the bounds of its parent if a buffer or child so requires.
524 *
525 * It is necessary to set at least a name via {@link Builder#setName}
Robert Carre625fcf2017-09-01 12:36:28 -0700526 */
527 public static class Builder {
528 private SurfaceSession mSession;
529 private int mFlags = HIDDEN;
530 private int mWidth;
531 private int mHeight;
532 private int mFormat = PixelFormat.OPAQUE;
533 private String mName;
534 private SurfaceControl mParent;
Evan Rosky485df202018-12-06 14:11:12 -0800535 private SparseIntArray mMetadata;
Robert Carre625fcf2017-09-01 12:36:28 -0700536
537 /**
538 * Begin building a SurfaceControl with a given {@link SurfaceSession}.
539 *
540 * @param session The {@link SurfaceSession} with which to eventually construct the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800541 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700542 */
543 public Builder(SurfaceSession session) {
544 mSession = session;
545 }
546
547 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800548 * Begin building a SurfaceControl.
549 */
550 public Builder() {
551 }
552
553 /**
554 * Construct a new {@link SurfaceControl} with the set parameters. The builder
555 * remains valid.
Robert Carre625fcf2017-09-01 12:36:28 -0700556 */
Robert Carrda1d2422019-03-07 15:54:37 -0800557 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700558 public SurfaceControl build() {
Vishnu Naire86bd982018-11-28 13:23:17 -0800559 if (mWidth < 0 || mHeight < 0) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700560 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800561 "width and height must be positive or unset");
562 }
563 if ((mWidth > 0 || mHeight > 0) && (isColorLayerSet() || isContainerLayerSet())) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700564 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800565 "Only buffer layers can set a valid buffer size.");
Robert Carre625fcf2017-09-01 12:36:28 -0700566 }
Evan Rosky485df202018-12-06 14:11:12 -0800567 return new SurfaceControl(
568 mSession, mName, mWidth, mHeight, mFormat, mFlags, mParent, mMetadata);
Robert Carre625fcf2017-09-01 12:36:28 -0700569 }
570
571 /**
572 * Set a debugging-name for the SurfaceControl.
573 *
574 * @param name A name to identify the Surface in debugging.
575 */
Robert Carrda1d2422019-03-07 15:54:37 -0800576 @NonNull
577 public Builder setName(@NonNull String name) {
Robert Carre625fcf2017-09-01 12:36:28 -0700578 mName = name;
579 return this;
580 }
581
582 /**
583 * Set the initial size of the controlled surface's buffers in pixels.
584 *
585 * @param width The buffer width in pixels.
586 * @param height The buffer height in pixels.
587 */
Robert Carrda1d2422019-03-07 15:54:37 -0800588 @NonNull
Robert Carr76907ee2019-01-11 13:38:19 -0800589 public Builder setBufferSize(@IntRange(from = 0) int width,
590 @IntRange(from = 0) int height) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800591 if (width < 0 || height < 0) {
Robert Carre625fcf2017-09-01 12:36:28 -0700592 throw new IllegalArgumentException(
593 "width and height must be positive");
594 }
595 mWidth = width;
596 mHeight = height;
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000597 // set this as a buffer layer since we are specifying a buffer size.
598 return setFlags(FX_SURFACE_NORMAL, FX_SURFACE_MASK);
599 }
600
601 /**
602 * Set the initial size of the controlled surface's buffers in pixels.
603 */
604 private void unsetBufferSize() {
605 mWidth = 0;
606 mHeight = 0;
Robert Carre625fcf2017-09-01 12:36:28 -0700607 }
608
609 /**
610 * Set the pixel format of the controlled surface's buffers, using constants from
611 * {@link android.graphics.PixelFormat}.
612 */
Robert Carr76907ee2019-01-11 13:38:19 -0800613 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700614 public Builder setFormat(@PixelFormat.Format int format) {
615 mFormat = format;
616 return this;
617 }
618
619 /**
620 * Specify if the app requires a hardware-protected path to
621 * an external display sync. If protected content is enabled, but
622 * such a path is not available, then the controlled Surface will
623 * not be displayed.
624 *
625 * @param protectedContent Whether to require a protected sink.
Robert Carra7827f72019-01-11 12:53:37 -0800626 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700627 */
Robert Carr76907ee2019-01-11 13:38:19 -0800628 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700629 public Builder setProtected(boolean protectedContent) {
630 if (protectedContent) {
631 mFlags |= PROTECTED_APP;
632 } else {
633 mFlags &= ~PROTECTED_APP;
634 }
635 return this;
636 }
637
638 /**
639 * Specify whether the Surface contains secure content. If true, the system
640 * will prevent the surfaces content from being copied by another process. In
641 * particular screenshots and VNC servers will be disabled. This is however
642 * not a complete prevention of readback as {@link #setProtected}.
Robert Carra7827f72019-01-11 12:53:37 -0800643 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700644 */
Robert Carr76907ee2019-01-11 13:38:19 -0800645 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700646 public Builder setSecure(boolean secure) {
647 if (secure) {
648 mFlags |= SECURE;
649 } else {
650 mFlags &= ~SECURE;
651 }
652 return this;
653 }
654
655 /**
656 * Indicates whether the surface must be considered opaque,
657 * even if its pixel format is set to translucent. This can be useful if an
658 * application needs full RGBA 8888 support for instance but will
659 * still draw every pixel opaque.
660 * <p>
661 * This flag only determines whether opacity will be sampled from the alpha channel.
662 * Plane-alpha from calls to setAlpha() can still result in blended composition
663 * regardless of the opaque setting.
664 *
665 * Combined effects are (assuming a buffer format with an alpha channel):
666 * <ul>
667 * <li>OPAQUE + alpha(1.0) == opaque composition
668 * <li>OPAQUE + alpha(0.x) == blended composition
669 * <li>OPAQUE + alpha(0.0) == no composition
670 * <li>!OPAQUE + alpha(1.0) == blended composition
671 * <li>!OPAQUE + alpha(0.x) == blended composition
672 * <li>!OPAQUE + alpha(0.0) == no composition
673 * </ul>
674 * If the underlying buffer lacks an alpha channel, it is as if setOpaque(true)
675 * were set automatically.
676 * @param opaque Whether the Surface is OPAQUE.
677 */
Robert Carr76907ee2019-01-11 13:38:19 -0800678 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700679 public Builder setOpaque(boolean opaque) {
680 if (opaque) {
681 mFlags |= OPAQUE;
682 } else {
683 mFlags &= ~OPAQUE;
684 }
685 return this;
686 }
687
688 /**
Tiger Huang969c6082019-12-24 20:08:57 +0800689 * Set the initial visibility for the SurfaceControl.
690 *
691 * @param hidden Whether the Surface is initially HIDDEN.
692 * @hide
693 */
694 @NonNull
695 public Builder setHidden(boolean hidden) {
696 if (hidden) {
697 mFlags |= HIDDEN;
698 } else {
699 mFlags &= ~HIDDEN;
700 }
701 return this;
702 }
703
704 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700705 * Set a parent surface for our new SurfaceControl.
706 *
707 * Child surfaces are constrained to the onscreen region of their parent.
708 * Furthermore they stack relatively in Z order, and inherit the transformation
709 * of the parent.
710 *
711 * @param parent The parent control.
712 */
Robert Carr76907ee2019-01-11 13:38:19 -0800713 @NonNull
714 public Builder setParent(@Nullable SurfaceControl parent) {
Robert Carre625fcf2017-09-01 12:36:28 -0700715 mParent = parent;
716 return this;
717 }
718
719 /**
Evan Rosky485df202018-12-06 14:11:12 -0800720 * Sets a metadata int.
Robert Carre625fcf2017-09-01 12:36:28 -0700721 *
Evan Rosky485df202018-12-06 14:11:12 -0800722 * @param key metadata key
723 * @param data associated data
Robert Carra7827f72019-01-11 12:53:37 -0800724 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700725 */
Evan Rosky485df202018-12-06 14:11:12 -0800726 public Builder setMetadata(int key, int data) {
727 if (mMetadata == null) {
728 mMetadata = new SparseIntArray();
Robert Carre625fcf2017-09-01 12:36:28 -0700729 }
Evan Rosky485df202018-12-06 14:11:12 -0800730 mMetadata.put(key, data);
Robert Carre625fcf2017-09-01 12:36:28 -0700731 return this;
732 }
733
734 /**
735 * Indicate whether a 'ColorLayer' is to be constructed.
736 *
737 * Color layers will not have an associated BufferQueue and will instead always render a
738 * solid color (that is, solid before plane alpha). Currently that color is black.
739 *
Robert Carra7827f72019-01-11 12:53:37 -0800740 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700741 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000742 public Builder setColorLayer() {
743 unsetBufferSize();
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800744 return setFlags(FX_SURFACE_EFFECT, FX_SURFACE_MASK);
Robert Carre625fcf2017-09-01 12:36:28 -0700745 }
746
Vishnu Naire86bd982018-11-28 13:23:17 -0800747 private boolean isColorLayerSet() {
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800748 return (mFlags & FX_SURFACE_EFFECT) == FX_SURFACE_EFFECT;
Vishnu Naire86bd982018-11-28 13:23:17 -0800749 }
750
Robert Carre625fcf2017-09-01 12:36:28 -0700751 /**
Robert Carr48ec4e02019-07-16 14:28:47 -0700752 * @hide
753 */
754 public Builder setBLASTLayer() {
755 unsetBufferSize();
756 return setFlags(FX_SURFACE_BLAST, FX_SURFACE_MASK);
757 }
758
759 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700760 * Indicates whether a 'ContainerLayer' is to be constructed.
761 *
762 * Container layers will not be rendered in any fashion and instead are used
763 * as a parent of renderable layers.
764 *
Robert Carra7827f72019-01-11 12:53:37 -0800765 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700766 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000767 public Builder setContainerLayer() {
768 unsetBufferSize();
769 return setFlags(FX_SURFACE_CONTAINER, FX_SURFACE_MASK);
Robert Carrb6cd6432018-08-13 13:01:47 -0700770 }
771
Vishnu Naire86bd982018-11-28 13:23:17 -0800772 private boolean isContainerLayerSet() {
773 return (mFlags & FX_SURFACE_CONTAINER) == FX_SURFACE_CONTAINER;
774 }
775
Robert Carrb6cd6432018-08-13 13:01:47 -0700776 /**
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000777 * Set 'Surface creation flags' such as {@link #HIDDEN}, {@link #SECURE}.
Robert Carre625fcf2017-09-01 12:36:28 -0700778 *
779 * TODO: Finish conversion to individual builder methods?
780 * @param flags The combined flags
Robert Carra7827f72019-01-11 12:53:37 -0800781 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700782 */
783 public Builder setFlags(int flags) {
784 mFlags = flags;
785 return this;
786 }
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000787
788 private Builder setFlags(int flags, int mask) {
789 mFlags = (mFlags & ~mask) | flags;
790 return this;
791 }
Robert Carre625fcf2017-09-01 12:36:28 -0700792 }
793
794 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800795 * Create a surface with a name.
Andy McFadden314405b2014-01-29 17:18:05 -0800796 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800797 * The surface creation flags specify what kind of surface to create and
798 * certain options such as whether the surface can be assumed to be opaque
799 * and whether it should be initially hidden. Surfaces should always be
800 * created with the {@link #HIDDEN} flag set to ensure that they are not
801 * made visible prematurely before all of the surface's properties have been
802 * configured.
Andy McFadden314405b2014-01-29 17:18:05 -0800803 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800804 * Good practice is to first create the surface with the {@link #HIDDEN} flag
805 * specified, open a transaction, set the surface layer, layer stack, alpha,
chaviw619da692019-06-10 15:39:40 -0700806 * and position, call {@link Transaction#show(SurfaceControl)} if appropriate, and close the
807 * transaction.
Vishnu Naird454442d2018-11-13 13:51:01 -0800808 * <p>
809 * Bounds of the surface is determined by its crop and its buffer size. If the
810 * surface has no buffer or crop, the surface is boundless and only constrained
811 * by the size of its parent bounds.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800812 *
chaviw619da692019-06-10 15:39:40 -0700813 * @param session The surface session, must not be null.
814 * @param name The surface name, must not be null.
815 * @param w The surface initial width.
816 * @param h The surface initial height.
Tiger Huang969c6082019-12-24 20:08:57 +0800817 * @param flags The surface creation flags.
Evan Rosky485df202018-12-06 14:11:12 -0800818 * @param metadata Initial metadata.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700819 * @throws throws OutOfResourcesException If the SurfaceControl cannot be created.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800820 */
Robert Carre625fcf2017-09-01 12:36:28 -0700821 private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags,
Evan Rosky485df202018-12-06 14:11:12 -0800822 SurfaceControl parent, SparseIntArray metadata)
Robert Carrb0f39362018-03-14 13:52:25 -0700823 throws OutOfResourcesException, IllegalArgumentException {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800824 if (name == null) {
825 throw new IllegalArgumentException("name must not be null");
826 }
827
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800828 mName = name;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100829 mWidth = w;
830 mHeight = h;
Evan Rosky485df202018-12-06 14:11:12 -0800831 Parcel metaParcel = Parcel.obtain();
832 try {
833 if (metadata != null && metadata.size() > 0) {
834 metaParcel.writeInt(metadata.size());
835 for (int i = 0; i < metadata.size(); ++i) {
836 metaParcel.writeInt(metadata.keyAt(i));
837 metaParcel.writeByteArray(
Evan Roskyc12c9a32019-02-06 13:38:03 -0800838 ByteBuffer.allocate(4).order(ByteOrder.nativeOrder())
839 .putInt(metadata.valueAt(i)).array());
Evan Rosky485df202018-12-06 14:11:12 -0800840 }
Evan Roskyc12c9a32019-02-06 13:38:03 -0800841 metaParcel.setDataPosition(0);
Evan Rosky485df202018-12-06 14:11:12 -0800842 }
843 mNativeObject = nativeCreate(session, name, w, h, format, flags,
844 parent != null ? parent.mNativeObject : 0, metaParcel);
845 } finally {
846 metaParcel.recycle();
847 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800848 if (mNativeObject == 0) {
849 throw new OutOfResourcesException(
850 "Couldn't allocate SurfaceControl native object");
851 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700852
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800853 mCloseGuard.open("release");
854 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700855
Robert Carra7827f72019-01-11 12:53:37 -0800856 /** This is a transfer constructor, useful for transferring a live SurfaceControl native
857 * object to another Java wrapper which could have some different behavior, e.g.
858 * event logging.
859 * @hide
860 */
Robert Carr3b716242016-08-16 16:02:21 -0700861 public SurfaceControl(SurfaceControl other) {
862 mName = other.mName;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100863 mWidth = other.mWidth;
864 mHeight = other.mHeight;
Robert Carr3b716242016-08-16 16:02:21 -0700865 mNativeObject = other.mNativeObject;
866 other.mCloseGuard.close();
867 other.mNativeObject = 0;
868 mCloseGuard.open("release");
869 }
870
Jorim Jaggi06975df2017-12-01 14:52:13 +0100871 private SurfaceControl(Parcel in) {
chaviwbeb7a0c2018-12-05 13:49:54 -0800872 readFromParcel(in);
chaviwbeb7a0c2018-12-05 13:49:54 -0800873 }
874
Robert Carra7827f72019-01-11 12:53:37 -0800875 /**
876 * @hide
877 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800878 public SurfaceControl() {
chaviwbeb7a0c2018-12-05 13:49:54 -0800879 }
880
881 public void readFromParcel(Parcel in) {
882 if (in == null) {
883 throw new IllegalArgumentException("source must not be null");
884 }
885
Jorim Jaggi06975df2017-12-01 14:52:13 +0100886 mName = in.readString();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100887 mWidth = in.readInt();
888 mHeight = in.readInt();
Robert Carr5fea55b2018-12-10 13:05:52 -0800889
Robert Carreb344c72019-01-07 18:35:30 -0800890 long object = 0;
Robert Carr5fea55b2018-12-10 13:05:52 -0800891 if (in.readInt() != 0) {
Robert Carreb344c72019-01-07 18:35:30 -0800892 object = nativeReadFromParcel(in);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100893 }
Robert Carreb344c72019-01-07 18:35:30 -0800894 assignNativeObject(object);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100895 }
896
897 @Override
898 public int describeContents() {
899 return 0;
900 }
901
902 @Override
903 public void writeToParcel(Parcel dest, int flags) {
904 dest.writeString(mName);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100905 dest.writeInt(mWidth);
906 dest.writeInt(mHeight);
Robert Carr5fea55b2018-12-10 13:05:52 -0800907 if (mNativeObject == 0) {
908 dest.writeInt(0);
909 } else {
910 dest.writeInt(1);
911 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100912 nativeWriteToParcel(mNativeObject, dest);
Robert Carr5fea55b2018-12-10 13:05:52 -0800913
914 if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) {
915 release();
916 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100917 }
918
Vishnu Nair04ab4392018-01-10 11:00:06 -0800919 /**
920 * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link
921 * android.view.SurfaceControlProto}.
922 *
923 * @param proto Stream to write the SurfaceControl object to.
924 * @param fieldId Field Id of the SurfaceControl as defined in the parent message.
925 * @hide
926 */
Jeffrey Huangcb782852019-12-05 11:28:11 -0800927 public void dumpDebug(ProtoOutputStream proto, long fieldId) {
Vishnu Nair04ab4392018-01-10 11:00:06 -0800928 final long token = proto.start(fieldId);
929 proto.write(HASH_CODE, System.identityHashCode(this));
930 proto.write(NAME, mName);
931 proto.end(token);
932 }
933
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700934 public static final @android.annotation.NonNull Creator<SurfaceControl> CREATOR
Jorim Jaggi06975df2017-12-01 14:52:13 +0100935 = new Creator<SurfaceControl>() {
936 public SurfaceControl createFromParcel(Parcel in) {
937 return new SurfaceControl(in);
938 }
939
940 public SurfaceControl[] newArray(int size) {
941 return new SurfaceControl[size];
942 }
943 };
944
Robert Carra7827f72019-01-11 12:53:37 -0800945 /**
946 * @hide
947 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800948 @Override
949 protected void finalize() throws Throwable {
950 try {
951 if (mCloseGuard != null) {
952 mCloseGuard.warnIfOpen();
953 }
954 if (mNativeObject != 0) {
955 nativeRelease(mNativeObject);
956 }
957 } finally {
958 super.finalize();
959 }
960 }
961
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800962 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800963 * Release the local reference to the server-side surface. The surface
964 * may continue to exist on-screen as long as its parent continues
965 * to exist. To explicitly remove a surface from the screen use
Robert Carr29fe58a2019-03-11 15:25:16 -0700966 * {@link Transaction#reparent} with a null-parent. After release,
967 * {@link #isValid} will return false and other methods will throw
968 * an exception.
Robert Carr76907ee2019-01-11 13:38:19 -0800969 *
970 * Always call release() when you're done with a SurfaceControl.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800971 */
972 public void release() {
973 if (mNativeObject != 0) {
974 nativeRelease(mNativeObject);
975 mNativeObject = 0;
Robert Carr01b0dd52020-02-27 13:25:17 -0800976 mCloseGuard.close();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800977 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800978 }
979
980 /**
Chong Zhang47e36a32016-02-29 16:44:33 -0800981 * Disconnect any client still connected to the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800982 * @hide
Chong Zhang47e36a32016-02-29 16:44:33 -0800983 */
984 public void disconnect() {
985 if (mNativeObject != 0) {
986 nativeDisconnect(mNativeObject);
987 }
988 }
989
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800990 private void checkNotReleased() {
991 if (mNativeObject == 0) throw new NullPointerException(
992 "mNativeObject is null. Have you called release() already?");
993 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700994
Robert Carra7827f72019-01-11 12:53:37 -0800995 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800996 * Check whether this instance points to a valid layer with the system-compositor. For
Robert Carr29fe58a2019-03-11 15:25:16 -0700997 * example this may be false if construction failed, or the layer was released
998 * ({@link #release}).
Robert Carr76907ee2019-01-11 13:38:19 -0800999 *
1000 * @return Whether this SurfaceControl is valid.
Robert Carra7827f72019-01-11 12:53:37 -08001001 */
Robert Carr5fea55b2018-12-10 13:05:52 -08001002 public boolean isValid() {
1003 return mNativeObject != 0;
1004 }
1005
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001006 /*
1007 * set surface parameters.
1008 * needs to be inside open/closeTransaction block
1009 */
1010
Robert Carra7827f72019-01-11 12:53:37 -08001011 /** start a transaction
1012 * @hide
1013 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001014 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001015 public static void openTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -07001016 synchronized (SurfaceControl.class) {
1017 if (sGlobalTransaction == null) {
1018 sGlobalTransaction = new Transaction();
1019 }
1020 synchronized(SurfaceControl.class) {
1021 sTransactionNestCount++;
1022 }
1023 }
1024 }
1025
Robert Carrb1579c82017-09-05 14:54:47 -07001026 /**
1027 * Merge the supplied transaction in to the deprecated "global" transaction.
1028 * This clears the supplied transaction in an identical fashion to {@link Transaction#merge}.
1029 * <p>
1030 * This is a utility for interop with legacy-code and will go away with the Global Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08001031 * @hide
Robert Carrb1579c82017-09-05 14:54:47 -07001032 */
1033 @Deprecated
1034 public static void mergeToGlobalTransaction(Transaction t) {
Robert Carrc5fc4612017-12-05 11:55:40 -08001035 synchronized(SurfaceControl.class) {
Robert Carrb1579c82017-09-05 14:54:47 -07001036 sGlobalTransaction.merge(t);
1037 }
1038 }
1039
chaviwdaaeab02019-03-20 12:25:37 -07001040 /** end a transaction
1041 * @hide
Robert Carra7827f72019-01-11 12:53:37 -08001042 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001043 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001044 public static void closeTransaction() {
chaviwdaaeab02019-03-20 12:25:37 -07001045 synchronized(SurfaceControl.class) {
1046 if (sTransactionNestCount == 0) {
1047 Log.e(TAG,
1048 "Call to SurfaceControl.closeTransaction without matching openTransaction");
1049 } else if (--sTransactionNestCount > 0) {
1050 return;
1051 }
1052 sGlobalTransaction.apply();
1053 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001054 }
1055
Robert Carra7827f72019-01-11 12:53:37 -08001056 /**
1057 * @hide
1058 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001059 public void deferTransactionUntil(SurfaceControl barrier, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -08001060 synchronized(SurfaceControl.class) {
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001061 sGlobalTransaction.deferTransactionUntil(this, barrier, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -08001062 }
1063 }
1064
Robert Carra7827f72019-01-11 12:53:37 -08001065 /**
1066 * @hide
1067 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001068 public void reparentChildren(SurfaceControl newParent) {
Robert Carre13b58e2017-08-31 14:50:44 -07001069 synchronized(SurfaceControl.class) {
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001070 sGlobalTransaction.reparentChildren(this, newParent);
Robert Carre13b58e2017-08-31 14:50:44 -07001071 }
Robert Carrd5c7dd62017-03-08 10:39:30 -08001072 }
1073
Robert Carra7827f72019-01-11 12:53:37 -08001074 /**
1075 * @hide
1076 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001077 public void detachChildren() {
Robert Carre13b58e2017-08-31 14:50:44 -07001078 synchronized(SurfaceControl.class) {
1079 sGlobalTransaction.detachChildren(this);
1080 }
Rob Carr64e516f2015-10-29 00:20:45 +00001081 }
1082
Robert Carra7827f72019-01-11 12:53:37 -08001083 /**
1084 * @hide
1085 */
Robert Carr1ca6a332016-04-11 18:00:43 -07001086 public void setOverrideScalingMode(int scalingMode) {
1087 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001088 synchronized(SurfaceControl.class) {
1089 sGlobalTransaction.setOverrideScalingMode(this, scalingMode);
1090 }
Robert Carr1ca6a332016-04-11 18:00:43 -07001091 }
1092
Robert Carra7827f72019-01-11 12:53:37 -08001093 /**
1094 * @hide
1095 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001096 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001097 public void setLayer(int zorder) {
1098 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001099 synchronized(SurfaceControl.class) {
1100 sGlobalTransaction.setLayer(this, zorder);
1101 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001102 }
1103
Robert Carra7827f72019-01-11 12:53:37 -08001104 /**
1105 * @hide
1106 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001107 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001108 public void setPosition(float x, float y) {
1109 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001110 synchronized(SurfaceControl.class) {
1111 sGlobalTransaction.setPosition(this, x, y);
1112 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001113 }
1114
Robert Carra7827f72019-01-11 12:53:37 -08001115 /**
1116 * @hide
1117 */
Vishnu Naire86bd982018-11-28 13:23:17 -08001118 public void setBufferSize(int w, int h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001119 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001120 synchronized(SurfaceControl.class) {
Vishnu Naire86bd982018-11-28 13:23:17 -08001121 sGlobalTransaction.setBufferSize(this, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07001122 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001123 }
1124
Robert Carra7827f72019-01-11 12:53:37 -08001125 /**
1126 * @hide
1127 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001128 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001129 public void hide() {
1130 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001131 synchronized(SurfaceControl.class) {
1132 sGlobalTransaction.hide(this);
1133 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001134 }
1135
Robert Carra7827f72019-01-11 12:53:37 -08001136 /**
1137 * @hide
1138 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001139 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001140 public void show() {
1141 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001142 synchronized(SurfaceControl.class) {
1143 sGlobalTransaction.show(this);
1144 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001145 }
1146
Robert Carra7827f72019-01-11 12:53:37 -08001147 /**
1148 * @hide
1149 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001150 public void setTransparentRegionHint(Region region) {
1151 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001152 synchronized(SurfaceControl.class) {
1153 sGlobalTransaction.setTransparentRegionHint(this, region);
1154 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001155 }
1156
Robert Carra7827f72019-01-11 12:53:37 -08001157 /**
1158 * @hide
1159 */
Svetoslav1376d602014-03-13 11:17:26 -07001160 public boolean clearContentFrameStats() {
1161 checkNotReleased();
1162 return nativeClearContentFrameStats(mNativeObject);
1163 }
1164
Robert Carra7827f72019-01-11 12:53:37 -08001165 /**
1166 * @hide
1167 */
Svetoslav1376d602014-03-13 11:17:26 -07001168 public boolean getContentFrameStats(WindowContentFrameStats outStats) {
1169 checkNotReleased();
1170 return nativeGetContentFrameStats(mNativeObject, outStats);
1171 }
1172
Robert Carra7827f72019-01-11 12:53:37 -08001173 /**
1174 * @hide
1175 */
Svetoslav1376d602014-03-13 11:17:26 -07001176 public static boolean clearAnimationFrameStats() {
1177 return nativeClearAnimationFrameStats();
1178 }
1179
Robert Carra7827f72019-01-11 12:53:37 -08001180 /**
1181 * @hide
1182 */
Svetoslav1376d602014-03-13 11:17:26 -07001183 public static boolean getAnimationFrameStats(WindowAnimationFrameStats outStats) {
1184 return nativeGetAnimationFrameStats(outStats);
1185 }
1186
Robert Carra7827f72019-01-11 12:53:37 -08001187 /**
1188 * @hide
1189 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001190 public void setAlpha(float alpha) {
1191 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001192 synchronized(SurfaceControl.class) {
1193 sGlobalTransaction.setAlpha(this, alpha);
1194 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001195 }
1196
Robert Carra7827f72019-01-11 12:53:37 -08001197 /**
1198 * @hide
1199 */
Robert Carr0edf18f2017-02-21 20:01:47 -08001200 public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001201 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001202 synchronized(SurfaceControl.class) {
1203 sGlobalTransaction.setMatrix(this, dsdx, dtdx, dtdy, dsdy);
1204 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001205 }
1206
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001207 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08001208 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
1209 * the color can be interpreted in any color space.
1210 * @param agnostic A boolean to indicate whether the surface is color space agnostic
1211 * @hide
1212 */
1213 public void setColorSpaceAgnostic(boolean agnostic) {
1214 checkNotReleased();
1215 synchronized (SurfaceControl.class) {
1216 sGlobalTransaction.setColorSpaceAgnostic(this, agnostic);
1217 }
1218 }
1219
1220 /**
Vishnu Naird454442d2018-11-13 13:51:01 -08001221 * Bounds the surface and its children to the bounds specified. Size of the surface will be
1222 * ignored and only the crop and buffer size will be used to determine the bounds of the
1223 * surface. If no crop is specified and the surface has no buffer, the surface bounds is only
1224 * constrained by the size of its parent bounds.
1225 *
1226 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08001227 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001228 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001229 public void setWindowCrop(Rect crop) {
1230 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001231 synchronized (SurfaceControl.class) {
1232 sGlobalTransaction.setWindowCrop(this, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001233 }
1234 }
1235
Vishnu Naird454442d2018-11-13 13:51:01 -08001236 /**
Robert Carra7827f72019-01-11 12:53:37 -08001237 * @hide
1238 */
Andy McFadden314405b2014-01-29 17:18:05 -08001239 public void setOpaque(boolean isOpaque) {
1240 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001241
1242 synchronized (SurfaceControl.class) {
1243 sGlobalTransaction.setOpaque(this, isOpaque);
Andy McFadden314405b2014-01-29 17:18:05 -08001244 }
1245 }
1246
Robert Carra7827f72019-01-11 12:53:37 -08001247 /**
1248 * @hide
1249 */
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001250 public void setSecure(boolean isSecure) {
1251 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001252
1253 synchronized (SurfaceControl.class) {
1254 sGlobalTransaction.setSecure(this, isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001255 }
1256 }
1257
Robert Carra7827f72019-01-11 12:53:37 -08001258 /**
1259 * @hide
1260 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001261 public int getWidth() {
1262 synchronized (mSizeLock) {
1263 return mWidth;
1264 }
1265 }
1266
Robert Carra7827f72019-01-11 12:53:37 -08001267 /**
1268 * @hide
1269 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001270 public int getHeight() {
1271 synchronized (mSizeLock) {
1272 return mHeight;
1273 }
1274 }
1275
Robert Carre13b58e2017-08-31 14:50:44 -07001276 @Override
1277 public String toString() {
1278 return "Surface(name=" + mName + ")/@0x" +
1279 Integer.toHexString(System.identityHashCode(this));
1280 }
1281
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001282 /**
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001283 * Immutable information about physical display.
1284 *
Robert Carr76907ee2019-01-11 13:38:19 -08001285 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001286 */
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001287 public static final class DisplayInfo {
Dominik Laskowski26290bb2020-01-15 16:09:55 -08001288 public boolean isInternal;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001289 public float density;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001290 public boolean secure;
Marin Shalamanov98af1592019-10-08 10:48:08 +02001291 public DeviceProductInfo deviceProductInfo;
Robert Carra7827f72019-01-11 12:53:37 -08001292
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001293 @Override
1294 public String toString() {
Dominik Laskowski26290bb2020-01-15 16:09:55 -08001295 return "DisplayInfo{isInternal=" + isInternal
1296 + ", density=" + density
Marin Shalamanov98af1592019-10-08 10:48:08 +02001297 + ", secure=" + secure
1298 + ", deviceProductInfo=" + deviceProductInfo + "}";
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001299 }
1300 }
1301
1302 /**
1303 * Configuration supported by physical display.
1304 *
1305 * @hide
1306 */
1307 public static final class DisplayConfig {
Ady Abraham8a5e3912020-02-18 17:28:26 -08001308 /**
1309 * Invalid display config id.
1310 */
1311 public static final int INVALID_DISPLAY_CONFIG_ID = -1;
1312
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001313 public int width;
1314 public int height;
1315 public float xDpi;
1316 public float yDpi;
1317
1318 public float refreshRate;
1319 public long appVsyncOffsetNanos;
1320 public long presentationDeadlineNanos;
1321
Ady Abraham8a5e3912020-02-18 17:28:26 -08001322 /**
1323 * The config group ID this config is associated to.
1324 * Configs in the same group are similar from vendor's perspective and switching between
1325 * configs within the same group can be done seamlessly in most cases.
1326 * @see: android.hardware.graphics.composer@2.4::IComposerClient::Attribute::CONFIG_GROUP
1327 */
1328 public int configGroup;
1329
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001330 @Override
1331 public String toString() {
1332 return "DisplayConfig{width=" + width
1333 + ", height=" + height
1334 + ", xDpi=" + xDpi
1335 + ", yDpi=" + yDpi
1336 + ", refreshRate=" + refreshRate
1337 + ", appVsyncOffsetNanos=" + appVsyncOffsetNanos
Ady Abraham8a5e3912020-02-18 17:28:26 -08001338 + ", presentationDeadlineNanos=" + presentationDeadlineNanos
1339 + ", configGroup=" + configGroup + "}";
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001340 }
1341 }
1342
Robert Carra7827f72019-01-11 12:53:37 -08001343 /**
1344 * @hide
1345 */
Prashant Malanic55929a2014-05-25 01:59:21 -07001346 public static void setDisplayPowerMode(IBinder displayToken, int mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001347 if (displayToken == null) {
1348 throw new IllegalArgumentException("displayToken must not be null");
1349 }
Prashant Malanic55929a2014-05-25 01:59:21 -07001350 nativeSetDisplayPowerMode(displayToken, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001351 }
1352
Robert Carra7827f72019-01-11 12:53:37 -08001353 /**
1354 * @hide
1355 */
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001356 public static SurfaceControl.DisplayInfo getDisplayInfo(IBinder displayToken) {
1357 if (displayToken == null) {
1358 throw new IllegalArgumentException("displayToken must not be null");
1359 }
1360 return nativeGetDisplayInfo(displayToken);
1361 }
1362
1363 /**
1364 * @hide
1365 */
1366 public static SurfaceControl.DisplayConfig[] getDisplayConfigs(IBinder displayToken) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001367 if (displayToken == null) {
1368 throw new IllegalArgumentException("displayToken must not be null");
1369 }
Dan Stoza00101052014-05-02 15:23:40 -07001370 return nativeGetDisplayConfigs(displayToken);
1371 }
1372
Robert Carra7827f72019-01-11 12:53:37 -08001373 /**
1374 * @hide
1375 */
Dan Stoza00101052014-05-02 15:23:40 -07001376 public static int getActiveConfig(IBinder displayToken) {
1377 if (displayToken == null) {
1378 throw new IllegalArgumentException("displayToken must not be null");
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001379 }
Dan Stoza00101052014-05-02 15:23:40 -07001380 return nativeGetActiveConfig(displayToken);
1381 }
1382
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001383 /**
1384 * @hide
1385 */
1386 public static DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
1387 IBinder displayToken) {
1388 if (displayToken == null) {
1389 throw new IllegalArgumentException("displayToken must not be null");
1390 }
1391 return nativeGetDisplayedContentSamplingAttributes(displayToken);
1392 }
1393
1394 /**
1395 * @hide
1396 */
1397 public static boolean setDisplayedContentSamplingEnabled(
1398 IBinder displayToken, boolean enable, int componentMask, int maxFrames) {
1399 if (displayToken == null) {
1400 throw new IllegalArgumentException("displayToken must not be null");
1401 }
1402 final int maxColorComponents = 4;
1403 if ((componentMask >> maxColorComponents) != 0) {
1404 throw new IllegalArgumentException("invalid componentMask when enabling sampling");
1405 }
1406 return nativeSetDisplayedContentSamplingEnabled(
1407 displayToken, enable, componentMask, maxFrames);
1408 }
1409
1410 /**
1411 * @hide
1412 */
1413 public static DisplayedContentSample getDisplayedContentSample(
1414 IBinder displayToken, long maxFrames, long timestamp) {
1415 if (displayToken == null) {
1416 throw new IllegalArgumentException("displayToken must not be null");
1417 }
1418 return nativeGetDisplayedContentSample(displayToken, maxFrames, timestamp);
1419 }
1420
1421
Robert Carra7827f72019-01-11 12:53:37 -08001422 /**
Ana Krulec52f12892019-11-18 03:57:20 -08001423 * Contains information about desired display configuration.
1424 *
1425 * @hide
1426 */
1427 public static final class DesiredDisplayConfigSpecs {
Ana Kruleca74a8642019-11-14 00:51:00 +01001428 public int defaultConfig;
1429 public float minRefreshRate;
1430 public float maxRefreshRate;
Ana Krulec52f12892019-11-18 03:57:20 -08001431
Ana Kruleca74a8642019-11-14 00:51:00 +01001432 public DesiredDisplayConfigSpecs() {}
Ana Krulec52f12892019-11-18 03:57:20 -08001433
Ana Kruleca74a8642019-11-14 00:51:00 +01001434 public DesiredDisplayConfigSpecs(DesiredDisplayConfigSpecs other) {
1435 copyFrom(other);
1436 }
Ana Krulec52f12892019-11-18 03:57:20 -08001437
Ana Krulec52f12892019-11-18 03:57:20 -08001438 public DesiredDisplayConfigSpecs(
Ana Kruleca74a8642019-11-14 00:51:00 +01001439 int defaultConfig, float minRefreshRate, float maxRefreshRate) {
1440 this.defaultConfig = defaultConfig;
1441 this.minRefreshRate = minRefreshRate;
1442 this.maxRefreshRate = maxRefreshRate;
1443 }
1444
1445 @Override
1446 public boolean equals(Object o) {
1447 return o instanceof DesiredDisplayConfigSpecs && equals((DesiredDisplayConfigSpecs) o);
1448 }
1449
1450 /**
1451 * Tests for equality.
1452 */
1453 public boolean equals(DesiredDisplayConfigSpecs other) {
1454 return other != null && defaultConfig == other.defaultConfig
1455 && minRefreshRate == other.minRefreshRate
1456 && maxRefreshRate == other.maxRefreshRate;
1457 }
1458
1459 @Override
1460 public int hashCode() {
1461 return 0; // don't care
1462 }
1463
1464 /**
1465 * Copies the supplied object's values to this object.
1466 */
1467 public void copyFrom(DesiredDisplayConfigSpecs other) {
1468 defaultConfig = other.defaultConfig;
1469 minRefreshRate = other.minRefreshRate;
1470 maxRefreshRate = other.maxRefreshRate;
1471 }
1472
1473 @Override
1474 public String toString() {
1475 return String.format("defaultConfig=%d min=%.0f max=%.0f", defaultConfig,
1476 minRefreshRate, maxRefreshRate);
Ana Krulec52f12892019-11-18 03:57:20 -08001477 }
1478 }
1479
1480 /**
Ady Abraham42f9a2fb2019-02-26 14:13:39 -08001481 * @hide
1482 */
Ana Krulec4f753aa2019-11-14 00:49:39 +01001483 public static boolean setDesiredDisplayConfigSpecs(IBinder displayToken,
Ana Krulec52f12892019-11-18 03:57:20 -08001484 SurfaceControl.DesiredDisplayConfigSpecs desiredDisplayConfigSpecs) {
Ana Krulec4f753aa2019-11-14 00:49:39 +01001485 if (displayToken == null) {
1486 throw new IllegalArgumentException("displayToken must not be null");
1487 }
1488
Ana Krulec52f12892019-11-18 03:57:20 -08001489 return nativeSetDesiredDisplayConfigSpecs(displayToken, desiredDisplayConfigSpecs);
Ana Krulec4f753aa2019-11-14 00:49:39 +01001490 }
1491
1492 /**
1493 * @hide
1494 */
Ana Kruleca74a8642019-11-14 00:51:00 +01001495 public static SurfaceControl.DesiredDisplayConfigSpecs getDesiredDisplayConfigSpecs(
1496 IBinder displayToken) {
1497 if (displayToken == null) {
1498 throw new IllegalArgumentException("displayToken must not be null");
1499 }
1500
1501 return nativeGetDesiredDisplayConfigSpecs(displayToken);
1502 }
1503
1504 /**
1505 * @hide
1506 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001507 public static int[] getDisplayColorModes(IBinder displayToken) {
1508 if (displayToken == null) {
1509 throw new IllegalArgumentException("displayToken must not be null");
1510 }
1511 return nativeGetDisplayColorModes(displayToken);
1512 }
1513
Robert Carra7827f72019-01-11 12:53:37 -08001514 /**
Daniel Solomon10e3b332019-01-20 21:09:11 -08001515 * Color coordinates in CIE1931 XYZ color space
1516 *
1517 * @hide
1518 */
1519 public static final class CieXyz {
1520 /**
1521 * @hide
1522 */
1523 public float X;
1524
1525 /**
1526 * @hide
1527 */
1528 public float Y;
1529
1530 /**
1531 * @hide
1532 */
1533 public float Z;
1534 }
1535
1536 /**
1537 * Contains a display's color primaries
1538 *
1539 * @hide
1540 */
1541 public static final class DisplayPrimaries {
1542 /**
1543 * @hide
1544 */
1545 public CieXyz red;
1546
1547 /**
1548 * @hide
1549 */
1550 public CieXyz green;
1551
1552 /**
1553 * @hide
1554 */
1555 public CieXyz blue;
1556
1557 /**
1558 * @hide
1559 */
1560 public CieXyz white;
1561
1562 /**
1563 * @hide
1564 */
1565 public DisplayPrimaries() {
1566 }
1567 }
1568
1569 /**
1570 * @hide
1571 */
1572 public static SurfaceControl.DisplayPrimaries getDisplayNativePrimaries(
1573 IBinder displayToken) {
1574 if (displayToken == null) {
1575 throw new IllegalArgumentException("displayToken must not be null");
1576 }
1577
1578 return nativeGetDisplayNativePrimaries(displayToken);
1579 }
1580
1581 /**
Robert Carra7827f72019-01-11 12:53:37 -08001582 * @hide
1583 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001584 public static int getActiveColorMode(IBinder displayToken) {
1585 if (displayToken == null) {
1586 throw new IllegalArgumentException("displayToken must not be null");
1587 }
1588 return nativeGetActiveColorMode(displayToken);
1589 }
1590
Robert Carra7827f72019-01-11 12:53:37 -08001591 /**
1592 * @hide
1593 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001594 public static boolean setActiveColorMode(IBinder displayToken, int colorMode) {
1595 if (displayToken == null) {
1596 throw new IllegalArgumentException("displayToken must not be null");
1597 }
1598 return nativeSetActiveColorMode(displayToken, colorMode);
1599 }
1600
Robert Carra7827f72019-01-11 12:53:37 -08001601 /**
Peiyong Lin5f4a5682019-01-17 11:37:07 -08001602 * Returns an array of color spaces with 2 elements. The first color space is the
1603 * default color space and second one is wide color gamut color space.
1604 * @hide
1605 */
1606 public static ColorSpace[] getCompositionColorSpaces() {
1607 int[] dataspaces = nativeGetCompositionDataspaces();
1608 ColorSpace srgb = ColorSpace.get(ColorSpace.Named.SRGB);
1609 ColorSpace[] colorSpaces = { srgb, srgb };
1610 if (dataspaces.length == 2) {
1611 for (int i = 0; i < 2; ++i) {
1612 switch(dataspaces[i]) {
1613 case INTERNAL_DATASPACE_DISPLAY_P3:
1614 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.DISPLAY_P3);
1615 break;
1616 case INTERNAL_DATASPACE_SCRGB:
1617 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.EXTENDED_SRGB);
1618 break;
1619 case INTERNAL_DATASPACE_SRGB:
1620 // Other dataspace is not recognized, use SRGB color space instead,
1621 // the default value of the array is already SRGB, thus do nothing.
1622 default:
1623 break;
1624 }
1625 }
1626 }
1627 return colorSpaces;
1628 }
1629
1630 /**
Robert Carra7827f72019-01-11 12:53:37 -08001631 * @hide
1632 */
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001633 public static void setAutoLowLatencyMode(IBinder displayToken, boolean on) {
1634 if (displayToken == null) {
1635 throw new IllegalArgumentException("displayToken must not be null");
1636 }
1637
1638 nativeSetAutoLowLatencyMode(displayToken, on);
1639 }
1640
1641 /**
1642 * @hide
1643 */
1644 public static void setGameContentType(IBinder displayToken, boolean on) {
1645 if (displayToken == null) {
1646 throw new IllegalArgumentException("displayToken must not be null");
1647 }
1648
1649 nativeSetGameContentType(displayToken, on);
1650 }
1651
1652 /**
1653 * @hide
1654 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001655 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001656 public static void setDisplayProjection(IBinder displayToken,
1657 int orientation, Rect layerStackRect, Rect displayRect) {
Robert Carre13b58e2017-08-31 14:50:44 -07001658 synchronized (SurfaceControl.class) {
1659 sGlobalTransaction.setDisplayProjection(displayToken, orientation,
1660 layerStackRect, displayRect);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001661 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001662 }
1663
Robert Carra7827f72019-01-11 12:53:37 -08001664 /**
1665 * @hide
1666 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001667 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001668 public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -07001669 synchronized (SurfaceControl.class) {
1670 sGlobalTransaction.setDisplayLayerStack(displayToken, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001671 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001672 }
1673
Robert Carra7827f72019-01-11 12:53:37 -08001674 /**
1675 * @hide
1676 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001677 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001678 public static void setDisplaySurface(IBinder displayToken, Surface surface) {
Robert Carre13b58e2017-08-31 14:50:44 -07001679 synchronized (SurfaceControl.class) {
1680 sGlobalTransaction.setDisplaySurface(displayToken, surface);
Jeff Brownfc0ebd72013-04-30 16:33:00 -07001681 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001682 }
1683
Robert Carra7827f72019-01-11 12:53:37 -08001684 /**
1685 * @hide
1686 */
Michael Wright01e840f2014-06-26 16:03:25 -07001687 public static void setDisplaySize(IBinder displayToken, int width, int height) {
Robert Carre13b58e2017-08-31 14:50:44 -07001688 synchronized (SurfaceControl.class) {
1689 sGlobalTransaction.setDisplaySize(displayToken, width, height);
Michael Wright01e840f2014-06-26 16:03:25 -07001690 }
Michael Wright01e840f2014-06-26 16:03:25 -07001691 }
1692
Robert Carra7827f72019-01-11 12:53:37 -08001693 /**
1694 * @hide
1695 */
Michael Wright9ff94c02016-03-30 18:05:40 -07001696 public static Display.HdrCapabilities getHdrCapabilities(IBinder displayToken) {
1697 if (displayToken == null) {
1698 throw new IllegalArgumentException("displayToken must not be null");
1699 }
1700 return nativeGetHdrCapabilities(displayToken);
1701 }
1702
Robert Carra7827f72019-01-11 12:53:37 -08001703 /**
1704 * @hide
1705 */
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001706 public static boolean getAutoLowLatencyModeSupport(IBinder displayToken) {
1707 if (displayToken == null) {
1708 throw new IllegalArgumentException("displayToken must not be null");
1709 }
1710
1711 return nativeGetAutoLowLatencyModeSupport(displayToken);
1712 }
1713
1714 /**
1715 * @hide
1716 */
1717 public static boolean getGameContentTypeSupport(IBinder displayToken) {
1718 if (displayToken == null) {
1719 throw new IllegalArgumentException("displayToken must not be null");
1720 }
1721
1722 return nativeGetGameContentTypeSupport(displayToken);
1723 }
1724
1725 /**
1726 * @hide
1727 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001728 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001729 public static IBinder createDisplay(String name, boolean secure) {
1730 if (name == null) {
1731 throw new IllegalArgumentException("name must not be null");
1732 }
1733 return nativeCreateDisplay(name, secure);
1734 }
1735
Robert Carra7827f72019-01-11 12:53:37 -08001736 /**
1737 * @hide
1738 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001739 @UnsupportedAppUsage
Jesse Hall6a6bc212013-08-08 12:15:03 -07001740 public static void destroyDisplay(IBinder displayToken) {
1741 if (displayToken == null) {
1742 throw new IllegalArgumentException("displayToken must not be null");
1743 }
1744 nativeDestroyDisplay(displayToken);
1745 }
1746
Robert Carra7827f72019-01-11 12:53:37 -08001747 /**
1748 * @hide
1749 */
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001750 public static long[] getPhysicalDisplayIds() {
1751 return nativeGetPhysicalDisplayIds();
1752 }
1753
1754 /**
1755 * @hide
1756 */
1757 public static IBinder getPhysicalDisplayToken(long physicalDisplayId) {
1758 return nativeGetPhysicalDisplayToken(physicalDisplayId);
1759 }
1760
1761 /**
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001762 * TODO(b/116025192): Remove this stopgap once framework is display-agnostic.
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001763 *
1764 * @hide
1765 */
1766 public static IBinder getInternalDisplayToken() {
1767 final long[] physicalDisplayIds = getPhysicalDisplayIds();
1768 if (physicalDisplayIds.length == 0) {
1769 return null;
1770 }
1771 return getPhysicalDisplayToken(physicalDisplayIds[0]);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001772 }
1773
Mathias Agopian0449a402013-03-01 23:01:51 -08001774 /**
chaviw08520a02018-09-10 16:44:56 -07001775 * @see SurfaceControl#screenshot(IBinder, Surface, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001776 * @hide
Mathias Agopian0449a402013-03-01 23:01:51 -08001777 */
1778 public static void screenshot(IBinder display, Surface consumer) {
chaviw08520a02018-09-10 16:44:56 -07001779 screenshot(display, consumer, new Rect(), 0, 0, false, 0);
1780 }
1781
1782 /**
1783 * Copy the current screen contents into the provided {@link Surface}
1784 *
1785 * @param consumer The {@link Surface} to take the screenshot into.
1786 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001787 * @hide
chaviw08520a02018-09-10 16:44:56 -07001788 */
1789 public static void screenshot(IBinder display, Surface consumer, Rect sourceCrop, int width,
1790 int height, boolean useIdentityTransform, int rotation) {
1791 if (consumer == null) {
1792 throw new IllegalArgumentException("consumer must not be null");
1793 }
1794
Peiyong Line3e5efd2019-03-21 20:59:47 +00001795 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(display, sourceCrop, width,
1796 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07001797 try {
Peiyong Linccc06b62019-06-25 17:31:09 -07001798 consumer.attachAndQueueBufferWithColorSpace(buffer.getGraphicBuffer(),
1799 buffer.getColorSpace());
chaviw08520a02018-09-10 16:44:56 -07001800 } catch (RuntimeException e) {
1801 Log.w(TAG, "Failed to take screenshot - " + e.getMessage());
1802 }
1803 }
1804
1805 /**
1806 * @see SurfaceControl#screenshot(Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001807 * @hide
chaviw08520a02018-09-10 16:44:56 -07001808 */
1809 @UnsupportedAppUsage
1810 public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) {
1811 return screenshot(sourceCrop, width, height, false, rotation);
Mathias Agopian0449a402013-03-01 23:01:51 -08001812 }
1813
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001814 /**
chaviw1da9cd92017-12-06 10:48:11 -08001815 * Copy the current screen contents into a hardware bitmap and return it.
1816 * Note: If you want to modify the Bitmap in software, you will need to copy the Bitmap into
1817 * a software Bitmap using {@link Bitmap#copy(Bitmap.Config, boolean)}
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001818 *
chaviw08520a02018-09-10 16:44:56 -07001819 * CAVEAT: Versions of screenshot that return a {@link Bitmap} can be extremely slow; avoid use
1820 * unless absolutely necessary; prefer the versions that use a {@link Surface} such as
1821 * {@link SurfaceControl#screenshot(IBinder, Surface)} or {@link GraphicBuffer} such as
1822 * {@link SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}.
Mathias Agopian0449a402013-03-01 23:01:51 -08001823 *
chaviw08520a02018-09-10 16:44:56 -07001824 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001825 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001826 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001827 @UnsupportedAppUsage
Dan Stoza9890e3412014-05-22 16:12:54 -07001828 public static Bitmap screenshot(Rect sourceCrop, int width, int height,
chaviw08520a02018-09-10 16:44:56 -07001829 boolean useIdentityTransform, int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001830 // TODO: should take the display as a parameter
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001831 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
1832 if (displayToken == null) {
1833 Log.w(TAG, "Failed to take screenshot because internal display is disconnected");
1834 return null;
1835 }
1836
chaviwa69e0a72017-11-29 17:55:12 -08001837 if (rotation == ROTATION_90 || rotation == ROTATION_270) {
1838 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
1839 }
1840
1841 SurfaceControl.rotateCropForSF(sourceCrop, rotation);
Peiyong Line3e5efd2019-03-21 20:59:47 +00001842 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(displayToken, sourceCrop, width,
1843 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07001844
1845 if (buffer == null) {
1846 Log.w(TAG, "Failed to take screenshot");
1847 return null;
1848 }
Sunny Goyal62915b22019-04-10 12:28:47 -07001849 return Bitmap.wrapHardwareBuffer(buffer.getGraphicBuffer(), buffer.getColorSpace());
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001850 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001851
chaviw08520a02018-09-10 16:44:56 -07001852 /**
1853 * Captures all the surfaces in a display and returns a {@link GraphicBuffer} with the content.
1854 *
1855 * @param display The display to take the screenshot of.
1856 * @param sourceCrop The portion of the screen to capture into the Bitmap; caller may
1857 * pass in 'new Rect()' if no cropping is desired.
1858 * @param width The desired width of the returned bitmap; the raw screen will be
1859 * scaled down to this size; caller may pass in 0 if no scaling is
1860 * desired.
1861 * @param height The desired height of the returned bitmap; the raw screen will
1862 * be scaled down to this size; caller may pass in 0 if no scaling
1863 * is desired.
1864 * @param useIdentityTransform Replace whatever transformation (rotation, scaling, translation)
1865 * the surface layers are currently using with the identity
1866 * transformation while taking the screenshot.
1867 * @param rotation Apply a custom clockwise rotation to the screenshot, i.e.
1868 * Surface.ROTATION_0,90,180,270. SurfaceFlinger will always take
1869 * screenshots in its native portrait orientation by default, so
1870 * this is useful for returning screenshots that are independent of
1871 * device orientation.
1872 * @return Returns a GraphicBuffer that contains the captured content.
Robert Carra7827f72019-01-11 12:53:37 -08001873 * @hide
chaviw08520a02018-09-10 16:44:56 -07001874 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00001875 public static ScreenshotGraphicBuffer screenshotToBuffer(IBinder display, Rect sourceCrop,
1876 int width, int height, boolean useIdentityTransform, int rotation) {
Mathias Agopian0449a402013-03-01 23:01:51 -08001877 if (display == null) {
1878 throw new IllegalArgumentException("displayToken must not be null");
1879 }
chaviw08520a02018-09-10 16:44:56 -07001880
Robert Carr5c52b132019-02-15 15:48:11 -08001881 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1882 false /* captureSecureLayers */);
1883 }
1884
1885 /**
1886 * Like screenshotToBuffer, but if the caller is AID_SYSTEM, allows
1887 * for the capture of secure layers. This is used for the screen rotation
1888 * animation where the system server takes screenshots but does
1889 * not persist them or allow them to leave the server. However in other
1890 * cases in the system server, we mostly want to omit secure layers
1891 * like when we take a screenshot on behalf of the assistant.
1892 *
1893 * @hide
1894 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00001895 public static ScreenshotGraphicBuffer screenshotToBufferWithSecureLayersUnsafe(IBinder display,
Robert Carr5c52b132019-02-15 15:48:11 -08001896 Rect sourceCrop, int width, int height, boolean useIdentityTransform,
1897 int rotation) {
1898 if (display == null) {
1899 throw new IllegalArgumentException("displayToken must not be null");
1900 }
1901
1902 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1903 true /* captureSecureLayers */);
Mathias Agopian0449a402013-03-01 23:01:51 -08001904 }
Robert Carre13b58e2017-08-31 14:50:44 -07001905
chaviwa69e0a72017-11-29 17:55:12 -08001906 private static void rotateCropForSF(Rect crop, int rot) {
1907 if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270) {
1908 int tmp = crop.top;
1909 crop.top = crop.left;
1910 crop.left = tmp;
1911 tmp = crop.right;
1912 crop.right = crop.bottom;
1913 crop.bottom = tmp;
1914 }
1915 }
1916
chaviw1cda84c2017-10-23 16:47:10 -07001917 /**
chaviw1da9cd92017-12-06 10:48:11 -08001918 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
chaviw1cda84c2017-10-23 16:47:10 -07001919 *
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001920 * @param layer The root layer to capture.
chaviwfbe47df2017-11-10 16:14:49 -08001921 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
Vishnu Naira8bff972019-12-03 10:18:10 -08001922 * Rect()' or null if no cropping is desired. If the root layer does not
1923 * have a buffer or a crop set, then a non-empty source crop must be
1924 * specified.
chaviwfbe47df2017-11-10 16:14:49 -08001925 * @param frameScale The desired scale of the returned buffer; the raw
1926 * screen will be scaled up/down.
Chavi Weingartend7ec64c2017-11-30 01:52:01 +00001927 *
1928 * @return Returns a GraphicBuffer that contains the layer capture.
Robert Carra7827f72019-01-11 12:53:37 -08001929 * @hide
chaviw1cda84c2017-10-23 16:47:10 -07001930 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001931 public static ScreenshotGraphicBuffer captureLayers(SurfaceControl layer, Rect sourceCrop,
chaviwfbe47df2017-11-10 16:14:49 -08001932 float frameScale) {
Chiawei Wang02202d12019-01-03 18:12:13 +08001933 return captureLayers(layer, sourceCrop, frameScale, PixelFormat.RGBA_8888);
1934 }
1935
1936 /**
1937 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
1938 *
1939 * @param layer The root layer to capture.
1940 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
Vishnu Naira8bff972019-12-03 10:18:10 -08001941 * Rect()' or null if no cropping is desired. If the root layer does not
1942 * have a buffer or a crop set, then a non-empty source crop must be
1943 * specified.
Chiawei Wang02202d12019-01-03 18:12:13 +08001944 * @param frameScale The desired scale of the returned buffer; the raw
1945 * screen will be scaled up/down.
1946 * @param format The desired pixel format of the returned buffer.
1947 *
1948 * @return Returns a GraphicBuffer that contains the layer capture.
1949 * @hide
1950 */
1951 public static ScreenshotGraphicBuffer captureLayers(SurfaceControl layer, Rect sourceCrop,
1952 float frameScale, int format) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07001953 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
Chiawei Wang02202d12019-01-03 18:12:13 +08001954 return nativeCaptureLayers(displayToken, layer.mNativeObject, sourceCrop, frameScale, null,
1955 format);
Robert Carrffcdc512019-04-02 11:51:11 -07001956 }
1957
1958 /**
1959 * Like {@link captureLayers} but with an array of layer handles to exclude.
1960 * @hide
1961 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001962 public static ScreenshotGraphicBuffer captureLayersExcluding(SurfaceControl layer,
Robert Carr689a1b02020-03-12 15:33:51 -07001963 Rect sourceCrop, float frameScale, int format, SurfaceControl[] exclude) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07001964 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001965 long[] nativeExcludeObjects = new long[exclude.length];
1966 for (int i = 0; i < exclude.length; i++) {
1967 nativeExcludeObjects[i] = exclude[i].mNativeObject;
1968 }
1969 return nativeCaptureLayers(displayToken, layer.mNativeObject, sourceCrop, frameScale,
Chiawei Wang02202d12019-01-03 18:12:13 +08001970 nativeExcludeObjects, PixelFormat.RGBA_8888);
chaviw1cda84c2017-10-23 16:47:10 -07001971 }
1972
Robert Carra7827f72019-01-11 12:53:37 -08001973 /**
Peiyong Lin0ddb7d42019-01-16 13:25:09 -08001974 * Returns whether protected content is supported in GPU composition.
1975 * @hide
1976 */
1977 public static boolean getProtectedContentSupport() {
1978 return nativeGetProtectedContentSupport();
1979 }
1980
1981 /**
Dan Gittik832b4972019-02-13 18:17:47 +00001982 * Returns whether brightness operations are supported on a display.
1983 *
1984 * @param displayToken
1985 * The token for the display.
1986 *
1987 * @return Whether brightness operations are supported on the display.
1988 *
1989 * @hide
1990 */
1991 public static boolean getDisplayBrightnessSupport(IBinder displayToken) {
1992 return nativeGetDisplayBrightnessSupport(displayToken);
1993 }
1994
1995 /**
1996 * Sets the brightness of a display.
1997 *
1998 * @param displayToken
1999 * The token for the display whose brightness is set.
2000 * @param brightness
2001 * A number between 0.0f (minimum brightness) and 1.0f (maximum brightness), or -1.0f to
2002 * turn the backlight off.
2003 *
2004 * @return Whether the method succeeded or not.
2005 *
2006 * @throws IllegalArgumentException if:
2007 * - displayToken is null;
2008 * - brightness is NaN or greater than 1.0f.
2009 *
2010 * @hide
2011 */
2012 public static boolean setDisplayBrightness(IBinder displayToken, float brightness) {
2013 Objects.requireNonNull(displayToken);
2014 if (Float.isNaN(brightness) || brightness > 1.0f
2015 || (brightness < 0.0f && brightness != -1.0f)) {
2016 throw new IllegalArgumentException("brightness must be a number between 0.0f and 1.0f,"
2017 + " or -1 to turn the backlight off.");
2018 }
2019 return nativeSetDisplayBrightness(displayToken, brightness);
2020 }
2021
chaviwa51724f2019-09-19 09:50:11 -07002022 /**
2023 * Creates a mirrored hierarchy for the mirrorOf {@link SurfaceControl}
2024 *
2025 * Real Hierarchy Mirror
2026 * SC (value that's returned)
2027 * |
2028 * A A'
2029 * | |
2030 * B B'
2031 *
2032 * @param mirrorOf The root of the hierarchy that should be mirrored.
2033 * @return A SurfaceControl that's the parent of the root of the mirrored hierarchy.
2034 *
2035 * @hide
2036 */
2037 public static SurfaceControl mirrorSurface(SurfaceControl mirrorOf) {
2038 long nativeObj = nativeMirrorSurface(mirrorOf.mNativeObject);
2039 SurfaceControl sc = new SurfaceControl();
2040 sc.assignNativeObject(nativeObj);
2041 return sc;
2042 }
2043
Vishnu Nair4a067c52019-11-19 14:25:56 -08002044 private static void validateColorArg(@Size(4) float[] color) {
2045 final String msg = "Color must be specified as a float array with"
2046 + " four values to represent r, g, b, a in range [0..1]";
2047 if (color.length != 4) {
2048 throw new IllegalArgumentException(msg);
2049 }
2050 for (float c:color) {
2051 if ((c < 0.f) || (c > 1.f)) {
2052 throw new IllegalArgumentException(msg);
2053 }
2054 }
2055 }
2056
2057 /**
2058 * Sets the global configuration for all the shadows drawn by SurfaceFlinger. Shadow follows
2059 * material design guidelines.
2060 *
2061 * @param ambientColor Color applied to the ambient shadow. The alpha is premultiplied. A
2062 * float array with four values to represent r, g, b, a in range [0..1]
2063 * @param spotColor Color applied to the spot shadow. The alpha is premultiplied. The position
2064 * of the spot shadow depends on the light position. A float array with
2065 * four values to represent r, g, b, a in range [0..1]
2066 * @param lightPosY Y axis position of the light used to cast the spot shadow in pixels.
2067 * @param lightPosZ Z axis position of the light used to cast the spot shadow in pixels. The X
2068 * axis position is set to the display width / 2.
2069 * @param lightRadius Radius of the light casting the shadow in pixels.
2070 *[
2071 * @hide
2072 */
2073 public static void setGlobalShadowSettings(@Size(4) float[] ambientColor,
2074 @Size(4) float[] spotColor, float lightPosY, float lightPosZ, float lightRadius) {
2075 validateColorArg(ambientColor);
2076 validateColorArg(spotColor);
2077 nativeSetGlobalShadowSettings(ambientColor, spotColor, lightPosY, lightPosZ, lightRadius);
2078 }
2079
Vishnu Nair629df2b2019-06-11 16:03:38 -07002080 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002081 * An atomic set of changes to a set of SurfaceControl.
Robert Carra7827f72019-01-11 12:53:37 -08002082 */
Vishnu Nair629df2b2019-06-11 16:03:38 -07002083 public static class Transaction implements Closeable, Parcelable {
Robert Carr76907ee2019-01-11 13:38:19 -08002084 /**
2085 * @hide
2086 */
Robert Carre13b58e2017-08-31 14:50:44 -07002087 public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
2088 Transaction.class.getClassLoader(),
2089 nativeGetNativeTransactionFinalizer(), 512);
Robert Carr48ec4e02019-07-16 14:28:47 -07002090 /**
2091 * @hide
2092 */
2093 public long mNativeObject;
Robert Carre13b58e2017-08-31 14:50:44 -07002094
Jorim Jaggia5e10572017-11-15 14:36:26 +01002095 private final ArrayMap<SurfaceControl, Point> mResizedSurfaces = new ArrayMap<>();
Robert Carre13b58e2017-08-31 14:50:44 -07002096 Runnable mFreeNativeResources;
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002097 private static final float[] INVALID_COLOR = {-1, -1, -1};
Robert Carre13b58e2017-08-31 14:50:44 -07002098
Robert Carra7827f72019-01-11 12:53:37 -08002099 /**
Robert Carrfee0b822019-12-17 23:56:44 -08002100 * @hide
2101 */
2102 protected void checkPreconditions(SurfaceControl sc) {
2103 sc.checkNotReleased();
2104 }
2105
2106 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002107 * Open a new transaction object. The transaction may be filed with commands to
2108 * manipulate {@link SurfaceControl} instances, and then applied atomically with
2109 * {@link #apply}. Eventually the user should invoke {@link #close}, when the object
2110 * is no longer required. Note however that re-using a transaction after a call to apply
2111 * is allowed as a convenience.
Robert Carra7827f72019-01-11 12:53:37 -08002112 */
Robert Carre13b58e2017-08-31 14:50:44 -07002113 public Transaction() {
2114 mNativeObject = nativeCreateTransaction();
2115 mFreeNativeResources
2116 = sRegistry.registerNativeAllocation(this, mNativeObject);
2117 }
2118
Vishnu Nair629df2b2019-06-11 16:03:38 -07002119 private Transaction(Parcel in) {
2120 readFromParcel(in);
2121 }
2122
Robert Carre13b58e2017-08-31 14:50:44 -07002123 /**
2124 * Apply the transaction, clearing it's state, and making it usable
2125 * as a new transaction.
2126 */
2127 public void apply() {
2128 apply(false);
2129 }
2130
2131 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002132 * Release the native transaction object, without applying it.
Robert Carre13b58e2017-08-31 14:50:44 -07002133 */
2134 @Override
2135 public void close() {
2136 mFreeNativeResources.run();
2137 mNativeObject = 0;
2138 }
2139
2140 /**
2141 * Jankier version of apply. Avoid use (b/28068298).
Robert Carra7827f72019-01-11 12:53:37 -08002142 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002143 */
2144 public void apply(boolean sync) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002145 applyResizedSurfaces();
Robert Carre13b58e2017-08-31 14:50:44 -07002146 nativeApplyTransaction(mNativeObject, sync);
2147 }
2148
Jorim Jaggia5e10572017-11-15 14:36:26 +01002149 private void applyResizedSurfaces() {
2150 for (int i = mResizedSurfaces.size() - 1; i >= 0; i--) {
2151 final Point size = mResizedSurfaces.valueAt(i);
2152 final SurfaceControl surfaceControl = mResizedSurfaces.keyAt(i);
2153 synchronized (surfaceControl.mSizeLock) {
2154 surfaceControl.mWidth = size.x;
2155 surfaceControl.mHeight = size.y;
2156 }
2157 }
2158 mResizedSurfaces.clear();
2159 }
2160
Robert Carra7827f72019-01-11 12:53:37 -08002161 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002162 * Toggle the visibility of a given Layer and it's sub-tree.
2163 *
2164 * @param sc The SurfaceControl for which to set the visibility
2165 * @param visible The new visibility
2166 * @return This transaction object.
2167 */
2168 @NonNull
2169 public Transaction setVisibility(@NonNull SurfaceControl sc, boolean visible) {
Robert Carrfee0b822019-12-17 23:56:44 -08002170 checkPreconditions(sc);
Robert Carr76907ee2019-01-11 13:38:19 -08002171 if (visible) {
2172 return show(sc);
2173 } else {
2174 return hide(sc);
2175 }
2176 }
2177
2178 /**
Ana Krulecfea97172019-11-02 23:11:02 +01002179 * This information is passed to SurfaceFlinger to decide which window should have a
2180 * priority when deciding about the refresh rate of the display. All windows have the
2181 * lowest priority by default.
2182 * @hide
2183 */
2184 @NonNull
2185 public Transaction setFrameRateSelectionPriority(@NonNull SurfaceControl sc, int priority) {
2186 sc.checkNotReleased();
2187 nativeSetFrameRateSelectionPriority(mNativeObject, sc.mNativeObject, priority);
2188 return this;
2189 }
2190
2191 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002192 * Request that a given surface and it's sub-tree be shown.
2193 *
2194 * @param sc The surface to show.
2195 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002196 * @hide
2197 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002198 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002199 public Transaction show(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002200 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002201 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_HIDDEN);
2202 return this;
2203 }
2204
Robert Carra7827f72019-01-11 12:53:37 -08002205 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002206 * Request that a given surface and it's sub-tree be hidden.
2207 *
2208 * @param sc The surface to hidden.
2209 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002210 * @hide
2211 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002212 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002213 public Transaction hide(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002214 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002215 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_HIDDEN, SURFACE_HIDDEN);
2216 return this;
2217 }
2218
Robert Carra7827f72019-01-11 12:53:37 -08002219 /**
2220 * @hide
2221 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002222 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002223 public Transaction setPosition(SurfaceControl sc, float x, float y) {
Robert Carrfee0b822019-12-17 23:56:44 -08002224 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002225 nativeSetPosition(mNativeObject, sc.mNativeObject, x, y);
2226 return this;
2227 }
2228
Robert Carra7827f72019-01-11 12:53:37 -08002229 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002230 * Set the default buffer size for the SurfaceControl, if there is a
2231 * {@link Surface} associated with the control, then
Robert Carr76907ee2019-01-11 13:38:19 -08002232 * this will be the default size for buffers dequeued from it.
2233 * @param sc The surface to set the buffer size for.
2234 * @param w The default width
2235 * @param h The default height
2236 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002237 */
Robert Carr76907ee2019-01-11 13:38:19 -08002238 @NonNull
2239 public Transaction setBufferSize(@NonNull SurfaceControl sc,
2240 @IntRange(from = 0) int w, @IntRange(from = 0) int h) {
Robert Carrfee0b822019-12-17 23:56:44 -08002241 checkPreconditions(sc);
Jorim Jaggia5e10572017-11-15 14:36:26 +01002242 mResizedSurfaces.put(sc, new Point(w, h));
Jorim Jaggidfc27372017-10-27 17:47:49 +02002243 nativeSetSize(mNativeObject, sc.mNativeObject, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07002244 return this;
2245 }
2246
Robert Carra7827f72019-01-11 12:53:37 -08002247 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002248 * Set the Z-order for a given SurfaceControl, relative to it's siblings.
2249 * If two siblings share the same Z order the ordering is undefined. Surfaces
2250 * with a negative Z will be placed below the parent surface.
2251 *
2252 * @param sc The SurfaceControl to set the Z order on
2253 * @param z The Z-order
2254 * @return This Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002255 */
Robert Carr76907ee2019-01-11 13:38:19 -08002256 @NonNull
2257 public Transaction setLayer(@NonNull SurfaceControl sc,
2258 @IntRange(from = Integer.MIN_VALUE, to = Integer.MAX_VALUE) int z) {
Robert Carrfee0b822019-12-17 23:56:44 -08002259 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002260 nativeSetLayer(mNativeObject, sc.mNativeObject, z);
2261 return this;
2262 }
2263
Robert Carra7827f72019-01-11 12:53:37 -08002264 /**
2265 * @hide
2266 */
Robert Carr77e34942017-10-18 19:13:56 -07002267 public Transaction setRelativeLayer(SurfaceControl sc, SurfaceControl relativeTo, int z) {
Robert Carrfee0b822019-12-17 23:56:44 -08002268 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002269 nativeSetRelativeLayer(mNativeObject, sc.mNativeObject, relativeTo.mNativeObject, z);
Robert Carre13b58e2017-08-31 14:50:44 -07002270 return this;
2271 }
2272
Robert Carra7827f72019-01-11 12:53:37 -08002273 /**
2274 * @hide
2275 */
Robert Carre13b58e2017-08-31 14:50:44 -07002276 public Transaction setTransparentRegionHint(SurfaceControl sc, Region transparentRegion) {
Robert Carrfee0b822019-12-17 23:56:44 -08002277 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002278 nativeSetTransparentRegionHint(mNativeObject,
2279 sc.mNativeObject, transparentRegion);
2280 return this;
2281 }
2282
Robert Carra7827f72019-01-11 12:53:37 -08002283 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002284 * Set the alpha for a given surface. If the alpha is non-zero the SurfaceControl
2285 * will be blended with the Surfaces under it according to the specified ratio.
2286 *
2287 * @param sc The given SurfaceControl.
2288 * @param alpha The alpha to set.
Robert Carra7827f72019-01-11 12:53:37 -08002289 */
Robert Carr76907ee2019-01-11 13:38:19 -08002290 @NonNull
2291 public Transaction setAlpha(@NonNull SurfaceControl sc,
2292 @FloatRange(from = 0.0, to = 1.0) float alpha) {
Robert Carrfee0b822019-12-17 23:56:44 -08002293 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002294 nativeSetAlpha(mNativeObject, sc.mNativeObject, alpha);
2295 return this;
2296 }
2297
Robert Carra7827f72019-01-11 12:53:37 -08002298 /**
2299 * @hide
2300 */
Robert Carr788f5742018-07-30 17:46:45 -07002301 public Transaction setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle) {
Robert Carrfee0b822019-12-17 23:56:44 -08002302 checkPreconditions(sc);
Robert Carr788f5742018-07-30 17:46:45 -07002303 nativeSetInputWindowInfo(mNativeObject, sc.mNativeObject, handle);
2304 return this;
2305 }
2306
chaviw59f532e2018-12-26 15:34:59 -08002307 /**
chaviw319cd0782019-02-14 11:00:23 -08002308 * Waits until any changes to input windows have been sent from SurfaceFlinger to
2309 * InputFlinger before returning.
2310 *
2311 * @hide
2312 */
2313 public Transaction syncInputWindows() {
2314 nativeSyncInputWindows(mNativeObject);
2315 return this;
2316 }
2317
2318 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002319 * Specify how the buffer assosciated with this Surface is mapped in to the
2320 * parent coordinate space. The source frame will be scaled to fit the destination
2321 * frame, after being rotated according to the orientation parameter.
2322 *
2323 * @param sc The SurfaceControl to specify the geometry of
2324 * @param sourceCrop The source rectangle in buffer space. Or null for the entire buffer.
2325 * @param destFrame The destination rectangle in parent space. Or null for the source frame.
2326 * @param orientation The buffer rotation
2327 * @return This transaction object.
2328 */
2329 @NonNull
2330 public Transaction setGeometry(@NonNull SurfaceControl sc, @Nullable Rect sourceCrop,
2331 @Nullable Rect destFrame, @Surface.Rotation int orientation) {
Robert Carrfee0b822019-12-17 23:56:44 -08002332 checkPreconditions(sc);
Robert Carr76907ee2019-01-11 13:38:19 -08002333 nativeSetGeometry(mNativeObject, sc.mNativeObject, sourceCrop, destFrame, orientation);
2334 return this;
2335 }
2336
2337 /**
Robert Carra7827f72019-01-11 12:53:37 -08002338 * @hide
2339 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002340 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002341 public Transaction setMatrix(SurfaceControl sc,
2342 float dsdx, float dtdx, float dtdy, float dsdy) {
Robert Carrfee0b822019-12-17 23:56:44 -08002343 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002344 nativeSetMatrix(mNativeObject, sc.mNativeObject,
2345 dsdx, dtdx, dtdy, dsdy);
2346 return this;
2347 }
2348
Robert Carra7827f72019-01-11 12:53:37 -08002349 /**
chaviw619da692019-06-10 15:39:40 -07002350 * Sets the transform and position of a {@link SurfaceControl} from a 3x3 transformation
2351 * matrix.
2352 *
2353 * @param sc SurfaceControl to set matrix of
2354 * @param matrix The matrix to apply.
2355 * @param float9 An array of 9 floats to be used to extract the values from the matrix.
Robert Carra7827f72019-01-11 12:53:37 -08002356 * @hide
2357 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002358 @UnsupportedAppUsage
Jorim Jaggi21c39a72017-10-20 15:47:51 +02002359 public Transaction setMatrix(SurfaceControl sc, Matrix matrix, float[] float9) {
2360 matrix.getValues(float9);
2361 setMatrix(sc, float9[MSCALE_X], float9[MSKEW_Y],
2362 float9[MSKEW_X], float9[MSCALE_Y]);
2363 setPosition(sc, float9[MTRANS_X], float9[MTRANS_Y]);
2364 return this;
2365 }
2366
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002367 /**
2368 * Sets the color transform for the Surface.
chaviw619da692019-06-10 15:39:40 -07002369 *
2370 * @param sc SurfaceControl to set color transform of
2371 * @param matrix A float array with 9 values represents a 3x3 transform matrix
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002372 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08002373 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002374 */
2375 public Transaction setColorTransform(SurfaceControl sc, @Size(9) float[] matrix,
2376 @Size(3) float[] translation) {
Robert Carrfee0b822019-12-17 23:56:44 -08002377 checkPreconditions(sc);
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002378 nativeSetColorTransform(mNativeObject, sc.mNativeObject, matrix, translation);
2379 return this;
2380 }
2381
Robert Carra7827f72019-01-11 12:53:37 -08002382 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08002383 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
2384 * the color can be interpreted in any color space.
2385 * @param agnostic A boolean to indicate whether the surface is color space agnostic
2386 * @hide
2387 */
2388 public Transaction setColorSpaceAgnostic(SurfaceControl sc, boolean agnostic) {
Robert Carrfee0b822019-12-17 23:56:44 -08002389 checkPreconditions(sc);
Peiyong Linf4f0f642019-03-01 14:36:05 -08002390 nativeSetColorSpaceAgnostic(mNativeObject, sc.mNativeObject, agnostic);
2391 return this;
2392 }
2393
2394 /**
chaviw619da692019-06-10 15:39:40 -07002395 * Bounds the surface and its children to the bounds specified. Size of the surface will be
2396 * ignored and only the crop and buffer size will be used to determine the bounds of the
2397 * surface. If no crop is specified and the surface has no buffer, the surface bounds is
2398 * only constrained by the size of its parent bounds.
2399 *
2400 * @param sc SurfaceControl to set crop of.
2401 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08002402 * @hide
2403 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002404 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002405 public Transaction setWindowCrop(SurfaceControl sc, Rect crop) {
Robert Carrfee0b822019-12-17 23:56:44 -08002406 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002407 if (crop != null) {
2408 nativeSetWindowCrop(mNativeObject, sc.mNativeObject,
2409 crop.left, crop.top, crop.right, crop.bottom);
2410 } else {
2411 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, 0, 0);
2412 }
2413
2414 return this;
2415 }
2416
Robert Carra7827f72019-01-11 12:53:37 -08002417 /**
chaviw619da692019-06-10 15:39:40 -07002418 * Same as {@link Transaction#setWindowCrop(SurfaceControl, Rect)} but sets the crop rect
2419 * top left at 0, 0.
2420 *
2421 * @param sc SurfaceControl to set crop of.
2422 * @param width width of crop rect
2423 * @param height height of crop rect
Robert Carra7827f72019-01-11 12:53:37 -08002424 * @hide
2425 */
Vishnu Naird454442d2018-11-13 13:51:01 -08002426 public Transaction setWindowCrop(SurfaceControl sc, int width, int height) {
Robert Carrfee0b822019-12-17 23:56:44 -08002427 checkPreconditions(sc);
Vishnu Naird454442d2018-11-13 13:51:01 -08002428 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, width, height);
2429 return this;
2430 }
2431
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002432 /**
2433 * Sets the corner radius of a {@link SurfaceControl}.
2434 * @param sc SurfaceControl
2435 * @param cornerRadius Corner radius in pixels.
2436 * @return Itself.
Robert Carra7827f72019-01-11 12:53:37 -08002437 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002438 */
2439 @UnsupportedAppUsage
2440 public Transaction setCornerRadius(SurfaceControl sc, float cornerRadius) {
Robert Carrfee0b822019-12-17 23:56:44 -08002441 checkPreconditions(sc);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002442 nativeSetCornerRadius(mNativeObject, sc.mNativeObject, cornerRadius);
2443
2444 return this;
2445 }
2446
Robert Carra7827f72019-01-11 12:53:37 -08002447 /**
Lucas Dupin991415e2019-11-25 17:48:58 -08002448 * Sets the background blur radius of the {@link SurfaceControl}.
2449 *
2450 * @param sc SurfaceControl.
2451 * @param radius Blur radius in pixels.
2452 * @return itself.
2453 * @hide
2454 */
2455 public Transaction setBackgroundBlurRadius(SurfaceControl sc, int radius) {
2456 checkPreconditions(sc);
2457 nativeSetBackgroundBlurRadius(mNativeObject, sc.mNativeObject, radius);
2458 return this;
2459 }
2460
2461 /**
Robert Carra7827f72019-01-11 12:53:37 -08002462 * @hide
2463 */
Mathew Inwood679c15e2019-02-06 15:36:04 +00002464 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.O)
Robert Carre13b58e2017-08-31 14:50:44 -07002465 public Transaction setLayerStack(SurfaceControl sc, int layerStack) {
Robert Carrfee0b822019-12-17 23:56:44 -08002466 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002467 nativeSetLayerStack(mNativeObject, sc.mNativeObject, layerStack);
2468 return this;
2469 }
2470
Robert Carra7827f72019-01-11 12:53:37 -08002471 /**
2472 * @hide
2473 */
Robert Carr76907ee2019-01-11 13:38:19 -08002474 @UnsupportedAppUsage
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002475 public Transaction deferTransactionUntil(SurfaceControl sc, SurfaceControl barrier,
Jorim Jaggidfc27372017-10-27 17:47:49 +02002476 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002477 if (frameNumber < 0) {
2478 return this;
2479 }
Robert Carrfee0b822019-12-17 23:56:44 -08002480 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002481 nativeDeferTransactionUntil(mNativeObject, sc.mNativeObject, barrier.mNativeObject,
2482 frameNumber);
Robert Carre13b58e2017-08-31 14:50:44 -07002483 return this;
2484 }
2485
Robert Carra7827f72019-01-11 12:53:37 -08002486 /**
2487 * @hide
2488 */
Robert Carrcef9a282020-01-14 09:08:05 -08002489 @Deprecated
Robert Carr76907ee2019-01-11 13:38:19 -08002490 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002491 public Transaction deferTransactionUntilSurface(SurfaceControl sc, Surface barrierSurface,
2492 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002493 if (frameNumber < 0) {
2494 return this;
2495 }
Robert Carrfee0b822019-12-17 23:56:44 -08002496 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002497 nativeDeferTransactionUntilSurface(mNativeObject, sc.mNativeObject,
2498 barrierSurface.mNativeObject, frameNumber);
2499 return this;
2500 }
2501
Robert Carra7827f72019-01-11 12:53:37 -08002502 /**
2503 * @hide
2504 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002505 public Transaction reparentChildren(SurfaceControl sc, SurfaceControl newParent) {
Robert Carrfee0b822019-12-17 23:56:44 -08002506 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002507 nativeReparentChildren(mNativeObject, sc.mNativeObject, newParent.mNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -07002508 return this;
2509 }
2510
Robert Carr10584fa2019-01-14 15:55:19 -08002511 /**
2512 * Re-parents a given layer to a new parent. Children inherit transform (position, scaling)
2513 * crop, visibility, and Z-ordering from their parents, as if the children were pixels within the
2514 * parent Surface.
2515 *
2516 * @param sc The SurfaceControl to reparent
2517 * @param newParent The new parent for the given control.
2518 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002519 */
Robert Carr76907ee2019-01-11 13:38:19 -08002520 @NonNull
2521 public Transaction reparent(@NonNull SurfaceControl sc,
2522 @Nullable SurfaceControl newParent) {
Robert Carrfee0b822019-12-17 23:56:44 -08002523 checkPreconditions(sc);
Robert Carr10584fa2019-01-14 15:55:19 -08002524 long otherObject = 0;
2525 if (newParent != null) {
2526 newParent.checkNotReleased();
2527 otherObject = newParent.mNativeObject;
2528 }
2529 nativeReparent(mNativeObject, sc.mNativeObject, otherObject);
Robert Carre13b58e2017-08-31 14:50:44 -07002530 return this;
2531 }
2532
Robert Carra7827f72019-01-11 12:53:37 -08002533 /**
2534 * @hide
2535 */
Robert Carre13b58e2017-08-31 14:50:44 -07002536 public Transaction detachChildren(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002537 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002538 nativeSeverChildren(mNativeObject, sc.mNativeObject);
2539 return this;
2540 }
2541
Robert Carra7827f72019-01-11 12:53:37 -08002542 /**
2543 * @hide
2544 */
Robert Carre13b58e2017-08-31 14:50:44 -07002545 public Transaction setOverrideScalingMode(SurfaceControl sc, int overrideScalingMode) {
Robert Carrfee0b822019-12-17 23:56:44 -08002546 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002547 nativeSetOverrideScalingMode(mNativeObject, sc.mNativeObject,
2548 overrideScalingMode);
2549 return this;
2550 }
2551
2552 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002553 * Fills the surface with the specified color.
2554 * @param color A float array with three values to represent r, g, b in range [0..1]. An
2555 * invalid color will remove the color fill.
Robert Carra7827f72019-01-11 12:53:37 -08002556 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002557 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002558 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002559 public Transaction setColor(SurfaceControl sc, @Size(3) float[] color) {
Robert Carrfee0b822019-12-17 23:56:44 -08002560 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002561 nativeSetColor(mNativeObject, sc.mNativeObject, color);
2562 return this;
2563 }
2564
2565 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002566 * Removes color fill.
2567 * @hide
2568 */
2569 public Transaction unsetColor(SurfaceControl sc) {
2570 checkPreconditions(sc);
2571 nativeSetColor(mNativeObject, sc.mNativeObject, INVALID_COLOR);
2572 return this;
2573 }
2574
2575 /**
Robert Carre13b58e2017-08-31 14:50:44 -07002576 * Sets the security of the surface. Setting the flag is equivalent to creating the
2577 * Surface with the {@link #SECURE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002578 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002579 */
Robert Carrb1579c82017-09-05 14:54:47 -07002580 public Transaction setSecure(SurfaceControl sc, boolean isSecure) {
Robert Carrfee0b822019-12-17 23:56:44 -08002581 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002582 if (isSecure) {
2583 nativeSetFlags(mNativeObject, sc.mNativeObject, SECURE, SECURE);
2584 } else {
2585 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SECURE);
2586 }
2587 return this;
2588 }
2589
2590 /**
2591 * Sets the opacity of the surface. Setting the flag is equivalent to creating the
2592 * Surface with the {@link #OPAQUE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002593 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002594 */
2595 public Transaction setOpaque(SurfaceControl sc, boolean isOpaque) {
Robert Carrfee0b822019-12-17 23:56:44 -08002596 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002597 if (isOpaque) {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002598 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_OPAQUE, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002599 } else {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002600 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002601 }
2602 return this;
2603 }
2604
Robert Carra7827f72019-01-11 12:53:37 -08002605 /**
2606 * @hide
2607 */
Robert Carre13b58e2017-08-31 14:50:44 -07002608 public Transaction setDisplaySurface(IBinder displayToken, Surface surface) {
2609 if (displayToken == null) {
2610 throw new IllegalArgumentException("displayToken must not be null");
2611 }
2612
2613 if (surface != null) {
2614 synchronized (surface.mLock) {
2615 nativeSetDisplaySurface(mNativeObject, displayToken, surface.mNativeObject);
2616 }
2617 } else {
2618 nativeSetDisplaySurface(mNativeObject, displayToken, 0);
2619 }
2620 return this;
2621 }
2622
Robert Carra7827f72019-01-11 12:53:37 -08002623 /**
2624 * @hide
2625 */
Robert Carre13b58e2017-08-31 14:50:44 -07002626 public Transaction setDisplayLayerStack(IBinder displayToken, int layerStack) {
2627 if (displayToken == null) {
2628 throw new IllegalArgumentException("displayToken must not be null");
2629 }
2630 nativeSetDisplayLayerStack(mNativeObject, displayToken, layerStack);
2631 return this;
2632 }
2633
Robert Carra7827f72019-01-11 12:53:37 -08002634 /**
2635 * @hide
2636 */
Robert Carre13b58e2017-08-31 14:50:44 -07002637 public Transaction setDisplayProjection(IBinder displayToken,
2638 int orientation, Rect layerStackRect, Rect displayRect) {
2639 if (displayToken == null) {
2640 throw new IllegalArgumentException("displayToken must not be null");
2641 }
2642 if (layerStackRect == null) {
2643 throw new IllegalArgumentException("layerStackRect must not be null");
2644 }
2645 if (displayRect == null) {
2646 throw new IllegalArgumentException("displayRect must not be null");
2647 }
2648 nativeSetDisplayProjection(mNativeObject, displayToken, orientation,
2649 layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom,
2650 displayRect.left, displayRect.top, displayRect.right, displayRect.bottom);
2651 return this;
2652 }
2653
Robert Carra7827f72019-01-11 12:53:37 -08002654 /**
2655 * @hide
2656 */
Robert Carre13b58e2017-08-31 14:50:44 -07002657 public Transaction setDisplaySize(IBinder displayToken, int width, int height) {
2658 if (displayToken == null) {
2659 throw new IllegalArgumentException("displayToken must not be null");
2660 }
2661 if (width <= 0 || height <= 0) {
2662 throw new IllegalArgumentException("width and height must be positive");
2663 }
2664
2665 nativeSetDisplaySize(mNativeObject, displayToken, width, height);
2666 return this;
2667 }
2668
Vishnu Nair629df2b2019-06-11 16:03:38 -07002669 /** flag the transaction as an animation
Robert Carra7827f72019-01-11 12:53:37 -08002670 * @hide
2671 */
Robert Carre13b58e2017-08-31 14:50:44 -07002672 public Transaction setAnimationTransaction() {
2673 nativeSetAnimationTransaction(mNativeObject);
2674 return this;
2675 }
Robert Carrb1579c82017-09-05 14:54:47 -07002676
2677 /**
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002678 * Indicate that SurfaceFlinger should wake up earlier than usual as a result of this
2679 * transaction. This should be used when the caller thinks that the scene is complex enough
2680 * that it's likely to hit GL composition, and thus, SurfaceFlinger needs to more time in
2681 * order not to miss frame deadlines.
2682 * <p>
2683 * Corresponds to setting ISurfaceComposer::eEarlyWakeup
Robert Carra7827f72019-01-11 12:53:37 -08002684 * @hide
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002685 */
2686 public Transaction setEarlyWakeup() {
2687 nativeSetEarlyWakeup(mNativeObject);
2688 return this;
2689 }
2690
2691 /**
Evan Rosky485df202018-12-06 14:11:12 -08002692 * Sets an arbitrary piece of metadata on the surface. This is a helper for int data.
2693 * @hide
2694 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002695 public Transaction setMetadata(SurfaceControl sc, int key, int data) {
Evan Rosky485df202018-12-06 14:11:12 -08002696 Parcel parcel = Parcel.obtain();
2697 parcel.writeInt(data);
2698 try {
Evan Roskyb51e2462019-04-03 19:27:18 -07002699 setMetadata(sc, key, parcel);
Evan Rosky485df202018-12-06 14:11:12 -08002700 } finally {
2701 parcel.recycle();
2702 }
2703 return this;
2704 }
2705
2706 /**
2707 * Sets an arbitrary piece of metadata on the surface.
2708 * @hide
2709 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002710 public Transaction setMetadata(SurfaceControl sc, int key, Parcel data) {
Robert Carrfee0b822019-12-17 23:56:44 -08002711 checkPreconditions(sc);
Evan Roskyb51e2462019-04-03 19:27:18 -07002712 nativeSetMetadata(mNativeObject, sc.mNativeObject, key, data);
Evan Rosky485df202018-12-06 14:11:12 -08002713 return this;
2714 }
2715
Vishnu Naird87984d2019-11-06 14:43:22 -08002716 /**
2717 * Draws shadows of length {@code shadowRadius} around the surface {@link SurfaceControl}.
2718 * If the length is 0.0f then the shadows will not be drawn.
2719 *
2720 * Shadows are drawn around the screen bounds, these are the post transformed cropped
2721 * bounds. They can draw over their parent bounds and will be occluded by layers with a
2722 * higher z-order. The shadows will respect the surface's corner radius if the
2723 * rounded corner bounds (transformed source bounds) are within the screen bounds.
2724 *
2725 * A shadow will only be drawn on buffer and color layers. If the radius is applied on a
2726 * container layer, it will be passed down the hierarchy to be applied on buffer and color
2727 * layers but not its children. A scenario where this is useful is when SystemUI animates
2728 * a task by controlling a leash to it, can draw a shadow around the app surface by
2729 * setting a shadow on the leash. This is similar to how rounded corners are set.
2730 *
2731 * @hide
2732 */
2733 public Transaction setShadowRadius(SurfaceControl sc, float shadowRadius) {
Robert Carrfee0b822019-12-17 23:56:44 -08002734 checkPreconditions(sc);
Vishnu Naird87984d2019-11-06 14:43:22 -08002735 nativeSetShadowRadius(mNativeObject, sc.mNativeObject, shadowRadius);
2736 return this;
2737 }
2738
Evan Rosky485df202018-12-06 14:11:12 -08002739 /**
Steven Thomas6cf051e2020-01-14 11:37:21 -08002740 * Sets the intended frame rate for the surface {@link SurfaceControl}.
2741 *
2742 * On devices that are capable of running the display at different refresh rates, the system
2743 * may choose a display refresh rate to better match this surface's frame rate. Usage of
2744 * this API won't directly affect the application's frame production pipeline. However,
2745 * because the system may change the display refresh rate, calls to this function may result
2746 * in changes to Choreographer callback timings, and changes to the time interval at which
2747 * the system releases buffers back to the application.
2748 *
2749 * @param sc The SurfaceControl to specify the frame rate of.
Steven Thomasadd6be12020-01-23 16:35:32 -08002750 * @param frameRate The intended frame rate for this surface, in frames per second. 0 is a
2751 * special value that indicates the app will accept the system's choice for
2752 * the display frame rate, which is the default behavior if this function
2753 * isn't called. The frameRate param does *not* need to be a valid refresh
2754 * rate for this device's display - e.g., it's fine to pass 30fps to a
2755 * device that can only run the display at 60fps.
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08002756 * @param compatibility The frame rate compatibility of this surface. The compatibility
2757 * value may influence the system's choice of display frame rate. See
2758 * the Surface.FRAME_RATE_COMPATIBILITY_* values for more info.
Steven Thomas6cf051e2020-01-14 11:37:21 -08002759 * @return This transaction object.
2760 */
2761 @NonNull
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08002762 public Transaction setFrameRate(@NonNull SurfaceControl sc,
2763 @FloatRange(from = 0.0) float frameRate,
2764 @Surface.FrameRateCompatibility int compatibility) {
Steven Thomas6cf051e2020-01-14 11:37:21 -08002765 checkPreconditions(sc);
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08002766 nativeSetFrameRate(mNativeObject, sc.mNativeObject, frameRate, compatibility);
Steven Thomas6cf051e2020-01-14 11:37:21 -08002767 return this;
2768 }
2769
2770 /**
Robert Carrb1579c82017-09-05 14:54:47 -07002771 * Merge the other transaction into this transaction, clearing the
2772 * other transaction as if it had been applied.
Robert Carr76907ee2019-01-11 13:38:19 -08002773 *
2774 * @param other The transaction to merge in to this one.
2775 * @return This transaction.
Robert Carrb1579c82017-09-05 14:54:47 -07002776 */
Robert Carr76907ee2019-01-11 13:38:19 -08002777 @NonNull
2778 public Transaction merge(@NonNull Transaction other) {
Tiger Huanged6794e2019-05-07 20:07:59 +08002779 if (this == other) {
2780 return this;
2781 }
Jorim Jaggia5e10572017-11-15 14:36:26 +01002782 mResizedSurfaces.putAll(other.mResizedSurfaces);
2783 other.mResizedSurfaces.clear();
Robert Carrb1579c82017-09-05 14:54:47 -07002784 nativeMergeTransaction(mNativeObject, other.mNativeObject);
2785 return this;
2786 }
Robert Carr71200f22019-02-05 09:44:53 -08002787
2788 /**
2789 * Equivalent to reparent with a null parent, in that it removes
2790 * the SurfaceControl from the scene, but it also releases
2791 * the local resources (by calling {@link SurfaceControl#release})
2792 * after this method returns, {@link SurfaceControl#isValid} will return
2793 * false for the argument.
2794 *
2795 * @param sc The surface to remove and release.
2796 * @return This transaction
2797 * @hide
2798 */
2799 @NonNull
2800 public Transaction remove(@NonNull SurfaceControl sc) {
2801 reparent(sc, null);
2802 sc.release();
2803 return this;
2804 }
Vishnu Nair629df2b2019-06-11 16:03:38 -07002805
Vishnu Nairf7645aa2019-06-18 11:14:01 -07002806 /**
2807 * Writes the transaction to parcel, clearing the transaction as if it had been applied so
2808 * it can be used to store future transactions. It's the responsibility of the parcel
2809 * reader to apply the original transaction.
2810 *
2811 * @param dest parcel to write the transaction to
2812 * @param flags
2813 */
Vishnu Nair629df2b2019-06-11 16:03:38 -07002814 @Override
2815 public void writeToParcel(@NonNull Parcel dest, @WriteFlags int flags) {
2816 if (mNativeObject == 0) {
2817 dest.writeInt(0);
2818 } else {
2819 dest.writeInt(1);
2820 }
2821 nativeWriteTransactionToParcel(mNativeObject, dest);
2822 }
2823
2824 private void readFromParcel(Parcel in) {
2825 mNativeObject = 0;
2826 if (in.readInt() != 0) {
2827 mNativeObject = nativeReadTransactionFromParcel(in);
2828 mFreeNativeResources = sRegistry.registerNativeAllocation(this, mNativeObject);
2829 }
2830 }
2831
2832 @Override
2833 public int describeContents() {
2834 return 0;
2835 }
2836
2837 public static final @NonNull Creator<Transaction> CREATOR = new Creator<Transaction>() {
2838 @Override
2839 public Transaction createFromParcel(Parcel in) {
2840 return new Transaction(in);
2841 }
2842 @Override
2843 public Transaction[] newArray(int size) {
2844 return new Transaction[size];
2845 }
2846 };
Robert Carre13b58e2017-08-31 14:50:44 -07002847 }
Robert Carrfee0b822019-12-17 23:56:44 -08002848
2849 /**
2850 * A debugging utility subclass of SurfaceControl.Transaction. At construction
2851 * you can pass in a monitor object, and all the other methods will throw an exception
2852 * if the monitor is not held when they are called.
2853 * @hide
2854 */
2855 public static class LockDebuggingTransaction extends SurfaceControl.Transaction {
2856 Object mMonitor;
2857
2858 public LockDebuggingTransaction(Object o) {
2859 mMonitor = o;
2860 }
2861
2862 @Override
2863 protected void checkPreconditions(SurfaceControl sc) {
2864 super.checkPreconditions(sc);
2865 if (!Thread.holdsLock(mMonitor)) {
2866 throw new RuntimeException(
2867 "Unlocked access to synchronized SurfaceControl.Transaction");
2868 }
2869 }
2870 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08002871}