Revert "Change image encode api to return sk_sp"

This reverts commit dc799550e2.

Reason for revert: need to fix sites in Document_none

Original change's description:
> Change image encode api to return sk_sp
> 
> Bug: skia:
> Change-Id: I238289bc630be27795cb1384955dd6e887597c05
> Reviewed-on: https://skia-review.googlesource.com/22208
> Commit-Queue: Mike Reed <reed@google.com>
> Reviewed-by: Florin Malita <fmalita@chromium.org>

TBR=scroggo@google.com,fmalita@chromium.org,reed@google.com

Change-Id: Id7f67027e5f1405a60fdbde29863cdf8daef0cb7
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: skia:
Reviewed-on: https://skia-review.googlesource.com/22280
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:00:24 +00:00 committed by Skia Commit-Bot
parent dc799550e2
commit 64778d9f27
13 changed files with 47 additions and 67 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->refEncodedData();
sk_sp<SkData> encoded(img->refEncoded());
SkASSERT(encoded);
if (!encoded) { return; }
fImage = img;

View File

@ -253,7 +253,8 @@ 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));
return SkImage::MakeFromEncoded(image->encodeToData());
sk_sp<SkData> data(image->encode());
return SkImage::MakeFromEncoded(data);
}
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->encodeToData(SkEncodedImageFormat::kPNG, 100);
sk_sp<SkData> encoded(src->encode(SkEncodedImageFormat::kPNG, 100));
if (!encoded) {
return nullptr;
}

View File

@ -359,16 +359,20 @@ public:
bool scalePixels(const SkPixmap& dst, SkFilterQuality, CachingHint = kAllow_CachingHint) const;
/**
* Encode the image's pixels and return the result as SkData.
* 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).
*
* If the image type cannot be encoded, or the requested encoder format is
* If the image type cannot be encoded, or the requested encoder type 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.
*/
sk_sp<SkData> encodeToData(SkEncodedImageFormat, int quality) const;
SkData* encode(SkEncodedImageFormat, int quality) const;
/**
* Encode the image and return the result as SkData. This will attempt to reuse existing
* encoded data (as returned by refEncodedData).
* Encode the image and return the result as a caller-managed SkData. This will
* attempt to reuse existing encoded data (as returned by refEncoded).
*
* We defer to the SkPixelSerializer both for vetting existing encoded data
* (useEncodedData) and for encoding the image (encode) when no such data is
@ -380,21 +384,18 @@ public:
* If no compatible encoded data exists and encoding fails, this method will also
* fail (return NULL).
*/
sk_sp<SkData> encodeToData(SkPixelSerializer* = nullptr) const;
SkData* encode(SkPixelSerializer* = nullptr) const;
/**
* 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 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.
*
* Note: to force the image to return its contents as encoded data, use encodeToData(...).
* 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(...).
*/
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

@ -32,15 +32,7 @@ public:
* Call to get the client's version of encoding these pixels. If it
* returns NULL, serialize the raw pixels.
*/
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) {
return this->encodeToData(pixmap).release();
}
#endif
SkData* encode(const SkPixmap& pixmap) { return this->onEncode(pixmap); }
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)->encodeToData().release());
return ToData(AsImage(cimage)->encode());
}
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->encodeToData(result);
sk_sp<SkData> data(fPixelSerializer->encode(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->encodeToData(this->getPixelSerializer());
sk_sp<SkData> encoded(image->encode(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);
}
sk_sp<SkData> SkImage::encodeToData(SkEncodedImageFormat type, int quality) const {
SkData* SkImage::encode(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() : nullptr;
return SkEncodeImage(&buf, bm, type, quality) ? buf.detachAsData().release() : nullptr;
}
return nullptr;
}
sk_sp<SkData> SkImage::encodeToData(SkPixelSerializer* serializer) const {
sk_sp<SkData> encoded(this->refEncodedData());
SkData* SkImage::encode(SkPixelSerializer* serializer) const {
sk_sp<SkData> encoded(this->refEncoded());
if (encoded &&
(!serializer || serializer->useEncodedData(encoded->data(), encoded->size()))) {
return encoded;
return encoded.release();
}
SkBitmap bm;
@ -116,19 +116,19 @@ sk_sp<SkData> SkImage::encodeToData(SkPixelSerializer* serializer) const {
SkColorSpace* legacyColorSpace = nullptr;
if (as_IB(this)->getROPixels(&bm, legacyColorSpace) && bm.peekPixels(&pmap)) {
if (serializer) {
return serializer->encodeToData(pmap);
return serializer->encode(pmap);
} else {
SkDynamicMemoryWStream buf;
return SkEncodeImage(&buf, pmap, SkEncodedImageFormat::kPNG, 100)
? buf.detachAsData() : nullptr;
? buf.detachAsData().release() : nullptr;
}
}
return nullptr;
}
sk_sp<SkData> SkImage::refEncodedData() const {
return sk_sp<SkData>(as_IB(this)->onRefEncoded());
SkData* SkImage::refEncoded() const {
return as_IB(this)->onRefEncoded();
}
sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) {
@ -138,20 +138,6 @@ 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();
}
#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->refEncodedData();
sk_sp<SkData> data(image->refEncoded());
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 = pixelSerializer->encodeToData(pmap);
data.reset(pixelSerializer->encode(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->encodeToData(&serial);
sk_sp<SkData> data(image->encode(&serial));
if (!data) {
data = image->encodeToData();
data.reset(image->encode());
}
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->encodeToData();
sk_sp<SkData> origEncoded(image->encode());
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->encodeToData(&serializer);
sk_sp<SkData> encoded(image->encode(&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->encodeToData(SkEncodedImageFormat::kPNG, 100);
sk_sp<SkData> data(img0->encode(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->encodeToData();
sk_sp<SkData> data(rasterImage->encode());
sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(data);
test_scale_pixels(reporter, codecImage.get(), pmRed);
}

View File

@ -454,8 +454,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->encodeToData();
sk_sp<SkData> enc1 = img1->encodeToData();
sk_sp<SkData> enc0(img0->encode());
sk_sp<SkData> enc1(img1->encode());
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 sk_sp<SkData> encode_snapshot(const sk_sp<SkSurface>& surface) {
static SkData* encode_snapshot(const sk_sp<SkSurface>& surface) {
sk_sp<SkImage> img(surface->makeImageSnapshot());
return img ? img->encodeToData() : nullptr;
return img ? img->encode() : 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 = encode_snapshot(rasterSurface);
rasterData.reset(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 = encode_snapshot(surface);
gpuData.reset(encode_snapshot(surface));
}
}
if (options.pdf) {