From 18fdfe83a5447298c137842ade7693526aeb7623 Mon Sep 17 00:00:00 2001 From: Ian Prest Date: Wed, 1 Jun 2022 14:06:10 -0700 Subject: [PATCH] Add SkSurfaceProps parameter to several APIs In general, SkSurfaceProps is only needed when rendering text. However, there are several existing APIs that don't allow SkSurfaceProps to be passed in by the user. This change adds new SkSurfaceProps parameters to several public-facing APIs: 1. SkRasterHandleAllocator::MakeCanvas -- The props are used by the canvas whenever text is rendered. 2. SkImage::MakeFromPicture and SkImageGenerator::MakeFromPicture -- The props are used to render any text in the SkPicture object. Change-Id: Ic48e8a30bb12b3170415c644de1a007b5eefb818 Bug: skia:13369 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/545396 Reviewed-by: Ben Wagner Reviewed-by: Brian Osman Commit-Queue: Brian Osman --- RELEASE_NOTES.txt | 3 +++ include/core/SkCanvas.h | 2 +- include/core/SkImage.h | 6 ++++-- include/core/SkImageGenerator.h | 4 +++- include/core/SkRasterHandleAllocator.h | 4 +++- src/core/SkCanvas.cpp | 12 ++++++++---- src/core/SkPictureImageGenerator.cpp | 21 +++++++++++---------- src/image/SkImage.cpp | 5 +++-- 8 files changed, 36 insertions(+), 21 deletions(-) diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 2d06d120b4..b9c448c5c2 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -5,6 +5,9 @@ This file includes a list of high level updates for each milestone release. Milestone 104 ------------- * SkRuntimeEffect takes and returns a const SkData. + * SkRasterHandleAllocator::MakeCanvas now takes optional SkSurfaceProps. + * SkImage::MakeFromPicture and SkImageGenerator::MakeFromPicture now take an optional + SkSurfaceProps to use when rasterizing the picture. * * * diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h index 0fcaea5811..72b2d17b7b 100644 --- a/include/core/SkCanvas.h +++ b/include/core/SkCanvas.h @@ -2418,7 +2418,7 @@ protected: SkCanvas(const SkIRect& bounds); private: SkCanvas(const SkBitmap&, std::unique_ptr, - SkRasterHandleAllocator::Handle); + SkRasterHandleAllocator::Handle, const SkSurfaceProps* props); SkCanvas(SkCanvas&&) = delete; SkCanvas(const SkCanvas&) = delete; diff --git a/include/core/SkImage.h b/include/core/SkImage.h index 848c5b5c57..63d455ca13 100644 --- a/include/core/SkImage.h +++ b/include/core/SkImage.h @@ -13,6 +13,7 @@ #include "include/core/SkSamplingOptions.h" #include "include/core/SkScalar.h" #include "include/core/SkShader.h" +#include "include/core/SkSurfaceProps.h" #include "include/core/SkTileMode.h" #if SK_SUPPORT_GPU #include "include/gpu/GrTypes.h" @@ -253,12 +254,13 @@ public: @param paint SkPaint to apply transparency, filtering, and so on; may be nullptr @param bitDepth 8-bit integer or 16-bit float: per component @param colorSpace range of colors; may be nullptr + @param props props to use when rasterizing the picture @return created SkImage, or nullptr */ static sk_sp MakeFromPicture(sk_sp picture, const SkISize& dimensions, const SkMatrix* matrix, const SkPaint* paint, - BitDepth bitDepth, - sk_sp colorSpace); + BitDepth bitDepth, sk_sp colorSpace, + SkSurfaceProps props = {}); #if SK_SUPPORT_GPU /** Creates a GPU-backed SkImage from compressed data. diff --git a/include/core/SkImageGenerator.h b/include/core/SkImageGenerator.h index 970ccdbab9..47b8240432 100644 --- a/include/core/SkImageGenerator.h +++ b/include/core/SkImageGenerator.h @@ -12,6 +12,7 @@ #include "include/core/SkColor.h" #include "include/core/SkImage.h" #include "include/core/SkImageInfo.h" +#include "include/core/SkSurfaceProps.h" #include "include/core/SkYUVAPixmaps.h" #include @@ -169,7 +170,8 @@ public: static std::unique_ptr MakeFromPicture(const SkISize&, sk_sp, const SkMatrix*, const SkPaint*, SkImage::BitDepth, - sk_sp); + sk_sp, + SkSurfaceProps props = {}); protected: static constexpr int kNeedNewImageUniqueID = 0; diff --git a/include/core/SkRasterHandleAllocator.h b/include/core/SkRasterHandleAllocator.h index ad7c379eef..6fe121a6de 100644 --- a/include/core/SkRasterHandleAllocator.h +++ b/include/core/SkRasterHandleAllocator.h @@ -13,6 +13,7 @@ class SkBitmap; class SkCanvas; class SkMatrix; +class SkSurfaceProps; /** * If a client wants to control the allocation of raster layers in a canvas, it should subclass @@ -76,7 +77,8 @@ public: * If rec is null, then the allocator will be called for the base-layer as well. */ static std::unique_ptr MakeCanvas(std::unique_ptr, - const SkImageInfo&, const Rec* rec = nullptr); + const SkImageInfo&, const Rec* rec = nullptr, + const SkSurfaceProps* props = nullptr); protected: SkRasterHandleAllocator() = default; diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp index fd4ea2763a..a0855f6bcd 100644 --- a/src/core/SkCanvas.cpp +++ b/src/core/SkCanvas.cpp @@ -465,8 +465,10 @@ SkCanvas::SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props) SkCanvas::SkCanvas(const SkBitmap& bitmap, std::unique_ptr alloc, - SkRasterHandleAllocator::Handle hndl) + SkRasterHandleAllocator::Handle hndl, + const SkSurfaceProps* props) : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) + , fProps(SkSurfacePropsCopyOrDefault(props)) , fAllocator(std::move(alloc)) { inc_canvas(); @@ -474,7 +476,7 @@ SkCanvas::SkCanvas(const SkBitmap& bitmap, this->init(device); } -SkCanvas::SkCanvas(const SkBitmap& bitmap) : SkCanvas(bitmap, nullptr, nullptr) {} +SkCanvas::SkCanvas(const SkBitmap& bitmap) : SkCanvas(bitmap, nullptr, nullptr, nullptr) {} #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK SkCanvas::SkCanvas(const SkBitmap& bitmap, ColorBehavior) @@ -2868,7 +2870,8 @@ SkRasterHandleAllocator::Handle SkRasterHandleAllocator::allocBitmap(const SkIma std::unique_ptr SkRasterHandleAllocator::MakeCanvas(std::unique_ptr alloc, - const SkImageInfo& info, const Rec* rec) { + const SkImageInfo& info, const Rec* rec, + const SkSurfaceProps* props) { if (!alloc || !SkSurfaceValidateRasterInfo(info, rec ? rec->fRowBytes : kIgnoreRowBytesValue)) { return nullptr; } @@ -2881,7 +2884,8 @@ SkRasterHandleAllocator::MakeCanvas(std::unique_ptr all } else { hndl = alloc->allocBitmap(info, &bm); } - return hndl ? std::unique_ptr(new SkCanvas(bm, std::move(alloc), hndl)) : nullptr; + return hndl ? std::unique_ptr(new SkCanvas(bm, std::move(alloc), hndl, props)) + : nullptr; } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/SkPictureImageGenerator.cpp b/src/core/SkPictureImageGenerator.cpp index 3aba369bc6..8c142e7985 100644 --- a/src/core/SkPictureImageGenerator.cpp +++ b/src/core/SkPictureImageGenerator.cpp @@ -18,7 +18,7 @@ class SkPictureImageGenerator : public SkImageGenerator { public: SkPictureImageGenerator(const SkImageInfo& info, sk_sp, const SkMatrix*, - const SkPaint*); + const SkPaint*, const SkSurfaceProps& props); protected: bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options& opts) @@ -33,6 +33,7 @@ private: sk_sp fPicture; SkMatrix fMatrix; SkTLazy fPaint; + SkSurfaceProps fProps; using INHERITED = SkImageGenerator; }; @@ -42,7 +43,8 @@ private: std::unique_ptr SkImageGenerator::MakeFromPicture(const SkISize& size, sk_sp picture, const SkMatrix* matrix, const SkPaint* paint, - SkImage::BitDepth bitDepth, sk_sp colorSpace) { + SkImage::BitDepth bitDepth, sk_sp colorSpace, + SkSurfaceProps props) { if (!picture || !colorSpace || size.isEmpty()) { return nullptr; } @@ -55,15 +57,17 @@ SkImageGenerator::MakeFromPicture(const SkISize& size, sk_sp picture, SkImageInfo info = SkImageInfo::Make(size, colorType, kPremul_SkAlphaType, std::move(colorSpace)); return std::unique_ptr( - new SkPictureImageGenerator(info, std::move(picture), matrix, paint)); + new SkPictureImageGenerator(info, std::move(picture), matrix, paint, props)); } /////////////////////////////////////////////////////////////////////////////////////////////////// SkPictureImageGenerator::SkPictureImageGenerator(const SkImageInfo& info, sk_sp picture, - const SkMatrix* matrix, const SkPaint* paint) + const SkMatrix* matrix, const SkPaint* paint, + const SkSurfaceProps& props) : INHERITED(info) - , fPicture(std::move(picture)) { + , fPicture(std::move(picture)) + , fProps(props) { if (matrix) { fMatrix = *matrix; @@ -78,8 +82,7 @@ SkPictureImageGenerator::SkPictureImageGenerator(const SkImageInfo& info, sk_sp< bool SkPictureImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options& opts) { - SkSurfaceProps props(0, kUnknown_SkPixelGeometry); - std::unique_ptr canvas = SkCanvas::MakeRasterDirect(info, pixels, rowBytes, &props); + std::unique_ptr canvas = SkCanvas::MakeRasterDirect(info, pixels, rowBytes, &fProps); if (!canvas) { return false; } @@ -102,13 +105,11 @@ GrSurfaceProxyView SkPictureImageGenerator::onGenerateTexture(GrRecordingContext GrImageTexGenPolicy texGenPolicy) { SkASSERT(ctx); - SkSurfaceProps props(0, kUnknown_SkPixelGeometry); - SkBudgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted ? SkBudgeted::kNo : SkBudgeted::kYes; auto surface = SkSurface::MakeRenderTarget(ctx, budgeted, info, 0, kTopLeft_GrSurfaceOrigin, - &props, mipmapped == GrMipmapped::kYes); + &fProps, mipmapped == GrMipmapped::kYes); if (!surface) { return {}; } diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp index 06edd85869..dddd9d481e 100644 --- a/src/image/SkImage.cpp +++ b/src/image/SkImage.cpp @@ -536,10 +536,11 @@ bool SkImage_Base::onAsLegacyBitmap(GrDirectContext* dContext, SkBitmap* bitmap) sk_sp SkImage::MakeFromPicture(sk_sp picture, const SkISize& dimensions, const SkMatrix* matrix, const SkPaint* paint, - BitDepth bitDepth, sk_sp colorSpace) { + BitDepth bitDepth, sk_sp colorSpace, + SkSurfaceProps props) { return MakeFromGenerator(SkImageGenerator::MakeFromPicture(dimensions, std::move(picture), matrix, paint, bitDepth, - std::move(colorSpace))); + std::move(colorSpace), props)); } sk_sp SkImage::makeWithFilter(GrRecordingContext* rContext, const SkImageFilter* filter,