blob: 0ff4ee2d17d018cf36f4af49def81340e33df12f [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
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000016SK_DEFINE_INST_COUNT(SkBitmapFactory::CacheSelector)
17
scroggo@google.comf8d7d272013-02-22 21:38:35 +000018SkBitmapFactory::SkBitmapFactory(SkBitmapFactory::DecodeProc proc)
19 : fDecodeProc(proc)
20 , fImageCache(NULL)
21 , fCacheSelector(NULL) {
22 SkASSERT(fDecodeProc != NULL);
23}
24
25SkBitmapFactory::~SkBitmapFactory() {
26 SkSafeUnref(fImageCache);
scroggo@google.combb281f72013-03-18 21:37:39 +000027 SkSafeUnref(fCacheSelector);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000028}
29
30void SkBitmapFactory::setImageCache(SkImageCache *cache) {
31 SkRefCnt_SafeAssign(fImageCache, cache);
32 if (cache != NULL) {
scroggo@google.combb281f72013-03-18 21:37:39 +000033 SkSafeUnref(fCacheSelector);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000034 fCacheSelector = NULL;
35 }
36}
37
scroggo@google.combb281f72013-03-18 21:37:39 +000038void SkBitmapFactory::setCacheSelector(CacheSelector* selector) {
39 SkRefCnt_SafeAssign(fCacheSelector, selector);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000040 if (selector != NULL) {
41 SkSafeUnref(fImageCache);
42 fImageCache = NULL;
43 }
44}
45
46bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) {
47 if (NULL == data || 0 == data->size() || dst == NULL) {
48 return false;
49 }
50
51 SkImage::Info info;
52 if (!fDecodeProc(data->data(), data->size(), &info, NULL)) {
53 return false;
54 }
55
56 bool isOpaque = false;
57 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
58
59 Target target;
60 // FIMXE: There will be a problem if this rowbytes is calculated differently from
61 // in SkLazyPixelRef.
62 target.fRowBytes = SkImageMinRowBytes(info);
63
64 dst->setConfig(config, info.fWidth, info.fHeight, target.fRowBytes);
65 dst->setIsOpaque(isOpaque);
66
67 // fImageCache and fCacheSelector are mutually exclusive.
68 SkASSERT(NULL == fImageCache || NULL == fCacheSelector);
69
scroggo@google.combb281f72013-03-18 21:37:39 +000070 SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector->selectCache(info);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000071
72 if (cache != NULL) {
73 // Now set a new LazyPixelRef on dst.
74 SkAutoTUnref<SkLazyPixelRef> lazyRef(SkNEW_ARGS(SkLazyPixelRef,
75 (data, fDecodeProc, cache)));
76 dst->setPixelRef(lazyRef);
77 return true;
78 } else {
79 dst->allocPixels();
80 target.fAddr = dst->getPixels();
81 return fDecodeProc(data->data(), data->size(), &info, &target);
82 }
83}