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

Side by Side Diff: src/core/SkPixelRef.cpp

Issue 1074983003: add SkPixmap and external locking to bitmaps (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase Created 5 years, 7 months 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
OLDNEW
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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 // only call me in your constructor, otherwise fLockCount tracking can get 165 // only call me in your constructor, otherwise fLockCount tracking can get
166 // out of sync. 166 // out of sync.
167 fRec.fPixels = pixels; 167 fRec.fPixels = pixels;
168 fRec.fColorTable = ctable; 168 fRec.fColorTable = ctable;
169 fRec.fRowBytes = rowBytes; 169 fRec.fRowBytes = rowBytes;
170 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; 170 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT;
171 fPreLocked = true; 171 fPreLocked = true;
172 #endif 172 #endif
173 } 173 }
174 174
175 bool SkPixelRef::lockPixelsInsideMutex(LockRec* rec) {
176 fMutex->assertHeld();
177
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) {
181 SkASSERT(fRec.isZero());
182
183 LockRec rec;
184 if (!this->onNewLockPixels(&rec)) {
185 fLockCount -= 1; // we return fLockCount unchanged if we fail.
186 return false;
187 }
188 SkASSERT(!rec.isZero()); // else why did onNewLock return true?
189 fRec = rec;
190 }
191 *rec = fRec;
192 return true;
193 }
194
175 bool SkPixelRef::lockPixels(LockRec* rec) { 195 bool SkPixelRef::lockPixels(LockRec* rec) {
176 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); 196 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
177 197
178 if (!fPreLocked) { 198 if (fPreLocked) {
199 *rec = fRec;
200 return true;
201 } else {
179 TRACE_EVENT_BEGIN0("skia", "SkPixelRef::lockPixelsMutex"); 202 TRACE_EVENT_BEGIN0("skia", "SkPixelRef::lockPixelsMutex");
180 SkAutoMutexAcquire ac(*fMutex); 203 SkAutoMutexAcquire ac(*fMutex);
181 TRACE_EVENT_END0("skia", "SkPixelRef::lockPixelsMutex"); 204 TRACE_EVENT_END0("skia", "SkPixelRef::lockPixelsMutex");
205 SkDEBUGCODE(int oldCount = fLockCount;)
206 bool success = this->lockPixelsInsideMutex(rec);
207 // lockPixelsInsideMutex only increments the count if it succeeds.
208 SkASSERT(oldCount + (int)success == fLockCount);
182 209
183 if (1 == ++fLockCount) { 210 if (!success) {
184 SkASSERT(fRec.isZero()); 211 // For compatibility with SkBitmap calling lockPixels, we still want to increment
185 212 // fLockCount even if we failed. If we updated SkBitmap we could rem ove this oddity.
186 LockRec rec; 213 fLockCount += 1;
187 if (!this->onNewLockPixels(&rec)) {
188 return false;
189 }
190 SkASSERT(!rec.isZero()); // else why did onNewLock return true?
191 fRec = rec;
192 } 214 }
215 return success;
193 } 216 }
194 *rec = fRec;
195 return true;
196 } 217 }
197 218
198 bool SkPixelRef::lockPixels() { 219 bool SkPixelRef::lockPixels() {
199 LockRec rec; 220 LockRec rec;
200 return this->lockPixels(&rec); 221 return this->lockPixels(&rec);
201 } 222 }
202 223
203 void SkPixelRef::unlockPixels() { 224 void SkPixelRef::unlockPixels() {
204 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); 225 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
205 226
206 if (!fPreLocked) { 227 if (!fPreLocked) {
207 SkAutoMutexAcquire ac(*fMutex); 228 SkAutoMutexAcquire ac(*fMutex);
208 229
209 SkASSERT(fLockCount > 0); 230 SkASSERT(fLockCount > 0);
210 if (0 == --fLockCount) { 231 if (0 == --fLockCount) {
211 // don't call onUnlockPixels unless onLockPixels succeeded 232 // don't call onUnlockPixels unless onLockPixels succeeded
212 if (fRec.fPixels) { 233 if (fRec.fPixels) {
213 this->onUnlockPixels(); 234 this->onUnlockPixels();
214 fRec.zero(); 235 fRec.zero();
215 } else { 236 } else {
216 SkASSERT(fRec.isZero()); 237 SkASSERT(fRec.isZero());
217 } 238 }
218 } 239 }
219 } 240 }
220 } 241 }
221 242
243 bool SkPixelRef::requestLock(const LockRequest& request, LockResult* result) {
244 SkASSERT(result);
245 if (request.fSize.isEmpty()) {
246 return false;
247 }
248
249 if (fPreLocked) {
250 result->fUnlockProc = NULL;
251 result->fUnlockContext = NULL;
252 result->fCTable = fRec.fColorTable;
253 result->fPixels = fRec.fPixels;
254 result->fRowBytes = fRec.fRowBytes;
255 result->fSize.set(fInfo.width(), fInfo.height());
256 return true;
257 } else {
258 SkAutoMutexAcquire ac(*fMutex);
259 return this->onRequestLock(request, result);
260 }
261 }
262
222 bool SkPixelRef::lockPixelsAreWritable() const { 263 bool SkPixelRef::lockPixelsAreWritable() const {
223 return this->onLockPixelsAreWritable(); 264 return this->onLockPixelsAreWritable();
224 } 265 }
225 266
226 bool SkPixelRef::onLockPixelsAreWritable() const { 267 bool SkPixelRef::onLockPixelsAreWritable() const {
227 return true; 268 return true;
228 } 269 }
229 270
230 uint32_t SkPixelRef::getGenerationID() const { 271 uint32_t SkPixelRef::getGenerationID() const {
231 uint32_t id = fTaggedGenID.load(); 272 uint32_t id = fTaggedGenID.load();
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 } 325 }
285 326
286 void SkPixelRef::setImmutable() { 327 void SkPixelRef::setImmutable() {
287 fIsImmutable = true; 328 fIsImmutable = true;
288 } 329 }
289 330
290 bool SkPixelRef::readPixels(SkBitmap* dst, const SkIRect* subset) { 331 bool SkPixelRef::readPixels(SkBitmap* dst, const SkIRect* subset) {
291 return this->onReadPixels(dst, subset); 332 return this->onReadPixels(dst, subset);
292 } 333 }
293 334
335 //////////////////////////////////////////////////////////////////////////////// ///////////////////
336
294 bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { 337 bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {
295 return false; 338 return false;
296 } 339 }
297 340
298 SkData* SkPixelRef::onRefEncodedData() { 341 SkData* SkPixelRef::onRefEncodedData() {
299 return NULL; 342 return NULL;
300 } 343 }
301 344
302 bool SkPixelRef::onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBy tes[3], 345 bool SkPixelRef::onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBy tes[3],
303 SkYUVColorSpace* colorSpace) { 346 SkYUVColorSpace* colorSpace) {
304 return false; 347 return false;
305 } 348 }
306 349
307 size_t SkPixelRef::getAllocatedSizeInBytes() const { 350 size_t SkPixelRef::getAllocatedSizeInBytes() const {
308 return 0; 351 return 0;
309 } 352 }
310 353
354 static void unlock_legacy_result(void* ctx) {
355 SkPixelRef* pr = (SkPixelRef*)ctx;
356 pr->unlockPixels();
357 pr->unref(); // balancing the Ref in onRequestLoc
scroggo 2015/05/22 15:04:14 onRequestLock*
358 }
359
360 bool SkPixelRef::onRequestLock(const LockRequest& request, LockResult* result) {
361 LockRec rec;
362 if (!this->lockPixelsInsideMutex(&rec)) {
363 return false;
364 }
365
366 result->fUnlockProc = unlock_legacy_result;
367 result->fUnlockContext = SkRef(this); // this is balanced in our fUnlockPr oc
368 result->fCTable = rec.fColorTable;
369 result->fPixels = rec.fPixels;
370 result->fRowBytes = rec.fRowBytes;
371 result->fSize.set(fInfo.width(), fInfo.height());
372 return true;
373 }
OLDNEW
« include/core/SkPixmap.h ('K') | « src/core/SkDraw.cpp ('k') | src/core/SkPixmap.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698