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 <gtest/gtest.h>
18
19 #include <binder/IMemory.h>
20
21 #include <gui/ISurfaceComposer.h>
22 #include <gui/Surface.h>
23 #include <gui/SurfaceComposerClient.h>
24 #include <private/gui/ComposerService.h>
25
26 #include <utils/String8.h>
27 #include <ui/DisplayInfo.h>
28
29 namespace android {
30
31 // Fill an RGBA_8888 formatted surface with a single color.
fillSurfaceRGBA8(const sp<SurfaceControl> & sc,uint8_t r,uint8_t g,uint8_t b)32 static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc,
33 uint8_t r, uint8_t g, uint8_t b) {
34 ANativeWindow_Buffer outBuffer;
35 sp<Surface> s = sc->getSurface();
36 ASSERT_TRUE(s != NULL);
37 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
38 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
39 for (uint32_t y = 0; y < outBuffer.height; y++) {
40 for (uint32_t x = 0; x < outBuffer.width; x++) {
41 uint8_t* pixel = img + (4 * (y*outBuffer.stride + x));
42 pixel[0] = r;
43 pixel[1] = g;
44 pixel[2] = b;
45 pixel[3] = 255;
46 }
47 }
48 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
49 }
50
51 // A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
52 // individual pixel values for testing purposes.
53 class ScreenCapture : public RefBase {
54 public:
captureScreen(sp<ScreenCapture> * sc)55 static void captureScreen(sp<ScreenCapture>* sc) {
56 sp<IMemoryHeap> heap;
57 uint32_t w=0, h=0;
58 PixelFormat fmt=0;
59 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
60 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
61 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &heap, &w, &h, &fmt, 0, 0,
62 0, INT_MAX));
63 ASSERT_TRUE(heap != NULL);
64 ASSERT_EQ(PIXEL_FORMAT_RGBA_8888, fmt);
65 *sc = new ScreenCapture(w, h, heap);
66 }
67
checkPixel(uint32_t x,uint32_t y,uint8_t r,uint8_t g,uint8_t b)68 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
69 const uint8_t* img = reinterpret_cast<const uint8_t*>(mHeap->base());
70 const uint8_t* pixel = img + (4 * (y*mWidth + x));
71 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
72 String8 err(String8::format("pixel @ (%3d, %3d): "
73 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
74 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
75 EXPECT_EQ(String8(), err);
76 }
77 }
78
79 private:
ScreenCapture(uint32_t w,uint32_t h,const sp<IMemoryHeap> & heap)80 ScreenCapture(uint32_t w, uint32_t h, const sp<IMemoryHeap>& heap) :
81 mWidth(w),
82 mHeight(h),
83 mHeap(heap)
84 {}
85
86 const uint32_t mWidth;
87 const uint32_t mHeight;
88 sp<IMemoryHeap> mHeap;
89 };
90
91 class LayerUpdateTest : public ::testing::Test {
92 protected:
SetUp()93 virtual void SetUp() {
94 mComposerClient = new SurfaceComposerClient;
95 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
96
97 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(
98 ISurfaceComposer::eDisplayIdMain));
99 DisplayInfo info;
100 SurfaceComposerClient::getDisplayInfo(display, &info);
101
102 ssize_t displayWidth = info.w;
103 ssize_t displayHeight = info.h;
104
105 // Background surface
106 mBGSurfaceControl = mComposerClient->createSurface(
107 String8("BG Test Surface"), displayWidth, displayHeight,
108 PIXEL_FORMAT_RGBA_8888, 0);
109 ASSERT_TRUE(mBGSurfaceControl != NULL);
110 ASSERT_TRUE(mBGSurfaceControl->isValid());
111 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
112
113 // Foreground surface
114 mFGSurfaceControl = mComposerClient->createSurface(
115 String8("FG Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
116 ASSERT_TRUE(mFGSurfaceControl != NULL);
117 ASSERT_TRUE(mFGSurfaceControl->isValid());
118
119 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
120
121 // Synchronization surface
122 mSyncSurfaceControl = mComposerClient->createSurface(
123 String8("Sync Test Surface"), 1, 1, PIXEL_FORMAT_RGBA_8888, 0);
124 ASSERT_TRUE(mSyncSurfaceControl != NULL);
125 ASSERT_TRUE(mSyncSurfaceControl->isValid());
126
127 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
128
129 SurfaceComposerClient::openGlobalTransaction();
130
131 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->setLayer(INT_MAX-2));
132 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->show());
133
134 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT_MAX-1));
135 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(64, 64));
136 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show());
137
138 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setLayer(INT_MAX-1));
139 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setPosition(displayWidth-2,
140 displayHeight-2));
141 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->show());
142
143 SurfaceComposerClient::closeGlobalTransaction(true);
144 }
145
TearDown()146 virtual void TearDown() {
147 mComposerClient->dispose();
148 mBGSurfaceControl = 0;
149 mFGSurfaceControl = 0;
150 mSyncSurfaceControl = 0;
151 mComposerClient = 0;
152 }
153
waitForPostedBuffers()154 void waitForPostedBuffers() {
155 // Since the sync surface is in synchronous mode (i.e. double buffered)
156 // posting three buffers to it should ensure that at least two
157 // SurfaceFlinger::handlePageFlip calls have been made, which should
158 // guaranteed that a buffer posted to another Surface has been retired.
159 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
160 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
161 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
162 }
163
164 sp<SurfaceComposerClient> mComposerClient;
165 sp<SurfaceControl> mBGSurfaceControl;
166 sp<SurfaceControl> mFGSurfaceControl;
167
168 // This surface is used to ensure that the buffers posted to
169 // mFGSurfaceControl have been picked up by SurfaceFlinger.
170 sp<SurfaceControl> mSyncSurfaceControl;
171 };
172
TEST_F(LayerUpdateTest,LayerMoveWorks)173 TEST_F(LayerUpdateTest, LayerMoveWorks) {
174 sp<ScreenCapture> sc;
175 {
176 SCOPED_TRACE("before move");
177 ScreenCapture::captureScreen(&sc);
178 sc->checkPixel( 0, 12, 63, 63, 195);
179 sc->checkPixel( 75, 75, 195, 63, 63);
180 sc->checkPixel(145, 145, 63, 63, 195);
181 }
182
183 SurfaceComposerClient::openGlobalTransaction();
184 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128, 128));
185 SurfaceComposerClient::closeGlobalTransaction(true);
186 {
187 // This should reflect the new position, but not the new color.
188 SCOPED_TRACE("after move, before redraw");
189 ScreenCapture::captureScreen(&sc);
190 sc->checkPixel( 24, 24, 63, 63, 195);
191 sc->checkPixel( 75, 75, 63, 63, 195);
192 sc->checkPixel(145, 145, 195, 63, 63);
193 }
194
195 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
196 waitForPostedBuffers();
197 {
198 // This should reflect the new position and the new color.
199 SCOPED_TRACE("after redraw");
200 ScreenCapture::captureScreen(&sc);
201 sc->checkPixel( 24, 24, 63, 63, 195);
202 sc->checkPixel( 75, 75, 63, 63, 195);
203 sc->checkPixel(145, 145, 63, 195, 63);
204 }
205 }
206
TEST_F(LayerUpdateTest,LayerResizeWorks)207 TEST_F(LayerUpdateTest, LayerResizeWorks) {
208 sp<ScreenCapture> sc;
209 {
210 SCOPED_TRACE("before resize");
211 ScreenCapture::captureScreen(&sc);
212 sc->checkPixel( 0, 12, 63, 63, 195);
213 sc->checkPixel( 75, 75, 195, 63, 63);
214 sc->checkPixel(145, 145, 63, 63, 195);
215 }
216
217 ALOGD("resizing");
218 SurfaceComposerClient::openGlobalTransaction();
219 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setSize(128, 128));
220 SurfaceComposerClient::closeGlobalTransaction(true);
221 ALOGD("resized");
222 {
223 // This should not reflect the new size or color because SurfaceFlinger
224 // has not yet received a buffer of the correct size.
225 SCOPED_TRACE("after resize, before redraw");
226 ScreenCapture::captureScreen(&sc);
227 sc->checkPixel( 0, 12, 63, 63, 195);
228 sc->checkPixel( 75, 75, 195, 63, 63);
229 sc->checkPixel(145, 145, 63, 63, 195);
230 }
231
232 ALOGD("drawing");
233 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
234 waitForPostedBuffers();
235 ALOGD("drawn");
236 {
237 // This should reflect the new size and the new color.
238 SCOPED_TRACE("after redraw");
239 ScreenCapture::captureScreen(&sc);
240 sc->checkPixel( 24, 24, 63, 63, 195);
241 sc->checkPixel( 75, 75, 63, 195, 63);
242 sc->checkPixel(145, 145, 63, 195, 63);
243 }
244 }
245
246 }
247