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