Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: src/lazy/SkLazyPixelRef.cpp

Issue 83663006: Modify SkLazyPixelRef to use SkImageGenerator. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: reupload Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "SkScaledImageCache.h"
15 15
16 #if LAZY_CACHE_STATS 16 #if LAZY_CACHE_STATS
17 #include "SkThread.h" 17 #include "SkThread.h"
18 18
19 int32_t SkLazyPixelRef::gCacheHits; 19 int32_t SkLazyPixelRef::gCacheHits;
20 int32_t SkLazyPixelRef::gCacheMisses; 20 int32_t SkLazyPixelRef::gCacheMisses;
21 #endif 21 #endif
22 22
23 SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S kImageCache* cache) 23
24 // Pass NULL for the Mutex so that the default (ring buffer) will be used. 24 bool SkLazyPixelRef::Install(SkImageGenerator* generator,
25 SkImageCache* imageCache,
26 SkBitmap* dst) {
27 SkImageInfo info;
28 SkASSERT(generator != NULL);
29 SkASSERT(dst != NULL);
30 SkASSERT(imageCache != NULL);
31 if ((NULL == generator)
32 || !(generator->getInfo(&info))
33 || !dst->setConfig(info, 0)) {
34 SkDELETE(generator);
35 return false;
36 }
37 SkAutoTUnref<SkLazyPixelRef> ref(SkNEW_ARGS(SkLazyPixelRef,
38 (generator,
39 info,
40 dst->getSize(),
41 dst->rowBytes(),
42 imageCache)));
43 dst->setPixelRef(ref);
44 return true;
45 }
46
47 SkLazyPixelRef::SkLazyPixelRef(SkImageGenerator* imageGenerator,
48 const SkImageInfo& info,
49 size_t size,
50 size_t rowBytes,
51 SkImageCache* cache)
25 : INHERITED(NULL) 52 : INHERITED(NULL)
26 , fErrorInDecoding(false) 53 , fErrorInDecoding(false)
27 , fDecodeProc(proc) 54 , fImageGenerator(imageGenerator)
28 , fImageCache(cache) 55 , fCacheId(SkImageCache::UNINITIALIZED_ID)
29 , fRowBytes(0) { 56 , fInfo(info)
30 SkASSERT(fDecodeProc != NULL); 57 , fSize(size)
31 if (NULL == data) { 58 , fRowBytes(rowBytes)
32 fData = SkData::NewEmpty(); 59 , fImageCache(cache) {
33 fErrorInDecoding = true; 60 SkASSERT(fImageCache != NULL);
34 } else { 61 SkASSERT(fImageGenerator != NULL);
35 fData = data; 62 fImageCache->ref();
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(); 63 this->setImmutable();
51 } 64 }
52
53 SkLazyPixelRef::~SkLazyPixelRef() { 65 SkLazyPixelRef::~SkLazyPixelRef() {
54 SkASSERT(fData != NULL); 66 SkDELETE(fImageGenerator);
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) { 67 if (fCacheId != SkImageCache::UNINITIALIZED_ID) {
66 fImageCache->throwAwayCache(fCacheId); 68 fImageCache->throwAwayCache(fCacheId);
67 } 69 }
68 fImageCache->unref(); 70 fImageCache->unref();
69 } 71 }
70 72
71 static size_t ComputeMinRowBytesAndSize(const SkImageInfo& info, size_t* rowByte s) { 73 void* SkLazyPixelRef::onLockPixels(SkColorTable** colorTable) {
72 *rowBytes = SkImageMinRowBytes(info); 74 (void)colorTable;
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) { 75 if (fErrorInDecoding) {
168 return NULL; 76 return NULL;
169 } 77 }
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. 78 // Check to see if the pixels still exist in the cache.
182 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { 79 void* pixels = NULL;
183 target.fAddr = NULL; 80 if (SkImageCache::UNINITIALIZED_ID != fCacheId) {
184 } else {
185 SkImageCache::DataStatus status; 81 SkImageCache::DataStatus status;
186 target.fAddr = fImageCache->pinCache(fCacheId, &status); 82 pixels = fImageCache->pinCache(fCacheId, &status);
187 if (target.fAddr == NULL) { 83 if (pixels == NULL) {
188 fCacheId = SkImageCache::UNINITIALIZED_ID; 84 fCacheId = SkImageCache::UNINITIALIZED_ID;
189 } else { 85 } else {
190 if (SkImageCache::kRetained_DataStatus == status) { 86 if (SkImageCache::kRetained_DataStatus == status) {
191 #if LAZY_CACHE_STATS 87 #if LAZY_CACHE_STATS
192 sk_atomic_inc(&gCacheHits); 88 sk_atomic_inc(&gCacheHits);
193 #endif 89 #endif
194 return target.fAddr; 90 return pixels;
195 } 91 }
196 SkASSERT(SkImageCache::kUninitialized_DataStatus == status); 92 SkASSERT(SkImageCache::kUninitialized_DataStatus == status);
197 } 93 }
198 // Cache miss. Either pinCache returned NULL or it returned a memory add ress without the old 94 // Cache miss. Either pinCache returned NULL or it returned a
199 // data 95 // memory address without the old data
200 #if LAZY_CACHE_STATS 96 #if LAZY_CACHE_STATS
201 sk_atomic_inc(&gCacheMisses); 97 sk_atomic_inc(&gCacheMisses);
202 #endif 98 #endif
203 } 99 }
204 100
205 SkASSERT(fData != NULL && fData->size() > 0); 101 if (NULL == pixels) {
206 if (NULL == target.fAddr) { 102 pixels = fImageCache->allocAndPinCache(fSize, &fCacheId);
207 const SkImageInfo* info = this->getCachedInfo(); 103 if (NULL == pixels) {
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. 104 // Space could not be allocated.
216 // Just like the last assert, fCacheId must be UNINITIALIZED_ID. 105 // Just like the last assert, fCacheId must be UNINITIALIZED_ID.
217 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId); 106 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId);
218 return NULL; 107 return NULL;
219 } 108 }
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 } 109 }
227 SkASSERT(target.fAddr != NULL); 110 SkASSERT(pixels != NULL);
228 SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId); 111 SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId);
229 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), NULL, &target) ; 112 if (!fImageGenerator->getPixels(fInfo, pixels, fRowBytes)) {
230 if (fErrorInDecoding) { 113 fErrorInDecoding = true;
231 fImageCache->throwAwayCache(fCacheId); 114 fImageCache->throwAwayCache(fCacheId);
232 fCacheId = SkImageCache::UNINITIALIZED_ID; 115 fCacheId = SkImageCache::UNINITIALIZED_ID;
233 return NULL; 116 return NULL;
234 } 117 }
235 // Upon success, store fRowBytes so it can be used in case pinCache later re turns purged memory. 118 return pixels;
236 fRowBytes = target.fRowBytes;
237 return target.fAddr;
238 } 119 }
239 120
240 void SkLazyPixelRef::onUnlockPixels() { 121 void SkLazyPixelRef::onUnlockPixels() {
241 if (fErrorInDecoding) { 122 if (fErrorInDecoding) {
242 return; 123 return;
243 } 124 }
244 if (NULL == fImageCache) { 125 SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId);
245 // onUnlockPixels() should never be called a second time from 126 if (SkImageCache::UNINITIALIZED_ID != fCacheId) {
246 // PixelRef::Unlock() without calling onLockPixels() first. 127 fImageCache->releaseCache(fCacheId);
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 } 128 }
258 } 129 }
259
260 SkData* SkLazyPixelRef::onRefEncodedData() { 130 SkData* SkLazyPixelRef::onRefEncodedData() {
261 fData->ref(); 131 return fImageGenerator->refEncodedData();
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 } 132 }
280 133
281 bool SkLazyPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { 134 bool SkLazyPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) {
282 SkASSERT(fData != NULL && fData->size() > 0); 135 (void) pow2;
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; 136 SkBitmap tmp;
299 if (!init_from_info(&tmp, info, target.fRowBytes)) { 137 tmp.setConfig(fInfo, fRowBytes);
300 return false; 138 tmp.setPixelRef(this);
301 } 139 return tmp.deepCopyTo(bitmap, tmp.config());
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 } 140 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698