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