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 "SkPixelRef.h" | 9 #include "SkPixelRef.h" |
10 #include "SkThread.h" | 10 #include "SkThread.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 void* pixels, const SkColorTable* ctabl
e, SkColorType ct) { |
| 164 SkASSERT(pixels); |
| 165 if (kIndex_8_SkColorType == ct) { |
| 166 SkASSERT(ctable); |
| 167 } else { |
| 168 SkASSERT(NULL == ctable); |
| 169 } |
| 170 } |
| 171 |
163 void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctabl
e) { | 172 void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctabl
e) { |
164 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED | 173 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED |
| 174 validate_pixels_ctable(pixels, ctable, fInfo.colorType()); |
165 // only call me in your constructor, otherwise fLockCount tracking can get | 175 // only call me in your constructor, otherwise fLockCount tracking can get |
166 // out of sync. | 176 // out of sync. |
167 fRec.fPixels = pixels; | 177 fRec.fPixels = pixels; |
168 fRec.fColorTable = ctable; | 178 fRec.fColorTable = ctable; |
169 fRec.fRowBytes = rowBytes; | 179 fRec.fRowBytes = rowBytes; |
170 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; | 180 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; |
171 fPreLocked = true; | 181 fPreLocked = true; |
172 #endif | 182 #endif |
173 } | 183 } |
174 | 184 |
175 bool SkPixelRef::lockPixelsInsideMutex(LockRec* rec) { | 185 // Increments fLockCount only on success |
| 186 bool SkPixelRef::lockPixelsInsideMutex() { |
176 fMutex->assertHeld(); | 187 fMutex->assertHeld(); |
177 | 188 |
178 // For historical reasons, we always inc fLockCount, even if we return false
. | |
179 // It would be nice to change this (it seems), and only inc if we actually s
ucceed... | |
180 if (1 == ++fLockCount) { | 189 if (1 == ++fLockCount) { |
181 SkASSERT(fRec.isZero()); | 190 SkASSERT(fRec.isZero()); |
182 | 191 if (!this->onNewLockPixels(&fRec)) { |
183 LockRec rec; | 192 fRec.zero(); |
184 if (!this->onNewLockPixels(&rec)) { | |
185 fLockCount -= 1; // we return fLockCount unchanged if we fail. | 193 fLockCount -= 1; // we return fLockCount unchanged if we fail. |
186 return false; | 194 return false; |
187 } | 195 } |
188 SkASSERT(!rec.isZero()); // else why did onNewLock return true? | |
189 fRec = rec; | |
190 } | 196 } |
191 *rec = fRec; | 197 validate_pixels_ctable(fRec.fPixels, fRec.fColorTable, fInfo.colorType()); |
192 return true; | 198 return true; |
193 } | 199 } |
194 | 200 |
195 bool SkPixelRef::lockPixels(LockRec* rec) { | 201 // For historical reasons, we always inc fLockCount, even if we return false. |
| 202 // It would be nice to change this (it seems), and only inc if we actually succe
ed... |
| 203 bool SkPixelRef::lockPixels() { |
196 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 204 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
197 | 205 |
198 if (fPreLocked) { | 206 if (!fPreLocked) { |
199 *rec = fRec; | |
200 return true; | |
201 } else { | |
202 TRACE_EVENT_BEGIN0("skia", "SkPixelRef::lockPixelsMutex"); | 207 TRACE_EVENT_BEGIN0("skia", "SkPixelRef::lockPixelsMutex"); |
203 SkAutoMutexAcquire ac(*fMutex); | 208 SkAutoMutexAcquire ac(*fMutex); |
204 TRACE_EVENT_END0("skia", "SkPixelRef::lockPixelsMutex"); | 209 TRACE_EVENT_END0("skia", "SkPixelRef::lockPixelsMutex"); |
205 SkDEBUGCODE(int oldCount = fLockCount;) | 210 SkDEBUGCODE(int oldCount = fLockCount;) |
206 bool success = this->lockPixelsInsideMutex(rec); | 211 bool success = this->lockPixelsInsideMutex(); |
207 // lockPixelsInsideMutex only increments the count if it succeeds. | 212 // lockPixelsInsideMutex only increments the count if it succeeds. |
208 SkASSERT(oldCount + (int)success == fLockCount); | 213 SkASSERT(oldCount + (int)success == fLockCount); |
209 | 214 |
210 if (!success) { | 215 if (!success) { |
211 // For compatibility with SkBitmap calling lockPixels, we still want
to increment | 216 // For compatibility with SkBitmap calling lockPixels, we still want
to increment |
212 // fLockCount even if we failed. If we updated SkBitmap we could rem
ove this oddity. | 217 // fLockCount even if we failed. If we updated SkBitmap we could rem
ove this oddity. |
213 fLockCount += 1; | 218 fLockCount += 1; |
| 219 return false; |
214 } | 220 } |
215 return success; | |
216 } | 221 } |
| 222 validate_pixels_ctable(fRec.fPixels, fRec.fColorTable, fInfo.colorType()); |
| 223 return true; |
217 } | 224 } |
218 | 225 |
219 bool SkPixelRef::lockPixels() { | 226 bool SkPixelRef::lockPixels(LockRec* rec) { |
220 LockRec rec; | 227 if (this->lockPixels()) { |
221 return this->lockPixels(&rec); | 228 *rec = fRec; |
| 229 return true; |
| 230 } |
| 231 return false; |
222 } | 232 } |
223 | 233 |
224 void SkPixelRef::unlockPixels() { | 234 void SkPixelRef::unlockPixels() { |
225 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 235 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
226 | 236 |
227 if (!fPreLocked) { | 237 if (!fPreLocked) { |
228 SkAutoMutexAcquire ac(*fMutex); | 238 SkAutoMutexAcquire ac(*fMutex); |
229 | 239 |
230 SkASSERT(fLockCount > 0); | 240 SkASSERT(fLockCount > 0); |
231 if (0 == --fLockCount) { | 241 if (0 == --fLockCount) { |
(...skipping 14 matching lines...) Expand all Loading... |
246 return false; | 256 return false; |
247 } | 257 } |
248 | 258 |
249 if (fPreLocked) { | 259 if (fPreLocked) { |
250 result->fUnlockProc = NULL; | 260 result->fUnlockProc = NULL; |
251 result->fUnlockContext = NULL; | 261 result->fUnlockContext = NULL; |
252 result->fCTable = fRec.fColorTable; | 262 result->fCTable = fRec.fColorTable; |
253 result->fPixels = fRec.fPixels; | 263 result->fPixels = fRec.fPixels; |
254 result->fRowBytes = fRec.fRowBytes; | 264 result->fRowBytes = fRec.fRowBytes; |
255 result->fSize.set(fInfo.width(), fInfo.height()); | 265 result->fSize.set(fInfo.width(), fInfo.height()); |
256 return true; | |
257 } else { | 266 } else { |
258 SkAutoMutexAcquire ac(*fMutex); | 267 SkAutoMutexAcquire ac(*fMutex); |
259 return this->onRequestLock(request, result); | 268 if (!this->onRequestLock(request, result)) { |
| 269 return false; |
| 270 } |
260 } | 271 } |
| 272 validate_pixels_ctable(result->fPixels, result->fCTable, fInfo.colorType()); |
| 273 return true; |
261 } | 274 } |
262 | 275 |
263 bool SkPixelRef::lockPixelsAreWritable() const { | 276 bool SkPixelRef::lockPixelsAreWritable() const { |
264 return this->onLockPixelsAreWritable(); | 277 return this->onLockPixelsAreWritable(); |
265 } | 278 } |
266 | 279 |
267 bool SkPixelRef::onLockPixelsAreWritable() const { | 280 bool SkPixelRef::onLockPixelsAreWritable() const { |
268 return true; | 281 return true; |
269 } | 282 } |
270 | 283 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 return 0; | 364 return 0; |
352 } | 365 } |
353 | 366 |
354 static void unlock_legacy_result(void* ctx) { | 367 static void unlock_legacy_result(void* ctx) { |
355 SkPixelRef* pr = (SkPixelRef*)ctx; | 368 SkPixelRef* pr = (SkPixelRef*)ctx; |
356 pr->unlockPixels(); | 369 pr->unlockPixels(); |
357 pr->unref(); // balancing the Ref in onRequestLoc | 370 pr->unref(); // balancing the Ref in onRequestLoc |
358 } | 371 } |
359 | 372 |
360 bool SkPixelRef::onRequestLock(const LockRequest& request, LockResult* result) { | 373 bool SkPixelRef::onRequestLock(const LockRequest& request, LockResult* result) { |
361 LockRec rec; | 374 if (!this->lockPixelsInsideMutex()) { |
362 if (!this->lockPixelsInsideMutex(&rec)) { | |
363 return false; | 375 return false; |
364 } | 376 } |
365 | 377 |
366 result->fUnlockProc = unlock_legacy_result; | 378 result->fUnlockProc = unlock_legacy_result; |
367 result->fUnlockContext = SkRef(this); // this is balanced in our fUnlockPr
oc | 379 result->fUnlockContext = SkRef(this); // this is balanced in our fUnlockPr
oc |
368 result->fCTable = rec.fColorTable; | 380 result->fCTable = fRec.fColorTable; |
369 result->fPixels = rec.fPixels; | 381 result->fPixels = fRec.fPixels; |
370 result->fRowBytes = rec.fRowBytes; | 382 result->fRowBytes = fRec.fRowBytes; |
371 result->fSize.set(fInfo.width(), fInfo.height()); | 383 result->fSize.set(fInfo.width(), fInfo.height()); |
372 return true; | 384 return true; |
373 } | 385 } |
OLD | NEW |