diff --git a/include/core/SkImageGenerator.h b/include/core/SkImageGenerator.h index 66db5e4884..86e3053a06 100644 --- a/include/core/SkImageGenerator.h +++ b/include/core/SkImageGenerator.h @@ -22,6 +22,12 @@ class SkMatrix; class SkPaint; class SkPicture; +#ifdef SK_SUPPORT_LEGACY_REFENCODEDDATA_NOCTX + #define SK_REFENCODEDDATA_CTXPARAM +#else + #define SK_REFENCODEDDATA_CTXPARAM GrContext* ctx +#endif + /** * Takes ownership of SkImageGenerator. If this method fails for * whatever reason, it will return false and immediatetely delete @@ -64,12 +70,20 @@ public: /** * Return a ref to the encoded (i.e. compressed) representation, - * of this data. + * of this data. If the GrContext is non-null, then the caller is only interested in + * gpu-specific formats, so the impl may return null even if they have encoded data, + * assuming they know it is not suitable for the gpu. * * If non-NULL is returned, the caller is responsible for calling * unref() on the data when it is finished. */ - SkData* refEncodedData() { return this->onRefEncodedData(); } + SkData* refEncodedData(GrContext* ctx = nullptr) { +#ifdef SK_SUPPORT_LEGACY_REFENCODEDDATA_NOCTX + return this->onRefEncodedData(); +#else + return this->onRefEncodedData(ctx); +#endif + } /** * Return the ImageInfo associated with this generator. @@ -230,7 +244,7 @@ public: protected: SkImageGenerator(const SkImageInfo& info); - virtual SkData* onRefEncodedData(); + virtual SkData* onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM); virtual bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[], int* ctableCount); diff --git a/src/core/SkImageCacherator.cpp b/src/core/SkImageCacherator.cpp index 9074924b2a..572778e19c 100644 --- a/src/core/SkImageCacherator.cpp +++ b/src/core/SkImageCacherator.cpp @@ -69,9 +69,9 @@ SkImageCacherator::SkImageCacherator(SkImageGenerator* gen, const SkImageInfo& i , fUniqueID(uniqueID) {} -SkData* SkImageCacherator::refEncoded() { +SkData* SkImageCacherator::refEncoded(GrContext* ctx) { ScopedGenerator generator(this); - return generator->refEncodedData(); + return generator->refEncodedData(ctx); } static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) { @@ -259,7 +259,7 @@ GrTexture* SkImageCacherator::lockTexture(GrContext* ctx, const GrUniqueKey& key const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo); // 3. Ask the generator to return a compressed form that the GPU might support - SkAutoTUnref data(this->refEncoded()); + SkAutoTUnref data(this->refEncoded(ctx)); if (data) { GrTexture* tex = load_compressed_into_texture(ctx, data, desc); if (tex) { diff --git a/src/core/SkImageCacherator.h b/src/core/SkImageCacherator.h index 42b93f6234..6b000668dd 100644 --- a/src/core/SkImageCacherator.h +++ b/src/core/SkImageCacherator.h @@ -54,8 +54,11 @@ public: /** * If the underlying src naturally is represented by an encoded blob (in SkData), this returns * a ref to that data. If not, it returns null. + * + * If a GrContext is specified, then the caller is only interested in gpu-specific encoded + * formats, so others (e.g. PNG) can just return nullptr. */ - SkData* refEncoded(); + SkData* refEncoded(GrContext*); // Only return true if the generate has already been cached. bool lockAsBitmapOnlyIfAlreadyCached(SkBitmap*); diff --git a/src/core/SkImageGenerator.cpp b/src/core/SkImageGenerator.cpp index dcd3ad00b1..7d71b6701c 100644 --- a/src/core/SkImageGenerator.cpp +++ b/src/core/SkImageGenerator.cpp @@ -137,7 +137,7 @@ bool SkImageGenerator::generateScaledPixels(const SkISize& scaledSize, ///////////////////////////////////////////////////////////////////////////////////////////// -SkData* SkImageGenerator::onRefEncodedData() { +SkData* SkImageGenerator::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) { return nullptr; } diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp index 338ab09130..32844b474f 100644 --- a/src/image/SkImage.cpp +++ b/src/image/SkImage.cpp @@ -202,7 +202,8 @@ SkData* SkImage::encode(SkPixelSerializer* serializer) const { } SkData* SkImage::refEncoded() const { - return as_IB(this)->onRefEncoded(); + GrContext* ctx = nullptr; // should we allow the caller to pass in a ctx? + return as_IB(this)->onRefEncoded(ctx); } SkImage* SkImage::NewFromEncoded(SkData* encoded, const SkIRect* subset) { diff --git a/src/image/SkImage_Base.h b/src/image/SkImage_Base.h index 321dcaaae7..4689abb3b7 100644 --- a/src/image/SkImage_Base.h +++ b/src/image/SkImage_Base.h @@ -51,7 +51,8 @@ public: virtual SkImage* onNewSubset(const SkIRect&) const = 0; - virtual SkData* onRefEncoded() const { return nullptr; } + // If a ctx is specified, then only gpu-specific formats are requested. + virtual SkData* onRefEncoded(GrContext*) const { return nullptr; } virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const; diff --git a/src/image/SkImage_Generator.cpp b/src/image/SkImage_Generator.cpp index 6671dcd3b1..688b4ffe3b 100644 --- a/src/image/SkImage_Generator.cpp +++ b/src/image/SkImage_Generator.cpp @@ -24,7 +24,7 @@ public: bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, CachingHint) const override; const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const override; SkImageCacherator* peekCacherator() const override { return fCache; } - SkData* onRefEncoded() const override; + SkData* onRefEncoded(GrContext*) const override; bool isOpaque() const override { return fCache->info().isOpaque(); } SkImage* onNewSubset(const SkIRect&) const override; bool getROPixels(SkBitmap*, CachingHint) const override; @@ -66,8 +66,8 @@ const void* SkImage_Generator::onPeekPixels(SkImageInfo* infoPtr, size_t* rowByt return NULL; } -SkData* SkImage_Generator::onRefEncoded() const { - return fCache->refEncoded(); +SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const { + return fCache->refEncoded(ctx); } bool SkImage_Generator::getROPixels(SkBitmap* bitmap, CachingHint chint) const { diff --git a/src/image/SkImage_Raster.cpp b/src/image/SkImage_Raster.cpp index 2b81da8915..d4debdf807 100644 --- a/src/image/SkImage_Raster.cpp +++ b/src/image/SkImage_Raster.cpp @@ -68,7 +68,7 @@ public: bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, CachingHint) const override; const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const override; - SkData* onRefEncoded() const override; + SkData* onRefEncoded(GrContext*) const override; bool getROPixels(SkBitmap*, CachingHint) const override; GrTexture* asTextureRef(GrContext*, const GrTextureParams&) const override; SkImage* onNewSubset(const SkIRect&) const override; @@ -150,7 +150,7 @@ const void* SkImage_Raster::onPeekPixels(SkImageInfo* infoPtr, size_t* rowBytesP return fBitmap.getPixels(); } -SkData* SkImage_Raster::onRefEncoded() const { +SkData* SkImage_Raster::onRefEncoded(GrContext*) const { SkPixelRef* pr = fBitmap.pixelRef(); const SkImageInfo prInfo = pr->info(); const SkImageInfo bmInfo = fBitmap.info(); diff --git a/src/images/SkDecodingImageGenerator.cpp b/src/images/SkDecodingImageGenerator.cpp index 8cfacc3fba..8d55bd36c7 100644 --- a/src/images/SkDecodingImageGenerator.cpp +++ b/src/images/SkDecodingImageGenerator.cpp @@ -37,7 +37,7 @@ public: bool ditherImage); protected: - SkData* onRefEncodedData() override; + SkData* onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) override; bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[], int* ctableCount) override; bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], @@ -126,7 +126,7 @@ DecodingImageGenerator::~DecodingImageGenerator() { SkSafeUnref(fData); } -SkData* DecodingImageGenerator::onRefEncodedData() { +SkData* DecodingImageGenerator::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) { // This functionality is used in `gm --serialize` // Does not encode options. if (nullptr == fData) { diff --git a/src/ports/SkImageGenerator_skia.cpp b/src/ports/SkImageGenerator_skia.cpp index afcd4b4a92..8dbad555dd 100644 --- a/src/ports/SkImageGenerator_skia.cpp +++ b/src/ports/SkImageGenerator_skia.cpp @@ -43,7 +43,7 @@ public: {} protected: - SkData* onRefEncodedData() override { + SkData* onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) override { return SkRef(fData.get()); } bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,