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 #ifdef SK_DEBUG | 20 #ifdef SK_DEBUG |
20 /** | 21 /** |
21 * Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref | 22 * Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref |
22 * subclasses to correctly handle lock/unlock pixels. For performance | 23 * subclasses to correctly handle lock/unlock pixels. For performance |
23 * reasons, simple malloc-based subclasses call setPreLocked() to skip | 24 * reasons, simple malloc-based subclasses call setPreLocked() to skip |
24 * the overhead of implementing these calls. | 25 * the overhead of implementing these calls. |
25 * | 26 * |
26 * This build-flag disables that optimization, to add in debugging our | 27 * This build-flag disables that optimization, to add in debugging our |
(...skipping 18 matching lines...) Expand all Loading... |
45 | 46 |
46 This class can be shared/accessed between multiple threads. | 47 This class can be shared/accessed between multiple threads. |
47 */ | 48 */ |
48 class SK_API SkPixelRef : public SkFlattenable { | 49 class SK_API SkPixelRef : public SkFlattenable { |
49 public: | 50 public: |
50 SK_DECLARE_INST_COUNT(SkPixelRef) | 51 SK_DECLARE_INST_COUNT(SkPixelRef) |
51 | 52 |
52 explicit SkPixelRef(SkBaseMutex* mutex = NULL); | 53 explicit SkPixelRef(SkBaseMutex* mutex = NULL); |
53 virtual ~SkPixelRef(); | 54 virtual ~SkPixelRef(); |
54 | 55 |
| 56 struct LockRec { |
| 57 void* fPixels; |
| 58 SkColorTable* fColorTable; |
| 59 size_t fRowBytes; |
| 60 |
| 61 void zero() { sk_bzero(this, sizeof(*this)); } |
| 62 }; |
| 63 |
| 64 /** |
| 65 * Return the data's rowbytes. Will be 0 if the the lockCount is 0. |
| 66 */ |
| 67 size_t rowBytes() const { return fRec.fRowBytes; } |
| 68 |
55 /** Return the pixel memory returned from lockPixels, or null if the | 69 /** Return the pixel memory returned from lockPixels, or null if the |
56 lockCount is 0. | 70 lockCount is 0. |
57 */ | 71 */ |
58 void* pixels() const { return fPixels; } | 72 void* pixels() const { return fRec.fPixels; } |
59 | 73 |
60 /** Return the current colorTable (if any) if pixels are locked, or null. | 74 /** Return the current colorTable (if any) if pixels are locked, or null. |
61 */ | 75 */ |
62 SkColorTable* colorTable() const { return fColorTable; } | 76 SkColorTable* colorTable() const { return fRec.fColorTable; } |
63 | 77 |
64 /** | 78 /** |
65 * Returns true if the lockcount > 0 | 79 * Returns true if the lockcount > 0 |
66 */ | 80 */ |
67 bool isLocked() const { return fLockCount > 0; } | 81 bool isLocked() const { return fLockCount > 0; } |
68 | 82 |
69 SkDEBUGCODE(int getLockCount() const { return fLockCount; }) | 83 SkDEBUGCODE(int getLockCount() const { return fLockCount; }) |
70 | 84 |
71 /** Call to access the pixel memory, which is returned. Balance with a call | 85 /** Call to access the pixel memory, which is returned. Balance with a call |
72 to unlockPixels(). | 86 to unlockPixels(). |
73 */ | 87 */ |
74 void lockPixels(); | 88 void lockPixels(); |
| 89 |
| 90 /** |
| 91 * Call to access the pixel memory. On success, return true and fill out |
| 92 * the specified rec. On failure, return false and ignore the rec parameter
. |
| 93 */ |
| 94 bool lockPixels(LockRec* rec); |
| 95 |
75 /** Call to balanace a previous call to lockPixels(). Returns the pixels | 96 /** 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 | 97 (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 | 98 matching number of unlock calls must be made in order to free the |
78 memory (if the subclass implements caching/deferred-decoding.) | 99 memory (if the subclass implements caching/deferred-decoding.) |
79 */ | 100 */ |
80 void unlockPixels(); | 101 void unlockPixels(); |
81 | 102 |
82 /** | 103 /** |
83 * Some bitmaps can return a copy of their pixels for lockPixels(), but | 104 * 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 | 105 * that copy, if modified, will not be pushed back. These bitmaps should |
85 * not be used as targets for a raster device/canvas (since all pixels | 106 * not be used as targets for a raster device/canvas (since all pixels |
86 * modifications will be lost when unlockPixels() is called.) | 107 * modifications will be lost when unlockPixels() is called.) |
87 */ | 108 */ |
88 bool lockPixelsAreWritable() const; | 109 bool lockPixelsAreWritable() const; |
89 | 110 |
| 111 /** |
| 112 * On success, return true and return the ImageInfo. On failure, return |
| 113 * false and ignore the ImageInfo parameter. |
| 114 */ |
| 115 bool getInfo(SkImageInfo*); |
| 116 |
90 /** Returns a non-zero, unique value corresponding to the pixels in this | 117 /** Returns a non-zero, unique value corresponding to the pixels in this |
91 pixelref. Each time the pixels are changed (and notifyPixelsChanged is | 118 pixelref. Each time the pixels are changed (and notifyPixelsChanged is |
92 called), a different generation ID will be returned. | 119 called), a different generation ID will be returned. |
93 */ | 120 */ |
94 uint32_t getGenerationID() const; | 121 uint32_t getGenerationID() const; |
95 | 122 |
96 /** Call this if you have changed the contents of the pixels. This will in- | 123 /** Call this if you have changed the contents of the pixels. This will in- |
97 turn cause a different generation ID value to be returned from | 124 turn cause a different generation ID value to be returned from |
98 getGenerationID(). | 125 getGenerationID(). |
99 */ | 126 */ |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 // This can be used to invalidate caches keyed by SkPixelRef generation ID. | 248 // This can be used to invalidate caches keyed by SkPixelRef generation ID. |
222 struct GenIDChangeListener { | 249 struct GenIDChangeListener { |
223 virtual ~GenIDChangeListener() {} | 250 virtual ~GenIDChangeListener() {} |
224 virtual void onChange() = 0; | 251 virtual void onChange() = 0; |
225 }; | 252 }; |
226 | 253 |
227 // Takes ownership of listener. | 254 // Takes ownership of listener. |
228 void addGenIDChangeListener(GenIDChangeListener* listener); | 255 void addGenIDChangeListener(GenIDChangeListener* listener); |
229 | 256 |
230 protected: | 257 protected: |
231 /** Called when the lockCount goes from 0 to 1. The caller will have already | 258 /** |
232 acquire a mutex for thread safety, so this method need not do that. | 259 * Return the ImageInfo, returning true on success. On failure, return |
233 */ | 260 * false and ignore the info parameter. |
234 virtual void* onLockPixels(SkColorTable**) = 0; | 261 * |
235 /** Called when the lock count goes from 1 to 0. The caller will have | 262 * The caller will have already acquired a mutex for thread safety, so this |
236 already acquire a mutex for thread safety, so this method need not do | 263 * method need not do that. |
237 that. | 264 */ |
238 */ | 265 virtual bool onGetInfo(SkImageInfo*) = 0; |
| 266 |
| 267 /** |
| 268 * On success, returns true and fills out the LockRec for the pixels. On |
| 269 * failure returns false and ignores the LockRec parameter. |
| 270 * |
| 271 * The caller will have already acquired a mutex for thread safety, so this |
| 272 * method need not do that. |
| 273 */ |
| 274 virtual bool onNewLockPixels(LockRec*) = 0; |
| 275 |
| 276 /** |
| 277 * Balancing the previous successful call to onNewLockPixels. The locked |
| 278 * pixel address will no longer be referenced, so the subclass is free to |
| 279 * move or discard that memory. |
| 280 * |
| 281 * The caller will have already acquired a mutex for thread safety, so this |
| 282 * method need not do that. |
| 283 */ |
239 virtual void onUnlockPixels() = 0; | 284 virtual void onUnlockPixels() = 0; |
240 | 285 |
241 /** Default impl returns true */ | 286 /** Default impl returns true */ |
242 virtual bool onLockPixelsAreWritable() const; | 287 virtual bool onLockPixelsAreWritable() const; |
243 | 288 |
244 // returns false; | 289 // returns false; |
245 virtual bool onImplementsDecodeInto(); | 290 virtual bool onImplementsDecodeInto(); |
246 // returns false; | 291 // returns false; |
247 virtual bool onDecodeInto(int pow2, SkBitmap* bitmap); | 292 virtual bool onDecodeInto(int pow2, SkBitmap* bitmap); |
248 | 293 |
(...skipping 13 matching lines...) Expand all Loading... |
262 */ | 307 */ |
263 SkBaseMutex* mutex() const { return fMutex; } | 308 SkBaseMutex* mutex() const { return fMutex; } |
264 | 309 |
265 // serialization | 310 // serialization |
266 SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*); | 311 SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*); |
267 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; | 312 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
268 | 313 |
269 // only call from constructor. Flags this to always be locked, removing | 314 // only call from constructor. Flags this to always be locked, removing |
270 // the need to grab the mutex and call onLockPixels/onUnlockPixels. | 315 // the need to grab the mutex and call onLockPixels/onUnlockPixels. |
271 // Performance tweak to avoid those calls (esp. in multi-thread use case). | 316 // Performance tweak to avoid those calls (esp. in multi-thread use case). |
272 void setPreLocked(void* pixels, SkColorTable* ctable); | 317 void setPreLocked(const SkImageInfo&, void*, size_t rowBytes, SkColorTable*)
; |
273 | 318 |
274 private: | 319 private: |
275 SkBaseMutex* fMutex; // must remain in scope for the life of this object | 320 SkBaseMutex* fMutex; // must remain in scope for the life of this object |
276 void* fPixels; | 321 SkImageInfo fInfo; |
277 SkColorTable* fColorTable; // we do not track ownership, subclass does | 322 LockRec fRec; |
278 int fLockCount; | 323 int fLockCount; |
279 | 324 |
280 mutable uint32_t fGenerationID; | 325 mutable uint32_t fGenerationID; |
281 mutable bool fUniqueGenerationID; | 326 mutable bool fUniqueGenerationID; |
282 | 327 |
283 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owne
d | 328 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owne
d |
284 | 329 |
285 SkString fURI; | 330 SkString fURI; |
286 | 331 |
287 // can go from false to true, but never from true to false | 332 // can go from false to true, but never from true to false |
288 bool fIsImmutable; | 333 bool fIsImmutable; |
289 // only ever set in constructor, const after that | 334 // only ever set in constructor, const after that |
290 bool fPreLocked; | 335 bool fPreLocked; |
291 | 336 |
292 void needsNewGenID(); | 337 void needsNewGenID(); |
293 void callGenIDChangeListeners(); | 338 void callGenIDChangeListeners(); |
294 | 339 |
295 void setMutex(SkBaseMutex* mutex); | 340 void setMutex(SkBaseMutex* mutex); |
296 | 341 |
297 // When copying a bitmap to another with the same shape and config, we can s
afely | 342 // 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. | 343 // clone the pixelref generation ID too, which makes them equivalent under c
aching. |
299 friend class SkBitmap; // only for cloneGenID | 344 friend class SkBitmap; // only for cloneGenID |
300 void cloneGenID(const SkPixelRef&); | 345 void cloneGenID(const SkPixelRef&); |
301 | 346 |
302 typedef SkFlattenable INHERITED; | 347 typedef SkFlattenable INHERITED; |
303 }; | 348 }; |
304 | 349 |
305 #endif | 350 #endif |
OLD | NEW |