blob: 5b61ae0e9bed0e68956a1e8e1d022f71299b1f42 [file] [log] [blame]
Jamie Gennis23c2c5d2011-10-11 19:22:19 -07001/*
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
Michael Lentine5a16a622015-05-21 13:48:24 -070019#include <android/native_window.h>
20
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070021#include <binder/IMemory.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080022
23#include <gui/ISurfaceComposer.h>
24#include <gui/Surface.h>
25#include <gui/SurfaceComposerClient.h>
26#include <private/gui/ComposerService.h>
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070027#include <private/gui/LayerState.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080028
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070029#include <utils/String8.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070030#include <ui/DisplayInfo.h>
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070031
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070032#include <math.h>
33
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070034namespace android {
35
36// Fill an RGBA_8888 formatted surface with a single color.
37static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc,
38 uint8_t r, uint8_t g, uint8_t b) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080039 ANativeWindow_Buffer outBuffer;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070040 sp<Surface> s = sc->getSurface();
41 ASSERT_TRUE(s != NULL);
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
43 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070044 for (int y = 0; y < outBuffer.height; y++) {
45 for (int x = 0; x < outBuffer.width; x++) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080046 uint8_t* pixel = img + (4 * (y*outBuffer.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070047 pixel[0] = r;
48 pixel[1] = g;
49 pixel[2] = b;
50 pixel[3] = 255;
51 }
52 }
53 ASSERT_EQ(NO_ERROR, s->unlockAndPost());
54}
55
56// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
57// individual pixel values for testing purposes.
58class ScreenCapture : public RefBase {
59public:
60 static void captureScreen(sp<ScreenCapture>* sc) {
Michael Lentine5a16a622015-05-21 13:48:24 -070061 sp<IGraphicBufferProducer> producer;
62 sp<IGraphicBufferConsumer> consumer;
63 BufferQueue::createBufferQueue(&producer, &consumer);
Michael Lentine5a16a622015-05-21 13:48:24 -070064 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070065 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Michael Lentine5a16a622015-05-21 13:48:24 -070066 sp<IBinder> display(sf->getBuiltInDisplay(
67 ISurfaceComposer::eDisplayIdMain));
Pablo Ceballos15311bd2016-06-01 18:53:40 -070068 SurfaceComposerClient::openGlobalTransaction();
69 SurfaceComposerClient::closeGlobalTransaction(true);
Michael Lentine5a16a622015-05-21 13:48:24 -070070 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(), 0, 0,
71 0, INT_MAX, false));
72 *sc = new ScreenCapture(cpuConsumer);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070073 }
74
75 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
Michael Lentine5a16a622015-05-21 13:48:24 -070076 ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
77 const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
78 const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070079 if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
80 String8 err(String8::format("pixel @ (%3d, %3d): "
81 "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
82 x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -070083 EXPECT_EQ(String8(), err) << err.string();
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070084 }
85 }
86
Robert Carr1f0a16a2016-10-24 16:27:39 -070087 void expectFGColor(uint32_t x, uint32_t y) {
88 checkPixel(x, y, 195, 63, 63);
89 }
90
91 void expectBGColor(uint32_t x, uint32_t y) {
92 checkPixel(x, y, 63, 63, 195);
93 }
94
95 void expectChildColor(uint32_t x, uint32_t y) {
96 checkPixel(x, y, 200, 200, 200);
97 }
98
Jamie Gennis23c2c5d2011-10-11 19:22:19 -070099private:
Michael Lentine5a16a622015-05-21 13:48:24 -0700100 ScreenCapture(const sp<CpuConsumer>& cc) :
101 mCC(cc) {
102 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
103 }
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700104
Michael Lentine5a16a622015-05-21 13:48:24 -0700105 ~ScreenCapture() {
106 mCC->unlockBuffer(mBuf);
107 }
108
109 sp<CpuConsumer> mCC;
110 CpuConsumer::LockedBuffer mBuf;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700111};
112
113class LayerUpdateTest : public ::testing::Test {
114protected:
115 virtual void SetUp() {
116 mComposerClient = new SurfaceComposerClient;
117 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
118
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700119 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(
120 ISurfaceComposer::eDisplayIdMain));
Mathias Agopianc666cae2012-07-25 18:56:13 -0700121 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700122 SurfaceComposerClient::getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700123
124 ssize_t displayWidth = info.w;
125 ssize_t displayHeight = info.h;
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700126
127 // Background surface
128 mBGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700129 String8("BG Test Surface"), displayWidth, displayHeight,
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700130 PIXEL_FORMAT_RGBA_8888, 0);
131 ASSERT_TRUE(mBGSurfaceControl != NULL);
132 ASSERT_TRUE(mBGSurfaceControl->isValid());
133 fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
134
135 // Foreground surface
136 mFGSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700137 String8("FG Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700138 ASSERT_TRUE(mFGSurfaceControl != NULL);
139 ASSERT_TRUE(mFGSurfaceControl->isValid());
140
141 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
142
143 // Synchronization surface
144 mSyncSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700145 String8("Sync Test Surface"), 1, 1, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700146 ASSERT_TRUE(mSyncSurfaceControl != NULL);
147 ASSERT_TRUE(mSyncSurfaceControl->isValid());
148
149 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
150
151 SurfaceComposerClient::openGlobalTransaction();
152
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700153 mComposerClient->setDisplayLayerStack(display, 0);
154
Robert Carr1f0a16a2016-10-24 16:27:39 -0700155 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->setLayer(INT32_MAX-2));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700156 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->show());
157
Robert Carr1f0a16a2016-10-24 16:27:39 -0700158 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT32_MAX-1));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700159 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(64, 64));
160 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show());
161
Robert Carr1f0a16a2016-10-24 16:27:39 -0700162 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setLayer(INT32_MAX-1));
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700163 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setPosition(displayWidth-2,
164 displayHeight-2));
165 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->show());
166
167 SurfaceComposerClient::closeGlobalTransaction(true);
168 }
169
170 virtual void TearDown() {
171 mComposerClient->dispose();
172 mBGSurfaceControl = 0;
173 mFGSurfaceControl = 0;
174 mSyncSurfaceControl = 0;
175 mComposerClient = 0;
176 }
177
178 void waitForPostedBuffers() {
179 // Since the sync surface is in synchronous mode (i.e. double buffered)
180 // posting three buffers to it should ensure that at least two
181 // SurfaceFlinger::handlePageFlip calls have been made, which should
182 // guaranteed that a buffer posted to another Surface has been retired.
183 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
184 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
185 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
186 }
187
188 sp<SurfaceComposerClient> mComposerClient;
189 sp<SurfaceControl> mBGSurfaceControl;
190 sp<SurfaceControl> mFGSurfaceControl;
191
192 // This surface is used to ensure that the buffers posted to
193 // mFGSurfaceControl have been picked up by SurfaceFlinger.
194 sp<SurfaceControl> mSyncSurfaceControl;
195};
196
197TEST_F(LayerUpdateTest, LayerMoveWorks) {
198 sp<ScreenCapture> sc;
199 {
200 SCOPED_TRACE("before move");
201 ScreenCapture::captureScreen(&sc);
202 sc->checkPixel( 0, 12, 63, 63, 195);
203 sc->checkPixel( 75, 75, 195, 63, 63);
204 sc->checkPixel(145, 145, 63, 63, 195);
205 }
206
207 SurfaceComposerClient::openGlobalTransaction();
208 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128, 128));
209 SurfaceComposerClient::closeGlobalTransaction(true);
210 {
211 // This should reflect the new position, but not the new color.
212 SCOPED_TRACE("after move, before redraw");
213 ScreenCapture::captureScreen(&sc);
214 sc->checkPixel( 24, 24, 63, 63, 195);
215 sc->checkPixel( 75, 75, 63, 63, 195);
216 sc->checkPixel(145, 145, 195, 63, 63);
217 }
218
219 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
220 waitForPostedBuffers();
221 {
222 // This should reflect the new position and the new color.
223 SCOPED_TRACE("after redraw");
224 ScreenCapture::captureScreen(&sc);
225 sc->checkPixel( 24, 24, 63, 63, 195);
226 sc->checkPixel( 75, 75, 63, 63, 195);
227 sc->checkPixel(145, 145, 63, 195, 63);
228 }
229}
230
231TEST_F(LayerUpdateTest, LayerResizeWorks) {
232 sp<ScreenCapture> sc;
233 {
234 SCOPED_TRACE("before resize");
235 ScreenCapture::captureScreen(&sc);
236 sc->checkPixel( 0, 12, 63, 63, 195);
237 sc->checkPixel( 75, 75, 195, 63, 63);
238 sc->checkPixel(145, 145, 63, 63, 195);
239 }
240
Steve Block9d453682011-12-20 16:23:08 +0000241 ALOGD("resizing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700242 SurfaceComposerClient::openGlobalTransaction();
243 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setSize(128, 128));
244 SurfaceComposerClient::closeGlobalTransaction(true);
Steve Block9d453682011-12-20 16:23:08 +0000245 ALOGD("resized");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700246 {
247 // This should not reflect the new size or color because SurfaceFlinger
248 // has not yet received a buffer of the correct size.
249 SCOPED_TRACE("after resize, before redraw");
250 ScreenCapture::captureScreen(&sc);
251 sc->checkPixel( 0, 12, 63, 63, 195);
252 sc->checkPixel( 75, 75, 195, 63, 63);
253 sc->checkPixel(145, 145, 63, 63, 195);
254 }
255
Steve Block9d453682011-12-20 16:23:08 +0000256 ALOGD("drawing");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700257 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
258 waitForPostedBuffers();
Steve Block9d453682011-12-20 16:23:08 +0000259 ALOGD("drawn");
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700260 {
261 // This should reflect the new size and the new color.
262 SCOPED_TRACE("after redraw");
263 ScreenCapture::captureScreen(&sc);
264 sc->checkPixel( 24, 24, 63, 63, 195);
265 sc->checkPixel( 75, 75, 63, 195, 63);
266 sc->checkPixel(145, 145, 63, 195, 63);
267 }
268}
269
Haixia Shid5750962015-07-27 16:50:49 -0700270TEST_F(LayerUpdateTest, LayerCropWorks) {
271 sp<ScreenCapture> sc;
272 {
273 SCOPED_TRACE("before crop");
274 ScreenCapture::captureScreen(&sc);
275 sc->checkPixel( 24, 24, 63, 63, 195);
276 sc->checkPixel( 75, 75, 195, 63, 63);
277 sc->checkPixel(145, 145, 63, 63, 195);
278 }
279
280 SurfaceComposerClient::openGlobalTransaction();
281 Rect cropRect(16, 16, 32, 32);
282 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setCrop(cropRect));
283 SurfaceComposerClient::closeGlobalTransaction(true);
284 {
285 // This should crop the foreground surface.
286 SCOPED_TRACE("after crop");
287 ScreenCapture::captureScreen(&sc);
288 sc->checkPixel( 24, 24, 63, 63, 195);
289 sc->checkPixel( 75, 75, 63, 63, 195);
290 sc->checkPixel( 95, 80, 195, 63, 63);
291 sc->checkPixel( 80, 95, 195, 63, 63);
292 sc->checkPixel( 96, 96, 63, 63, 195);
293 }
294}
295
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000296TEST_F(LayerUpdateTest, LayerFinalCropWorks) {
297 sp<ScreenCapture> sc;
298 {
299 SCOPED_TRACE("before crop");
300 ScreenCapture::captureScreen(&sc);
301 sc->checkPixel( 24, 24, 63, 63, 195);
302 sc->checkPixel( 75, 75, 195, 63, 63);
303 sc->checkPixel(145, 145, 63, 63, 195);
304 }
305 SurfaceComposerClient::openGlobalTransaction();
306 Rect cropRect(16, 16, 32, 32);
307 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setFinalCrop(cropRect));
308 SurfaceComposerClient::closeGlobalTransaction(true);
309 {
310 // This should crop the foreground surface.
311 SCOPED_TRACE("after crop");
312 ScreenCapture::captureScreen(&sc);
313 sc->checkPixel( 24, 24, 63, 63, 195);
314 sc->checkPixel( 75, 75, 63, 63, 195);
315 sc->checkPixel( 95, 80, 63, 63, 195);
316 sc->checkPixel( 80, 95, 63, 63, 195);
317 sc->checkPixel( 96, 96, 63, 63, 195);
318 }
319}
320
Haixia Shid5750962015-07-27 16:50:49 -0700321TEST_F(LayerUpdateTest, LayerSetLayerWorks) {
322 sp<ScreenCapture> sc;
323 {
324 SCOPED_TRACE("before setLayer");
325 ScreenCapture::captureScreen(&sc);
326 sc->checkPixel( 24, 24, 63, 63, 195);
327 sc->checkPixel( 75, 75, 195, 63, 63);
328 sc->checkPixel(145, 145, 63, 63, 195);
329 }
330
331 SurfaceComposerClient::openGlobalTransaction();
332 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT_MAX - 3));
333 SurfaceComposerClient::closeGlobalTransaction(true);
334 {
335 // This should hide the foreground surface beneath the background.
336 SCOPED_TRACE("after setLayer");
337 ScreenCapture::captureScreen(&sc);
338 sc->checkPixel( 24, 24, 63, 63, 195);
339 sc->checkPixel( 75, 75, 63, 63, 195);
340 sc->checkPixel(145, 145, 63, 63, 195);
341 }
342}
343
344TEST_F(LayerUpdateTest, LayerShowHideWorks) {
345 sp<ScreenCapture> sc;
346 {
347 SCOPED_TRACE("before hide");
348 ScreenCapture::captureScreen(&sc);
349 sc->checkPixel( 24, 24, 63, 63, 195);
350 sc->checkPixel( 75, 75, 195, 63, 63);
351 sc->checkPixel(145, 145, 63, 63, 195);
352 }
353
354 SurfaceComposerClient::openGlobalTransaction();
355 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->hide());
356 SurfaceComposerClient::closeGlobalTransaction(true);
357 {
358 // This should hide the foreground surface.
359 SCOPED_TRACE("after hide, before show");
360 ScreenCapture::captureScreen(&sc);
361 sc->checkPixel( 24, 24, 63, 63, 195);
362 sc->checkPixel( 75, 75, 63, 63, 195);
363 sc->checkPixel(145, 145, 63, 63, 195);
364 }
365
366 SurfaceComposerClient::openGlobalTransaction();
367 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show());
368 SurfaceComposerClient::closeGlobalTransaction(true);
369 {
370 // This should show the foreground surface.
371 SCOPED_TRACE("after show");
372 ScreenCapture::captureScreen(&sc);
373 sc->checkPixel( 24, 24, 63, 63, 195);
374 sc->checkPixel( 75, 75, 195, 63, 63);
375 sc->checkPixel(145, 145, 63, 63, 195);
376 }
377}
378
379TEST_F(LayerUpdateTest, LayerSetAlphaWorks) {
380 sp<ScreenCapture> sc;
381 {
382 SCOPED_TRACE("before setAlpha");
383 ScreenCapture::captureScreen(&sc);
384 sc->checkPixel( 24, 24, 63, 63, 195);
385 sc->checkPixel( 75, 75, 195, 63, 63);
386 sc->checkPixel(145, 145, 63, 63, 195);
387 }
388
389 SurfaceComposerClient::openGlobalTransaction();
390 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75f));
391 SurfaceComposerClient::closeGlobalTransaction(true);
392 {
393 // This should set foreground to be 75% opaque.
394 SCOPED_TRACE("after setAlpha");
395 ScreenCapture::captureScreen(&sc);
396 sc->checkPixel( 24, 24, 63, 63, 195);
397 sc->checkPixel( 75, 75, 162, 63, 96);
398 sc->checkPixel(145, 145, 63, 63, 195);
399 }
400}
401
Pablo Ceballos5e4fcbe2015-09-02 09:53:16 -0700402TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) {
403 sp<ScreenCapture> sc;
404 {
405 SCOPED_TRACE("before setLayerStack");
406 ScreenCapture::captureScreen(&sc);
407 sc->checkPixel( 24, 24, 63, 63, 195);
408 sc->checkPixel( 75, 75, 195, 63, 63);
409 sc->checkPixel(145, 145, 63, 63, 195);
410 }
411
412 SurfaceComposerClient::openGlobalTransaction();
413 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayerStack(1));
414 SurfaceComposerClient::closeGlobalTransaction(true);
415 {
416 // This should hide the foreground surface since it goes to a different
417 // layer stack.
418 SCOPED_TRACE("after setLayerStack");
419 ScreenCapture::captureScreen(&sc);
420 sc->checkPixel( 24, 24, 63, 63, 195);
421 sc->checkPixel( 75, 75, 63, 63, 195);
422 sc->checkPixel(145, 145, 63, 63, 195);
423 }
424}
425
426TEST_F(LayerUpdateTest, LayerSetFlagsWorks) {
427 sp<ScreenCapture> sc;
428 {
429 SCOPED_TRACE("before setFlags");
430 ScreenCapture::captureScreen(&sc);
431 sc->checkPixel( 24, 24, 63, 63, 195);
432 sc->checkPixel( 75, 75, 195, 63, 63);
433 sc->checkPixel(145, 145, 63, 63, 195);
434 }
435
436 SurfaceComposerClient::openGlobalTransaction();
437 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setFlags(
438 layer_state_t::eLayerHidden, layer_state_t::eLayerHidden));
439 SurfaceComposerClient::closeGlobalTransaction(true);
440 {
441 // This should hide the foreground surface
442 SCOPED_TRACE("after setFlags");
443 ScreenCapture::captureScreen(&sc);
444 sc->checkPixel( 24, 24, 63, 63, 195);
445 sc->checkPixel( 75, 75, 63, 63, 195);
446 sc->checkPixel(145, 145, 63, 63, 195);
447 }
448}
449
450TEST_F(LayerUpdateTest, LayerSetMatrixWorks) {
451 sp<ScreenCapture> sc;
452 {
453 SCOPED_TRACE("before setMatrix");
454 ScreenCapture::captureScreen(&sc);
455 sc->checkPixel( 24, 24, 63, 63, 195);
456 sc->checkPixel( 91, 96, 195, 63, 63);
457 sc->checkPixel( 96, 101, 195, 63, 63);
458 sc->checkPixel(145, 145, 63, 63, 195);
459 }
460
461 SurfaceComposerClient::openGlobalTransaction();
462 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setMatrix(M_SQRT1_2, M_SQRT1_2,
463 -M_SQRT1_2, M_SQRT1_2));
464 SurfaceComposerClient::closeGlobalTransaction(true);
465 {
466 SCOPED_TRACE("after setMatrix");
467 ScreenCapture::captureScreen(&sc);
468 sc->checkPixel( 24, 24, 63, 63, 195);
469 sc->checkPixel( 91, 96, 195, 63, 63);
470 sc->checkPixel( 96, 91, 63, 63, 195);
471 sc->checkPixel(145, 145, 63, 63, 195);
472 }
473}
474
Pablo Ceballos05289c22016-04-14 15:49:55 -0700475TEST_F(LayerUpdateTest, DeferredTransactionTest) {
476 sp<ScreenCapture> sc;
477 {
478 SCOPED_TRACE("before anything");
479 ScreenCapture::captureScreen(&sc);
480 sc->checkPixel( 32, 32, 63, 63, 195);
481 sc->checkPixel( 96, 96, 195, 63, 63);
482 sc->checkPixel(160, 160, 63, 63, 195);
483 }
484
485 // set up two deferred transactions on different frames
486 SurfaceComposerClient::openGlobalTransaction();
487 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75));
488 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
489 mSyncSurfaceControl->getSurface()->getNextFrameNumber());
490 SurfaceComposerClient::closeGlobalTransaction(true);
491
492 SurfaceComposerClient::openGlobalTransaction();
493 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128,128));
494 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(),
495 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
496 SurfaceComposerClient::closeGlobalTransaction(true);
497
498 {
499 SCOPED_TRACE("before any trigger");
500 ScreenCapture::captureScreen(&sc);
501 sc->checkPixel( 32, 32, 63, 63, 195);
502 sc->checkPixel( 96, 96, 195, 63, 63);
503 sc->checkPixel(160, 160, 63, 63, 195);
504 }
505
506 // should trigger the first deferred transaction, but not the second one
507 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
508 {
509 SCOPED_TRACE("after first trigger");
510 ScreenCapture::captureScreen(&sc);
511 sc->checkPixel( 32, 32, 63, 63, 195);
512 sc->checkPixel( 96, 96, 162, 63, 96);
513 sc->checkPixel(160, 160, 63, 63, 195);
514 }
515
516 // should show up immediately since it's not deferred
517 SurfaceComposerClient::openGlobalTransaction();
518 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(1.0));
519 SurfaceComposerClient::closeGlobalTransaction(true);
520
521 // trigger the second deferred transaction
522 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
523 {
524 SCOPED_TRACE("after second trigger");
525 ScreenCapture::captureScreen(&sc);
526 sc->checkPixel( 32, 32, 63, 63, 195);
527 sc->checkPixel( 96, 96, 63, 63, 195);
528 sc->checkPixel(160, 160, 195, 63, 63);
529 }
530}
531
Robert Carr1f0a16a2016-10-24 16:27:39 -0700532class ChildLayerTest : public LayerUpdateTest {
533protected:
534 void SetUp() override {
535 LayerUpdateTest::SetUp();
536 mChild = mComposerClient->createSurface(
537 String8("Child surface"),
538 10, 10, PIXEL_FORMAT_RGBA_8888,
539 0, mFGSurfaceControl.get());
540 fillSurfaceRGBA8(mChild, 200, 200, 200);
541
542 {
543 SCOPED_TRACE("before anything");
544 ScreenCapture::captureScreen(&mCapture);
545 mCapture->expectChildColor(64, 64);
546 }
547 }
548 void TearDown() override {
549 LayerUpdateTest::TearDown();
550 mChild = 0;
551 }
552
553 sp<SurfaceControl> mChild;
554 sp<ScreenCapture> mCapture;
555};
556
557TEST_F(ChildLayerTest, ChildLayerPositioning) {
558 SurfaceComposerClient::openGlobalTransaction();
559 mChild->show();
560 mChild->setPosition(10, 10);
561 mFGSurfaceControl->setPosition(64, 64);
562 SurfaceComposerClient::closeGlobalTransaction(true);
563
564 {
565 ScreenCapture::captureScreen(&mCapture);
566 // Top left of foreground must now be visible
567 mCapture->expectFGColor(64, 64);
568 // But 10 pixels in we should see the child surface
569 mCapture->expectChildColor(74, 74);
570 // And 10 more pixels we should be back to the foreground surface
571 mCapture->expectFGColor(84, 84);
572 }
573
574 SurfaceComposerClient::openGlobalTransaction();
575 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(0, 0));
576 SurfaceComposerClient::closeGlobalTransaction(true);
577
578 {
579 ScreenCapture::captureScreen(&mCapture);
580 // Top left of foreground should now be at 0, 0
581 mCapture->expectFGColor(0, 0);
582 // But 10 pixels in we should see the child surface
583 mCapture->expectChildColor(10, 10);
584 // And 10 more pixels we should be back to the foreground surface
585 mCapture->expectFGColor(20, 20);
586 }
587}
588
589TEST_F(ChildLayerTest, ChildLayerConstraints) {
590 SurfaceComposerClient::openGlobalTransaction();
591 mChild->show();
592 mFGSurfaceControl->setPosition(0, 0);
593 mChild->setPosition(63, 63);
594 SurfaceComposerClient::closeGlobalTransaction(true);
595
596 {
597 ScreenCapture::captureScreen(&mCapture);
598 mCapture->expectFGColor(0, 0);
599 // Last pixel in foreground should now be the child.
600 mCapture->expectChildColor(63, 63);
601 // But the child should be constrained and the next pixel
602 // must be the background
603 mCapture->expectBGColor(64, 64);
604 }
605}
606
607TEST_F(ChildLayerTest, ChildLayerScaling) {
608 SurfaceComposerClient::openGlobalTransaction();
609 mFGSurfaceControl->setPosition(0, 0);
610 SurfaceComposerClient::closeGlobalTransaction(true);
611
612 // Find the boundary between the parent and child
613 {
614 ScreenCapture::captureScreen(&mCapture);
615 mCapture->expectChildColor(9, 9);
616 mCapture->expectFGColor(10, 10);
617 }
618
619 SurfaceComposerClient::openGlobalTransaction();
620 mFGSurfaceControl->setMatrix(2.0, 0, 0, 2.0);
621 SurfaceComposerClient::closeGlobalTransaction(true);
622
623 // The boundary should be twice as far from the origin now.
624 // The pixels from the last test should all be child now
625 {
626 ScreenCapture::captureScreen(&mCapture);
627 mCapture->expectChildColor(9, 9);
628 mCapture->expectChildColor(10, 10);
629 mCapture->expectChildColor(19, 19);
630 mCapture->expectFGColor(20, 20);
631 }
632}
Robert Carr9524cb32017-02-13 11:32:32 -0800633
634TEST_F(ChildLayerTest, ReparentChildren) {
635 SurfaceComposerClient::openGlobalTransaction();
636 mChild->show();
637 mChild->setPosition(10, 10);
638 mFGSurfaceControl->setPosition(64, 64);
639 SurfaceComposerClient::closeGlobalTransaction(true);
640
641 {
642 ScreenCapture::captureScreen(&mCapture);
643 // Top left of foreground must now be visible
644 mCapture->expectFGColor(64, 64);
645 // But 10 pixels in we should see the child surface
646 mCapture->expectChildColor(74, 74);
647 // And 10 more pixels we should be back to the foreground surface
648 mCapture->expectFGColor(84, 84);
649 }
650 mFGSurfaceControl->reparentChildren(mBGSurfaceControl->getHandle());
651 {
652 ScreenCapture::captureScreen(&mCapture);
653 mCapture->expectFGColor(64, 64);
654 // In reparenting we should have exposed the entire foreground surface.
655 mCapture->expectFGColor(74, 74);
656 // And the child layer should now begin at 10, 10 (since the BG
657 // layer is at (0, 0)).
658 mCapture->expectBGColor(9, 9);
659 mCapture->expectChildColor(10, 10);
660 }
661}
662
663TEST_F(ChildLayerTest, DetachChildren) {
664 SurfaceComposerClient::openGlobalTransaction();
665 mChild->show();
666 mChild->setPosition(10, 10);
667 mFGSurfaceControl->setPosition(64, 64);
668 SurfaceComposerClient::closeGlobalTransaction(true);
669
670 {
671 ScreenCapture::captureScreen(&mCapture);
672 // Top left of foreground must now be visible
673 mCapture->expectFGColor(64, 64);
674 // But 10 pixels in we should see the child surface
675 mCapture->expectChildColor(74, 74);
676 // And 10 more pixels we should be back to the foreground surface
677 mCapture->expectFGColor(84, 84);
678 }
679
680 SurfaceComposerClient::openGlobalTransaction();
681 mFGSurfaceControl->detachChildren();
682 SurfaceComposerClient::closeGlobalTransaction();
683
684 SurfaceComposerClient::openGlobalTransaction();
685 mChild->hide();
686 SurfaceComposerClient::closeGlobalTransaction();
687
688 // Nothing should have changed.
689 {
690 ScreenCapture::captureScreen(&mCapture);
691 mCapture->expectFGColor(64, 64);
692 mCapture->expectChildColor(74, 74);
693 mCapture->expectFGColor(84, 84);
694 }
695}
696
Jamie Gennis23c2c5d2011-10-11 19:22:19 -0700697}