| 1 |
/* |
| 2 |
* Copyright (C) 2020 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 |
package com.android.test |
| 17 |
|
| 18 |
import android.graphics.Color |
| 19 |
import android.graphics.Point |
| 20 |
import android.graphics.Rect |
| 21 |
import android.os.SystemClock |
| 22 |
import com.android.server.wm.flicker.traces.layers.LayersTraceSubject.Companion.assertThat |
| 23 |
import com.android.test.SurfaceViewBufferTestBase.Companion.ScalingMode |
| 24 |
import com.android.test.SurfaceViewBufferTestBase.Companion.Transform |
| 25 |
import junit.framework.Assert.assertEquals |
| 26 |
import junit.framework.Assert.assertTrue |
| 27 |
import org.junit.Test |
| 28 |
import org.junit.runner.RunWith |
| 29 |
import org.junit.runners.Parameterized |
| 30 |
import java.util.concurrent.CountDownLatch |
| 31 |
import java.util.concurrent.TimeUnit |
| 32 |
|
| 33 |
@RunWith(Parameterized::class) |
| 34 |
class GeometryTests(useBlastAdapter: Boolean) : SurfaceTracingTestBase(useBlastAdapter) { |
| 35 |
@Test |
| 36 |
fun testSetBuffersGeometry_0x0_resetsBufferSize() { |
| 37 |
val trace = withTrace { activity -> |
| 38 |
activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, 0, 0, |
| 39 |
R8G8B8A8_UNORM) |
| 40 |
activity.mSurfaceProxy.ANativeWindowLock() |
| 41 |
activity.mSurfaceProxy.ANativeWindowUnlockAndPost() |
| 42 |
activity.mSurfaceProxy.waitUntilBufferDisplayed(1, 500 /* ms */) |
| 43 |
} |
| 44 |
|
| 45 |
// verify buffer size is reset to default buffer size |
| 46 |
assertThat(trace).layer("SurfaceView", 1).hasBufferSize(defaultBufferSize) |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
fun testSetBuffersGeometry_smallerThanBuffer() { |
| 51 |
val bufferSize = Point(300, 200) |
| 52 |
val trace = withTrace { activity -> |
| 53 |
activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, bufferSize, |
| 54 |
R8G8B8A8_UNORM) |
| 55 |
activity.drawFrame() |
| 56 |
activity.mSurfaceProxy.waitUntilBufferDisplayed(1, 500 /* ms */) |
| 57 |
} |
| 58 |
|
| 59 |
assertThat(trace).layer("SurfaceView", 1).also { |
| 60 |
it.hasBufferSize(bufferSize) |
| 61 |
it.hasLayerSize(defaultBufferSize) |
| 62 |
it.hasScalingMode(ScalingMode.SCALE_TO_WINDOW.ordinal) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
fun testSetBuffersGeometry_largerThanBuffer() { |
| 68 |
val bufferSize = Point(3000, 2000) |
| 69 |
val trace = withTrace { activity -> |
| 70 |
activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, bufferSize, |
| 71 |
R8G8B8A8_UNORM) |
| 72 |
activity.drawFrame() |
| 73 |
activity.mSurfaceProxy.waitUntilBufferDisplayed(1, 500 /* ms */) |
| 74 |
} |
| 75 |
|
| 76 |
assertThat(trace).layer("SurfaceView", 1).also { |
| 77 |
it.hasBufferSize(bufferSize) |
| 78 |
it.hasLayerSize(defaultBufferSize) |
| 79 |
it.hasScalingMode(ScalingMode.SCALE_TO_WINDOW.ordinal) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
fun testSetBufferScalingMode_freeze() { |
| 85 |
val bufferSize = Point(300, 200) |
| 86 |
val trace = withTrace { activity -> |
| 87 |
activity.drawFrame() |
| 88 |
assertEquals(activity.mSurfaceProxy.waitUntilBufferDisplayed(1, 500 /* ms */), 0) |
| 89 |
activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, bufferSize, |
| 90 |
R8G8B8A8_UNORM) |
| 91 |
assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(0, 1000 /* ms */)) |
| 92 |
assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(1, 1000 /* ms */)) |
| 93 |
// Change buffer size and set scaling mode to freeze |
| 94 |
activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, Point(0, 0), |
| 95 |
R8G8B8A8_UNORM) |
| 96 |
|
| 97 |
// first dequeued buffer does not have the new size so it should be rejected. |
| 98 |
activity.mSurfaceProxy.SurfaceQueueBuffer(0) |
| 99 |
activity.mSurfaceProxy.SurfaceSetScalingMode(ScalingMode.SCALE_TO_WINDOW) |
| 100 |
activity.mSurfaceProxy.SurfaceQueueBuffer(1) |
| 101 |
assertEquals(activity.mSurfaceProxy.waitUntilBufferDisplayed(3, 500 /* ms */), 0) |
| 102 |
} |
| 103 |
|
| 104 |
// verify buffer size is reset to default buffer size |
| 105 |
assertThat(trace).layer("SurfaceView", 1).hasBufferSize(defaultBufferSize) |
| 106 |
assertThat(trace).layer("SurfaceView", 2).doesNotExist() |
| 107 |
assertThat(trace).layer("SurfaceView", 3).hasBufferSize(bufferSize) |
| 108 |
} |
| 109 |
|
| 110 |
@Test |
| 111 |
fun testSetBuffersTransform_FLIP() { |
| 112 |
val transforms = arrayOf(Transform.FLIP_H, Transform.FLIP_V, Transform.ROT_180).withIndex() |
| 113 |
for ((index, transform) in transforms) { |
| 114 |
val trace = withTrace { activity -> |
| 115 |
activity.mSurfaceProxy.ANativeWindowSetBuffersTransform(transform) |
| 116 |
activity.mSurfaceProxy.ANativeWindowLock() |
| 117 |
activity.mSurfaceProxy.ANativeWindowUnlockAndPost() |
| 118 |
activity.mSurfaceProxy.waitUntilBufferDisplayed(index + 1L, 500 /* ms */) |
| 119 |
} |
| 120 |
|
| 121 |
assertThat(trace).layer("SurfaceView", index + 1L).also { |
| 122 |
it.hasBufferSize(defaultBufferSize) |
| 123 |
it.hasLayerSize(defaultBufferSize) |
| 124 |
it.hasBufferOrientation(transform.value) |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
@Test |
| 130 |
fun testSurfaceViewResizeImmediatelyWithNonFreezeScaling() { |
| 131 |
val surfaceViewPosition = Rect() |
| 132 |
var trace = withTrace { activity -> |
| 133 |
activity.mSurfaceProxy.SurfaceSetScalingMode(ScalingMode.SCALE_TO_WINDOW) |
| 134 |
assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(0, 1 /* ms */)) |
| 135 |
activity.mSurfaceProxy.drawBuffer(0, Color.BLUE) |
| 136 |
activity.mSurfaceProxy.SurfaceQueueBuffer(0) |
| 137 |
activity.mSurfaceProxy.waitUntilBufferDisplayed(1, 500 /* ms */) |
| 138 |
activity.mSurfaceView!!.getBoundsOnScreen(surfaceViewPosition) |
| 139 |
} |
| 140 |
|
| 141 |
runOnUiThread { |
| 142 |
val svBounds = Rect(0, 0, defaultBufferSize.x, defaultBufferSize.y) |
| 143 |
svBounds.offsetTo(surfaceViewPosition.left, surfaceViewPosition.top) |
| 144 |
checkPixels(svBounds, Color.BLUE) |
| 145 |
} |
| 146 |
|
| 147 |
// check that the layer and buffer starts with the default size |
| 148 |
assertThat(trace).layer("SurfaceView", 1).also { |
| 149 |
it.hasBufferSize(defaultBufferSize) |
| 150 |
it.hasLayerSize(defaultBufferSize) |
| 151 |
} |
| 152 |
val newSize = Point(1280, 960) |
| 153 |
lateinit var resizeCountDownLatch: CountDownLatch |
| 154 |
runOnUiThread { |
| 155 |
resizeCountDownLatch = it.resizeSurfaceView(newSize) |
| 156 |
} |
| 157 |
assertTrue(resizeCountDownLatch.await(1000, TimeUnit.MILLISECONDS)) |
| 158 |
// wait for sf to handle the resize transaction request |
| 159 |
SystemClock.sleep(500) |
| 160 |
trace = withTrace { _ -> |
| 161 |
// take a trace with the new size |
| 162 |
} |
| 163 |
|
| 164 |
// check that the layer size has changed and the buffer is now streched to the new layer |
| 165 |
// size |
| 166 |
runOnUiThread { |
| 167 |
val svBounds = Rect(0, 0, newSize.x, newSize.y) |
| 168 |
svBounds.offsetTo(surfaceViewPosition.left, surfaceViewPosition.top) |
| 169 |
checkPixels(svBounds, Color.BLUE) |
| 170 |
} |
| 171 |
|
| 172 |
assertThat(trace).layer("SurfaceView", 1).also { |
| 173 |
it.hasLayerSize(newSize) |
| 174 |
it.hasBufferSize(defaultBufferSize) |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
fun testSurfaceViewDoesNotResizeWithDefaultScaling() { |
| 180 |
val surfaceViewPosition = Rect() |
| 181 |
var trace = withTrace { activity -> |
| 182 |
assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(0, 1 /* ms */)) |
| 183 |
activity.mSurfaceProxy.drawBuffer(0, Color.BLUE) |
| 184 |
activity.mSurfaceProxy.SurfaceQueueBuffer(0) |
| 185 |
activity.mSurfaceProxy.waitUntilBufferDisplayed(1, 500 /* ms */) |
| 186 |
activity.mSurfaceView!!.getBoundsOnScreen(surfaceViewPosition) |
| 187 |
} |
| 188 |
|
| 189 |
runOnUiThread { |
| 190 |
val svBounds = Rect(0, 0, defaultBufferSize.x, defaultBufferSize.y) |
| 191 |
svBounds.offsetTo(surfaceViewPosition.left, surfaceViewPosition.top) |
| 192 |
checkPixels(svBounds, Color.BLUE) |
| 193 |
} |
| 194 |
|
| 195 |
// check that the layer and buffer starts with the default size |
| 196 |
assertThat(trace).layer("SurfaceView", 1).also { |
| 197 |
it.hasBufferSize(defaultBufferSize) |
| 198 |
it.hasLayerSize(defaultBufferSize) |
| 199 |
} |
| 200 |
val newSize = Point(1280, 960) |
| 201 |
lateinit var resizeCountDownLatch: CountDownLatch |
| 202 |
runOnUiThread { |
| 203 |
resizeCountDownLatch = it.resizeSurfaceView(newSize) |
| 204 |
} |
| 205 |
assertTrue(resizeCountDownLatch.await(1000, TimeUnit.MILLISECONDS)) |
| 206 |
// wait for sf to handle the resize transaction request |
| 207 |
SystemClock.sleep(500) |
| 208 |
trace = withTrace { _ -> |
| 209 |
// take a trace after the size change |
| 210 |
} |
| 211 |
|
| 212 |
// check the layer and buffer remains the same size |
| 213 |
runOnUiThread { |
| 214 |
val svBounds = Rect(0, 0, defaultBufferSize.x, defaultBufferSize.y) |
| 215 |
svBounds.offsetTo(surfaceViewPosition.left, surfaceViewPosition.top) |
| 216 |
checkPixels(svBounds, Color.BLUE) |
| 217 |
} |
| 218 |
|
| 219 |
assertThat(trace).layer("SurfaceView", 1).also { |
| 220 |
it.hasLayerSize(defaultBufferSize) |
| 221 |
it.hasBufferSize(defaultBufferSize) |
| 222 |
} |
| 223 |
} |
| 224 |
} |