aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/lazy/SkBitmapFactory.cpp
blob: 546485140203b070e9a78c9a017f9765edfe4071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkBitmapFactory.h"

#include "SkBitmap.h"
#include "SkData.h"
#include "SkImageCache.h"
#include "SkImagePriv.h"
#include "SkLazyPixelRef.h"

SK_DEFINE_INST_COUNT(SkBitmapFactory::CacheSelector)

SkBitmapFactory::SkBitmapFactory(SkBitmapFactory::DecodeProc proc)
    : fDecodeProc(proc)
    , fImageCache(NULL)
    , fCacheSelector(NULL) {
    SkASSERT(fDecodeProc != NULL);
}

SkBitmapFactory::~SkBitmapFactory() {
    SkSafeUnref(fImageCache);
    SkSafeUnref(fCacheSelector);
}

void SkBitmapFactory::setImageCache(SkImageCache *cache) {
    SkRefCnt_SafeAssign(fImageCache, cache);
    if (cache != NULL) {
        SkSafeUnref(fCacheSelector);
        fCacheSelector = NULL;
    }
}

void SkBitmapFactory::setCacheSelector(CacheSelector* selector) {
    SkRefCnt_SafeAssign(fCacheSelector, selector);
    if (selector != NULL) {
        SkSafeUnref(fImageCache);
        fImageCache = NULL;
    }
}

bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) {
    if (NULL == data || 0 == data->size() || dst == NULL) {
        return false;
    }

    SkImage::Info info;
    if (!fDecodeProc(data->data(), data->size(), &info, NULL)) {
        return false;
    }

    SkBitmap::Config config = SkImageInfoToBitmapConfig(info);

    Target target;
    // FIMXE: There will be a problem if this rowbytes is calculated differently from
    // in SkLazyPixelRef.
    target.fRowBytes = SkImageMinRowBytes(info);
    dst->setConfig(config, info.fWidth, info.fHeight, target.fRowBytes, info.fAlphaType);

    // fImageCache and fCacheSelector are mutually exclusive.
    SkASSERT(NULL == fImageCache || NULL == fCacheSelector);

    SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector->selectCache(info);

    if (cache != NULL) {
        // Now set a new LazyPixelRef on dst.
        SkAutoTUnref<SkLazyPixelRef> lazyRef(SkNEW_ARGS(SkLazyPixelRef,
                                                        (data, fDecodeProc, cache)));
        dst->setPixelRef(lazyRef);
        return true;
    } else {
        dst->allocPixels();
        target.fAddr = dst->getPixels();
        return fDecodeProc(data->data(), data->size(), &info, &target);
    }
}