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 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
104 // that we need to dequeue a buffer in order for it to actually get
105 // allocated in SurfaceFlinger.
106 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
107 GRALLOC_USAGE_PROTECTED));
108 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
109 ANativeWindowBuffer* buf = 0;
110
111 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
112 if (err) {
113 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
114 // that's okay as long as this is the reason for the failure.
115 // try again without the GRALLOC_USAGE_PROTECTED bit.
116 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
117 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
118 &buf));
119 return;
120 }
121 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
122
123 for (int i = 0; i < 4; i++) {
124 // Loop to make sure SurfaceFlinger has retired a protected buffer.
125 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
126 &buf));
127 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
128 }
129 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
130 64, 64, 0, 0x7fffffff, false));
131 }
132
TEST_F(SurfaceTest,ConcreteTypeIsSurface)133 TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
134 sp<ANativeWindow> anw(mSurface);
135 int result = -123;
136 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
137 EXPECT_EQ(NO_ERROR, err);
138 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
139 }
140
TEST_F(SurfaceTest,QueryConsumerUsage)141 TEST_F(SurfaceTest, QueryConsumerUsage) {
142 const int TEST_USAGE_FLAGS =
143 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
144 sp<IGraphicBufferProducer> producer;
145 sp<IGraphicBufferConsumer> consumer;
146 BufferQueue::createBufferQueue(&producer, &consumer);
147 sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
148 TEST_USAGE_FLAGS);
149 sp<Surface> s = new Surface(producer);
150
151 sp<ANativeWindow> anw(s);
152
153 int flags = -1;
154 int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
155
156 ASSERT_EQ(NO_ERROR, err);
157 ASSERT_EQ(TEST_USAGE_FLAGS, flags);
158 }
159
TEST_F(SurfaceTest,QueryDefaultBuffersDataSpace)160 TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
161 const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
162 sp<IGraphicBufferProducer> producer;
163 sp<IGraphicBufferConsumer> consumer;
164 BufferQueue::createBufferQueue(&producer, &consumer);
165 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
166
167 cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
168
169 sp<Surface> s = new Surface(producer);
170
171 sp<ANativeWindow> anw(s);
172
173 android_dataspace dataSpace;
174
175 int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
176 reinterpret_cast<int*>(&dataSpace));
177
178 ASSERT_EQ(NO_ERROR, err);
179 ASSERT_EQ(TEST_DATASPACE, dataSpace);
180 }
181
TEST_F(SurfaceTest,SettingGenerationNumber)182 TEST_F(SurfaceTest, SettingGenerationNumber) {
183 sp<IGraphicBufferProducer> producer;
184 sp<IGraphicBufferConsumer> consumer;
185 BufferQueue::createBufferQueue(&producer, &consumer);
186 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
187 sp<Surface> surface = new Surface(producer);
188 sp<ANativeWindow> window(surface);
189
190 // Allocate a buffer with a generation number of 0
191 ANativeWindowBuffer* buffer;
192 int fenceFd;
193 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
194 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
195
196 // Detach the buffer and check its generation number
197 sp<GraphicBuffer> graphicBuffer;
198 sp<Fence> fence;
199 ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
200 ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
201
202 ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
203 buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
204
205 // This should change the generation number of the GraphicBuffer
206 ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
207
208 // Check that the new generation number sticks with the buffer
209 ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
210 ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
211 graphicBuffer = static_cast<GraphicBuffer*>(buffer);
212 ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
213 }
214
TEST_F(SurfaceTest,GetConsumerName)215 TEST_F(SurfaceTest, GetConsumerName) {
216 sp<IGraphicBufferProducer> producer;
217 sp<IGraphicBufferConsumer> consumer;
218 BufferQueue::createBufferQueue(&producer, &consumer);
219
220 sp<DummyConsumer> dummyConsumer(new DummyConsumer);
221 consumer->consumerConnect(dummyConsumer, false);
222 consumer->setConsumerName(String8("TestConsumer"));
223
224 sp<Surface> surface = new Surface(producer);
225 sp<ANativeWindow> window(surface);
226 native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
227
228 EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
229 }
230
231 }
232