Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "SkBitmapFactory.h" | 8 #include "SkBitmapFactory.h" |
| 9 | 9 |
| 10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
| 11 #include "SkData.h" | 11 #include "SkData.h" |
| 12 #include "SkImageCache.h" | 12 #include "SkImageCache.h" |
| 13 #include "SkImagePriv.h" | 13 #include "SkImagePriv.h" |
| 14 #include "SkLazyPixelRef.h" | 14 #include "SkLazyPixelRef.h" |
| 15 #include "SkMallocPixelRef.h" | |
| 15 | 16 |
| 16 SK_DEFINE_INST_COUNT(SkBitmapFactory::CacheSelector) | 17 SK_DEFINE_INST_COUNT(SkBitmapFactory::CacheSelector) |
| 17 | 18 |
| 18 SkBitmapFactory::SkBitmapFactory(SkBitmapFactory::DecodeProc proc) | 19 SkBitmapFactory::SkBitmapFactory(SkBitmapFactory::DecodeProc proc) |
| 19 : fDecodeProc(proc) | 20 : fDecodeProc(proc) |
| 20 , fImageCache(NULL) | 21 , fImageCache(NULL) |
| 21 , fCacheSelector(NULL) { | 22 , fCacheSelector(NULL) { |
| 22 SkASSERT(fDecodeProc != NULL); | 23 SkASSERT(fDecodeProc != NULL); |
| 23 } | 24 } |
| 24 | 25 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 36 } | 37 } |
| 37 | 38 |
| 38 void SkBitmapFactory::setCacheSelector(CacheSelector* selector) { | 39 void SkBitmapFactory::setCacheSelector(CacheSelector* selector) { |
| 39 SkRefCnt_SafeAssign(fCacheSelector, selector); | 40 SkRefCnt_SafeAssign(fCacheSelector, selector); |
| 40 if (selector != NULL) { | 41 if (selector != NULL) { |
| 41 SkSafeUnref(fImageCache); | 42 SkSafeUnref(fImageCache); |
| 42 fImageCache = NULL; | 43 fImageCache = NULL; |
| 43 } | 44 } |
| 44 } | 45 } |
| 45 | 46 |
| 47 namespace { | |
| 48 typedef SkBitmapFactory::DecodeProc DecodeProc; | |
| 49 | |
| 50 class FlexibleGenerator : public SkImageGenerator { | |
|
reed1
2013/11/25 18:06:56
Not sure why this is named Flexible. Can you add a
| |
| 51 public: | |
| 52 FlexibleGenerator(SkData* data, | |
| 53 SkBitmapFactory::DecodeProc proc); | |
| 54 virtual ~FlexibleGenerator(); | |
| 55 virtual SkData* refEncodedData() SK_OVERRIDE; | |
| 56 virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE; | |
| 57 virtual bool getPixels(const SkImageInfo& info, | |
| 58 void* pixels, | |
| 59 size_t rowBytes) SK_OVERRIDE; | |
| 60 private: | |
| 61 SkData* fData; | |
| 62 DecodeProc fDecodeProc; | |
| 63 SkImageInfo fInfo; | |
| 64 bool fError; | |
| 65 }; | |
| 66 FlexibleGenerator::FlexibleGenerator(SkData* data, DecodeProc proc) | |
| 67 : fData(data) | |
| 68 , fDecodeProc(proc) | |
| 69 , fError(false) { | |
| 70 SkASSERT(fDecodeProc != NULL); | |
| 71 SkASSERT(fData != NULL); | |
| 72 fData->ref(); | |
| 73 fError = !fDecodeProc(fData->data(), fData->size(), &fInfo, NULL); | |
| 74 } | |
| 75 | |
| 76 FlexibleGenerator::~FlexibleGenerator() { | |
| 77 fData->unref(); | |
| 78 } | |
| 79 | |
| 80 SkData* FlexibleGenerator::refEncodedData() { | |
| 81 fData->ref(); | |
| 82 return fData; | |
| 83 } | |
| 84 | |
| 85 bool FlexibleGenerator::getInfo(SkImageInfo* info) { | |
| 86 if (fError) | |
| 87 return false; | |
| 88 SkASSERT(info != NULL); | |
| 89 *info = fInfo; | |
| 90 return true; | |
| 91 } | |
| 92 | |
| 93 bool FlexibleGenerator::getPixels(const SkImageInfo& info, | |
| 94 void* pixels, | |
| 95 size_t rowBytes) { | |
| 96 if (fError) | |
| 97 return false; | |
| 98 SkASSERT(pixels != NULL); | |
| 99 SkBitmapFactory::Target target = {pixels, rowBytes}; | |
| 100 SkImageInfo tmpInfo = info; | |
| 101 return fDecodeProc(fData->data(), fData->size(), &tmpInfo, &target); | |
| 102 } | |
| 103 } // namespace | |
| 104 | |
| 46 bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) { | 105 bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) { |
| 47 if (NULL == data || 0 == data->size() || dst == NULL) { | 106 if (NULL == data || 0 == data->size() || dst == NULL) { |
| 48 return false; | 107 return false; |
| 49 } | 108 } |
| 50 | 109 FlexibleGenerator* gen = SkNEW_ARGS(FlexibleGenerator, |
| 51 SkImageInfo info; | 110 (data, fDecodeProc)); |
| 52 if (!fDecodeProc(data->data(), data->size(), &info, NULL)) { | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); | |
| 57 | |
| 58 Target target; | |
| 59 // FIMXE: There will be a problem if this rowbytes is calculated differently from | |
| 60 // in SkLazyPixelRef. | |
| 61 target.fRowBytes = SkImageMinRowBytes(info); | |
| 62 dst->setConfig(config, info.fWidth, info.fHeight, target.fRowBytes, info.fAl phaType); | |
| 63 | 111 |
| 64 // fImageCache and fCacheSelector are mutually exclusive. | 112 // fImageCache and fCacheSelector are mutually exclusive. |
| 65 SkASSERT(NULL == fImageCache || NULL == fCacheSelector); | 113 SkASSERT(NULL == fImageCache || NULL == fCacheSelector); |
| 66 | 114 SkImageCache* cache = fImageCache; |
| 67 SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector- >selectCache(info); | 115 if ((cache == NULL) && (NULL != fCacheSelector)) { |
| 116 SkImageInfo info; | |
| 117 if (!gen->getInfo(&info)) { | |
| 118 SkDELETE(gen); | |
| 119 return false; | |
| 120 } | |
| 121 cache = fCacheSelector->selectCache(info); | |
| 122 } | |
| 68 | 123 |
| 69 if (cache != NULL) { | 124 if (cache != NULL) { |
| 70 // Now set a new LazyPixelRef on dst. | 125 return SkLazyPixelRef::Install(gen, cache, dst); |
| 71 SkAutoTUnref<SkLazyPixelRef> lazyRef(SkNEW_ARGS(SkLazyPixelRef, | |
| 72 (data, fDecodeProc, cach e))); | |
| 73 dst->setPixelRef(lazyRef); | |
| 74 return true; | |
| 75 } else { | 126 } else { |
| 76 dst->allocPixels(); | 127 return SkMallocPixelRef::Install(gen, dst); |
| 77 target.fAddr = dst->getPixels(); | |
| 78 return fDecodeProc(data->data(), data->size(), &info, &target); | |
| 79 } | 128 } |
| 80 } | 129 } |
| OLD | NEW |