blob: 3898269ef458e16440ae09006ceb34fbe199f16f [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2008 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkPixelRef_DEFINED
9#define SkPixelRef_DEFINED
10
mtklein86821b52015-02-24 14:38:12 -080011#include "SkAtomics.h"
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000012#include "SkBitmap.h"
reed92fc2ae2015-05-22 08:06:21 -070013#include "SkFilterQuality.h"
mtklein86821b52015-02-24 14:38:12 -080014#include "SkImageInfo.h"
mtkleine72a80d2015-02-09 14:47:06 -080015#include "SkMutex.h"
reed92fc2ae2015-05-22 08:06:21 -070016#include "SkPixmap.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkRefCnt.h"
sugoi518d83d2014-07-21 11:37:39 -070018#include "SkSize.h"
mtklein86821b52015-02-24 14:38:12 -080019#include "SkString.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000020#include "SkTDArray.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
reed@google.com2a3f08b2012-12-13 21:41:00 +000022#ifdef SK_DEBUG
23 /**
24 * Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref
25 * subclasses to correctly handle lock/unlock pixels. For performance
26 * reasons, simple malloc-based subclasses call setPreLocked() to skip
27 * the overhead of implementing these calls.
28 *
29 * This build-flag disables that optimization, to add in debugging our
30 * call-sites, to ensure that they correctly balance their calls of
31 * lock and unlock.
32 */
33// #define SK_IGNORE_PIXELREF_SETPRELOCKED
34#endif
35
reed@android.com8a1c16f2008-12-17 15:59:43 +000036class SkColorTable;
reed@google.comeb776122012-12-06 14:26:02 +000037class SkData;
reed@google.com50dfa012011-04-01 19:05:36 +000038struct SkIRect;
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +000040class GrTexture;
reed@android.comce4e53a2010-09-09 16:01:26 +000041
reed@android.com8a1c16f2008-12-17 15:59:43 +000042/** \class SkPixelRef
43
44 This class is the smart container for pixel memory, and is used with
45 SkBitmap. A pixelref is installed into a bitmap, and then the bitmap can
46 access the actual pixel memory by calling lockPixels/unlockPixels.
47
48 This class can be shared/accessed between multiple threads.
49*/
reeded458682014-07-14 09:21:31 -070050class SK_API SkPixelRef : public SkRefCnt {
reed@android.com8a1c16f2008-12-17 15:59:43 +000051public:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000052 SK_DECLARE_INST_COUNT(SkPixelRef)
53
reed@google.comf1715702013-12-06 22:07:17 +000054 explicit SkPixelRef(const SkImageInfo&);
reed@google.comb0eb1022013-12-06 22:16:10 +000055 SkPixelRef(const SkImageInfo&, SkBaseMutex* mutex);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000056 virtual ~SkPixelRef();
reed@google.com93c5f9e2011-02-24 18:09:46 +000057
reed@google.com9230ea22013-12-09 22:01:03 +000058 const SkImageInfo& info() const {
59 return fInfo;
60 }
61
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 /** Return the pixel memory returned from lockPixels, or null if the
63 lockCount is 0.
64 */
reed@google.comd0419b12014-01-06 17:08:27 +000065 void* pixels() const { return fRec.fPixels; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000066
67 /** Return the current colorTable (if any) if pixels are locked, or null.
68 */
reed@google.comd0419b12014-01-06 17:08:27 +000069 SkColorTable* colorTable() const { return fRec.fColorTable; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
reed@google.com672588b2014-01-08 15:42:01 +000071 size_t rowBytes() const { return fRec.fRowBytes; }
72
reed@google.comff0da4f2012-05-17 13:14:52 +000073 /**
reed@google.com92713892014-01-03 17:58:57 +000074 * To access the actual pixels of a pixelref, it must be "locked".
75 * Calling lockPixels returns a LockRec struct (on success).
76 */
77 struct LockRec {
reeddb74f622015-05-30 13:41:15 -070078 LockRec() : fPixels(NULL), fColorTable(NULL) {}
79
reed@google.com92713892014-01-03 17:58:57 +000080 void* fPixels;
81 SkColorTable* fColorTable;
82 size_t fRowBytes;
skia.committer@gmail.com98272d92014-01-04 07:01:40 +000083
reed@google.com92713892014-01-03 17:58:57 +000084 void zero() { sk_bzero(this, sizeof(*this)); }
85
86 bool isZero() const {
87 return NULL == fPixels && NULL == fColorTable && 0 == fRowBytes;
88 }
89 };
skia.committer@gmail.com98272d92014-01-04 07:01:40 +000090
mtklein96d68b72015-02-20 12:40:40 -080091 SkDEBUGCODE(bool isLocked() const { return fLockCount > 0; })
reed@google.comea033602012-12-14 13:13:55 +000092 SkDEBUGCODE(int getLockCount() const { return fLockCount; })
93
reed@google.comd0419b12014-01-06 17:08:27 +000094 /**
95 * Call to access the pixel memory. Return true on success. Balance this
96 * with a call to unlockPixels().
97 */
98 bool lockPixels();
99
100 /**
101 * Call to access the pixel memory. On success, return true and fill out
102 * the specified rec. On failure, return false and ignore the rec parameter.
103 * Balance this with a call to unlockPixels().
104 */
105 bool lockPixels(LockRec* rec);
106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 /** Call to balanace a previous call to lockPixels(). Returns the pixels
108 (or null) after the unlock. NOTE: lock calls can be nested, but the
109 matching number of unlock calls must be made in order to free the
110 memory (if the subclass implements caching/deferred-decoding.)
111 */
112 void unlockPixels();
reed@google.com93c5f9e2011-02-24 18:09:46 +0000113
reed@google.com9c49bc32011-07-07 13:42:37 +0000114 /**
115 * Some bitmaps can return a copy of their pixels for lockPixels(), but
116 * that copy, if modified, will not be pushed back. These bitmaps should
117 * not be used as targets for a raster device/canvas (since all pixels
118 * modifications will be lost when unlockPixels() is called.)
119 */
120 bool lockPixelsAreWritable() const;
121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 /** Returns a non-zero, unique value corresponding to the pixels in this
123 pixelref. Each time the pixels are changed (and notifyPixelsChanged is
124 called), a different generation ID will be returned.
125 */
126 uint32_t getGenerationID() const;
reed@google.com93c5f9e2011-02-24 18:09:46 +0000127
scroggof3ca41c2014-11-25 13:42:12 -0800128#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
129 /** Returns a non-zero, unique value corresponding to this SkPixelRef.
130 Unlike the generation ID, this ID remains the same even when the pixels
131 are changed. IDs are not reused (until uint32_t wraps), so it is safe
132 to consider this ID unique even after this SkPixelRef is deleted.
133
134 Can be used as a key which uniquely identifies this SkPixelRef
135 regardless of changes to its pixels or deletion of this object.
136 */
137 uint32_t getStableID() const { return fStableID; }
138#endif
139
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000140 /**
141 * Call this if you have changed the contents of the pixels. This will in-
142 * turn cause a different generation ID value to be returned from
143 * getGenerationID().
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000144 */
reed@google.comc1587f92014-01-28 16:05:39 +0000145 void notifyPixelsChanged();
146
147 /**
148 * Change the info's AlphaType. Note that this does not automatically
149 * invalidate the generation ID. If the pixel values themselves have
150 * changed, then you must explicitly call notifyPixelsChanged() as well.
151 */
152 void changeAlphaType(SkAlphaType at);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153
154 /** Returns true if this pixelref is marked as immutable, meaning that the
155 contents of its pixels will not change for the lifetime of the pixelref.
156 */
157 bool isImmutable() const { return fIsImmutable; }
reed@google.com93c5f9e2011-02-24 18:09:46 +0000158
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 /** Marks this pixelref is immutable, meaning that the contents of its
160 pixels will not change for the lifetime of the pixelref. This state can
161 be set on a pixelref, but it cannot be cleared once it is set.
162 */
163 void setImmutable();
164
165 /** Return the optional URI string associated with this pixelref. May be
166 null.
167 */
168 const char* getURI() const { return fURI.size() ? fURI.c_str() : NULL; }
169
170 /** Copy a URI string to this pixelref, or clear the URI if the uri is null
171 */
172 void setURI(const char uri[]) {
173 fURI.set(uri);
174 }
reed@google.com93c5f9e2011-02-24 18:09:46 +0000175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 /** Copy a URI string to this pixelref
177 */
178 void setURI(const char uri[], size_t len) {
179 fURI.set(uri, len);
180 }
reed@google.com93c5f9e2011-02-24 18:09:46 +0000181
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 /** Assign a URI string to this pixelref.
183 */
184 void setURI(const SkString& uri) { fURI = uri; }
185
reed@google.comeb776122012-12-06 14:26:02 +0000186 /**
187 * If the pixelRef has an encoded (i.e. compressed) representation,
188 * return a ref to its data. If the pixelRef
189 * is uncompressed or otherwise does not have this form, return NULL.
190 *
191 * If non-null is returned, the caller is responsible for calling unref()
192 * on the data when it is finished.
193 */
194 SkData* refEncodedData() {
195 return this->onRefEncodedData();
196 }
197
reed92fc2ae2015-05-22 08:06:21 -0700198 struct LockRequest {
199 SkISize fSize;
200 SkFilterQuality fQuality;
201 };
202
203 struct LockResult {
reeddb74f622015-05-30 13:41:15 -0700204 LockResult() : fPixels(NULL), fCTable(NULL) {}
205
reed92fc2ae2015-05-22 08:06:21 -0700206 void (*fUnlockProc)(void* ctx);
207 void* fUnlockContext;
208
reed11e833d2015-05-30 09:20:29 -0700209 const void* fPixels;
reeddb74f622015-05-30 13:41:15 -0700210 SkColorTable* fCTable; // should be NULL unless colortype is kIndex8
reed92fc2ae2015-05-22 08:06:21 -0700211 size_t fRowBytes;
212 SkISize fSize;
213
214 void unlock() {
215 if (fUnlockProc) {
216 fUnlockProc(fUnlockContext);
217 fUnlockProc = NULL; // can't unlock twice!
218 }
219 }
220 };
221
222 bool requestLock(const LockRequest&, LockResult*);
223
reed@android.comce4e53a2010-09-09 16:01:26 +0000224 /** Are we really wrapping a texture instead of a bitmap?
225 */
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000226 virtual GrTexture* getTexture() { return NULL; }
reed@android.comce4e53a2010-09-09 16:01:26 +0000227
sugoi518d83d2014-07-21 11:37:39 -0700228 /**
229 * If any planes or rowBytes is NULL, this should output the sizes and return true
230 * if it can efficiently return YUV planar data. If it cannot, it should return false.
231 *
232 * If all planes and rowBytes are not NULL, then it should copy the associated Y,U,V data
233 * into those planes of memory supplied by the caller. It should validate that the sizes
234 * match what it expected. If the sizes do not match, it should return false.
rileyaabaef862014-09-12 17:45:58 -0700235 *
236 * If colorSpace is not NULL, the YUV color space of the data should be stored in the address
237 * it points at.
sugoi518d83d2014-07-21 11:37:39 -0700238 */
rileyaabaef862014-09-12 17:45:58 -0700239 bool getYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
240 SkYUVColorSpace* colorSpace) {
241 return this->onGetYUV8Planes(sizes, planes, rowBytes, colorSpace);
sugoi518d83d2014-07-21 11:37:39 -0700242 }
243
reed@google.com50dfa012011-04-01 19:05:36 +0000244 bool readPixels(SkBitmap* dst, const SkIRect* subset = NULL);
245
scroggo@google.coma2a31922012-12-07 19:14:45 +0000246 /**
247 * Makes a deep copy of this PixelRef, respecting the requested config.
reede4538f52014-06-11 06:09:50 -0700248 * @param colorType Desired colortype.
jvanverthfa1e8a72014-12-22 08:31:49 -0800249 * @param profileType Desired colorprofiletype.
scroggo@google.coma2a31922012-12-07 19:14:45 +0000250 * @param subset Subset of this PixelRef to copy. Must be fully contained within the bounds of
251 * of this PixelRef.
252 * @return A new SkPixelRef, or NULL if either there is an error (e.g. the destination could
253 * not be created with the given config), or this PixelRef does not support deep
254 * copies.
255 */
jvanverthfa1e8a72014-12-22 08:31:49 -0800256 virtual SkPixelRef* deepCopy(SkColorType, SkColorProfileType, const SkIRect* /*subset*/) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000257 return NULL;
258 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000259
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000260 // Register a listener that may be called the next time our generation ID changes.
261 //
262 // We'll only call the listener if we're confident that we are the only SkPixelRef with this
263 // generation ID. If our generation ID changes and we decide not to call the listener, we'll
264 // never call it: you must add a new listener for each generation ID change. We also won't call
265 // the listener when we're certain no one knows what our generation ID is.
266 //
267 // This can be used to invalidate caches keyed by SkPixelRef generation ID.
268 struct GenIDChangeListener {
269 virtual ~GenIDChangeListener() {}
270 virtual void onChange() = 0;
271 };
272
273 // Takes ownership of listener.
274 void addGenIDChangeListener(GenIDChangeListener* listener);
275
reed83787d02015-02-25 07:17:11 -0800276 // Call when this pixelref is part of the key to a resourcecache entry. This allows the cache
277 // to know automatically those entries can be purged when this pixelref is changed or deleted.
278 void notifyAddedToCache() {
mtklein61010772015-02-25 08:27:41 -0800279 fAddedToCache.store(true);
reed83787d02015-02-25 07:17:11 -0800280 }
281
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282protected:
reed@google.comd0419b12014-01-06 17:08:27 +0000283 /**
284 * On success, returns true and fills out the LockRec for the pixels. On
285 * failure returns false and ignores the LockRec parameter.
286 *
287 * The caller will have already acquired a mutex for thread safety, so this
288 * method need not do that.
289 */
290 virtual bool onNewLockPixels(LockRec*) = 0;
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000291
reed@google.comc83a91f2013-12-13 13:41:14 +0000292 /**
reed@google.comd0419b12014-01-06 17:08:27 +0000293 * Balancing the previous successful call to onNewLockPixels. The locked
294 * pixel address will no longer be referenced, so the subclass is free to
295 * move or discard that memory.
reed@google.comc83a91f2013-12-13 13:41:14 +0000296 *
reed@google.comd0419b12014-01-06 17:08:27 +0000297 * The caller will have already acquired a mutex for thread safety, so this
298 * method need not do that.
reed@google.comc83a91f2013-12-13 13:41:14 +0000299 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 virtual void onUnlockPixels() = 0;
301
reed@google.com9c49bc32011-07-07 13:42:37 +0000302 /** Default impl returns true */
303 virtual bool onLockPixelsAreWritable() const;
304
reed@google.com50dfa012011-04-01 19:05:36 +0000305 /**
306 * For pixelrefs that don't have access to their raw pixels, they may be
307 * able to make a copy of them (e.g. if the pixels are on the GPU).
308 *
309 * The base class implementation returns false;
310 */
311 virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subsetOrNull);
312
reed@google.comeb776122012-12-06 14:26:02 +0000313 // default impl returns NULL.
314 virtual SkData* onRefEncodedData();
315
sugoi518d83d2014-07-21 11:37:39 -0700316 // default impl returns false.
rileyaabaef862014-09-12 17:45:58 -0700317 virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
318 SkYUVColorSpace* colorSpace);
sugoi518d83d2014-07-21 11:37:39 -0700319
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000320 /**
321 * Returns the size (in bytes) of the internally allocated memory.
322 * This should be implemented in all serializable SkPixelRef derived classes.
323 * SkBitmap::fPixelRefOffset + SkBitmap::getSafeSize() should never overflow this value,
324 * otherwise the rendering code may attempt to read memory out of bounds.
325 *
326 * @return default impl returns 0.
327 */
328 virtual size_t getAllocatedSizeInBytes() const;
329
reed92fc2ae2015-05-22 08:06:21 -0700330 virtual bool onRequestLock(const LockRequest&, LockResult*);
331
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 /** Return the mutex associated with this pixelref. This value is assigned
333 in the constructor, and cannot change during the lifetime of the object.
334 */
digit@google.com1771cbf2012-01-26 21:26:40 +0000335 SkBaseMutex* mutex() const { return fMutex; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000336
reed@google.comff0da4f2012-05-17 13:14:52 +0000337 // only call from constructor. Flags this to always be locked, removing
338 // the need to grab the mutex and call onLockPixels/onUnlockPixels.
339 // Performance tweak to avoid those calls (esp. in multi-thread use case).
reed@google.comd0419b12014-01-06 17:08:27 +0000340 void setPreLocked(void*, size_t rowBytes, SkColorTable*);
reed@google.comff0da4f2012-05-17 13:14:52 +0000341
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342private:
digit@google.com1771cbf2012-01-26 21:26:40 +0000343 SkBaseMutex* fMutex; // must remain in scope for the life of this object
reed@google.com33cc9892014-01-02 18:07:46 +0000344
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000345 // mostly const. fInfo.fAlpahType can be changed at runtime.
reed@google.com33cc9892014-01-02 18:07:46 +0000346 const SkImageInfo fInfo;
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +0000347
reed@google.comd0419b12014-01-06 17:08:27 +0000348 // LockRec is only valid if we're in a locked state (isLocked())
349 LockRec fRec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350 int fLockCount;
reed@google.com93c5f9e2011-02-24 18:09:46 +0000351
reeddb74f622015-05-30 13:41:15 -0700352 bool lockPixelsInsideMutex();
reed92fc2ae2015-05-22 08:06:21 -0700353
mtklein63d00242015-02-25 09:10:57 -0800354 // Bottom bit indicates the Gen ID is unique.
355 bool genIDIsUnique() const { return SkToBool(fTaggedGenID.load() & 1); }
356 mutable SkAtomic<uint32_t> fTaggedGenID;
357
scroggof3ca41c2014-11-25 13:42:12 -0800358#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
359 const uint32_t fStableID;
360#endif
reed@google.com93c5f9e2011-02-24 18:09:46 +0000361
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000362 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owned
scroggo@google.comd5764e82012-08-22 15:00:05 +0000363
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364 SkString fURI;
365
mtklein63d00242015-02-25 09:10:57 -0800366 // Set true by caches when they cache content that's derived from the current pixels.
367 SkAtomic<bool> fAddedToCache;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000368 // can go from false to true, but never from true to false
mtklein63d00242015-02-25 09:10:57 -0800369 bool fIsImmutable;
reed@google.comff0da4f2012-05-17 13:14:52 +0000370 // only ever set in constructor, const after that
mtklein63d00242015-02-25 09:10:57 -0800371 bool fPreLocked;
reed@google.comff0da4f2012-05-17 13:14:52 +0000372
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000373 void needsNewGenID();
374 void callGenIDChangeListeners();
375
reed@google.comff0da4f2012-05-17 13:14:52 +0000376 void setMutex(SkBaseMutex* mutex);
caryclark@google.com9d0c6ec2011-12-20 20:26:56 +0000377
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000378 // When copying a bitmap to another with the same shape and config, we can safely
379 // clone the pixelref generation ID too, which makes them equivalent under caching.
380 friend class SkBitmap; // only for cloneGenID
381 void cloneGenID(const SkPixelRef&);
382
reeded458682014-07-14 09:21:31 -0700383 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000384};
385
reed@google.com9ebcac52014-01-24 18:53:42 +0000386class SkPixelRefFactory : public SkRefCnt {
387public:
388 /**
389 * Allocate a new pixelref matching the specified ImageInfo, allocating
390 * the memory for the pixels. If the ImageInfo requires a ColorTable,
391 * the pixelref will ref() the colortable.
392 * On failure return NULL.
393 */
reedf0aed972014-07-01 12:48:11 -0700394 virtual SkPixelRef* create(const SkImageInfo&, size_t rowBytes, SkColorTable*) = 0;
reed@google.com9ebcac52014-01-24 18:53:42 +0000395};
396
reed@android.com8a1c16f2008-12-17 15:59:43 +0000397#endif