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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 } | 61 } |
62 return; | 62 return; |
63 } | 63 } |
64 SkASSERT(fImageCache); | 64 SkASSERT(fImageCache); |
65 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { | 65 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { |
66 fImageCache->throwAwayCache(fCacheId); | 66 fImageCache->throwAwayCache(fCacheId); |
67 } | 67 } |
68 fImageCache->unref(); | 68 fImageCache->unref(); |
69 } | 69 } |
70 | 70 |
71 static size_t ComputeMinRowBytesAndSize(const SkImage::Info& info, size_t* rowBy
tes) { | 71 static size_t ComputeMinRowBytesAndSize(const SkImageInfo& info, size_t* rowByte
s) { |
72 *rowBytes = SkImageMinRowBytes(info); | 72 *rowBytes = SkImageMinRowBytes(info); |
73 | 73 |
74 Sk64 safeSize; | 74 Sk64 safeSize; |
75 safeSize.setZero(); | 75 safeSize.setZero(); |
76 if (info.fHeight > 0) { | 76 if (info.fHeight > 0) { |
77 safeSize.setMul(info.fHeight, SkToS32(*rowBytes)); | 77 safeSize.setMul(info.fHeight, SkToS32(*rowBytes)); |
78 } | 78 } |
79 SkASSERT(!safeSize.isNeg()); | 79 SkASSERT(!safeSize.isNeg()); |
80 return safeSize.is32() ? safeSize.get32() : 0; | 80 return safeSize.is32() ? safeSize.get32() : 0; |
81 } | 81 } |
82 | 82 |
83 const SkImage::Info* SkLazyPixelRef::getCachedInfo() { | 83 const SkImageInfo* SkLazyPixelRef::getCachedInfo() { |
84 if (fLazilyCachedInfo.fWidth < 0) { | 84 if (fLazilyCachedInfo.fWidth < 0) { |
85 SkImage::Info info; | 85 SkImageInfo info; |
86 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL
L); | 86 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL
L); |
87 if (fErrorInDecoding) { | 87 if (fErrorInDecoding) { |
88 return NULL; | 88 return NULL; |
89 } | 89 } |
90 fLazilyCachedInfo = info; | 90 fLazilyCachedInfo = info; |
91 } | 91 } |
92 return &fLazilyCachedInfo; | 92 return &fLazilyCachedInfo; |
93 } | 93 } |
94 | 94 |
95 /** | 95 /** |
96 Returns bitmap->getPixels() on success; NULL on failure */ | 96 Returns bitmap->getPixels() on success; NULL on failure */ |
97 static void* decode_into_bitmap(SkImage::Info* info, | 97 static void* decode_into_bitmap(SkImageInfo* info, |
98 SkBitmapFactory::DecodeProc decodeProc, | 98 SkBitmapFactory::DecodeProc decodeProc, |
99 size_t* rowBytes, | 99 size_t* rowBytes, |
100 SkData* data, | 100 SkData* data, |
101 SkBitmap* bm) { | 101 SkBitmap* bm) { |
102 SkASSERT(info && decodeProc && rowBytes && data && bm); | 102 SkASSERT(info && decodeProc && rowBytes && data && bm); |
103 if (!(bm->setConfig(SkImageInfoToBitmapConfig(*info), info->fWidth, | 103 if (!(bm->setConfig(SkImageInfoToBitmapConfig(*info), info->fWidth, |
104 info->fHeight, *rowBytes, info->fAlphaType) | 104 info->fHeight, *rowBytes, info->fAlphaType) |
105 && bm->allocPixels(NULL, NULL))) { | 105 && bm->allocPixels(NULL, NULL))) { |
106 // Use the default allocator. It may be necessary for the | 106 // Use the default allocator. It may be necessary for the |
107 // SkLazyPixelRef to have a allocator field which is passed | 107 // SkLazyPixelRef to have a allocator field which is passed |
108 // into allocPixels(). | 108 // into allocPixels(). |
109 return NULL; | 109 return NULL; |
110 } | 110 } |
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 void* SkLazyPixelRef::lockScaledImageCachePixels() { |
122 SkASSERT(!fErrorInDecoding); | 122 SkASSERT(!fErrorInDecoding); |
123 SkASSERT(NULL == fImageCache); | 123 SkASSERT(NULL == fImageCache); |
124 SkBitmap bitmap; | 124 SkBitmap bitmap; |
125 const SkImage::Info* info = this->getCachedInfo(); | 125 const SkImageInfo* info = this->getCachedInfo(); |
126 if (info == NULL) { | 126 if (info == NULL) { |
127 return NULL; | 127 return NULL; |
128 } | 128 } |
129 // If this is the first time though, this is guaranteed to fail. | 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" | 130 // Maybe we should have a flag that says "don't even bother looking" |
131 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), | 131 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), |
132 info->fWidth, | 132 info->fWidth, |
133 info->fHeight, | 133 info->fHeight, |
134 &bitmap); | 134 &bitmap); |
135 if (fScaledCacheId != NULL) { | 135 if (fScaledCacheId != NULL) { |
136 SkAutoLockPixels autoLockPixels(bitmap); | 136 SkAutoLockPixels autoLockPixels(bitmap); |
137 void* pixels = bitmap.getPixels(); | 137 void* pixels = bitmap.getPixels(); |
138 SkASSERT(NULL != pixels); | 138 SkASSERT(NULL != pixels); |
139 // At this point, the autoLockPixels will unlockPixels() | 139 // At this point, the autoLockPixels will unlockPixels() |
140 // to remove bitmap's lock on the pixels. We will then | 140 // to remove bitmap's lock on the pixels. We will then |
141 // destroy bitmap. The *only* guarantee that this pointer | 141 // destroy bitmap. The *only* guarantee that this pointer |
142 // remains valid is the guarantee made by | 142 // remains valid is the guarantee made by |
143 // SkScaledImageCache that it will not destroy the *other* | 143 // SkScaledImageCache that it will not destroy the *other* |
144 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a | 144 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a |
145 // reference to the concrete PixelRef while this record is | 145 // reference to the concrete PixelRef while this record is |
146 // locked. | 146 // locked. |
147 return pixels; | 147 return pixels; |
148 } else { | 148 } else { |
149 // Cache has been purged, must re-decode. | 149 // Cache has been purged, must re-decode. |
150 void* pixels = decode_into_bitmap(const_cast<SkImage::Info*>(info), | 150 void* pixels = decode_into_bitmap(const_cast<SkImageInfo*>(info), |
151 fDecodeProc, &fRowBytes, fData, | 151 fDecodeProc, &fRowBytes, fData, |
152 &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); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 } | 197 } |
198 // Cache miss. Either pinCache returned NULL or it returned a memory add
ress without the old | 198 // Cache miss. Either pinCache returned NULL or it returned a memory add
ress without the old |
199 // data | 199 // data |
200 #if LAZY_CACHE_STATS | 200 #if LAZY_CACHE_STATS |
201 sk_atomic_inc(&gCacheMisses); | 201 sk_atomic_inc(&gCacheMisses); |
202 #endif | 202 #endif |
203 } | 203 } |
204 | 204 |
205 SkASSERT(fData != NULL && fData->size() > 0); | 205 SkASSERT(fData != NULL && fData->size() > 0); |
206 if (NULL == target.fAddr) { | 206 if (NULL == target.fAddr) { |
207 const SkImage::Info* info = this->getCachedInfo(); | 207 const SkImageInfo* info = this->getCachedInfo(); |
208 if (NULL == info) { | 208 if (NULL == info) { |
209 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId); | 209 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId); |
210 return NULL; | 210 return NULL; |
211 } | 211 } |
212 size_t bytes = ComputeMinRowBytesAndSize(*info, &target.fRowBytes); | 212 size_t bytes = ComputeMinRowBytesAndSize(*info, &target.fRowBytes); |
213 target.fAddr = fImageCache->allocAndPinCache(bytes, &fCacheId); | 213 target.fAddr = fImageCache->allocAndPinCache(bytes, &fCacheId); |
214 if (NULL == target.fAddr) { | 214 if (NULL == target.fAddr) { |
215 // Space could not be allocated. | 215 // Space could not be allocated. |
216 // Just like the last assert, fCacheId must be UNINITIALIZED_ID. | 216 // Just like the last assert, fCacheId must be UNINITIALIZED_ID. |
217 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId); | 217 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 fImageCache->releaseCache(fCacheId); | 255 fImageCache->releaseCache(fCacheId); |
256 } | 256 } |
257 } | 257 } |
258 } | 258 } |
259 | 259 |
260 SkData* SkLazyPixelRef::onRefEncodedData() { | 260 SkData* SkLazyPixelRef::onRefEncodedData() { |
261 fData->ref(); | 261 fData->ref(); |
262 return fData; | 262 return fData; |
263 } | 263 } |
264 | 264 |
265 static bool init_from_info(SkBitmap* bm, const SkImage::Info& info, | 265 static bool init_from_info(SkBitmap* bm, const SkImageInfo& info, |
266 size_t rowBytes) { | 266 size_t rowBytes) { |
267 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); | 267 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); |
268 if (SkBitmap::kNo_Config == config) { | 268 if (SkBitmap::kNo_Config == config) { |
269 return false; | 269 return false; |
270 } | 270 } |
271 | 271 |
272 return bm->setConfig(config, info.fWidth, info.fHeight, rowBytes, info.fAlph
aType) | 272 return bm->setConfig(config, info.fWidth, info.fHeight, rowBytes, info.fAlph
aType) |
273 && | 273 && |
274 bm->allocPixels(); | 274 bm->allocPixels(); |
275 } | 275 } |
276 | 276 |
277 bool SkLazyPixelRef::onImplementsDecodeInto() { | 277 bool SkLazyPixelRef::onImplementsDecodeInto() { |
278 return true; | 278 return true; |
279 } | 279 } |
280 | 280 |
281 bool SkLazyPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { | 281 bool SkLazyPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { |
282 SkASSERT(fData != NULL && fData->size() > 0); | 282 SkASSERT(fData != NULL && fData->size() > 0); |
283 if (fErrorInDecoding) { | 283 if (fErrorInDecoding) { |
284 return false; | 284 return false; |
285 } | 285 } |
286 | 286 |
287 SkImage::Info info; | 287 SkImageInfo info; |
288 // Determine the size of the image in order to determine how much memory to
allocate. | 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. | 289 // FIXME: As an optimization, only do this part once. |
290 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NULL); | 290 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NULL); |
291 if (fErrorInDecoding) { | 291 if (fErrorInDecoding) { |
292 return false; | 292 return false; |
293 } | 293 } |
294 | 294 |
295 SkBitmapFactory::Target target; | 295 SkBitmapFactory::Target target; |
296 (void)ComputeMinRowBytesAndSize(info, &target.fRowBytes); | 296 (void)ComputeMinRowBytesAndSize(info, &target.fRowBytes); |
297 | 297 |
298 SkBitmap tmp; | 298 SkBitmap tmp; |
299 if (!init_from_info(&tmp, info, target.fRowBytes)) { | 299 if (!init_from_info(&tmp, info, target.fRowBytes)) { |
300 return false; | 300 return false; |
301 } | 301 } |
302 | 302 |
303 target.fAddr = tmp.getPixels(); | 303 target.fAddr = tmp.getPixels(); |
304 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target
); | 304 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target
); |
305 if (fErrorInDecoding) { | 305 if (fErrorInDecoding) { |
306 return false; | 306 return false; |
307 } | 307 } |
308 | 308 |
309 *bitmap = tmp; | 309 *bitmap = tmp; |
310 return true; | 310 return true; |
311 } | 311 } |
OLD | NEW |