2011-10-12 02:22:19 +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>
|
|
|
|
|
2015-05-21 20:48:24 +00:00
|
|
|
#include <android/native_window.h>
|
|
|
|
|
2011-10-12 02:22:19 +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>
|
|
|
|
#include <private/gui/ComposerService.h>
|
|
|
|
|
2011-10-12 02:22:19 +00:00
|
|
|
#include <utils/String8.h>
|
2012-07-26 01:56:13 +00:00
|
|
|
#include <ui/DisplayInfo.h>
|
2011-10-12 02:22:19 +00:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
// Fill an RGBA_8888 formatted surface with a single color.
|
|
|
|
static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc,
|
|
|
|
uint8_t r, uint8_t g, uint8_t b) {
|
2013-02-15 01:11:02 +00:00
|
|
|
ANativeWindow_Buffer outBuffer;
|
2011-10-12 02:22:19 +00:00
|
|
|
sp<Surface> s = sc->getSurface();
|
|
|
|
ASSERT_TRUE(s != NULL);
|
2013-02-15 01:11:02 +00:00
|
|
|
ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, NULL));
|
|
|
|
uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
|
|
|
|
for (uint32_t y = 0; y < outBuffer.height; y++) {
|
|
|
|
for (uint32_t x = 0; x < outBuffer.width; x++) {
|
|
|
|
uint8_t* pixel = img + (4 * (y*outBuffer.stride + x));
|
2011-10-12 02:22:19 +00:00
|
|
|
pixel[0] = r;
|
|
|
|
pixel[1] = g;
|
|
|
|
pixel[2] = b;
|
|
|
|
pixel[3] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_EQ(NO_ERROR, s->unlockAndPost());
|
|
|
|
}
|
|
|
|
|
|
|
|
// A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
|
|
|
|
// individual pixel values for testing purposes.
|
|
|
|
class ScreenCapture : public RefBase {
|
|
|
|
public:
|
|
|
|
static void captureScreen(sp<ScreenCapture>* sc) {
|
2015-05-21 20:48:24 +00:00
|
|
|
sp<IGraphicBufferProducer> producer;
|
|
|
|
sp<IGraphicBufferConsumer> consumer;
|
|
|
|
BufferQueue::createBufferQueue(&producer, &consumer);
|
|
|
|
IGraphicBufferProducer::QueueBufferOutput bufferOutput;
|
|
|
|
sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
|
2011-10-12 02:22:19 +00:00
|
|
|
sp<ISurfaceComposer> sf(ComposerService::getComposerService());
|
2015-05-21 20:48:24 +00:00
|
|
|
sp<IBinder> display(sf->getBuiltInDisplay(
|
|
|
|
ISurfaceComposer::eDisplayIdMain));
|
|
|
|
ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(), 0, 0,
|
|
|
|
0, INT_MAX, false));
|
|
|
|
*sc = new ScreenCapture(cpuConsumer);
|
2011-10-12 02:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
|
2015-05-21 20:48:24 +00:00
|
|
|
ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mBuf.format);
|
|
|
|
const uint8_t* img = static_cast<const uint8_t*>(mBuf.data);
|
|
|
|
const uint8_t* pixel = img + (4 * (y * mBuf.stride + x));
|
2011-10-12 02:22:19 +00:00
|
|
|
if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
|
|
|
|
String8 err(String8::format("pixel @ (%3d, %3d): "
|
|
|
|
"expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
|
|
|
|
x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
|
|
|
|
EXPECT_EQ(String8(), err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-05-21 20:48:24 +00:00
|
|
|
ScreenCapture(const sp<CpuConsumer>& cc) :
|
|
|
|
mCC(cc) {
|
|
|
|
EXPECT_EQ(NO_ERROR, mCC->lockNextBuffer(&mBuf));
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScreenCapture() {
|
|
|
|
mCC->unlockBuffer(mBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<CpuConsumer> mCC;
|
|
|
|
CpuConsumer::LockedBuffer mBuf;
|
2011-10-12 02:22:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class LayerUpdateTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
virtual void SetUp() {
|
|
|
|
mComposerClient = new SurfaceComposerClient;
|
|
|
|
ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
|
|
|
|
|
2012-08-25 03:00:51 +00:00
|
|
|
sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(
|
|
|
|
ISurfaceComposer::eDisplayIdMain));
|
2012-07-26 01:56:13 +00:00
|
|
|
DisplayInfo info;
|
2012-08-25 03:00:51 +00:00
|
|
|
SurfaceComposerClient::getDisplayInfo(display, &info);
|
2012-07-26 01:56:13 +00:00
|
|
|
|
|
|
|
ssize_t displayWidth = info.w;
|
|
|
|
ssize_t displayHeight = info.h;
|
2011-10-12 02:22:19 +00:00
|
|
|
|
|
|
|
// Background surface
|
|
|
|
mBGSurfaceControl = mComposerClient->createSurface(
|
2012-08-25 03:00:51 +00:00
|
|
|
String8("BG Test Surface"), displayWidth, displayHeight,
|
2011-10-12 02:22:19 +00:00
|
|
|
PIXEL_FORMAT_RGBA_8888, 0);
|
|
|
|
ASSERT_TRUE(mBGSurfaceControl != NULL);
|
|
|
|
ASSERT_TRUE(mBGSurfaceControl->isValid());
|
|
|
|
fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
|
|
|
|
|
|
|
|
// Foreground surface
|
|
|
|
mFGSurfaceControl = mComposerClient->createSurface(
|
2012-08-25 03:00:51 +00:00
|
|
|
String8("FG Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
|
2011-10-12 02:22:19 +00:00
|
|
|
ASSERT_TRUE(mFGSurfaceControl != NULL);
|
|
|
|
ASSERT_TRUE(mFGSurfaceControl->isValid());
|
|
|
|
|
|
|
|
fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
|
|
|
|
|
|
|
|
// Synchronization surface
|
|
|
|
mSyncSurfaceControl = mComposerClient->createSurface(
|
2012-08-25 03:00:51 +00:00
|
|
|
String8("Sync Test Surface"), 1, 1, PIXEL_FORMAT_RGBA_8888, 0);
|
2011-10-12 02:22:19 +00:00
|
|
|
ASSERT_TRUE(mSyncSurfaceControl != NULL);
|
|
|
|
ASSERT_TRUE(mSyncSurfaceControl->isValid());
|
|
|
|
|
|
|
|
fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
|
|
|
|
|
|
|
|
SurfaceComposerClient::openGlobalTransaction();
|
|
|
|
|
|
|
|
ASSERT_EQ(NO_ERROR, mBGSurfaceControl->setLayer(INT_MAX-2));
|
|
|
|
ASSERT_EQ(NO_ERROR, mBGSurfaceControl->show());
|
|
|
|
|
|
|
|
ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setLayer(INT_MAX-1));
|
|
|
|
ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(64, 64));
|
|
|
|
ASSERT_EQ(NO_ERROR, mFGSurfaceControl->show());
|
|
|
|
|
|
|
|
ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setLayer(INT_MAX-1));
|
|
|
|
ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->setPosition(displayWidth-2,
|
|
|
|
displayHeight-2));
|
|
|
|
ASSERT_EQ(NO_ERROR, mSyncSurfaceControl->show());
|
|
|
|
|
|
|
|
SurfaceComposerClient::closeGlobalTransaction(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void TearDown() {
|
|
|
|
mComposerClient->dispose();
|
|
|
|
mBGSurfaceControl = 0;
|
|
|
|
mFGSurfaceControl = 0;
|
|
|
|
mSyncSurfaceControl = 0;
|
|
|
|
mComposerClient = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void waitForPostedBuffers() {
|
|
|
|
// Since the sync surface is in synchronous mode (i.e. double buffered)
|
|
|
|
// posting three buffers to it should ensure that at least two
|
|
|
|
// SurfaceFlinger::handlePageFlip calls have been made, which should
|
|
|
|
// guaranteed that a buffer posted to another Surface has been retired.
|
|
|
|
fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
|
|
|
|
fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
|
|
|
|
fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<SurfaceComposerClient> mComposerClient;
|
|
|
|
sp<SurfaceControl> mBGSurfaceControl;
|
|
|
|
sp<SurfaceControl> mFGSurfaceControl;
|
|
|
|
|
|
|
|
// This surface is used to ensure that the buffers posted to
|
|
|
|
// mFGSurfaceControl have been picked up by SurfaceFlinger.
|
|
|
|
sp<SurfaceControl> mSyncSurfaceControl;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(LayerUpdateTest, LayerMoveWorks) {
|
|
|
|
sp<ScreenCapture> sc;
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("before move");
|
|
|
|
ScreenCapture::captureScreen(&sc);
|
|
|
|
sc->checkPixel( 0, 12, 63, 63, 195);
|
|
|
|
sc->checkPixel( 75, 75, 195, 63, 63);
|
|
|
|
sc->checkPixel(145, 145, 63, 63, 195);
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceComposerClient::openGlobalTransaction();
|
|
|
|
ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(128, 128));
|
|
|
|
SurfaceComposerClient::closeGlobalTransaction(true);
|
|
|
|
{
|
|
|
|
// This should reflect the new position, but not the new color.
|
|
|
|
SCOPED_TRACE("after move, before redraw");
|
|
|
|
ScreenCapture::captureScreen(&sc);
|
|
|
|
sc->checkPixel( 24, 24, 63, 63, 195);
|
|
|
|
sc->checkPixel( 75, 75, 63, 63, 195);
|
|
|
|
sc->checkPixel(145, 145, 195, 63, 63);
|
|
|
|
}
|
|
|
|
|
|
|
|
fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
|
|
|
|
waitForPostedBuffers();
|
|
|
|
{
|
|
|
|
// This should reflect the new position and the new color.
|
|
|
|
SCOPED_TRACE("after redraw");
|
|
|
|
ScreenCapture::captureScreen(&sc);
|
|
|
|
sc->checkPixel( 24, 24, 63, 63, 195);
|
|
|
|
sc->checkPixel( 75, 75, 63, 63, 195);
|
|
|
|
sc->checkPixel(145, 145, 63, 195, 63);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LayerUpdateTest, LayerResizeWorks) {
|
|
|
|
sp<ScreenCapture> sc;
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("before resize");
|
|
|
|
ScreenCapture::captureScreen(&sc);
|
|
|
|
sc->checkPixel( 0, 12, 63, 63, 195);
|
|
|
|
sc->checkPixel( 75, 75, 195, 63, 63);
|
|
|
|
sc->checkPixel(145, 145, 63, 63, 195);
|
|
|
|
}
|
|
|
|
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("resizing");
|
2011-10-12 02:22:19 +00:00
|
|
|
SurfaceComposerClient::openGlobalTransaction();
|
|
|
|
ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setSize(128, 128));
|
|
|
|
SurfaceComposerClient::closeGlobalTransaction(true);
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("resized");
|
2011-10-12 02:22:19 +00:00
|
|
|
{
|
|
|
|
// This should not reflect the new size or color because SurfaceFlinger
|
|
|
|
// has not yet received a buffer of the correct size.
|
|
|
|
SCOPED_TRACE("after resize, before redraw");
|
|
|
|
ScreenCapture::captureScreen(&sc);
|
|
|
|
sc->checkPixel( 0, 12, 63, 63, 195);
|
|
|
|
sc->checkPixel( 75, 75, 195, 63, 63);
|
|
|
|
sc->checkPixel(145, 145, 63, 63, 195);
|
|
|
|
}
|
|
|
|
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("drawing");
|
2011-10-12 02:22:19 +00:00
|
|
|
fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
|
|
|
|
waitForPostedBuffers();
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("drawn");
|
2011-10-12 02:22:19 +00:00
|
|
|
{
|
|
|
|
// This should reflect the new size and the new color.
|
|
|
|
SCOPED_TRACE("after redraw");
|
|
|
|
ScreenCapture::captureScreen(&sc);
|
|
|
|
sc->checkPixel( 24, 24, 63, 63, 195);
|
|
|
|
sc->checkPixel( 75, 75, 63, 195, 63);
|
|
|
|
sc->checkPixel(145, 145, 63, 195, 63);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 07:11:27 +00:00
|
|
|
// Ensure that if we move and resize a surface in the same
|
|
|
|
// transaction, we don't reposition the surface and draw
|
|
|
|
// using the incorrect buffer size
|
|
|
|
TEST_F(LayerUpdateTest, LayerMoveAndResizeWorks) {
|
|
|
|
sp<ScreenCapture> sc;
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("before resize and reposition");
|
|
|
|
ScreenCapture::captureScreen(&sc);
|
|
|
|
sc->checkPixel( 0, 12, 63, 63, 195);
|
|
|
|
sc->checkPixel( 75, 75, 195, 63, 63);
|
|
|
|
sc->checkPixel(145, 145, 63, 63, 195);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALOGD("resizing and repositioning");
|
|
|
|
SurfaceComposerClient::openGlobalTransaction();
|
|
|
|
ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setPosition(64, 0));
|
|
|
|
ASSERT_EQ(NO_ERROR, mFGSurfaceControl->setSize(64, 128));
|
|
|
|
SurfaceComposerClient::closeGlobalTransaction(true);
|
|
|
|
|
|
|
|
ALOGD("resized and repositioned");
|
|
|
|
{
|
|
|
|
// This should not reflect the new size, position or color because SurfaceFlinger
|
|
|
|
// has not yet received a buffer of the correct size.
|
|
|
|
SCOPED_TRACE("after resize, before redraw");
|
|
|
|
ScreenCapture::captureScreen(&sc);
|
|
|
|
sc->checkPixel( 0, 12, 63, 63, 195);
|
|
|
|
sc->checkPixel( 75, 75, 195, 63, 63);
|
|
|
|
sc->checkPixel(145, 145, 63, 63, 195);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALOGD("drawing");
|
|
|
|
fillSurfaceRGBA8(mFGSurfaceControl, 63, 195, 63);
|
|
|
|
waitForPostedBuffers();
|
|
|
|
ALOGD("drawn");
|
|
|
|
{
|
|
|
|
// This should reflect the new size, position and the new color.
|
|
|
|
SCOPED_TRACE("after redraw");
|
|
|
|
ScreenCapture::captureScreen(&sc);
|
|
|
|
sc->checkPixel( 64, 0, 63, 195, 63);
|
|
|
|
// This should pass to imply that we didn't have a frame where the
|
|
|
|
// surface was moved but not yet resized even though the operations
|
|
|
|
// were part of the same transaction
|
|
|
|
sc->checkPixel( 64, 75, 63, 195, 63);
|
|
|
|
sc->checkPixel(145, 145, 63, 63, 195);
|
|
|
|
}
|
|
|
|
}
|
2011-10-12 02:22:19 +00:00
|
|
|
}
|