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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/gfx/2D.h"
#include "Common.h"
#include "Decoder.h"
#include "DecoderFactory.h"
#include "SourceBuffer.h"
#include "SurfacePipe.h"
using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::image;
namespace mozilla {
namespace image {
class TestSurfacePipeFactory
{
public:
static SurfacePipe SimpleSurfacePipe()
{
SurfacePipe pipe;
return Move(pipe);
}
template <typename T>
static SurfacePipe SurfacePipeFromPipeline(T&& aPipeline)
{
return SurfacePipe { Move(aPipeline) };
}
private:
TestSurfacePipeFactory() { }
};
} // namespace image
} // namespace mozilla
void
CheckSurfacePipeMethodResults(SurfacePipe* aPipe,
Decoder* aDecoder,
const IntRect& aRect = IntRect(0, 0, 100, 100))
{
// Check that the pipeline ended up in the state we expect. Note that we're
// explicitly testing the SurfacePipe versions of these methods, so we don't
// want to use AssertCorrectPipelineFinalState() here.
EXPECT_TRUE(aPipe->IsSurfaceFinished());
Maybe<SurfaceInvalidRect> invalidRect = aPipe->TakeInvalidRect();
EXPECT_TRUE(invalidRect.isSome());
EXPECT_EQ(IntRect(0, 0, 100, 100), invalidRect->mInputSpaceRect);
EXPECT_EQ(IntRect(0, 0, 100, 100), invalidRect->mOutputSpaceRect);
// Check the generated image.
CheckGeneratedImage(aDecoder, aRect);
// Reset and clear the image before the next test.
aPipe->ResetToFirstRow();
EXPECT_FALSE(aPipe->IsSurfaceFinished());
invalidRect = aPipe->TakeInvalidRect();
EXPECT_TRUE(invalidRect.isNothing());
uint32_t count = 0;
auto result = aPipe->WritePixels<uint32_t>([&]() {
++count;
return AsVariant(BGRAColor::Transparent().AsPixel());
});
EXPECT_EQ(WriteState::FINISHED, result);
EXPECT_EQ(100u * 100u, count);
EXPECT_TRUE(aPipe->IsSurfaceFinished());
invalidRect = aPipe->TakeInvalidRect();
EXPECT_TRUE(invalidRect.isSome());
EXPECT_EQ(IntRect(0, 0, 100, 100), invalidRect->mInputSpaceRect);
EXPECT_EQ(IntRect(0, 0, 100, 100), invalidRect->mOutputSpaceRect);
aPipe->ResetToFirstRow();
EXPECT_FALSE(aPipe->IsSurfaceFinished());
invalidRect = aPipe->TakeInvalidRect();
EXPECT_TRUE(invalidRect.isNothing());
}
void
CheckPalettedSurfacePipeMethodResults(SurfacePipe* aPipe,
Decoder* aDecoder,
const IntRect& aRect
= IntRect(0, 0, 100, 100))
{
// Check that the pipeline ended up in the state we expect. Note that we're
// explicitly testing the SurfacePipe versions of these methods, so we don't
// want to use AssertCorrectPipelineFinalState() here.
EXPECT_TRUE(aPipe->IsSurfaceFinished());
Maybe<SurfaceInvalidRect> invalidRect = aPipe->TakeInvalidRect();
EXPECT_TRUE(invalidRect.isSome());
EXPECT_EQ(IntRect(0, 0, 100, 100), invalidRect->mInputSpaceRect);
EXPECT_EQ(IntRect(0, 0, 100, 100), invalidRect->mOutputSpaceRect);
// Check the generated image.
CheckGeneratedPalettedImage(aDecoder, aRect);
// Reset and clear the image before the next test.
aPipe->ResetToFirstRow();
EXPECT_FALSE(aPipe->IsSurfaceFinished());
invalidRect = aPipe->TakeInvalidRect();
EXPECT_TRUE(invalidRect.isNothing());
uint32_t count = 0;
auto result = aPipe->WritePixels<uint8_t>([&]() {
++count;
return AsVariant(uint8_t(0));
});
EXPECT_EQ(WriteState::FINISHED, result);
EXPECT_EQ(100u * 100u, count);
EXPECT_TRUE(aPipe->IsSurfaceFinished());
invalidRect = aPipe->TakeInvalidRect();
EXPECT_TRUE(invalidRect.isSome());
EXPECT_EQ(IntRect(0, 0, 100, 100), invalidRect->mInputSpaceRect);
EXPECT_EQ(IntRect(0, 0, 100, 100), invalidRect->mOutputSpaceRect);
aPipe->ResetToFirstRow();
EXPECT_FALSE(aPipe->IsSurfaceFinished());
invalidRect = aPipe->TakeInvalidRect();
EXPECT_TRUE(invalidRect.isNothing());
}
class ImageSurfacePipeIntegration : public ::testing::Test
{
protected:
AutoInitializeImageLib mInit;
};
TEST_F(ImageSurfacePipeIntegration, SurfacePipe)
{
// Test that SurfacePipe objects can be initialized and move constructed.
SurfacePipe pipe = TestSurfacePipeFactory::SimpleSurfacePipe();
// Test that SurfacePipe objects can be move assigned.
pipe = TestSurfacePipeFactory::SimpleSurfacePipe();
// Test that SurfacePipe objects can be initialized with a pipeline.
RefPtr<Decoder> decoder = CreateTrivialDecoder();
ASSERT_TRUE(decoder != nullptr);
auto sink = MakeUnique<SurfaceSink>();
nsresult rv =
sink->Configure(SurfaceConfig { decoder, 0, IntSize(100, 100),
SurfaceFormat::B8G8R8A8, false });
ASSERT_TRUE(NS_SUCCEEDED(rv));
pipe = TestSurfacePipeFactory::SurfacePipeFromPipeline(sink);
// Test that WritePixels() gets passed through to the underlying pipeline.
{
uint32_t count = 0;
auto result = pipe.WritePixels<uint32_t>([&]() {
++count;
return AsVariant(BGRAColor::Green().AsPixel());
});
EXPECT_EQ(WriteState::FINISHED, result);
EXPECT_EQ(100u * 100u, count);
CheckSurfacePipeMethodResults(&pipe, decoder);
}
// Create a buffer the same size as one row of the surface, containing all
// green pixels. We'll use this for the WriteBuffer() tests.
uint32_t buffer[100];
for (int i = 0; i < 100; ++i) {
buffer[i] = BGRAColor::Green().AsPixel();
}
// Test that WriteBuffer() gets passed through to the underlying pipeline.
{
uint32_t count = 0;
WriteState result = WriteState::NEED_MORE_DATA;
while (result == WriteState::NEED_MORE_DATA) {
result = pipe.WriteBuffer(buffer);
++count;
}
EXPECT_EQ(WriteState::FINISHED, result);
EXPECT_EQ(100u, count);
CheckSurfacePipeMethodResults(&pipe, decoder);
}
// Test that the 3 argument version of WriteBuffer() gets passed through to
// the underlying pipeline.
{
uint32_t count = 0;
WriteState result = WriteState::NEED_MORE_DATA;
while (result == WriteState::NEED_MORE_DATA) {
result = pipe.WriteBuffer(buffer, 0, 100);
++count;
}
EXPECT_EQ(WriteState::FINISHED, result);
EXPECT_EQ(100u, count);
CheckSurfacePipeMethodResults(&pipe, decoder);
}
// Test that WriteEmptyRow() gets passed through to the underlying pipeline.
{
uint32_t count = 0;
WriteState result = WriteState::NEED_MORE_DATA;
while (result == WriteState::NEED_MORE_DATA) {
result = pipe.WriteEmptyRow();
++count;
}
EXPECT_EQ(WriteState::FINISHED, result);
EXPECT_EQ(100u, count);
CheckSurfacePipeMethodResults(&pipe, decoder, IntRect(0, 0, 0, 0));
}
// Mark the frame as finished so we don't get an assertion.
RawAccessFrameRef currentFrame = decoder->GetCurrentFrameRef();
currentFrame->Finish();
}
TEST_F(ImageSurfacePipeIntegration, PalettedSurfacePipe)
{
// Create a SurfacePipe containing a PalettedSurfaceSink.
RefPtr<Decoder> decoder = CreateTrivialDecoder();
ASSERT_TRUE(decoder != nullptr);
auto sink = MakeUnique<PalettedSurfaceSink>();
nsresult rv =
sink->Configure(PalettedSurfaceConfig { decoder, 0, IntSize(100, 100),
IntRect(0, 0, 100, 100),
SurfaceFormat::B8G8R8A8,
8, false });
ASSERT_TRUE(NS_SUCCEEDED(rv));
SurfacePipe pipe = TestSurfacePipeFactory::SurfacePipeFromPipeline(sink);
// Test that WritePixels() gets passed through to the underlying pipeline.
{
uint32_t count = 0;
auto result = pipe.WritePixels<uint8_t>([&]() {
++count;
return AsVariant(uint8_t(255));
});
EXPECT_EQ(WriteState::FINISHED, result);
EXPECT_EQ(100u * 100u, count);
CheckPalettedSurfacePipeMethodResults(&pipe, decoder);
}
// Create a buffer the same size as one row of the surface, containing all
// 255 pixels. We'll use this for the WriteBuffer() tests.
uint8_t buffer[100];
for (int i = 0; i < 100; ++i) {
buffer[i] = 255;
}
// Test that WriteBuffer() gets passed through to the underlying pipeline.
{
uint32_t count = 0;
WriteState result = WriteState::NEED_MORE_DATA;
while (result == WriteState::NEED_MORE_DATA) {
result = pipe.WriteBuffer(buffer);
++count;
}
EXPECT_EQ(WriteState::FINISHED, result);
EXPECT_EQ(100u, count);
CheckPalettedSurfacePipeMethodResults(&pipe, decoder);
}
// Test that the 3 argument version of WriteBuffer() gets passed through to
// the underlying pipeline.
{
uint32_t count = 0;
WriteState result = WriteState::NEED_MORE_DATA;
while (result == WriteState::NEED_MORE_DATA) {
result = pipe.WriteBuffer(buffer, 0, 100);
++count;
}
EXPECT_EQ(WriteState::FINISHED, result);
EXPECT_EQ(100u, count);
CheckPalettedSurfacePipeMethodResults(&pipe, decoder);
}
// Test that WriteEmptyRow() gets passed through to the underlying pipeline.
{
uint32_t count = 0;
WriteState result = WriteState::NEED_MORE_DATA;
while (result == WriteState::NEED_MORE_DATA) {
result = pipe.WriteEmptyRow();
++count;
}
EXPECT_EQ(WriteState::FINISHED, result);
EXPECT_EQ(100u, count);
CheckPalettedSurfacePipeMethodResults(&pipe, decoder, IntRect(0, 0, 0, 0));
}
// Mark the frame as finished so we don't get an assertion.
RawAccessFrameRef currentFrame = decoder->GetCurrentFrameRef();
currentFrame->Finish();
}
TEST_F(ImageSurfacePipeIntegration, DeinterlaceDownscaleWritePixels)
{
RefPtr<Decoder> decoder = CreateTrivialDecoder();
ASSERT_TRUE(decoder != nullptr);
auto test = [](Decoder* aDecoder, SurfaceFilter* aFilter) {
CheckWritePixels(aDecoder, aFilter,
/* aOutputRect = */ Some(IntRect(0, 0, 25, 25)));
};
WithFilterPipeline(decoder, test,
DeinterlacingConfig<uint32_t> { /* mProgressiveDisplay = */ true },
DownscalingConfig { IntSize(100, 100),
SurfaceFormat::B8G8R8A8 },
SurfaceConfig { decoder, 0, IntSize(25, 25),
SurfaceFormat::B8G8R8A8, false });
}
TEST_F(ImageSurfacePipeIntegration, RemoveFrameRectBottomRightDownscaleWritePixels)
{
// This test case uses a frame rect that extends beyond the borders of the
// image to the bottom and to the right. It looks roughly like this (with the
// box made of '#'s representing the frame rect):
//
// +------------+
// + +
// + +------------+
// + +############+
// +------+############+
// +############+
// +------------+
RefPtr<Decoder> decoder = CreateTrivialDecoder();
ASSERT_TRUE(decoder != nullptr);
// Note that aInputWriteRect is 100x50 because RemoveFrameRectFilter ignores
// trailing rows that don't show up in the output. (Leading rows unfortunately
// can't be ignored.) So the action of the pipeline is as follows:
//
// (1) RemoveFrameRectFilter reads a 100x50 region of the input.
// (aInputWriteRect captures this fact.) The remaining 50 rows are ignored
// because they extend off the bottom of the image due to the frame rect's
// (50, 50) offset. The 50 columns on the right also don't end up in the
// output, so ultimately only a 50x50 region in the output contains data
// from the input. The filter's output is not 50x50, though, but 100x100,
// because what RemoveFrameRectFilter does is introduce blank rows or
// columns as necessary to transform an image that needs a frame rect into
// an image that doesn't.
//
// (2) DownscalingFilter reads the output of RemoveFrameRectFilter (100x100)
// and downscales it to 20x20.
//
// (3) The surface owned by SurfaceSink logically has only a 10x10 region
// region in it that's non-blank; this is the downscaled version of the
// 50x50 region discussed in (1). (aOutputWriteRect captures this fact.)
// Some fuzz, as usual, is necessary when dealing with Lanczos downscaling.
auto test = [](Decoder* aDecoder, SurfaceFilter* aFilter) {
CheckWritePixels(aDecoder, aFilter,
/* aOutputRect = */ Some(IntRect(0, 0, 20, 20)),
/* aInputRect = */ Some(IntRect(0, 0, 100, 100)),
/* aInputWriteRect = */ Some(IntRect(50, 50, 100, 50)),
/* aOutputWriteRect = */ Some(IntRect(10, 10, 10, 10)),
/* aFuzz = */ 0x33);
};
WithFilterPipeline(decoder, test,
RemoveFrameRectConfig { IntRect(50, 50, 100, 100) },
DownscalingConfig { IntSize(100, 100),
SurfaceFormat::B8G8R8A8 },
SurfaceConfig { decoder, 0, IntSize(20, 20),
SurfaceFormat::B8G8R8A8, false });
}
TEST_F(ImageSurfacePipeIntegration, RemoveFrameRectTopLeftDownscaleWritePixels)
{
// This test case uses a frame rect that extends beyond the borders of the
// image to the top and to the left. It looks roughly like this (with the
// box made of '#'s representing the frame rect):
//
// +------------+
// +############+
// +############+------+
// +############+ +
// +------------+ +
// + +
// +------------+
RefPtr<Decoder> decoder = CreateTrivialDecoder();
ASSERT_TRUE(decoder != nullptr);
auto test = [](Decoder* aDecoder, SurfaceFilter* aFilter) {
CheckWritePixels(aDecoder, aFilter,
/* aOutputRect = */ Some(IntRect(0, 0, 20, 20)),
/* aInputRect = */ Some(IntRect(0, 0, 100, 100)),
/* aInputWriteRect = */ Some(IntRect(0, 0, 100, 100)),
/* aOutputWriteRect = */ Some(IntRect(0, 0, 10, 10)),
/* aFuzz = */ 0x21);
};
WithFilterPipeline(decoder, test,
RemoveFrameRectConfig { IntRect(-50, -50, 100, 100) },
DownscalingConfig { IntSize(100, 100),
SurfaceFormat::B8G8R8A8 },
SurfaceConfig { decoder, 0, IntSize(20, 20),
SurfaceFormat::B8G8R8A8, false });
}
TEST_F(ImageSurfacePipeIntegration, DeinterlaceRemoveFrameRectWritePixels)
{
RefPtr<Decoder> decoder = CreateTrivialDecoder();
ASSERT_TRUE(decoder != nullptr);
// Note that aInputRect is the full 100x100 size even though
// RemoveFrameRectFilter is part of this pipeline, because deinterlacing
// requires reading every row.
auto test = [](Decoder* aDecoder, SurfaceFilter* aFilter) {
CheckWritePixels(aDecoder, aFilter,
/* aOutputRect = */ Some(IntRect(0, 0, 100, 100)),
/* aInputRect = */ Some(IntRect(0, 0, 100, 100)),
/* aInputWriteRect = */ Some(IntRect(50, 50, 100, 100)),
/* aOutputWriteRect = */ Some(IntRect(50, 50, 50, 50)));
};
WithFilterPipeline(decoder, test,
DeinterlacingConfig<uint32_t> { /* mProgressiveDisplay = */ true },
RemoveFrameRectConfig { IntRect(50, 50, 100, 100) },
SurfaceConfig { decoder, 0, IntSize(100, 100),
SurfaceFormat::B8G8R8A8, false });
}
TEST_F(ImageSurfacePipeIntegration, DeinterlaceRemoveFrameRectDownscaleWritePixels)
{
RefPtr<Decoder> decoder = CreateTrivialDecoder();
ASSERT_TRUE(decoder != nullptr);
auto test = [](Decoder* aDecoder, SurfaceFilter* aFilter) {
CheckWritePixels(aDecoder, aFilter,
/* aOutputRect = */ Some(IntRect(0, 0, 20, 20)),
/* aInputRect = */ Some(IntRect(0, 0, 100, 100)),
/* aInputWriteRect = */ Some(IntRect(50, 50, 100, 100)),
/* aOutputWriteRect = */ Some(IntRect(10, 10, 10, 10)),
/* aFuzz = */ 33);
};
WithFilterPipeline(decoder, test,
DeinterlacingConfig<uint32_t> { /* mProgressiveDisplay = */ true },
RemoveFrameRectConfig { IntRect(50, 50, 100, 100) },
DownscalingConfig { IntSize(100, 100),
SurfaceFormat::B8G8R8A8 },
SurfaceConfig { decoder, 0, IntSize(20, 20),
SurfaceFormat::B8G8R8A8, false });
}
TEST_F(ImageSurfacePipeIntegration, ConfiguringPalettedRemoveFrameRectDownscaleFails)
{
RefPtr<Decoder> decoder = CreateTrivialDecoder();
ASSERT_TRUE(decoder != nullptr);
// This is an invalid pipeline for paletted images, so configuration should
// fail.
AssertConfiguringPipelineFails(decoder,
RemoveFrameRectConfig { IntRect(0, 0, 50, 50) },
DownscalingConfig { IntSize(100, 100),
SurfaceFormat::B8G8R8A8 },
PalettedSurfaceConfig { decoder, 0, IntSize(100, 100),
IntRect(0, 0, 50, 50),
SurfaceFormat::B8G8R8A8, 8,
false });
}
TEST_F(ImageSurfacePipeIntegration, ConfiguringPalettedDeinterlaceDownscaleFails)
{
RefPtr<Decoder> decoder = CreateTrivialDecoder();
ASSERT_TRUE(decoder != nullptr);
// This is an invalid pipeline for paletted images, so configuration should
// fail.
AssertConfiguringPipelineFails(decoder,
DeinterlacingConfig<uint8_t> { /* mProgressiveDisplay = */ true},
DownscalingConfig { IntSize(100, 100),
SurfaceFormat::B8G8R8A8 },
PalettedSurfaceConfig { decoder, 0, IntSize(100, 100),
IntRect(0, 0, 20, 20),
SurfaceFormat::B8G8R8A8, 8,
false });
}
TEST_F(ImageSurfacePipeIntegration, ConfiguringHugeDeinterlacingBufferFails)
{
RefPtr<Decoder> decoder = CreateTrivialDecoder();
ASSERT_TRUE(decoder != nullptr);
// When DownscalingFilter is used, we may succeed in allocating an output
// surface for huge images, because we only need to store the scaled-down
// version of the image. However, regardless of downscaling,
// DeinterlacingFilter needs to allocate a buffer as large as the size of the
// input. This can cause OOMs on operating systems that allow overcommit. This
// test makes sure that we reject such allocations.
AssertConfiguringPipelineFails(decoder,
DeinterlacingConfig<uint32_t> { /* mProgressiveDisplay = */ true},
DownscalingConfig { IntSize(60000, 60000),
SurfaceFormat::B8G8R8A8 },
SurfaceConfig { decoder, 0, IntSize(600, 600),
SurfaceFormat::B8G8R8A8, false });
}
|