Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 7894763

Browse files
committed
Bug 1551088 - Part 7. Add gtests for SwizzleFilter. r=tnikkel
Differential Revision: https://phabricator.services.mozilla.com/D46450 --HG-- extra : moz-landing-system : lando
1 parent dda166a commit 7894763

4 files changed

Lines changed: 142 additions & 181 deletions

File tree

image/test/gtest/Common.cpp

Lines changed: 13 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,6 @@ bool IsSolidColor(SourceSurface* aSurface, BGRAColor aColor,
167167
aColor, aFuzz);
168168
}
169169

170-
bool IsSolidPalettedColor(Decoder* aDecoder, uint8_t aColor) {
171-
RawAccessFrameRef currentFrame = aDecoder->GetCurrentFrameRef();
172-
return PalettedRectIsSolidColor(aDecoder, currentFrame->GetRect(), aColor);
173-
}
174-
175170
bool RowsAreSolidColor(SourceSurface* aSurface, int32_t aStartRow,
176171
int32_t aRowCount, BGRAColor aColor,
177172
uint8_t aFuzz /* = 0 */) {
@@ -180,15 +175,6 @@ bool RowsAreSolidColor(SourceSurface* aSurface, int32_t aStartRow,
180175
aSurface, IntRect(0, aStartRow, size.width, aRowCount), aColor, aFuzz);
181176
}
182177

183-
bool PalettedRowsAreSolidColor(Decoder* aDecoder, int32_t aStartRow,
184-
int32_t aRowCount, uint8_t aColor) {
185-
RawAccessFrameRef currentFrame = aDecoder->GetCurrentFrameRef();
186-
IntRect frameRect = currentFrame->GetRect();
187-
IntRect solidColorRect(frameRect.X(), aStartRow, frameRect.Width(),
188-
aRowCount);
189-
return PalettedRectIsSolidColor(aDecoder, solidColorRect, aColor);
190-
}
191-
192178
bool RectIsSolidColor(SourceSurface* aSurface, const IntRect& aRect,
193179
BGRAColor aColor, uint8_t aFuzz /* = 0 */) {
194180
IntSize surfaceSize = aSurface->GetSize();
@@ -228,42 +214,6 @@ bool RectIsSolidColor(SourceSurface* aSurface, const IntRect& aRect,
228214
return true;
229215
}
230216

231-
bool PalettedRectIsSolidColor(Decoder* aDecoder, const IntRect& aRect,
232-
uint8_t aColor) {
233-
RawAccessFrameRef currentFrame = aDecoder->GetCurrentFrameRef();
234-
uint8_t* imageData;
235-
uint32_t imageLength;
236-
currentFrame->GetImageData(&imageData, &imageLength);
237-
ASSERT_TRUE_OR_RETURN(imageData, false);
238-
239-
// Clamp to the frame rect. If any pixels outside the frame rect are included,
240-
// we immediately fail, because such pixels don't have any "color" in the
241-
// sense this function measures - they're transparent, and that doesn't
242-
// necessarily correspond to any color palette index at all.
243-
IntRect frameRect = currentFrame->GetRect();
244-
ASSERT_EQ_OR_RETURN(imageLength, uint32_t(frameRect.Area()), false);
245-
IntRect rect = aRect.Intersect(frameRect);
246-
ASSERT_EQ_OR_RETURN(rect.Area(), aRect.Area(), false);
247-
248-
// Translate |rect| by |frameRect.TopLeft()| to reflect the fact that the
249-
// frame rect's offset doesn't actually mean anything in terms of the
250-
// in-memory representation of the surface. The image data starts at the upper
251-
// left corner of the frame rect, in other words.
252-
rect -= frameRect.TopLeft();
253-
254-
// Walk through the image data and make sure that the entire rect has the
255-
// palette index |aColor|.
256-
int32_t rowLength = frameRect.Width();
257-
for (int32_t row = rect.Y(); row < rect.YMost(); ++row) {
258-
for (int32_t col = rect.X(); col < rect.XMost(); ++col) {
259-
int32_t i = row * rowLength + col;
260-
ASSERT_EQ_OR_RETURN(aColor, imageData[i], false);
261-
}
262-
}
263-
264-
return true;
265-
}
266-
267217
bool RowHasPixels(SourceSurface* aSurface, int32_t aRow,
268218
const vector<BGRAColor>& aPixels) {
269219
ASSERT_GE_OR_RETURN(aRow, 0, false);
@@ -372,93 +322,20 @@ void CheckGeneratedSurface(SourceSurface* aSurface, const IntRect& aRect,
372322
aOuterColor, aFuzz));
373323
}
374324

375-
void CheckGeneratedPalettedImage(Decoder* aDecoder, const IntRect& aRect) {
376-
RawAccessFrameRef currentFrame = aDecoder->GetCurrentFrameRef();
377-
IntSize imageSize = currentFrame->GetSize();
378-
379-
// This diagram shows how the surface is divided into regions that the code
380-
// below tests for the correct content. The output rect is the bounds of the
381-
// region labeled 'C'.
382-
//
383-
// +---------------------------+
384-
// | A |
385-
// +---------+--------+--------+
386-
// | B | C | D |
387-
// +---------+--------+--------+
388-
// | E |
389-
// +---------------------------+
390-
391-
// Check that the output rect itself is all 255's. (Region 'C'.)
392-
EXPECT_TRUE(PalettedRectIsSolidColor(aDecoder, aRect, 255));
393-
394-
// Check that the area above the output rect is all 0's. (Region 'A'.)
395-
EXPECT_TRUE(PalettedRectIsSolidColor(
396-
aDecoder, IntRect(0, 0, imageSize.width, aRect.Y()), 0));
397-
398-
// Check that the area to the left of the output rect is all 0's. (Region
399-
// 'B'.)
400-
EXPECT_TRUE(PalettedRectIsSolidColor(
401-
aDecoder, IntRect(0, aRect.Y(), aRect.X(), aRect.YMost()), 0));
402-
403-
// Check that the area to the right of the output rect is all 0's. (Region
404-
// 'D'.)
405-
const int32_t widthOnRight = imageSize.width - aRect.XMost();
406-
EXPECT_TRUE(PalettedRectIsSolidColor(
407-
aDecoder, IntRect(aRect.XMost(), aRect.Y(), widthOnRight, aRect.YMost()),
408-
0));
409-
410-
// Check that the area below the output rect is transparent. (Region 'E'.)
411-
const int32_t heightBelow = imageSize.height - aRect.YMost();
412-
EXPECT_TRUE(PalettedRectIsSolidColor(
413-
aDecoder, IntRect(0, aRect.YMost(), imageSize.width, heightBelow), 0));
414-
}
415-
416325
void CheckWritePixels(Decoder* aDecoder, SurfaceFilter* aFilter,
417326
const Maybe<IntRect>& aOutputRect /* = Nothing() */,
418327
const Maybe<IntRect>& aInputRect /* = Nothing() */,
419328
const Maybe<IntRect>& aInputWriteRect /* = Nothing() */,
420329
const Maybe<IntRect>& aOutputWriteRect /* = Nothing() */,
421330
uint8_t aFuzz /* = 0 */) {
422-
IntRect outputRect = aOutputRect.valueOr(IntRect(0, 0, 100, 100));
423-
IntRect inputRect = aInputRect.valueOr(IntRect(0, 0, 100, 100));
424-
IntRect inputWriteRect = aInputWriteRect.valueOr(inputRect);
425-
IntRect outputWriteRect = aOutputWriteRect.valueOr(outputRect);
426-
427-
// Fill the image.
428-
int32_t count = 0;
429-
auto result = aFilter->WritePixels<uint32_t>([&] {
430-
++count;
431-
return AsVariant(BGRAColor::Green().AsPixel());
432-
});
433-
EXPECT_EQ(WriteState::FINISHED, result);
434-
EXPECT_EQ(inputWriteRect.Width() * inputWriteRect.Height(), count);
435-
436-
AssertCorrectPipelineFinalState(aFilter, inputRect, outputRect);
437-
438-
// Attempt to write more data and make sure nothing changes.
439-
const int32_t oldCount = count;
440-
result = aFilter->WritePixels<uint32_t>([&] {
441-
++count;
442-
return AsVariant(BGRAColor::Green().AsPixel());
443-
});
444-
EXPECT_EQ(oldCount, count);
445-
EXPECT_EQ(WriteState::FINISHED, result);
446-
EXPECT_TRUE(aFilter->IsSurfaceFinished());
447-
Maybe<SurfaceInvalidRect> invalidRect = aFilter->TakeInvalidRect();
448-
EXPECT_TRUE(invalidRect.isNothing());
449-
450-
// Attempt to advance to the next row and make sure nothing changes.
451-
aFilter->AdvanceRow();
452-
EXPECT_TRUE(aFilter->IsSurfaceFinished());
453-
invalidRect = aFilter->TakeInvalidRect();
454-
EXPECT_TRUE(invalidRect.isNothing());
455-
456-
// Check that the generated image is correct.
457-
CheckGeneratedImage(aDecoder, outputWriteRect, aFuzz);
331+
CheckTransformedWritePixels(aDecoder, aFilter, BGRAColor::Green(),
332+
BGRAColor::Green(), aOutputRect, aInputRect,
333+
aInputWriteRect, aOutputWriteRect, aFuzz);
458334
}
459335

460-
void CheckPalettedWritePixels(
461-
Decoder* aDecoder, SurfaceFilter* aFilter,
336+
void CheckTransformedWritePixels(
337+
Decoder* aDecoder, SurfaceFilter* aFilter, const BGRAColor& aInputColor,
338+
const BGRAColor& aOutputColor,
462339
const Maybe<IntRect>& aOutputRect /* = Nothing() */,
463340
const Maybe<IntRect>& aInputRect /* = Nothing() */,
464341
const Maybe<IntRect>& aInputWriteRect /* = Nothing() */,
@@ -471,9 +348,9 @@ void CheckPalettedWritePixels(
471348

472349
// Fill the image.
473350
int32_t count = 0;
474-
auto result = aFilter->WritePixels<uint8_t>([&] {
351+
auto result = aFilter->WritePixels<uint32_t>([&] {
475352
++count;
476-
return AsVariant(uint8_t(255));
353+
return AsVariant(aInputColor.AsPixel());
477354
});
478355
EXPECT_EQ(WriteState::FINISHED, result);
479356
EXPECT_EQ(inputWriteRect.Width() * inputWriteRect.Height(), count);
@@ -482,9 +359,9 @@ void CheckPalettedWritePixels(
482359

483360
// Attempt to write more data and make sure nothing changes.
484361
const int32_t oldCount = count;
485-
result = aFilter->WritePixels<uint8_t>([&] {
362+
result = aFilter->WritePixels<uint32_t>([&] {
486363
++count;
487-
return AsVariant(uint8_t(255));
364+
return AsVariant(aInputColor.AsPixel());
488365
});
489366
EXPECT_EQ(oldCount, count);
490367
EXPECT_EQ(WriteState::FINISHED, result);
@@ -500,15 +377,9 @@ void CheckPalettedWritePixels(
500377

501378
// Check that the generated image is correct.
502379
RawAccessFrameRef currentFrame = aDecoder->GetCurrentFrameRef();
503-
uint8_t* imageData;
504-
uint32_t imageLength;
505-
currentFrame->GetImageData(&imageData, &imageLength);
506-
ASSERT_TRUE(imageData != nullptr);
507-
ASSERT_EQ(outputWriteRect.Width() * outputWriteRect.Height(),
508-
int32_t(imageLength));
509-
for (uint32_t i = 0; i < imageLength; ++i) {
510-
ASSERT_EQ(uint8_t(255), imageData[i]);
511-
}
380+
RefPtr<SourceSurface> surface = currentFrame->GetSourceSurface();
381+
CheckGeneratedSurface(surface, outputWriteRect, aOutputColor,
382+
BGRAColor::Transparent(), aFuzz);
512383
}
513384

514385
///////////////////////////////////////////////////////////////////////////////

image/test/gtest/Common.h

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,6 @@ already_AddRefed<nsIInputStream> LoadFile(const char* aRelativePath);
158158
bool IsSolidColor(gfx::SourceSurface* aSurface, BGRAColor aColor,
159159
uint8_t aFuzz = 0);
160160

161-
/**
162-
* @returns true if every pixel of @aDecoder's surface has the palette index
163-
* specified by @aColor.
164-
*/
165-
bool IsSolidPalettedColor(Decoder* aDecoder, uint8_t aColor);
166-
167161
/**
168162
* @returns true if every pixel in the range of rows specified by @aStartRow and
169163
* @aRowCount of @aSurface is @aColor.
@@ -175,13 +169,6 @@ bool IsSolidPalettedColor(Decoder* aDecoder, uint8_t aColor);
175169
bool RowsAreSolidColor(gfx::SourceSurface* aSurface, int32_t aStartRow,
176170
int32_t aRowCount, BGRAColor aColor, uint8_t aFuzz = 0);
177171

178-
/**
179-
* @returns true if every pixel in the range of rows specified by @aStartRow and
180-
* @aRowCount of @aDecoder's surface has the palette index specified by @aColor.
181-
*/
182-
bool PalettedRowsAreSolidColor(Decoder* aDecoder, int32_t aStartRow,
183-
int32_t aRowCount, uint8_t aColor);
184-
185172
/**
186173
* @returns true if every pixel in the rect specified by @aRect is @aColor.
187174
*
@@ -192,13 +179,6 @@ bool PalettedRowsAreSolidColor(Decoder* aDecoder, int32_t aStartRow,
192179
bool RectIsSolidColor(gfx::SourceSurface* aSurface, const gfx::IntRect& aRect,
193180
BGRAColor aColor, uint8_t aFuzz = 0);
194181

195-
/**
196-
* @returns true if every pixel in the rect specified by @aRect has the palette
197-
* index specified by @aColor.
198-
*/
199-
bool PalettedRectIsSolidColor(Decoder* aDecoder, const gfx::IntRect& aRect,
200-
uint8_t aColor);
201-
202182
/**
203183
* @returns true if the pixels in @aRow of @aSurface match the pixels given in
204184
* @aPixels.
@@ -244,9 +224,9 @@ class CountResumes : public IResumable {
244224
* that requires a decoder to initialize or to allocate surfaces but doesn't
245225
* actually need the decoder to do any decoding.
246226
*
247-
* XXX(seth): We only need this because SurfaceSink and PalettedSurfaceSink
248-
* defer to the decoder for surface allocation. Once all decoders use
249-
* SurfacePipe we won't need to do that anymore and we can remove this function.
227+
* XXX(seth): We only need this because SurfaceSink defer to the decoder for
228+
* surface allocation. Once all decoders use SurfacePipe we won't need to do
229+
* that anymore and we can remove this function.
250230
*/
251231
already_AddRefed<Decoder> CreateTrivialDecoder();
252232

@@ -349,19 +329,6 @@ void CheckGeneratedSurface(gfx::SourceSurface* aSurface,
349329
const BGRAColor& aInnerColor,
350330
const BGRAColor& aOuterColor, uint8_t aFuzz = 0);
351331

352-
/**
353-
* Checks a generated paletted image for correctness. Reports any unexpected
354-
* deviation from the expected image as GTest failures.
355-
*
356-
* @param aDecoder The decoder which contains the image. The decoder's current
357-
* frame will be checked.
358-
* @param aRect The region in the space of the output surface that the filter
359-
* pipeline will actually write to. It's expected that pixels in
360-
* this region have a palette index of 255, while pixels outside
361-
* this region have a palette index of 0.
362-
*/
363-
void CheckGeneratedPalettedImage(Decoder* aDecoder, const gfx::IntRect& aRect);
364-
365332
/**
366333
* Tests the result of calling WritePixels() using the provided SurfaceFilter
367334
* pipeline. The pipeline must be a normal (i.e., non-paletted) pipeline.
@@ -399,11 +366,13 @@ void CheckWritePixels(Decoder* aDecoder, SurfaceFilter* aFilter,
399366

400367
/**
401368
* Tests the result of calling WritePixels() using the provided SurfaceFilter
402-
* pipeline. The pipeline must be a paletted pipeline.
369+
* pipeline. Allows for control over the input color to write, and the expected
370+
* output color.
403371
* @see CheckWritePixels() for documentation of the arguments.
404372
*/
405-
void CheckPalettedWritePixels(
406-
Decoder* aDecoder, SurfaceFilter* aFilter,
373+
void CheckTransformedWritePixels(
374+
Decoder* aDecoder, SurfaceFilter* aFilter, const BGRAColor& aInputColor,
375+
const BGRAColor& aOutputColor,
407376
const Maybe<gfx::IntRect>& aOutputRect = Nothing(),
408377
const Maybe<gfx::IntRect>& aInputRect = Nothing(),
409378
const Maybe<gfx::IntRect>& aInputWriteRect = Nothing(),

0 commit comments

Comments
 (0)