| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2012 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 | |
| 8 #include "SkBitmapFactory.h" | |
| 9 | |
| 10 #include "SkBitmap.h" | |
| 11 #include "SkData.h" | |
| 12 #include "SkImageCache.h" | |
| 13 #include "SkImagePriv.h" | |
| 14 #include "SkLazyPixelRef.h" | |
| 15 | |
| 16 SK_DEFINE_INST_COUNT(SkBitmapFactory::CacheSelector) | |
| 17 | |
| 18 SkBitmapFactory::SkBitmapFactory(SkBitmapFactory::DecodeProc proc) | |
| 19 : fDecodeProc(proc) | |
| 20 , fImageCache(NULL) | |
| 21 , fCacheSelector(NULL) { | |
| 22 SkASSERT(fDecodeProc != NULL); | |
| 23 } | |
| 24 | |
| 25 SkBitmapFactory::~SkBitmapFactory() { | |
| 26 SkSafeUnref(fImageCache); | |
| 27 SkSafeUnref(fCacheSelector); | |
| 28 } | |
| 29 | |
| 30 void SkBitmapFactory::setImageCache(SkImageCache *cache) { | |
| 31 SkRefCnt_SafeAssign(fImageCache, cache); | |
| 32 if (cache != NULL) { | |
| 33 SkSafeUnref(fCacheSelector); | |
| 34 fCacheSelector = NULL; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 void SkBitmapFactory::setCacheSelector(CacheSelector* selector) { | |
| 39 SkRefCnt_SafeAssign(fCacheSelector, selector); | |
| 40 if (selector != NULL) { | |
| 41 SkSafeUnref(fImageCache); | |
| 42 fImageCache = NULL; | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) { | |
| 47 if (NULL == data || 0 == data->size() || dst == NULL) { | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 SkImageInfo info; | |
| 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 | |
| 64 // fImageCache and fCacheSelector are mutually exclusive. | |
| 65 SkASSERT(NULL == fImageCache || NULL == fCacheSelector); | |
| 66 | |
| 67 SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector-
>selectCache(info); | |
| 68 | |
| 69 if (cache != NULL) { | |
| 70 // Now set a new LazyPixelRef on dst. | |
| 71 SkAutoTUnref<SkLazyPixelRef> lazyRef(SkNEW_ARGS(SkLazyPixelRef, | |
| 72 (data, fDecodeProc, cach
e))); | |
| 73 dst->setPixelRef(lazyRef); | |
| 74 return true; | |
| 75 } else { | |
| 76 dst->allocPixels(); | |
| 77 target.fAddr = dst->getPixels(); | |
| 78 return fDecodeProc(data->data(), data->size(), &info, &target); | |
| 79 } | |
| 80 } | |
| OLD | NEW |