Explicitly encode SkImages with Png encoder

Skia plans to stop the default behavior of encoding SkImages in
SkPictures using a PNG encoder. To preserve that behavior,
clients need to explicitly set an SkSerialProc to do so.

The only other call which I could find that made use of the
implicit SkPicture::serialize behavior was Picture [1].
The API is "soft removed" and thus we don't need to maintain
the behavior of serializing or deserialization as long as
it does not crash (which it should not).

[1] frameworks/base/libs/hwui/jni/Picture.cpp

See also: http://review.skia.org/753496

Bug: b/40045064

Change-Id: I2df9bffc5d1046869b5971b0fdd6ff4f1a2216af
diff --git a/libs/hwui/pipeline/skia/SkiaPipeline.cpp b/libs/hwui/pipeline/skia/SkiaPipeline.cpp
index 6679f8f..e0f1f6e 100644
--- a/libs/hwui/pipeline/skia/SkiaPipeline.cpp
+++ b/libs/hwui/pipeline/skia/SkiaPipeline.cpp
@@ -18,6 +18,7 @@
 
 #include <include/android/SkSurfaceAndroid.h>
 #include <include/gpu/ganesh/SkSurfaceGanesh.h>
+#include <include/encode/SkPngEncoder.h>
 #include <SkCanvas.h>
 #include <SkColor.h>
 #include <SkColorSpace.h>
@@ -440,6 +441,13 @@
                 procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){
                     return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
                 };
+                procs.fImageProc = [](SkImage* img, void* ctx) -> sk_sp<SkData> {
+                    GrDirectContext* dCtx = static_cast<GrDirectContext*>(ctx);
+                    return SkPngEncoder::Encode(dCtx,
+                                                img,
+                                                SkPngEncoder::Options{});
+                };
+                procs.fImageCtx = mRenderThread.getGrContext();
                 auto data = picture->serialize(&procs);
                 savePictureAsync(data, mCapturedFile);
                 mCaptureSequence = 0;