1 /*
2  * Copyright (C) 2017 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 
21 // #define LOG_NDEBUG 0
22 #undef LOG_TAG
23 #define LOG_TAG "FakeHwcTest"
24 
25 #include "FakeComposerClient.h"
26 #include "FakeComposerService.h"
27 #include "FakeComposerUtils.h"
28 #include "MockComposerHal.h"
29 
30 #include <gui/DisplayEventReceiver.h>
31 #include <gui/ISurfaceComposer.h>
32 #include <gui/LayerDebugInfo.h>
33 #include <gui/LayerState.h>
34 #include <gui/Surface.h>
35 #include <gui/SurfaceComposerClient.h>
36 
37 #include <android/hidl/manager/1.0/IServiceManager.h>
38 #include <android/looper.h>
39 #include <android/native_window.h>
40 #include <binder/ProcessState.h>
41 #include <hwbinder/ProcessState.h>
42 #include <log/log.h>
43 #include <private/gui/ComposerService.h>
44 #include <ui/DisplayConfig.h>
45 #include <utils/Looper.h>
46 
47 #include <gmock/gmock.h>
48 #include <gtest/gtest.h>
49 
50 #include <limits>
51 #include <thread>
52 
53 using namespace std::chrono_literals;
54 
55 using namespace android;
56 using namespace android::hardware;
57 
58 using namespace sftest;
59 
60 namespace {
61 
62 // Mock test helpers
63 using ::testing::_;
64 using ::testing::AtLeast;
65 using ::testing::DoAll;
66 using ::testing::Invoke;
67 using ::testing::Return;
68 using ::testing::SetArgPointee;
69 
70 using Transaction = SurfaceComposerClient::Transaction;
71 using Attribute = V2_4::IComposerClient::Attribute;
72 
73 ///////////////////////////////////////////////
74 
75 struct TestColor {
76 public:
77     uint8_t r;
78     uint8_t g;
79     uint8_t b;
80     uint8_t a;
81 };
82 
83 constexpr static TestColor RED = {195, 63, 63, 255};
84 constexpr static TestColor LIGHT_RED = {255, 177, 177, 255};
85 constexpr static TestColor GREEN = {63, 195, 63, 255};
86 constexpr static TestColor BLUE = {63, 63, 195, 255};
87 constexpr static TestColor DARK_GRAY = {63, 63, 63, 255};
88 constexpr static TestColor LIGHT_GRAY = {200, 200, 200, 255};
89 
90 // Fill an RGBA_8888 formatted surface with a single color.
fillSurfaceRGBA8(const sp<SurfaceControl> & sc,const TestColor & color,bool unlock=true)91 static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, const TestColor& color,
92                              bool unlock = true) {
93     ANativeWindow_Buffer outBuffer;
94     sp<Surface> s = sc->getSurface();
95     ASSERT_TRUE(s != nullptr);
96     ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
97     uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
98     for (int y = 0; y < outBuffer.height; y++) {
99         for (int x = 0; x < outBuffer.width; x++) {
100             uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
101             pixel[0] = color.r;
102             pixel[1] = color.g;
103             pixel[2] = color.b;
104             pixel[3] = color.a;
105         }
106     }
107     if (unlock) {
108         ASSERT_EQ(NO_ERROR, s->unlockAndPost());
109     }
110 }
111 
makeSimpleRect(int left,int top,int right,int bottom)112 inline RenderState makeSimpleRect(int left, int top, int right, int bottom) {
113     RenderState res;
114     res.mDisplayFrame = hwc_rect_t{left, top, right, bottom};
115     res.mPlaneAlpha = 1.0f;
116     res.mSwapCount = 0;
117     res.mSourceCrop = hwc_frect_t{0.f, 0.f, static_cast<float>(right - left),
118                                   static_cast<float>(bottom - top)};
119     return res;
120 }
121 
makeSimpleRect(unsigned int left,unsigned int top,unsigned int right,unsigned int bottom)122 inline RenderState makeSimpleRect(unsigned int left, unsigned int top, unsigned int right,
123                                   unsigned int bottom) {
124     EXPECT_LE(left, static_cast<unsigned int>(INT_MAX));
125     EXPECT_LE(top, static_cast<unsigned int>(INT_MAX));
126     EXPECT_LE(right, static_cast<unsigned int>(INT_MAX));
127     EXPECT_LE(bottom, static_cast<unsigned int>(INT_MAX));
128     return makeSimpleRect(static_cast<int>(left), static_cast<int>(top), static_cast<int>(right),
129                           static_cast<int>(bottom));
130 }
131 
132 ///////////////////////////////////////////////
133 template <typename FakeComposerService>
134 class DisplayTest : public ::testing::Test {
135 protected:
136     struct TestConfig {
137         int32_t id;
138         int32_t w;
139         int32_t h;
140         int32_t vsyncPeriod;
141         int32_t group;
142     };
143 
processDisplayEvents(int,int,void * data)144     static int processDisplayEvents(int /*fd*/, int /*events*/, void* data) {
145         auto self = static_cast<DisplayTest*>(data);
146 
147         ssize_t n;
148         DisplayEventReceiver::Event buffer[1];
149 
150         while ((n = self->mReceiver->getEvents(buffer, 1)) > 0) {
151             for (int i = 0; i < n; i++) {
152                 self->mReceivedDisplayEvents.push_back(buffer[i]);
153             }
154         }
155         ALOGD_IF(n < 0, "Error reading events (%s)\n", strerror(-n));
156         return 1;
157     }
158 
getDisplayAttributeNoMock(Display display,Config config,V2_4::IComposerClient::Attribute attribute,int32_t * outValue)159     Error getDisplayAttributeNoMock(Display display, Config config,
160                                     V2_4::IComposerClient::Attribute attribute, int32_t* outValue) {
161         mFakeComposerClient->setMockHal(nullptr);
162         auto ret =
163                 mFakeComposerClient->getDisplayAttribute_2_4(display, config, attribute, outValue);
164         mFakeComposerClient->setMockHal(mMockComposer.get());
165         return ret;
166     }
167 
setExpectationsForConfigs(Display display,std::vector<TestConfig> testConfigs,Config activeConfig,V2_4::VsyncPeriodNanos defaultVsyncPeriod)168     void setExpectationsForConfigs(Display display, std::vector<TestConfig> testConfigs,
169                                    Config activeConfig, V2_4::VsyncPeriodNanos defaultVsyncPeriod) {
170         std::vector<Config> configIds;
171         for (int i = 0; i < testConfigs.size(); i++) {
172             configIds.push_back(testConfigs[i].id);
173 
174             EXPECT_CALL(*mMockComposer,
175                         getDisplayAttribute_2_4(display, testConfigs[i].id, Attribute::WIDTH, _))
176                     .WillRepeatedly(DoAll(SetArgPointee<3>(testConfigs[i].w), Return(Error::NONE)));
177             EXPECT_CALL(*mMockComposer,
178                         getDisplayAttribute_2_4(display, testConfigs[i].id, Attribute::HEIGHT, _))
179                     .WillRepeatedly(DoAll(SetArgPointee<3>(testConfigs[i].h), Return(Error::NONE)));
180             EXPECT_CALL(*mMockComposer,
181                         getDisplayAttribute_2_4(display, testConfigs[i].id, Attribute::VSYNC_PERIOD,
182                                                 _))
183                     .WillRepeatedly(DoAll(SetArgPointee<3>(testConfigs[i].vsyncPeriod),
184                                           Return(Error::NONE)));
185             EXPECT_CALL(*mMockComposer,
186                         getDisplayAttribute_2_4(display, testConfigs[i].id, Attribute::CONFIG_GROUP,
187                                                 _))
188                     .WillRepeatedly(
189                             DoAll(SetArgPointee<3>(testConfigs[i].group), Return(Error::NONE)));
190             EXPECT_CALL(*mMockComposer,
191                         getDisplayAttribute_2_4(display, testConfigs[i].id, Attribute::DPI_X, _))
192                     .WillRepeatedly(Return(Error::UNSUPPORTED));
193             EXPECT_CALL(*mMockComposer,
194                         getDisplayAttribute_2_4(display, testConfigs[i].id, Attribute::DPI_Y, _))
195                     .WillRepeatedly(Return(Error::UNSUPPORTED));
196         }
197 
198         EXPECT_CALL(*mMockComposer, getDisplayConfigs(display, _))
199                 .WillRepeatedly(DoAll(SetArgPointee<1>(hidl_vec<Config>(configIds)),
200                                       Return(V2_1::Error::NONE)));
201 
202         EXPECT_CALL(*mMockComposer, getActiveConfig(display, _))
203                 .WillRepeatedly(DoAll(SetArgPointee<1>(activeConfig), Return(V2_1::Error::NONE)));
204 
205         EXPECT_CALL(*mMockComposer, getDisplayVsyncPeriod(display, _))
206                 .WillRepeatedly(
207                         DoAll(SetArgPointee<1>(defaultVsyncPeriod), Return(V2_4::Error::NONE)));
208     }
209 
SetUp()210     void SetUp() override {
211         mMockComposer = std::make_unique<MockComposerHal>();
212         mFakeComposerClient = new FakeComposerClient();
213         mFakeComposerClient->setMockHal(mMockComposer.get());
214 
215         sp<V2_4::hal::ComposerClient> client = new V2_4::hal::ComposerClient(mFakeComposerClient);
216         mFakeService = new FakeComposerService(client);
217         ASSERT_EQ(android::OK, mFakeService->registerAsService("mock"));
218 
219         android::hardware::ProcessState::self()->startThreadPool();
220         android::ProcessState::self()->startThreadPool();
221 
222         setExpectationsForConfigs(PRIMARY_DISPLAY,
223                                   {{
224                                           .id = 1,
225                                           .w = 1920,
226                                           .h = 1024,
227                                           .vsyncPeriod = 16'666'666,
228                                           .group = 0,
229                                   }},
230                                   1, 16'666'666);
231 
232         startSurfaceFlinger();
233 
234         // Fake composer wants to enable VSync injection
235         mFakeComposerClient->onSurfaceFlingerStart();
236 
237         mComposerClient = new SurfaceComposerClient;
238         ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
239 
240         mReceiver.reset(new DisplayEventReceiver(ISurfaceComposer::eVsyncSourceApp,
241                                                  ISurfaceComposer::eConfigChangedDispatch));
242         mLooper = new Looper(false);
243         mLooper->addFd(mReceiver->getFd(), 0, ALOOPER_EVENT_INPUT, processDisplayEvents, this);
244     }
245 
TearDown()246     void TearDown() override {
247         mLooper = nullptr;
248         mReceiver = nullptr;
249 
250         mComposerClient->dispose();
251         mComposerClient = nullptr;
252 
253         // Fake composer needs to release SurfaceComposerClient before the stop.
254         mFakeComposerClient->onSurfaceFlingerStop();
255         stopSurfaceFlinger();
256 
257         mFakeComposerClient->setMockHal(nullptr);
258 
259         mFakeService = nullptr;
260         // TODO: Currently deleted in FakeComposerClient::removeClient(). Devise better lifetime
261         // management.
262         mMockComposer = nullptr;
263     }
264 
waitForDisplayTransaction()265     void waitForDisplayTransaction() {
266         // Both a refresh and a vsync event are needed to apply pending display
267         // transactions.
268         mFakeComposerClient->refreshDisplay(EXTERNAL_DISPLAY);
269         mFakeComposerClient->runVSyncAndWait();
270 
271         // Extra vsync and wait to avoid a 10% flake due to a race.
272         mFakeComposerClient->runVSyncAndWait();
273     }
274 
waitForHotplugEvent(PhysicalDisplayId displayId,bool connected)275     bool waitForHotplugEvent(PhysicalDisplayId displayId, bool connected) {
276         int waitCount = 20;
277         while (waitCount--) {
278             while (!mReceivedDisplayEvents.empty()) {
279                 auto event = mReceivedDisplayEvents.front();
280                 mReceivedDisplayEvents.pop_front();
281 
282                 ALOGV_IF(event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG,
283                          "event hotplug: displayId %" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT
284                          ", connected %d\t",
285                          event.header.displayId, event.hotplug.connected);
286 
287                 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG &&
288                     event.header.displayId == displayId && event.hotplug.connected == connected) {
289                     return true;
290                 }
291             }
292 
293             mLooper->pollOnce(1);
294         }
295         return false;
296     }
297 
waitForConfigChangedEvent(PhysicalDisplayId displayId,int32_t configId)298     bool waitForConfigChangedEvent(PhysicalDisplayId displayId, int32_t configId) {
299         int waitCount = 20;
300         while (waitCount--) {
301             while (!mReceivedDisplayEvents.empty()) {
302                 auto event = mReceivedDisplayEvents.front();
303                 mReceivedDisplayEvents.pop_front();
304 
305                 ALOGV_IF(event.header.type == DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED,
306                          "event config: displayId %" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT
307                          ", configId %d\t",
308                          event.header.displayId, event.config.configId);
309 
310                 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED &&
311                     event.header.displayId == displayId && event.config.configId == configId) {
312                     return true;
313                 }
314             }
315 
316             mLooper->pollOnce(1);
317         }
318         return false;
319     }
320 
Test_HotplugOneConfig()321     void Test_HotplugOneConfig() {
322         ALOGD("DisplayTest::Test_Hotplug_oneConfig");
323 
324         setExpectationsForConfigs(EXTERNAL_DISPLAY,
325                                   {{.id = 1,
326                                     .w = 200,
327                                     .h = 400,
328                                     .vsyncPeriod = 16'666'666,
329                                     .group = 0}},
330                                   1, 16'666'666);
331 
332         mFakeComposerClient->hotplugDisplay(EXTERNAL_DISPLAY,
333                                             V2_1::IComposerCallback::Connection::CONNECTED);
334         waitForDisplayTransaction();
335         EXPECT_TRUE(waitForHotplugEvent(EXTERNAL_DISPLAY, true));
336 
337         {
338             const auto display = SurfaceComposerClient::getPhysicalDisplayToken(EXTERNAL_DISPLAY);
339             EXPECT_FALSE(display == nullptr);
340 
341             DisplayConfig config;
342             EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
343             const ui::Size& resolution = config.resolution;
344             EXPECT_EQ(ui::Size(200, 400), resolution);
345             EXPECT_EQ(1e9f / 16'666'666, config.refreshRate);
346 
347             auto surfaceControl =
348                     mComposerClient->createSurface(String8("Display Test Surface Foo"),
349                                                    resolution.getWidth(), resolution.getHeight(),
350                                                    PIXEL_FORMAT_RGBA_8888, 0);
351             EXPECT_TRUE(surfaceControl != nullptr);
352             EXPECT_TRUE(surfaceControl->isValid());
353             fillSurfaceRGBA8(surfaceControl, BLUE);
354 
355             {
356                 TransactionScope ts(*mFakeComposerClient);
357                 ts.setDisplayLayerStack(display, 0);
358 
359                 ts.setLayer(surfaceControl, INT32_MAX - 2).show(surfaceControl);
360             }
361         }
362 
363         mFakeComposerClient->hotplugDisplay(EXTERNAL_DISPLAY,
364                                             V2_1::IComposerCallback::Connection::DISCONNECTED);
365         waitForDisplayTransaction();
366         mFakeComposerClient->clearFrames();
367         EXPECT_TRUE(waitForHotplugEvent(EXTERNAL_DISPLAY, false));
368 
369         {
370             const auto display = SurfaceComposerClient::getPhysicalDisplayToken(EXTERNAL_DISPLAY);
371             EXPECT_TRUE(display == nullptr);
372 
373             DisplayConfig config;
374             EXPECT_NE(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
375         }
376     }
377 
Test_HotplugTwoSeparateConfigs()378     void Test_HotplugTwoSeparateConfigs() {
379         ALOGD("DisplayTest::Test_HotplugTwoSeparateConfigs");
380 
381         setExpectationsForConfigs(EXTERNAL_DISPLAY,
382                                   {{.id = 1,
383                                     .w = 200,
384                                     .h = 400,
385                                     .vsyncPeriod = 16'666'666,
386                                     .group = 0},
387                                    {.id = 2,
388                                     .w = 800,
389                                     .h = 1600,
390                                     .vsyncPeriod = 11'111'111,
391                                     .group = 1}},
392                                   1, 16'666'666);
393 
394         mFakeComposerClient->hotplugDisplay(EXTERNAL_DISPLAY,
395                                             V2_1::IComposerCallback::Connection::CONNECTED);
396         waitForDisplayTransaction();
397         EXPECT_TRUE(waitForHotplugEvent(EXTERNAL_DISPLAY, true));
398 
399         const auto display = SurfaceComposerClient::getPhysicalDisplayToken(EXTERNAL_DISPLAY);
400         EXPECT_FALSE(display == nullptr);
401 
402         DisplayConfig config;
403         EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
404         EXPECT_EQ(ui::Size(200, 400), config.resolution);
405         EXPECT_EQ(1e9f / 16'666'666, config.refreshRate);
406 
407         mFakeComposerClient->clearFrames();
408         {
409             const ui::Size& resolution = config.resolution;
410             auto surfaceControl =
411                     mComposerClient->createSurface(String8("Display Test Surface Foo"),
412                                                    resolution.getWidth(), resolution.getHeight(),
413                                                    PIXEL_FORMAT_RGBA_8888, 0);
414             EXPECT_TRUE(surfaceControl != nullptr);
415             EXPECT_TRUE(surfaceControl->isValid());
416             fillSurfaceRGBA8(surfaceControl, BLUE);
417 
418             {
419                 TransactionScope ts(*mFakeComposerClient);
420                 ts.setDisplayLayerStack(display, 0);
421 
422                 ts.setLayer(surfaceControl, INT32_MAX - 2).show(surfaceControl);
423             }
424         }
425 
426         Vector<DisplayConfig> configs;
427         EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayConfigs(display, &configs));
428         EXPECT_EQ(configs.size(), 2);
429 
430         // change active config
431 
432         if (mIs2_4Client) {
433             EXPECT_CALL(*mMockComposer, setActiveConfigWithConstraints(EXTERNAL_DISPLAY, 2, _, _))
434                     .WillOnce(Return(V2_4::Error::NONE));
435         } else {
436             EXPECT_CALL(*mMockComposer, setActiveConfig(EXTERNAL_DISPLAY, 2))
437                     .WillOnce(Return(V2_1::Error::NONE));
438         }
439 
440         for (int i = 0; i < configs.size(); i++) {
441             const auto& config = configs[i];
442             if (config.resolution.getWidth() == 800) {
443                 EXPECT_EQ(NO_ERROR,
444                           SurfaceComposerClient::setDesiredDisplayConfigSpecs(display, i,
445                                                                               config.refreshRate,
446                                                                               config.refreshRate,
447                                                                               config.refreshRate,
448                                                                               config.refreshRate));
449                 waitForDisplayTransaction();
450                 EXPECT_TRUE(waitForConfigChangedEvent(EXTERNAL_DISPLAY, i));
451                 break;
452             }
453         }
454 
455         EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
456         EXPECT_EQ(ui::Size(800, 1600), config.resolution);
457         EXPECT_EQ(1e9f / 11'111'111, config.refreshRate);
458 
459         mFakeComposerClient->clearFrames();
460         {
461             const ui::Size& resolution = config.resolution;
462             auto surfaceControl =
463                     mComposerClient->createSurface(String8("Display Test Surface Foo"),
464                                                    resolution.getWidth(), resolution.getHeight(),
465                                                    PIXEL_FORMAT_RGBA_8888, 0);
466             EXPECT_TRUE(surfaceControl != nullptr);
467             EXPECT_TRUE(surfaceControl->isValid());
468             fillSurfaceRGBA8(surfaceControl, BLUE);
469 
470             {
471                 TransactionScope ts(*mFakeComposerClient);
472                 ts.setDisplayLayerStack(display, 0);
473 
474                 ts.setLayer(surfaceControl, INT32_MAX - 2).show(surfaceControl);
475             }
476         }
477 
478         mFakeComposerClient->hotplugDisplay(EXTERNAL_DISPLAY,
479                                             V2_1::IComposerCallback::Connection::DISCONNECTED);
480         waitForDisplayTransaction();
481         mFakeComposerClient->clearFrames();
482         EXPECT_TRUE(waitForHotplugEvent(EXTERNAL_DISPLAY, false));
483     }
484 
Test_HotplugTwoConfigsSameGroup()485     void Test_HotplugTwoConfigsSameGroup() {
486         ALOGD("DisplayTest::Test_HotplugTwoConfigsSameGroup");
487 
488         setExpectationsForConfigs(EXTERNAL_DISPLAY,
489                                   {{.id = 2,
490                                     .w = 800,
491                                     .h = 1600,
492                                     .vsyncPeriod = 16'666'666,
493                                     .group = 31},
494                                    {.id = 3,
495                                     .w = 800,
496                                     .h = 1600,
497                                     .vsyncPeriod = 11'111'111,
498                                     .group = 31}},
499                                   2, 16'666'666);
500 
501         mFakeComposerClient->hotplugDisplay(EXTERNAL_DISPLAY,
502                                             V2_1::IComposerCallback::Connection::CONNECTED);
503         waitForDisplayTransaction();
504         EXPECT_TRUE(waitForHotplugEvent(EXTERNAL_DISPLAY, true));
505 
506         const auto display = SurfaceComposerClient::getPhysicalDisplayToken(EXTERNAL_DISPLAY);
507         EXPECT_FALSE(display == nullptr);
508 
509         DisplayConfig config;
510         EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
511         EXPECT_EQ(ui::Size(800, 1600), config.resolution);
512         EXPECT_EQ(1e9f / 16'666'666, config.refreshRate);
513 
514         mFakeComposerClient->clearFrames();
515         {
516             const ui::Size& resolution = config.resolution;
517             auto surfaceControl =
518                     mComposerClient->createSurface(String8("Display Test Surface Foo"),
519                                                    resolution.getWidth(), resolution.getHeight(),
520                                                    PIXEL_FORMAT_RGBA_8888, 0);
521             EXPECT_TRUE(surfaceControl != nullptr);
522             EXPECT_TRUE(surfaceControl->isValid());
523             fillSurfaceRGBA8(surfaceControl, BLUE);
524 
525             {
526                 TransactionScope ts(*mFakeComposerClient);
527                 ts.setDisplayLayerStack(display, 0);
528 
529                 ts.setLayer(surfaceControl, INT32_MAX - 2).show(surfaceControl);
530             }
531         }
532 
533         Vector<DisplayConfig> configs;
534         EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayConfigs(display, &configs));
535         EXPECT_EQ(configs.size(), 2);
536 
537         // change active config
538         if (mIs2_4Client) {
539             EXPECT_CALL(*mMockComposer, setActiveConfigWithConstraints(EXTERNAL_DISPLAY, 3, _, _))
540                     .WillOnce(Return(V2_4::Error::NONE));
541         } else {
542             EXPECT_CALL(*mMockComposer, setActiveConfig(EXTERNAL_DISPLAY, 3))
543                     .WillOnce(Return(V2_1::Error::NONE));
544         }
545 
546         for (int i = 0; i < configs.size(); i++) {
547             const auto& config = configs[i];
548             if (config.refreshRate == 1e9f / 11'111'111) {
549                 EXPECT_EQ(NO_ERROR,
550                           SurfaceComposerClient::setDesiredDisplayConfigSpecs(display, i,
551                                                                               config.refreshRate,
552                                                                               config.refreshRate,
553                                                                               config.refreshRate,
554                                                                               config.refreshRate));
555                 waitForDisplayTransaction();
556                 EXPECT_TRUE(waitForConfigChangedEvent(EXTERNAL_DISPLAY, i));
557                 break;
558             }
559         }
560 
561         EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
562         EXPECT_EQ(ui::Size(800, 1600), config.resolution);
563         EXPECT_EQ(1e9f / 11'111'111, config.refreshRate);
564 
565         mFakeComposerClient->clearFrames();
566         {
567             const ui::Size& resolution = config.resolution;
568             auto surfaceControl =
569                     mComposerClient->createSurface(String8("Display Test Surface Foo"),
570                                                    resolution.getWidth(), resolution.getHeight(),
571                                                    PIXEL_FORMAT_RGBA_8888, 0);
572             EXPECT_TRUE(surfaceControl != nullptr);
573             EXPECT_TRUE(surfaceControl->isValid());
574             fillSurfaceRGBA8(surfaceControl, BLUE);
575 
576             {
577                 TransactionScope ts(*mFakeComposerClient);
578                 ts.setDisplayLayerStack(display, 0);
579 
580                 ts.setLayer(surfaceControl, INT32_MAX - 2).show(surfaceControl);
581             }
582         }
583 
584         mFakeComposerClient->hotplugDisplay(EXTERNAL_DISPLAY,
585                                             V2_1::IComposerCallback::Connection::DISCONNECTED);
586         waitForDisplayTransaction();
587         mFakeComposerClient->clearFrames();
588         EXPECT_TRUE(waitForHotplugEvent(EXTERNAL_DISPLAY, false));
589     }
590 
Test_HotplugThreeConfigsMixedGroups()591     void Test_HotplugThreeConfigsMixedGroups() {
592         ALOGD("DisplayTest::Test_HotplugThreeConfigsMixedGroups");
593 
594         setExpectationsForConfigs(EXTERNAL_DISPLAY,
595                                   {{.id = 2,
596                                     .w = 800,
597                                     .h = 1600,
598                                     .vsyncPeriod = 16'666'666,
599                                     .group = 0},
600                                    {.id = 3,
601                                     .w = 800,
602                                     .h = 1600,
603                                     .vsyncPeriod = 11'111'111,
604                                     .group = 0},
605                                    {.id = 4,
606                                     .w = 1600,
607                                     .h = 3200,
608                                     .vsyncPeriod = 8'333'333,
609                                     .group = 1},
610                                    {.id = 5,
611                                     .w = 1600,
612                                     .h = 3200,
613                                     .vsyncPeriod = 11'111'111,
614                                     .group = 1}},
615                                   2, 16'666'666);
616 
617         mFakeComposerClient->hotplugDisplay(EXTERNAL_DISPLAY,
618                                             V2_1::IComposerCallback::Connection::CONNECTED);
619         waitForDisplayTransaction();
620         EXPECT_TRUE(waitForHotplugEvent(EXTERNAL_DISPLAY, true));
621 
622         const auto display = SurfaceComposerClient::getPhysicalDisplayToken(EXTERNAL_DISPLAY);
623         EXPECT_FALSE(display == nullptr);
624 
625         DisplayConfig config;
626         EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
627         EXPECT_EQ(ui::Size(800, 1600), config.resolution);
628         EXPECT_EQ(1e9f / 16'666'666, config.refreshRate);
629 
630         mFakeComposerClient->clearFrames();
631         {
632             const ui::Size& resolution = config.resolution;
633             auto surfaceControl =
634                     mComposerClient->createSurface(String8("Display Test Surface Foo"),
635                                                    resolution.getWidth(), resolution.getHeight(),
636                                                    PIXEL_FORMAT_RGBA_8888, 0);
637             EXPECT_TRUE(surfaceControl != nullptr);
638             EXPECT_TRUE(surfaceControl->isValid());
639             fillSurfaceRGBA8(surfaceControl, BLUE);
640 
641             {
642                 TransactionScope ts(*mFakeComposerClient);
643                 ts.setDisplayLayerStack(display, 0);
644 
645                 ts.setLayer(surfaceControl, INT32_MAX - 2).show(surfaceControl);
646             }
647         }
648 
649         Vector<DisplayConfig> configs;
650         EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayConfigs(display, &configs));
651         EXPECT_EQ(configs.size(), 4);
652 
653         // change active config to 800x1600@90Hz
654         if (mIs2_4Client) {
655             EXPECT_CALL(*mMockComposer, setActiveConfigWithConstraints(EXTERNAL_DISPLAY, 3, _, _))
656                     .WillOnce(Return(V2_4::Error::NONE));
657         } else {
658             EXPECT_CALL(*mMockComposer, setActiveConfig(EXTERNAL_DISPLAY, 3))
659                     .WillOnce(Return(V2_1::Error::NONE));
660         }
661 
662         for (int i = 0; i < configs.size(); i++) {
663             const auto& config = configs[i];
664             if (config.resolution.getWidth() == 800 && config.refreshRate == 1e9f / 11'111'111) {
665                 EXPECT_EQ(NO_ERROR,
666                           SurfaceComposerClient::
667                                   setDesiredDisplayConfigSpecs(display, i, configs[i].refreshRate,
668                                                                configs[i].refreshRate,
669                                                                configs[i].refreshRate,
670                                                                configs[i].refreshRate));
671                 waitForDisplayTransaction();
672                 EXPECT_TRUE(waitForConfigChangedEvent(EXTERNAL_DISPLAY, i));
673                 break;
674             }
675         }
676 
677         EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
678         EXPECT_EQ(ui::Size(800, 1600), config.resolution);
679         EXPECT_EQ(1e9f / 11'111'111, config.refreshRate);
680 
681         mFakeComposerClient->clearFrames();
682         {
683             const ui::Size& resolution = config.resolution;
684             auto surfaceControl =
685                     mComposerClient->createSurface(String8("Display Test Surface Foo"),
686                                                    resolution.getWidth(), resolution.getHeight(),
687                                                    PIXEL_FORMAT_RGBA_8888, 0);
688             EXPECT_TRUE(surfaceControl != nullptr);
689             EXPECT_TRUE(surfaceControl->isValid());
690             fillSurfaceRGBA8(surfaceControl, BLUE);
691 
692             {
693                 TransactionScope ts(*mFakeComposerClient);
694                 ts.setDisplayLayerStack(display, 0);
695 
696                 ts.setLayer(surfaceControl, INT32_MAX - 2).show(surfaceControl);
697             }
698         }
699 
700         // change active config to 1600x3200@120Hz
701         if (mIs2_4Client) {
702             EXPECT_CALL(*mMockComposer, setActiveConfigWithConstraints(EXTERNAL_DISPLAY, 4, _, _))
703                     .WillOnce(Return(V2_4::Error::NONE));
704         } else {
705             EXPECT_CALL(*mMockComposer, setActiveConfig(EXTERNAL_DISPLAY, 4))
706                     .WillOnce(Return(V2_1::Error::NONE));
707         }
708 
709         for (int i = 0; i < configs.size(); i++) {
710             const auto& config = configs[i];
711             if (config.refreshRate == 1e9f / 8'333'333) {
712                 EXPECT_EQ(NO_ERROR,
713                           SurfaceComposerClient::setDesiredDisplayConfigSpecs(display, i,
714                                                                               config.refreshRate,
715                                                                               config.refreshRate,
716                                                                               config.refreshRate,
717                                                                               config.refreshRate));
718                 waitForDisplayTransaction();
719                 EXPECT_TRUE(waitForConfigChangedEvent(EXTERNAL_DISPLAY, i));
720                 break;
721             }
722         }
723 
724         EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
725         EXPECT_EQ(ui::Size(1600, 3200), config.resolution);
726         EXPECT_EQ(1e9f / 8'333'333, config.refreshRate);
727 
728         mFakeComposerClient->clearFrames();
729         {
730             const ui::Size& resolution = config.resolution;
731             auto surfaceControl =
732                     mComposerClient->createSurface(String8("Display Test Surface Foo"),
733                                                    resolution.getWidth(), resolution.getHeight(),
734                                                    PIXEL_FORMAT_RGBA_8888, 0);
735             EXPECT_TRUE(surfaceControl != nullptr);
736             EXPECT_TRUE(surfaceControl->isValid());
737             fillSurfaceRGBA8(surfaceControl, BLUE);
738 
739             {
740                 TransactionScope ts(*mFakeComposerClient);
741                 ts.setDisplayLayerStack(display, 0);
742 
743                 ts.setLayer(surfaceControl, INT32_MAX - 2).show(surfaceControl);
744             }
745         }
746 
747         // change active config to 1600x3200@90Hz
748         if (mIs2_4Client) {
749             EXPECT_CALL(*mMockComposer, setActiveConfigWithConstraints(EXTERNAL_DISPLAY, 5, _, _))
750                     .WillOnce(Return(V2_4::Error::NONE));
751         } else {
752             EXPECT_CALL(*mMockComposer, setActiveConfig(EXTERNAL_DISPLAY, 5))
753                     .WillOnce(Return(V2_1::Error::NONE));
754         }
755 
756         for (int i = 0; i < configs.size(); i++) {
757             const auto& config = configs[i];
758             if (config.resolution.getWidth() == 1600 && config.refreshRate == 1e9f / 11'111'111) {
759                 EXPECT_EQ(NO_ERROR,
760                           SurfaceComposerClient::setDesiredDisplayConfigSpecs(display, i,
761                                                                               config.refreshRate,
762                                                                               config.refreshRate,
763                                                                               config.refreshRate,
764                                                                               config.refreshRate));
765                 waitForDisplayTransaction();
766                 EXPECT_TRUE(waitForConfigChangedEvent(EXTERNAL_DISPLAY, i));
767                 break;
768             }
769         }
770 
771         EXPECT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
772         EXPECT_EQ(ui::Size(1600, 3200), config.resolution);
773         EXPECT_EQ(1e9f / 11'111'111, config.refreshRate);
774 
775         mFakeComposerClient->clearFrames();
776         {
777             const ui::Size& resolution = config.resolution;
778             auto surfaceControl =
779                     mComposerClient->createSurface(String8("Display Test Surface Foo"),
780                                                    resolution.getWidth(), resolution.getHeight(),
781                                                    PIXEL_FORMAT_RGBA_8888, 0);
782             EXPECT_TRUE(surfaceControl != nullptr);
783             EXPECT_TRUE(surfaceControl->isValid());
784             fillSurfaceRGBA8(surfaceControl, BLUE);
785 
786             {
787                 TransactionScope ts(*mFakeComposerClient);
788                 ts.setDisplayLayerStack(display, 0);
789 
790                 ts.setLayer(surfaceControl, INT32_MAX - 2).show(surfaceControl);
791             }
792         }
793 
794         mFakeComposerClient->hotplugDisplay(EXTERNAL_DISPLAY,
795                                             V2_1::IComposerCallback::Connection::DISCONNECTED);
796         waitForDisplayTransaction();
797         mFakeComposerClient->clearFrames();
798         EXPECT_TRUE(waitForHotplugEvent(EXTERNAL_DISPLAY, false));
799     }
800 
Test_HotplugPrimaryDisplay()801     void Test_HotplugPrimaryDisplay() {
802         ALOGD("DisplayTest::HotplugPrimaryDisplay");
803 
804         mFakeComposerClient->hotplugDisplay(PRIMARY_DISPLAY,
805                                             V2_1::IComposerCallback::Connection::DISCONNECTED);
806 
807         waitForDisplayTransaction();
808 
809         EXPECT_TRUE(waitForHotplugEvent(PRIMARY_DISPLAY, false));
810         {
811             const auto display = SurfaceComposerClient::getPhysicalDisplayToken(PRIMARY_DISPLAY);
812             EXPECT_TRUE(display == nullptr);
813 
814             DisplayConfig config;
815             auto result = SurfaceComposerClient::getActiveDisplayConfig(display, &config);
816             EXPECT_NE(NO_ERROR, result);
817         }
818 
819         mFakeComposerClient->clearFrames();
820 
821         setExpectationsForConfigs(PRIMARY_DISPLAY,
822                                   {{.id = 1,
823                                     .w = 400,
824                                     .h = 200,
825                                     .vsyncPeriod = 16'666'666,
826                                     .group = 0}},
827                                   1, 16'666'666);
828 
829         mFakeComposerClient->hotplugDisplay(PRIMARY_DISPLAY,
830                                             V2_1::IComposerCallback::Connection::CONNECTED);
831 
832         waitForDisplayTransaction();
833 
834         EXPECT_TRUE(waitForHotplugEvent(PRIMARY_DISPLAY, true));
835 
836         {
837             const auto display = SurfaceComposerClient::getPhysicalDisplayToken(PRIMARY_DISPLAY);
838             EXPECT_FALSE(display == nullptr);
839 
840             DisplayConfig config;
841             auto result = SurfaceComposerClient::getActiveDisplayConfig(display, &config);
842             EXPECT_EQ(NO_ERROR, result);
843             ASSERT_EQ(ui::Size(400, 200), config.resolution);
844             EXPECT_EQ(1e9f / 16'666'666, config.refreshRate);
845         }
846     }
847 
848     sp<V2_1::IComposer> mFakeService;
849     sp<SurfaceComposerClient> mComposerClient;
850 
851     std::unique_ptr<MockComposerHal> mMockComposer;
852     FakeComposerClient* mFakeComposerClient;
853 
854     std::unique_ptr<DisplayEventReceiver> mReceiver;
855     sp<Looper> mLooper;
856     std::deque<DisplayEventReceiver::Event> mReceivedDisplayEvents;
857 
858     static constexpr bool mIs2_4Client =
859             std::is_same<FakeComposerService, FakeComposerService_2_4>::value;
860 };
861 
862 using DisplayTest_2_1 = DisplayTest<FakeComposerService_2_1>;
863 
TEST_F(DisplayTest_2_1,HotplugOneConfig)864 TEST_F(DisplayTest_2_1, HotplugOneConfig) {
865     Test_HotplugOneConfig();
866 }
867 
TEST_F(DisplayTest_2_1,HotplugTwoSeparateConfigs)868 TEST_F(DisplayTest_2_1, HotplugTwoSeparateConfigs) {
869     Test_HotplugTwoSeparateConfigs();
870 }
871 
TEST_F(DisplayTest_2_1,HotplugTwoConfigsSameGroup)872 TEST_F(DisplayTest_2_1, HotplugTwoConfigsSameGroup) {
873     Test_HotplugTwoConfigsSameGroup();
874 }
875 
TEST_F(DisplayTest_2_1,HotplugThreeConfigsMixedGroups)876 TEST_F(DisplayTest_2_1, HotplugThreeConfigsMixedGroups) {
877     Test_HotplugThreeConfigsMixedGroups();
878 }
879 
TEST_F(DisplayTest_2_1,HotplugPrimaryOneConfig)880 TEST_F(DisplayTest_2_1, HotplugPrimaryOneConfig) {
881     Test_HotplugPrimaryDisplay();
882 }
883 
884 using DisplayTest_2_2 = DisplayTest<FakeComposerService_2_2>;
885 
TEST_F(DisplayTest_2_2,HotplugOneConfig)886 TEST_F(DisplayTest_2_2, HotplugOneConfig) {
887     Test_HotplugOneConfig();
888 }
889 
TEST_F(DisplayTest_2_2,HotplugTwoSeparateConfigs)890 TEST_F(DisplayTest_2_2, HotplugTwoSeparateConfigs) {
891     Test_HotplugTwoSeparateConfigs();
892 }
893 
TEST_F(DisplayTest_2_2,HotplugTwoConfigsSameGroup)894 TEST_F(DisplayTest_2_2, HotplugTwoConfigsSameGroup) {
895     Test_HotplugTwoConfigsSameGroup();
896 }
897 
TEST_F(DisplayTest_2_2,HotplugThreeConfigsMixedGroups)898 TEST_F(DisplayTest_2_2, HotplugThreeConfigsMixedGroups) {
899     Test_HotplugThreeConfigsMixedGroups();
900 }
901 
TEST_F(DisplayTest_2_2,HotplugPrimaryOneConfig)902 TEST_F(DisplayTest_2_2, HotplugPrimaryOneConfig) {
903     Test_HotplugPrimaryDisplay();
904 }
905 
906 using DisplayTest_2_3 = DisplayTest<FakeComposerService_2_3>;
907 
TEST_F(DisplayTest_2_3,HotplugOneConfig)908 TEST_F(DisplayTest_2_3, HotplugOneConfig) {
909     Test_HotplugOneConfig();
910 }
911 
TEST_F(DisplayTest_2_3,HotplugTwoSeparateConfigs)912 TEST_F(DisplayTest_2_3, HotplugTwoSeparateConfigs) {
913     Test_HotplugTwoSeparateConfigs();
914 }
915 
TEST_F(DisplayTest_2_3,HotplugTwoConfigsSameGroup)916 TEST_F(DisplayTest_2_3, HotplugTwoConfigsSameGroup) {
917     Test_HotplugTwoConfigsSameGroup();
918 }
919 
TEST_F(DisplayTest_2_3,HotplugThreeConfigsMixedGroups)920 TEST_F(DisplayTest_2_3, HotplugThreeConfigsMixedGroups) {
921     Test_HotplugThreeConfigsMixedGroups();
922 }
923 
TEST_F(DisplayTest_2_3,HotplugPrimaryOneConfig)924 TEST_F(DisplayTest_2_3, HotplugPrimaryOneConfig) {
925     Test_HotplugPrimaryDisplay();
926 }
927 
928 using DisplayTest_2_4 = DisplayTest<FakeComposerService_2_4>;
929 
TEST_F(DisplayTest_2_4,HotplugOneConfig)930 TEST_F(DisplayTest_2_4, HotplugOneConfig) {
931     Test_HotplugOneConfig();
932 }
933 
TEST_F(DisplayTest_2_4,HotplugTwoSeparateConfigs)934 TEST_F(DisplayTest_2_4, HotplugTwoSeparateConfigs) {
935     Test_HotplugTwoSeparateConfigs();
936 }
937 
TEST_F(DisplayTest_2_4,HotplugTwoConfigsSameGroup)938 TEST_F(DisplayTest_2_4, HotplugTwoConfigsSameGroup) {
939     Test_HotplugTwoConfigsSameGroup();
940 }
941 
TEST_F(DisplayTest_2_4,HotplugThreeConfigsMixedGroups)942 TEST_F(DisplayTest_2_4, HotplugThreeConfigsMixedGroups) {
943     Test_HotplugThreeConfigsMixedGroups();
944 }
945 
TEST_F(DisplayTest_2_4,HotplugPrimaryOneConfig)946 TEST_F(DisplayTest_2_4, HotplugPrimaryOneConfig) {
947     Test_HotplugPrimaryDisplay();
948 }
949 
950 ////////////////////////////////////////////////
951 
952 template <typename FakeComposerService>
953 class TransactionTest : public ::testing::Test {
954 protected:
955     // Layer array indexing constants.
956     constexpr static int BG_LAYER = 0;
957     constexpr static int FG_LAYER = 1;
958 
SetUpTestCase()959     static void SetUpTestCase() {
960         // TODO: See TODO comment at DisplayTest::SetUp for background on
961         // the lifetime of the FakeComposerClient.
962         sFakeComposer = new FakeComposerClient;
963         sp<V2_4::hal::ComposerClient> client = new V2_4::hal::ComposerClient(sFakeComposer);
964         sp<V2_1::IComposer> fakeService = new FakeComposerService(client);
965         (void)fakeService->registerAsService("mock");
966 
967         android::hardware::ProcessState::self()->startThreadPool();
968         android::ProcessState::self()->startThreadPool();
969 
970         startSurfaceFlinger();
971 
972         // Fake composer wants to enable VSync injection
973         sFakeComposer->onSurfaceFlingerStart();
974     }
975 
TearDownTestCase()976     static void TearDownTestCase() {
977         // Fake composer needs to release SurfaceComposerClient before the stop.
978         sFakeComposer->onSurfaceFlingerStop();
979         stopSurfaceFlinger();
980         // TODO: This is deleted when the ComposerClient calls
981         // removeClient. Devise better lifetime control.
982         sFakeComposer = nullptr;
983     }
984 
SetUp()985     void SetUp() override {
986         ALOGI("TransactionTest::SetUp");
987         mComposerClient = new SurfaceComposerClient;
988         ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
989 
990         ALOGI("TransactionTest::SetUp - display");
991         const auto display = SurfaceComposerClient::getPhysicalDisplayToken(PRIMARY_DISPLAY);
992         ASSERT_FALSE(display == nullptr);
993 
994         DisplayConfig config;
995         ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayConfig(display, &config));
996 
997         const ui::Size& resolution = config.resolution;
998         mDisplayWidth = resolution.getWidth();
999         mDisplayHeight = resolution.getHeight();
1000 
1001         // Background surface
1002         mBGSurfaceControl =
1003                 mComposerClient->createSurface(String8("BG Test Surface"), mDisplayWidth,
1004                                                mDisplayHeight, PIXEL_FORMAT_RGBA_8888, 0);
1005         ASSERT_TRUE(mBGSurfaceControl != nullptr);
1006         ASSERT_TRUE(mBGSurfaceControl->isValid());
1007         fillSurfaceRGBA8(mBGSurfaceControl, BLUE);
1008 
1009         // Foreground surface
1010         mFGSurfaceControl = mComposerClient->createSurface(String8("FG Test Surface"), 64, 64,
1011                                                            PIXEL_FORMAT_RGBA_8888, 0);
1012         ASSERT_TRUE(mFGSurfaceControl != nullptr);
1013         ASSERT_TRUE(mFGSurfaceControl->isValid());
1014 
1015         fillSurfaceRGBA8(mFGSurfaceControl, RED);
1016 
1017         Transaction t;
1018         t.setDisplayLayerStack(display, 0);
1019 
1020         t.setLayer(mBGSurfaceControl, INT32_MAX - 2);
1021         t.show(mBGSurfaceControl);
1022 
1023         t.setLayer(mFGSurfaceControl, INT32_MAX - 1);
1024         t.setPosition(mFGSurfaceControl, 64, 64);
1025         t.show(mFGSurfaceControl);
1026 
1027         // Synchronous transaction will stop this thread, so we set up a
1028         // delayed, off-thread vsync request before closing the
1029         // transaction. In the test code this is usually done with
1030         // TransactionScope. Leaving here in the 'vanilla' form for
1031         // reference.
1032         ASSERT_EQ(0, sFakeComposer->getFrameCount());
1033         sFakeComposer->runVSyncAfter(1ms);
1034         t.apply();
1035         sFakeComposer->waitUntilFrame(1);
1036 
1037         // Reference data. This is what the HWC should see.
1038         static_assert(BG_LAYER == 0 && FG_LAYER == 1, "Unexpected enum values for array indexing");
1039         mBaseFrame.push_back(makeSimpleRect(0u, 0u, mDisplayWidth, mDisplayHeight));
1040         mBaseFrame[BG_LAYER].mSwapCount = 1;
1041         mBaseFrame.push_back(makeSimpleRect(64, 64, 64 + 64, 64 + 64));
1042         mBaseFrame[FG_LAYER].mSwapCount = 1;
1043 
1044         auto frame = sFakeComposer->getFrameRects(0);
1045         ASSERT_TRUE(framesAreSame(mBaseFrame, frame));
1046     }
1047 
TearDown()1048     void TearDown() override {
1049         ALOGD("TransactionTest::TearDown");
1050 
1051         mComposerClient->dispose();
1052         mBGSurfaceControl = 0;
1053         mFGSurfaceControl = 0;
1054         mComposerClient = 0;
1055 
1056         sFakeComposer->runVSyncAndWait();
1057         mBaseFrame.clear();
1058         sFakeComposer->clearFrames();
1059         ASSERT_EQ(0, sFakeComposer->getFrameCount());
1060 
1061         sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1062         std::vector<LayerDebugInfo> layers;
1063         status_t result = sf->getLayerDebugInfo(&layers);
1064         if (result != NO_ERROR) {
1065             ALOGE("Failed to get layers %s %d", strerror(-result), result);
1066         } else {
1067             // If this fails, the test being torn down leaked layers.
1068             EXPECT_EQ(0u, layers.size());
1069             if (layers.size() > 0) {
1070                 for (auto layer = layers.begin(); layer != layers.end(); ++layer) {
1071                     std::cout << to_string(*layer).c_str();
1072                 }
1073                 // To ensure the next test has clean slate, will run the class
1074                 // tear down and setup here.
1075                 TearDownTestCase();
1076                 SetUpTestCase();
1077             }
1078         }
1079         ALOGD("TransactionTest::TearDown - complete");
1080     }
1081 
Test_LayerMove()1082     void Test_LayerMove() {
1083         ALOGD("TransactionTest::LayerMove");
1084 
1085         // The scope opens and closes a global transaction and, at the
1086         // same time, makes sure the SurfaceFlinger progresses one frame
1087         // after the transaction closes. The results of the transaction
1088         // should be available in the latest frame stored by the fake
1089         // composer.
1090         {
1091             TransactionScope ts(*sFakeComposer);
1092             ts.setPosition(mFGSurfaceControl, 128, 128);
1093             // NOTE: No changes yet, so vsync will do nothing, HWC does not get any calls.
1094             // (How to verify that? Throw in vsync and wait a 2x frame time? Separate test?)
1095             //
1096             // sFakeComposer->runVSyncAndWait();
1097         }
1098 
1099         fillSurfaceRGBA8(mFGSurfaceControl, GREEN);
1100         sFakeComposer->runVSyncAndWait();
1101 
1102         ASSERT_EQ(3, sFakeComposer->getFrameCount()); // Make sure the waits didn't time out and
1103                                                       // there's no extra frames.
1104 
1105         // NOTE: Frame 0 is produced in the SetUp.
1106         auto frame1Ref = mBaseFrame;
1107         frame1Ref[FG_LAYER].mDisplayFrame =
1108                 hwc_rect_t{128, 128, 128 + 64, 128 + 64}; // Top-most layer moves.
1109         EXPECT_TRUE(framesAreSame(frame1Ref, sFakeComposer->getFrameRects(1)));
1110 
1111         auto frame2Ref = frame1Ref;
1112         frame2Ref[FG_LAYER].mSwapCount++;
1113         EXPECT_TRUE(framesAreSame(frame2Ref, sFakeComposer->getFrameRects(2)));
1114     }
1115 
Test_LayerResize()1116     void Test_LayerResize() {
1117         ALOGD("TransactionTest::LayerResize");
1118         {
1119             TransactionScope ts(*sFakeComposer);
1120             ts.setSize(mFGSurfaceControl, 128, 128);
1121         }
1122 
1123         fillSurfaceRGBA8(mFGSurfaceControl, GREEN);
1124         sFakeComposer->runVSyncAndWait();
1125 
1126         ASSERT_EQ(3, sFakeComposer->getFrameCount()); // Make sure the waits didn't time out and
1127                                                       // there's no extra frames.
1128 
1129         auto frame1Ref = mBaseFrame;
1130         // NOTE: The resize should not be visible for frame 1 as there's no buffer with new size
1131         // posted.
1132         EXPECT_TRUE(framesAreSame(frame1Ref, sFakeComposer->getFrameRects(1)));
1133 
1134         auto frame2Ref = frame1Ref;
1135         frame2Ref[FG_LAYER].mSwapCount++;
1136         frame2Ref[FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 128, 64 + 128};
1137         frame2Ref[FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 128.f, 128.f};
1138         EXPECT_TRUE(framesAreSame(frame2Ref, sFakeComposer->getFrameRects(2)));
1139     }
1140 
Test_LayerCrop()1141     void Test_LayerCrop() {
1142         // TODO: Add scaling to confirm that crop happens in buffer space?
1143         {
1144             TransactionScope ts(*sFakeComposer);
1145             Rect cropRect(16, 16, 32, 32);
1146             ts.setCrop_legacy(mFGSurfaceControl, cropRect);
1147         }
1148         ASSERT_EQ(2, sFakeComposer->getFrameCount());
1149 
1150         auto referenceFrame = mBaseFrame;
1151         referenceFrame[FG_LAYER].mSourceCrop = hwc_frect_t{16.f, 16.f, 32.f, 32.f};
1152         referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{64 + 16, 64 + 16, 64 + 32, 64 + 32};
1153         EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1154     }
1155 
Test_LayerSetLayer()1156     void Test_LayerSetLayer() {
1157         {
1158             TransactionScope ts(*sFakeComposer);
1159             ts.setLayer(mFGSurfaceControl, INT_MAX - 3);
1160         }
1161         ASSERT_EQ(2, sFakeComposer->getFrameCount());
1162 
1163         // The layers will switch order, but both are rendered because the background layer is
1164         // transparent (RGBA8888).
1165         std::vector<RenderState> referenceFrame(2);
1166         referenceFrame[0] = mBaseFrame[FG_LAYER];
1167         referenceFrame[1] = mBaseFrame[BG_LAYER];
1168         EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1169     }
1170 
Test_LayerSetLayerOpaque()1171     void Test_LayerSetLayerOpaque() {
1172         {
1173             TransactionScope ts(*sFakeComposer);
1174             ts.setLayer(mFGSurfaceControl, INT_MAX - 3);
1175             ts.setFlags(mBGSurfaceControl, layer_state_t::eLayerOpaque,
1176                         layer_state_t::eLayerOpaque);
1177         }
1178         ASSERT_EQ(2, sFakeComposer->getFrameCount());
1179 
1180         // The former foreground layer is now covered with opaque layer - it should have disappeared
1181         std::vector<RenderState> referenceFrame(1);
1182         referenceFrame[BG_LAYER] = mBaseFrame[BG_LAYER];
1183         EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1184     }
1185 
Test_SetLayerStack()1186     void Test_SetLayerStack() {
1187         ALOGD("TransactionTest::SetLayerStack");
1188         {
1189             TransactionScope ts(*sFakeComposer);
1190             ts.setLayerStack(mFGSurfaceControl, 1);
1191         }
1192 
1193         // Foreground layer should have disappeared.
1194         ASSERT_EQ(2, sFakeComposer->getFrameCount());
1195         std::vector<RenderState> refFrame(1);
1196         refFrame[BG_LAYER] = mBaseFrame[BG_LAYER];
1197         EXPECT_TRUE(framesAreSame(refFrame, sFakeComposer->getLatestFrame()));
1198     }
1199 
Test_LayerShowHide()1200     void Test_LayerShowHide() {
1201         ALOGD("TransactionTest::LayerShowHide");
1202         {
1203             TransactionScope ts(*sFakeComposer);
1204             ts.hide(mFGSurfaceControl);
1205         }
1206 
1207         // Foreground layer should have disappeared.
1208         ASSERT_EQ(2, sFakeComposer->getFrameCount());
1209         std::vector<RenderState> refFrame(1);
1210         refFrame[BG_LAYER] = mBaseFrame[BG_LAYER];
1211         EXPECT_TRUE(framesAreSame(refFrame, sFakeComposer->getLatestFrame()));
1212 
1213         {
1214             TransactionScope ts(*sFakeComposer);
1215             ts.show(mFGSurfaceControl);
1216         }
1217 
1218         // Foreground layer should be back
1219         ASSERT_EQ(3, sFakeComposer->getFrameCount());
1220         EXPECT_TRUE(framesAreSame(mBaseFrame, sFakeComposer->getLatestFrame()));
1221     }
1222 
Test_LayerSetAlpha()1223     void Test_LayerSetAlpha() {
1224         {
1225             TransactionScope ts(*sFakeComposer);
1226             ts.setAlpha(mFGSurfaceControl, 0.75f);
1227         }
1228 
1229         ASSERT_EQ(2, sFakeComposer->getFrameCount());
1230         auto referenceFrame = mBaseFrame;
1231         referenceFrame[FG_LAYER].mPlaneAlpha = 0.75f;
1232         EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1233     }
1234 
Test_LayerSetFlags()1235     void Test_LayerSetFlags() {
1236         {
1237             TransactionScope ts(*sFakeComposer);
1238             ts.setFlags(mFGSurfaceControl, layer_state_t::eLayerHidden,
1239                         layer_state_t::eLayerHidden);
1240         }
1241 
1242         // Foreground layer should have disappeared.
1243         ASSERT_EQ(2, sFakeComposer->getFrameCount());
1244         std::vector<RenderState> refFrame(1);
1245         refFrame[BG_LAYER] = mBaseFrame[BG_LAYER];
1246         EXPECT_TRUE(framesAreSame(refFrame, sFakeComposer->getLatestFrame()));
1247     }
1248 
Test_LayerSetMatrix()1249     void Test_LayerSetMatrix() {
1250         struct matrixTestData {
1251             float matrix[4];
1252             hwc_transform_t expectedTransform;
1253             hwc_rect_t expectedDisplayFrame;
1254         };
1255 
1256         // The matrix operates on the display frame and is applied before
1257         // the position is added. So, the foreground layer rect is (0, 0,
1258         // 64, 64) is first transformed, potentially yielding negative
1259         // coordinates and then the position (64, 64) is added yielding
1260         // the final on-screen rectangles given.
1261 
1262         const matrixTestData MATRIX_TESTS[7] = // clang-format off
1263                 {{{-1.f, 0.f, 0.f, 1.f},    HWC_TRANSFORM_FLIP_H,           {0, 64, 64, 128}},
1264                  {{1.f, 0.f, 0.f, -1.f},    HWC_TRANSFORM_FLIP_V,           {64, 0, 128, 64}},
1265                  {{0.f, 1.f, -1.f, 0.f},    HWC_TRANSFORM_ROT_90,           {0, 64, 64, 128}},
1266                  {{-1.f, 0.f, 0.f, -1.f},   HWC_TRANSFORM_ROT_180,          {0, 0, 64, 64}},
1267                  {{0.f, -1.f, 1.f, 0.f},    HWC_TRANSFORM_ROT_270,          {64, 0, 128, 64}},
1268                  {{0.f, 1.f, 1.f, 0.f},     HWC_TRANSFORM_FLIP_H_ROT_90,    {64, 64, 128, 128}},
1269                  {{0.f, 1.f, 1.f, 0.f},     HWC_TRANSFORM_FLIP_V_ROT_90,    {64, 64, 128, 128}}};
1270         // clang-format on
1271         constexpr int TEST_COUNT = sizeof(MATRIX_TESTS) / sizeof(matrixTestData);
1272 
1273         for (int i = 0; i < TEST_COUNT; i++) {
1274             // TODO: How to leverage the HWC2 stringifiers?
1275             const matrixTestData& xform = MATRIX_TESTS[i];
1276             SCOPED_TRACE(i);
1277             {
1278                 TransactionScope ts(*sFakeComposer);
1279                 ts.setMatrix(mFGSurfaceControl, xform.matrix[0], xform.matrix[1], xform.matrix[2],
1280                              xform.matrix[3]);
1281             }
1282 
1283             auto referenceFrame = mBaseFrame;
1284             referenceFrame[FG_LAYER].mTransform = xform.expectedTransform;
1285             referenceFrame[FG_LAYER].mDisplayFrame = xform.expectedDisplayFrame;
1286 
1287             EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1288         }
1289     }
1290 
Test_DeferredTransaction()1291     void Test_DeferredTransaction() {
1292         // Synchronization surface
1293         constexpr static int SYNC_LAYER = 2;
1294         auto syncSurfaceControl = mComposerClient->createSurface(String8("Sync Test Surface"), 1, 1,
1295                                                                  PIXEL_FORMAT_RGBA_8888, 0);
1296         ASSERT_TRUE(syncSurfaceControl != nullptr);
1297         ASSERT_TRUE(syncSurfaceControl->isValid());
1298 
1299         fillSurfaceRGBA8(syncSurfaceControl, DARK_GRAY);
1300 
1301         {
1302             TransactionScope ts(*sFakeComposer);
1303             ts.setLayer(syncSurfaceControl, INT32_MAX - 1);
1304             ts.setPosition(syncSurfaceControl, mDisplayWidth - 2, mDisplayHeight - 2);
1305             ts.show(syncSurfaceControl);
1306         }
1307         auto referenceFrame = mBaseFrame;
1308         referenceFrame.push_back(makeSimpleRect(mDisplayWidth - 2, mDisplayHeight - 2,
1309                                                 mDisplayWidth - 1, mDisplayHeight - 1));
1310         referenceFrame[SYNC_LAYER].mSwapCount = 1;
1311         EXPECT_EQ(2, sFakeComposer->getFrameCount());
1312         EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1313 
1314         // set up two deferred transactions on different frames - these should not yield composited
1315         // frames
1316         {
1317             TransactionScope ts(*sFakeComposer);
1318             ts.setAlpha(mFGSurfaceControl, 0.75);
1319             ts.deferTransactionUntil_legacy(mFGSurfaceControl, syncSurfaceControl->getHandle(),
1320                                             syncSurfaceControl->getSurface()->getNextFrameNumber());
1321         }
1322         EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1323 
1324         {
1325             TransactionScope ts(*sFakeComposer);
1326             ts.setPosition(mFGSurfaceControl, 128, 128);
1327             ts.deferTransactionUntil_legacy(mFGSurfaceControl, syncSurfaceControl->getHandle(),
1328                                             syncSurfaceControl->getSurface()->getNextFrameNumber() +
1329                                                     1);
1330         }
1331         EXPECT_EQ(4, sFakeComposer->getFrameCount());
1332         EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1333 
1334         // should trigger the first deferred transaction, but not the second one
1335         fillSurfaceRGBA8(syncSurfaceControl, DARK_GRAY);
1336         sFakeComposer->runVSyncAndWait();
1337         EXPECT_EQ(5, sFakeComposer->getFrameCount());
1338 
1339         referenceFrame[FG_LAYER].mPlaneAlpha = 0.75f;
1340         referenceFrame[SYNC_LAYER].mSwapCount++;
1341         EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1342 
1343         // should show up immediately since it's not deferred
1344         {
1345             TransactionScope ts(*sFakeComposer);
1346             ts.setAlpha(mFGSurfaceControl, 1.0);
1347         }
1348         referenceFrame[FG_LAYER].mPlaneAlpha = 1.f;
1349         EXPECT_EQ(6, sFakeComposer->getFrameCount());
1350         EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1351 
1352         // trigger the second deferred transaction
1353         fillSurfaceRGBA8(syncSurfaceControl, DARK_GRAY);
1354         sFakeComposer->runVSyncAndWait();
1355         // TODO: Compute from layer size?
1356         referenceFrame[FG_LAYER].mDisplayFrame = hwc_rect_t{128, 128, 128 + 64, 128 + 64};
1357         referenceFrame[SYNC_LAYER].mSwapCount++;
1358         EXPECT_EQ(7, sFakeComposer->getFrameCount());
1359         EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1360     }
1361 
Test_SetRelativeLayer()1362     void Test_SetRelativeLayer() {
1363         constexpr int RELATIVE_LAYER = 2;
1364         auto relativeSurfaceControl = mComposerClient->createSurface(String8("Test Surface"), 64,
1365                                                                      64, PIXEL_FORMAT_RGBA_8888, 0);
1366         fillSurfaceRGBA8(relativeSurfaceControl, LIGHT_RED);
1367 
1368         // Now we stack the surface above the foreground surface and make sure it is visible.
1369         {
1370             TransactionScope ts(*sFakeComposer);
1371             ts.setPosition(relativeSurfaceControl, 64, 64);
1372             ts.show(relativeSurfaceControl);
1373             ts.setRelativeLayer(relativeSurfaceControl, mFGSurfaceControl->getHandle(), 1);
1374         }
1375         auto referenceFrame = mBaseFrame;
1376         // NOTE: All three layers will be visible as the surfaces are
1377         // transparent because of the RGBA format.
1378         referenceFrame.push_back(makeSimpleRect(64, 64, 64 + 64, 64 + 64));
1379         referenceFrame[RELATIVE_LAYER].mSwapCount = 1;
1380         EXPECT_TRUE(framesAreSame(referenceFrame, sFakeComposer->getLatestFrame()));
1381 
1382         // A call to setLayer will override a call to setRelativeLayer
1383         {
1384             TransactionScope ts(*sFakeComposer);
1385             ts.setLayer(relativeSurfaceControl, 0);
1386         }
1387 
1388         // Previous top layer will now appear at the bottom.
1389         auto referenceFrame2 = mBaseFrame;
1390         referenceFrame2.insert(referenceFrame2.begin(), referenceFrame[RELATIVE_LAYER]);
1391         EXPECT_EQ(3, sFakeComposer->getFrameCount());
1392         EXPECT_TRUE(framesAreSame(referenceFrame2, sFakeComposer->getLatestFrame()));
1393     }
1394 
1395     sp<SurfaceComposerClient> mComposerClient;
1396     sp<SurfaceControl> mBGSurfaceControl;
1397     sp<SurfaceControl> mFGSurfaceControl;
1398     std::vector<RenderState> mBaseFrame;
1399     uint32_t mDisplayWidth;
1400     uint32_t mDisplayHeight;
1401 
1402     static inline FakeComposerClient* sFakeComposer;
1403 };
1404 
1405 using TransactionTest_2_1 = TransactionTest<FakeComposerService_2_1>;
1406 
TEST_F(TransactionTest_2_1,DISABLED_LayerMove)1407 TEST_F(TransactionTest_2_1, DISABLED_LayerMove) {
1408     Test_LayerMove();
1409 }
1410 
TEST_F(TransactionTest_2_1,DISABLED_LayerResize)1411 TEST_F(TransactionTest_2_1, DISABLED_LayerResize) {
1412     Test_LayerResize();
1413 }
1414 
TEST_F(TransactionTest_2_1,DISABLED_LayerCrop)1415 TEST_F(TransactionTest_2_1, DISABLED_LayerCrop) {
1416     Test_LayerCrop();
1417 }
1418 
TEST_F(TransactionTest_2_1,DISABLED_LayerSetLayer)1419 TEST_F(TransactionTest_2_1, DISABLED_LayerSetLayer) {
1420     Test_LayerSetLayer();
1421 }
1422 
TEST_F(TransactionTest_2_1,DISABLED_LayerSetLayerOpaque)1423 TEST_F(TransactionTest_2_1, DISABLED_LayerSetLayerOpaque) {
1424     Test_LayerSetLayerOpaque();
1425 }
1426 
TEST_F(TransactionTest_2_1,DISABLED_SetLayerStack)1427 TEST_F(TransactionTest_2_1, DISABLED_SetLayerStack) {
1428     Test_SetLayerStack();
1429 }
1430 
TEST_F(TransactionTest_2_1,DISABLED_LayerShowHide)1431 TEST_F(TransactionTest_2_1, DISABLED_LayerShowHide) {
1432     Test_LayerShowHide();
1433 }
1434 
TEST_F(TransactionTest_2_1,DISABLED_LayerSetAlpha)1435 TEST_F(TransactionTest_2_1, DISABLED_LayerSetAlpha) {
1436     Test_LayerSetAlpha();
1437 }
1438 
TEST_F(TransactionTest_2_1,DISABLED_LayerSetFlags)1439 TEST_F(TransactionTest_2_1, DISABLED_LayerSetFlags) {
1440     Test_LayerSetFlags();
1441 }
1442 
TEST_F(TransactionTest_2_1,DISABLED_LayerSetMatrix)1443 TEST_F(TransactionTest_2_1, DISABLED_LayerSetMatrix) {
1444     Test_LayerSetMatrix();
1445 }
1446 
TEST_F(TransactionTest_2_1,DISABLED_DeferredTransaction)1447 TEST_F(TransactionTest_2_1, DISABLED_DeferredTransaction) {
1448     Test_DeferredTransaction();
1449 }
1450 
TEST_F(TransactionTest_2_1,DISABLED_SetRelativeLayer)1451 TEST_F(TransactionTest_2_1, DISABLED_SetRelativeLayer) {
1452     Test_SetRelativeLayer();
1453 }
1454 
1455 template <typename FakeComposerService>
1456 class ChildLayerTest : public TransactionTest<FakeComposerService> {
1457     using Base = TransactionTest<FakeComposerService>;
1458 
1459 protected:
1460     constexpr static int CHILD_LAYER = 2;
1461 
SetUp()1462     void SetUp() override {
1463         Base::SetUp();
1464         mChild = Base::mComposerClient->createSurface(String8("Child surface"), 10, 10,
1465                                                       PIXEL_FORMAT_RGBA_8888, 0,
1466                                                       Base::mFGSurfaceControl.get());
1467         fillSurfaceRGBA8(mChild, LIGHT_GRAY);
1468 
1469         Base::sFakeComposer->runVSyncAndWait();
1470         Base::mBaseFrame.push_back(makeSimpleRect(64, 64, 64 + 10, 64 + 10));
1471         Base::mBaseFrame[CHILD_LAYER].mSwapCount = 1;
1472         ASSERT_EQ(2, Base::sFakeComposer->getFrameCount());
1473         ASSERT_TRUE(framesAreSame(Base::mBaseFrame, Base::sFakeComposer->getLatestFrame()));
1474     }
1475 
TearDown()1476     void TearDown() override {
1477         mChild = 0;
1478         Base::TearDown();
1479     }
1480 
Test_Positioning()1481     void Test_Positioning() {
1482         {
1483             TransactionScope ts(*Base::sFakeComposer);
1484             ts.show(mChild);
1485             ts.setPosition(mChild, 10, 10);
1486             // Move to the same position as in the original setup.
1487             ts.setPosition(Base::mFGSurfaceControl, 64, 64);
1488         }
1489 
1490         auto referenceFrame = Base::mBaseFrame;
1491         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 64, 64 + 64};
1492         referenceFrame[CHILD_LAYER].mDisplayFrame =
1493                 hwc_rect_t{64 + 10, 64 + 10, 64 + 10 + 10, 64 + 10 + 10};
1494         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1495 
1496         {
1497             TransactionScope ts(*Base::sFakeComposer);
1498             ts.setPosition(Base::mFGSurfaceControl, 0, 0);
1499         }
1500 
1501         auto referenceFrame2 = Base::mBaseFrame;
1502         referenceFrame2[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 0 + 64, 0 + 64};
1503         referenceFrame2[CHILD_LAYER].mDisplayFrame =
1504                 hwc_rect_t{0 + 10, 0 + 10, 0 + 10 + 10, 0 + 10 + 10};
1505         EXPECT_TRUE(framesAreSame(referenceFrame2, Base::sFakeComposer->getLatestFrame()));
1506     }
1507 
Test_Cropping()1508     void Test_Cropping() {
1509         {
1510             TransactionScope ts(*Base::sFakeComposer);
1511             ts.show(mChild);
1512             ts.setPosition(mChild, 0, 0);
1513             ts.setPosition(Base::mFGSurfaceControl, 0, 0);
1514             ts.setCrop_legacy(Base::mFGSurfaceControl, Rect(0, 0, 5, 5));
1515         }
1516         // NOTE: The foreground surface would be occluded by the child
1517         // now, but is included in the stack because the child is
1518         // transparent.
1519         auto referenceFrame = Base::mBaseFrame;
1520         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 0 + 5, 0 + 5};
1521         referenceFrame[Base::FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 5.f, 5.f};
1522         referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 0 + 5, 0 + 5};
1523         referenceFrame[CHILD_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 5.f, 5.f};
1524         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1525     }
1526 
Test_Constraints()1527     void Test_Constraints() {
1528         {
1529             TransactionScope ts(*Base::sFakeComposer);
1530             ts.show(mChild);
1531             ts.setPosition(Base::mFGSurfaceControl, 0, 0);
1532             ts.setPosition(mChild, 63, 63);
1533         }
1534         auto referenceFrame = Base::mBaseFrame;
1535         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 64, 64};
1536         referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{63, 63, 64, 64};
1537         referenceFrame[CHILD_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 1.f, 1.f};
1538         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1539     }
1540 
Test_Scaling()1541     void Test_Scaling() {
1542         {
1543             TransactionScope ts(*Base::sFakeComposer);
1544             ts.setPosition(Base::mFGSurfaceControl, 0, 0);
1545         }
1546         auto referenceFrame = Base::mBaseFrame;
1547         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 64, 64};
1548         referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 10, 10};
1549         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1550 
1551         {
1552             TransactionScope ts(*Base::sFakeComposer);
1553             ts.setMatrix(Base::mFGSurfaceControl, 2.0, 0, 0, 2.0);
1554         }
1555 
1556         auto referenceFrame2 = Base::mBaseFrame;
1557         referenceFrame2[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 128, 128};
1558         referenceFrame2[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 20, 20};
1559         EXPECT_TRUE(framesAreSame(referenceFrame2, Base::sFakeComposer->getLatestFrame()));
1560     }
1561 
Test_LayerAlpha()1562     void Test_LayerAlpha() {
1563         {
1564             TransactionScope ts(*Base::sFakeComposer);
1565             ts.show(mChild);
1566             ts.setPosition(mChild, 0, 0);
1567             ts.setPosition(Base::mFGSurfaceControl, 0, 0);
1568             ts.setAlpha(mChild, 0.5);
1569         }
1570 
1571         auto referenceFrame = Base::mBaseFrame;
1572         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 64, 64};
1573         referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 10, 10};
1574         referenceFrame[CHILD_LAYER].mPlaneAlpha = 0.5f;
1575         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1576 
1577         {
1578             TransactionScope ts(*Base::sFakeComposer);
1579             ts.setAlpha(Base::mFGSurfaceControl, 0.5);
1580         }
1581 
1582         auto referenceFrame2 = referenceFrame;
1583         referenceFrame2[Base::FG_LAYER].mPlaneAlpha = 0.5f;
1584         referenceFrame2[CHILD_LAYER].mPlaneAlpha = 0.25f;
1585         EXPECT_TRUE(framesAreSame(referenceFrame2, Base::sFakeComposer->getLatestFrame()));
1586     }
1587 
Test_ReparentChildren()1588     void Test_ReparentChildren() {
1589         {
1590             TransactionScope ts(*Base::sFakeComposer);
1591             ts.show(mChild);
1592             ts.setPosition(mChild, 10, 10);
1593             ts.setPosition(Base::mFGSurfaceControl, 64, 64);
1594         }
1595         auto referenceFrame = Base::mBaseFrame;
1596         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 64, 64 + 64};
1597         referenceFrame[CHILD_LAYER].mDisplayFrame =
1598                 hwc_rect_t{64 + 10, 64 + 10, 64 + 10 + 10, 64 + 10 + 10};
1599         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1600 
1601         {
1602             TransactionScope ts(*Base::sFakeComposer);
1603             ts.reparentChildren(Base::mFGSurfaceControl, Base::mBGSurfaceControl->getHandle());
1604         }
1605 
1606         auto referenceFrame2 = referenceFrame;
1607         referenceFrame2[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 64, 64 + 64};
1608         referenceFrame2[CHILD_LAYER].mDisplayFrame = hwc_rect_t{10, 10, 10 + 10, 10 + 10};
1609         EXPECT_TRUE(framesAreSame(referenceFrame2, Base::sFakeComposer->getLatestFrame()));
1610     }
1611 
Test_DetachChildrenSameClient()1612     void Test_DetachChildrenSameClient() {
1613         {
1614             TransactionScope ts(*Base::sFakeComposer);
1615             ts.show(mChild);
1616             ts.setPosition(mChild, 10, 10);
1617             ts.setPosition(Base::mFGSurfaceControl, 64, 64);
1618         }
1619 
1620         auto referenceFrame = Base::mBaseFrame;
1621         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 64, 64 + 64};
1622         referenceFrame[CHILD_LAYER].mDisplayFrame =
1623                 hwc_rect_t{64 + 10, 64 + 10, 64 + 10 + 10, 64 + 10 + 10};
1624         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1625 
1626         {
1627             TransactionScope ts(*Base::sFakeComposer);
1628             ts.setPosition(Base::mFGSurfaceControl, 0, 0);
1629             ts.detachChildren(Base::mFGSurfaceControl);
1630         }
1631 
1632         {
1633             TransactionScope ts(*Base::sFakeComposer);
1634             ts.setPosition(Base::mFGSurfaceControl, 64, 64);
1635             ts.hide(mChild);
1636         }
1637 
1638         std::vector<RenderState> refFrame(2);
1639         refFrame[Base::BG_LAYER] = Base::mBaseFrame[Base::BG_LAYER];
1640         refFrame[Base::FG_LAYER] = Base::mBaseFrame[Base::FG_LAYER];
1641 
1642         EXPECT_TRUE(framesAreSame(refFrame, Base::sFakeComposer->getLatestFrame()));
1643     }
1644 
Test_DetachChildrenDifferentClient()1645     void Test_DetachChildrenDifferentClient() {
1646         sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
1647         sp<SurfaceControl> childNewClient =
1648                 newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
1649                                                  PIXEL_FORMAT_RGBA_8888, 0,
1650                                                  Base::mFGSurfaceControl.get());
1651         ASSERT_TRUE(childNewClient != nullptr);
1652         ASSERT_TRUE(childNewClient->isValid());
1653         fillSurfaceRGBA8(childNewClient, LIGHT_GRAY);
1654 
1655         {
1656             TransactionScope ts(*Base::sFakeComposer);
1657             ts.hide(mChild);
1658             ts.show(childNewClient);
1659             ts.setPosition(childNewClient, 10, 10);
1660             ts.setPosition(Base::mFGSurfaceControl, 64, 64);
1661         }
1662 
1663         auto referenceFrame = Base::mBaseFrame;
1664         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 64, 64 + 64};
1665         referenceFrame[CHILD_LAYER].mDisplayFrame =
1666                 hwc_rect_t{64 + 10, 64 + 10, 64 + 10 + 10, 64 + 10 + 10};
1667         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1668 
1669         {
1670             TransactionScope ts(*Base::sFakeComposer);
1671             ts.detachChildren(Base::mFGSurfaceControl);
1672             ts.setPosition(Base::mFGSurfaceControl, 0, 0);
1673         }
1674 
1675         {
1676             TransactionScope ts(*Base::sFakeComposer);
1677             ts.setPosition(Base::mFGSurfaceControl, 64, 64);
1678             ts.setPosition(childNewClient, 0, 0);
1679             ts.hide(childNewClient);
1680         }
1681 
1682         // Nothing should have changed. The child control becomes a no-op
1683         // zombie on detach. See comments for detachChildren in the
1684         // SurfaceControl.h file.
1685         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1686     }
1687 
Test_InheritNonTransformScalingFromParent()1688     void Test_InheritNonTransformScalingFromParent() {
1689         {
1690             TransactionScope ts(*Base::sFakeComposer);
1691             ts.show(mChild);
1692             ts.setPosition(mChild, 0, 0);
1693             ts.setPosition(Base::mFGSurfaceControl, 0, 0);
1694         }
1695 
1696         {
1697             TransactionScope ts(*Base::sFakeComposer);
1698             ts.setOverrideScalingMode(Base::mFGSurfaceControl,
1699                                       NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1700             // We cause scaling by 2.
1701             ts.setSize(Base::mFGSurfaceControl, 128, 128);
1702         }
1703 
1704         auto referenceFrame = Base::mBaseFrame;
1705         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 128, 128};
1706         referenceFrame[Base::FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 64.f, 64.f};
1707         referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 20, 20};
1708         referenceFrame[CHILD_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 10.f, 10.f};
1709         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1710     }
1711 
1712     // Regression test for b/37673612
Test_ChildrenWithParentBufferTransform()1713     void Test_ChildrenWithParentBufferTransform() {
1714         {
1715             TransactionScope ts(*Base::sFakeComposer);
1716             ts.show(mChild);
1717             ts.setPosition(mChild, 0, 0);
1718             ts.setPosition(Base::mFGSurfaceControl, 0, 0);
1719         }
1720 
1721         // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
1722         // the WM specified state size.
1723         {
1724             TransactionScope ts(*Base::sFakeComposer);
1725             ts.setSize(Base::mFGSurfaceControl, 128, 64);
1726         }
1727 
1728         sp<Surface> s = Base::mFGSurfaceControl->getSurface();
1729         auto anw = static_cast<ANativeWindow*>(s.get());
1730         native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
1731         native_window_set_buffers_dimensions(anw, 64, 128);
1732         fillSurfaceRGBA8(Base::mFGSurfaceControl, RED);
1733         Base::sFakeComposer->runVSyncAndWait();
1734 
1735         // The child should still be in the same place and not have any strange scaling as in
1736         // b/37673612.
1737         auto referenceFrame = Base::mBaseFrame;
1738         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 128, 64};
1739         referenceFrame[Base::FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 64.f, 128.f};
1740         referenceFrame[Base::FG_LAYER].mSwapCount++;
1741         referenceFrame[CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 10, 10};
1742         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1743     }
1744 
Test_Bug36858924()1745     void Test_Bug36858924() {
1746         // Destroy the child layer
1747         mChild.clear();
1748 
1749         // Now recreate it as hidden
1750         mChild = Base::mComposerClient->createSurface(String8("Child surface"), 10, 10,
1751                                                       PIXEL_FORMAT_RGBA_8888,
1752                                                       ISurfaceComposerClient::eHidden,
1753                                                       Base::mFGSurfaceControl.get());
1754 
1755         // Show the child layer in a deferred transaction
1756         {
1757             TransactionScope ts(*Base::sFakeComposer);
1758             ts.deferTransactionUntil_legacy(mChild, Base::mFGSurfaceControl->getHandle(),
1759                                             Base::mFGSurfaceControl->getSurface()
1760                                                     ->getNextFrameNumber());
1761             ts.show(mChild);
1762         }
1763 
1764         // Render the foreground surface a few times
1765         //
1766         // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the
1767         // third frame because SurfaceFlinger would never process the deferred transaction and would
1768         // therefore never acquire/release the first buffer
1769         ALOGI("Filling 1");
1770         fillSurfaceRGBA8(Base::mFGSurfaceControl, GREEN);
1771         Base::sFakeComposer->runVSyncAndWait();
1772         ALOGI("Filling 2");
1773         fillSurfaceRGBA8(Base::mFGSurfaceControl, BLUE);
1774         Base::sFakeComposer->runVSyncAndWait();
1775         ALOGI("Filling 3");
1776         fillSurfaceRGBA8(Base::mFGSurfaceControl, RED);
1777         Base::sFakeComposer->runVSyncAndWait();
1778         ALOGI("Filling 4");
1779         fillSurfaceRGBA8(Base::mFGSurfaceControl, GREEN);
1780         Base::sFakeComposer->runVSyncAndWait();
1781     }
1782 
1783     sp<SurfaceControl> mChild;
1784 };
1785 
1786 using ChildLayerTest_2_1 = ChildLayerTest<FakeComposerService_2_1>;
1787 
TEST_F(ChildLayerTest_2_1,DISABLED_Positioning)1788 TEST_F(ChildLayerTest_2_1, DISABLED_Positioning) {
1789     Test_Positioning();
1790 }
1791 
TEST_F(ChildLayerTest_2_1,DISABLED_Cropping)1792 TEST_F(ChildLayerTest_2_1, DISABLED_Cropping) {
1793     Test_Cropping();
1794 }
1795 
TEST_F(ChildLayerTest_2_1,DISABLED_Constraints)1796 TEST_F(ChildLayerTest_2_1, DISABLED_Constraints) {
1797     Test_Constraints();
1798 }
1799 
TEST_F(ChildLayerTest_2_1,DISABLED_Scaling)1800 TEST_F(ChildLayerTest_2_1, DISABLED_Scaling) {
1801     Test_Scaling();
1802 }
1803 
TEST_F(ChildLayerTest_2_1,DISABLED_LayerAlpha)1804 TEST_F(ChildLayerTest_2_1, DISABLED_LayerAlpha) {
1805     Test_LayerAlpha();
1806 }
1807 
TEST_F(ChildLayerTest_2_1,DISABLED_ReparentChildren)1808 TEST_F(ChildLayerTest_2_1, DISABLED_ReparentChildren) {
1809     Test_ReparentChildren();
1810 }
1811 
TEST_F(ChildLayerTest_2_1,DISABLED_DetachChildrenSameClient)1812 TEST_F(ChildLayerTest_2_1, DISABLED_DetachChildrenSameClient) {
1813     Test_DetachChildrenSameClient();
1814 }
1815 
TEST_F(ChildLayerTest_2_1,DISABLED_DetachChildrenDifferentClient)1816 TEST_F(ChildLayerTest_2_1, DISABLED_DetachChildrenDifferentClient) {
1817     Test_DetachChildrenDifferentClient();
1818 }
1819 
TEST_F(ChildLayerTest_2_1,DISABLED_InheritNonTransformScalingFromParent)1820 TEST_F(ChildLayerTest_2_1, DISABLED_InheritNonTransformScalingFromParent) {
1821     Test_InheritNonTransformScalingFromParent();
1822 }
1823 
1824 // Regression test for b/37673612
TEST_F(ChildLayerTest_2_1,DISABLED_ChildrenWithParentBufferTransform)1825 TEST_F(ChildLayerTest_2_1, DISABLED_ChildrenWithParentBufferTransform) {
1826     Test_ChildrenWithParentBufferTransform();
1827 }
1828 
TEST_F(ChildLayerTest_2_1,DISABLED_Bug36858924)1829 TEST_F(ChildLayerTest_2_1, DISABLED_Bug36858924) {
1830     Test_Bug36858924();
1831 }
1832 
1833 template <typename FakeComposerService>
1834 class ChildColorLayerTest : public ChildLayerTest<FakeComposerService> {
1835     using Base = ChildLayerTest<FakeComposerService>;
1836 
1837 protected:
SetUp()1838     void SetUp() override {
1839         Base::SetUp();
1840         Base::mChild =
1841                 Base::mComposerClient->createSurface(String8("Child surface"), 0, 0,
1842                                                      PIXEL_FORMAT_RGBA_8888,
1843                                                      ISurfaceComposerClient::eFXSurfaceEffect,
1844                                                      Base::mFGSurfaceControl.get());
1845         {
1846             TransactionScope ts(*Base::sFakeComposer);
1847             ts.setColor(Base::mChild,
1848                         {LIGHT_GRAY.r / 255.0f, LIGHT_GRAY.g / 255.0f, LIGHT_GRAY.b / 255.0f});
1849             ts.setCrop_legacy(Base::mChild, Rect(0, 0, 10, 10));
1850         }
1851 
1852         Base::sFakeComposer->runVSyncAndWait();
1853         Base::mBaseFrame.push_back(makeSimpleRect(64, 64, 64 + 10, 64 + 10));
1854         Base::mBaseFrame[Base::CHILD_LAYER].mSourceCrop = hwc_frect_t{0.0f, 0.0f, 0.0f, 0.0f};
1855         Base::mBaseFrame[Base::CHILD_LAYER].mSwapCount = 0;
1856         ASSERT_EQ(2, Base::sFakeComposer->getFrameCount());
1857         ASSERT_TRUE(framesAreSame(Base::mBaseFrame, Base::sFakeComposer->getLatestFrame()));
1858     }
1859 
Test_LayerAlpha()1860     void Test_LayerAlpha() {
1861         {
1862             TransactionScope ts(*Base::sFakeComposer);
1863             ts.show(Base::mChild);
1864             ts.setPosition(Base::mChild, 0, 0);
1865             ts.setPosition(Base::mFGSurfaceControl, 0, 0);
1866             ts.setAlpha(Base::mChild, 0.5);
1867         }
1868 
1869         auto referenceFrame = Base::mBaseFrame;
1870         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 64, 64};
1871         referenceFrame[Base::CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 10, 10};
1872         referenceFrame[Base::CHILD_LAYER].mPlaneAlpha = 0.5f;
1873         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1874 
1875         {
1876             TransactionScope ts(*Base::sFakeComposer);
1877             ts.setAlpha(Base::mFGSurfaceControl, 0.5);
1878         }
1879 
1880         auto referenceFrame2 = referenceFrame;
1881         referenceFrame2[Base::FG_LAYER].mPlaneAlpha = 0.5f;
1882         referenceFrame2[Base::CHILD_LAYER].mPlaneAlpha = 0.25f;
1883         EXPECT_TRUE(framesAreSame(referenceFrame2, Base::sFakeComposer->getLatestFrame()));
1884     }
1885 
Test_LayerZeroAlpha()1886     void Test_LayerZeroAlpha() {
1887         {
1888             TransactionScope ts(*Base::sFakeComposer);
1889             ts.show(Base::mChild);
1890             ts.setPosition(Base::mChild, 0, 0);
1891             ts.setPosition(Base::mFGSurfaceControl, 0, 0);
1892             ts.setAlpha(Base::mChild, 0.5);
1893         }
1894 
1895         auto referenceFrame = Base::mBaseFrame;
1896         referenceFrame[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 64, 64};
1897         referenceFrame[Base::CHILD_LAYER].mDisplayFrame = hwc_rect_t{0, 0, 10, 10};
1898         referenceFrame[Base::CHILD_LAYER].mPlaneAlpha = 0.5f;
1899         EXPECT_TRUE(framesAreSame(referenceFrame, Base::sFakeComposer->getLatestFrame()));
1900 
1901         {
1902             TransactionScope ts(*Base::sFakeComposer);
1903             ts.setAlpha(Base::mFGSurfaceControl, 0.0f);
1904         }
1905 
1906         std::vector<RenderState> refFrame(1);
1907         refFrame[Base::BG_LAYER] = Base::mBaseFrame[Base::BG_LAYER];
1908 
1909         EXPECT_TRUE(framesAreSame(refFrame, Base::sFakeComposer->getLatestFrame()));
1910     }
1911 };
1912 
1913 using ChildColorLayerTest_2_1 = ChildColorLayerTest<FakeComposerService_2_1>;
1914 
TEST_F(ChildColorLayerTest_2_1,DISABLED_LayerAlpha)1915 TEST_F(ChildColorLayerTest_2_1, DISABLED_LayerAlpha) {
1916     Test_LayerAlpha();
1917 }
1918 
TEST_F(ChildColorLayerTest_2_1,DISABLED_LayerZeroAlpha)1919 TEST_F(ChildColorLayerTest_2_1, DISABLED_LayerZeroAlpha) {
1920     Test_LayerZeroAlpha();
1921 }
1922 
1923 template <typename FakeComposerService>
1924 class LatchingTest : public TransactionTest<FakeComposerService> {
1925     using Base = TransactionTest<FakeComposerService>;
1926 
1927 protected:
lockAndFillFGBuffer()1928     void lockAndFillFGBuffer() { fillSurfaceRGBA8(Base::mFGSurfaceControl, RED, false); }
1929 
unlockFGBuffer()1930     void unlockFGBuffer() {
1931         sp<Surface> s = Base::mFGSurfaceControl->getSurface();
1932         ASSERT_EQ(NO_ERROR, s->unlockAndPost());
1933         Base::sFakeComposer->runVSyncAndWait();
1934     }
1935 
completeFGResize()1936     void completeFGResize() {
1937         fillSurfaceRGBA8(Base::mFGSurfaceControl, RED);
1938         Base::sFakeComposer->runVSyncAndWait();
1939     }
restoreInitialState()1940     void restoreInitialState() {
1941         TransactionScope ts(*Base::sFakeComposer);
1942         ts.setSize(Base::mFGSurfaceControl, 64, 64);
1943         ts.setPosition(Base::mFGSurfaceControl, 64, 64);
1944         ts.setCrop_legacy(Base::mFGSurfaceControl, Rect(0, 0, 64, 64));
1945     }
1946 
Test_SurfacePositionLatching()1947     void Test_SurfacePositionLatching() {
1948         // By default position can be updated even while
1949         // a resize is pending.
1950         {
1951             TransactionScope ts(*Base::sFakeComposer);
1952             ts.setSize(Base::mFGSurfaceControl, 32, 32);
1953             ts.setPosition(Base::mFGSurfaceControl, 100, 100);
1954         }
1955 
1956         // The size should not have updated as we have not provided a new buffer.
1957         auto referenceFrame1 = Base::mBaseFrame;
1958         referenceFrame1[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{100, 100, 100 + 64, 100 + 64};
1959         EXPECT_TRUE(framesAreSame(referenceFrame1, Base::sFakeComposer->getLatestFrame()));
1960 
1961         restoreInitialState();
1962 
1963         completeFGResize();
1964 
1965         auto referenceFrame2 = Base::mBaseFrame;
1966         referenceFrame2[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{100, 100, 100 + 32, 100 + 32};
1967         referenceFrame2[Base::FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 32.f, 32.f};
1968         referenceFrame2[Base::FG_LAYER].mSwapCount++;
1969         EXPECT_TRUE(framesAreSame(referenceFrame2, Base::sFakeComposer->getLatestFrame()));
1970     }
1971 
Test_CropLatching()1972     void Test_CropLatching() {
1973         // Normally the crop applies immediately even while a resize is pending.
1974         {
1975             TransactionScope ts(*Base::sFakeComposer);
1976             ts.setSize(Base::mFGSurfaceControl, 128, 128);
1977             ts.setCrop_legacy(Base::mFGSurfaceControl, Rect(0, 0, 63, 63));
1978         }
1979 
1980         auto referenceFrame1 = Base::mBaseFrame;
1981         referenceFrame1[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 63, 64 + 63};
1982         referenceFrame1[Base::FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 63.f, 63.f};
1983         EXPECT_TRUE(framesAreSame(referenceFrame1, Base::sFakeComposer->getLatestFrame()));
1984 
1985         restoreInitialState();
1986 
1987         completeFGResize();
1988 
1989         auto referenceFrame2 = Base::mBaseFrame;
1990         referenceFrame2[Base::FG_LAYER].mDisplayFrame = hwc_rect_t{64, 64, 64 + 63, 64 + 63};
1991         referenceFrame2[Base::FG_LAYER].mSourceCrop = hwc_frect_t{0.f, 0.f, 63.f, 63.f};
1992         referenceFrame2[Base::FG_LAYER].mSwapCount++;
1993         EXPECT_TRUE(framesAreSame(referenceFrame2, Base::sFakeComposer->getLatestFrame()));
1994     }
1995 };
1996 
1997 using LatchingTest_2_1 = LatchingTest<FakeComposerService_2_1>;
1998 
TEST_F(LatchingTest_2_1,DISABLED_SurfacePositionLatching)1999 TEST_F(LatchingTest_2_1, DISABLED_SurfacePositionLatching) {
2000     Test_SurfacePositionLatching();
2001 }
2002 
TEST_F(LatchingTest_2_1,DISABLED_CropLatching)2003 TEST_F(LatchingTest_2_1, DISABLED_CropLatching) {
2004     Test_CropLatching();
2005 }
2006 
2007 } // namespace
2008 
main(int argc,char ** argv)2009 int main(int argc, char** argv) {
2010     ::testing::InitGoogleTest(&argc, argv);
2011 
2012     auto* fakeEnvironment = new sftest::FakeHwcEnvironment;
2013     ::testing::AddGlobalTestEnvironment(fakeEnvironment);
2014     ::testing::InitGoogleMock(&argc, argv);
2015     return RUN_ALL_TESTS();
2016 }
2017 
2018 // TODO(b/129481165): remove the #pragma below and fix conversion issues
2019 #pragma clang diagnostic pop // ignored "-Wconversion"
2020