OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmapCache.h" | 8 #include "SkBitmapCache.h" |
9 #include "SkScaledImageCache.h" | 9 #include "SkResourceCache.h" |
10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
11 #include "SkRect.h" | 11 #include "SkRect.h" |
12 | 12 |
13 /** | 13 /** |
14 This function finds the bounds of the bitmap *within its pixelRef*. | 14 This function finds the bounds of the bitmap *within its pixelRef*. |
15 If the bitmap lacks a pixelRef, it will return an empty rect, since | 15 If the bitmap lacks a pixelRef, it will return an empty rect, since |
16 that doesn't make sense. This may be a useful enough function that | 16 that doesn't make sense. This may be a useful enough function that |
17 it should be somewhere else (in SkBitmap?). | 17 it should be somewhere else (in SkBitmap?). |
18 */ | 18 */ |
19 static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) { | 19 static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) { |
20 if (!(bm.pixelRef())) { | 20 if (!(bm.pixelRef())) { |
21 return SkIRect::MakeEmpty(); | 21 return SkIRect::MakeEmpty(); |
22 } | 22 } |
23 SkIPoint origin = bm.pixelRefOrigin(); | 23 SkIPoint origin = bm.pixelRefOrigin(); |
24 return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height()); | 24 return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height()); |
25 } | 25 } |
26 | 26 |
27 struct BitmapKey : public SkScaledImageCache::Key { | 27 struct BitmapKey : public SkResourceCache::Key { |
28 public: | 28 public: |
29 BitmapKey(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b
ounds) | 29 BitmapKey(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b
ounds) |
30 : fGenID(genID) | 30 : fGenID(genID) |
31 , fScaleX(scaleX) | 31 , fScaleX(scaleX) |
32 , fScaleY(scaleY) | 32 , fScaleY(scaleY) |
33 , fBounds(bounds) | 33 , fBounds(bounds) |
34 { | 34 { |
35 this->init(sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(f
Bounds)); | 35 this->init(sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(f
Bounds)); |
36 } | 36 } |
37 | 37 |
38 uint32_t fGenID; | 38 uint32_t fGenID; |
39 SkScalar fScaleX; | 39 SkScalar fScaleX; |
40 SkScalar fScaleY; | 40 SkScalar fScaleY; |
41 SkIRect fBounds; | 41 SkIRect fBounds; |
42 }; | 42 }; |
43 | 43 |
44 ////////////////////////////////////////////////////////////////////////////////
////////// | 44 ////////////////////////////////////////////////////////////////////////////////
////////// |
45 | 45 |
46 struct BitmapRec : public SkScaledImageCache::Rec { | 46 struct BitmapRec : public SkResourceCache::Rec { |
47 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b
ounds, | 47 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b
ounds, |
48 const SkBitmap& result) | 48 const SkBitmap& result) |
49 : fKey(genID, scaleX, scaleY, bounds) | 49 : fKey(genID, scaleX, scaleY, bounds) |
50 , fBitmap(result) | 50 , fBitmap(result) |
51 {} | 51 {} |
52 | 52 |
53 BitmapKey fKey; | 53 BitmapKey fKey; |
54 SkBitmap fBitmap; | 54 SkBitmap fBitmap; |
55 | 55 |
56 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | 56 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
57 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap
.getSize(); } | 57 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap
.getSize(); } |
58 }; | 58 }; |
59 | 59 |
60 static bool find_and_return(const BitmapKey& key, SkBitmap* result) { | 60 static bool find_and_return(const BitmapKey& key, SkBitmap* result) { |
61 const BitmapRec* rec = (BitmapRec*)SkScaledImageCache::FindAndLock(key); | 61 const BitmapRec* rec = (BitmapRec*)SkResourceCache::FindAndLock(key); |
62 if (rec) { | 62 if (rec) { |
63 *result = rec->fBitmap; | 63 *result = rec->fBitmap; |
64 SkScaledImageCache::Unlock(rec); | 64 SkResourceCache::Unlock(rec); |
65 | 65 |
66 result->lockPixels(); | 66 result->lockPixels(); |
67 if (result->getPixels()) { | 67 if (result->getPixels()) { |
68 return true; | 68 return true; |
69 } | 69 } |
70 // todo: we should explicitly purge rec from the cache at this point, si
nce | 70 // todo: we should explicitly purge rec from the cache at this point, si
nce |
71 // it is effectively purged already (has no memory behind it) | 71 // it is effectively purged already (has no memory behind it) |
72 result->reset(); | 72 result->reset(); |
73 // fall-through to false | 73 // fall-through to false |
74 } | 74 } |
75 return false; | 75 return false; |
76 } | 76 } |
77 | 77 |
78 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc
aleY, | 78 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc
aleY, |
79 SkBitmap* result) { | 79 SkBitmap* result) { |
80 if (0 == invScaleX || 0 == invScaleY) { | 80 if (0 == invScaleX || 0 == invScaleY) { |
81 // degenerate, and the key we use for mipmaps | 81 // degenerate, and the key we use for mipmaps |
82 return false; | 82 return false; |
83 } | 83 } |
84 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b
itmap(src)); | 84 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b
itmap(src)); |
85 return find_and_return(key, result); | 85 return find_and_return(key, result); |
86 } | 86 } |
87 | 87 |
88 void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invSca
leY, | 88 void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invSca
leY, |
89 const SkBitmap& result) { | 89 const SkBitmap& result) { |
90 if (0 == invScaleX || 0 == invScaleY) { | 90 if (0 == invScaleX || 0 == invScaleY) { |
91 // degenerate, and the key we use for mipmaps | 91 // degenerate, and the key we use for mipmaps |
92 return; | 92 return; |
93 } | 93 } |
94 SkScaledImageCache::Add(SkNEW_ARGS(BitmapRec, | 94 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (src.getGenerationID(), invScaleX
, invScaleY, |
95 (src.getGenerationID(), invScaleX, invSca
leY, | 95 get_bounds_from_bitmap(src), res
ult))); |
96 get_bounds_from_bitmap(src), result))); | |
97 } | 96 } |
98 | 97 |
99 bool SkBitmapCache::Find(uint32_t genID, int width, int height, SkBitmap* result
) { | 98 bool SkBitmapCache::Find(uint32_t genID, int width, int height, SkBitmap* result
) { |
100 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height))
; | 99 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height))
; |
101 return find_and_return(key, result); | 100 return find_and_return(key, result); |
102 } | 101 } |
103 | 102 |
104 void SkBitmapCache::Add(uint32_t genID, int width, int height, const SkBitmap& r
esult) { | 103 void SkBitmapCache::Add(uint32_t genID, int width, int height, const SkBitmap& r
esult) { |
105 SkScaledImageCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1
, | 104 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1, |
106 SkIRect::MakeWH(width, height
), result))); | 105 SkIRect::MakeWH(width, height),
result))); |
107 } | 106 } |
108 | 107 |
109 ////////////////////////////////////////////////////////////////////////////////
////////// | 108 ////////////////////////////////////////////////////////////////////////////////
////////// |
110 | 109 |
111 struct MipMapRec : public SkScaledImageCache::Rec { | 110 struct MipMapRec : public SkResourceCache::Rec { |
112 MipMapRec(const SkBitmap& src, const SkMipMap* result) | 111 MipMapRec(const SkBitmap& src, const SkMipMap* result) |
113 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) | 112 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) |
114 , fMipMap(SkRef(result)) | 113 , fMipMap(SkRef(result)) |
115 {} | 114 {} |
116 | 115 |
117 virtual ~MipMapRec() { | 116 virtual ~MipMapRec() { |
118 fMipMap->unref(); | 117 fMipMap->unref(); |
119 } | 118 } |
120 | 119 |
121 BitmapKey fKey; | 120 BitmapKey fKey; |
122 const SkMipMap* fMipMap; | 121 const SkMipMap* fMipMap; |
123 | 122 |
124 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | 123 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
125 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap
->getSize(); } | 124 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap
->getSize(); } |
126 }; | 125 }; |
127 | 126 |
128 | 127 |
129 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { | 128 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { |
130 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); | 129 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); |
131 const MipMapRec* rec = (MipMapRec*)SkScaledImageCache::FindAndLock(key); | 130 const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key); |
132 const SkMipMap* result = NULL; | 131 const SkMipMap* result = NULL; |
133 if (rec) { | 132 if (rec) { |
134 result = SkRef(rec->fMipMap); | 133 result = SkRef(rec->fMipMap); |
135 SkScaledImageCache::Unlock(rec); | 134 SkResourceCache::Unlock(rec); |
136 } | 135 } |
137 return result; | 136 return result; |
138 } | 137 } |
139 | 138 |
140 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { | 139 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { |
141 if (result) { | 140 if (result) { |
142 SkScaledImageCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); | 141 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); |
143 } | 142 } |
144 } | 143 } |
145 | 144 |
OLD | NEW |