blob: 6d4f4b4cc7d7349173f34048e9ba36a16cd1c508 [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(); }
reeddee6a8e2014-09-15 06:44:47 -070062};
reed680fb9e2014-08-26 09:08:04 -070063
reed595aa052014-09-15 10:15:18 -070064static bool find_and_return(const BitmapKey& key, SkBitmap* result) {
65 const BitmapRec* rec = (BitmapRec*)SkResourceCache::FindAndLock(key);
66 if (rec) {
67 *result = rec->fBitmap;
68 SkResourceCache::Unlock(rec);
69
70 result->lockPixels();
71 if (result->getPixels()) {
72 return true;
73 }
74
75 SkResourceCache::Remove(rec);
76 result->reset();
77 // fall-through to false
78 }
79 return false;
80}
81
reed680fb9e2014-08-26 09:08:04 -070082bool 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));
reed595aa052014-09-15 10:15:18 -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
piotaixr42b0dfe2014-09-03 11:33:13 -0700103bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result) {
104 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset);
reed595aa052014-09-15 10:15:18 -0700105 return find_and_return(key, result);
reed04617132014-08-21 09:46:49 -0700106}
107
piotaixr42b0dfe2014-09-03 11:33:13 -0700108bool SkBitmapCache::Add(uint32_t genID, const SkIRect& subset, const SkBitmap& result) {
reed14b6aba2014-08-29 10:25:26 -0700109 SkASSERT(result.isImmutable());
reed04617132014-08-21 09:46:49 -0700110
piotaixr42b0dfe2014-09-03 11:33:13 -0700111 if (subset.isEmpty()
112 || subset.top() < 0
113 || subset.left() < 0
114 || result.width() != subset.width()
115 || result.height() != subset.height()) {
116 return false;
117 } else {
118 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1,
119 subset, result)));
120
121 return true;
122 }
123}
reed680fb9e2014-08-26 09:08:04 -0700124//////////////////////////////////////////////////////////////////////////////////////////
reed04617132014-08-21 09:46:49 -0700125
reed011f39a2014-08-28 13:35:23 -0700126struct MipMapRec : public SkResourceCache::Rec {
reed680fb9e2014-08-26 09:08:04 -0700127 MipMapRec(const SkBitmap& src, const SkMipMap* result)
128 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src))
129 , fMipMap(SkRef(result))
130 {}
131
132 virtual ~MipMapRec() {
133 fMipMap->unref();
134 }
135
136 BitmapKey fKey;
137 const SkMipMap* fMipMap;
piotaixr42b0dfe2014-09-03 11:33:13 -0700138
reed680fb9e2014-08-26 09:08:04 -0700139 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
140 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap->getSize(); }
reeddee6a8e2014-09-15 06:44:47 -0700141};
reed680fb9e2014-08-26 09:08:04 -0700142
reed595aa052014-09-15 10:15:18 -0700143
reed680fb9e2014-08-26 09:08:04 -0700144const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) {
145 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src));
reed595aa052014-09-15 10:15:18 -0700146 const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key);
147 const SkMipMap* result = NULL;
148 if (rec) {
149 result = SkRef(rec->fMipMap);
150 SkResourceCache::Unlock(rec);
reed680fb9e2014-08-26 09:08:04 -0700151 }
152 return result;
reed04617132014-08-21 09:46:49 -0700153}
154
reed680fb9e2014-08-26 09:08:04 -0700155void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) {
156 if (result) {
reed011f39a2014-08-28 13:35:23 -0700157 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result)));
reed680fb9e2014-08-26 09:08:04 -0700158 }
reed04617132014-08-21 09:46:49 -0700159}
160