1 /*
2  * Copyright (C) 2011 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 #include "DummyConsumer.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <binder/IMemory.h>
22 #include <gui/ISurfaceComposer.h>
23 #include <gui/Surface.h>
24 #include <gui/SurfaceComposerClient.h>
25 #include <gui/BufferItemConsumer.h>
26 #include <ui/Rect.h>
27 #include <utils/String8.h>
28 
29 #include <private/gui/ComposerService.h>
30 #include <binder/ProcessState.h>
31 
32 namespace android {
33 
34 class SurfaceTest : public ::testing::Test {
35 protected:
36 
SurfaceTest()37     SurfaceTest() {
38         ProcessState::self()->startThreadPool();
39     }
40 
SetUp()41     virtual void SetUp() {
42         mComposerClient = new SurfaceComposerClient;
43         ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
44 
45         mSurfaceControl = mComposerClient->createSurface(
46                 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
47 
48         ASSERT_TRUE(mSurfaceControl != NULL);
49         ASSERT_TRUE(mSurfaceControl->isValid());
50 
51         SurfaceComposerClient::openGlobalTransaction();
52         ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
53         ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
54         SurfaceComposerClient::closeGlobalTransaction();
55 
56         mSurface = mSurfaceControl->getSurface();
57         ASSERT_TRUE(mSurface != NULL);
58     }
59 
TearDown()60     virtual void TearDown() {
61         mComposerClient->dispose();
62     }
63 
64     sp<Surface> mSurface;
65     sp<SurfaceComposerClient> mComposerClient;
66     sp<SurfaceControl> mSurfaceControl;
67 };
68 
TEST_F(SurfaceTest,QueuesToWindowComposerIsTrueWhenVisible)69 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
70     sp<ANativeWindow> anw(mSurface);
71     int result = -123;
72     int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
73             &result);
74     EXPECT_EQ(NO_ERROR, err);
75     EXPECT_EQ(1, result);
76 }
77 
TEST_F(SurfaceTest,QueuesToWindowComposerIsTrueWhenPurgatorized)78 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
79     mSurfaceControl.clear();
80 
81     sp<ANativeWindow> anw(mSurface);
82     int result = -123;
83     int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
84             &result);
85     EXPECT_EQ(NO_ERROR, err);
86     EXPECT_EQ(1, result);
87 }
88 
89 // This test probably doesn't belong here.
TEST_F(SurfaceTest,ScreenshotsOfProtectedBuffersSucceed)90 TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
91     sp<ANativeWindow> anw(mSurface);
92 
93     // Verify the screenshot works with no protected buffers.
94     sp<IGraphicBufferProducer> producer;
95     sp<IGraphicBufferConsumer> consumer;
96     BufferQueue::createBufferQueue(&producer, &consumer);
97     sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
98     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
99     sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
100     ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
101             64, 64, 0, 0x7fffffff, false));
102 
103     ASSERT_EQ(NO_ERROR, native_window_api_connect(anw.get(),
104             NATIVE_WINDOW_API_CPU));
105     // Set the PROTECTED usage bit and verify that the screenshot fails.  Note
106     // that we need to dequeue a buffer in order for it to actually get
107     // allocated in SurfaceFlinger.
108     ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
109             GRALLOC_USAGE_PROTECTED));
110     ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
111     ANativeWindowBuffer* buf = 0;
112 
113     status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
114     if (err) {
115         // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
116         // that's okay as long as this is the reason for the failure.
117         // try again without the GRALLOC_USAGE_PROTECTED bit.
118         ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
119         ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
120                 &buf));
121         return;
122     }
123     ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
124 
125     for (int i = 0; i < 4; i++) {
126         // Loop to make sure SurfaceFlinger has retired a protected buffer.
127         ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
128                 &buf));
129         ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
130     }
131     ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
132             64, 64, 0, 0x7fffffff, false));
133 }
134 
TEST_F(SurfaceTest,ConcreteTypeIsSurface)135 TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
136     sp<ANativeWindow> anw(mSurface);
137     int result = -123;
138     int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
139     EXPECT_EQ(NO_ERROR, err);
140     EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
141 }
142 
TEST_F(SurfaceTest,QueryConsumerUsage)143 TEST_F(SurfaceTest, QueryConsumerUsage) {
144     const int TEST_USAGE_FLAGS =
145             GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
146     sp<IGraphicBufferProducer> producer;
147     sp<IGraphicBufferConsumer> consumer;
148     BufferQueue::createBufferQueue(&producer, &consumer);
149     sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
150             TEST_USAGE_FLAGS);
151     sp<Surface> s = new Surface(producer);
152 
153     sp<ANativeWindow> anw(s);
154 
155     int flags = -1;
156     int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
157 
158     ASSERT_EQ(NO_ERROR, err);
159     ASSERT_EQ(TEST_USAGE_FLAGS, flags);
160 }
161 
TEST_F(SurfaceTest,QueryDefaultBuffersDataSpace)162 TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
163     const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
164     sp<IGraphicBufferProducer> producer;
165     sp<IGraphicBufferConsumer> consumer;
166     BufferQueue::createBufferQueue(&producer, &consumer);
167     sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
168 
169     cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
170 
171     sp<Surface> s = new Surface(producer);
172 
173     sp<ANativeWindow> anw(s);
174 
175     android_dataspace dataSpace;
176 
177     int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
178             reinterpret_cast<int*>(&dataSpace));
179 
180     ASSERT_EQ(NO_ERROR, err);
181     ASSERT_EQ(TEST_DATASPACE, dataSpace);
182 }
183 
TEST_F(SurfaceTest,SettingGenerationNumber)184 TEST_F(SurfaceTest, SettingGenerationNumber) {
185     sp<IGraphicBufferProducer> producer;
186     sp<IGraphicBufferConsumer> consumer;
187     BufferQueue::createBufferQueue(&producer, &consumer);
188     sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
189     sp<Surface> surface = new Surface(producer);
190     sp<ANativeWindow> window(surface);
191 
192     // Allocate a buffer with a generation number of 0
193     ANativeWindowBuffer* buffer;
194     int fenceFd;
195     ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
196             NATIVE_WINDOW_API_CPU));
197     ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
198     ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
199 
200     // Detach the buffer and check its generation number
201     sp<GraphicBuffer> graphicBuffer;
202     sp<Fence> fence;
203     ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
204     ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
205 
206     ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
207     buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
208 
209     // This should change the generation number of the GraphicBuffer
210     ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
211 
212     // Check that the new generation number sticks with the buffer
213     ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
214     ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
215     graphicBuffer = static_cast<GraphicBuffer*>(buffer);
216     ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
217 }
218 
TEST_F(SurfaceTest,GetConsumerName)219 TEST_F(SurfaceTest, GetConsumerName) {
220     sp<IGraphicBufferProducer> producer;
221     sp<IGraphicBufferConsumer> consumer;
222     BufferQueue::createBufferQueue(&producer, &consumer);
223 
224     sp<DummyConsumer> dummyConsumer(new DummyConsumer);
225     consumer->consumerConnect(dummyConsumer, false);
226     consumer->setConsumerName(String8("TestConsumer"));
227 
228     sp<Surface> surface = new Surface(producer);
229     sp<ANativeWindow> window(surface);
230     native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
231 
232     EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
233 }
234 
TEST_F(SurfaceTest,DynamicSetBufferCount)235 TEST_F(SurfaceTest, DynamicSetBufferCount) {
236     sp<IGraphicBufferProducer> producer;
237     sp<IGraphicBufferConsumer> consumer;
238     BufferQueue::createBufferQueue(&producer, &consumer);
239 
240     sp<DummyConsumer> dummyConsumer(new DummyConsumer);
241     consumer->consumerConnect(dummyConsumer, false);
242     consumer->setConsumerName(String8("TestConsumer"));
243 
244     sp<Surface> surface = new Surface(producer);
245     sp<ANativeWindow> window(surface);
246 
247     ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(),
248             NATIVE_WINDOW_API_CPU));
249     native_window_set_buffer_count(window.get(), 4);
250 
251     int fence;
252     ANativeWindowBuffer* buffer;
253     ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
254     native_window_set_buffer_count(window.get(), 3);
255     ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
256     native_window_set_buffer_count(window.get(), 2);
257     ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fence));
258     ASSERT_EQ(NO_ERROR, window->queueBuffer(window.get(), buffer, fence));
259 }
260 
261 }
262