blob: daeb1c9c1e01580adfc7593c0cc53f3f748ce78e [file] [log] [blame]
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Jorim Jaggi21c39a72017-10-20 15:47:51 +020019import static android.graphics.Matrix.MSCALE_X;
20import static android.graphics.Matrix.MSCALE_Y;
21import static android.graphics.Matrix.MSKEW_X;
22import static android.graphics.Matrix.MSKEW_Y;
23import static android.graphics.Matrix.MTRANS_X;
24import static android.graphics.Matrix.MTRANS_Y;
Vishnu Nair04ab4392018-01-10 11:00:06 -080025import static android.view.Surface.ROTATION_270;
26import static android.view.Surface.ROTATION_90;
27import static android.view.SurfaceControlProto.HASH_CODE;
28import static android.view.SurfaceControlProto.NAME;
chaviwa69e0a72017-11-29 17:55:12 -080029
Robert Carr76907ee2019-01-11 13:38:19 -080030import android.annotation.FloatRange;
31import android.annotation.IntRange;
32import android.annotation.NonNull;
33import android.annotation.Nullable;
chaviw0dd03f52017-08-25 12:15:26 -070034import android.annotation.Size;
Steven Thomas6ec6fbc2020-03-24 16:06:33 -070035import android.annotation.TestApi;
Artur Satayevad9254c2019-12-10 17:47:54 +000036import android.compat.annotation.UnsupportedAppUsage;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080037import android.graphics.Bitmap;
Peiyong Lin5f4a5682019-01-17 11:37:07 -080038import android.graphics.ColorSpace;
Robert Carr6486d312017-01-09 19:48:29 -080039import android.graphics.GraphicBuffer;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020040import android.graphics.Matrix;
Vishnu Nair04ab4392018-01-10 11:00:06 -080041import android.graphics.PixelFormat;
Jorim Jaggia5e10572017-11-15 14:36:26 +010042import android.graphics.Point;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080043import android.graphics.Rect;
44import android.graphics.Region;
Marin Shalamanov98af1592019-10-08 10:48:08 +020045import android.hardware.display.DeviceProductInfo;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070046import android.hardware.display.DisplayedContentSample;
47import android.hardware.display.DisplayedContentSamplingAttributes;
Mathew Inwood679c15e2019-02-06 15:36:04 +000048import android.os.Build;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080049import android.os.IBinder;
Jorim Jaggi06975df2017-12-01 14:52:13 +010050import android.os.Parcel;
51import android.os.Parcelable;
Jorim Jaggibfa95a72020-06-18 22:51:49 +020052import android.os.Trace;
Jorim Jaggia5e10572017-11-15 14:36:26 +010053import android.util.ArrayMap;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080054import android.util.Log;
Evan Rosky485df202018-12-06 14:11:12 -080055import android.util.SparseIntArray;
Vishnu Nair04ab4392018-01-10 11:00:06 -080056import android.util.proto.ProtoOutputStream;
Igor Murashkina86ab6402013-08-30 12:58:36 -070057import android.view.Surface.OutOfResourcesException;
Jorim Jaggia5e10572017-11-15 14:36:26 +010058
59import com.android.internal.annotations.GuardedBy;
60
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070061import dalvik.system.CloseGuard;
Vishnu Nair04ab4392018-01-10 11:00:06 -080062
Robert Carre625fcf2017-09-01 12:36:28 -070063import libcore.util.NativeAllocationRegistry;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070064
Robert Carre13b58e2017-08-31 14:50:44 -070065import java.io.Closeable;
Svet Ganova5b49902020-06-18 00:47:22 -070066import java.lang.ref.WeakReference;
Evan Rosky485df202018-12-06 14:11:12 -080067import java.nio.ByteBuffer;
Evan Roskyc12c9a32019-02-06 13:38:03 -080068import java.nio.ByteOrder;
Svet Ganova5b49902020-06-18 00:47:22 -070069import java.util.ArrayList;
Dan Gittik832b4972019-02-13 18:17:47 +000070import java.util.Objects;
Robert Carre13b58e2017-08-31 14:50:44 -070071
Mathias Agopian3866f0d2013-02-11 22:08:48 -080072/**
Robert Carr76907ee2019-01-11 13:38:19 -080073 * Handle to an on-screen Surface managed by the system compositor. The SurfaceControl is
74 * a combination of a buffer source, and metadata about how to display the buffers.
75 * By constructing a {@link Surface} from this SurfaceControl you can submit buffers to be
76 * composited. Using {@link SurfaceControl.Transaction} you can manipulate various
77 * properties of how the buffer will be displayed on-screen. SurfaceControl's are
78 * arranged into a scene-graph like hierarchy, and as such any SurfaceControl may have
79 * a parent. Geometric properties like transform, crop, and Z-ordering will be inherited
80 * from the parent, as if the child were content in the parents buffer stream.
Mathias Agopian3866f0d2013-02-11 22:08:48 -080081 */
Robert Carr76907ee2019-01-11 13:38:19 -080082public final class SurfaceControl implements Parcelable {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080083 private static final String TAG = "SurfaceControl";
Mathias Agopian29479eb2013-02-14 14:36:04 -080084
Ashok Bhat36bef0b2014-01-20 20:08:01 +000085 private static native long nativeCreate(SurfaceSession session, String name,
Evan Rosky485df202018-12-06 14:11:12 -080086 int w, int h, int format, int flags, long parentObject, Parcel metadata)
Mathias Agopian29479eb2013-02-14 14:36:04 -080087 throws OutOfResourcesException;
Jorim Jaggi06975df2017-12-01 14:52:13 +010088 private static native long nativeReadFromParcel(Parcel in);
chaviwbeb7a0c2018-12-05 13:49:54 -080089 private static native long nativeCopyFromSurfaceControl(long nativeObject);
Jorim Jaggi06975df2017-12-01 14:52:13 +010090 private static native void nativeWriteToParcel(long nativeObject, Parcel out);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000091 private static native void nativeRelease(long nativeObject);
Chong Zhang47e36a32016-02-29 16:44:33 -080092 private static native void nativeDisconnect(long nativeObject);
Mathias Agopian29479eb2013-02-14 14:36:04 -080093
Peiyong Line3e5efd2019-03-21 20:59:47 +000094 private static native ScreenshotGraphicBuffer nativeScreenshot(IBinder displayToken,
Robert Carr5c52b132019-02-15 15:48:11 -080095 Rect sourceCrop, int width, int height, boolean useIdentityTransform, int rotation,
96 boolean captureSecureLayers);
Peiyong Lin21e499a2019-04-03 16:37:46 -070097 private static native ScreenshotGraphicBuffer nativeCaptureLayers(IBinder displayToken,
Chiawei Wang02202d12019-01-03 18:12:13 +080098 long layerObject, Rect sourceCrop, float frameScale, long[] excludeLayerObjects,
99 int format);
chaviwa51724f2019-09-19 09:50:11 -0700100 private static native long nativeMirrorSurface(long mirrorOfObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700101 private static native long nativeCreateTransaction();
102 private static native long nativeGetNativeTransactionFinalizer();
103 private static native void nativeApplyTransaction(long transactionObj, boolean sync);
Robert Carrb1579c82017-09-05 14:54:47 -0700104 private static native void nativeMergeTransaction(long transactionObj,
105 long otherTransactionObj);
Robert Carre13b58e2017-08-31 14:50:44 -0700106 private static native void nativeSetAnimationTransaction(long transactionObj);
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100107 private static native void nativeSetEarlyWakeup(long transactionObj);
Vishnu Nair2ed39d82020-06-17 15:43:13 -0700108 private static native void nativeSetEarlyWakeupStart(long transactionObj);
109 private static native void nativeSetEarlyWakeupEnd(long transactionObj);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800110
Robert Carre13b58e2017-08-31 14:50:44 -0700111 private static native void nativeSetLayer(long transactionObj, long nativeObject, int zorder);
112 private static native void nativeSetRelativeLayer(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700113 long relativeToObject, int zorder);
Robert Carre13b58e2017-08-31 14:50:44 -0700114 private static native void nativeSetPosition(long transactionObj, long nativeObject,
115 float x, float y);
Robert Carre13b58e2017-08-31 14:50:44 -0700116 private static native void nativeSetSize(long transactionObj, long nativeObject, int w, int h);
117 private static native void nativeSetTransparentRegionHint(long transactionObj,
118 long nativeObject, Region region);
119 private static native void nativeSetAlpha(long transactionObj, long nativeObject, float alpha);
120 private static native void nativeSetMatrix(long transactionObj, long nativeObject,
121 float dsdx, float dtdx,
Andrii Kulian283acd22017-08-03 04:03:51 -0700122 float dtdy, float dsdy);
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700123 private static native void nativeSetColorTransform(long transactionObj, long nativeObject,
124 float[] matrix, float[] translation);
Peiyong Linf4f0f642019-03-01 14:36:05 -0800125 private static native void nativeSetColorSpaceAgnostic(long transactionObj, long nativeObject,
126 boolean agnostic);
Robert Carr76907ee2019-01-11 13:38:19 -0800127 private static native void nativeSetGeometry(long transactionObj, long nativeObject,
128 Rect sourceCrop, Rect dest, long orientation);
Robert Carre13b58e2017-08-31 14:50:44 -0700129 private static native void nativeSetColor(long transactionObj, long nativeObject, float[] color);
130 private static native void nativeSetFlags(long transactionObj, long nativeObject,
131 int flags, int mask);
Ana Krulecfea97172019-11-02 23:11:02 +0100132 private static native void nativeSetFrameRateSelectionPriority(long transactionObj,
133 long nativeObject, int priority);
Robert Carre13b58e2017-08-31 14:50:44 -0700134 private static native void nativeSetWindowCrop(long transactionObj, long nativeObject,
135 int l, int t, int r, int b);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -0700136 private static native void nativeSetCornerRadius(long transactionObj, long nativeObject,
137 float cornerRadius);
Lucas Dupin991415e2019-11-25 17:48:58 -0800138 private static native void nativeSetBackgroundBlurRadius(long transactionObj, long nativeObject,
139 int blurRadius);
Robert Carre13b58e2017-08-31 14:50:44 -0700140 private static native void nativeSetLayerStack(long transactionObj, long nativeObject,
141 int layerStack);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800142
Svetoslav1376d602014-03-13 11:17:26 -0700143 private static native boolean nativeClearContentFrameStats(long nativeObject);
144 private static native boolean nativeGetContentFrameStats(long nativeObject, WindowContentFrameStats outStats);
145 private static native boolean nativeClearAnimationFrameStats();
146 private static native boolean nativeGetAnimationFrameStats(WindowAnimationFrameStats outStats);
147
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800148 private static native long[] nativeGetPhysicalDisplayIds();
149 private static native IBinder nativeGetPhysicalDisplayToken(long physicalDisplayId);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800150 private static native IBinder nativeCreateDisplay(String name, boolean secure);
Jesse Hall6a6bc212013-08-08 12:15:03 -0700151 private static native void nativeDestroyDisplay(IBinder displayToken);
Robert Carre13b58e2017-08-31 14:50:44 -0700152 private static native void nativeSetDisplaySurface(long transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000153 IBinder displayToken, long nativeSurfaceObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700154 private static native void nativeSetDisplayLayerStack(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800155 IBinder displayToken, int layerStack);
Robert Carre13b58e2017-08-31 14:50:44 -0700156 private static native void nativeSetDisplayProjection(long transactionObj,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800157 IBinder displayToken, int orientation,
Jesse Hall6a6bc212013-08-08 12:15:03 -0700158 int l, int t, int r, int b,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800159 int L, int T, int R, int B);
Robert Carre13b58e2017-08-31 14:50:44 -0700160 private static native void nativeSetDisplaySize(long transactionObj, IBinder displayToken,
161 int width, int height);
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800162 private static native SurfaceControl.DisplayInfo nativeGetDisplayInfo(IBinder displayToken);
163 private static native SurfaceControl.DisplayConfig[] nativeGetDisplayConfigs(
Dan Stoza00101052014-05-02 15:23:40 -0700164 IBinder displayToken);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -0700165 private static native DisplayedContentSamplingAttributes
166 nativeGetDisplayedContentSamplingAttributes(IBinder displayToken);
167 private static native boolean nativeSetDisplayedContentSamplingEnabled(IBinder displayToken,
168 boolean enable, int componentMask, int maxFrames);
169 private static native DisplayedContentSample nativeGetDisplayedContentSample(
170 IBinder displayToken, long numFrames, long timestamp);
Dan Stoza00101052014-05-02 15:23:40 -0700171 private static native int nativeGetActiveConfig(IBinder displayToken);
Ana Krulec4f753aa2019-11-14 00:49:39 +0100172 private static native boolean nativeSetDesiredDisplayConfigSpecs(IBinder displayToken,
Ana Krulec52f12892019-11-18 03:57:20 -0800173 SurfaceControl.DesiredDisplayConfigSpecs desiredDisplayConfigSpecs);
Ana Kruleca74a8642019-11-14 00:51:00 +0100174 private static native SurfaceControl.DesiredDisplayConfigSpecs
175 nativeGetDesiredDisplayConfigSpecs(IBinder displayToken);
Michael Wright1c9977b2016-07-12 13:30:10 -0700176 private static native int[] nativeGetDisplayColorModes(IBinder displayToken);
Daniel Solomon10e3b332019-01-20 21:09:11 -0800177 private static native SurfaceControl.DisplayPrimaries nativeGetDisplayNativePrimaries(
178 IBinder displayToken);
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800179 private static native int[] nativeGetCompositionDataspaces();
Michael Wright1c9977b2016-07-12 13:30:10 -0700180 private static native int nativeGetActiveColorMode(IBinder displayToken);
181 private static native boolean nativeSetActiveColorMode(IBinder displayToken,
182 int colorMode);
Galia Peycheva056b3ee2019-06-26 14:05:12 +0200183 private static native void nativeSetAutoLowLatencyMode(IBinder displayToken, boolean on);
184 private static native void nativeSetGameContentType(IBinder displayToken, boolean on);
Prashant Malanic55929a2014-05-25 01:59:21 -0700185 private static native void nativeSetDisplayPowerMode(
186 IBinder displayToken, int mode);
Robert Carre13b58e2017-08-31 14:50:44 -0700187 private static native void nativeDeferTransactionUntil(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700188 long barrierObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700189 private static native void nativeDeferTransactionUntilSurface(long transactionObj,
190 long nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800191 long surfaceObject, long frame);
Robert Carre13b58e2017-08-31 14:50:44 -0700192 private static native void nativeReparentChildren(long transactionObj, long nativeObject,
Vishnu Nairbc9beab2019-06-25 17:28:58 -0700193 long newParentObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700194 private static native void nativeReparent(long transactionObj, long nativeObject,
Robert Carr10584fa2019-01-14 15:55:19 -0800195 long newParentNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700196 private static native void nativeSeverChildren(long transactionObj, long nativeObject);
197 private static native void nativeSetOverrideScalingMode(long transactionObj, long nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700198 int scalingMode);
Robert Carr6da3cc02016-06-16 15:17:07 -0700199
Michael Wright9ff94c02016-03-30 18:05:40 -0700200 private static native Display.HdrCapabilities nativeGetHdrCapabilities(IBinder displayToken);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800201
Galia Peycheva056b3ee2019-06-26 14:05:12 +0200202 private static native boolean nativeGetAutoLowLatencyModeSupport(IBinder displayToken);
203 private static native boolean nativeGetGameContentTypeSupport(IBinder displayToken);
204
Robert Carr788f5742018-07-30 17:46:45 -0700205 private static native void nativeSetInputWindowInfo(long transactionObj, long nativeObject,
206 InputWindowHandle handle);
Arthur Hungc499ad32019-09-05 15:51:14 +0800207
Peiyong Lin0ddb7d42019-01-16 13:25:09 -0800208 private static native boolean nativeGetProtectedContentSupport();
Evan Roskyb51e2462019-04-03 19:27:18 -0700209 private static native void nativeSetMetadata(long transactionObj, long nativeObject, int key,
210 Parcel data);
chaviw319cd0782019-02-14 11:00:23 -0800211 private static native void nativeSyncInputWindows(long transactionObj);
Dan Gittik832b4972019-02-13 18:17:47 +0000212 private static native boolean nativeGetDisplayBrightnessSupport(IBinder displayToken);
213 private static native boolean nativeSetDisplayBrightness(IBinder displayToken,
214 float brightness);
Vishnu Nair629df2b2019-06-11 16:03:38 -0700215 private static native long nativeReadTransactionFromParcel(Parcel in);
216 private static native void nativeWriteTransactionToParcel(long nativeObject, Parcel out);
Vishnu Naird87984d2019-11-06 14:43:22 -0800217 private static native void nativeSetShadowRadius(long transactionObj, long nativeObject,
218 float shadowRadius);
Vishnu Nair4a067c52019-11-19 14:25:56 -0800219 private static native void nativeSetGlobalShadowSettings(@Size(4) float[] ambientColor,
220 @Size(4) float[] spotColor, float lightPosY, float lightPosZ, float lightRadius);
Mathias Agopian29479eb2013-02-14 14:36:04 -0800221
Steven Thomas6cf051e2020-01-14 11:37:21 -0800222 private static native void nativeSetFrameRate(
Steven Thomasdd7bf2f2020-01-31 18:50:02 -0800223 long transactionObj, long nativeObject, float frameRate, int compatibility);
Jorim Jaggiee540702020-04-02 21:40:52 +0200224 private static native long nativeGetHandle(long nativeObject);
Steven Thomas6cf051e2020-01-14 11:37:21 -0800225
Steven Thomas6ec6fbc2020-03-24 16:06:33 -0700226 private static native long nativeAcquireFrameRateFlexibilityToken();
227 private static native void nativeReleaseFrameRateFlexibilityToken(long token);
Vishnu Naireb53e522020-05-08 18:03:12 -0700228 private static native void nativeSetFixedTransformHint(long transactionObj, long nativeObject,
229 int transformHint);
Steven Thomas6ec6fbc2020-03-24 16:06:33 -0700230
Svet Ganova5b49902020-06-18 00:47:22 -0700231 @Nullable
Svet Ganov48468032020-06-23 23:38:07 -0700232 @GuardedBy("mLock")
Svet Ganova5b49902020-06-18 00:47:22 -0700233 private ArrayList<OnReparentListener> mReparentListeners;
234
235 /**
236 * Listener to observe surface reparenting.
237 *
238 * @hide
239 */
240 public interface OnReparentListener {
241
242 /**
243 * Callback for reparenting surfaces.
244 *
245 * Important: You should only interact with the provided surface control
246 * only if you have a contract with its owner to avoid them closing it
247 * under you or vise versa.
248 *
249 * @param transaction The transaction that would commit reparenting.
250 * @param parent The future parent surface.
251 */
252 void onReparent(@NonNull Transaction transaction, @Nullable SurfaceControl parent);
253 }
254
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800255 private final CloseGuard mCloseGuard = CloseGuard.get();
chaviwbeb7a0c2018-12-05 13:49:54 -0800256 private String mName;
Svet Ganova5b49902020-06-18 00:47:22 -0700257
258 /**
Robert Carr48ec4e02019-07-16 14:28:47 -0700259 * @hide
260 */
261 public long mNativeObject;
Jorim Jaggiee540702020-04-02 21:40:52 +0200262 private long mNativeHandle;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800263
Svet Ganova5b49902020-06-18 00:47:22 -0700264 // TODO: Move width/height to native and fix locking through out.
265 private final Object mLock = new Object();
266 @GuardedBy("mLock")
Jorim Jaggia5e10572017-11-15 14:36:26 +0100267 private int mWidth;
Svet Ganova5b49902020-06-18 00:47:22 -0700268 @GuardedBy("mLock")
Jorim Jaggia5e10572017-11-15 14:36:26 +0100269 private int mHeight;
270
Svet Ganova5b49902020-06-18 00:47:22 -0700271 private WeakReference<View> mLocalOwnerView;
272
Robert Carre13b58e2017-08-31 14:50:44 -0700273 static Transaction sGlobalTransaction;
274 static long sTransactionNestCount = 0;
275
Svet Ganova5b49902020-06-18 00:47:22 -0700276 /**
277 * Adds a reparenting listener.
278 *
279 * @param listener The listener.
280 * @return Whether listener was added.
281 *
282 * @hide
283 */
284 public boolean addOnReparentListener(@NonNull OnReparentListener listener) {
285 synchronized (mLock) {
286 if (mReparentListeners == null) {
287 mReparentListeners = new ArrayList<>(1);
288 }
289 return mReparentListeners.add(listener);
290 }
291 }
292
293 /**
294 * Removes a reparenting listener.
295 *
296 * @param listener The listener.
297 * @return Whether listener was removed.
298 *
299 * @hide
300 */
301 public boolean removeOnReparentListener(@NonNull OnReparentListener listener) {
302 synchronized (mLock) {
303 final boolean removed = mReparentListeners.remove(listener);
304 if (mReparentListeners.isEmpty()) {
305 mReparentListeners = null;
306 }
307 return removed;
308 }
309 }
310
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800311 /* flags used in constructor (keep in sync with ISurfaceComposerClient.h) */
312
313 /**
314 * Surface creation flag: Surface is created hidden
Robert Carra7827f72019-01-11 12:53:37 -0800315 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800316 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100317 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800318 public static final int HIDDEN = 0x00000004;
319
320 /**
321 * Surface creation flag: The surface contains secure content, special
322 * measures will be taken to disallow the surface's content to be copied
323 * from another process. In particular, screenshots and VNC servers will
324 * be disabled, but other measures can take place, for instance the
Jesse Hall6a6bc212013-08-08 12:15:03 -0700325 * surface might not be hardware accelerated.
Robert Carra7827f72019-01-11 12:53:37 -0800326 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800327 */
328 public static final int SECURE = 0x00000080;
329
330 /**
331 * Surface creation flag: Creates a surface where color components are interpreted
332 * as "non pre-multiplied" by their alpha channel. Of course this flag is
333 * meaningless for surfaces without an alpha channel. By default
334 * surfaces are pre-multiplied, which means that each color component is
335 * already multiplied by its alpha value. In this case the blending
336 * equation used is:
Andy McFadden314405b2014-01-29 17:18:05 -0800337 * <p>
338 * <code>DEST = SRC + DEST * (1-SRC_ALPHA)</code>
339 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800340 * By contrast, non pre-multiplied surfaces use the following equation:
Andy McFadden314405b2014-01-29 17:18:05 -0800341 * <p>
342 * <code>DEST = SRC * SRC_ALPHA * DEST * (1-SRC_ALPHA)</code>
343 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800344 * pre-multiplied surfaces must always be used if transparent pixels are
345 * composited on top of each-other into the surface. A pre-multiplied
346 * surface can never lower the value of the alpha component of a given
347 * pixel.
Andy McFadden314405b2014-01-29 17:18:05 -0800348 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800349 * In some rare situations, a non pre-multiplied surface is preferable.
Robert Carra7827f72019-01-11 12:53:37 -0800350 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800351 */
352 public static final int NON_PREMULTIPLIED = 0x00000100;
353
354 /**
355 * Surface creation flag: Indicates that the surface must be considered opaque,
Robert Carre625fcf2017-09-01 12:36:28 -0700356 * even if its pixel format contains an alpha channel. This can be useful if an
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800357 * application needs full RGBA 8888 support for instance but will
358 * still draw every pixel opaque.
Andy McFadden314405b2014-01-29 17:18:05 -0800359 * <p>
360 * This flag is ignored if setAlpha() is used to make the surface non-opaque.
361 * Combined effects are (assuming a buffer format with an alpha channel):
362 * <ul>
363 * <li>OPAQUE + alpha(1.0) == opaque composition
364 * <li>OPAQUE + alpha(0.x) == blended composition
365 * <li>!OPAQUE + alpha(1.0) == blended composition
366 * <li>!OPAQUE + alpha(0.x) == blended composition
367 * </ul>
368 * If the underlying buffer lacks an alpha channel, the OPAQUE flag is effectively
369 * set automatically.
Robert Carra7827f72019-01-11 12:53:37 -0800370 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800371 */
372 public static final int OPAQUE = 0x00000400;
373
374 /**
375 * Surface creation flag: Application requires a hardware-protected path to an
376 * external display sink. If a hardware-protected path is not available,
377 * then this surface will not be displayed on the external sink.
378 *
Robert Carra7827f72019-01-11 12:53:37 -0800379 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800380 */
381 public static final int PROTECTED_APP = 0x00000800;
382
383 // 0x1000 is reserved for an independent DRM protected flag in framework
384
385 /**
Riley Andrews68eccda2014-07-07 11:47:35 -0700386 * Surface creation flag: Window represents a cursor glyph.
Robert Carra7827f72019-01-11 12:53:37 -0800387 * @hide
Riley Andrews68eccda2014-07-07 11:47:35 -0700388 */
389 public static final int CURSOR_WINDOW = 0x00002000;
390
391 /**
Vishnu Nair06cff302020-05-29 14:39:20 -0700392 * Surface creation flag: Indicates the effect layer will not have a color fill on
393 * creation.
394 *
395 * @hide
396 */
397 public static final int NO_COLOR_FILL = 0x00004000;
398
399 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800400 * Surface creation flag: Creates a normal surface.
401 * This is the default.
402 *
Robert Carra7827f72019-01-11 12:53:37 -0800403 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800404 */
405 public static final int FX_SURFACE_NORMAL = 0x00000000;
406
407 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800408 * Surface creation flag: Creates a effect surface which
409 * represents a solid color and or shadows.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800410 *
Robert Carra7827f72019-01-11 12:53:37 -0800411 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800412 */
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800413 public static final int FX_SURFACE_EFFECT = 0x00020000;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800414
415 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700416 * Surface creation flag: Creates a container surface.
417 * This surface will have no buffers and will only be used
418 * as a container for other surfaces, or for its InputInfo.
Robert Carra7827f72019-01-11 12:53:37 -0800419 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700420 */
421 public static final int FX_SURFACE_CONTAINER = 0x00080000;
422
423 /**
Robert Carr48ec4e02019-07-16 14:28:47 -0700424 * @hide
425 */
426 public static final int FX_SURFACE_BLAST = 0x00040000;
427
428 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800429 * Mask used for FX values above.
430 *
Robert Carra7827f72019-01-11 12:53:37 -0800431 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800432 */
433 public static final int FX_SURFACE_MASK = 0x000F0000;
434
435 /* flags used with setFlags() (keep in sync with ISurfaceComposer.h) */
436
437 /**
438 * Surface flag: Hide the surface.
439 * Equivalent to calling hide().
Andy McFadden314405b2014-01-29 17:18:05 -0800440 * Updates the value set during Surface creation (see {@link #HIDDEN}).
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800441 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800442 private static final int SURFACE_HIDDEN = 0x01;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800443
Andy McFadden314405b2014-01-29 17:18:05 -0800444 /**
445 * Surface flag: composite without blending when possible.
446 * Updates the value set during Surface creation (see {@link #OPAQUE}).
447 */
Andy McFadden40b9ef12014-01-30 13:44:47 -0800448 private static final int SURFACE_OPAQUE = 0x02;
Andy McFadden314405b2014-01-29 17:18:05 -0800449
Robert Carr76907ee2019-01-11 13:38:19 -0800450 // Display power modes.
Prashant Malanic55929a2014-05-25 01:59:21 -0700451 /**
452 * Display power mode off: used while blanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700453 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800454 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700455 */
456 public static final int POWER_MODE_OFF = 0;
457
458 /**
459 * Display power mode doze: used while putting the screen into low power mode.
Jeff Brown5dc21912014-07-17 18:50:18 -0700460 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800461 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700462 */
463 public static final int POWER_MODE_DOZE = 1;
464
465 /**
466 * Display power mode normal: used while unblanking the screen.
Jeff Brown5dc21912014-07-17 18:50:18 -0700467 * Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800468 * @hide
Prashant Malanic55929a2014-05-25 01:59:21 -0700469 */
470 public static final int POWER_MODE_NORMAL = 2;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800471
Jeff Brown5dc21912014-07-17 18:50:18 -0700472 /**
473 * Display power mode doze: used while putting the screen into a suspended
474 * low power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800475 * @hide
Jeff Brown5dc21912014-07-17 18:50:18 -0700476 */
477 public static final int POWER_MODE_DOZE_SUSPEND = 3;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800478
479 /**
Chris Phoenix10a4a642017-09-25 13:21:00 -0700480 * Display power mode on: used while putting the screen into a suspended
481 * full power mode. Use only with {@link SurfaceControl#setDisplayPowerMode}.
Robert Carra7827f72019-01-11 12:53:37 -0800482 * @hide
Chris Phoenix10a4a642017-09-25 13:21:00 -0700483 */
484 public static final int POWER_MODE_ON_SUSPEND = 4;
485
486 /**
Robert Carr132c9f52017-07-31 17:02:30 -0700487 * A value for windowType used to indicate that the window should be omitted from screenshots
488 * and display mirroring. A temporary workaround until we express such things with
489 * the hierarchy.
490 * TODO: b/64227542
491 * @hide
492 */
493 public static final int WINDOW_TYPE_DONT_SCREENSHOT = 441731;
494
Peiyong Lin5f4a5682019-01-17 11:37:07 -0800495 /**
496 * internal representation of how to interpret pixel value, used only to convert to ColorSpace.
497 */
498 private static final int INTERNAL_DATASPACE_SRGB = 142671872;
499 private static final int INTERNAL_DATASPACE_DISPLAY_P3 = 143261696;
500 private static final int INTERNAL_DATASPACE_SCRGB = 411107328;
501
Robert Carreb344c72019-01-07 18:35:30 -0800502 private void assignNativeObject(long nativeObject) {
503 if (mNativeObject != 0) {
504 release();
505 }
Tiger Huang0fd67482020-04-15 22:16:13 +0800506 if (nativeObject != 0) {
Jorim Jaggibfa95a72020-06-18 22:51:49 +0200507 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "closeGuard");
Robert Carr01b0dd52020-02-27 13:25:17 -0800508 mCloseGuard.open("release");
Jorim Jaggibfa95a72020-06-18 22:51:49 +0200509 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Robert Carr01b0dd52020-02-27 13:25:17 -0800510 }
Robert Carreb344c72019-01-07 18:35:30 -0800511 mNativeObject = nativeObject;
Jorim Jaggiee540702020-04-02 21:40:52 +0200512 mNativeHandle = mNativeObject != 0 ? nativeGetHandle(nativeObject) : 0;
Robert Carreb344c72019-01-07 18:35:30 -0800513 }
514
Robert Carra7827f72019-01-11 12:53:37 -0800515 /**
516 * @hide
517 */
Jorim Jaggiee540702020-04-02 21:40:52 +0200518 public void copyFrom(@NonNull SurfaceControl other) {
chaviwbeb7a0c2018-12-05 13:49:54 -0800519 mName = other.mName;
520 mWidth = other.mWidth;
521 mHeight = other.mHeight;
Svet Ganova5b49902020-06-18 00:47:22 -0700522 mLocalOwnerView = other.mLocalOwnerView;
Robert Carreb344c72019-01-07 18:35:30 -0800523 assignNativeObject(nativeCopyFromSurfaceControl(other.mNativeObject));
chaviwbeb7a0c2018-12-05 13:49:54 -0800524 }
525
Robert Carr132c9f52017-07-31 17:02:30 -0700526 /**
Evan Rosky485df202018-12-06 14:11:12 -0800527 * owner UID.
528 * @hide
529 */
530 public static final int METADATA_OWNER_UID = 1;
531
532 /**
533 * Window type as per {@link WindowManager.LayoutParams}.
534 * @hide
535 */
536 public static final int METADATA_WINDOW_TYPE = 2;
537
538 /**
Evan Rosky9020c072018-12-06 14:11:12 -0800539 * Task id to allow association between surfaces and task.
540 * @hide
541 */
542 public static final int METADATA_TASK_ID = 3;
543
544 /**
Daichi Hirono638ae6d2019-12-18 13:59:58 +0900545 * The style of mouse cursor and hotspot.
546 * @hide
547 */
548 public static final int METADATA_MOUSE_CURSOR = 4;
549
550 /**
Daichi Hirono1d56ce32019-11-01 14:15:10 +0900551 * Accessibility ID to allow association between surfaces and accessibility tree.
552 * @hide
553 */
Daichi Hirono638ae6d2019-12-18 13:59:58 +0900554 public static final int METADATA_ACCESSIBILITY_ID = 5;
Daichi Hirono1d56ce32019-11-01 14:15:10 +0900555
556 /**
Peiyong Line3e5efd2019-03-21 20:59:47 +0000557 * A wrapper around GraphicBuffer that contains extra information about how to
558 * interpret the screenshot GraphicBuffer.
559 * @hide
560 */
561 public static class ScreenshotGraphicBuffer {
562 private final GraphicBuffer mGraphicBuffer;
563 private final ColorSpace mColorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700564 private final boolean mContainsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000565
Robert Carr66b5664f2019-04-02 14:18:56 -0700566 public ScreenshotGraphicBuffer(GraphicBuffer graphicBuffer, ColorSpace colorSpace,
567 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000568 mGraphicBuffer = graphicBuffer;
569 mColorSpace = colorSpace;
Robert Carr66b5664f2019-04-02 14:18:56 -0700570 mContainsSecureLayers = containsSecureLayers;
Peiyong Line3e5efd2019-03-21 20:59:47 +0000571 }
572
573 /**
574 * Create ScreenshotGraphicBuffer from existing native GraphicBuffer object.
575 * @param width The width in pixels of the buffer
576 * @param height The height in pixels of the buffer
577 * @param format The format of each pixel as specified in {@link PixelFormat}
578 * @param usage Hint indicating how the buffer will be used
579 * @param unwrappedNativeObject The native object of GraphicBuffer
580 * @param namedColorSpace Integer value of a named color space {@link ColorSpace.Named}
Robert Carr66b5664f2019-04-02 14:18:56 -0700581 * @param containsSecureLayer Indicates whether this graphic buffer contains captured contents
582 * of secure layers, in which case the screenshot should not be persisted.
Peiyong Line3e5efd2019-03-21 20:59:47 +0000583 */
584 private static ScreenshotGraphicBuffer createFromNative(int width, int height, int format,
Robert Carr66b5664f2019-04-02 14:18:56 -0700585 int usage, long unwrappedNativeObject, int namedColorSpace,
586 boolean containsSecureLayers) {
Peiyong Line3e5efd2019-03-21 20:59:47 +0000587 GraphicBuffer graphicBuffer = GraphicBuffer.createFromExisting(width, height, format,
588 usage, unwrappedNativeObject);
589 ColorSpace colorSpace = ColorSpace.get(ColorSpace.Named.values()[namedColorSpace]);
Robert Carr66b5664f2019-04-02 14:18:56 -0700590 return new ScreenshotGraphicBuffer(graphicBuffer, colorSpace, containsSecureLayers);
Peiyong Line3e5efd2019-03-21 20:59:47 +0000591 }
592
593 public ColorSpace getColorSpace() {
594 return mColorSpace;
595 }
596
597 public GraphicBuffer getGraphicBuffer() {
598 return mGraphicBuffer;
599 }
Robert Carr66b5664f2019-04-02 14:18:56 -0700600
601 public boolean containsSecureLayers() {
602 return mContainsSecureLayers;
603 }
Peiyong Line3e5efd2019-03-21 20:59:47 +0000604 }
605
606 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700607 * Builder class for {@link SurfaceControl} objects.
Robert Carr29fe58a2019-03-11 15:25:16 -0700608 *
609 * By default the surface will be hidden, and have "unset" bounds, meaning it can
610 * be as large as the bounds of its parent if a buffer or child so requires.
611 *
612 * It is necessary to set at least a name via {@link Builder#setName}
Robert Carre625fcf2017-09-01 12:36:28 -0700613 */
614 public static class Builder {
615 private SurfaceSession mSession;
616 private int mFlags = HIDDEN;
617 private int mWidth;
618 private int mHeight;
619 private int mFormat = PixelFormat.OPAQUE;
620 private String mName;
Svet Ganova5b49902020-06-18 00:47:22 -0700621 private WeakReference<View> mLocalOwnerView;
Robert Carre625fcf2017-09-01 12:36:28 -0700622 private SurfaceControl mParent;
Evan Rosky485df202018-12-06 14:11:12 -0800623 private SparseIntArray mMetadata;
Robert Carre625fcf2017-09-01 12:36:28 -0700624
625 /**
626 * Begin building a SurfaceControl with a given {@link SurfaceSession}.
627 *
628 * @param session The {@link SurfaceSession} with which to eventually construct the surface.
Robert Carra7827f72019-01-11 12:53:37 -0800629 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700630 */
631 public Builder(SurfaceSession session) {
632 mSession = session;
633 }
634
635 /**
Robert Carr76907ee2019-01-11 13:38:19 -0800636 * Begin building a SurfaceControl.
637 */
638 public Builder() {
639 }
640
641 /**
642 * Construct a new {@link SurfaceControl} with the set parameters. The builder
643 * remains valid.
Robert Carre625fcf2017-09-01 12:36:28 -0700644 */
Robert Carrda1d2422019-03-07 15:54:37 -0800645 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700646 public SurfaceControl build() {
Vishnu Naire86bd982018-11-28 13:23:17 -0800647 if (mWidth < 0 || mHeight < 0) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700648 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800649 "width and height must be positive or unset");
650 }
Vishnu Nair06cff302020-05-29 14:39:20 -0700651 if ((mWidth > 0 || mHeight > 0) && (isEffectLayer() || isContainerLayer())) {
Robert Carr29fe58a2019-03-11 15:25:16 -0700652 throw new IllegalStateException(
Vishnu Naire86bd982018-11-28 13:23:17 -0800653 "Only buffer layers can set a valid buffer size.");
Robert Carre625fcf2017-09-01 12:36:28 -0700654 }
Evan Rosky485df202018-12-06 14:11:12 -0800655 return new SurfaceControl(
Svet Ganova5b49902020-06-18 00:47:22 -0700656 mSession, mName, mWidth, mHeight, mFormat, mFlags, mParent, mMetadata,
657 mLocalOwnerView);
Robert Carre625fcf2017-09-01 12:36:28 -0700658 }
659
660 /**
661 * Set a debugging-name for the SurfaceControl.
662 *
663 * @param name A name to identify the Surface in debugging.
664 */
Robert Carrda1d2422019-03-07 15:54:37 -0800665 @NonNull
666 public Builder setName(@NonNull String name) {
Robert Carre625fcf2017-09-01 12:36:28 -0700667 mName = name;
668 return this;
669 }
670
671 /**
Svet Ganova5b49902020-06-18 00:47:22 -0700672 * Set the local owner view for the surface. This view is only
673 * valid in the same process and is not transferred in an IPC.
674 *
675 * Note: This is used for cases where we want to know the view
676 * that manages the surface control while intercepting reparenting.
677 * A specific example is InlineContentView which exposes is surface
678 * control for reparenting as a way to implement clipping of several
679 * InlineContentView instances within a certain area.
680 *
681 * @param view The owner view.
682 * @return This builder.
683 *
684 * @hide
685 */
686 @NonNull
687 public Builder setLocalOwnerView(@NonNull View view) {
688 mLocalOwnerView = new WeakReference<>(view);
689 return this;
690 }
691
692 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700693 * Set the initial size of the controlled surface's buffers in pixels.
694 *
695 * @param width The buffer width in pixels.
696 * @param height The buffer height in pixels.
697 */
Robert Carrda1d2422019-03-07 15:54:37 -0800698 @NonNull
Robert Carr76907ee2019-01-11 13:38:19 -0800699 public Builder setBufferSize(@IntRange(from = 0) int width,
700 @IntRange(from = 0) int height) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800701 if (width < 0 || height < 0) {
Robert Carre625fcf2017-09-01 12:36:28 -0700702 throw new IllegalArgumentException(
703 "width and height must be positive");
704 }
705 mWidth = width;
706 mHeight = height;
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000707 // set this as a buffer layer since we are specifying a buffer size.
708 return setFlags(FX_SURFACE_NORMAL, FX_SURFACE_MASK);
709 }
710
711 /**
712 * Set the initial size of the controlled surface's buffers in pixels.
713 */
714 private void unsetBufferSize() {
715 mWidth = 0;
716 mHeight = 0;
Robert Carre625fcf2017-09-01 12:36:28 -0700717 }
718
719 /**
720 * Set the pixel format of the controlled surface's buffers, using constants from
721 * {@link android.graphics.PixelFormat}.
722 */
Robert Carr76907ee2019-01-11 13:38:19 -0800723 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700724 public Builder setFormat(@PixelFormat.Format int format) {
725 mFormat = format;
726 return this;
727 }
728
729 /**
730 * Specify if the app requires a hardware-protected path to
731 * an external display sync. If protected content is enabled, but
732 * such a path is not available, then the controlled Surface will
733 * not be displayed.
734 *
735 * @param protectedContent Whether to require a protected sink.
Robert Carra7827f72019-01-11 12:53:37 -0800736 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700737 */
Robert Carr76907ee2019-01-11 13:38:19 -0800738 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700739 public Builder setProtected(boolean protectedContent) {
740 if (protectedContent) {
741 mFlags |= PROTECTED_APP;
742 } else {
743 mFlags &= ~PROTECTED_APP;
744 }
745 return this;
746 }
747
748 /**
749 * Specify whether the Surface contains secure content. If true, the system
750 * will prevent the surfaces content from being copied by another process. In
751 * particular screenshots and VNC servers will be disabled. This is however
752 * not a complete prevention of readback as {@link #setProtected}.
Robert Carra7827f72019-01-11 12:53:37 -0800753 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700754 */
Robert Carr76907ee2019-01-11 13:38:19 -0800755 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700756 public Builder setSecure(boolean secure) {
757 if (secure) {
758 mFlags |= SECURE;
759 } else {
760 mFlags &= ~SECURE;
761 }
762 return this;
763 }
764
765 /**
766 * Indicates whether the surface must be considered opaque,
767 * even if its pixel format is set to translucent. This can be useful if an
768 * application needs full RGBA 8888 support for instance but will
769 * still draw every pixel opaque.
770 * <p>
771 * This flag only determines whether opacity will be sampled from the alpha channel.
772 * Plane-alpha from calls to setAlpha() can still result in blended composition
773 * regardless of the opaque setting.
774 *
775 * Combined effects are (assuming a buffer format with an alpha channel):
776 * <ul>
777 * <li>OPAQUE + alpha(1.0) == opaque composition
778 * <li>OPAQUE + alpha(0.x) == blended composition
779 * <li>OPAQUE + alpha(0.0) == no composition
780 * <li>!OPAQUE + alpha(1.0) == blended composition
781 * <li>!OPAQUE + alpha(0.x) == blended composition
782 * <li>!OPAQUE + alpha(0.0) == no composition
783 * </ul>
784 * If the underlying buffer lacks an alpha channel, it is as if setOpaque(true)
785 * were set automatically.
786 * @param opaque Whether the Surface is OPAQUE.
787 */
Robert Carr76907ee2019-01-11 13:38:19 -0800788 @NonNull
Robert Carre625fcf2017-09-01 12:36:28 -0700789 public Builder setOpaque(boolean opaque) {
790 if (opaque) {
791 mFlags |= OPAQUE;
792 } else {
793 mFlags &= ~OPAQUE;
794 }
795 return this;
796 }
797
798 /**
Tiger Huang969c6082019-12-24 20:08:57 +0800799 * Set the initial visibility for the SurfaceControl.
800 *
801 * @param hidden Whether the Surface is initially HIDDEN.
802 * @hide
803 */
804 @NonNull
805 public Builder setHidden(boolean hidden) {
806 if (hidden) {
807 mFlags |= HIDDEN;
808 } else {
809 mFlags &= ~HIDDEN;
810 }
811 return this;
812 }
813
814 /**
Robert Carre625fcf2017-09-01 12:36:28 -0700815 * Set a parent surface for our new SurfaceControl.
816 *
817 * Child surfaces are constrained to the onscreen region of their parent.
818 * Furthermore they stack relatively in Z order, and inherit the transformation
819 * of the parent.
820 *
821 * @param parent The parent control.
822 */
Robert Carr76907ee2019-01-11 13:38:19 -0800823 @NonNull
824 public Builder setParent(@Nullable SurfaceControl parent) {
Robert Carre625fcf2017-09-01 12:36:28 -0700825 mParent = parent;
826 return this;
827 }
828
829 /**
Evan Rosky485df202018-12-06 14:11:12 -0800830 * Sets a metadata int.
Robert Carre625fcf2017-09-01 12:36:28 -0700831 *
Evan Rosky485df202018-12-06 14:11:12 -0800832 * @param key metadata key
833 * @param data associated data
Robert Carra7827f72019-01-11 12:53:37 -0800834 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700835 */
Evan Rosky485df202018-12-06 14:11:12 -0800836 public Builder setMetadata(int key, int data) {
837 if (mMetadata == null) {
838 mMetadata = new SparseIntArray();
Robert Carre625fcf2017-09-01 12:36:28 -0700839 }
Evan Rosky485df202018-12-06 14:11:12 -0800840 mMetadata.put(key, data);
Robert Carre625fcf2017-09-01 12:36:28 -0700841 return this;
842 }
843
844 /**
Vishnu Nair06cff302020-05-29 14:39:20 -0700845 * Indicate whether an 'EffectLayer' is to be constructed.
Robert Carre625fcf2017-09-01 12:36:28 -0700846 *
Vishnu Nair06cff302020-05-29 14:39:20 -0700847 * An effect layer behaves like a container layer by default but it can support
848 * color fill, shadows and/or blur. These layers will not have an associated buffer.
849 * When created, this layer has no effects set and will be transparent but the caller
850 * can render an effect by calling:
851 * - {@link Transaction#setColor(SurfaceControl, float[])}
852 * - {@link Transaction#setBackgroundBlurRadius(SurfaceControl, int)}
853 * - {@link Transaction#setShadowRadius(SurfaceControl, float)}
854 *
855 * @hide
856 */
857 public Builder setEffectLayer() {
858 mFlags |= NO_COLOR_FILL;
859 unsetBufferSize();
860 return setFlags(FX_SURFACE_EFFECT, FX_SURFACE_MASK);
861 }
862
863 /**
864 * A convenience function to create an effect layer with a default color fill
865 * applied to it. Currently that color is black.
Robert Carre625fcf2017-09-01 12:36:28 -0700866 *
Robert Carra7827f72019-01-11 12:53:37 -0800867 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700868 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000869 public Builder setColorLayer() {
870 unsetBufferSize();
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800871 return setFlags(FX_SURFACE_EFFECT, FX_SURFACE_MASK);
Robert Carre625fcf2017-09-01 12:36:28 -0700872 }
873
Vishnu Nair06cff302020-05-29 14:39:20 -0700874 private boolean isEffectLayer() {
Vishnu Nairfd6fb672020-02-14 12:56:37 -0800875 return (mFlags & FX_SURFACE_EFFECT) == FX_SURFACE_EFFECT;
Vishnu Naire86bd982018-11-28 13:23:17 -0800876 }
877
Robert Carre625fcf2017-09-01 12:36:28 -0700878 /**
Robert Carr48ec4e02019-07-16 14:28:47 -0700879 * @hide
880 */
881 public Builder setBLASTLayer() {
882 unsetBufferSize();
883 return setFlags(FX_SURFACE_BLAST, FX_SURFACE_MASK);
884 }
885
886 /**
Robert Carrb6cd6432018-08-13 13:01:47 -0700887 * Indicates whether a 'ContainerLayer' is to be constructed.
888 *
889 * Container layers will not be rendered in any fashion and instead are used
890 * as a parent of renderable layers.
891 *
Robert Carra7827f72019-01-11 12:53:37 -0800892 * @hide
Robert Carrb6cd6432018-08-13 13:01:47 -0700893 */
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000894 public Builder setContainerLayer() {
895 unsetBufferSize();
896 return setFlags(FX_SURFACE_CONTAINER, FX_SURFACE_MASK);
Robert Carrb6cd6432018-08-13 13:01:47 -0700897 }
898
Vishnu Nair06cff302020-05-29 14:39:20 -0700899 private boolean isContainerLayer() {
Vishnu Naire86bd982018-11-28 13:23:17 -0800900 return (mFlags & FX_SURFACE_CONTAINER) == FX_SURFACE_CONTAINER;
901 }
902
Robert Carrb6cd6432018-08-13 13:01:47 -0700903 /**
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000904 * Set 'Surface creation flags' such as {@link #HIDDEN}, {@link #SECURE}.
Robert Carre625fcf2017-09-01 12:36:28 -0700905 *
906 * TODO: Finish conversion to individual builder methods?
907 * @param flags The combined flags
Robert Carra7827f72019-01-11 12:53:37 -0800908 * @hide
Robert Carre625fcf2017-09-01 12:36:28 -0700909 */
910 public Builder setFlags(int flags) {
911 mFlags = flags;
912 return this;
913 }
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000914
915 private Builder setFlags(int flags, int mask) {
916 mFlags = (mFlags & ~mask) | flags;
917 return this;
918 }
Robert Carre625fcf2017-09-01 12:36:28 -0700919 }
920
921 /**
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800922 * Create a surface with a name.
Andy McFadden314405b2014-01-29 17:18:05 -0800923 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800924 * The surface creation flags specify what kind of surface to create and
925 * certain options such as whether the surface can be assumed to be opaque
926 * and whether it should be initially hidden. Surfaces should always be
927 * created with the {@link #HIDDEN} flag set to ensure that they are not
928 * made visible prematurely before all of the surface's properties have been
929 * configured.
Andy McFadden314405b2014-01-29 17:18:05 -0800930 * <p>
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800931 * Good practice is to first create the surface with the {@link #HIDDEN} flag
932 * specified, open a transaction, set the surface layer, layer stack, alpha,
chaviw619da692019-06-10 15:39:40 -0700933 * and position, call {@link Transaction#show(SurfaceControl)} if appropriate, and close the
934 * transaction.
Vishnu Naird454442d2018-11-13 13:51:01 -0800935 * <p>
936 * Bounds of the surface is determined by its crop and its buffer size. If the
937 * surface has no buffer or crop, the surface is boundless and only constrained
938 * by the size of its parent bounds.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800939 *
chaviw619da692019-06-10 15:39:40 -0700940 * @param session The surface session, must not be null.
941 * @param name The surface name, must not be null.
942 * @param w The surface initial width.
943 * @param h The surface initial height.
Tiger Huang969c6082019-12-24 20:08:57 +0800944 * @param flags The surface creation flags.
Evan Rosky485df202018-12-06 14:11:12 -0800945 * @param metadata Initial metadata.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700946 * @throws throws OutOfResourcesException If the SurfaceControl cannot be created.
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800947 */
Robert Carre625fcf2017-09-01 12:36:28 -0700948 private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags,
Svet Ganova5b49902020-06-18 00:47:22 -0700949 SurfaceControl parent, SparseIntArray metadata, WeakReference<View> localOwnerView)
Robert Carrb0f39362018-03-14 13:52:25 -0700950 throws OutOfResourcesException, IllegalArgumentException {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800951 if (name == null) {
952 throw new IllegalArgumentException("name must not be null");
953 }
954
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800955 mName = name;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100956 mWidth = w;
957 mHeight = h;
Svet Ganova5b49902020-06-18 00:47:22 -0700958 mLocalOwnerView = localOwnerView;
Evan Rosky485df202018-12-06 14:11:12 -0800959 Parcel metaParcel = Parcel.obtain();
960 try {
961 if (metadata != null && metadata.size() > 0) {
962 metaParcel.writeInt(metadata.size());
963 for (int i = 0; i < metadata.size(); ++i) {
964 metaParcel.writeInt(metadata.keyAt(i));
965 metaParcel.writeByteArray(
Evan Roskyc12c9a32019-02-06 13:38:03 -0800966 ByteBuffer.allocate(4).order(ByteOrder.nativeOrder())
967 .putInt(metadata.valueAt(i)).array());
Evan Rosky485df202018-12-06 14:11:12 -0800968 }
Evan Roskyc12c9a32019-02-06 13:38:03 -0800969 metaParcel.setDataPosition(0);
Evan Rosky485df202018-12-06 14:11:12 -0800970 }
971 mNativeObject = nativeCreate(session, name, w, h, format, flags,
972 parent != null ? parent.mNativeObject : 0, metaParcel);
973 } finally {
974 metaParcel.recycle();
975 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800976 if (mNativeObject == 0) {
977 throw new OutOfResourcesException(
978 "Couldn't allocate SurfaceControl native object");
979 }
Jorim Jaggiee540702020-04-02 21:40:52 +0200980 mNativeHandle = nativeGetHandle(mNativeObject);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800981 mCloseGuard.open("release");
982 }
Jesse Hall6a6bc212013-08-08 12:15:03 -0700983
Jorim Jaggiee540702020-04-02 21:40:52 +0200984 /**
985 * Copy constructor. Creates a new native object pointing to the same surface as {@code other}.
986 *
987 * @param other The object to copy the surface from.
Robert Carra7827f72019-01-11 12:53:37 -0800988 * @hide
989 */
Jorim Jaggiee540702020-04-02 21:40:52 +0200990 @TestApi
991 public SurfaceControl(@NonNull SurfaceControl other) {
992 copyFrom(other);
Robert Carr3b716242016-08-16 16:02:21 -0700993 }
994
Jorim Jaggi06975df2017-12-01 14:52:13 +0100995 private SurfaceControl(Parcel in) {
chaviwbeb7a0c2018-12-05 13:49:54 -0800996 readFromParcel(in);
chaviwbeb7a0c2018-12-05 13:49:54 -0800997 }
998
Robert Carra7827f72019-01-11 12:53:37 -0800999 /**
1000 * @hide
1001 */
chaviwbeb7a0c2018-12-05 13:49:54 -08001002 public SurfaceControl() {
chaviwbeb7a0c2018-12-05 13:49:54 -08001003 }
1004
1005 public void readFromParcel(Parcel in) {
1006 if (in == null) {
1007 throw new IllegalArgumentException("source must not be null");
1008 }
1009
Jeff Sharkey1639e6b2020-04-22 17:36:53 -06001010 mName = in.readString8();
Jorim Jaggia5e10572017-11-15 14:36:26 +01001011 mWidth = in.readInt();
1012 mHeight = in.readInt();
Robert Carr5fea55b2018-12-10 13:05:52 -08001013
Robert Carreb344c72019-01-07 18:35:30 -08001014 long object = 0;
Robert Carr5fea55b2018-12-10 13:05:52 -08001015 if (in.readInt() != 0) {
Robert Carreb344c72019-01-07 18:35:30 -08001016 object = nativeReadFromParcel(in);
Jorim Jaggi06975df2017-12-01 14:52:13 +01001017 }
Robert Carreb344c72019-01-07 18:35:30 -08001018 assignNativeObject(object);
Jorim Jaggi06975df2017-12-01 14:52:13 +01001019 }
1020
1021 @Override
1022 public int describeContents() {
1023 return 0;
1024 }
1025
1026 @Override
1027 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey1639e6b2020-04-22 17:36:53 -06001028 dest.writeString8(mName);
Jorim Jaggia5e10572017-11-15 14:36:26 +01001029 dest.writeInt(mWidth);
1030 dest.writeInt(mHeight);
Robert Carr5fea55b2018-12-10 13:05:52 -08001031 if (mNativeObject == 0) {
1032 dest.writeInt(0);
1033 } else {
1034 dest.writeInt(1);
1035 }
Jorim Jaggi06975df2017-12-01 14:52:13 +01001036 nativeWriteToParcel(mNativeObject, dest);
Robert Carr5fea55b2018-12-10 13:05:52 -08001037
1038 if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) {
1039 release();
1040 }
Jorim Jaggi06975df2017-12-01 14:52:13 +01001041 }
1042
Vishnu Nair04ab4392018-01-10 11:00:06 -08001043 /**
Jorim Jaggiee540702020-04-02 21:40:52 +02001044 * Checks whether two {@link SurfaceControl} objects represent the same surface.
1045 *
1046 * @param other The other object to check
1047 * @return {@code true} if these two {@link SurfaceControl} objects represent the same surface.
1048 * @hide
1049 */
1050 @TestApi
1051 public boolean isSameSurface(@NonNull SurfaceControl other) {
1052 return other.mNativeHandle == mNativeHandle;
1053 }
1054
1055 /**
Vishnu Nair04ab4392018-01-10 11:00:06 -08001056 * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link
1057 * android.view.SurfaceControlProto}.
1058 *
1059 * @param proto Stream to write the SurfaceControl object to.
1060 * @param fieldId Field Id of the SurfaceControl as defined in the parent message.
1061 * @hide
1062 */
Jeffrey Huangcb782852019-12-05 11:28:11 -08001063 public void dumpDebug(ProtoOutputStream proto, long fieldId) {
Vishnu Nair04ab4392018-01-10 11:00:06 -08001064 final long token = proto.start(fieldId);
1065 proto.write(HASH_CODE, System.identityHashCode(this));
1066 proto.write(NAME, mName);
1067 proto.end(token);
1068 }
1069
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001070 public static final @android.annotation.NonNull Creator<SurfaceControl> CREATOR
Jorim Jaggi06975df2017-12-01 14:52:13 +01001071 = new Creator<SurfaceControl>() {
1072 public SurfaceControl createFromParcel(Parcel in) {
1073 return new SurfaceControl(in);
1074 }
1075
1076 public SurfaceControl[] newArray(int size) {
1077 return new SurfaceControl[size];
1078 }
1079 };
1080
Robert Carra7827f72019-01-11 12:53:37 -08001081 /**
1082 * @hide
1083 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001084 @Override
1085 protected void finalize() throws Throwable {
1086 try {
1087 if (mCloseGuard != null) {
1088 mCloseGuard.warnIfOpen();
1089 }
1090 if (mNativeObject != 0) {
1091 nativeRelease(mNativeObject);
1092 }
1093 } finally {
1094 super.finalize();
1095 }
1096 }
1097
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001098 /**
Robert Carr76907ee2019-01-11 13:38:19 -08001099 * Release the local reference to the server-side surface. The surface
1100 * may continue to exist on-screen as long as its parent continues
1101 * to exist. To explicitly remove a surface from the screen use
Robert Carr29fe58a2019-03-11 15:25:16 -07001102 * {@link Transaction#reparent} with a null-parent. After release,
1103 * {@link #isValid} will return false and other methods will throw
1104 * an exception.
Robert Carr76907ee2019-01-11 13:38:19 -08001105 *
1106 * Always call release() when you're done with a SurfaceControl.
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001107 */
1108 public void release() {
1109 if (mNativeObject != 0) {
1110 nativeRelease(mNativeObject);
1111 mNativeObject = 0;
Jorim Jaggiee540702020-04-02 21:40:52 +02001112 mNativeHandle = 0;
Robert Carr01b0dd52020-02-27 13:25:17 -08001113 mCloseGuard.close();
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001114 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001115 }
1116
1117 /**
Chong Zhang47e36a32016-02-29 16:44:33 -08001118 * Disconnect any client still connected to the surface.
Robert Carra7827f72019-01-11 12:53:37 -08001119 * @hide
Chong Zhang47e36a32016-02-29 16:44:33 -08001120 */
1121 public void disconnect() {
1122 if (mNativeObject != 0) {
1123 nativeDisconnect(mNativeObject);
1124 }
1125 }
1126
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001127 private void checkNotReleased() {
Jorim Jaggie1f741b2020-06-16 22:40:12 +02001128 if (mNativeObject == 0) throw new NullPointerException(
1129 "Invalid " + this + ", mNativeObject is null. Have you called release() already?");
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001130 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07001131
Robert Carra7827f72019-01-11 12:53:37 -08001132 /**
Robert Carr76907ee2019-01-11 13:38:19 -08001133 * Check whether this instance points to a valid layer with the system-compositor. For
Robert Carr29fe58a2019-03-11 15:25:16 -07001134 * example this may be false if construction failed, or the layer was released
1135 * ({@link #release}).
Robert Carr76907ee2019-01-11 13:38:19 -08001136 *
1137 * @return Whether this SurfaceControl is valid.
Robert Carra7827f72019-01-11 12:53:37 -08001138 */
Robert Carr5fea55b2018-12-10 13:05:52 -08001139 public boolean isValid() {
1140 return mNativeObject != 0;
1141 }
1142
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001143 /*
1144 * set surface parameters.
1145 * needs to be inside open/closeTransaction block
1146 */
1147
Robert Carra7827f72019-01-11 12:53:37 -08001148 /** start a transaction
1149 * @hide
1150 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001151 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001152 public static void openTransaction() {
Robert Carre13b58e2017-08-31 14:50:44 -07001153 synchronized (SurfaceControl.class) {
1154 if (sGlobalTransaction == null) {
1155 sGlobalTransaction = new Transaction();
1156 }
1157 synchronized(SurfaceControl.class) {
1158 sTransactionNestCount++;
1159 }
1160 }
1161 }
1162
Robert Carrb1579c82017-09-05 14:54:47 -07001163 /**
1164 * Merge the supplied transaction in to the deprecated "global" transaction.
1165 * This clears the supplied transaction in an identical fashion to {@link Transaction#merge}.
1166 * <p>
1167 * This is a utility for interop with legacy-code and will go away with the Global Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08001168 * @hide
Robert Carrb1579c82017-09-05 14:54:47 -07001169 */
1170 @Deprecated
1171 public static void mergeToGlobalTransaction(Transaction t) {
Robert Carrc5fc4612017-12-05 11:55:40 -08001172 synchronized(SurfaceControl.class) {
Robert Carrb1579c82017-09-05 14:54:47 -07001173 sGlobalTransaction.merge(t);
1174 }
1175 }
1176
chaviwdaaeab02019-03-20 12:25:37 -07001177 /** end a transaction
1178 * @hide
Robert Carra7827f72019-01-11 12:53:37 -08001179 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001180 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001181 public static void closeTransaction() {
chaviwdaaeab02019-03-20 12:25:37 -07001182 synchronized(SurfaceControl.class) {
1183 if (sTransactionNestCount == 0) {
1184 Log.e(TAG,
1185 "Call to SurfaceControl.closeTransaction without matching openTransaction");
1186 } else if (--sTransactionNestCount > 0) {
1187 return;
1188 }
1189 sGlobalTransaction.apply();
1190 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001191 }
1192
Robert Carra7827f72019-01-11 12:53:37 -08001193 /**
1194 * @hide
1195 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001196 public void deferTransactionUntil(SurfaceControl barrier, long frame) {
Robert Carr024378c2018-02-27 11:21:05 -08001197 synchronized(SurfaceControl.class) {
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001198 sGlobalTransaction.deferTransactionUntil(this, barrier, frame);
Robert Carrd5c7dd62017-03-08 10:39:30 -08001199 }
1200 }
1201
Robert Carra7827f72019-01-11 12:53:37 -08001202 /**
1203 * @hide
1204 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001205 public void reparentChildren(SurfaceControl newParent) {
Robert Carre13b58e2017-08-31 14:50:44 -07001206 synchronized(SurfaceControl.class) {
Vishnu Nairbc9beab2019-06-25 17:28:58 -07001207 sGlobalTransaction.reparentChildren(this, newParent);
Robert Carre13b58e2017-08-31 14:50:44 -07001208 }
Robert Carrd5c7dd62017-03-08 10:39:30 -08001209 }
1210
Robert Carra7827f72019-01-11 12:53:37 -08001211 /**
1212 * @hide
1213 */
Robert Carrd5c7dd62017-03-08 10:39:30 -08001214 public void detachChildren() {
Robert Carre13b58e2017-08-31 14:50:44 -07001215 synchronized(SurfaceControl.class) {
1216 sGlobalTransaction.detachChildren(this);
1217 }
Rob Carr64e516f2015-10-29 00:20:45 +00001218 }
1219
Robert Carra7827f72019-01-11 12:53:37 -08001220 /**
1221 * @hide
1222 */
Robert Carr1ca6a332016-04-11 18:00:43 -07001223 public void setOverrideScalingMode(int scalingMode) {
1224 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001225 synchronized(SurfaceControl.class) {
1226 sGlobalTransaction.setOverrideScalingMode(this, scalingMode);
1227 }
Robert Carr1ca6a332016-04-11 18:00:43 -07001228 }
1229
Robert Carra7827f72019-01-11 12:53:37 -08001230 /**
1231 * @hide
1232 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001233 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001234 public void setLayer(int zorder) {
1235 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001236 synchronized(SurfaceControl.class) {
1237 sGlobalTransaction.setLayer(this, zorder);
1238 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001239 }
1240
Robert Carra7827f72019-01-11 12:53:37 -08001241 /**
1242 * @hide
1243 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001244 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001245 public void setPosition(float x, float y) {
1246 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001247 synchronized(SurfaceControl.class) {
1248 sGlobalTransaction.setPosition(this, x, y);
1249 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001250 }
1251
Robert Carra7827f72019-01-11 12:53:37 -08001252 /**
1253 * @hide
1254 */
Vishnu Naire86bd982018-11-28 13:23:17 -08001255 public void setBufferSize(int w, int h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001256 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001257 synchronized(SurfaceControl.class) {
Vishnu Naire86bd982018-11-28 13:23:17 -08001258 sGlobalTransaction.setBufferSize(this, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07001259 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001260 }
1261
Robert Carra7827f72019-01-11 12:53:37 -08001262 /**
1263 * @hide
1264 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001265 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001266 public void hide() {
1267 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001268 synchronized(SurfaceControl.class) {
1269 sGlobalTransaction.hide(this);
1270 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001271 }
1272
Robert Carra7827f72019-01-11 12:53:37 -08001273 /**
1274 * @hide
1275 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001276 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001277 public void show() {
1278 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001279 synchronized(SurfaceControl.class) {
1280 sGlobalTransaction.show(this);
1281 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001282 }
1283
Robert Carra7827f72019-01-11 12:53:37 -08001284 /**
1285 * @hide
1286 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001287 public void setTransparentRegionHint(Region region) {
1288 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001289 synchronized(SurfaceControl.class) {
1290 sGlobalTransaction.setTransparentRegionHint(this, region);
1291 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001292 }
1293
Robert Carra7827f72019-01-11 12:53:37 -08001294 /**
1295 * @hide
1296 */
Svetoslav1376d602014-03-13 11:17:26 -07001297 public boolean clearContentFrameStats() {
1298 checkNotReleased();
1299 return nativeClearContentFrameStats(mNativeObject);
1300 }
1301
Robert Carra7827f72019-01-11 12:53:37 -08001302 /**
1303 * @hide
1304 */
Svetoslav1376d602014-03-13 11:17:26 -07001305 public boolean getContentFrameStats(WindowContentFrameStats outStats) {
1306 checkNotReleased();
1307 return nativeGetContentFrameStats(mNativeObject, outStats);
1308 }
1309
Robert Carra7827f72019-01-11 12:53:37 -08001310 /**
1311 * @hide
1312 */
Svetoslav1376d602014-03-13 11:17:26 -07001313 public static boolean clearAnimationFrameStats() {
1314 return nativeClearAnimationFrameStats();
1315 }
1316
Robert Carra7827f72019-01-11 12:53:37 -08001317 /**
1318 * @hide
1319 */
Svetoslav1376d602014-03-13 11:17:26 -07001320 public static boolean getAnimationFrameStats(WindowAnimationFrameStats outStats) {
1321 return nativeGetAnimationFrameStats(outStats);
1322 }
1323
Robert Carra7827f72019-01-11 12:53:37 -08001324 /**
1325 * @hide
1326 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001327 public void setAlpha(float alpha) {
1328 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001329 synchronized(SurfaceControl.class) {
1330 sGlobalTransaction.setAlpha(this, alpha);
1331 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001332 }
1333
Robert Carra7827f72019-01-11 12:53:37 -08001334 /**
1335 * @hide
1336 */
Robert Carr0edf18f2017-02-21 20:01:47 -08001337 public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001338 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001339 synchronized(SurfaceControl.class) {
1340 sGlobalTransaction.setMatrix(this, dsdx, dtdx, dtdy, dsdy);
1341 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001342 }
1343
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001344 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08001345 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
1346 * the color can be interpreted in any color space.
1347 * @param agnostic A boolean to indicate whether the surface is color space agnostic
1348 * @hide
1349 */
1350 public void setColorSpaceAgnostic(boolean agnostic) {
1351 checkNotReleased();
1352 synchronized (SurfaceControl.class) {
1353 sGlobalTransaction.setColorSpaceAgnostic(this, agnostic);
1354 }
1355 }
1356
1357 /**
Vishnu Naird454442d2018-11-13 13:51:01 -08001358 * Bounds the surface and its children to the bounds specified. Size of the surface will be
1359 * ignored and only the crop and buffer size will be used to determine the bounds of the
1360 * surface. If no crop is specified and the surface has no buffer, the surface bounds is only
1361 * constrained by the size of its parent bounds.
1362 *
1363 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08001364 * @hide
Vishnu Naird454442d2018-11-13 13:51:01 -08001365 */
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001366 public void setWindowCrop(Rect crop) {
1367 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001368 synchronized (SurfaceControl.class) {
1369 sGlobalTransaction.setWindowCrop(this, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001370 }
1371 }
1372
Vishnu Naird454442d2018-11-13 13:51:01 -08001373 /**
Robert Carra7827f72019-01-11 12:53:37 -08001374 * @hide
1375 */
Andy McFadden314405b2014-01-29 17:18:05 -08001376 public void setOpaque(boolean isOpaque) {
1377 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001378
1379 synchronized (SurfaceControl.class) {
1380 sGlobalTransaction.setOpaque(this, isOpaque);
Andy McFadden314405b2014-01-29 17:18:05 -08001381 }
1382 }
1383
Robert Carra7827f72019-01-11 12:53:37 -08001384 /**
1385 * @hide
1386 */
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001387 public void setSecure(boolean isSecure) {
1388 checkNotReleased();
Robert Carre13b58e2017-08-31 14:50:44 -07001389
1390 synchronized (SurfaceControl.class) {
1391 sGlobalTransaction.setSecure(this, isSecure);
Wale Ogunwalef5ad42f2015-06-12 13:59:03 -07001392 }
1393 }
1394
Robert Carra7827f72019-01-11 12:53:37 -08001395 /**
1396 * @hide
1397 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001398 public int getWidth() {
Svet Ganova5b49902020-06-18 00:47:22 -07001399 synchronized (mLock) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01001400 return mWidth;
1401 }
1402 }
1403
Robert Carra7827f72019-01-11 12:53:37 -08001404 /**
1405 * @hide
1406 */
Jorim Jaggia5e10572017-11-15 14:36:26 +01001407 public int getHeight() {
Svet Ganova5b49902020-06-18 00:47:22 -07001408 synchronized (mLock) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01001409 return mHeight;
1410 }
1411 }
1412
Svet Ganova5b49902020-06-18 00:47:22 -07001413 /**
1414 * Gets the local view that owns this surface.
1415 *
1416 * @return The owner view.
1417 *
1418 * @hide
1419 */
1420 public @Nullable View getLocalOwnerView() {
1421 return (mLocalOwnerView != null) ? mLocalOwnerView.get() : null;
1422 }
1423
Robert Carre13b58e2017-08-31 14:50:44 -07001424 @Override
1425 public String toString() {
1426 return "Surface(name=" + mName + ")/@0x" +
1427 Integer.toHexString(System.identityHashCode(this));
1428 }
1429
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001430 /**
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001431 * Immutable information about physical display.
1432 *
Robert Carr76907ee2019-01-11 13:38:19 -08001433 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001434 */
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001435 public static final class DisplayInfo {
Dominik Laskowski26290bb2020-01-15 16:09:55 -08001436 public boolean isInternal;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001437 public float density;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001438 public boolean secure;
Marin Shalamanov98af1592019-10-08 10:48:08 +02001439 public DeviceProductInfo deviceProductInfo;
Robert Carra7827f72019-01-11 12:53:37 -08001440
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001441 @Override
1442 public String toString() {
Dominik Laskowski26290bb2020-01-15 16:09:55 -08001443 return "DisplayInfo{isInternal=" + isInternal
1444 + ", density=" + density
Marin Shalamanov98af1592019-10-08 10:48:08 +02001445 + ", secure=" + secure
1446 + ", deviceProductInfo=" + deviceProductInfo + "}";
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001447 }
1448 }
1449
1450 /**
1451 * Configuration supported by physical display.
1452 *
1453 * @hide
1454 */
1455 public static final class DisplayConfig {
Ady Abraham8a5e3912020-02-18 17:28:26 -08001456 /**
1457 * Invalid display config id.
1458 */
1459 public static final int INVALID_DISPLAY_CONFIG_ID = -1;
1460
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001461 public int width;
1462 public int height;
1463 public float xDpi;
1464 public float yDpi;
1465
1466 public float refreshRate;
1467 public long appVsyncOffsetNanos;
1468 public long presentationDeadlineNanos;
1469
Ady Abraham8a5e3912020-02-18 17:28:26 -08001470 /**
1471 * The config group ID this config is associated to.
1472 * Configs in the same group are similar from vendor's perspective and switching between
1473 * configs within the same group can be done seamlessly in most cases.
1474 * @see: android.hardware.graphics.composer@2.4::IComposerClient::Attribute::CONFIG_GROUP
1475 */
1476 public int configGroup;
1477
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001478 @Override
1479 public String toString() {
1480 return "DisplayConfig{width=" + width
1481 + ", height=" + height
1482 + ", xDpi=" + xDpi
1483 + ", yDpi=" + yDpi
1484 + ", refreshRate=" + refreshRate
1485 + ", appVsyncOffsetNanos=" + appVsyncOffsetNanos
Ady Abraham8a5e3912020-02-18 17:28:26 -08001486 + ", presentationDeadlineNanos=" + presentationDeadlineNanos
1487 + ", configGroup=" + configGroup + "}";
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001488 }
1489 }
1490
Robert Carra7827f72019-01-11 12:53:37 -08001491 /**
1492 * @hide
1493 */
Prashant Malanic55929a2014-05-25 01:59:21 -07001494 public static void setDisplayPowerMode(IBinder displayToken, int mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001495 if (displayToken == null) {
1496 throw new IllegalArgumentException("displayToken must not be null");
1497 }
Prashant Malanic55929a2014-05-25 01:59:21 -07001498 nativeSetDisplayPowerMode(displayToken, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001499 }
1500
Robert Carra7827f72019-01-11 12:53:37 -08001501 /**
1502 * @hide
1503 */
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001504 public static SurfaceControl.DisplayInfo getDisplayInfo(IBinder displayToken) {
1505 if (displayToken == null) {
1506 throw new IllegalArgumentException("displayToken must not be null");
1507 }
1508 return nativeGetDisplayInfo(displayToken);
1509 }
1510
1511 /**
1512 * @hide
1513 */
1514 public static SurfaceControl.DisplayConfig[] getDisplayConfigs(IBinder displayToken) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001515 if (displayToken == null) {
1516 throw new IllegalArgumentException("displayToken must not be null");
1517 }
Dan Stoza00101052014-05-02 15:23:40 -07001518 return nativeGetDisplayConfigs(displayToken);
1519 }
1520
Robert Carra7827f72019-01-11 12:53:37 -08001521 /**
1522 * @hide
1523 */
Dan Stoza00101052014-05-02 15:23:40 -07001524 public static int getActiveConfig(IBinder displayToken) {
1525 if (displayToken == null) {
1526 throw new IllegalArgumentException("displayToken must not be null");
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001527 }
Dan Stoza00101052014-05-02 15:23:40 -07001528 return nativeGetActiveConfig(displayToken);
1529 }
1530
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001531 /**
1532 * @hide
1533 */
1534 public static DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
1535 IBinder displayToken) {
1536 if (displayToken == null) {
1537 throw new IllegalArgumentException("displayToken must not be null");
1538 }
1539 return nativeGetDisplayedContentSamplingAttributes(displayToken);
1540 }
1541
1542 /**
1543 * @hide
1544 */
1545 public static boolean setDisplayedContentSamplingEnabled(
1546 IBinder displayToken, boolean enable, int componentMask, int maxFrames) {
1547 if (displayToken == null) {
1548 throw new IllegalArgumentException("displayToken must not be null");
1549 }
1550 final int maxColorComponents = 4;
1551 if ((componentMask >> maxColorComponents) != 0) {
1552 throw new IllegalArgumentException("invalid componentMask when enabling sampling");
1553 }
1554 return nativeSetDisplayedContentSamplingEnabled(
1555 displayToken, enable, componentMask, maxFrames);
1556 }
1557
1558 /**
1559 * @hide
1560 */
1561 public static DisplayedContentSample getDisplayedContentSample(
1562 IBinder displayToken, long maxFrames, long timestamp) {
1563 if (displayToken == null) {
1564 throw new IllegalArgumentException("displayToken must not be null");
1565 }
1566 return nativeGetDisplayedContentSample(displayToken, maxFrames, timestamp);
1567 }
1568
1569
Robert Carra7827f72019-01-11 12:53:37 -08001570 /**
Ana Krulec52f12892019-11-18 03:57:20 -08001571 * Contains information about desired display configuration.
1572 *
1573 * @hide
1574 */
1575 public static final class DesiredDisplayConfigSpecs {
Ana Kruleca74a8642019-11-14 00:51:00 +01001576 public int defaultConfig;
Steven Thomas305a57c2020-04-17 12:28:36 -07001577 /**
1578 * The primary refresh rate range represents display manager's general guidance on the
1579 * display configs surface flinger will consider when switching refresh rates. Unless
1580 * surface flinger has a specific reason to do otherwise, it will stay within this range.
1581 */
1582 public float primaryRefreshRateMin;
1583 public float primaryRefreshRateMax;
1584 /**
1585 * The app request refresh rate range allows surface flinger to consider more display
1586 * configs when switching refresh rates. Although surface flinger will generally stay within
1587 * the primary range, specific considerations, such as layer frame rate settings specified
1588 * via the setFrameRate() api, may cause surface flinger to go outside the primary
1589 * range. Surface flinger never goes outside the app request range. The app request range
1590 * will be greater than or equal to the primary refresh rate range, never smaller.
1591 */
1592 public float appRequestRefreshRateMin;
1593 public float appRequestRefreshRateMax;
Ana Krulec52f12892019-11-18 03:57:20 -08001594
Ana Kruleca74a8642019-11-14 00:51:00 +01001595 public DesiredDisplayConfigSpecs() {}
Ana Krulec52f12892019-11-18 03:57:20 -08001596
Ana Kruleca74a8642019-11-14 00:51:00 +01001597 public DesiredDisplayConfigSpecs(DesiredDisplayConfigSpecs other) {
1598 copyFrom(other);
1599 }
Ana Krulec52f12892019-11-18 03:57:20 -08001600
Steven Thomas305a57c2020-04-17 12:28:36 -07001601 public DesiredDisplayConfigSpecs(int defaultConfig, float primaryRefreshRateMin,
1602 float primaryRefreshRateMax, float appRequestRefreshRateMin,
1603 float appRequestRefreshRateMax) {
Ana Kruleca74a8642019-11-14 00:51:00 +01001604 this.defaultConfig = defaultConfig;
Steven Thomas305a57c2020-04-17 12:28:36 -07001605 this.primaryRefreshRateMin = primaryRefreshRateMin;
1606 this.primaryRefreshRateMax = primaryRefreshRateMax;
1607 this.appRequestRefreshRateMin = appRequestRefreshRateMin;
1608 this.appRequestRefreshRateMax = appRequestRefreshRateMax;
Ana Kruleca74a8642019-11-14 00:51:00 +01001609 }
1610
1611 @Override
1612 public boolean equals(Object o) {
1613 return o instanceof DesiredDisplayConfigSpecs && equals((DesiredDisplayConfigSpecs) o);
1614 }
1615
1616 /**
1617 * Tests for equality.
1618 */
1619 public boolean equals(DesiredDisplayConfigSpecs other) {
1620 return other != null && defaultConfig == other.defaultConfig
Steven Thomas305a57c2020-04-17 12:28:36 -07001621 && primaryRefreshRateMin == other.primaryRefreshRateMin
1622 && primaryRefreshRateMax == other.primaryRefreshRateMax
1623 && appRequestRefreshRateMin == other.appRequestRefreshRateMin
1624 && appRequestRefreshRateMax == other.appRequestRefreshRateMax;
Ana Kruleca74a8642019-11-14 00:51:00 +01001625 }
1626
1627 @Override
1628 public int hashCode() {
1629 return 0; // don't care
1630 }
1631
1632 /**
1633 * Copies the supplied object's values to this object.
1634 */
1635 public void copyFrom(DesiredDisplayConfigSpecs other) {
1636 defaultConfig = other.defaultConfig;
Steven Thomas305a57c2020-04-17 12:28:36 -07001637 primaryRefreshRateMin = other.primaryRefreshRateMin;
1638 primaryRefreshRateMax = other.primaryRefreshRateMax;
1639 appRequestRefreshRateMin = other.appRequestRefreshRateMin;
1640 appRequestRefreshRateMax = other.appRequestRefreshRateMax;
Ana Kruleca74a8642019-11-14 00:51:00 +01001641 }
1642
1643 @Override
1644 public String toString() {
Steven Thomas305a57c2020-04-17 12:28:36 -07001645 return String.format("defaultConfig=%d primaryRefreshRateRange=[%.0f %.0f]"
1646 + " appRequestRefreshRateRange=[%.0f %.0f]",
1647 defaultConfig, primaryRefreshRateMin, primaryRefreshRateMax,
1648 appRequestRefreshRateMin, appRequestRefreshRateMax);
Ana Krulec52f12892019-11-18 03:57:20 -08001649 }
1650 }
1651
1652 /**
Ady Abraham42f9a2fb2019-02-26 14:13:39 -08001653 * @hide
1654 */
Ana Krulec4f753aa2019-11-14 00:49:39 +01001655 public static boolean setDesiredDisplayConfigSpecs(IBinder displayToken,
Ana Krulec52f12892019-11-18 03:57:20 -08001656 SurfaceControl.DesiredDisplayConfigSpecs desiredDisplayConfigSpecs) {
Ana Krulec4f753aa2019-11-14 00:49:39 +01001657 if (displayToken == null) {
1658 throw new IllegalArgumentException("displayToken must not be null");
1659 }
1660
Ana Krulec52f12892019-11-18 03:57:20 -08001661 return nativeSetDesiredDisplayConfigSpecs(displayToken, desiredDisplayConfigSpecs);
Ana Krulec4f753aa2019-11-14 00:49:39 +01001662 }
1663
1664 /**
1665 * @hide
1666 */
Ana Kruleca74a8642019-11-14 00:51:00 +01001667 public static SurfaceControl.DesiredDisplayConfigSpecs getDesiredDisplayConfigSpecs(
1668 IBinder displayToken) {
1669 if (displayToken == null) {
1670 throw new IllegalArgumentException("displayToken must not be null");
1671 }
1672
1673 return nativeGetDesiredDisplayConfigSpecs(displayToken);
1674 }
1675
1676 /**
1677 * @hide
1678 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001679 public static int[] getDisplayColorModes(IBinder displayToken) {
1680 if (displayToken == null) {
1681 throw new IllegalArgumentException("displayToken must not be null");
1682 }
1683 return nativeGetDisplayColorModes(displayToken);
1684 }
1685
Robert Carra7827f72019-01-11 12:53:37 -08001686 /**
Daniel Solomon10e3b332019-01-20 21:09:11 -08001687 * Color coordinates in CIE1931 XYZ color space
1688 *
1689 * @hide
1690 */
1691 public static final class CieXyz {
1692 /**
1693 * @hide
1694 */
1695 public float X;
1696
1697 /**
1698 * @hide
1699 */
1700 public float Y;
1701
1702 /**
1703 * @hide
1704 */
1705 public float Z;
1706 }
1707
1708 /**
1709 * Contains a display's color primaries
1710 *
1711 * @hide
1712 */
1713 public static final class DisplayPrimaries {
1714 /**
1715 * @hide
1716 */
1717 public CieXyz red;
1718
1719 /**
1720 * @hide
1721 */
1722 public CieXyz green;
1723
1724 /**
1725 * @hide
1726 */
1727 public CieXyz blue;
1728
1729 /**
1730 * @hide
1731 */
1732 public CieXyz white;
1733
1734 /**
1735 * @hide
1736 */
1737 public DisplayPrimaries() {
1738 }
1739 }
1740
1741 /**
1742 * @hide
1743 */
1744 public static SurfaceControl.DisplayPrimaries getDisplayNativePrimaries(
1745 IBinder displayToken) {
1746 if (displayToken == null) {
1747 throw new IllegalArgumentException("displayToken must not be null");
1748 }
1749
1750 return nativeGetDisplayNativePrimaries(displayToken);
1751 }
1752
1753 /**
Robert Carra7827f72019-01-11 12:53:37 -08001754 * @hide
1755 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001756 public static int getActiveColorMode(IBinder displayToken) {
1757 if (displayToken == null) {
1758 throw new IllegalArgumentException("displayToken must not be null");
1759 }
1760 return nativeGetActiveColorMode(displayToken);
1761 }
1762
Robert Carra7827f72019-01-11 12:53:37 -08001763 /**
1764 * @hide
1765 */
Michael Wright1c9977b2016-07-12 13:30:10 -07001766 public static boolean setActiveColorMode(IBinder displayToken, int colorMode) {
1767 if (displayToken == null) {
1768 throw new IllegalArgumentException("displayToken must not be null");
1769 }
1770 return nativeSetActiveColorMode(displayToken, colorMode);
1771 }
1772
Robert Carra7827f72019-01-11 12:53:37 -08001773 /**
Peiyong Lin5f4a5682019-01-17 11:37:07 -08001774 * Returns an array of color spaces with 2 elements. The first color space is the
1775 * default color space and second one is wide color gamut color space.
1776 * @hide
1777 */
1778 public static ColorSpace[] getCompositionColorSpaces() {
1779 int[] dataspaces = nativeGetCompositionDataspaces();
1780 ColorSpace srgb = ColorSpace.get(ColorSpace.Named.SRGB);
1781 ColorSpace[] colorSpaces = { srgb, srgb };
1782 if (dataspaces.length == 2) {
1783 for (int i = 0; i < 2; ++i) {
1784 switch(dataspaces[i]) {
1785 case INTERNAL_DATASPACE_DISPLAY_P3:
1786 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.DISPLAY_P3);
1787 break;
1788 case INTERNAL_DATASPACE_SCRGB:
1789 colorSpaces[i] = ColorSpace.get(ColorSpace.Named.EXTENDED_SRGB);
1790 break;
1791 case INTERNAL_DATASPACE_SRGB:
1792 // Other dataspace is not recognized, use SRGB color space instead,
1793 // the default value of the array is already SRGB, thus do nothing.
1794 default:
1795 break;
1796 }
1797 }
1798 }
1799 return colorSpaces;
1800 }
1801
1802 /**
Robert Carra7827f72019-01-11 12:53:37 -08001803 * @hide
1804 */
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001805 public static void setAutoLowLatencyMode(IBinder displayToken, boolean on) {
1806 if (displayToken == null) {
1807 throw new IllegalArgumentException("displayToken must not be null");
1808 }
1809
1810 nativeSetAutoLowLatencyMode(displayToken, on);
1811 }
1812
1813 /**
1814 * @hide
1815 */
1816 public static void setGameContentType(IBinder displayToken, boolean on) {
1817 if (displayToken == null) {
1818 throw new IllegalArgumentException("displayToken must not be null");
1819 }
1820
1821 nativeSetGameContentType(displayToken, on);
1822 }
1823
1824 /**
1825 * @hide
1826 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001827 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001828 public static void setDisplayProjection(IBinder displayToken,
1829 int orientation, Rect layerStackRect, Rect displayRect) {
Robert Carre13b58e2017-08-31 14:50:44 -07001830 synchronized (SurfaceControl.class) {
1831 sGlobalTransaction.setDisplayProjection(displayToken, orientation,
1832 layerStackRect, displayRect);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001833 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001834 }
1835
Robert Carra7827f72019-01-11 12:53:37 -08001836 /**
1837 * @hide
1838 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001839 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001840 public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -07001841 synchronized (SurfaceControl.class) {
1842 sGlobalTransaction.setDisplayLayerStack(displayToken, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001843 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001844 }
1845
Robert Carra7827f72019-01-11 12:53:37 -08001846 /**
1847 * @hide
1848 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001849 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001850 public static void setDisplaySurface(IBinder displayToken, Surface surface) {
Robert Carre13b58e2017-08-31 14:50:44 -07001851 synchronized (SurfaceControl.class) {
1852 sGlobalTransaction.setDisplaySurface(displayToken, surface);
Jeff Brownfc0ebd72013-04-30 16:33:00 -07001853 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001854 }
1855
Robert Carra7827f72019-01-11 12:53:37 -08001856 /**
1857 * @hide
1858 */
Michael Wright01e840f2014-06-26 16:03:25 -07001859 public static void setDisplaySize(IBinder displayToken, int width, int height) {
Robert Carre13b58e2017-08-31 14:50:44 -07001860 synchronized (SurfaceControl.class) {
1861 sGlobalTransaction.setDisplaySize(displayToken, width, height);
Michael Wright01e840f2014-06-26 16:03:25 -07001862 }
Michael Wright01e840f2014-06-26 16:03:25 -07001863 }
1864
Robert Carra7827f72019-01-11 12:53:37 -08001865 /**
1866 * @hide
1867 */
Michael Wright9ff94c02016-03-30 18:05:40 -07001868 public static Display.HdrCapabilities getHdrCapabilities(IBinder displayToken) {
1869 if (displayToken == null) {
1870 throw new IllegalArgumentException("displayToken must not be null");
1871 }
1872 return nativeGetHdrCapabilities(displayToken);
1873 }
1874
Robert Carra7827f72019-01-11 12:53:37 -08001875 /**
1876 * @hide
1877 */
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001878 public static boolean getAutoLowLatencyModeSupport(IBinder displayToken) {
1879 if (displayToken == null) {
1880 throw new IllegalArgumentException("displayToken must not be null");
1881 }
1882
1883 return nativeGetAutoLowLatencyModeSupport(displayToken);
1884 }
1885
1886 /**
1887 * @hide
1888 */
1889 public static boolean getGameContentTypeSupport(IBinder displayToken) {
1890 if (displayToken == null) {
1891 throw new IllegalArgumentException("displayToken must not be null");
1892 }
1893
1894 return nativeGetGameContentTypeSupport(displayToken);
1895 }
1896
1897 /**
1898 * @hide
1899 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001900 @UnsupportedAppUsage
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001901 public static IBinder createDisplay(String name, boolean secure) {
1902 if (name == null) {
1903 throw new IllegalArgumentException("name must not be null");
1904 }
1905 return nativeCreateDisplay(name, secure);
1906 }
1907
Robert Carra7827f72019-01-11 12:53:37 -08001908 /**
1909 * @hide
1910 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001911 @UnsupportedAppUsage
Jesse Hall6a6bc212013-08-08 12:15:03 -07001912 public static void destroyDisplay(IBinder displayToken) {
1913 if (displayToken == null) {
1914 throw new IllegalArgumentException("displayToken must not be null");
1915 }
1916 nativeDestroyDisplay(displayToken);
1917 }
1918
Robert Carra7827f72019-01-11 12:53:37 -08001919 /**
1920 * @hide
1921 */
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001922 public static long[] getPhysicalDisplayIds() {
1923 return nativeGetPhysicalDisplayIds();
1924 }
1925
1926 /**
1927 * @hide
1928 */
1929 public static IBinder getPhysicalDisplayToken(long physicalDisplayId) {
1930 return nativeGetPhysicalDisplayToken(physicalDisplayId);
1931 }
1932
1933 /**
Dominik Laskowski69b281d2019-11-22 14:13:12 -08001934 * TODO(b/116025192): Remove this stopgap once framework is display-agnostic.
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001935 *
1936 * @hide
1937 */
1938 public static IBinder getInternalDisplayToken() {
1939 final long[] physicalDisplayIds = getPhysicalDisplayIds();
1940 if (physicalDisplayIds.length == 0) {
1941 return null;
1942 }
1943 return getPhysicalDisplayToken(physicalDisplayIds[0]);
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001944 }
1945
Mathias Agopian0449a402013-03-01 23:01:51 -08001946 /**
chaviw08520a02018-09-10 16:44:56 -07001947 * @see SurfaceControl#screenshot(IBinder, Surface, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001948 * @hide
Mathias Agopian0449a402013-03-01 23:01:51 -08001949 */
1950 public static void screenshot(IBinder display, Surface consumer) {
chaviw08520a02018-09-10 16:44:56 -07001951 screenshot(display, consumer, new Rect(), 0, 0, false, 0);
1952 }
1953
1954 /**
1955 * Copy the current screen contents into the provided {@link Surface}
1956 *
1957 * @param consumer The {@link Surface} to take the screenshot into.
1958 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)
Robert Carra7827f72019-01-11 12:53:37 -08001959 * @hide
chaviw08520a02018-09-10 16:44:56 -07001960 */
1961 public static void screenshot(IBinder display, Surface consumer, Rect sourceCrop, int width,
1962 int height, boolean useIdentityTransform, int rotation) {
1963 if (consumer == null) {
1964 throw new IllegalArgumentException("consumer must not be null");
1965 }
1966
Peiyong Line3e5efd2019-03-21 20:59:47 +00001967 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(display, sourceCrop, width,
1968 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07001969 try {
Peiyong Linccc06b62019-06-25 17:31:09 -07001970 consumer.attachAndQueueBufferWithColorSpace(buffer.getGraphicBuffer(),
1971 buffer.getColorSpace());
chaviw08520a02018-09-10 16:44:56 -07001972 } catch (RuntimeException e) {
1973 Log.w(TAG, "Failed to take screenshot - " + e.getMessage());
1974 }
1975 }
1976
1977 /**
1978 * @see SurfaceControl#screenshot(Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001979 * @hide
chaviw08520a02018-09-10 16:44:56 -07001980 */
1981 @UnsupportedAppUsage
1982 public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) {
1983 return screenshot(sourceCrop, width, height, false, rotation);
Mathias Agopian0449a402013-03-01 23:01:51 -08001984 }
1985
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001986 /**
chaviw1da9cd92017-12-06 10:48:11 -08001987 * Copy the current screen contents into a hardware bitmap and return it.
1988 * Note: If you want to modify the Bitmap in software, you will need to copy the Bitmap into
1989 * a software Bitmap using {@link Bitmap#copy(Bitmap.Config, boolean)}
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001990 *
chaviw08520a02018-09-10 16:44:56 -07001991 * CAVEAT: Versions of screenshot that return a {@link Bitmap} can be extremely slow; avoid use
1992 * unless absolutely necessary; prefer the versions that use a {@link Surface} such as
1993 * {@link SurfaceControl#screenshot(IBinder, Surface)} or {@link GraphicBuffer} such as
1994 * {@link SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}.
Mathias Agopian0449a402013-03-01 23:01:51 -08001995 *
chaviw08520a02018-09-10 16:44:56 -07001996 * @see SurfaceControl#screenshotToBuffer(IBinder, Rect, int, int, boolean, int)}
Robert Carra7827f72019-01-11 12:53:37 -08001997 * @hide
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001998 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001999 @UnsupportedAppUsage
Dan Stoza9890e3412014-05-22 16:12:54 -07002000 public static Bitmap screenshot(Rect sourceCrop, int width, int height,
chaviw08520a02018-09-10 16:44:56 -07002001 boolean useIdentityTransform, int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -08002002 // TODO: should take the display as a parameter
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08002003 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
2004 if (displayToken == null) {
2005 Log.w(TAG, "Failed to take screenshot because internal display is disconnected");
2006 return null;
2007 }
2008
chaviwa69e0a72017-11-29 17:55:12 -08002009 if (rotation == ROTATION_90 || rotation == ROTATION_270) {
2010 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2011 }
2012
2013 SurfaceControl.rotateCropForSF(sourceCrop, rotation);
Peiyong Line3e5efd2019-03-21 20:59:47 +00002014 final ScreenshotGraphicBuffer buffer = screenshotToBuffer(displayToken, sourceCrop, width,
2015 height, useIdentityTransform, rotation);
chaviw08520a02018-09-10 16:44:56 -07002016
2017 if (buffer == null) {
2018 Log.w(TAG, "Failed to take screenshot");
2019 return null;
2020 }
Sunny Goyal62915b22019-04-10 12:28:47 -07002021 return Bitmap.wrapHardwareBuffer(buffer.getGraphicBuffer(), buffer.getColorSpace());
Mathias Agopian3866f0d2013-02-11 22:08:48 -08002022 }
Jesse Hall6a6bc212013-08-08 12:15:03 -07002023
chaviw08520a02018-09-10 16:44:56 -07002024 /**
2025 * Captures all the surfaces in a display and returns a {@link GraphicBuffer} with the content.
2026 *
2027 * @param display The display to take the screenshot of.
2028 * @param sourceCrop The portion of the screen to capture into the Bitmap; caller may
2029 * pass in 'new Rect()' if no cropping is desired.
2030 * @param width The desired width of the returned bitmap; the raw screen will be
2031 * scaled down to this size; caller may pass in 0 if no scaling is
2032 * desired.
2033 * @param height The desired height of the returned bitmap; the raw screen will
2034 * be scaled down to this size; caller may pass in 0 if no scaling
2035 * is desired.
2036 * @param useIdentityTransform Replace whatever transformation (rotation, scaling, translation)
2037 * the surface layers are currently using with the identity
2038 * transformation while taking the screenshot.
2039 * @param rotation Apply a custom clockwise rotation to the screenshot, i.e.
2040 * Surface.ROTATION_0,90,180,270. SurfaceFlinger will always take
2041 * screenshots in its native portrait orientation by default, so
2042 * this is useful for returning screenshots that are independent of
2043 * device orientation.
2044 * @return Returns a GraphicBuffer that contains the captured content.
Robert Carra7827f72019-01-11 12:53:37 -08002045 * @hide
chaviw08520a02018-09-10 16:44:56 -07002046 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00002047 public static ScreenshotGraphicBuffer screenshotToBuffer(IBinder display, Rect sourceCrop,
2048 int width, int height, boolean useIdentityTransform, int rotation) {
Mathias Agopian0449a402013-03-01 23:01:51 -08002049 if (display == null) {
2050 throw new IllegalArgumentException("displayToken must not be null");
2051 }
chaviw08520a02018-09-10 16:44:56 -07002052
Robert Carr5c52b132019-02-15 15:48:11 -08002053 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
2054 false /* captureSecureLayers */);
2055 }
2056
2057 /**
2058 * Like screenshotToBuffer, but if the caller is AID_SYSTEM, allows
2059 * for the capture of secure layers. This is used for the screen rotation
2060 * animation where the system server takes screenshots but does
2061 * not persist them or allow them to leave the server. However in other
2062 * cases in the system server, we mostly want to omit secure layers
2063 * like when we take a screenshot on behalf of the assistant.
2064 *
2065 * @hide
2066 */
Peiyong Line3e5efd2019-03-21 20:59:47 +00002067 public static ScreenshotGraphicBuffer screenshotToBufferWithSecureLayersUnsafe(IBinder display,
Robert Carr5c52b132019-02-15 15:48:11 -08002068 Rect sourceCrop, int width, int height, boolean useIdentityTransform,
2069 int rotation) {
2070 if (display == null) {
2071 throw new IllegalArgumentException("displayToken must not be null");
2072 }
2073
2074 return nativeScreenshot(display, sourceCrop, width, height, useIdentityTransform, rotation,
2075 true /* captureSecureLayers */);
Mathias Agopian0449a402013-03-01 23:01:51 -08002076 }
Robert Carre13b58e2017-08-31 14:50:44 -07002077
chaviwa69e0a72017-11-29 17:55:12 -08002078 private static void rotateCropForSF(Rect crop, int rot) {
2079 if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270) {
2080 int tmp = crop.top;
2081 crop.top = crop.left;
2082 crop.left = tmp;
2083 tmp = crop.right;
2084 crop.right = crop.bottom;
2085 crop.bottom = tmp;
2086 }
2087 }
2088
chaviw1cda84c2017-10-23 16:47:10 -07002089 /**
chaviw1da9cd92017-12-06 10:48:11 -08002090 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
chaviw1cda84c2017-10-23 16:47:10 -07002091 *
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002092 * @param layer The root layer to capture.
chaviwfbe47df2017-11-10 16:14:49 -08002093 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
Vishnu Naira8bff972019-12-03 10:18:10 -08002094 * Rect()' or null if no cropping is desired. If the root layer does not
2095 * have a buffer or a crop set, then a non-empty source crop must be
2096 * specified.
chaviwfbe47df2017-11-10 16:14:49 -08002097 * @param frameScale The desired scale of the returned buffer; the raw
2098 * screen will be scaled up/down.
Chavi Weingartend7ec64c2017-11-30 01:52:01 +00002099 *
2100 * @return Returns a GraphicBuffer that contains the layer capture.
Robert Carra7827f72019-01-11 12:53:37 -08002101 * @hide
chaviw1cda84c2017-10-23 16:47:10 -07002102 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002103 public static ScreenshotGraphicBuffer captureLayers(SurfaceControl layer, Rect sourceCrop,
chaviwfbe47df2017-11-10 16:14:49 -08002104 float frameScale) {
Chiawei Wang02202d12019-01-03 18:12:13 +08002105 return captureLayers(layer, sourceCrop, frameScale, PixelFormat.RGBA_8888);
2106 }
2107
2108 /**
2109 * Captures a layer and its children and returns a {@link GraphicBuffer} with the content.
2110 *
2111 * @param layer The root layer to capture.
2112 * @param sourceCrop The portion of the root surface to capture; caller may pass in 'new
Vishnu Naira8bff972019-12-03 10:18:10 -08002113 * Rect()' or null if no cropping is desired. If the root layer does not
2114 * have a buffer or a crop set, then a non-empty source crop must be
2115 * specified.
Chiawei Wang02202d12019-01-03 18:12:13 +08002116 * @param frameScale The desired scale of the returned buffer; the raw
2117 * screen will be scaled up/down.
2118 * @param format The desired pixel format of the returned buffer.
2119 *
2120 * @return Returns a GraphicBuffer that contains the layer capture.
2121 * @hide
2122 */
2123 public static ScreenshotGraphicBuffer captureLayers(SurfaceControl layer, Rect sourceCrop,
2124 float frameScale, int format) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07002125 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
Chiawei Wang02202d12019-01-03 18:12:13 +08002126 return nativeCaptureLayers(displayToken, layer.mNativeObject, sourceCrop, frameScale, null,
2127 format);
Robert Carrffcdc512019-04-02 11:51:11 -07002128 }
2129
2130 /**
2131 * Like {@link captureLayers} but with an array of layer handles to exclude.
2132 * @hide
2133 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002134 public static ScreenshotGraphicBuffer captureLayersExcluding(SurfaceControl layer,
Robert Carr689a1b02020-03-12 15:33:51 -07002135 Rect sourceCrop, float frameScale, int format, SurfaceControl[] exclude) {
Peiyong Lin21e499a2019-04-03 16:37:46 -07002136 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002137 long[] nativeExcludeObjects = new long[exclude.length];
2138 for (int i = 0; i < exclude.length; i++) {
2139 nativeExcludeObjects[i] = exclude[i].mNativeObject;
2140 }
2141 return nativeCaptureLayers(displayToken, layer.mNativeObject, sourceCrop, frameScale,
Chiawei Wang02202d12019-01-03 18:12:13 +08002142 nativeExcludeObjects, PixelFormat.RGBA_8888);
chaviw1cda84c2017-10-23 16:47:10 -07002143 }
2144
Robert Carra7827f72019-01-11 12:53:37 -08002145 /**
Peiyong Lin0ddb7d42019-01-16 13:25:09 -08002146 * Returns whether protected content is supported in GPU composition.
2147 * @hide
2148 */
2149 public static boolean getProtectedContentSupport() {
2150 return nativeGetProtectedContentSupport();
2151 }
2152
2153 /**
Dan Gittik832b4972019-02-13 18:17:47 +00002154 * Returns whether brightness operations are supported on a display.
2155 *
2156 * @param displayToken
2157 * The token for the display.
2158 *
2159 * @return Whether brightness operations are supported on the display.
2160 *
2161 * @hide
2162 */
2163 public static boolean getDisplayBrightnessSupport(IBinder displayToken) {
2164 return nativeGetDisplayBrightnessSupport(displayToken);
2165 }
2166
2167 /**
2168 * Sets the brightness of a display.
2169 *
2170 * @param displayToken
2171 * The token for the display whose brightness is set.
2172 * @param brightness
2173 * A number between 0.0f (minimum brightness) and 1.0f (maximum brightness), or -1.0f to
2174 * turn the backlight off.
2175 *
2176 * @return Whether the method succeeded or not.
2177 *
2178 * @throws IllegalArgumentException if:
2179 * - displayToken is null;
2180 * - brightness is NaN or greater than 1.0f.
2181 *
2182 * @hide
2183 */
2184 public static boolean setDisplayBrightness(IBinder displayToken, float brightness) {
2185 Objects.requireNonNull(displayToken);
2186 if (Float.isNaN(brightness) || brightness > 1.0f
2187 || (brightness < 0.0f && brightness != -1.0f)) {
2188 throw new IllegalArgumentException("brightness must be a number between 0.0f and 1.0f,"
2189 + " or -1 to turn the backlight off.");
2190 }
2191 return nativeSetDisplayBrightness(displayToken, brightness);
2192 }
2193
chaviwa51724f2019-09-19 09:50:11 -07002194 /**
2195 * Creates a mirrored hierarchy for the mirrorOf {@link SurfaceControl}
2196 *
2197 * Real Hierarchy Mirror
2198 * SC (value that's returned)
2199 * |
2200 * A A'
2201 * | |
2202 * B B'
2203 *
2204 * @param mirrorOf The root of the hierarchy that should be mirrored.
2205 * @return A SurfaceControl that's the parent of the root of the mirrored hierarchy.
2206 *
2207 * @hide
2208 */
2209 public static SurfaceControl mirrorSurface(SurfaceControl mirrorOf) {
2210 long nativeObj = nativeMirrorSurface(mirrorOf.mNativeObject);
2211 SurfaceControl sc = new SurfaceControl();
2212 sc.assignNativeObject(nativeObj);
2213 return sc;
2214 }
2215
Vishnu Nair4a067c52019-11-19 14:25:56 -08002216 private static void validateColorArg(@Size(4) float[] color) {
2217 final String msg = "Color must be specified as a float array with"
2218 + " four values to represent r, g, b, a in range [0..1]";
2219 if (color.length != 4) {
2220 throw new IllegalArgumentException(msg);
2221 }
2222 for (float c:color) {
2223 if ((c < 0.f) || (c > 1.f)) {
2224 throw new IllegalArgumentException(msg);
2225 }
2226 }
2227 }
2228
2229 /**
2230 * Sets the global configuration for all the shadows drawn by SurfaceFlinger. Shadow follows
2231 * material design guidelines.
2232 *
2233 * @param ambientColor Color applied to the ambient shadow. The alpha is premultiplied. A
2234 * float array with four values to represent r, g, b, a in range [0..1]
2235 * @param spotColor Color applied to the spot shadow. The alpha is premultiplied. The position
2236 * of the spot shadow depends on the light position. A float array with
2237 * four values to represent r, g, b, a in range [0..1]
2238 * @param lightPosY Y axis position of the light used to cast the spot shadow in pixels.
2239 * @param lightPosZ Z axis position of the light used to cast the spot shadow in pixels. The X
2240 * axis position is set to the display width / 2.
2241 * @param lightRadius Radius of the light casting the shadow in pixels.
2242 *[
2243 * @hide
2244 */
2245 public static void setGlobalShadowSettings(@Size(4) float[] ambientColor,
2246 @Size(4) float[] spotColor, float lightPosY, float lightPosZ, float lightRadius) {
2247 validateColorArg(ambientColor);
2248 validateColorArg(spotColor);
2249 nativeSetGlobalShadowSettings(ambientColor, spotColor, lightPosY, lightPosZ, lightRadius);
2250 }
2251
Vishnu Nair629df2b2019-06-11 16:03:38 -07002252 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002253 * An atomic set of changes to a set of SurfaceControl.
Robert Carra7827f72019-01-11 12:53:37 -08002254 */
Vishnu Nair629df2b2019-06-11 16:03:38 -07002255 public static class Transaction implements Closeable, Parcelable {
Robert Carr76907ee2019-01-11 13:38:19 -08002256 /**
2257 * @hide
2258 */
Robert Carre13b58e2017-08-31 14:50:44 -07002259 public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
2260 Transaction.class.getClassLoader(),
2261 nativeGetNativeTransactionFinalizer(), 512);
Robert Carr48ec4e02019-07-16 14:28:47 -07002262 /**
2263 * @hide
2264 */
2265 public long mNativeObject;
Robert Carre13b58e2017-08-31 14:50:44 -07002266
Jorim Jaggia5e10572017-11-15 14:36:26 +01002267 private final ArrayMap<SurfaceControl, Point> mResizedSurfaces = new ArrayMap<>();
Svet Ganova5b49902020-06-18 00:47:22 -07002268 private final ArrayMap<SurfaceControl, SurfaceControl> mReparentedSurfaces =
2269 new ArrayMap<>();
2270
Robert Carre13b58e2017-08-31 14:50:44 -07002271 Runnable mFreeNativeResources;
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002272 private static final float[] INVALID_COLOR = {-1, -1, -1};
Robert Carre13b58e2017-08-31 14:50:44 -07002273
Robert Carra7827f72019-01-11 12:53:37 -08002274 /**
Robert Carrfee0b822019-12-17 23:56:44 -08002275 * @hide
2276 */
2277 protected void checkPreconditions(SurfaceControl sc) {
2278 sc.checkNotReleased();
2279 }
2280
2281 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002282 * Open a new transaction object. The transaction may be filed with commands to
2283 * manipulate {@link SurfaceControl} instances, and then applied atomically with
2284 * {@link #apply}. Eventually the user should invoke {@link #close}, when the object
2285 * is no longer required. Note however that re-using a transaction after a call to apply
2286 * is allowed as a convenience.
Robert Carra7827f72019-01-11 12:53:37 -08002287 */
Robert Carre13b58e2017-08-31 14:50:44 -07002288 public Transaction() {
2289 mNativeObject = nativeCreateTransaction();
2290 mFreeNativeResources
2291 = sRegistry.registerNativeAllocation(this, mNativeObject);
2292 }
2293
Vishnu Nair629df2b2019-06-11 16:03:38 -07002294 private Transaction(Parcel in) {
2295 readFromParcel(in);
2296 }
2297
Robert Carre13b58e2017-08-31 14:50:44 -07002298 /**
2299 * Apply the transaction, clearing it's state, and making it usable
2300 * as a new transaction.
2301 */
2302 public void apply() {
2303 apply(false);
2304 }
2305
2306 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002307 * Release the native transaction object, without applying it.
Robert Carre13b58e2017-08-31 14:50:44 -07002308 */
2309 @Override
2310 public void close() {
Svet Ganova5b49902020-06-18 00:47:22 -07002311 mResizedSurfaces.clear();
2312 mReparentedSurfaces.clear();
Robert Carre13b58e2017-08-31 14:50:44 -07002313 mFreeNativeResources.run();
2314 mNativeObject = 0;
2315 }
2316
2317 /**
2318 * Jankier version of apply. Avoid use (b/28068298).
Robert Carra7827f72019-01-11 12:53:37 -08002319 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002320 */
2321 public void apply(boolean sync) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002322 applyResizedSurfaces();
Svet Ganova5b49902020-06-18 00:47:22 -07002323 notifyReparentedSurfaces();
Robert Carre13b58e2017-08-31 14:50:44 -07002324 nativeApplyTransaction(mNativeObject, sync);
2325 }
2326
Jorim Jaggia5e10572017-11-15 14:36:26 +01002327 private void applyResizedSurfaces() {
2328 for (int i = mResizedSurfaces.size() - 1; i >= 0; i--) {
2329 final Point size = mResizedSurfaces.valueAt(i);
2330 final SurfaceControl surfaceControl = mResizedSurfaces.keyAt(i);
Svet Ganova5b49902020-06-18 00:47:22 -07002331 synchronized (surfaceControl.mLock) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002332 surfaceControl.mWidth = size.x;
2333 surfaceControl.mHeight = size.y;
2334 }
2335 }
2336 mResizedSurfaces.clear();
2337 }
2338
Svet Ganova5b49902020-06-18 00:47:22 -07002339 private void notifyReparentedSurfaces() {
2340 final int reparentCount = mReparentedSurfaces.size();
2341 for (int i = reparentCount - 1; i >= 0; i--) {
2342 final SurfaceControl child = mReparentedSurfaces.keyAt(i);
2343 synchronized (child.mLock) {
2344 final int listenerCount = (child.mReparentListeners != null)
2345 ? child.mReparentListeners.size() : 0;
2346 for (int j = 0; j < listenerCount; j++) {
2347 final OnReparentListener listener = child.mReparentListeners.get(j);
2348 listener.onReparent(this, mReparentedSurfaces.valueAt(i));
2349 }
2350 mReparentedSurfaces.removeAt(i);
2351 }
2352 }
2353 }
2354
Robert Carra7827f72019-01-11 12:53:37 -08002355 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002356 * Toggle the visibility of a given Layer and it's sub-tree.
2357 *
2358 * @param sc The SurfaceControl for which to set the visibility
2359 * @param visible The new visibility
2360 * @return This transaction object.
2361 */
2362 @NonNull
2363 public Transaction setVisibility(@NonNull SurfaceControl sc, boolean visible) {
Robert Carrfee0b822019-12-17 23:56:44 -08002364 checkPreconditions(sc);
Robert Carr76907ee2019-01-11 13:38:19 -08002365 if (visible) {
2366 return show(sc);
2367 } else {
2368 return hide(sc);
2369 }
2370 }
2371
2372 /**
Ana Krulecfea97172019-11-02 23:11:02 +01002373 * This information is passed to SurfaceFlinger to decide which window should have a
2374 * priority when deciding about the refresh rate of the display. All windows have the
2375 * lowest priority by default.
2376 * @hide
2377 */
2378 @NonNull
2379 public Transaction setFrameRateSelectionPriority(@NonNull SurfaceControl sc, int priority) {
2380 sc.checkNotReleased();
2381 nativeSetFrameRateSelectionPriority(mNativeObject, sc.mNativeObject, priority);
2382 return this;
2383 }
2384
2385 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002386 * Request that a given surface and it's sub-tree be shown.
2387 *
2388 * @param sc The surface to show.
2389 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002390 * @hide
2391 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002392 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002393 public Transaction show(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002394 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002395 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_HIDDEN);
2396 return this;
2397 }
2398
Robert Carra7827f72019-01-11 12:53:37 -08002399 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002400 * Request that a given surface and it's sub-tree be hidden.
2401 *
2402 * @param sc The surface to hidden.
2403 * @return This transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002404 * @hide
2405 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002406 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002407 public Transaction hide(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002408 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002409 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_HIDDEN, SURFACE_HIDDEN);
2410 return this;
2411 }
2412
Robert Carra7827f72019-01-11 12:53:37 -08002413 /**
2414 * @hide
2415 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002416 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002417 public Transaction setPosition(SurfaceControl sc, float x, float y) {
Robert Carrfee0b822019-12-17 23:56:44 -08002418 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002419 nativeSetPosition(mNativeObject, sc.mNativeObject, x, y);
2420 return this;
2421 }
2422
Robert Carra7827f72019-01-11 12:53:37 -08002423 /**
Robert Carr29fe58a2019-03-11 15:25:16 -07002424 * Set the default buffer size for the SurfaceControl, if there is a
2425 * {@link Surface} associated with the control, then
Robert Carr76907ee2019-01-11 13:38:19 -08002426 * this will be the default size for buffers dequeued from it.
2427 * @param sc The surface to set the buffer size for.
2428 * @param w The default width
2429 * @param h The default height
2430 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002431 */
Robert Carr76907ee2019-01-11 13:38:19 -08002432 @NonNull
2433 public Transaction setBufferSize(@NonNull SurfaceControl sc,
2434 @IntRange(from = 0) int w, @IntRange(from = 0) int h) {
Robert Carrfee0b822019-12-17 23:56:44 -08002435 checkPreconditions(sc);
Jorim Jaggia5e10572017-11-15 14:36:26 +01002436 mResizedSurfaces.put(sc, new Point(w, h));
Jorim Jaggidfc27372017-10-27 17:47:49 +02002437 nativeSetSize(mNativeObject, sc.mNativeObject, w, h);
Robert Carre13b58e2017-08-31 14:50:44 -07002438 return this;
2439 }
2440
Robert Carra7827f72019-01-11 12:53:37 -08002441 /**
Vishnu Naireb53e522020-05-08 18:03:12 -07002442 * Provide the graphic producer a transform hint if the layer and its children are
2443 * in an orientation different from the display's orientation. The caller is responsible
2444 * for clearing this transform hint if the layer is no longer in a fixed orientation.
2445 *
2446 * The transform hint is used to prevent allocating a buffer of different size when a
2447 * layer is rotated. The producer can choose to consume the hint and allocate the buffer
2448 * with the same size.
2449 *
2450 * @return This Transaction.
2451 * @hide
2452 */
2453 @NonNull
2454 public Transaction setFixedTransformHint(@NonNull SurfaceControl sc,
2455 @Surface.Rotation int transformHint) {
2456 checkPreconditions(sc);
2457 nativeSetFixedTransformHint(mNativeObject, sc.mNativeObject, transformHint);
2458 return this;
2459 }
2460
2461 /**
2462 * Clearing any transform hint if set on this layer.
2463 *
2464 * @return This Transaction.
2465 * @hide
2466 */
2467 @NonNull
2468 public Transaction unsetFixedTransformHint(@NonNull SurfaceControl sc) {
2469 checkPreconditions(sc);
2470 nativeSetFixedTransformHint(mNativeObject, sc.mNativeObject, -1/* INVALID_ROTATION */);
2471 return this;
2472 }
2473
2474 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002475 * Set the Z-order for a given SurfaceControl, relative to it's siblings.
2476 * If two siblings share the same Z order the ordering is undefined. Surfaces
2477 * with a negative Z will be placed below the parent surface.
2478 *
2479 * @param sc The SurfaceControl to set the Z order on
2480 * @param z The Z-order
2481 * @return This Transaction.
Robert Carra7827f72019-01-11 12:53:37 -08002482 */
Robert Carr76907ee2019-01-11 13:38:19 -08002483 @NonNull
2484 public Transaction setLayer(@NonNull SurfaceControl sc,
2485 @IntRange(from = Integer.MIN_VALUE, to = Integer.MAX_VALUE) int z) {
Robert Carrfee0b822019-12-17 23:56:44 -08002486 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002487 nativeSetLayer(mNativeObject, sc.mNativeObject, z);
2488 return this;
2489 }
2490
Robert Carra7827f72019-01-11 12:53:37 -08002491 /**
2492 * @hide
2493 */
Robert Carr77e34942017-10-18 19:13:56 -07002494 public Transaction setRelativeLayer(SurfaceControl sc, SurfaceControl relativeTo, int z) {
Robert Carrfee0b822019-12-17 23:56:44 -08002495 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002496 nativeSetRelativeLayer(mNativeObject, sc.mNativeObject, relativeTo.mNativeObject, z);
Robert Carre13b58e2017-08-31 14:50:44 -07002497 return this;
2498 }
2499
Robert Carra7827f72019-01-11 12:53:37 -08002500 /**
2501 * @hide
2502 */
Robert Carre13b58e2017-08-31 14:50:44 -07002503 public Transaction setTransparentRegionHint(SurfaceControl sc, Region transparentRegion) {
Robert Carrfee0b822019-12-17 23:56:44 -08002504 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002505 nativeSetTransparentRegionHint(mNativeObject,
2506 sc.mNativeObject, transparentRegion);
2507 return this;
2508 }
2509
Robert Carra7827f72019-01-11 12:53:37 -08002510 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002511 * Set the alpha for a given surface. If the alpha is non-zero the SurfaceControl
2512 * will be blended with the Surfaces under it according to the specified ratio.
2513 *
2514 * @param sc The given SurfaceControl.
2515 * @param alpha The alpha to set.
Robert Carra7827f72019-01-11 12:53:37 -08002516 */
Robert Carr76907ee2019-01-11 13:38:19 -08002517 @NonNull
2518 public Transaction setAlpha(@NonNull SurfaceControl sc,
2519 @FloatRange(from = 0.0, to = 1.0) float alpha) {
Robert Carrfee0b822019-12-17 23:56:44 -08002520 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002521 nativeSetAlpha(mNativeObject, sc.mNativeObject, alpha);
2522 return this;
2523 }
2524
Robert Carra7827f72019-01-11 12:53:37 -08002525 /**
2526 * @hide
2527 */
Robert Carr788f5742018-07-30 17:46:45 -07002528 public Transaction setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle) {
Robert Carrfee0b822019-12-17 23:56:44 -08002529 checkPreconditions(sc);
Robert Carr788f5742018-07-30 17:46:45 -07002530 nativeSetInputWindowInfo(mNativeObject, sc.mNativeObject, handle);
2531 return this;
2532 }
2533
chaviw59f532e2018-12-26 15:34:59 -08002534 /**
chaviw319cd0782019-02-14 11:00:23 -08002535 * Waits until any changes to input windows have been sent from SurfaceFlinger to
2536 * InputFlinger before returning.
2537 *
2538 * @hide
2539 */
2540 public Transaction syncInputWindows() {
2541 nativeSyncInputWindows(mNativeObject);
2542 return this;
2543 }
2544
2545 /**
Robert Carr76907ee2019-01-11 13:38:19 -08002546 * Specify how the buffer assosciated with this Surface is mapped in to the
2547 * parent coordinate space. The source frame will be scaled to fit the destination
2548 * frame, after being rotated according to the orientation parameter.
2549 *
2550 * @param sc The SurfaceControl to specify the geometry of
2551 * @param sourceCrop The source rectangle in buffer space. Or null for the entire buffer.
2552 * @param destFrame The destination rectangle in parent space. Or null for the source frame.
2553 * @param orientation The buffer rotation
2554 * @return This transaction object.
2555 */
2556 @NonNull
2557 public Transaction setGeometry(@NonNull SurfaceControl sc, @Nullable Rect sourceCrop,
2558 @Nullable Rect destFrame, @Surface.Rotation int orientation) {
Robert Carrfee0b822019-12-17 23:56:44 -08002559 checkPreconditions(sc);
Robert Carr76907ee2019-01-11 13:38:19 -08002560 nativeSetGeometry(mNativeObject, sc.mNativeObject, sourceCrop, destFrame, orientation);
2561 return this;
2562 }
2563
2564 /**
Robert Carra7827f72019-01-11 12:53:37 -08002565 * @hide
2566 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002567 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002568 public Transaction setMatrix(SurfaceControl sc,
2569 float dsdx, float dtdx, float dtdy, float dsdy) {
Robert Carrfee0b822019-12-17 23:56:44 -08002570 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002571 nativeSetMatrix(mNativeObject, sc.mNativeObject,
2572 dsdx, dtdx, dtdy, dsdy);
2573 return this;
2574 }
2575
Robert Carra7827f72019-01-11 12:53:37 -08002576 /**
chaviw619da692019-06-10 15:39:40 -07002577 * Sets the transform and position of a {@link SurfaceControl} from a 3x3 transformation
2578 * matrix.
2579 *
2580 * @param sc SurfaceControl to set matrix of
2581 * @param matrix The matrix to apply.
2582 * @param float9 An array of 9 floats to be used to extract the values from the matrix.
Robert Carra7827f72019-01-11 12:53:37 -08002583 * @hide
2584 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002585 @UnsupportedAppUsage
Jorim Jaggi21c39a72017-10-20 15:47:51 +02002586 public Transaction setMatrix(SurfaceControl sc, Matrix matrix, float[] float9) {
2587 matrix.getValues(float9);
2588 setMatrix(sc, float9[MSCALE_X], float9[MSKEW_Y],
2589 float9[MSKEW_X], float9[MSCALE_Y]);
2590 setPosition(sc, float9[MTRANS_X], float9[MTRANS_Y]);
2591 return this;
2592 }
2593
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002594 /**
2595 * Sets the color transform for the Surface.
chaviw619da692019-06-10 15:39:40 -07002596 *
2597 * @param sc SurfaceControl to set color transform of
2598 * @param matrix A float array with 9 values represents a 3x3 transform matrix
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002599 * @param translation A float array with 3 values represents a translation vector
Robert Carra7827f72019-01-11 12:53:37 -08002600 * @hide
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002601 */
2602 public Transaction setColorTransform(SurfaceControl sc, @Size(9) float[] matrix,
2603 @Size(3) float[] translation) {
Robert Carrfee0b822019-12-17 23:56:44 -08002604 checkPreconditions(sc);
Peiyong Lin52bb6b42018-10-01 11:40:50 -07002605 nativeSetColorTransform(mNativeObject, sc.mNativeObject, matrix, translation);
2606 return this;
2607 }
2608
Robert Carra7827f72019-01-11 12:53:37 -08002609 /**
Peiyong Linf4f0f642019-03-01 14:36:05 -08002610 * Sets the Surface to be color space agnostic. If a surface is color space agnostic,
2611 * the color can be interpreted in any color space.
2612 * @param agnostic A boolean to indicate whether the surface is color space agnostic
2613 * @hide
2614 */
2615 public Transaction setColorSpaceAgnostic(SurfaceControl sc, boolean agnostic) {
Robert Carrfee0b822019-12-17 23:56:44 -08002616 checkPreconditions(sc);
Peiyong Linf4f0f642019-03-01 14:36:05 -08002617 nativeSetColorSpaceAgnostic(mNativeObject, sc.mNativeObject, agnostic);
2618 return this;
2619 }
2620
2621 /**
chaviw619da692019-06-10 15:39:40 -07002622 * Bounds the surface and its children to the bounds specified. Size of the surface will be
2623 * ignored and only the crop and buffer size will be used to determine the bounds of the
2624 * surface. If no crop is specified and the surface has no buffer, the surface bounds is
2625 * only constrained by the size of its parent bounds.
2626 *
2627 * @param sc SurfaceControl to set crop of.
2628 * @param crop Bounds of the crop to apply.
Robert Carra7827f72019-01-11 12:53:37 -08002629 * @hide
2630 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002631 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002632 public Transaction setWindowCrop(SurfaceControl sc, Rect crop) {
Robert Carrfee0b822019-12-17 23:56:44 -08002633 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002634 if (crop != null) {
2635 nativeSetWindowCrop(mNativeObject, sc.mNativeObject,
2636 crop.left, crop.top, crop.right, crop.bottom);
2637 } else {
2638 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, 0, 0);
2639 }
2640
2641 return this;
2642 }
2643
Robert Carra7827f72019-01-11 12:53:37 -08002644 /**
chaviw619da692019-06-10 15:39:40 -07002645 * Same as {@link Transaction#setWindowCrop(SurfaceControl, Rect)} but sets the crop rect
2646 * top left at 0, 0.
2647 *
2648 * @param sc SurfaceControl to set crop of.
2649 * @param width width of crop rect
2650 * @param height height of crop rect
Robert Carra7827f72019-01-11 12:53:37 -08002651 * @hide
2652 */
Vishnu Naird454442d2018-11-13 13:51:01 -08002653 public Transaction setWindowCrop(SurfaceControl sc, int width, int height) {
Robert Carrfee0b822019-12-17 23:56:44 -08002654 checkPreconditions(sc);
Vishnu Naird454442d2018-11-13 13:51:01 -08002655 nativeSetWindowCrop(mNativeObject, sc.mNativeObject, 0, 0, width, height);
2656 return this;
2657 }
2658
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002659 /**
2660 * Sets the corner radius of a {@link SurfaceControl}.
2661 * @param sc SurfaceControl
2662 * @param cornerRadius Corner radius in pixels.
2663 * @return Itself.
Robert Carra7827f72019-01-11 12:53:37 -08002664 * @hide
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002665 */
2666 @UnsupportedAppUsage
2667 public Transaction setCornerRadius(SurfaceControl sc, float cornerRadius) {
Robert Carrfee0b822019-12-17 23:56:44 -08002668 checkPreconditions(sc);
Lucas Dupinff9d6ab2018-10-16 18:05:50 -07002669 nativeSetCornerRadius(mNativeObject, sc.mNativeObject, cornerRadius);
2670
2671 return this;
2672 }
2673
Robert Carra7827f72019-01-11 12:53:37 -08002674 /**
Lucas Dupin991415e2019-11-25 17:48:58 -08002675 * Sets the background blur radius of the {@link SurfaceControl}.
2676 *
2677 * @param sc SurfaceControl.
2678 * @param radius Blur radius in pixels.
2679 * @return itself.
2680 * @hide
2681 */
2682 public Transaction setBackgroundBlurRadius(SurfaceControl sc, int radius) {
2683 checkPreconditions(sc);
2684 nativeSetBackgroundBlurRadius(mNativeObject, sc.mNativeObject, radius);
2685 return this;
2686 }
2687
2688 /**
Robert Carra7827f72019-01-11 12:53:37 -08002689 * @hide
2690 */
Mathew Inwood679c15e2019-02-06 15:36:04 +00002691 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.O)
Robert Carre13b58e2017-08-31 14:50:44 -07002692 public Transaction setLayerStack(SurfaceControl sc, int layerStack) {
Robert Carrfee0b822019-12-17 23:56:44 -08002693 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002694 nativeSetLayerStack(mNativeObject, sc.mNativeObject, layerStack);
2695 return this;
2696 }
2697
Robert Carra7827f72019-01-11 12:53:37 -08002698 /**
2699 * @hide
2700 */
Robert Carr76907ee2019-01-11 13:38:19 -08002701 @UnsupportedAppUsage
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002702 public Transaction deferTransactionUntil(SurfaceControl sc, SurfaceControl barrier,
Jorim Jaggidfc27372017-10-27 17:47:49 +02002703 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002704 if (frameNumber < 0) {
2705 return this;
2706 }
Robert Carrfee0b822019-12-17 23:56:44 -08002707 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002708 nativeDeferTransactionUntil(mNativeObject, sc.mNativeObject, barrier.mNativeObject,
2709 frameNumber);
Robert Carre13b58e2017-08-31 14:50:44 -07002710 return this;
2711 }
2712
Robert Carra7827f72019-01-11 12:53:37 -08002713 /**
2714 * @hide
2715 */
Robert Carrcef9a282020-01-14 09:08:05 -08002716 @Deprecated
Robert Carr76907ee2019-01-11 13:38:19 -08002717 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002718 public Transaction deferTransactionUntilSurface(SurfaceControl sc, Surface barrierSurface,
2719 long frameNumber) {
Robert Carr024378c2018-02-27 11:21:05 -08002720 if (frameNumber < 0) {
2721 return this;
2722 }
Robert Carrfee0b822019-12-17 23:56:44 -08002723 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002724 nativeDeferTransactionUntilSurface(mNativeObject, sc.mNativeObject,
2725 barrierSurface.mNativeObject, frameNumber);
2726 return this;
2727 }
2728
Robert Carra7827f72019-01-11 12:53:37 -08002729 /**
2730 * @hide
2731 */
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002732 public Transaction reparentChildren(SurfaceControl sc, SurfaceControl newParent) {
Robert Carrfee0b822019-12-17 23:56:44 -08002733 checkPreconditions(sc);
Vishnu Nairbc9beab2019-06-25 17:28:58 -07002734 nativeReparentChildren(mNativeObject, sc.mNativeObject, newParent.mNativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -07002735 return this;
2736 }
2737
Robert Carr10584fa2019-01-14 15:55:19 -08002738 /**
2739 * Re-parents a given layer to a new parent. Children inherit transform (position, scaling)
2740 * crop, visibility, and Z-ordering from their parents, as if the children were pixels within the
2741 * parent Surface.
2742 *
2743 * @param sc The SurfaceControl to reparent
2744 * @param newParent The new parent for the given control.
2745 * @return This Transaction
Robert Carra7827f72019-01-11 12:53:37 -08002746 */
Robert Carr76907ee2019-01-11 13:38:19 -08002747 @NonNull
2748 public Transaction reparent(@NonNull SurfaceControl sc,
2749 @Nullable SurfaceControl newParent) {
Robert Carrfee0b822019-12-17 23:56:44 -08002750 checkPreconditions(sc);
Robert Carr10584fa2019-01-14 15:55:19 -08002751 long otherObject = 0;
2752 if (newParent != null) {
2753 newParent.checkNotReleased();
2754 otherObject = newParent.mNativeObject;
2755 }
2756 nativeReparent(mNativeObject, sc.mNativeObject, otherObject);
Svet Ganova5b49902020-06-18 00:47:22 -07002757 mReparentedSurfaces.put(sc, newParent);
Robert Carre13b58e2017-08-31 14:50:44 -07002758 return this;
2759 }
2760
Robert Carra7827f72019-01-11 12:53:37 -08002761 /**
2762 * @hide
2763 */
Robert Carre13b58e2017-08-31 14:50:44 -07002764 public Transaction detachChildren(SurfaceControl sc) {
Robert Carrfee0b822019-12-17 23:56:44 -08002765 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002766 nativeSeverChildren(mNativeObject, sc.mNativeObject);
2767 return this;
2768 }
2769
Robert Carra7827f72019-01-11 12:53:37 -08002770 /**
2771 * @hide
2772 */
Robert Carre13b58e2017-08-31 14:50:44 -07002773 public Transaction setOverrideScalingMode(SurfaceControl sc, int overrideScalingMode) {
Robert Carrfee0b822019-12-17 23:56:44 -08002774 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002775 nativeSetOverrideScalingMode(mNativeObject, sc.mNativeObject,
2776 overrideScalingMode);
2777 return this;
2778 }
2779
2780 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002781 * Fills the surface with the specified color.
2782 * @param color A float array with three values to represent r, g, b in range [0..1]. An
2783 * invalid color will remove the color fill.
Robert Carra7827f72019-01-11 12:53:37 -08002784 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002785 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002786 @UnsupportedAppUsage
Robert Carre13b58e2017-08-31 14:50:44 -07002787 public Transaction setColor(SurfaceControl sc, @Size(3) float[] color) {
Robert Carrfee0b822019-12-17 23:56:44 -08002788 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002789 nativeSetColor(mNativeObject, sc.mNativeObject, color);
2790 return this;
2791 }
2792
2793 /**
Vishnu Nairfd6fb672020-02-14 12:56:37 -08002794 * Removes color fill.
2795 * @hide
2796 */
2797 public Transaction unsetColor(SurfaceControl sc) {
2798 checkPreconditions(sc);
2799 nativeSetColor(mNativeObject, sc.mNativeObject, INVALID_COLOR);
2800 return this;
2801 }
2802
2803 /**
Robert Carre13b58e2017-08-31 14:50:44 -07002804 * Sets the security of the surface. Setting the flag is equivalent to creating the
2805 * Surface with the {@link #SECURE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002806 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002807 */
Robert Carrb1579c82017-09-05 14:54:47 -07002808 public Transaction setSecure(SurfaceControl sc, boolean isSecure) {
Robert Carrfee0b822019-12-17 23:56:44 -08002809 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002810 if (isSecure) {
2811 nativeSetFlags(mNativeObject, sc.mNativeObject, SECURE, SECURE);
2812 } else {
2813 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SECURE);
2814 }
2815 return this;
2816 }
2817
2818 /**
2819 * Sets the opacity of the surface. Setting the flag is equivalent to creating the
2820 * Surface with the {@link #OPAQUE} flag.
Robert Carra7827f72019-01-11 12:53:37 -08002821 * @hide
Robert Carre13b58e2017-08-31 14:50:44 -07002822 */
2823 public Transaction setOpaque(SurfaceControl sc, boolean isOpaque) {
Robert Carrfee0b822019-12-17 23:56:44 -08002824 checkPreconditions(sc);
Robert Carre13b58e2017-08-31 14:50:44 -07002825 if (isOpaque) {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002826 nativeSetFlags(mNativeObject, sc.mNativeObject, SURFACE_OPAQUE, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002827 } else {
Robert Carr2b8a5e12017-10-18 18:46:27 -07002828 nativeSetFlags(mNativeObject, sc.mNativeObject, 0, SURFACE_OPAQUE);
Robert Carre13b58e2017-08-31 14:50:44 -07002829 }
2830 return this;
2831 }
2832
Robert Carra7827f72019-01-11 12:53:37 -08002833 /**
2834 * @hide
2835 */
Robert Carre13b58e2017-08-31 14:50:44 -07002836 public Transaction setDisplaySurface(IBinder displayToken, Surface surface) {
2837 if (displayToken == null) {
2838 throw new IllegalArgumentException("displayToken must not be null");
2839 }
2840
2841 if (surface != null) {
2842 synchronized (surface.mLock) {
2843 nativeSetDisplaySurface(mNativeObject, displayToken, surface.mNativeObject);
2844 }
2845 } else {
2846 nativeSetDisplaySurface(mNativeObject, displayToken, 0);
2847 }
2848 return this;
2849 }
2850
Robert Carra7827f72019-01-11 12:53:37 -08002851 /**
2852 * @hide
2853 */
Robert Carre13b58e2017-08-31 14:50:44 -07002854 public Transaction setDisplayLayerStack(IBinder displayToken, int layerStack) {
2855 if (displayToken == null) {
2856 throw new IllegalArgumentException("displayToken must not be null");
2857 }
2858 nativeSetDisplayLayerStack(mNativeObject, displayToken, layerStack);
2859 return this;
2860 }
2861
Robert Carra7827f72019-01-11 12:53:37 -08002862 /**
2863 * @hide
2864 */
Robert Carre13b58e2017-08-31 14:50:44 -07002865 public Transaction setDisplayProjection(IBinder displayToken,
2866 int orientation, Rect layerStackRect, Rect displayRect) {
2867 if (displayToken == null) {
2868 throw new IllegalArgumentException("displayToken must not be null");
2869 }
2870 if (layerStackRect == null) {
2871 throw new IllegalArgumentException("layerStackRect must not be null");
2872 }
2873 if (displayRect == null) {
2874 throw new IllegalArgumentException("displayRect must not be null");
2875 }
2876 nativeSetDisplayProjection(mNativeObject, displayToken, orientation,
2877 layerStackRect.left, layerStackRect.top, layerStackRect.right, layerStackRect.bottom,
2878 displayRect.left, displayRect.top, displayRect.right, displayRect.bottom);
2879 return this;
2880 }
2881
Robert Carra7827f72019-01-11 12:53:37 -08002882 /**
2883 * @hide
2884 */
Robert Carre13b58e2017-08-31 14:50:44 -07002885 public Transaction setDisplaySize(IBinder displayToken, int width, int height) {
2886 if (displayToken == null) {
2887 throw new IllegalArgumentException("displayToken must not be null");
2888 }
2889 if (width <= 0 || height <= 0) {
2890 throw new IllegalArgumentException("width and height must be positive");
2891 }
2892
2893 nativeSetDisplaySize(mNativeObject, displayToken, width, height);
2894 return this;
2895 }
2896
Vishnu Nair629df2b2019-06-11 16:03:38 -07002897 /** flag the transaction as an animation
Robert Carra7827f72019-01-11 12:53:37 -08002898 * @hide
2899 */
Robert Carre13b58e2017-08-31 14:50:44 -07002900 public Transaction setAnimationTransaction() {
2901 nativeSetAnimationTransaction(mNativeObject);
2902 return this;
2903 }
Robert Carrb1579c82017-09-05 14:54:47 -07002904
2905 /**
Vishnu Nair2ed39d82020-06-17 15:43:13 -07002906 * @deprecated use {@link Transaction#setEarlyWakeupStart()}
2907 *
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002908 * Indicate that SurfaceFlinger should wake up earlier than usual as a result of this
2909 * transaction. This should be used when the caller thinks that the scene is complex enough
2910 * that it's likely to hit GL composition, and thus, SurfaceFlinger needs to more time in
2911 * order not to miss frame deadlines.
2912 * <p>
2913 * Corresponds to setting ISurfaceComposer::eEarlyWakeup
Robert Carra7827f72019-01-11 12:53:37 -08002914 * @hide
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002915 */
Vishnu Nair2ed39d82020-06-17 15:43:13 -07002916 @Deprecated
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002917 public Transaction setEarlyWakeup() {
2918 nativeSetEarlyWakeup(mNativeObject);
2919 return this;
2920 }
2921
Vishnu Nair2ed39d82020-06-17 15:43:13 -07002922 /**
2923 * Provides a hint to SurfaceFlinger to change its offset so that SurfaceFlinger wakes up
2924 * earlier to compose surfaces. The caller should use this as a hint to SurfaceFlinger
2925 * when the scene is complex enough to use GPU composition. The hint will remain active
2926 * until until the client calls {@link Transaction#setEarlyWakeupEnd}.
2927 *
2928 * @hide
2929 */
2930 public Transaction setEarlyWakeupStart() {
2931 nativeSetEarlyWakeupStart(mNativeObject);
2932 return this;
2933 }
2934
2935 /**
2936 * Removes the early wake up hint set by {@link Transaction#setEarlyWakeupStart}.
2937 *
2938 * @hide
2939 */
2940 public Transaction setEarlyWakeupEnd() {
2941 nativeSetEarlyWakeupEnd(mNativeObject);
2942 return this;
2943 }
2944
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01002945 /**
Evan Rosky485df202018-12-06 14:11:12 -08002946 * Sets an arbitrary piece of metadata on the surface. This is a helper for int data.
2947 * @hide
2948 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002949 public Transaction setMetadata(SurfaceControl sc, int key, int data) {
Evan Rosky485df202018-12-06 14:11:12 -08002950 Parcel parcel = Parcel.obtain();
2951 parcel.writeInt(data);
2952 try {
Evan Roskyb51e2462019-04-03 19:27:18 -07002953 setMetadata(sc, key, parcel);
Evan Rosky485df202018-12-06 14:11:12 -08002954 } finally {
2955 parcel.recycle();
2956 }
2957 return this;
2958 }
2959
2960 /**
2961 * Sets an arbitrary piece of metadata on the surface.
2962 * @hide
2963 */
Evan Roskyb51e2462019-04-03 19:27:18 -07002964 public Transaction setMetadata(SurfaceControl sc, int key, Parcel data) {
Robert Carrfee0b822019-12-17 23:56:44 -08002965 checkPreconditions(sc);
Evan Roskyb51e2462019-04-03 19:27:18 -07002966 nativeSetMetadata(mNativeObject, sc.mNativeObject, key, data);
Evan Rosky485df202018-12-06 14:11:12 -08002967 return this;
2968 }
2969
Vishnu Naird87984d2019-11-06 14:43:22 -08002970 /**
2971 * Draws shadows of length {@code shadowRadius} around the surface {@link SurfaceControl}.
2972 * If the length is 0.0f then the shadows will not be drawn.
2973 *
2974 * Shadows are drawn around the screen bounds, these are the post transformed cropped
2975 * bounds. They can draw over their parent bounds and will be occluded by layers with a
2976 * higher z-order. The shadows will respect the surface's corner radius if the
2977 * rounded corner bounds (transformed source bounds) are within the screen bounds.
2978 *
2979 * A shadow will only be drawn on buffer and color layers. If the radius is applied on a
2980 * container layer, it will be passed down the hierarchy to be applied on buffer and color
2981 * layers but not its children. A scenario where this is useful is when SystemUI animates
2982 * a task by controlling a leash to it, can draw a shadow around the app surface by
2983 * setting a shadow on the leash. This is similar to how rounded corners are set.
2984 *
2985 * @hide
2986 */
2987 public Transaction setShadowRadius(SurfaceControl sc, float shadowRadius) {
Robert Carrfee0b822019-12-17 23:56:44 -08002988 checkPreconditions(sc);
Vishnu Naird87984d2019-11-06 14:43:22 -08002989 nativeSetShadowRadius(mNativeObject, sc.mNativeObject, shadowRadius);
2990 return this;
2991 }
2992
Evan Rosky485df202018-12-06 14:11:12 -08002993 /**
Steven Thomas6cf051e2020-01-14 11:37:21 -08002994 * Sets the intended frame rate for the surface {@link SurfaceControl}.
Steven Thomas4528b332020-03-26 21:27:54 -07002995 * <p>
Steven Thomas6cf051e2020-01-14 11:37:21 -08002996 * On devices that are capable of running the display at different refresh rates, the system
2997 * may choose a display refresh rate to better match this surface's frame rate. Usage of
2998 * this API won't directly affect the application's frame production pipeline. However,
2999 * because the system may change the display refresh rate, calls to this function may result
3000 * in changes to Choreographer callback timings, and changes to the time interval at which
3001 * the system releases buffers back to the application.
3002 *
3003 * @param sc The SurfaceControl to specify the frame rate of.
Steven Thomasadd6be12020-01-23 16:35:32 -08003004 * @param frameRate The intended frame rate for this surface, in frames per second. 0 is a
3005 * special value that indicates the app will accept the system's choice for
3006 * the display frame rate, which is the default behavior if this function
Steven Thomas4528b332020-03-26 21:27:54 -07003007 * isn't called. The frameRate param does <em>not</em> need to be a valid
3008 * refresh rate for this device's display - e.g., it's fine to pass 30fps
3009 * to a device that can only run the display at 60fps.
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08003010 * @param compatibility The frame rate compatibility of this surface. The compatibility
3011 * value may influence the system's choice of display frame rate. See
3012 * the Surface.FRAME_RATE_COMPATIBILITY_* values for more info.
Steven Thomas6cf051e2020-01-14 11:37:21 -08003013 * @return This transaction object.
3014 */
3015 @NonNull
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08003016 public Transaction setFrameRate(@NonNull SurfaceControl sc,
3017 @FloatRange(from = 0.0) float frameRate,
3018 @Surface.FrameRateCompatibility int compatibility) {
Steven Thomas6cf051e2020-01-14 11:37:21 -08003019 checkPreconditions(sc);
Steven Thomasdd7bf2f2020-01-31 18:50:02 -08003020 nativeSetFrameRate(mNativeObject, sc.mNativeObject, frameRate, compatibility);
Steven Thomas6cf051e2020-01-14 11:37:21 -08003021 return this;
3022 }
3023
3024 /**
Robert Carrb1579c82017-09-05 14:54:47 -07003025 * Merge the other transaction into this transaction, clearing the
3026 * other transaction as if it had been applied.
Robert Carr76907ee2019-01-11 13:38:19 -08003027 *
3028 * @param other The transaction to merge in to this one.
3029 * @return This transaction.
Robert Carrb1579c82017-09-05 14:54:47 -07003030 */
Robert Carr76907ee2019-01-11 13:38:19 -08003031 @NonNull
3032 public Transaction merge(@NonNull Transaction other) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003033 if (this == other) {
3034 return this;
3035 }
Jorim Jaggia5e10572017-11-15 14:36:26 +01003036 mResizedSurfaces.putAll(other.mResizedSurfaces);
3037 other.mResizedSurfaces.clear();
Svet Ganova5b49902020-06-18 00:47:22 -07003038 mReparentedSurfaces.putAll(other.mReparentedSurfaces);
3039 other.mReparentedSurfaces.clear();
Robert Carrb1579c82017-09-05 14:54:47 -07003040 nativeMergeTransaction(mNativeObject, other.mNativeObject);
3041 return this;
3042 }
Robert Carr71200f22019-02-05 09:44:53 -08003043
3044 /**
3045 * Equivalent to reparent with a null parent, in that it removes
3046 * the SurfaceControl from the scene, but it also releases
3047 * the local resources (by calling {@link SurfaceControl#release})
3048 * after this method returns, {@link SurfaceControl#isValid} will return
3049 * false for the argument.
3050 *
3051 * @param sc The surface to remove and release.
3052 * @return This transaction
3053 * @hide
3054 */
3055 @NonNull
3056 public Transaction remove(@NonNull SurfaceControl sc) {
3057 reparent(sc, null);
3058 sc.release();
3059 return this;
3060 }
Vishnu Nair629df2b2019-06-11 16:03:38 -07003061
Vishnu Nairf7645aa2019-06-18 11:14:01 -07003062 /**
3063 * Writes the transaction to parcel, clearing the transaction as if it had been applied so
3064 * it can be used to store future transactions. It's the responsibility of the parcel
3065 * reader to apply the original transaction.
3066 *
3067 * @param dest parcel to write the transaction to
3068 * @param flags
3069 */
Vishnu Nair629df2b2019-06-11 16:03:38 -07003070 @Override
3071 public void writeToParcel(@NonNull Parcel dest, @WriteFlags int flags) {
3072 if (mNativeObject == 0) {
3073 dest.writeInt(0);
3074 } else {
3075 dest.writeInt(1);
3076 }
3077 nativeWriteTransactionToParcel(mNativeObject, dest);
3078 }
3079
3080 private void readFromParcel(Parcel in) {
3081 mNativeObject = 0;
3082 if (in.readInt() != 0) {
3083 mNativeObject = nativeReadTransactionFromParcel(in);
3084 mFreeNativeResources = sRegistry.registerNativeAllocation(this, mNativeObject);
3085 }
3086 }
3087
3088 @Override
3089 public int describeContents() {
3090 return 0;
3091 }
3092
3093 public static final @NonNull Creator<Transaction> CREATOR = new Creator<Transaction>() {
3094 @Override
3095 public Transaction createFromParcel(Parcel in) {
3096 return new Transaction(in);
3097 }
3098 @Override
3099 public Transaction[] newArray(int size) {
3100 return new Transaction[size];
3101 }
3102 };
Robert Carre13b58e2017-08-31 14:50:44 -07003103 }
Robert Carrfee0b822019-12-17 23:56:44 -08003104
3105 /**
3106 * A debugging utility subclass of SurfaceControl.Transaction. At construction
3107 * you can pass in a monitor object, and all the other methods will throw an exception
3108 * if the monitor is not held when they are called.
3109 * @hide
3110 */
3111 public static class LockDebuggingTransaction extends SurfaceControl.Transaction {
3112 Object mMonitor;
3113
3114 public LockDebuggingTransaction(Object o) {
3115 mMonitor = o;
3116 }
3117
3118 @Override
3119 protected void checkPreconditions(SurfaceControl sc) {
3120 super.checkPreconditions(sc);
3121 if (!Thread.holdsLock(mMonitor)) {
3122 throw new RuntimeException(
3123 "Unlocked access to synchronized SurfaceControl.Transaction");
3124 }
3125 }
3126 }
Steven Thomas6ec6fbc2020-03-24 16:06:33 -07003127
3128 /**
3129 * Acquire a frame rate flexibility token, which allows surface flinger to freely switch display
3130 * frame rates. This is used by CTS tests to put the device in a consistent state. See
Steven Thomas6faeaa22020-04-07 21:26:38 -07003131 * ISurfaceComposer::acquireFrameRateFlexibilityToken(). The caller must have the
3132 * ACCESS_SURFACE_FLINGER permission, or else the call will fail, returning 0.
Steven Thomas6ec6fbc2020-03-24 16:06:33 -07003133 * @hide
3134 */
3135 @TestApi
3136 public static long acquireFrameRateFlexibilityToken() {
3137 return nativeAcquireFrameRateFlexibilityToken();
3138 }
3139
3140 /**
3141 * Release a frame rate flexibility token.
3142 * @hide
3143 */
3144 @TestApi
3145 public static void releaseFrameRateFlexibilityToken(long token) {
3146 nativeReleaseFrameRateFlexibilityToken(token);
3147 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -08003148}