Bug 1732115 - Part 1. Add swizzling support for reorienting by row. r=tnikkel

This is a new swizzle operation which allows us process a reorientation
row by row into its new orientation. This will be used in a later part
in the series in the image decoding pipeline.

Differential Revision: https://phabricator.services.mozilla.com/D126379
This commit is contained in:
Andrew Osmond 2021-10-06 14:41:17 +00:00
Родитель ed43fdd207
Коммит 1afab525ff
3 изменённых файлов: 434 добавлений и 0 удалений

Просмотреть файл

@ -6,6 +6,7 @@
#include "Swizzle.h"
#include "Logging.h"
#include "Orientation.h"
#include "Tools.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/EndianUtils.h"
@ -1305,5 +1306,180 @@ SwizzleRowFn SwizzleRow(SurfaceFormat aSrcFormat, SurfaceFormat aDstFormat) {
return nullptr;
}
static IntRect ReorientRowRotate0FlipFallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Reverse order of pixels in the row.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.width;
uint32_t* dst = reinterpret_cast<uint32_t*>(aDst + aSrcRow * aDstStride) +
aDstSize.width - 1;
do {
*dst-- = *src++;
} while (src < end);
return IntRect(0, aSrcRow, aDstSize.width, 1);
}
static IntRect ReorientRowRotate90FlipFallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels from top to bottom, into left to right columns.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.height;
uint32_t* dst = reinterpret_cast<uint32_t*>(aDst) + aSrcRow;
int32_t stride = aDstStride / sizeof(uint32_t);
do {
*dst = *src++;
dst += stride;
} while (src < end);
return IntRect(aSrcRow, 0, 1, aDstSize.height);
}
static IntRect ReorientRowRotate180FlipFallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels from top to bottom, into bottom to top rows.
uint8_t* dst = aDst + (aDstSize.height - aSrcRow - 1) * aDstStride;
memcpy(dst, aSrc, aDstSize.width * sizeof(uint32_t));
return IntRect(0, aDstSize.height - aSrcRow - 1, aDstSize.width, 1);
}
static IntRect ReorientRowRotate270FlipFallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels in reverse order from top to bottom, into right to left
// columns.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.height;
uint32_t* dst =
reinterpret_cast<uint32_t*>(aDst + (aDstSize.height - 1) * aDstStride) +
aDstSize.width - aSrcRow - 1;
int32_t stride = aDstStride / sizeof(uint32_t);
do {
*dst = *src++;
dst -= stride;
} while (src < end);
return IntRect(aDstSize.width - aSrcRow - 1, 0, 1, aDstSize.height);
}
static IntRect ReorientRowRotate0Fallback(const uint8_t* aSrc, int32_t aSrcRow,
uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels into the destination.
uint8_t* dst = aDst + aSrcRow * aDstStride;
memcpy(dst, aSrc, aDstSize.width * sizeof(uint32_t));
return IntRect(0, aSrcRow, aDstSize.width, 1);
}
static IntRect ReorientRowRotate90Fallback(const uint8_t* aSrc, int32_t aSrcRow,
uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels from top to bottom, into right to left columns.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.height;
uint32_t* dst =
reinterpret_cast<uint32_t*>(aDst) + aDstSize.width - aSrcRow - 1;
int32_t stride = aDstStride / sizeof(uint32_t);
do {
*dst = *src++;
dst += stride;
} while (src < end);
return IntRect(aDstSize.width - aSrcRow - 1, 0, 1, aDstSize.height);
}
static IntRect ReorientRowRotate180Fallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels in reverse order from top to bottom, into bottom to top
// rows.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.width;
uint32_t* dst = reinterpret_cast<uint32_t*>(
aDst + (aDstSize.height - aSrcRow - 1) * aDstStride) +
aDstSize.width - 1;
do {
*dst-- = *src++;
} while (src < end);
return IntRect(0, aDstSize.height - aSrcRow - 1, aDstSize.width, 1);
}
static IntRect ReorientRowRotate270Fallback(const uint8_t* aSrc,
int32_t aSrcRow, uint8_t* aDst,
const IntSize& aDstSize,
int32_t aDstStride) {
// Copy row of pixels in reverse order from top to bottom, into left to right
// column.
const uint32_t* src = reinterpret_cast<const uint32_t*>(aSrc);
const uint32_t* end = src + aDstSize.height;
uint32_t* dst =
reinterpret_cast<uint32_t*>(aDst + (aDstSize.height - 1) * aDstStride) +
aSrcRow;
int32_t stride = aDstStride / sizeof(uint32_t);
do {
*dst = *src++;
dst -= stride;
} while (src < end);
return IntRect(aSrcRow, 0, 1, aDstSize.height);
}
ReorientRowFn ReorientRow(const struct image::Orientation& aOrientation) {
switch (aOrientation.flip) {
case image::Flip::Unflipped:
switch (aOrientation.rotation) {
case image::Angle::D0:
return &ReorientRowRotate0Fallback;
case image::Angle::D90:
return &ReorientRowRotate90Fallback;
case image::Angle::D180:
return &ReorientRowRotate180Fallback;
case image::Angle::D270:
return &ReorientRowRotate270Fallback;
default:
break;
}
break;
case image::Flip::Horizontal:
switch (aOrientation.rotation) {
case image::Angle::D0:
return &ReorientRowRotate0FlipFallback;
case image::Angle::D90:
if (aOrientation.flipFirst) {
return &ReorientRowRotate270FlipFallback;
} else {
return &ReorientRowRotate90FlipFallback;
}
case image::Angle::D180:
return &ReorientRowRotate180FlipFallback;
case image::Angle::D270:
if (aOrientation.flipFirst) {
return &ReorientRowRotate90FlipFallback;
} else {
return &ReorientRowRotate270FlipFallback;
}
default:
break;
}
break;
default:
break;
}
MOZ_ASSERT_UNREACHABLE("Unhandled orientation!");
return nullptr;
}
} // namespace gfx
} // namespace mozilla

Просмотреть файл

@ -8,8 +8,13 @@
#define MOZILLA_GFX_SWIZZLE_H_
#include "Point.h"
#include "Rect.h"
namespace mozilla {
namespace image {
struct Orientation;
}
namespace gfx {
/**
@ -66,6 +71,20 @@ GFX2D_API SwizzleRowFn UnpremultiplyRow(SurfaceFormat aSrcFormat,
GFX2D_API SwizzleRowFn SwizzleRow(SurfaceFormat aSrcFormat,
SurfaceFormat aDstFormat);
/**
* Reorients source and writes it to destination. Returns the dirty rect of
* what was changed in aDst.
*/
typedef IntRect (*ReorientRowFn)(const uint8_t* aSrc, int32_t aSrcRow,
uint8_t* aDst, const IntSize& aDstSize,
int32_t aDstStride);
/**
* Get a function pointer to perform reorientation by row.
*/
GFX2D_API ReorientRowFn
ReorientRow(const struct image::Orientation& aOrientation);
} // namespace gfx
} // namespace mozilla

Просмотреть файл

@ -7,9 +7,11 @@
#include "gtest/gtest.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/gfx/Swizzle.h"
#include "Orientation.h"
using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::image;
TEST(Moz2D, PremultiplyData)
{
@ -330,3 +332,240 @@ TEST(Moz2D, SwizzleRow)
func(out_unpack, out_unpack, 16);
EXPECT_TRUE(ArrayEqual(out_unpack, check_unpack_xrgb));
}
TEST(Moz2D, ReorientRow)
{
// Input is a 3x4 image.
const uint8_t in_row0[3 * 4] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
};
const uint8_t in_row1[3 * 4] = {
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
};
const uint8_t in_row2[3 * 4] = {
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
};
const uint8_t in_row3[3 * 4] = {
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
};
// Output is either a 3x4 image or 4x3 image.
uint8_t out[3 * 4 * 4];
IntSize outSize(3, 4);
IntSize outSizeSwap(4, 3);
int32_t outStride = 3 * 4;
int32_t outStrideSwap = 4 * 4;
IntRect dirty;
auto func = ReorientRow(Orientation());
dirty = func(in_row0, 0, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 0, 3, 1));
dirty = func(in_row1, 1, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 1, 3, 1));
dirty = func(in_row2, 2, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 2, 3, 1));
dirty = func(in_row3, 3, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 3, 3, 1));
// clang-format off
const uint8_t check_identity[3 * 4 * 4] = {
0, 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,
};
// clang-format on
EXPECT_TRUE(ArrayEqual(out, check_identity));
func = ReorientRow(Orientation(Angle::D90));
dirty = func(in_row0, 0, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(3, 0, 1, 3));
dirty = func(in_row1, 1, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(2, 0, 1, 3));
dirty = func(in_row2, 2, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(1, 0, 1, 3));
dirty = func(in_row3, 3, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(0, 0, 1, 3));
// clang-format off
const uint8_t check_d90[3 * 4 * 4] = {
36, 37, 38, 39, 24, 25, 26, 27, 12, 13, 14, 15, 0, 1, 2, 3,
40, 41, 42, 43, 28, 29, 30, 31, 16, 17, 18, 19, 4, 5, 6, 7,
44, 45, 46, 47, 32, 33, 34, 35, 20, 21, 22, 23, 8, 9, 10, 11,
};
// clang-format on
EXPECT_TRUE(ArrayEqual(out, check_d90));
func = ReorientRow(Orientation(Angle::D180));
dirty = func(in_row0, 0, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 3, 3, 1));
dirty = func(in_row1, 1, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 2, 3, 1));
dirty = func(in_row2, 2, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 1, 3, 1));
dirty = func(in_row3, 3, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 0, 3, 1));
// clang-format off
const uint8_t check_d180[3 * 4 * 4] = {
44, 45, 46, 47, 40, 41, 42, 43, 36, 37, 38, 39,
32, 33, 34, 35, 28, 29, 30, 31, 24, 25, 26, 27,
20, 21, 22, 23, 16, 17, 18, 19, 12, 13, 14, 15,
8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3,
};
// clang-format on
EXPECT_TRUE(ArrayEqual(out, check_d180));
func = ReorientRow(Orientation(Angle::D270));
dirty = func(in_row0, 0, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(0, 0, 1, 3));
dirty = func(in_row1, 1, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(1, 0, 1, 3));
dirty = func(in_row2, 2, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(2, 0, 1, 3));
dirty = func(in_row3, 3, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(3, 0, 1, 3));
// clang-format off
const uint8_t check_d270[3 * 4 * 4] = {
8, 9, 10, 11, 20, 21, 22, 23, 32, 33, 34, 35, 44, 45, 46, 47,
4, 5, 6, 7, 16, 17, 18, 19, 28, 29, 30, 31, 40, 41, 42, 43,
0, 1, 2, 3, 12, 13, 14, 15, 24, 25, 26, 27, 36, 37, 38, 39,
};
// clang-format on
EXPECT_TRUE(ArrayEqual(out, check_d270));
func = ReorientRow(Orientation(Angle::D0, Flip::Horizontal));
dirty = func(in_row0, 0, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 0, 3, 1));
dirty = func(in_row1, 1, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 1, 3, 1));
dirty = func(in_row2, 2, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 2, 3, 1));
dirty = func(in_row3, 3, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 3, 3, 1));
// clang-format off
const uint8_t check_d0_flip[3 * 4 * 4] = {
8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3,
20, 21, 22, 23, 16, 17, 18, 19, 12, 13, 14, 15,
32, 33, 34, 35, 28, 29, 30, 31, 24, 25, 26, 27,
44, 45, 46, 47, 40, 41, 42, 43, 36, 37, 38, 39,
};
// clang-format on
EXPECT_TRUE(ArrayEqual(out, check_d0_flip));
func = ReorientRow(Orientation(Angle::D90, Flip::Horizontal));
dirty = func(in_row0, 0, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(0, 0, 1, 3));
dirty = func(in_row1, 1, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(1, 0, 1, 3));
dirty = func(in_row2, 2, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(2, 0, 1, 3));
dirty = func(in_row3, 3, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(3, 0, 1, 3));
// clang-format off
const uint8_t check_d90_flip[3 * 4 * 4] = {
0, 1, 2, 3, 12, 13, 14, 15, 24, 25, 26, 27, 36, 37, 38, 39,
4, 5, 6, 7, 16, 17, 18, 19, 28, 29, 30, 31, 40, 41, 42, 43,
8, 9, 10, 11, 20, 21, 22, 23, 32, 33, 34, 35, 44, 45, 46, 47,
};
// clang-format on
EXPECT_TRUE(ArrayEqual(out, check_d90_flip));
func = ReorientRow(Orientation(Angle::D180, Flip::Horizontal));
dirty = func(in_row0, 0, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 3, 3, 1));
dirty = func(in_row1, 1, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 2, 3, 1));
dirty = func(in_row2, 2, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 1, 3, 1));
dirty = func(in_row3, 3, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 0, 3, 1));
// clang-format off
const uint8_t check_d180_flip[3 * 4 * 4] = {
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
};
// clang-format on
EXPECT_TRUE(ArrayEqual(out, check_d180_flip));
func = ReorientRow(Orientation(Angle::D270, Flip::Horizontal));
dirty = func(in_row0, 0, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(3, 0, 1, 3));
dirty = func(in_row1, 1, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(2, 0, 1, 3));
dirty = func(in_row2, 2, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(1, 0, 1, 3));
dirty = func(in_row3, 3, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(0, 0, 1, 3));
// clang-format off
const uint8_t check_d270_flip[3 * 4 * 4] = {
44, 45, 46, 47, 32, 33, 34, 35, 20, 21, 22, 23, 8, 9, 10, 11,
40, 41, 42, 43, 28, 29, 30, 31, 16, 17, 18, 19, 4, 5, 6, 7,
36, 37, 38, 39, 24, 25, 26, 27, 12, 13, 14, 15, 0, 1, 2, 3,
};
// clang-format on
EXPECT_TRUE(ArrayEqual(out, check_d270_flip));
func = ReorientRow(
Orientation(Angle::D0, Flip::Horizontal, /* aFlipFirst */ true));
dirty = func(in_row0, 0, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 0, 3, 1));
dirty = func(in_row1, 1, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 1, 3, 1));
dirty = func(in_row2, 2, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 2, 3, 1));
dirty = func(in_row3, 3, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 3, 3, 1));
// No rotation, so flipping before and after are the same.
EXPECT_TRUE(ArrayEqual(out, check_d0_flip));
func = ReorientRow(
Orientation(Angle::D90, Flip::Horizontal, /* aFlipFirst */ true));
dirty = func(in_row0, 0, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(3, 0, 1, 3));
dirty = func(in_row1, 1, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(2, 0, 1, 3));
dirty = func(in_row2, 2, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(1, 0, 1, 3));
dirty = func(in_row3, 3, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(0, 0, 1, 3));
// Flip, rotate 90 degrees is the same as rotate 270 degrees, flip.
EXPECT_TRUE(ArrayEqual(out, check_d270_flip));
func = ReorientRow(
Orientation(Angle::D180, Flip::Horizontal, /* aFlipFirst */ true));
dirty = func(in_row0, 0, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 3, 3, 1));
dirty = func(in_row1, 1, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 2, 3, 1));
dirty = func(in_row2, 2, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 1, 3, 1));
dirty = func(in_row3, 3, out, outSize, outStride);
EXPECT_EQ(dirty, IntRect(0, 0, 3, 1));
// Flip, rotate 180 degrees is the same as rotate 180 degrees, flip.
EXPECT_TRUE(ArrayEqual(out, check_d180_flip));
func = ReorientRow(
Orientation(Angle::D270, Flip::Horizontal, /* aFlipFirst */ true));
dirty = func(in_row0, 0, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(0, 0, 1, 3));
dirty = func(in_row1, 1, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(1, 0, 1, 3));
dirty = func(in_row2, 2, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(2, 0, 1, 3));
dirty = func(in_row3, 3, out, outSizeSwap, outStrideSwap);
EXPECT_EQ(dirty, IntRect(3, 0, 1, 3));
// Flip, rotate 270 degrees is the same as rotate 90 degrees, flip.
EXPECT_TRUE(ArrayEqual(out, check_d90_flip));
}