blob: 60c4993e3cf0230982065aa736c6a5cdf2fa964a [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
49 SkImage::Info info;
50 if (!fDecodeProc(data->data(), data->size(), &info, NULL)) {
51 return false;
52 }
53
54 bool isOpaque = false;
55 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
56
57 Target target;
58 // FIMXE: There will be a problem if this rowbytes is calculated differently from
59 // in SkLazyPixelRef.
60 target.fRowBytes = SkImageMinRowBytes(info);
61
62 dst->setConfig(config, info.fWidth, info.fHeight, target.fRowBytes);
63 dst->setIsOpaque(isOpaque);
64
65 // fImageCache and fCacheSelector are mutually exclusive.
66 SkASSERT(NULL == fImageCache || NULL == fCacheSelector);
67
scroggo@google.combb281f72013-03-18 21:37:39 +000068 SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector->selectCache(info);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000069
70 if (cache != NULL) {
71 // Now set a new LazyPixelRef on dst.
72 SkAutoTUnref<SkLazyPixelRef> lazyRef(SkNEW_ARGS(SkLazyPixelRef,
73 (data, fDecodeProc, cache)));
74 dst->setPixelRef(lazyRef);
75 return true;
76 } else {
77 dst->allocPixels();
78 target.fAddr = dst->getPixels();
79 return fDecodeProc(data->data(), data->size(), &info, &target);
80 }
81}