blob: 1d91bcc0f98e359c95b59cac8ac73d79feacdecf [file] [log] [blame]
halcanary@google.comad04eb42013-11-21 15:32:08 +00001/*
2 * Copyright 2013 Google Inc.
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
commit-bot@chromium.org1ad518b2013-12-18 18:33:15 +00008#include "SkData.h"
halcanary@google.com3d50ea12014-01-02 13:15:13 +00009#include "SkDecodingImageGenerator.h"
halcanary@google.comad04eb42013-11-21 15:32:08 +000010#include "SkImageDecoder.h"
halcanary@google.com3d50ea12014-01-02 13:15:13 +000011#include "SkImageInfo.h"
halcanary@google.comedd370f2013-12-10 21:11:12 +000012#include "SkImageGenerator.h"
halcanary@google.com29d96932013-12-09 13:45:02 +000013#include "SkImagePriv.h"
14#include "SkStream.h"
halcanary@google.com3d50ea12014-01-02 13:15:13 +000015#include "SkUtils.h"
halcanary@google.com29d96932013-12-09 13:45:02 +000016
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +000017namespace {
18bool equal_modulo_alpha(const SkImageInfo& a, const SkImageInfo& b) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000019 return a.width() == b.width() && a.height() == b.height() &&
20 a.colorType() == b.colorType();
21}
22
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +000023class DecodingImageGenerator : public SkImageGenerator {
24public:
25 virtual ~DecodingImageGenerator();
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +000026
27 SkData* fData;
28 SkStreamRewindable* fStream;
29 const SkImageInfo fInfo;
30 const int fSampleSize;
31 const bool fDitherImage;
32
33 DecodingImageGenerator(SkData* data,
34 SkStreamRewindable* stream,
35 const SkImageInfo& info,
36 int sampleSize,
37 bool ditherImage);
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000038
39protected:
40 virtual SkData* onRefEncodedData() SK_OVERRIDE;
41 virtual bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
42 *info = fInfo;
43 return true;
44 }
45 virtual bool onGetPixels(const SkImageInfo& info,
46 void* pixels, size_t rowBytes,
47 SkPMColor ctable[], int* ctableCount) SK_OVERRIDE;
48
49private:
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +000050 typedef SkImageGenerator INHERITED;
51};
52
halcanary@google.com29d96932013-12-09 13:45:02 +000053/**
54 * Special allocator used by getPixels(). Uses preallocated memory
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000055 * provided if possible, else fall-back on the default allocator
halcanary@google.com29d96932013-12-09 13:45:02 +000056 */
57class TargetAllocator : public SkBitmap::Allocator {
58public:
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000059 TargetAllocator(const SkImageInfo& info,
60 void* target,
61 size_t rowBytes)
62 : fInfo(info)
63 , fTarget(target)
halcanary@google.com29d96932013-12-09 13:45:02 +000064 , fRowBytes(rowBytes)
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000065 {}
halcanary@google.com29d96932013-12-09 13:45:02 +000066
halcanary@google.com3d50ea12014-01-02 13:15:13 +000067 bool isReady() { return (fTarget != NULL); }
68
69 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000070 if (NULL == fTarget || !equal_modulo_alpha(fInfo, bm->info())) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +000071 // Call default allocator.
reed84825042014-09-02 12:50:45 -070072 return bm->tryAllocPixels(NULL, ct);
halcanary@google.com29d96932013-12-09 13:45:02 +000073 }
skia.committer@gmail.com86cbf992014-02-24 03:01:55 +000074
halcanary@google.com3d50ea12014-01-02 13:15:13 +000075 // TODO(halcanary): verify that all callers of this function
76 // will respect new RowBytes. Will be moot once rowbytes belongs
77 // to PixelRef.
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000078 bm->installPixels(fInfo, fTarget, fRowBytes, ct, NULL, NULL);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000079
halcanary@google.com3d50ea12014-01-02 13:15:13 +000080 fTarget = NULL; // never alloc same pixels twice!
halcanary@google.com29d96932013-12-09 13:45:02 +000081 return true;
82 }
83
84private:
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000085 const SkImageInfo fInfo;
halcanary@google.com3d50ea12014-01-02 13:15:13 +000086 void* fTarget; // Block of memory to be supplied as pixel memory
87 // in allocPixelRef. Must be large enough to hold
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000088 // a bitmap described by fInfo and fRowBytes
89 const size_t fRowBytes; // rowbytes for the destination bitmap
90
halcanary@google.com29d96932013-12-09 13:45:02 +000091 typedef SkBitmap::Allocator INHERITED;
92};
halcanary@google.comad04eb42013-11-21 15:32:08 +000093
halcanary@google.com29d96932013-12-09 13:45:02 +000094// TODO(halcanary): Give this macro a better name and move it into SkTypes.h
95#ifdef SK_DEBUG
96 #define SkCheckResult(expr, value) SkASSERT((value) == (expr))
97#else
98 #define SkCheckResult(expr, value) (void)(expr)
99#endif
100
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000101#ifdef SK_DEBUG
102inline bool check_alpha(SkAlphaType reported, SkAlphaType actual) {
103 return ((reported == actual)
104 || ((reported == kPremul_SkAlphaType)
105 && (actual == kOpaque_SkAlphaType)));
106}
107#endif // SK_DEBUG
108
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000109////////////////////////////////////////////////////////////////////////////////
110
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000111DecodingImageGenerator::DecodingImageGenerator(
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000112 SkData* data,
113 SkStreamRewindable* stream,
114 const SkImageInfo& info,
115 int sampleSize,
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000116 bool ditherImage)
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000117 : fData(data)
118 , fStream(stream)
119 , fInfo(info)
120 , fSampleSize(sampleSize)
121 , fDitherImage(ditherImage)
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000122{
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000123 SkASSERT(stream != NULL);
124 SkSafeRef(fData); // may be NULL.
125}
126
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000127DecodingImageGenerator::~DecodingImageGenerator() {
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000128 SkSafeUnref(fData);
129 fStream->unref();
130}
131
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000132SkData* DecodingImageGenerator::onRefEncodedData() {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000133 // This functionality is used in `gm --serialize`
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000134 // Does not encode options.
halcanary@google.com29d96932013-12-09 13:45:02 +0000135 if (fData != NULL) {
136 return SkSafeRef(fData);
137 }
138 // TODO(halcanary): SkStreamRewindable needs a refData() function
139 // which returns a cheap copy of the underlying data.
140 if (!fStream->rewind()) {
141 return NULL;
142 }
143 size_t length = fStream->getLength();
144 if (0 == length) {
145 return NULL;
146 }
147 void* buffer = sk_malloc_flags(length, 0);
148 SkCheckResult(fStream->read(buffer, length), length);
149 fData = SkData::NewFromMalloc(buffer, length);
150 return SkSafeRef(fData);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000151}
152
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000153bool DecodingImageGenerator::onGetPixels(const SkImageInfo& info,
154 void* pixels, size_t rowBytes,
155 SkPMColor ctableEntries[], int* ctableCount) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000156 if (fInfo != info) {
157 // The caller has specified a different info. This is an
158 // error for this kind of SkImageGenerator. Use the Options
159 // to change the settings.
halcanary@google.com29d96932013-12-09 13:45:02 +0000160 return false;
161 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000162
halcanary@google.com29d96932013-12-09 13:45:02 +0000163 SkAssertResult(fStream->rewind());
164 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream));
165 if (NULL == decoder.get()) {
166 return false;
167 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000168 decoder->setDitherImage(fDitherImage);
169 decoder->setSampleSize(fSampleSize);
reede5ea5002014-09-03 11:54:58 -0700170 decoder->setRequireUnpremultipliedColors(info.alphaType() == kUnpremul_SkAlphaType);
commit-bot@chromium.org1ad518b2013-12-18 18:33:15 +0000171
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000172 SkBitmap bitmap;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000173 TargetAllocator allocator(fInfo, pixels, rowBytes);
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000174 decoder->setAllocator(&allocator);
reedbfefc7c2014-06-12 17:40:00 -0700175 bool success = decoder->decode(fStream, &bitmap, info.colorType(),
halcanary@google.com29d96932013-12-09 13:45:02 +0000176 SkImageDecoder::kDecodePixels_Mode);
177 decoder->setAllocator(NULL);
178 if (!success) {
179 return false;
180 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000181 if (allocator.isReady()) { // Did not use pixels!
182 SkBitmap bm;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000183 SkASSERT(bitmap.canCopyTo(info.colorType()));
184 bool copySuccess = bitmap.copyTo(&bm, info.colorType(), &allocator);
185 if (!copySuccess || allocator.isReady()) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000186 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed.");
187 // Earlier we checked canCopyto(); we expect consistency.
188 return false;
189 }
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000190 SkASSERT(check_alpha(info.alphaType(), bm.alphaType()));
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000191 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000192 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType()));
halcanary@google.com29d96932013-12-09 13:45:02 +0000193 }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000194
195 if (kIndex_8_SkColorType == info.colorType()) {
196 if (kIndex_8_SkColorType != bitmap.colorType()) {
197 return false; // they asked for Index8, but we didn't receive that from decoder
198 }
199 SkColorTable* ctable = bitmap.getColorTable();
200 if (NULL == ctable) {
201 return false;
202 }
203 const int count = ctable->count();
204 memcpy(ctableEntries, ctable->lockColors(), count * sizeof(SkPMColor));
205 ctable->unlockColors();
206 *ctableCount = count;
207 }
halcanary@google.com29d96932013-12-09 13:45:02 +0000208 return true;
halcanary@google.comad04eb42013-11-21 15:32:08 +0000209}
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000210
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000211// A contructor-type function that returns NULL on failure. This
212// prevents the returned SkImageGenerator from ever being in a bad
213// state. Called by both Create() functions
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000214SkImageGenerator* CreateDecodingImageGenerator(
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000215 SkData* data,
216 SkStreamRewindable* stream,
217 const SkDecodingImageGenerator::Options& opts) {
218 SkASSERT(stream);
219 SkAutoTUnref<SkStreamRewindable> autoStream(stream); // always unref this.
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000220 SkAssertResult(autoStream->rewind());
221 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(autoStream));
222 if (NULL == decoder.get()) {
223 return NULL;
224 }
225 SkBitmap bitmap;
226 decoder->setSampleSize(opts.fSampleSize);
commit-bot@chromium.org1ae492f2014-04-07 21:37:36 +0000227 decoder->setRequireUnpremultipliedColors(opts.fRequireUnpremul);
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000228 if (!decoder->decode(stream, &bitmap, SkImageDecoder::kDecodeBounds_Mode)) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000229 return NULL;
230 }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000231 if (kUnknown_SkColorType == bitmap.colorType()) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000232 return NULL;
233 }
234
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000235 SkImageInfo info = bitmap.info();
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000236
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000237 if (opts.fUseRequestedColorType && (opts.fRequestedColorType != info.colorType())) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000238 if (!bitmap.canCopyTo(opts.fRequestedColorType)) {
239 SkASSERT(bitmap.colorType() != opts.fRequestedColorType);
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000240 return NULL; // Can not translate to needed config.
241 }
reede5ea5002014-09-03 11:54:58 -0700242 info = info.makeColorType(opts.fRequestedColorType);
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000243 }
commit-bot@chromium.org1ae492f2014-04-07 21:37:36 +0000244
reede5ea5002014-09-03 11:54:58 -0700245 if (opts.fRequireUnpremul && info.alphaType() != kOpaque_SkAlphaType) {
246 info = info.makeAlphaType(kUnpremul_SkAlphaType);
commit-bot@chromium.org1ae492f2014-04-07 21:37:36 +0000247 }
scroggo2fd0d142014-07-01 07:08:19 -0700248
reede5ea5002014-09-03 11:54:58 -0700249 SkAlphaType newAlphaType = info.alphaType();
250 if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAlphaType)) {
scroggo2fd0d142014-07-01 07:08:19 -0700251 return NULL;
252 }
253
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000254 return SkNEW_ARGS(DecodingImageGenerator,
reede5ea5002014-09-03 11:54:58 -0700255 (data, autoStream.detach(), info.makeAlphaType(newAlphaType),
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000256 opts.fSampleSize, opts.fDitherImage));
halcanary@google.com29d96932013-12-09 13:45:02 +0000257}
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000258
259} // namespace
260
261////////////////////////////////////////////////////////////////////////////////
262
263SkImageGenerator* SkDecodingImageGenerator::Create(
264 SkData* data,
265 const SkDecodingImageGenerator::Options& opts) {
266 SkASSERT(data != NULL);
267 if (NULL == data) {
268 return NULL;
269 }
270 SkStreamRewindable* stream = SkNEW_ARGS(SkMemoryStream, (data));
271 SkASSERT(stream != NULL);
272 SkASSERT(stream->unique());
273 return CreateDecodingImageGenerator(data, stream, opts);
274}
275
276SkImageGenerator* SkDecodingImageGenerator::Create(
277 SkStreamRewindable* stream,
278 const SkDecodingImageGenerator::Options& opts) {
279 SkASSERT(stream != NULL);
280 SkASSERT(stream->unique());
281 if ((stream == NULL) || !stream->unique()) {
282 SkSafeUnref(stream);
283 return NULL;
284 }
285 return CreateDecodingImageGenerator(NULL, stream, opts);
286}