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) |
scroggo
2013/10/23 15:27:49
Could you add a comment or an assert that 0 is an
hal.canary
2013/10/23 16:11:52
Done.
| |
28 , fRowBytes(0) { | 30 , fRowBytes(0) |
31 , fAllocator(NULL) { | |
29 SkASSERT(fDecodeProc != NULL); | 32 SkASSERT(fDecodeProc != NULL); |
30 if (NULL == data) { | 33 if (NULL == data) { |
31 fData = SkData::NewEmpty(); | 34 fData = SkData::NewEmpty(); |
32 fErrorInDecoding = true; | 35 fErrorInDecoding = true; |
33 } else { | 36 } else { |
34 fData = data; | 37 fData = data; |
35 fData->ref(); | 38 fData->ref(); |
36 fErrorInDecoding = data->size() == 0; | 39 fErrorInDecoding = data->size() == 0; |
37 } | 40 } |
38 SkASSERT(cache != NULL); | 41 if (cache != NULL) { |
mtklein
2013/10/23 18:50:14
I'd slightly prefer it if you started calling it f
hal.canary
2013/10/23 22:57:41
Done.
| |
39 cache->ref(); | 42 cache->ref(); |
scroggo
2013/10/23 15:27:49
nit: 4 spaces
You could also use SkSafeRef, which
hal.canary
2013/10/23 16:11:52
Done.
| |
43 } // else use global SkScaledImageCache | |
40 | 44 |
41 // mark as uninitialized -- all fields are -1 | 45 // mark as uninitialized -- all fields are -1 |
42 memset(&fLazilyCachedInfo, 0xFF, sizeof(fLazilyCachedInfo)); | 46 memset(&fLazilyCachedInfo, 0xFF, sizeof(fLazilyCachedInfo)); |
43 | 47 |
44 // Since this pixel ref bases its data on encoded data, it should never chan ge. | 48 // Since this pixel ref bases its data on encoded data, it should never chan ge. |
45 this->setImmutable(); | 49 this->setImmutable(); |
46 } | 50 } |
47 | 51 |
48 SkLazyPixelRef::~SkLazyPixelRef() { | 52 SkLazyPixelRef::~SkLazyPixelRef() { |
49 SkASSERT(fData != NULL); | 53 SkASSERT(fData != NULL); |
50 fData->unref(); | 54 fData->unref(); |
55 if (fImageCache == NULL) { | |
scroggo
2013/10/23 15:27:49
nit: NULL == fImageCache
hal.canary
2013/10/23 16:11:52
Done.
| |
56 if (fCacheId != 0) { | |
57 SkScaledImageCache::Unlock((SkScaledImageCache::ID *)(fCacheId)); | |
mtklein
2013/10/23 18:50:14
This sort of cast makes me nervous. Can we make f
hal.canary
2013/10/23 22:57:41
Can you show me what that would look like? I don'
mtklein
2013/10/24 16:52:12
In the header, replace this:
// fCacheId is a (S
| |
58 } | |
59 return; | |
60 } | |
51 SkASSERT(fImageCache); | 61 SkASSERT(fImageCache); |
52 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { | 62 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { |
53 fImageCache->throwAwayCache(fCacheId); | 63 fImageCache->throwAwayCache(fCacheId); |
54 } | 64 } |
55 fImageCache->unref(); | 65 fImageCache->unref(); |
56 } | 66 } |
57 | 67 |
58 static size_t ComputeMinRowBytesAndSize(const SkImage::Info& info, size_t* rowBy tes) { | 68 static size_t ComputeMinRowBytesAndSize(const SkImage::Info& info, size_t* rowBy tes) { |
59 *rowBytes = SkImageMinRowBytes(info); | 69 *rowBytes = SkImageMinRowBytes(info); |
60 | 70 |
(...skipping 11 matching lines...) Expand all Loading... | |
72 SkImage::Info info; | 82 SkImage::Info info; |
73 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL L); | 83 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL L); |
74 if (fErrorInDecoding) { | 84 if (fErrorInDecoding) { |
75 return NULL; | 85 return NULL; |
76 } | 86 } |
77 fLazilyCachedInfo = info; | 87 fLazilyCachedInfo = info; |
78 } | 88 } |
79 return &fLazilyCachedInfo; | 89 return &fLazilyCachedInfo; |
80 } | 90 } |
81 | 91 |
92 /** | |
93 Returns bitmap->getPixels() on success; NULL on failure */ | |
94 static void * decode_into_bitmap(SkImage::Info * info, | |
mtklein
2013/10/23 18:50:14
These *'s look wishy washy! Pick a side! (Choose
hal.canary
2013/10/23 22:57:41
Done.
| |
95 SkBitmapFactory::DecodeProc decodeProc, | |
96 size_t * rowBytes, | |
97 SkData * data, | |
98 SkBitmap::Allocator * allocator, | |
99 SkBitmap * bm) { | |
scroggo
2013/10/23 15:27:49
Maybe assert that none of these parameters are NUL
hal.canary
2013/10/23 16:11:52
Done.
| |
100 if (!(bm->setConfig(SkImageInfoToBitmapConfig(*info), info->fWidth, | |
101 info->fHeight, *rowBytes, info->fAlphaType) | |
102 && bm->allocPixels(allocator, NULL))) { | |
103 return NULL; | |
104 } | |
105 SkBitmapFactory::Target target; | |
106 target.fAddr = bm->getPixels(); | |
107 target.fRowBytes = bm->rowBytes(); | |
108 *rowBytes = target.fRowBytes; | |
109 if (!decodeProc(data->data(), data->size(), info, &target)) { | |
110 return NULL; | |
111 } | |
112 return target.fAddr; | |
113 } | |
114 | |
82 void* SkLazyPixelRef::onLockPixels(SkColorTable**) { | 115 void* SkLazyPixelRef::onLockPixels(SkColorTable**) { |
83 if (fErrorInDecoding) { | 116 if (fErrorInDecoding) { |
84 return NULL; | 117 return NULL; |
85 } | 118 } |
119 if (NULL == fImageCache) { | |
mtklein
2013/10/23 18:50:14
Usually when the nesting gets this deep I think it
| |
120 SkBitmap bitmap; | |
121 if (fLazilyCachedInfo.fWidth > 0) { // fInfo already populated | |
mtklein
2013/10/23 18:50:14
Sometimes a little private inline method can be he
mtklein
2013/10/23 18:50:14
We've got about three levels of code jammed into o
| |
122 fCacheId = (intptr_t)(SkScaledImageCache::FindAndLock( | |
123 this->getGenerationID(), | |
124 fLazilyCachedInfo.fWidth, | |
125 fLazilyCachedInfo.fHeight, | |
126 SK_Scalar1, SK_Scalar1, &bitmap)); | |
127 if (fCacheId != 0) { | |
128 return bitmap.getPixels(); | |
129 } // else cache has been purged, must re-decode. | |
130 } else { // first time through this code, must populate info | |
131 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), | |
132 &fLazilyCachedInfo, NULL); | |
133 if (fErrorInDecoding) { | |
134 return NULL; | |
135 } | |
136 } | |
137 SkASSERT(fLazilyCachedInfo.fWidth > 0); | |
138 void * ptr = decode_into_bitmap(&fLazilyCachedInfo, fDecodeProc, | |
139 &fRowBytes, fData, fAllocator, &bitmap); | |
140 if (NULL == ptr) { | |
141 fErrorInDecoding = true; | |
142 return NULL; | |
143 } | |
144 fCacheId = (intptr_t)(SkScaledImageCache::AddAndLock( | |
145 this->getGenerationID(), | |
146 fLazilyCachedInfo.fWidth, | |
147 fLazilyCachedInfo.fHeight, | |
148 SK_Scalar1, SK_Scalar1, bitmap)); | |
149 return ptr; | |
150 } | |
151 // else use fImageCache | |
86 SkBitmapFactory::Target target; | 152 SkBitmapFactory::Target target; |
87 // Check to see if the pixels still exist in the cache. | 153 // Check to see if the pixels still exist in the cache. |
88 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { | 154 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { |
89 target.fAddr = NULL; | 155 target.fAddr = NULL; |
90 } else { | 156 } else { |
91 SkImageCache::DataStatus status; | 157 SkImageCache::DataStatus status; |
92 target.fAddr = fImageCache->pinCache(fCacheId, &status); | 158 target.fAddr = fImageCache->pinCache(fCacheId, &status); |
93 if (target.fAddr == NULL) { | 159 if (target.fAddr == NULL) { |
94 fCacheId = SkImageCache::UNINITIALIZED_ID; | 160 fCacheId = SkImageCache::UNINITIALIZED_ID; |
95 } else { | 161 } else { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 fImageCache->throwAwayCache(fCacheId); | 203 fImageCache->throwAwayCache(fCacheId); |
138 fCacheId = SkImageCache::UNINITIALIZED_ID; | 204 fCacheId = SkImageCache::UNINITIALIZED_ID; |
139 return NULL; | 205 return NULL; |
140 } | 206 } |
141 // Upon success, store fRowBytes so it can be used in case pinCache later re turns purged memory. | 207 // Upon success, store fRowBytes so it can be used in case pinCache later re turns purged memory. |
142 fRowBytes = target.fRowBytes; | 208 fRowBytes = target.fRowBytes; |
143 return target.fAddr; | 209 return target.fAddr; |
144 } | 210 } |
145 | 211 |
146 void SkLazyPixelRef::onUnlockPixels() { | 212 void SkLazyPixelRef::onUnlockPixels() { |
147 if (fErrorInDecoding) { | 213 if (fErrorInDecoding || (0 == fCacheId)) { |
148 return; | 214 return; |
149 } | 215 } |
150 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { | 216 if (NULL == fImageCache) { |
mtklein
2013/10/23 18:50:14
This guy is small enough that even I'd probably le
| |
217 SkScaledImageCache::Unlock((SkScaledImageCache::ID *)(fCacheId)); | |
218 fCacheId = 0; | |
219 return; | |
220 } else { // use fImageCache | |
151 fImageCache->releaseCache(fCacheId); | 221 fImageCache->releaseCache(fCacheId); |
152 } | 222 } |
153 } | 223 } |
154 | 224 |
155 SkData* SkLazyPixelRef::onRefEncodedData() { | 225 SkData* SkLazyPixelRef::onRefEncodedData() { |
156 fData->ref(); | 226 fData->ref(); |
157 return fData; | 227 return fData; |
158 } | 228 } |
159 | 229 |
160 #include "SkImagePriv.h" | 230 #include "SkImagePriv.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 | 269 |
200 target.fAddr = tmp.getPixels(); | 270 target.fAddr = tmp.getPixels(); |
201 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target ); | 271 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target ); |
202 if (fErrorInDecoding) { | 272 if (fErrorInDecoding) { |
203 return false; | 273 return false; |
204 } | 274 } |
205 | 275 |
206 *bitmap = tmp; | 276 *bitmap = tmp; |
207 return true; | 277 return true; |
208 } | 278 } |
OLD | NEW |