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