blob: 3c045f4fdb88e6efad7f038de6e872e439eda805 [file] [log] [blame]
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Jorim Jaggi21c39a72017-10-20 15:47:51 +020019import static android.graphics.Matrix.MSCALE_X;
20import static android.graphics.Matrix.MSCALE_Y;
21import static android.graphics.Matrix.MSKEW_X;
22import static android.graphics.Matrix.MSKEW_Y;
23import static android.graphics.Matrix.MTRANS_X;
24import static android.graphics.Matrix.MTRANS_Y;
Vishnu Nair04ab4392018-01-10 11:00:06 -080025import static android.view.Surface.ROTATION_270;
26import static android.view.Surface.ROTATION_90;
27import static android.view.SurfaceControlProto.HASH_CODE;
28import static android.view.SurfaceControlProto.NAME;
chaviwa69e0a72017-11-29 17:55:12 -080029
Robert Carr76907ee2019-01-11 13:38:19 -080030import android.annotation.FloatRange;
31import android.annotation.IntRange;
32import android.annotation.NonNull;
33import android.annotation.Nullable;
chaviw0dd03f52017-08-25 12:15:26 -070034import android.annotation.Size;
Mathew Inwooda570dee2018-08-17 14:56:00 +010035import android.annotation.UnsupportedAppUsage;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080036import android.graphics.Bitmap;
Peiyong Lin5f4a5682019-01-17 11:37:07 -080037import android.graphics.ColorSpace;
Robert Carr6486d312017-01-09 19:48:29 -080038import android.graphics.GraphicBuffer;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020039import android.graphics.Matrix;
Vishnu Nair04ab4392018-01-10 11:00:06 -080040import android.graphics.PixelFormat;
Jorim Jaggia5e10572017-11-15 14:36:26 +010041import android.graphics.Point;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080042import android.graphics.Rect;
43import android.graphics.Region;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070044import android.hardware.display.DisplayedContentSample;
45import android.hardware.display.DisplayedContentSamplingAttributes;
Mathew Inwood679c15e2019-02-06 15:36:04 +000046import android.os.Build;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080047import android.os.IBinder;
Jorim Jaggi06975df2017-12-01 14:52:13 +010048import android.os.Parcel;
49import android.os.Parcelable;
Jorim Jaggia5e10572017-11-15 14:36:26 +010050import android.util.ArrayMap;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080051import android.util.Log;
Evan Rosky485df202018-12-06 14:11:12 -080052import android.util.SparseIntArray;
Vishnu Nair04ab4392018-01-10 11:00:06 -080053import android.util.proto.ProtoOutputStream;
Igor Murashkina86ab6402013-08-30 12:58:36 -070054import android.view.Surface.OutOfResourcesException;
Jorim Jaggia5e10572017-11-15 14:36:26 +010055
56import com.android.internal.annotations.GuardedBy;
57
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070058import dalvik.system.CloseGuard;
Vishnu Nair04ab4392018-01-10 11:00:06 -080059
Robert Carre625fcf2017-09-01 12:36:28 -070060import libcore.util.NativeAllocationRegistry;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070061
Robert Carre13b58e2017-08-31 14:50:44 -070062import java.io.Closeable;
Evan Rosky485df202018-12-06 14:11:12 -080063import java.nio.ByteBuffer;
Evan Roskyc12c9a32019-02-06 13:38:03 -080064import java.nio.ByteOrder;
Dan Gittik832b4972019-02-13 18:17:47 +000065import java.util.Objects;
Robert Carre13b58e2017-08-31 14:50:44 -070066
Mathias Agopian3866f0d2013-02-11 22:08:48 -080067/**
Robert Carr76907ee2019-01-11 13:38:19 -080068 * Handle to an on-screen Surface managed by the system compositor. The SurfaceControl is
69 * a combination of a buffer source, and metadata about how to display the buffers.
70 * By constructing a {@link Surface} from this SurfaceControl you can submit buffers to be
71 * composited. Using {@link SurfaceControl.Transaction} you can manipulate various
72 * properties of how the buffer will be displayed on-screen. SurfaceControl's are
73 * arranged into a scene-graph like hierarchy, and as such any SurfaceControl may have
74 * a parent. Geometric properties like transform, crop, and Z-ordering will be inherited
75 * from the parent, as if the child were content in the parents buffer stream.
Mathias Agopian3866f0d2013-02-11 22:08:48 -080076 */
Robert Carr76907ee2019-01-11 13:38:19 -080077public final class SurfaceControl implements Parcelable {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080078 private static final String TAG = "SurfaceControl";
Mathias Agopian29479eb2013-02-14 14:36:04 -080079
Ashok Bhat36bef0b2014-01-20 20:08:01 +000080 private static native long nativeCreate(SurfaceSession session, String name,
Evan Rosky485df202018-12-06 14:11:12 -080081 int w, int h, int format, int flags, long parentObject, Parcel metadata)
Mathias Agopian29479eb2013-02-14 14:36:04 -080082 throws OutOfResourcesException;
Jorim Jaggi06975df2017-12-01 14:52:13 +010083 private static native long nativeReadFromParcel(Parcel in);
chaviwbeb7a0c2018-12-05 13:49:54 -080084 private static native long nativeCopyFromSurfaceControl(long nativeObject);
Jorim Jaggi06975df2017-12-01 14:52:13 +010085 private static native void nativeWriteToParcel(long nativeObject, Parcel out);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000086 private static native void nativeRelease(long nativeObject);
87 private static native void nativeDestroy(long nativeObject);
Chong Zhang47e36a32016-02-29 16:44:33 -080088 private static native void nativeDisconnect(long nativeObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080089
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,
94 IBinder layerHandleToken, Rect sourceCrop, float frameScale, IBinder[] excludeLayers);
Mathias Agopian29479eb2013-02-14 14:36:04 -080095
Robert Carre13b58e2017-08-31 14:50:44 -070096 private static native long nativeCreateTransaction();
97 private static native long nativeGetNativeTransactionFinalizer();
98 private static native void nativeApplyTransaction(long transactionObj, boolean sync);
Robert Carrb1579c82017-09-05 14:54:47 -070099 private static native void nativeMergeTransaction(long transactionObj,
100 long otherTransactionObj);
Robert Carre13b58e2017-08-31 14:50:44 -0700101 private static native void nativeSetAnimationTransaction(long transactionObj);
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100102 private static native void nativeSetEarlyWakeup(long transactionObj);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800103
Robert Carre13b58e2017-08-31 14:50:44 -0700104 private static native void nativeSetLayer(long transactionObj, long nativeObject, int zorder);
105 private static native void nativeSetRelativeLayer(long transactionObj, long nativeObject,
106 IBinder relativeTo, int zorder);
107 private static native void nativeSetPosition(long transactionObj, long nativeObject,
108 float x, float y);
109 private static native void nativeSetGeometryAppliesWithResize(long transactionObj,
110 long nativeObject);
111 private static native void nativeSetSize(long transactionObj, long nativeObject, int w, int h);
112 private static native void nativeSetTransparentRegionHint(long transactionObj,
113 long nativeObject, Region region);
114 private static native void nativeSetAlpha(long transactionObj, long nativeObject, float alpha);
115 private static native void nativeSetMatrix(long transactionObj, long nativeObject,
116 float dsdx, float dtdx,
Andrii Kulian283acd22017-08-03 04:03:51 -0700117 float dtdy, float dsdy);
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700118 private static native void nativeSetColorTransform(long transactionObj, long nativeObject,
119 float[] matrix, float[] translation);
Peiyong Linf4f0f642019-03-01 14:36:05 -0800120 private static native void nativeSetColorSpaceAgnostic(long transactionObj, long nativeObject,
121 boolean agnostic);
Robert Carr76907ee2019-01-11 13:38:19 -0800122 private static native void nativeSetGeometry(long transactionObj, long nativeObject,
123 Rect sourceCrop, Rect dest, long orientation);
Robert Carre13b58e2017-08-31 14:50:44 -0700124 private static native void nativeSetColor(long transactionObj, long nativeObject, float[] color);
125 private static native void nativeSetFlags(long transactionObj, long nativeObject,
126 int flags, int mask);
127 private static native void nativeSetWindowCrop(long transactionObj, long nativeObject,
128 int l, int t, int r, int b);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -0700129 private static native void nativeSetCornerRadius(long transactionObj, long nativeObject,
130 float cornerRadius);
Robert Carre13b58e2017-08-31 14:50:44 -0700131 private static native void nativeSetLayerStack(long transactionObj, long nativeObject,
132 int layerStack);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800133
Svetoslav1376d602014-03-13 11:17:26 -0700134 private static native boolean nativeClearContentFrameStats(long nativeObject);
135 private static native boolean nativeGetContentFrameStats(long nativeObject, WindowContentFrameStats outStats);
136 private static native boolean nativeClearAnimationFrameStats();
137 private static native boolean nativeGetAnimationFrameStats(WindowAnimationFrameStats outStats);
138
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800139 private static native long[] nativeGetPhysicalDisplayIds();
140 private static native IBinder nativeGetPhysicalDisplayToken(long physicalDisplayId);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800141 private static native IBinder nativeCreateDisplay(String name, boolean secure);
Jesse Hall6a6bc212013-08-08 12:15:03 -0700142 private static native void nativeDestroyDisplay(IBinder displayToken);
Robert Carre13b58e2017-08-31 14:50:44 -0700143 private static native void nativeSetDisplaySurface(long transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000144 IBinder displayToken, long nativeSurfaceObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700145 private static native void nativeSetDisplayLayerStack(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800146 IBinder displayToken, int layerStack);
Robert Carre13b58e2017-08-31 14:50:44 -0700147 private static native void nativeSetDisplayProjection(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800148 IBinder displayToken, int orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -0700149 int l, int t, int r, int b,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800150 int L, int T, int R, int B);
Robert Carre13b58e2017-08-31 14:50:44 -0700151 private static native void nativeSetDisplaySize(long transactionObj, IBinder displayToken,
152 int width, int height);
Dan Stoza00101052014-05-02 15:23:40 -0700153 private static native SurfaceControl.PhysicalDisplayInfo[] nativeGetDisplayConfigs(
154 IBinder displayToken);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -0700155 private static native DisplayedContentSamplingAttributes
156 nativeGetDisplayedContentSamplingAttributes(IBinder displayToken);
157 private static native boolean nativeSetDisplayedContentSamplingEnabled(IBinder displayToken,
158 boolean enable, int componentMask, int maxFrames);
159 private static native DisplayedContentSample nativeGetDisplayedContentSample(
160 IBinder displayToken, long numFrames, long timestamp);
Dan Stoza00101052014-05-02 15:23:40 -0700161 private static native int nativeGetActiveConfig(IBinder displayToken);
162 private static native boolean nativeSetActiveConfig(IBinder displayToken, int id);
Ady Abraham6070ce12019-01-24 18:48:58 -0800163 private static native boolean nativeSetAllowedDisplayConfigs(IBinder displayToken,
164 int[] allowedConfigs);
Ady Abraham42f9a2fb2019-02-26 14:13:39 -0800165 private static native int[] nativeGetAllowedDisplayConfigs(IBinder displayToken);
Michael Wright1c9977b2016-07-12 13:30:10 -0700166 private static native int[] nativeGetDisplayColorModes(IBinder displayToken);
Daniel Solomon10e3b332019-01-20 21:09:11 -0800167 private static native SurfaceControl.DisplayPrimaries nativeGetDisplayNativePrimaries(
168 IBinder displayToken);
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800169 private static native int[] nativeGetCompositionDataspaces();
Michael Wright1c9977b2016-07-12 13:30:10 -0700170 private static native int nativeGetActiveColorMode(IBinder displayToken);
171 private static native boolean nativeSetActiveColorMode(IBinder displayToken,
172 int colorMode);
Prashant Malanic55929a2014-05-25 01:59:21 -0700173 private static native void nativeSetDisplayPowerMode(
174 IBinder displayToken, int mode);
Robert Carre13b58e2017-08-31 14:50:44 -0700175 private static native void nativeDeferTransactionUntil(long transactionObj, long nativeObject,
Rob Carr64e516f2015-10-29 00:20:45 +0000176 IBinder handle, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700177 private static native void nativeDeferTransactionUntilSurface(long transactionObj,
178 long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800179 long surfaceObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700180 private static native void nativeReparentChildren(long transactionObj, long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800181 IBinder handle);
Robert Carre13b58e2017-08-31 14:50:44 -0700182 private static native void nativeReparent(long transactionObj, long nativeObject,
Robert Carr10584fa2019-01-14 15:55:19 -0800183 long newParentNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700184 private static native void nativeSeverChildren(long transactionObj, long nativeObject);
185 private static native void nativeSetOverrideScalingMode(long transactionObj, long nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700186 int scalingMode);
Rob Carr64e516f2015-10-29 00:20:45 +0000187 private static native IBinder nativeGetHandle(long nativeObject);
Robert Carr6da3cc02016-06-16 15:17:07 -0700188 private static native boolean nativeGetTransformToDisplayInverse(long nativeObject);
189
Michael Wright9ff94c02016-03-30 18:05:40 -0700190 private static native Display.HdrCapabilities nativeGetHdrCapabilities(IBinder displayToken);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800191
Robert Carr788f5742018-07-30 17:46:45 -0700192 private static native void nativeSetInputWindowInfo(long transactionObj, long nativeObject,
193 InputWindowHandle handle);
chaviw59f532e2018-12-26 15:34:59 -0800194 private static native void nativeTransferTouchFocus(long transactionObj, IBinder fromToken,
195 IBinder toToken);
Peiyong Lin0ddb7d42019-01-16 13:25:09 -0800196 private static native boolean nativeGetProtectedContentSupport();
Evan Roskyb51e2462019-04-03 19:27:18 -0700197 private static native void nativeSetMetadata(long transactionObj, long nativeObject, int key,
198 Parcel data);
chaviw319cd0782019-02-14 11:00:23 -0800199 private static native void nativeSyncInputWindows(long transactionObj);
Dan Gittik832b4972019-02-13 18:17:47 +0000200 private static native boolean nativeGetDisplayBrightnessSupport(IBinder displayToken);
201 private static native boolean nativeSetDisplayBrightness(IBinder displayToken,
202 float brightness);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800203
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800204 private final CloseGuard mCloseGuard = CloseGuard.get();
chaviwbeb7a0c2018-12-05 13:49:54 -0800205 private String mName;
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000206 long mNativeObject; // package visibility only for Surface.java access
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800207
Jorim Jaggia5e10572017-11-15 14:36:26 +0100208 // TODO: Move this to native.
209 private final Object mSizeLock = new Object();
210 @GuardedBy("mSizeLock")
211 private int mWidth;
212 @GuardedBy("mSizeLock")
213 private int mHeight;
214
Robert Carre13b58e2017-08-31 14:50:44 -0700215 static Transaction sGlobalTransaction;
216 static long sTransactionNestCount = 0;
217
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800218 /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */
219
220 /**
221 * Surface creation flag: Surface is created hidden
Robert Carra7827f72019-01-11 12:53:37 -0800222 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800223 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100224 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800225 public static final int HIDDEN = 0x00000004;
226
227 /**
228 * Surface creation flag: The surface contains secure content, special
229 * measures will be taken to disallow the surface's content to be copied
230 * from another process. In particular, screenshots and VNC servers will
231 * be disabled, but other measures can take place, for instance the
Jesse Hall6a6bc212013-08-08 12:15:03 -0700232 * surface might not be hardware accelerated.
Robert Carra7827f72019-01-11 12:53:37 -0800233 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800234 */
235 public static final int SECURE = 0x00000080;
236
237 /**
238 * Surface creation flag: Creates a surface where color components are interpreted
239 * as "non pre-multiplied" by their alpha channel. Of course this flag is
240 * meaningless for surfaces without an alpha channel. By default
241 * surfaces are pre-multiplied, which means that each color component is
242 * already multiplied by its alpha value. In this case the blending
243 * equation used is:
Andy McFadden314405b2014-01-29 17:18:05 -0800244 * <p>
245 * <code>DEST = SRC + DEST * (1-SRC_ALPHA)</code>
246 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800247 * By contrast, non pre-multiplied surfaces use the following equation:
Andy McFadden314405b2014-01-29 17:18:05 -0800248 * <p>
249 * <code>DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)</code>
250 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800251 * pre-multiplied surfaces must always be used if transparent pixels are
252 * composited on top of each-other into the surface. A pre-multiplied
253 * surface can never lower the value of the alpha component of a given
254 * pixel.
Andy McFadden314405b2014-01-29 17:18:05 -0800255 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800256 * In some rare situations, a non pre-multiplied surface is preferable.
Robert Carra7827f72019-01-11 12:53:37 -0800257 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800258 */
259 public static final int NON_PREMULTIPLIED = 0x00000100;
260
261 /**
262 * Surface creation flag: Indicates that the surface must be considered opaque,
Robert Carre625fcf2017-09-01 12:36:28 -0700263 * even if its pixel format contains an alpha channel. This can be useful if an
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800264 * application needs full RGBA 8888 support for instance but will
265 * still draw every pixel opaque.
Andy McFadden314405b2014-01-29 17:18:05 -0800266 * <p>
267 * This flag is ignored if setAlpha() is used to make the surface non-opaque.
268 * Combined effects are (assuming a buffer format with an alpha channel):
269 * <ul>
270 * <li>OPAQUE + alpha(1.0) == opaque composition
271 * <li>OPAQUE + alpha(0.x) == blended composition
272 * <li>!OPAQUE + alpha(1.0) == blended composition
273 * <li>!OPAQUE + alpha(0.x) == blended composition
274 * </ul>
275 * If the underlying buffer lacks an alpha channel, the OPAQUE flag is effectively
276 * set automatically.
Robert Carra7827f72019-01-11 12:53:37 -0800277 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800278 */
279 public static final int OPAQUE = 0x00000400;
280
281 /**
282 * Surface creation flag: Application requires a hardware-protected path to an
283 * external display sink. If a hardware-protected path is not available,
284 * then this surface will not be displayed on the external sink.
285 *
Robert Carra7827f72019-01-11 12:53:37 -0800286 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800287 */
288 public static final int PROTECTED_APP = 0x00000800;
289
290 // 0x1000 is reserved for an independent DRM protected flag in framework
291
292 /**
Riley Andrews68eccda2014-07-07 11:47:35 -0700293 * Surface creation flag: Window represents a cursor glyph.
Robert Carra7827f72019-01-11 12:53:37 -0800294 * @hide
Riley Andrews68eccda2014-07-07 11:47:35 -0700295 */
296 public static final int CURSOR_WINDOW = 0x00002000;
297
298 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800299 * Surface creation flag: Creates a normal surface.
300 * This is the default.
301 *
Robert Carra7827f72019-01-11 12:53:37 -0800302 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800303 */
304 public static final int FX_SURFACE_NORMAL = 0x00000000;
305
306 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800307 * Surface creation flag: Creates a Dim surface.
308 * Everything behind this surface is dimmed by the amount specified
309 * in {@link #setAlpha}. It is an error to lock a Dim surface, since it
310 * doesn't have a backing store.
311 *
Robert Carra7827f72019-01-11 12:53:37 -0800312 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800313 */
314 public static final int FX_SURFACE_DIM = 0x00020000;
315
316 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700317 * Surface creation flag: Creates a container surface.
318 * This surface will have no buffers and will only be used
319 * as a container for other surfaces, or for its InputInfo.
Robert Carra7827f72019-01-11 12:53:37 -0800320 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700321 */
322 public static final int FX_SURFACE_CONTAINER = 0x00080000;
323
324 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800325 * Mask used for FX values above.
326 *
Robert Carra7827f72019-01-11 12:53:37 -0800327 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800328 */
329 public static final int FX_SURFACE_MASK = 0x000F0000;
330
331 /* flags used with setFlags() (keep in sync with ISurfaceComposer.h) */
332
333 /**
334 * Surface flag: Hide the surface.
335 * Equivalent to calling hide().
Andy McFadden314405b2014-01-29 17:18:05 -0800336 * Updates the value set during Surface creation (see {@link #HIDDEN}).
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800337 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800338 private static final int SURFACE_HIDDEN = 0x01;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800339
Andy McFadden314405b2014-01-29 17:18:05 -0800340 /**
341 * Surface flag: composite without blending when possible.
342 * Updates the value set during Surface creation (see {@link #OPAQUE}).
343 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800344 private static final int SURFACE_OPAQUE = 0x02;
Andy McFadden314405b2014-01-29 17:18:05 -0800345
Robert Carr76907ee2019-01-11 13:38:19 -0800346 // Display power modes.
Prashant Malanic55929a2014-05-25 01:59:21 -0700347 /**
348 * Display power mode off: used while blanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700349 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800350 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700351 */
352 public static final int POWER_MODE_OFF = 0;
353
354 /**
355 * Display power mode doze: used while putting the screen into low power mode.
Jeff Brown5dc21912014-07-17 18:50:18 -0700356 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800357 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700358 */
359 public static final int POWER_MODE_DOZE = 1;
360
361 /**
362 * Display power mode normal: used while unblanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700363 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800364 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700365 */
366 public static final int POWER_MODE_NORMAL = 2;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800367
Jeff Brown5dc21912014-07-17 18:50:18 -0700368 /**
369 * Display power mode doze: used while putting the screen into a suspended
370 * low power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800371 * @hide
Jeff Brown5dc21912014-07-17 18:50:18 -0700372 */
373 public static final int POWER_MODE_DOZE_SUSPEND = 3;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800374
375 /**
Chris Phoenix10a4a642017-09-25 13:21:00 -0700376 * Display power mode on: used while putting the screen into a suspended
377 * full power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800378 * @hide
Chris Phoenix10a4a642017-09-25 13:21:00 -0700379 */
380 public static final int POWER_MODE_ON_SUSPEND = 4;
381
382 /**
Robert Carr132c9f52017-07-31 17:02:30 -0700383 * A value for windowType used to indicate that the window should be omitted from screenshots
384 * and display mirroring. A temporary workaround until we express such things with
385 * the hierarchy.
386 * TODO: b/64227542
387 * @hide
388 */
389 public static final int WINDOW_TYPE_DONT_SCREENSHOT = 441731;
390
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800391 /**
392 * internal representation of how to interpret pixel value, used only to convert to ColorSpace.
393 */
394 private static final int INTERNAL_DATASPACE_SRGB = 142671872;
395 private static final int INTERNAL_DATASPACE_DISPLAY_P3 = 143261696;
396 private static final int INTERNAL_DATASPACE_SCRGB = 411107328;
397
Robert Carreb344c72019-01-07 18:35:30 -0800398 private void assignNativeObject(long nativeObject) {
399 if (mNativeObject != 0) {
400 release();
401 }
402 mNativeObject = nativeObject;
403 }
404
Robert Carra7827f72019-01-11 12:53:37 -0800405 /**
406 * @hide
407 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800408 public void copyFrom(SurfaceControl other) {
409 mName = other.mName;
410 mWidth = other.mWidth;
411 mHeight = other.mHeight;
Robert Carreb344c72019-01-07 18:35:30 -0800412 assignNativeObject(nativeCopyFromSurfaceControl(other.mNativeObject));
chaviwbeb7a0c2018-12-05 13:49:54 -0800413 }
414
Robert Carr132c9f52017-07-31 17:02:30 -0700415 /**
Evan Rosky485df202018-12-06 14:11:12 -0800416 * owner UID.
417 * @hide
418 */
419 public static final int METADATA_OWNER_UID = 1;
420
421 /**
422 * Window type as per {@link WindowManager.LayoutParams}.
423 * @hide
424 */
425 public static final int METADATA_WINDOW_TYPE = 2;
426
427 /**
Evan Rosky9020c072018-12-06 14:11:12 -0800428 * Task id to allow association between surfaces and task.
429 * @hide
430 */
431 public static final int METADATA_TASK_ID = 3;
432
433 /**
Peiyong Line3e5efd2019-03-21 20:59:47 +0000434 * A wrapper around GraphicBuffer that contains extra information about how to
435 * interpret the screenshot GraphicBuffer.
436 * @hide
437 */
438 public static class ScreenshotGraphicBuffer {
439 private final GraphicBuffer mGraphicBuffer;
440 private final ColorSpace mColorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700441 private final boolean mContainsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000442
Robert Carr66b5664f2019-04-02 14:18:56 -0700443 public ScreenshotGraphicBuffer(GraphicBuffer graphicBuffer, ColorSpace colorSpace,
444 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000445 mGraphicBuffer = graphicBuffer;
446 mColorSpace = colorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700447 mContainsSecureLayers = containsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000448 }
449
450 /**
451 * Create ScreenshotGraphicBuffer from existing native GraphicBuffer object.
452 * @param width The width in pixels of the buffer
453 * @param height The height in pixels of the buffer
454 * @param format The format of each pixel as specified in {@link PixelFormat}
455 * @param usage Hint indicating how the buffer will be used
456 * @param unwrappedNativeObject The native object of GraphicBuffer
457 * @param namedColorSpace Integer value of a named color space {@link ColorSpace.Named}
Robert Carr66b5664f2019-04-02 14:18:56 -0700458 * @param containsSecureLayer Indicates whether this graphic buffer contains captured contents
459 * of secure layers, in which case the screenshot should not be persisted.
Peiyong Line3e5efd2019-03-21 20:59:47 +0000460 */
461 private static ScreenshotGraphicBuffer createFromNative(int width, int height, int format,
Robert Carr66b5664f2019-04-02 14:18:56 -0700462 int usage, long unwrappedNativeObject, int namedColorSpace,
463 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000464 GraphicBuffer graphicBuffer = GraphicBuffer.createFromExisting(width, height, format,
465 usage, unwrappedNativeObject);
466 ColorSpace colorSpace = ColorSpace.get(ColorSpace.Named.values()[namedColorSpace]);
Robert Carr66b5664f2019-04-02 14:18:56 -0700467 return new ScreenshotGraphicBuffer(graphicBuffer, colorSpace, containsSecureLayers);
Peiyong Line3e5efd2019-03-21 20:59:47 +0000468 }
469
470 public ColorSpace getColorSpace() {
471 return mColorSpace;
472 }
473
474 public GraphicBuffer getGraphicBuffer() {
475 return mGraphicBuffer;
476 }
Robert Carr66b5664f2019-04-02 14:18:56 -0700477
478 public boolean containsSecureLayers() {
479 return mContainsSecureLayers;
480 }
Peiyong Line3e5efd2019-03-21 20:59:47 +0000481 }
482
483 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700484 * Builder class for {@link SurfaceControl} objects.
Robert Carr29fe58a2019-03-11 15:25:16 -0700485 *
486 * By default the surface will be hidden, and have "unset" bounds, meaning it can
487 * be as large as the bounds of its parent if a buffer or child so requires.
488 *
489 * It is necessary to set at least a name via {@link Builder#setName}
Robert Carre625fcf2017-09-01 12:36:28 -0700490 */
491 public static class Builder {
492 private SurfaceSession mSession;
493 private int mFlags = HIDDEN;
494 private int mWidth;
495 private int mHeight;
496 private int mFormat = PixelFormat.OPAQUE;
497 private String mName;
498 private SurfaceControl mParent;
Evan Rosky485df202018-12-06 14:11:12 -0800499 private SparseIntArray mMetadata;
Robert Carre625fcf2017-09-01 12:36:28 -0700500
501 /**
502 * Begin building a SurfaceControl with a given {@link SurfaceSession}.
503 *
504 * @param session The {@link SurfaceSession} with which to eventually construct the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800505 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700506 */
507 public Builder(SurfaceSession session) {
508 mSession = session;
509 }
510
511 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800512 * Begin building a SurfaceControl.
513 */
514 public Builder() {
515 }
516
517 /**
518 * Construct a new {@link SurfaceControl} with the set parameters. The builder
519 * remains valid.
Robert Carre625fcf2017-09-01 12:36:28 -0700520 */
Robert Carrda1d2422019-03-07 15:54:37 -0800521 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700522 public SurfaceControl build() {
Vishnu Naire86bd982018-11-28 13:23:17 -0800523 if (mWidth < 0 || mHeight < 0) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700524 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800525 "width and height must be positive or unset");
526 }
527 if ((mWidth > 0 || mHeight > 0) && (isColorLayerSet() || isContainerLayerSet())) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700528 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800529 "Only buffer layers can set a valid buffer size.");
Robert Carre625fcf2017-09-01 12:36:28 -0700530 }
Evan Rosky485df202018-12-06 14:11:12 -0800531 return new SurfaceControl(
532 mSession, mName, mWidth, mHeight, mFormat, mFlags, mParent, mMetadata);
Robert Carre625fcf2017-09-01 12:36:28 -0700533 }
534
535 /**
536 * Set a debugging-name for the SurfaceControl.
537 *
538 * @param name A name to identify the Surface in debugging.
539 */
Robert Carrda1d2422019-03-07 15:54:37 -0800540 @NonNull
541 public Builder setName(@NonNull String name) {
Robert Carre625fcf2017-09-01 12:36:28 -0700542 mName = name;
543 return this;
544 }
545
546 /**
547 * Set the initial size of the controlled surface's buffers in pixels.
548 *
549 * @param width The buffer width in pixels.
550 * @param height The buffer height in pixels.
551 */
Robert Carrda1d2422019-03-07 15:54:37 -0800552 @NonNull
Robert Carr76907ee2019-01-11 13:38:19 -0800553 public Builder setBufferSize(@IntRange(from = 0) int width,
554 @IntRange(from = 0) int height) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800555 if (width < 0 || height < 0) {
Robert Carre625fcf2017-09-01 12:36:28 -0700556 throw new IllegalArgumentException(
557 "width and height must be positive");
558 }
559 mWidth = width;
560 mHeight = height;
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000561 // set this as a buffer layer since we are specifying a buffer size.
562 return setFlags(FX_SURFACE_NORMAL, FX_SURFACE_MASK);
563 }
564
565 /**
566 * Set the initial size of the controlled surface's buffers in pixels.
567 */
568 private void unsetBufferSize() {
569 mWidth = 0;
570 mHeight = 0;
Robert Carre625fcf2017-09-01 12:36:28 -0700571 }
572
573 /**
574 * Set the pixel format of the controlled surface's buffers, using constants from
575 * {@link android.graphics.PixelFormat}.
576 */
Robert Carr76907ee2019-01-11 13:38:19 -0800577 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700578 public Builder setFormat(@PixelFormat.Format int format) {
579 mFormat = format;
580 return this;
581 }
582
583 /**
584 * Specify if the app requires a hardware-protected path to
585 * an external display sync. If protected content is enabled, but
586 * such a path is not available, then the controlled Surface will
587 * not be displayed.
588 *
589 * @param protectedContent Whether to require a protected sink.
Robert Carra7827f72019-01-11 12:53:37 -0800590 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700591 */
Robert Carr76907ee2019-01-11 13:38:19 -0800592 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700593 public Builder setProtected(boolean protectedContent) {
594 if (protectedContent) {
595 mFlags |= PROTECTED_APP;
596 } else {
597 mFlags &= ~PROTECTED_APP;
598 }
599 return this;
600 }
601
602 /**
603 * Specify whether the Surface contains secure content. If true, the system
604 * will prevent the surfaces content from being copied by another process. In
605 * particular screenshots and VNC servers will be disabled. This is however
606 * not a complete prevention of readback as {@link #setProtected}.
Robert Carra7827f72019-01-11 12:53:37 -0800607 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700608 */
Robert Carr76907ee2019-01-11 13:38:19 -0800609 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700610 public Builder setSecure(boolean secure) {
611 if (secure) {
612 mFlags |= SECURE;
613 } else {
614 mFlags &= ~SECURE;
615 }
616 return this;
617 }
618
619 /**
620 * Indicates whether the surface must be considered opaque,
621 * even if its pixel format is set to translucent. This can be useful if an
622 * application needs full RGBA 8888 support for instance but will
623 * still draw every pixel opaque.
624 * <p>
625 * This flag only determines whether opacity will be sampled from the alpha channel.
626 * Plane-alpha from calls to setAlpha() can still result in blended composition
627 * regardless of the opaque setting.
628 *
629 * Combined effects are (assuming a buffer format with an alpha channel):
630 * <ul>
631 * <li>OPAQUE + alpha(1.0) == opaque composition
632 * <li>OPAQUE + alpha(0.x) == blended composition
633 * <li>OPAQUE + alpha(0.0) == no composition
634 * <li>!OPAQUE + alpha(1.0) == blended composition
635 * <li>!OPAQUE + alpha(0.x) == blended composition
636 * <li>!OPAQUE + alpha(0.0) == no composition
637 * </ul>
638 * If the underlying buffer lacks an alpha channel, it is as if setOpaque(true)
639 * were set automatically.
640 * @param opaque Whether the Surface is OPAQUE.
641 */
Robert Carr76907ee2019-01-11 13:38:19 -0800642 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700643 public Builder setOpaque(boolean opaque) {
644 if (opaque) {
645 mFlags |= OPAQUE;
646 } else {
647 mFlags &= ~OPAQUE;
648 }
649 return this;
650 }
651
652 /**
653 * Set a parent surface for our new SurfaceControl.
654 *
655 * Child surfaces are constrained to the onscreen region of their parent.
656 * Furthermore they stack relatively in Z order, and inherit the transformation
657 * of the parent.
658 *
659 * @param parent The parent control.
660 */
Robert Carr76907ee2019-01-11 13:38:19 -0800661 @NonNull
662 public Builder setParent(@Nullable SurfaceControl parent) {
Robert Carre625fcf2017-09-01 12:36:28 -0700663 mParent = parent;
664 return this;
665 }
666
667 /**
Evan Rosky485df202018-12-06 14:11:12 -0800668 * Sets a metadata int.
Robert Carre625fcf2017-09-01 12:36:28 -0700669 *
Evan Rosky485df202018-12-06 14:11:12 -0800670 * @param key metadata key
671 * @param data associated data
Robert Carra7827f72019-01-11 12:53:37 -0800672 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700673 */
Evan Rosky485df202018-12-06 14:11:12 -0800674 public Builder setMetadata(int key, int data) {
675 if (mMetadata == null) {
676 mMetadata = new SparseIntArray();
Robert Carre625fcf2017-09-01 12:36:28 -0700677 }
Evan Rosky485df202018-12-06 14:11:12 -0800678 mMetadata.put(key, data);
Robert Carre625fcf2017-09-01 12:36:28 -0700679 return this;
680 }
681
682 /**
683 * Indicate whether a 'ColorLayer' is to be constructed.
684 *
685 * Color layers will not have an associated BufferQueue and will instead always render a
686 * solid color (that is, solid before plane alpha). Currently that color is black.
687 *
Robert Carra7827f72019-01-11 12:53:37 -0800688 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700689 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000690 public Builder setColorLayer() {
691 unsetBufferSize();
692 return setFlags(FX_SURFACE_DIM, FX_SURFACE_MASK);
Robert Carre625fcf2017-09-01 12:36:28 -0700693 }
694
Vishnu Naire86bd982018-11-28 13:23:17 -0800695 private boolean isColorLayerSet() {
696 return (mFlags & FX_SURFACE_DIM) == FX_SURFACE_DIM;
697 }
698
Robert Carre625fcf2017-09-01 12:36:28 -0700699 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700700 * Indicates whether a 'ContainerLayer' is to be constructed.
701 *
702 * Container layers will not be rendered in any fashion and instead are used
703 * as a parent of renderable layers.
704 *
Robert Carra7827f72019-01-11 12:53:37 -0800705 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700706 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000707 public Builder setContainerLayer() {
708 unsetBufferSize();
709 return setFlags(FX_SURFACE_CONTAINER, FX_SURFACE_MASK);
Robert Carrb6cd6432018-08-13 13:01:47 -0700710 }
711
Vishnu Naire86bd982018-11-28 13:23:17 -0800712 private boolean isContainerLayerSet() {
713 return (mFlags & FX_SURFACE_CONTAINER) == FX_SURFACE_CONTAINER;
714 }
715
Robert Carrb6cd6432018-08-13 13:01:47 -0700716 /**
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000717 * Set 'Surface creation flags' such as {@link #HIDDEN}, {@link #SECURE}.
Robert Carre625fcf2017-09-01 12:36:28 -0700718 *
719 * TODO: Finish conversion to individual builder methods?
720 * @param flags The combined flags
Robert Carra7827f72019-01-11 12:53:37 -0800721 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700722 */
723 public Builder setFlags(int flags) {
724 mFlags = flags;
725 return this;
726 }
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000727
728 private Builder setFlags(int flags, int mask) {
729 mFlags = (mFlags & ~mask) | flags;
730 return this;
731 }
Robert Carre625fcf2017-09-01 12:36:28 -0700732 }
733
734 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800735 * Create a surface with a name.
Andy McFadden314405b2014-01-29 17:18:05 -0800736 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800737 * The surface creation flags specify what kind of surface to create and
738 * certain options such as whether the surface can be assumed to be opaque
739 * and whether it should be initially hidden. Surfaces should always be
740 * created with the {@link #HIDDEN} flag set to ensure that they are not
741 * made visible prematurely before all of the surface's properties have been
742 * configured.
Andy McFadden314405b2014-01-29 17:18:05 -0800743 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800744 * Good practice is to first create the surface with the {@link #HIDDEN} flag
745 * specified, open a transaction, set the surface layer, layer stack, alpha,
746 * and position, call {@link #show} if appropriate, and close the transaction.
Vishnu Naird454442d2018-11-13 13:51:01 -0800747 * <p>
748 * Bounds of the surface is determined by its crop and its buffer size. If the
749 * surface has no buffer or crop, the surface is boundless and only constrained
750 * by the size of its parent bounds.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800751 *
752 * @param session The surface session, must not be null.
753 * @param name The surface name, must not be null.
754 * @param w The surface initial width.
755 * @param h The surface initial height.
756 * @param flags The surface creation flags. Should always include {@link #HIDDEN}
757 * in the creation flags.
Evan Rosky485df202018-12-06 14:11:12 -0800758 * @param metadata Initial metadata.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700759 *
760 * @throws throws OutOfResourcesException If the SurfaceControl cannot be created.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800761 */
Robert Carre625fcf2017-09-01 12:36:28 -0700762 private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags,
Evan Rosky485df202018-12-06 14:11:12 -0800763 SurfaceControl parent, SparseIntArray metadata)
Robert Carrb0f39362018-03-14 13:52:25 -0700764 throws OutOfResourcesException, IllegalArgumentException {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800765 if (name == null) {
766 throw new IllegalArgumentException("name must not be null");
767 }
768
769 if ((flags & SurfaceControl.HIDDEN) == 0) {
770 Log.w(TAG, "Surfaces should always be created with the HIDDEN flag set "
771 + "to ensure that they are not made visible prematurely before "
772 + "all of the surface's properties have been configured. "
773 + "Set the other properties and make the surface visible within "
774 + "a transaction. New surface name: " + name,
775 new Throwable());
776 }
777
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800778 mName = name;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100779 mWidth = w;
780 mHeight = h;
Evan Rosky485df202018-12-06 14:11:12 -0800781 Parcel metaParcel = Parcel.obtain();
782 try {
783 if (metadata != null && metadata.size() > 0) {
784 metaParcel.writeInt(metadata.size());
785 for (int i = 0; i < metadata.size(); ++i) {
786 metaParcel.writeInt(metadata.keyAt(i));
787 metaParcel.writeByteArray(
Evan Roskyc12c9a32019-02-06 13:38:03 -0800788 ByteBuffer.allocate(4).order(ByteOrder.nativeOrder())
789 .putInt(metadata.valueAt(i)).array());
Evan Rosky485df202018-12-06 14:11:12 -0800790 }
Evan Roskyc12c9a32019-02-06 13:38:03 -0800791 metaParcel.setDataPosition(0);
Evan Rosky485df202018-12-06 14:11:12 -0800792 }
793 mNativeObject = nativeCreate(session, name, w, h, format, flags,
794 parent != null ? parent.mNativeObject : 0, metaParcel);
795 } finally {
796 metaParcel.recycle();
797 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800798 if (mNativeObject == 0) {
799 throw new OutOfResourcesException(
800 "Couldn't allocate SurfaceControl native object");
801 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700802
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800803 mCloseGuard.open("release");
804 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700805
Robert Carra7827f72019-01-11 12:53:37 -0800806 /** This is a transfer constructor, useful for transferring a live SurfaceControl native
807 * object to another Java wrapper which could have some different behavior, e.g.
808 * event logging.
809 * @hide
810 */
Robert Carr3b716242016-08-16 16:02:21 -0700811 public SurfaceControl(SurfaceControl other) {
812 mName = other.mName;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100813 mWidth = other.mWidth;
814 mHeight = other.mHeight;
Robert Carr3b716242016-08-16 16:02:21 -0700815 mNativeObject = other.mNativeObject;
816 other.mCloseGuard.close();
817 other.mNativeObject = 0;
818 mCloseGuard.open("release");
819 }
820
Jorim Jaggi06975df2017-12-01 14:52:13 +0100821 private SurfaceControl(Parcel in) {
chaviwbeb7a0c2018-12-05 13:49:54 -0800822 readFromParcel(in);
823 mCloseGuard.open("release");
824 }
825
Robert Carra7827f72019-01-11 12:53:37 -0800826 /**
827 * @hide
828 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800829 public SurfaceControl() {
830 mCloseGuard.open("release");
831 }
832
833 public void readFromParcel(Parcel in) {
834 if (in == null) {
835 throw new IllegalArgumentException("source must not be null");
836 }
837
Jorim Jaggi06975df2017-12-01 14:52:13 +0100838 mName = in.readString();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100839 mWidth = in.readInt();
840 mHeight = in.readInt();
Robert Carr5fea55b2018-12-10 13:05:52 -0800841
Robert Carreb344c72019-01-07 18:35:30 -0800842 long object = 0;
Robert Carr5fea55b2018-12-10 13:05:52 -0800843 if (in.readInt() != 0) {
Robert Carreb344c72019-01-07 18:35:30 -0800844 object = nativeReadFromParcel(in);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100845 }
Robert Carreb344c72019-01-07 18:35:30 -0800846 assignNativeObject(object);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100847 }
848
849 @Override
850 public int describeContents() {
851 return 0;
852 }
853
854 @Override
855 public void writeToParcel(Parcel dest, int flags) {
856 dest.writeString(mName);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100857 dest.writeInt(mWidth);
858 dest.writeInt(mHeight);
Robert Carr5fea55b2018-12-10 13:05:52 -0800859 if (mNativeObject == 0) {
860 dest.writeInt(0);
861 } else {
862 dest.writeInt(1);
863 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100864 nativeWriteToParcel(mNativeObject, dest);
Robert Carr5fea55b2018-12-10 13:05:52 -0800865
866 if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) {
867 release();
868 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100869 }
870
Vishnu Nair04ab4392018-01-10 11:00:06 -0800871 /**
872 * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link
873 * android.view.SurfaceControlProto}.
874 *
875 * @param proto Stream to write the SurfaceControl object to.
876 * @param fieldId Field Id of the SurfaceControl as defined in the parent message.
877 * @hide
878 */
879 public void writeToProto(ProtoOutputStream proto, long fieldId) {
880 final long token = proto.start(fieldId);
881 proto.write(HASH_CODE, System.identityHashCode(this));
882 proto.write(NAME, mName);
883 proto.end(token);
884 }
885
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700886 public static final @android.annotation.NonNull Creator<SurfaceControl> CREATOR
Jorim Jaggi06975df2017-12-01 14:52:13 +0100887 = new Creator<SurfaceControl>() {
888 public SurfaceControl createFromParcel(Parcel in) {
889 return new SurfaceControl(in);
890 }
891
892 public SurfaceControl[] newArray(int size) {
893 return new SurfaceControl[size];
894 }
895 };
896
Robert Carra7827f72019-01-11 12:53:37 -0800897 /**
898 * @hide
899 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800900 @Override
901 protected void finalize() throws Throwable {
902 try {
903 if (mCloseGuard != null) {
904 mCloseGuard.warnIfOpen();
905 }
906 if (mNativeObject != 0) {
907 nativeRelease(mNativeObject);
908 }
909 } finally {
910 super.finalize();
911 }
912 }
913
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800914 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800915 * Release the local reference to the server-side surface. The surface
916 * may continue to exist on-screen as long as its parent continues
917 * to exist. To explicitly remove a surface from the screen use
Robert Carr29fe58a2019-03-11 15:25:16 -0700918 * {@link Transaction#reparent} with a null-parent. After release,
919 * {@link #isValid} will return false and other methods will throw
920 * an exception.
Robert Carr76907ee2019-01-11 13:38:19 -0800921 *
922 * Always call release() when you're done with a SurfaceControl.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800923 */
924 public void release() {
925 if (mNativeObject != 0) {
926 nativeRelease(mNativeObject);
927 mNativeObject = 0;
928 }
929 mCloseGuard.close();
930 }
931
932 /**
Robert Carr5ea304d2019-02-04 16:04:55 -0800933 * Release the local resources like {@link #release} but also
934 * remove the Surface from the screen.
Robert Carra7827f72019-01-11 12:53:37 -0800935 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800936 */
Robert Carr5ea304d2019-02-04 16:04:55 -0800937 public void remove() {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800938 if (mNativeObject != 0) {
939 nativeDestroy(mNativeObject);
940 mNativeObject = 0;
941 }
942 mCloseGuard.close();
943 }
944
Chong Zhang47e36a32016-02-29 16:44:33 -0800945 /**
946 * Disconnect any client still connected to the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800947 * @hide
Chong Zhang47e36a32016-02-29 16:44:33 -0800948 */
949 public void disconnect() {
950 if (mNativeObject != 0) {
951 nativeDisconnect(mNativeObject);
952 }
953 }
954
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800955 private void checkNotReleased() {
956 if (mNativeObject == 0) throw new NullPointerException(
957 "mNativeObject is null. Have you called release() already?");
958 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700959
Robert Carra7827f72019-01-11 12:53:37 -0800960 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800961 * Check whether this instance points to a valid layer with the system-compositor. For
Robert Carr29fe58a2019-03-11 15:25:16 -0700962 * example this may be false if construction failed, or the layer was released
963 * ({@link #release}).
Robert Carr76907ee2019-01-11 13:38:19 -0800964 *
965 * @return Whether this SurfaceControl is valid.
Robert Carra7827f72019-01-11 12:53:37 -0800966 */
Robert Carr5fea55b2018-12-10 13:05:52 -0800967 public boolean isValid() {
968 return mNativeObject != 0;
969 }
970
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800971 /*
972 * set surface parameters.
973 * needs to be inside open/closeTransaction block
974 */
975
Robert Carra7827f72019-01-11 12:53:37 -0800976 /** start a transaction
977 * @hide
978 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100979 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800980 public static void openTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -0700981 synchronized (SurfaceControl.class) {
982 if (sGlobalTransaction == null) {
983 sGlobalTransaction = new Transaction();
984 }
985 synchronized(SurfaceControl.class) {
986 sTransactionNestCount++;
987 }
988 }
989 }
990
Robert Carrb1579c82017-09-05 14:54:47 -0700991 /**
992 * Merge the supplied transaction in to the deprecated "global" transaction.
993 * This clears the supplied transaction in an identical fashion to {@link Transaction#merge}.
994 * <p>
995 * This is a utility for interop with legacy-code and will go away with the Global Transaction.
Robert Carra7827f72019-01-11 12:53:37 -0800996 * @hide
Robert Carrb1579c82017-09-05 14:54:47 -0700997 */
998 @Deprecated
999 public static void mergeToGlobalTransaction(Transaction t) {
Robert Carrc5fc4612017-12-05 11:55:40 -08001000 synchronized(SurfaceControl.class) {
Robert Carrb1579c82017-09-05 14:54:47 -07001001 sGlobalTransaction.merge(t);
1002 }
1003 }
1004
chaviwdaaeab02019-03-20 12:25:37 -07001005 /** end a transaction
1006 * @hide
Robert Carra7827f72019-01-11 12:53:37 -08001007 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001008 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001009 public static void closeTransaction() {
chaviwdaaeab02019-03-20 12:25:37 -07001010 synchronized(SurfaceControl.class) {
1011 if (sTransactionNestCount == 0) {
1012 Log.e(TAG,
1013 "Call to SurfaceControl.closeTransaction without matching openTransaction");
1014 } else if (--sTransactionNestCount > 0) {
1015 return;
1016 }
1017 sGlobalTransaction.apply();
1018 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001019 }
1020
Robert Carra7827f72019-01-11 12:53:37 -08001021 /**
1022 * @hide
1023 */
Rob Carr64e516f2015-10-29 00:20:45 +00001024 public void deferTransactionUntil(IBinder handle, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -08001025 synchronized(SurfaceControl.class) {
1026 sGlobalTransaction.deferTransactionUntil(this, handle, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -08001027 }
1028 }
1029
Robert Carra7827f72019-01-11 12:53:37 -08001030 /**
1031 * @hide
1032 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001033 public void deferTransactionUntil(Surface barrier, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -08001034 synchronized(SurfaceControl.class) {
1035 sGlobalTransaction.deferTransactionUntilSurface(this, barrier, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -08001036 }
1037 }
1038
Robert Carra7827f72019-01-11 12:53:37 -08001039 /**
1040 * @hide
1041 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001042 public void reparentChildren(IBinder newParentHandle) {
Robert Carre13b58e2017-08-31 14:50:44 -07001043 synchronized(SurfaceControl.class) {
1044 sGlobalTransaction.reparentChildren(this, newParentHandle);
1045 }
Robert Carrd5c7dd62017-03-08 10:39:30 -08001046 }
1047
Robert Carra7827f72019-01-11 12:53:37 -08001048 /**
1049 * @hide
1050 */
Robert Carr10584fa2019-01-14 15:55:19 -08001051 public void reparent(SurfaceControl newParent) {
Robert Carre13b58e2017-08-31 14:50:44 -07001052 synchronized(SurfaceControl.class) {
Robert Carr10584fa2019-01-14 15:55:19 -08001053 sGlobalTransaction.reparent(this, newParent);
Robert Carre13b58e2017-08-31 14:50:44 -07001054 }
chaviw63542382017-08-17 17:39:29 -07001055 }
1056
Robert Carra7827f72019-01-11 12:53:37 -08001057 /**
1058 * @hide
1059 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001060 public void detachChildren() {
Robert Carre13b58e2017-08-31 14:50:44 -07001061 synchronized(SurfaceControl.class) {
1062 sGlobalTransaction.detachChildren(this);
1063 }
Rob Carr64e516f2015-10-29 00:20:45 +00001064 }
1065
Robert Carra7827f72019-01-11 12:53:37 -08001066 /**
1067 * @hide
1068 */
Robert Carr1ca6a332016-04-11 18:00:43 -07001069 public void setOverrideScalingMode(int scalingMode) {
1070 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001071 synchronized(SurfaceControl.class) {
1072 sGlobalTransaction.setOverrideScalingMode(this, scalingMode);
1073 }
Robert Carr1ca6a332016-04-11 18:00:43 -07001074 }
1075
Robert Carra7827f72019-01-11 12:53:37 -08001076 /**
1077 * @hide
1078 */
Rob Carr64e516f2015-10-29 00:20:45 +00001079 public IBinder getHandle() {
1080 return nativeGetHandle(mNativeObject);
1081 }
1082
Robert Carra7827f72019-01-11 12:53:37 -08001083 /**
1084 * @hide
1085 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001086 public static void setAnimationTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -07001087 synchronized (SurfaceControl.class) {
1088 sGlobalTransaction.setAnimationTransaction();
1089 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001090 }
1091
Robert Carra7827f72019-01-11 12:53:37 -08001092 /**
1093 * @hide
1094 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001095 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001096 public void setLayer(int zorder) {
1097 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001098 synchronized(SurfaceControl.class) {
1099 sGlobalTransaction.setLayer(this, zorder);
1100 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001101 }
1102
Robert Carra7827f72019-01-11 12:53:37 -08001103 /**
1104 * @hide
1105 */
Robert Carr77e34942017-10-18 19:13:56 -07001106 public void setRelativeLayer(SurfaceControl relativeTo, int zorder) {
Robert Carraf422a82017-04-10 18:34:33 -07001107 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001108 synchronized(SurfaceControl.class) {
1109 sGlobalTransaction.setRelativeLayer(this, relativeTo, zorder);
1110 }
Robert Carraf422a82017-04-10 18:34:33 -07001111 }
1112
Robert Carra7827f72019-01-11 12:53:37 -08001113 /**
1114 * @hide
1115 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001116 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001117 public void setPosition(float x, float y) {
1118 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001119 synchronized(SurfaceControl.class) {
1120 sGlobalTransaction.setPosition(this, x, y);
1121 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001122 }
1123
Robert Carra7827f72019-01-11 12:53:37 -08001124 /**
1125 * @hide
1126 */
Robert Carr6da3cc02016-06-16 15:17:07 -07001127 public void setGeometryAppliesWithResize() {
Robert Carra9408d42016-06-03 13:28:48 -07001128 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001129 synchronized(SurfaceControl.class) {
1130 sGlobalTransaction.setGeometryAppliesWithResize(this);
1131 }
Robert Carra9408d42016-06-03 13:28:48 -07001132 }
1133
Robert Carra7827f72019-01-11 12:53:37 -08001134 /**
1135 * @hide
1136 */
Vishnu Naire86bd982018-11-28 13:23:17 -08001137 public void setBufferSize(int w, int h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001138 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001139 synchronized(SurfaceControl.class) {
Vishnu Naire86bd982018-11-28 13:23:17 -08001140 sGlobalTransaction.setBufferSize(this, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07001141 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001142 }
1143
Robert Carra7827f72019-01-11 12:53:37 -08001144 /**
1145 * @hide
1146 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001147 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001148 public void hide() {
1149 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001150 synchronized(SurfaceControl.class) {
1151 sGlobalTransaction.hide(this);
1152 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001153 }
1154
Robert Carra7827f72019-01-11 12:53:37 -08001155 /**
1156 * @hide
1157 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001158 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001159 public void show() {
1160 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001161 synchronized(SurfaceControl.class) {
1162 sGlobalTransaction.show(this);
1163 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001164 }
1165
Robert Carra7827f72019-01-11 12:53:37 -08001166 /**
1167 * @hide
1168 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001169 public void setTransparentRegionHint(Region region) {
1170 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001171 synchronized(SurfaceControl.class) {
1172 sGlobalTransaction.setTransparentRegionHint(this, region);
1173 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001174 }
1175
Robert Carra7827f72019-01-11 12:53:37 -08001176 /**
1177 * @hide
1178 */
Svetoslav1376d602014-03-13 11:17:26 -07001179 public boolean clearContentFrameStats() {
1180 checkNotReleased();
1181 return nativeClearContentFrameStats(mNativeObject);
1182 }
1183
Robert Carra7827f72019-01-11 12:53:37 -08001184 /**
1185 * @hide
1186 */
Svetoslav1376d602014-03-13 11:17:26 -07001187 public boolean getContentFrameStats(WindowContentFrameStats outStats) {
1188 checkNotReleased();
1189 return nativeGetContentFrameStats(mNativeObject, outStats);
1190 }
1191
Robert Carra7827f72019-01-11 12:53:37 -08001192 /**
1193 * @hide
1194 */
Svetoslav1376d602014-03-13 11:17:26 -07001195 public static boolean clearAnimationFrameStats() {
1196 return nativeClearAnimationFrameStats();
1197 }
1198
Robert Carra7827f72019-01-11 12:53:37 -08001199 /**
1200 * @hide
1201 */
Svetoslav1376d602014-03-13 11:17:26 -07001202 public static boolean getAnimationFrameStats(WindowAnimationFrameStats outStats) {
1203 return nativeGetAnimationFrameStats(outStats);
1204 }
1205
Robert Carra7827f72019-01-11 12:53:37 -08001206 /**
1207 * @hide
1208 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001209 public void setAlpha(float alpha) {
1210 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001211 synchronized(SurfaceControl.class) {
1212 sGlobalTransaction.setAlpha(this, alpha);
1213 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001214 }
1215
Robert Carra7827f72019-01-11 12:53:37 -08001216 /**
1217 * @hide
1218 */
chaviw0dd03f52017-08-25 12:15:26 -07001219 public void setColor(@Size(3) float[] color) {
1220 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001221 synchronized (SurfaceControl.class) {
1222 sGlobalTransaction.setColor(this, color);
1223 }
chaviw0dd03f52017-08-25 12:15:26 -07001224 }
1225
Robert Carra7827f72019-01-11 12:53:37 -08001226 /**
1227 * @hide
1228 */
Robert Carr0edf18f2017-02-21 20:01:47 -08001229 public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001230 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001231 synchronized(SurfaceControl.class) {
1232 sGlobalTransaction.setMatrix(this, dsdx, dtdx, dtdy, dsdy);
1233 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001234 }
1235
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001236 /**
1237 * Sets the transform and position of a {@link SurfaceControl} from a 3x3 transformation matrix.
1238 *
1239 * @param matrix The matrix to apply.
1240 * @param float9 An array of 9 floats to be used to extract the values from the matrix.
Robert Carra7827f72019-01-11 12:53:37 -08001241 * @hide
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001242 */
1243 public void setMatrix(Matrix matrix, float[] float9) {
1244 checkNotReleased();
1245 matrix.getValues(float9);
1246 synchronized (SurfaceControl.class) {
1247 sGlobalTransaction.setMatrix(this, float9[MSCALE_X], float9[MSKEW_Y],
1248 float9[MSKEW_X], float9[MSCALE_Y]);
1249 sGlobalTransaction.setPosition(this, float9[MTRANS_X], float9[MTRANS_Y]);
1250 }
1251 }
1252
Peiyong Lin52bb6b42018-10-01 11:40:50 -07001253 /**
1254 * Sets the color transform for the Surface.
1255 * @param matrix A float array with 9 values represents a 3x3 transform matrix
1256 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08001257 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07001258 */
1259 public void setColorTransform(@Size(9) float[] matrix, @Size(3) float[] translation) {
1260 checkNotReleased();
1261 synchronized (SurfaceControl.class) {
1262 sGlobalTransaction.setColorTransform(this, matrix, translation);
1263 }
1264 }
1265
Vishnu Naird454442d2018-11-13 13:51:01 -08001266 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08001267 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
1268 * the color can be interpreted in any color space.
1269 * @param agnostic A boolean to indicate whether the surface is color space agnostic
1270 * @hide
1271 */
1272 public void setColorSpaceAgnostic(boolean agnostic) {
1273 checkNotReleased();
1274 synchronized (SurfaceControl.class) {
1275 sGlobalTransaction.setColorSpaceAgnostic(this, agnostic);
1276 }
1277 }
1278
1279 /**
Vishnu Naird454442d2018-11-13 13:51:01 -08001280 * Bounds the surface and its children to the bounds specified. Size of the surface will be
1281 * ignored and only the crop and buffer size will be used to determine the bounds of the
1282 * surface. If no crop is specified and the surface has no buffer, the surface bounds is only
1283 * constrained by the size of its parent bounds.
1284 *
1285 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08001286 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001287 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001288 public void setWindowCrop(Rect crop) {
1289 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001290 synchronized (SurfaceControl.class) {
1291 sGlobalTransaction.setWindowCrop(this, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001292 }
1293 }
1294
Vishnu Naird454442d2018-11-13 13:51:01 -08001295 /**
1296 * Same as {@link SurfaceControl#setWindowCrop(Rect)} but sets the crop rect top left at 0, 0.
1297 *
1298 * @param width width of crop rect
1299 * @param height height of crop rect
Robert Carra7827f72019-01-11 12:53:37 -08001300 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001301 */
1302 public void setWindowCrop(int width, int height) {
1303 checkNotReleased();
1304 synchronized (SurfaceControl.class) {
1305 sGlobalTransaction.setWindowCrop(this, width, height);
1306 }
1307 }
1308
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07001309 /**
1310 * Sets the corner radius of a {@link SurfaceControl}.
1311 *
1312 * @param cornerRadius Corner radius in pixels.
Robert Carra7827f72019-01-11 12:53:37 -08001313 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07001314 */
1315 public void setCornerRadius(float cornerRadius) {
1316 checkNotReleased();
1317 synchronized (SurfaceControl.class) {
1318 sGlobalTransaction.setCornerRadius(this, cornerRadius);
1319 }
1320 }
1321
Robert Carra7827f72019-01-11 12:53:37 -08001322 /**
1323 * @hide
1324 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001325 public void setLayerStack(int layerStack) {
1326 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001327 synchronized(SurfaceControl.class) {
1328 sGlobalTransaction.setLayerStack(this, layerStack);
1329 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001330 }
1331
Robert Carra7827f72019-01-11 12:53:37 -08001332 /**
1333 * @hide
1334 */
Andy McFadden314405b2014-01-29 17:18:05 -08001335 public void setOpaque(boolean isOpaque) {
1336 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001337
1338 synchronized (SurfaceControl.class) {
1339 sGlobalTransaction.setOpaque(this, isOpaque);
Andy McFadden314405b2014-01-29 17:18:05 -08001340 }
1341 }
1342
Robert Carra7827f72019-01-11 12:53:37 -08001343 /**
1344 * @hide
1345 */
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001346 public void setSecure(boolean isSecure) {
1347 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001348
1349 synchronized (SurfaceControl.class) {
1350 sGlobalTransaction.setSecure(this, isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001351 }
1352 }
1353
Robert Carra7827f72019-01-11 12:53:37 -08001354 /**
1355 * @hide
1356 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001357 public int getWidth() {
1358 synchronized (mSizeLock) {
1359 return mWidth;
1360 }
1361 }
1362
Robert Carra7827f72019-01-11 12:53:37 -08001363 /**
1364 * @hide
1365 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001366 public int getHeight() {
1367 synchronized (mSizeLock) {
1368 return mHeight;
1369 }
1370 }
1371
Robert Carre13b58e2017-08-31 14:50:44 -07001372 @Override
1373 public String toString() {
1374 return "Surface(name=" + mName + ")/@0x" +
1375 Integer.toHexString(System.identityHashCode(this));
1376 }
1377
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001378 /*
1379 * set display parameters.
1380 * needs to be inside open/closeTransaction block
1381 */
1382
1383 /**
1384 * Describes the properties of a physical display known to surface flinger.
Robert Carr76907ee2019-01-11 13:38:19 -08001385 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001386 */
1387 public static final class PhysicalDisplayInfo {
Robert Carra7827f72019-01-11 12:53:37 -08001388 /**
1389 * @hide
1390 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001391 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001392 public int width;
Robert Carra7827f72019-01-11 12:53:37 -08001393
1394 /**
1395 * @hide
1396 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001397 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001398 public int height;
Robert Carra7827f72019-01-11 12:53:37 -08001399
1400 /**
1401 * @hide
1402 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001403 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001404 public float refreshRate;
Robert Carra7827f72019-01-11 12:53:37 -08001405
1406 /**
1407 * @hide
1408 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001409 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001410 public float density;
Robert Carra7827f72019-01-11 12:53:37 -08001411
1412 /**
1413 * @hide
1414 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001415 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001416 public float xDpi;
Robert Carra7827f72019-01-11 12:53:37 -08001417
1418 /**
1419 * @hide
1420 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001421 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001422 public float yDpi;
Robert Carra7827f72019-01-11 12:53:37 -08001423
1424 /**
1425 * @hide
1426 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001427 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001428 public boolean secure;
Robert Carra7827f72019-01-11 12:53:37 -08001429
1430 /**
1431 * @hide
1432 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001433 @UnsupportedAppUsage
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001434 public long appVsyncOffsetNanos;
Robert Carra7827f72019-01-11 12:53:37 -08001435
1436 /**
1437 * @hide
1438 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001439 @UnsupportedAppUsage
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001440 public long presentationDeadlineNanos;
Jesse Hall6a6bc212013-08-08 12:15:03 -07001441
Robert Carra7827f72019-01-11 12:53:37 -08001442 /**
1443 * @hide
1444 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001445 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001446 public PhysicalDisplayInfo() {
1447 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001448
Robert Carra7827f72019-01-11 12:53:37 -08001449 /**
1450 * @hide
1451 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001452 public PhysicalDisplayInfo(PhysicalDisplayInfo other) {
1453 copyFrom(other);
1454 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001455
Robert Carra7827f72019-01-11 12:53:37 -08001456 /**
1457 * @hide
1458 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001459 @Override
1460 public boolean equals(Object o) {
1461 return o instanceof PhysicalDisplayInfo && equals((PhysicalDisplayInfo)o);
1462 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001463
Robert Carra7827f72019-01-11 12:53:37 -08001464 /**
1465 * @hide
1466 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001467 public boolean equals(PhysicalDisplayInfo other) {
1468 return other != null
1469 && width == other.width
1470 && height == other.height
1471 && refreshRate == other.refreshRate
1472 && density == other.density
1473 && xDpi == other.xDpi
1474 && yDpi == other.yDpi
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001475 && secure == other.secure
1476 && appVsyncOffsetNanos == other.appVsyncOffsetNanos
Michael Wright1c9977b2016-07-12 13:30:10 -07001477 && presentationDeadlineNanos == other.presentationDeadlineNanos;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001478 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001479
Robert Carra7827f72019-01-11 12:53:37 -08001480 /**
1481 * @hide
1482 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001483 @Override
1484 public int hashCode() {
1485 return 0; // don't care
1486 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001487
Robert Carra7827f72019-01-11 12:53:37 -08001488 /**
1489 * @hide
1490 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001491 public void copyFrom(PhysicalDisplayInfo other) {
1492 width = other.width;
1493 height = other.height;
1494 refreshRate = other.refreshRate;
1495 density = other.density;
1496 xDpi = other.xDpi;
1497 yDpi = other.yDpi;
1498 secure = other.secure;
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001499 appVsyncOffsetNanos = other.appVsyncOffsetNanos;
1500 presentationDeadlineNanos = other.presentationDeadlineNanos;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001501 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001502
Robert Carra7827f72019-01-11 12:53:37 -08001503 /**
1504 * @hide
1505 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001506 @Override
1507 public String toString() {
1508 return "PhysicalDisplayInfo{" + width + " x " + height + ", " + refreshRate + " fps, "
1509 + "density " + density + ", " + xDpi + " x " + yDpi + " dpi, secure " + secure
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001510 + ", appVsyncOffset " + appVsyncOffsetNanos
Michael Wright1c9977b2016-07-12 13:30:10 -07001511 + ", bufferDeadline " + presentationDeadlineNanos + "}";
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001512 }
1513 }
1514
Robert Carra7827f72019-01-11 12:53:37 -08001515 /**
1516 * @hide
1517 */
Prashant Malanic55929a2014-05-25 01:59:21 -07001518 public static void setDisplayPowerMode(IBinder displayToken, int mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001519 if (displayToken == null) {
1520 throw new IllegalArgumentException("displayToken must not be null");
1521 }
Prashant Malanic55929a2014-05-25 01:59:21 -07001522 nativeSetDisplayPowerMode(displayToken, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001523 }
1524
Robert Carra7827f72019-01-11 12:53:37 -08001525 /**
1526 * @hide
1527 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001528 @UnsupportedAppUsage
Dan Stoza00101052014-05-02 15:23:40 -07001529 public static SurfaceControl.PhysicalDisplayInfo[] getDisplayConfigs(IBinder displayToken) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001530 if (displayToken == null) {
1531 throw new IllegalArgumentException("displayToken must not be null");
1532 }
Dan Stoza00101052014-05-02 15:23:40 -07001533 return nativeGetDisplayConfigs(displayToken);
1534 }
1535
Robert Carra7827f72019-01-11 12:53:37 -08001536 /**
1537 * @hide
1538 */
Dan Stoza00101052014-05-02 15:23:40 -07001539 public static int getActiveConfig(IBinder displayToken) {
1540 if (displayToken == null) {
1541 throw new IllegalArgumentException("displayToken must not be null");
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001542 }
Dan Stoza00101052014-05-02 15:23:40 -07001543 return nativeGetActiveConfig(displayToken);
1544 }
1545
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001546 /**
1547 * @hide
1548 */
1549 public static DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
1550 IBinder displayToken) {
1551 if (displayToken == null) {
1552 throw new IllegalArgumentException("displayToken must not be null");
1553 }
1554 return nativeGetDisplayedContentSamplingAttributes(displayToken);
1555 }
1556
1557 /**
1558 * @hide
1559 */
1560 public static boolean setDisplayedContentSamplingEnabled(
1561 IBinder displayToken, boolean enable, int componentMask, int maxFrames) {
1562 if (displayToken == null) {
1563 throw new IllegalArgumentException("displayToken must not be null");
1564 }
1565 final int maxColorComponents = 4;
1566 if ((componentMask >> maxColorComponents) != 0) {
1567 throw new IllegalArgumentException("invalid componentMask when enabling sampling");
1568 }
1569 return nativeSetDisplayedContentSamplingEnabled(
1570 displayToken, enable, componentMask, maxFrames);
1571 }
1572
1573 /**
1574 * @hide
1575 */
1576 public static DisplayedContentSample getDisplayedContentSample(
1577 IBinder displayToken, long maxFrames, long timestamp) {
1578 if (displayToken == null) {
1579 throw new IllegalArgumentException("displayToken must not be null");
1580 }
1581 return nativeGetDisplayedContentSample(displayToken, maxFrames, timestamp);
1582 }
1583
1584
Robert Carra7827f72019-01-11 12:53:37 -08001585 /**
1586 * @hide
1587 */
Dan Stoza00101052014-05-02 15:23:40 -07001588 public static boolean setActiveConfig(IBinder displayToken, int id) {
1589 if (displayToken == null) {
1590 throw new IllegalArgumentException("displayToken must not be null");
1591 }
1592 return nativeSetActiveConfig(displayToken, id);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001593 }
1594
Robert Carra7827f72019-01-11 12:53:37 -08001595 /**
1596 * @hide
1597 */
Ady Abraham6070ce12019-01-24 18:48:58 -08001598 public static boolean setAllowedDisplayConfigs(IBinder displayToken, int[] allowedConfigs) {
1599 if (displayToken == null) {
1600 throw new IllegalArgumentException("displayToken must not be null");
1601 }
1602 if (allowedConfigs == null) {
1603 throw new IllegalArgumentException("allowedConfigs must not be null");
1604 }
1605
1606 return nativeSetAllowedDisplayConfigs(displayToken, allowedConfigs);
1607 }
1608
1609 /**
1610 * @hide
1611 */
Ady Abraham42f9a2fb2019-02-26 14:13:39 -08001612 public static int[] getAllowedDisplayConfigs(IBinder displayToken) {
1613 if (displayToken == null) {
1614 throw new IllegalArgumentException("displayToken must not be null");
1615 }
1616 return nativeGetAllowedDisplayConfigs(displayToken);
1617 }
1618
1619 /**
1620 * @hide
1621 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001622 public static int[] getDisplayColorModes(IBinder displayToken) {
1623 if (displayToken == null) {
1624 throw new IllegalArgumentException("displayToken must not be null");
1625 }
1626 return nativeGetDisplayColorModes(displayToken);
1627 }
1628
Robert Carra7827f72019-01-11 12:53:37 -08001629 /**
Daniel Solomon10e3b332019-01-20 21:09:11 -08001630 * Color coordinates in CIE1931 XYZ color space
1631 *
1632 * @hide
1633 */
1634 public static final class CieXyz {
1635 /**
1636 * @hide
1637 */
1638 public float X;
1639
1640 /**
1641 * @hide
1642 */
1643 public float Y;
1644
1645 /**
1646 * @hide
1647 */
1648 public float Z;
1649 }
1650
1651 /**
1652 * Contains a display's color primaries
1653 *
1654 * @hide
1655 */
1656 public static final class DisplayPrimaries {
1657 /**
1658 * @hide
1659 */
1660 public CieXyz red;
1661
1662 /**
1663 * @hide
1664 */
1665 public CieXyz green;
1666
1667 /**
1668 * @hide
1669 */
1670 public CieXyz blue;
1671
1672 /**
1673 * @hide
1674 */
1675 public CieXyz white;
1676
1677 /**
1678 * @hide
1679 */
1680 public DisplayPrimaries() {
1681 }
1682 }
1683
1684 /**
1685 * @hide
1686 */
1687 public static SurfaceControl.DisplayPrimaries getDisplayNativePrimaries(
1688 IBinder displayToken) {
1689 if (displayToken == null) {
1690 throw new IllegalArgumentException("displayToken must not be null");
1691 }
1692
1693 return nativeGetDisplayNativePrimaries(displayToken);
1694 }
1695
1696 /**
Robert Carra7827f72019-01-11 12:53:37 -08001697 * @hide
1698 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001699 public static int getActiveColorMode(IBinder displayToken) {
1700 if (displayToken == null) {
1701 throw new IllegalArgumentException("displayToken must not be null");
1702 }
1703 return nativeGetActiveColorMode(displayToken);
1704 }
1705
Robert Carra7827f72019-01-11 12:53:37 -08001706 /**
1707 * @hide
1708 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001709 public static boolean setActiveColorMode(IBinder displayToken, int colorMode) {
1710 if (displayToken == null) {
1711 throw new IllegalArgumentException("displayToken must not be null");
1712 }
1713 return nativeSetActiveColorMode(displayToken, colorMode);
1714 }
1715
Robert Carra7827f72019-01-11 12:53:37 -08001716 /**
Peiyong Lin5f4a5682019-01-17 11:37:07 -08001717 * Returns an array of color spaces with 2 elements. The first color space is the
1718 * default color space and second one is wide color gamut color space.
1719 * @hide
1720 */
1721 public static ColorSpace[] getCompositionColorSpaces() {
1722 int[] dataspaces = nativeGetCompositionDataspaces();
1723 ColorSpace srgb = ColorSpace.get(ColorSpace.Named.SRGB);
1724 ColorSpace[] colorSpaces = { srgb, srgb };
1725 if (dataspaces.length == 2) {
1726 for (int i = 0; i < 2; ++i) {
1727 switch(dataspaces[i]) {
1728 case INTERNAL_DATASPACE_DISPLAY_P3:
1729 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.DISPLAY_P3);
1730 break;
1731 case INTERNAL_DATASPACE_SCRGB:
1732 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.EXTENDED_SRGB);
1733 break;
1734 case INTERNAL_DATASPACE_SRGB:
1735 // Other dataspace is not recognized, use SRGB color space instead,
1736 // the default value of the array is already SRGB, thus do nothing.
1737 default:
1738 break;
1739 }
1740 }
1741 }
1742 return colorSpaces;
1743 }
1744
1745 /**
Robert Carra7827f72019-01-11 12:53:37 -08001746 * @hide
1747 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001748 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001749 public static void setDisplayProjection(IBinder displayToken,
1750 int orientation, Rect layerStackRect, Rect displayRect) {
Robert Carre13b58e2017-08-31 14:50:44 -07001751 synchronized (SurfaceControl.class) {
1752 sGlobalTransaction.setDisplayProjection(displayToken, orientation,
1753 layerStackRect, displayRect);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001754 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001755 }
1756
Robert Carra7827f72019-01-11 12:53:37 -08001757 /**
1758 * @hide
1759 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001760 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001761 public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -07001762 synchronized (SurfaceControl.class) {
1763 sGlobalTransaction.setDisplayLayerStack(displayToken, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001764 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001765 }
1766
Robert Carra7827f72019-01-11 12:53:37 -08001767 /**
1768 * @hide
1769 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001770 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001771 public static void setDisplaySurface(IBinder displayToken, Surface surface) {
Robert Carre13b58e2017-08-31 14:50:44 -07001772 synchronized (SurfaceControl.class) {
1773 sGlobalTransaction.setDisplaySurface(displayToken, surface);
Jeff Brownfc0ebd72013-04-30 16:33:00 -07001774 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001775 }
1776
Robert Carra7827f72019-01-11 12:53:37 -08001777 /**
1778 * @hide
1779 */
Michael Wright01e840f2014-06-26 16:03:25 -07001780 public static void setDisplaySize(IBinder displayToken, int width, int height) {
Robert Carre13b58e2017-08-31 14:50:44 -07001781 synchronized (SurfaceControl.class) {
1782 sGlobalTransaction.setDisplaySize(displayToken, width, height);
Michael Wright01e840f2014-06-26 16:03:25 -07001783 }
Michael Wright01e840f2014-06-26 16:03:25 -07001784 }
1785
Robert Carra7827f72019-01-11 12:53:37 -08001786 /**
1787 * @hide
1788 */
Michael Wright9ff94c02016-03-30 18:05:40 -07001789 public static Display.HdrCapabilities getHdrCapabilities(IBinder displayToken) {
1790 if (displayToken == null) {
1791 throw new IllegalArgumentException("displayToken must not be null");
1792 }
1793 return nativeGetHdrCapabilities(displayToken);
1794 }
1795
Robert Carra7827f72019-01-11 12:53:37 -08001796 /**
1797 * @hide
1798 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001799 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001800 public static IBinder createDisplay(String name, boolean secure) {
1801 if (name == null) {
1802 throw new IllegalArgumentException("name must not be null");
1803 }
1804 return nativeCreateDisplay(name, secure);
1805 }
1806
Robert Carra7827f72019-01-11 12:53:37 -08001807 /**
1808 * @hide
1809 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001810 @UnsupportedAppUsage
Jesse Hall6a6bc212013-08-08 12:15:03 -07001811 public static void destroyDisplay(IBinder displayToken) {
1812 if (displayToken == null) {
1813 throw new IllegalArgumentException("displayToken must not be null");
1814 }
1815 nativeDestroyDisplay(displayToken);
1816 }
1817
Robert Carra7827f72019-01-11 12:53:37 -08001818 /**
1819 * @hide
1820 */
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001821 public static long[] getPhysicalDisplayIds() {
1822 return nativeGetPhysicalDisplayIds();
1823 }
1824
1825 /**
1826 * @hide
1827 */
1828 public static IBinder getPhysicalDisplayToken(long physicalDisplayId) {
1829 return nativeGetPhysicalDisplayToken(physicalDisplayId);
1830 }
1831
1832 /**
1833 * TODO(116025192): Remove this stopgap once framework is display-agnostic.
1834 *
1835 * @hide
1836 */
1837 public static IBinder getInternalDisplayToken() {
1838 final long[] physicalDisplayIds = getPhysicalDisplayIds();
1839 if (physicalDisplayIds.length == 0) {
1840 return null;
1841 }
1842 return getPhysicalDisplayToken(physicalDisplayIds[0]);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001843 }
1844
Mathias Agopian0449a402013-03-01 23:01:51 -08001845 /**
chaviw08520a02018-09-10 16:44:56 -07001846 * @see SurfaceControl#screenshot(IBinder, Surface, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001847 * @hide
Mathias Agopian0449a402013-03-01 23:01:51 -08001848 */
1849 public static void screenshot(IBinder display, Surface consumer) {
chaviw08520a02018-09-10 16:44:56 -07001850 screenshot(display, consumer, new Rect(), 0, 0, false, 0);
1851 }
1852
1853 /**
1854 * Copy the current screen contents into the provided {@link Surface}
1855 *
1856 * @param consumer The {@link Surface} to take the screenshot into.
1857 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001858 * @hide
chaviw08520a02018-09-10 16:44:56 -07001859 */
1860 public static void screenshot(IBinder display, Surface consumer, Rect sourceCrop, int width,
1861 int height, boolean useIdentityTransform, int rotation) {
1862 if (consumer == null) {
1863 throw new IllegalArgumentException("consumer must not be null");
1864 }
1865
Peiyong Line3e5efd2019-03-21 20:59:47 +00001866 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(display, sourceCrop, width,
1867 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07001868 try {
Peiyong Linccc06b62019-06-25 17:31:09 -07001869 consumer.attachAndQueueBufferWithColorSpace(buffer.getGraphicBuffer(),
1870 buffer.getColorSpace());
chaviw08520a02018-09-10 16:44:56 -07001871 } catch (RuntimeException e) {
1872 Log.w(TAG, "Failed to take screenshot - " + e.getMessage());
1873 }
1874 }
1875
1876 /**
1877 * @see SurfaceControl#screenshot(Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001878 * @hide
chaviw08520a02018-09-10 16:44:56 -07001879 */
1880 @UnsupportedAppUsage
1881 public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) {
1882 return screenshot(sourceCrop, width, height, false, rotation);
Mathias Agopian0449a402013-03-01 23:01:51 -08001883 }
1884
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001885 /**
chaviw1da9cd92017-12-06 10:48:11 -08001886 * Copy the current screen contents into a hardware bitmap and return it.
1887 * Note: If you want to modify the Bitmap in software, you will need to copy the Bitmap into
1888 * a software Bitmap using {@link Bitmap#copy(Bitmap.Config, boolean)}
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001889 *
chaviw08520a02018-09-10 16:44:56 -07001890 * CAVEAT: Versions of screenshot that return a {@link Bitmap} can be extremely slow; avoid use
1891 * unless absolutely necessary; prefer the versions that use a {@link Surface} such as
1892 * {@link SurfaceControl#screenshot(IBinder, Surface)} or {@link GraphicBuffer} such as
1893 * {@link SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}.
Mathias Agopian0449a402013-03-01 23:01:51 -08001894 *
chaviw08520a02018-09-10 16:44:56 -07001895 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001896 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001897 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001898 @UnsupportedAppUsage
Dan Stoza9890e3412014-05-22 16:12:54 -07001899 public static Bitmap screenshot(Rect sourceCrop, int width, int height,
chaviw08520a02018-09-10 16:44:56 -07001900 boolean useIdentityTransform, int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001901 // TODO: should take the display as a parameter
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001902 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
1903 if (displayToken == null) {
1904 Log.w(TAG, "Failed to take screenshot because internal display is disconnected");
1905 return null;
1906 }
1907
chaviwa69e0a72017-11-29 17:55:12 -08001908 if (rotation == ROTATION_90 || rotation == ROTATION_270) {
1909 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
1910 }
1911
1912 SurfaceControl.rotateCropForSF(sourceCrop, rotation);
Peiyong Line3e5efd2019-03-21 20:59:47 +00001913 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(displayToken, sourceCrop, width,
1914 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07001915
1916 if (buffer == null) {
1917 Log.w(TAG, "Failed to take screenshot");
1918 return null;
1919 }
Sunny Goyal62915b22019-04-10 12:28:47 -07001920 return Bitmap.wrapHardwareBuffer(buffer.getGraphicBuffer(), buffer.getColorSpace());
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001921 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001922
chaviw08520a02018-09-10 16:44:56 -07001923 /**
1924 * Captures all the surfaces in a display and returns a {@link GraphicBuffer} with the content.
1925 *
1926 * @param display The display to take the screenshot of.
1927 * @param sourceCrop The portion of the screen to capture into the Bitmap; caller may
1928 * pass in 'new Rect()' if no cropping is desired.
1929 * @param width The desired width of the returned bitmap; the raw screen will be
1930 * scaled down to this size; caller may pass in 0 if no scaling is
1931 * desired.
1932 * @param height The desired height of the returned bitmap; the raw screen will
1933 * be scaled down to this size; caller may pass in 0 if no scaling
1934 * is desired.
1935 * @param useIdentityTransform Replace whatever transformation (rotation, scaling, translation)
1936 * the surface layers are currently using with the identity
1937 * transformation while taking the screenshot.
1938 * @param rotation Apply a custom clockwise rotation to the screenshot, i.e.
1939 * Surface.ROTATION_0,90,180,270. SurfaceFlinger will always take
1940 * screenshots in its native portrait orientation by default, so
1941 * this is useful for returning screenshots that are independent of
1942 * device orientation.
1943 * @return Returns a GraphicBuffer that contains the captured content.
Robert Carra7827f72019-01-11 12:53:37 -08001944 * @hide
chaviw08520a02018-09-10 16:44:56 -07001945 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00001946 public static ScreenshotGraphicBuffer screenshotToBuffer(IBinder display, Rect sourceCrop,
1947 int width, int height, boolean useIdentityTransform, int rotation) {
Mathias Agopian0449a402013-03-01 23:01:51 -08001948 if (display == null) {
1949 throw new IllegalArgumentException("displayToken must not be null");
1950 }
chaviw08520a02018-09-10 16:44:56 -07001951
Robert Carr5c52b132019-02-15 15:48:11 -08001952 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1953 false /* captureSecureLayers */);
1954 }
1955
1956 /**
1957 * Like screenshotToBuffer, but if the caller is AID_SYSTEM, allows
1958 * for the capture of secure layers. This is used for the screen rotation
1959 * animation where the system server takes screenshots but does
1960 * not persist them or allow them to leave the server. However in other
1961 * cases in the system server, we mostly want to omit secure layers
1962 * like when we take a screenshot on behalf of the assistant.
1963 *
1964 * @hide
1965 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00001966 public static ScreenshotGraphicBuffer screenshotToBufferWithSecureLayersUnsafe(IBinder display,
Robert Carr5c52b132019-02-15 15:48:11 -08001967 Rect sourceCrop, int width, int height, boolean useIdentityTransform,
1968 int rotation) {
1969 if (display == null) {
1970 throw new IllegalArgumentException("displayToken must not be null");
1971 }
1972
1973 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1974 true /* captureSecureLayers */);
Mathias Agopian0449a402013-03-01 23:01:51 -08001975 }
Robert Carre13b58e2017-08-31 14:50:44 -07001976
chaviwa69e0a72017-11-29 17:55:12 -08001977 private static void rotateCropForSF(Rect crop, int rot) {
1978 if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270) {
1979 int tmp = crop.top;
1980 crop.top = crop.left;
1981 crop.left = tmp;
1982 tmp = crop.right;
1983 crop.right = crop.bottom;
1984 crop.bottom = tmp;
1985 }
1986 }
1987
chaviw1cda84c2017-10-23 16:47:10 -07001988 /**
chaviw1da9cd92017-12-06 10:48:11 -08001989 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
chaviw1cda84c2017-10-23 16:47:10 -07001990 *
1991 * @param layerHandleToken The root layer to capture.
chaviwfbe47df2017-11-10 16:14:49 -08001992 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
1993 * Rect()' or null if no cropping is desired.
1994 * @param frameScale The desired scale of the returned buffer; the raw
1995 * screen will be scaled up/down.
Chavi Weingartend7ec64c2017-11-30 01:52:01 +00001996 *
1997 * @return Returns a GraphicBuffer that contains the layer capture.
Robert Carra7827f72019-01-11 12:53:37 -08001998 * @hide
chaviw1cda84c2017-10-23 16:47:10 -07001999 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00002000 public static ScreenshotGraphicBuffer captureLayers(IBinder layerHandleToken, Rect sourceCrop,
chaviwfbe47df2017-11-10 16:14:49 -08002001 float frameScale) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07002002 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
2003 return nativeCaptureLayers(displayToken, layerHandleToken, sourceCrop, frameScale, null);
Robert Carrffcdc512019-04-02 11:51:11 -07002004 }
2005
2006 /**
2007 * Like {@link captureLayers} but with an array of layer handles to exclude.
2008 * @hide
2009 */
2010 public static ScreenshotGraphicBuffer captureLayersExcluding(IBinder layerHandleToken,
2011 Rect sourceCrop, float frameScale, IBinder[] exclude) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07002012 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
2013 return nativeCaptureLayers(displayToken, layerHandleToken, sourceCrop, frameScale, exclude);
chaviw1cda84c2017-10-23 16:47:10 -07002014 }
2015
Robert Carra7827f72019-01-11 12:53:37 -08002016 /**
Peiyong Lin0ddb7d42019-01-16 13:25:09 -08002017 * Returns whether protected content is supported in GPU composition.
2018 * @hide
2019 */
2020 public static boolean getProtectedContentSupport() {
2021 return nativeGetProtectedContentSupport();
2022 }
2023
2024 /**
Dan Gittik832b4972019-02-13 18:17:47 +00002025 * Returns whether brightness operations are supported on a display.
2026 *
2027 * @param displayToken
2028 * The token for the display.
2029 *
2030 * @return Whether brightness operations are supported on the display.
2031 *
2032 * @hide
2033 */
2034 public static boolean getDisplayBrightnessSupport(IBinder displayToken) {
2035 return nativeGetDisplayBrightnessSupport(displayToken);
2036 }
2037
2038 /**
2039 * Sets the brightness of a display.
2040 *
2041 * @param displayToken
2042 * The token for the display whose brightness is set.
2043 * @param brightness
2044 * A number between 0.0f (minimum brightness) and 1.0f (maximum brightness), or -1.0f to
2045 * turn the backlight off.
2046 *
2047 * @return Whether the method succeeded or not.
2048 *
2049 * @throws IllegalArgumentException if:
2050 * - displayToken is null;
2051 * - brightness is NaN or greater than 1.0f.
2052 *
2053 * @hide
2054 */
2055 public static boolean setDisplayBrightness(IBinder displayToken, float brightness) {
2056 Objects.requireNonNull(displayToken);
2057 if (Float.isNaN(brightness) || brightness > 1.0f
2058 || (brightness < 0.0f && brightness != -1.0f)) {
2059 throw new IllegalArgumentException("brightness must be a number between 0.0f and 1.0f,"
2060 + " or -1 to turn the backlight off.");
2061 }
2062 return nativeSetDisplayBrightness(displayToken, brightness);
2063 }
2064
2065 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002066 * An atomic set of changes to a set of SurfaceControl.
Robert Carra7827f72019-01-11 12:53:37 -08002067 */
Robert Carre13b58e2017-08-31 14:50:44 -07002068 public static class Transaction implements Closeable {
Robert Carr76907ee2019-01-11 13:38:19 -08002069 /**
2070 * @hide
2071 */
Robert Carre13b58e2017-08-31 14:50:44 -07002072 public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
2073 Transaction.class.getClassLoader(),
2074 nativeGetNativeTransactionFinalizer(), 512);
2075 private long mNativeObject;
2076
Jorim Jaggia5e10572017-11-15 14:36:26 +01002077 private final ArrayMap<SurfaceControl, Point> mResizedSurfaces = new ArrayMap<>();
Robert Carre13b58e2017-08-31 14:50:44 -07002078 Runnable mFreeNativeResources;
2079
Robert Carra7827f72019-01-11 12:53:37 -08002080 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002081 * Open a new transaction object. The transaction may be filed with commands to
2082 * manipulate {@link SurfaceControl} instances, and then applied atomically with
2083 * {@link #apply}. Eventually the user should invoke {@link #close}, when the object
2084 * is no longer required. Note however that re-using a transaction after a call to apply
2085 * is allowed as a convenience.
Robert Carra7827f72019-01-11 12:53:37 -08002086 */
Robert Carre13b58e2017-08-31 14:50:44 -07002087 public Transaction() {
2088 mNativeObject = nativeCreateTransaction();
2089 mFreeNativeResources
2090 = sRegistry.registerNativeAllocation(this, mNativeObject);
2091 }
2092
2093 /**
2094 * Apply the transaction, clearing it's state, and making it usable
2095 * as a new transaction.
2096 */
2097 public void apply() {
2098 apply(false);
2099 }
2100
2101 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002102 * Release the native transaction object, without applying it.
Robert Carre13b58e2017-08-31 14:50:44 -07002103 */
2104 @Override
2105 public void close() {
2106 mFreeNativeResources.run();
2107 mNativeObject = 0;
2108 }
2109
2110 /**
2111 * Jankier version of apply. Avoid use (b/28068298).
Robert Carra7827f72019-01-11 12:53:37 -08002112 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002113 */
2114 public void apply(boolean sync) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002115 applyResizedSurfaces();
Robert Carre13b58e2017-08-31 14:50:44 -07002116 nativeApplyTransaction(mNativeObject, sync);
2117 }
2118
Jorim Jaggia5e10572017-11-15 14:36:26 +01002119 private void applyResizedSurfaces() {
2120 for (int i = mResizedSurfaces.size() - 1; i >= 0; i--) {
2121 final Point size = mResizedSurfaces.valueAt(i);
2122 final SurfaceControl surfaceControl = mResizedSurfaces.keyAt(i);
2123 synchronized (surfaceControl.mSizeLock) {
2124 surfaceControl.mWidth = size.x;
2125 surfaceControl.mHeight = size.y;
2126 }
2127 }
2128 mResizedSurfaces.clear();
2129 }
2130
Robert Carra7827f72019-01-11 12:53:37 -08002131 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002132 * Toggle the visibility of a given Layer and it's sub-tree.
2133 *
2134 * @param sc The SurfaceControl for which to set the visibility
2135 * @param visible The new visibility
2136 * @return This transaction object.
2137 */
2138 @NonNull
2139 public Transaction setVisibility(@NonNull SurfaceControl sc, boolean visible) {
2140 sc.checkNotReleased();
2141 if (visible) {
2142 return show(sc);
2143 } else {
2144 return hide(sc);
2145 }
2146 }
2147
2148 /**
2149 * Request that a given surface and it's sub-tree be shown.
2150 *
2151 * @param sc The surface to show.
2152 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002153 * @hide
2154 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002155 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002156 public Transaction show(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002157 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002158 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_HIDDEN);
2159 return this;
2160 }
2161
Robert Carra7827f72019-01-11 12:53:37 -08002162 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002163 * Request that a given surface and it's sub-tree be hidden.
2164 *
2165 * @param sc The surface to hidden.
2166 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002167 * @hide
2168 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002169 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002170 public Transaction hide(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002171 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002172 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_HIDDEN, SURFACE_HIDDEN);
2173 return this;
2174 }
2175
Robert Carra7827f72019-01-11 12:53:37 -08002176 /**
2177 * @hide
2178 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002179 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002180 public Transaction setPosition(SurfaceControl sc, float x, float y) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002181 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002182 nativeSetPosition(mNativeObject, sc.mNativeObject, x, y);
2183 return this;
2184 }
2185
Robert Carra7827f72019-01-11 12:53:37 -08002186 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002187 * Set the default buffer size for the SurfaceControl, if there is a
2188 * {@link Surface} associated with the control, then
Robert Carr76907ee2019-01-11 13:38:19 -08002189 * this will be the default size for buffers dequeued from it.
2190 * @param sc The surface to set the buffer size for.
2191 * @param w The default width
2192 * @param h The default height
2193 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002194 */
Robert Carr76907ee2019-01-11 13:38:19 -08002195 @NonNull
2196 public Transaction setBufferSize(@NonNull SurfaceControl sc,
2197 @IntRange(from = 0) int w, @IntRange(from = 0) int h) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002198 sc.checkNotReleased();
Jorim Jaggia5e10572017-11-15 14:36:26 +01002199 mResizedSurfaces.put(sc, new Point(w, h));
Jorim Jaggidfc27372017-10-27 17:47:49 +02002200 nativeSetSize(mNativeObject, sc.mNativeObject, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07002201 return this;
2202 }
2203
Robert Carra7827f72019-01-11 12:53:37 -08002204 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002205 * Set the Z-order for a given SurfaceControl, relative to it's siblings.
2206 * If two siblings share the same Z order the ordering is undefined. Surfaces
2207 * with a negative Z will be placed below the parent surface.
2208 *
2209 * @param sc The SurfaceControl to set the Z order on
2210 * @param z The Z-order
2211 * @return This Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002212 */
Robert Carr76907ee2019-01-11 13:38:19 -08002213 @NonNull
2214 public Transaction setLayer(@NonNull SurfaceControl sc,
2215 @IntRange(from = Integer.MIN_VALUE, to = Integer.MAX_VALUE) int z) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002216 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002217 nativeSetLayer(mNativeObject, sc.mNativeObject, z);
2218 return this;
2219 }
2220
Robert Carra7827f72019-01-11 12:53:37 -08002221 /**
2222 * @hide
2223 */
Robert Carr77e34942017-10-18 19:13:56 -07002224 public Transaction setRelativeLayer(SurfaceControl sc, SurfaceControl relativeTo, int z) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002225 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002226 nativeSetRelativeLayer(mNativeObject, sc.mNativeObject,
Robert Carr77e34942017-10-18 19:13:56 -07002227 relativeTo.getHandle(), z);
Robert Carre13b58e2017-08-31 14:50:44 -07002228 return this;
2229 }
2230
Robert Carra7827f72019-01-11 12:53:37 -08002231 /**
2232 * @hide
2233 */
Robert Carre13b58e2017-08-31 14:50:44 -07002234 public Transaction setTransparentRegionHint(SurfaceControl sc, Region transparentRegion) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002235 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002236 nativeSetTransparentRegionHint(mNativeObject,
2237 sc.mNativeObject, transparentRegion);
2238 return this;
2239 }
2240
Robert Carra7827f72019-01-11 12:53:37 -08002241 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002242 * Set the alpha for a given surface. If the alpha is non-zero the SurfaceControl
2243 * will be blended with the Surfaces under it according to the specified ratio.
2244 *
2245 * @param sc The given SurfaceControl.
2246 * @param alpha The alpha to set.
Robert Carra7827f72019-01-11 12:53:37 -08002247 */
Robert Carr76907ee2019-01-11 13:38:19 -08002248 @NonNull
2249 public Transaction setAlpha(@NonNull SurfaceControl sc,
2250 @FloatRange(from = 0.0, to = 1.0) float alpha) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002251 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002252 nativeSetAlpha(mNativeObject, sc.mNativeObject, alpha);
2253 return this;
2254 }
2255
Robert Carra7827f72019-01-11 12:53:37 -08002256 /**
2257 * @hide
2258 */
Robert Carr788f5742018-07-30 17:46:45 -07002259 public Transaction setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle) {
2260 sc.checkNotReleased();
2261 nativeSetInputWindowInfo(mNativeObject, sc.mNativeObject, handle);
2262 return this;
2263 }
2264
chaviw59f532e2018-12-26 15:34:59 -08002265 /**
2266 * Transfers touch focus from one window to another. It is possible for multiple windows to
2267 * have touch focus if they support split touch dispatch
2268 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this
2269 * method only transfers touch focus of the specified window without affecting
2270 * other windows that may also have touch focus at the same time.
2271 * @param fromToken The token of a window that currently has touch focus.
2272 * @param toToken The token of the window that should receive touch focus in
2273 * place of the first.
Robert Carra7827f72019-01-11 12:53:37 -08002274 * @hide
chaviw59f532e2018-12-26 15:34:59 -08002275 */
2276 public Transaction transferTouchFocus(IBinder fromToken, IBinder toToken) {
2277 nativeTransferTouchFocus(mNativeObject, fromToken, toToken);
2278 return this;
2279 }
2280
Robert Carra7827f72019-01-11 12:53:37 -08002281 /**
chaviw319cd0782019-02-14 11:00:23 -08002282 * Waits until any changes to input windows have been sent from SurfaceFlinger to
2283 * InputFlinger before returning.
2284 *
2285 * @hide
2286 */
2287 public Transaction syncInputWindows() {
2288 nativeSyncInputWindows(mNativeObject);
2289 return this;
2290 }
2291
2292 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002293 * Specify how the buffer assosciated with this Surface is mapped in to the
2294 * parent coordinate space. The source frame will be scaled to fit the destination
2295 * frame, after being rotated according to the orientation parameter.
2296 *
2297 * @param sc The SurfaceControl to specify the geometry of
2298 * @param sourceCrop The source rectangle in buffer space. Or null for the entire buffer.
2299 * @param destFrame The destination rectangle in parent space. Or null for the source frame.
2300 * @param orientation The buffer rotation
2301 * @return This transaction object.
2302 */
2303 @NonNull
2304 public Transaction setGeometry(@NonNull SurfaceControl sc, @Nullable Rect sourceCrop,
2305 @Nullable Rect destFrame, @Surface.Rotation int orientation) {
2306 sc.checkNotReleased();
2307 nativeSetGeometry(mNativeObject, sc.mNativeObject, sourceCrop, destFrame, orientation);
2308 return this;
2309 }
2310
2311 /**
Robert Carra7827f72019-01-11 12:53:37 -08002312 * @hide
2313 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002314 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002315 public Transaction setMatrix(SurfaceControl sc,
2316 float dsdx, float dtdx, float dtdy, float dsdy) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002317 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002318 nativeSetMatrix(mNativeObject, sc.mNativeObject,
2319 dsdx, dtdx, dtdy, dsdy);
2320 return this;
2321 }
2322
Robert Carra7827f72019-01-11 12:53:37 -08002323 /**
2324 * @hide
2325 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002326 @UnsupportedAppUsage
Jorim Jaggi21c39a72017-10-20 15:47:51 +02002327 public Transaction setMatrix(SurfaceControl sc, Matrix matrix, float[] float9) {
2328 matrix.getValues(float9);
2329 setMatrix(sc, float9[MSCALE_X], float9[MSKEW_Y],
2330 float9[MSKEW_X], float9[MSCALE_Y]);
2331 setPosition(sc, float9[MTRANS_X], float9[MTRANS_Y]);
2332 return this;
2333 }
2334
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002335 /**
2336 * Sets the color transform for the Surface.
2337 * @param matrix A float array with 9 values represents a 3x3 transform matrix
2338 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08002339 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002340 */
2341 public Transaction setColorTransform(SurfaceControl sc, @Size(9) float[] matrix,
2342 @Size(3) float[] translation) {
2343 sc.checkNotReleased();
2344 nativeSetColorTransform(mNativeObject, sc.mNativeObject, matrix, translation);
2345 return this;
2346 }
2347
Robert Carra7827f72019-01-11 12:53:37 -08002348 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08002349 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
2350 * the color can be interpreted in any color space.
2351 * @param agnostic A boolean to indicate whether the surface is color space agnostic
2352 * @hide
2353 */
2354 public Transaction setColorSpaceAgnostic(SurfaceControl sc, boolean agnostic) {
2355 sc.checkNotReleased();
2356 nativeSetColorSpaceAgnostic(mNativeObject, sc.mNativeObject, agnostic);
2357 return this;
2358 }
2359
2360 /**
Robert Carra7827f72019-01-11 12:53:37 -08002361 * @hide
2362 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002363 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002364 public Transaction setWindowCrop(SurfaceControl sc, Rect crop) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002365 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002366 if (crop != null) {
2367 nativeSetWindowCrop(mNativeObject, sc.mNativeObject,
2368 crop.left, crop.top, crop.right, crop.bottom);
2369 } else {
2370 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, 0, 0);
2371 }
2372
2373 return this;
2374 }
2375
Robert Carra7827f72019-01-11 12:53:37 -08002376 /**
2377 * @hide
2378 */
Vishnu Naird454442d2018-11-13 13:51:01 -08002379 public Transaction setWindowCrop(SurfaceControl sc, int width, int height) {
2380 sc.checkNotReleased();
2381 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, width, height);
2382 return this;
2383 }
2384
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002385 /**
2386 * Sets the corner radius of a {@link SurfaceControl}.
2387 * @param sc SurfaceControl
2388 * @param cornerRadius Corner radius in pixels.
2389 * @return Itself.
Robert Carra7827f72019-01-11 12:53:37 -08002390 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002391 */
2392 @UnsupportedAppUsage
2393 public Transaction setCornerRadius(SurfaceControl sc, float cornerRadius) {
2394 sc.checkNotReleased();
2395 nativeSetCornerRadius(mNativeObject, sc.mNativeObject, cornerRadius);
2396
2397 return this;
2398 }
2399
Robert Carra7827f72019-01-11 12:53:37 -08002400 /**
2401 * @hide
2402 */
Mathew Inwood679c15e2019-02-06 15:36:04 +00002403 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.O)
Robert Carre13b58e2017-08-31 14:50:44 -07002404 public Transaction setLayerStack(SurfaceControl sc, int layerStack) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002405 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002406 nativeSetLayerStack(mNativeObject, sc.mNativeObject, layerStack);
2407 return this;
2408 }
2409
Robert Carra7827f72019-01-11 12:53:37 -08002410 /**
2411 * @hide
2412 */
Robert Carr76907ee2019-01-11 13:38:19 -08002413 @UnsupportedAppUsage
Jorim Jaggidfc27372017-10-27 17:47:49 +02002414 public Transaction deferTransactionUntil(SurfaceControl sc, IBinder handle,
2415 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002416 if (frameNumber < 0) {
2417 return this;
2418 }
Jorim Jaggidfc27372017-10-27 17:47:49 +02002419 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002420 nativeDeferTransactionUntil(mNativeObject, sc.mNativeObject, handle, frameNumber);
2421 return this;
2422 }
2423
Robert Carra7827f72019-01-11 12:53:37 -08002424 /**
2425 * @hide
2426 */
Robert Carr76907ee2019-01-11 13:38:19 -08002427 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002428 public Transaction deferTransactionUntilSurface(SurfaceControl sc, Surface barrierSurface,
2429 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002430 if (frameNumber < 0) {
2431 return this;
2432 }
Jorim Jaggidfc27372017-10-27 17:47:49 +02002433 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002434 nativeDeferTransactionUntilSurface(mNativeObject, sc.mNativeObject,
2435 barrierSurface.mNativeObject, frameNumber);
2436 return this;
2437 }
2438
Robert Carra7827f72019-01-11 12:53:37 -08002439 /**
2440 * @hide
2441 */
Robert Carre13b58e2017-08-31 14:50:44 -07002442 public Transaction reparentChildren(SurfaceControl sc, IBinder newParentHandle) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002443 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002444 nativeReparentChildren(mNativeObject, sc.mNativeObject, newParentHandle);
2445 return this;
2446 }
2447
Robert Carr10584fa2019-01-14 15:55:19 -08002448 /**
2449 * Re-parents a given layer to a new parent. Children inherit transform (position, scaling)
2450 * crop, visibility, and Z-ordering from their parents, as if the children were pixels within the
2451 * parent Surface.
2452 *
2453 * @param sc The SurfaceControl to reparent
2454 * @param newParent The new parent for the given control.
2455 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002456 */
Robert Carr76907ee2019-01-11 13:38:19 -08002457 @NonNull
2458 public Transaction reparent(@NonNull SurfaceControl sc,
2459 @Nullable SurfaceControl newParent) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002460 sc.checkNotReleased();
Robert Carr10584fa2019-01-14 15:55:19 -08002461 long otherObject = 0;
2462 if (newParent != null) {
2463 newParent.checkNotReleased();
2464 otherObject = newParent.mNativeObject;
2465 }
2466 nativeReparent(mNativeObject, sc.mNativeObject, otherObject);
Robert Carre13b58e2017-08-31 14:50:44 -07002467 return this;
2468 }
2469
Robert Carra7827f72019-01-11 12:53:37 -08002470 /**
2471 * @hide
2472 */
Robert Carre13b58e2017-08-31 14:50:44 -07002473 public Transaction detachChildren(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002474 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002475 nativeSeverChildren(mNativeObject, sc.mNativeObject);
2476 return this;
2477 }
2478
Robert Carra7827f72019-01-11 12:53:37 -08002479 /**
2480 * @hide
2481 */
Robert Carre13b58e2017-08-31 14:50:44 -07002482 public Transaction setOverrideScalingMode(SurfaceControl sc, int overrideScalingMode) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002483 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002484 nativeSetOverrideScalingMode(mNativeObject, sc.mNativeObject,
2485 overrideScalingMode);
2486 return this;
2487 }
2488
2489 /**
2490 * Sets a color for the Surface.
2491 * @param color A float array with three values to represent r, g, b in range [0..1]
Robert Carra7827f72019-01-11 12:53:37 -08002492 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002493 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002494 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002495 public Transaction setColor(SurfaceControl sc, @Size(3) float[] color) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002496 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002497 nativeSetColor(mNativeObject, sc.mNativeObject, color);
2498 return this;
2499 }
2500
2501 /**
2502 * If the buffer size changes in this transaction, position and crop updates specified
2503 * in this transaction will not complete until a buffer of the new size
2504 * arrives. As transform matrix and size are already frozen in this fashion,
2505 * this enables totally freezing the surface until the resize has completed
2506 * (at which point the geometry influencing aspects of this transaction will then occur)
Robert Carra7827f72019-01-11 12:53:37 -08002507 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002508 */
2509 public Transaction setGeometryAppliesWithResize(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002510 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002511 nativeSetGeometryAppliesWithResize(mNativeObject, sc.mNativeObject);
2512 return this;
2513 }
2514
2515 /**
2516 * Sets the security of the surface. Setting the flag is equivalent to creating the
2517 * Surface with the {@link #SECURE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002518 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002519 */
Robert Carrb1579c82017-09-05 14:54:47 -07002520 public Transaction setSecure(SurfaceControl sc, boolean isSecure) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002521 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002522 if (isSecure) {
2523 nativeSetFlags(mNativeObject, sc.mNativeObject, SECURE, SECURE);
2524 } else {
2525 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SECURE);
2526 }
2527 return this;
2528 }
2529
2530 /**
2531 * Sets the opacity of the surface. Setting the flag is equivalent to creating the
2532 * Surface with the {@link #OPAQUE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002533 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002534 */
2535 public Transaction setOpaque(SurfaceControl sc, boolean isOpaque) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002536 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002537 if (isOpaque) {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002538 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_OPAQUE, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002539 } else {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002540 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002541 }
2542 return this;
2543 }
2544
Robert Carra7827f72019-01-11 12:53:37 -08002545 /**
2546 * @hide
2547 */
Robert Carre13b58e2017-08-31 14:50:44 -07002548 public Transaction setDisplaySurface(IBinder displayToken, Surface surface) {
2549 if (displayToken == null) {
2550 throw new IllegalArgumentException("displayToken must not be null");
2551 }
2552
2553 if (surface != null) {
2554 synchronized (surface.mLock) {
2555 nativeSetDisplaySurface(mNativeObject, displayToken, surface.mNativeObject);
2556 }
2557 } else {
2558 nativeSetDisplaySurface(mNativeObject, displayToken, 0);
2559 }
2560 return this;
2561 }
2562
Robert Carra7827f72019-01-11 12:53:37 -08002563 /**
2564 * @hide
2565 */
Robert Carre13b58e2017-08-31 14:50:44 -07002566 public Transaction setDisplayLayerStack(IBinder displayToken, int layerStack) {
2567 if (displayToken == null) {
2568 throw new IllegalArgumentException("displayToken must not be null");
2569 }
2570 nativeSetDisplayLayerStack(mNativeObject, displayToken, layerStack);
2571 return this;
2572 }
2573
Robert Carra7827f72019-01-11 12:53:37 -08002574 /**
2575 * @hide
2576 */
Robert Carre13b58e2017-08-31 14:50:44 -07002577 public Transaction setDisplayProjection(IBinder displayToken,
2578 int orientation, Rect layerStackRect, Rect displayRect) {
2579 if (displayToken == null) {
2580 throw new IllegalArgumentException("displayToken must not be null");
2581 }
2582 if (layerStackRect == null) {
2583 throw new IllegalArgumentException("layerStackRect must not be null");
2584 }
2585 if (displayRect == null) {
2586 throw new IllegalArgumentException("displayRect must not be null");
2587 }
2588 nativeSetDisplayProjection(mNativeObject, displayToken, orientation,
2589 layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom,
2590 displayRect.left, displayRect.top, displayRect.right, displayRect.bottom);
2591 return this;
2592 }
2593
Robert Carra7827f72019-01-11 12:53:37 -08002594 /**
2595 * @hide
2596 */
Robert Carre13b58e2017-08-31 14:50:44 -07002597 public Transaction setDisplaySize(IBinder displayToken, int width, int height) {
2598 if (displayToken == null) {
2599 throw new IllegalArgumentException("displayToken must not be null");
2600 }
2601 if (width <= 0 || height <= 0) {
2602 throw new IllegalArgumentException("width and height must be positive");
2603 }
2604
2605 nativeSetDisplaySize(mNativeObject, displayToken, width, height);
2606 return this;
2607 }
2608
Robert Carra7827f72019-01-11 12:53:37 -08002609 /** flag the transaction as an animation
2610 * @hide
2611 */
Robert Carre13b58e2017-08-31 14:50:44 -07002612 public Transaction setAnimationTransaction() {
2613 nativeSetAnimationTransaction(mNativeObject);
2614 return this;
2615 }
Robert Carrb1579c82017-09-05 14:54:47 -07002616
2617 /**
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002618 * Indicate that SurfaceFlinger should wake up earlier than usual as a result of this
2619 * transaction. This should be used when the caller thinks that the scene is complex enough
2620 * that it's likely to hit GL composition, and thus, SurfaceFlinger needs to more time in
2621 * order not to miss frame deadlines.
2622 * <p>
2623 * Corresponds to setting ISurfaceComposer::eEarlyWakeup
Robert Carra7827f72019-01-11 12:53:37 -08002624 * @hide
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002625 */
2626 public Transaction setEarlyWakeup() {
2627 nativeSetEarlyWakeup(mNativeObject);
2628 return this;
2629 }
2630
2631 /**
Evan Rosky485df202018-12-06 14:11:12 -08002632 * Sets an arbitrary piece of metadata on the surface. This is a helper for int data.
2633 * @hide
2634 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002635 public Transaction setMetadata(SurfaceControl sc, int key, int data) {
Evan Rosky485df202018-12-06 14:11:12 -08002636 Parcel parcel = Parcel.obtain();
2637 parcel.writeInt(data);
2638 try {
Evan Roskyb51e2462019-04-03 19:27:18 -07002639 setMetadata(sc, key, parcel);
Evan Rosky485df202018-12-06 14:11:12 -08002640 } finally {
2641 parcel.recycle();
2642 }
2643 return this;
2644 }
2645
2646 /**
2647 * Sets an arbitrary piece of metadata on the surface.
2648 * @hide
2649 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002650 public Transaction setMetadata(SurfaceControl sc, int key, Parcel data) {
2651 nativeSetMetadata(mNativeObject, sc.mNativeObject, key, data);
Evan Rosky485df202018-12-06 14:11:12 -08002652 return this;
2653 }
2654
2655 /**
Robert Carrb1579c82017-09-05 14:54:47 -07002656 * Merge the other transaction into this transaction, clearing the
2657 * other transaction as if it had been applied.
Robert Carr76907ee2019-01-11 13:38:19 -08002658 *
2659 * @param other The transaction to merge in to this one.
2660 * @return This transaction.
Robert Carrb1579c82017-09-05 14:54:47 -07002661 */
Robert Carr76907ee2019-01-11 13:38:19 -08002662 @NonNull
2663 public Transaction merge(@NonNull Transaction other) {
Tiger Huanged6794e2019-05-07 20:07:59 +08002664 if (this == other) {
2665 return this;
2666 }
Jorim Jaggia5e10572017-11-15 14:36:26 +01002667 mResizedSurfaces.putAll(other.mResizedSurfaces);
2668 other.mResizedSurfaces.clear();
Robert Carrb1579c82017-09-05 14:54:47 -07002669 nativeMergeTransaction(mNativeObject, other.mNativeObject);
2670 return this;
2671 }
Robert Carr71200f22019-02-05 09:44:53 -08002672
2673 /**
2674 * Equivalent to reparent with a null parent, in that it removes
2675 * the SurfaceControl from the scene, but it also releases
2676 * the local resources (by calling {@link SurfaceControl#release})
2677 * after this method returns, {@link SurfaceControl#isValid} will return
2678 * false for the argument.
2679 *
2680 * @param sc The surface to remove and release.
2681 * @return This transaction
2682 * @hide
2683 */
2684 @NonNull
2685 public Transaction remove(@NonNull SurfaceControl sc) {
2686 reparent(sc, null);
2687 sc.release();
2688 return this;
2689 }
Robert Carre13b58e2017-08-31 14:50:44 -07002690 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08002691}