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" |
11 #include "SkData.h" | 11 #include "SkData.h" |
12 #include "SkImageCache.h" | 12 #include "SkImageCache.h" |
13 #include "SkImagePriv.h" | 13 #include "SkImagePriv.h" |
14 #include "SkScaledImageCache.h" | |
14 | 15 |
15 #if LAZY_CACHE_STATS | 16 #if LAZY_CACHE_STATS |
16 #include "SkThread.h" | 17 #include "SkThread.h" |
17 | 18 |
18 int32_t SkLazyPixelRef::gCacheHits; | 19 int32_t SkLazyPixelRef::gCacheHits; |
19 int32_t SkLazyPixelRef::gCacheMisses; | 20 int32_t SkLazyPixelRef::gCacheMisses; |
20 #endif | 21 #endif |
21 | 22 |
22 SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S kImageCache* cache) | 23 SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S kImageCache* cache) |
23 // Pass NULL for the Mutex so that the default (ring buffer) will be used. | 24 // Pass NULL for the Mutex so that the default (ring buffer) will be used. |
24 : INHERITED(NULL) | 25 : INHERITED(NULL) |
26 , fErrorInDecoding(false) | |
25 , fDecodeProc(proc) | 27 , fDecodeProc(proc) |
26 , fImageCache(cache) | 28 , fImageCache(cache) |
27 , fCacheId(SkImageCache::UNINITIALIZED_ID) | 29 , fCacheId(0) |
28 , fRowBytes(0) { | 30 , fRowBytes(0) |
31 , fAllocator(NULL) { | |
29 SkASSERT(fDecodeProc != NULL); | 32 SkASSERT(fDecodeProc != NULL); |
33 SkASSERT(0 == SkImageCache::UNINITIALIZED_ID); // for fCacheId | |
30 if (NULL == data) { | 34 if (NULL == data) { |
31 fData = SkData::NewEmpty(); | 35 fData = SkData::NewEmpty(); |
32 fErrorInDecoding = true; | 36 fErrorInDecoding = true; |
33 } else { | 37 } else { |
34 fData = data; | 38 fData = data; |
35 fData->ref(); | 39 fData->ref(); |
36 fErrorInDecoding = data->size() == 0; | 40 fErrorInDecoding = data->size() == 0; |
37 } | 41 } |
38 SkASSERT(cache != NULL); | 42 if (fImageCache != NULL) { |
39 cache->ref(); | 43 fImageCache->ref(); |
44 } | |
40 | 45 |
41 // mark as uninitialized -- all fields are -1 | 46 // mark as uninitialized -- all fields are -1 |
42 memset(&fLazilyCachedInfo, 0xFF, sizeof(fLazilyCachedInfo)); | 47 memset(&fLazilyCachedInfo, 0xFF, sizeof(fLazilyCachedInfo)); |
43 | 48 |
44 // Since this pixel ref bases its data on encoded data, it should never chan ge. | 49 // Since this pixel ref bases its data on encoded data, it should never chan ge. |
45 this->setImmutable(); | 50 this->setImmutable(); |
46 } | 51 } |
47 | 52 |
48 SkLazyPixelRef::~SkLazyPixelRef() { | 53 SkLazyPixelRef::~SkLazyPixelRef() { |
49 SkASSERT(fData != NULL); | 54 SkASSERT(fData != NULL); |
50 fData->unref(); | 55 fData->unref(); |
56 SkSafeUnref(fAllocator); | |
57 if (NULL == fImageCache) { | |
58 if (fCacheId != 0) { | |
59 SkScaledImageCache::Unlock((SkScaledImageCache::ID *)(fCacheId)); | |
mtklein
2013/10/24 17:03:25
* affinity
| |
60 // SkScaledImageCache needs a throwAwayCache(id) method. | |
61 } | |
62 return; | |
63 } | |
51 SkASSERT(fImageCache); | 64 SkASSERT(fImageCache); |
52 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { | 65 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { |
53 fImageCache->throwAwayCache(fCacheId); | 66 fImageCache->throwAwayCache(fCacheId); |
54 } | 67 } |
55 fImageCache->unref(); | 68 fImageCache->unref(); |
56 } | 69 } |
57 | 70 |
58 static size_t ComputeMinRowBytesAndSize(const SkImage::Info& info, size_t* rowBy tes) { | 71 static size_t ComputeMinRowBytesAndSize(const SkImage::Info& info, size_t* rowBy tes) { |
59 *rowBytes = SkImageMinRowBytes(info); | 72 *rowBytes = SkImageMinRowBytes(info); |
60 | 73 |
(...skipping 11 matching lines...) Expand all Loading... | |
72 SkImage::Info info; | 85 SkImage::Info info; |
73 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL L); | 86 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL L); |
74 if (fErrorInDecoding) { | 87 if (fErrorInDecoding) { |
75 return NULL; | 88 return NULL; |
76 } | 89 } |
77 fLazilyCachedInfo = info; | 90 fLazilyCachedInfo = info; |
78 } | 91 } |
79 return &fLazilyCachedInfo; | 92 return &fLazilyCachedInfo; |
80 } | 93 } |
81 | 94 |
95 /** | |
96 Returns bitmap->getPixels() on success; NULL on failure */ | |
97 static void* decode_into_bitmap(SkImage::Info* info, | |
98 SkBitmapFactory::DecodeProc decodeProc, | |
99 size_t* rowBytes, | |
100 SkData* data, | |
101 SkBitmap::Allocator* allocator, | |
102 SkBitmap* bm) { | |
103 SkASSERT(info && decodeProc && rowBytes && data && bm); | |
104 if (!(bm->setConfig(SkImageInfoToBitmapConfig(*info), info->fWidth, | |
105 info->fHeight, *rowBytes, info->fAlphaType) | |
106 && bm->allocPixels(allocator, NULL))) { | |
107 return NULL; | |
108 } | |
109 SkBitmapFactory::Target target; | |
110 target.fAddr = bm->getPixels(); | |
111 target.fRowBytes = bm->rowBytes(); | |
112 *rowBytes = target.fRowBytes; | |
113 if (!decodeProc(data->data(), data->size(), info, &target)) { | |
114 return NULL; | |
115 } | |
116 return target.fAddr; | |
117 } | |
118 | |
119 void* SkLazyPixelRef::lockScaledImageCachePixels() { | |
120 SkASSERT(!fErrorInDecoding); | |
121 SkASSERT(NULL == fImageCache); | |
122 SkBitmap bitmap; | |
123 if (fLazilyCachedInfo.fWidth > 0) { // fInfo already populated | |
124 fCacheId = (intptr_t)(SkScaledImageCache::FindAndLock( | |
125 this->getGenerationID(), | |
126 fLazilyCachedInfo.fWidth, | |
127 fLazilyCachedInfo.fHeight, | |
128 &bitmap)); | |
129 if (fCacheId != 0) { | |
130 SkAutoLockPixels autoLockPixels(bitmap); | |
131 void* pixels = bitmap.getPixels(); | |
132 SkASSERT(NULL != pixels); | |
133 // At this point, the autoLockPixels will unlockPixels() | |
134 // to remove bitmap's lock on the pixels. We will then | |
135 // destroy bitmap. The *only* guarantee that this pointer | |
136 // remains valid is the guarantee made by | |
137 // SkScaledImageCache that it will not destroy the *other* | |
138 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a | |
139 // reference to the concrete PixelRef while this record is | |
140 // locked. | |
141 return pixels; | |
142 } // else cache has been purged, must re-decode. | |
143 } else { // first time through this code, must populate info | |
144 (void)this->getCachedInfo(); | |
145 if (fErrorInDecoding) { | |
146 return NULL; | |
147 } | |
148 } | |
149 SkASSERT(fLazilyCachedInfo.fWidth > 0); | |
150 void * ptr = decode_into_bitmap(&fLazilyCachedInfo, fDecodeProc, | |
mtklein
2013/10/24 17:03:25
*
| |
151 &fRowBytes, fData, fAllocator, &bitmap); | |
152 if (NULL == ptr) { | |
153 fErrorInDecoding = true; | |
154 return NULL; | |
155 } | |
156 fCacheId = (intptr_t)(SkScaledImageCache::AddAndLock( | |
157 this->getGenerationID(), | |
158 fLazilyCachedInfo.fWidth, | |
159 fLazilyCachedInfo.fHeight, | |
160 bitmap)); | |
161 return ptr; | |
162 } | |
163 | |
82 void* SkLazyPixelRef::onLockPixels(SkColorTable**) { | 164 void* SkLazyPixelRef::onLockPixels(SkColorTable**) { |
83 if (fErrorInDecoding) { | 165 if (fErrorInDecoding) { |
84 return NULL; | 166 return NULL; |
85 } | 167 } |
168 if (NULL == fImageCache) { | |
169 return this->lockScaledImageCachePixels(); | |
170 } else { | |
171 return this->lockImageCachePixels(); | |
172 } | |
173 } | |
174 | |
175 void* SkLazyPixelRef::lockImageCachePixels() { | |
176 SkASSERT(fImageCache != NULL); | |
177 SkASSERT(!fErrorInDecoding); | |
86 SkBitmapFactory::Target target; | 178 SkBitmapFactory::Target target; |
87 // Check to see if the pixels still exist in the cache. | 179 // Check to see if the pixels still exist in the cache. |
88 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { | 180 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { |
89 target.fAddr = NULL; | 181 target.fAddr = NULL; |
90 } else { | 182 } else { |
91 SkImageCache::DataStatus status; | 183 SkImageCache::DataStatus status; |
92 target.fAddr = fImageCache->pinCache(fCacheId, &status); | 184 target.fAddr = fImageCache->pinCache(fCacheId, &status); |
93 if (target.fAddr == NULL) { | 185 if (target.fAddr == NULL) { |
94 fCacheId = SkImageCache::UNINITIALIZED_ID; | 186 fCacheId = SkImageCache::UNINITIALIZED_ID; |
95 } else { | 187 } else { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 fImageCache->throwAwayCache(fCacheId); | 229 fImageCache->throwAwayCache(fCacheId); |
138 fCacheId = SkImageCache::UNINITIALIZED_ID; | 230 fCacheId = SkImageCache::UNINITIALIZED_ID; |
139 return NULL; | 231 return NULL; |
140 } | 232 } |
141 // Upon success, store fRowBytes so it can be used in case pinCache later re turns purged memory. | 233 // Upon success, store fRowBytes so it can be used in case pinCache later re turns purged memory. |
142 fRowBytes = target.fRowBytes; | 234 fRowBytes = target.fRowBytes; |
143 return target.fAddr; | 235 return target.fAddr; |
144 } | 236 } |
145 | 237 |
146 void SkLazyPixelRef::onUnlockPixels() { | 238 void SkLazyPixelRef::onUnlockPixels() { |
147 if (fErrorInDecoding) { | 239 if (fErrorInDecoding || (0 == fCacheId)) { |
148 return; | 240 return; |
149 } | 241 } |
150 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { | 242 if (NULL == fImageCache) { |
243 SkScaledImageCache::Unlock((SkScaledImageCache::ID *)(fCacheId)); | |
mtklein
2013/10/24 17:03:25
*
| |
244 fCacheId = 0; | |
245 return; | |
246 } else { // use fImageCache | |
151 fImageCache->releaseCache(fCacheId); | 247 fImageCache->releaseCache(fCacheId); |
152 } | 248 } |
153 } | 249 } |
154 | 250 |
155 SkData* SkLazyPixelRef::onRefEncodedData() { | 251 SkData* SkLazyPixelRef::onRefEncodedData() { |
156 fData->ref(); | 252 fData->ref(); |
157 return fData; | 253 return fData; |
158 } | 254 } |
159 | 255 |
160 #include "SkImagePriv.h" | |
161 | |
162 static bool init_from_info(SkBitmap* bm, const SkImage::Info& info, | 256 static bool init_from_info(SkBitmap* bm, const SkImage::Info& info, |
163 size_t rowBytes) { | 257 size_t rowBytes) { |
164 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); | 258 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); |
165 if (SkBitmap::kNo_Config == config) { | 259 if (SkBitmap::kNo_Config == config) { |
166 return false; | 260 return false; |
167 } | 261 } |
168 | 262 |
169 return bm->setConfig(config, info.fWidth, info.fHeight, rowBytes, info.fAlph aType) | 263 return bm->setConfig(config, info.fWidth, info.fHeight, rowBytes, info.fAlph aType) |
170 && | 264 && |
171 bm->allocPixels(); | 265 bm->allocPixels(); |
(...skipping 27 matching lines...) Expand all Loading... | |
199 | 293 |
200 target.fAddr = tmp.getPixels(); | 294 target.fAddr = tmp.getPixels(); |
201 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target ); | 295 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target ); |
202 if (fErrorInDecoding) { | 296 if (fErrorInDecoding) { |
203 return false; | 297 return false; |
204 } | 298 } |
205 | 299 |
206 *bitmap = tmp; | 300 *bitmap = tmp; |
207 return true; | 301 return true; |
208 } | 302 } |
303 | |
304 void SkLazyPixelRef::setAllocator(SkBitmap::Allocator * allocator) { | |
mtklein
2013/10/24 17:03:25
*
| |
305 SkRefCnt_SafeAssign(fAllocator, allocator); | |
306 } | |
OLD | NEW |