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 "SkTDArray.h" |
17 | 18 |
18 #ifdef SK_DEBUG | 19 #ifdef SK_DEBUG |
19 /** | 20 /** |
20 * Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref | 21 * Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref |
21 * subclasses to correctly handle lock/unlock pixels. For performance | 22 * subclasses to correctly handle lock/unlock pixels. For performance |
22 * reasons, simple malloc-based subclasses call setPreLocked() to skip | 23 * reasons, simple malloc-based subclasses call setPreLocked() to skip |
23 * the overhead of implementing these calls. | 24 * the overhead of implementing these calls. |
24 * | 25 * |
25 * This build-flag disables that optimization, to add in debugging our | 26 * This build-flag disables that optimization, to add in debugging our |
26 * call-sites, to ensure that they correctly balance their calls of | 27 * call-sites, to ensure that they correctly balance their calls of |
(...skipping 15 matching lines...) Expand all Loading... |
42 SkBitmap. A pixelref is installed into a bitmap, and then the bitmap can | 43 SkBitmap. A pixelref is installed into a bitmap, and then the bitmap can |
43 access the actual pixel memory by calling lockPixels/unlockPixels. | 44 access the actual pixel memory by calling lockPixels/unlockPixels. |
44 | 45 |
45 This class can be shared/accessed between multiple threads. | 46 This class can be shared/accessed between multiple threads. |
46 */ | 47 */ |
47 class SK_API SkPixelRef : public SkFlattenable { | 48 class SK_API SkPixelRef : public SkFlattenable { |
48 public: | 49 public: |
49 SK_DECLARE_INST_COUNT(SkPixelRef) | 50 SK_DECLARE_INST_COUNT(SkPixelRef) |
50 | 51 |
51 explicit SkPixelRef(SkBaseMutex* mutex = NULL); | 52 explicit SkPixelRef(SkBaseMutex* mutex = NULL); |
| 53 virtual ~SkPixelRef(); |
52 | 54 |
53 /** Return the pixel memory returned from lockPixels, or null if the | 55 /** Return the pixel memory returned from lockPixels, or null if the |
54 lockCount is 0. | 56 lockCount is 0. |
55 */ | 57 */ |
56 void* pixels() const { return fPixels; } | 58 void* pixels() const { return fPixels; } |
57 | 59 |
58 /** Return the current colorTable (if any) if pixels are locked, or null. | 60 /** Return the current colorTable (if any) if pixels are locked, or null. |
59 */ | 61 */ |
60 SkColorTable* colorTable() const { return fColorTable; } | 62 SkColorTable* colorTable() const { return fColorTable; } |
61 | 63 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 virtual void globalRef(void* data=NULL); | 202 virtual void globalRef(void* data=NULL); |
201 | 203 |
202 /** | 204 /** |
203 * Release a "global" ref on this object. | 205 * Release a "global" ref on this object. |
204 * The default implementation just calls unref(), but subclasses can overri
de | 206 * The default implementation just calls unref(), but subclasses can overri
de |
205 * this method to implement additional behavior. | 207 * this method to implement additional behavior. |
206 */ | 208 */ |
207 virtual void globalUnref(); | 209 virtual void globalUnref(); |
208 #endif | 210 #endif |
209 | 211 |
| 212 // Register a listener that may be called the next time our generation ID ch
anges. |
| 213 // |
| 214 // We'll only call the listener if we're confident that we are the only SkPi
xelRef with this |
| 215 // generation ID. If our generation ID changes and we decide not to call th
e listener, we'll |
| 216 // never call it: you must add a new listener for each generation ID change.
We also won't call |
| 217 // the listener when we're certain no one knows what our generation ID is. |
| 218 // |
| 219 // This can be used to invalidate caches keyed by SkPixelRef generation ID. |
| 220 struct GenIDChangeListener { |
| 221 virtual ~GenIDChangeListener() {} |
| 222 virtual void onChange() = 0; |
| 223 }; |
| 224 |
| 225 // Takes ownership of listener. |
| 226 void addGenIDChangeListener(GenIDChangeListener* listener); |
| 227 |
210 protected: | 228 protected: |
211 /** Called when the lockCount goes from 0 to 1. The caller will have already | 229 /** Called when the lockCount goes from 0 to 1. The caller will have already |
212 acquire a mutex for thread safety, so this method need not do that. | 230 acquire a mutex for thread safety, so this method need not do that. |
213 */ | 231 */ |
214 virtual void* onLockPixels(SkColorTable**) = 0; | 232 virtual void* onLockPixels(SkColorTable**) = 0; |
215 /** Called when the lock count goes from 1 to 0. The caller will have | 233 /** Called when the lock count goes from 1 to 0. The caller will have |
216 already acquire a mutex for thread safety, so this method need not do | 234 already acquire a mutex for thread safety, so this method need not do |
217 that. | 235 that. |
218 */ | 236 */ |
219 virtual void onUnlockPixels() = 0; | 237 virtual void onUnlockPixels() = 0; |
(...skipping 25 matching lines...) Expand all Loading... |
245 // serialization | 263 // serialization |
246 SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*); | 264 SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*); |
247 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; | 265 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
248 | 266 |
249 // only call from constructor. Flags this to always be locked, removing | 267 // only call from constructor. Flags this to always be locked, removing |
250 // the need to grab the mutex and call onLockPixels/onUnlockPixels. | 268 // the need to grab the mutex and call onLockPixels/onUnlockPixels. |
251 // Performance tweak to avoid those calls (esp. in multi-thread use case). | 269 // Performance tweak to avoid those calls (esp. in multi-thread use case). |
252 void setPreLocked(void* pixels, SkColorTable* ctable); | 270 void setPreLocked(void* pixels, SkColorTable* ctable); |
253 | 271 |
254 private: | 272 private: |
255 | |
256 SkBaseMutex* fMutex; // must remain in scope for the life of this object | 273 SkBaseMutex* fMutex; // must remain in scope for the life of this object |
257 void* fPixels; | 274 void* fPixels; |
258 SkColorTable* fColorTable; // we do not track ownership, subclass does | 275 SkColorTable* fColorTable; // we do not track ownership, subclass does |
259 int fLockCount; | 276 int fLockCount; |
260 | 277 |
261 mutable uint32_t fGenerationID; | 278 mutable uint32_t fGenerationID; |
| 279 mutable bool fUniqueGenerationID; |
262 | 280 |
263 // SkBitmap is only a friend so that when copying, it can modify the new SkP
ixelRef to have the | 281 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owne
d |
264 // same fGenerationID as the original. | |
265 friend class SkBitmap; | |
266 | 282 |
267 SkString fURI; | 283 SkString fURI; |
268 | 284 |
269 // can go from false to true, but never from true to false | 285 // can go from false to true, but never from true to false |
270 bool fIsImmutable; | 286 bool fIsImmutable; |
271 // only ever set in constructor, const after that | 287 // only ever set in constructor, const after that |
272 bool fPreLocked; | 288 bool fPreLocked; |
273 | 289 |
| 290 void needsNewGenID(); |
| 291 void callGenIDChangeListeners(); |
| 292 |
274 void setMutex(SkBaseMutex* mutex); | 293 void setMutex(SkBaseMutex* mutex); |
275 | 294 |
| 295 // When copying a bitmap to another with the same shape and config, we can s
afely |
| 296 // clone the pixelref generation ID too, which makes them equivalent under c
aching. |
| 297 friend class SkBitmap; // only for cloneGenID |
| 298 void cloneGenID(const SkPixelRef&); |
| 299 |
276 typedef SkFlattenable INHERITED; | 300 typedef SkFlattenable INHERITED; |
277 }; | 301 }; |
278 | 302 |
279 #endif | 303 #endif |
OLD | NEW |