Add support for row-by-row jpeg encoding

Bug: 713862
Change-Id: I787b7c49662a00b89ae0ef35845dfbd6be3e6fb1
Reviewed-on: https://skia-review.googlesource.com/14641
Commit-Queue: Matt Sarett <msarett@google.com>
Reviewed-by: Leon Scroggins <scroggo@google.com>
diff --git a/gm/encode-platform.cpp b/gm/encode-platform.cpp
index f08da73..f53ebc0 100644
--- a/gm/encode-platform.cpp
+++ b/gm/encode-platform.cpp
@@ -11,6 +11,7 @@
 #include "SkCanvas.h"
 #include "SkData.h"
 #include "SkImageEncoderPriv.h"
+#include "SkJpegEncoder.h"
 #include "SkUnPreMultiply.h"
 
 namespace skiagm {
@@ -70,8 +71,10 @@
             case SkEncodedImageFormat::kPNG:
                 return SkEncodeImageAsPNG(&buf, src, SkEncodeOptions()) ? buf.detachAsData()
                                                                         : nullptr;
-            case SkEncodedImageFormat::kJPEG:
-                return SkEncodeImageAsJPEG(&buf, src, 100) ? buf.detachAsData() : nullptr;
+            case SkEncodedImageFormat::kJPEG: {
+                bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options());
+                return success ? buf.detachAsData() : nullptr;
+            }
             case SkEncodedImageFormat::kWEBP:
                 return SkEncodeImageAsWEBP(&buf, src, 100) ? buf.detachAsData() : nullptr;
             default:
diff --git a/gm/encode-srgb.cpp b/gm/encode-srgb.cpp
index 3015baa..fef1332 100644
--- a/gm/encode-srgb.cpp
+++ b/gm/encode-srgb.cpp
@@ -13,6 +13,7 @@
 #include "SkColorSpace_Base.h"
 #include "SkData.h"
 #include "SkImageEncoderPriv.h"
+#include "SkJpegEncoder.h"
 #include "SkPM4f.h"
 #include "SkSRGB.h"
 
@@ -128,7 +129,7 @@
             SkAssertResult(SkEncodeImageAsWEBP(&buf, src, options));
             break;
         case SkEncodedImageFormat::kJPEG:
-            SkAssertResult(SkEncodeImageAsJPEG(&buf, src, options));
+            SkAssertResult(SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()));
             break;
         default:
             break;
diff --git a/gn/tests.gni b/gn/tests.gni
index ffd8b41..be4389b 100644
--- a/gn/tests.gni
+++ b/gn/tests.gni
@@ -63,6 +63,7 @@
   "$_tests/DynamicHashTest.cpp",
   "$_tests/EGLImageTest.cpp",
   "$_tests/EmptyPathTest.cpp",
+  "$_tests/EncodeTest.cpp",
   "$_tests/ExifTest.cpp",
   "$_tests/F16StagesTest.cpp",
   "$_tests/FillPathTest.cpp",
diff --git a/src/core/SkImageInfoPriv.h b/src/core/SkImageInfoPriv.h
index 855e506..a0c1b34 100644
--- a/src/core/SkImageInfoPriv.h
+++ b/src/core/SkImageInfoPriv.h
@@ -11,10 +11,10 @@
 #include "SkImageInfo.h"
 
 /**
- *  Returns true if |info| contains a valid combination of width, height, colorType, alphaType,
- *  colorSpace.  Returns false otherwise.
+ *  This contains shared checks on SkImageInfo.  Depending on the desired color space behavior,
+ *  the caller should choose one of the two versions below.
  */
-static inline bool SkImageInfoIsValid(const SkImageInfo& info) {
+static inline bool SkImageInfoIsValidCommon(const SkImageInfo& info) {
     if (info.width() <= 0 || info.height() <= 0) {
         return false;
     }
@@ -38,6 +38,35 @@
         return false;
     }
 
+    return true;
+}
+
+/**
+ *  Returns true if |info| contains a valid combination of width, height, colorType, alphaType,
+ *  colorSpace.  Allows numerical color spaces.  Returns false otherwise.
+ */
+static inline bool SkImageInfoIsValidAllowNumericalCS(const SkImageInfo& info) {
+    if (!SkImageInfoIsValidCommon(info)) {
+        return false;
+    }
+
+    SkColorSpaceTransferFn fn;
+    if (info.colorSpace() && !info.colorSpace()->isNumericalTransferFn(&fn)) {
+        return false;
+    }
+
+    return true;
+}
+
+/**
+ *  Returns true if |info| contains a valid combination of width, height, colorType, alphaType,
+ *  colorSpace.  Only supports rendering color spaces.  Returns false otherwise.
+ */
+static inline bool SkImageInfoIsValidRenderingCS(const SkImageInfo& info) {
+    if (!SkImageInfoIsValidCommon(info)) {
+        return false;
+    }
+
     if (info.colorSpace() &&
        (!info.colorSpace()->gammaCloseToSRGB() && !info.colorSpace()->gammaIsLinear())) {
         return false;
@@ -65,7 +94,7 @@
  *      conversion is not well-defined.
  */
 static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) {
-    if (!SkImageInfoIsValid(dst) || !SkImageInfoIsValid(src)) {
+    if (!SkImageInfoIsValidRenderingCS(dst) || !SkImageInfoIsValidRenderingCS(src)) {
         return false;
     }
 
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index 5401a10..895fdec 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -176,7 +176,7 @@
 sk_sp<GrTextureProxy> GrUploadPixmapToTextureProxy(GrResourceProvider* resourceProvider,
                                                    const SkPixmap& pixmap,
                                                    SkBudgeted budgeted) {
-    if (!SkImageInfoIsValid(pixmap.info())) {
+    if (!SkImageInfoIsValidRenderingCS(pixmap.info())) {
         return nullptr;
     }
 
@@ -205,7 +205,7 @@
         ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
         : SkDestinationSurfaceColorMode::kLegacy;
 
-    if (!SkImageInfoIsValid(bitmap.info())) {
+    if (!SkImageInfoIsValidRenderingCS(bitmap.info())) {
         return nullptr;
     }
 
@@ -252,7 +252,7 @@
                                                    const GrMipLevel* texels,
                                                    int mipLevelCount,
                                                    SkDestinationSurfaceColorMode colorMode) {
-    if (!SkImageInfoIsValid(info)) {
+    if (!SkImageInfoIsValidRenderingCS(info)) {
         return nullptr;
     }
 
diff --git a/src/images/SkImageEncoder.cpp b/src/images/SkImageEncoder.cpp
index fecadbf..23f46e4 100644
--- a/src/images/SkImageEncoder.cpp
+++ b/src/images/SkImageEncoder.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "SkImageEncoderPriv.h"
+#include "SkJpegEncoder.h"
 
 bool SkEncodeImage(SkWStream* dst, const SkPixmap& src,
                    SkEncodedImageFormat format, int quality) {
@@ -16,8 +17,11 @@
         return SkEncodeImageWithWIC(dst, src, format, quality);
     #else
         switch(format) {
-            case SkEncodedImageFormat::kJPEG:
-                return SkEncodeImageAsJPEG(dst, src, quality);
+            case SkEncodedImageFormat::kJPEG: {
+                SkJpegEncoder::Options opts;
+                opts.fQuality = quality;
+                return SkJpegEncoder::Encode(dst, src, opts);
+            }
             case SkEncodedImageFormat::kPNG:
                 return SkEncodeImageAsPNG(dst, src, SkEncodeOptions());
             case SkEncodedImageFormat::kWEBP:
diff --git a/src/images/SkImageEncoderPriv.h b/src/images/SkImageEncoderPriv.h
index 540d930..5e57cd1 100644
--- a/src/images/SkImageEncoderPriv.h
+++ b/src/images/SkImageEncoderPriv.h
@@ -14,13 +14,6 @@
     SkTransferFunctionBehavior fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore;
 };
 
-#ifdef SK_HAS_JPEG_LIBRARY
-    bool SkEncodeImageAsJPEG(SkWStream*, const SkPixmap&, const SkEncodeOptions&);
-    bool SkEncodeImageAsJPEG(SkWStream*, const SkPixmap&, int quality);
-#else
-    #define SkEncodeImageAsJPEG(...) false
-#endif
-
 #ifdef SK_HAS_PNG_LIBRARY
     bool SkEncodeImageAsPNG(SkWStream*, const SkPixmap&, const SkEncodeOptions&);
 #else
diff --git a/src/images/SkJPEGImageEncoder.cpp b/src/images/SkJPEGImageEncoder.cpp
index 764e9c4..b875898 100644
--- a/src/images/SkJPEGImageEncoder.cpp
+++ b/src/images/SkJPEGImageEncoder.cpp
@@ -11,6 +11,8 @@
 
 #include "SkColorPriv.h"
 #include "SkImageEncoderFns.h"
+#include "SkImageInfoPriv.h"
+#include "SkJpegEncoder.h"
 #include "SkJPEGWriteUtility.h"
 #include "SkStream.h"
 #include "SkTemplates.h"
@@ -22,122 +24,144 @@
     #include "jerror.h"
 }
 
-/**
- *  Returns true if |info| is supported by the jpeg encoder and false otherwise.
- *  |jpegColorType| will be set to the proper libjpeg-turbo type for input to the library.
- *  |numComponents| will be set to the number of components in the |jpegColorType|.
- *  |proc|          will be set if we need to pre-convert the input before passing to
- *                  libjpeg-turbo.  Otherwise will be set to nullptr.
- */
-// TODO (skbug.com/1501):
-// Should we fail on non-opaque encodes?
-// Or should we change alpha behavior (ex: unpremultiply when the input is premul)?
-// Or is ignoring the alpha type and alpha channel ok here?
-static bool set_encode_config(J_COLOR_SPACE* jpegColorType, int* numComponents,
-                              transform_scanline_proc* proc, const SkImageInfo& info) {
-    *proc = nullptr;
-    switch (info.colorType()) {
+// This warning triggers false postives way too often in here.
+#if defined(__GNUC__) && !defined(__clang__)
+    #pragma GCC diagnostic ignored "-Wclobbered"
+#endif
+
+class SkJpegEncoderMgr : SkNoncopyable {
+public:
+
+    /*
+     * Create the decode manager
+     * Does not take ownership of stream
+     */
+    static std::unique_ptr<SkJpegEncoderMgr> Make(SkWStream* stream) {
+        return std::unique_ptr<SkJpegEncoderMgr>(new SkJpegEncoderMgr(stream));
+    }
+
+    bool setParams(const SkImageInfo& srcInfo);
+
+    jpeg_compress_struct* cinfo() { return &fCInfo; }
+
+    jmp_buf& jmpBuf() { return fErrMgr.fJmpBuf; }
+
+    transform_scanline_proc proc() const { return fProc; }
+
+    ~SkJpegEncoderMgr() {
+        jpeg_destroy_compress(&fCInfo);
+    }
+
+private:
+
+    SkJpegEncoderMgr(SkWStream* stream)
+        : fDstMgr(stream)
+        , fProc(nullptr)
+    {
+        fCInfo.err = jpeg_std_error(&fErrMgr);
+        fErrMgr.error_exit = skjpeg_error_exit;
+        jpeg_create_compress(&fCInfo);
+        fCInfo.dest = &fDstMgr;
+    }
+
+    jpeg_compress_struct    fCInfo;
+    skjpeg_error_mgr        fErrMgr;
+    skjpeg_destination_mgr  fDstMgr;
+    transform_scanline_proc fProc;
+};
+
+bool SkJpegEncoderMgr::setParams(const SkImageInfo& srcInfo) {
+    J_COLOR_SPACE jpegColorType = JCS_EXT_RGBA;
+    int numComponents = 0;
+    switch (srcInfo.colorType()) {
         case kRGBA_8888_SkColorType:
-            *jpegColorType = JCS_EXT_RGBA;
-            *numComponents = 4;
-            return true;
+            jpegColorType = JCS_EXT_RGBA;
+            numComponents = 4;
+            break;
         case kBGRA_8888_SkColorType:
-            *jpegColorType = JCS_EXT_BGRA;
-            *numComponents = 4;
-            return true;
+            jpegColorType = JCS_EXT_BGRA;
+            numComponents = 4;
+            break;
         case kRGB_565_SkColorType:
-            *proc = transform_scanline_565;
-            *jpegColorType = JCS_RGB;
-            *numComponents = 3;
-            return true;
+            fProc = transform_scanline_565;
+            jpegColorType = JCS_RGB;
+            numComponents = 3;
+            break;
         case kARGB_4444_SkColorType:
-            *proc = transform_scanline_444;
-            *jpegColorType = JCS_RGB;
-            *numComponents = 3;
-            return true;
+            fProc = transform_scanline_444;
+            jpegColorType = JCS_RGB;
+            numComponents = 3;
+            break;
         case kIndex_8_SkColorType:
-            *proc = transform_scanline_index8_opaque;
-            *jpegColorType = JCS_RGB;
-            *numComponents = 3;
-            return true;
+            fProc = transform_scanline_index8_opaque;
+            jpegColorType = JCS_RGB;
+            numComponents = 3;
+            break;
         case kGray_8_SkColorType:
-            SkASSERT(info.isOpaque());
-            *jpegColorType = JCS_GRAYSCALE;
-            *numComponents = 1;
-            return true;
+            SkASSERT(srcInfo.isOpaque());
+            jpegColorType = JCS_GRAYSCALE;
+            numComponents = 1;
+            break;
         case kRGBA_F16_SkColorType:
-            if (!info.colorSpace() || !info.colorSpace()->gammaIsLinear()) {
+            if (!srcInfo.colorSpace() || !srcInfo.colorSpace()->gammaIsLinear()) {
                 return false;
             }
 
-            *proc = transform_scanline_F16_to_8888;
-            *jpegColorType = JCS_EXT_RGBA;
-            *numComponents = 4;
-            return true;
+            fProc = transform_scanline_F16_to_8888;
+            jpegColorType = JCS_EXT_RGBA;
+            numComponents = 4;
+            break;
         default:
             return false;
     }
 
-
-}
-
-bool SkEncodeImageAsJPEG(SkWStream* stream, const SkPixmap& pixmap, const SkEncodeOptions& opts) {
-    if (SkTransferFunctionBehavior::kRespect == opts.fUnpremulBehavior) {
-        // Respecting the transfer function requries a color space.  It's not actually critical
-        // in the jpeg case (since jpegs are opaque), but Skia color correct behavior generally
-        // requires pixels to be tagged with color spaces.
-        if (!pixmap.colorSpace() || (!pixmap.colorSpace()->gammaCloseToSRGB() &&
-                                     !pixmap.colorSpace()->gammaIsLinear())) {
-            return false;
-        }
-    }
-
-    return SkEncodeImageAsJPEG(stream, pixmap, 100);
-}
-
-bool SkEncodeImageAsJPEG(SkWStream* stream, const SkPixmap& pixmap, int quality) {
-    if (!pixmap.addr()) {
-        return false;
-    }
-    jpeg_compress_struct    cinfo;
-    skjpeg_error_mgr        sk_err;
-    skjpeg_destination_mgr  sk_wstream(stream);
-
-    // Declare before calling setjmp.
-    SkAutoTMalloc<uint8_t>  storage;
-
-    cinfo.err = jpeg_std_error(&sk_err);
-    sk_err.error_exit = skjpeg_error_exit;
-    if (setjmp(sk_err.fJmpBuf)) {
-        return false;
-    }
-
-    J_COLOR_SPACE jpegColorSpace;
-    int numComponents;
-    transform_scanline_proc proc;
-    if (!set_encode_config(&jpegColorSpace, &numComponents, &proc, pixmap.info())) {
-        return false;
-    }
-
-    jpeg_create_compress(&cinfo);
-    cinfo.dest = &sk_wstream;
-    cinfo.image_width = pixmap.width();
-    cinfo.image_height = pixmap.height();
-    cinfo.input_components = numComponents;
-    cinfo.in_color_space = jpegColorSpace;
-
-    jpeg_set_defaults(&cinfo);
+    fCInfo.image_width = srcInfo.width();
+    fCInfo.image_height = srcInfo.height();
+    fCInfo.in_color_space = jpegColorType;
+    fCInfo.input_components = numComponents;
+    jpeg_set_defaults(&fCInfo);
 
     // Tells libjpeg-turbo to compute optimal Huffman coding tables
     // for the image.  This improves compression at the cost of
     // slower encode performance.
-    cinfo.optimize_coding = TRUE;
-    jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
+    fCInfo.optimize_coding = TRUE;
+    return true;
+}
 
-    jpeg_start_compress(&cinfo, TRUE);
+class SkJpegEncoder_Base : public SkJpegEncoder {
+public:
+    SkJpegEncoder_Base(std::unique_ptr<SkJpegEncoderMgr> encoderMgr, const SkPixmap& src);
 
-    if (pixmap.colorSpace()) {
-        sk_sp<SkData> icc = icc_from_color_space(*pixmap.colorSpace());
+    bool onEncodeRows(int numRows);
+
+private:
+    std::unique_ptr<SkJpegEncoderMgr> fEncoderMgr;
+    SkPixmap                          fSrc;
+    int                               fCurrRow;
+    SkAutoTMalloc<uint8_t>            fStorage;
+};
+
+std::unique_ptr<SkJpegEncoder> SkJpegEncoder::Make(SkWStream* dst, const SkPixmap& src,
+                                                   const Options& options) {
+    if (!SkImageInfoIsValidAllowNumericalCS(src.info()) || !src.addr() ||
+            src.rowBytes() < src.info().minRowBytes()) {
+        return nullptr;
+    }
+
+    std::unique_ptr<SkJpegEncoderMgr> encoderMgr = SkJpegEncoderMgr::Make(dst);
+    if (setjmp(encoderMgr->jmpBuf())) {
+        return nullptr;
+    }
+
+    if (!encoderMgr->setParams(src.info())) {
+        return nullptr;
+    }
+
+    jpeg_set_quality(encoderMgr->cinfo(), options.fQuality, TRUE);
+    jpeg_start_compress(encoderMgr->cinfo(), TRUE);
+
+    if (src.colorSpace()) {
+        sk_sp<SkData> icc = icc_from_color_space(*src.colorSpace());
         if (icc) {
             // Create a contiguous block of memory with the icc signature followed by the profile.
             sk_sp<SkData> markerData =
@@ -149,30 +173,68 @@
             *ptr++ = 1; // Out of one total markers.
             memcpy(ptr, icc->data(), icc->size());
 
-            jpeg_write_marker(&cinfo, kICCMarker, markerData->bytes(), markerData->size());
+            jpeg_write_marker(encoderMgr->cinfo(), kICCMarker, markerData->bytes(),
+                              markerData->size());
         }
     }
 
-    if (proc) {
-        storage.reset(numComponents * pixmap.width());
+    return std::unique_ptr<SkJpegEncoder>(new SkJpegEncoder_Base(std::move(encoderMgr), src));
+}
+
+
+SkJpegEncoder_Base::SkJpegEncoder_Base(std::unique_ptr<SkJpegEncoderMgr> encoderMgr,
+                                       const SkPixmap& src)
+    : fEncoderMgr(std::move(encoderMgr))
+    , fSrc(src)
+    , fCurrRow(0)
+    , fStorage(fEncoderMgr->proc() ? fEncoderMgr->cinfo()->input_components*src.width() : 0)
+{}
+
+bool SkJpegEncoder::encodeRows(int numRows) {
+    return ((SkJpegEncoder_Base*) this)->onEncodeRows(numRows);
+}
+
+bool SkJpegEncoder_Base::onEncodeRows(int numRows) {
+    SkASSERT(numRows > 0 && fCurrRow < fSrc.height());
+    if (numRows <= 0 || fCurrRow >= fSrc.height()) {
+        return false;
     }
 
-    const void* srcRow = pixmap.addr();
-    const SkPMColor* colors = pixmap.ctable() ? pixmap.ctable()->readColors() : nullptr;
-    while (cinfo.next_scanline < cinfo.image_height) {
+    if (fCurrRow + numRows > fSrc.height()) {
+        numRows = fSrc.height() - fCurrRow;
+    }
+
+    if (setjmp(fEncoderMgr->jmpBuf())) {
+        // Short circuit any future calls after failing.
+        fCurrRow = fSrc.height();
+        return false;
+    }
+
+    const void* srcRow = fSrc.addr(0, fCurrRow);
+    const SkPMColor* colors = fSrc.ctable() ? fSrc.ctable()->readColors() : nullptr;
+    for (int i = 0; i < numRows; i++) {
         JSAMPLE* jpegSrcRow = (JSAMPLE*) srcRow;
-        if (proc) {
-            proc((char*)storage.get(), (const char*)srcRow, pixmap.width(), numComponents, colors);
-            jpegSrcRow = storage.get();
+        if (fEncoderMgr->proc()) {
+            fEncoderMgr->proc()((char*)fStorage.get(), (const char*)srcRow, fSrc.width(),
+                                fEncoderMgr->cinfo()->input_components, colors);
+            jpegSrcRow = fStorage.get();
         }
 
-        (void) jpeg_write_scanlines(&cinfo, &jpegSrcRow, 1);
-        srcRow = SkTAddOffset<const void>(srcRow, pixmap.rowBytes());
+        jpeg_write_scanlines(fEncoderMgr->cinfo(), &jpegSrcRow, 1);
+        srcRow = SkTAddOffset<const void>(srcRow, fSrc.rowBytes());
     }
 
-    jpeg_finish_compress(&cinfo);
-    jpeg_destroy_compress(&cinfo);
+    fCurrRow += numRows;
+    if (fCurrRow == fSrc.height()) {
+        jpeg_finish_compress(fEncoderMgr->cinfo());
+    }
 
     return true;
 }
+
+bool SkJpegEncoder::Encode(SkWStream* dst, const SkPixmap& src, const Options& options) {
+    auto encoder = SkJpegEncoder::Make(dst, src, options);
+    return encoder && encoder->encodeRows(src.height());
+}
+
 #endif
diff --git a/src/images/SkJpegEncoder.h b/src/images/SkJpegEncoder.h
new file mode 100644
index 0000000..0c41acc
--- /dev/null
+++ b/src/images/SkJpegEncoder.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkJpegEncoder_DEFINED
+#define SkJpegEncoder_DEFINED
+
+#include "SkPixmap.h"
+#include "SkTypes.h"
+
+class SkWStream;
+
+class SkJpegEncoder : SkNoncopyable {
+public:
+
+    // TODO (skbug.com/1501):
+    // Since jpegs are always opaque, this encoder ignores the alpha channel and treats the
+    // pixels as opaque.
+    // Another possible behavior is to blend the pixels onto opaque black.  We'll need to add
+    // an option for this - and an SkTransferFunctionBehavior.
+
+    struct Options {
+        /**
+         * |fQuality| must be in [0, 100] where 0 corresponds to the lowest quality.
+         */
+        int fQuality = 100;
+    };
+
+    /**
+     *  Encode the |src| pixels to the |dst| stream.
+     *  |options| may be used to control the encoding behavior.
+     *
+     *  Returns true on success.  Returns false on an invalid or unsupported |src|.
+     */
+    static bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options);
+
+    /**
+     *  Create a jpeg encoder that will encode the |src| pixels to the |dst| stream.
+     *  |options| may be used to control the encoding behavior.
+     *
+     *  |dst| is unowned but must remain valid for the lifetime of the object.
+     *
+     *  This returns nullptr on an invalid or unsupported |src|.
+     */
+    static std::unique_ptr<SkJpegEncoder> Make(SkWStream* dst, const SkPixmap& src,
+                                               const Options& options);
+
+    /**
+     *  Encode |numRows| rows of input.  If the caller requests more rows than are remaining
+     *  in the src, this will encode all of the remaining rows.  |numRows| must be greater
+     *  than zero.
+     */
+    bool encodeRows(int numRows);
+};
+
+#endif
diff --git a/tests/CodecTest.cpp b/tests/CodecTest.cpp
index 4a13533..45362db 100644
--- a/tests/CodecTest.cpp
+++ b/tests/CodecTest.cpp
@@ -20,6 +20,7 @@
 #include "SkImageEncoderPriv.h"
 #include "SkMD5.h"
 #include "SkOSPath.h"
+#include "SkJpegEncoder.h"
 #include "SkPngChunkReader.h"
 #include "SkRandom.h"
 #include "SkStream.h"
@@ -1530,7 +1531,7 @@
             SkEncodeImageAsPNG(stream, pixmap, opts);
             break;
         case SkEncodedImageFormat::kJPEG:
-            SkEncodeImageAsJPEG(stream, pixmap, opts);
+            SkJpegEncoder::Encode(stream, pixmap, SkJpegEncoder::Options());
             break;
         case SkEncodedImageFormat::kWEBP:
             SkEncodeImageAsWEBP(stream, pixmap, opts);
diff --git a/tests/EncodeTest.cpp b/tests/EncodeTest.cpp
new file mode 100644
index 0000000..9db5039
--- /dev/null
+++ b/tests/EncodeTest.cpp
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "Resources.h"
+#include "Test.h"
+
+#include "SkBitmap.h"
+#include "SkJpegEncoder.h"
+#include "SkStream.h"
+
+DEF_TEST(Encode_Jpeg, r) {
+    SkBitmap bitmap;
+    bool success = GetResourceAsBitmap("mandrill_128.png", &bitmap);
+    if (!success) {
+        return;
+    }
+
+    SkPixmap src;
+    success = bitmap.peekPixels(&src);
+    REPORTER_ASSERT(r, success);
+    if (!success) {
+        return;
+    }
+
+    SkDynamicMemoryWStream dst0, dst1, dst2, dst3;
+    success = SkJpegEncoder::Encode(&dst0, src, SkJpegEncoder::Options());
+    REPORTER_ASSERT(r, success);
+
+    auto encoder1 = SkJpegEncoder::Make(&dst1, src, SkJpegEncoder::Options());
+    for (int i = 0; i < src.height(); i++) {
+        success = encoder1->encodeRows(1);
+        REPORTER_ASSERT(r, success);
+    }
+
+    auto encoder2 = SkJpegEncoder::Make(&dst2, src, SkJpegEncoder::Options());
+    for (int i = 0; i < src.height(); i+=3) {
+        success = encoder2->encodeRows(3);
+        REPORTER_ASSERT(r, success);
+    }
+
+    auto encoder3 = SkJpegEncoder::Make(&dst3, src, SkJpegEncoder::Options());
+    success = encoder3->encodeRows(200);
+    REPORTER_ASSERT(r, success);
+
+    sk_sp<SkData> data0 = dst0.detachAsData();
+    sk_sp<SkData> data1 = dst1.detachAsData();
+    sk_sp<SkData> data2 = dst2.detachAsData();
+    sk_sp<SkData> data3 = dst3.detachAsData();
+    REPORTER_ASSERT(r, data0->equals(data1.get()));
+    REPORTER_ASSERT(r, data0->equals(data2.get()));
+    REPORTER_ASSERT(r, data0->equals(data3.get()));
+}
diff --git a/tests/ReadPixelsTest.cpp b/tests/ReadPixelsTest.cpp
index 9cb9200..9363e71 100644
--- a/tests/ReadPixelsTest.cpp
+++ b/tests/ReadPixelsTest.cpp
@@ -530,7 +530,7 @@
 
 static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
                             const SkImageInfo& srcInfo) {
-    if (!SkImageInfoIsValid(srcInfo)) {
+    if (!SkImageInfoIsValidRenderingCS(srcInfo)) {
         return;
     }