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