blob: 280a18902c93adf95478a51a226674072ce1230d [file] [log] [blame]
reed04617132014-08-21 09:46:49 -07001/*
2 * Copyright 2014 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 "SkBitmapCache.h"
reed011f39a2014-08-28 13:35:23 -07009#include "SkResourceCache.h"
reed680fb9e2014-08-26 09:08:04 -070010#include "SkMipMap.h"
reed04617132014-08-21 09:46:49 -070011#include "SkRect.h"
12
reed14b6aba2014-08-29 10:25:26 -070013SkBitmap::Allocator* SkBitmapCache::GetAllocator() {
14 return SkResourceCache::GetAllocator();
15}
16
reed04617132014-08-21 09:46:49 -070017/**
18 This function finds the bounds of the bitmap *within its pixelRef*.
19 If the bitmap lacks a pixelRef, it will return an empty rect, since
20 that doesn't make sense. This may be a useful enough function that
21 it should be somewhere else (in SkBitmap?).
22 */
23static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) {
24 if (!(bm.pixelRef())) {
25 return SkIRect::MakeEmpty();
26 }
27 SkIPoint origin = bm.pixelRefOrigin();
28 return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height());
29}
30
reed011f39a2014-08-28 13:35:23 -070031struct BitmapKey : public SkResourceCache::Key {
reed04617132014-08-21 09:46:49 -070032public:
33 BitmapKey(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& bounds)
34 : fGenID(genID)
35 , fScaleX(scaleX)
36 , fScaleY(scaleY)
37 , fBounds(bounds)
38 {
39 this->init(sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(fBounds));
40 }
qiankun.miao045bb7f2014-08-25 06:08:25 -070041
reed04617132014-08-21 09:46:49 -070042 uint32_t fGenID;
43 SkScalar fScaleX;
44 SkScalar fScaleY;
45 SkIRect fBounds;
46};
47
48//////////////////////////////////////////////////////////////////////////////////////////
49
reed011f39a2014-08-28 13:35:23 -070050struct BitmapRec : public SkResourceCache::Rec {
reed680fb9e2014-08-26 09:08:04 -070051 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& bounds,
52 const SkBitmap& result)
53 : fKey(genID, scaleX, scaleY, bounds)
54 , fBitmap(result)
55 {}
56
57 BitmapKey fKey;
58 SkBitmap fBitmap;
piotaixr42b0dfe2014-09-03 11:33:13 -070059
reed680fb9e2014-08-26 09:08:04 -070060 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
61 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap.getSize(); }
62};
63
64static bool find_and_return(const BitmapKey& key, SkBitmap* result) {
reed011f39a2014-08-28 13:35:23 -070065 const BitmapRec* rec = (BitmapRec*)SkResourceCache::FindAndLock(key);
reed680fb9e2014-08-26 09:08:04 -070066 if (rec) {
67 *result = rec->fBitmap;
reed011f39a2014-08-28 13:35:23 -070068 SkResourceCache::Unlock(rec);
reed680fb9e2014-08-26 09:08:04 -070069
70 result->lockPixels();
71 if (result->getPixels()) {
72 return true;
73 }
74 // todo: we should explicitly purge rec from the cache at this point, since
75 // it is effectively purged already (has no memory behind it)
76 result->reset();
77 // fall-through to false
78 }
79 return false;
80}
81
82bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY,
83 SkBitmap* result) {
reed04617132014-08-21 09:46:49 -070084 if (0 == invScaleX || 0 == invScaleY) {
85 // degenerate, and the key we use for mipmaps
reed680fb9e2014-08-26 09:08:04 -070086 return false;
reed04617132014-08-21 09:46:49 -070087 }
88 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_bitmap(src));
reed680fb9e2014-08-26 09:08:04 -070089 return find_and_return(key, result);
reed04617132014-08-21 09:46:49 -070090}
91
reed680fb9e2014-08-26 09:08:04 -070092void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY,
93 const SkBitmap& result) {
reed04617132014-08-21 09:46:49 -070094 if (0 == invScaleX || 0 == invScaleY) {
95 // degenerate, and the key we use for mipmaps
reed680fb9e2014-08-26 09:08:04 -070096 return;
reed04617132014-08-21 09:46:49 -070097 }
reed14b6aba2014-08-29 10:25:26 -070098 SkASSERT(result.isImmutable());
reed011f39a2014-08-28 13:35:23 -070099 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (src.getGenerationID(), invScaleX, invScaleY,
100 get_bounds_from_bitmap(src), result)));
reed04617132014-08-21 09:46:49 -0700101}
102
reed680fb9e2014-08-26 09:08:04 -0700103bool SkBitmapCache::Find(uint32_t genID, int width, int height, SkBitmap* result) {
piotaixr42b0dfe2014-09-03 11:33:13 -0700104 return Find(genID, SkIRect::MakeWH(width, height), result);
105}
106
107bool SkBitmapCache::Add(uint32_t genID, int width, int height, const SkBitmap& result) {
108 return Add(genID, SkIRect::MakeWH(width, height), result);
109}
110
111bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result) {
112 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset);
reed680fb9e2014-08-26 09:08:04 -0700113 return find_and_return(key, result);
reed04617132014-08-21 09:46:49 -0700114}
115
piotaixr42b0dfe2014-09-03 11:33:13 -0700116bool SkBitmapCache::Add(uint32_t genID, const SkIRect& subset, const SkBitmap& result) {
reed14b6aba2014-08-29 10:25:26 -0700117 SkASSERT(result.isImmutable());
reed04617132014-08-21 09:46:49 -0700118
piotaixr42b0dfe2014-09-03 11:33:13 -0700119 if (subset.isEmpty()
120 || subset.top() < 0
121 || subset.left() < 0
122 || result.width() != subset.width()
123 || result.height() != subset.height()) {
124 return false;
125 } else {
126 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1,
127 subset, result)));
128
129 return true;
130 }
131}
reed680fb9e2014-08-26 09:08:04 -0700132//////////////////////////////////////////////////////////////////////////////////////////
reed04617132014-08-21 09:46:49 -0700133
reed011f39a2014-08-28 13:35:23 -0700134struct MipMapRec : public SkResourceCache::Rec {
reed680fb9e2014-08-26 09:08:04 -0700135 MipMapRec(const SkBitmap& src, const SkMipMap* result)
136 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src))
137 , fMipMap(SkRef(result))
138 {}
139
140 virtual ~MipMapRec() {
141 fMipMap->unref();
142 }
143
144 BitmapKey fKey;
145 const SkMipMap* fMipMap;
piotaixr42b0dfe2014-09-03 11:33:13 -0700146
reed680fb9e2014-08-26 09:08:04 -0700147 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
148 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap->getSize(); }
149};
150
151
152const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) {
153 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src));
reed011f39a2014-08-28 13:35:23 -0700154 const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key);
reed680fb9e2014-08-26 09:08:04 -0700155 const SkMipMap* result = NULL;
156 if (rec) {
157 result = SkRef(rec->fMipMap);
reed011f39a2014-08-28 13:35:23 -0700158 SkResourceCache::Unlock(rec);
reed680fb9e2014-08-26 09:08:04 -0700159 }
160 return result;
reed04617132014-08-21 09:46:49 -0700161}
162
reed680fb9e2014-08-26 09:08:04 -0700163void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) {
164 if (result) {
reed011f39a2014-08-28 13:35:23 -0700165 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result)));
reed680fb9e2014-08-26 09:08:04 -0700166 }
reed04617132014-08-21 09:46:49 -0700167}
168