blob: 2b11b81fd793b978187bfe8854c81b589d507473 [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"
reed83787d02015-02-25 07:17:11 -080011#include "SkPixelRef.h"
reed04617132014-08-21 09:46:49 -070012#include "SkRect.h"
13
reed7eeba252015-02-24 13:54:23 -080014/**
15 * Use this for bitmapcache and mipmapcache entries.
16 */
17uint64_t SkMakeResourceCacheSharedIDForBitmap(uint32_t bitmapGenID) {
18 uint64_t sharedID = SkSetFourByteTag('b', 'm', 'a', 'p');
19 return (sharedID << 32) | bitmapGenID;
20}
21
22void SkNotifyBitmapGenIDIsStale(uint32_t bitmapGenID) {
23 SkResourceCache::PostPurgeSharedID(SkMakeResourceCacheSharedIDForBitmap(bitmapGenID));
24}
25
26///////////////////////////////////////////////////////////////////////////////////////////////////
27
reed14b6aba2014-08-29 10:25:26 -070028SkBitmap::Allocator* SkBitmapCache::GetAllocator() {
29 return SkResourceCache::GetAllocator();
30}
31
reed04617132014-08-21 09:46:49 -070032/**
33 This function finds the bounds of the bitmap *within its pixelRef*.
34 If the bitmap lacks a pixelRef, it will return an empty rect, since
35 that doesn't make sense. This may be a useful enough function that
36 it should be somewhere else (in SkBitmap?).
37 */
38static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) {
39 if (!(bm.pixelRef())) {
40 return SkIRect::MakeEmpty();
41 }
42 SkIPoint origin = bm.pixelRefOrigin();
43 return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height());
44}
45
fmalita171e5b72014-10-22 11:20:40 -070046namespace {
47static unsigned gBitmapKeyNamespaceLabel;
48
reed011f39a2014-08-28 13:35:23 -070049struct BitmapKey : public SkResourceCache::Key {
reed04617132014-08-21 09:46:49 -070050public:
reed7eeba252015-02-24 13:54:23 -080051 BitmapKey(uint32_t genID, SkScalar sx, SkScalar sy, const SkIRect& bounds)
52 : fGenID(genID)
53 , fScaleX(sx)
54 , fScaleY(sy)
55 , fBounds(bounds)
reed04617132014-08-21 09:46:49 -070056 {
reed7eeba252015-02-24 13:54:23 -080057 this->init(&gBitmapKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitmap(genID),
fmalita171e5b72014-10-22 11:20:40 -070058 sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(fBounds));
reed04617132014-08-21 09:46:49 -070059 }
qiankun.miao045bb7f2014-08-25 06:08:25 -070060
reed04617132014-08-21 09:46:49 -070061 uint32_t fGenID;
62 SkScalar fScaleX;
63 SkScalar fScaleY;
64 SkIRect fBounds;
65};
66
reed011f39a2014-08-28 13:35:23 -070067struct BitmapRec : public SkResourceCache::Rec {
reed680fb9e2014-08-26 09:08:04 -070068 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& bounds,
69 const SkBitmap& result)
70 : fKey(genID, scaleX, scaleY, bounds)
71 , fBitmap(result)
72 {}
73
mtklein36352bf2015-03-25 18:17:31 -070074 const Key& getKey() const override { return fKey; }
75 size_t bytesUsed() const override { return sizeof(fKey) + fBitmap.getSize(); }
reed680fb9e2014-08-26 09:08:04 -070076
reed216b6432015-08-19 12:25:40 -070077 const char* getCategory() const override { return "bitmap"; }
78 SkDiscardableMemory* diagnostic_only_getDiscardable() const override {
79 return fBitmap.pixelRef()->diagnostic_only_getDiscardable();
80 }
81
reed7eeba252015-02-24 13:54:23 -080082 static bool Finder(const SkResourceCache::Rec& baseRec, void* contextBitmap) {
reedc90e0142014-09-15 11:39:44 -070083 const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec);
84 SkBitmap* result = (SkBitmap*)contextBitmap;
reed595aa052014-09-15 10:15:18 -070085
reedc90e0142014-09-15 11:39:44 -070086 *result = rec.fBitmap;
reed595aa052014-09-15 10:15:18 -070087 result->lockPixels();
reedc90e0142014-09-15 11:39:44 -070088 return SkToBool(result->getPixels());
reed595aa052014-09-15 10:15:18 -070089 }
reed7eeba252015-02-24 13:54:23 -080090
91private:
92 BitmapKey fKey;
93 SkBitmap fBitmap;
reedc90e0142014-09-15 11:39:44 -070094};
fmalita171e5b72014-10-22 11:20:40 -070095} // namespace
reed595aa052014-09-15 10:15:18 -070096
reed30ad5302014-09-16 10:39:55 -070097#define CHECK_LOCAL(localCache, localName, globalName, ...) \
reed9d93c2e2014-10-08 05:17:12 -070098 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalName(__VA_ARGS__))
reed30ad5302014-09-16 10:39:55 -070099
100bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY, SkBitmap* result,
101 SkResourceCache* localCache) {
reed04617132014-08-21 09:46:49 -0700102 if (0 == invScaleX || 0 == invScaleY) {
103 // degenerate, and the key we use for mipmaps
reed680fb9e2014-08-26 09:08:04 -0700104 return false;
reed04617132014-08-21 09:46:49 -0700105 }
106 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_bitmap(src));
reed30ad5302014-09-16 10:39:55 -0700107
reed7eeba252015-02-24 13:54:23 -0800108 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result);
reed04617132014-08-21 09:46:49 -0700109}
110
reed680fb9e2014-08-26 09:08:04 -0700111void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY,
reed30ad5302014-09-16 10:39:55 -0700112 const SkBitmap& result, SkResourceCache* localCache) {
reed04617132014-08-21 09:46:49 -0700113 if (0 == invScaleX || 0 == invScaleY) {
114 // degenerate, and the key we use for mipmaps
reed680fb9e2014-08-26 09:08:04 -0700115 return;
reed04617132014-08-21 09:46:49 -0700116 }
reed14b6aba2014-08-29 10:25:26 -0700117 SkASSERT(result.isImmutable());
halcanary385fe4d2015-08-26 13:07:48 -0700118 BitmapRec* rec = new BitmapRec(src.getGenerationID(), invScaleX, invScaleY,
119 get_bounds_from_bitmap(src), result);
reed30ad5302014-09-16 10:39:55 -0700120 CHECK_LOCAL(localCache, add, Add, rec);
reed83787d02015-02-25 07:17:11 -0800121 src.pixelRef()->notifyAddedToCache();
reed04617132014-08-21 09:46:49 -0700122}
123
reed30ad5302014-09-16 10:39:55 -0700124bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result,
125 SkResourceCache* localCache) {
piotaixr42b0dfe2014-09-03 11:33:13 -0700126 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset);
reed30ad5302014-09-16 10:39:55 -0700127
reed7eeba252015-02-24 13:54:23 -0800128 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result);
reed04617132014-08-21 09:46:49 -0700129}
130
reed83787d02015-02-25 07:17:11 -0800131bool SkBitmapCache::Add(SkPixelRef* pr, const SkIRect& subset, const SkBitmap& result,
reed30ad5302014-09-16 10:39:55 -0700132 SkResourceCache* localCache) {
reed14b6aba2014-08-29 10:25:26 -0700133 SkASSERT(result.isImmutable());
reed04617132014-08-21 09:46:49 -0700134
piotaixr42b0dfe2014-09-03 11:33:13 -0700135 if (subset.isEmpty()
136 || subset.top() < 0
137 || subset.left() < 0
138 || result.width() != subset.width()
139 || result.height() != subset.height()) {
140 return false;
141 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700142 BitmapRec* rec = new BitmapRec(pr->getGenerationID(), 1, 1, subset, result);
piotaixr42b0dfe2014-09-03 11:33:13 -0700143
reed30ad5302014-09-16 10:39:55 -0700144 CHECK_LOCAL(localCache, add, Add, rec);
reed83787d02015-02-25 07:17:11 -0800145 pr->notifyAddedToCache();
piotaixr42b0dfe2014-09-03 11:33:13 -0700146 return true;
147 }
148}
reed7eeba252015-02-24 13:54:23 -0800149
reed6f1216a2015-08-04 08:10:13 -0700150bool SkBitmapCache::Find(uint32_t genID, SkBitmap* result, SkResourceCache* localCache) {
151 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeEmpty());
152
153 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result);
154}
155
156void SkBitmapCache::Add(uint32_t genID, const SkBitmap& result, SkResourceCache* localCache) {
157 SkASSERT(result.isImmutable());
158
halcanary385fe4d2015-08-26 13:07:48 -0700159 BitmapRec* rec = new BitmapRec(genID, 1, 1, SkIRect::MakeEmpty(), result);
reed6f1216a2015-08-04 08:10:13 -0700160
161 CHECK_LOCAL(localCache, add, Add, rec);
162}
163
reed680fb9e2014-08-26 09:08:04 -0700164//////////////////////////////////////////////////////////////////////////////////////////
reed7eeba252015-02-24 13:54:23 -0800165//////////////////////////////////////////////////////////////////////////////////////////
166
167namespace {
168static unsigned gMipMapKeyNamespaceLabel;
169
170struct MipMapKey : public SkResourceCache::Key {
171public:
172 MipMapKey(uint32_t genID, const SkIRect& bounds) : fGenID(genID), fBounds(bounds) {
173 this->init(&gMipMapKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitmap(genID),
174 sizeof(fGenID) + sizeof(fBounds));
175 }
176
177 uint32_t fGenID;
178 SkIRect fBounds;
179};
reed04617132014-08-21 09:46:49 -0700180
reed011f39a2014-08-28 13:35:23 -0700181struct MipMapRec : public SkResourceCache::Rec {
reed680fb9e2014-08-26 09:08:04 -0700182 MipMapRec(const SkBitmap& src, const SkMipMap* result)
reed7eeba252015-02-24 13:54:23 -0800183 : fKey(src.getGenerationID(), get_bounds_from_bitmap(src))
reed9d93c2e2014-10-08 05:17:12 -0700184 , fMipMap(result)
185 {
186 fMipMap->attachToCacheAndRef();
reed92561a02014-10-02 13:47:08 -0700187 }
piotaixr42b0dfe2014-09-03 11:33:13 -0700188
reed9d93c2e2014-10-08 05:17:12 -0700189 virtual ~MipMapRec() {
190 fMipMap->detachFromCacheAndUnref();
191 }
reed37c5a812014-10-03 13:23:30 -0700192
mtklein36352bf2015-03-25 18:17:31 -0700193 const Key& getKey() const override { return fKey; }
194 size_t bytesUsed() const override { return sizeof(fKey) + fMipMap->size(); }
reed216b6432015-08-19 12:25:40 -0700195 const char* getCategory() const override { return "mipmap"; }
196 SkDiscardableMemory* diagnostic_only_getDiscardable() const override {
197 return fMipMap->diagnostic_only_getDiscardable();
198 }
reed680fb9e2014-08-26 09:08:04 -0700199
reed7eeba252015-02-24 13:54:23 -0800200 static bool Finder(const SkResourceCache::Rec& baseRec, void* contextMip) {
reedc90e0142014-09-15 11:39:44 -0700201 const MipMapRec& rec = static_cast<const MipMapRec&>(baseRec);
reed9d93c2e2014-10-08 05:17:12 -0700202 const SkMipMap* mm = SkRef(rec.fMipMap);
203 // the call to ref() above triggers a "lock" in the case of discardable memory,
204 // which means we can now check for null (in case the lock failed).
halcanary96fcdcc2015-08-27 07:41:13 -0700205 if (nullptr == mm->data()) {
reed9d93c2e2014-10-08 05:17:12 -0700206 mm->unref(); // balance our call to ref()
207 return false;
208 }
209 // the call must call unref() when they are done.
210 *(const SkMipMap**)contextMip = mm;
reedc90e0142014-09-15 11:39:44 -0700211 return true;
212 }
reed9d93c2e2014-10-08 05:17:12 -0700213
214private:
reed7eeba252015-02-24 13:54:23 -0800215 MipMapKey fKey;
reed9d93c2e2014-10-08 05:17:12 -0700216 const SkMipMap* fMipMap;
reedc90e0142014-09-15 11:39:44 -0700217};
reed7eeba252015-02-24 13:54:23 -0800218}
reed595aa052014-09-15 10:15:18 -0700219
reed9d93c2e2014-10-08 05:17:12 -0700220const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src, SkResourceCache* localCache) {
reed7eeba252015-02-24 13:54:23 -0800221 MipMapKey key(src.getGenerationID(), get_bounds_from_bitmap(src));
reedc90e0142014-09-15 11:39:44 -0700222 const SkMipMap* result;
reed9d93c2e2014-10-08 05:17:12 -0700223
reed7eeba252015-02-24 13:54:23 -0800224 if (!CHECK_LOCAL(localCache, find, Find, key, MipMapRec::Finder, &result)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700225 result = nullptr;
reed680fb9e2014-08-26 09:08:04 -0700226 }
227 return result;
reed04617132014-08-21 09:46:49 -0700228}
229
reed9d93c2e2014-10-08 05:17:12 -0700230static SkResourceCache::DiscardableFactory get_fact(SkResourceCache* localCache) {
231 return localCache ? localCache->GetDiscardableFactory()
232 : SkResourceCache::GetDiscardableFactory();
233}
234
235const SkMipMap* SkMipMapCache::AddAndRef(const SkBitmap& src, SkResourceCache* localCache) {
236 SkMipMap* mipmap = SkMipMap::Build(src, get_fact(localCache));
237 if (mipmap) {
halcanary385fe4d2015-08-26 13:07:48 -0700238 MipMapRec* rec = new MipMapRec(src, mipmap);
reed9d93c2e2014-10-08 05:17:12 -0700239 CHECK_LOCAL(localCache, add, Add, rec);
reed83787d02015-02-25 07:17:11 -0800240 src.pixelRef()->notifyAddedToCache();
reed680fb9e2014-08-26 09:08:04 -0700241 }
reed9d93c2e2014-10-08 05:17:12 -0700242 return mipmap;
reed04617132014-08-21 09:46:49 -0700243}