Add NewFromStream and getEncodedFormat to BitmapRegionDecoder
authormsarett <msarett@google.com>
Tue, 27 Oct 2015 20:12:59 +0000 (13:12 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 27 Oct 2015 20:12:59 +0000 (13:12 -0700)
The function will be needed in Android.

BUG=skia:

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

tools/SkBitmapRegionCanvas.h
tools/SkBitmapRegionCodec.h
tools/SkBitmapRegionDecoderInterface.cpp
tools/SkBitmapRegionDecoderInterface.h
tools/SkBitmapRegionSampler.h

index 217082a..0dfd5b8 100644 (file)
@@ -30,6 +30,8 @@ public:
 
     bool conversionSupported(SkColorType colorType) override;
 
+    SkEncodedFormat getEncodedFormat() override { return fDecoder->getEncodedFormat(); }
+
 private:
 
     SkAutoTDelete<SkCodec> fDecoder;
index 4dd69ab..ade72fa 100644 (file)
@@ -26,6 +26,8 @@ public:
 
     bool conversionSupported(SkColorType colorType) override;
 
+    SkEncodedFormat getEncodedFormat() override { return fCodec->getEncodedFormat(); }
+
 private:
 
     SkAutoTDelete<SkAndroidCodec> fCodec;
index ec6327e..5941538 100644 (file)
 
 SkBitmapRegionDecoderInterface* SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(
         SkData* data, Strategy strategy) {
+    return SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(new SkMemoryStream(data),
+            strategy);
+}
+
+SkBitmapRegionDecoderInterface* SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(
+        SkStreamRewindable* stream, Strategy strategy) {
+    SkAutoTDelete<SkStreamRewindable> streamDeleter(stream);
     switch (strategy) {
         case kOriginal_Strategy: {
-            SkAutoTDelete<SkStreamRewindable> stream(new SkMemoryStream(data));
-            SkImageDecoder* decoder = SkImageDecoder::Factory(stream);
+            SkImageDecoder* decoder = SkImageDecoder::Factory(streamDeleter);
             int width, height;
             if (nullptr == decoder) {
                 SkCodecPrintf("Error: Could not create image decoder.\n");
                 return nullptr;
             }
-            if (!decoder->buildTileIndex(stream.detach(), &width, &height)) {
+            if (!decoder->buildTileIndex(streamDeleter.detach(), &width, &height)) {
                 SkCodecPrintf("Error: Could not build tile index.\n");
                 delete decoder;
                 return nullptr;
@@ -33,7 +39,7 @@ SkBitmapRegionDecoderInterface* SkBitmapRegionDecoderInterface::CreateBitmapRegi
             return new SkBitmapRegionSampler(decoder, width, height);
         }
         case kCanvas_Strategy: {
-            SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
+            SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(streamDeleter.detach()));
             if (nullptr == codec) {
                 SkCodecPrintf("Error: Failed to create decoder.\n");
                 return nullptr;
@@ -49,7 +55,8 @@ SkBitmapRegionDecoderInterface* SkBitmapRegionDecoderInterface::CreateBitmapRegi
             return new SkBitmapRegionCanvas(codec.detach());
         }
         case kAndroidCodec_Strategy: {
-            SkAutoTDelete<SkAndroidCodec> codec = SkAndroidCodec::NewFromData(data);
+            SkAutoTDelete<SkAndroidCodec> codec =
+                    SkAndroidCodec::NewFromStream(streamDeleter.detach());
             if (NULL == codec) {
                 SkCodecPrintf("Error: Failed to create codec.\n");
                 return NULL;
index a6a3111..8e19e95 100644 (file)
@@ -9,6 +9,7 @@
 #define SkBitmapRegionDecoder_DEFINED
 
 #include "SkBitmap.h"
+#include "SkEncodedFormat.h"
 #include "SkStream.h"
 
 /*
@@ -33,6 +34,14 @@ public:
             SkData* data, Strategy strategy);
 
     /*
+     * @param stream   Takes ownership of the stream
+     * @param strategy Strategy used for scaling and subsetting
+     * @return         Tries to create an SkBitmapRegionDecoder, returns NULL on failure
+     */
+    static SkBitmapRegionDecoderInterface* CreateBitmapRegionDecoder(
+            SkStreamRewindable* stream, Strategy strategy);
+
+    /*
      * Decode a scaled region of the encoded image stream
      *
      * @param bitmap          Container for decoded pixels.  It is assumed that the pixels
@@ -59,6 +68,8 @@ public:
      */
     virtual bool conversionSupported(SkColorType colorType) = 0;
 
+    virtual SkEncodedFormat getEncodedFormat() = 0;
+
     int width() const { return fWidth; }
     int height() const { return fHeight; }
 
index be2d239..48bb23f 100644 (file)
@@ -34,6 +34,8 @@ public:
         return true;
     }
 
+    SkEncodedFormat getEncodedFormat() override { return (SkEncodedFormat) fDecoder->getFormat(); }
+
 private:
 
     SkAutoTDelete<SkImageDecoder> fDecoder;