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** result = (const SkMipMap**)contextMip; |
144 | 143 |
145 *result = SkRef(rec.fMipMap); | 144 *result = SkRef(rec.fMipMap); |
146 // mipmaps don't use the custom allocator yet, so we don't need to check
pixels | 145 // mipmaps don't use the custom allocator yet, so we don't need to check
pixels |
147 return true; | 146 return true; |
148 } | 147 } |
| 148 |
| 149 private: |
| 150 BitmapKey fKey; |
| 151 const SkMipMap* fMipMap; |
149 }; | 152 }; |
150 | 153 |
151 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { | 154 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src, SkResourceCache*
localCache) { |
152 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); | 155 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); |
153 const SkMipMap* result; | 156 const SkMipMap* result; |
154 if (!SkResourceCache::Find(key, MipMapRec::Visitor, &result)) { | 157 |
| 158 if (!CHECK_LOCAL(localCache, find, Find, key, MipMapRec::Visitor, &result))
{ |
155 result = NULL; | 159 result = NULL; |
156 } | 160 } |
157 return result; | 161 return result; |
158 } | 162 } |
159 | 163 |
160 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { | 164 static SkResourceCache::DiscardableFactory get_fact(SkResourceCache* localCache)
{ |
161 if (result) { | 165 return localCache ? localCache->GetDiscardableFactory() |
162 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); | 166 : SkResourceCache::GetDiscardableFactory(); |
163 } | |
164 } | 167 } |
165 | 168 |
| 169 const SkMipMap* SkMipMapCache::AddAndRef(const SkBitmap& src, SkResourceCache* l
ocalCache) { |
| 170 SkMipMap* mipmap = SkMipMap::Build(src, get_fact(localCache)); |
| 171 if (mipmap) { |
| 172 MipMapRec* rec = SkNEW_ARGS(MipMapRec, (src, mipmap)); |
| 173 CHECK_LOCAL(localCache, add, Add, rec); |
| 174 } |
| 175 return mipmap; |
| 176 } |
| 177 |
OLD | NEW |