| 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 "SkData.h" |
| 9 #include "SkDecodingImageGenerator.h" | 9 #include "SkDecodingImageGenerator.h" |
| 10 #include "SkImageDecoder.h" | 10 #include "SkImageDecoder.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 class TargetAllocator : public SkBitmap::Allocator { | 54 class TargetAllocator : public SkBitmap::Allocator { |
| 55 public: | 55 public: |
| 56 TargetAllocator(const SkImageInfo& info, | 56 TargetAllocator(const SkImageInfo& info, |
| 57 void* target, | 57 void* target, |
| 58 size_t rowBytes) | 58 size_t rowBytes) |
| 59 : fInfo(info) | 59 : fInfo(info) |
| 60 , fTarget(target) | 60 , fTarget(target) |
| 61 , fRowBytes(rowBytes) | 61 , fRowBytes(rowBytes) |
| 62 {} | 62 {} |
| 63 | 63 |
| 64 bool isReady() { return (fTarget != NULL); } | 64 bool isReady() { return (fTarget != nullptr); } |
| 65 | 65 |
| 66 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) { | 66 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) { |
| 67 if (NULL == fTarget || !equal_modulo_alpha(fInfo, bm->info())) { | 67 if (nullptr == fTarget || !equal_modulo_alpha(fInfo, bm->info())) { |
| 68 // Call default allocator. | 68 // Call default allocator. |
| 69 return bm->tryAllocPixels(NULL, ct); | 69 return bm->tryAllocPixels(nullptr, ct); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // TODO(halcanary): verify that all callers of this function | 72 // TODO(halcanary): verify that all callers of this function |
| 73 // will respect new RowBytes. Will be moot once rowbytes belongs | 73 // will respect new RowBytes. Will be moot once rowbytes belongs |
| 74 // to PixelRef. | 74 // to PixelRef. |
| 75 bm->installPixels(fInfo, fTarget, fRowBytes, ct, NULL, NULL); | 75 bm->installPixels(fInfo, fTarget, fRowBytes, ct, nullptr, nullptr); |
| 76 | 76 |
| 77 fTarget = NULL; // never alloc same pixels twice! | 77 fTarget = nullptr; // never alloc same pixels twice! |
| 78 return true; | 78 return true; |
| 79 } | 79 } |
| 80 | 80 |
| 81 private: | 81 private: |
| 82 const SkImageInfo fInfo; | 82 const SkImageInfo fInfo; |
| 83 void* fTarget; // Block of memory to be supplied as pixel memory | 83 void* fTarget; // Block of memory to be supplied as pixel memory |
| 84 // in allocPixelRef. Must be large enough to hold | 84 // in allocPixelRef. Must be large enough to hold |
| 85 // a bitmap described by fInfo and fRowBytes | 85 // a bitmap described by fInfo and fRowBytes |
| 86 const size_t fRowBytes; // rowbytes for the destination bitmap | 86 const size_t fRowBytes; // rowbytes for the destination bitmap |
| 87 | 87 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 111 const SkImageInfo& info, | 111 const SkImageInfo& info, |
| 112 int sampleSize, | 112 int sampleSize, |
| 113 bool ditherImage) | 113 bool ditherImage) |
| 114 : INHERITED(info) | 114 : INHERITED(info) |
| 115 , fData(data) | 115 , fData(data) |
| 116 , fStream(stream) | 116 , fStream(stream) |
| 117 , fInfo(info) | 117 , fInfo(info) |
| 118 , fSampleSize(sampleSize) | 118 , fSampleSize(sampleSize) |
| 119 , fDitherImage(ditherImage) | 119 , fDitherImage(ditherImage) |
| 120 { | 120 { |
| 121 SkASSERT(stream != NULL); | 121 SkASSERT(stream != nullptr); |
| 122 SkSafeRef(fData); // may be NULL. | 122 SkSafeRef(fData); // may be nullptr. |
| 123 } | 123 } |
| 124 | 124 |
| 125 DecodingImageGenerator::~DecodingImageGenerator() { | 125 DecodingImageGenerator::~DecodingImageGenerator() { |
| 126 SkSafeUnref(fData); | 126 SkSafeUnref(fData); |
| 127 } | 127 } |
| 128 | 128 |
| 129 SkData* DecodingImageGenerator::onRefEncodedData() { | 129 SkData* DecodingImageGenerator::onRefEncodedData() { |
| 130 // This functionality is used in `gm --serialize` | 130 // This functionality is used in `gm --serialize` |
| 131 // Does not encode options. | 131 // Does not encode options. |
| 132 if (NULL == fData) { | 132 if (nullptr == fData) { |
| 133 // TODO(halcanary): SkStreamRewindable needs a refData() function | 133 // TODO(halcanary): SkStreamRewindable needs a refData() function |
| 134 // which returns a cheap copy of the underlying data. | 134 // which returns a cheap copy of the underlying data. |
| 135 if (!fStream->rewind()) { | 135 if (!fStream->rewind()) { |
| 136 return NULL; | 136 return nullptr; |
| 137 } | 137 } |
| 138 size_t length = fStream->getLength(); | 138 size_t length = fStream->getLength(); |
| 139 if (length) { | 139 if (length) { |
| 140 fData = SkData::NewFromStream(fStream, length); | 140 fData = SkData::NewFromStream(fStream, length); |
| 141 } | 141 } |
| 142 } | 142 } |
| 143 return SkSafeRef(fData); | 143 return SkSafeRef(fData); |
| 144 } | 144 } |
| 145 | 145 |
| 146 bool DecodingImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels,
size_t rowBytes, | 146 bool DecodingImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels,
size_t rowBytes, |
| 147 SkPMColor ctableEntries[], int* ctableC
ount) { | 147 SkPMColor ctableEntries[], int* ctableC
ount) { |
| 148 if (fInfo != info) { | 148 if (fInfo != info) { |
| 149 // The caller has specified a different info. This is an | 149 // The caller has specified a different info. This is an |
| 150 // error for this kind of SkImageGenerator. Use the Options | 150 // error for this kind of SkImageGenerator. Use the Options |
| 151 // to change the settings. | 151 // to change the settings. |
| 152 return false; | 152 return false; |
| 153 } | 153 } |
| 154 | 154 |
| 155 SkAssertResult(fStream->rewind()); | 155 SkAssertResult(fStream->rewind()); |
| 156 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | 156 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| 157 if (NULL == decoder.get()) { | 157 if (nullptr == decoder.get()) { |
| 158 return false; | 158 return false; |
| 159 } | 159 } |
| 160 decoder->setDitherImage(fDitherImage); | 160 decoder->setDitherImage(fDitherImage); |
| 161 decoder->setSampleSize(fSampleSize); | 161 decoder->setSampleSize(fSampleSize); |
| 162 decoder->setRequireUnpremultipliedColors(info.alphaType() == kUnpremul_SkAlp
haType); | 162 decoder->setRequireUnpremultipliedColors(info.alphaType() == kUnpremul_SkAlp
haType); |
| 163 | 163 |
| 164 SkBitmap bitmap; | 164 SkBitmap bitmap; |
| 165 TargetAllocator allocator(fInfo, pixels, rowBytes); | 165 TargetAllocator allocator(fInfo, pixels, rowBytes); |
| 166 decoder->setAllocator(&allocator); | 166 decoder->setAllocator(&allocator); |
| 167 const SkImageDecoder::Result decodeResult = decoder->decode(fStream, &bitmap
, info.colorType(), | 167 const SkImageDecoder::Result decodeResult = decoder->decode(fStream, &bitmap
, info.colorType(), |
| 168 SkImageDecoder::
kDecodePixels_Mode); | 168 SkImageDecoder::
kDecodePixels_Mode); |
| 169 decoder->setAllocator(NULL); | 169 decoder->setAllocator(nullptr); |
| 170 if (SkImageDecoder::kFailure == decodeResult) { | 170 if (SkImageDecoder::kFailure == decodeResult) { |
| 171 return false; | 171 return false; |
| 172 } | 172 } |
| 173 if (allocator.isReady()) { // Did not use pixels! | 173 if (allocator.isReady()) { // Did not use pixels! |
| 174 SkBitmap bm; | 174 SkBitmap bm; |
| 175 SkASSERT(bitmap.canCopyTo(info.colorType())); | 175 SkASSERT(bitmap.canCopyTo(info.colorType())); |
| 176 bool copySuccess = bitmap.copyTo(&bm, info.colorType(), &allocator); | 176 bool copySuccess = bitmap.copyTo(&bm, info.colorType(), &allocator); |
| 177 if (!copySuccess || allocator.isReady()) { | 177 if (!copySuccess || allocator.isReady()) { |
| 178 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); | 178 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); |
| 179 // Earlier we checked canCopyto(); we expect consistency. | 179 // Earlier we checked canCopyto(); we expect consistency. |
| 180 return false; | 180 return false; |
| 181 } | 181 } |
| 182 SkASSERT(check_alpha(info.alphaType(), bm.alphaType())); | 182 SkASSERT(check_alpha(info.alphaType(), bm.alphaType())); |
| 183 } else { | 183 } else { |
| 184 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType())); | 184 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType())); |
| 185 } | 185 } |
| 186 | 186 |
| 187 if (kIndex_8_SkColorType == info.colorType()) { | 187 if (kIndex_8_SkColorType == info.colorType()) { |
| 188 if (kIndex_8_SkColorType != bitmap.colorType()) { | 188 if (kIndex_8_SkColorType != bitmap.colorType()) { |
| 189 // they asked for Index8, but we didn't receive that from decoder | 189 // they asked for Index8, but we didn't receive that from decoder |
| 190 return false; | 190 return false; |
| 191 } | 191 } |
| 192 SkColorTable* ctable = bitmap.getColorTable(); | 192 SkColorTable* ctable = bitmap.getColorTable(); |
| 193 if (NULL == ctable) { | 193 if (nullptr == ctable) { |
| 194 return false; | 194 return false; |
| 195 } | 195 } |
| 196 const int count = ctable->count(); | 196 const int count = ctable->count(); |
| 197 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor)); | 197 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor)); |
| 198 *ctableCount = count; | 198 *ctableCount = count; |
| 199 } | 199 } |
| 200 return true; | 200 return true; |
| 201 } | 201 } |
| 202 | 202 |
| 203 bool DecodingImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], | 203 bool DecodingImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], |
| 204 size_t rowBytes[3], SkYUVColorSpace
* colorSpace) { | 204 size_t rowBytes[3], SkYUVColorSpace
* colorSpace) { |
| 205 if (!fStream->rewind()) { | 205 if (!fStream->rewind()) { |
| 206 return false; | 206 return false; |
| 207 } | 207 } |
| 208 | 208 |
| 209 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | 209 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| 210 if (NULL == decoder.get()) { | 210 if (nullptr == decoder.get()) { |
| 211 return false; | 211 return false; |
| 212 } | 212 } |
| 213 | 213 |
| 214 return decoder->decodeYUV8Planes(fStream, sizes, planes, rowBytes, colorSpac
e); | 214 return decoder->decodeYUV8Planes(fStream, sizes, planes, rowBytes, colorSpac
e); |
| 215 } | 215 } |
| 216 | 216 |
| 217 // A contructor-type function that returns NULL on failure. This | 217 // A contructor-type function that returns nullptr on failure. This |
| 218 // prevents the returned SkImageGenerator from ever being in a bad | 218 // prevents the returned SkImageGenerator from ever being in a bad |
| 219 // state. Called by both Create() functions | 219 // state. Called by both Create() functions |
| 220 SkImageGenerator* CreateDecodingImageGenerator( | 220 SkImageGenerator* CreateDecodingImageGenerator( |
| 221 SkData* data, | 221 SkData* data, |
| 222 SkStreamRewindable* stream, | 222 SkStreamRewindable* stream, |
| 223 const SkDecodingImageGenerator::Options& opts) { | 223 const SkDecodingImageGenerator::Options& opts) { |
| 224 SkASSERT(stream); | 224 SkASSERT(stream); |
| 225 SkAutoTDelete<SkStreamRewindable> autoStream(stream); // always delete this | 225 SkAutoTDelete<SkStreamRewindable> autoStream(stream); // always delete this |
| 226 SkAssertResult(autoStream->rewind()); | 226 SkAssertResult(autoStream->rewind()); |
| 227 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(autoStream)); | 227 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(autoStream)); |
| 228 if (NULL == decoder.get()) { | 228 if (nullptr == decoder.get()) { |
| 229 return NULL; | 229 return nullptr; |
| 230 } | 230 } |
| 231 SkBitmap bitmap; | 231 SkBitmap bitmap; |
| 232 decoder->setSampleSize(opts.fSampleSize); | 232 decoder->setSampleSize(opts.fSampleSize); |
| 233 decoder->setRequireUnpremultipliedColors(opts.fRequireUnpremul); | 233 decoder->setRequireUnpremultipliedColors(opts.fRequireUnpremul); |
| 234 if (!decoder->decode(stream, &bitmap, SkImageDecoder::kDecodeBounds_Mode)) { | 234 if (!decoder->decode(stream, &bitmap, SkImageDecoder::kDecodeBounds_Mode)) { |
| 235 return NULL; | 235 return nullptr; |
| 236 } | 236 } |
| 237 if (kUnknown_SkColorType == bitmap.colorType()) { | 237 if (kUnknown_SkColorType == bitmap.colorType()) { |
| 238 return NULL; | 238 return nullptr; |
| 239 } | 239 } |
| 240 | 240 |
| 241 SkImageInfo info = bitmap.info(); | 241 SkImageInfo info = bitmap.info(); |
| 242 | 242 |
| 243 if (opts.fUseRequestedColorType && (opts.fRequestedColorType != info.colorTy
pe())) { | 243 if (opts.fUseRequestedColorType && (opts.fRequestedColorType != info.colorTy
pe())) { |
| 244 if (!bitmap.canCopyTo(opts.fRequestedColorType)) { | 244 if (!bitmap.canCopyTo(opts.fRequestedColorType)) { |
| 245 SkASSERT(bitmap.colorType() != opts.fRequestedColorType); | 245 SkASSERT(bitmap.colorType() != opts.fRequestedColorType); |
| 246 return NULL; // Can not translate to needed config. | 246 return nullptr; // Can not translate to needed config. |
| 247 } | 247 } |
| 248 info = info.makeColorType(opts.fRequestedColorType); | 248 info = info.makeColorType(opts.fRequestedColorType); |
| 249 } | 249 } |
| 250 | 250 |
| 251 if (opts.fRequireUnpremul && info.alphaType() != kOpaque_SkAlphaType) { | 251 if (opts.fRequireUnpremul && info.alphaType() != kOpaque_SkAlphaType) { |
| 252 info = info.makeAlphaType(kUnpremul_SkAlphaType); | 252 info = info.makeAlphaType(kUnpremul_SkAlphaType); |
| 253 } | 253 } |
| 254 | 254 |
| 255 SkAlphaType newAlphaType = info.alphaType(); | 255 SkAlphaType newAlphaType = info.alphaType(); |
| 256 if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAl
phaType)) { | 256 if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAl
phaType)) { |
| 257 return NULL; | 257 return nullptr; |
| 258 } | 258 } |
| 259 | 259 |
| 260 return new DecodingImageGenerator(data, autoStream.detach(), info.makeAlphaT
ype(newAlphaType), | 260 return new DecodingImageGenerator(data, autoStream.detach(), info.makeAlphaT
ype(newAlphaType), |
| 261 opts.fSampleSize, opts.fDitherImage); | 261 opts.fSampleSize, opts.fDitherImage); |
| 262 } | 262 } |
| 263 | 263 |
| 264 } // namespace | 264 } // namespace |
| 265 | 265 |
| 266 //////////////////////////////////////////////////////////////////////////////// | 266 //////////////////////////////////////////////////////////////////////////////// |
| 267 | 267 |
| 268 SkImageGenerator* SkDecodingImageGenerator::Create( | 268 SkImageGenerator* SkDecodingImageGenerator::Create( |
| 269 SkData* data, | 269 SkData* data, |
| 270 const SkDecodingImageGenerator::Options& opts) { | 270 const SkDecodingImageGenerator::Options& opts) { |
| 271 SkASSERT(data != NULL); | 271 SkASSERT(data != nullptr); |
| 272 if (NULL == data) { | 272 if (nullptr == data) { |
| 273 return NULL; | 273 return nullptr; |
| 274 } | 274 } |
| 275 SkStreamRewindable* stream = new SkMemoryStream(data); | 275 SkStreamRewindable* stream = new SkMemoryStream(data); |
| 276 SkASSERT(stream != NULL); | 276 SkASSERT(stream != nullptr); |
| 277 return CreateDecodingImageGenerator(data, stream, opts); | 277 return CreateDecodingImageGenerator(data, stream, opts); |
| 278 } | 278 } |
| 279 | 279 |
| 280 SkImageGenerator* SkDecodingImageGenerator::Create( | 280 SkImageGenerator* SkDecodingImageGenerator::Create( |
| 281 SkStreamRewindable* stream, | 281 SkStreamRewindable* stream, |
| 282 const SkDecodingImageGenerator::Options& opts) { | 282 const SkDecodingImageGenerator::Options& opts) { |
| 283 SkASSERT(stream != NULL); | 283 SkASSERT(stream != nullptr); |
| 284 if (stream == NULL) { | 284 if (stream == nullptr) { |
| 285 return NULL; | 285 return nullptr; |
| 286 } | 286 } |
| 287 return CreateDecodingImageGenerator(NULL, stream, opts); | 287 return CreateDecodingImageGenerator(nullptr, stream, opts); |
| 288 } | 288 } |
| OLD | NEW |