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 "SkDiscardablePixelRef.h" | |
| 12 #include "SkImageCache.h" | 13 #include "SkImageCache.h" |
| 14 #include "SkDecodingImageGenerator.h" | |
| 13 #include "SkImagePriv.h" | 15 #include "SkImagePriv.h" |
| 14 #include "SkLazyPixelRef.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 class FlexibleImageGenerator : public SkImageGenerator { | |
| 49 public: | |
| 50 FlexibleImageGenerator(SkBitmapFactory::DecodeProc proc, | |
| 51 SkData* data) | |
| 52 : fProc(proc), fData(data) { | |
| 53 SkASSERT(fData); | |
| 54 SkASSERT(fProc); | |
| 55 fData->ref(); | |
| 56 } | |
| 57 ~FlexibleImageGenerator() { | |
| 58 fData->unref(); | |
| 59 } | |
| 60 SkData* refEncodedData() { | |
| 61 fData->ref(); | |
| 62 return fData; | |
| 63 } | |
| 64 bool getInfo(SkImageInfo* info) { | |
| 65 SkASSERT(info != NULL); | |
| 66 return fProc(fData->data(), fData->size(), info, NULL); | |
| 67 } | |
| 68 bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { | |
| 69 SkASSERT(pixels != NULL); | |
| 70 SkBitmapFactory::Target target = {pixels, rowBytes}; | |
| 71 SkImageInfo tmpInfo = info; | |
| 72 return fProc(fData->data(), fData->size(), &tmpInfo, &target); | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 SkBitmapFactory::DecodeProc fProc; | |
| 77 SkData* fData; | |
| 78 }; | |
| 79 } // namespace | |
| 80 | |
| 46 bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) { | 81 bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) { |
| 47 if (NULL == data || 0 == data->size() || dst == NULL) { | 82 if (NULL == data || 0 == data->size() || dst == NULL) { |
| 48 return false; | 83 return false; |
| 49 } | 84 } |
| 50 | 85 |
| 86 SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(FlexibleImageGenerator, | |
| 87 (fDecodeProc, data))); | |
| 51 SkImageInfo info; | 88 SkImageInfo info; |
| 52 if (!fDecodeProc(data->data(), data->size(), &info, NULL)) { | 89 if (!gen->getInfo(&info)) { |
| 53 return false; | 90 return false; |
| 54 } | 91 } |
| 55 | 92 |
| 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 | |
| 64 // fImageCache and fCacheSelector are mutually exclusive. | 93 // fImageCache and fCacheSelector are mutually exclusive. |
| 65 SkASSERT(NULL == fImageCache || NULL == fCacheSelector); | 94 SkASSERT(NULL == fImageCache || NULL == fCacheSelector); |
| 66 | 95 |
| 67 SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector- >selectCache(info); | 96 SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector- >selectCache(info); |
| 68 | 97 |
| 98 // TODO(halcanary): the SkBitmapFactory::CacheSelector needs a way | |
| 99 // to specify that SkDiscardableMemory should be created via | |
| 100 // SkDiscardableMemory::Create(). Currently, if the cache | |
| 101 // selector reutuns NULL, that is taken as an instruction to use | |
|
scroggo
2013/12/02 19:00:09
returns*
| |
| 102 // normal malloc()ed pixels. | |
| 103 | |
| 69 if (cache != NULL) { | 104 if (cache != NULL) { |
| 70 // Now set a new LazyPixelRef on dst. | 105 return SkDiscardablePixelRef::Install(gen.detach(), dst, cache); |
| 71 SkAutoTUnref<SkLazyPixelRef> lazyRef(SkNEW_ARGS(SkLazyPixelRef, | 106 } else { |
| 72 (data, fDecodeProc, cach e))); | 107 SkBitmap tmp; |
| 73 dst->setPixelRef(lazyRef); | 108 if (!tmp.setConfig(info, 0)) { |
| 109 return false; | |
| 110 } | |
| 111 tmp.allocPixels(); | |
| 112 if (!gen->getPixels(info, tmp.getPixels(), tmp.rowBytes())) { | |
| 113 return false; | |
| 114 } | |
| 115 dst->swap(tmp); | |
| 74 return true; | 116 return true; |
| 75 } else { | |
| 76 dst->allocPixels(); | |
| 77 target.fAddr = dst->getPixels(); | |
| 78 return fDecodeProc(data->data(), data->size(), &info, &target); | |
| 79 } | 117 } |
| 80 } | 118 } |
| OLD | NEW |