|
1 /* |
|
2 * Copyright 2008 The Android Open Source Project |
|
3 * |
|
4 * Use of this source code is governed by a BSD-style license that can be |
|
5 * found in the LICENSE file. |
|
6 */ |
|
7 |
|
8 #ifndef SkPixelRef_DEFINED |
|
9 #define SkPixelRef_DEFINED |
|
10 |
|
11 #include "SkBitmap.h" |
|
12 #include "SkRefCnt.h" |
|
13 #include "SkString.h" |
|
14 #include "SkFlattenable.h" |
|
15 #include "SkImageInfo.h" |
|
16 #include "SkTDArray.h" |
|
17 |
|
18 //#define xed |
|
19 |
|
20 #ifdef SK_DEBUG |
|
21 /** |
|
22 * Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref |
|
23 * subclasses to correctly handle lock/unlock pixels. For performance |
|
24 * reasons, simple malloc-based subclasses call setPreLocked() to skip |
|
25 * the overhead of implementing these calls. |
|
26 * |
|
27 * This build-flag disables that optimization, to add in debugging our |
|
28 * call-sites, to ensure that they correctly balance their calls of |
|
29 * lock and unlock. |
|
30 */ |
|
31 // #define SK_IGNORE_PIXELREF_SETPRELOCKED |
|
32 #endif |
|
33 |
|
34 class SkColorTable; |
|
35 class SkData; |
|
36 struct SkIRect; |
|
37 class SkMutex; |
|
38 |
|
39 class GrTexture; |
|
40 |
|
41 /** \class SkPixelRef |
|
42 |
|
43 This class is the smart container for pixel memory, and is used with |
|
44 SkBitmap. A pixelref is installed into a bitmap, and then the bitmap can |
|
45 access the actual pixel memory by calling lockPixels/unlockPixels. |
|
46 |
|
47 This class can be shared/accessed between multiple threads. |
|
48 */ |
|
49 class SK_API SkPixelRef : public SkFlattenable { |
|
50 public: |
|
51 SK_DECLARE_INST_COUNT(SkPixelRef) |
|
52 |
|
53 explicit SkPixelRef(const SkImageInfo&); |
|
54 SkPixelRef(const SkImageInfo&, SkBaseMutex* mutex); |
|
55 virtual ~SkPixelRef(); |
|
56 |
|
57 const SkImageInfo& info() const { |
|
58 return fInfo; |
|
59 } |
|
60 |
|
61 /** Return the pixel memory returned from lockPixels, or null if the |
|
62 lockCount is 0. |
|
63 */ |
|
64 void* pixels() const { return fRec.fPixels; } |
|
65 |
|
66 /** Return the current colorTable (if any) if pixels are locked, or null. |
|
67 */ |
|
68 SkColorTable* colorTable() const { return fRec.fColorTable; } |
|
69 |
|
70 size_t rowBytes() const { return fRec.fRowBytes; } |
|
71 |
|
72 /** |
|
73 * To access the actual pixels of a pixelref, it must be "locked". |
|
74 * Calling lockPixels returns a LockRec struct (on success). |
|
75 */ |
|
76 struct LockRec { |
|
77 void* fPixels; |
|
78 SkColorTable* fColorTable; |
|
79 size_t fRowBytes; |
|
80 |
|
81 void zero() { sk_bzero(this, sizeof(*this)); } |
|
82 |
|
83 bool isZero() const { |
|
84 return NULL == fPixels && NULL == fColorTable && 0 == fRowBytes; |
|
85 } |
|
86 }; |
|
87 |
|
88 /** |
|
89 * Returns true if the lockcount > 0 |
|
90 */ |
|
91 bool isLocked() const { return fLockCount > 0; } |
|
92 |
|
93 SkDEBUGCODE(int getLockCount() const { return fLockCount; }) |
|
94 |
|
95 /** |
|
96 * Call to access the pixel memory. Return true on success. Balance this |
|
97 * with a call to unlockPixels(). |
|
98 */ |
|
99 bool lockPixels(); |
|
100 |
|
101 /** |
|
102 * Call to access the pixel memory. On success, return true and fill out |
|
103 * the specified rec. On failure, return false and ignore the rec parameter. |
|
104 * Balance this with a call to unlockPixels(). |
|
105 */ |
|
106 bool lockPixels(LockRec* rec); |
|
107 |
|
108 /** Call to balanace a previous call to lockPixels(). Returns the pixels |
|
109 (or null) after the unlock. NOTE: lock calls can be nested, but the |
|
110 matching number of unlock calls must be made in order to free the |
|
111 memory (if the subclass implements caching/deferred-decoding.) |
|
112 */ |
|
113 void unlockPixels(); |
|
114 |
|
115 /** |
|
116 * Some bitmaps can return a copy of their pixels for lockPixels(), but |
|
117 * that copy, if modified, will not be pushed back. These bitmaps should |
|
118 * not be used as targets for a raster device/canvas (since all pixels |
|
119 * modifications will be lost when unlockPixels() is called.) |
|
120 */ |
|
121 bool lockPixelsAreWritable() const; |
|
122 |
|
123 /** Returns a non-zero, unique value corresponding to the pixels in this |
|
124 pixelref. Each time the pixels are changed (and notifyPixelsChanged is |
|
125 called), a different generation ID will be returned. |
|
126 */ |
|
127 uint32_t getGenerationID() const; |
|
128 |
|
129 /** |
|
130 * Call this if you have changed the contents of the pixels. This will in- |
|
131 * turn cause a different generation ID value to be returned from |
|
132 * getGenerationID(). |
|
133 */ |
|
134 void notifyPixelsChanged(); |
|
135 |
|
136 /** |
|
137 * Change the info's AlphaType. Note that this does not automatically |
|
138 * invalidate the generation ID. If the pixel values themselves have |
|
139 * changed, then you must explicitly call notifyPixelsChanged() as well. |
|
140 */ |
|
141 void changeAlphaType(SkAlphaType at); |
|
142 |
|
143 /** Returns true if this pixelref is marked as immutable, meaning that the |
|
144 contents of its pixels will not change for the lifetime of the pixelref. |
|
145 */ |
|
146 bool isImmutable() const { return fIsImmutable; } |
|
147 |
|
148 /** Marks this pixelref is immutable, meaning that the contents of its |
|
149 pixels will not change for the lifetime of the pixelref. This state can |
|
150 be set on a pixelref, but it cannot be cleared once it is set. |
|
151 */ |
|
152 void setImmutable(); |
|
153 |
|
154 /** Return the optional URI string associated with this pixelref. May be |
|
155 null. |
|
156 */ |
|
157 const char* getURI() const { return fURI.size() ? fURI.c_str() : NULL; } |
|
158 |
|
159 /** Copy a URI string to this pixelref, or clear the URI if the uri is null |
|
160 */ |
|
161 void setURI(const char uri[]) { |
|
162 fURI.set(uri); |
|
163 } |
|
164 |
|
165 /** Copy a URI string to this pixelref |
|
166 */ |
|
167 void setURI(const char uri[], size_t len) { |
|
168 fURI.set(uri, len); |
|
169 } |
|
170 |
|
171 /** Assign a URI string to this pixelref. |
|
172 */ |
|
173 void setURI(const SkString& uri) { fURI = uri; } |
|
174 |
|
175 /** |
|
176 * If the pixelRef has an encoded (i.e. compressed) representation, |
|
177 * return a ref to its data. If the pixelRef |
|
178 * is uncompressed or otherwise does not have this form, return NULL. |
|
179 * |
|
180 * If non-null is returned, the caller is responsible for calling unref() |
|
181 * on the data when it is finished. |
|
182 */ |
|
183 SkData* refEncodedData() { |
|
184 return this->onRefEncodedData(); |
|
185 } |
|
186 |
|
187 /** |
|
188 * Experimental -- tells the caller if it is worth it to call decodeInto(). |
|
189 * Just an optimization at this point, to avoid checking the cache first. |
|
190 * We may remove/change this call in the future. |
|
191 */ |
|
192 bool implementsDecodeInto() { |
|
193 return this->onImplementsDecodeInto(); |
|
194 } |
|
195 |
|
196 /** |
|
197 * Return a decoded instance of this pixelRef in bitmap. If this cannot be |
|
198 * done, return false and the bitmap parameter is ignored/unchanged. |
|
199 * |
|
200 * pow2 is the requeste power-of-two downscale that the caller needs. This |
|
201 * can be ignored, and the "original" size can be returned, but if the |
|
202 * underlying codec can efficiently return a smaller size, that should be |
|
203 * done. Some examples: |
|
204 * |
|
205 * To request the "base" version (original scale), pass 0 for pow2 |
|
206 * To request 1/2 scale version (1/2 width, 1/2 height), pass 1 for pow2 |
|
207 * To request 1/4 scale version (1/4 width, 1/4 height), pass 2 for pow2 |
|
208 * ... |
|
209 * |
|
210 * If this returns true, then bitmap must be "locked" such that |
|
211 * bitmap->getPixels() will return the correct address. |
|
212 */ |
|
213 bool decodeInto(int pow2, SkBitmap* bitmap) { |
|
214 SkASSERT(pow2 >= 0); |
|
215 return this->onDecodeInto(pow2, bitmap); |
|
216 } |
|
217 |
|
218 /** Are we really wrapping a texture instead of a bitmap? |
|
219 */ |
|
220 virtual GrTexture* getTexture() { return NULL; } |
|
221 |
|
222 bool readPixels(SkBitmap* dst, const SkIRect* subset = NULL); |
|
223 |
|
224 /** |
|
225 * Makes a deep copy of this PixelRef, respecting the requested config. |
|
226 * @param config Desired config. |
|
227 * @param subset Subset of this PixelRef to copy. Must be fully contained within the bounds of |
|
228 * of this PixelRef. |
|
229 * @return A new SkPixelRef, or NULL if either there is an error (e.g. the destination could |
|
230 * not be created with the given config), or this PixelRef does not support deep |
|
231 * copies. |
|
232 */ |
|
233 virtual SkPixelRef* deepCopy(SkBitmap::Config config, const SkIRect* subset = NULL) { |
|
234 return NULL; |
|
235 } |
|
236 |
|
237 #ifdef SK_BUILD_FOR_ANDROID |
|
238 /** |
|
239 * Acquire a "global" ref on this object. |
|
240 * The default implementation just calls ref(), but subclasses can override |
|
241 * this method to implement additional behavior. |
|
242 */ |
|
243 virtual void globalRef(void* data=NULL); |
|
244 |
|
245 /** |
|
246 * Release a "global" ref on this object. |
|
247 * The default implementation just calls unref(), but subclasses can override |
|
248 * this method to implement additional behavior. |
|
249 */ |
|
250 virtual void globalUnref(); |
|
251 #endif |
|
252 |
|
253 SK_DEFINE_FLATTENABLE_TYPE(SkPixelRef) |
|
254 |
|
255 // Register a listener that may be called the next time our generation ID changes. |
|
256 // |
|
257 // We'll only call the listener if we're confident that we are the only SkPixelRef with this |
|
258 // generation ID. If our generation ID changes and we decide not to call the listener, we'll |
|
259 // never call it: you must add a new listener for each generation ID change. We also won't call |
|
260 // the listener when we're certain no one knows what our generation ID is. |
|
261 // |
|
262 // This can be used to invalidate caches keyed by SkPixelRef generation ID. |
|
263 struct GenIDChangeListener { |
|
264 virtual ~GenIDChangeListener() {} |
|
265 virtual void onChange() = 0; |
|
266 }; |
|
267 |
|
268 // Takes ownership of listener. |
|
269 void addGenIDChangeListener(GenIDChangeListener* listener); |
|
270 |
|
271 protected: |
|
272 /** |
|
273 * On success, returns true and fills out the LockRec for the pixels. On |
|
274 * failure returns false and ignores the LockRec parameter. |
|
275 * |
|
276 * The caller will have already acquired a mutex for thread safety, so this |
|
277 * method need not do that. |
|
278 */ |
|
279 virtual bool onNewLockPixels(LockRec*) = 0; |
|
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 */ |
|
289 virtual void onUnlockPixels() = 0; |
|
290 |
|
291 /** Default impl returns true */ |
|
292 virtual bool onLockPixelsAreWritable() const; |
|
293 |
|
294 // returns false; |
|
295 virtual bool onImplementsDecodeInto(); |
|
296 // returns false; |
|
297 virtual bool onDecodeInto(int pow2, SkBitmap* bitmap); |
|
298 |
|
299 /** |
|
300 * For pixelrefs that don't have access to their raw pixels, they may be |
|
301 * able to make a copy of them (e.g. if the pixels are on the GPU). |
|
302 * |
|
303 * The base class implementation returns false; |
|
304 */ |
|
305 virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subsetOrNull); |
|
306 |
|
307 // default impl returns NULL. |
|
308 virtual SkData* onRefEncodedData(); |
|
309 |
|
310 /** |
|
311 * Returns the size (in bytes) of the internally allocated memory. |
|
312 * This should be implemented in all serializable SkPixelRef derived classes. |
|
313 * SkBitmap::fPixelRefOffset + SkBitmap::getSafeSize() should never overflow this value, |
|
314 * otherwise the rendering code may attempt to read memory out of bounds. |
|
315 * |
|
316 * @return default impl returns 0. |
|
317 */ |
|
318 virtual size_t getAllocatedSizeInBytes() const; |
|
319 |
|
320 /** Return the mutex associated with this pixelref. This value is assigned |
|
321 in the constructor, and cannot change during the lifetime of the object. |
|
322 */ |
|
323 SkBaseMutex* mutex() const { return fMutex; } |
|
324 |
|
325 // serialization |
|
326 SkPixelRef(SkReadBuffer&, SkBaseMutex*); |
|
327 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
|
328 |
|
329 // only call from constructor. Flags this to always be locked, removing |
|
330 // the need to grab the mutex and call onLockPixels/onUnlockPixels. |
|
331 // Performance tweak to avoid those calls (esp. in multi-thread use case). |
|
332 void setPreLocked(void*, size_t rowBytes, SkColorTable*); |
|
333 |
|
334 private: |
|
335 SkBaseMutex* fMutex; // must remain in scope for the life of this object |
|
336 |
|
337 // mostly const. fInfo.fAlpahType can be changed at runtime. |
|
338 const SkImageInfo fInfo; |
|
339 |
|
340 // LockRec is only valid if we're in a locked state (isLocked()) |
|
341 LockRec fRec; |
|
342 int fLockCount; |
|
343 |
|
344 mutable uint32_t fGenerationID; |
|
345 mutable bool fUniqueGenerationID; |
|
346 |
|
347 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owned |
|
348 |
|
349 SkString fURI; |
|
350 |
|
351 // can go from false to true, but never from true to false |
|
352 bool fIsImmutable; |
|
353 // only ever set in constructor, const after that |
|
354 bool fPreLocked; |
|
355 |
|
356 void needsNewGenID(); |
|
357 void callGenIDChangeListeners(); |
|
358 |
|
359 void setMutex(SkBaseMutex* mutex); |
|
360 |
|
361 // When copying a bitmap to another with the same shape and config, we can safely |
|
362 // clone the pixelref generation ID too, which makes them equivalent under caching. |
|
363 friend class SkBitmap; // only for cloneGenID |
|
364 void cloneGenID(const SkPixelRef&); |
|
365 |
|
366 typedef SkFlattenable INHERITED; |
|
367 }; |
|
368 |
|
369 class SkPixelRefFactory : public SkRefCnt { |
|
370 public: |
|
371 /** |
|
372 * Allocate a new pixelref matching the specified ImageInfo, allocating |
|
373 * the memory for the pixels. If the ImageInfo requires a ColorTable, |
|
374 * the pixelref will ref() the colortable. |
|
375 * On failure return NULL. |
|
376 */ |
|
377 virtual SkPixelRef* create(const SkImageInfo&, SkColorTable*) = 0; |
|
378 }; |
|
379 |
|
380 #endif |