Surface_test.cpp revision fc850124b35abd00a31e9bcf57e14e0fa74f4cd7
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
27class SurfaceTest : public ::testing::Test {
28protected:
29    virtual void SetUp() {
30        mComposerClient = new SurfaceComposerClient;
31        ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
32
33        mSurfaceControl = mComposerClient->createSurface(
34                String8("Test Surface"), 0, 32, 32, PIXEL_FORMAT_RGB_888, 0);
35
36        ASSERT_TRUE(mSurfaceControl != NULL);
37        ASSERT_TRUE(mSurfaceControl->isValid());
38
39        ASSERT_EQ(NO_ERROR, mComposerClient->openTransaction());
40        ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(30000));
41        ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
42        ASSERT_EQ(NO_ERROR, mComposerClient->closeTransaction());
43
44        mSurface = mSurfaceControl->getSurface();
45        ASSERT_TRUE(mSurface != NULL);
46    }
47
48    virtual void TearDown() {
49        mComposerClient->dispose();
50    }
51
52    sp<Surface> mSurface;
53    sp<SurfaceComposerClient> mComposerClient;
54    sp<SurfaceControl> mSurfaceControl;
55};
56
57TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
58    sp<ANativeWindow> anw(mSurface);
59    int result = -123;
60    int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
61            &result);
62    EXPECT_EQ(NO_ERROR, err);
63    EXPECT_EQ(1, result);
64}
65
66TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
67    mSurfaceControl.clear();
68
69    sp<ANativeWindow> anw(mSurface);
70    int result = -123;
71    int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
72            &result);
73    EXPECT_EQ(NO_ERROR, err);
74    EXPECT_EQ(1, result);
75}
76
77// This test probably doesn't belong here.
78TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersFail) {
79    sp<ANativeWindow> anw(mSurface);
80
81    // Verify the screenshot works with no protected buffers.
82    sp<IMemoryHeap> heap;
83    uint32_t w=0, h=0;
84    PixelFormat fmt=0;
85    sp<ISurfaceComposer> sf(ComposerService::getComposerService());
86    ASSERT_EQ(NO_ERROR, sf->captureScreen(0, &heap, &w, &h, &fmt, 64, 64, 0,
87            40000));
88    ASSERT_TRUE(heap != NULL);
89
90    // Set the PROTECTED usage bit and verify that the screenshot fails.  Note
91    // that we need to dequeue a buffer in order for it to actually get
92    // allocated in SurfaceFlinger.
93    ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
94            GRALLOC_USAGE_PROTECTED));
95    ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
96    android_native_buffer_t* buf = 0;
97    for (int i = 0; i < 4; i++) {
98        // Loop to make sure SurfaceFlinger has retired a protected buffer.
99        ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &buf));
100        ASSERT_EQ(NO_ERROR, anw->lockBuffer(anw.get(), buf));
101        ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf));
102    }
103    heap = 0;
104    w = h = fmt = 0;
105    ASSERT_EQ(INVALID_OPERATION, sf->captureScreen(0, &heap, &w, &h, &fmt,
106            64, 64, 0, 40000));
107    ASSERT_TRUE(heap == NULL);
108
109    // XXX: This should not be needed, but it seems that the new buffers don't
110    // correctly show up after the upcoming dequeue/lock/queue loop without it.
111    // We should look into this at some point.
112    ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
113
114    // Un-set the PROTECTED usage bit and verify that the screenshot works
115    // again.  Note that we have to change the buffers geometry to ensure that
116    // the buffers get reallocated, as the new usage bits are a subset of the
117    // old.
118    ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
119    ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(anw.get(), 32, 32, 0));
120    for (int i = 0; i < 4; i++) {
121        // Loop to make sure SurfaceFlinger has retired a protected buffer.
122        ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &buf));
123        ASSERT_EQ(NO_ERROR, anw->lockBuffer(anw.get(), buf));
124        ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf));
125    }
126    heap = 0;
127    w = h = fmt = 0;
128    ASSERT_EQ(NO_ERROR, sf->captureScreen(0, &heap, &w, &h, &fmt, 64, 64, 0,
129            40000));
130    ASSERT_TRUE(heap != NULL);
131}
132
133TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
134    sp<ANativeWindow> anw(mSurface);
135    int result = -123;
136    int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
137    EXPECT_EQ(NO_ERROR, err);
138    EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
139}
140
141}
142