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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec); | 64 const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec); |
65 SkBitmap* result = (SkBitmap*)contextBitmap; | 65 SkBitmap* result = (SkBitmap*)contextBitmap; |
66 | 66 |
67 *result = rec.fBitmap; | 67 *result = rec.fBitmap; |
68 result->lockPixels(); | 68 result->lockPixels(); |
69 return SkToBool(result->getPixels()); | 69 return SkToBool(result->getPixels()); |
70 } | 70 } |
71 }; | 71 }; |
72 | 72 |
73 #define CHECK_LOCAL(localCache, localName, globalName, ...) \ | 73 #define CHECK_LOCAL(localCache, localName, globalName, ...) \ |
74 (localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalN
ame(__VA_ARGS__) | 74 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::global
Name(__VA_ARGS__)) |
75 | 75 |
76 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc
aleY, SkBitmap* result, | 76 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc
aleY, SkBitmap* result, |
77 SkResourceCache* localCache) { | 77 SkResourceCache* localCache) { |
78 if (0 == invScaleX || 0 == invScaleY) { | 78 if (0 == invScaleX || 0 == invScaleY) { |
79 // degenerate, and the key we use for mipmaps | 79 // degenerate, and the key we use for mipmaps |
80 return false; | 80 return false; |
81 } | 81 } |
82 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)); |
83 | 83 |
84 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Visitor, result); | 84 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Visitor, result); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 118 |
119 CHECK_LOCAL(localCache, add, Add, rec); | 119 CHECK_LOCAL(localCache, add, Add, rec); |
120 return true; | 120 return true; |
121 } | 121 } |
122 } | 122 } |
123 ////////////////////////////////////////////////////////////////////////////////
////////// | 123 ////////////////////////////////////////////////////////////////////////////////
////////// |
124 | 124 |
125 struct MipMapRec : public SkResourceCache::Rec { | 125 struct MipMapRec : public SkResourceCache::Rec { |
126 MipMapRec(const SkBitmap& src, const SkMipMap* result) | 126 MipMapRec(const SkBitmap& src, const SkMipMap* result) |
127 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) | 127 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) |
128 , fMipMap(SkRef(result)) | 128 , fMipMap(result) |
129 {} | 129 { |
| 130 fMipMap->attachToCacheAndRef(); |
| 131 } |
130 | 132 |
131 virtual ~MipMapRec() { | 133 virtual ~MipMapRec() { |
132 fMipMap->unref(); | 134 fMipMap->detachFromCacheAndUnref(); |
133 } | 135 } |
134 | 136 |
135 BitmapKey fKey; | |
136 const SkMipMap* fMipMap; | |
137 | |
138 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | 137 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
139 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap
->getSize(); } | 138 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap
->size(); } |
140 | 139 |
141 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextMip) { | 140 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextMip) { |
142 const MipMapRec& rec = static_cast<const MipMapRec&>(baseRec); | 141 const MipMapRec& rec = static_cast<const MipMapRec&>(baseRec); |
143 const SkMipMap** result = (const SkMipMap**)contextMip; | 142 const SkMipMap* mm = SkRef(rec.fMipMap); |
144 | 143 // the call to ref() above triggers a "lock" in the case of discardable
memory, |
145 *result = SkRef(rec.fMipMap); | 144 // which means we can now check for null (in case the lock failed). |
146 // mipmaps don't use the custom allocator yet, so we don't need to check
pixels | 145 if (NULL == mm->data()) { |
| 146 mm->unref(); // balance our call to ref() |
| 147 return false; |
| 148 } |
| 149 // the call must call unref() when they are done. |
| 150 *(const SkMipMap**)contextMip = mm; |
147 return true; | 151 return true; |
148 } | 152 } |
| 153 |
| 154 private: |
| 155 BitmapKey fKey; |
| 156 const SkMipMap* fMipMap; |
149 }; | 157 }; |
150 | 158 |
151 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { | 159 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src, SkResourceCache*
localCache) { |
152 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); | 160 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); |
153 const SkMipMap* result; | 161 const SkMipMap* result; |
154 if (!SkResourceCache::Find(key, MipMapRec::Visitor, &result)) { | 162 |
| 163 if (!CHECK_LOCAL(localCache, find, Find, key, MipMapRec::Visitor, &result))
{ |
155 result = NULL; | 164 result = NULL; |
156 } | 165 } |
157 return result; | 166 return result; |
158 } | 167 } |
159 | 168 |
160 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { | 169 static SkResourceCache::DiscardableFactory get_fact(SkResourceCache* localCache)
{ |
161 if (result) { | 170 return localCache ? localCache->GetDiscardableFactory() |
162 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); | 171 : SkResourceCache::GetDiscardableFactory(); |
163 } | |
164 } | 172 } |
165 | 173 |
| 174 const SkMipMap* SkMipMapCache::AddAndRef(const SkBitmap& src, SkResourceCache* l
ocalCache) { |
| 175 SkMipMap* mipmap = SkMipMap::Build(src, get_fact(localCache)); |
| 176 if (mipmap) { |
| 177 MipMapRec* rec = SkNEW_ARGS(MipMapRec, (src, mipmap)); |
| 178 CHECK_LOCAL(localCache, add, Add, rec); |
| 179 } |
| 180 return mipmap; |
| 181 } |
| 182 |
OLD | NEW |