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.view.Surface; 23 24 public class ASurfaceControlTestUtils { 25 static { 26 System.loadLibrary("ctsview_jni"); 27 } 28 29 public interface TransactionCompleteListener { onTransactionComplete(long latchTime, long presentTime)30 void onTransactionComplete(long latchTime, long presentTime); 31 } 32 createSurfaceTransaction()33 public static long createSurfaceTransaction() { 34 long surfaceTransaction = nSurfaceTransaction_create(); 35 assertTrue("failed to create surface transaction", surfaceTransaction != 0); 36 return surfaceTransaction; 37 } 38 reparent(long surfaceControl, long newParentSurfaceControl)39 public static void reparent(long surfaceControl, long newParentSurfaceControl) { 40 long surfaceTransaction = createSurfaceTransaction(); 41 nSurfaceTransaction_reparent(surfaceControl, newParentSurfaceControl, surfaceTransaction); 42 applyAndDeleteSurfaceTransaction(surfaceTransaction); 43 } 44 applyAndDeleteSurfaceTransaction(long surfaceTransaction)45 public static void applyAndDeleteSurfaceTransaction(long surfaceTransaction) { 46 nSurfaceTransaction_apply(surfaceTransaction); 47 nSurfaceTransaction_delete(surfaceTransaction); 48 } 49 setVisibility(long surfaceControl, boolean visible)50 public static void setVisibility(long surfaceControl, boolean visible) { 51 long surfaceTransaction = createSurfaceTransaction(); 52 nSurfaceTransaction_setVisibility(surfaceControl, surfaceTransaction, visible); 53 applyAndDeleteSurfaceTransaction(surfaceTransaction); 54 } 55 setBufferOpaque(long surfaceControl, boolean opaque)56 public static void setBufferOpaque(long surfaceControl, boolean opaque) { 57 long surfaceTransaction = createSurfaceTransaction(); 58 nSurfaceTransaction_setBufferOpaque(surfaceControl, surfaceTransaction, opaque); 59 applyAndDeleteSurfaceTransaction(surfaceTransaction); 60 } 61 setGeometry(long surfaceControl, int srcLeft, int srcTop, int srcRight, int srcBottom, int dstLeft, int dstTop, int dstRight, int dstBottom, int transform)62 public static void setGeometry(long surfaceControl, int srcLeft, int srcTop, int srcRight, 63 int srcBottom, int dstLeft, int dstTop, int dstRight, int dstBottom, 64 int transform) { 65 long surfaceTransaction = createSurfaceTransaction(); 66 nSurfaceTransaction_setGeometry(surfaceControl, surfaceTransaction, srcLeft, srcTop, 67 srcRight, srcBottom, 68 dstLeft, dstTop, dstRight, dstBottom, transform); 69 applyAndDeleteSurfaceTransaction(surfaceTransaction); 70 } 71 setZOrder(long surfaceControl, int z)72 public static void setZOrder(long surfaceControl, int z) { 73 long surfaceTransaction = createSurfaceTransaction(); 74 nSurfaceTransaction_setZOrder(surfaceControl, surfaceTransaction, z); 75 applyAndDeleteSurfaceTransaction(surfaceTransaction); 76 } 77 setBufferAlpha(long surfaceControl, double alpha)78 public static void setBufferAlpha(long surfaceControl, double alpha) { 79 long surfaceTransaction = createSurfaceTransaction(); 80 nSurfaceTransaction_setBufferAlpha(surfaceControl, surfaceTransaction, alpha); 81 applyAndDeleteSurfaceTransaction(surfaceTransaction); 82 } 83 setColor(long surfaceControl, float red, float green, float blue, float alpha)84 public static void setColor(long surfaceControl, float red, float green, float blue, 85 float alpha) { 86 long surfaceTransaction = createSurfaceTransaction(); 87 nSurfaceTransaction_setColor(surfaceControl, surfaceTransaction, red, green, blue, alpha); 88 applyAndDeleteSurfaceTransaction(surfaceTransaction); 89 } 90 setPosition(long surfaceControl, int x, int y)91 public static void setPosition(long surfaceControl, int x, int y) { 92 long surfaceTransaction = createSurfaceTransaction(); 93 nSurfaceTransaction_setPosition(surfaceControl, surfaceTransaction, x, y); 94 applyAndDeleteSurfaceTransaction(surfaceTransaction); 95 } 96 setScale(long surfaceControl, float xScale, float yScale)97 public static void setScale(long surfaceControl, float xScale, float yScale) { 98 long surfaceTransaction = createSurfaceTransaction(); 99 nSurfaceTransaction_setScale(surfaceControl, surfaceTransaction, xScale, yScale); 100 applyAndDeleteSurfaceTransaction(surfaceTransaction); 101 } 102 setBufferTransform(long surfaceControl, int bufferTransform)103 public static void setBufferTransform(long surfaceControl, int bufferTransform) { 104 long surfaceTransaction = createSurfaceTransaction(); 105 nSurfaceTransaction_setBufferTransform(surfaceControl, surfaceTransaction, 106 bufferTransform); 107 applyAndDeleteSurfaceTransaction(surfaceTransaction); 108 } 109 setCrop(long surfaceControl, Rect crop)110 public static void setCrop(long surfaceControl, Rect crop) { 111 long surfaceTransaction = createSurfaceTransaction(); 112 nSurfaceTransaction_setCrop(surfaceControl, surfaceTransaction, crop.left, crop.top, 113 crop.right, crop.bottom); 114 applyAndDeleteSurfaceTransaction(surfaceTransaction); 115 } 116 117 /////////////////////////////////////////////////////////////////////////// 118 // Native function prototypes 119 /////////////////////////////////////////////////////////////////////////// 120 nSurfaceTransaction_create()121 public static native long nSurfaceTransaction_create(); nSurfaceTransaction_delete(long surfaceTransaction)122 public static native void nSurfaceTransaction_delete(long surfaceTransaction); nSurfaceTransaction_apply(long surfaceTransaction)123 public static native void nSurfaceTransaction_apply(long surfaceTransaction); nSurfaceControl_createFromWindow(Surface surface)124 public static native long nSurfaceControl_createFromWindow(Surface surface); nSurfaceControl_create(long surfaceControl)125 public static native long nSurfaceControl_create(long surfaceControl); nSurfaceControl_acquire(long surfaceControl)126 public static native void nSurfaceControl_acquire(long surfaceControl); nSurfaceControl_release(long surfaceControl)127 public static native void nSurfaceControl_release(long surfaceControl); nSurfaceTransaction_setSolidBuffer( long surfaceControl, long surfaceTransaction, int width, int height, int color)128 public static native long nSurfaceTransaction_setSolidBuffer( 129 long surfaceControl, long surfaceTransaction, int width, int height, int color); nSurfaceTransaction_setBuffer(long surfaceControl, long surfaceTransaction, long buffer)130 public static native void nSurfaceTransaction_setBuffer(long surfaceControl, 131 long surfaceTransaction, long buffer); nSurfaceTransaction_setQuadrantBuffer(long surfaceControl, long surfaceTransaction, int width, int height, int colorTopLeft, int colorTopRight, int colorBottomRight, int colorBottomLeft)132 public static native long nSurfaceTransaction_setQuadrantBuffer(long surfaceControl, 133 long surfaceTransaction, int width, int height, int colorTopLeft, int colorTopRight, 134 int colorBottomRight, int colorBottomLeft); nSurfaceTransaction_releaseBuffer(long buffer)135 public static native void nSurfaceTransaction_releaseBuffer(long buffer); nSurfaceTransaction_setVisibility( long surfaceControl, long surfaceTransaction, boolean show)136 public static native void nSurfaceTransaction_setVisibility( 137 long surfaceControl, long surfaceTransaction, boolean show); nSurfaceTransaction_setBufferOpaque( long surfaceControl, long surfaceTransaction, boolean opaque)138 public static native void nSurfaceTransaction_setBufferOpaque( 139 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)140 public static native void nSurfaceTransaction_setGeometry( 141 long surfaceControl, long surfaceTransaction, int srcRight, int srcTop, int srcLeft, 142 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)143 public static native void nSurfaceTransaction_setCrop(long surfaceControl, 144 long surfaceTransaction, int left, int top, int right, int bottom); nSurfaceTransaction_setPosition(long surfaceControl, long surfaceTransaction, int left, int top)145 public static native void nSurfaceTransaction_setPosition(long surfaceControl, 146 long surfaceTransaction, int left, int top); nSurfaceTransaction_setBufferTransform( long surfaceControl, long surfaceTransaction, int transform)147 public static native void nSurfaceTransaction_setBufferTransform( 148 long surfaceControl, long surfaceTransaction, int transform); nSurfaceTransaction_setScale(long surfaceControl, long surfaceTransaction, float xScale, float yScale)149 public static native void nSurfaceTransaction_setScale(long surfaceControl, 150 long surfaceTransaction, float xScale, float yScale); nSurfaceTransaction_setDamageRegion( long surfaceControl, long surfaceTransaction, int right, int top, int left, int bottom)151 public static native void nSurfaceTransaction_setDamageRegion( 152 long surfaceControl, long surfaceTransaction, int right, int top, int left, int bottom); nSurfaceTransaction_setZOrder( long surfaceControl, long surfaceTransaction, int z)153 public static native void nSurfaceTransaction_setZOrder( 154 long surfaceControl, long surfaceTransaction, int z); nSurfaceTransaction_setDesiredPresentTime(long surfaceTransaction, long desiredPresentTimeOffset)155 public static native long nSurfaceTransaction_setDesiredPresentTime(long surfaceTransaction, 156 long desiredPresentTimeOffset); nSurfaceTransaction_setBufferAlpha(long surfaceControl, long surfaceTransaction, double alpha)157 public static native void nSurfaceTransaction_setBufferAlpha(long surfaceControl, 158 long surfaceTransaction, double alpha); nSurfaceTransaction_reparent(long surfaceControl, long newParentSurfaceControl, long surfaceTransaction)159 public static native void nSurfaceTransaction_reparent(long surfaceControl, 160 long newParentSurfaceControl, long surfaceTransaction); nSurfaceTransaction_setColor(long surfaceControl, long surfaceTransaction, float r, float g, float b, float alpha)161 public static native void nSurfaceTransaction_setColor(long surfaceControl, 162 long surfaceTransaction, float r, float g, float b, float alpha); nSurfaceTransaction_setEnableBackPressure(long surfaceControl, long surfaceTransaction, boolean enableBackPressure)163 public static native void nSurfaceTransaction_setEnableBackPressure(long surfaceControl, 164 long surfaceTransaction, boolean enableBackPressure); nSurfaceTransaction_setOnCompleteCallback(long surfaceTransaction, boolean waitForFence, TransactionCompleteListener listener)165 public static native void nSurfaceTransaction_setOnCompleteCallback(long surfaceTransaction, 166 boolean waitForFence, TransactionCompleteListener listener); nSurfaceTransaction_setOnCommitCallback(long surfaceTransaction, TransactionCompleteListener listener)167 public static native void nSurfaceTransaction_setOnCommitCallback(long surfaceTransaction, 168 TransactionCompleteListener listener); nSurfaceTransaction_setOnCompleteCallbackWithoutContext( long surfaceTransaction, boolean waitForFence, TransactionCompleteListener listener)169 public static native void nSurfaceTransaction_setOnCompleteCallbackWithoutContext( 170 long surfaceTransaction, boolean waitForFence, TransactionCompleteListener listener); nSurfaceTransaction_setOnCommitCallbackWithoutContext( long surfaceTransaction, TransactionCompleteListener listener)171 public static native void nSurfaceTransaction_setOnCommitCallbackWithoutContext( 172 long surfaceTransaction, TransactionCompleteListener listener); 173 } 174