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