2011-03-08 20:18:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2011-03-10 01:05:02 +00:00
|
|
|
|
|
|
|
#include <binder/IMemory.h>
|
2012-02-26 02:48:35 +00:00
|
|
|
#include <gui/ISurfaceComposer.h>
|
|
|
|
#include <gui/Surface.h>
|
|
|
|
#include <gui/SurfaceComposerClient.h>
|
2013-07-30 21:05:02 +00:00
|
|
|
#include <gui/BufferItemConsumer.h>
|
2014-05-22 22:59:05 +00:00
|
|
|
#include <ui/Rect.h>
|
2011-03-08 20:18:54 +00:00
|
|
|
#include <utils/String8.h>
|
|
|
|
|
2011-11-18 01:48:35 +00:00
|
|
|
#include <private/gui/ComposerService.h>
|
2013-03-20 22:56:04 +00:00
|
|
|
#include <binder/ProcessState.h>
|
2011-11-18 01:48:35 +00:00
|
|
|
|
2011-03-08 20:18:54 +00:00
|
|
|
namespace android {
|
|
|
|
|
|
|
|
class SurfaceTest : public ::testing::Test {
|
|
|
|
protected:
|
2013-03-20 22:56:04 +00:00
|
|
|
|
|
|
|
SurfaceTest() {
|
|
|
|
ProcessState::self()->startThreadPool();
|
|
|
|
}
|
|
|
|
|
2011-03-08 20:18:54 +00:00
|
|
|
virtual void SetUp() {
|
2011-03-10 01:05:02 +00:00
|
|
|
mComposerClient = new SurfaceComposerClient;
|
2011-03-08 20:18:54 +00:00
|
|
|
ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
|
|
|
|
|
2011-04-25 23:40:05 +00:00
|
|
|
mSurfaceControl = mComposerClient->createSurface(
|
2012-08-25 03:00:51 +00:00
|
|
|
String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
|
2011-03-08 20:18:54 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(mSurfaceControl != NULL);
|
|
|
|
ASSERT_TRUE(mSurfaceControl->isValid());
|
|
|
|
|
2011-06-29 02:09:31 +00:00
|
|
|
SurfaceComposerClient::openGlobalTransaction();
|
2011-07-01 22:27:27 +00:00
|
|
|
ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
|
2011-03-08 20:18:54 +00:00
|
|
|
ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
|
2011-06-29 02:09:31 +00:00
|
|
|
SurfaceComposerClient::closeGlobalTransaction();
|
2011-03-08 20:18:54 +00:00
|
|
|
|
|
|
|
mSurface = mSurfaceControl->getSurface();
|
|
|
|
ASSERT_TRUE(mSurface != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void TearDown() {
|
|
|
|
mComposerClient->dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<Surface> mSurface;
|
|
|
|
sp<SurfaceComposerClient> mComposerClient;
|
|
|
|
sp<SurfaceControl> mSurfaceControl;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
|
|
|
|
sp<ANativeWindow> anw(mSurface);
|
|
|
|
int result = -123;
|
|
|
|
int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
|
|
|
|
&result);
|
|
|
|
EXPECT_EQ(NO_ERROR, err);
|
|
|
|
EXPECT_EQ(1, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
|
|
|
|
mSurfaceControl.clear();
|
|
|
|
|
|
|
|
sp<ANativeWindow> anw(mSurface);
|
|
|
|
int result = -123;
|
|
|
|
int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
|
|
|
|
&result);
|
|
|
|
EXPECT_EQ(NO_ERROR, err);
|
|
|
|
EXPECT_EQ(1, result);
|
|
|
|
}
|
|
|
|
|
2011-03-10 01:05:02 +00:00
|
|
|
// This test probably doesn't belong here.
|
2011-10-11 23:02:31 +00:00
|
|
|
TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
|
2011-03-10 01:05:02 +00:00
|
|
|
sp<ANativeWindow> anw(mSurface);
|
|
|
|
|
|
|
|
// Verify the screenshot works with no protected buffers.
|
2014-04-07 20:41:37 +00:00
|
|
|
sp<IGraphicBufferProducer> producer;
|
|
|
|
sp<IGraphicBufferConsumer> consumer;
|
|
|
|
BufferQueue::createBufferQueue(&producer, &consumer);
|
|
|
|
sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
|
2011-03-10 01:05:02 +00:00
|
|
|
sp<ISurfaceComposer> sf(ComposerService::getComposerService());
|
2012-08-25 03:00:51 +00:00
|
|
|
sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
|
2014-05-22 22:59:05 +00:00
|
|
|
ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
|
2014-02-20 02:35:30 +00:00
|
|
|
64, 64, 0, 0x7fffffff, false));
|
2011-03-10 01:05:02 +00:00
|
|
|
|
|
|
|
// Set the PROTECTED usage bit and verify that the screenshot fails. Note
|
|
|
|
// that we need to dequeue a buffer in order for it to actually get
|
|
|
|
// allocated in SurfaceFlinger.
|
|
|
|
ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
|
|
|
|
GRALLOC_USAGE_PROTECTED));
|
|
|
|
ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
|
2011-05-01 18:33:26 +00:00
|
|
|
ANativeWindowBuffer* buf = 0;
|
2011-07-01 22:27:27 +00:00
|
|
|
|
2012-06-13 23:32:25 +00:00
|
|
|
status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
|
2011-07-01 22:27:27 +00:00
|
|
|
if (err) {
|
|
|
|
// we could fail if GRALLOC_USAGE_PROTECTED is not supported.
|
|
|
|
// that's okay as long as this is the reason for the failure.
|
|
|
|
// try again without the GRALLOC_USAGE_PROTECTED bit.
|
|
|
|
ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
|
2012-06-13 23:32:25 +00:00
|
|
|
ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
|
|
|
|
&buf));
|
2011-07-01 22:27:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-06-13 23:32:25 +00:00
|
|
|
ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
|
2011-07-01 22:27:27 +00:00
|
|
|
|
2011-03-10 01:05:02 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
// Loop to make sure SurfaceFlinger has retired a protected buffer.
|
2012-06-13 23:32:25 +00:00
|
|
|
ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
|
|
|
|
&buf));
|
|
|
|
ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
|
2011-03-10 01:05:02 +00:00
|
|
|
}
|
2014-05-22 22:59:05 +00:00
|
|
|
ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
|
2014-02-20 02:35:30 +00:00
|
|
|
64, 64, 0, 0x7fffffff, false));
|
2011-03-10 01:05:02 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 22:00:06 +00:00
|
|
|
TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
|
|
|
|
sp<ANativeWindow> anw(mSurface);
|
|
|
|
int result = -123;
|
|
|
|
int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
|
|
|
|
EXPECT_EQ(NO_ERROR, err);
|
|
|
|
EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
|
|
|
|
}
|
|
|
|
|
2013-07-30 21:05:02 +00:00
|
|
|
TEST_F(SurfaceTest, QueryConsumerUsage) {
|
|
|
|
const int TEST_USAGE_FLAGS =
|
|
|
|
GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
|
2014-04-07 20:41:37 +00:00
|
|
|
sp<IGraphicBufferProducer> producer;
|
|
|
|
sp<IGraphicBufferConsumer> consumer;
|
|
|
|
BufferQueue::createBufferQueue(&producer, &consumer);
|
|
|
|
sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
|
2013-07-30 21:05:02 +00:00
|
|
|
TEST_USAGE_FLAGS);
|
2014-04-07 20:41:37 +00:00
|
|
|
sp<Surface> s = new Surface(producer);
|
2013-07-30 21:05:02 +00:00
|
|
|
|
|
|
|
sp<ANativeWindow> anw(s);
|
|
|
|
|
|
|
|
int flags = -1;
|
|
|
|
int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
|
|
|
|
|
|
|
|
ASSERT_EQ(NO_ERROR, err);
|
|
|
|
ASSERT_EQ(TEST_USAGE_FLAGS, flags);
|
|
|
|
}
|
|
|
|
|
2015-02-20 00:10:43 +00:00
|
|
|
TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
|
|
|
|
const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
|
|
|
|
sp<IGraphicBufferProducer> producer;
|
|
|
|
sp<IGraphicBufferConsumer> consumer;
|
|
|
|
BufferQueue::createBufferQueue(&producer, &consumer);
|
|
|
|
sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
|
|
|
|
|
|
|
|
cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
|
|
|
|
|
|
|
|
sp<Surface> s = new Surface(producer);
|
|
|
|
|
|
|
|
sp<ANativeWindow> anw(s);
|
|
|
|
|
|
|
|
android_dataspace dataSpace;
|
|
|
|
|
|
|
|
int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
|
|
|
|
reinterpret_cast<int*>(&dataSpace));
|
|
|
|
|
|
|
|
ASSERT_EQ(NO_ERROR, err);
|
|
|
|
ASSERT_EQ(TEST_DATASPACE, dataSpace);
|
|
|
|
}
|
|
|
|
|
2015-06-02 22:45:22 +00:00
|
|
|
TEST_F(SurfaceTest, SettingGenerationNumber) {
|
|
|
|
sp<IGraphicBufferProducer> producer;
|
|
|
|
sp<IGraphicBufferConsumer> consumer;
|
|
|
|
BufferQueue::createBufferQueue(&producer, &consumer);
|
|
|
|
sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
|
|
|
|
sp<Surface> surface = new Surface(producer);
|
|
|
|
sp<ANativeWindow> window(surface);
|
|
|
|
|
|
|
|
// Allocate a buffer with a generation number of 0
|
|
|
|
ANativeWindowBuffer* buffer;
|
|
|
|
int fenceFd;
|
|
|
|
ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
|
|
|
|
ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
|
|
|
|
|
|
|
|
// Detach the buffer and check its generation number
|
|
|
|
sp<GraphicBuffer> graphicBuffer;
|
|
|
|
sp<Fence> fence;
|
|
|
|
ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
|
|
|
|
ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
|
|
|
|
|
|
|
|
ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
|
|
|
|
buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
|
|
|
|
|
|
|
|
// This should change the generation number of the GraphicBuffer
|
|
|
|
ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
|
|
|
|
|
|
|
|
// Check that the new generation number sticks with the buffer
|
|
|
|
ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
|
|
|
|
ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
|
|
|
|
graphicBuffer = static_cast<GraphicBuffer*>(buffer);
|
|
|
|
ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
|
|
|
|
}
|
|
|
|
|
2011-03-08 20:18:54 +00:00
|
|
|
}
|