Chromium Code Reviews| 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 "SkResourceCache.h" | 9 #include "SkResourceCache.h" |
| 10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 | 49 |
| 50 struct BitmapRec : public SkResourceCache::Rec { | 50 struct BitmapRec : public SkResourceCache::Rec { |
| 51 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b ounds, | 51 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b ounds, |
| 52 const SkBitmap& result) | 52 const SkBitmap& result) |
| 53 : fKey(genID, scaleX, scaleY, bounds) | 53 : fKey(genID, scaleX, scaleY, bounds) |
| 54 , fBitmap(result) | 54 , fBitmap(result) |
| 55 {} | 55 {} |
| 56 | 56 |
| 57 BitmapKey fKey; | 57 BitmapKey fKey; |
| 58 SkBitmap fBitmap; | 58 SkBitmap fBitmap; |
| 59 | 59 |
| 60 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | 60 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
| 61 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap .getSize(); } | 61 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap .getSize(); } |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 static bool find_and_return(const BitmapKey& key, SkBitmap* result) { | 64 static bool find_and_return(const BitmapKey& key, SkBitmap* result) { |
| 65 const BitmapRec* rec = (BitmapRec*)SkResourceCache::FindAndLock(key); | 65 const BitmapRec* rec = (BitmapRec*)SkResourceCache::FindAndLock(key); |
| 66 if (rec) { | 66 if (rec) { |
| 67 *result = rec->fBitmap; | 67 *result = rec->fBitmap; |
| 68 SkResourceCache::Unlock(rec); | 68 SkResourceCache::Unlock(rec); |
| 69 | 69 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 94 if (0 == invScaleX || 0 == invScaleY) { | 94 if (0 == invScaleX || 0 == invScaleY) { |
| 95 // degenerate, and the key we use for mipmaps | 95 // degenerate, and the key we use for mipmaps |
| 96 return; | 96 return; |
| 97 } | 97 } |
| 98 SkASSERT(result.isImmutable()); | 98 SkASSERT(result.isImmutable()); |
| 99 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (src.getGenerationID(), invScaleX , invScaleY, | 99 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (src.getGenerationID(), invScaleX , invScaleY, |
| 100 get_bounds_from_bitmap(src), res ult))); | 100 get_bounds_from_bitmap(src), res ult))); |
| 101 } | 101 } |
| 102 | 102 |
| 103 bool SkBitmapCache::Find(uint32_t genID, int width, int height, SkBitmap* result ) { | 103 bool SkBitmapCache::Find(uint32_t genID, int width, int height, SkBitmap* result ) { |
| 104 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height)) ; | 104 return Find(genID, SkIRect::MakeWH(width, height), result); |
| 105 } | |
| 106 | |
| 107 void SkBitmapCache::Add(uint32_t genID, int width, int height, const SkBitmap& r esult) { | |
| 108 Add(genID, SkIRect::MakeWH(width, height), result); | |
| 109 } | |
| 110 | |
| 111 bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result ) { | |
| 112 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset); | |
| 105 return find_and_return(key, result); | 113 return find_and_return(key, result); |
| 106 } | 114 } |
| 107 | 115 |
| 108 void SkBitmapCache::Add(uint32_t genID, int width, int height, const SkBitmap& r esult) { | 116 void SkBitmapCache::Add(uint32_t genID, const SkIRect& subset, const SkBitmap& r esult) { |
| 109 SkASSERT(result.isImmutable()); | 117 SkASSERT(result.isImmutable()); |
| 110 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1, | 118 |
| 111 SkIRect::MakeWH(width, height), result))); | 119 // We need to take only the width and height of 'subset' to compute the inte rsection |
|
reed1
2014/09/02 20:38:45
Actually the subset rect is very interesting becau
Rémi Piotaix
2014/09/02 20:48:44
Because the given subset is relative to the origin
| |
| 120 SkIRect localSubset = SkIRect::MakeWH(subset.width(), subset.height()); | |
| 121 SkIRect bitmapBounds = SkIRect::MakeWH(result.width(), result.height()); | |
| 122 | |
| 123 if (localSubset.intersect(bitmapBounds)) { | |
| 124 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar 1, | |
| 125 localSubset, result))); | |
| 126 } | |
| 112 } | 127 } |
| 113 | |
| 114 //////////////////////////////////////////////////////////////////////////////// ////////// | 128 //////////////////////////////////////////////////////////////////////////////// ////////// |
| 115 | 129 |
| 116 struct MipMapRec : public SkResourceCache::Rec { | 130 struct MipMapRec : public SkResourceCache::Rec { |
| 117 MipMapRec(const SkBitmap& src, const SkMipMap* result) | 131 MipMapRec(const SkBitmap& src, const SkMipMap* result) |
| 118 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) | 132 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) |
| 119 , fMipMap(SkRef(result)) | 133 , fMipMap(SkRef(result)) |
| 120 {} | 134 {} |
| 121 | 135 |
| 122 virtual ~MipMapRec() { | 136 virtual ~MipMapRec() { |
| 123 fMipMap->unref(); | 137 fMipMap->unref(); |
| 124 } | 138 } |
| 125 | 139 |
| 126 BitmapKey fKey; | 140 BitmapKey fKey; |
| 127 const SkMipMap* fMipMap; | 141 const SkMipMap* fMipMap; |
| 128 | 142 |
| 129 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | 143 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
| 130 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap ->getSize(); } | 144 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap ->getSize(); } |
| 131 }; | 145 }; |
| 132 | 146 |
| 133 | 147 |
| 134 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { | 148 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { |
| 135 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); | 149 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); |
| 136 const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key); | 150 const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key); |
| 137 const SkMipMap* result = NULL; | 151 const SkMipMap* result = NULL; |
| 138 if (rec) { | 152 if (rec) { |
| 139 result = SkRef(rec->fMipMap); | 153 result = SkRef(rec->fMipMap); |
| 140 SkResourceCache::Unlock(rec); | 154 SkResourceCache::Unlock(rec); |
| 141 } | 155 } |
| 142 return result; | 156 return result; |
| 143 } | 157 } |
| 144 | 158 |
| 145 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { | 159 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { |
| 146 if (result) { | 160 if (result) { |
| 147 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); | 161 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); |
| 148 } | 162 } |
| 149 } | 163 } |
| 150 | 164 |
| OLD | NEW |