diff --git a/src/image_util/copyimage.cpp b/src/image_util/copyimage.cpp index 138c4a27a..b5c2773e2 100644 --- a/src/image_util/copyimage.cpp +++ b/src/image_util/copyimage.cpp @@ -11,12 +11,64 @@ namespace angle { -void CopyBGRA8ToRGBA8(const uint8_t *source, uint8_t *dest) +namespace { - uint32_t argb = *reinterpret_cast(source); - *reinterpret_cast(dest) = (argb & 0xFF00FF00) | // Keep alpha and green - (argb & 0x00FF0000) >> 16 | // Move red to blue - (argb & 0x000000FF) << 16; // Move blue to red +inline uint32_t SwizzleBGRAToRGBA(uint32_t argb) +{ + return ((argb & 0x000000FF) << 16) | // Move BGRA blue to RGBA blue + ((argb & 0x00FF0000) >> 16) | // Move BGRA red to RGBA red + ((argb & 0xFF00FF00)); // Keep alpha and green +} + +void CopyBGRA8ToRGBA8Fast(const uint8_t *source, + int srcYAxisPitch, + uint8_t *dest, + int destYAxisPitch, + int destWidth, + int destHeight) +{ + for (int y = 0; y < destHeight; ++y) + { + const uint32_t *src32 = reinterpret_cast(source + y * srcYAxisPitch); + uint32_t *dest32 = reinterpret_cast(dest + y * destYAxisPitch); + const uint32_t *end32 = src32 + destWidth; + while (src32 != end32) + { + *dest32++ = SwizzleBGRAToRGBA(*src32++); + } + } +} +} // namespace + +void CopyBGRA8ToRGBA8(const uint8_t *source, + int srcXAxisPitch, + int srcYAxisPitch, + uint8_t *dest, + int destXAxisPitch, + int destYAxisPitch, + int destWidth, + int destHeight) +{ + if (srcXAxisPitch == 4 && destXAxisPitch == 4) + { + CopyBGRA8ToRGBA8Fast(source, srcYAxisPitch, dest, destYAxisPitch, destWidth, destHeight); + return; + } + + for (int y = 0; y < destHeight; ++y) + { + uint8_t *dst = dest + y * destYAxisPitch; + const uint8_t *src = source + y * srcYAxisPitch; + const uint8_t *end = src + destWidth * srcXAxisPitch; + + while (src != end) + { + *reinterpret_cast(dst) = + SwizzleBGRAToRGBA(*reinterpret_cast(src)); + src += srcXAxisPitch; + dst += destXAxisPitch; + } + } } } // namespace angle diff --git a/src/image_util/copyimage.h b/src/image_util/copyimage.h index aa5f668c8..9825b86f9 100644 --- a/src/image_util/copyimage.h +++ b/src/image_util/copyimage.h @@ -30,7 +30,14 @@ void ReadDepthStencil(const uint8_t *source, uint8_t *dest); template void WriteDepthStencil(const uint8_t *source, uint8_t *dest); -void CopyBGRA8ToRGBA8(const uint8_t *source, uint8_t *dest); +void CopyBGRA8ToRGBA8(const uint8_t *source, + int srcXAxisPitch, + int srcYAxisPitch, + uint8_t *dest, + int destXAxisPitch, + int destYAxisPitch, + int destWidth, + int destHeight); } // namespace angle diff --git a/src/libANGLE/renderer/renderer_utils.cpp b/src/libANGLE/renderer/renderer_utils.cpp index 189040330..a896d88c3 100644 --- a/src/libANGLE/renderer/renderer_utils.cpp +++ b/src/libANGLE/renderer/renderer_utils.cpp @@ -382,22 +382,13 @@ void PackPixels(const PackPixelsParams ¶ms, return; } - PixelCopyFunction fastCopyFunc = sourceFormat.fastCopyFunctions.get(params.destFormat->id); + FastCopyFunction fastCopyFunc = sourceFormat.fastCopyFunctions.get(params.destFormat->id); if (fastCopyFunc) { // Fast copy is possible through some special function - for (int y = 0; y < destHeight; ++y) - { - for (int x = 0; x < destWidth; ++x) - { - uint8_t *dest = - destWithOffset + y * params.outputPitch + x * params.destFormat->pixelBytes; - const uint8_t *src = source + y * yAxisPitch + x * xAxisPitch; - - fastCopyFunc(src, dest); - } - } + fastCopyFunc(source, xAxisPitch, yAxisPitch, destWithOffset, params.destFormat->pixelBytes, + params.outputPitch, destWidth, destHeight); return; } @@ -435,19 +426,34 @@ bool FastCopyFunctionMap::has(angle::FormatID formatID) const return (get(formatID) != nullptr); } -PixelCopyFunction FastCopyFunctionMap::get(angle::FormatID formatID) const +namespace { - for (size_t index = 0; index < mSize; ++index) + +const FastCopyFunctionMap::Entry *getEntry(const FastCopyFunctionMap::Entry *entry, + size_t numEntries, + angle::FormatID formatID) +{ + const FastCopyFunctionMap::Entry *end = entry + numEntries; + while (entry != end) { - if (mData[index].formatID == formatID) + if (entry->formatID == formatID) { - return mData[index].func; + return entry; } + ++entry; } return nullptr; } +} // namespace + +FastCopyFunction FastCopyFunctionMap::get(angle::FormatID formatID) const +{ + const FastCopyFunctionMap::Entry *entry = getEntry(mData, mSize, formatID); + return entry ? entry->func : nullptr; +} + bool ShouldUseDebugLayers(const egl::AttributeMap &attribs) { EGLAttrib debugSetting = diff --git a/src/libANGLE/renderer/renderer_utils.h b/src/libANGLE/renderer/renderer_utils.h index ecdba2fd3..8fb18fc5d 100644 --- a/src/libANGLE/renderer/renderer_utils.h +++ b/src/libANGLE/renderer/renderer_utils.h @@ -82,7 +82,14 @@ using MipGenerationFunction = void (*)(size_t sourceWidth, typedef void (*PixelReadFunction)(const uint8_t *source, uint8_t *dest); typedef void (*PixelWriteFunction)(const uint8_t *source, uint8_t *dest); -typedef void (*PixelCopyFunction)(const uint8_t *source, uint8_t *dest); +typedef void (*FastCopyFunction)(const uint8_t *source, + int srcXAxisPitch, + int srcYAxisPitch, + uint8_t *dest, + int destXAxisPitch, + int destYAxisPitch, + int width, + int height); class FastCopyFunctionMap { @@ -90,7 +97,7 @@ class FastCopyFunctionMap struct Entry { angle::FormatID formatID; - PixelCopyFunction func; + FastCopyFunction func; }; constexpr FastCopyFunctionMap() : FastCopyFunctionMap(nullptr, 0) {} @@ -98,7 +105,7 @@ class FastCopyFunctionMap constexpr FastCopyFunctionMap(const Entry *data, size_t size) : mSize(size), mData(data) {} bool has(angle::FormatID formatID) const; - PixelCopyFunction get(angle::FormatID formatID) const; + FastCopyFunction get(angle::FormatID formatID) const; private: size_t mSize;