blob: ce587b3ccdd87ffd370467bff6b627209a5bedfb [file] [log] [blame]
Jamie Gennis134f0422011-03-08 12:18:54 -08001/*
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>
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080018
19#include <binder/IMemory.h>
Jamie Gennis134f0422011-03-08 12:18:54 -080020#include <surfaceflinger/ISurfaceComposer.h>
21#include <surfaceflinger/Surface.h>
22#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis134f0422011-03-08 12:18:54 -080023#include <utils/String8.h>
24
25namespace android {
26
27class SurfaceTest : public ::testing::Test {
28protected:
Jamie Gennis134f0422011-03-08 12:18:54 -080029 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080030 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080031 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
32
Jamie Gennisfc850122011-04-25 16:40:05 -070033 mSurfaceControl = mComposerClient->createSurface(
Mathias Agopian9303eee2011-07-01 15:27:27 -070034 String8("Test Surface"), 0, 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080035
36 ASSERT_TRUE(mSurfaceControl != NULL);
37 ASSERT_TRUE(mSurfaceControl->isValid());
38
Mathias Agopian698c0872011-06-28 19:09:31 -070039 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian9303eee2011-07-01 15:27:27 -070040 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
Jamie Gennis134f0422011-03-08 12:18:54 -080041 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070042 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennis134f0422011-03-08 12:18:54 -080043
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
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080077// 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,
Mathias Agopian9303eee2011-07-01 15:27:27 -070087 0x7fffffff));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080088 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));
Iliyan Malchev697526b2011-05-01 11:33:26 -070096 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -070097
98 status_t err = anw->dequeueBuffer(anw.get(), &buf);
99 if (err) {
100 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
101 // that's okay as long as this is the reason for the failure.
102 // try again without the GRALLOC_USAGE_PROTECTED bit.
103 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
104 ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &buf));
105 return;
106 }
107 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf));
108
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800109 for (int i = 0; i < 4; i++) {
110 // Loop to make sure SurfaceFlinger has retired a protected buffer.
111 ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &buf));
112 ASSERT_EQ(NO_ERROR, anw->lockBuffer(anw.get(), buf));
113 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf));
114 }
115 heap = 0;
116 w = h = fmt = 0;
117 ASSERT_EQ(INVALID_OPERATION, sf->captureScreen(0, &heap, &w, &h, &fmt,
Mathias Agopian9303eee2011-07-01 15:27:27 -0700118 64, 64, 0, 0x7fffffff));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800119 ASSERT_TRUE(heap == NULL);
120
121 // XXX: This should not be needed, but it seems that the new buffers don't
122 // correctly show up after the upcoming dequeue/lock/queue loop without it.
123 // We should look into this at some point.
124 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
125
126 // Un-set the PROTECTED usage bit and verify that the screenshot works
127 // again. Note that we have to change the buffers geometry to ensure that
128 // the buffers get reallocated, as the new usage bits are a subset of the
129 // old.
130 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
131 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(anw.get(), 32, 32, 0));
132 for (int i = 0; i < 4; i++) {
133 // Loop to make sure SurfaceFlinger has retired a protected buffer.
134 ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &buf));
135 ASSERT_EQ(NO_ERROR, anw->lockBuffer(anw.get(), buf));
136 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf));
137 }
138 heap = 0;
139 w = h = fmt = 0;
140 ASSERT_EQ(NO_ERROR, sf->captureScreen(0, &heap, &w, &h, &fmt, 64, 64, 0,
Mathias Agopian9303eee2011-07-01 15:27:27 -0700141 0x7fffffff));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800142 ASSERT_TRUE(heap != NULL);
143}
144
Jamie Gennis391bbe22011-03-14 15:00:06 -0700145TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
146 sp<ANativeWindow> anw(mSurface);
147 int result = -123;
148 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
149 EXPECT_EQ(NO_ERROR, err);
150 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
151}
152
Jamie Gennis134f0422011-03-08 12:18:54 -0800153}