blob: 17ecf47c86b1968c43752e635cd00b8ab7c5aa49 [file] [log] [blame]
scroggo@google.comf8d7d272013-02-22 21:38:35 +00001/*
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
16SkBitmapFactory::SkBitmapFactory(SkBitmapFactory::DecodeProc proc)
17 : fDecodeProc(proc)
18 , fImageCache(NULL)
19 , fCacheSelector(NULL) {
20 SkASSERT(fDecodeProc != NULL);
21}
22
23SkBitmapFactory::~SkBitmapFactory() {
24 SkSafeUnref(fImageCache);
scroggo@google.combb281f72013-03-18 21:37:39 +000025 SkSafeUnref(fCacheSelector);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000026}
27
28void SkBitmapFactory::setImageCache(SkImageCache *cache) {
29 SkRefCnt_SafeAssign(fImageCache, cache);
30 if (cache != NULL) {
scroggo@google.combb281f72013-03-18 21:37:39 +000031 SkSafeUnref(fCacheSelector);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000032 fCacheSelector = NULL;
33 }
34}
35
scroggo@google.combb281f72013-03-18 21:37:39 +000036void SkBitmapFactory::setCacheSelector(CacheSelector* selector) {
37 SkRefCnt_SafeAssign(fCacheSelector, selector);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000038 if (selector != NULL) {
39 SkSafeUnref(fImageCache);
40 fImageCache = NULL;
41 }
42}
43
44bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) {
45 if (NULL == data || 0 == data->size() || dst == NULL) {
46 return false;
47 }
48
reed@google.com2bd8b812013-11-01 13:46:54 +000049 SkImageInfo info;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000050 if (!fDecodeProc(data->data(), data->size(), &info, NULL)) {
51 return false;
52 }
53
reed@google.com383a6972013-10-21 14:00:07 +000054 SkBitmap::Config config = SkImageInfoToBitmapConfig(info);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000055
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.com383a6972013-10-21 14:00:07 +000060 dst->setConfig(config, info.fWidth, info.fHeight, target.fRowBytes, info.fAlphaType);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000061
62 // fImageCache and fCacheSelector are mutually exclusive.
63 SkASSERT(NULL == fImageCache || NULL == fCacheSelector);
64
scroggo@google.combb281f72013-03-18 21:37:39 +000065 SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector->selectCache(info);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000066
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}