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 #include <binder/ProcessState.h> 27 28 namespace android { 29 30 class SurfaceTest : public ::testing::Test { 31 protected: 32 33 SurfaceTest() { 34 ProcessState::self()->startThreadPool(); 35 } 36 37 virtual void SetUp() { 38 mComposerClient = new SurfaceComposerClient; 39 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); 40 41 mSurfaceControl = mComposerClient->createSurface( 42 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0); 43 44 ASSERT_TRUE(mSurfaceControl != NULL); 45 ASSERT_TRUE(mSurfaceControl->isValid()); 46 47 SurfaceComposerClient::openGlobalTransaction(); 48 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff)); 49 ASSERT_EQ(NO_ERROR, mSurfaceControl->show()); 50 SurfaceComposerClient::closeGlobalTransaction(); 51 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 65 TEST_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 74 TEST_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 85 // This test probably doesn't belong here. 86 TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) { 87 sp<ANativeWindow> anw(mSurface); 88 89 // Verify the screenshot works with no protected buffers. 90 sp<CpuConsumer> consumer = new CpuConsumer(1); 91 sp<ISurfaceComposer> sf(ComposerService::getComposerService()); 92 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); 93 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, consumer->getBufferQueue(), 94 64, 64, 0, 0x7fffffff, true)); 95 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)); 102 ANativeWindowBuffer* buf = 0; 103 104 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf); 105 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)); 110 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(), 111 &buf)); 112 return; 113 } 114 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1)); 115 116 for (int i = 0; i < 4; i++) { 117 // Loop to make sure SurfaceFlinger has retired a protected buffer. 118 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)); 121 } 122 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, consumer->getBufferQueue(), 123 64, 64, 0, 0x7fffffff, true)); 124 } 125 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