1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/* 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"
#include "TestLayers.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Tools.h"
#include "mozilla/layers/BufferTexture.h"
#include "mozilla/layers/ImageBridgeChild.h" // for ImageBridgeChild
#include "mozilla/layers/TextureClient.h"
#include "mozilla/layers/TextureHost.h"
#include "mozilla/RefPtr.h"
#include "gfx2DGlue.h"
#include "gfxImageSurface.h"
#include "gfxTypes.h"
#include "ImageContainer.h"
#include "mozilla/layers/ImageDataSerializer.h"
using namespace mozilla;
using namespace mozilla::gfx;
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.
*/
namespace mozilla {
namespace layers {
// 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]);
}
}
}
}
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();
}
// Run the test for a texture client and a surface
void TestTextureClientSurface(TextureClient* texture, gfxImageSurface* surface) {
// client allocation
ASSERT_TRUE(texture->CanExposeDrawTarget());
ASSERT_TRUE(texture->Lock(OpenMode::OPEN_READ_WRITE));
// client painting
RefPtr<DrawTarget> dt = texture->BorrowDrawTarget();
RefPtr<SourceSurface> source =
gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(dt, surface);
dt->CopySurface(source, IntRect(IntPoint(), source->GetSize()), IntPoint());
RefPtr<SourceSurface> snapshot = dt->Snapshot();
AssertSurfacesEqual(snapshot, source);
dt = nullptr; // drop reference before calling Unlock()
texture->Unlock();
// client serialization
SurfaceDescriptor descriptor;
ASSERT_TRUE(texture->ToSurfaceDescriptor(descriptor));
ASSERT_NE(descriptor.type(), SurfaceDescriptor::Tnull_t);
// host deserialization
RefPtr<TestSurfaceAllocator> deallocator = new TestSurfaceAllocator();
RefPtr<TextureHost> host = CreateBackendIndependentTextureHost(descriptor, deallocator,
texture->GetFlags());
ASSERT_TRUE(host.get() != nullptr);
ASSERT_EQ(host->GetFlags(), texture->GetFlags());
// host read
// XXX - this can fail because lock tries to upload the texture but it needs a
// Compositor to do that. We could add a DummyComposior for testing but I am
// not sure it'll be worth it. Maybe always test against a BasicCompositor,
// but the latter needs a widget...
if (host->Lock()) {
RefPtr<mozilla::gfx::DataSourceSurface> hostDataSurface = host->GetAsSurface();
RefPtr<gfxImageSurface> hostSurface =
new gfxImageSurface(hostDataSurface->GetData(),
hostDataSurface->GetSize(),
hostDataSurface->Stride(),
SurfaceFormatToImageFormat(hostDataSurface->GetFormat()));
AssertSurfacesEqual(surface, hostSurface.get());
host->Unlock();
}
}
// Same as above, for YCbCr surfaces
void TestTextureClientYCbCr(TextureClient* client, PlanarYCbCrData& ycbcrData) {
client->Lock(OpenMode::OPEN_READ_WRITE);
UpdateYCbCrTextureClient(client, ycbcrData);
client->Unlock();
// client serialization
SurfaceDescriptor descriptor;
ASSERT_TRUE(client->ToSurfaceDescriptor(descriptor));
ASSERT_EQ(descriptor.type(), SurfaceDescriptor::TSurfaceDescriptorBuffer);
auto bufferDesc = descriptor.get_SurfaceDescriptorBuffer();
ASSERT_EQ(bufferDesc.desc().type(), BufferDescriptor::TYCbCrDescriptor);
auto ycbcrDesc = bufferDesc.desc().get_YCbCrDescriptor();
ASSERT_EQ(ycbcrDesc.ySize(), ycbcrData.mYSize);
ASSERT_EQ(ycbcrDesc.cbCrSize(), ycbcrData.mCbCrSize);
ASSERT_EQ(ycbcrDesc.stereoMode(), ycbcrData.mStereoMode);
// host deserialization
RefPtr<TestSurfaceAllocator> deallocator = new TestSurfaceAllocator();
RefPtr<TextureHost> textureHost = CreateBackendIndependentTextureHost(descriptor, deallocator,
client->GetFlags());
RefPtr<BufferTextureHost> host = static_cast<BufferTextureHost*>(textureHost.get());
ASSERT_TRUE(host.get() != nullptr);
ASSERT_EQ(host->GetFlags(), client->GetFlags());
// host read
if (host->Lock()) {
// This will work iff the compositor is not BasicCompositor
ASSERT_EQ(host->GetFormat(), mozilla::gfx::SurfaceFormat::YUV);
host->Unlock();
}
}
} // namespace layers
} // namespace mozilla
TEST(Layers, TextureSerialization) {
// the test is run on all the following image formats
gfxImageFormat formats[3] = {
SurfaceFormat::A8R8G8B8_UINT32,
SurfaceFormat::X8R8G8B8_UINT32,
SurfaceFormat::A8,
};
for (int f = 0; f < 3; ++f) {
RefPtr<gfxImageSurface> surface = new gfxImageSurface(IntSize(400,300), formats[f]);
SetupSurface(surface.get());
AssertSurfacesEqual(surface, surface);
auto texData = BufferTextureData::Create(surface->GetSize(),
gfx::ImageFormatToSurfaceFormat(surface->Format()),
gfx::BackendType::CAIRO, LayersBackend::LAYERS_NONE,
TextureFlags::DEALLOCATE_CLIENT, ALLOC_DEFAULT, nullptr
);
ASSERT_TRUE(!!texData);
RefPtr<TextureClient> client = new TextureClient(
texData, TextureFlags::DEALLOCATE_CLIENT, nullptr
);
TestTextureClientSurface(client, surface);
// XXX - Test more texture client types.
}
}
TEST(Layers, TextureYCbCrSerialization) {
RefPtr<gfxImageSurface> ySurface = new gfxImageSurface(IntSize(400,300), SurfaceFormat::A8);
RefPtr<gfxImageSurface> cbSurface = new gfxImageSurface(IntSize(200,150), SurfaceFormat::A8);
RefPtr<gfxImageSurface> crSurface = new gfxImageSurface(IntSize(200,150), SurfaceFormat::A8);
SetupSurface(ySurface.get());
SetupSurface(cbSurface.get());
SetupSurface(crSurface.get());
PlanarYCbCrData clientData;
clientData.mYChannel = ySurface->Data();
clientData.mCbChannel = cbSurface->Data();
clientData.mCrChannel = crSurface->Data();
clientData.mYSize = ySurface->GetSize();
clientData.mPicSize = ySurface->GetSize();
clientData.mCbCrSize = cbSurface->GetSize();
clientData.mYStride = ySurface->Stride();
clientData.mCbCrStride = cbSurface->Stride();
clientData.mStereoMode = StereoMode::MONO;
clientData.mYUVColorSpace = YUVColorSpace::BT601;
clientData.mYSkip = 0;
clientData.mCbSkip = 0;
clientData.mCrSkip = 0;
clientData.mCrSkip = 0;
clientData.mPicX = 0;
clientData.mPicX = 0;
ImageBridgeChild::InitSameProcess();
RefPtr<ImageBridgeChild> imageBridge = ImageBridgeChild::GetSingleton();
static int retry = 5;
while(!imageBridge->IPCOpen() && retry) {
// IPDL connection takes time especially in slow testing environment, like
// VM machines. Here we added retry mechanism to wait for IPDL connnection.
#ifdef XP_WIN
Sleep(1);
#else
sleep(1);
#endif
retry--;
}
// Skip this testing if IPDL connection is not ready
if (!retry && !imageBridge->IPCOpen()) {
return;
}
RefPtr<TextureClient> client = TextureClient::CreateForYCbCr(imageBridge, clientData.mYSize, clientData.mCbCrSize,
StereoMode::MONO, YUVColorSpace::BT601,
ColorRange::LIMITED,
TextureFlags::DEALLOCATE_CLIENT);
TestTextureClientYCbCr(client, clientData);
// XXX - Test more texture client types.
}
|