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 result->reset(); |
| 71 // fall-through to false |
| 72 } |
| 73 return false; |
| 74 } |
| 75 |
| 76 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc
aleY, |
| 77 SkBitmap* result) { |
47 if (0 == invScaleX || 0 == invScaleY) { | 78 if (0 == invScaleX || 0 == invScaleY) { |
48 // degenerate, and the key we use for mipmaps | 79 // degenerate, and the key we use for mipmaps |
49 return NULL; | 80 return false; |
50 } | 81 } |
51 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b
itmap(src)); | 82 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b
itmap(src)); |
52 return SkScaledImageCache::FindAndLock(key, result); | 83 return find_and_return(key, result); |
53 } | 84 } |
54 | 85 |
55 SkScaledImageCache::ID* SkBitmapCache::AddAndLock(const SkBitmap& src, | 86 void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invSca
leY, |
56 SkScalar invScaleX, SkScalar i
nvScaleY, | 87 const SkBitmap& result) { |
57 const SkBitmap& result) { | |
58 if (0 == invScaleX || 0 == invScaleY) { | 88 if (0 == invScaleX || 0 == invScaleY) { |
59 // degenerate, and the key we use for mipmaps | 89 // degenerate, and the key we use for mipmaps |
60 return NULL; | 90 return; |
61 } | 91 } |
62 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b
itmap(src)); | 92 SkScaledImageCache::Add(SkNEW_ARGS(BitmapRec, |
63 return SkScaledImageCache::AddAndLock(key, result); | 93 (src.getGenerationID(), invScaleX, invSca
leY, |
| 94 get_bounds_from_bitmap(src), result))); |
64 } | 95 } |
65 | 96 |
66 //// | 97 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))
; | 98 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height))
; |
71 return SkScaledImageCache::FindAndLock(key, result); | 99 return find_and_return(key, result); |
72 } | 100 } |
73 | 101 |
74 SkScaledImageCache::ID* SkBitmapCache::AddAndLock(uint32_t genID, int width, int
height, | 102 void SkBitmapCache::Add(uint32_t genID, int width, int height, const SkBitmap& r
esult) { |
75 const SkBitmap& result) { | 103 SkScaledImageCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1
, |
76 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height))
; | 104 SkIRect::MakeWH(width, height
), result))); |
77 return SkScaledImageCache::AddAndLock(key, result); | |
78 } | 105 } |
79 | 106 |
80 //// | 107 ////////////////////////////////////////////////////////////////////////////////
////////// |
81 | 108 |
82 SkScaledImageCache::ID* SkMipMapCache::FindAndLock(const SkBitmap& src, const Sk
MipMap** result) { | 109 struct MipMapRec : public SkScaledImageCache::Rec { |
83 BitmapKey key(src.getGenerationID(), SK_Scalar1, SK_Scalar1, get_bounds_from
_bitmap(src)); | 110 MipMapRec(const SkBitmap& src, const SkMipMap* result) |
84 return SkScaledImageCache::FindAndLock(key, result); | 111 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) |
| 112 , fMipMap(SkRef(result)) |
| 113 {} |
| 114 |
| 115 virtual ~MipMapRec() { |
| 116 fMipMap->unref(); |
| 117 } |
| 118 |
| 119 BitmapKey fKey; |
| 120 const SkMipMap* fMipMap; |
| 121 |
| 122 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
| 123 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap
->getSize(); } |
| 124 }; |
| 125 |
| 126 |
| 127 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { |
| 128 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); |
| 129 const MipMapRec* rec = (MipMapRec*)SkScaledImageCache::FindAndLock(key); |
| 130 const SkMipMap* result = NULL; |
| 131 if (rec) { |
| 132 result = SkRef(rec->fMipMap); |
| 133 SkScaledImageCache::Unlock(rec); |
| 134 } |
| 135 return result; |
85 } | 136 } |
86 | 137 |
87 SkScaledImageCache::ID* SkMipMapCache::AddAndLock(const SkBitmap& src, const SkM
ipMap* result) { | 138 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { |
88 BitmapKey key(src.getGenerationID(), SK_Scalar1, SK_Scalar1, get_bounds_from
_bitmap(src)); | 139 if (result) { |
89 return SkScaledImageCache::AddAndLock(key, result); | 140 SkScaledImageCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); |
| 141 } |
90 } | 142 } |
91 | 143 |
OLD | NEW |