diff options
-rw-r--r-- | gm/readpixels.cpp | 170 |
1 files changed, 167 insertions, 3 deletions
diff --git a/gm/readpixels.cpp b/gm/readpixels.cpp index 024cb43362..df7fb0751a 100644 --- a/gm/readpixels.cpp +++ b/gm/readpixels.cpp @@ -12,6 +12,7 @@ #include "SkColorSpace_Base.h" #include "SkHalf.h" #include "SkImage.h" +#include "SkPictureRecorder.h" static void clamp_if_necessary(const SkImageInfo& info, void* pixels) { if (kRGBA_F16_SkColorType != info.colorType()) { @@ -62,6 +63,39 @@ static sk_sp<SkImage> make_raster_image(SkColorType colorType, SkAlphaType alpha return SkImage::MakeFromBitmap(bitmap); } +static sk_sp<SkImage> make_codec_image() { + sk_sp<SkData> encoded = GetResourceAsData("randPixels.png"); + return SkImage::MakeFromEncoded(encoded); +} + +static void draw_contents(SkCanvas* canvas) { + SkPaint paint; + paint.setStyle(SkPaint::kStroke_Style); + paint.setStrokeWidth(20); + paint.setColor(0xFF800000); + canvas->drawCircle(40, 40, 35, paint); + paint.setColor(0xFF008000); + canvas->drawCircle(50, 50, 35, paint); + paint.setColor(0xFF000080); + canvas->drawCircle(60, 60, 35, paint); +} + +static sk_sp<SkImage> make_tagged_picture_image() { + SkPictureRecorder recorder; + draw_contents(recorder.beginRecording(SkRect::MakeIWH(kWidth, kHeight))); + return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), + SkISize::Make(kWidth, kHeight), nullptr, nullptr, + SkImage::BitDepth::kU8, + SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named)); +} + +static sk_sp<SkImage> make_untagged_picture_image() { + SkPictureRecorder recorder; + draw_contents(recorder.beginRecording(SkRect::MakeIWH(kWidth, kHeight))); + return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), + SkISize::Make(kWidth, kHeight), nullptr, nullptr); +} + static sk_sp<SkColorSpace> make_srgb_transfer_fn(const SkColorSpacePrimaries& primaries) { SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor); SkAssertResult(primaries.toXYZD50(&toXYZD50)); @@ -96,13 +130,14 @@ static sk_sp<SkColorSpace> make_small_gamut() { } static void draw_image(SkCanvas* canvas, SkImage* image, SkColorType dstColorType, - SkAlphaType dstAlphaType, sk_sp<SkColorSpace> dstColorSpace) { + SkAlphaType dstAlphaType, sk_sp<SkColorSpace> dstColorSpace, + SkImage::CachingHint hint) { size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(dstColorType); sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height()); dstColorSpace = fix_for_colortype(dstColorSpace.get(), dstColorType); SkImageInfo dstInfo = SkImageInfo::Make(image->width(), image->height(), dstColorType, dstAlphaType, dstColorSpace); - image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0); + image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, hint); // readPixels() does not always clamp F16. The drawing code expects pixels in the 0-1 range. clamp_if_necessary(dstInfo, data->writable_data()); @@ -156,7 +191,7 @@ protected: for (SkColorType dstColorType : colorTypes) { for (SkAlphaType dstAlphaType : alphaTypes) { draw_image(canvas, image.get(), dstColorType, dstAlphaType, - dstColorSpace); + dstColorSpace, SkImage::kAllow_CachingHint); canvas->translate((float) kWidth, 0.0f); } } @@ -171,3 +206,132 @@ private: typedef skiagm::GM INHERITED; }; DEF_GM( return new ReadPixelsGM; ) + +class ReadPixelsCodecGM : public skiagm::GM { +public: + ReadPixelsCodecGM() {} + +protected: + SkString onShortName() override { + return SkString("readpixelscodec"); + } + + SkISize onISize() override { + return SkISize::Make(3 * (kEncodedWidth + 1), 12 * (kEncodedHeight + 1)); + } + + void onDraw(SkCanvas* canvas) override { + if (!canvas->imageInfo().colorSpace()) { + // This gm is only interesting in color correct modes. + return; + } + + const SkAlphaType alphaTypes[] = { + kUnpremul_SkAlphaType, + kPremul_SkAlphaType, + }; + const SkColorType colorTypes[] = { + kRGBA_8888_SkColorType, + kBGRA_8888_SkColorType, + kRGBA_F16_SkColorType, + }; + const sk_sp<SkColorSpace> colorSpaces[] = { + make_wide_gamut(), + SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named), + make_small_gamut(), + }; + const SkImage::CachingHint hints[] = { + SkImage::kAllow_CachingHint, + SkImage::kDisallow_CachingHint, + }; + + sk_sp<SkImage> image = make_codec_image(); + for (sk_sp<SkColorSpace> dstColorSpace : colorSpaces) { + canvas->save(); + for (SkColorType dstColorType : colorTypes) { + for (SkAlphaType dstAlphaType : alphaTypes) { + for (SkImage::CachingHint hint : hints) { + draw_image(canvas, image.get(), dstColorType, dstAlphaType, dstColorSpace, + hint); + canvas->translate(0.0f, (float) kEncodedHeight + 1); + } + } + } + canvas->restore(); + canvas->translate((float) kEncodedWidth + 1, 0.0f); + } + } + +private: + static const int kEncodedWidth = 8; + static const int kEncodedHeight = 8; + + typedef skiagm::GM INHERITED; +}; +DEF_GM( return new ReadPixelsCodecGM; ) + +class ReadPixelsPictureGM : public skiagm::GM { +public: + ReadPixelsPictureGM() {} + +protected: + SkString onShortName() override { + return SkString("readpixelspicture"); + } + + SkISize onISize() override { + return SkISize::Make(3 * kWidth, 12 * kHeight); + } + + void onDraw(SkCanvas* canvas) override { + if (!canvas->imageInfo().colorSpace()) { + // This gm is only interesting in color correct modes. + return; + } + + const sk_sp<SkImage> images[] = { + make_tagged_picture_image(), + make_untagged_picture_image(), + }; + const SkAlphaType alphaTypes[] = { + kUnpremul_SkAlphaType, + kPremul_SkAlphaType, + }; + const SkColorType colorTypes[] = { + kRGBA_8888_SkColorType, + kBGRA_8888_SkColorType, + kRGBA_F16_SkColorType, + }; + const sk_sp<SkColorSpace> colorSpaces[] = { + make_wide_gamut(), + SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named), + make_small_gamut(), + }; + const SkImage::CachingHint hints[] = { + SkImage::kAllow_CachingHint, + SkImage::kDisallow_CachingHint, + }; + + for (sk_sp<SkImage> image : images) { + for (sk_sp<SkColorSpace> dstColorSpace : colorSpaces) { + canvas->save(); + for (SkColorType dstColorType : colorTypes) { + for (SkAlphaType dstAlphaType : alphaTypes) { + for (SkImage::CachingHint hint : hints) { + draw_image(canvas, image.get(), dstColorType, dstAlphaType, + dstColorSpace, hint); + canvas->translate(0.0f, (float) kHeight); + } + } + } + canvas->restore(); + canvas->translate((float) kWidth, 0.0f); + } + } + } + +private: + + typedef skiagm::GM INHERITED; +}; +DEF_GM( return new ReadPixelsPictureGM; ) |