Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1448)

Unified Diff: Source/core/platform/image-decoders/png/PNGImageDecoder.cpp

Issue 17448010: Speed up png decoding by eliminating branches from the core loop that fills ImageFrame with pixels (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/platform/image-decoders/png/PNGImageDecoder.cpp
diff --git a/Source/core/platform/image-decoders/png/PNGImageDecoder.cpp b/Source/core/platform/image-decoders/png/PNGImageDecoder.cpp
index 828328b956c2bb0c8d8d8de534228c2abee60223..e2bd53dc124a194128f8673275019c11138bba9b 100644
--- a/Source/core/platform/image-decoders/png/PNGImageDecoder.cpp
+++ b/Source/core/platform/image-decoders/png/PNGImageDecoder.cpp
@@ -489,18 +489,30 @@ void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex,
// Write the decoded row pixels to the frame buffer.
ImageFrame::PixelData* address = buffer.getAddr(0, y);
- bool nonTrivialAlpha = false;
+ unsigned alphaMask = 255;
int width = size().width();
+ // Do not merge the loops below. They are hand rolled for each case for maximal performance.
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 + x, pixel[0], pixel[1], pixel[2], pixel[3]);
+ alphaMask &= pixel[3];
+ }
+ } else {
+ for (int x = 0; x < width; ++x, pixel += 4) {
+ buffer.setRGBARaw(address + x, pixel[0], pixel[1], pixel[2], pixel[3]);
+ alphaMask &= pixel[3];
+ }
+ }
+ } else {
+ for (int x = 0; x < width; ++x, pixel += 3)
+ buffer.setRGB(address + x, pixel[0], pixel[1], pixel[2]);
}
- if (nonTrivialAlpha && !buffer.hasAlpha())
- buffer.setHasAlpha(nonTrivialAlpha);
+ bool hasNonTrivialAlpha = alphaMask != 255;
+ buffer.setHasAlpha(hasNonTrivialAlpha);
kbalazs_ 2013/06/25 13:20:18 Oops, this war wrong, we cannot make !hasAlpha() j
}
void PNGImageDecoder::pngComplete()

Powered by Google App Engine
This is Rietveld 408576698