xref: /aosp_15_r20/cts/tests/surfacecontrol/src/android/view/cts/util/ASurfaceControlTestUtils.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2021 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 
17 package android.view.cts.util;
18 
19 import static org.junit.Assert.assertTrue;
20 
21 import android.graphics.Rect;
22 import android.hardware.HardwareBuffer;
23 import android.view.Surface;
24 import android.view.SurfaceControl;
25 
26 public class ASurfaceControlTestUtils {
27     static {
28         System.loadLibrary("ctssurfacecontrol_jni");
29     }
30 
31     public interface TransactionCompleteListener {
onTransactionComplete(long latchTime, long presentTime)32         void onTransactionComplete(long latchTime, long presentTime);
shouldQueryTransactionStats()33         default long shouldQueryTransactionStats() {
34                 return 0;
35         }
onTransactionStatsRead(boolean surfaceControlFound, boolean releaseFenceQueried, boolean acquireTimeQueried)36         default void onTransactionStatsRead(boolean surfaceControlFound,
37                 boolean releaseFenceQueried, boolean acquireTimeQueried) {
38         }
39     }
40 
41     public interface BufferReleaseCallback {
onBufferRelease()42         void onBufferRelease();
43     }
44 
createSurfaceTransaction()45     public static long createSurfaceTransaction() {
46         long surfaceTransaction = nSurfaceTransaction_create();
47         assertTrue("failed to create surface transaction", surfaceTransaction != 0);
48         return surfaceTransaction;
49     }
50 
reparent(long surfaceControl, long newParentSurfaceControl)51     public static void reparent(long surfaceControl, long newParentSurfaceControl) {
52         long surfaceTransaction = createSurfaceTransaction();
53         nSurfaceTransaction_reparent(surfaceControl, newParentSurfaceControl, surfaceTransaction);
54         applyAndDeleteSurfaceTransaction(surfaceTransaction);
55     }
56 
applyAndDeleteSurfaceTransaction(long surfaceTransaction)57     public static void applyAndDeleteSurfaceTransaction(long surfaceTransaction) {
58         nSurfaceTransaction_apply(surfaceTransaction);
59         nSurfaceTransaction_delete(surfaceTransaction);
60     }
61 
setVisibility(long surfaceControl, boolean visible)62     public static void setVisibility(long surfaceControl, boolean visible) {
63         long surfaceTransaction = createSurfaceTransaction();
64         nSurfaceTransaction_setVisibility(surfaceControl, surfaceTransaction, visible);
65         applyAndDeleteSurfaceTransaction(surfaceTransaction);
66     }
67 
setBufferOpaque(long surfaceControl, boolean opaque)68     public static void setBufferOpaque(long surfaceControl, boolean opaque) {
69         long surfaceTransaction = createSurfaceTransaction();
70         nSurfaceTransaction_setBufferOpaque(surfaceControl, surfaceTransaction, opaque);
71         applyAndDeleteSurfaceTransaction(surfaceTransaction);
72     }
73 
setGeometry(long surfaceControl, int srcLeft, int srcTop, int srcRight, int srcBottom, int dstLeft, int dstTop, int dstRight, int dstBottom, int transform)74     public static void setGeometry(long surfaceControl, int srcLeft, int srcTop, int srcRight,
75             int srcBottom, int dstLeft, int dstTop, int dstRight, int dstBottom,
76             int transform) {
77         long surfaceTransaction = createSurfaceTransaction();
78         nSurfaceTransaction_setGeometry(surfaceControl, surfaceTransaction, srcLeft, srcTop,
79                 srcRight, srcBottom,
80                 dstLeft, dstTop, dstRight, dstBottom, transform);
81         applyAndDeleteSurfaceTransaction(surfaceTransaction);
82     }
83 
setZOrder(long surfaceControl, int z)84     public static void setZOrder(long surfaceControl, int z) {
85         long surfaceTransaction = createSurfaceTransaction();
86         nSurfaceTransaction_setZOrder(surfaceControl, surfaceTransaction, z);
87         applyAndDeleteSurfaceTransaction(surfaceTransaction);
88     }
89 
setBufferAlpha(long surfaceControl, double alpha)90     public static void setBufferAlpha(long surfaceControl, double alpha) {
91         long surfaceTransaction = createSurfaceTransaction();
92         nSurfaceTransaction_setBufferAlpha(surfaceControl, surfaceTransaction, alpha);
93         applyAndDeleteSurfaceTransaction(surfaceTransaction);
94     }
95 
setColor(long surfaceControl, float red, float green, float blue, float alpha)96     public static void setColor(long surfaceControl, float red, float green, float blue,
97             float alpha) {
98         long surfaceTransaction = createSurfaceTransaction();
99         nSurfaceTransaction_setColor(surfaceControl, surfaceTransaction, red, green, blue, alpha);
100         applyAndDeleteSurfaceTransaction(surfaceTransaction);
101     }
102 
setPosition(long surfaceControl, int x, int y)103     public static void setPosition(long surfaceControl, int x, int y) {
104         long surfaceTransaction = createSurfaceTransaction();
105         nSurfaceTransaction_setPosition(surfaceControl, surfaceTransaction, x, y);
106         applyAndDeleteSurfaceTransaction(surfaceTransaction);
107     }
108 
setScale(long surfaceControl, float xScale, float yScale)109     public static void setScale(long surfaceControl, float xScale, float yScale) {
110         long surfaceTransaction = createSurfaceTransaction();
111         nSurfaceTransaction_setScale(surfaceControl, surfaceTransaction, xScale, yScale);
112         applyAndDeleteSurfaceTransaction(surfaceTransaction);
113     }
114 
setBufferTransform(long surfaceControl, int bufferTransform)115     public static void setBufferTransform(long surfaceControl, int bufferTransform) {
116         long surfaceTransaction = createSurfaceTransaction();
117         nSurfaceTransaction_setBufferTransform(surfaceControl, surfaceTransaction,
118                 bufferTransform);
119         applyAndDeleteSurfaceTransaction(surfaceTransaction);
120     }
121 
setCrop(long surfaceControl, Rect crop)122     public static void setCrop(long surfaceControl, Rect crop) {
123         long surfaceTransaction = createSurfaceTransaction();
124         nSurfaceTransaction_setCrop(surfaceControl, surfaceTransaction, crop.left, crop.top,
125                 crop.right, crop.bottom);
126         applyAndDeleteSurfaceTransaction(surfaceTransaction);
127     }
128 
setExtendedRangeBrightness(long surfaceControl, float currentRatio, float desiredRatio)129     public static void setExtendedRangeBrightness(long surfaceControl, float currentRatio,
130             float desiredRatio) {
131         long surfaceTransaction = createSurfaceTransaction();
132         nSurfaceTransaction_setExtendedRangeBrightness(surfaceControl, surfaceTransaction,
133                 currentRatio, desiredRatio);
134         applyAndDeleteSurfaceTransaction(surfaceTransaction);
135     }
136 
137     /** Dispatches setDesiredHdrHeadroom to the NDK */
setDesiredHdrHeadroom(long surfaceControl, float desiredRatio)138     public static void setDesiredHdrHeadroom(long surfaceControl, float desiredRatio) {
139         long surfaceTransaction = createSurfaceTransaction();
140         nSurfaceTransaction_setDesiredHdrHeadroom(surfaceControl, surfaceTransaction,
141                 desiredRatio);
142         applyAndDeleteSurfaceTransaction(surfaceTransaction);
143     }
144 
145     ///////////////////////////////////////////////////////////////////////////
146     // Native function prototypes
147     ///////////////////////////////////////////////////////////////////////////
148 
nSurfaceTransaction_create()149     public static native long nSurfaceTransaction_create();
nSurfaceTransaction_delete(long surfaceTransaction)150     public static native void nSurfaceTransaction_delete(long surfaceTransaction);
nSurfaceTransaction_fromJava( SurfaceControl.Transaction transaction)151     public static native long nSurfaceTransaction_fromJava(
152             SurfaceControl.Transaction transaction);
nSurfaceTransaction_apply(long surfaceTransaction)153     public static native void nSurfaceTransaction_apply(long surfaceTransaction);
nSurfaceControl_createFromWindow(Surface surface)154     public static native long nSurfaceControl_createFromWindow(Surface surface);
nSurfaceControl_create(long surfaceControl)155     public static native long nSurfaceControl_create(long surfaceControl);
nSurfaceControl_acquire(long surfaceControl)156     public static native void nSurfaceControl_acquire(long surfaceControl);
nSurfaceControl_release(long surfaceControl)157     public static native void nSurfaceControl_release(long surfaceControl);
nSurfaceControl_fromJava( SurfaceControl surfaceControl)158     public static native long nSurfaceControl_fromJava(
159             SurfaceControl surfaceControl);
nSurfaceTransaction_setSolidBuffer( long surfaceControl, long surfaceTransaction, int width, int height, int color)160     public static native long nSurfaceTransaction_setSolidBuffer(
161             long surfaceControl, long surfaceTransaction, int width, int height, int color);
162 
nSurfaceTransaction_setSolidBufferWithRelease( long surfaceControl, long surfaceTransaction, int width, int height, int color, BufferReleaseCallback callback)163   public static native long nSurfaceTransaction_setSolidBufferWithRelease(
164       long surfaceControl,
165       long surfaceTransaction,
166       int width,
167       int height,
168       int color,
169       BufferReleaseCallback callback);
170 
nSurfaceTransaction_setBuffer(long surfaceControl, long surfaceTransaction, long buffer)171     public static native void nSurfaceTransaction_setBuffer(long surfaceControl,
172             long surfaceTransaction, long buffer);
nSurfaceTransaction_setQuadrantBuffer(long surfaceControl, long surfaceTransaction, int width, int height, int colorTopLeft, int colorTopRight, int colorBottomRight, int colorBottomLeft)173     public static native long nSurfaceTransaction_setQuadrantBuffer(long surfaceControl,
174             long surfaceTransaction, int width, int height, int colorTopLeft, int colorTopRight,
175             int colorBottomRight, int colorBottomLeft);
nSurfaceTransaction_releaseBuffer(long buffer)176     public static native void nSurfaceTransaction_releaseBuffer(long buffer);
nSurfaceTransaction_setVisibility( long surfaceControl, long surfaceTransaction, boolean show)177     public static native void nSurfaceTransaction_setVisibility(
178             long surfaceControl, long surfaceTransaction, boolean show);
nSurfaceTransaction_setBufferOpaque( long surfaceControl, long surfaceTransaction, boolean opaque)179     public static native void nSurfaceTransaction_setBufferOpaque(
180             long surfaceControl, long surfaceTransaction, boolean opaque);
nSurfaceTransaction_setGeometry( long surfaceControl, long surfaceTransaction, int srcRight, int srcTop, int srcLeft, int srcBottom, int dstRight, int dstTop, int dstLeft, int dstBottom, int transform)181     public static native void nSurfaceTransaction_setGeometry(
182             long surfaceControl, long surfaceTransaction, int srcRight, int srcTop, int srcLeft,
183             int srcBottom, int dstRight, int dstTop, int dstLeft, int dstBottom, int transform);
nSurfaceTransaction_setCrop(long surfaceControl, long surfaceTransaction, int left, int top, int right, int bottom)184     public static native void nSurfaceTransaction_setCrop(long surfaceControl,
185             long surfaceTransaction, int left, int top, int right, int bottom);
nSurfaceTransaction_setPosition(long surfaceControl, long surfaceTransaction, int left, int top)186     public static native void nSurfaceTransaction_setPosition(long surfaceControl,
187             long surfaceTransaction, int left, int top);
nSurfaceTransaction_setBufferTransform( long surfaceControl, long surfaceTransaction, int transform)188     public static native void nSurfaceTransaction_setBufferTransform(
189             long surfaceControl, long surfaceTransaction, int transform);
nSurfaceTransaction_setScale(long surfaceControl, long surfaceTransaction, float xScale, float yScale)190     public static native void nSurfaceTransaction_setScale(long surfaceControl,
191             long surfaceTransaction, float xScale, float yScale);
nSurfaceTransaction_setDamageRegion( long surfaceControl, long surfaceTransaction, int right, int top, int left, int bottom)192     public static native void nSurfaceTransaction_setDamageRegion(
193             long surfaceControl, long surfaceTransaction, int right, int top, int left, int bottom);
nSurfaceTransaction_setZOrder( long surfaceControl, long surfaceTransaction, int z)194     public static native void nSurfaceTransaction_setZOrder(
195             long surfaceControl, long surfaceTransaction, int z);
nSurfaceTransaction_setDesiredPresentTime(long surfaceTransaction, long desiredPresentTimeOffset)196     public static native long nSurfaceTransaction_setDesiredPresentTime(long surfaceTransaction,
197             long desiredPresentTimeOffset);
nSurfaceTransaction_setBufferAlpha(long surfaceControl, long surfaceTransaction, double alpha)198     public static native void nSurfaceTransaction_setBufferAlpha(long surfaceControl,
199             long surfaceTransaction, double alpha);
nSurfaceTransaction_reparent(long surfaceControl, long newParentSurfaceControl, long surfaceTransaction)200     public static native void nSurfaceTransaction_reparent(long surfaceControl,
201             long newParentSurfaceControl, long surfaceTransaction);
nSurfaceTransaction_setColor(long surfaceControl, long surfaceTransaction, float r, float g, float b, float alpha)202     public static native void nSurfaceTransaction_setColor(long surfaceControl,
203             long surfaceTransaction, float r, float g, float b, float alpha);
nSurfaceTransaction_setEnableBackPressure( SurfaceControl.Transaction transaction, SurfaceControl surfaceControl, boolean enableBackPressure)204     public static native void nSurfaceTransaction_setEnableBackPressure(
205             SurfaceControl.Transaction transaction,
206             SurfaceControl surfaceControl, boolean enableBackPressure);
nSurfaceTransaction_setOnCompleteCallback(long surfaceTransaction, boolean waitForFence, TransactionCompleteListener listener)207     public static native void nSurfaceTransaction_setOnCompleteCallback(long surfaceTransaction,
208             boolean waitForFence, TransactionCompleteListener listener);
nSurfaceTransaction_setOnCommitCallback(long surfaceTransaction, TransactionCompleteListener listener)209     public static native void nSurfaceTransaction_setOnCommitCallback(long surfaceTransaction,
210             TransactionCompleteListener listener);
nSurfaceTransaction_setOnCompleteCallbackWithoutContext( long surfaceTransaction, boolean waitForFence, TransactionCompleteListener listener)211     public static native void nSurfaceTransaction_setOnCompleteCallbackWithoutContext(
212             long surfaceTransaction, boolean waitForFence, TransactionCompleteListener listener);
nSurfaceTransaction_setOnCommitCallbackWithoutContext( long surfaceTransaction, TransactionCompleteListener listener)213     public static native void nSurfaceTransaction_setOnCommitCallbackWithoutContext(
214             long surfaceTransaction, TransactionCompleteListener listener);
nSurfaceTransaction_setFrameTimeline(long surfaceTransaction, long vsyncId)215     public static native void nSurfaceTransaction_setFrameTimeline(long surfaceTransaction,
216             long vsyncId);
nSurfaceTransaction_setExtendedRangeBrightness( long surfaceControl, long surfaceTransaction, float currentRatio, float desiredRatio)217     public static native void nSurfaceTransaction_setExtendedRangeBrightness(
218             long surfaceControl, long surfaceTransaction, float currentRatio, float desiredRatio);
219     /** Dispatches to the NDK */
nSurfaceTransaction_setDesiredHdrHeadroom( long surfaceControl, long surfaceTransaction, float desiredRatio)220     public static native void nSurfaceTransaction_setDesiredHdrHeadroom(
221             long surfaceControl, long surfaceTransaction, float desiredRatio);
nSurfaceTransaction_setDataSpace( long surfaceControl, long surfaceTransaction, int dataspace)222     public static native void nSurfaceTransaction_setDataSpace(
223             long surfaceControl, long surfaceTransaction, int dataspace);
nSurfaceTransaction_setLuts( long surfaceControl, long surfaceTransaction, float[] buffers, int[] offsets, int[] dimensions, int[] sizes, int[] samplingKeys)224     public static native void nSurfaceTransaction_setLuts(
225             long surfaceControl, long surfaceTransaction, float[] buffers, int[] offsets,
226             int[] dimensions, int[] sizes, int[] samplingKeys);
227 
getSolidBuffer(int width, int height, int color)228     public static native HardwareBuffer getSolidBuffer(int width, int height, int color);
getQuadrantBuffer(int width, int height, int colorTopLeft, int colorTopRight, int colorBottomRight, int colorBottomLeft)229     public static native HardwareBuffer getQuadrantBuffer(int width, int height,
230             int colorTopLeft, int colorTopRight, int colorBottomRight, int colorBottomLeft);
getBufferId(HardwareBuffer buffer)231     public static native long getBufferId(HardwareBuffer buffer);
232 }
233