Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: src/core/SkBitmapCache.cpp

Issue 518983002: The key for SkBitmapCache can now be genID+SkIRect (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Check on Add verifying width+offset Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkBitmapCache.h ('k') | tests/SkResourceCacheTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 bool SkBitmapCache::Add(uint32_t genID, int width, int height, const SkBitmap& r esult) {
108 return 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 bool 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 if (subset.isEmpty()
120 || subset.top() < 0
121 || subset.left() < 0
122 || result.width() != subset.width()
123 || result.height() != subset.height()) {
124 return false;
125 } else {
126 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar 1,
127 subset, result)));
128
129 return true;
130 }
112 } 131 }
113
114 //////////////////////////////////////////////////////////////////////////////// ////////// 132 //////////////////////////////////////////////////////////////////////////////// //////////
115 133
116 struct MipMapRec : public SkResourceCache::Rec { 134 struct MipMapRec : public SkResourceCache::Rec {
117 MipMapRec(const SkBitmap& src, const SkMipMap* result) 135 MipMapRec(const SkBitmap& src, const SkMipMap* result)
118 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) 136 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src))
119 , fMipMap(SkRef(result)) 137 , fMipMap(SkRef(result))
120 {} 138 {}
121 139
122 virtual ~MipMapRec() { 140 virtual ~MipMapRec() {
123 fMipMap->unref(); 141 fMipMap->unref();
124 } 142 }
125 143
126 BitmapKey fKey; 144 BitmapKey fKey;
127 const SkMipMap* fMipMap; 145 const SkMipMap* fMipMap;
128 146
129 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } 147 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
130 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap ->getSize(); } 148 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap ->getSize(); }
131 }; 149 };
132 150
133 151
134 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { 152 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) {
135 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); 153 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src));
136 const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key); 154 const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key);
137 const SkMipMap* result = NULL; 155 const SkMipMap* result = NULL;
138 if (rec) { 156 if (rec) {
139 result = SkRef(rec->fMipMap); 157 result = SkRef(rec->fMipMap);
140 SkResourceCache::Unlock(rec); 158 SkResourceCache::Unlock(rec);
141 } 159 }
142 return result; 160 return result;
143 } 161 }
144 162
145 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { 163 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) {
146 if (result) { 164 if (result) {
147 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); 165 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result)));
148 } 166 }
149 } 167 }
150 168
OLDNEW
« no previous file with comments | « src/core/SkBitmapCache.h ('k') | tests/SkResourceCacheTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698