Home | History | Annotate | Download | only in tests
      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 <gui/BufferItemConsumer.h>
     24 #include <utils/String8.h>
     25 
     26 #include <private/gui/ComposerService.h>
     27 #include <binder/ProcessState.h>
     28 
     29 namespace android {
     30 
     31 class SurfaceTest : public ::testing::Test {
     32 protected:
     33 
     34     SurfaceTest() {
     35         ProcessState::self()->startThreadPool();
     36     }
     37 
     38     virtual void SetUp() {
     39         mComposerClient = new SurfaceComposerClient;
     40         ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
     41 
     42         mSurfaceControl = mComposerClient->createSurface(
     43                 String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
     44 
     45         ASSERT_TRUE(mSurfaceControl != NULL);
     46         ASSERT_TRUE(mSurfaceControl->isValid());
     47 
     48         SurfaceComposerClient::openGlobalTransaction();
     49         ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
     50         ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
     51         SurfaceComposerClient::closeGlobalTransaction();
     52 
     53         mSurface = mSurfaceControl->getSurface();
     54         ASSERT_TRUE(mSurface != NULL);
     55     }
     56 
     57     virtual void TearDown() {
     58         mComposerClient->dispose();
     59     }
     60 
     61     sp<Surface> mSurface;
     62     sp<SurfaceComposerClient> mComposerClient;
     63     sp<SurfaceControl> mSurfaceControl;
     64 };
     65 
     66 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
     67     sp<ANativeWindow> anw(mSurface);
     68     int result = -123;
     69     int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
     70             &result);
     71     EXPECT_EQ(NO_ERROR, err);
     72     EXPECT_EQ(1, result);
     73 }
     74 
     75 TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
     76     mSurfaceControl.clear();
     77 
     78     sp<ANativeWindow> anw(mSurface);
     79     int result = -123;
     80     int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
     81             &result);
     82     EXPECT_EQ(NO_ERROR, err);
     83     EXPECT_EQ(1, result);
     84 }
     85 
     86 // This test probably doesn't belong here.
     87 TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
     88     sp<ANativeWindow> anw(mSurface);
     89 
     90     // Verify the screenshot works with no protected buffers.
     91     sp<BufferQueue> bq = new BufferQueue();
     92     sp<CpuConsumer> consumer = new CpuConsumer(bq, 1);
     93     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
     94     sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
     95     ASSERT_EQ(NO_ERROR, sf->captureScreen(display, bq,
     96             64, 64, 0, 0x7fffffff));
     97 
     98     // Set the PROTECTED usage bit and verify that the screenshot fails.  Note
     99     // that we need to dequeue a buffer in order for it to actually get
    100     // allocated in SurfaceFlinger.
    101     ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
    102             GRALLOC_USAGE_PROTECTED));
    103     ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
    104     ANativeWindowBuffer* buf = 0;
    105 
    106     status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
    107     if (err) {
    108         // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
    109         // that's okay as long as this is the reason for the failure.
    110         // try again without the GRALLOC_USAGE_PROTECTED bit.
    111         ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
    112         ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
    113                 &buf));
    114         return;
    115     }
    116     ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
    117 
    118     for (int i = 0; i < 4; i++) {
    119         // Loop to make sure SurfaceFlinger has retired a protected buffer.
    120         ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
    121                 &buf));
    122         ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
    123     }
    124     ASSERT_EQ(NO_ERROR, sf->captureScreen(display, bq,
    125             64, 64, 0, 0x7fffffff));
    126 }
    127 
    128 TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
    129     sp<ANativeWindow> anw(mSurface);
    130     int result = -123;
    131     int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
    132     EXPECT_EQ(NO_ERROR, err);
    133     EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
    134 }
    135 
    136 TEST_F(SurfaceTest, QueryConsumerUsage) {
    137     const int TEST_USAGE_FLAGS =
    138             GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
    139     sp<BufferQueue> bq = new BufferQueue();
    140     sp<BufferItemConsumer> c = new BufferItemConsumer(bq,
    141             TEST_USAGE_FLAGS);
    142     sp<Surface> s = new Surface(bq);
    143 
    144     sp<ANativeWindow> anw(s);
    145 
    146     int flags = -1;
    147     int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
    148 
    149     ASSERT_EQ(NO_ERROR, err);
    150     ASSERT_EQ(TEST_USAGE_FLAGS, flags);
    151 }
    152 
    153 }
    154