OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2012 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "Sk64.h" | |
9 #include "SkLazyPixelRef.h" | |
10 #include "SkColorTable.h" | |
11 #include "SkData.h" | |
12 #include "SkImageCache.h" | |
13 #include "SkImagePriv.h" | |
14 #include "SkScaledImageCache.h" | |
15 | |
16 #if LAZY_CACHE_STATS | |
17 #include "SkThread.h" | |
18 | |
19 int32_t SkLazyPixelRef::gCacheHits; | |
20 int32_t SkLazyPixelRef::gCacheMisses; | |
21 #endif | |
22 | |
23 SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S
kImageCache* cache) | |
24 // Pass NULL for the Mutex so that the default (ring buffer) will be used. | |
25 : INHERITED(NULL) | |
26 , fErrorInDecoding(false) | |
27 , fDecodeProc(proc) | |
28 , fImageCache(cache) | |
29 , fRowBytes(0) { | |
30 SkASSERT(fDecodeProc != NULL); | |
31 if (NULL == data) { | |
32 fData = SkData::NewEmpty(); | |
33 fErrorInDecoding = true; | |
34 } else { | |
35 fData = data; | |
36 fData->ref(); | |
37 fErrorInDecoding = data->size() == 0; | |
38 } | |
39 if (fImageCache != NULL) { | |
40 fImageCache->ref(); | |
41 fCacheId = SkImageCache::UNINITIALIZED_ID; | |
42 } else { | |
43 fScaledCacheId = NULL; | |
44 } | |
45 | |
46 // mark as uninitialized -- all fields are -1 | |
47 memset(&fLazilyCachedInfo, 0xFF, sizeof(fLazilyCachedInfo)); | |
48 | |
49 // Since this pixel ref bases its data on encoded data, it should never chan
ge. | |
50 this->setImmutable(); | |
51 } | |
52 | |
53 SkLazyPixelRef::~SkLazyPixelRef() { | |
54 SkASSERT(fData != NULL); | |
55 fData->unref(); | |
56 if (NULL == fImageCache) { | |
57 if (fScaledCacheId != NULL) { | |
58 SkScaledImageCache::Unlock(fScaledCacheId); | |
59 // TODO(halcanary): SkScaledImageCache needs a | |
60 // throwAwayCache(id) method. | |
61 } | |
62 return; | |
63 } | |
64 SkASSERT(fImageCache); | |
65 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { | |
66 fImageCache->throwAwayCache(fCacheId); | |
67 } | |
68 fImageCache->unref(); | |
69 } | |
70 | |
71 static size_t ComputeMinRowBytesAndSize(const SkImageInfo& info, size_t* rowByte
s) { | |
72 *rowBytes = SkImageMinRowBytes(info); | |
73 | |
74 Sk64 safeSize; | |
75 safeSize.setZero(); | |
76 if (info.fHeight > 0) { | |
77 safeSize.setMul(info.fHeight, SkToS32(*rowBytes)); | |
78 } | |
79 SkASSERT(!safeSize.isNeg()); | |
80 return safeSize.is32() ? safeSize.get32() : 0; | |
81 } | |
82 | |
83 const SkImageInfo* SkLazyPixelRef::getCachedInfo() { | |
84 if (fLazilyCachedInfo.fWidth < 0) { | |
85 SkImageInfo info; | |
86 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL
L); | |
87 if (fErrorInDecoding) { | |
88 return NULL; | |
89 } | |
90 fLazilyCachedInfo = info; | |
91 } | |
92 return &fLazilyCachedInfo; | |
93 } | |
94 | |
95 /** | |
96 Returns bitmap->getPixels() on success; NULL on failure */ | |
97 static void* decode_into_bitmap(SkImageInfo* info, | |
98 SkBitmapFactory::DecodeProc decodeProc, | |
99 size_t* rowBytes, | |
100 SkData* data, | |
101 SkBitmap* bm) { | |
102 SkASSERT(info && decodeProc && rowBytes && data && bm); | |
103 if (!(bm->setConfig(SkImageInfoToBitmapConfig(*info), info->fWidth, | |
104 info->fHeight, *rowBytes, info->fAlphaType) | |
105 && bm->allocPixels(NULL, NULL))) { | |
106 // Use the default allocator. It may be necessary for the | |
107 // SkLazyPixelRef to have a allocator field which is passed | |
108 // into allocPixels(). | |
109 return NULL; | |
110 } | |
111 SkBitmapFactory::Target target; | |
112 target.fAddr = bm->getPixels(); | |
113 target.fRowBytes = bm->rowBytes(); | |
114 *rowBytes = target.fRowBytes; | |
115 if (!decodeProc(data->data(), data->size(), info, &target)) { | |
116 return NULL; | |
117 } | |
118 return target.fAddr; | |
119 } | |
120 | |
121 void* SkLazyPixelRef::lockScaledImageCachePixels() { | |
122 SkASSERT(!fErrorInDecoding); | |
123 SkASSERT(NULL == fImageCache); | |
124 SkBitmap bitmap; | |
125 const SkImageInfo* info = this->getCachedInfo(); | |
126 if (info == NULL) { | |
127 return NULL; | |
128 } | |
129 // If this is the first time though, this is guaranteed to fail. | |
130 // Maybe we should have a flag that says "don't even bother looking" | |
131 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), | |
132 info->fWidth, | |
133 info->fHeight, | |
134 &bitmap); | |
135 if (fScaledCacheId != NULL) { | |
136 SkAutoLockPixels autoLockPixels(bitmap); | |
137 void* pixels = bitmap.getPixels(); | |
138 SkASSERT(NULL != pixels); | |
139 // At this point, the autoLockPixels will unlockPixels() | |
140 // to remove bitmap's lock on the pixels. We will then | |
141 // destroy bitmap. The *only* guarantee that this pointer | |
142 // remains valid is the guarantee made by | |
143 // SkScaledImageCache that it will not destroy the *other* | |
144 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a | |
145 // reference to the concrete PixelRef while this record is | |
146 // locked. | |
147 return pixels; | |
148 } else { | |
149 // Cache has been purged, must re-decode. | |
150 void* pixels = decode_into_bitmap(const_cast<SkImageInfo*>(info), | |
151 fDecodeProc, &fRowBytes, fData, | |
152 &bitmap); | |
153 if (NULL == pixels) { | |
154 fErrorInDecoding = true; | |
155 return NULL; | |
156 } | |
157 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), | |
158 info->fWidth, | |
159 info->fHeight, | |
160 bitmap); | |
161 SkASSERT(fScaledCacheId != NULL); | |
162 return pixels; | |
163 } | |
164 } | |
165 | |
166 void* SkLazyPixelRef::onLockPixels(SkColorTable**) { | |
167 if (fErrorInDecoding) { | |
168 return NULL; | |
169 } | |
170 if (NULL == fImageCache) { | |
171 return this->lockScaledImageCachePixels(); | |
172 } else { | |
173 return this->lockImageCachePixels(); | |
174 } | |
175 } | |
176 | |
177 void* SkLazyPixelRef::lockImageCachePixels() { | |
178 SkASSERT(fImageCache != NULL); | |
179 SkASSERT(!fErrorInDecoding); | |
180 SkBitmapFactory::Target target; | |
181 // Check to see if the pixels still exist in the cache. | |
182 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { | |
183 target.fAddr = NULL; | |
184 } else { | |
185 SkImageCache::DataStatus status; | |
186 target.fAddr = fImageCache->pinCache(fCacheId, &status); | |
187 if (target.fAddr == NULL) { | |
188 fCacheId = SkImageCache::UNINITIALIZED_ID; | |
189 } else { | |
190 if (SkImageCache::kRetained_DataStatus == status) { | |
191 #if LAZY_CACHE_STATS | |
192 sk_atomic_inc(&gCacheHits); | |
193 #endif | |
194 return target.fAddr; | |
195 } | |
196 SkASSERT(SkImageCache::kUninitialized_DataStatus == status); | |
197 } | |
198 // Cache miss. Either pinCache returned NULL or it returned a memory add
ress without the old | |
199 // data | |
200 #if LAZY_CACHE_STATS | |
201 sk_atomic_inc(&gCacheMisses); | |
202 #endif | |
203 } | |
204 | |
205 SkASSERT(fData != NULL && fData->size() > 0); | |
206 if (NULL == target.fAddr) { | |
207 const SkImageInfo* info = this->getCachedInfo(); | |
208 if (NULL == info) { | |
209 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId); | |
210 return NULL; | |
211 } | |
212 size_t bytes = ComputeMinRowBytesAndSize(*info, &target.fRowBytes); | |
213 target.fAddr = fImageCache->allocAndPinCache(bytes, &fCacheId); | |
214 if (NULL == target.fAddr) { | |
215 // Space could not be allocated. | |
216 // Just like the last assert, fCacheId must be UNINITIALIZED_ID. | |
217 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId); | |
218 return NULL; | |
219 } | |
220 } else { | |
221 // pinCache returned purged memory to which target.fAddr already points.
Set | |
222 // target.fRowBytes properly. | |
223 target.fRowBytes = fRowBytes; | |
224 // Assume that the size is correct, since it was determined by this same
function | |
225 // previously. | |
226 } | |
227 SkASSERT(target.fAddr != NULL); | |
228 SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId); | |
229 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), NULL, &target)
; | |
230 if (fErrorInDecoding) { | |
231 fImageCache->throwAwayCache(fCacheId); | |
232 fCacheId = SkImageCache::UNINITIALIZED_ID; | |
233 return NULL; | |
234 } | |
235 // Upon success, store fRowBytes so it can be used in case pinCache later re
turns purged memory. | |
236 fRowBytes = target.fRowBytes; | |
237 return target.fAddr; | |
238 } | |
239 | |
240 void SkLazyPixelRef::onUnlockPixels() { | |
241 if (fErrorInDecoding) { | |
242 return; | |
243 } | |
244 if (NULL == fImageCache) { | |
245 // onUnlockPixels() should never be called a second time from | |
246 // PixelRef::Unlock() without calling onLockPixels() first. | |
247 SkASSERT(NULL != fScaledCacheId); | |
248 if (NULL != fScaledCacheId) { | |
249 SkScaledImageCache::Unlock(fScaledCacheId); | |
250 fScaledCacheId = NULL; | |
251 } | |
252 } else { // use fImageCache | |
253 SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId); | |
254 if (SkImageCache::UNINITIALIZED_ID != fCacheId) { | |
255 fImageCache->releaseCache(fCacheId); | |
256 } | |
257 } | |
258 } | |
259 | |
260 SkData* SkLazyPixelRef::onRefEncodedData() { | |
261 fData->ref(); | |
262 return fData; | |
263 } | |
264 | |
265 static bool init_from_info(SkBitmap* bm, const SkImageInfo& info, | |
266 size_t rowBytes) { | |
267 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); | |
268 if (SkBitmap::kNo_Config == config) { | |
269 return false; | |
270 } | |
271 | |
272 return bm->setConfig(config, info.fWidth, info.fHeight, rowBytes, info.fAlph
aType) | |
273 && | |
274 bm->allocPixels(); | |
275 } | |
276 | |
277 bool SkLazyPixelRef::onImplementsDecodeInto() { | |
278 return true; | |
279 } | |
280 | |
281 bool SkLazyPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { | |
282 SkASSERT(fData != NULL && fData->size() > 0); | |
283 if (fErrorInDecoding) { | |
284 return false; | |
285 } | |
286 | |
287 SkImageInfo info; | |
288 // Determine the size of the image in order to determine how much memory to
allocate. | |
289 // FIXME: As an optimization, only do this part once. | |
290 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NULL); | |
291 if (fErrorInDecoding) { | |
292 return false; | |
293 } | |
294 | |
295 SkBitmapFactory::Target target; | |
296 (void)ComputeMinRowBytesAndSize(info, &target.fRowBytes); | |
297 | |
298 SkBitmap tmp; | |
299 if (!init_from_info(&tmp, info, target.fRowBytes)) { | |
300 return false; | |
301 } | |
302 | |
303 target.fAddr = tmp.getPixels(); | |
304 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target
); | |
305 if (fErrorInDecoding) { | |
306 return false; | |
307 } | |
308 | |
309 *bitmap = tmp; | |
310 return true; | |
311 } | |
OLD | NEW |