OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
| 8 #include "SkData.h" |
8 #include "SkDecodingImageGenerator.h" | 9 #include "SkDecodingImageGenerator.h" |
9 #include "SkData.h" | |
10 #include "SkImageDecoder.h" | 10 #include "SkImageDecoder.h" |
| 11 #include "SkImageInfo.h" |
11 #include "SkImageGenerator.h" | 12 #include "SkImageGenerator.h" |
12 #include "SkImagePriv.h" | 13 #include "SkImagePriv.h" |
13 #include "SkStream.h" | 14 #include "SkStream.h" |
14 | 15 #include "SkUtils.h" |
15 | 16 |
16 namespace { | 17 namespace { |
17 /** | 18 /** |
18 * Special allocator used by getPixels(). Uses preallocated memory | 19 * Special allocator used by getPixels(). Uses preallocated memory |
19 * provided. | 20 * provided. |
20 */ | 21 */ |
21 class TargetAllocator : public SkBitmap::Allocator { | 22 class TargetAllocator : public SkBitmap::Allocator { |
22 public: | 23 public: |
23 TargetAllocator(void* target, size_t rowBytes, const SkImageInfo& info) | 24 TargetAllocator(void* target, |
| 25 size_t rowBytes, |
| 26 int width, |
| 27 int height, |
| 28 SkBitmap::Config config) |
24 : fTarget(target) | 29 : fTarget(target) |
25 , fRowBytes(rowBytes) | 30 , fRowBytes(rowBytes) |
26 , fInfo(info) { } | 31 , fWidth(width) |
| 32 , fHeight(height) |
| 33 , fConfig(config) { } |
| 34 |
| 35 bool isReady() { return (fTarget != NULL); } |
27 | 36 |
28 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) SK_OVERRIDE { | 37 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) SK_OVERRIDE { |
29 if ((SkImageInfoToBitmapConfig(fInfo) != bm->config()) | 38 if ((NULL == fTarget) |
30 || (bm->width() != fInfo.fWidth) | 39 || (fConfig != bm->config()) |
31 || (bm->height() != fInfo.fHeight)) { | 40 || (fWidth != bm->width()) |
32 return false; | 41 || (fHeight != bm->height()) |
| 42 || (ct != NULL)) { |
| 43 // Call default allocator. |
| 44 return bm->allocPixels(NULL, ct); |
33 } | 45 } |
34 bm->setConfig(bm->config(), bm->width(), bm->height(), | 46 // make sure fRowBytes is correct. |
35 fRowBytes, bm->alphaType()); | 47 bm->setConfig(fConfig, fWidth, fHeight, fRowBytes, bm->alphaType()); |
36 bm->setPixels(fTarget, ct); | 48 // TODO(halcanary): verify that all callers of this function |
| 49 // will respect new RowBytes. Will be moot once rowbytes belongs |
| 50 // to PixelRef. |
| 51 bm->setPixels(fTarget, NULL); |
| 52 fTarget = NULL; // never alloc same pixels twice! |
37 return true; | 53 return true; |
38 } | 54 } |
39 | 55 |
40 private: | 56 private: |
41 void* fTarget; | 57 void* fTarget; // Block of memory to be supplied as pixel memory |
42 size_t fRowBytes; | 58 // in allocPixelRef. Must be large enough to hold |
43 SkImageInfo fInfo; | 59 // a bitmap described by fWidth, fHeight, and |
| 60 // fRowBytes. |
| 61 size_t fRowBytes; // rowbytes for the destination bitmap |
| 62 int fWidth; // Along with fHeight and fConfig, the information |
| 63 int fHeight; // about the bitmap whose pixels this allocator is |
| 64 // expected to allocate. If they do not match the |
| 65 // bitmap passed to allocPixelRef, it is assumed |
| 66 // that the bitmap will be copied to a bitmap with |
| 67 // the correct info using this allocator, so the |
| 68 // default allocator will be used instead of |
| 69 // fTarget. |
| 70 SkBitmap::Config fConfig; |
44 typedef SkBitmap::Allocator INHERITED; | 71 typedef SkBitmap::Allocator INHERITED; |
45 }; | 72 }; |
46 } // namespace | |
47 //////////////////////////////////////////////////////////////////////////////// | |
48 | 73 |
49 SkDecodingImageGenerator::SkDecodingImageGenerator(SkData* data) | 74 /** |
50 : fData(data) | 75 * An implementation of SkImageGenerator that calls into |
51 , fHasInfo(false) | 76 * SkImageDecoder. Since the consumers of this class only rely on |
52 , fDoCopyTo(false) { | 77 * virtual functions, we hide all implementation details inside this |
53 SkASSERT(fData != NULL); | 78 * source file. This implementaion of getInfo() always returns true. |
54 fStream = SkNEW_ARGS(SkMemoryStream, (fData)); | 79 */ |
55 SkASSERT(fStream != NULL); | 80 class DecodingImageGenerator : public SkImageGenerator { |
56 SkASSERT(fStream->unique()); | 81 public: |
57 fData->ref(); | 82 DecodingImageGenerator(SkData* data, |
58 } | 83 SkStreamRewindable* stream, |
| 84 const SkImageInfo& info, |
| 85 int sampleSize, |
| 86 bool ditherImage, |
| 87 SkBitmap::Config requestedConfig) |
| 88 : fData(data) |
| 89 , fStream(stream) |
| 90 , fInfo(info) |
| 91 , fSampleSize(sampleSize) |
| 92 , fDitherImage(ditherImage) |
| 93 , fRequestedConfig(requestedConfig) { |
| 94 SkASSERT(stream != NULL); |
| 95 SkSafeRef(fData); // may be NULL. |
| 96 } |
| 97 virtual ~DecodingImageGenerator() { |
| 98 SkSafeUnref(fData); |
| 99 fStream->unref(); |
| 100 } |
59 | 101 |
60 SkDecodingImageGenerator::SkDecodingImageGenerator(SkStreamRewindable* stream) | 102 virtual SkData* refEncodedData() SK_OVERRIDE; |
61 : fData(NULL) | |
62 , fStream(stream) | |
63 , fHasInfo(false) | |
64 , fDoCopyTo(false) { | |
65 SkASSERT(fStream != NULL); | |
66 SkASSERT(fStream->unique()); | |
67 } | |
68 | 103 |
69 SkDecodingImageGenerator::~SkDecodingImageGenerator() { | 104 virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE { |
70 SkSafeUnref(fData); | 105 if (info != NULL) { |
71 fStream->unref(); | 106 *info = fInfo; |
72 } | 107 } |
| 108 return true; |
| 109 } |
73 | 110 |
74 // TODO(halcanary): Give this macro a better name and move it into SkTypes.h | 111 virtual bool getPixels(const SkImageInfo& info, |
| 112 void* pixels, |
| 113 size_t rowBytes) SK_OVERRIDE; |
| 114 |
| 115 private: |
| 116 SkData* fData; |
| 117 SkStreamRewindable* fStream; |
| 118 const SkImageInfo fInfo; |
| 119 const int fSampleSize; |
| 120 const bool fDitherImage; |
| 121 const SkBitmap::Config fRequestedConfig; |
| 122 typedef SkImageGenerator INHERITED; |
| 123 }; |
| 124 |
75 #ifdef SK_DEBUG | 125 #ifdef SK_DEBUG |
76 #define SkCheckResult(expr, value) SkASSERT((value) == (expr)) | 126 #define SkCheckResult(expr, value) SkASSERT((value) == (expr)) |
77 #else | 127 #else |
78 #define SkCheckResult(expr, value) (void)(expr) | 128 #define SkCheckResult(expr, value) (void)(expr) |
79 #endif | 129 #endif |
80 | 130 |
81 SkData* SkDecodingImageGenerator::refEncodedData() { | 131 |
| 132 SkData* DecodingImageGenerator::refEncodedData() { |
82 // This functionality is used in `gm --serialize` | 133 // This functionality is used in `gm --serialize` |
| 134 // Does not encode options. |
83 if (fData != NULL) { | 135 if (fData != NULL) { |
84 return SkSafeRef(fData); | 136 return SkSafeRef(fData); |
85 } | 137 } |
86 // TODO(halcanary): SkStreamRewindable needs a refData() function | 138 // TODO(halcanary): SkStreamRewindable needs a refData() function |
87 // which returns a cheap copy of the underlying data. | 139 // which returns a cheap copy of the underlying data. |
88 if (!fStream->rewind()) { | 140 if (!fStream->rewind()) { |
89 return NULL; | 141 return NULL; |
90 } | 142 } |
91 size_t length = fStream->getLength(); | 143 size_t length = fStream->getLength(); |
92 if (0 == length) { | 144 if (0 == length) { |
93 return NULL; | 145 return NULL; |
94 } | 146 } |
95 void* buffer = sk_malloc_flags(length, 0); | 147 void* buffer = sk_malloc_flags(length, 0); |
96 SkCheckResult(fStream->read(buffer, length), length); | 148 SkCheckResult(fStream->read(buffer, length), length); |
97 fData = SkData::NewFromMalloc(buffer, length); | 149 fData = SkData::NewFromMalloc(buffer, length); |
98 return SkSafeRef(fData); | 150 return SkSafeRef(fData); |
99 } | 151 } |
100 | 152 |
101 bool SkDecodingImageGenerator::getInfo(SkImageInfo* info) { | 153 // TODO(halcanary) move this function into a standard header. |
102 // info can be NULL. If so, will update fInfo, fDoCopyTo, and fHasInfo. | 154 static bool config_to_colorType(SkBitmap::Config config, SkColorType* ctOut) { |
103 if (fHasInfo) { | 155 SkColorType ct; |
104 if (info != NULL) { | 156 switch (config) { |
105 *info = fInfo; | 157 case SkBitmap::kA8_Config: |
| 158 ct = kAlpha_8_SkColorType; |
| 159 break; |
| 160 case SkBitmap::kIndex8_Config: |
| 161 ct = kIndex_8_SkColorType; |
| 162 break; |
| 163 case SkBitmap::kRGB_565_Config: |
| 164 ct = kRGB_565_SkColorType; |
| 165 break; |
| 166 case SkBitmap::kARGB_4444_Config: |
| 167 ct = kARGB_4444_SkColorType; |
| 168 break; |
| 169 case SkBitmap::kARGB_8888_Config: |
| 170 ct = kPMColor_SkColorType; |
| 171 break; |
| 172 case SkBitmap::kNo_Config: |
| 173 default: |
| 174 return false; |
| 175 } |
| 176 if (ctOut) { |
| 177 *ctOut = ct; |
| 178 } |
| 179 return true; |
| 180 } |
| 181 |
| 182 // A contructor-type function that returns NULL on failure. This |
| 183 // prevents the returned SkImageGenerator from ever being in a bad |
| 184 // state. |
| 185 static SkImageGenerator* create_decoding_generator(SkData* data, |
| 186 SkStreamRewindable* stream, |
| 187 const SkDecoderOptions& opts)
{ |
| 188 SkASSERT(stream); |
| 189 SkAutoTUnref<SkStreamRewindable> autoStream(stream); // always unref this. |
| 190 if (SkBitmap::kIndex8_Config == opts.fRequestedConfig) { |
| 191 // We do not support indexed color with SkImageGenerators, |
| 192 return NULL; |
| 193 } |
| 194 SkAssertResult(autoStream->rewind()); |
| 195 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(autoStream)); |
| 196 if (NULL == decoder.get()) { |
| 197 return NULL; |
| 198 } |
| 199 SkBitmap bitmap; |
| 200 decoder->setSampleSize(opts.fSampleSize); |
| 201 if (!decoder->decode(stream, &bitmap, |
| 202 SkImageDecoder::kDecodeBounds_Mode)) { |
| 203 return NULL; |
| 204 } |
| 205 if (bitmap.config() == SkBitmap::kNo_Config) { |
| 206 return NULL; |
| 207 } |
| 208 |
| 209 SkImageInfo info; |
| 210 SkBitmap::Config config = opts.fRequestedConfig; |
| 211 if (opts.fRequestedConfig == SkBitmap::kNo_Config) { |
| 212 // Use default config. |
| 213 if (SkBitmap::kIndex8_Config == bitmap.config()) { |
| 214 // We don't support kIndex8 because we don't support |
| 215 // colortables in this workflow. |
| 216 config = SkBitmap::kARGB_8888_Config; |
| 217 info.fWidth = bitmap.width(); |
| 218 info.fHeight = bitmap.height(); |
| 219 info.fColorType = kPMColor_SkColorType; |
| 220 info.fAlphaType = bitmap.alphaType(); |
| 221 } else { |
| 222 config = bitmap.config(); // Save for later! |
| 223 if (!bitmap.asImageInfo(&info)) { |
| 224 SkDEBUGFAIL("Getting SkImageInfo from bitmap failed."); |
| 225 return NULL; |
| 226 } |
106 } | 227 } |
107 return true; | 228 } else { |
| 229 if (!bitmap.canCopyTo(opts.fRequestedConfig)) { |
| 230 SkASSERT(bitmap.config() != opts.fRequestedConfig); |
| 231 return NULL; // Can not translate to needed config. |
| 232 } |
| 233 info.fWidth = bitmap.width(); |
| 234 info.fHeight = bitmap.height(); |
| 235 if (!config_to_colorType(opts.fRequestedConfig, |
| 236 &(info.fColorType))) { |
| 237 SkDEBUGFAIL("SkBitmapConfigToColorType fails."); |
| 238 return NULL; |
| 239 } |
| 240 info.fAlphaType = bitmap.alphaType(); |
108 } | 241 } |
| 242 return SkNEW_ARGS(DecodingImageGenerator, |
| 243 (data, autoStream.detach(), info, |
| 244 opts.fSampleSize, opts.fDitherImage, config)); |
| 245 } |
| 246 |
| 247 #ifdef SK_DEBUG |
| 248 inline bool check_alpha(SkAlphaType reported, SkAlphaType actual) { |
| 249 return ((reported == actual) |
| 250 || ((reported == kPremul_SkAlphaType) |
| 251 && (actual == kOpaque_SkAlphaType))); |
| 252 } |
| 253 #endif // SK_DEBUG |
| 254 |
| 255 bool DecodingImageGenerator::getPixels(const SkImageInfo& info, |
| 256 void* pixels, |
| 257 size_t rowBytes) { |
| 258 if (NULL == pixels) { |
| 259 return false; |
| 260 } |
| 261 if (fInfo != info) { |
| 262 // The caller has specified a different info. This is an |
| 263 // error for this kind of SkImageGenerator. Use the Options |
| 264 // to change the settings. |
| 265 return false; |
| 266 } |
| 267 int bpp = SkBitmap::ComputeBytesPerPixel(fRequestedConfig); |
| 268 if (static_cast<size_t>(bpp * info.fWidth) > rowBytes) { |
| 269 // The caller has specified a bad rowBytes. |
| 270 return false; |
| 271 } |
| 272 |
109 SkAssertResult(fStream->rewind()); | 273 SkAssertResult(fStream->rewind()); |
110 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | 274 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
111 if (NULL == decoder.get()) { | 275 if (NULL == decoder.get()) { |
112 return false; | 276 return false; |
113 } | 277 } |
| 278 decoder->setDitherImage(fDitherImage); |
| 279 decoder->setSampleSize(fSampleSize); |
| 280 |
114 SkBitmap bitmap; | 281 SkBitmap bitmap; |
115 if (!decoder->decode(fStream, &bitmap, | 282 TargetAllocator allocator(pixels, rowBytes, info.fWidth, |
116 SkImageDecoder::kDecodeBounds_Mode)) { | 283 info.fHeight, fRequestedConfig); |
117 return false; | 284 decoder->setAllocator(&allocator); |
118 } | 285 bool success = decoder->decode(fStream, &bitmap, fRequestedConfig, |
119 if (bitmap.config() == SkBitmap::kNo_Config) { | |
120 return false; | |
121 } | |
122 if (!bitmap.asImageInfo(&fInfo)) { | |
123 // We can't use bitmap.config() as is. | |
124 if (!bitmap.canCopyTo(SkBitmap::kARGB_8888_Config)) { | |
125 SkDEBUGFAIL("!bitmap->canCopyTo(SkBitmap::kARGB_8888_Config)"); | |
126 return false; | |
127 } | |
128 fDoCopyTo = true; | |
129 fInfo.fWidth = bitmap.width(); | |
130 fInfo.fHeight = bitmap.height(); | |
131 fInfo.fColorType = kPMColor_SkColorType; | |
132 fInfo.fAlphaType = bitmap.alphaType(); | |
133 } | |
134 if (info != NULL) { | |
135 *info = fInfo; | |
136 } | |
137 fHasInfo = true; | |
138 return true; | |
139 } | |
140 | |
141 bool SkDecodingImageGenerator::getPixels(const SkImageInfo& info, | |
142 void* pixels, | |
143 size_t rowBytes) { | |
144 if (NULL == pixels) { | |
145 return false; | |
146 } | |
147 if (!this->getInfo(NULL)) { | |
148 return false; | |
149 } | |
150 if (SkImageInfoToBitmapConfig(info) == SkBitmap::kNo_Config) { | |
151 return false; // Unsupported SkColorType. | |
152 } | |
153 SkAssertResult(fStream->rewind()); | |
154 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | |
155 if (NULL == decoder.get()) { | |
156 return false; | |
157 } | |
158 if (fInfo != info) { | |
159 // The caller has specified a different info. For now, this | |
160 // is an error. In the future, we will check to see if we can | |
161 // convert. | |
162 return false; | |
163 } | |
164 int bpp = SkBitmap::ComputeBytesPerPixel(SkImageInfoToBitmapConfig(info)); | |
165 if (static_cast<size_t>(bpp * info.fWidth) > rowBytes) { | |
166 return false; | |
167 } | |
168 SkBitmap bitmap; | |
169 if (!bitmap.setConfig(info, rowBytes)) { | |
170 return false; | |
171 } | |
172 | |
173 TargetAllocator allocator(pixels, rowBytes, info); | |
174 if (!fDoCopyTo) { | |
175 decoder->setAllocator(&allocator); | |
176 } | |
177 bool success = decoder->decode(fStream, &bitmap, | |
178 SkImageDecoder::kDecodePixels_Mode); | 286 SkImageDecoder::kDecodePixels_Mode); |
179 decoder->setAllocator(NULL); | 287 decoder->setAllocator(NULL); |
180 if (!success) { | 288 if (!success) { |
181 return false; | 289 return false; |
182 } | 290 } |
183 if (fDoCopyTo) { | 291 if (allocator.isReady()) { // Did not use pixels! |
184 SkBitmap bm8888; | 292 SkBitmap bm; |
185 bitmap.copyTo(&bm8888, SkBitmap::kARGB_8888_Config, &allocator); | 293 SkASSERT(bitmap.canCopyTo(fRequestedConfig)); |
| 294 if (!bitmap.copyTo(&bm, fRequestedConfig, &allocator) |
| 295 || allocator.isReady()) { |
| 296 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); |
| 297 // Earlier we checked canCopyto(); we expect consistency. |
| 298 return false; |
| 299 } |
| 300 SkASSERT(check_alpha(fInfo.fAlphaType, bm.alphaType())); |
| 301 } else { |
| 302 SkASSERT(check_alpha(fInfo.fAlphaType, bitmap.alphaType())); |
186 } | 303 } |
187 return true; | 304 return true; |
188 } | 305 } |
189 bool SkDecodingImageGenerator::Install(SkData* data, SkBitmap* dst, | 306 } // namespace |
190 SkDiscardableMemory::Factory* factory) { | 307 //////////////////////////////////////////////////////////////////////////////// |
| 308 |
| 309 SkImageGenerator* SkNewDecodingImageGenerator(SkData* data, |
| 310 const SkDecoderOptions& opts) { |
191 SkASSERT(data != NULL); | 311 SkASSERT(data != NULL); |
192 SkASSERT(dst != NULL); | 312 if (NULL == data) { |
193 SkImageGenerator* gen(SkNEW_ARGS(SkDecodingImageGenerator, (data))); | 313 return NULL; |
194 return SkInstallDiscardablePixelRef(gen, dst, factory); | 314 } |
| 315 SkStreamRewindable* stream = SkNEW_ARGS(SkMemoryStream, (data)); |
| 316 SkASSERT(stream != NULL); |
| 317 SkASSERT(stream->unique()); |
| 318 return create_decoding_generator(data, stream, opts); |
195 } | 319 } |
196 | 320 |
197 bool SkDecodingImageGenerator::Install(SkStreamRewindable* stream, | 321 SkImageGenerator* SkNewDecodingImageGenerator(SkStreamRewindable* stream, |
198 SkBitmap* dst, | 322 const SkDecoderOptions& opts) { |
199 SkDiscardableMemory::Factory* factory) { | |
200 SkASSERT(stream != NULL); | 323 SkASSERT(stream != NULL); |
201 SkASSERT(dst != NULL); | 324 SkASSERT(stream->unique()); |
202 if ((stream == NULL) || !stream->unique()) { | 325 if ((stream == NULL) || !stream->unique()) { |
203 SkSafeUnref(stream); | 326 SkSafeUnref(stream); |
204 return false; | 327 return NULL; |
205 } | 328 } |
206 SkImageGenerator* gen(SkNEW_ARGS(SkDecodingImageGenerator, (stream))); | 329 return create_decoding_generator(NULL, stream, opts); |
207 return SkInstallDiscardablePixelRef(gen, dst, factory); | |
208 } | 330 } |
| 331 |
OLD | NEW |