blob: d5c2ba88186eeb547595e52cb890a16a5ff6abf4 [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;
sugoib227e372014-10-16 13:10:57 -070048 virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
49 SkYUVColorSpace* colorSpace) SK_OVERRIDE;
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000050
51private:
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +000052 typedef SkImageGenerator INHERITED;
53};
54
halcanary@google.com29d96932013-12-09 13:45:02 +000055/**
56 * Special allocator used by getPixels(). Uses preallocated memory
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000057 * provided if possible, else fall-back on the default allocator
halcanary@google.com29d96932013-12-09 13:45:02 +000058 */
59class TargetAllocator : public SkBitmap::Allocator {
60public:
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000061 TargetAllocator(const SkImageInfo& info,
62 void* target,
63 size_t rowBytes)
64 : fInfo(info)
65 , fTarget(target)
halcanary@google.com29d96932013-12-09 13:45:02 +000066 , fRowBytes(rowBytes)
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000067 {}
halcanary@google.com29d96932013-12-09 13:45:02 +000068
halcanary@google.com3d50ea12014-01-02 13:15:13 +000069 bool isReady() { return (fTarget != NULL); }
70
71 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000072 if (NULL == fTarget || !equal_modulo_alpha(fInfo, bm->info())) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +000073 // Call default allocator.
reed84825042014-09-02 12:50:45 -070074 return bm->tryAllocPixels(NULL, ct);
halcanary@google.com29d96932013-12-09 13:45:02 +000075 }
skia.committer@gmail.com86cbf992014-02-24 03:01:55 +000076
halcanary@google.com3d50ea12014-01-02 13:15:13 +000077 // TODO(halcanary): verify that all callers of this function
78 // will respect new RowBytes. Will be moot once rowbytes belongs
79 // to PixelRef.
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000080 bm->installPixels(fInfo, fTarget, fRowBytes, ct, NULL, NULL);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000081
halcanary@google.com3d50ea12014-01-02 13:15:13 +000082 fTarget = NULL; // never alloc same pixels twice!
halcanary@google.com29d96932013-12-09 13:45:02 +000083 return true;
84 }
85
86private:
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000087 const SkImageInfo fInfo;
halcanary@google.com3d50ea12014-01-02 13:15:13 +000088 void* fTarget; // Block of memory to be supplied as pixel memory
89 // in allocPixelRef. Must be large enough to hold
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000090 // a bitmap described by fInfo and fRowBytes
91 const size_t fRowBytes; // rowbytes for the destination bitmap
92
halcanary@google.com29d96932013-12-09 13:45:02 +000093 typedef SkBitmap::Allocator INHERITED;
94};
halcanary@google.comad04eb42013-11-21 15:32:08 +000095
halcanary@google.com29d96932013-12-09 13:45:02 +000096// TODO(halcanary): Give this macro a better name and move it into SkTypes.h
97#ifdef SK_DEBUG
98 #define SkCheckResult(expr, value) SkASSERT((value) == (expr))
99#else
100 #define SkCheckResult(expr, value) (void)(expr)
101#endif
102
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000103#ifdef SK_DEBUG
104inline bool check_alpha(SkAlphaType reported, SkAlphaType actual) {
105 return ((reported == actual)
106 || ((reported == kPremul_SkAlphaType)
107 && (actual == kOpaque_SkAlphaType)));
108}
109#endif // SK_DEBUG
110
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000111////////////////////////////////////////////////////////////////////////////////
112
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000113DecodingImageGenerator::DecodingImageGenerator(
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000114 SkData* data,
115 SkStreamRewindable* stream,
116 const SkImageInfo& info,
117 int sampleSize,
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000118 bool ditherImage)
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000119 : fData(data)
120 , fStream(stream)
121 , fInfo(info)
122 , fSampleSize(sampleSize)
123 , fDitherImage(ditherImage)
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000124{
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000125 SkASSERT(stream != NULL);
126 SkSafeRef(fData); // may be NULL.
127}
128
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000129DecodingImageGenerator::~DecodingImageGenerator() {
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000130 SkSafeUnref(fData);
131 fStream->unref();
132}
133
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000134SkData* DecodingImageGenerator::onRefEncodedData() {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000135 // This functionality is used in `gm --serialize`
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000136 // Does not encode options.
reed33a30502014-09-11 08:42:36 -0700137 if (NULL == fData) {
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();
reed9594da12014-09-12 12:12:27 -0700144 if (length) {
145 fData = SkData::NewFromStream(fStream, length);
reed33a30502014-09-11 08:42:36 -0700146 }
halcanary@google.com29d96932013-12-09 13:45:02 +0000147 }
reed9594da12014-09-12 12:12:27 -0700148 return SkSafeRef(fData);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000149}
150
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000151bool DecodingImageGenerator::onGetPixels(const SkImageInfo& info,
152 void* pixels, size_t rowBytes,
153 SkPMColor ctableEntries[], int* ctableCount) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000154 if (fInfo != info) {
155 // The caller has specified a different info. This is an
156 // error for this kind of SkImageGenerator. Use the Options
157 // to change the settings.
halcanary@google.com29d96932013-12-09 13:45:02 +0000158 return false;
159 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000160
halcanary@google.com29d96932013-12-09 13:45:02 +0000161 SkAssertResult(fStream->rewind());
162 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream));
163 if (NULL == decoder.get()) {
164 return false;
165 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000166 decoder->setDitherImage(fDitherImage);
167 decoder->setSampleSize(fSampleSize);
reede5ea5002014-09-03 11:54:58 -0700168 decoder->setRequireUnpremultipliedColors(info.alphaType() == kUnpremul_SkAlphaType);
commit-bot@chromium.org1ad518b2013-12-18 18:33:15 +0000169
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000170 SkBitmap bitmap;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000171 TargetAllocator allocator(fInfo, pixels, rowBytes);
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000172 decoder->setAllocator(&allocator);
reedbfefc7c2014-06-12 17:40:00 -0700173 bool success = decoder->decode(fStream, &bitmap, info.colorType(),
scroggo2a120802014-10-22 12:07:00 -0700174 SkImageDecoder::kDecodePixels_Mode) != SkImageDecoder::kFailure;
halcanary@google.com29d96932013-12-09 13:45:02 +0000175 decoder->setAllocator(NULL);
176 if (!success) {
177 return false;
178 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000179 if (allocator.isReady()) { // Did not use pixels!
180 SkBitmap bm;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000181 SkASSERT(bitmap.canCopyTo(info.colorType()));
182 bool copySuccess = bitmap.copyTo(&bm, info.colorType(), &allocator);
183 if (!copySuccess || allocator.isReady()) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000184 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed.");
185 // Earlier we checked canCopyto(); we expect consistency.
186 return false;
187 }
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000188 SkASSERT(check_alpha(info.alphaType(), bm.alphaType()));
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000189 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000190 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType()));
halcanary@google.com29d96932013-12-09 13:45:02 +0000191 }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000192
193 if (kIndex_8_SkColorType == info.colorType()) {
194 if (kIndex_8_SkColorType != bitmap.colorType()) {
195 return false; // they asked for Index8, but we didn't receive that from decoder
196 }
197 SkColorTable* ctable = bitmap.getColorTable();
198 if (NULL == ctable) {
199 return false;
200 }
201 const int count = ctable->count();
mtklein775b8192014-12-02 09:11:25 -0800202 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor));
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000203 *ctableCount = count;
204 }
halcanary@google.com29d96932013-12-09 13:45:02 +0000205 return true;
halcanary@google.comad04eb42013-11-21 15:32:08 +0000206}
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000207
sugoib227e372014-10-16 13:10:57 -0700208bool DecodingImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3],
209 size_t rowBytes[3], SkYUVColorSpace* colorSpace) {
210 if (!fStream->rewind()) {
211 return false;
212 }
213
214 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream));
215 if (NULL == decoder.get()) {
216 return false;
217 }
218
219 return decoder->decodeYUV8Planes(fStream, sizes, planes, rowBytes, colorSpace);
220}
221
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000222// A contructor-type function that returns NULL on failure. This
223// prevents the returned SkImageGenerator from ever being in a bad
224// state. Called by both Create() functions
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000225SkImageGenerator* CreateDecodingImageGenerator(
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000226 SkData* data,
227 SkStreamRewindable* stream,
228 const SkDecodingImageGenerator::Options& opts) {
229 SkASSERT(stream);
230 SkAutoTUnref<SkStreamRewindable> autoStream(stream); // always unref this.
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000231 SkAssertResult(autoStream->rewind());
232 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(autoStream));
233 if (NULL == decoder.get()) {
234 return NULL;
235 }
236 SkBitmap bitmap;
237 decoder->setSampleSize(opts.fSampleSize);
commit-bot@chromium.org1ae492f2014-04-07 21:37:36 +0000238 decoder->setRequireUnpremultipliedColors(opts.fRequireUnpremul);
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000239 if (!decoder->decode(stream, &bitmap, SkImageDecoder::kDecodeBounds_Mode)) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000240 return NULL;
241 }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000242 if (kUnknown_SkColorType == bitmap.colorType()) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000243 return NULL;
244 }
245
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000246 SkImageInfo info = bitmap.info();
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000247
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000248 if (opts.fUseRequestedColorType && (opts.fRequestedColorType != info.colorType())) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000249 if (!bitmap.canCopyTo(opts.fRequestedColorType)) {
250 SkASSERT(bitmap.colorType() != opts.fRequestedColorType);
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000251 return NULL; // Can not translate to needed config.
252 }
reede5ea5002014-09-03 11:54:58 -0700253 info = info.makeColorType(opts.fRequestedColorType);
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000254 }
commit-bot@chromium.org1ae492f2014-04-07 21:37:36 +0000255
reede5ea5002014-09-03 11:54:58 -0700256 if (opts.fRequireUnpremul && info.alphaType() != kOpaque_SkAlphaType) {
257 info = info.makeAlphaType(kUnpremul_SkAlphaType);
commit-bot@chromium.org1ae492f2014-04-07 21:37:36 +0000258 }
scroggo2fd0d142014-07-01 07:08:19 -0700259
reede5ea5002014-09-03 11:54:58 -0700260 SkAlphaType newAlphaType = info.alphaType();
261 if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAlphaType)) {
scroggo2fd0d142014-07-01 07:08:19 -0700262 return NULL;
263 }
264
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000265 return SkNEW_ARGS(DecodingImageGenerator,
reede5ea5002014-09-03 11:54:58 -0700266 (data, autoStream.detach(), info.makeAlphaType(newAlphaType),
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000267 opts.fSampleSize, opts.fDitherImage));
halcanary@google.com29d96932013-12-09 13:45:02 +0000268}
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000269
270} // namespace
271
272////////////////////////////////////////////////////////////////////////////////
273
274SkImageGenerator* SkDecodingImageGenerator::Create(
275 SkData* data,
276 const SkDecodingImageGenerator::Options& opts) {
277 SkASSERT(data != NULL);
278 if (NULL == data) {
279 return NULL;
280 }
281 SkStreamRewindable* stream = SkNEW_ARGS(SkMemoryStream, (data));
282 SkASSERT(stream != NULL);
283 SkASSERT(stream->unique());
284 return CreateDecodingImageGenerator(data, stream, opts);
285}
286
287SkImageGenerator* SkDecodingImageGenerator::Create(
288 SkStreamRewindable* stream,
289 const SkDecodingImageGenerator::Options& opts) {
290 SkASSERT(stream != NULL);
291 SkASSERT(stream->unique());
292 if ((stream == NULL) || !stream->unique()) {
293 SkSafeUnref(stream);
294 return NULL;
295 }
296 return CreateDecodingImageGenerator(NULL, stream, opts);
297}