blob: 5044d305186bfc00ef24a197defe42e94b2ec8be [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(); }
reed680fb9e2014-08-26 09:08:04 -070062
reedc90e0142014-09-15 11:39:44 -070063 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextBitmap) {
64 const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec);
65 SkBitmap* result = (SkBitmap*)contextBitmap;
reed595aa052014-09-15 10:15:18 -070066
reedc90e0142014-09-15 11:39:44 -070067 *result = rec.fBitmap;
reed595aa052014-09-15 10:15:18 -070068 result->lockPixels();
reedc90e0142014-09-15 11:39:44 -070069 return SkToBool(result->getPixels());
reed595aa052014-09-15 10:15:18 -070070 }
reedc90e0142014-09-15 11:39:44 -070071};
reed595aa052014-09-15 10:15:18 -070072
reed30ad5302014-09-16 10:39:55 -070073#define CHECK_LOCAL(localCache, localName, globalName, ...) \
74 (localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalName(__VA_ARGS__)
75
76bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY, SkBitmap* result,
77 SkResourceCache* localCache) {
reed04617132014-08-21 09:46:49 -070078 if (0 == invScaleX || 0 == invScaleY) {
79 // degenerate, and the key we use for mipmaps
reed680fb9e2014-08-26 09:08:04 -070080 return false;
reed04617132014-08-21 09:46:49 -070081 }
82 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_bitmap(src));
reed30ad5302014-09-16 10:39:55 -070083
84 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Visitor, result);
reed04617132014-08-21 09:46:49 -070085}
86
reed680fb9e2014-08-26 09:08:04 -070087void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY,
reed30ad5302014-09-16 10:39:55 -070088 const SkBitmap& result, SkResourceCache* localCache) {
reed04617132014-08-21 09:46:49 -070089 if (0 == invScaleX || 0 == invScaleY) {
90 // degenerate, and the key we use for mipmaps
reed680fb9e2014-08-26 09:08:04 -070091 return;
reed04617132014-08-21 09:46:49 -070092 }
reed14b6aba2014-08-29 10:25:26 -070093 SkASSERT(result.isImmutable());
reed30ad5302014-09-16 10:39:55 -070094 BitmapRec* rec = SkNEW_ARGS(BitmapRec, (src.getGenerationID(), invScaleX, invScaleY,
95 get_bounds_from_bitmap(src), result));
96 CHECK_LOCAL(localCache, add, Add, rec);
reed04617132014-08-21 09:46:49 -070097}
98
reed30ad5302014-09-16 10:39:55 -070099bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result,
100 SkResourceCache* localCache) {
piotaixr42b0dfe2014-09-03 11:33:13 -0700101 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset);
reed30ad5302014-09-16 10:39:55 -0700102
103 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Visitor, result);
reed04617132014-08-21 09:46:49 -0700104}
105
reed30ad5302014-09-16 10:39:55 -0700106bool SkBitmapCache::Add(uint32_t genID, const SkIRect& subset, const SkBitmap& result,
107 SkResourceCache* localCache) {
reed14b6aba2014-08-29 10:25:26 -0700108 SkASSERT(result.isImmutable());
reed04617132014-08-21 09:46:49 -0700109
piotaixr42b0dfe2014-09-03 11:33:13 -0700110 if (subset.isEmpty()
111 || subset.top() < 0
112 || subset.left() < 0
113 || result.width() != subset.width()
114 || result.height() != subset.height()) {
115 return false;
116 } else {
reed30ad5302014-09-16 10:39:55 -0700117 BitmapRec* rec = SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1, subset, result));
piotaixr42b0dfe2014-09-03 11:33:13 -0700118
reed30ad5302014-09-16 10:39:55 -0700119 CHECK_LOCAL(localCache, add, Add, rec);
piotaixr42b0dfe2014-09-03 11:33:13 -0700120 return true;
121 }
122}
reed680fb9e2014-08-26 09:08:04 -0700123//////////////////////////////////////////////////////////////////////////////////////////
reed04617132014-08-21 09:46:49 -0700124
reed011f39a2014-08-28 13:35:23 -0700125struct MipMapRec : public SkResourceCache::Rec {
reed680fb9e2014-08-26 09:08:04 -0700126 MipMapRec(const SkBitmap& src, const SkMipMap* result)
127 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src))
128 , fMipMap(SkRef(result))
129 {}
130
131 virtual ~MipMapRec() {
132 fMipMap->unref();
133 }
134
135 BitmapKey fKey;
136 const SkMipMap* fMipMap;
piotaixr42b0dfe2014-09-03 11:33:13 -0700137
reed680fb9e2014-08-26 09:08:04 -0700138 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
139 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap->getSize(); }
reed680fb9e2014-08-26 09:08:04 -0700140
reedc90e0142014-09-15 11:39:44 -0700141 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextMip) {
142 const MipMapRec& rec = static_cast<const MipMapRec&>(baseRec);
143 const SkMipMap** result = (const SkMipMap**)contextMip;
144
145 *result = SkRef(rec.fMipMap);
146 // mipmaps don't use the custom allocator yet, so we don't need to check pixels
147 return true;
148 }
149};
reed595aa052014-09-15 10:15:18 -0700150
reed680fb9e2014-08-26 09:08:04 -0700151const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) {
152 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src));
reedc90e0142014-09-15 11:39:44 -0700153 const SkMipMap* result;
154 if (!SkResourceCache::Find(key, MipMapRec::Visitor, &result)) {
155 result = NULL;
reed680fb9e2014-08-26 09:08:04 -0700156 }
157 return result;
reed04617132014-08-21 09:46:49 -0700158}
159
reed680fb9e2014-08-26 09:08:04 -0700160void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) {
161 if (result) {
reed011f39a2014-08-28 13:35:23 -0700162 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result)));
reed680fb9e2014-08-26 09:08:04 -0700163 }
reed04617132014-08-21 09:46:49 -0700164}
165