• 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.Color
19 import android.graphics.Rect
20 import com.android.server.wm.flicker.traces.layers.LayersTraceSubject.Companion.assertThat
21 import junit.framework.Assert.assertEquals
22 import org.junit.Test
23 import org.junit.runner.RunWith
24 import org.junit.runners.Parameterized
25 
26 @RunWith(Parameterized::class)
27 class SharedBufferModeTests(useBlastAdapter: Boolean) : SurfaceTracingTestBase(useBlastAdapter) {
28     /** Sanity test to check each buffer is presented if its submitted with enough delay
29      * for SF to present the buffers. */
30     @Test
31     fun testCanPresentBuffers() {
32         val numFrames = 15L
33         val trace = withTrace { activity ->
34             assertEquals(0, activity.mSurfaceProxy.NativeWindowSetSharedBufferMode(true))
35             for (i in 1..numFrames) {
36                 assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(0, 1 /* ms */))
37                 activity.mSurfaceProxy.SurfaceQueueBuffer(0)
38                 assertEquals(0, activity.mSurfaceProxy.waitUntilBufferDisplayed(i, 5000 /* ms */))
39             }
40         }
41 
42         assertThat(trace).hasFrameSequence("SurfaceView", 1..numFrames)
43     }
44 
45     /** Submit buffers as fast as possible testing that we are not blocked when dequeuing the buffer
46      * by setting the dequeue timeout to 1ms and checking that we present the newest buffer. */
47     @Test
48     fun testFastQueueBuffers() {
49         val numFrames = 15L
50         val trace = withTrace { activity ->
51             assertEquals(0, activity.mSurfaceProxy.NativeWindowSetSharedBufferMode(true))
52             for (i in 1..numFrames) {
53                 assertEquals(0, activity.mSurfaceProxy.SurfaceDequeueBuffer(0, 1 /* ms */))
54                 activity.mSurfaceProxy.SurfaceQueueBuffer(0)
55             }
56             assertEquals(0, activity.mSurfaceProxy.waitUntilBufferDisplayed(numFrames,
57                     5000 /* ms */))
58         }
59 
60         assertThat(trace).hasFrameSequence("SurfaceView", numFrames..numFrames)
61     }
62 
63     /** Keep overwriting the buffer without queuing buffers and check that we present the latest
64      * buffer content. */
65     @Test
66     fun testAutoRefresh() {
67         var svBounds = Rect()
68         runOnUiThread {
69             assertEquals(0, it.mSurfaceProxy.NativeWindowSetSharedBufferMode(true))
70             assertEquals(0, it.mSurfaceProxy.NativeWindowSetAutoRefresh(true))
71             assertEquals(0, it.mSurfaceProxy.SurfaceDequeueBuffer(0, 1 /* ms */))
72             it.mSurfaceProxy.SurfaceQueueBuffer(0, false /* freeSlot */)
73             assertEquals(0, it.mSurfaceProxy.waitUntilBufferDisplayed(1, 5000 /* ms */))
74 
75             svBounds = Rect(0, 0, it.mSurfaceView!!.width, it.mSurfaceView!!.height)
76             val position = Rect()
77             it.mSurfaceView!!.getBoundsOnScreen(position)
78             svBounds.offsetTo(position.left, position.top)
79         }
80 
81         runOnUiThread {
82             it.mSurfaceProxy.drawBuffer(0, Color.RED)
83             checkPixels(svBounds, Color.RED)
84             it.mSurfaceProxy.drawBuffer(0, Color.GREEN)
85             checkPixels(svBounds, Color.GREEN)
86             it.mSurfaceProxy.drawBuffer(0, Color.BLUE)
87             checkPixels(svBounds, Color.BLUE)
88         }
89     }
90 }