Commit d2bf8e14 authored by noel@chromium.org's avatar noel@chromium.org

Improve PNG decode performance: avoid branching in the row write loop

No change in behavior, no new tests.

BUG=258324

Review URL: https://codereview.chromium.org/18099004

git-svn-id: svn://svn.chromium.org/blink/trunk@175666 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 3b06a7a3
......@@ -162,11 +162,19 @@ public:
setRGBA(getAddr(x, y), r, g, b, a);
}
inline void setRGBA(PixelData* dest, unsigned r, unsigned g, unsigned b, unsigned a)
{
if (m_premultiplyAlpha)
setRGBAPremultiply(dest, r, g, b, a);
else
*dest = SkPackARGB32NoCheck(a, r, g, b);
}
static const unsigned div255 = static_cast<unsigned>(1.0 / 255 * (1 << 24)) + 1;
inline void setRGBA(PixelData* dest, unsigned r, unsigned g, unsigned b, unsigned a)
static inline void setRGBAPremultiply(PixelData* dest, unsigned r, unsigned g, unsigned b, unsigned a)
{
if (m_premultiplyAlpha && a < 255) {
if (a < 255) {
if (!a) {
*dest = 0;
return;
......@@ -183,7 +191,7 @@ public:
*dest = SkPackARGB32NoCheck(a, r, g, b);
}
inline void setRGBARaw(PixelData* dest, unsigned r, unsigned g, unsigned b, unsigned a)
static inline void setRGBARaw(PixelData* dest, unsigned r, unsigned g, unsigned b, unsigned a)
{
*dest = SkPackARGB32NoCheck(a, r, g, b);
}
......
......@@ -489,20 +489,33 @@ void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex,
}
#endif
// Write the decoded row pixels to the frame buffer.
// Write the decoded row pixels to the frame buffer. The repetitive
// form of the row write loops is for speed.
ImageFrame::PixelData* address = buffer.getAddr(0, y);
bool nonTrivialAlpha = false;
unsigned alphaMask = 255;
int width = size().width();
png_bytep pixel = row;
for (int x = 0; x < width; ++x, pixel += colorChannels) {
unsigned alpha = hasAlpha ? pixel[3] : 255;
buffer.setRGBA(address++, pixel[0], pixel[1], pixel[2], alpha);
nonTrivialAlpha |= alpha < 255;
if (hasAlpha) {
if (buffer.premultiplyAlpha()) {
for (int x = 0; x < width; ++x, pixel += 4) {
buffer.setRGBAPremultiply(address++, pixel[0], pixel[1], pixel[2], pixel[3]);
alphaMask &= pixel[3];
}
} else {
for (int x = 0; x < width; ++x, pixel += 4) {
buffer.setRGBARaw(address++, pixel[0], pixel[1], pixel[2], pixel[3]);
alphaMask &= pixel[3];
}
}
} else {
for (int x = 0; x < width; ++x, pixel += 3) {
buffer.setRGBARaw(address++, pixel[0], pixel[1], pixel[2], 255);
}
}
if (nonTrivialAlpha && !buffer.hasAlpha())
buffer.setHasAlpha(nonTrivialAlpha);
if (alphaMask != 255 && !buffer.hasAlpha())
buffer.setHasAlpha(true);
buffer.setPixelsChanged(true);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment