Merge SkCodec with SkScanlineDecoder

Benefits:
- This mimics other decoding APIs (including the ones SkCodec relies
on, e.g. a png_struct, which can be used to decode an entire image or
one line at a time).

- It allows a client to ask us to do what we can do efficiently - i.e.
start from encoded data and either decode the whole thing or scanlines.

- It removes the duplicate methods which appeared in both SkCodec and
SkScanlineDecoder (some of which, e.g. in SkJpegScanlineDecoder, just
call fCodec->sameMethod()).

- It simplifies moving more checks into the base class (e.g. the
examples in skbug.com/4284).

BUG=skia:4175
BUG=skia:4284

=====================================================================

SkScanlineDecoder.h/.cpp:
Removed.

SkCodec.h/.cpp:
Add methods, enums, and variables which were previously in
SkScanlineDecoder.
Default fCurrScanline to -1, as a sentinel that start has not been
called.

General changes:
Convert SkScanlineDecoders to SkCodecs.

General changes in SkCodec subclasses:
Merge SkScanlineDecoder implementation into SkCodec. Most (all?) owned
an SkCodec, so they now call this-> instead of fCodec->.

SkBmpCodec.h/.cpp:
Replace the unused rowOrder method with an override for
onGetScanlineOrder.
Make getDstRow const, since it is called by onGetY, which is const.

SkCodec_libpng.h/.cpp:
Make SkPngCodec an abstract class, with two subclasses which handle
scanline decoding separately (they share code for decoding the entire
image). Reimplement onReallyHasAlpha so that it can return the most
recent result (e.g. after a scanline decode which only decoded part
of the image) or a better answer (e.g. if the whole image is known to
be opaque).
Compute fNumberPasses early, so we know which subclass to instantiate.
Make SkPngInterlaceScanlineDecoder use the base class' fCurrScanline
rather than a separate variable.

CodexTest.cpp:
Add tests for the state changes in SkCodec (need to call start before
decoding scanlines; calling getPixels means that start will need to
be called again before decoding more scanlines).
Add a test which decodes in stripes, currently only used for an
interlaced PNG.

TODO: Add tests for onReallyHasAlpha.

Review URL: https://codereview.chromium.org/1365313002
diff --git a/src/codec/SkCodec_wbmp.cpp b/src/codec/SkCodec_wbmp.cpp
index 22f2bac..0c85ead 100644
--- a/src/codec/SkCodec_wbmp.cpp
+++ b/src/codec/SkCodec_wbmp.cpp
@@ -96,6 +96,8 @@
 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
     : INHERITED(info, stream)
     , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
+    , fColorTable(nullptr)
+    , fSwizzler(nullptr)
 {}
 
 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
@@ -120,7 +122,7 @@
     }
 
     if (!valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
-        return SkCodec::kInvalidConversion;
+        return kInvalidConversion;
     }
 
     // Prepare a color table if necessary
@@ -163,90 +165,55 @@
     return new SkWbmpCodec(info, streamDeleter.detach());
 }
 
-class SkWbmpScanlineDecoder : public SkScanlineDecoder {
-public:
-    /*
-     * Takes ownership of all pointer paramters.
-     */
-    SkWbmpScanlineDecoder(SkWbmpCodec* codec)
-        : INHERITED(codec->getInfo())
-        , fCodec(codec)
-        , fColorTable(nullptr)
-        , fSwizzler(nullptr)
-        , fSrcBuffer(codec->fSrcRowBytes)
-    {}
-
-    SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) override {
-        void* dstRow = dst;
-        for (int y = 0; y < count; ++y) {
-            SkCodec::Result rowResult = fCodec->readRow(fSrcBuffer.get());
-            if (SkCodec::kSuccess != rowResult) {
-                return rowResult;
-            }
-            fSwizzler->swizzle(dstRow, fSrcBuffer.get());
-            dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
+SkCodec::Result SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
+    void* dstRow = dst;
+    for (int y = 0; y < count; ++y) {
+        Result rowResult = this->readRow(fSrcBuffer.get());
+        if (kSuccess != rowResult) {
+            return rowResult;
         }
-        return SkCodec::kSuccess;
+        fSwizzler->swizzle(dstRow, fSrcBuffer.get());
+        dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
     }
-
-    SkCodec::Result onStart(const SkImageInfo& dstInfo,
-            const SkCodec::Options& options, SkPMColor inputColorTable[],
-            int* inputColorCount) {
-        if (!fCodec->rewindIfNeeded()) {
-            return SkCodec::kCouldNotRewind;
-        }
-        if (options.fSubset) {
-            // Subsets are not supported.
-            return SkCodec::kUnimplemented;
-        }
-        if (dstInfo.dimensions() != this->getInfo().dimensions()) {
-            if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(), dstInfo)) {
-                return SkCodec::kInvalidScale;
-            }
-        }
-
-        if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
-            return SkCodec::kInvalidConversion;
-        }
-
-        // Fill in the color table
-        setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
-
-        // Copy the color table to a pointer that can be owned by the scanline decoder
-        if (kIndex_8_SkColorType == dstInfo.colorType()) {
-            fColorTable.reset(new SkColorTable(inputColorTable, 2));
-        }
-
-        // Initialize the swizzler
-        fSwizzler.reset(fCodec->initializeSwizzler(dstInfo,
-                get_color_ptr(fColorTable.get()), options));
-        if (nullptr == fSwizzler.get()) {
-            return SkCodec::kInvalidConversion;
-        }
-
-        return SkCodec::kSuccess;
-    }
-
-    SkEncodedFormat onGetEncodedFormat() const {
-        return kWBMP_SkEncodedFormat;
-    }
-
-private:
-    SkAutoTDelete<SkWbmpCodec>   fCodec;
-    SkAutoTUnref<SkColorTable>   fColorTable;
-    SkAutoTDelete<SkSwizzler>    fSwizzler;
-    SkAutoTMalloc<uint8_t>       fSrcBuffer;
-
-    typedef SkScanlineDecoder INHERITED;
-};
-
-SkScanlineDecoder* SkWbmpCodec::NewSDFromStream(SkStream* stream) {
-    SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>(
-            SkWbmpCodec::NewFromStream(stream)));
-    if (!codec) {
-        return nullptr;
-    }
-
-    // Return the new scanline decoder
-    return new SkWbmpScanlineDecoder(codec.detach());
+    return kSuccess;
 }
+
+SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
+        const Options& options, SkPMColor inputColorTable[], int* inputColorCount) {
+    if (!this->rewindIfNeeded()) {
+        return kCouldNotRewind;
+    }
+    if (options.fSubset) {
+        // Subsets are not supported.
+        return kUnimplemented;
+    }
+    if (dstInfo.dimensions() != this->getInfo().dimensions()) {
+        if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(), dstInfo)) {
+                return kInvalidScale;
+        }
+    }
+
+    if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
+        return kInvalidConversion;
+    }
+
+    // Fill in the color table
+    setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
+
+    // Copy the color table to a pointer that can be owned by the scanline decoder
+    if (kIndex_8_SkColorType == dstInfo.colorType()) {
+        fColorTable.reset(new SkColorTable(inputColorTable, 2));
+    }
+
+    // Initialize the swizzler
+    fSwizzler.reset(this->initializeSwizzler(dstInfo,
+            get_color_ptr(fColorTable.get()), options));
+    if (nullptr == fSwizzler.get()) {
+        return kInvalidConversion;
+    }
+
+    fSrcBuffer.reset(fSrcRowBytes);
+
+    return kSuccess;
+}
+