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);
33     }
34 
createSurfaceTransaction()35     public static long createSurfaceTransaction() {
36         long surfaceTransaction = nSurfaceTransaction_create();
37         assertTrue("failed to create surface transaction", surfaceTransaction != 0);
38         return surfaceTransaction;
39     }
40 
reparent(long surfaceControl, long newParentSurfaceControl)41     public static void reparent(long surfaceControl, long newParentSurfaceControl) {
42         long surfaceTransaction = createSurfaceTransaction();
43         nSurfaceTransaction_reparent(surfaceControl, newParentSurfaceControl, surfaceTransaction);
44         applyAndDeleteSurfaceTransaction(surfaceTransaction);
45     }
46 
applyAndDeleteSurfaceTransaction(long surfaceTransaction)47     public static void applyAndDeleteSurfaceTransaction(long surfaceTransaction) {
48         nSurfaceTransaction_apply(surfaceTransaction);
49         nSurfaceTransaction_delete(surfaceTransaction);
50     }
51 
setVisibility(long surfaceControl, boolean visible)52     public static void setVisibility(long surfaceControl, boolean visible) {
53         long surfaceTransaction = createSurfaceTransaction();
54         nSurfaceTransaction_setVisibility(surfaceControl, surfaceTransaction, visible);
55         applyAndDeleteSurfaceTransaction(surfaceTransaction);
56     }
57 
setBufferOpaque(long surfaceControl, boolean opaque)58     public static void setBufferOpaque(long surfaceControl, boolean opaque) {
59         long surfaceTransaction = createSurfaceTransaction();
60         nSurfaceTransaction_setBufferOpaque(surfaceControl, surfaceTransaction, opaque);
61         applyAndDeleteSurfaceTransaction(surfaceTransaction);
62     }
63 
setGeometry(long surfaceControl, int srcLeft, int srcTop, int srcRight, int srcBottom, int dstLeft, int dstTop, int dstRight, int dstBottom, int transform)64     public static void setGeometry(long surfaceControl, int srcLeft, int srcTop, int srcRight,
65             int srcBottom, int dstLeft, int dstTop, int dstRight, int dstBottom,
66             int transform) {
67         long surfaceTransaction = createSurfaceTransaction();
68         nSurfaceTransaction_setGeometry(surfaceControl, surfaceTransaction, srcLeft, srcTop,
69                 srcRight, srcBottom,
70                 dstLeft, dstTop, dstRight, dstBottom, transform);
71         applyAndDeleteSurfaceTransaction(surfaceTransaction);
72     }
73 
setZOrder(long surfaceControl, int z)74     public static void setZOrder(long surfaceControl, int z) {
75         long surfaceTransaction = createSurfaceTransaction();
76         nSurfaceTransaction_setZOrder(surfaceControl, surfaceTransaction, z);
77         applyAndDeleteSurfaceTransaction(surfaceTransaction);
78     }
79 
setBufferAlpha(long surfaceControl, double alpha)80     public static void setBufferAlpha(long surfaceControl, double alpha) {
81         long surfaceTransaction = createSurfaceTransaction();
82         nSurfaceTransaction_setBufferAlpha(surfaceControl, surfaceTransaction, alpha);
83         applyAndDeleteSurfaceTransaction(surfaceTransaction);
84     }
85 
setColor(long surfaceControl, float red, float green, float blue, float alpha)86     public static void setColor(long surfaceControl, float red, float green, float blue,
87             float alpha) {
88         long surfaceTransaction = createSurfaceTransaction();
89         nSurfaceTransaction_setColor(surfaceControl, surfaceTransaction, red, green, blue, alpha);
90         applyAndDeleteSurfaceTransaction(surfaceTransaction);
91     }
92 
setPosition(long surfaceControl, int x, int y)93     public static void setPosition(long surfaceControl, int x, int y) {
94         long surfaceTransaction = createSurfaceTransaction();
95         nSurfaceTransaction_setPosition(surfaceControl, surfaceTransaction, x, y);
96         applyAndDeleteSurfaceTransaction(surfaceTransaction);
97     }
98 
setScale(long surfaceControl, float xScale, float yScale)99     public static void setScale(long surfaceControl, float xScale, float yScale) {
100         long surfaceTransaction = createSurfaceTransaction();
101         nSurfaceTransaction_setScale(surfaceControl, surfaceTransaction, xScale, yScale);
102         applyAndDeleteSurfaceTransaction(surfaceTransaction);
103     }
104 
setBufferTransform(long surfaceControl, int bufferTransform)105     public static void setBufferTransform(long surfaceControl, int bufferTransform) {
106         long surfaceTransaction = createSurfaceTransaction();
107         nSurfaceTransaction_setBufferTransform(surfaceControl, surfaceTransaction,
108                 bufferTransform);
109         applyAndDeleteSurfaceTransaction(surfaceTransaction);
110     }
111 
setCrop(long surfaceControl, Rect crop)112     public static void setCrop(long surfaceControl, Rect crop) {
113         long surfaceTransaction = createSurfaceTransaction();
114         nSurfaceTransaction_setCrop(surfaceControl, surfaceTransaction, crop.left, crop.top,
115                 crop.right, crop.bottom);
116         applyAndDeleteSurfaceTransaction(surfaceTransaction);
117     }
118 
setExtendedRangeBrightness(long surfaceControl, float currentRatio, float desiredRatio)119     public static void setExtendedRangeBrightness(long surfaceControl, float currentRatio,
120             float desiredRatio) {
121         long surfaceTransaction = createSurfaceTransaction();
122         nSurfaceTransaction_setExtendedRangeBrightness(surfaceControl, surfaceTransaction,
123                 currentRatio, desiredRatio);
124         applyAndDeleteSurfaceTransaction(surfaceTransaction);
125     }
126 
127     /** Dispatches setDesiredHdrHeadroom to the NDK */
setDesiredHdrHeadroom(long surfaceControl, float desiredRatio)128     public static void setDesiredHdrHeadroom(long surfaceControl, float desiredRatio) {
129         long surfaceTransaction = createSurfaceTransaction();
130         nSurfaceTransaction_setDesiredHdrHeadroom(surfaceControl, surfaceTransaction,
131                 desiredRatio);
132         applyAndDeleteSurfaceTransaction(surfaceTransaction);
133     }
134 
135     ///////////////////////////////////////////////////////////////////////////
136     // Native function prototypes
137     ///////////////////////////////////////////////////////////////////////////
138 
nSurfaceTransaction_create()139     public static native long nSurfaceTransaction_create();
nSurfaceTransaction_delete(long surfaceTransaction)140     public static native void nSurfaceTransaction_delete(long surfaceTransaction);
nSurfaceTransaction_fromJava( SurfaceControl.Transaction transaction)141     public static native long nSurfaceTransaction_fromJava(
142             SurfaceControl.Transaction transaction);
nSurfaceTransaction_apply(long surfaceTransaction)143     public static native void nSurfaceTransaction_apply(long surfaceTransaction);
nSurfaceControl_createFromWindow(Surface surface)144     public static native long nSurfaceControl_createFromWindow(Surface surface);
nSurfaceControl_create(long surfaceControl)145     public static native long nSurfaceControl_create(long surfaceControl);
nSurfaceControl_acquire(long surfaceControl)146     public static native void nSurfaceControl_acquire(long surfaceControl);
nSurfaceControl_release(long surfaceControl)147     public static native void nSurfaceControl_release(long surfaceControl);
nSurfaceControl_fromJava( SurfaceControl surfaceControl)148     public static native long nSurfaceControl_fromJava(
149             SurfaceControl surfaceControl);
nSurfaceTransaction_setSolidBuffer( long surfaceControl, long surfaceTransaction, int width, int height, int color)150     public static native long nSurfaceTransaction_setSolidBuffer(
151             long surfaceControl, long surfaceTransaction, int width, int height, int color);
nSurfaceTransaction_setBuffer(long surfaceControl, long surfaceTransaction, long buffer)152     public static native void nSurfaceTransaction_setBuffer(long surfaceControl,
153             long surfaceTransaction, long buffer);
nSurfaceTransaction_setQuadrantBuffer(long surfaceControl, long surfaceTransaction, int width, int height, int colorTopLeft, int colorTopRight, int colorBottomRight, int colorBottomLeft)154     public static native long nSurfaceTransaction_setQuadrantBuffer(long surfaceControl,
155             long surfaceTransaction, int width, int height, int colorTopLeft, int colorTopRight,
156             int colorBottomRight, int colorBottomLeft);
nSurfaceTransaction_releaseBuffer(long buffer)157     public static native void nSurfaceTransaction_releaseBuffer(long buffer);
nSurfaceTransaction_setVisibility( long surfaceControl, long surfaceTransaction, boolean show)158     public static native void nSurfaceTransaction_setVisibility(
159             long surfaceControl, long surfaceTransaction, boolean show);
nSurfaceTransaction_setBufferOpaque( long surfaceControl, long surfaceTransaction, boolean opaque)160     public static native void nSurfaceTransaction_setBufferOpaque(
161             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)162     public static native void nSurfaceTransaction_setGeometry(
163             long surfaceControl, long surfaceTransaction, int srcRight, int srcTop, int srcLeft,
164             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)165     public static native void nSurfaceTransaction_setCrop(long surfaceControl,
166             long surfaceTransaction, int left, int top, int right, int bottom);
nSurfaceTransaction_setPosition(long surfaceControl, long surfaceTransaction, int left, int top)167     public static native void nSurfaceTransaction_setPosition(long surfaceControl,
168             long surfaceTransaction, int left, int top);
nSurfaceTransaction_setBufferTransform( long surfaceControl, long surfaceTransaction, int transform)169     public static native void nSurfaceTransaction_setBufferTransform(
170             long surfaceControl, long surfaceTransaction, int transform);
nSurfaceTransaction_setScale(long surfaceControl, long surfaceTransaction, float xScale, float yScale)171     public static native void nSurfaceTransaction_setScale(long surfaceControl,
172             long surfaceTransaction, float xScale, float yScale);
nSurfaceTransaction_setDamageRegion( long surfaceControl, long surfaceTransaction, int right, int top, int left, int bottom)173     public static native void nSurfaceTransaction_setDamageRegion(
174             long surfaceControl, long surfaceTransaction, int right, int top, int left, int bottom);
nSurfaceTransaction_setZOrder( long surfaceControl, long surfaceTransaction, int z)175     public static native void nSurfaceTransaction_setZOrder(
176             long surfaceControl, long surfaceTransaction, int z);
nSurfaceTransaction_setDesiredPresentTime(long surfaceTransaction, long desiredPresentTimeOffset)177     public static native long nSurfaceTransaction_setDesiredPresentTime(long surfaceTransaction,
178             long desiredPresentTimeOffset);
nSurfaceTransaction_setBufferAlpha(long surfaceControl, long surfaceTransaction, double alpha)179     public static native void nSurfaceTransaction_setBufferAlpha(long surfaceControl,
180             long surfaceTransaction, double alpha);
nSurfaceTransaction_reparent(long surfaceControl, long newParentSurfaceControl, long surfaceTransaction)181     public static native void nSurfaceTransaction_reparent(long surfaceControl,
182             long newParentSurfaceControl, long surfaceTransaction);
nSurfaceTransaction_setColor(long surfaceControl, long surfaceTransaction, float r, float g, float b, float alpha)183     public static native void nSurfaceTransaction_setColor(long surfaceControl,
184             long surfaceTransaction, float r, float g, float b, float alpha);
nSurfaceTransaction_setEnableBackPressure(long surfaceControl, long surfaceTransaction, boolean enableBackPressure)185     public static native void nSurfaceTransaction_setEnableBackPressure(long surfaceControl,
186             long surfaceTransaction, boolean enableBackPressure);
nSurfaceTransaction_setOnCompleteCallback(long surfaceTransaction, boolean waitForFence, TransactionCompleteListener listener)187     public static native void nSurfaceTransaction_setOnCompleteCallback(long surfaceTransaction,
188             boolean waitForFence, TransactionCompleteListener listener);
nSurfaceTransaction_setOnCommitCallback(long surfaceTransaction, TransactionCompleteListener listener)189     public static native void nSurfaceTransaction_setOnCommitCallback(long surfaceTransaction,
190             TransactionCompleteListener listener);
nSurfaceTransaction_setOnCompleteCallbackWithoutContext( long surfaceTransaction, boolean waitForFence, TransactionCompleteListener listener)191     public static native void nSurfaceTransaction_setOnCompleteCallbackWithoutContext(
192             long surfaceTransaction, boolean waitForFence, TransactionCompleteListener listener);
nSurfaceTransaction_setOnCommitCallbackWithoutContext( long surfaceTransaction, TransactionCompleteListener listener)193     public static native void nSurfaceTransaction_setOnCommitCallbackWithoutContext(
194             long surfaceTransaction, TransactionCompleteListener listener);
nSurfaceTransaction_setFrameTimeline(long surfaceTransaction, long vsyncId)195     public static native void nSurfaceTransaction_setFrameTimeline(long surfaceTransaction,
196             long vsyncId);
nSurfaceTransaction_setExtendedRangeBrightness( long surfaceControl, long surfaceTransaction, float currentRatio, float desiredRatio)197     public static native void nSurfaceTransaction_setExtendedRangeBrightness(
198             long surfaceControl, long surfaceTransaction, float currentRatio, float desiredRatio);
199     /** Dispatches to the NDK */
nSurfaceTransaction_setDesiredHdrHeadroom( long surfaceControl, long surfaceTransaction, float desiredRatio)200     public static native void nSurfaceTransaction_setDesiredHdrHeadroom(
201             long surfaceControl, long surfaceTransaction, float desiredRatio);
nSurfaceTransaction_setDataSpace( long surfaceControl, long surfaceTransaction, int dataspace)202     public static native void nSurfaceTransaction_setDataSpace(
203             long surfaceControl, long surfaceTransaction, int dataspace);
204 
getSolidBuffer(int width, int height, int color)205     public static native HardwareBuffer getSolidBuffer(int width, int height, int color);
getQuadrantBuffer(int width, int height, int colorTopLeft, int colorTopRight, int colorBottomRight, int colorBottomLeft)206     public static native HardwareBuffer getQuadrantBuffer(int width, int height,
207             int colorTopLeft, int colorTopRight, int colorBottomRight, int colorBottomLeft);
getBufferId(HardwareBuffer buffer)208     public static native long getBufferId(HardwareBuffer buffer);
209 }
210