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 |
19 #define SK_SUPPORT_LEGACY_PIXELREF_CONSTRUCTOR | 20 //#define SK_SUPPORT_LEGACY_PIXELREF_CONSTRUCTOR |
| 21 |
| 22 #define SK_SUPPORT_LEGACY_ONLOCKPIXELS |
20 | 23 |
21 #ifdef SK_DEBUG | 24 #ifdef SK_DEBUG |
22 /** | 25 /** |
23 * Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref | 26 * Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref |
24 * subclasses to correctly handle lock/unlock pixels. For performance | 27 * subclasses to correctly handle lock/unlock pixels. For performance |
25 * reasons, simple malloc-based subclasses call setPreLocked() to skip | 28 * reasons, simple malloc-based subclasses call setPreLocked() to skip |
26 * the overhead of implementing these calls. | 29 * the overhead of implementing these calls. |
27 * | 30 * |
28 * This build-flag disables that optimization, to add in debugging our | 31 * This build-flag disables that optimization, to add in debugging our |
29 * call-sites, to ensure that they correctly balance their calls of | 32 * call-sites, to ensure that they correctly balance their calls of |
(...skipping 30 matching lines...) Expand all Loading... |
60 SkPixelRef(const SkImageInfo&, SkBaseMutex* mutex); | 63 SkPixelRef(const SkImageInfo&, SkBaseMutex* mutex); |
61 virtual ~SkPixelRef(); | 64 virtual ~SkPixelRef(); |
62 | 65 |
63 const SkImageInfo& info() const { | 66 const SkImageInfo& info() const { |
64 return fInfo; | 67 return fInfo; |
65 } | 68 } |
66 | 69 |
67 /** Return the pixel memory returned from lockPixels, or null if the | 70 /** Return the pixel memory returned from lockPixels, or null if the |
68 lockCount is 0. | 71 lockCount is 0. |
69 */ | 72 */ |
70 void* pixels() const { return fPixels; } | 73 void* pixels() const { return fRec.fPixels; } |
71 | 74 |
72 /** Return the current colorTable (if any) if pixels are locked, or null. | 75 /** Return the current colorTable (if any) if pixels are locked, or null. |
73 */ | 76 */ |
74 SkColorTable* colorTable() const { return fColorTable; } | 77 SkColorTable* colorTable() const { return fRec.fColorTable; } |
75 | 78 |
76 /** | 79 /** |
| 80 * To access the actual pixels of a pixelref, it must be "locked". |
| 81 * Calling lockPixels returns a LockRec struct (on success). |
| 82 */ |
| 83 struct LockRec { |
| 84 void* fPixels; |
| 85 SkColorTable* fColorTable; |
| 86 size_t fRowBytes; |
| 87 |
| 88 void zero() { sk_bzero(this, sizeof(*this)); } |
| 89 }; |
| 90 |
| 91 /** |
77 * Returns true if the lockcount > 0 | 92 * Returns true if the lockcount > 0 |
78 */ | 93 */ |
79 bool isLocked() const { return fLockCount > 0; } | 94 bool isLocked() const { return fLockCount > 0; } |
80 | 95 |
81 SkDEBUGCODE(int getLockCount() const { return fLockCount; }) | 96 SkDEBUGCODE(int getLockCount() const { return fLockCount; }) |
82 | 97 |
83 /** Call to access the pixel memory, which is returned. Balance with a call | 98 /** |
84 to unlockPixels(). | 99 * Call to access the pixel memory. Return true on success. Balance this |
85 */ | 100 * with a call to unlockPixels(). |
86 void lockPixels(); | 101 */ |
| 102 bool lockPixels(); |
| 103 |
| 104 /** |
| 105 * Call to access the pixel memory. On success, return true and fill out |
| 106 * the specified rec. On failure, return false and ignore the rec parameter
. |
| 107 * Balance this with a call to unlockPixels(). |
| 108 */ |
| 109 bool lockPixels(LockRec* rec); |
| 110 |
87 /** Call to balanace a previous call to lockPixels(). Returns the pixels | 111 /** Call to balanace a previous call to lockPixels(). Returns the pixels |
88 (or null) after the unlock. NOTE: lock calls can be nested, but the | 112 (or null) after the unlock. NOTE: lock calls can be nested, but the |
89 matching number of unlock calls must be made in order to free the | 113 matching number of unlock calls must be made in order to free the |
90 memory (if the subclass implements caching/deferred-decoding.) | 114 memory (if the subclass implements caching/deferred-decoding.) |
91 */ | 115 */ |
92 void unlockPixels(); | 116 void unlockPixels(); |
93 | 117 |
94 /** | 118 /** |
95 * Some bitmaps can return a copy of their pixels for lockPixels(), but | 119 * Some bitmaps can return a copy of their pixels for lockPixels(), but |
96 * that copy, if modified, will not be pushed back. These bitmaps should | 120 * 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... |
233 // This can be used to invalidate caches keyed by SkPixelRef generation ID. | 257 // This can be used to invalidate caches keyed by SkPixelRef generation ID. |
234 struct GenIDChangeListener { | 258 struct GenIDChangeListener { |
235 virtual ~GenIDChangeListener() {} | 259 virtual ~GenIDChangeListener() {} |
236 virtual void onChange() = 0; | 260 virtual void onChange() = 0; |
237 }; | 261 }; |
238 | 262 |
239 // Takes ownership of listener. | 263 // Takes ownership of listener. |
240 void addGenIDChangeListener(GenIDChangeListener* listener); | 264 void addGenIDChangeListener(GenIDChangeListener* listener); |
241 | 265 |
242 protected: | 266 protected: |
243 /** Called when the lockCount goes from 0 to 1. The caller will have already | 267 #ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS |
244 acquire a mutex for thread safety, so this method need not do that. | 268 virtual void* onLockPixels(SkColorTable**); |
245 */ | 269 virtual bool onNewLockPixels(LockRec*); |
246 virtual void* onLockPixels(SkColorTable**) = 0; | 270 #else |
247 /** Called when the lock count goes from 1 to 0. The caller will have | 271 /** |
248 already acquire a mutex for thread safety, so this method need not do | 272 * On success, returns true and fills out the LockRec for the pixels. On |
249 that. | 273 * failure returns false and ignores the LockRec parameter. |
250 */ | 274 * |
| 275 * The caller will have already acquired a mutex for thread safety, so this |
| 276 * method need not do that. |
| 277 */ |
| 278 virtual bool onNewLockPixels(LockRec*) = 0; |
| 279 #endif |
| 280 |
| 281 /** |
| 282 * Balancing the previous successful call to onNewLockPixels. The locked |
| 283 * pixel address will no longer be referenced, so the subclass is free to |
| 284 * move or discard that memory. |
| 285 * |
| 286 * The caller will have already acquired a mutex for thread safety, so this |
| 287 * method need not do that. |
| 288 */ |
251 virtual void onUnlockPixels() = 0; | 289 virtual void onUnlockPixels() = 0; |
252 | 290 |
253 /** Default impl returns true */ | 291 /** Default impl returns true */ |
254 virtual bool onLockPixelsAreWritable() const; | 292 virtual bool onLockPixelsAreWritable() const; |
255 | 293 |
256 // returns false; | 294 // returns false; |
257 virtual bool onImplementsDecodeInto(); | 295 virtual bool onImplementsDecodeInto(); |
258 // returns false; | 296 // returns false; |
259 virtual bool onDecodeInto(int pow2, SkBitmap* bitmap); | 297 virtual bool onDecodeInto(int pow2, SkBitmap* bitmap); |
260 | 298 |
(...skipping 23 matching lines...) Expand all Loading... |
284 */ | 322 */ |
285 SkBaseMutex* mutex() const { return fMutex; } | 323 SkBaseMutex* mutex() const { return fMutex; } |
286 | 324 |
287 // serialization | 325 // serialization |
288 SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*); | 326 SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*); |
289 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; | 327 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
290 | 328 |
291 // only call from constructor. Flags this to always be locked, removing | 329 // only call from constructor. Flags this to always be locked, removing |
292 // the need to grab the mutex and call onLockPixels/onUnlockPixels. | 330 // the need to grab the mutex and call onLockPixels/onUnlockPixels. |
293 // Performance tweak to avoid those calls (esp. in multi-thread use case). | 331 // Performance tweak to avoid those calls (esp. in multi-thread use case). |
294 void setPreLocked(void* pixels, SkColorTable* ctable); | 332 void setPreLocked(void*, size_t rowBytes, SkColorTable*); |
295 | 333 |
296 private: | 334 private: |
297 SkBaseMutex* fMutex; // must remain in scope for the life of this object | 335 SkBaseMutex* fMutex; // must remain in scope for the life of this object |
298 SkImageInfo fInfo; | 336 SkImageInfo fInfo; |
299 | 337 |
300 void* fPixels; | 338 // LockRec is only valid if we're in a locked state (isLocked()) |
301 SkColorTable* fColorTable; // we do not track ownership, subclass does | 339 LockRec fRec; |
302 int fLockCount; | 340 int fLockCount; |
303 | 341 |
304 mutable uint32_t fGenerationID; | 342 mutable uint32_t fGenerationID; |
305 mutable bool fUniqueGenerationID; | 343 mutable bool fUniqueGenerationID; |
306 | 344 |
307 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owne
d | 345 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owne
d |
308 | 346 |
309 SkString fURI; | 347 SkString fURI; |
310 | 348 |
311 // can go from false to true, but never from true to false | 349 // can go from false to true, but never from true to false |
312 bool fIsImmutable; | 350 bool fIsImmutable; |
313 // only ever set in constructor, const after that | 351 // only ever set in constructor, const after that |
314 bool fPreLocked; | 352 bool fPreLocked; |
315 | 353 |
316 void needsNewGenID(); | 354 void needsNewGenID(); |
317 void callGenIDChangeListeners(); | 355 void callGenIDChangeListeners(); |
318 | 356 |
319 void setMutex(SkBaseMutex* mutex); | 357 void setMutex(SkBaseMutex* mutex); |
320 | 358 |
321 // When copying a bitmap to another with the same shape and config, we can s
afely | 359 // When copying a bitmap to another with the same shape and config, we can s
afely |
322 // clone the pixelref generation ID too, which makes them equivalent under c
aching. | 360 // clone the pixelref generation ID too, which makes them equivalent under c
aching. |
323 friend class SkBitmap; // only for cloneGenID | 361 friend class SkBitmap; // only for cloneGenID |
324 void cloneGenID(const SkPixelRef&); | 362 void cloneGenID(const SkPixelRef&); |
325 | 363 |
326 typedef SkFlattenable INHERITED; | 364 typedef SkFlattenable INHERITED; |
327 }; | 365 }; |
328 | 366 |
329 #endif | 367 #endif |
OLD | NEW |