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 to be called the next time our generation ID changes, | |
213 // if we're confident that we're the only SkPixelRef with that generation ID . | |
214 // This can be used to invalidate caches keyed by generation ID. | |
scroggo
2013/10/22 21:04:02
It's not obvious from this comment that an Invalid
mtklein
2013/10/23 15:28:10
Yeah, I was hoping you guys would get tripped up b
| |
215 struct InvalidationListener { | |
216 virtual ~InvalidationListener() {} | |
217 virtual void onInvalidate() = 0; | |
218 }; | |
219 | |
220 // Takes ownership of listener. | |
221 void addInvalidationListener(InvalidationListener* listener); | |
222 | |
223 // Sometimes it's not worth the effort to add a listener that we know will n ot be called. | |
224 bool addingAnInvalidationListenerMakesSense() const { return fUniqueGenerati onID; } | |
scroggo
2013/10/22 21:04:02
It was initially odd to me that this function depe
bsalomon
2013/10/23 13:52:57
MakesSense seems ambiguous. invalidationListenerMa
mtklein
2013/10/23 15:28:10
The idea was that we could guard against an unnece
| |
225 | |
210 protected: | 226 protected: |
211 /** Called when the lockCount goes from 0 to 1. The caller will have already | 227 /** 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. | 228 acquire a mutex for thread safety, so this method need not do that. |
213 */ | 229 */ |
214 virtual void* onLockPixels(SkColorTable**) = 0; | 230 virtual void* onLockPixels(SkColorTable**) = 0; |
215 /** Called when the lock count goes from 1 to 0. The caller will have | 231 /** 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 | 232 already acquire a mutex for thread safety, so this method need not do |
217 that. | 233 that. |
218 */ | 234 */ |
219 virtual void onUnlockPixels() = 0; | 235 virtual void onUnlockPixels() = 0; |
(...skipping 25 matching lines...) Expand all Loading... | |
245 // serialization | 261 // serialization |
246 SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*); | 262 SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*); |
247 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; | 263 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
248 | 264 |
249 // only call from constructor. Flags this to always be locked, removing | 265 // only call from constructor. Flags this to always be locked, removing |
250 // the need to grab the mutex and call onLockPixels/onUnlockPixels. | 266 // the need to grab the mutex and call onLockPixels/onUnlockPixels. |
251 // Performance tweak to avoid those calls (esp. in multi-thread use case). | 267 // Performance tweak to avoid those calls (esp. in multi-thread use case). |
252 void setPreLocked(void* pixels, SkColorTable* ctable); | 268 void setPreLocked(void* pixels, SkColorTable* ctable); |
253 | 269 |
254 private: | 270 private: |
255 | |
256 SkBaseMutex* fMutex; // must remain in scope for the life of this object | 271 SkBaseMutex* fMutex; // must remain in scope for the life of this object |
257 void* fPixels; | 272 void* fPixels; |
258 SkColorTable* fColorTable; // we do not track ownership, subclass does | 273 SkColorTable* fColorTable; // we do not track ownership, subclass does |
259 int fLockCount; | 274 int fLockCount; |
260 | 275 |
276 void needsNewGenID(); | |
scroggo
2013/10/22 21:04:02
Style nit: fields should go before methods (these
mtklein
2013/10/23 15:28:10
Done. Are you sure this is a good thing to be in
| |
261 mutable uint32_t fGenerationID; | 277 mutable uint32_t fGenerationID; |
278 mutable bool fUniqueGenerationID; | |
262 | 279 |
263 // SkBitmap is only a friend so that when copying, it can modify the new SkP ixelRef to have the | 280 void invalidate(); |
scroggo
2013/10/22 21:04:02
This name seems to be lacking some context. It mak
bsalomon
2013/10/23 13:52:57
sendInvalidationEvents(), notifyInvalidationListen
mtklein
2013/10/23 15:28:10
How's that?
bsalomon
2013/10/23 15:32:27
good
| |
264 // same fGenerationID as the original. | 281 SkTDArray<InvalidationListener*> fInvalidationListeners; // pointers are ow ned |
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 |
274 void setMutex(SkBaseMutex* mutex); | 290 void setMutex(SkBaseMutex* mutex); |
275 | 291 |
292 // When copying a bitmap to another with the same shape and config, we can s afely | |
293 // clone the pixelref generation ID too, which makes them equivalent under c aching. | |
294 friend class SkBitmap; // only for cloneGenID | |
295 void cloneGenID(const SkPixelRef&); | |
296 | |
276 typedef SkFlattenable INHERITED; | 297 typedef SkFlattenable INHERITED; |
277 }; | 298 }; |
278 | 299 |
279 #endif | 300 #endif |
OLD | NEW |