blob: d67e0197554a1188a02012801f55c0071c99c5ca [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);
25}
26
27void SkBitmapFactory::setImageCache(SkImageCache *cache) {
28 SkRefCnt_SafeAssign(fImageCache, cache);
29 if (cache != NULL) {
30 fCacheSelector = NULL;
31 }
32}
33
34void SkBitmapFactory::setCacheSelector(CacheSelector selector) {
35 fCacheSelector = selector;
36 if (selector != NULL) {
37 SkSafeUnref(fImageCache);
38 fImageCache = NULL;
39 }
40}
41
42bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) {
43 if (NULL == data || 0 == data->size() || dst == NULL) {
44 return false;
45 }
46
47 SkImage::Info info;
48 if (!fDecodeProc(data->data(), data->size(), &info, NULL)) {
49 return false;
50 }
51
52 bool isOpaque = false;
53 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
54
55 Target target;
56 // FIMXE: There will be a problem if this rowbytes is calculated differently from
57 // in SkLazyPixelRef.
58 target.fRowBytes = SkImageMinRowBytes(info);
59
60 dst->setConfig(config, info.fWidth, info.fHeight, target.fRowBytes);
61 dst->setIsOpaque(isOpaque);
62
63 // fImageCache and fCacheSelector are mutually exclusive.
64 SkASSERT(NULL == fImageCache || NULL == fCacheSelector);
65
66 SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector(info);
67
68 if (cache != NULL) {
69 // Now set a new LazyPixelRef on dst.
70 SkAutoTUnref<SkLazyPixelRef> lazyRef(SkNEW_ARGS(SkLazyPixelRef,
71 (data, fDecodeProc, cache)));
72 dst->setPixelRef(lazyRef);
73 return true;
74 } else {
75 dst->allocPixels();
76 target.fAddr = dst->getPixels();
77 return fDecodeProc(data->data(), data->size(), &info, &target);
78 }
79}