Report error on failure to create SkCodec

Update NewFromStream to report an error on failure to create an
SkCodec, so that a client can distinguish between
- not enough data
- invalid data

In Chromium, this will allow blink::ImageDecoder to call SetFailed if
the stream is invalid early and we never create an SkCodec. Without
this, ImageDecoder will keep trying to create an SkCodec when it
receives more data.

Change-Id: I4f505c56d91c982be36a828fd0f7db17b1596588
Reviewed-on: https://skia-review.googlesource.com/22642
Commit-Queue: Leon Scroggins <scroggo@google.com>
Reviewed-by: Derek Sollenberger <djsollen@google.com>
Reviewed-by: Chris Blume <cblume@chromium.org>
diff --git a/src/codec/SkCodec.cpp b/src/codec/SkCodec.cpp
index 4bd3917..872fe2b 100644
--- a/src/codec/SkCodec.cpp
+++ b/src/codec/SkCodec.cpp
@@ -26,7 +26,7 @@
 
 struct DecoderProc {
     bool (*IsFormat)(const void*, size_t);
-    SkCodec* (*NewFromStream)(SkStream*);
+    SkCodec* (*NewFromStream)(SkStream*, SkCodec::Result*);
 };
 
 static const DecoderProc gDecoderProcs[] = {
@@ -49,8 +49,14 @@
 }
 
 SkCodec* SkCodec::NewFromStream(SkStream* stream,
-                                SkPngChunkReader* chunkReader) {
+        Result* outResult, SkPngChunkReader* chunkReader) {
+    Result resultStorage;
+    if (!outResult) {
+        outResult = &resultStorage;
+    }
+
     if (!stream) {
+        *outResult = kInvalidInput;
         return nullptr;
     }
 
@@ -81,6 +87,7 @@
         bytesRead = stream->read(buffer, bytesToRead);
         if (!stream->rewind()) {
             SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
+            *outResult = kCouldNotRewind;
             return nullptr;
         }
     }
@@ -89,22 +96,28 @@
     // But this code follows the same pattern as the loop.
 #ifdef SK_HAS_PNG_LIBRARY
     if (SkPngCodec::IsPng(buffer, bytesRead)) {
-        return SkPngCodec::NewFromStream(streamDeleter.release(), chunkReader);
+        return SkPngCodec::NewFromStream(streamDeleter.release(), outResult, chunkReader);
     } else
 #endif
     {
         for (DecoderProc proc : gDecoderProcs) {
             if (proc.IsFormat(buffer, bytesRead)) {
-                return proc.NewFromStream(streamDeleter.release());
+                return proc.NewFromStream(streamDeleter.release(), outResult);
             }
         }
 
 #ifdef SK_CODEC_DECODES_RAW
         // Try to treat the input as RAW if all the other checks failed.
-        return SkRawCodec::NewFromStream(streamDeleter.release());
+        return SkRawCodec::NewFromStream(streamDeleter.release(), outResult);
 #endif
     }
 
+    if (bytesRead < bytesToRead) {
+        *outResult = kIncompleteInput;
+    } else {
+        *outResult = kUnimplemented;
+    }
+
     return nullptr;
 }
 
@@ -112,7 +125,7 @@
     if (!data) {
         return nullptr;
     }
-    return NewFromStream(new SkMemoryStream(data), reader);
+    return NewFromStream(new SkMemoryStream(data), nullptr, reader);
 }
 
 SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,