2013-09-25 15:47:14 +04:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et: */
|
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
* http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
|
2014-04-13 14:07:12 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "mozilla/gfx/Tools.h"
|
2013-09-25 15:47:14 +04:00
|
|
|
#include "mozilla/layers/TextureClient.h"
|
|
|
|
#include "mozilla/layers/TextureHost.h"
|
2014-04-13 14:07:12 +04:00
|
|
|
#include "mozilla/RefPtr.h"
|
2013-09-25 15:47:14 +04:00
|
|
|
#include "gfx2DGlue.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "gfxTypes.h"
|
2013-10-16 04:42:03 +04:00
|
|
|
#include "ImageContainer.h"
|
|
|
|
#include "mozilla/layers/YCbCrImageDataSerializer.h"
|
2013-09-25 15:47:14 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-04-13 14:07:12 +04:00
|
|
|
using namespace mozilla::gfx;
|
2013-09-25 15:47:14 +04:00
|
|
|
using namespace mozilla::layers;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This test performs the following actions:
|
|
|
|
* - creates a surface
|
|
|
|
* - initialize a texture client with it
|
|
|
|
* - serilaizes the texture client
|
|
|
|
* - deserializes the data into a texture host
|
|
|
|
* - reads the surface from the texture host.
|
|
|
|
*
|
|
|
|
* The surface in the end should be equal to the inital one.
|
|
|
|
* This test is run for different combinations of texture types and
|
|
|
|
* image formats.
|
|
|
|
*/
|
|
|
|
|
2014-04-10 11:24:59 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
2013-10-10 02:37:07 +04:00
|
|
|
|
|
|
|
// fills the surface with values betwee 0 and 100.
|
|
|
|
void SetupSurface(gfxImageSurface* surface) {
|
|
|
|
int bpp = gfxASurface::BytePerPixelFromFormat(surface->Format());
|
|
|
|
int stride = surface->Stride();
|
|
|
|
uint8_t val = 0;
|
|
|
|
uint8_t* data = surface->Data();
|
|
|
|
for (int y = 0; y < surface->Height(); ++y) {
|
|
|
|
for (int x = 0; x < surface->Height(); ++x) {
|
|
|
|
for (int b = 0; b < bpp; ++b) {
|
|
|
|
data[y*stride + x*bpp + b] = val;
|
|
|
|
if (val == 100) {
|
|
|
|
val = 0;
|
|
|
|
} else {
|
|
|
|
++val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if two surfaces contain the same data
|
|
|
|
void AssertSurfacesEqual(gfxImageSurface* surface1,
|
|
|
|
gfxImageSurface* surface2)
|
|
|
|
{
|
|
|
|
ASSERT_EQ(surface1->GetSize(), surface2->GetSize());
|
|
|
|
ASSERT_EQ(surface1->Format(), surface2->Format());
|
|
|
|
|
|
|
|
uint8_t* data1 = surface1->Data();
|
|
|
|
uint8_t* data2 = surface2->Data();
|
|
|
|
int stride1 = surface1->Stride();
|
|
|
|
int stride2 = surface2->Stride();
|
|
|
|
int bpp = gfxASurface::BytePerPixelFromFormat(surface1->Format());
|
|
|
|
|
|
|
|
for (int y = 0; y < surface1->Height(); ++y) {
|
|
|
|
for (int x = 0; x < surface1->Width(); ++x) {
|
|
|
|
for (int b = 0; b < bpp; ++b) {
|
|
|
|
ASSERT_EQ(data1[y*stride1 + x*bpp + b],
|
|
|
|
data2[y*stride2 + x*bpp + b]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-13 14:07:12 +04:00
|
|
|
void AssertSurfacesEqual(SourceSurface* surface1,
|
|
|
|
SourceSurface* surface2)
|
|
|
|
{
|
|
|
|
ASSERT_EQ(surface1->GetSize(), surface2->GetSize());
|
|
|
|
ASSERT_EQ(surface1->GetFormat(), surface2->GetFormat());
|
|
|
|
|
|
|
|
RefPtr<DataSourceSurface> dataSurface1 = surface1->GetDataSurface();
|
|
|
|
RefPtr<DataSourceSurface> dataSurface2 = surface2->GetDataSurface();
|
|
|
|
DataSourceSurface::MappedSurface map1;
|
|
|
|
DataSourceSurface::MappedSurface map2;
|
|
|
|
if (!dataSurface1->Map(DataSourceSurface::READ, &map1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!dataSurface2->Map(DataSourceSurface::READ, &map2)) {
|
|
|
|
dataSurface1->Unmap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint8_t* data1 = map1.mData;
|
|
|
|
uint8_t* data2 = map2.mData;
|
|
|
|
int stride1 = map1.mStride;
|
|
|
|
int stride2 = map2.mStride;
|
|
|
|
int bpp = BytesPerPixel(surface1->GetFormat());
|
|
|
|
int width = surface1->GetSize().width;
|
|
|
|
int height = surface1->GetSize().height;
|
|
|
|
|
|
|
|
for (int y = 0; y < height; ++y) {
|
|
|
|
for (int x = 0; x < width; ++x) {
|
|
|
|
for (int b = 0; b < bpp; ++b) {
|
|
|
|
ASSERT_EQ(data1[y*stride1 + x*bpp + b],
|
|
|
|
data2[y*stride2 + x*bpp + b]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dataSurface1->Unmap();
|
|
|
|
dataSurface2->Unmap();
|
|
|
|
}
|
|
|
|
|
2013-10-16 04:42:03 +04:00
|
|
|
// Same as above, for YCbCr surfaces
|
|
|
|
void AssertYCbCrSurfacesEqual(PlanarYCbCrData* surface1,
|
|
|
|
PlanarYCbCrData* surface2)
|
|
|
|
{
|
|
|
|
ASSERT_EQ(surface1->mYSize, surface2->mYSize);
|
|
|
|
ASSERT_EQ(surface1->mCbCrSize, surface2->mCbCrSize);
|
|
|
|
ASSERT_EQ(surface1->mStereoMode, surface2->mStereoMode);
|
|
|
|
ASSERT_EQ(surface1->mPicSize, surface2->mPicSize);
|
|
|
|
|
|
|
|
for (int y = 0; y < surface1->mYSize.height; ++y) {
|
|
|
|
for (int x = 0; x < surface1->mYSize.width; ++x) {
|
|
|
|
ASSERT_EQ(surface1->mYChannel[y*surface1->mYStride + x*(1+surface1->mYSkip)],
|
|
|
|
surface2->mYChannel[y*surface2->mYStride + x*(1+surface2->mYSkip)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int y = 0; y < surface1->mCbCrSize.height; ++y) {
|
|
|
|
for (int x = 0; x < surface1->mCbCrSize.width; ++x) {
|
|
|
|
ASSERT_EQ(surface1->mCbChannel[y*surface1->mCbCrStride + x*(1+surface1->mCbSkip)],
|
|
|
|
surface2->mCbChannel[y*surface2->mCbCrStride + x*(1+surface2->mCbSkip)]);
|
|
|
|
ASSERT_EQ(surface1->mCrChannel[y*surface1->mCbCrStride + x*(1+surface1->mCrSkip)],
|
|
|
|
surface2->mCrChannel[y*surface2->mCbCrStride + x*(1+surface2->mCrSkip)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 15:47:14 +04:00
|
|
|
// Run the test for a texture client and a surface
|
|
|
|
void TestTextureClientSurface(TextureClient* texture, gfxImageSurface* surface) {
|
|
|
|
|
|
|
|
// client allocation
|
2014-04-14 23:29:42 +04:00
|
|
|
ASSERT_TRUE(texture->CanExposeDrawTarget());
|
|
|
|
texture->AllocateForSurface(ToIntSize(surface->GetSize()));
|
2013-09-25 15:47:14 +04:00
|
|
|
ASSERT_TRUE(texture->IsAllocated());
|
|
|
|
|
2014-04-27 11:45:08 +04:00
|
|
|
ASSERT_TRUE(texture->Lock(OPEN_READ_WRITE));
|
2013-09-25 15:47:14 +04:00
|
|
|
// client painting
|
2014-04-13 14:07:12 +04:00
|
|
|
RefPtr<DrawTarget> dt = texture->GetAsDrawTarget();
|
|
|
|
RefPtr<SourceSurface> source =
|
|
|
|
gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(dt, surface);
|
|
|
|
dt->CopySurface(source, IntRect(IntPoint(), source->GetSize()), IntPoint());
|
|
|
|
|
|
|
|
RefPtr<SourceSurface> snapshot = dt->Snapshot();
|
2013-09-25 15:47:14 +04:00
|
|
|
|
2014-04-13 14:07:12 +04:00
|
|
|
AssertSurfacesEqual(snapshot, source);
|
2013-09-25 15:47:14 +04:00
|
|
|
|
2014-04-13 14:07:12 +04:00
|
|
|
dt = nullptr; // drop reference before calling Unlock()
|
2013-09-25 15:47:14 +04:00
|
|
|
texture->Unlock();
|
|
|
|
|
|
|
|
// client serialization
|
|
|
|
SurfaceDescriptor descriptor;
|
|
|
|
ASSERT_TRUE(texture->ToSurfaceDescriptor(descriptor));
|
|
|
|
|
|
|
|
ASSERT_NE(descriptor.type(), SurfaceDescriptor::Tnull_t);
|
|
|
|
|
|
|
|
// host deserialization
|
2013-12-12 05:44:47 +04:00
|
|
|
RefPtr<TextureHost> host = CreateBackendIndependentTextureHost(descriptor, nullptr,
|
2013-09-25 15:47:14 +04:00
|
|
|
texture->GetFlags());
|
|
|
|
|
|
|
|
ASSERT_TRUE(host.get() != nullptr);
|
|
|
|
ASSERT_EQ(host->GetFlags(), texture->GetFlags());
|
|
|
|
|
|
|
|
// host read
|
|
|
|
ASSERT_TRUE(host->Lock());
|
2013-11-27 13:59:01 +04:00
|
|
|
RefPtr<mozilla::gfx::DataSourceSurface> hostDataSurface = host->GetAsSurface();
|
2013-09-25 15:47:14 +04:00
|
|
|
host->Unlock();
|
|
|
|
|
2013-11-27 13:59:01 +04:00
|
|
|
nsRefPtr<gfxImageSurface> hostSurface =
|
|
|
|
new gfxImageSurface(hostDataSurface->GetData(),
|
|
|
|
ThebesIntSize(hostDataSurface->GetSize()),
|
|
|
|
hostDataSurface->Stride(),
|
|
|
|
SurfaceFormatToImageFormat(hostDataSurface->GetFormat()));
|
2013-09-25 15:47:14 +04:00
|
|
|
AssertSurfacesEqual(surface, hostSurface.get());
|
|
|
|
}
|
|
|
|
|
2013-10-16 04:42:03 +04:00
|
|
|
// Same as above, for YCbCr surfaces
|
|
|
|
void TestTextureClientYCbCr(TextureClient* client, PlanarYCbCrData& ycbcrData) {
|
|
|
|
|
|
|
|
// client allocation
|
|
|
|
ASSERT_TRUE(client->AsTextureClientYCbCr() != nullptr);
|
|
|
|
TextureClientYCbCr* texture = client->AsTextureClientYCbCr();
|
2013-12-20 20:46:29 +04:00
|
|
|
texture->AllocateForYCbCr(ycbcrData.mYSize,
|
|
|
|
ycbcrData.mCbCrSize,
|
2013-10-16 04:42:03 +04:00
|
|
|
ycbcrData.mStereoMode);
|
|
|
|
ASSERT_TRUE(client->IsAllocated());
|
|
|
|
|
2014-04-27 11:45:08 +04:00
|
|
|
ASSERT_TRUE(client->Lock(OPEN_READ_WRITE));
|
2013-10-16 04:42:03 +04:00
|
|
|
// client painting
|
|
|
|
texture->UpdateYCbCr(ycbcrData);
|
|
|
|
client->Unlock();
|
|
|
|
|
|
|
|
// client serialization
|
|
|
|
SurfaceDescriptor descriptor;
|
|
|
|
ASSERT_TRUE(client->ToSurfaceDescriptor(descriptor));
|
|
|
|
|
|
|
|
ASSERT_NE(descriptor.type(), SurfaceDescriptor::Tnull_t);
|
|
|
|
|
|
|
|
// host deserialization
|
2013-12-12 05:44:47 +04:00
|
|
|
RefPtr<TextureHost> textureHost = CreateBackendIndependentTextureHost(descriptor, nullptr,
|
2013-10-16 04:42:03 +04:00
|
|
|
client->GetFlags());
|
|
|
|
|
|
|
|
RefPtr<BufferTextureHost> host = static_cast<BufferTextureHost*>(textureHost.get());
|
|
|
|
|
|
|
|
ASSERT_TRUE(host.get() != nullptr);
|
|
|
|
ASSERT_EQ(host->GetFlags(), client->GetFlags());
|
|
|
|
|
|
|
|
// host read
|
|
|
|
ASSERT_TRUE(host->Lock());
|
|
|
|
|
2014-02-19 20:34:49 +04:00
|
|
|
// This will work iff the compositor is not BasicCompositor
|
|
|
|
ASSERT_EQ(host->GetFormat(), mozilla::gfx::SurfaceFormat::YUV);
|
2013-10-16 04:42:03 +04:00
|
|
|
|
2014-02-21 01:04:13 +04:00
|
|
|
YCbCrImageDataDeserializer yuvDeserializer(host->GetBuffer(), host->GetBufferSize());
|
2013-10-16 04:42:03 +04:00
|
|
|
ASSERT_TRUE(yuvDeserializer.IsValid());
|
|
|
|
PlanarYCbCrData data;
|
|
|
|
data.mYChannel = yuvDeserializer.GetYData();
|
|
|
|
data.mCbChannel = yuvDeserializer.GetCbData();
|
|
|
|
data.mCrChannel = yuvDeserializer.GetCrData();
|
|
|
|
data.mYStride = yuvDeserializer.GetYStride();
|
|
|
|
data.mCbCrStride = yuvDeserializer.GetCbCrStride();
|
|
|
|
data.mStereoMode = yuvDeserializer.GetStereoMode();
|
|
|
|
data.mYSize = yuvDeserializer.GetYSize();
|
|
|
|
data.mCbCrSize = yuvDeserializer.GetCbCrSize();
|
|
|
|
data.mYSkip = 0;
|
|
|
|
data.mCbSkip = 0;
|
|
|
|
data.mCrSkip = 0;
|
|
|
|
data.mPicSize = data.mYSize;
|
|
|
|
data.mPicX = 0;
|
|
|
|
data.mPicY = 0;
|
|
|
|
|
|
|
|
AssertYCbCrSurfacesEqual(&ycbcrData, &data);
|
|
|
|
host->Unlock();
|
|
|
|
}
|
|
|
|
|
2014-04-10 11:24:59 +04:00
|
|
|
} // namespace
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-25 15:47:14 +04:00
|
|
|
TEST(Layers, TextureSerialization) {
|
|
|
|
// the test is run on all the following image formats
|
|
|
|
gfxImageFormat formats[3] = {
|
2014-01-23 22:26:40 +04:00
|
|
|
gfxImageFormat::ARGB32,
|
|
|
|
gfxImageFormat::RGB24,
|
|
|
|
gfxImageFormat::A8,
|
2013-09-25 15:47:14 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
for (int f = 0; f < 3; ++f) {
|
|
|
|
RefPtr<gfxImageSurface> surface = new gfxImageSurface(gfxIntSize(400,300), formats[f]);
|
|
|
|
SetupSurface(surface.get());
|
|
|
|
AssertSurfacesEqual(surface, surface);
|
|
|
|
|
|
|
|
RefPtr<TextureClient> client
|
|
|
|
= new MemoryTextureClient(nullptr,
|
|
|
|
mozilla::gfx::ImageFormatToSurfaceFormat(surface->Format()),
|
2014-03-18 01:35:20 +04:00
|
|
|
gfx::BackendType::CAIRO,
|
2014-04-27 11:45:08 +04:00
|
|
|
TEXTURE_DEALLOCATE_CLIENT);
|
2013-09-25 15:47:14 +04:00
|
|
|
|
|
|
|
TestTextureClientSurface(client, surface);
|
|
|
|
|
|
|
|
// XXX - Test more texture client types.
|
|
|
|
}
|
|
|
|
}
|
2013-10-16 04:42:03 +04:00
|
|
|
|
|
|
|
TEST(Layers, TextureYCbCrSerialization) {
|
2014-01-23 22:26:40 +04:00
|
|
|
RefPtr<gfxImageSurface> ySurface = new gfxImageSurface(gfxIntSize(400,300), gfxImageFormat::A8);
|
|
|
|
RefPtr<gfxImageSurface> cbSurface = new gfxImageSurface(gfxIntSize(200,150), gfxImageFormat::A8);
|
|
|
|
RefPtr<gfxImageSurface> crSurface = new gfxImageSurface(gfxIntSize(200,150), gfxImageFormat::A8);
|
2013-10-16 04:42:03 +04:00
|
|
|
SetupSurface(ySurface.get());
|
|
|
|
SetupSurface(cbSurface.get());
|
|
|
|
SetupSurface(crSurface.get());
|
|
|
|
|
|
|
|
PlanarYCbCrData clientData;
|
|
|
|
clientData.mYChannel = ySurface->Data();
|
|
|
|
clientData.mCbChannel = cbSurface->Data();
|
|
|
|
clientData.mCrChannel = crSurface->Data();
|
2013-12-31 13:06:12 +04:00
|
|
|
clientData.mYSize = ySurface->GetSize().ToIntSize();
|
|
|
|
clientData.mPicSize = ySurface->GetSize().ToIntSize();
|
|
|
|
clientData.mCbCrSize = cbSurface->GetSize().ToIntSize();
|
2013-10-16 04:42:03 +04:00
|
|
|
clientData.mYStride = ySurface->Stride();
|
|
|
|
clientData.mCbCrStride = cbSurface->Stride();
|
2014-01-31 02:58:49 +04:00
|
|
|
clientData.mStereoMode = StereoMode::MONO;
|
2013-10-16 04:42:03 +04:00
|
|
|
clientData.mYSkip = 0;
|
|
|
|
clientData.mCbSkip = 0;
|
|
|
|
clientData.mCrSkip = 0;
|
|
|
|
clientData.mCrSkip = 0;
|
|
|
|
clientData.mPicX = 0;
|
|
|
|
clientData.mPicX = 0;
|
|
|
|
|
|
|
|
RefPtr<TextureClient> client
|
|
|
|
= new MemoryTextureClient(nullptr,
|
2014-01-10 23:06:16 +04:00
|
|
|
mozilla::gfx::SurfaceFormat::YUV,
|
2014-03-18 01:35:20 +04:00
|
|
|
gfx::BackendType::CAIRO,
|
2014-04-27 11:45:08 +04:00
|
|
|
TEXTURE_DEALLOCATE_CLIENT);
|
2013-10-16 04:42:03 +04:00
|
|
|
|
|
|
|
TestTextureClientYCbCr(client, clientData);
|
|
|
|
|
|
|
|
// XXX - Test more texture client types.
|
|
|
|
}
|