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