OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "Sk64.h" | 8 #include "Sk64.h" |
9 #include "SkLazyPixelRef.h" | 9 #include "SkLazyPixelRef.h" |
10 #include "SkColorTable.h" | 10 #include "SkColorTable.h" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 SkBitmapFactory::Target target; | 111 SkBitmapFactory::Target target; |
112 target.fAddr = bm->getPixels(); | 112 target.fAddr = bm->getPixels(); |
113 target.fRowBytes = bm->rowBytes(); | 113 target.fRowBytes = bm->rowBytes(); |
114 *rowBytes = target.fRowBytes; | 114 *rowBytes = target.fRowBytes; |
115 if (!decodeProc(data->data(), data->size(), info, &target)) { | 115 if (!decodeProc(data->data(), data->size(), info, &target)) { |
116 return NULL; | 116 return NULL; |
117 } | 117 } |
118 return target.fAddr; | 118 return target.fAddr; |
119 } | 119 } |
120 | 120 |
121 void* SkLazyPixelRef::lockScaledImageCachePixels() { | 121 bool SkLazyPixelRef::lockScaledImageCachePixels(LockRec* rec) { |
122 SkASSERT(!fErrorInDecoding); | 122 SkASSERT(!fErrorInDecoding); |
123 SkASSERT(NULL == fImageCache); | 123 SkASSERT(NULL == fImageCache); |
124 SkBitmap bitmap; | 124 SkBitmap bitmap; |
125 const SkImageInfo* info = this->getCachedInfo(); | 125 const SkImageInfo* info = this->getCachedInfo(); |
126 if (info == NULL) { | 126 if (NULL == info) { |
127 return NULL; | 127 return false; |
128 } | 128 } |
| 129 |
129 // If this is the first time though, this is guaranteed to fail. | 130 // 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 // Maybe we should have a flag that says "don't even bother looking" |
131 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), | 132 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), |
132 info->fWidth, | 133 info->fWidth, |
133 info->fHeight, | 134 info->fHeight, |
134 &bitmap); | 135 &bitmap); |
| 136 void* pixels; |
135 if (fScaledCacheId != NULL) { | 137 if (fScaledCacheId != NULL) { |
136 SkAutoLockPixels autoLockPixels(bitmap); | 138 SkAutoLockPixels autoLockPixels(bitmap); |
137 void* pixels = bitmap.getPixels(); | 139 pixels = bitmap.getPixels(); |
138 SkASSERT(NULL != pixels); | 140 SkASSERT(NULL != pixels); |
139 // At this point, the autoLockPixels will unlockPixels() | 141 // At this point, the autoLockPixels will unlockPixels() |
140 // to remove bitmap's lock on the pixels. We will then | 142 // to remove bitmap's lock on the pixels. We will then |
141 // destroy bitmap. The *only* guarantee that this pointer | 143 // destroy bitmap. The *only* guarantee that this pointer |
142 // remains valid is the guarantee made by | 144 // remains valid is the guarantee made by |
143 // SkScaledImageCache that it will not destroy the *other* | 145 // SkScaledImageCache that it will not destroy the *other* |
144 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a | 146 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a |
145 // reference to the concrete PixelRef while this record is | 147 // reference to the concrete PixelRef while this record is |
146 // locked. | 148 // locked. |
147 return pixels; | |
148 } else { | 149 } else { |
149 // Cache has been purged, must re-decode. | 150 // Cache has been purged, must re-decode. |
150 void* pixels = decode_into_bitmap(const_cast<SkImageInfo*>(info), | 151 pixels = decode_into_bitmap(const_cast<SkImageInfo*>(info), fDecodeProc, |
151 fDecodeProc, &fRowBytes, fData, | 152 &fRowBytes, fData, &bitmap); |
152 &bitmap); | |
153 if (NULL == pixels) { | 153 if (NULL == pixels) { |
154 fErrorInDecoding = true; | 154 fErrorInDecoding = true; |
155 return NULL; | 155 return NULL; |
156 } | 156 } |
157 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), | 157 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), |
158 info->fWidth, | 158 info->fWidth, |
159 info->fHeight, | 159 info->fHeight, |
160 bitmap); | 160 bitmap); |
161 SkASSERT(fScaledCacheId != NULL); | 161 SkASSERT(fScaledCacheId != NULL); |
162 return pixels; | |
163 } | 162 } |
| 163 |
| 164 rec->fPixels = pixels; |
| 165 rec->fColorTable = NULL; |
| 166 rec->fRowBytes = bitmap.rowBytes(); |
| 167 return true; |
164 } | 168 } |
165 | 169 |
166 void* SkLazyPixelRef::onLockPixels(SkColorTable**) { | 170 bool SkLazyPixelRef::lockImageCachePixels(LockRec* rec) { |
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); | 171 SkASSERT(fImageCache != NULL); |
179 SkASSERT(!fErrorInDecoding); | 172 SkASSERT(!fErrorInDecoding); |
180 SkBitmapFactory::Target target; | 173 SkBitmapFactory::Target target; |
181 // Check to see if the pixels still exist in the cache. | 174 // Check to see if the pixels still exist in the cache. |
182 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { | 175 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { |
183 target.fAddr = NULL; | 176 target.fAddr = NULL; |
184 } else { | 177 } else { |
185 SkImageCache::DataStatus status; | 178 SkImageCache::DataStatus status; |
186 target.fAddr = fImageCache->pinCache(fCacheId, &status); | 179 target.fAddr = fImageCache->pinCache(fCacheId, &status); |
187 if (target.fAddr == NULL) { | 180 if (target.fAddr == NULL) { |
(...skipping 12 matching lines...) Expand all Loading... |
200 #if LAZY_CACHE_STATS | 193 #if LAZY_CACHE_STATS |
201 sk_atomic_inc(&gCacheMisses); | 194 sk_atomic_inc(&gCacheMisses); |
202 #endif | 195 #endif |
203 } | 196 } |
204 | 197 |
205 SkASSERT(fData != NULL && fData->size() > 0); | 198 SkASSERT(fData != NULL && fData->size() > 0); |
206 if (NULL == target.fAddr) { | 199 if (NULL == target.fAddr) { |
207 const SkImageInfo* info = this->getCachedInfo(); | 200 const SkImageInfo* info = this->getCachedInfo(); |
208 if (NULL == info) { | 201 if (NULL == info) { |
209 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId); | 202 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId); |
210 return NULL; | 203 return false; |
211 } | 204 } |
212 size_t bytes = ComputeMinRowBytesAndSize(*info, &target.fRowBytes); | 205 size_t bytes = ComputeMinRowBytesAndSize(*info, &target.fRowBytes); |
213 target.fAddr = fImageCache->allocAndPinCache(bytes, &fCacheId); | 206 target.fAddr = fImageCache->allocAndPinCache(bytes, &fCacheId); |
214 if (NULL == target.fAddr) { | 207 if (NULL == target.fAddr) { |
215 // Space could not be allocated. | 208 // Space could not be allocated. |
216 // Just like the last assert, fCacheId must be UNINITIALIZED_ID. | 209 // Just like the last assert, fCacheId must be UNINITIALIZED_ID. |
217 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId); | 210 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId); |
218 return NULL; | 211 return false; |
219 } | 212 } |
220 } else { | 213 } else { |
221 // pinCache returned purged memory to which target.fAddr already points.
Set | 214 // pinCache returned purged memory to which target.fAddr already points.
Set |
222 // target.fRowBytes properly. | 215 // target.fRowBytes properly. |
223 target.fRowBytes = fRowBytes; | 216 target.fRowBytes = fRowBytes; |
224 // Assume that the size is correct, since it was determined by this same
function | 217 // Assume that the size is correct, since it was determined by this same
function |
225 // previously. | 218 // previously. |
226 } | 219 } |
227 SkASSERT(target.fAddr != NULL); | 220 SkASSERT(target.fAddr != NULL); |
228 SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId); | 221 SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId); |
229 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), NULL, &target)
; | 222 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), NULL, &target)
; |
230 if (fErrorInDecoding) { | 223 if (fErrorInDecoding) { |
231 fImageCache->throwAwayCache(fCacheId); | 224 fImageCache->throwAwayCache(fCacheId); |
232 fCacheId = SkImageCache::UNINITIALIZED_ID; | 225 fCacheId = SkImageCache::UNINITIALIZED_ID; |
233 return NULL; | 226 return NULL; |
234 } | 227 } |
235 // Upon success, store fRowBytes so it can be used in case pinCache later re
turns purged memory. | 228 // Upon success, store fRowBytes so it can be used in case pinCache later re
turns purged memory. |
236 fRowBytes = target.fRowBytes; | 229 fRowBytes = target.fRowBytes; |
237 return target.fAddr; | 230 |
| 231 rec->fPixels = target.fAddr; |
| 232 rec->fColorTable = NULL; |
| 233 rec->fRowBytes = target.fRowBytes; |
| 234 return true; |
| 235 } |
| 236 |
| 237 /////////////////////////////////////////////////////////////////////////////// |
| 238 |
| 239 bool SkLazyPixelRef::onGetInfo(SkImageInfo* info) { |
| 240 const SkImageInfo* cachedInfo = this->getCachedInfo(); |
| 241 if (NULL == cachedInfo) { |
| 242 return false; |
| 243 } |
| 244 *info = *cachedInfo; |
| 245 return true; |
| 246 } |
| 247 |
| 248 bool SkLazyPixelRef::onNewLockPixels(LockRec* rec) { |
| 249 if (fErrorInDecoding) { |
| 250 return false; |
| 251 } |
| 252 if (NULL == fImageCache) { |
| 253 return this->lockScaledImageCachePixels(rec); |
| 254 } else { |
| 255 return this->lockImageCachePixels(rec); |
| 256 } |
238 } | 257 } |
239 | 258 |
240 void SkLazyPixelRef::onUnlockPixels() { | 259 void SkLazyPixelRef::onUnlockPixels() { |
241 if (fErrorInDecoding) { | 260 if (fErrorInDecoding) { |
242 return; | 261 return; |
243 } | 262 } |
244 if (NULL == fImageCache) { | 263 if (NULL == fImageCache) { |
245 // onUnlockPixels() should never be called a second time from | 264 // onUnlockPixels() should never be called a second time from |
246 // PixelRef::Unlock() without calling onLockPixels() first. | 265 // PixelRef::Unlock() without calling onLockPixels() first. |
247 SkASSERT(NULL != fScaledCacheId); | 266 SkASSERT(NULL != fScaledCacheId); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 | 321 |
303 target.fAddr = tmp.getPixels(); | 322 target.fAddr = tmp.getPixels(); |
304 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target
); | 323 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target
); |
305 if (fErrorInDecoding) { | 324 if (fErrorInDecoding) { |
306 return false; | 325 return false; |
307 } | 326 } |
308 | 327 |
309 *bitmap = tmp; | 328 *bitmap = tmp; |
310 return true; | 329 return true; |
311 } | 330 } |
OLD | NEW |