blob: 545b547af109df90f44b6ccc5d822e60c02f34d1 [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>
26
Jamie Gennis134f0422011-03-08 12:18:54 -080027namespace android {
28
29class SurfaceTest : public ::testing::Test {
30protected:
Jamie Gennis134f0422011-03-08 12:18:54 -080031 virtual void SetUp() {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080032 mComposerClient = new SurfaceComposerClient;
Jamie Gennis134f0422011-03-08 12:18:54 -080033 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
34
Jamie Gennisfc850122011-04-25 16:40:05 -070035 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070036 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
Jamie Gennis134f0422011-03-08 12:18:54 -080037
38 ASSERT_TRUE(mSurfaceControl != NULL);
39 ASSERT_TRUE(mSurfaceControl->isValid());
40
Mathias Agopian698c0872011-06-28 19:09:31 -070041 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian9303eee2011-07-01 15:27:27 -070042 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
Jamie Gennis134f0422011-03-08 12:18:54 -080043 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070044 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennis134f0422011-03-08 12:18:54 -080045
46 mSurface = mSurfaceControl->getSurface();
47 ASSERT_TRUE(mSurface != NULL);
48 }
49
50 virtual void TearDown() {
51 mComposerClient->dispose();
52 }
53
54 sp<Surface> mSurface;
55 sp<SurfaceComposerClient> mComposerClient;
56 sp<SurfaceControl> mSurfaceControl;
57};
58
59TEST_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
68TEST_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
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080079// This test probably doesn't belong here.
Jamie Gennisc901ca02011-10-11 16:02:31 -070080TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080081 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());
Jeff Brown9d4e3d22012-08-24 20:00:51 -070088 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
89 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &heap, &w, &h, &fmt, 64, 64, 0,
Mathias Agopian9303eee2011-07-01 15:27:27 -070090 0x7fffffff));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -080091 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));
Iliyan Malchev697526b2011-05-01 11:33:26 -070099 ANativeWindowBuffer* buf = 0;
Mathias Agopian9303eee2011-07-01 15:27:27 -0700100
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700101 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
Mathias Agopian9303eee2011-07-01 15:27:27 -0700102 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));
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700107 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
108 &buf));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700109 return;
110 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700111 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
Mathias Agopian9303eee2011-07-01 15:27:27 -0700112
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800113 for (int i = 0; i < 4; i++) {
114 // Loop to make sure SurfaceFlinger has retired a protected buffer.
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700115 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));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800118 }
119 heap = 0;
120 w = h = fmt = 0;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700121 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &heap, &w, &h, &fmt,
Mathias Agopian9303eee2011-07-01 15:27:27 -0700122 64, 64, 0, 0x7fffffff));
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800123 ASSERT_TRUE(heap != NULL);
124}
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}