aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/LazyDecodeBitmap.cpp
blob: c0b7eea724fa621325b539c6dfd8dc7fb144854d (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
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "LazyDecodeBitmap.h"

#include "PictureRenderingFlags.h"  // --deferImageDecoding is defined here.
#include "SkBitmap.h"
#include "SkData.h"
#include "SkImageDecoder.h"
#include "SkForceLinking.h"
#include "SkLruImageCache.h"
#include "SkPurgeableImageCache.h"
#include "SkCommandLineFlags.h"

__SK_FORCE_IMAGE_DECODER_LINKING;

DEFINE_bool(useVolatileCache, false, "Use a volatile cache for deferred image decoding pixels. "
            "Only meaningful if --deferImageDecoding is set to true and the platform has an "
            "implementation.");

SkLruImageCache gLruImageCache(1024 * 1024);

namespace sk_tools {

// Simple cache selector to choose between a purgeable cache for large images and the standard one
// for smaller images.
//
class CacheSelector : public SkBitmapFactory::CacheSelector {

public:
    CacheSelector() {
        fPurgeableImageCache = SkPurgeableImageCache::Create();
    }

    ~CacheSelector() {
        SkSafeUnref(fPurgeableImageCache);
    }

    virtual SkImageCache* selectCache(const SkImageInfo& info) SK_OVERRIDE {
        if (info.fWidth * info.fHeight > 32 * 1024 && fPurgeableImageCache != NULL) {
            return fPurgeableImageCache;
        }
        return &gLruImageCache;
    }
private:
    SkImageCache* fPurgeableImageCache;
};

static CacheSelector gCacheSelector;
static SkBitmapFactory gFactory(&SkImageDecoder::DecodeMemoryToTarget);

bool LazyDecodeBitmap(const void* buffer, size_t size, SkBitmap* bitmap) {
    void* copiedBuffer = sk_malloc_throw(size);
    memcpy(copiedBuffer, buffer, size);
    SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size));

    static bool gOnce;
    if (!gOnce) {
        // Only use the cache selector if there is a purgeable image cache to use for large
        // images.
        if (FLAGS_useVolatileCache && SkAutoTUnref<SkImageCache>(
                SkPurgeableImageCache::Create()).get() != NULL) {
            gFactory.setCacheSelector(&gCacheSelector);
        } else {
            gFactory.setImageCache(&gLruImageCache);
        }
        gOnce = true;
    }
    return gFactory.installPixelRef(data, bitmap);
}

}  // namespace sk_tools