6938880357356271eb6aab8d20f0f570946d90a1
[platform/upstream/libSkiaSharp.git] / src / ports / SkImageGenerator_skia.cpp
1 /*
2  * Copyright 2015 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8 #include "SkData.h"
9 #include "SkImageDecoder.h"
10 #include "SkImageGenerator.h"
11 #include "SkStream.h"
12
13 class BareMemoryAllocator : public SkBitmap::Allocator {
14     const SkImageInfo   fInfo;
15     void* const         fMemory;
16     const size_t        fRowBytes;
17
18 public:
19     BareMemoryAllocator(const SkImageInfo& info, void* memory, size_t rowBytes)
20         : fInfo(info), fMemory(memory), fRowBytes(rowBytes)
21     {}
22
23 protected:
24     bool allocPixelRef(SkBitmap* bm, SkColorTable* ctable) SK_OVERRIDE {
25         const SkImageInfo bmi = bm->info();
26         if (bmi.width() != fInfo.width() || bmi.height() != fInfo.height() ||
27             bmi.colorType() != fInfo.colorType())
28         {
29             return false;
30         }
31         return bm->installPixels(bmi, fMemory, fRowBytes, ctable, NULL, NULL);
32     }
33 };
34
35 class SkImageDecoderGenerator : public SkImageGenerator {
36     const SkImageInfo               fInfo;
37     SkAutoTDelete<SkImageDecoder>   fDecoder;
38     SkAutoTUnref<SkData>            fData;
39
40 public:
41     SkImageDecoderGenerator(const SkImageInfo& info, SkImageDecoder* decoder, SkData* data)
42         : INHERITED(info), fInfo(info), fDecoder(decoder), fData(SkRef(data))
43     {}
44
45 protected:
46     SkData* onRefEncodedData() SK_OVERRIDE {
47         return SkRef(fData.get());
48     }
49
50 #ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
51     virtual bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
52         *info = fInfo;
53         return true;
54     }
55 #endif
56
57     virtual Result onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
58                                const Options&,
59                                SkPMColor ctableEntries[], int* ctableCount) SK_OVERRIDE {
60         SkMemoryStream stream(fData->data(), fData->size(), false);
61         SkAutoTUnref<BareMemoryAllocator> allocator(SkNEW_ARGS(BareMemoryAllocator,
62                                                                (info, pixels, rowBytes)));
63         fDecoder->setAllocator(allocator);
64         fDecoder->setRequireUnpremultipliedColors(kUnpremul_SkAlphaType == info.alphaType());
65
66         SkBitmap bm;
67         const SkImageDecoder::Result result = fDecoder->decode(&stream, &bm, info.colorType(),
68                                                                SkImageDecoder::kDecodePixels_Mode);
69         if (SkImageDecoder::kFailure == result) {
70             return kInvalidInput;
71         }
72
73         SkASSERT(info.colorType() == bm.info().colorType());
74
75         if (kIndex_8_SkColorType == info.colorType()) {
76             SkASSERT(ctableEntries);
77
78             SkColorTable* ctable = bm.getColorTable();
79             if (NULL == ctable) {
80                 return kInvalidConversion;
81             }
82             const int count = ctable->count();
83             memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor));
84             *ctableCount = count;
85         }
86         if (SkImageDecoder::kPartialSuccess == result) {
87             return kIncompleteInput;
88         }
89         return kSuccess;
90     }
91
92     bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
93                          SkYUVColorSpace* colorSpace) SK_OVERRIDE {
94         SkMemoryStream stream(fData->data(), fData->size(), false);
95         return fDecoder->decodeYUV8Planes(&stream, sizes, planes, rowBytes, colorSpace);
96     }
97
98 private:
99     typedef SkImageGenerator INHERITED;
100 };
101
102 SkImageGenerator* SkImageGenerator::NewFromData(SkData* data) {
103     if (NULL == data) {
104         return NULL;
105     }
106
107     SkMemoryStream stream(data->data(), data->size(), false);
108     SkImageDecoder* decoder = SkImageDecoder::Factory(&stream);
109     if (NULL == decoder) {
110         return NULL;
111     }
112
113     SkBitmap bm;
114     stream.rewind();
115     if (!decoder->decode(&stream, &bm, kUnknown_SkColorType, SkImageDecoder::kDecodeBounds_Mode)) {
116         SkDELETE(decoder);
117         return NULL;
118     }
119
120     return SkNEW_ARGS(SkImageDecoderGenerator, (bm.info(), decoder, data));
121 }