scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 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 | SkBitmapFactory::SkBitmapFactory(SkBitmapFactory::DecodeProc proc) |
| 17 | : fDecodeProc(proc) |
| 18 | , fImageCache(NULL) |
| 19 | , fCacheSelector(NULL) { |
| 20 | SkASSERT(fDecodeProc != NULL); |
| 21 | } |
| 22 | |
| 23 | SkBitmapFactory::~SkBitmapFactory() { |
| 24 | SkSafeUnref(fImageCache); |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame] | 25 | SkSafeUnref(fCacheSelector); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void SkBitmapFactory::setImageCache(SkImageCache *cache) { |
| 29 | SkRefCnt_SafeAssign(fImageCache, cache); |
| 30 | if (cache != NULL) { |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame] | 31 | SkSafeUnref(fCacheSelector); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 32 | fCacheSelector = NULL; |
| 33 | } |
| 34 | } |
| 35 | |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame] | 36 | void SkBitmapFactory::setCacheSelector(CacheSelector* selector) { |
| 37 | SkRefCnt_SafeAssign(fCacheSelector, selector); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 38 | if (selector != NULL) { |
| 39 | SkSafeUnref(fImageCache); |
| 40 | fImageCache = NULL; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) { |
| 45 | if (NULL == data || 0 == data->size() || dst == NULL) { |
| 46 | return false; |
| 47 | } |
| 48 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 49 | SkImageInfo info; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 50 | if (!fDecodeProc(data->data(), data->size(), &info, NULL)) { |
| 51 | return false; |
| 52 | } |
| 53 | |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 54 | SkBitmap::Config config = SkImageInfoToBitmapConfig(info); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 55 | |
| 56 | Target target; |
| 57 | // FIMXE: There will be a problem if this rowbytes is calculated differently from |
| 58 | // in SkLazyPixelRef. |
| 59 | target.fRowBytes = SkImageMinRowBytes(info); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 60 | dst->setConfig(config, info.fWidth, info.fHeight, target.fRowBytes, info.fAlphaType); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 61 | |
| 62 | // fImageCache and fCacheSelector are mutually exclusive. |
| 63 | SkASSERT(NULL == fImageCache || NULL == fCacheSelector); |
| 64 | |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame] | 65 | SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector->selectCache(info); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 66 | |
| 67 | if (cache != NULL) { |
| 68 | // Now set a new LazyPixelRef on dst. |
| 69 | SkAutoTUnref<SkLazyPixelRef> lazyRef(SkNEW_ARGS(SkLazyPixelRef, |
| 70 | (data, fDecodeProc, cache))); |
| 71 | dst->setPixelRef(lazyRef); |
| 72 | return true; |
| 73 | } else { |
| 74 | dst->allocPixels(); |
| 75 | target.fAddr = dst->getPixels(); |
| 76 | return fDecodeProc(data->data(), data->size(), &info, &target); |
| 77 | } |
| 78 | } |