blob: ea7f31dd8507071aee8dc45afbc677e32ac68aff [file] [log] [blame]
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Jorim Jaggi21c39a72017-10-20 15:47:51 +020019import static android.graphics.Matrix.MSCALE_X;
20import static android.graphics.Matrix.MSCALE_Y;
21import static android.graphics.Matrix.MSKEW_X;
22import static android.graphics.Matrix.MSKEW_Y;
23import static android.graphics.Matrix.MTRANS_X;
24import static android.graphics.Matrix.MTRANS_Y;
Vishnu Nair04ab4392018-01-10 11:00:06 -080025import static android.view.Surface.ROTATION_270;
26import static android.view.Surface.ROTATION_90;
27import static android.view.SurfaceControlProto.HASH_CODE;
28import static android.view.SurfaceControlProto.NAME;
chaviwa69e0a72017-11-29 17:55:12 -080029
Robert Carr76907ee2019-01-11 13:38:19 -080030import android.annotation.FloatRange;
31import android.annotation.IntRange;
32import android.annotation.NonNull;
33import android.annotation.Nullable;
chaviw0dd03f52017-08-25 12:15:26 -070034import android.annotation.Size;
Mathew Inwooda570dee2018-08-17 14:56:00 +010035import android.annotation.UnsupportedAppUsage;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080036import android.graphics.Bitmap;
Peiyong Lin5f4a5682019-01-17 11:37:07 -080037import android.graphics.ColorSpace;
Robert Carr6486d312017-01-09 19:48:29 -080038import android.graphics.GraphicBuffer;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020039import android.graphics.Matrix;
Vishnu Nair04ab4392018-01-10 11:00:06 -080040import android.graphics.PixelFormat;
Jorim Jaggia5e10572017-11-15 14:36:26 +010041import android.graphics.Point;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080042import android.graphics.Rect;
43import android.graphics.Region;
Peiyong Lin9d427402019-01-23 18:39:06 -080044import android.hardware.HardwareBuffer;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070045import android.hardware.display.DisplayedContentSample;
46import android.hardware.display.DisplayedContentSamplingAttributes;
Mathew Inwood679c15e2019-02-06 15:36:04 +000047import android.os.Build;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080048import android.os.IBinder;
Jorim Jaggi06975df2017-12-01 14:52:13 +010049import android.os.Parcel;
50import android.os.Parcelable;
Jorim Jaggia5e10572017-11-15 14:36:26 +010051import android.util.ArrayMap;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080052import android.util.Log;
Evan Rosky485df202018-12-06 14:11:12 -080053import android.util.SparseIntArray;
Vishnu Nair04ab4392018-01-10 11:00:06 -080054import android.util.proto.ProtoOutputStream;
Igor Murashkina86ab6402013-08-30 12:58:36 -070055import android.view.Surface.OutOfResourcesException;
Jorim Jaggia5e10572017-11-15 14:36:26 +010056
57import com.android.internal.annotations.GuardedBy;
58
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070059import dalvik.system.CloseGuard;
Vishnu Nair04ab4392018-01-10 11:00:06 -080060
Robert Carre625fcf2017-09-01 12:36:28 -070061import libcore.util.NativeAllocationRegistry;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070062
Robert Carre13b58e2017-08-31 14:50:44 -070063import java.io.Closeable;
Evan Rosky485df202018-12-06 14:11:12 -080064import java.nio.ByteBuffer;
Evan Roskyc12c9a32019-02-06 13:38:03 -080065import java.nio.ByteOrder;
Robert Carre13b58e2017-08-31 14:50:44 -070066
Mathias Agopian3866f0d2013-02-11 22:08:48 -080067/**
Robert Carr76907ee2019-01-11 13:38:19 -080068 * Handle to an on-screen Surface managed by the system compositor. The SurfaceControl is
69 * a combination of a buffer source, and metadata about how to display the buffers.
70 * By constructing a {@link Surface} from this SurfaceControl you can submit buffers to be
71 * composited. Using {@link SurfaceControl.Transaction} you can manipulate various
72 * properties of how the buffer will be displayed on-screen. SurfaceControl's are
73 * arranged into a scene-graph like hierarchy, and as such any SurfaceControl may have
74 * a parent. Geometric properties like transform, crop, and Z-ordering will be inherited
75 * from the parent, as if the child were content in the parents buffer stream.
Mathias Agopian3866f0d2013-02-11 22:08:48 -080076 */
Robert Carr76907ee2019-01-11 13:38:19 -080077public final class SurfaceControl implements Parcelable {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080078 private static final String TAG = "SurfaceControl";
Mathias Agopian29479eb2013-02-14 14:36:04 -080079
Ashok Bhat36bef0b2014-01-20 20:08:01 +000080 private static native long nativeCreate(SurfaceSession session, String name,
Evan Rosky485df202018-12-06 14:11:12 -080081 int w, int h, int format, int flags, long parentObject, Parcel metadata)
Mathias Agopian29479eb2013-02-14 14:36:04 -080082 throws OutOfResourcesException;
Jorim Jaggi06975df2017-12-01 14:52:13 +010083 private static native long nativeReadFromParcel(Parcel in);
chaviwbeb7a0c2018-12-05 13:49:54 -080084 private static native long nativeCopyFromSurfaceControl(long nativeObject);
Jorim Jaggi06975df2017-12-01 14:52:13 +010085 private static native void nativeWriteToParcel(long nativeObject, Parcel out);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000086 private static native void nativeRelease(long nativeObject);
87 private static native void nativeDestroy(long nativeObject);
Chong Zhang47e36a32016-02-29 16:44:33 -080088 private static native void nativeDisconnect(long nativeObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080089
chaviw08520a02018-09-10 16:44:56 -070090 private static native GraphicBuffer nativeScreenshot(IBinder displayToken,
Robert Carr5c52b132019-02-15 15:48:11 -080091 Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation,
92 boolean captureSecureLayers);
chaviwfbe47df2017-11-10 16:14:49 -080093 private static native GraphicBuffer nativeCaptureLayers(IBinder layerHandleToken,
94 Rect sourceCrop, float frameScale);
Mathias Agopian29479eb2013-02-14 14:36:04 -080095
Robert Carre13b58e2017-08-31 14:50:44 -070096 private static native long nativeCreateTransaction();
97 private static native long nativeGetNativeTransactionFinalizer();
98 private static native void nativeApplyTransaction(long transactionObj, boolean sync);
Robert Carrb1579c82017-09-05 14:54:47 -070099 private static native void nativeMergeTransaction(long transactionObj,
100 long otherTransactionObj);
Robert Carre13b58e2017-08-31 14:50:44 -0700101 private static native void nativeSetAnimationTransaction(long transactionObj);
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100102 private static native void nativeSetEarlyWakeup(long transactionObj);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800103
Robert Carre13b58e2017-08-31 14:50:44 -0700104 private static native void nativeSetLayer(long transactionObj, long nativeObject, int zorder);
105 private static native void nativeSetRelativeLayer(long transactionObj, long nativeObject,
106 IBinder relativeTo, int zorder);
107 private static native void nativeSetPosition(long transactionObj, long nativeObject,
108 float x, float y);
109 private static native void nativeSetGeometryAppliesWithResize(long transactionObj,
110 long nativeObject);
111 private static native void nativeSetSize(long transactionObj, long nativeObject, int w, int h);
112 private static native void nativeSetTransparentRegionHint(long transactionObj,
113 long nativeObject, Region region);
114 private static native void nativeSetAlpha(long transactionObj, long nativeObject, float alpha);
115 private static native void nativeSetMatrix(long transactionObj, long nativeObject,
116 float dsdx, float dtdx,
Andrii Kulian283acd22017-08-03 04:03:51 -0700117 float dtdy, float dsdy);
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700118 private static native void nativeSetColorTransform(long transactionObj, long nativeObject,
119 float[] matrix, float[] translation);
Robert Carr76907ee2019-01-11 13:38:19 -0800120 private static native void nativeSetGeometry(long transactionObj, long nativeObject,
121 Rect sourceCrop, Rect dest, long orientation);
Robert Carre13b58e2017-08-31 14:50:44 -0700122 private static native void nativeSetColor(long transactionObj, long nativeObject, float[] color);
123 private static native void nativeSetFlags(long transactionObj, long nativeObject,
124 int flags, int mask);
125 private static native void nativeSetWindowCrop(long transactionObj, long nativeObject,
126 int l, int t, int r, int b);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -0700127 private static native void nativeSetCornerRadius(long transactionObj, long nativeObject,
128 float cornerRadius);
Robert Carre13b58e2017-08-31 14:50:44 -0700129 private static native void nativeSetLayerStack(long transactionObj, long nativeObject,
130 int layerStack);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800131
Svetoslav1376d602014-03-13 11:17:26 -0700132 private static native boolean nativeClearContentFrameStats(long nativeObject);
133 private static native boolean nativeGetContentFrameStats(long nativeObject, WindowContentFrameStats outStats);
134 private static native boolean nativeClearAnimationFrameStats();
135 private static native boolean nativeGetAnimationFrameStats(WindowAnimationFrameStats outStats);
136
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800137 private static native long[] nativeGetPhysicalDisplayIds();
138 private static native IBinder nativeGetPhysicalDisplayToken(long physicalDisplayId);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800139 private static native IBinder nativeCreateDisplay(String name, boolean secure);
Jesse Hall6a6bc212013-08-08 12:15:03 -0700140 private static native void nativeDestroyDisplay(IBinder displayToken);
Robert Carre13b58e2017-08-31 14:50:44 -0700141 private static native void nativeSetDisplaySurface(long transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000142 IBinder displayToken, long nativeSurfaceObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700143 private static native void nativeSetDisplayLayerStack(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800144 IBinder displayToken, int layerStack);
Robert Carre13b58e2017-08-31 14:50:44 -0700145 private static native void nativeSetDisplayProjection(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800146 IBinder displayToken, int orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -0700147 int l, int t, int r, int b,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800148 int L, int T, int R, int B);
Robert Carre13b58e2017-08-31 14:50:44 -0700149 private static native void nativeSetDisplaySize(long transactionObj, IBinder displayToken,
150 int width, int height);
Dan Stoza00101052014-05-02 15:23:40 -0700151 private static native SurfaceControl.PhysicalDisplayInfo[] nativeGetDisplayConfigs(
152 IBinder displayToken);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -0700153 private static native DisplayedContentSamplingAttributes
154 nativeGetDisplayedContentSamplingAttributes(IBinder displayToken);
155 private static native boolean nativeSetDisplayedContentSamplingEnabled(IBinder displayToken,
156 boolean enable, int componentMask, int maxFrames);
157 private static native DisplayedContentSample nativeGetDisplayedContentSample(
158 IBinder displayToken, long numFrames, long timestamp);
Dan Stoza00101052014-05-02 15:23:40 -0700159 private static native int nativeGetActiveConfig(IBinder displayToken);
160 private static native boolean nativeSetActiveConfig(IBinder displayToken, int id);
Ady Abraham6070ce12019-01-24 18:48:58 -0800161 private static native boolean nativeSetAllowedDisplayConfigs(IBinder displayToken,
162 int[] allowedConfigs);
Michael Wright1c9977b2016-07-12 13:30:10 -0700163 private static native int[] nativeGetDisplayColorModes(IBinder displayToken);
Daniel Solomon10e3b332019-01-20 21:09:11 -0800164 private static native SurfaceControl.DisplayPrimaries nativeGetDisplayNativePrimaries(
165 IBinder displayToken);
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800166 private static native int[] nativeGetCompositionDataspaces();
Michael Wright1c9977b2016-07-12 13:30:10 -0700167 private static native int nativeGetActiveColorMode(IBinder displayToken);
168 private static native boolean nativeSetActiveColorMode(IBinder displayToken,
169 int colorMode);
Prashant Malanic55929a2014-05-25 01:59:21 -0700170 private static native void nativeSetDisplayPowerMode(
171 IBinder displayToken, int mode);
Robert Carre13b58e2017-08-31 14:50:44 -0700172 private static native void nativeDeferTransactionUntil(long transactionObj, long nativeObject,
Rob Carr64e516f2015-10-29 00:20:45 +0000173 IBinder handle, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700174 private static native void nativeDeferTransactionUntilSurface(long transactionObj,
175 long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800176 long surfaceObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700177 private static native void nativeReparentChildren(long transactionObj, long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800178 IBinder handle);
Robert Carre13b58e2017-08-31 14:50:44 -0700179 private static native void nativeReparent(long transactionObj, long nativeObject,
Robert Carr10584fa2019-01-14 15:55:19 -0800180 long newParentNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700181 private static native void nativeSeverChildren(long transactionObj, long nativeObject);
182 private static native void nativeSetOverrideScalingMode(long transactionObj, long nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700183 int scalingMode);
Rob Carr64e516f2015-10-29 00:20:45 +0000184 private static native IBinder nativeGetHandle(long nativeObject);
Robert Carr6da3cc02016-06-16 15:17:07 -0700185 private static native boolean nativeGetTransformToDisplayInverse(long nativeObject);
186
Michael Wright9ff94c02016-03-30 18:05:40 -0700187 private static native Display.HdrCapabilities nativeGetHdrCapabilities(IBinder displayToken);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800188
Robert Carr788f5742018-07-30 17:46:45 -0700189 private static native void nativeSetInputWindowInfo(long transactionObj, long nativeObject,
190 InputWindowHandle handle);
chaviw59f532e2018-12-26 15:34:59 -0800191 private static native void nativeTransferTouchFocus(long transactionObj, IBinder fromToken,
192 IBinder toToken);
Peiyong Lin0ddb7d42019-01-16 13:25:09 -0800193 private static native boolean nativeGetProtectedContentSupport();
Evan Rosky485df202018-12-06 14:11:12 -0800194 private static native void nativeSetMetadata(long transactionObj, int key, Parcel data);
chaviw319cd0782019-02-14 11:00:23 -0800195 private static native void nativeSyncInputWindows(long transactionObj);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800196
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800197 private final CloseGuard mCloseGuard = CloseGuard.get();
chaviwbeb7a0c2018-12-05 13:49:54 -0800198 private String mName;
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000199 long mNativeObject; // package visibility only for Surface.java access
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800200
Jorim Jaggia5e10572017-11-15 14:36:26 +0100201 // TODO: Move this to native.
202 private final Object mSizeLock = new Object();
203 @GuardedBy("mSizeLock")
204 private int mWidth;
205 @GuardedBy("mSizeLock")
206 private int mHeight;
207
Robert Carre13b58e2017-08-31 14:50:44 -0700208 static Transaction sGlobalTransaction;
209 static long sTransactionNestCount = 0;
210
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800211 /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */
212
213 /**
214 * Surface creation flag: Surface is created hidden
Robert Carra7827f72019-01-11 12:53:37 -0800215 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800216 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100217 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800218 public static final int HIDDEN = 0x00000004;
219
220 /**
221 * Surface creation flag: The surface contains secure content, special
222 * measures will be taken to disallow the surface's content to be copied
223 * from another process. In particular, screenshots and VNC servers will
224 * be disabled, but other measures can take place, for instance the
Jesse Hall6a6bc212013-08-08 12:15:03 -0700225 * surface might not be hardware accelerated.
Robert Carra7827f72019-01-11 12:53:37 -0800226 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800227 */
228 public static final int SECURE = 0x00000080;
229
230 /**
231 * Surface creation flag: Creates a surface where color components are interpreted
232 * as "non pre-multiplied" by their alpha channel. Of course this flag is
233 * meaningless for surfaces without an alpha channel. By default
234 * surfaces are pre-multiplied, which means that each color component is
235 * already multiplied by its alpha value. In this case the blending
236 * equation used is:
Andy McFadden314405b2014-01-29 17:18:05 -0800237 * <p>
238 * <code>DEST = SRC + DEST * (1-SRC_ALPHA)</code>
239 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800240 * By contrast, non pre-multiplied surfaces use the following equation:
Andy McFadden314405b2014-01-29 17:18:05 -0800241 * <p>
242 * <code>DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)</code>
243 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800244 * pre-multiplied surfaces must always be used if transparent pixels are
245 * composited on top of each-other into the surface. A pre-multiplied
246 * surface can never lower the value of the alpha component of a given
247 * pixel.
Andy McFadden314405b2014-01-29 17:18:05 -0800248 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800249 * In some rare situations, a non pre-multiplied surface is preferable.
Robert Carra7827f72019-01-11 12:53:37 -0800250 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800251 */
252 public static final int NON_PREMULTIPLIED = 0x00000100;
253
254 /**
255 * Surface creation flag: Indicates that the surface must be considered opaque,
Robert Carre625fcf2017-09-01 12:36:28 -0700256 * even if its pixel format contains an alpha channel. This can be useful if an
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800257 * application needs full RGBA 8888 support for instance but will
258 * still draw every pixel opaque.
Andy McFadden314405b2014-01-29 17:18:05 -0800259 * <p>
260 * This flag is ignored if setAlpha() is used to make the surface non-opaque.
261 * Combined effects are (assuming a buffer format with an alpha channel):
262 * <ul>
263 * <li>OPAQUE + alpha(1.0) == opaque composition
264 * <li>OPAQUE + alpha(0.x) == blended composition
265 * <li>!OPAQUE + alpha(1.0) == blended composition
266 * <li>!OPAQUE + alpha(0.x) == blended composition
267 * </ul>
268 * If the underlying buffer lacks an alpha channel, the OPAQUE flag is effectively
269 * set automatically.
Robert Carra7827f72019-01-11 12:53:37 -0800270 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800271 */
272 public static final int OPAQUE = 0x00000400;
273
274 /**
275 * Surface creation flag: Application requires a hardware-protected path to an
276 * external display sink. If a hardware-protected path is not available,
277 * then this surface will not be displayed on the external sink.
278 *
Robert Carra7827f72019-01-11 12:53:37 -0800279 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800280 */
281 public static final int PROTECTED_APP = 0x00000800;
282
283 // 0x1000 is reserved for an independent DRM protected flag in framework
284
285 /**
Riley Andrews68eccda2014-07-07 11:47:35 -0700286 * Surface creation flag: Window represents a cursor glyph.
Robert Carra7827f72019-01-11 12:53:37 -0800287 * @hide
Riley Andrews68eccda2014-07-07 11:47:35 -0700288 */
289 public static final int CURSOR_WINDOW = 0x00002000;
290
291 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800292 * Surface creation flag: Creates a normal surface.
293 * This is the default.
294 *
Robert Carra7827f72019-01-11 12:53:37 -0800295 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800296 */
297 public static final int FX_SURFACE_NORMAL = 0x00000000;
298
299 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800300 * Surface creation flag: Creates a Dim surface.
301 * Everything behind this surface is dimmed by the amount specified
302 * in {@link #setAlpha}. It is an error to lock a Dim surface, since it
303 * doesn't have a backing store.
304 *
Robert Carra7827f72019-01-11 12:53:37 -0800305 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800306 */
307 public static final int FX_SURFACE_DIM = 0x00020000;
308
309 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700310 * Surface creation flag: Creates a container surface.
311 * This surface will have no buffers and will only be used
312 * as a container for other surfaces, or for its InputInfo.
Robert Carra7827f72019-01-11 12:53:37 -0800313 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700314 */
315 public static final int FX_SURFACE_CONTAINER = 0x00080000;
316
317 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800318 * Mask used for FX values above.
319 *
Robert Carra7827f72019-01-11 12:53:37 -0800320 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800321 */
322 public static final int FX_SURFACE_MASK = 0x000F0000;
323
324 /* flags used with setFlags() (keep in sync with ISurfaceComposer.h) */
325
326 /**
327 * Surface flag: Hide the surface.
328 * Equivalent to calling hide().
Andy McFadden314405b2014-01-29 17:18:05 -0800329 * Updates the value set during Surface creation (see {@link #HIDDEN}).
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800330 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800331 private static final int SURFACE_HIDDEN = 0x01;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800332
Andy McFadden314405b2014-01-29 17:18:05 -0800333 /**
334 * Surface flag: composite without blending when possible.
335 * Updates the value set during Surface creation (see {@link #OPAQUE}).
336 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800337 private static final int SURFACE_OPAQUE = 0x02;
Andy McFadden314405b2014-01-29 17:18:05 -0800338
Robert Carr76907ee2019-01-11 13:38:19 -0800339 // Display power modes.
Prashant Malanic55929a2014-05-25 01:59:21 -0700340 /**
341 * Display power mode off: used while blanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700342 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800343 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700344 */
345 public static final int POWER_MODE_OFF = 0;
346
347 /**
348 * Display power mode doze: used while putting the screen into low power mode.
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_DOZE = 1;
353
354 /**
355 * Display power mode normal: used while unblanking the screen.
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_NORMAL = 2;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800360
Jeff Brown5dc21912014-07-17 18:50:18 -0700361 /**
362 * Display power mode doze: used while putting the screen into a suspended
363 * low power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800364 * @hide
Jeff Brown5dc21912014-07-17 18:50:18 -0700365 */
366 public static final int POWER_MODE_DOZE_SUSPEND = 3;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800367
368 /**
Chris Phoenix10a4a642017-09-25 13:21:00 -0700369 * Display power mode on: used while putting the screen into a suspended
370 * full power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800371 * @hide
Chris Phoenix10a4a642017-09-25 13:21:00 -0700372 */
373 public static final int POWER_MODE_ON_SUSPEND = 4;
374
375 /**
Robert Carr132c9f52017-07-31 17:02:30 -0700376 * A value for windowType used to indicate that the window should be omitted from screenshots
377 * and display mirroring. A temporary workaround until we express such things with
378 * the hierarchy.
379 * TODO: b/64227542
380 * @hide
381 */
382 public static final int WINDOW_TYPE_DONT_SCREENSHOT = 441731;
383
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800384 /**
385 * internal representation of how to interpret pixel value, used only to convert to ColorSpace.
386 */
387 private static final int INTERNAL_DATASPACE_SRGB = 142671872;
388 private static final int INTERNAL_DATASPACE_DISPLAY_P3 = 143261696;
389 private static final int INTERNAL_DATASPACE_SCRGB = 411107328;
390
Robert Carreb344c72019-01-07 18:35:30 -0800391 private void assignNativeObject(long nativeObject) {
392 if (mNativeObject != 0) {
393 release();
394 }
395 mNativeObject = nativeObject;
396 }
397
Robert Carra7827f72019-01-11 12:53:37 -0800398 /**
399 * @hide
400 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800401 public void copyFrom(SurfaceControl other) {
402 mName = other.mName;
403 mWidth = other.mWidth;
404 mHeight = other.mHeight;
Robert Carreb344c72019-01-07 18:35:30 -0800405 assignNativeObject(nativeCopyFromSurfaceControl(other.mNativeObject));
chaviwbeb7a0c2018-12-05 13:49:54 -0800406 }
407
Robert Carr132c9f52017-07-31 17:02:30 -0700408 /**
Evan Rosky485df202018-12-06 14:11:12 -0800409 * owner UID.
410 * @hide
411 */
412 public static final int METADATA_OWNER_UID = 1;
413
414 /**
415 * Window type as per {@link WindowManager.LayoutParams}.
416 * @hide
417 */
418 public static final int METADATA_WINDOW_TYPE = 2;
419
420 /**
Evan Rosky9020c072018-12-06 14:11:12 -0800421 * Task id to allow association between surfaces and task.
422 * @hide
423 */
424 public static final int METADATA_TASK_ID = 3;
425
426 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700427 * Builder class for {@link SurfaceControl} objects.
428 */
429 public static class Builder {
430 private SurfaceSession mSession;
431 private int mFlags = HIDDEN;
432 private int mWidth;
433 private int mHeight;
434 private int mFormat = PixelFormat.OPAQUE;
435 private String mName;
436 private SurfaceControl mParent;
Evan Rosky485df202018-12-06 14:11:12 -0800437 private SparseIntArray mMetadata;
Robert Carre625fcf2017-09-01 12:36:28 -0700438
439 /**
440 * Begin building a SurfaceControl with a given {@link SurfaceSession}.
441 *
442 * @param session The {@link SurfaceSession} with which to eventually construct the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800443 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700444 */
445 public Builder(SurfaceSession session) {
446 mSession = session;
447 }
448
449 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800450 * Begin building a SurfaceControl.
451 */
452 public Builder() {
453 }
454
455 /**
456 * Construct a new {@link SurfaceControl} with the set parameters. The builder
457 * remains valid.
Robert Carre625fcf2017-09-01 12:36:28 -0700458 */
459 public SurfaceControl build() {
Vishnu Naire86bd982018-11-28 13:23:17 -0800460 if (mWidth < 0 || mHeight < 0) {
Robert Carre625fcf2017-09-01 12:36:28 -0700461 throw new IllegalArgumentException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800462 "width and height must be positive or unset");
463 }
464 if ((mWidth > 0 || mHeight > 0) && (isColorLayerSet() || isContainerLayerSet())) {
465 throw new IllegalArgumentException(
466 "Only buffer layers can set a valid buffer size.");
Robert Carre625fcf2017-09-01 12:36:28 -0700467 }
Evan Rosky485df202018-12-06 14:11:12 -0800468 return new SurfaceControl(
469 mSession, mName, mWidth, mHeight, mFormat, mFlags, mParent, mMetadata);
Robert Carre625fcf2017-09-01 12:36:28 -0700470 }
471
472 /**
473 * Set a debugging-name for the SurfaceControl.
474 *
475 * @param name A name to identify the Surface in debugging.
476 */
477 public Builder setName(String name) {
478 mName = name;
479 return this;
480 }
481
482 /**
483 * Set the initial size of the controlled surface's buffers in pixels.
484 *
485 * @param width The buffer width in pixels.
486 * @param height The buffer height in pixels.
487 */
Robert Carr76907ee2019-01-11 13:38:19 -0800488 public Builder setBufferSize(@IntRange(from = 0) int width,
489 @IntRange(from = 0) int height) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800490 if (width < 0 || height < 0) {
Robert Carre625fcf2017-09-01 12:36:28 -0700491 throw new IllegalArgumentException(
492 "width and height must be positive");
493 }
494 mWidth = width;
495 mHeight = height;
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000496 // set this as a buffer layer since we are specifying a buffer size.
497 return setFlags(FX_SURFACE_NORMAL, FX_SURFACE_MASK);
498 }
499
500 /**
501 * Set the initial size of the controlled surface's buffers in pixels.
502 */
503 private void unsetBufferSize() {
504 mWidth = 0;
505 mHeight = 0;
Robert Carre625fcf2017-09-01 12:36:28 -0700506 }
507
508 /**
509 * Set the pixel format of the controlled surface's buffers, using constants from
510 * {@link android.graphics.PixelFormat}.
511 */
Robert Carr76907ee2019-01-11 13:38:19 -0800512 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700513 public Builder setFormat(@PixelFormat.Format int format) {
514 mFormat = format;
515 return this;
516 }
517
518 /**
519 * Specify if the app requires a hardware-protected path to
520 * an external display sync. If protected content is enabled, but
521 * such a path is not available, then the controlled Surface will
522 * not be displayed.
523 *
524 * @param protectedContent Whether to require a protected sink.
Robert Carra7827f72019-01-11 12:53:37 -0800525 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700526 */
Robert Carr76907ee2019-01-11 13:38:19 -0800527 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700528 public Builder setProtected(boolean protectedContent) {
529 if (protectedContent) {
530 mFlags |= PROTECTED_APP;
531 } else {
532 mFlags &= ~PROTECTED_APP;
533 }
534 return this;
535 }
536
537 /**
538 * Specify whether the Surface contains secure content. If true, the system
539 * will prevent the surfaces content from being copied by another process. In
540 * particular screenshots and VNC servers will be disabled. This is however
541 * not a complete prevention of readback as {@link #setProtected}.
Robert Carra7827f72019-01-11 12:53:37 -0800542 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700543 */
Robert Carr76907ee2019-01-11 13:38:19 -0800544 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700545 public Builder setSecure(boolean secure) {
546 if (secure) {
547 mFlags |= SECURE;
548 } else {
549 mFlags &= ~SECURE;
550 }
551 return this;
552 }
553
554 /**
555 * Indicates whether the surface must be considered opaque,
556 * even if its pixel format is set to translucent. This can be useful if an
557 * application needs full RGBA 8888 support for instance but will
558 * still draw every pixel opaque.
559 * <p>
560 * This flag only determines whether opacity will be sampled from the alpha channel.
561 * Plane-alpha from calls to setAlpha() can still result in blended composition
562 * regardless of the opaque setting.
563 *
564 * Combined effects are (assuming a buffer format with an alpha channel):
565 * <ul>
566 * <li>OPAQUE + alpha(1.0) == opaque composition
567 * <li>OPAQUE + alpha(0.x) == blended composition
568 * <li>OPAQUE + alpha(0.0) == no composition
569 * <li>!OPAQUE + alpha(1.0) == blended composition
570 * <li>!OPAQUE + alpha(0.x) == blended composition
571 * <li>!OPAQUE + alpha(0.0) == no composition
572 * </ul>
573 * If the underlying buffer lacks an alpha channel, it is as if setOpaque(true)
574 * were set automatically.
575 * @param opaque Whether the Surface is OPAQUE.
576 */
Robert Carr76907ee2019-01-11 13:38:19 -0800577 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700578 public Builder setOpaque(boolean opaque) {
579 if (opaque) {
580 mFlags |= OPAQUE;
581 } else {
582 mFlags &= ~OPAQUE;
583 }
584 return this;
585 }
586
587 /**
588 * Set a parent surface for our new SurfaceControl.
589 *
590 * Child surfaces are constrained to the onscreen region of their parent.
591 * Furthermore they stack relatively in Z order, and inherit the transformation
592 * of the parent.
593 *
594 * @param parent The parent control.
595 */
Robert Carr76907ee2019-01-11 13:38:19 -0800596 @NonNull
597 public Builder setParent(@Nullable SurfaceControl parent) {
Robert Carre625fcf2017-09-01 12:36:28 -0700598 mParent = parent;
599 return this;
600 }
601
602 /**
Evan Rosky485df202018-12-06 14:11:12 -0800603 * Sets a metadata int.
Robert Carre625fcf2017-09-01 12:36:28 -0700604 *
Evan Rosky485df202018-12-06 14:11:12 -0800605 * @param key metadata key
606 * @param data associated data
Robert Carra7827f72019-01-11 12:53:37 -0800607 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700608 */
Evan Rosky485df202018-12-06 14:11:12 -0800609 public Builder setMetadata(int key, int data) {
610 if (mMetadata == null) {
611 mMetadata = new SparseIntArray();
Robert Carre625fcf2017-09-01 12:36:28 -0700612 }
Evan Rosky485df202018-12-06 14:11:12 -0800613 mMetadata.put(key, data);
Robert Carre625fcf2017-09-01 12:36:28 -0700614 return this;
615 }
616
617 /**
618 * Indicate whether a 'ColorLayer' is to be constructed.
619 *
620 * Color layers will not have an associated BufferQueue and will instead always render a
621 * solid color (that is, solid before plane alpha). Currently that color is black.
622 *
Robert Carra7827f72019-01-11 12:53:37 -0800623 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700624 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000625 public Builder setColorLayer() {
626 unsetBufferSize();
627 return setFlags(FX_SURFACE_DIM, FX_SURFACE_MASK);
Robert Carre625fcf2017-09-01 12:36:28 -0700628 }
629
Vishnu Naire86bd982018-11-28 13:23:17 -0800630 private boolean isColorLayerSet() {
631 return (mFlags & FX_SURFACE_DIM) == FX_SURFACE_DIM;
632 }
633
Robert Carre625fcf2017-09-01 12:36:28 -0700634 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700635 * Indicates whether a 'ContainerLayer' is to be constructed.
636 *
637 * Container layers will not be rendered in any fashion and instead are used
638 * as a parent of renderable layers.
639 *
Robert Carra7827f72019-01-11 12:53:37 -0800640 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700641 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000642 public Builder setContainerLayer() {
643 unsetBufferSize();
644 return setFlags(FX_SURFACE_CONTAINER, FX_SURFACE_MASK);
Robert Carrb6cd6432018-08-13 13:01:47 -0700645 }
646
Vishnu Naire86bd982018-11-28 13:23:17 -0800647 private boolean isContainerLayerSet() {
648 return (mFlags & FX_SURFACE_CONTAINER) == FX_SURFACE_CONTAINER;
649 }
650
Robert Carrb6cd6432018-08-13 13:01:47 -0700651 /**
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000652 * Set 'Surface creation flags' such as {@link #HIDDEN}, {@link #SECURE}.
Robert Carre625fcf2017-09-01 12:36:28 -0700653 *
654 * TODO: Finish conversion to individual builder methods?
655 * @param flags The combined flags
Robert Carra7827f72019-01-11 12:53:37 -0800656 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700657 */
658 public Builder setFlags(int flags) {
659 mFlags = flags;
660 return this;
661 }
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000662
663 private Builder setFlags(int flags, int mask) {
664 mFlags = (mFlags & ~mask) | flags;
665 return this;
666 }
Robert Carre625fcf2017-09-01 12:36:28 -0700667 }
668
669 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800670 * Create a surface with a name.
Andy McFadden314405b2014-01-29 17:18:05 -0800671 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800672 * The surface creation flags specify what kind of surface to create and
673 * certain options such as whether the surface can be assumed to be opaque
674 * and whether it should be initially hidden. Surfaces should always be
675 * created with the {@link #HIDDEN} flag set to ensure that they are not
676 * made visible prematurely before all of the surface's properties have been
677 * configured.
Andy McFadden314405b2014-01-29 17:18:05 -0800678 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800679 * Good practice is to first create the surface with the {@link #HIDDEN} flag
680 * specified, open a transaction, set the surface layer, layer stack, alpha,
681 * and position, call {@link #show} if appropriate, and close the transaction.
Vishnu Naird454442d2018-11-13 13:51:01 -0800682 * <p>
683 * Bounds of the surface is determined by its crop and its buffer size. If the
684 * surface has no buffer or crop, the surface is boundless and only constrained
685 * by the size of its parent bounds.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800686 *
687 * @param session The surface session, must not be null.
688 * @param name The surface name, must not be null.
689 * @param w The surface initial width.
690 * @param h The surface initial height.
691 * @param flags The surface creation flags. Should always include {@link #HIDDEN}
692 * in the creation flags.
Evan Rosky485df202018-12-06 14:11:12 -0800693 * @param metadata Initial metadata.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700694 *
695 * @throws throws OutOfResourcesException If the SurfaceControl cannot be created.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800696 */
Robert Carre625fcf2017-09-01 12:36:28 -0700697 private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags,
Evan Rosky485df202018-12-06 14:11:12 -0800698 SurfaceControl parent, SparseIntArray metadata)
Robert Carrb0f39362018-03-14 13:52:25 -0700699 throws OutOfResourcesException, IllegalArgumentException {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800700 if (name == null) {
701 throw new IllegalArgumentException("name must not be null");
702 }
703
704 if ((flags & SurfaceControl.HIDDEN) == 0) {
705 Log.w(TAG, "Surfaces should always be created with the HIDDEN flag set "
706 + "to ensure that they are not made visible prematurely before "
707 + "all of the surface's properties have been configured. "
708 + "Set the other properties and make the surface visible within "
709 + "a transaction. New surface name: " + name,
710 new Throwable());
711 }
712
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800713 mName = name;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100714 mWidth = w;
715 mHeight = h;
Evan Rosky485df202018-12-06 14:11:12 -0800716 Parcel metaParcel = Parcel.obtain();
717 try {
718 if (metadata != null && metadata.size() > 0) {
719 metaParcel.writeInt(metadata.size());
720 for (int i = 0; i < metadata.size(); ++i) {
721 metaParcel.writeInt(metadata.keyAt(i));
722 metaParcel.writeByteArray(
Evan Roskyc12c9a32019-02-06 13:38:03 -0800723 ByteBuffer.allocate(4).order(ByteOrder.nativeOrder())
724 .putInt(metadata.valueAt(i)).array());
Evan Rosky485df202018-12-06 14:11:12 -0800725 }
Evan Roskyc12c9a32019-02-06 13:38:03 -0800726 metaParcel.setDataPosition(0);
Evan Rosky485df202018-12-06 14:11:12 -0800727 }
728 mNativeObject = nativeCreate(session, name, w, h, format, flags,
729 parent != null ? parent.mNativeObject : 0, metaParcel);
730 } finally {
731 metaParcel.recycle();
732 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800733 if (mNativeObject == 0) {
734 throw new OutOfResourcesException(
735 "Couldn't allocate SurfaceControl native object");
736 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700737
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800738 mCloseGuard.open("release");
739 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700740
Robert Carra7827f72019-01-11 12:53:37 -0800741 /** This is a transfer constructor, useful for transferring a live SurfaceControl native
742 * object to another Java wrapper which could have some different behavior, e.g.
743 * event logging.
744 * @hide
745 */
Robert Carr3b716242016-08-16 16:02:21 -0700746 public SurfaceControl(SurfaceControl other) {
747 mName = other.mName;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100748 mWidth = other.mWidth;
749 mHeight = other.mHeight;
Robert Carr3b716242016-08-16 16:02:21 -0700750 mNativeObject = other.mNativeObject;
751 other.mCloseGuard.close();
752 other.mNativeObject = 0;
753 mCloseGuard.open("release");
754 }
755
Jorim Jaggi06975df2017-12-01 14:52:13 +0100756 private SurfaceControl(Parcel in) {
chaviwbeb7a0c2018-12-05 13:49:54 -0800757 readFromParcel(in);
758 mCloseGuard.open("release");
759 }
760
Robert Carra7827f72019-01-11 12:53:37 -0800761 /**
762 * @hide
763 */
chaviwbeb7a0c2018-12-05 13:49:54 -0800764 public SurfaceControl() {
765 mCloseGuard.open("release");
766 }
767
768 public void readFromParcel(Parcel in) {
769 if (in == null) {
770 throw new IllegalArgumentException("source must not be null");
771 }
772
Jorim Jaggi06975df2017-12-01 14:52:13 +0100773 mName = in.readString();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100774 mWidth = in.readInt();
775 mHeight = in.readInt();
Robert Carr5fea55b2018-12-10 13:05:52 -0800776
Robert Carreb344c72019-01-07 18:35:30 -0800777 long object = 0;
Robert Carr5fea55b2018-12-10 13:05:52 -0800778 if (in.readInt() != 0) {
Robert Carreb344c72019-01-07 18:35:30 -0800779 object = nativeReadFromParcel(in);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100780 }
Robert Carreb344c72019-01-07 18:35:30 -0800781 assignNativeObject(object);
Jorim Jaggi06975df2017-12-01 14:52:13 +0100782 }
783
784 @Override
785 public int describeContents() {
786 return 0;
787 }
788
789 @Override
790 public void writeToParcel(Parcel dest, int flags) {
791 dest.writeString(mName);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100792 dest.writeInt(mWidth);
793 dest.writeInt(mHeight);
Robert Carr5fea55b2018-12-10 13:05:52 -0800794 if (mNativeObject == 0) {
795 dest.writeInt(0);
796 } else {
797 dest.writeInt(1);
798 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100799 nativeWriteToParcel(mNativeObject, dest);
Robert Carr5fea55b2018-12-10 13:05:52 -0800800
801 if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) {
802 release();
803 }
Jorim Jaggi06975df2017-12-01 14:52:13 +0100804 }
805
Vishnu Nair04ab4392018-01-10 11:00:06 -0800806 /**
807 * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link
808 * android.view.SurfaceControlProto}.
809 *
810 * @param proto Stream to write the SurfaceControl object to.
811 * @param fieldId Field Id of the SurfaceControl as defined in the parent message.
812 * @hide
813 */
814 public void writeToProto(ProtoOutputStream proto, long fieldId) {
815 final long token = proto.start(fieldId);
816 proto.write(HASH_CODE, System.identityHashCode(this));
817 proto.write(NAME, mName);
818 proto.end(token);
819 }
820
Jorim Jaggi06975df2017-12-01 14:52:13 +0100821 public static final Creator<SurfaceControl> CREATOR
822 = new Creator<SurfaceControl>() {
823 public SurfaceControl createFromParcel(Parcel in) {
824 return new SurfaceControl(in);
825 }
826
827 public SurfaceControl[] newArray(int size) {
828 return new SurfaceControl[size];
829 }
830 };
831
Robert Carra7827f72019-01-11 12:53:37 -0800832 /**
833 * @hide
834 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800835 @Override
836 protected void finalize() throws Throwable {
837 try {
838 if (mCloseGuard != null) {
839 mCloseGuard.warnIfOpen();
840 }
841 if (mNativeObject != 0) {
842 nativeRelease(mNativeObject);
843 }
844 } finally {
845 super.finalize();
846 }
847 }
848
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800849 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800850 * Release the local reference to the server-side surface. The surface
851 * may continue to exist on-screen as long as its parent continues
852 * to exist. To explicitly remove a surface from the screen use
853 * {@link Transaction#reparent} with a null-parent.
854 *
855 * Always call release() when you're done with a SurfaceControl.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800856 */
857 public void release() {
858 if (mNativeObject != 0) {
859 nativeRelease(mNativeObject);
860 mNativeObject = 0;
861 }
862 mCloseGuard.close();
863 }
864
865 /**
Robert Carr5ea304d2019-02-04 16:04:55 -0800866 * Release the local resources like {@link #release} but also
867 * remove the Surface from the screen.
Robert Carra7827f72019-01-11 12:53:37 -0800868 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800869 */
Robert Carr5ea304d2019-02-04 16:04:55 -0800870 public void remove() {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800871 if (mNativeObject != 0) {
872 nativeDestroy(mNativeObject);
873 mNativeObject = 0;
874 }
875 mCloseGuard.close();
876 }
877
Chong Zhang47e36a32016-02-29 16:44:33 -0800878 /**
879 * Disconnect any client still connected to the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800880 * @hide
Chong Zhang47e36a32016-02-29 16:44:33 -0800881 */
882 public void disconnect() {
883 if (mNativeObject != 0) {
884 nativeDisconnect(mNativeObject);
885 }
886 }
887
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800888 private void checkNotReleased() {
889 if (mNativeObject == 0) throw new NullPointerException(
890 "mNativeObject is null. Have you called release() already?");
891 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700892
Robert Carra7827f72019-01-11 12:53:37 -0800893 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800894 * Check whether this instance points to a valid layer with the system-compositor. For
895 * example this may be false if construction failed, or the layer was released.
896 *
897 * @return Whether this SurfaceControl is valid.
Robert Carra7827f72019-01-11 12:53:37 -0800898 */
Robert Carr5fea55b2018-12-10 13:05:52 -0800899 public boolean isValid() {
900 return mNativeObject != 0;
901 }
902
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800903 /*
904 * set surface parameters.
905 * needs to be inside open/closeTransaction block
906 */
907
Robert Carra7827f72019-01-11 12:53:37 -0800908 /** start a transaction
909 * @hide
910 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100911 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800912 public static void openTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -0700913 synchronized (SurfaceControl.class) {
914 if (sGlobalTransaction == null) {
915 sGlobalTransaction = new Transaction();
916 }
917 synchronized(SurfaceControl.class) {
918 sTransactionNestCount++;
919 }
920 }
921 }
922
923 private static void closeTransaction(boolean sync) {
924 synchronized(SurfaceControl.class) {
925 if (sTransactionNestCount == 0) {
926 Log.e(TAG, "Call to SurfaceControl.closeTransaction without matching openTransaction");
927 } else if (--sTransactionNestCount > 0) {
928 return;
929 }
930 sGlobalTransaction.apply(sync);
931 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800932 }
933
Robert Carrb1579c82017-09-05 14:54:47 -0700934 /**
935 * Merge the supplied transaction in to the deprecated "global" transaction.
936 * This clears the supplied transaction in an identical fashion to {@link Transaction#merge}.
937 * <p>
938 * This is a utility for interop with legacy-code and will go away with the Global Transaction.
Robert Carra7827f72019-01-11 12:53:37 -0800939 * @hide
Robert Carrb1579c82017-09-05 14:54:47 -0700940 */
941 @Deprecated
942 public static void mergeToGlobalTransaction(Transaction t) {
Robert Carrc5fc4612017-12-05 11:55:40 -0800943 synchronized(SurfaceControl.class) {
Robert Carrb1579c82017-09-05 14:54:47 -0700944 sGlobalTransaction.merge(t);
945 }
946 }
947
Robert Carra7827f72019-01-11 12:53:37 -0800948 /** end a transaction
949 * @hide
950 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100951 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800952 public static void closeTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -0700953 closeTransaction(false);
Robert Carre9953b12016-05-23 20:52:04 -0700954 }
955
Robert Carra7827f72019-01-11 12:53:37 -0800956 /**
957 * @hide
958 */
Robert Carre9953b12016-05-23 20:52:04 -0700959 public static void closeTransactionSync() {
Robert Carre13b58e2017-08-31 14:50:44 -0700960 closeTransaction(true);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800961 }
962
Robert Carra7827f72019-01-11 12:53:37 -0800963 /**
964 * @hide
965 */
Rob Carr64e516f2015-10-29 00:20:45 +0000966 public void deferTransactionUntil(IBinder handle, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -0800967 synchronized(SurfaceControl.class) {
968 sGlobalTransaction.deferTransactionUntil(this, handle, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800969 }
970 }
971
Robert Carra7827f72019-01-11 12:53:37 -0800972 /**
973 * @hide
974 */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800975 public void deferTransactionUntil(Surface barrier, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -0800976 synchronized(SurfaceControl.class) {
977 sGlobalTransaction.deferTransactionUntilSurface(this, barrier, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800978 }
979 }
980
Robert Carra7827f72019-01-11 12:53:37 -0800981 /**
982 * @hide
983 */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800984 public void reparentChildren(IBinder newParentHandle) {
Robert Carre13b58e2017-08-31 14:50:44 -0700985 synchronized(SurfaceControl.class) {
986 sGlobalTransaction.reparentChildren(this, newParentHandle);
987 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800988 }
989
Robert Carra7827f72019-01-11 12:53:37 -0800990 /**
991 * @hide
992 */
Robert Carr10584fa2019-01-14 15:55:19 -0800993 public void reparent(SurfaceControl newParent) {
Robert Carre13b58e2017-08-31 14:50:44 -0700994 synchronized(SurfaceControl.class) {
Robert Carr10584fa2019-01-14 15:55:19 -0800995 sGlobalTransaction.reparent(this, newParent);
Robert Carre13b58e2017-08-31 14:50:44 -0700996 }
chaviw63542382017-08-17 17:39:29 -0700997 }
998
Robert Carra7827f72019-01-11 12:53:37 -0800999 /**
1000 * @hide
1001 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001002 public void detachChildren() {
Robert Carre13b58e2017-08-31 14:50:44 -07001003 synchronized(SurfaceControl.class) {
1004 sGlobalTransaction.detachChildren(this);
1005 }
Rob Carr64e516f2015-10-29 00:20:45 +00001006 }
1007
Robert Carra7827f72019-01-11 12:53:37 -08001008 /**
1009 * @hide
1010 */
Robert Carr1ca6a332016-04-11 18:00:43 -07001011 public void setOverrideScalingMode(int scalingMode) {
1012 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001013 synchronized(SurfaceControl.class) {
1014 sGlobalTransaction.setOverrideScalingMode(this, scalingMode);
1015 }
Robert Carr1ca6a332016-04-11 18:00:43 -07001016 }
1017
Robert Carra7827f72019-01-11 12:53:37 -08001018 /**
1019 * @hide
1020 */
Rob Carr64e516f2015-10-29 00:20:45 +00001021 public IBinder getHandle() {
1022 return nativeGetHandle(mNativeObject);
1023 }
1024
Robert Carra7827f72019-01-11 12:53:37 -08001025 /**
1026 * @hide
1027 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001028 public static void setAnimationTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -07001029 synchronized (SurfaceControl.class) {
1030 sGlobalTransaction.setAnimationTransaction();
1031 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001032 }
1033
Robert Carra7827f72019-01-11 12:53:37 -08001034 /**
1035 * @hide
1036 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001037 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001038 public void setLayer(int zorder) {
1039 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001040 synchronized(SurfaceControl.class) {
1041 sGlobalTransaction.setLayer(this, zorder);
1042 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001043 }
1044
Robert Carra7827f72019-01-11 12:53:37 -08001045 /**
1046 * @hide
1047 */
Robert Carr77e34942017-10-18 19:13:56 -07001048 public void setRelativeLayer(SurfaceControl relativeTo, int zorder) {
Robert Carraf422a82017-04-10 18:34:33 -07001049 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001050 synchronized(SurfaceControl.class) {
1051 sGlobalTransaction.setRelativeLayer(this, relativeTo, zorder);
1052 }
Robert Carraf422a82017-04-10 18:34:33 -07001053 }
1054
Robert Carra7827f72019-01-11 12:53:37 -08001055 /**
1056 * @hide
1057 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001058 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001059 public void setPosition(float x, float y) {
1060 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001061 synchronized(SurfaceControl.class) {
1062 sGlobalTransaction.setPosition(this, x, y);
1063 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001064 }
1065
Robert Carra7827f72019-01-11 12:53:37 -08001066 /**
1067 * @hide
1068 */
Robert Carr6da3cc02016-06-16 15:17:07 -07001069 public void setGeometryAppliesWithResize() {
Robert Carra9408d42016-06-03 13:28:48 -07001070 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001071 synchronized(SurfaceControl.class) {
1072 sGlobalTransaction.setGeometryAppliesWithResize(this);
1073 }
Robert Carra9408d42016-06-03 13:28:48 -07001074 }
1075
Robert Carra7827f72019-01-11 12:53:37 -08001076 /**
1077 * @hide
1078 */
Vishnu Naire86bd982018-11-28 13:23:17 -08001079 public void setBufferSize(int w, int h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001080 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001081 synchronized(SurfaceControl.class) {
Vishnu Naire86bd982018-11-28 13:23:17 -08001082 sGlobalTransaction.setBufferSize(this, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07001083 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001084 }
1085
Robert Carra7827f72019-01-11 12:53:37 -08001086 /**
1087 * @hide
1088 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001089 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001090 public void hide() {
1091 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001092 synchronized(SurfaceControl.class) {
1093 sGlobalTransaction.hide(this);
1094 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001095 }
1096
Robert Carra7827f72019-01-11 12:53:37 -08001097 /**
1098 * @hide
1099 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001100 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001101 public void show() {
1102 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001103 synchronized(SurfaceControl.class) {
1104 sGlobalTransaction.show(this);
1105 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001106 }
1107
Robert Carra7827f72019-01-11 12:53:37 -08001108 /**
1109 * @hide
1110 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001111 public void setTransparentRegionHint(Region region) {
1112 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001113 synchronized(SurfaceControl.class) {
1114 sGlobalTransaction.setTransparentRegionHint(this, region);
1115 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001116 }
1117
Robert Carra7827f72019-01-11 12:53:37 -08001118 /**
1119 * @hide
1120 */
Svetoslav1376d602014-03-13 11:17:26 -07001121 public boolean clearContentFrameStats() {
1122 checkNotReleased();
1123 return nativeClearContentFrameStats(mNativeObject);
1124 }
1125
Robert Carra7827f72019-01-11 12:53:37 -08001126 /**
1127 * @hide
1128 */
Svetoslav1376d602014-03-13 11:17:26 -07001129 public boolean getContentFrameStats(WindowContentFrameStats outStats) {
1130 checkNotReleased();
1131 return nativeGetContentFrameStats(mNativeObject, outStats);
1132 }
1133
Robert Carra7827f72019-01-11 12:53:37 -08001134 /**
1135 * @hide
1136 */
Svetoslav1376d602014-03-13 11:17:26 -07001137 public static boolean clearAnimationFrameStats() {
1138 return nativeClearAnimationFrameStats();
1139 }
1140
Robert Carra7827f72019-01-11 12:53:37 -08001141 /**
1142 * @hide
1143 */
Svetoslav1376d602014-03-13 11:17:26 -07001144 public static boolean getAnimationFrameStats(WindowAnimationFrameStats outStats) {
1145 return nativeGetAnimationFrameStats(outStats);
1146 }
1147
Robert Carra7827f72019-01-11 12:53:37 -08001148 /**
1149 * @hide
1150 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001151 public void setAlpha(float alpha) {
1152 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001153 synchronized(SurfaceControl.class) {
1154 sGlobalTransaction.setAlpha(this, alpha);
1155 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001156 }
1157
Robert Carra7827f72019-01-11 12:53:37 -08001158 /**
1159 * @hide
1160 */
chaviw0dd03f52017-08-25 12:15:26 -07001161 public void setColor(@Size(3) float[] color) {
1162 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001163 synchronized (SurfaceControl.class) {
1164 sGlobalTransaction.setColor(this, color);
1165 }
chaviw0dd03f52017-08-25 12:15:26 -07001166 }
1167
Robert Carra7827f72019-01-11 12:53:37 -08001168 /**
1169 * @hide
1170 */
Robert Carr0edf18f2017-02-21 20:01:47 -08001171 public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001172 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001173 synchronized(SurfaceControl.class) {
1174 sGlobalTransaction.setMatrix(this, dsdx, dtdx, dtdy, dsdy);
1175 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001176 }
1177
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001178 /**
1179 * Sets the transform and position of a {@link SurfaceControl} from a 3x3 transformation matrix.
1180 *
1181 * @param matrix The matrix to apply.
1182 * @param float9 An array of 9 floats to be used to extract the values from the matrix.
Robert Carra7827f72019-01-11 12:53:37 -08001183 * @hide
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001184 */
1185 public void setMatrix(Matrix matrix, float[] float9) {
1186 checkNotReleased();
1187 matrix.getValues(float9);
1188 synchronized (SurfaceControl.class) {
1189 sGlobalTransaction.setMatrix(this, float9[MSCALE_X], float9[MSKEW_Y],
1190 float9[MSKEW_X], float9[MSCALE_Y]);
1191 sGlobalTransaction.setPosition(this, float9[MTRANS_X], float9[MTRANS_Y]);
1192 }
1193 }
1194
Peiyong Lin52bb6b42018-10-01 11:40:50 -07001195 /**
1196 * Sets the color transform for the Surface.
1197 * @param matrix A float array with 9 values represents a 3x3 transform matrix
1198 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08001199 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07001200 */
1201 public void setColorTransform(@Size(9) float[] matrix, @Size(3) float[] translation) {
1202 checkNotReleased();
1203 synchronized (SurfaceControl.class) {
1204 sGlobalTransaction.setColorTransform(this, matrix, translation);
1205 }
1206 }
1207
Vishnu Naird454442d2018-11-13 13:51:01 -08001208 /**
1209 * Bounds the surface and its children to the bounds specified. Size of the surface will be
1210 * ignored and only the crop and buffer size will be used to determine the bounds of the
1211 * surface. If no crop is specified and the surface has no buffer, the surface bounds is only
1212 * constrained by the size of its parent bounds.
1213 *
1214 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08001215 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001216 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001217 public void setWindowCrop(Rect crop) {
1218 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001219 synchronized (SurfaceControl.class) {
1220 sGlobalTransaction.setWindowCrop(this, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001221 }
1222 }
1223
Vishnu Naird454442d2018-11-13 13:51:01 -08001224 /**
1225 * Same as {@link SurfaceControl#setWindowCrop(Rect)} but sets the crop rect top left at 0, 0.
1226 *
1227 * @param width width of crop rect
1228 * @param height height of crop rect
Robert Carra7827f72019-01-11 12:53:37 -08001229 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001230 */
1231 public void setWindowCrop(int width, int height) {
1232 checkNotReleased();
1233 synchronized (SurfaceControl.class) {
1234 sGlobalTransaction.setWindowCrop(this, width, height);
1235 }
1236 }
1237
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07001238 /**
1239 * Sets the corner radius of a {@link SurfaceControl}.
1240 *
1241 * @param cornerRadius Corner radius in pixels.
Robert Carra7827f72019-01-11 12:53:37 -08001242 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07001243 */
1244 public void setCornerRadius(float cornerRadius) {
1245 checkNotReleased();
1246 synchronized (SurfaceControl.class) {
1247 sGlobalTransaction.setCornerRadius(this, cornerRadius);
1248 }
1249 }
1250
Robert Carra7827f72019-01-11 12:53:37 -08001251 /**
1252 * @hide
1253 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001254 public void setLayerStack(int layerStack) {
1255 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001256 synchronized(SurfaceControl.class) {
1257 sGlobalTransaction.setLayerStack(this, layerStack);
1258 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001259 }
1260
Robert Carra7827f72019-01-11 12:53:37 -08001261 /**
1262 * @hide
1263 */
Andy McFadden314405b2014-01-29 17:18:05 -08001264 public void setOpaque(boolean isOpaque) {
1265 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001266
1267 synchronized (SurfaceControl.class) {
1268 sGlobalTransaction.setOpaque(this, isOpaque);
Andy McFadden314405b2014-01-29 17:18:05 -08001269 }
1270 }
1271
Robert Carra7827f72019-01-11 12:53:37 -08001272 /**
1273 * @hide
1274 */
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001275 public void setSecure(boolean isSecure) {
1276 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001277
1278 synchronized (SurfaceControl.class) {
1279 sGlobalTransaction.setSecure(this, isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001280 }
1281 }
1282
Robert Carra7827f72019-01-11 12:53:37 -08001283 /**
1284 * @hide
1285 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001286 public int getWidth() {
1287 synchronized (mSizeLock) {
1288 return mWidth;
1289 }
1290 }
1291
Robert Carra7827f72019-01-11 12:53:37 -08001292 /**
1293 * @hide
1294 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001295 public int getHeight() {
1296 synchronized (mSizeLock) {
1297 return mHeight;
1298 }
1299 }
1300
Robert Carre13b58e2017-08-31 14:50:44 -07001301 @Override
1302 public String toString() {
1303 return "Surface(name=" + mName + ")/@0x" +
1304 Integer.toHexString(System.identityHashCode(this));
1305 }
1306
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001307 /*
1308 * set display parameters.
1309 * needs to be inside open/closeTransaction block
1310 */
1311
1312 /**
1313 * Describes the properties of a physical display known to surface flinger.
Robert Carr76907ee2019-01-11 13:38:19 -08001314 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001315 */
1316 public static final class PhysicalDisplayInfo {
Robert Carra7827f72019-01-11 12:53:37 -08001317 /**
1318 * @hide
1319 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001320 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001321 public int width;
Robert Carra7827f72019-01-11 12:53:37 -08001322
1323 /**
1324 * @hide
1325 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001326 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001327 public int height;
Robert Carra7827f72019-01-11 12:53:37 -08001328
1329 /**
1330 * @hide
1331 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001332 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001333 public float refreshRate;
Robert Carra7827f72019-01-11 12:53:37 -08001334
1335 /**
1336 * @hide
1337 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001338 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001339 public float density;
Robert Carra7827f72019-01-11 12:53:37 -08001340
1341 /**
1342 * @hide
1343 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001344 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001345 public float xDpi;
Robert Carra7827f72019-01-11 12:53:37 -08001346
1347 /**
1348 * @hide
1349 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001350 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001351 public float yDpi;
Robert Carra7827f72019-01-11 12:53:37 -08001352
1353 /**
1354 * @hide
1355 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001356 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001357 public boolean secure;
Robert Carra7827f72019-01-11 12:53:37 -08001358
1359 /**
1360 * @hide
1361 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001362 @UnsupportedAppUsage
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001363 public long appVsyncOffsetNanos;
Robert Carra7827f72019-01-11 12:53:37 -08001364
1365 /**
1366 * @hide
1367 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001368 @UnsupportedAppUsage
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001369 public long presentationDeadlineNanos;
Jesse Hall6a6bc212013-08-08 12:15:03 -07001370
Robert Carra7827f72019-01-11 12:53:37 -08001371 /**
1372 * @hide
1373 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001374 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001375 public PhysicalDisplayInfo() {
1376 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001377
Robert Carra7827f72019-01-11 12:53:37 -08001378 /**
1379 * @hide
1380 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001381 public PhysicalDisplayInfo(PhysicalDisplayInfo other) {
1382 copyFrom(other);
1383 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001384
Robert Carra7827f72019-01-11 12:53:37 -08001385 /**
1386 * @hide
1387 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001388 @Override
1389 public boolean equals(Object o) {
1390 return o instanceof PhysicalDisplayInfo && equals((PhysicalDisplayInfo)o);
1391 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001392
Robert Carra7827f72019-01-11 12:53:37 -08001393 /**
1394 * @hide
1395 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001396 public boolean equals(PhysicalDisplayInfo other) {
1397 return other != null
1398 && width == other.width
1399 && height == other.height
1400 && refreshRate == other.refreshRate
1401 && density == other.density
1402 && xDpi == other.xDpi
1403 && yDpi == other.yDpi
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001404 && secure == other.secure
1405 && appVsyncOffsetNanos == other.appVsyncOffsetNanos
Michael Wright1c9977b2016-07-12 13:30:10 -07001406 && presentationDeadlineNanos == other.presentationDeadlineNanos;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001407 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001408
Robert Carra7827f72019-01-11 12:53:37 -08001409 /**
1410 * @hide
1411 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001412 @Override
1413 public int hashCode() {
1414 return 0; // don't care
1415 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001416
Robert Carra7827f72019-01-11 12:53:37 -08001417 /**
1418 * @hide
1419 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001420 public void copyFrom(PhysicalDisplayInfo other) {
1421 width = other.width;
1422 height = other.height;
1423 refreshRate = other.refreshRate;
1424 density = other.density;
1425 xDpi = other.xDpi;
1426 yDpi = other.yDpi;
1427 secure = other.secure;
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001428 appVsyncOffsetNanos = other.appVsyncOffsetNanos;
1429 presentationDeadlineNanos = other.presentationDeadlineNanos;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001430 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001431
Robert Carra7827f72019-01-11 12:53:37 -08001432 /**
1433 * @hide
1434 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001435 @Override
1436 public String toString() {
1437 return "PhysicalDisplayInfo{" + width + " x " + height + ", " + refreshRate + " fps, "
1438 + "density " + density + ", " + xDpi + " x " + yDpi + " dpi, secure " + secure
Andy McFaddene8b1aeb2014-06-13 14:05:40 -07001439 + ", appVsyncOffset " + appVsyncOffsetNanos
Michael Wright1c9977b2016-07-12 13:30:10 -07001440 + ", bufferDeadline " + presentationDeadlineNanos + "}";
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001441 }
1442 }
1443
Robert Carra7827f72019-01-11 12:53:37 -08001444 /**
1445 * @hide
1446 */
Prashant Malanic55929a2014-05-25 01:59:21 -07001447 public static void setDisplayPowerMode(IBinder displayToken, int mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001448 if (displayToken == null) {
1449 throw new IllegalArgumentException("displayToken must not be null");
1450 }
Prashant Malanic55929a2014-05-25 01:59:21 -07001451 nativeSetDisplayPowerMode(displayToken, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001452 }
1453
Robert Carra7827f72019-01-11 12:53:37 -08001454 /**
1455 * @hide
1456 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001457 @UnsupportedAppUsage
Dan Stoza00101052014-05-02 15:23:40 -07001458 public static SurfaceControl.PhysicalDisplayInfo[] getDisplayConfigs(IBinder displayToken) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001459 if (displayToken == null) {
1460 throw new IllegalArgumentException("displayToken must not be null");
1461 }
Dan Stoza00101052014-05-02 15:23:40 -07001462 return nativeGetDisplayConfigs(displayToken);
1463 }
1464
Robert Carra7827f72019-01-11 12:53:37 -08001465 /**
1466 * @hide
1467 */
Dan Stoza00101052014-05-02 15:23:40 -07001468 public static int getActiveConfig(IBinder displayToken) {
1469 if (displayToken == null) {
1470 throw new IllegalArgumentException("displayToken must not be null");
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001471 }
Dan Stoza00101052014-05-02 15:23:40 -07001472 return nativeGetActiveConfig(displayToken);
1473 }
1474
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001475 /**
1476 * @hide
1477 */
1478 public static DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
1479 IBinder displayToken) {
1480 if (displayToken == null) {
1481 throw new IllegalArgumentException("displayToken must not be null");
1482 }
1483 return nativeGetDisplayedContentSamplingAttributes(displayToken);
1484 }
1485
1486 /**
1487 * @hide
1488 */
1489 public static boolean setDisplayedContentSamplingEnabled(
1490 IBinder displayToken, boolean enable, int componentMask, int maxFrames) {
1491 if (displayToken == null) {
1492 throw new IllegalArgumentException("displayToken must not be null");
1493 }
1494 final int maxColorComponents = 4;
1495 if ((componentMask >> maxColorComponents) != 0) {
1496 throw new IllegalArgumentException("invalid componentMask when enabling sampling");
1497 }
1498 return nativeSetDisplayedContentSamplingEnabled(
1499 displayToken, enable, componentMask, maxFrames);
1500 }
1501
1502 /**
1503 * @hide
1504 */
1505 public static DisplayedContentSample getDisplayedContentSample(
1506 IBinder displayToken, long maxFrames, long timestamp) {
1507 if (displayToken == null) {
1508 throw new IllegalArgumentException("displayToken must not be null");
1509 }
1510 return nativeGetDisplayedContentSample(displayToken, maxFrames, timestamp);
1511 }
1512
1513
Robert Carra7827f72019-01-11 12:53:37 -08001514 /**
1515 * @hide
1516 */
Dan Stoza00101052014-05-02 15:23:40 -07001517 public static boolean setActiveConfig(IBinder displayToken, int id) {
1518 if (displayToken == null) {
1519 throw new IllegalArgumentException("displayToken must not be null");
1520 }
1521 return nativeSetActiveConfig(displayToken, id);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001522 }
1523
Robert Carra7827f72019-01-11 12:53:37 -08001524 /**
1525 * @hide
1526 */
Ady Abraham6070ce12019-01-24 18:48:58 -08001527 public static boolean setAllowedDisplayConfigs(IBinder displayToken, int[] allowedConfigs) {
1528 if (displayToken == null) {
1529 throw new IllegalArgumentException("displayToken must not be null");
1530 }
1531 if (allowedConfigs == null) {
1532 throw new IllegalArgumentException("allowedConfigs must not be null");
1533 }
1534
1535 return nativeSetAllowedDisplayConfigs(displayToken, allowedConfigs);
1536 }
1537
1538 /**
1539 * @hide
1540 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001541 public static int[] getDisplayColorModes(IBinder displayToken) {
1542 if (displayToken == null) {
1543 throw new IllegalArgumentException("displayToken must not be null");
1544 }
1545 return nativeGetDisplayColorModes(displayToken);
1546 }
1547
Robert Carra7827f72019-01-11 12:53:37 -08001548 /**
Daniel Solomon10e3b332019-01-20 21:09:11 -08001549 * Color coordinates in CIE1931 XYZ color space
1550 *
1551 * @hide
1552 */
1553 public static final class CieXyz {
1554 /**
1555 * @hide
1556 */
1557 public float X;
1558
1559 /**
1560 * @hide
1561 */
1562 public float Y;
1563
1564 /**
1565 * @hide
1566 */
1567 public float Z;
1568 }
1569
1570 /**
1571 * Contains a display's color primaries
1572 *
1573 * @hide
1574 */
1575 public static final class DisplayPrimaries {
1576 /**
1577 * @hide
1578 */
1579 public CieXyz red;
1580
1581 /**
1582 * @hide
1583 */
1584 public CieXyz green;
1585
1586 /**
1587 * @hide
1588 */
1589 public CieXyz blue;
1590
1591 /**
1592 * @hide
1593 */
1594 public CieXyz white;
1595
1596 /**
1597 * @hide
1598 */
1599 public DisplayPrimaries() {
1600 }
1601 }
1602
1603 /**
1604 * @hide
1605 */
1606 public static SurfaceControl.DisplayPrimaries getDisplayNativePrimaries(
1607 IBinder displayToken) {
1608 if (displayToken == null) {
1609 throw new IllegalArgumentException("displayToken must not be null");
1610 }
1611
1612 return nativeGetDisplayNativePrimaries(displayToken);
1613 }
1614
1615 /**
Robert Carra7827f72019-01-11 12:53:37 -08001616 * @hide
1617 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001618 public static int getActiveColorMode(IBinder displayToken) {
1619 if (displayToken == null) {
1620 throw new IllegalArgumentException("displayToken must not be null");
1621 }
1622 return nativeGetActiveColorMode(displayToken);
1623 }
1624
Robert Carra7827f72019-01-11 12:53:37 -08001625 /**
1626 * @hide
1627 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001628 public static boolean setActiveColorMode(IBinder displayToken, int colorMode) {
1629 if (displayToken == null) {
1630 throw new IllegalArgumentException("displayToken must not be null");
1631 }
1632 return nativeSetActiveColorMode(displayToken, colorMode);
1633 }
1634
Robert Carra7827f72019-01-11 12:53:37 -08001635 /**
Peiyong Lin5f4a5682019-01-17 11:37:07 -08001636 * Returns an array of color spaces with 2 elements. The first color space is the
1637 * default color space and second one is wide color gamut color space.
1638 * @hide
1639 */
1640 public static ColorSpace[] getCompositionColorSpaces() {
1641 int[] dataspaces = nativeGetCompositionDataspaces();
1642 ColorSpace srgb = ColorSpace.get(ColorSpace.Named.SRGB);
1643 ColorSpace[] colorSpaces = { srgb, srgb };
1644 if (dataspaces.length == 2) {
1645 for (int i = 0; i < 2; ++i) {
1646 switch(dataspaces[i]) {
1647 case INTERNAL_DATASPACE_DISPLAY_P3:
1648 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.DISPLAY_P3);
1649 break;
1650 case INTERNAL_DATASPACE_SCRGB:
1651 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.EXTENDED_SRGB);
1652 break;
1653 case INTERNAL_DATASPACE_SRGB:
1654 // Other dataspace is not recognized, use SRGB color space instead,
1655 // the default value of the array is already SRGB, thus do nothing.
1656 default:
1657 break;
1658 }
1659 }
1660 }
1661 return colorSpaces;
1662 }
1663
1664 /**
Robert Carra7827f72019-01-11 12:53:37 -08001665 * @hide
1666 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001667 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001668 public static void setDisplayProjection(IBinder displayToken,
1669 int orientation, Rect layerStackRect, Rect displayRect) {
Robert Carre13b58e2017-08-31 14:50:44 -07001670 synchronized (SurfaceControl.class) {
1671 sGlobalTransaction.setDisplayProjection(displayToken, orientation,
1672 layerStackRect, displayRect);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001673 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001674 }
1675
Robert Carra7827f72019-01-11 12:53:37 -08001676 /**
1677 * @hide
1678 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001679 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001680 public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -07001681 synchronized (SurfaceControl.class) {
1682 sGlobalTransaction.setDisplayLayerStack(displayToken, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001683 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001684 }
1685
Robert Carra7827f72019-01-11 12:53:37 -08001686 /**
1687 * @hide
1688 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001689 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001690 public static void setDisplaySurface(IBinder displayToken, Surface surface) {
Robert Carre13b58e2017-08-31 14:50:44 -07001691 synchronized (SurfaceControl.class) {
1692 sGlobalTransaction.setDisplaySurface(displayToken, surface);
Jeff Brownfc0ebd72013-04-30 16:33:00 -07001693 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001694 }
1695
Robert Carra7827f72019-01-11 12:53:37 -08001696 /**
1697 * @hide
1698 */
Michael Wright01e840f2014-06-26 16:03:25 -07001699 public static void setDisplaySize(IBinder displayToken, int width, int height) {
Robert Carre13b58e2017-08-31 14:50:44 -07001700 synchronized (SurfaceControl.class) {
1701 sGlobalTransaction.setDisplaySize(displayToken, width, height);
Michael Wright01e840f2014-06-26 16:03:25 -07001702 }
Michael Wright01e840f2014-06-26 16:03:25 -07001703 }
1704
Robert Carra7827f72019-01-11 12:53:37 -08001705 /**
1706 * @hide
1707 */
Michael Wright9ff94c02016-03-30 18:05:40 -07001708 public static Display.HdrCapabilities getHdrCapabilities(IBinder displayToken) {
1709 if (displayToken == null) {
1710 throw new IllegalArgumentException("displayToken must not be null");
1711 }
1712 return nativeGetHdrCapabilities(displayToken);
1713 }
1714
Robert Carra7827f72019-01-11 12:53:37 -08001715 /**
1716 * @hide
1717 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001718 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001719 public static IBinder createDisplay(String name, boolean secure) {
1720 if (name == null) {
1721 throw new IllegalArgumentException("name must not be null");
1722 }
1723 return nativeCreateDisplay(name, secure);
1724 }
1725
Robert Carra7827f72019-01-11 12:53:37 -08001726 /**
1727 * @hide
1728 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001729 @UnsupportedAppUsage
Jesse Hall6a6bc212013-08-08 12:15:03 -07001730 public static void destroyDisplay(IBinder displayToken) {
1731 if (displayToken == null) {
1732 throw new IllegalArgumentException("displayToken must not be null");
1733 }
1734 nativeDestroyDisplay(displayToken);
1735 }
1736
Robert Carra7827f72019-01-11 12:53:37 -08001737 /**
1738 * @hide
1739 */
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001740 public static long[] getPhysicalDisplayIds() {
1741 return nativeGetPhysicalDisplayIds();
1742 }
1743
1744 /**
1745 * @hide
1746 */
1747 public static IBinder getPhysicalDisplayToken(long physicalDisplayId) {
1748 return nativeGetPhysicalDisplayToken(physicalDisplayId);
1749 }
1750
1751 /**
1752 * TODO(116025192): Remove this stopgap once framework is display-agnostic.
1753 *
1754 * @hide
1755 */
1756 public static IBinder getInternalDisplayToken() {
1757 final long[] physicalDisplayIds = getPhysicalDisplayIds();
1758 if (physicalDisplayIds.length == 0) {
1759 return null;
1760 }
1761 return getPhysicalDisplayToken(physicalDisplayIds[0]);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001762 }
1763
Mathias Agopian0449a402013-03-01 23:01:51 -08001764 /**
chaviw08520a02018-09-10 16:44:56 -07001765 * @see SurfaceControl#screenshot(IBinder, Surface, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001766 * @hide
Mathias Agopian0449a402013-03-01 23:01:51 -08001767 */
1768 public static void screenshot(IBinder display, Surface consumer) {
chaviw08520a02018-09-10 16:44:56 -07001769 screenshot(display, consumer, new Rect(), 0, 0, false, 0);
1770 }
1771
1772 /**
1773 * Copy the current screen contents into the provided {@link Surface}
1774 *
1775 * @param consumer The {@link Surface} to take the screenshot into.
1776 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001777 * @hide
chaviw08520a02018-09-10 16:44:56 -07001778 */
1779 public static void screenshot(IBinder display, Surface consumer, Rect sourceCrop, int width,
1780 int height, boolean useIdentityTransform, int rotation) {
1781 if (consumer == null) {
1782 throw new IllegalArgumentException("consumer must not be null");
1783 }
1784
1785 final GraphicBuffer buffer = screenshotToBuffer(display, sourceCrop, width, height,
1786 useIdentityTransform, rotation);
1787 try {
1788 consumer.attachAndQueueBuffer(buffer);
1789 } catch (RuntimeException e) {
1790 Log.w(TAG, "Failed to take screenshot - " + e.getMessage());
1791 }
1792 }
1793
1794 /**
1795 * @see SurfaceControl#screenshot(Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001796 * @hide
chaviw08520a02018-09-10 16:44:56 -07001797 */
1798 @UnsupportedAppUsage
1799 public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) {
1800 return screenshot(sourceCrop, width, height, false, rotation);
Mathias Agopian0449a402013-03-01 23:01:51 -08001801 }
1802
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001803 /**
chaviw1da9cd92017-12-06 10:48:11 -08001804 * Copy the current screen contents into a hardware bitmap and return it.
1805 * Note: If you want to modify the Bitmap in software, you will need to copy the Bitmap into
1806 * a software Bitmap using {@link Bitmap#copy(Bitmap.Config, boolean)}
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001807 *
chaviw08520a02018-09-10 16:44:56 -07001808 * CAVEAT: Versions of screenshot that return a {@link Bitmap} can be extremely slow; avoid use
1809 * unless absolutely necessary; prefer the versions that use a {@link Surface} such as
1810 * {@link SurfaceControl#screenshot(IBinder, Surface)} or {@link GraphicBuffer} such as
1811 * {@link SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}.
Mathias Agopian0449a402013-03-01 23:01:51 -08001812 *
chaviw08520a02018-09-10 16:44:56 -07001813 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001814 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001815 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001816 @UnsupportedAppUsage
Dan Stoza9890e3412014-05-22 16:12:54 -07001817 public static Bitmap screenshot(Rect sourceCrop, int width, int height,
chaviw08520a02018-09-10 16:44:56 -07001818 boolean useIdentityTransform, int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001819 // TODO: should take the display as a parameter
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001820 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
1821 if (displayToken == null) {
1822 Log.w(TAG, "Failed to take screenshot because internal display is disconnected");
1823 return null;
1824 }
1825
chaviwa69e0a72017-11-29 17:55:12 -08001826 if (rotation == ROTATION_90 || rotation == ROTATION_270) {
1827 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
1828 }
1829
1830 SurfaceControl.rotateCropForSF(sourceCrop, rotation);
chaviw08520a02018-09-10 16:44:56 -07001831 final GraphicBuffer buffer = screenshotToBuffer(displayToken, sourceCrop, width, height,
1832 useIdentityTransform, rotation);
1833
1834 if (buffer == null) {
1835 Log.w(TAG, "Failed to take screenshot");
1836 return null;
1837 }
Peiyong Lin9d427402019-01-23 18:39:06 -08001838 // TODO(b/116112787) Now that hardware bitmap creation can take color space, we
1839 // should continue to fix screenshot.
1840 return Bitmap.wrapHardwareBuffer(HardwareBuffer.createFromGraphicBuffer(buffer),
1841 ColorSpace.get(ColorSpace.Named.SRGB));
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001842 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001843
chaviw08520a02018-09-10 16:44:56 -07001844 /**
1845 * Captures all the surfaces in a display and returns a {@link GraphicBuffer} with the content.
1846 *
1847 * @param display The display to take the screenshot of.
1848 * @param sourceCrop The portion of the screen to capture into the Bitmap; caller may
1849 * pass in 'new Rect()' if no cropping is desired.
1850 * @param width The desired width of the returned bitmap; the raw screen will be
1851 * scaled down to this size; caller may pass in 0 if no scaling is
1852 * desired.
1853 * @param height The desired height of the returned bitmap; the raw screen will
1854 * be scaled down to this size; caller may pass in 0 if no scaling
1855 * is desired.
1856 * @param useIdentityTransform Replace whatever transformation (rotation, scaling, translation)
1857 * the surface layers are currently using with the identity
1858 * transformation while taking the screenshot.
1859 * @param rotation Apply a custom clockwise rotation to the screenshot, i.e.
1860 * Surface.ROTATION_0,90,180,270. SurfaceFlinger will always take
1861 * screenshots in its native portrait orientation by default, so
1862 * this is useful for returning screenshots that are independent of
1863 * device orientation.
1864 * @return Returns a GraphicBuffer that contains the captured content.
Robert Carra7827f72019-01-11 12:53:37 -08001865 * @hide
chaviw08520a02018-09-10 16:44:56 -07001866 */
1867 public static GraphicBuffer screenshotToBuffer(IBinder display, Rect sourceCrop, int width,
1868 int height, boolean useIdentityTransform, int rotation) {
Mathias Agopian0449a402013-03-01 23:01:51 -08001869 if (display == null) {
1870 throw new IllegalArgumentException("displayToken must not be null");
1871 }
chaviw08520a02018-09-10 16:44:56 -07001872
Robert Carr5c52b132019-02-15 15:48:11 -08001873 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1874 false /* captureSecureLayers */);
1875 }
1876
1877 /**
1878 * Like screenshotToBuffer, but if the caller is AID_SYSTEM, allows
1879 * for the capture of secure layers. This is used for the screen rotation
1880 * animation where the system server takes screenshots but does
1881 * not persist them or allow them to leave the server. However in other
1882 * cases in the system server, we mostly want to omit secure layers
1883 * like when we take a screenshot on behalf of the assistant.
1884 *
1885 * @hide
1886 */
1887 public static GraphicBuffer screenshotToBufferWithSecureLayersUnsafe(IBinder display,
1888 Rect sourceCrop, int width, int height, boolean useIdentityTransform,
1889 int rotation) {
1890 if (display == null) {
1891 throw new IllegalArgumentException("displayToken must not be null");
1892 }
1893
1894 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
1895 true /* captureSecureLayers */);
Mathias Agopian0449a402013-03-01 23:01:51 -08001896 }
Robert Carre13b58e2017-08-31 14:50:44 -07001897
chaviwa69e0a72017-11-29 17:55:12 -08001898 private static void rotateCropForSF(Rect crop, int rot) {
1899 if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270) {
1900 int tmp = crop.top;
1901 crop.top = crop.left;
1902 crop.left = tmp;
1903 tmp = crop.right;
1904 crop.right = crop.bottom;
1905 crop.bottom = tmp;
1906 }
1907 }
1908
chaviw1cda84c2017-10-23 16:47:10 -07001909 /**
chaviw1da9cd92017-12-06 10:48:11 -08001910 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
chaviw1cda84c2017-10-23 16:47:10 -07001911 *
1912 * @param layerHandleToken The root layer to capture.
chaviwfbe47df2017-11-10 16:14:49 -08001913 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
1914 * Rect()' or null if no cropping is desired.
1915 * @param frameScale The desired scale of the returned buffer; the raw
1916 * screen will be scaled up/down.
Chavi Weingartend7ec64c2017-11-30 01:52:01 +00001917 *
1918 * @return Returns a GraphicBuffer that contains the layer capture.
Robert Carra7827f72019-01-11 12:53:37 -08001919 * @hide
chaviw1cda84c2017-10-23 16:47:10 -07001920 */
Chavi Weingartend7ec64c2017-11-30 01:52:01 +00001921 public static GraphicBuffer captureLayers(IBinder layerHandleToken, Rect sourceCrop,
chaviwfbe47df2017-11-10 16:14:49 -08001922 float frameScale) {
1923 return nativeCaptureLayers(layerHandleToken, sourceCrop, frameScale);
chaviw1cda84c2017-10-23 16:47:10 -07001924 }
1925
Robert Carra7827f72019-01-11 12:53:37 -08001926 /**
Peiyong Lin0ddb7d42019-01-16 13:25:09 -08001927 * Returns whether protected content is supported in GPU composition.
1928 * @hide
1929 */
1930 public static boolean getProtectedContentSupport() {
1931 return nativeGetProtectedContentSupport();
1932 }
1933
1934 /**
Robert Carr76907ee2019-01-11 13:38:19 -08001935 * An atomic set of changes to a set of SurfaceControl.
Robert Carra7827f72019-01-11 12:53:37 -08001936 */
Robert Carre13b58e2017-08-31 14:50:44 -07001937 public static class Transaction implements Closeable {
Robert Carr76907ee2019-01-11 13:38:19 -08001938 /**
1939 * @hide
1940 */
Robert Carre13b58e2017-08-31 14:50:44 -07001941 public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
1942 Transaction.class.getClassLoader(),
1943 nativeGetNativeTransactionFinalizer(), 512);
1944 private long mNativeObject;
1945
Jorim Jaggia5e10572017-11-15 14:36:26 +01001946 private final ArrayMap<SurfaceControl, Point> mResizedSurfaces = new ArrayMap<>();
Robert Carre13b58e2017-08-31 14:50:44 -07001947 Runnable mFreeNativeResources;
1948
Robert Carra7827f72019-01-11 12:53:37 -08001949 /**
Robert Carr76907ee2019-01-11 13:38:19 -08001950 * Open a new transaction object. The transaction may be filed with commands to
1951 * manipulate {@link SurfaceControl} instances, and then applied atomically with
1952 * {@link #apply}. Eventually the user should invoke {@link #close}, when the object
1953 * is no longer required. Note however that re-using a transaction after a call to apply
1954 * is allowed as a convenience.
Robert Carra7827f72019-01-11 12:53:37 -08001955 */
Robert Carre13b58e2017-08-31 14:50:44 -07001956 public Transaction() {
1957 mNativeObject = nativeCreateTransaction();
1958 mFreeNativeResources
1959 = sRegistry.registerNativeAllocation(this, mNativeObject);
1960 }
1961
1962 /**
1963 * Apply the transaction, clearing it's state, and making it usable
1964 * as a new transaction.
1965 */
1966 public void apply() {
1967 apply(false);
1968 }
1969
1970 /**
1971 * Close the transaction, if the transaction was not already applied this will cancel the
1972 * transaction.
1973 */
1974 @Override
1975 public void close() {
1976 mFreeNativeResources.run();
1977 mNativeObject = 0;
1978 }
1979
1980 /**
1981 * Jankier version of apply. Avoid use (b/28068298).
Robert Carra7827f72019-01-11 12:53:37 -08001982 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07001983 */
1984 public void apply(boolean sync) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01001985 applyResizedSurfaces();
Robert Carre13b58e2017-08-31 14:50:44 -07001986 nativeApplyTransaction(mNativeObject, sync);
1987 }
1988
Jorim Jaggia5e10572017-11-15 14:36:26 +01001989 private void applyResizedSurfaces() {
1990 for (int i = mResizedSurfaces.size() - 1; i >= 0; i--) {
1991 final Point size = mResizedSurfaces.valueAt(i);
1992 final SurfaceControl surfaceControl = mResizedSurfaces.keyAt(i);
1993 synchronized (surfaceControl.mSizeLock) {
1994 surfaceControl.mWidth = size.x;
1995 surfaceControl.mHeight = size.y;
1996 }
1997 }
1998 mResizedSurfaces.clear();
1999 }
2000
Robert Carra7827f72019-01-11 12:53:37 -08002001 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002002 * Toggle the visibility of a given Layer and it's sub-tree.
2003 *
2004 * @param sc The SurfaceControl for which to set the visibility
2005 * @param visible The new visibility
2006 * @return This transaction object.
2007 */
2008 @NonNull
2009 public Transaction setVisibility(@NonNull SurfaceControl sc, boolean visible) {
2010 sc.checkNotReleased();
2011 if (visible) {
2012 return show(sc);
2013 } else {
2014 return hide(sc);
2015 }
2016 }
2017
2018 /**
2019 * Request that a given surface and it's sub-tree be shown.
2020 *
2021 * @param sc The surface to show.
2022 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002023 * @hide
2024 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002025 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002026 public Transaction show(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002027 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002028 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_HIDDEN);
2029 return this;
2030 }
2031
Robert Carra7827f72019-01-11 12:53:37 -08002032 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002033 * Request that a given surface and it's sub-tree be hidden.
2034 *
2035 * @param sc The surface to hidden.
2036 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002037 * @hide
2038 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002039 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002040 public Transaction hide(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002041 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002042 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_HIDDEN, SURFACE_HIDDEN);
2043 return this;
2044 }
2045
Robert Carra7827f72019-01-11 12:53:37 -08002046 /**
2047 * @hide
2048 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002049 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002050 public Transaction setPosition(SurfaceControl sc, float x, float y) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002051 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002052 nativeSetPosition(mNativeObject, sc.mNativeObject, x, y);
2053 return this;
2054 }
2055
Robert Carra7827f72019-01-11 12:53:37 -08002056 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002057 * Set the default buffer size for the SurfaceControl, if there is an
2058 * {@link Surface} assosciated with the control, then
2059 * this will be the default size for buffers dequeued from it.
2060 * @param sc The surface to set the buffer size for.
2061 * @param w The default width
2062 * @param h The default height
2063 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002064 */
Robert Carr76907ee2019-01-11 13:38:19 -08002065 @NonNull
2066 public Transaction setBufferSize(@NonNull SurfaceControl sc,
2067 @IntRange(from = 0) int w, @IntRange(from = 0) int h) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002068 sc.checkNotReleased();
Jorim Jaggia5e10572017-11-15 14:36:26 +01002069 mResizedSurfaces.put(sc, new Point(w, h));
Jorim Jaggidfc27372017-10-27 17:47:49 +02002070 nativeSetSize(mNativeObject, sc.mNativeObject, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07002071 return this;
2072 }
2073
Robert Carra7827f72019-01-11 12:53:37 -08002074 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002075 * Set the Z-order for a given SurfaceControl, relative to it's siblings.
2076 * If two siblings share the same Z order the ordering is undefined. Surfaces
2077 * with a negative Z will be placed below the parent surface.
2078 *
2079 * @param sc The SurfaceControl to set the Z order on
2080 * @param z The Z-order
2081 * @return This Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002082 */
Robert Carr76907ee2019-01-11 13:38:19 -08002083 @NonNull
2084 public Transaction setLayer(@NonNull SurfaceControl sc,
2085 @IntRange(from = Integer.MIN_VALUE, to = Integer.MAX_VALUE) int z) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002086 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002087 nativeSetLayer(mNativeObject, sc.mNativeObject, z);
2088 return this;
2089 }
2090
Robert Carra7827f72019-01-11 12:53:37 -08002091 /**
2092 * @hide
2093 */
Robert Carr77e34942017-10-18 19:13:56 -07002094 public Transaction setRelativeLayer(SurfaceControl sc, SurfaceControl relativeTo, int z) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002095 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002096 nativeSetRelativeLayer(mNativeObject, sc.mNativeObject,
Robert Carr77e34942017-10-18 19:13:56 -07002097 relativeTo.getHandle(), z);
Robert Carre13b58e2017-08-31 14:50:44 -07002098 return this;
2099 }
2100
Robert Carra7827f72019-01-11 12:53:37 -08002101 /**
2102 * @hide
2103 */
Robert Carre13b58e2017-08-31 14:50:44 -07002104 public Transaction setTransparentRegionHint(SurfaceControl sc, Region transparentRegion) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002105 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002106 nativeSetTransparentRegionHint(mNativeObject,
2107 sc.mNativeObject, transparentRegion);
2108 return this;
2109 }
2110
Robert Carra7827f72019-01-11 12:53:37 -08002111 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002112 * Set the alpha for a given surface. If the alpha is non-zero the SurfaceControl
2113 * will be blended with the Surfaces under it according to the specified ratio.
2114 *
2115 * @param sc The given SurfaceControl.
2116 * @param alpha The alpha to set.
Robert Carra7827f72019-01-11 12:53:37 -08002117 */
Robert Carr76907ee2019-01-11 13:38:19 -08002118 @NonNull
2119 public Transaction setAlpha(@NonNull SurfaceControl sc,
2120 @FloatRange(from = 0.0, to = 1.0) float alpha) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002121 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002122 nativeSetAlpha(mNativeObject, sc.mNativeObject, alpha);
2123 return this;
2124 }
2125
Robert Carra7827f72019-01-11 12:53:37 -08002126 /**
2127 * @hide
2128 */
Robert Carr788f5742018-07-30 17:46:45 -07002129 public Transaction setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle) {
2130 sc.checkNotReleased();
2131 nativeSetInputWindowInfo(mNativeObject, sc.mNativeObject, handle);
2132 return this;
2133 }
2134
chaviw59f532e2018-12-26 15:34:59 -08002135 /**
2136 * Transfers touch focus from one window to another. It is possible for multiple windows to
2137 * have touch focus if they support split touch dispatch
2138 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this
2139 * method only transfers touch focus of the specified window without affecting
2140 * other windows that may also have touch focus at the same time.
2141 * @param fromToken The token of a window that currently has touch focus.
2142 * @param toToken The token of the window that should receive touch focus in
2143 * place of the first.
Robert Carra7827f72019-01-11 12:53:37 -08002144 * @hide
chaviw59f532e2018-12-26 15:34:59 -08002145 */
2146 public Transaction transferTouchFocus(IBinder fromToken, IBinder toToken) {
2147 nativeTransferTouchFocus(mNativeObject, fromToken, toToken);
2148 return this;
2149 }
2150
Robert Carra7827f72019-01-11 12:53:37 -08002151 /**
chaviw319cd0782019-02-14 11:00:23 -08002152 * Waits until any changes to input windows have been sent from SurfaceFlinger to
2153 * InputFlinger before returning.
2154 *
2155 * @hide
2156 */
2157 public Transaction syncInputWindows() {
2158 nativeSyncInputWindows(mNativeObject);
2159 return this;
2160 }
2161
2162 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002163 * Specify how the buffer assosciated with this Surface is mapped in to the
2164 * parent coordinate space. The source frame will be scaled to fit the destination
2165 * frame, after being rotated according to the orientation parameter.
2166 *
2167 * @param sc The SurfaceControl to specify the geometry of
2168 * @param sourceCrop The source rectangle in buffer space. Or null for the entire buffer.
2169 * @param destFrame The destination rectangle in parent space. Or null for the source frame.
2170 * @param orientation The buffer rotation
2171 * @return This transaction object.
2172 */
2173 @NonNull
2174 public Transaction setGeometry(@NonNull SurfaceControl sc, @Nullable Rect sourceCrop,
2175 @Nullable Rect destFrame, @Surface.Rotation int orientation) {
2176 sc.checkNotReleased();
2177 nativeSetGeometry(mNativeObject, sc.mNativeObject, sourceCrop, destFrame, orientation);
2178 return this;
2179 }
2180
2181 /**
Robert Carra7827f72019-01-11 12:53:37 -08002182 * @hide
2183 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002184 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002185 public Transaction setMatrix(SurfaceControl sc,
2186 float dsdx, float dtdx, float dtdy, float dsdy) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002187 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002188 nativeSetMatrix(mNativeObject, sc.mNativeObject,
2189 dsdx, dtdx, dtdy, dsdy);
2190 return this;
2191 }
2192
Robert Carra7827f72019-01-11 12:53:37 -08002193 /**
2194 * @hide
2195 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002196 @UnsupportedAppUsage
Jorim Jaggi21c39a72017-10-20 15:47:51 +02002197 public Transaction setMatrix(SurfaceControl sc, Matrix matrix, float[] float9) {
2198 matrix.getValues(float9);
2199 setMatrix(sc, float9[MSCALE_X], float9[MSKEW_Y],
2200 float9[MSKEW_X], float9[MSCALE_Y]);
2201 setPosition(sc, float9[MTRANS_X], float9[MTRANS_Y]);
2202 return this;
2203 }
2204
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002205 /**
2206 * Sets the color transform for the Surface.
2207 * @param matrix A float array with 9 values represents a 3x3 transform matrix
2208 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08002209 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002210 */
2211 public Transaction setColorTransform(SurfaceControl sc, @Size(9) float[] matrix,
2212 @Size(3) float[] translation) {
2213 sc.checkNotReleased();
2214 nativeSetColorTransform(mNativeObject, sc.mNativeObject, matrix, translation);
2215 return this;
2216 }
2217
Robert Carra7827f72019-01-11 12:53:37 -08002218 /**
2219 * @hide
2220 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002221 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002222 public Transaction setWindowCrop(SurfaceControl sc, Rect crop) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002223 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002224 if (crop != null) {
2225 nativeSetWindowCrop(mNativeObject, sc.mNativeObject,
2226 crop.left, crop.top, crop.right, crop.bottom);
2227 } else {
2228 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, 0, 0);
2229 }
2230
2231 return this;
2232 }
2233
Robert Carra7827f72019-01-11 12:53:37 -08002234 /**
2235 * @hide
2236 */
Vishnu Naird454442d2018-11-13 13:51:01 -08002237 public Transaction setWindowCrop(SurfaceControl sc, int width, int height) {
2238 sc.checkNotReleased();
2239 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, width, height);
2240 return this;
2241 }
2242
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002243 /**
2244 * Sets the corner radius of a {@link SurfaceControl}.
2245 * @param sc SurfaceControl
2246 * @param cornerRadius Corner radius in pixels.
2247 * @return Itself.
Robert Carra7827f72019-01-11 12:53:37 -08002248 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002249 */
2250 @UnsupportedAppUsage
2251 public Transaction setCornerRadius(SurfaceControl sc, float cornerRadius) {
2252 sc.checkNotReleased();
2253 nativeSetCornerRadius(mNativeObject, sc.mNativeObject, cornerRadius);
2254
2255 return this;
2256 }
2257
Robert Carra7827f72019-01-11 12:53:37 -08002258 /**
2259 * @hide
2260 */
Mathew Inwood679c15e2019-02-06 15:36:04 +00002261 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.O)
Robert Carre13b58e2017-08-31 14:50:44 -07002262 public Transaction setLayerStack(SurfaceControl sc, int layerStack) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002263 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002264 nativeSetLayerStack(mNativeObject, sc.mNativeObject, layerStack);
2265 return this;
2266 }
2267
Robert Carra7827f72019-01-11 12:53:37 -08002268 /**
2269 * @hide
2270 */
Robert Carr76907ee2019-01-11 13:38:19 -08002271 @UnsupportedAppUsage
Jorim Jaggidfc27372017-10-27 17:47:49 +02002272 public Transaction deferTransactionUntil(SurfaceControl sc, IBinder handle,
2273 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002274 if (frameNumber < 0) {
2275 return this;
2276 }
Jorim Jaggidfc27372017-10-27 17:47:49 +02002277 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002278 nativeDeferTransactionUntil(mNativeObject, sc.mNativeObject, handle, frameNumber);
2279 return this;
2280 }
2281
Robert Carra7827f72019-01-11 12:53:37 -08002282 /**
2283 * @hide
2284 */
Robert Carr76907ee2019-01-11 13:38:19 -08002285 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002286 public Transaction deferTransactionUntilSurface(SurfaceControl sc, Surface barrierSurface,
2287 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002288 if (frameNumber < 0) {
2289 return this;
2290 }
Jorim Jaggidfc27372017-10-27 17:47:49 +02002291 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002292 nativeDeferTransactionUntilSurface(mNativeObject, sc.mNativeObject,
2293 barrierSurface.mNativeObject, frameNumber);
2294 return this;
2295 }
2296
Robert Carra7827f72019-01-11 12:53:37 -08002297 /**
2298 * @hide
2299 */
Robert Carre13b58e2017-08-31 14:50:44 -07002300 public Transaction reparentChildren(SurfaceControl sc, IBinder newParentHandle) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002301 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002302 nativeReparentChildren(mNativeObject, sc.mNativeObject, newParentHandle);
2303 return this;
2304 }
2305
Robert Carr10584fa2019-01-14 15:55:19 -08002306 /**
2307 * Re-parents a given layer to a new parent. Children inherit transform (position, scaling)
2308 * crop, visibility, and Z-ordering from their parents, as if the children were pixels within the
2309 * parent Surface.
2310 *
2311 * @param sc The SurfaceControl to reparent
2312 * @param newParent The new parent for the given control.
2313 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002314 */
Robert Carr76907ee2019-01-11 13:38:19 -08002315 @NonNull
2316 public Transaction reparent(@NonNull SurfaceControl sc,
2317 @Nullable SurfaceControl newParent) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002318 sc.checkNotReleased();
Robert Carr10584fa2019-01-14 15:55:19 -08002319 long otherObject = 0;
2320 if (newParent != null) {
2321 newParent.checkNotReleased();
2322 otherObject = newParent.mNativeObject;
2323 }
2324 nativeReparent(mNativeObject, sc.mNativeObject, otherObject);
Robert Carre13b58e2017-08-31 14:50:44 -07002325 return this;
2326 }
2327
Robert Carra7827f72019-01-11 12:53:37 -08002328 /**
2329 * @hide
2330 */
Robert Carre13b58e2017-08-31 14:50:44 -07002331 public Transaction detachChildren(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002332 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002333 nativeSeverChildren(mNativeObject, sc.mNativeObject);
2334 return this;
2335 }
2336
Robert Carra7827f72019-01-11 12:53:37 -08002337 /**
2338 * @hide
2339 */
Robert Carre13b58e2017-08-31 14:50:44 -07002340 public Transaction setOverrideScalingMode(SurfaceControl sc, int overrideScalingMode) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002341 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002342 nativeSetOverrideScalingMode(mNativeObject, sc.mNativeObject,
2343 overrideScalingMode);
2344 return this;
2345 }
2346
2347 /**
2348 * Sets a color for the Surface.
2349 * @param color A float array with three values to represent r, g, b in range [0..1]
Robert Carra7827f72019-01-11 12:53:37 -08002350 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002351 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002352 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002353 public Transaction setColor(SurfaceControl sc, @Size(3) float[] color) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002354 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002355 nativeSetColor(mNativeObject, sc.mNativeObject, color);
2356 return this;
2357 }
2358
2359 /**
2360 * If the buffer size changes in this transaction, position and crop updates specified
2361 * in this transaction will not complete until a buffer of the new size
2362 * arrives. As transform matrix and size are already frozen in this fashion,
2363 * this enables totally freezing the surface until the resize has completed
2364 * (at which point the geometry influencing aspects of this transaction will then occur)
Robert Carra7827f72019-01-11 12:53:37 -08002365 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002366 */
2367 public Transaction setGeometryAppliesWithResize(SurfaceControl sc) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002368 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002369 nativeSetGeometryAppliesWithResize(mNativeObject, sc.mNativeObject);
2370 return this;
2371 }
2372
2373 /**
2374 * Sets the security of the surface. Setting the flag is equivalent to creating the
2375 * Surface with the {@link #SECURE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002376 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002377 */
Robert Carrb1579c82017-09-05 14:54:47 -07002378 public Transaction setSecure(SurfaceControl sc, boolean isSecure) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002379 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002380 if (isSecure) {
2381 nativeSetFlags(mNativeObject, sc.mNativeObject, SECURE, SECURE);
2382 } else {
2383 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SECURE);
2384 }
2385 return this;
2386 }
2387
2388 /**
2389 * Sets the opacity of the surface. Setting the flag is equivalent to creating the
2390 * Surface with the {@link #OPAQUE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002391 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002392 */
2393 public Transaction setOpaque(SurfaceControl sc, boolean isOpaque) {
Jorim Jaggidfc27372017-10-27 17:47:49 +02002394 sc.checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07002395 if (isOpaque) {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002396 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_OPAQUE, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002397 } else {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002398 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002399 }
2400 return this;
2401 }
2402
Robert Carra7827f72019-01-11 12:53:37 -08002403 /**
2404 * @hide
2405 */
Robert Carre13b58e2017-08-31 14:50:44 -07002406 public Transaction setDisplaySurface(IBinder displayToken, Surface surface) {
2407 if (displayToken == null) {
2408 throw new IllegalArgumentException("displayToken must not be null");
2409 }
2410
2411 if (surface != null) {
2412 synchronized (surface.mLock) {
2413 nativeSetDisplaySurface(mNativeObject, displayToken, surface.mNativeObject);
2414 }
2415 } else {
2416 nativeSetDisplaySurface(mNativeObject, displayToken, 0);
2417 }
2418 return this;
2419 }
2420
Robert Carra7827f72019-01-11 12:53:37 -08002421 /**
2422 * @hide
2423 */
Robert Carre13b58e2017-08-31 14:50:44 -07002424 public Transaction setDisplayLayerStack(IBinder displayToken, int layerStack) {
2425 if (displayToken == null) {
2426 throw new IllegalArgumentException("displayToken must not be null");
2427 }
2428 nativeSetDisplayLayerStack(mNativeObject, displayToken, layerStack);
2429 return this;
2430 }
2431
Robert Carra7827f72019-01-11 12:53:37 -08002432 /**
2433 * @hide
2434 */
Robert Carre13b58e2017-08-31 14:50:44 -07002435 public Transaction setDisplayProjection(IBinder displayToken,
2436 int orientation, Rect layerStackRect, Rect displayRect) {
2437 if (displayToken == null) {
2438 throw new IllegalArgumentException("displayToken must not be null");
2439 }
2440 if (layerStackRect == null) {
2441 throw new IllegalArgumentException("layerStackRect must not be null");
2442 }
2443 if (displayRect == null) {
2444 throw new IllegalArgumentException("displayRect must not be null");
2445 }
2446 nativeSetDisplayProjection(mNativeObject, displayToken, orientation,
2447 layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom,
2448 displayRect.left, displayRect.top, displayRect.right, displayRect.bottom);
2449 return this;
2450 }
2451
Robert Carra7827f72019-01-11 12:53:37 -08002452 /**
2453 * @hide
2454 */
Robert Carre13b58e2017-08-31 14:50:44 -07002455 public Transaction setDisplaySize(IBinder displayToken, int width, int height) {
2456 if (displayToken == null) {
2457 throw new IllegalArgumentException("displayToken must not be null");
2458 }
2459 if (width <= 0 || height <= 0) {
2460 throw new IllegalArgumentException("width and height must be positive");
2461 }
2462
2463 nativeSetDisplaySize(mNativeObject, displayToken, width, height);
2464 return this;
2465 }
2466
Robert Carra7827f72019-01-11 12:53:37 -08002467 /** flag the transaction as an animation
2468 * @hide
2469 */
Robert Carre13b58e2017-08-31 14:50:44 -07002470 public Transaction setAnimationTransaction() {
2471 nativeSetAnimationTransaction(mNativeObject);
2472 return this;
2473 }
Robert Carrb1579c82017-09-05 14:54:47 -07002474
2475 /**
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002476 * Indicate that SurfaceFlinger should wake up earlier than usual as a result of this
2477 * transaction. This should be used when the caller thinks that the scene is complex enough
2478 * that it's likely to hit GL composition, and thus, SurfaceFlinger needs to more time in
2479 * order not to miss frame deadlines.
2480 * <p>
2481 * Corresponds to setting ISurfaceComposer::eEarlyWakeup
Robert Carra7827f72019-01-11 12:53:37 -08002482 * @hide
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002483 */
2484 public Transaction setEarlyWakeup() {
2485 nativeSetEarlyWakeup(mNativeObject);
2486 return this;
2487 }
2488
2489 /**
Evan Rosky485df202018-12-06 14:11:12 -08002490 * Sets an arbitrary piece of metadata on the surface. This is a helper for int data.
2491 * @hide
2492 */
2493 public Transaction setMetadata(int key, int data) {
2494 Parcel parcel = Parcel.obtain();
2495 parcel.writeInt(data);
2496 try {
2497 setMetadata(key, parcel);
2498 } finally {
2499 parcel.recycle();
2500 }
2501 return this;
2502 }
2503
2504 /**
2505 * Sets an arbitrary piece of metadata on the surface.
2506 * @hide
2507 */
2508 public Transaction setMetadata(int key, Parcel data) {
2509 nativeSetMetadata(mNativeObject, key, data);
2510 return this;
2511 }
2512
2513 /**
Robert Carrb1579c82017-09-05 14:54:47 -07002514 * Merge the other transaction into this transaction, clearing the
2515 * other transaction as if it had been applied.
Robert Carr76907ee2019-01-11 13:38:19 -08002516 *
2517 * @param other The transaction to merge in to this one.
2518 * @return This transaction.
Robert Carrb1579c82017-09-05 14:54:47 -07002519 */
Robert Carr76907ee2019-01-11 13:38:19 -08002520 @NonNull
2521 public Transaction merge(@NonNull Transaction other) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002522 mResizedSurfaces.putAll(other.mResizedSurfaces);
2523 other.mResizedSurfaces.clear();
Robert Carrb1579c82017-09-05 14:54:47 -07002524 nativeMergeTransaction(mNativeObject, other.mNativeObject);
2525 return this;
2526 }
Robert Carr71200f22019-02-05 09:44:53 -08002527
2528 /**
2529 * Equivalent to reparent with a null parent, in that it removes
2530 * the SurfaceControl from the scene, but it also releases
2531 * the local resources (by calling {@link SurfaceControl#release})
2532 * after this method returns, {@link SurfaceControl#isValid} will return
2533 * false for the argument.
2534 *
2535 * @param sc The surface to remove and release.
2536 * @return This transaction
2537 * @hide
2538 */
2539 @NonNull
2540 public Transaction remove(@NonNull SurfaceControl sc) {
2541 reparent(sc, null);
2542 sc.release();
2543 return this;
2544 }
Robert Carre13b58e2017-08-31 14:50:44 -07002545 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08002546}