OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "SkBitmapCache.h" | 8 #include "SkBitmapCache.h" |
9 #include "SkMutex.h" | 9 #include "SkMutex.h" |
10 #include "SkPixelRef.h" | 10 #include "SkPixelRef.h" |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 // Neither ID is unique any more. | 153 // Neither ID is unique any more. |
154 // (These & ~1u are actually redundant. that.getGenerationID() just did it
for us.) | 154 // (These & ~1u are actually redundant. that.getGenerationID() just did it
for us.) |
155 this->fTaggedGenID.store(genID & ~1u); | 155 this->fTaggedGenID.store(genID & ~1u); |
156 that. fTaggedGenID.store(genID & ~1u); | 156 that. fTaggedGenID.store(genID & ~1u); |
157 | 157 |
158 // This method isn't threadsafe, so these asserts should be fine. | 158 // This method isn't threadsafe, so these asserts should be fine. |
159 SkASSERT(!this->genIDIsUnique()); | 159 SkASSERT(!this->genIDIsUnique()); |
160 SkASSERT(!that. genIDIsUnique()); | 160 SkASSERT(!that. genIDIsUnique()); |
161 } | 161 } |
162 | 162 |
163 static void validate_pixels_ctable(const SkImageInfo& info, const void* pixels, | 163 static void validate_pixels_ctable(const SkImageInfo& info, const SkColorTable*
ctable) { |
164 const SkColorTable* ctable) { | |
165 if (info.isEmpty()) { | 164 if (info.isEmpty()) { |
166 return; // can't require pixels if the dimensions are empty | 165 return; // can't require ctable if the dimensions are empty |
167 } | 166 } |
168 SkASSERT(pixels); | |
169 if (kIndex_8_SkColorType == info.colorType()) { | 167 if (kIndex_8_SkColorType == info.colorType()) { |
170 SkASSERT(ctable); | 168 SkASSERT(ctable); |
171 } else { | 169 } else { |
172 SkASSERT(NULL == ctable); | 170 SkASSERT(NULL == ctable); |
173 } | 171 } |
174 } | 172 } |
175 | 173 |
176 void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctabl
e) { | 174 void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctabl
e) { |
177 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED | 175 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED |
178 validate_pixels_ctable(fInfo, pixels, ctable); | 176 SkASSERT(pixels); |
| 177 validate_pixels_ctable(fInfo, ctable); |
179 // only call me in your constructor, otherwise fLockCount tracking can get | 178 // only call me in your constructor, otherwise fLockCount tracking can get |
180 // out of sync. | 179 // out of sync. |
181 fRec.fPixels = pixels; | 180 fRec.fPixels = pixels; |
182 fRec.fColorTable = ctable; | 181 fRec.fColorTable = ctable; |
183 fRec.fRowBytes = rowBytes; | 182 fRec.fRowBytes = rowBytes; |
184 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; | 183 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; |
185 fPreLocked = true; | 184 fPreLocked = true; |
186 #endif | 185 #endif |
187 } | 186 } |
188 | 187 |
189 // Increments fLockCount only on success | 188 // Increments fLockCount only on success |
190 bool SkPixelRef::lockPixelsInsideMutex() { | 189 bool SkPixelRef::lockPixelsInsideMutex() { |
191 fMutex->assertHeld(); | 190 fMutex->assertHeld(); |
192 | 191 |
193 if (1 == ++fLockCount) { | 192 if (1 == ++fLockCount) { |
194 SkASSERT(fRec.isZero()); | 193 SkASSERT(fRec.isZero()); |
195 if (!this->onNewLockPixels(&fRec)) { | 194 if (!this->onNewLockPixels(&fRec)) { |
196 fRec.zero(); | 195 fRec.zero(); |
197 fLockCount -= 1; // we return fLockCount unchanged if we fail. | 196 fLockCount -= 1; // we return fLockCount unchanged if we fail. |
198 return false; | 197 return false; |
199 } | 198 } |
200 } | 199 } |
201 validate_pixels_ctable(fInfo, fRec.fPixels, fRec.fColorTable); | 200 if (fRec.fPixels) { |
202 return fRec.fPixels != NULL; | 201 validate_pixels_ctable(fInfo, fRec.fColorTable); |
| 202 return true; |
| 203 } |
| 204 return false; |
203 } | 205 } |
204 | 206 |
205 // For historical reasons, we always inc fLockCount, even if we return false. | 207 // For historical reasons, we always inc fLockCount, even if we return false. |
206 // It would be nice to change this (it seems), and only inc if we actually succe
ed... | 208 // It would be nice to change this (it seems), and only inc if we actually succe
ed... |
207 bool SkPixelRef::lockPixels() { | 209 bool SkPixelRef::lockPixels() { |
208 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 210 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
209 | 211 |
210 if (!fPreLocked) { | 212 if (!fPreLocked) { |
211 TRACE_EVENT_BEGIN0("skia", "SkPixelRef::lockPixelsMutex"); | 213 TRACE_EVENT_BEGIN0("skia", "SkPixelRef::lockPixelsMutex"); |
212 SkAutoMutexAcquire ac(*fMutex); | 214 SkAutoMutexAcquire ac(*fMutex); |
213 TRACE_EVENT_END0("skia", "SkPixelRef::lockPixelsMutex"); | 215 TRACE_EVENT_END0("skia", "SkPixelRef::lockPixelsMutex"); |
214 SkDEBUGCODE(int oldCount = fLockCount;) | 216 SkDEBUGCODE(int oldCount = fLockCount;) |
215 bool success = this->lockPixelsInsideMutex(); | 217 bool success = this->lockPixelsInsideMutex(); |
216 // lockPixelsInsideMutex only increments the count if it succeeds. | 218 // lockPixelsInsideMutex only increments the count if it succeeds. |
217 SkASSERT(oldCount + (int)success == fLockCount); | 219 SkASSERT(oldCount + (int)success == fLockCount); |
218 | 220 |
219 if (!success) { | 221 if (!success) { |
220 // For compatibility with SkBitmap calling lockPixels, we still want
to increment | 222 // For compatibility with SkBitmap calling lockPixels, we still want
to increment |
221 // fLockCount even if we failed. If we updated SkBitmap we could rem
ove this oddity. | 223 // fLockCount even if we failed. If we updated SkBitmap we could rem
ove this oddity. |
222 fLockCount += 1; | 224 fLockCount += 1; |
223 return false; | 225 return false; |
224 } | 226 } |
225 } | 227 } |
226 validate_pixels_ctable(fInfo, fRec.fPixels, fRec.fColorTable); | 228 if (fRec.fPixels) { |
227 return fRec.fPixels != NULL; | 229 validate_pixels_ctable(fInfo, fRec.fColorTable); |
| 230 return true; |
| 231 } |
| 232 return false; |
228 } | 233 } |
229 | 234 |
230 bool SkPixelRef::lockPixels(LockRec* rec) { | 235 bool SkPixelRef::lockPixels(LockRec* rec) { |
231 if (this->lockPixels()) { | 236 if (this->lockPixels()) { |
232 *rec = fRec; | 237 *rec = fRec; |
233 return true; | 238 return true; |
234 } | 239 } |
235 return false; | 240 return false; |
236 } | 241 } |
237 | 242 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 result->fCTable = fRec.fColorTable; | 275 result->fCTable = fRec.fColorTable; |
271 result->fPixels = fRec.fPixels; | 276 result->fPixels = fRec.fPixels; |
272 result->fRowBytes = fRec.fRowBytes; | 277 result->fRowBytes = fRec.fRowBytes; |
273 result->fSize.set(fInfo.width(), fInfo.height()); | 278 result->fSize.set(fInfo.width(), fInfo.height()); |
274 } else { | 279 } else { |
275 SkAutoMutexAcquire ac(*fMutex); | 280 SkAutoMutexAcquire ac(*fMutex); |
276 if (!this->onRequestLock(request, result)) { | 281 if (!this->onRequestLock(request, result)) { |
277 return false; | 282 return false; |
278 } | 283 } |
279 } | 284 } |
280 validate_pixels_ctable(fInfo, result->fPixels, result->fCTable); | 285 if (result->fPixels) { |
281 return result->fPixels != NULL; | 286 validate_pixels_ctable(fInfo, result->fCTable); |
| 287 return true; |
| 288 } |
| 289 return false; |
282 } | 290 } |
283 | 291 |
284 bool SkPixelRef::lockPixelsAreWritable() const { | 292 bool SkPixelRef::lockPixelsAreWritable() const { |
285 return this->onLockPixelsAreWritable(); | 293 return this->onLockPixelsAreWritable(); |
286 } | 294 } |
287 | 295 |
288 bool SkPixelRef::onLockPixelsAreWritable() const { | 296 bool SkPixelRef::onLockPixelsAreWritable() const { |
289 return true; | 297 return true; |
290 } | 298 } |
291 | 299 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 } | 395 } |
388 | 396 |
389 result->fUnlockProc = unlock_legacy_result; | 397 result->fUnlockProc = unlock_legacy_result; |
390 result->fUnlockContext = SkRef(this); // this is balanced in our fUnlockPr
oc | 398 result->fUnlockContext = SkRef(this); // this is balanced in our fUnlockPr
oc |
391 result->fCTable = fRec.fColorTable; | 399 result->fCTable = fRec.fColorTable; |
392 result->fPixels = fRec.fPixels; | 400 result->fPixels = fRec.fPixels; |
393 result->fRowBytes = fRec.fRowBytes; | 401 result->fRowBytes = fRec.fRowBytes; |
394 result->fSize.set(fInfo.width(), fInfo.height()); | 402 result->fSize.set(fInfo.width(), fInfo.height()); |
395 return true; | 403 return true; |
396 } | 404 } |
OLD | NEW |