Surface_test.cpp revision a3fbda3cef04d51a35a3eb64b2f744a989800856
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 28namespace android { 29 30class SurfaceTest : public ::testing::Test { 31protected: 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 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 85// This test probably doesn't belong here. 86TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) { 87 sp<ANativeWindow> anw(mSurface); 88 89 // Verify the screenshot works with no protected buffers. 90 sp<BufferQueue> bq = new BufferQueue(); 91 sp<CpuConsumer> consumer = new CpuConsumer(bq, 1); 92 sp<ISurfaceComposer> sf(ComposerService::getComposerService()); 93 sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); 94 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, consumer->getBufferQueue(), 95 64, 64, 0, 0x7fffffff, true)); 96 97 // Set the PROTECTED usage bit and verify that the screenshot fails. Note 98 // that we need to dequeue a buffer in order for it to actually get 99 // allocated in SurfaceFlinger. 100 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 101 GRALLOC_USAGE_PROTECTED)); 102 ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3)); 103 ANativeWindowBuffer* buf = 0; 104 105 status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf); 106 if (err) { 107 // we could fail if GRALLOC_USAGE_PROTECTED is not supported. 108 // that's okay as long as this is the reason for the failure. 109 // try again without the GRALLOC_USAGE_PROTECTED bit. 110 ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0)); 111 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(), 112 &buf)); 113 return; 114 } 115 ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1)); 116 117 for (int i = 0; i < 4; i++) { 118 // Loop to make sure SurfaceFlinger has retired a protected buffer. 119 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(), 120 &buf)); 121 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1)); 122 } 123 ASSERT_EQ(NO_ERROR, sf->captureScreen(display, consumer->getBufferQueue(), 124 64, 64, 0, 0x7fffffff, true)); 125} 126 127TEST_F(SurfaceTest, ConcreteTypeIsSurface) { 128 sp<ANativeWindow> anw(mSurface); 129 int result = -123; 130 int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result); 131 EXPECT_EQ(NO_ERROR, err); 132 EXPECT_EQ(NATIVE_WINDOW_SURFACE, result); 133} 134 135} 136