1 /* <lambda>null2 * 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.Point 19 import com.android.server.wm.flicker.traces.layers.LayersTraceSubject.Companion.assertThat 20 import com.android.test.SurfaceViewBufferTestBase.Companion.ScalingMode 21 import com.android.test.SurfaceViewBufferTestBase.Companion.Transform 22 import junit.framework.Assert.assertEquals 23 import org.junit.Test 24 import org.junit.runner.RunWith 25 import org.junit.runners.Parameterized 26 27 @RunWith(Parameterized::class) 28 class BufferRejectionTests(useBlastAdapter: Boolean) : SurfaceTracingTestBase(useBlastAdapter) { 29 @Test 30 fun testSetBuffersGeometry_0x0_rejectsBuffer() { 31 val trace = withTrace { activity -> 32 activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, 100, 100, 33 R8G8B8A8_UNORM) 34 activity.mSurfaceProxy.ANativeWindowLock() 35 activity.mSurfaceProxy.ANativeWindowUnlockAndPost() 36 activity.mSurfaceProxy.ANativeWindowLock() 37 activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, 0, 0, 38 R8G8B8A8_UNORM) 39 // Submit buffer one with a different size which should be rejected 40 activity.mSurfaceProxy.ANativeWindowUnlockAndPost() 41 42 // submit a buffer with the default buffer size 43 activity.mSurfaceProxy.ANativeWindowLock() 44 activity.mSurfaceProxy.ANativeWindowUnlockAndPost() 45 activity.mSurfaceProxy.waitUntilBufferDisplayed(3, 500 /* ms */) 46 } 47 // Verify we reject buffers since scaling mode == NATIVE_WINDOW_SCALING_MODE_FREEZE 48 assertThat(trace).layer("SurfaceView", 2).doesNotExist() 49 50 // Verify the next buffer is submitted with the correct size 51 assertThat(trace).layer("SurfaceView", 3).also { 52 it.hasBufferSize(defaultBufferSize) 53 // scaling mode is not passed down to the layer for blast 54 if (useBlastAdapter) { 55 it.hasScalingMode(ScalingMode.SCALE_TO_WINDOW.ordinal) 56 } else { 57 it.hasScalingMode(ScalingMode.FREEZE.ordinal) 58 } 59 } 60 } 61 62 @Test 63 fun testSetBufferScalingMode_freeze() { 64 val bufferSize = Point(300, 200) 65 val trace = withTrace { activity -> 66 activity.drawFrame() 67 assertEquals(activity.mSurfaceProxy.waitUntilBufferDisplayed(1, 500 /* ms */), 0) 68 activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, bufferSize, 69 R8G8B8A8_UNORM) 70 assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(0, 1000 /* ms */)) 71 assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(1, 1000 /* ms */)) 72 // Change buffer size and set scaling mode to freeze 73 activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, Point(0, 0), 74 R8G8B8A8_UNORM) 75 76 // first dequeued buffer does not have the new size so it should be rejected. 77 activity.mSurfaceProxy.SurfaceQueueBuffer(0) 78 activity.mSurfaceProxy.SurfaceSetScalingMode(ScalingMode.SCALE_TO_WINDOW) 79 activity.mSurfaceProxy.SurfaceQueueBuffer(1) 80 assertEquals(activity.mSurfaceProxy.waitUntilBufferDisplayed(3, 500 /* ms */), 0) 81 } 82 83 // verify buffer size is reset to default buffer size 84 assertThat(trace).layer("SurfaceView", 1).hasBufferSize(defaultBufferSize) 85 assertThat(trace).layer("SurfaceView", 2).doesNotExist() 86 assertThat(trace).layer("SurfaceView", 3).hasBufferSize(bufferSize) 87 } 88 89 @Test 90 fun testSetBufferScalingMode_freeze_withBufferRotation() { 91 val rotatedBufferSize = Point(defaultBufferSize.y, defaultBufferSize.x) 92 val trace = withTrace { activity -> 93 activity.drawFrame() 94 assertEquals(activity.mSurfaceProxy.waitUntilBufferDisplayed(1, 500 /* ms */), 0) 95 activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, 96 rotatedBufferSize, R8G8B8A8_UNORM) 97 assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(0, 1000 /* ms */)) 98 assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(1, 1000 /* ms */)) 99 // Change buffer size and set scaling mode to freeze 100 activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, Point(0, 0), 101 R8G8B8A8_UNORM) 102 103 // first dequeued buffer does not have the new size so it should be rejected. 104 activity.mSurfaceProxy.SurfaceQueueBuffer(0) 105 // add a buffer transform so the buffer size is correct. 106 activity.mSurfaceProxy.ANativeWindowSetBuffersTransform(Transform.ROT_90) 107 activity.mSurfaceProxy.SurfaceQueueBuffer(1) 108 assertEquals(activity.mSurfaceProxy.waitUntilBufferDisplayed(3, 500 /* ms */), 0) 109 } 110 111 // verify buffer size is reset to default buffer size 112 assertThat(trace).layer("SurfaceView", 1).hasBufferSize(defaultBufferSize) 113 assertThat(trace).layer("SurfaceView", 2).doesNotExist() 114 assertThat(trace).layer("SurfaceView", 3).hasBufferSize(rotatedBufferSize) 115 assertThat(trace).layer("SurfaceView", 3).hasBufferOrientation(Transform.ROT_90.value) 116 } 117 118 @Test 119 fun testRejectedBuffersAreReleased() { 120 val bufferSize = Point(300, 200) 121 val trace = withTrace { activity -> 122 for (count in 0 until 5) { 123 activity.drawFrame() 124 assertEquals(activity.mSurfaceProxy.waitUntilBufferDisplayed((count * 3) + 1L, 125 500 /* ms */), 0) 126 activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, 127 bufferSize, R8G8B8A8_UNORM) 128 assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(0, 1000 /* ms */)) 129 assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(1, 1000 /* ms */)) 130 // Change buffer size and set scaling mode to freeze 131 activity.mSurfaceProxy.ANativeWindowSetBuffersGeometry(activity.surface!!, 132 Point(0, 0), R8G8B8A8_UNORM) 133 134 // first dequeued buffer does not have the new size so it should be rejected. 135 activity.mSurfaceProxy.SurfaceQueueBuffer(0) 136 activity.mSurfaceProxy.SurfaceSetScalingMode(ScalingMode.SCALE_TO_WINDOW) 137 activity.mSurfaceProxy.SurfaceQueueBuffer(1) 138 assertEquals(activity.mSurfaceProxy.waitUntilBufferDisplayed((count * 3) + 3L, 139 500 /* ms */), 0) 140 } 141 } 142 143 for (count in 0 until 5) { 144 assertThat(trace).layer("SurfaceView", (count * 3) + 1L) 145 .hasBufferSize(defaultBufferSize) 146 assertThat(trace).layer("SurfaceView", (count * 3) + 2L) 147 .doesNotExist() 148 assertThat(trace).layer("SurfaceView", (count * 3) + 3L) 149 .hasBufferSize(bufferSize) 150 } 151 } 152 }