SkEncodeImage: no more link-time registration

Also, no more SkImageEncoder class.

SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS now only guards some
old API shims.

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=5006

Change-Id: I3797f584f3e8e12ade10d31e8733163453725f40
Reviewed-on: https://skia-review.googlesource.com/5006
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Leon Scroggins <scroggo@google.com>
Reviewed-by: Mike Reed <reed@google.com>
diff --git a/src/ports/SkImageEncoder_CG.cpp b/src/ports/SkImageEncoder_CG.cpp
index 0c3d22d..b3fd243 100644
--- a/src/ports/SkImageEncoder_CG.cpp
+++ b/src/ports/SkImageEncoder_CG.cpp
@@ -5,14 +5,14 @@
  * found in the LICENSE file.
  */
 
-#include "SkTypes.h"
+#include "SkImageEncoderPriv.h"
+
 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
 
 #include "SkBitmap.h"
 #include "SkCGUtils.h"
 #include "SkColorPriv.h"
 #include "SkData.h"
-#include "SkImageEncoderPriv.h"
 #include "SkStream.h"
 #include "SkStreamPriv.h"
 #include "SkTemplates.h"
@@ -57,29 +57,19 @@
     return CGImageDestinationCreateWithDataConsumer(consumer, type, 1, nullptr);
 }
 
-class SkImageEncoder_CG : public SkImageEncoder {
-public:
-    SkImageEncoder_CG(SkEncodedImageFormat t) : fType(t) {}
-
-protected:
-    virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality);
-
-private:
-    SkEncodedImageFormat fType;
-};
-
 /*  Encode bitmaps via CGImageDestination. We setup a DataConsumer which writes
     to our SkWStream. Since we don't reference/own the SkWStream, our consumer
     must only live for the duration of the onEncode() method.
  */
-bool SkImageEncoder_CG::onEncode(SkWStream* stream, const SkBitmap& bm,
-                                 int quality) {
-    // Used for converting a bitmap to 8888.
-    const SkBitmap* bmPtr = &bm;
-    SkBitmap bitmap8888;
+bool SkEncodeImageWithCG(SkWStream* stream, const SkPixmap& pixmap, SkEncodedImageFormat format) {
+    SkBitmap bm;
+    if (!bm.installPixels(pixmap)) {
+        return false;
+    }
+    bm.setImmutable();
 
     CFStringRef type;
-    switch (fType) {
+    switch (format) {
         case SkEncodedImageFormat::kICO:
             type = kUTTypeICO;
             break;
@@ -99,8 +89,9 @@
             // <Error>: CGImageDestinationFinalize image destination does not have enough images
             // So instead we copy to 8888.
             if (bm.colorType() == kARGB_4444_SkColorType) {
+                SkBitmap bitmap8888;
                 bm.copyTo(&bitmap8888, kN32_SkColorType);
-                bmPtr = &bitmap8888;
+                bm.swap(bitmap8888);
             }
             type = kUTTypePNG;
             break;
@@ -114,7 +105,7 @@
     }
     SkAutoTCallVProc<const void, CFRelease> ardst(dst);
 
-    CGImageRef image = SkCreateCGImageRef(*bmPtr);
+    CGImageRef image = SkCreateCGImageRef(bm);
     if (nullptr == image) {
         return false;
     }
@@ -124,28 +115,4 @@
     return CGImageDestinationFinalize(dst);
 }
 
-///////////////////////////////////////////////////////////////////////////////
-
-#ifdef SK_USE_CG_ENCODER
-static SkImageEncoder* sk_imageencoder_cg_factory(SkEncodedImageFormat t) {
-    switch (t) {
-        case SkEncodedImageFormat::kICO:
-        case SkEncodedImageFormat::kBMP:
-        case SkEncodedImageFormat::kGIF:
-        case SkEncodedImageFormat::kJPEG:
-        case SkEncodedImageFormat::kPNG:
-            break;
-        default:
-            return nullptr;
-    }
-    return new SkImageEncoder_CG(t);
-}
-
-static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_cg_factory);
-#endif
-
-SkImageEncoder* CreateImageEncoder_CG(SkImageEncoder::Type type) {
-    return new SkImageEncoder_CG((SkEncodedImageFormat)type);
-}
-
 #endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
diff --git a/src/ports/SkImageEncoder_WIC.cpp b/src/ports/SkImageEncoder_WIC.cpp
index 035359e..52f4599 100644
--- a/src/ports/SkImageEncoder_WIC.cpp
+++ b/src/ports/SkImageEncoder_WIC.cpp
@@ -46,23 +46,10 @@
 #undef CLSID_WICImagingFactory
 #endif
 
-class SkImageEncoder_WIC : public SkImageEncoder {
-public:
-    SkImageEncoder_WIC(SkEncodedImageFormat t) : fType(t) {}
-
-protected:
-    virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality);
-
-private:
-    SkEncodedImageFormat fType;
-};
-
-bool SkImageEncoder_WIC::onEncode(SkWStream* stream
-                                , const SkBitmap& bitmapOrig
-                                , int quality)
-{
+bool SkEncodeImageWithWIC(SkWStream* stream, const SkPixmap& pixmap,
+                          SkEncodedImageFormat format, int quality) {
     GUID type;
-    switch (fType) {
+    switch (format) {
         case SkEncodedImageFormat::kJPEG:
             type = GUID_ContainerFormatJpeg;
             break;
@@ -72,6 +59,11 @@
         default:
             return false;
     }
+    SkBitmap bitmapOrig;
+    if (!bitmapOrig.installPixels(pixmap)) {
+        return false;
+    }
+    bitmapOrig.setImmutable();
 
     // First convert to BGRA if necessary.
     SkBitmap bitmap;
@@ -97,7 +89,7 @@
     size_t rowBytes = bitmap.rowBytes();
     SkAutoMalloc pixelStorage;
     WICPixelFormatGUID formatDesired = GUID_WICPixelFormat32bppBGRA;
-    if (SkEncodedImageFormat::kJPEG == fType) {
+    if (SkEncodedImageFormat::kJPEG == format) {
         formatDesired = GUID_WICPixelFormat24bppBGR;
         rowBytes = SkAlign4(bitmap.width() * 3);
         pixelStorage.reset(rowBytes * bitmap.height());
@@ -216,25 +208,4 @@
     return SUCCEEDED(hr);
 }
 
-///////////////////////////////////////////////////////////////////////////////
-
-#ifdef SK_USE_WIC_ENCODER
-static SkImageEncoder* sk_imageencoder_wic_factory(SkEncodedImageFormat t) {
-    switch (t) {
-        case SkEncodedImageFormat::kPNG:
-        case SkEncodedImageFormat::kJPEG:
-            break;
-        default:
-            return nullptr;
-    }
-    return new SkImageEncoder_WIC(t);
-}
-
-static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_wic_factory);
-#endif
-
-SkImageEncoder* CreateImageEncoder_WIC(SkImageEncoder::Type type) {
-    return new SkImageEncoder_WIC((SkEncodedImageFormat)type);
-}
-
 #endif // defined(SK_BUILD_FOR_WIN32)