gfx/tests/gtest/TestTextures.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 2 /* Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 4 */
michael@0 5
michael@0 6 #include "gtest/gtest.h"
michael@0 7 #include "gmock/gmock.h"
michael@0 8
michael@0 9 #include "mozilla/gfx/2D.h"
michael@0 10 #include "mozilla/gfx/Tools.h"
michael@0 11 #include "mozilla/layers/TextureClient.h"
michael@0 12 #include "mozilla/layers/TextureHost.h"
michael@0 13 #include "mozilla/RefPtr.h"
michael@0 14 #include "gfx2DGlue.h"
michael@0 15 #include "gfxImageSurface.h"
michael@0 16 #include "gfxTypes.h"
michael@0 17 #include "ImageContainer.h"
michael@0 18 #include "mozilla/layers/YCbCrImageDataSerializer.h"
michael@0 19
michael@0 20 using namespace mozilla;
michael@0 21 using namespace mozilla::gfx;
michael@0 22 using namespace mozilla::layers;
michael@0 23
michael@0 24 /*
michael@0 25 * This test performs the following actions:
michael@0 26 * - creates a surface
michael@0 27 * - initialize a texture client with it
michael@0 28 * - serilaizes the texture client
michael@0 29 * - deserializes the data into a texture host
michael@0 30 * - reads the surface from the texture host.
michael@0 31 *
michael@0 32 * The surface in the end should be equal to the inital one.
michael@0 33 * This test is run for different combinations of texture types and
michael@0 34 * image formats.
michael@0 35 */
michael@0 36
michael@0 37 namespace mozilla {
michael@0 38 namespace layers {
michael@0 39
michael@0 40 // fills the surface with values betwee 0 and 100.
michael@0 41 void SetupSurface(gfxImageSurface* surface) {
michael@0 42 int bpp = gfxASurface::BytePerPixelFromFormat(surface->Format());
michael@0 43 int stride = surface->Stride();
michael@0 44 uint8_t val = 0;
michael@0 45 uint8_t* data = surface->Data();
michael@0 46 for (int y = 0; y < surface->Height(); ++y) {
michael@0 47 for (int x = 0; x < surface->Height(); ++x) {
michael@0 48 for (int b = 0; b < bpp; ++b) {
michael@0 49 data[y*stride + x*bpp + b] = val;
michael@0 50 if (val == 100) {
michael@0 51 val = 0;
michael@0 52 } else {
michael@0 53 ++val;
michael@0 54 }
michael@0 55 }
michael@0 56 }
michael@0 57 }
michael@0 58 }
michael@0 59
michael@0 60 // return true if two surfaces contain the same data
michael@0 61 void AssertSurfacesEqual(gfxImageSurface* surface1,
michael@0 62 gfxImageSurface* surface2)
michael@0 63 {
michael@0 64 ASSERT_EQ(surface1->GetSize(), surface2->GetSize());
michael@0 65 ASSERT_EQ(surface1->Format(), surface2->Format());
michael@0 66
michael@0 67 uint8_t* data1 = surface1->Data();
michael@0 68 uint8_t* data2 = surface2->Data();
michael@0 69 int stride1 = surface1->Stride();
michael@0 70 int stride2 = surface2->Stride();
michael@0 71 int bpp = gfxASurface::BytePerPixelFromFormat(surface1->Format());
michael@0 72
michael@0 73 for (int y = 0; y < surface1->Height(); ++y) {
michael@0 74 for (int x = 0; x < surface1->Width(); ++x) {
michael@0 75 for (int b = 0; b < bpp; ++b) {
michael@0 76 ASSERT_EQ(data1[y*stride1 + x*bpp + b],
michael@0 77 data2[y*stride2 + x*bpp + b]);
michael@0 78 }
michael@0 79 }
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83 void AssertSurfacesEqual(SourceSurface* surface1,
michael@0 84 SourceSurface* surface2)
michael@0 85 {
michael@0 86 ASSERT_EQ(surface1->GetSize(), surface2->GetSize());
michael@0 87 ASSERT_EQ(surface1->GetFormat(), surface2->GetFormat());
michael@0 88
michael@0 89 RefPtr<DataSourceSurface> dataSurface1 = surface1->GetDataSurface();
michael@0 90 RefPtr<DataSourceSurface> dataSurface2 = surface2->GetDataSurface();
michael@0 91 DataSourceSurface::MappedSurface map1;
michael@0 92 DataSourceSurface::MappedSurface map2;
michael@0 93 if (!dataSurface1->Map(DataSourceSurface::READ, &map1)) {
michael@0 94 return;
michael@0 95 }
michael@0 96 if (!dataSurface2->Map(DataSourceSurface::READ, &map2)) {
michael@0 97 dataSurface1->Unmap();
michael@0 98 return;
michael@0 99 }
michael@0 100 uint8_t* data1 = map1.mData;
michael@0 101 uint8_t* data2 = map2.mData;
michael@0 102 int stride1 = map1.mStride;
michael@0 103 int stride2 = map2.mStride;
michael@0 104 int bpp = BytesPerPixel(surface1->GetFormat());
michael@0 105 int width = surface1->GetSize().width;
michael@0 106 int height = surface1->GetSize().height;
michael@0 107
michael@0 108 for (int y = 0; y < height; ++y) {
michael@0 109 for (int x = 0; x < width; ++x) {
michael@0 110 for (int b = 0; b < bpp; ++b) {
michael@0 111 ASSERT_EQ(data1[y*stride1 + x*bpp + b],
michael@0 112 data2[y*stride2 + x*bpp + b]);
michael@0 113 }
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 dataSurface1->Unmap();
michael@0 118 dataSurface2->Unmap();
michael@0 119 }
michael@0 120
michael@0 121 // Same as above, for YCbCr surfaces
michael@0 122 void AssertYCbCrSurfacesEqual(PlanarYCbCrData* surface1,
michael@0 123 PlanarYCbCrData* surface2)
michael@0 124 {
michael@0 125 ASSERT_EQ(surface1->mYSize, surface2->mYSize);
michael@0 126 ASSERT_EQ(surface1->mCbCrSize, surface2->mCbCrSize);
michael@0 127 ASSERT_EQ(surface1->mStereoMode, surface2->mStereoMode);
michael@0 128 ASSERT_EQ(surface1->mPicSize, surface2->mPicSize);
michael@0 129
michael@0 130 for (int y = 0; y < surface1->mYSize.height; ++y) {
michael@0 131 for (int x = 0; x < surface1->mYSize.width; ++x) {
michael@0 132 ASSERT_EQ(surface1->mYChannel[y*surface1->mYStride + x*(1+surface1->mYSkip)],
michael@0 133 surface2->mYChannel[y*surface2->mYStride + x*(1+surface2->mYSkip)]);
michael@0 134 }
michael@0 135 }
michael@0 136 for (int y = 0; y < surface1->mCbCrSize.height; ++y) {
michael@0 137 for (int x = 0; x < surface1->mCbCrSize.width; ++x) {
michael@0 138 ASSERT_EQ(surface1->mCbChannel[y*surface1->mCbCrStride + x*(1+surface1->mCbSkip)],
michael@0 139 surface2->mCbChannel[y*surface2->mCbCrStride + x*(1+surface2->mCbSkip)]);
michael@0 140 ASSERT_EQ(surface1->mCrChannel[y*surface1->mCbCrStride + x*(1+surface1->mCrSkip)],
michael@0 141 surface2->mCrChannel[y*surface2->mCbCrStride + x*(1+surface2->mCrSkip)]);
michael@0 142 }
michael@0 143 }
michael@0 144 }
michael@0 145
michael@0 146 // Run the test for a texture client and a surface
michael@0 147 void TestTextureClientSurface(TextureClient* texture, gfxImageSurface* surface) {
michael@0 148
michael@0 149 // client allocation
michael@0 150 ASSERT_TRUE(texture->CanExposeDrawTarget());
michael@0 151 texture->AllocateForSurface(ToIntSize(surface->GetSize()));
michael@0 152 ASSERT_TRUE(texture->IsAllocated());
michael@0 153
michael@0 154 ASSERT_TRUE(texture->Lock(OPEN_READ_WRITE));
michael@0 155 // client painting
michael@0 156 RefPtr<DrawTarget> dt = texture->GetAsDrawTarget();
michael@0 157 RefPtr<SourceSurface> source =
michael@0 158 gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(dt, surface);
michael@0 159 dt->CopySurface(source, IntRect(IntPoint(), source->GetSize()), IntPoint());
michael@0 160
michael@0 161 RefPtr<SourceSurface> snapshot = dt->Snapshot();
michael@0 162
michael@0 163 AssertSurfacesEqual(snapshot, source);
michael@0 164
michael@0 165 dt = nullptr; // drop reference before calling Unlock()
michael@0 166 texture->Unlock();
michael@0 167
michael@0 168 // client serialization
michael@0 169 SurfaceDescriptor descriptor;
michael@0 170 ASSERT_TRUE(texture->ToSurfaceDescriptor(descriptor));
michael@0 171
michael@0 172 ASSERT_NE(descriptor.type(), SurfaceDescriptor::Tnull_t);
michael@0 173
michael@0 174 // host deserialization
michael@0 175 RefPtr<TextureHost> host = CreateBackendIndependentTextureHost(descriptor, nullptr,
michael@0 176 texture->GetFlags());
michael@0 177
michael@0 178 ASSERT_TRUE(host.get() != nullptr);
michael@0 179 ASSERT_EQ(host->GetFlags(), texture->GetFlags());
michael@0 180
michael@0 181 // host read
michael@0 182 ASSERT_TRUE(host->Lock());
michael@0 183 RefPtr<mozilla::gfx::DataSourceSurface> hostDataSurface = host->GetAsSurface();
michael@0 184 host->Unlock();
michael@0 185
michael@0 186 nsRefPtr<gfxImageSurface> hostSurface =
michael@0 187 new gfxImageSurface(hostDataSurface->GetData(),
michael@0 188 ThebesIntSize(hostDataSurface->GetSize()),
michael@0 189 hostDataSurface->Stride(),
michael@0 190 SurfaceFormatToImageFormat(hostDataSurface->GetFormat()));
michael@0 191 AssertSurfacesEqual(surface, hostSurface.get());
michael@0 192 }
michael@0 193
michael@0 194 // Same as above, for YCbCr surfaces
michael@0 195 void TestTextureClientYCbCr(TextureClient* client, PlanarYCbCrData& ycbcrData) {
michael@0 196
michael@0 197 // client allocation
michael@0 198 ASSERT_TRUE(client->AsTextureClientYCbCr() != nullptr);
michael@0 199 TextureClientYCbCr* texture = client->AsTextureClientYCbCr();
michael@0 200 texture->AllocateForYCbCr(ycbcrData.mYSize,
michael@0 201 ycbcrData.mCbCrSize,
michael@0 202 ycbcrData.mStereoMode);
michael@0 203 ASSERT_TRUE(client->IsAllocated());
michael@0 204
michael@0 205 ASSERT_TRUE(client->Lock(OPEN_READ_WRITE));
michael@0 206 // client painting
michael@0 207 texture->UpdateYCbCr(ycbcrData);
michael@0 208 client->Unlock();
michael@0 209
michael@0 210 // client serialization
michael@0 211 SurfaceDescriptor descriptor;
michael@0 212 ASSERT_TRUE(client->ToSurfaceDescriptor(descriptor));
michael@0 213
michael@0 214 ASSERT_NE(descriptor.type(), SurfaceDescriptor::Tnull_t);
michael@0 215
michael@0 216 // host deserialization
michael@0 217 RefPtr<TextureHost> textureHost = CreateBackendIndependentTextureHost(descriptor, nullptr,
michael@0 218 client->GetFlags());
michael@0 219
michael@0 220 RefPtr<BufferTextureHost> host = static_cast<BufferTextureHost*>(textureHost.get());
michael@0 221
michael@0 222 ASSERT_TRUE(host.get() != nullptr);
michael@0 223 ASSERT_EQ(host->GetFlags(), client->GetFlags());
michael@0 224
michael@0 225 // host read
michael@0 226 ASSERT_TRUE(host->Lock());
michael@0 227
michael@0 228 // This will work iff the compositor is not BasicCompositor
michael@0 229 ASSERT_EQ(host->GetFormat(), mozilla::gfx::SurfaceFormat::YUV);
michael@0 230
michael@0 231 YCbCrImageDataDeserializer yuvDeserializer(host->GetBuffer(), host->GetBufferSize());
michael@0 232 ASSERT_TRUE(yuvDeserializer.IsValid());
michael@0 233 PlanarYCbCrData data;
michael@0 234 data.mYChannel = yuvDeserializer.GetYData();
michael@0 235 data.mCbChannel = yuvDeserializer.GetCbData();
michael@0 236 data.mCrChannel = yuvDeserializer.GetCrData();
michael@0 237 data.mYStride = yuvDeserializer.GetYStride();
michael@0 238 data.mCbCrStride = yuvDeserializer.GetCbCrStride();
michael@0 239 data.mStereoMode = yuvDeserializer.GetStereoMode();
michael@0 240 data.mYSize = yuvDeserializer.GetYSize();
michael@0 241 data.mCbCrSize = yuvDeserializer.GetCbCrSize();
michael@0 242 data.mYSkip = 0;
michael@0 243 data.mCbSkip = 0;
michael@0 244 data.mCrSkip = 0;
michael@0 245 data.mPicSize = data.mYSize;
michael@0 246 data.mPicX = 0;
michael@0 247 data.mPicY = 0;
michael@0 248
michael@0 249 AssertYCbCrSurfacesEqual(&ycbcrData, &data);
michael@0 250 host->Unlock();
michael@0 251 }
michael@0 252
michael@0 253 } // namespace
michael@0 254 } // namespace
michael@0 255
michael@0 256 TEST(Layers, TextureSerialization) {
michael@0 257 // the test is run on all the following image formats
michael@0 258 gfxImageFormat formats[3] = {
michael@0 259 gfxImageFormat::ARGB32,
michael@0 260 gfxImageFormat::RGB24,
michael@0 261 gfxImageFormat::A8,
michael@0 262 };
michael@0 263
michael@0 264 for (int f = 0; f < 3; ++f) {
michael@0 265 RefPtr<gfxImageSurface> surface = new gfxImageSurface(gfxIntSize(400,300), formats[f]);
michael@0 266 SetupSurface(surface.get());
michael@0 267 AssertSurfacesEqual(surface, surface);
michael@0 268
michael@0 269 RefPtr<TextureClient> client
michael@0 270 = new MemoryTextureClient(nullptr,
michael@0 271 mozilla::gfx::ImageFormatToSurfaceFormat(surface->Format()),
michael@0 272 gfx::BackendType::CAIRO,
michael@0 273 TEXTURE_DEALLOCATE_CLIENT);
michael@0 274
michael@0 275 TestTextureClientSurface(client, surface);
michael@0 276
michael@0 277 // XXX - Test more texture client types.
michael@0 278 }
michael@0 279 }
michael@0 280
michael@0 281 TEST(Layers, TextureYCbCrSerialization) {
michael@0 282 RefPtr<gfxImageSurface> ySurface = new gfxImageSurface(gfxIntSize(400,300), gfxImageFormat::A8);
michael@0 283 RefPtr<gfxImageSurface> cbSurface = new gfxImageSurface(gfxIntSize(200,150), gfxImageFormat::A8);
michael@0 284 RefPtr<gfxImageSurface> crSurface = new gfxImageSurface(gfxIntSize(200,150), gfxImageFormat::A8);
michael@0 285 SetupSurface(ySurface.get());
michael@0 286 SetupSurface(cbSurface.get());
michael@0 287 SetupSurface(crSurface.get());
michael@0 288
michael@0 289 PlanarYCbCrData clientData;
michael@0 290 clientData.mYChannel = ySurface->Data();
michael@0 291 clientData.mCbChannel = cbSurface->Data();
michael@0 292 clientData.mCrChannel = crSurface->Data();
michael@0 293 clientData.mYSize = ySurface->GetSize().ToIntSize();
michael@0 294 clientData.mPicSize = ySurface->GetSize().ToIntSize();
michael@0 295 clientData.mCbCrSize = cbSurface->GetSize().ToIntSize();
michael@0 296 clientData.mYStride = ySurface->Stride();
michael@0 297 clientData.mCbCrStride = cbSurface->Stride();
michael@0 298 clientData.mStereoMode = StereoMode::MONO;
michael@0 299 clientData.mYSkip = 0;
michael@0 300 clientData.mCbSkip = 0;
michael@0 301 clientData.mCrSkip = 0;
michael@0 302 clientData.mCrSkip = 0;
michael@0 303 clientData.mPicX = 0;
michael@0 304 clientData.mPicX = 0;
michael@0 305
michael@0 306 RefPtr<TextureClient> client
michael@0 307 = new MemoryTextureClient(nullptr,
michael@0 308 mozilla::gfx::SurfaceFormat::YUV,
michael@0 309 gfx::BackendType::CAIRO,
michael@0 310 TEXTURE_DEALLOCATE_CLIENT);
michael@0 311
michael@0 312 TestTextureClientYCbCr(client, clientData);
michael@0 313
michael@0 314 // XXX - Test more texture client types.
michael@0 315 }

mercurial