SkImage::NewFromGenerator(SkImageGenerator*), and a unit test.
authorhalcanary <halcanary@google.com>
Tue, 12 Aug 2014 15:04:58 +0000 (08:04 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 12 Aug 2014 15:04:58 +0000 (08:04 -0700)
R=reed@google.com

Author: halcanary@google.com

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

gyp/core.gyp
include/core/SkImage.h
src/image/SkImage_Raster.cpp
tests/CachedDecodingPixelRefTest.cpp

index 64cc79b..afbc78a 100644 (file)
@@ -20,6 +20,7 @@
         '../include/ports',
         '../include/utils',
         '../include/xml',
+        '../include/images',
         '../src/core',
         '../src/sfnt',
         '../src/image',
index 581129e..1316e2b 100644 (file)
@@ -16,6 +16,7 @@
 
 class SkData;
 class SkCanvas;
+class SkImageGenerator;
 class SkPaint;
 class GrContext;
 class GrTexture;
@@ -47,6 +48,13 @@ public:
      */
     static SkImage* NewTexture(const SkBitmap&);
 
+    /**
+     *  Construct a new SkImage based on the given ImageGenerator.
+     *  This function will always take ownership of the passed
+     *  ImageGenerator.  Returns NULL on error.
+     */
+    static SkImage* NewFromGenerator(SkImageGenerator*);
+
     int width() const { return fWidth; }
     int height() const { return fHeight; }
     uint32_t uniqueID() const { return fUniqueID; }
index e4768af..a1cd602 100644 (file)
@@ -10,6 +10,7 @@
 #include "SkBitmap.h"
 #include "SkCanvas.h"
 #include "SkData.h"
+#include "SkDecodingImageGenerator.h"
 #include "SkMallocPixelRef.h"
 
 class SkImage_Raster : public SkImage_Base {
@@ -69,6 +70,10 @@ public:
                                   SkShader::TileMode,
                                   const SkMatrix* localMatrix) const SK_OVERRIDE;
 
+    SkImage_Raster(const SkBitmap& bm)
+        : INHERITED(bm.width(), bm.height())
+        , fBitmap(bm) {}
+
 private:
     SkImage_Raster() : INHERITED(0, 0) {}
 
@@ -198,6 +203,14 @@ SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro
     return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes));
 }
 
+SkImage* SkImage::NewFromGenerator(SkImageGenerator* generator) {
+    SkBitmap bitmap;
+    if (!SkInstallDiscardablePixelRef(generator, &bitmap)) {
+        return NULL;
+    }
+    return SkNEW_ARGS(SkImage_Raster, (bitmap));
+}
+
 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
                                 size_t rowBytes) {
     return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes));
index 8de7da7..b409987 100644 (file)
@@ -322,3 +322,42 @@ DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
     check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
                    reporter, kSkDiscardable_PixelRefType, globalPool);
 }
+
+////////////////////////////////////////////////////////////////////////////////
+
+DEF_TEST(Image_NewFromGenerator, r) {
+    TestImageGenerator::TestType testTypes[] = {
+        TestImageGenerator::kFailGetInfo_TestType,
+        TestImageGenerator::kFailGetPixels_TestType,
+        TestImageGenerator::kSucceedGetPixels_TestType,
+    };
+    for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
+        TestImageGenerator::TestType test = testTypes[i];
+        SkImageGenerator* gen = SkNEW_ARGS(TestImageGenerator, (test, r));
+        SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen));
+        if (TestImageGenerator::kFailGetInfo_TestType == test) {
+            REPORTER_ASSERT(r, NULL == image.get());
+            continue;
+        }
+        if (NULL == image.get()) {
+            ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
+                   SK_SIZE_T_SPECIFIER "]", i);
+            continue;
+        }
+        REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
+        REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
+
+        SkBitmap bitmap;
+        SkAssertResult(bitmap.allocN32Pixels(TestImageGenerator::Width(),
+                                             TestImageGenerator::Height()));
+        SkCanvas canvas(bitmap);
+        canvas.clear(SK_ColorMAGENTA);
+        image->draw(&canvas, 0, 0, NULL);
+        SkColor color = bitmap.getColor(0, 0);
+        if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
+            REPORTER_ASSERT(r, TestImageGenerator::Color() == color);
+        } else {
+            REPORTER_ASSERT(r, SK_ColorMAGENTA == color);
+        }
+    }
+}