Remove subset parameter from making encoded/generator images

Since subsetting may require rasterizing/resolving the generator, we may
also need access to the GrDirectContext. To simplify apis, rely on
makeSubset() for that.

Related: https://skia-review.googlesource.com/c/skia/+/305970

Change-Id: I1980e3c823fb6cf54f197c350942c2f82b03e20f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/306136
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
This commit is contained in:
Mike Reed 2020-07-28 12:52:31 -04:00 committed by Skia Commit-Bot
parent 3d404beb7c
commit 564d49ec10
11 changed files with 21 additions and 26 deletions

View File

@ -936,7 +936,7 @@ Result ImageGenSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
// Test deferred decoding path on GPU
if (fIsGpu) {
sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen), nullptr));
sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
if (!image) {
return Result::Fatal("Could not create image from codec image generator.");
}

View File

@ -9,7 +9,7 @@ void draw(SkCanvas* canvas) {
int x = 0;
for (int quality : { 0, 10, 50, 100 } ) {
sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data)->makeSubset(subset);
canvas->drawImage(filtered, x, 0);
x += 16;
}

View File

@ -7,7 +7,7 @@ void draw(SkCanvas* canvas) {
canvas->scale(4, 4);
SkIRect subset = {136, 32, 200, 96};
sk_sp<SkData> data(image->encodeToData());
sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data)->makeSubset(subset);
canvas->drawImage(eye, 0, 0);
}
} // END FIDDLE

View File

@ -70,7 +70,7 @@ void sk_test_c_api(sk_canvas_t* canvas) {
sk_data_t* data = sk_image_encode(img0);
sk_image_unref(img0);
sk_image_t* img1 = sk_image_new_from_encoded(data, NULL);
sk_image_t* img1 = sk_image_new_from_encoded(data);
sk_data_unref(data);
if (img1) {

View File

@ -262,7 +262,7 @@ protected:
const SkIRect subset = SkIRect::MakeLTRB(50, 50, 100, 100);
gen = fFactory(rContext, fPicture);
fImageSubset = SkImage::MakeFromGenerator(std::move(gen), &subset);
fImageSubset = SkImage::MakeFromGenerator(std::move(gen))->makeSubset(subset);
SkASSERT(fImage->dimensions() == SkISize::Make(100, 100));
SkASSERT(fImageSubset->dimensions() == SkISize::Make(50, 50));

View File

@ -28,7 +28,7 @@ SK_API sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t*, const void* p
* On success, the encoded data may be processed immediately, or it may be ref()'d for later
* use.
*/
SK_API sk_image_t* sk_image_new_from_encoded(const sk_data_t* encoded, const sk_irect_t* subset);
SK_API sk_image_t* sk_image_new_from_encoded(const sk_data_t* encoded);
/**
* Encode the image's pixels and return the result as a new PNG in a

View File

@ -176,20 +176,15 @@ public:
/** Creates SkImage from data returned by imageGenerator. Generated data is owned by SkImage and
may not be shared or accessed.
subset allows selecting a portion of the full image. Pass nullptr to select the entire
image; otherwise, subset must be contained by image bounds.
SkImage is returned if generator data is valid. Valid data parameters vary by type of data
and platform.
imageGenerator may wrap SkPicture data, codec data, or custom data.
@param imageGenerator stock or custom routines to retrieve SkImage
@param subset bounds of returned SkImage; may be nullptr
@return created SkImage, or nullptr
*/
static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
const SkIRect* subset = nullptr);
static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator);
/**
* Return an image backed by the encoded data, but attempt to defer decoding until the image
@ -209,12 +204,11 @@ public:
*
* @param encoded the encoded data
* @param length the number of bytes of encoded data
* @param subset the bounds of the pixels within the decoded image to return. may be null.
* @return created SkImage, or nullptr
example: https://fiddle.skia.org/c/@Image_MakeFromEncoded
*/
static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr);
static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded);
/**
* Decode the data in encoded/length into a raster image.

View File

@ -142,9 +142,8 @@ sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pi
return (sk_image_t*)SkImage::MakeRasterCopy(SkPixmap(*info, pixels, rowBytes)).release();
}
sk_image_t* sk_image_new_from_encoded(const sk_data_t* cdata, const sk_irect_t* subset) {
return ToImage(SkImage::MakeFromEncoded(sk_ref_sp(AsData(cdata)),
reinterpret_cast<const SkIRect*>(subset)).release());
sk_image_t* sk_image_new_from_encoded(const sk_data_t* cdata) {
return ToImage(SkImage::MakeFromEncoded(sk_ref_sp(AsData(cdata))).release());
}
sk_data_t* sk_image_encode(const sk_image_t* cimage) {

View File

@ -168,12 +168,11 @@ sk_sp<SkData> SkImage::refEncodedData() const {
return sk_sp<SkData>(as_IB(this)->onRefEncoded());
}
sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) {
sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded) {
if (nullptr == encoded || 0 == encoded->size()) {
return nullptr;
}
return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(std::move(encoded)),
subset);
return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(std::move(encoded)));
}
///////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -293,10 +293,9 @@ sk_sp<SkImage> SkImage_Lazy::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS)
return nullptr;
}
sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,
const SkIRect* subset) {
sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator) {
SkImage_Lazy::Validator
validator(SharedGenerator::Make(std::move(generator)), subset, nullptr, nullptr);
validator(SharedGenerator::Make(std::move(generator)), nullptr, nullptr, nullptr);
return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
}
@ -667,11 +666,15 @@ void SkImage_Lazy::addUniqueIDListener(sk_sp<SkIDChangeListener> listener) const
sk_sp<SkImage> SkImage::DecodeToTexture(GrDirectContext* direct, const void* encoded,
size_t length, const SkIRect* subset) {
// img will not survive this function, so we don't need to copy/own the encoded data,
auto img = MakeFromEncoded(SkData::MakeWithoutCopy(encoded, length), subset);
auto img = MakeFromEncoded(SkData::MakeWithoutCopy(encoded, length));
if (!img) {
return nullptr;
}
return img->makeTextureImage(direct);
img = img->makeTextureImage(direct);
if (img && subset) {
img = img->makeSubset(*subset, direct);
}
return img;
}
#endif // SK_SUPPORT_GPU

View File

@ -173,7 +173,7 @@ static void test_encode(skiatest::Reporter* reporter, SkImage* image) {
// Now see if we can instantiate an image from a subset of the surface/origEncoded
decoded = SkImage::MakeFromEncoded(origEncoded, &ir);
decoded = SkImage::MakeFromEncoded(origEncoded)->makeSubset(ir);
REPORTER_ASSERT(reporter, decoded);
assert_equal(reporter, image, &ir, decoded.get());
}