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

This reverts commit 64778d9f275d8ce3df8f4ab39ff334b7ef5b70d3.

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>
diff --git a/bench/PDFBench.cpp b/bench/PDFBench.cpp
index fa7cfe9..b371452 100644
--- a/bench/PDFBench.cpp
+++ b/bench/PDFBench.cpp
@@ -110,7 +110,7 @@
     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;
diff --git a/gm/image.cpp b/gm/image.cpp
index fc7a5ff..6bf9f01 100644
--- a/gm/image.cpp
+++ b/gm/image.cpp
@@ -253,8 +253,7 @@
 
 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*)) {
diff --git a/gm/image_shader.cpp b/gm/image_shader.cpp
index 02e6679..5e55786 100644
--- a/gm/image_shader.cpp
+++ b/gm/image_shader.cpp
@@ -57,7 +57,7 @@
     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;
     }
diff --git a/include/core/SkImage.h b/include/core/SkImage.h
index 11d5602..862dc66 100644
--- a/include/core/SkImage.h
+++ b/include/core/SkImage.h
@@ -359,20 +359,16 @@
     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 @@
      *  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;
 
diff --git a/include/core/SkPixelSerializer.h b/include/core/SkPixelSerializer.h
index b168f79..c3cbc0a 100644
--- a/include/core/SkPixelSerializer.h
+++ b/include/core/SkPixelSerializer.h
@@ -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 @@
      *  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:
     /**
diff --git a/src/c/sk_surface.cpp b/src/c/sk_surface.cpp
index 7e1fb3d..a77f61e 100644
--- a/src/c/sk_surface.cpp
+++ b/src/c/sk_surface.cpp
@@ -233,7 +233,7 @@
 }
 
 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) {
diff --git a/src/core/SkWriteBuffer.cpp b/src/core/SkWriteBuffer.cpp
index 3322e8a..43a63c9 100644
--- a/src/core/SkWriteBuffer.cpp
+++ b/src/core/SkWriteBuffer.cpp
@@ -156,7 +156,7 @@
     // 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 @@
     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;
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp
index 9dcf3d7..8cb5267 100644
--- a/src/image/SkImage.cpp
+++ b/src/image/SkImage.cpp
@@ -94,21 +94,21 @@
     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 @@
     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 @@
     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" : "");
diff --git a/src/pdf/SkPDFBitmap.cpp b/src/pdf/SkPDFBitmap.cpp
index fa09be3..68d5272 100644
--- a/src/pdf/SkPDFBitmap.cpp
+++ b/src/pdf/SkPDFBitmap.cpp
@@ -479,7 +479,7 @@
 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 @@
         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.
diff --git a/src/pipe/SkPipeCanvas.cpp b/src/pipe/SkPipeCanvas.cpp
index 7b665d9..226a957 100644
--- a/src/pipe/SkPipeCanvas.cpp
+++ b/src/pipe/SkPipeCanvas.cpp
@@ -828,9 +828,9 @@
 
 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;
 }
diff --git a/tests/ImageTest.cpp b/tests/ImageTest.cpp
index 0b62b00..69b2a22 100644
--- a/tests/ImageTest.cpp
+++ b/tests/ImageTest.cpp
@@ -160,7 +160,7 @@
 
 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 @@
         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 @@
     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 @@
     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);
 }
diff --git a/tests/TextBlobTest.cpp b/tests/TextBlobTest.cpp
index 60ad653..c00970f 100644
--- a/tests/TextBlobTest.cpp
+++ b/tests/TextBlobTest.cpp
@@ -458,8 +458,8 @@
         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());
diff --git a/tools/fiddle/fiddle_main.cpp b/tools/fiddle/fiddle_main.cpp
index 74aefb8..6ceffc9 100644
--- a/tools/fiddle/fiddle_main.cpp
+++ b/tools/fiddle/fiddle_main.cpp
@@ -88,9 +88,9 @@
     }
 }
 
-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 @@
         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 @@
             }
             srand(0);
             draw(prepare_canvas(surface->getCanvas()));
-            gpuData.reset(encode_snapshot(surface));
+            gpuData = encode_snapshot(surface);
         }
     }
     if (options.pdf) {