SkImageEncoder: simplify API

(re-land 248ff02 & 2cb6cb7, with changes)

  - Hide SkImageEncoder class in private header.
  - SkImageEncoder::Type becomes SkEncodedImageFormat
  - SkEncodedFormat becomes SkEncodedImageFormat
  - SkImageEncoder static functions replaced with
    single function EncodeImage()
  - utility wrappers for EncodeImage() are in
    sk_tool_utils.h

TODO: remove link-time registration mechanism.
TODO: clean up clients use of API and flip the flag.
TODO: implement EncodeImage() in chromeium/skia/ext

Change-Id: I47d451e50be4d5c6c130869c7fa7c2857243d9f0
Reviewed-on: https://skia-review.googlesource.com/4909
Reviewed-by: Mike Reed <reed@google.com>
Reviewed-by: Leon Scroggins <scroggo@google.com>
Reviewed-on: https://skia-review.googlesource.com/5186
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Hal Canary <halcanary@google.com>
diff --git a/tools/colorspaceinfo.cpp b/tools/colorspaceinfo.cpp
index 301f47d..945c4a5 100644
--- a/tools/colorspaceinfo.cpp
+++ b/tools/colorspaceinfo.cpp
@@ -17,6 +17,8 @@
 #include "SkMatrix44.h"
 #include "SkOSFile.h"
 
+#include "sk_tool_utils.h"
+
 __SK_FORCE_IMAGE_DECODER_LINKING;
 
 DEFINE_string(input, "input.png", "A path to the input image or icc profile.");
@@ -219,7 +221,7 @@
     }
 
     // Finally, encode the result to the output file.
-    sk_sp<SkData> out(SkImageEncoder::EncodeData(gamut, SkImageEncoder::kPNG_Type, 100));
+    sk_sp<SkData> out = sk_tool_utils::EncodeImageToData(gamut, SkEncodedImageFormat::kPNG, 100);
     if (!out) {
         SkDebugf("Failed to encode gamut output.\n");
         return -1;
@@ -243,7 +245,7 @@
             SkDebugf("Could not decode input image.\n");
             return -1;
         }
-        out.reset(SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100));
+        out = sk_tool_utils::EncodeImageToData(bitmap, SkEncodedImageFormat::kPNG, 100);
         if (!out) {
             SkDebugf("Failed to encode uncorrected image.\n");
             return -1;
diff --git a/tools/get_images_from_skps.cpp b/tools/get_images_from_skps.cpp
index 6cd3512..d3a2343 100644
--- a/tools/get_images_from_skps.cpp
+++ b/tools/get_images_from_skps.cpp
@@ -65,14 +65,14 @@
         }
         SkString ext;
         switch (codec->getEncodedFormat()) {
-            case SkEncodedFormat::kBMP_SkEncodedFormat:  ext =  "bmp"; break;
-            case SkEncodedFormat::kGIF_SkEncodedFormat:  ext =  "gif"; break;
-            case SkEncodedFormat::kICO_SkEncodedFormat:  ext =  "ico"; break;
-            case SkEncodedFormat::kJPEG_SkEncodedFormat: ext =  "jpg"; break;
-            case SkEncodedFormat::kPNG_SkEncodedFormat:  ext =  "png"; break;
-            case SkEncodedFormat::kDNG_SkEncodedFormat:  ext =  "dng"; break;
-            case SkEncodedFormat::kWBMP_SkEncodedFormat: ext = "wbmp"; break;
-            case SkEncodedFormat::kWEBP_SkEncodedFormat: ext = "webp"; break;
+            case SkEncodedImageFormat::kBMP:  ext =  "bmp"; break;
+            case SkEncodedImageFormat::kGIF:  ext =  "gif"; break;
+            case SkEncodedImageFormat::kICO:  ext =  "ico"; break;
+            case SkEncodedImageFormat::kJPEG: ext =  "jpg"; break;
+            case SkEncodedImageFormat::kPNG:  ext =  "png"; break;
+            case SkEncodedImageFormat::kDNG:  ext =  "dng"; break;
+            case SkEncodedImageFormat::kWBMP: ext = "wbmp"; break;
+            case SkEncodedImageFormat::kWEBP: ext = "webp"; break;
             default:
                 // This should be unreachable because we cannot create a codec if we do not know
                 // the image type.
diff --git a/tools/imgblur.cpp b/tools/imgblur.cpp
index 3a0ae00..28d8447 100644
--- a/tools/imgblur.cpp
+++ b/tools/imgblur.cpp
@@ -66,7 +66,7 @@
 
     SkBitmap dst = sk_tool_utils::slow_blur(src, (float) FLAGS_sigma);
 
-    if (!SkImageEncoder::EncodeFile(FLAGS_out[0], dst, SkImageEncoder::kPNG_Type, 100)) {
+    if (!sk_tool_utils::EncodeImageToFile(FLAGS_out[0], dst, SkEncodedImageFormat::kPNG, 100)) {
         if (!FLAGS_quiet) {
             SkDebugf("Couldn't write to file: %s\n", FLAGS_out[0]);
         }
diff --git a/tools/picture_utils.cpp b/tools/picture_utils.cpp
index 43ca2f0..84925b4 100644
--- a/tools/picture_utils.cpp
+++ b/tools/picture_utils.cpp
@@ -17,6 +17,8 @@
 #include "SkStream.h"
 #include "SkString.h"
 
+#include "sk_tool_utils.h"
+
 namespace sk_tools {
     void force_all_opaque(const SkBitmap& bitmap) {
         SkASSERT(kN32_SkColorType == bitmap.colorType());
@@ -63,7 +65,7 @@
             partialPath.set(dirPath);
         }
         SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str());
-        if (SkImageEncoder::EncodeFile(fullPath.c_str(), bm, SkImageEncoder::kPNG_Type, 100)) {
+        if (sk_tool_utils::EncodeImageToFile(fullPath.c_str(), bm, SkEncodedImageFormat::kPNG, 100)) {
             return true;
         } else {
             SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str());
diff --git a/tools/sk_tool_utils.h b/tools/sk_tool_utils.h
index c6abbcf..526f0ef 100644
--- a/tools/sk_tool_utils.h
+++ b/tools/sk_tool_utils.h
@@ -225,6 +225,31 @@
         SkTDArray<TopoTestNode*> fDependencies;
     };
 
+    template <typename T>
+    inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) {
+        SkFILEWStream file(path);
+        return file.isValid() && SkEncodeImage(&file, src, f, q);
+    }
+
+    template <typename T>
+    inline sk_sp<SkData> EncodeImageToData(const T& src, SkEncodedImageFormat f, int q) {
+        SkDynamicMemoryWStream buf;
+        return SkEncodeImage(&buf, src , f, q) ? buf.detachAsData() : nullptr;
+    }
+
+    /**
+     * Uses SkEncodeImage to serialize images that are not already
+     * encoded as SkEncodedImageFormat::kPNG images.
+     */
+    inline sk_sp<SkPixelSerializer> MakePixelSerializer() {
+        struct EncodeImagePixelSerializer final : SkPixelSerializer {
+            bool onUseEncodedData(const void*, size_t) override { return true; }
+            SkData* onEncode(const SkPixmap& pmap) override {
+                return EncodeImageToData(pmap, SkEncodedImageFormat::kPNG, 100).release();
+            }
+        };
+        return sk_make_sp<EncodeImagePixelSerializer>();
+    }
 }  // namespace sk_tool_utils
 
 #endif  // sk_tool_utils_DEFINED
diff --git a/tools/skdiff/skdiff_utils.cpp b/tools/skdiff/skdiff_utils.cpp
index f788ec8..f8eed90 100644
--- a/tools/skdiff/skdiff_utils.cpp
+++ b/tools/skdiff/skdiff_utils.cpp
@@ -6,6 +6,7 @@
  */
 #include "skdiff.h"
 #include "skdiff_utils.h"
+#include "sk_tool_utils.h"
 #include "SkBitmap.h"
 #include "SkCodec.h"
 #include "SkData.h"
@@ -82,8 +83,8 @@
     SkBitmap copy;
     bitmap.copyTo(&copy, kN32_SkColorType);
     force_all_opaque(copy);
-    return SkImageEncoder::EncodeFile(path.c_str(), copy,
-                                      SkImageEncoder::kPNG_Type, 100);
+    return sk_tool_utils::EncodeImageToFile(path.c_str(), copy,
+                                      SkEncodedImageFormat::kPNG, 100);
 }
 
 /// Return a copy of the "input" string, within which we have replaced all instances
diff --git a/tools/skiaserve/Request.cpp b/tools/skiaserve/Request.cpp
index c1b3b25..2064a46 100644
--- a/tools/skiaserve/Request.cpp
+++ b/tools/skiaserve/Request.cpp
@@ -11,6 +11,7 @@
 #include "SkPixelSerializer.h"
 #include "SkPM4fPriv.h"
 #include "picture_utils.h"
+#include "sk_tool_utils.h"
 
 using namespace sk_gpu_test;
 
@@ -117,7 +118,7 @@
 
     SkDynamicMemoryWStream outStream;
 
-    sk_sp<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer());
+    sk_sp<SkPixelSerializer> serializer = sk_tool_utils::MakePixelSerializer();
     picture->serialize(&outStream, serializer.get());
 
     return outStream.detachAsData();