blob: 429becfd4f784c152d76bdaf2fcad416128faee5 [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>
Mathias Agopian90ac7992012-02-25 18:48:35 -080020#include <gui/ISurfaceComposer.h>
21#include <gui/Surface.h>
22#include <gui/SurfaceComposerClient.h>
Jamie Gennis134f0422011-03-08 12:18:54 -080023#include <utils/String8.h>
24
Mathias Agopian41f673c2011-11-17 17:48:35 -080025#include <private/gui/ComposerService.h>
Mathias Agopian7c1a4872013-03-20 15:56:04 -070026#include <binder/ProcessState.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080027
Jamie Gennis134f0422011-03-08 12:18:54 -080028namespace android {
29
30class SurfaceTest : public ::testing::Test {
31protected:
Mathias Agopian7c1a4872013-03-20 15:56:04 -070032
33 SurfaceTest() {
34 ProcessState::self()->startThreadPool();
35 }
36
Jamie Gennis134f0422011-03-08 12:18:54 -080037 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080038 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080039 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
40
Jamie Gennisfc850122011-04-25 16:40:05 -070041 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070042 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080043
44 ASSERT_TRUE(mSurfaceControl != NULL);
45 ASSERT_TRUE(mSurfaceControl->isValid());
46
Mathias Agopian698c0872011-06-28 19:09:31 -070047 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian9303eee2011-07-01 15:27:27 -070048 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
Jamie Gennis134f0422011-03-08 12:18:54 -080049 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070050 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennis134f0422011-03-08 12:18:54 -080051
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
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080085// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -070086TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080087 sp<ANativeWindow> anw(mSurface);
88
89 // Verify the screenshot works with no protected buffers.
Mathias Agopian7c1a4872013-03-20 15:56:04 -070090 sp<CpuConsumer> consumer = new CpuConsumer(1);
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080091 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Jeff Brown9d4e3d22012-08-24 20:00:51 -070092 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Mathias Agopian7c1a4872013-03-20 15:56:04 -070093 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, consumer->getBufferQueue(),
94 64, 64, 0, 0x7fffffff, true));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080095
96 // Set the PROTECTED usage bit and verify that the screenshot fails. Note
97 // that we need to dequeue a buffer in order for it to actually get
98 // allocated in SurfaceFlinger.
99 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
100 GRALLOC_USAGE_PROTECTED));
101 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
Iliyan Malchev697526b2011-05-01 11:33:26 -0700102 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700103
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700104 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700105 if (err) {
106 // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
107 // that's okay as long as this is the reason for the failure.
108 // try again without the GRALLOC_USAGE_PROTECTED bit.
109 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700110 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
111 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700112 return;
113 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700114 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700115
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800116 for (int i = 0; i < 4; i++) {
117 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700118 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
119 &buf));
120 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800121 }
Mathias Agopian7c1a4872013-03-20 15:56:04 -0700122 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, consumer->getBufferQueue(),
123 64, 64, 0, 0x7fffffff, true));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800124}
125
Jamie Gennis391bbe22011-03-14 15:00:06 -0700126TEST_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
Jamie Gennis134f0422011-03-08 12:18:54 -0800134}