Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkPixelRef_DEFINED | 10 #ifndef SkPixelRef_DEFINED |
| 11 #define SkPixelRef_DEFINED | 11 #define SkPixelRef_DEFINED |
| 12 | 12 |
| 13 #include "SkBitmap.h" | 13 #include "SkBitmap.h" |
| 14 #include "SkRefCnt.h" | 14 #include "SkRefCnt.h" |
| 15 #include "SkString.h" | 15 #include "SkString.h" |
| 16 #include "SkFlattenable.h" | 16 #include "SkFlattenable.h" |
| 17 #include "SkImageInfo.h" | |
| 17 #include "SkTDArray.h" | 18 #include "SkTDArray.h" |
| 18 | 19 |
| 20 #define SK_SUPPORT_LEGACY_ONLOCKPIXELS | |
| 21 | |
| 19 #ifdef SK_DEBUG | 22 #ifdef SK_DEBUG |
| 20 /** | 23 /** |
| 21 * Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref | 24 * Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref |
| 22 * subclasses to correctly handle lock/unlock pixels. For performance | 25 * subclasses to correctly handle lock/unlock pixels. For performance |
| 23 * reasons, simple malloc-based subclasses call setPreLocked() to skip | 26 * reasons, simple malloc-based subclasses call setPreLocked() to skip |
| 24 * the overhead of implementing these calls. | 27 * the overhead of implementing these calls. |
| 25 * | 28 * |
| 26 * This build-flag disables that optimization, to add in debugging our | 29 * This build-flag disables that optimization, to add in debugging our |
| 27 * call-sites, to ensure that they correctly balance their calls of | 30 * call-sites, to ensure that they correctly balance their calls of |
| 28 * lock and unlock. | 31 * lock and unlock. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 42 This class is the smart container for pixel memory, and is used with | 45 This class is the smart container for pixel memory, and is used with |
| 43 SkBitmap. A pixelref is installed into a bitmap, and then the bitmap can | 46 SkBitmap. A pixelref is installed into a bitmap, and then the bitmap can |
| 44 access the actual pixel memory by calling lockPixels/unlockPixels. | 47 access the actual pixel memory by calling lockPixels/unlockPixels. |
| 45 | 48 |
| 46 This class can be shared/accessed between multiple threads. | 49 This class can be shared/accessed between multiple threads. |
| 47 */ | 50 */ |
| 48 class SK_API SkPixelRef : public SkFlattenable { | 51 class SK_API SkPixelRef : public SkFlattenable { |
| 49 public: | 52 public: |
| 50 SK_DECLARE_INST_COUNT(SkPixelRef) | 53 SK_DECLARE_INST_COUNT(SkPixelRef) |
| 51 | 54 |
| 52 explicit SkPixelRef(SkBaseMutex* mutex = NULL); | 55 explicit SkPixelRef(const SkImageInfo&, SkBaseMutex* mutex = NULL); |
|
mtklein
2013/12/05 17:43:04
You may want to take a pass over these constructor
reed1
2013/12/05 18:44:14
Doh. Will do.
| |
| 53 virtual ~SkPixelRef(); | 56 virtual ~SkPixelRef(); |
| 54 | 57 |
| 58 const SkImageInfo& info() const { | |
| 59 return fInfo; | |
| 60 } | |
| 61 | |
| 55 /** Return the pixel memory returned from lockPixels, or null if the | 62 /** Return the pixel memory returned from lockPixels, or null if the |
| 56 lockCount is 0. | 63 lockCount is 0. |
| 57 */ | 64 */ |
| 58 void* pixels() const { return fPixels; } | 65 void* pixels() const { return fRec.fPixels; } |
| 59 | 66 |
| 60 /** Return the current colorTable (if any) if pixels are locked, or null. | 67 /** Return the current colorTable (if any) if pixels are locked, or null. |
| 61 */ | 68 */ |
| 62 SkColorTable* colorTable() const { return fColorTable; } | 69 SkColorTable* colorTable() const { return fRec.fColorTable; } |
| 63 | 70 |
| 64 /** | 71 /** |
| 72 * To access the actual pixels of a pixelref, it must be "locked". | |
| 73 * Calling lockPixels returns a LockRec struct (on success). | |
| 74 */ | |
| 75 struct LockRec { | |
| 76 void* fPixels; | |
| 77 SkColorTable* fColorTable; | |
| 78 size_t fRowBytes; | |
| 79 | |
| 80 void zero() { sk_bzero(this, sizeof(*this)); } | |
| 81 }; | |
| 82 | |
| 83 /** | |
| 65 * Returns true if the lockcount > 0 | 84 * Returns true if the lockcount > 0 |
| 66 */ | 85 */ |
| 67 bool isLocked() const { return fLockCount > 0; } | 86 bool isLocked() const { return fLockCount > 0; } |
| 68 | 87 |
| 69 SkDEBUGCODE(int getLockCount() const { return fLockCount; }) | 88 SkDEBUGCODE(int getLockCount() const { return fLockCount; }) |
| 70 | 89 |
| 71 /** Call to access the pixel memory, which is returned. Balance with a call | 90 /** |
| 72 to unlockPixels(). | 91 * Call to access the pixel memory. Return true on success. Balance this |
| 73 */ | 92 * with a call to unlockPixels(). |
| 74 void lockPixels(); | 93 */ |
| 94 bool lockPixels(); | |
| 95 | |
| 96 /** | |
| 97 * Call to access the pixel memory. On success, return true and fill out | |
| 98 * the specified rec. On failure, return false and ignore the rec parameter . | |
| 99 * Balance this with a call to unlockPixels(). | |
| 100 */ | |
| 101 bool lockPixels(LockRec* rec); | |
| 102 | |
| 75 /** Call to balanace a previous call to lockPixels(). Returns the pixels | 103 /** Call to balanace a previous call to lockPixels(). Returns the pixels |
| 76 (or null) after the unlock. NOTE: lock calls can be nested, but the | 104 (or null) after the unlock. NOTE: lock calls can be nested, but the |
| 77 matching number of unlock calls must be made in order to free the | 105 matching number of unlock calls must be made in order to free the |
| 78 memory (if the subclass implements caching/deferred-decoding.) | 106 memory (if the subclass implements caching/deferred-decoding.) |
| 79 */ | 107 */ |
| 80 void unlockPixels(); | 108 void unlockPixels(); |
| 81 | 109 |
| 82 /** | 110 /** |
| 83 * Some bitmaps can return a copy of their pixels for lockPixels(), but | 111 * Some bitmaps can return a copy of their pixels for lockPixels(), but |
| 84 * that copy, if modified, will not be pushed back. These bitmaps should | 112 * that copy, if modified, will not be pushed back. These bitmaps should |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 // This can be used to invalidate caches keyed by SkPixelRef generation ID. | 249 // This can be used to invalidate caches keyed by SkPixelRef generation ID. |
| 222 struct GenIDChangeListener { | 250 struct GenIDChangeListener { |
| 223 virtual ~GenIDChangeListener() {} | 251 virtual ~GenIDChangeListener() {} |
| 224 virtual void onChange() = 0; | 252 virtual void onChange() = 0; |
| 225 }; | 253 }; |
| 226 | 254 |
| 227 // Takes ownership of listener. | 255 // Takes ownership of listener. |
| 228 void addGenIDChangeListener(GenIDChangeListener* listener); | 256 void addGenIDChangeListener(GenIDChangeListener* listener); |
| 229 | 257 |
| 230 protected: | 258 protected: |
| 231 /** Called when the lockCount goes from 0 to 1. The caller will have already | 259 #ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS |
| 232 acquire a mutex for thread safety, so this method need not do that. | 260 virtual void* onLockPixels(SkColorTable**); |
| 233 */ | 261 virtual bool onNewLockPixels(LockRec*); |
| 234 virtual void* onLockPixels(SkColorTable**) = 0; | 262 #else |
| 235 /** Called when the lock count goes from 1 to 0. The caller will have | 263 /** |
| 236 already acquire a mutex for thread safety, so this method need not do | 264 * On success, returns true and fills out the LockRec for the pixels. On |
| 237 that. | 265 * failure returns false and ignores the LockRec parameter. |
| 238 */ | 266 * |
| 267 * The caller will have already acquired a mutex for thread safety, so this | |
| 268 * method need not do that. | |
| 269 */ | |
| 270 virtual bool onNewLockPixels(LockRec*) = 0; | |
| 271 #endif | |
| 272 | |
| 273 /** | |
| 274 * Balancing the previous successful call to onNewLockPixels. The locked | |
| 275 * pixel address will no longer be referenced, so the subclass is free to | |
| 276 * move or discard that memory. | |
| 277 * | |
| 278 * The caller will have already acquired a mutex for thread safety, so this | |
| 279 * method need not do that. | |
| 280 */ | |
| 239 virtual void onUnlockPixels() = 0; | 281 virtual void onUnlockPixels() = 0; |
| 240 | 282 |
| 241 /** Default impl returns true */ | 283 /** Default impl returns true */ |
| 242 virtual bool onLockPixelsAreWritable() const; | 284 virtual bool onLockPixelsAreWritable() const; |
| 243 | 285 |
| 244 // returns false; | 286 // returns false; |
| 245 virtual bool onImplementsDecodeInto(); | 287 virtual bool onImplementsDecodeInto(); |
| 246 // returns false; | 288 // returns false; |
| 247 virtual bool onDecodeInto(int pow2, SkBitmap* bitmap); | 289 virtual bool onDecodeInto(int pow2, SkBitmap* bitmap); |
| 248 | 290 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 262 */ | 304 */ |
| 263 SkBaseMutex* mutex() const { return fMutex; } | 305 SkBaseMutex* mutex() const { return fMutex; } |
| 264 | 306 |
| 265 // serialization | 307 // serialization |
| 266 SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*); | 308 SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*); |
| 267 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; | 309 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
| 268 | 310 |
| 269 // only call from constructor. Flags this to always be locked, removing | 311 // only call from constructor. Flags this to always be locked, removing |
| 270 // the need to grab the mutex and call onLockPixels/onUnlockPixels. | 312 // the need to grab the mutex and call onLockPixels/onUnlockPixels. |
| 271 // Performance tweak to avoid those calls (esp. in multi-thread use case). | 313 // Performance tweak to avoid those calls (esp. in multi-thread use case). |
| 272 void setPreLocked(void* pixels, SkColorTable* ctable); | 314 void setPreLocked(void*, size_t rowBytes, SkColorTable*); |
| 273 | 315 |
| 274 private: | 316 private: |
| 275 SkBaseMutex* fMutex; // must remain in scope for the life of this object | 317 SkBaseMutex* fMutex; // must remain in scope for the life of this object |
| 276 void* fPixels; | 318 SkImageInfo fInfo; |
| 277 SkColorTable* fColorTable; // we do not track ownership, subclass does | 319 |
| 320 // LockRec is only valid if we're in a locked state (isLocked()) | |
| 321 LockRec fRec; | |
| 278 int fLockCount; | 322 int fLockCount; |
| 279 | 323 |
| 280 mutable uint32_t fGenerationID; | 324 mutable uint32_t fGenerationID; |
| 281 mutable bool fUniqueGenerationID; | 325 mutable bool fUniqueGenerationID; |
| 282 | 326 |
| 283 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owne d | 327 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owne d |
| 284 | 328 |
| 285 SkString fURI; | 329 SkString fURI; |
| 286 | 330 |
| 287 // can go from false to true, but never from true to false | 331 // can go from false to true, but never from true to false |
| 288 bool fIsImmutable; | 332 bool fIsImmutable; |
| 289 // only ever set in constructor, const after that | 333 // only ever set in constructor, const after that |
| 290 bool fPreLocked; | 334 bool fPreLocked; |
| 291 | 335 |
| 292 void needsNewGenID(); | 336 void needsNewGenID(); |
| 293 void callGenIDChangeListeners(); | 337 void callGenIDChangeListeners(); |
| 294 | 338 |
| 295 void setMutex(SkBaseMutex* mutex); | 339 void setMutex(SkBaseMutex* mutex); |
| 296 | 340 |
| 297 // When copying a bitmap to another with the same shape and config, we can s afely | 341 // When copying a bitmap to another with the same shape and config, we can s afely |
| 298 // clone the pixelref generation ID too, which makes them equivalent under c aching. | 342 // clone the pixelref generation ID too, which makes them equivalent under c aching. |
| 299 friend class SkBitmap; // only for cloneGenID | 343 friend class SkBitmap; // only for cloneGenID |
| 300 void cloneGenID(const SkPixelRef&); | 344 void cloneGenID(const SkPixelRef&); |
| 301 | 345 |
| 302 typedef SkFlattenable INHERITED; | 346 typedef SkFlattenable INHERITED; |
| 303 }; | 347 }; |
| 304 | 348 |
| 305 #endif | 349 #endif |
| OLD | NEW |