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 <android/native_window.h> 20 21 #include <gui/ISurfaceComposer.h> 22 #include <gui/Surface.h> 23 #include <gui/SurfaceComposerClient.h> 24 #include <private/gui/ComposerService.h> 25 #include <private/gui/LayerState.h> 26 27 #include <utils/String8.h> 28 #include <ui/DisplayInfo.h> 29 30 #include <math.h> 31 32 namespace android { 33 34 // Fill an RGBA_8888 formatted surface with a single color. 35 static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, 36 uint8_t r, uint8_t g, uint8_t b, bool unlock=true) { 37 ANativeWindow_Buffer outBuffer; 38 sp<Surface> s = sc->getSurface(); 39 ASSERT_TRUE(s != NULL); 40 ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL)); 41 uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits); 42 for (int y = 0; y < outBuffer.height; y++) { 43 for (int x = 0; x < outBuffer.width; x++) { 44 uint8_t* pixel = img + (4 * (y*outBuffer.stride + x)); 45 pixel[0] = r; 46 pixel[1] = g; 47 pixel[2] = b; 48 pixel[3] = 255; 49 } 50 } 51 if (unlock) { 52 ASSERT_EQ(NO_ERROR, s->unlockAndPost()); 53 } 54 } 55 56 // A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check 57 // individual pixel values for testing purposes. 58 class ScreenCapture : public RefBase { 59 public: 60 static void captureScreen(sp<ScreenCapture>* sc) { 61 sp<IGraphicBufferProducer> producer; 62 sp<IGraphicBufferConsumer> consumer; 63 BufferQueue::createBufferQueue(&producer, &consumer); 64 sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1); 65 sp<ISurfaceComposer> sf(ComposerService::getComposerService()); 66 sp<IBinder> display(sf->getBuiltInDisplay( 67 ISurfaceComposer::eDisplayIdMain)); 68 SurfaceComposerClient::openGlobalTransaction(); 69 SurfaceComposerClient::closeGlobalTransaction(true); 70 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(), 0, 0, 71 0, INT_MAX, false)); 72 *sc = new ScreenCapture(cpuConsumer); 73 } 74 75 void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) { 76 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)); 79 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])); 83 EXPECT_EQ(String8(), err) << err.string(); 84 } 85 } 86 87 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 99 private: 100 ScreenCapture(const sp<CpuConsumer>& cc) : 101 mCC(cc) { 102 EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf)); 103 } 104 105 ~ScreenCapture() { 106 mCC->unlockBuffer(mBuf); 107 } 108 109 sp<CpuConsumer> mCC; 110 CpuConsumer::LockedBuffer mBuf; 111 }; 112 113 class LayerUpdateTest : public ::testing::Test { 114 protected: 115 virtual void SetUp() { 116 mComposerClient = new SurfaceComposerClient; 117 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); 118 119 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay( 120 ISurfaceComposer::eDisplayIdMain)); 121 DisplayInfo info; 122 SurfaceComposerClient::getDisplayInfo(display, &info); 123 124 ssize_t displayWidth = info.w; 125 ssize_t displayHeight = info.h; 126 127 // Background surface 128 mBGSurfaceControl = mComposerClient->createSurface( 129 String8("BG Test Surface"), displayWidth, displayHeight, 130 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( 137 String8("FG Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0); 138 ASSERT_TRUE(mFGSurfaceControl != NULL); 139 ASSERT_TRUE(mFGSurfaceControl->isValid()); 140 141 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); 142 143 // Synchronization surface 144 mSyncSurfaceControl = mComposerClient->createSurface( 145 String8("Sync Test Surface"), 1, 1, PIXEL_FORMAT_RGBA_8888, 0); 146 ASSERT_TRUE(mSyncSurfaceControl != NULL); 147 ASSERT_TRUE(mSyncSurfaceControl->isValid()); 148 149 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); 150 151 SurfaceComposerClient::openGlobalTransaction(); 152 153 mComposerClient->setDisplayLayerStack(display, 0); 154 155 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->setLayer(INT32_MAX-2)); 156 ASSERT_EQ(NO_ERROR, mBGSurfaceControl->show()); 157 158 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT32_MAX-1)); 159 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(64, 64)); 160 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show()); 161 162 ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setLayer(INT32_MAX-1)); 163 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 197 TEST_F(LayerUpdateTest, LayerMoveWorks) { 198 sp<ScreenCapture> sc; 199 { 200 SCOPED_TRACE("before move"); 201 ScreenCapture::captureScreen(&sc); 202 sc->expectBGColor(0, 12); 203 sc->expectFGColor(75, 75); 204 sc->expectBGColor(145, 145); 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->expectBGColor(24, 24); 215 sc->expectBGColor(75, 75); 216 sc->expectFGColor(145, 145); 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->expectBGColor(24, 24); 226 sc->expectBGColor(75, 75); 227 sc->checkPixel(145, 145, 63, 195, 63); 228 } 229 } 230 231 TEST_F(LayerUpdateTest, LayerResizeWorks) { 232 sp<ScreenCapture> sc; 233 { 234 SCOPED_TRACE("before resize"); 235 ScreenCapture::captureScreen(&sc); 236 sc->expectBGColor(0, 12); 237 sc->expectFGColor(75, 75); 238 sc->expectBGColor(145, 145); 239 } 240 241 ALOGD("resizing"); 242 SurfaceComposerClient::openGlobalTransaction(); 243 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setSize(128, 128)); 244 SurfaceComposerClient::closeGlobalTransaction(true); 245 ALOGD("resized"); 246 { 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->expectBGColor(0, 12); 252 sc->expectFGColor(75, 75); 253 sc->expectBGColor(145, 145); 254 } 255 256 ALOGD("drawing"); 257 fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63); 258 waitForPostedBuffers(); 259 ALOGD("drawn"); 260 { 261 // This should reflect the new size and the new color. 262 SCOPED_TRACE("after redraw"); 263 ScreenCapture::captureScreen(&sc); 264 sc->expectBGColor(24, 24); 265 sc->checkPixel(75, 75, 63, 195, 63); 266 sc->checkPixel(145, 145, 63, 195, 63); 267 } 268 } 269 270 TEST_F(LayerUpdateTest, LayerCropWorks) { 271 sp<ScreenCapture> sc; 272 { 273 SCOPED_TRACE("before crop"); 274 ScreenCapture::captureScreen(&sc); 275 sc->expectBGColor(24, 24); 276 sc->expectFGColor(75, 75); 277 sc->expectBGColor(145, 145); 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->expectBGColor(24, 24); 289 sc->expectBGColor(75, 75); 290 sc->expectFGColor(95, 80); 291 sc->expectFGColor(80, 95); 292 sc->expectBGColor(96, 96); 293 } 294 } 295 296 TEST_F(LayerUpdateTest, LayerFinalCropWorks) { 297 sp<ScreenCapture> sc; 298 { 299 SCOPED_TRACE("before crop"); 300 ScreenCapture::captureScreen(&sc); 301 sc->expectBGColor(24, 24); 302 sc->expectFGColor(75, 75); 303 sc->expectBGColor(145, 145); 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->expectBGColor(24, 24); 314 sc->expectBGColor(75, 75); 315 sc->expectBGColor(95, 80); 316 sc->expectBGColor(80, 95); 317 sc->expectBGColor(96, 96); 318 } 319 } 320 321 TEST_F(LayerUpdateTest, LayerSetLayerWorks) { 322 sp<ScreenCapture> sc; 323 { 324 SCOPED_TRACE("before setLayer"); 325 ScreenCapture::captureScreen(&sc); 326 sc->expectBGColor(24, 24); 327 sc->expectFGColor(75, 75); 328 sc->expectBGColor(145, 145); 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->expectBGColor(24, 24); 339 sc->expectBGColor(75, 75); 340 sc->expectBGColor(145, 145); 341 } 342 } 343 344 TEST_F(LayerUpdateTest, LayerShowHideWorks) { 345 sp<ScreenCapture> sc; 346 { 347 SCOPED_TRACE("before hide"); 348 ScreenCapture::captureScreen(&sc); 349 sc->expectBGColor(24, 24); 350 sc->expectFGColor(75, 75); 351 sc->expectBGColor(145, 145); 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->expectBGColor(24, 24); 362 sc->expectBGColor(75, 75); 363 sc->expectBGColor(145, 145); 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->expectBGColor(24, 24); 374 sc->expectFGColor(75, 75); 375 sc->expectBGColor(145, 145); 376 } 377 } 378 379 TEST_F(LayerUpdateTest, LayerSetAlphaWorks) { 380 sp<ScreenCapture> sc; 381 { 382 SCOPED_TRACE("before setAlpha"); 383 ScreenCapture::captureScreen(&sc); 384 sc->expectBGColor(24, 24); 385 sc->expectFGColor(75, 75); 386 sc->expectBGColor(145, 145); 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->expectBGColor(24, 24); 397 sc->checkPixel(75, 75, 162, 63, 96); 398 sc->expectBGColor(145, 145); 399 } 400 } 401 402 TEST_F(LayerUpdateTest, LayerSetLayerStackWorks) { 403 sp<ScreenCapture> sc; 404 { 405 SCOPED_TRACE("before setLayerStack"); 406 ScreenCapture::captureScreen(&sc); 407 sc->expectBGColor(24, 24); 408 sc->expectFGColor(75, 75); 409 sc->expectBGColor(145, 145); 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->expectBGColor(24, 24); 421 sc->expectBGColor(75, 75); 422 sc->expectBGColor(145, 145); 423 } 424 } 425 426 TEST_F(LayerUpdateTest, LayerSetFlagsWorks) { 427 sp<ScreenCapture> sc; 428 { 429 SCOPED_TRACE("before setFlags"); 430 ScreenCapture::captureScreen(&sc); 431 sc->expectBGColor(24, 24); 432 sc->expectFGColor(75, 75); 433 sc->expectBGColor(145, 145); 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->expectBGColor(24, 24); 445 sc->expectBGColor(75, 75); 446 sc->expectBGColor(145, 145); 447 } 448 } 449 450 TEST_F(LayerUpdateTest, LayerSetMatrixWorks) { 451 sp<ScreenCapture> sc; 452 { 453 SCOPED_TRACE("before setMatrix"); 454 ScreenCapture::captureScreen(&sc); 455 sc->expectBGColor(24, 24); 456 sc->expectFGColor(91, 96); 457 sc->expectFGColor(96, 101); 458 sc->expectBGColor(145, 145); 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->expectBGColor(24, 24); 469 sc->expectFGColor(91, 96); 470 sc->expectBGColor(96, 91); 471 sc->expectBGColor(145, 145); 472 } 473 } 474 475 class GeometryLatchingTest : public LayerUpdateTest { 476 protected: 477 void EXPECT_INITIAL_STATE(const char * trace) { 478 SCOPED_TRACE(trace); 479 ScreenCapture::captureScreen(&sc); 480 // We find the leading edge of the FG surface. 481 sc->expectFGColor(127, 127); 482 sc->expectBGColor(128, 128); 483 } 484 485 void lockAndFillFGBuffer() { 486 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); 487 } 488 489 void unlockFGBuffer() { 490 sp<Surface> s = mFGSurfaceControl->getSurface(); 491 ASSERT_EQ(NO_ERROR, s->unlockAndPost()); 492 waitForPostedBuffers(); 493 } 494 495 void completeFGResize() { 496 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); 497 waitForPostedBuffers(); 498 } 499 void restoreInitialState() { 500 SurfaceComposerClient::openGlobalTransaction(); 501 mFGSurfaceControl->setSize(64, 64); 502 mFGSurfaceControl->setPosition(64, 64); 503 mFGSurfaceControl->setCrop(Rect(0, 0, 64, 64)); 504 mFGSurfaceControl->setFinalCrop(Rect(0, 0, -1, -1)); 505 SurfaceComposerClient::closeGlobalTransaction(true); 506 507 EXPECT_INITIAL_STATE("After restoring initial state"); 508 } 509 sp<ScreenCapture> sc; 510 }; 511 512 TEST_F(GeometryLatchingTest, SurfacePositionLatching) { 513 EXPECT_INITIAL_STATE("before anything"); 514 515 // By default position can be updated even while 516 // a resize is pending. 517 SurfaceComposerClient::openGlobalTransaction(); 518 mFGSurfaceControl->setSize(32, 32); 519 mFGSurfaceControl->setPosition(100, 100); 520 SurfaceComposerClient::closeGlobalTransaction(true); 521 522 { 523 SCOPED_TRACE("After moving surface"); 524 ScreenCapture::captureScreen(&sc); 525 // If we moved, the FG Surface should cover up what was previously BG 526 // however if we didn't move the FG wouldn't be large enough now. 527 sc->expectFGColor(163, 163); 528 } 529 530 restoreInitialState(); 531 532 // Now we repeat with setGeometryAppliesWithResize 533 // and verify the position DOESN'T latch. 534 SurfaceComposerClient::openGlobalTransaction(); 535 mFGSurfaceControl->setGeometryAppliesWithResize(); 536 mFGSurfaceControl->setSize(32, 32); 537 mFGSurfaceControl->setPosition(100, 100); 538 SurfaceComposerClient::closeGlobalTransaction(true); 539 540 { 541 SCOPED_TRACE("While resize is pending"); 542 ScreenCapture::captureScreen(&sc); 543 // This time we shouldn't have moved, so the BG color 544 // should still be visible. 545 sc->expectBGColor(128, 128); 546 } 547 548 completeFGResize(); 549 550 { 551 SCOPED_TRACE("After the resize"); 552 ScreenCapture::captureScreen(&sc); 553 // But after the resize completes, we should move 554 // and the FG should be visible here. 555 sc->expectFGColor(128, 128); 556 } 557 } 558 559 class CropLatchingTest : public GeometryLatchingTest { 560 protected: 561 void EXPECT_CROPPED_STATE(const char* trace) { 562 SCOPED_TRACE(trace); 563 ScreenCapture::captureScreen(&sc); 564 // The edge should be moved back one pixel by our crop. 565 sc->expectFGColor(126, 126); 566 sc->expectBGColor(127, 127); 567 sc->expectBGColor(128, 128); 568 } 569 570 void EXPECT_RESIZE_STATE(const char* trace) { 571 SCOPED_TRACE(trace); 572 ScreenCapture::captureScreen(&sc); 573 // The FG is now resized too 128,128 at 64,64 574 sc->expectFGColor(64, 64); 575 sc->expectFGColor(191, 191); 576 sc->expectBGColor(192, 192); 577 } 578 }; 579 580 TEST_F(CropLatchingTest, CropLatching) { 581 EXPECT_INITIAL_STATE("before anything"); 582 // Normally the crop applies immediately even while a resize is pending. 583 SurfaceComposerClient::openGlobalTransaction(); 584 mFGSurfaceControl->setSize(128, 128); 585 mFGSurfaceControl->setCrop(Rect(0, 0, 63, 63)); 586 SurfaceComposerClient::closeGlobalTransaction(true); 587 588 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)"); 589 590 restoreInitialState(); 591 592 SurfaceComposerClient::openGlobalTransaction(); 593 mFGSurfaceControl->setSize(128, 128); 594 mFGSurfaceControl->setGeometryAppliesWithResize(); 595 mFGSurfaceControl->setCrop(Rect(0, 0, 63, 63)); 596 SurfaceComposerClient::closeGlobalTransaction(true); 597 598 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)"); 599 600 completeFGResize(); 601 602 EXPECT_CROPPED_STATE("after the resize finishes"); 603 } 604 605 TEST_F(CropLatchingTest, FinalCropLatching) { 606 EXPECT_INITIAL_STATE("before anything"); 607 // Normally the crop applies immediately even while a resize is pending. 608 SurfaceComposerClient::openGlobalTransaction(); 609 mFGSurfaceControl->setSize(128, 128); 610 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127)); 611 SurfaceComposerClient::closeGlobalTransaction(true); 612 613 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)"); 614 615 restoreInitialState(); 616 617 SurfaceComposerClient::openGlobalTransaction(); 618 mFGSurfaceControl->setSize(128, 128); 619 mFGSurfaceControl->setGeometryAppliesWithResize(); 620 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127)); 621 SurfaceComposerClient::closeGlobalTransaction(true); 622 623 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)"); 624 625 completeFGResize(); 626 627 EXPECT_CROPPED_STATE("after the resize finishes"); 628 } 629 630 // In this test we ensure that setGeometryAppliesWithResize actually demands 631 // a buffer of the new size, and not just any size. 632 TEST_F(CropLatchingTest, FinalCropLatchingBufferOldSize) { 633 EXPECT_INITIAL_STATE("before anything"); 634 // Normally the crop applies immediately even while a resize is pending. 635 SurfaceComposerClient::openGlobalTransaction(); 636 mFGSurfaceControl->setSize(128, 128); 637 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127)); 638 SurfaceComposerClient::closeGlobalTransaction(true); 639 640 EXPECT_CROPPED_STATE("after setting crop (without geometryAppliesWithResize)"); 641 642 restoreInitialState(); 643 644 // In order to prepare to submit a buffer at the wrong size, we acquire it prior to 645 // initiating the resize. 646 lockAndFillFGBuffer(); 647 648 SurfaceComposerClient::openGlobalTransaction(); 649 mFGSurfaceControl->setSize(128, 128); 650 mFGSurfaceControl->setGeometryAppliesWithResize(); 651 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127)); 652 SurfaceComposerClient::closeGlobalTransaction(true); 653 654 EXPECT_INITIAL_STATE("after setting crop (with geometryAppliesWithResize)"); 655 656 // We now submit our old buffer, at the old size, and ensure it doesn't 657 // trigger geometry latching. 658 unlockFGBuffer(); 659 660 EXPECT_INITIAL_STATE("after unlocking FG buffer (with geometryAppliesWithResize)"); 661 662 completeFGResize(); 663 664 EXPECT_CROPPED_STATE("after the resize finishes"); 665 } 666 667 TEST_F(CropLatchingTest, FinalCropLatchingRegressionForb37531386) { 668 EXPECT_INITIAL_STATE("before anything"); 669 // In this scenario, we attempt to set the final crop a second time while the resize 670 // is still pending, and ensure we are successful. Success meaning the second crop 671 // is the one which eventually latches and not the first. 672 SurfaceComposerClient::openGlobalTransaction(); 673 mFGSurfaceControl->setSize(128, 128); 674 mFGSurfaceControl->setGeometryAppliesWithResize(); 675 mFGSurfaceControl->setFinalCrop(Rect(64, 64, 127, 127)); 676 SurfaceComposerClient::closeGlobalTransaction(true); 677 678 EXPECT_INITIAL_STATE("after setting crops with geometryAppliesWithResize"); 679 680 SurfaceComposerClient::openGlobalTransaction(); 681 mFGSurfaceControl->setFinalCrop(Rect(0, 0, -1, -1)); 682 SurfaceComposerClient::closeGlobalTransaction(true); 683 684 EXPECT_INITIAL_STATE("after setting another crop"); 685 686 completeFGResize(); 687 688 EXPECT_RESIZE_STATE("after the resize finishes"); 689 } 690 691 TEST_F(LayerUpdateTest, DeferredTransactionTest) { 692 sp<ScreenCapture> sc; 693 { 694 SCOPED_TRACE("before anything"); 695 ScreenCapture::captureScreen(&sc); 696 sc->expectBGColor(32, 32); 697 sc->expectFGColor(96, 96); 698 sc->expectBGColor(160, 160); 699 } 700 701 // set up two deferred transactions on different frames 702 SurfaceComposerClient::openGlobalTransaction(); 703 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.75)); 704 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(), 705 mSyncSurfaceControl->getSurface()->getNextFrameNumber()); 706 SurfaceComposerClient::closeGlobalTransaction(true); 707 708 SurfaceComposerClient::openGlobalTransaction(); 709 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128,128)); 710 mFGSurfaceControl->deferTransactionUntil(mSyncSurfaceControl->getHandle(), 711 mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1); 712 SurfaceComposerClient::closeGlobalTransaction(true); 713 714 { 715 SCOPED_TRACE("before any trigger"); 716 ScreenCapture::captureScreen(&sc); 717 sc->expectBGColor(32, 32); 718 sc->expectFGColor(96, 96); 719 sc->expectBGColor(160, 160); 720 } 721 722 // should trigger the first deferred transaction, but not the second one 723 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); 724 { 725 SCOPED_TRACE("after first trigger"); 726 ScreenCapture::captureScreen(&sc); 727 sc->expectBGColor(32, 32); 728 sc->checkPixel(96, 96, 162, 63, 96); 729 sc->expectBGColor(160, 160); 730 } 731 732 // should show up immediately since it's not deferred 733 SurfaceComposerClient::openGlobalTransaction(); 734 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(1.0)); 735 SurfaceComposerClient::closeGlobalTransaction(true); 736 737 // trigger the second deferred transaction 738 fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31); 739 { 740 SCOPED_TRACE("after second trigger"); 741 ScreenCapture::captureScreen(&sc); 742 sc->expectBGColor(32, 32); 743 sc->expectBGColor(96, 96); 744 sc->expectFGColor(160, 160); 745 } 746 } 747 748 TEST_F(LayerUpdateTest, LayerSetRelativeLayerWorks) { 749 sp<ScreenCapture> sc; 750 { 751 SCOPED_TRACE("before adding relative surface"); 752 ScreenCapture::captureScreen(&sc); 753 sc->expectBGColor(24, 24); 754 sc->expectFGColor(75, 75); 755 sc->expectBGColor(145, 145); 756 } 757 758 auto relativeSurfaceControl = mComposerClient->createSurface( 759 String8("Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0); 760 fillSurfaceRGBA8(relativeSurfaceControl, 255, 177, 177); 761 waitForPostedBuffers(); 762 763 // Now we stack the surface above the foreground surface and make sure it is visible. 764 SurfaceComposerClient::openGlobalTransaction(); 765 relativeSurfaceControl->setPosition(64, 64); 766 relativeSurfaceControl->show(); 767 relativeSurfaceControl->setRelativeLayer(mFGSurfaceControl->getHandle(), 1); 768 SurfaceComposerClient::closeGlobalTransaction(true); 769 770 771 { 772 SCOPED_TRACE("after adding relative surface"); 773 ScreenCapture::captureScreen(&sc); 774 // our relative surface should be visible now. 775 sc->checkPixel(75, 75, 255, 177, 177); 776 } 777 778 // A call to setLayer will override a call to setRelativeLayer 779 SurfaceComposerClient::openGlobalTransaction(); 780 relativeSurfaceControl->setLayer(0); 781 SurfaceComposerClient::closeGlobalTransaction(); 782 783 { 784 SCOPED_TRACE("after set layer"); 785 ScreenCapture::captureScreen(&sc); 786 // now the FG surface should be visible again. 787 sc->expectFGColor(75, 75); 788 } 789 } 790 791 class ChildLayerTest : public LayerUpdateTest { 792 protected: 793 void SetUp() override { 794 LayerUpdateTest::SetUp(); 795 mChild = mComposerClient->createSurface( 796 String8("Child surface"), 797 10, 10, PIXEL_FORMAT_RGBA_8888, 798 0, mFGSurfaceControl.get()); 799 fillSurfaceRGBA8(mChild, 200, 200, 200); 800 801 { 802 SCOPED_TRACE("before anything"); 803 ScreenCapture::captureScreen(&mCapture); 804 mCapture->expectChildColor(64, 64); 805 } 806 } 807 void TearDown() override { 808 LayerUpdateTest::TearDown(); 809 mChild = 0; 810 } 811 812 sp<SurfaceControl> mChild; 813 sp<ScreenCapture> mCapture; 814 }; 815 816 TEST_F(ChildLayerTest, ChildLayerPositioning) { 817 SurfaceComposerClient::openGlobalTransaction(); 818 mChild->show(); 819 mChild->setPosition(10, 10); 820 mFGSurfaceControl->setPosition(64, 64); 821 SurfaceComposerClient::closeGlobalTransaction(true); 822 823 { 824 ScreenCapture::captureScreen(&mCapture); 825 // Top left of foreground must now be visible 826 mCapture->expectFGColor(64, 64); 827 // But 10 pixels in we should see the child surface 828 mCapture->expectChildColor(74, 74); 829 // And 10 more pixels we should be back to the foreground surface 830 mCapture->expectFGColor(84, 84); 831 } 832 833 SurfaceComposerClient::openGlobalTransaction(); 834 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(0, 0)); 835 SurfaceComposerClient::closeGlobalTransaction(true); 836 837 { 838 ScreenCapture::captureScreen(&mCapture); 839 // Top left of foreground should now be at 0, 0 840 mCapture->expectFGColor(0, 0); 841 // But 10 pixels in we should see the child surface 842 mCapture->expectChildColor(10, 10); 843 // And 10 more pixels we should be back to the foreground surface 844 mCapture->expectFGColor(20, 20); 845 } 846 } 847 848 TEST_F(ChildLayerTest, ChildLayerCropping) { 849 SurfaceComposerClient::openGlobalTransaction(); 850 mChild->show(); 851 mChild->setPosition(0, 0); 852 mFGSurfaceControl->setPosition(0, 0); 853 mFGSurfaceControl->setCrop(Rect(0, 0, 5, 5)); 854 SurfaceComposerClient::closeGlobalTransaction(true); 855 856 { 857 ScreenCapture::captureScreen(&mCapture); 858 mCapture->expectChildColor(0, 0); 859 mCapture->expectChildColor(4, 4); 860 mCapture->expectBGColor(5, 5); 861 } 862 } 863 864 TEST_F(ChildLayerTest, ChildLayerFinalCropping) { 865 SurfaceComposerClient::openGlobalTransaction(); 866 mChild->show(); 867 mChild->setPosition(0, 0); 868 mFGSurfaceControl->setPosition(0, 0); 869 mFGSurfaceControl->setFinalCrop(Rect(0, 0, 5, 5)); 870 SurfaceComposerClient::closeGlobalTransaction(true); 871 872 { 873 ScreenCapture::captureScreen(&mCapture); 874 mCapture->expectChildColor(0, 0); 875 mCapture->expectChildColor(4, 4); 876 mCapture->expectBGColor(5, 5); 877 } 878 } 879 880 TEST_F(ChildLayerTest, ChildLayerConstraints) { 881 SurfaceComposerClient::openGlobalTransaction(); 882 mChild->show(); 883 mFGSurfaceControl->setPosition(0, 0); 884 mChild->setPosition(63, 63); 885 SurfaceComposerClient::closeGlobalTransaction(true); 886 887 { 888 ScreenCapture::captureScreen(&mCapture); 889 mCapture->expectFGColor(0, 0); 890 // Last pixel in foreground should now be the child. 891 mCapture->expectChildColor(63, 63); 892 // But the child should be constrained and the next pixel 893 // must be the background 894 mCapture->expectBGColor(64, 64); 895 } 896 } 897 898 TEST_F(ChildLayerTest, ChildLayerScaling) { 899 SurfaceComposerClient::openGlobalTransaction(); 900 mFGSurfaceControl->setPosition(0, 0); 901 SurfaceComposerClient::closeGlobalTransaction(true); 902 903 // Find the boundary between the parent and child 904 { 905 ScreenCapture::captureScreen(&mCapture); 906 mCapture->expectChildColor(9, 9); 907 mCapture->expectFGColor(10, 10); 908 } 909 910 SurfaceComposerClient::openGlobalTransaction(); 911 mFGSurfaceControl->setMatrix(2.0, 0, 0, 2.0); 912 SurfaceComposerClient::closeGlobalTransaction(true); 913 914 // The boundary should be twice as far from the origin now. 915 // The pixels from the last test should all be child now 916 { 917 ScreenCapture::captureScreen(&mCapture); 918 mCapture->expectChildColor(9, 9); 919 mCapture->expectChildColor(10, 10); 920 mCapture->expectChildColor(19, 19); 921 mCapture->expectFGColor(20, 20); 922 } 923 } 924 925 TEST_F(ChildLayerTest, ChildLayerAlpha) { 926 fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254); 927 fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0); 928 fillSurfaceRGBA8(mChild, 0, 254, 0); 929 waitForPostedBuffers(); 930 931 SurfaceComposerClient::openGlobalTransaction(); 932 mChild->show(); 933 mChild->setPosition(0, 0); 934 mFGSurfaceControl->setPosition(0, 0); 935 SurfaceComposerClient::closeGlobalTransaction(true); 936 937 { 938 ScreenCapture::captureScreen(&mCapture); 939 // Unblended child color 940 mCapture->checkPixel(0, 0, 0, 254, 0); 941 } 942 943 SurfaceComposerClient::openGlobalTransaction(); 944 ASSERT_EQ(NO_ERROR, mChild->setAlpha(0.5)); 945 SurfaceComposerClient::closeGlobalTransaction(true); 946 947 { 948 ScreenCapture::captureScreen(&mCapture); 949 // Child and BG blended. 950 mCapture->checkPixel(0, 0, 127, 127, 0); 951 } 952 953 SurfaceComposerClient::openGlobalTransaction(); 954 ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setAlpha(0.5)); 955 SurfaceComposerClient::closeGlobalTransaction(true); 956 957 { 958 ScreenCapture::captureScreen(&mCapture); 959 // Child and BG blended. 960 mCapture->checkPixel(0, 0, 95, 64, 95); 961 } 962 } 963 964 TEST_F(ChildLayerTest, ReparentChildren) { 965 SurfaceComposerClient::openGlobalTransaction(); 966 mChild->show(); 967 mChild->setPosition(10, 10); 968 mFGSurfaceControl->setPosition(64, 64); 969 SurfaceComposerClient::closeGlobalTransaction(true); 970 971 { 972 ScreenCapture::captureScreen(&mCapture); 973 // Top left of foreground must now be visible 974 mCapture->expectFGColor(64, 64); 975 // But 10 pixels in we should see the child surface 976 mCapture->expectChildColor(74, 74); 977 // And 10 more pixels we should be back to the foreground surface 978 mCapture->expectFGColor(84, 84); 979 } 980 mFGSurfaceControl->reparentChildren(mBGSurfaceControl->getHandle()); 981 { 982 ScreenCapture::captureScreen(&mCapture); 983 mCapture->expectFGColor(64, 64); 984 // In reparenting we should have exposed the entire foreground surface. 985 mCapture->expectFGColor(74, 74); 986 // And the child layer should now begin at 10, 10 (since the BG 987 // layer is at (0, 0)). 988 mCapture->expectBGColor(9, 9); 989 mCapture->expectChildColor(10, 10); 990 } 991 } 992 993 TEST_F(ChildLayerTest, DetachChildren) { 994 SurfaceComposerClient::openGlobalTransaction(); 995 mChild->show(); 996 mChild->setPosition(10, 10); 997 mFGSurfaceControl->setPosition(64, 64); 998 SurfaceComposerClient::closeGlobalTransaction(true); 999 1000 { 1001 ScreenCapture::captureScreen(&mCapture); 1002 // Top left of foreground must now be visible 1003 mCapture->expectFGColor(64, 64); 1004 // But 10 pixels in we should see the child surface 1005 mCapture->expectChildColor(74, 74); 1006 // And 10 more pixels we should be back to the foreground surface 1007 mCapture->expectFGColor(84, 84); 1008 } 1009 1010 SurfaceComposerClient::openGlobalTransaction(); 1011 mFGSurfaceControl->detachChildren(); 1012 SurfaceComposerClient::closeGlobalTransaction(true); 1013 1014 SurfaceComposerClient::openGlobalTransaction(); 1015 mChild->hide(); 1016 SurfaceComposerClient::closeGlobalTransaction(true); 1017 1018 // Nothing should have changed. 1019 { 1020 ScreenCapture::captureScreen(&mCapture); 1021 mCapture->expectFGColor(64, 64); 1022 mCapture->expectChildColor(74, 74); 1023 mCapture->expectFGColor(84, 84); 1024 } 1025 } 1026 1027 TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) { 1028 SurfaceComposerClient::openGlobalTransaction(); 1029 mChild->show(); 1030 mChild->setPosition(0, 0); 1031 mFGSurfaceControl->setPosition(0, 0); 1032 SurfaceComposerClient::closeGlobalTransaction(true); 1033 1034 { 1035 ScreenCapture::captureScreen(&mCapture); 1036 // We've positioned the child in the top left. 1037 mCapture->expectChildColor(0, 0); 1038 // But it's only 10x10. 1039 mCapture->expectFGColor(10, 10); 1040 } 1041 1042 SurfaceComposerClient::openGlobalTransaction(); 1043 mFGSurfaceControl->setOverrideScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); 1044 // We cause scaling by 2. 1045 mFGSurfaceControl->setSize(128, 128); 1046 SurfaceComposerClient::closeGlobalTransaction(); 1047 1048 { 1049 ScreenCapture::captureScreen(&mCapture); 1050 // We've positioned the child in the top left. 1051 mCapture->expectChildColor(0, 0); 1052 mCapture->expectChildColor(10, 10); 1053 mCapture->expectChildColor(19, 19); 1054 // And now it should be scaled all the way to 20x20 1055 mCapture->expectFGColor(20, 20); 1056 } 1057 } 1058 1059 // Regression test for b/37673612 1060 TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) { 1061 SurfaceComposerClient::openGlobalTransaction(); 1062 mChild->show(); 1063 mChild->setPosition(0, 0); 1064 mFGSurfaceControl->setPosition(0, 0); 1065 SurfaceComposerClient::closeGlobalTransaction(true); 1066 1067 { 1068 ScreenCapture::captureScreen(&mCapture); 1069 // We've positioned the child in the top left. 1070 mCapture->expectChildColor(0, 0); 1071 // But it's only 10x10. 1072 mCapture->expectFGColor(10, 10); 1073 } 1074 1075 1076 // We set things up as in b/37673612 so that there is a mismatch between the buffer size and 1077 // the WM specified state size. 1078 mFGSurfaceControl->setSize(128, 64); 1079 sp<Surface> s = mFGSurfaceControl->getSurface(); 1080 auto anw = static_cast<ANativeWindow*>(s.get()); 1081 native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90); 1082 native_window_set_buffers_dimensions(anw, 64, 128); 1083 fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63); 1084 waitForPostedBuffers(); 1085 1086 { 1087 // The child should still be in the same place and not have any strange scaling as in 1088 // b/37673612. 1089 ScreenCapture::captureScreen(&mCapture); 1090 mCapture->expectChildColor(0, 0); 1091 mCapture->expectFGColor(10, 10); 1092 } 1093 } 1094 1095 TEST_F(ChildLayerTest, Bug36858924) { 1096 // Destroy the child layer 1097 mChild.clear(); 1098 1099 // Now recreate it as hidden 1100 mChild = mComposerClient->createSurface(String8("Child surface"), 10, 10, 1101 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eHidden, 1102 mFGSurfaceControl.get()); 1103 1104 // Show the child layer in a deferred transaction 1105 SurfaceComposerClient::openGlobalTransaction(); 1106 mChild->deferTransactionUntil(mFGSurfaceControl->getHandle(), 1107 mFGSurfaceControl->getSurface()->getNextFrameNumber()); 1108 mChild->show(); 1109 SurfaceComposerClient::closeGlobalTransaction(true); 1110 1111 // Render the foreground surface a few times 1112 // 1113 // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third 1114 // frame because SurfaceFlinger would never process the deferred transaction and would therefore 1115 // never acquire/release the first buffer 1116 ALOGI("Filling 1"); 1117 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0); 1118 ALOGI("Filling 2"); 1119 fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255); 1120 ALOGI("Filling 3"); 1121 fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0); 1122 ALOGI("Filling 4"); 1123 fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0); 1124 } 1125 1126 } 1127