Surface_test.cpp revision 5603a2fbbd1aae74c4635e2f600819fb05d112e0
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 <gui/ISurfaceComposer.h>
21#include <gui/Surface.h>
22#include <gui/SurfaceComposerClient.h>
23#include <gui/BufferItemConsumer.h>
24#include <utils/String8.h>
25
26#include <private/gui/ComposerService.h>
27#include <binder/ProcessState.h>
28
29namespace android {
30
31class SurfaceTest : public ::testing::Test {
32protected:
33
34    SurfaceTest() {
35        ProcessState::self()->startThreadPool();
36    }
37
38    virtual void SetUp() {
39        mComposerClient = new SurfaceComposerClient;
40        ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
41
42        mSurfaceControl = mComposerClient->createSurface(
43                String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
44
45        ASSERT_TRUE(mSurfaceControl != NULL);
46        ASSERT_TRUE(mSurfaceControl->isValid());
47
48        SurfaceComposerClient::openGlobalTransaction();
49        ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
50        ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
51        SurfaceComposerClient::closeGlobalTransaction();
52
53        mSurface = mSurfaceControl->getSurface();
54        ASSERT_TRUE(mSurface != NULL);
55    }
56
57    virtual void TearDown() {
58        mComposerClient->dispose();
59    }
60
61    sp<Surface> mSurface;
62    sp<SurfaceComposerClient> mComposerClient;
63    sp<SurfaceControl> mSurfaceControl;
64};
65
66TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
67    sp<ANativeWindow> anw(mSurface);
68    int result = -123;
69    int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
70            &result);
71    EXPECT_EQ(NO_ERROR, err);
72    EXPECT_EQ(1, result);
73}
74
75TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
76    mSurfaceControl.clear();
77
78    sp<ANativeWindow> anw(mSurface);
79    int result = -123;
80    int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
81            &result);
82    EXPECT_EQ(NO_ERROR, err);
83    EXPECT_EQ(1, result);
84}
85
86// This test probably doesn't belong here.
87TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
88    sp<ANativeWindow> anw(mSurface);
89
90    // Verify the screenshot works with no protected buffers.
91    sp<IGraphicBufferProducer> producer;
92    sp<IGraphicBufferConsumer> consumer;
93    BufferQueue::createBufferQueue(&producer, &consumer);
94    sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
95    sp<ISurfaceComposer> sf(ComposerService::getComposerService());
96    sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
97    ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer,
98            64, 64, 0, 0x7fffffff, false));
99
100    // Set the PROTECTED usage bit and verify that the screenshot fails.  Note
101    // that we need to dequeue a buffer in order for it to actually get
102    // allocated in SurfaceFlinger.
103    ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
104            GRALLOC_USAGE_PROTECTED));
105    ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
106    ANativeWindowBuffer* buf = 0;
107
108    status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
109    if (err) {
110        // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
111        // that's okay as long as this is the reason for the failure.
112        // try again without the GRALLOC_USAGE_PROTECTED bit.
113        ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
114        ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
115                &buf));
116        return;
117    }
118    ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
119
120    for (int i = 0; i < 4; i++) {
121        // Loop to make sure SurfaceFlinger has retired a protected buffer.
122        ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
123                &buf));
124        ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
125    }
126    ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer,
127            64, 64, 0, 0x7fffffff, false));
128}
129
130TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
131    sp<ANativeWindow> anw(mSurface);
132    int result = -123;
133    int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
134    EXPECT_EQ(NO_ERROR, err);
135    EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
136}
137
138TEST_F(SurfaceTest, QueryConsumerUsage) {
139    const int TEST_USAGE_FLAGS =
140            GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
141    sp<IGraphicBufferProducer> producer;
142    sp<IGraphicBufferConsumer> consumer;
143    BufferQueue::createBufferQueue(&producer, &consumer);
144    sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
145            TEST_USAGE_FLAGS);
146    sp<Surface> s = new Surface(producer);
147
148    sp<ANativeWindow> anw(s);
149
150    int flags = -1;
151    int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
152
153    ASSERT_EQ(NO_ERROR, err);
154    ASSERT_EQ(TEST_USAGE_FLAGS, flags);
155}
156
157}
158