Specify bit depth and color space in SkImage::MakeFromPicture()
authorMatt Sarett <msarett@google.com>
Mon, 9 Jan 2017 17:38:59 +0000 (12:38 -0500)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Mon, 9 Jan 2017 18:14:06 +0000 (18:14 +0000)
BUG=skia:

Change-Id: I1d2a2b1f97557fc3e7ca6c2bdad6329f7760dbd2
Reviewed-on: https://skia-review.googlesource.com/6685
Commit-Queue: Matt Sarett <msarett@google.com>
Reviewed-by: Mike Reed <reed@google.com>
13 files changed:
gm/image.cpp
gm/image_pict.cpp
gm/image_shader.cpp
gm/pictureimagegenerator.cpp
gm/verylargebitmap.cpp
include/core/SkImage.h
include/core/SkImageGenerator.h
src/core/SkPictureImageGenerator.cpp
src/core/SkPictureShader.cpp
src/image/SkImage.cpp
tests/ImageIsOpaqueTest.cpp
tests/ImageTest.cpp
tests/SkResourceCacheTest.cpp

index 0bdd994..595a5ef 100644 (file)
@@ -246,7 +246,7 @@ static sk_sp<SkImage> make_picture(const SkImageInfo& info, GrContext*, void (*d
     SkPictureRecorder recorder;
     draw(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height())));
     return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
-                                    info.dimensions(), nullptr, nullptr,
+                                    info.dimensions(), nullptr, nullptr, SkImage::BitDepth::kU8,
                                     SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named));
 }
 
@@ -345,6 +345,7 @@ static SkImageGenerator* gen_picture(const SkImageInfo& info) {
     draw_opaque_contents(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height())));
     sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
     return SkImageGenerator::NewFromPicture(info.dimensions(), pict.get(), nullptr, nullptr,
+                                            SkImage::BitDepth::kU8,
                                             SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named));
 }
 
index 229f3c5..1fdff21 100644 (file)
@@ -66,11 +66,13 @@ protected:
 
         SkMatrix matrix;
         matrix.setTranslate(-100, -100);
-        fImage0 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr, srgbColorSpace);
+        fImage0 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr,
+                                           SkImage::BitDepth::kU8, srgbColorSpace);
         matrix.postTranslate(-50, -50);
         matrix.postRotate(45);
         matrix.postTranslate(50, 50);
-        fImage1 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr, srgbColorSpace);
+        fImage1 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr,
+                                           SkImage::BitDepth::kU8, srgbColorSpace);
     }
 
     void drawSet(SkCanvas* canvas) const {
@@ -109,6 +111,7 @@ static SkImageGenerator* make_pic_generator(GrContext*, SkPicture* pic) {
     SkMatrix matrix;
     matrix.setTranslate(-100, -100);
     return SkImageGenerator::NewFromPicture(SkISize::Make(100, 100), pic, &matrix, nullptr,
+                                            SkImage::BitDepth::kU8,
                                             SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named));
 }
 
index 7803739..522d373 100644 (file)
@@ -48,6 +48,7 @@ static sk_sp<SkImage> make_texture(GrContext* ctx, SkPicture* pic, const SkImage
 
 static sk_sp<SkImage> make_pict_gen(GrContext*, SkPicture* pic, const SkImageInfo& info) {
     return SkImage::MakeFromPicture(sk_ref_sp(pic), info.dimensions(), nullptr, nullptr,
+                                    SkImage::BitDepth::kU8,
                                     SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named));
 }
 
index b247af2..accf072 100644 (file)
@@ -156,7 +156,7 @@ protected:
             std::unique_ptr<SkImageGenerator> gen(
                 SkImageGenerator::NewFromPicture(configs[i].size, fPicture.get(), &m,
                                                  p.getAlpha() != 255 ? &p : nullptr,
-                                                 srgbColorSpace));
+                                                 SkImage::BitDepth::kU8, srgbColorSpace));
 
             SkImageInfo bmInfo = gen->getInfo().makeColorSpace(
                 sk_ref_sp(canvas->imageInfo().colorSpace()));
index 280d117..449b229 100644 (file)
@@ -33,6 +33,7 @@ static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2
     draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors);
     return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
                                     SkISize::Make(width, height), nullptr, nullptr,
+                                    SkImage::BitDepth::kU8,
                                     SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named));
 }
 
index 19ea8b8..7d282da 100644 (file)
@@ -154,15 +154,24 @@ public:
                                                    const SkISize nv12Sizes[2], GrSurfaceOrigin,
                                                    sk_sp<SkColorSpace> = nullptr);
 
-    /**
-     *  Create a new image from the specified picture. The color space becomes a preferred
-     *  color space for playback of the picture when retrieving the rasterized image contents.
-     */
-    static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture>, const SkISize& dimensions,
-                                          const SkMatrix*, const SkPaint*, sk_sp<SkColorSpace>);
+    enum class BitDepth {
+        kU8,
+        kF16,
+    };
 
+#ifdef SK_USE_LEGACY_MAKE_PICTURE_API
     static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
                                           const SkMatrix* matrix, const SkPaint* paint);
+#endif
+
+    /**
+     *  Create a new image from the specified picture.
+     *  Creating an SkImage from an SkPicture requires snapping the picture to a particular
+     *  BitDepth and SkColorSpace.
+     */
+    static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture>, const SkISize& dimensions,
+                                          const SkMatrix*, const SkPaint*, BitDepth,
+                                          sk_sp<SkColorSpace>);
 
     static sk_sp<SkImage> MakeTextureFromPixmap(GrContext*, const SkPixmap&, SkBudgeted budgeted);
 
index 0ab27ae..aec940f 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "SkBitmap.h"
 #include "SkColor.h"
+#include "SkImage.h"
 #include "SkImageInfo.h"
 #include "SkYUVSizeInfo.h"
 
@@ -19,8 +20,6 @@ class GrTexture;
 class GrSamplerParams;
 class SkBitmap;
 class SkData;
-class SkImage;
-class SkImageGenerator;
 class SkMatrix;
 class SkPaint;
 class SkPicture;
@@ -228,7 +227,7 @@ public:
      *  time.
      */
     static SkImageGenerator* NewFromPicture(const SkISize&, const SkPicture*, const SkMatrix*,
-                                            const SkPaint*, sk_sp<SkColorSpace>);
+                                            const SkPaint*, SkImage::BitDepth, sk_sp<SkColorSpace>);
 
     bool tryGenerateBitmap(SkBitmap* bm, const SkImageInfo& info, SkBitmap::Allocator* allocator);
 
index 4de49f0..3a4749b 100644 (file)
@@ -17,7 +17,7 @@
 class SkPictureImageGenerator : SkImageGenerator {
 public:
     static SkImageGenerator* Create(const SkISize&, const SkPicture*, const SkMatrix*,
-                                    const SkPaint*, sk_sp<SkColorSpace>);
+                                    const SkPaint*, SkImage::BitDepth, sk_sp<SkColorSpace>);
 
 protected:
     bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[],
@@ -42,20 +42,25 @@ private:
 
 SkImageGenerator* SkPictureImageGenerator::Create(const SkISize& size, const SkPicture* picture,
                                                   const SkMatrix* matrix, const SkPaint* paint,
+                                                  SkImage::BitDepth bitDepth,
                                                   sk_sp<SkColorSpace> colorSpace) {
-    if (!picture || size.isEmpty()) {
+    if (!picture || size.isEmpty() || !colorSpace) {
         return nullptr;
     }
 
-    SkColorType colorType;
-    if (!colorSpace || colorSpace->gammaCloseToSRGB()) {
-        colorType = kN32_SkColorType;
-    } else if (colorSpace->gammaIsLinear()) {
-        colorType = kRGBA_F16_SkColorType;
-    } else {
+    if (SkImage::BitDepth::kF16 == bitDepth && !colorSpace->gammaIsLinear()) {
+        return nullptr;
+    }
+
+    if (!colorSpace->gammaCloseToSRGB() && !colorSpace->gammaIsLinear()) {
         return nullptr;
     }
 
+    SkColorType colorType = kN32_SkColorType;
+    if (SkImage::BitDepth::kF16 == bitDepth) {
+        colorType = kRGBA_F16_SkColorType;
+    }
+
     SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType,
                                          kPremul_SkAlphaType, std::move(colorSpace));
     return new SkPictureImageGenerator(info, picture, matrix, paint);
@@ -134,8 +139,10 @@ bool SkPictureImageGenerator::onGenerateScaledPixels(const SkPixmap& scaledPixel
 
 SkImageGenerator* SkImageGenerator::NewFromPicture(const SkISize& size, const SkPicture* picture,
                                                    const SkMatrix* matrix, const SkPaint* paint,
+                                                   SkImage::BitDepth bitDepth,
                                                    sk_sp<SkColorSpace> colorSpace) {
-    return SkPictureImageGenerator::Create(size, picture, matrix, paint, std::move(colorSpace));
+    return SkPictureImageGenerator::Create(size, picture, matrix, paint, bitDepth,
+                                           std::move(colorSpace));
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
index e34ca99..1f20623 100644 (file)
@@ -227,7 +227,7 @@ sk_sp<SkShader> SkPictureShader::refBitmapShader(const SkMatrix& viewMatrix, con
 
         sk_sp<SkImage> tileImage(
             SkImage::MakeFromPicture(fPicture, tileSize, &tileMatrix, nullptr,
-                                     sk_ref_sp(dstColorSpace)));
+                                     SkImage::BitDepth::kU8, sk_ref_sp(dstColorSpace)));
         if (!tileImage) {
             return nullptr;
         }
index 8a4fb29..f49a2ce 100644 (file)
@@ -299,21 +299,21 @@ bool SkImage_Base::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) con
     return true;
 }
 
+#ifdef SK_USE_LEGACY_MAKE_PICTURE_API
 sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
-                                        const SkMatrix* matrix, const SkPaint* paint,
-                                        sk_sp<SkColorSpace> colorSpace) {
-    if (!picture) {
-        return nullptr;
-    }
-    return MakeFromGenerator(SkImageGenerator::NewFromPicture(dimensions, picture.get(), matrix,
-                                                              paint, std::move(colorSpace)));
+                                        const SkMatrix* matrix, const SkPaint* paint) {
+    return SkImage::MakeFromPicture(std::move(picture), dimensions, matrix, paint, BitDepth::kU8,
+                                    SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named));
 }
+#endif
 
 sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
-                                        const SkMatrix* matrix, const SkPaint* paint) {
-    return MakeFromPicture(std::move(picture), dimensions, matrix, paint, nullptr);
+                                        const SkMatrix* matrix, const SkPaint* paint,
+                                        BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace) {
+    return MakeFromGenerator(SkImageGenerator::NewFromPicture(dimensions, picture.get(), matrix,
+                                                              paint, bitDepth,
+                                                              std::move(colorSpace)));
 }
-
 sk_sp<SkImage> SkImage::makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
                                        const SkIRect& clipBounds, SkIRect* outSubset,
                                        SkIPoint* offset) const {
index 21ddb5d..cbbe331 100644 (file)
@@ -125,6 +125,7 @@ DEF_TEST(Image_isAlphaOnly, reporter) {
         GetResourceAsImage("mandrill_128.png"),
         GetResourceAsImage("color_wheel.jpg"),
         SkImage::MakeFromPicture(make_picture(), { 10, 10 }, nullptr, nullptr,
+                                 SkImage::BitDepth::kU8,
                                  SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named)),
     })
     {
index 161dff8..4f9d944 100644 (file)
@@ -130,7 +130,7 @@ static sk_sp<SkImage> create_picture_image() {
     SkCanvas* canvas = recorder.beginRecording(10, 10);
     canvas->clear(SK_ColorCYAN);
     return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
-                                    nullptr, nullptr,
+                                    nullptr, nullptr, SkImage::BitDepth::kU8,
                                     SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named));
 };
 #endif
index ed2ff74..922acef 100644 (file)
@@ -330,6 +330,7 @@ DEF_TEST(BitmapCache_discarded_image, reporter) {
             canvas->clear(SK_ColorCYAN);
             return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
                                             SkISize::Make(10, 10), nullptr, nullptr,
+                                            SkImage::BitDepth::kU8,
                                             SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named));
         });
     }