Revert "Revert "Change image encode api to return sk_sp""

This reverts commit 64778d9f27.

Bug: skia:
Change-Id: I779515ff1e16a40c33890a4bac7a8a07171aadfe
Reviewed-on: https://skia-review.googlesource.com/22261
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2017-07-11 16:03:13 -04:00 committed by Skia Commit-Bot
parent ebb38256c6
commit 6409f84fc3
13 changed files with 70 additions and 50 deletions

View File

@ -110,7 +110,7 @@ protected:
void onDelayedSetup() override {
sk_sp<SkImage> img(GetResourceAsImage("mandrill_512_q075.jpg"));
if (!img) { return; }
sk_sp<SkData> encoded(img->refEncoded());
sk_sp<SkData> encoded = img->refEncodedData();
SkASSERT(encoded);
if (!encoded) { return; }
fImage = img;

View File

@ -253,8 +253,7 @@ static sk_sp<SkImage> make_picture(const SkImageInfo& info, GrContext*, void (*d
static sk_sp<SkImage> make_codec(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
sk_sp<SkImage> image(make_raster(info, nullptr, draw));
sk_sp<SkData> data(image->encode());
return SkImage::MakeFromEncoded(data);
return SkImage::MakeFromEncoded(image->encodeToData());
}
static sk_sp<SkImage> make_gpu(const SkImageInfo& info, GrContext* ctx, void (*draw)(SkCanvas*)) {

View File

@ -57,7 +57,7 @@ static sk_sp<SkImage> make_encode_gen(GrContext* ctx, SkPicture* pic, const SkIm
if (!src) {
return nullptr;
}
sk_sp<SkData> encoded(src->encode(SkEncodedImageFormat::kPNG, 100));
sk_sp<SkData> encoded = src->encodeToData(SkEncodedImageFormat::kPNG, 100);
if (!encoded) {
return nullptr;
}

View File

@ -359,20 +359,16 @@ public:
bool scalePixels(const SkPixmap& dst, SkFilterQuality, CachingHint = kAllow_CachingHint) const;
/**
* Encode the image's pixels and return the result as a new SkData, which
* the caller must manage (i.e. call unref() when they are done).
* Encode the image's pixels and return the result as SkData.
*
* If the image type cannot be encoded, or the requested encoder type is
* If the image type cannot be encoded, or the requested encoder format is
* not supported, this will return NULL.
*
* Note: this will attempt to encode the image's pixels in the specified format,
* even if the image returns a data from refEncoded(). That data will be ignored.
*/
SkData* encode(SkEncodedImageFormat, int quality) const;
sk_sp<SkData> encodeToData(SkEncodedImageFormat, int quality) const;
/**
* Encode the image and return the result as a caller-managed SkData. This will
* attempt to reuse existing encoded data (as returned by refEncoded).
* Encode the image and return the result as SkData. This will attempt to reuse existing
* encoded data (as returned by refEncodedData).
*
* We defer to the SkPixelSerializer both for vetting existing encoded data
* (useEncodedData) and for encoding the image (encode) when no such data is
@ -384,18 +380,21 @@ public:
* If no compatible encoded data exists and encoding fails, this method will also
* fail (return NULL).
*/
SkData* encode(SkPixelSerializer* = nullptr) const;
sk_sp<SkData> encodeToData(SkPixelSerializer* = nullptr) const;
/**
* If the image already has its contents in encoded form (e.g. PNG or JPEG), return a ref
* to that data (which the caller must call unref() on). The caller is responsible for calling
* unref on the data when they are done.
* If the image already has its contents in encoded form (e.g. PNG or JPEG), return that
* as SkData. If the image does not already has its contents in encoded form, return NULL.
*
* If the image does not already has its contents in encoded form, return NULL.
*
* Note: to force the image to return its contents as encoded data, try calling encode(...).
* Note: to force the image to return its contents as encoded data, use encodeToData(...).
*/
sk_sp<SkData> refEncodedData() const;
#ifdef SK_SUPPORT_LEGACY_IMAGE_ENCODE_API
SkData* encode(SkEncodedImageFormat, int quality) const;
SkData* encode(SkPixelSerializer* = nullptr) const;
SkData* refEncoded() const;
#endif
const char* toString(SkString*) const;

View File

@ -8,10 +8,9 @@
#ifndef SkPixelSerializer_DEFINED
#define SkPixelSerializer_DEFINED
#include "SkRefCnt.h"
#include "SkData.h"
#include "SkPixmap.h"
class SkData;
#include "SkRefCnt.h"
/**
* Interface for serializing pixels, e.g. SkBitmaps in an SkPicture.
@ -32,7 +31,13 @@ public:
* Call to get the client's version of encoding these pixels. If it
* returns NULL, serialize the raw pixels.
*/
SkData* encode(const SkPixmap& pixmap) { return this->onEncode(pixmap); }
sk_sp<SkData> encodeToData(const SkPixmap& pixmap) {
return sk_sp<SkData>(this->onEncode(pixmap));
}
#ifdef SK_SUPPORT_LEGACY_IMAGE_ENCODE_API
SkData* encode(const SkPixmap& pixmap);
#endif
protected:
/**

View File

@ -233,7 +233,7 @@ sk_image_t* sk_image_new_from_encoded(const sk_data_t* cdata, const sk_irect_t*
}
sk_data_t* sk_image_encode(const sk_image_t* cimage) {
return ToData(AsImage(cimage)->encode());
return ToData(AsImage(cimage)->encodeToData().release());
}
void sk_image_ref(const sk_image_t* cimage) {

View File

@ -156,7 +156,7 @@ void SkBinaryWriteBuffer::writeBitmap(const SkBitmap& bitmap) {
// see if the caller wants to manually encode
SkPixmap result;
if (fPixelSerializer && bitmap.peekPixels(&result)) {
sk_sp<SkData> data(fPixelSerializer->encode(result));
sk_sp<SkData> data = fPixelSerializer->encodeToData(result);
if (data) {
// if we have to "encode" the bitmap, then we assume there is no
// offset to share, since we are effectively creating a new pixelref
@ -178,7 +178,7 @@ void SkBinaryWriteBuffer::writeImage(const SkImage* image) {
this->writeInt(image->width());
this->writeInt(image->height());
sk_sp<SkData> encoded(image->encode(this->getPixelSerializer()));
sk_sp<SkData> encoded = image->encodeToData(this->getPixelSerializer());
if (encoded && encoded->size() > 0) {
write_encoded_bitmap(this, encoded.get(), SkIPoint::Make(0, 0));
return;

View File

@ -94,21 +94,21 @@ sk_sp<SkShader> SkImage::makeShader(SkShader::TileMode tileX, SkShader::TileMode
return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tileX, tileY, localMatrix);
}
SkData* SkImage::encode(SkEncodedImageFormat type, int quality) const {
sk_sp<SkData> SkImage::encodeToData(SkEncodedImageFormat type, int quality) const {
SkBitmap bm;
SkColorSpace* legacyColorSpace = nullptr;
if (as_IB(this)->getROPixels(&bm, legacyColorSpace)) {
SkDynamicMemoryWStream buf;
return SkEncodeImage(&buf, bm, type, quality) ? buf.detachAsData().release() : nullptr;
return SkEncodeImage(&buf, bm, type, quality) ? buf.detachAsData() : nullptr;
}
return nullptr;
}
SkData* SkImage::encode(SkPixelSerializer* serializer) const {
sk_sp<SkData> encoded(this->refEncoded());
sk_sp<SkData> SkImage::encodeToData(SkPixelSerializer* serializer) const {
sk_sp<SkData> encoded(this->refEncodedData());
if (encoded &&
(!serializer || serializer->useEncodedData(encoded->data(), encoded->size()))) {
return encoded.release();
return encoded;
}
SkBitmap bm;
@ -116,19 +116,19 @@ SkData* SkImage::encode(SkPixelSerializer* serializer) const {
SkColorSpace* legacyColorSpace = nullptr;
if (as_IB(this)->getROPixels(&bm, legacyColorSpace) && bm.peekPixels(&pmap)) {
if (serializer) {
return serializer->encode(pmap);
return serializer->encodeToData(pmap);
} else {
SkDynamicMemoryWStream buf;
return SkEncodeImage(&buf, pmap, SkEncodedImageFormat::kPNG, 100)
? buf.detachAsData().release() : nullptr;
? buf.detachAsData() : nullptr;
}
}
return nullptr;
}
SkData* SkImage::refEncoded() const {
return as_IB(this)->onRefEncoded();
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) {
@ -138,6 +138,23 @@ sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* su
return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(encoded), subset);
}
#ifdef SK_SUPPORT_LEGACY_IMAGE_ENCODE_API
SkData* SkImage::encode(SkEncodedImageFormat format, int quality) const {
return this->encodeToData(format, quality).release();
}
SkData* SkImage::encode(SkPixelSerializer* serial) const {
return this->encodeToData(serial).release();
}
SkData* SkImage::refEncoded() const {
return this->refEncodedData().release();
}
SkData* SkPixelSerializer::encode(const SkPixmap& pixmap) {
return this->encodeToData(pixmap).release();
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////
const char* SkImage::toString(SkString* str) const {
str->appendf("image: (id:%d (%d, %d) %s)", this->uniqueID(), this->width(), this->height(),
this->isOpaque() ? "opaque" : "");

View File

@ -479,7 +479,7 @@ void PDFJpegBitmap::emitObject(SkWStream* stream,
sk_sp<SkPDFObject> SkPDFCreateBitmapObject(sk_sp<SkImage> image,
SkPixelSerializer* pixelSerializer) {
SkASSERT(image);
sk_sp<SkData> data(image->refEncoded());
sk_sp<SkData> data = image->refEncodedData();
SkJFIFInfo info;
if (data && SkIsJFIF(data.get(), &info) &&
(!pixelSerializer ||
@ -502,7 +502,7 @@ sk_sp<SkPDFObject> SkPDFCreateBitmapObject(sk_sp<SkImage> image,
SkPixmap pmap;
SkColorSpace* legacyColorSpace = nullptr;
if (as_IB(image.get())->getROPixels(&bm, legacyColorSpace) && bm.peekPixels(&pmap)) {
data.reset(pixelSerializer->encode(pmap));
data = pixelSerializer->encodeToData(pmap);
if (data && SkIsJFIF(data.get(), &info)) {
bool yuv = info.fType == SkJFIFInfo::kYCbCr;
if (info.fSize == image->dimensions()) { // Sanity check.

View File

@ -828,9 +828,9 @@ protected:
static sk_sp<SkData> default_image_serializer(SkImage* image) {
A8Serializer serial;
sk_sp<SkData> data(image->encode(&serial));
sk_sp<SkData> data = image->encodeToData(&serial);
if (!data) {
data.reset(image->encode());
data = image->encodeToData();
}
return data;
}

View File

@ -160,7 +160,7 @@ static sk_sp<SkImage> create_gpu_image(GrContext* context) {
static void test_encode(skiatest::Reporter* reporter, SkImage* image) {
const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10);
sk_sp<SkData> origEncoded(image->encode());
sk_sp<SkData> origEncoded = image->encodeToData();
REPORTER_ASSERT(reporter, origEncoded);
REPORTER_ASSERT(reporter, origEncoded->size() > 0);
@ -256,7 +256,7 @@ DEF_TEST(Image_Encode_Serializer, reporter) {
return SkData::MakeWithCString(kSerializedData);
});
sk_sp<SkImage> image(create_image());
sk_sp<SkData> encoded(image->encode(&serializer));
sk_sp<SkData> encoded = image->encodeToData(&serializer);
sk_sp<SkData> reference(SkData::MakeWithCString(kSerializedData));
REPORTER_ASSERT(reporter, serializer.didEncode());
@ -1236,7 +1236,7 @@ DEF_TEST(image_roundtrip_encode, reporter) {
make_all_premul(&bm0);
auto img0 = SkImage::MakeFromBitmap(bm0);
sk_sp<SkData> data(img0->encode(SkEncodedImageFormat::kPNG, 100));
sk_sp<SkData> data = img0->encodeToData(SkEncodedImageFormat::kPNG, 100);
auto img1 = SkImage::MakeFromEncoded(data);
SkBitmap bm1;
@ -1306,7 +1306,7 @@ DEF_TEST(ImageScalePixels, reporter) {
test_scale_pixels(reporter, rasterImage.get(), pmRed);
// Test encoded image
sk_sp<SkData> data(rasterImage->encode());
sk_sp<SkData> data = rasterImage->encodeToData();
sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(data);
test_scale_pixels(reporter, codecImage.get(), pmRed);
}

View File

@ -458,8 +458,8 @@ DEF_TEST(TextBlob_serialize, reporter) {
REPORTER_ASSERT(reporter, img0->width() == img1->width());
REPORTER_ASSERT(reporter, img0->height() == img1->height());
sk_sp<SkData> enc0(img0->encode());
sk_sp<SkData> enc1(img1->encode());
sk_sp<SkData> enc0 = img0->encodeToData();
sk_sp<SkData> enc1 = img1->encodeToData();
REPORTER_ASSERT(reporter, enc0->equals(enc1.get()));
if (false) { // in case you want to actually see the images...
SkFILEWStream("textblob_serialize_img0.png").write(enc0->data(), enc0->size());

View File

@ -88,9 +88,9 @@ static void dump_output(const sk_sp<SkData>& data,
}
}
static SkData* encode_snapshot(const sk_sp<SkSurface>& surface) {
static sk_sp<SkData> encode_snapshot(const sk_sp<SkSurface>& surface) {
sk_sp<SkImage> img(surface->makeImageSnapshot());
return img ? img->encode() : nullptr;
return img ? img->encodeToData() : nullptr;
}
static SkCanvas* prepare_canvas(SkCanvas * canvas) {
@ -142,7 +142,7 @@ int main(int argc, char** argv) {
auto rasterSurface = SkSurface::MakeRaster(info);
srand(0);
draw(prepare_canvas(rasterSurface->getCanvas()));
rasterData.reset(encode_snapshot(rasterSurface));
rasterData = encode_snapshot(rasterSurface);
}
if (options.gpu) {
auto grContext = create_grcontext(gGLDriverInfo);
@ -156,7 +156,7 @@ int main(int argc, char** argv) {
}
srand(0);
draw(prepare_canvas(surface->getCanvas()));
gpuData.reset(encode_snapshot(surface));
gpuData = encode_snapshot(surface);
}
}
if (options.pdf) {