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