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" |
| 10 #include "SkMipMap.h" |
9 #include "SkRect.h" | 11 #include "SkRect.h" |
10 | 12 |
11 /** | 13 /** |
12 This function finds the bounds of the bitmap *within its pixelRef*. | 14 This function finds the bounds of the bitmap *within its pixelRef*. |
13 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 |
14 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 |
15 it should be somewhere else (in SkBitmap?). | 17 it should be somewhere else (in SkBitmap?). |
16 */ | 18 */ |
17 static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) { | 19 static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) { |
18 if (!(bm.pixelRef())) { | 20 if (!(bm.pixelRef())) { |
(...skipping 15 matching lines...) Expand all Loading... |
34 } | 36 } |
35 | 37 |
36 uint32_t fGenID; | 38 uint32_t fGenID; |
37 SkScalar fScaleX; | 39 SkScalar fScaleX; |
38 SkScalar fScaleY; | 40 SkScalar fScaleY; |
39 SkIRect fBounds; | 41 SkIRect fBounds; |
40 }; | 42 }; |
41 | 43 |
42 ////////////////////////////////////////////////////////////////////////////////
////////// | 44 ////////////////////////////////////////////////////////////////////////////////
////////// |
43 | 45 |
44 SkScaledImageCache::ID* SkBitmapCache::FindAndLock(const SkBitmap& src, | 46 struct BitmapRec : public SkScaledImageCache::Rec { |
45 SkScalar invScaleX, SkScalar
invScaleY, | 47 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b
ounds, |
46 SkBitmap* result) { | 48 const SkBitmap& result) |
| 49 : fKey(genID, scaleX, scaleY, bounds) |
| 50 , fBitmap(result) |
| 51 {} |
| 52 |
| 53 BitmapKey fKey; |
| 54 SkBitmap fBitmap; |
| 55 |
| 56 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
| 57 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap
.getSize(); } |
| 58 }; |
| 59 |
| 60 static bool find_and_return(const BitmapKey& key, SkBitmap* result) { |
| 61 const BitmapRec* rec = (BitmapRec*)SkScaledImageCache::FindAndLock(key); |
| 62 if (rec) { |
| 63 *result = rec->fBitmap; |
| 64 SkScaledImageCache::Unlock(rec); |
| 65 |
| 66 result->lockPixels(); |
| 67 if (result->getPixels()) { |
| 68 return true; |
| 69 } |
| 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) |
| 72 result->reset(); |
| 73 // fall-through to false |
| 74 } |
| 75 return false; |
| 76 } |
| 77 |
| 78 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc
aleY, |
| 79 SkBitmap* result) { |
47 if (0 == invScaleX || 0 == invScaleY) { | 80 if (0 == invScaleX || 0 == invScaleY) { |
48 // degenerate, and the key we use for mipmaps | 81 // degenerate, and the key we use for mipmaps |
49 return NULL; | 82 return false; |
50 } | 83 } |
51 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)); |
52 return SkScaledImageCache::FindAndLock(key, result); | 85 return find_and_return(key, result); |
53 } | 86 } |
54 | 87 |
55 SkScaledImageCache::ID* SkBitmapCache::AddAndLock(const SkBitmap& src, | 88 void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invSca
leY, |
56 SkScalar invScaleX, SkScalar i
nvScaleY, | 89 const SkBitmap& result) { |
57 const SkBitmap& result) { | |
58 if (0 == invScaleX || 0 == invScaleY) { | 90 if (0 == invScaleX || 0 == invScaleY) { |
59 // degenerate, and the key we use for mipmaps | 91 // degenerate, and the key we use for mipmaps |
60 return NULL; | 92 return; |
61 } | 93 } |
62 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b
itmap(src)); | 94 SkScaledImageCache::Add(SkNEW_ARGS(BitmapRec, |
63 return SkScaledImageCache::AddAndLock(key, result); | 95 (src.getGenerationID(), invScaleX, invSca
leY, |
| 96 get_bounds_from_bitmap(src), result))); |
64 } | 97 } |
65 | 98 |
66 //// | 99 bool SkBitmapCache::Find(uint32_t genID, int width, int height, SkBitmap* result
) { |
67 | |
68 SkScaledImageCache::ID* SkBitmapCache::FindAndLock(uint32_t genID, int width, in
t height, | |
69 SkBitmap* result) { | |
70 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height))
; | 100 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height))
; |
71 return SkScaledImageCache::FindAndLock(key, result); | 101 return find_and_return(key, result); |
72 } | 102 } |
73 | 103 |
74 SkScaledImageCache::ID* SkBitmapCache::AddAndLock(uint32_t genID, int width, int
height, | 104 void SkBitmapCache::Add(uint32_t genID, int width, int height, const SkBitmap& r
esult) { |
75 const SkBitmap& result) { | 105 SkScaledImageCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1
, |
76 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height))
; | 106 SkIRect::MakeWH(width, height
), result))); |
77 return SkScaledImageCache::AddAndLock(key, result); | |
78 } | 107 } |
79 | 108 |
80 //// | 109 ////////////////////////////////////////////////////////////////////////////////
////////// |
81 | 110 |
82 SkScaledImageCache::ID* SkMipMapCache::FindAndLock(const SkBitmap& src, const Sk
MipMap** result) { | 111 struct MipMapRec : public SkScaledImageCache::Rec { |
83 BitmapKey key(src.getGenerationID(), SK_Scalar1, SK_Scalar1, get_bounds_from
_bitmap(src)); | 112 MipMapRec(const SkBitmap& src, const SkMipMap* result) |
84 return SkScaledImageCache::FindAndLock(key, result); | 113 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) |
| 114 , fMipMap(SkRef(result)) |
| 115 {} |
| 116 |
| 117 virtual ~MipMapRec() { |
| 118 fMipMap->unref(); |
| 119 } |
| 120 |
| 121 BitmapKey fKey; |
| 122 const SkMipMap* fMipMap; |
| 123 |
| 124 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
| 125 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap
->getSize(); } |
| 126 }; |
| 127 |
| 128 |
| 129 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { |
| 130 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); |
| 131 const MipMapRec* rec = (MipMapRec*)SkScaledImageCache::FindAndLock(key); |
| 132 const SkMipMap* result = NULL; |
| 133 if (rec) { |
| 134 result = SkRef(rec->fMipMap); |
| 135 SkScaledImageCache::Unlock(rec); |
| 136 } |
| 137 return result; |
85 } | 138 } |
86 | 139 |
87 SkScaledImageCache::ID* SkMipMapCache::AddAndLock(const SkBitmap& src, const SkM
ipMap* result) { | 140 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { |
88 BitmapKey key(src.getGenerationID(), SK_Scalar1, SK_Scalar1, get_bounds_from
_bitmap(src)); | 141 if (result) { |
89 return SkScaledImageCache::AddAndLock(key, result); | 142 SkScaledImageCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); |
| 143 } |
90 } | 144 } |
91 | 145 |
OLD | NEW |