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

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: Add checks on SkBitmapCache::Add 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);
Rémi Piotaix 2014/09/02 19:20:58 Because width and height must be the ones of 'resu
109 }
110
111 bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result ) {
112 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset);
Rémi Piotaix 2014/09/02 19:20:58 Do we need to add the check on the Find methods? (
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 if (subset.isEmpty()
111 SkIRect::MakeWH(width, height), result))); 119 || result.width() != subset.width()
120 || result.height() != subset.height()) {
121 return false;
122 } else {
123 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar 1,
124 subset, result)));
125
126 return true;
127 }
112 } 128 }
113
114 //////////////////////////////////////////////////////////////////////////////// ////////// 129 //////////////////////////////////////////////////////////////////////////////// //////////
115 130
116 struct MipMapRec : public SkResourceCache::Rec { 131 struct MipMapRec : public SkResourceCache::Rec {
117 MipMapRec(const SkBitmap& src, const SkMipMap* result) 132 MipMapRec(const SkBitmap& src, const SkMipMap* result)
118 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) 133 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src))
119 , fMipMap(SkRef(result)) 134 , fMipMap(SkRef(result))
120 {} 135 {}
121 136
122 virtual ~MipMapRec() { 137 virtual ~MipMapRec() {
123 fMipMap->unref(); 138 fMipMap->unref();
124 } 139 }
125 140
126 BitmapKey fKey; 141 BitmapKey fKey;
127 const SkMipMap* fMipMap; 142 const SkMipMap* fMipMap;
128 143
129 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } 144 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
130 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap ->getSize(); } 145 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap ->getSize(); }
131 }; 146 };
132 147
133 148
134 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { 149 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) {
135 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); 150 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src));
136 const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key); 151 const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key);
137 const SkMipMap* result = NULL; 152 const SkMipMap* result = NULL;
138 if (rec) { 153 if (rec) {
139 result = SkRef(rec->fMipMap); 154 result = SkRef(rec->fMipMap);
140 SkResourceCache::Unlock(rec); 155 SkResourceCache::Unlock(rec);
141 } 156 }
142 return result; 157 return result;
143 } 158 }
144 159
145 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { 160 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) {
146 if (result) { 161 if (result) {
147 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); 162 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result)));
148 } 163 }
149 } 164 }
150 165
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