Make BRD take advantage of zero initialized memory
authormsarett <msarett@google.com>
Wed, 11 Nov 2015 21:30:43 +0000 (13:30 -0800)
committerCommit bot <commit-bot@chromium.org>
Wed, 11 Nov 2015 21:30:43 +0000 (13:30 -0800)
This is the third step in a three part change:
(1) Skia: Add SkBRDAllocator.
(2) Android: Make JavaPixelAllocator and RecyclingClippingPixelAllocator
             implement SkBRDAllocator.
(3) Skia: Change SkBitmapRegionDecoder to use SkBRDAllocator and take
          advantage of zero allocated memory when possible.

BUG=skia:

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

tools/android/SkBitmapRegionCanvas.cpp
tools/android/SkBitmapRegionCanvas.h
tools/android/SkBitmapRegionCodec.cpp
tools/android/SkBitmapRegionCodec.h
tools/android/SkBitmapRegionDecoder.h

index ffcab95..bac5dc1 100644 (file)
@@ -15,7 +15,7 @@ SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder)
     , fDecoder(decoder)
 {}
 
-bool SkBitmapRegionCanvas::decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* allocator,
+bool SkBitmapRegionCanvas::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
         const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
         bool requireUnpremul) {
     // Reject color types not supported by this method
@@ -102,11 +102,12 @@ bool SkBitmapRegionCanvas::decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* a
     // TODO (msarett): Can we make this faster by implementing it to only
     //                 zero parts of the image that we won't overwrite with
     //                 pixels?
-    // TODO (msarett): This could be skipped if memory is zero initialized.
-    //                 This would matter if this code is moved to Android and
-    //                 uses Android bitmaps.
     if (SubsetType::kPartiallyInside_SubsetType == type) {
-        bitmap->eraseColor(0);
+        SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
+                    SkCodec::kNo_ZeroInitialized;
+        if (SkCodec::kNo_ZeroInitialized == zeroInit) {
+            bitmap->eraseColor(0);
+        }
     }
 
     // Use a canvas to crop and scale to the destination bitmap
index f41d5b9..2edbf1f 100644 (file)
@@ -24,7 +24,7 @@ public:
      */
     SkBitmapRegionCanvas(SkCodec* decoder);
 
-    bool decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* allocator,
+    bool decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
                       const SkIRect& desiredSubset, int sampleSize,
                       SkColorType colorType, bool requireUnpremul) override;
 
index 93ece4a..7c3c247 100644 (file)
@@ -16,7 +16,7 @@ SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec)
     , fCodec(codec)
 {}
 
-bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* allocator,
+bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
         const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
         bool requireUnpremul) {
 
@@ -104,10 +104,10 @@ bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* al
     // TODO (msarett): Can we make this faster by implementing it to only
     //                 zero parts of the image that we won't overwrite with
     //                 pixels?
-    // TODO (msarett): This could be skipped if memory is zero initialized.
-    //                 This would matter if this code is moved to Android and
-    //                 uses Android bitmaps.
-    if (SubsetType::kPartiallyInside_SubsetType == type) {
+    SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
+            SkCodec::kNo_ZeroInitialized;
+    if (SubsetType::kPartiallyInside_SubsetType == type &&
+            SkCodec::kNo_ZeroInitialized == zeroInit) {
         void* pixels = bitmap->getPixels();
         size_t bytes = outInfo.getSafeSize(bitmap->rowBytes());
         memset(pixels, 0, bytes);
@@ -119,6 +119,7 @@ bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* al
     options.fSubset = &subset;
     options.fColorPtr = colorPtr;
     options.fColorCount = colorCountPtr;
+    options.fZeroInitialized = zeroInit;
     void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
 
     // FIXME: skbug.com/4538
index 1739a04..7977417 100644 (file)
@@ -20,7 +20,7 @@ public:
      */
     SkBitmapRegionCodec(SkAndroidCodec* codec);
 
-    bool decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* allocator,
+    bool decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
                       const SkIRect& desiredSubset, int sampleSize,
                       SkColorType colorType, bool requireUnpremul) override;
 
index 294adc3..575ad9d 100644 (file)
@@ -9,6 +9,7 @@
 #define SkBitmapRegionDecoder_DEFINED
 
 #include "SkBitmap.h"
+#include "SkBRDAllocator.h"
 #include "SkEncodedFormat.h"
 #include "SkStream.h"
 
@@ -58,7 +59,7 @@ public:
      *                        alpha type to use.
      *
      */
-    virtual bool decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* allocator,
+    virtual bool decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
                               const SkIRect& desiredSubset, int sampleSize,
                               SkColorType colorType, bool requireUnpremul) = 0;
     /*