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 | |
herb | e6e41a8 | 2015-09-28 11:24:13 -0700 | [diff] [blame] | 11 | #include "../private/SkAtomics.h" |
bungeman | a7e9f05 | 2016-02-18 08:53:33 -0800 | [diff] [blame] | 12 | #include "../private/SkMutex.h" |
| 13 | #include "../private/SkTDArray.h" |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 14 | #include "SkBitmap.h" |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 15 | #include "SkFilterQuality.h" |
mtklein | 86821b5 | 2015-02-24 14:38:12 -0800 | [diff] [blame] | 16 | #include "SkImageInfo.h" |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 17 | #include "SkPixmap.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | #include "SkRefCnt.h" |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 19 | #include "SkSize.h" |
mtklein | 86821b5 | 2015-02-24 14:38:12 -0800 | [diff] [blame] | 20 | #include "SkString.h" |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 21 | #include "SkYUVSizeInfo.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | |
| 23 | class SkColorTable; |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 24 | struct SkIRect; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 26 | class GrTexture; |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 27 | class SkDiscardableMemory; |
reed@android.com | ce4e53a | 2010-09-09 16:01:26 +0000 | [diff] [blame] | 28 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | /** \class SkPixelRef |
| 30 | |
| 31 | This class is the smart container for pixel memory, and is used with |
| 32 | SkBitmap. A pixelref is installed into a bitmap, and then the bitmap can |
| 33 | access the actual pixel memory by calling lockPixels/unlockPixels. |
| 34 | |
| 35 | This class can be shared/accessed between multiple threads. |
| 36 | */ |
reed | ed45868 | 2014-07-14 09:21:31 -0700 | [diff] [blame] | 37 | class SK_API SkPixelRef : public SkRefCnt { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | public: |
reed@google.com | f171570 | 2013-12-06 22:07:17 +0000 | [diff] [blame] | 39 | explicit SkPixelRef(const SkImageInfo&); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 40 | virtual ~SkPixelRef(); |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 41 | |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 42 | const SkImageInfo& info() const { |
| 43 | return fInfo; |
| 44 | } |
| 45 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | /** Return the pixel memory returned from lockPixels, or null if the |
| 47 | lockCount is 0. |
| 48 | */ |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 49 | void* pixels() const { return fRec.fPixels; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | |
| 51 | /** Return the current colorTable (if any) if pixels are locked, or null. |
| 52 | */ |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 53 | SkColorTable* colorTable() const { return fRec.fColorTable; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 55 | size_t rowBytes() const { return fRec.fRowBytes; } |
| 56 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 57 | /** |
reed@google.com | 9271389 | 2014-01-03 17:58:57 +0000 | [diff] [blame] | 58 | * To access the actual pixels of a pixelref, it must be "locked". |
| 59 | * Calling lockPixels returns a LockRec struct (on success). |
| 60 | */ |
| 61 | struct LockRec { |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 62 | LockRec() : fPixels(NULL), fColorTable(NULL) {} |
| 63 | |
reed@google.com | 9271389 | 2014-01-03 17:58:57 +0000 | [diff] [blame] | 64 | void* fPixels; |
| 65 | SkColorTable* fColorTable; |
| 66 | size_t fRowBytes; |
skia.committer@gmail.com | 98272d9 | 2014-01-04 07:01:40 +0000 | [diff] [blame] | 67 | |
reed@google.com | 9271389 | 2014-01-03 17:58:57 +0000 | [diff] [blame] | 68 | void zero() { sk_bzero(this, sizeof(*this)); } |
| 69 | |
| 70 | bool isZero() const { |
| 71 | return NULL == fPixels && NULL == fColorTable && 0 == fRowBytes; |
| 72 | } |
| 73 | }; |
skia.committer@gmail.com | 98272d9 | 2014-01-04 07:01:40 +0000 | [diff] [blame] | 74 | |
mtklein | 96d68b7 | 2015-02-20 12:40:40 -0800 | [diff] [blame] | 75 | SkDEBUGCODE(bool isLocked() const { return fLockCount > 0; }) |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 76 | SkDEBUGCODE(int getLockCount() const { return fLockCount; }) |
| 77 | |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 78 | /** |
| 79 | * Call to access the pixel memory. Return true on success. Balance this |
| 80 | * with a call to unlockPixels(). |
| 81 | */ |
| 82 | bool lockPixels(); |
| 83 | |
| 84 | /** |
| 85 | * Call to access the pixel memory. On success, return true and fill out |
| 86 | * the specified rec. On failure, return false and ignore the rec parameter. |
| 87 | * Balance this with a call to unlockPixels(). |
| 88 | */ |
| 89 | bool lockPixels(LockRec* rec); |
| 90 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | /** Call to balanace a previous call to lockPixels(). Returns the pixels |
| 92 | (or null) after the unlock. NOTE: lock calls can be nested, but the |
| 93 | matching number of unlock calls must be made in order to free the |
| 94 | memory (if the subclass implements caching/deferred-decoding.) |
| 95 | */ |
| 96 | void unlockPixels(); |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 97 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 98 | /** |
| 99 | * Some bitmaps can return a copy of their pixels for lockPixels(), but |
| 100 | * that copy, if modified, will not be pushed back. These bitmaps should |
| 101 | * not be used as targets for a raster device/canvas (since all pixels |
| 102 | * modifications will be lost when unlockPixels() is called.) |
| 103 | */ |
| 104 | bool lockPixelsAreWritable() const; |
| 105 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | /** Returns a non-zero, unique value corresponding to the pixels in this |
| 107 | pixelref. Each time the pixels are changed (and notifyPixelsChanged is |
| 108 | called), a different generation ID will be returned. |
| 109 | */ |
| 110 | uint32_t getGenerationID() const; |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 111 | |
scroggo | f3ca41c | 2014-11-25 13:42:12 -0800 | [diff] [blame] | 112 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 113 | /** Returns a non-zero, unique value corresponding to this SkPixelRef. |
| 114 | Unlike the generation ID, this ID remains the same even when the pixels |
| 115 | are changed. IDs are not reused (until uint32_t wraps), so it is safe |
| 116 | to consider this ID unique even after this SkPixelRef is deleted. |
| 117 | |
| 118 | Can be used as a key which uniquely identifies this SkPixelRef |
| 119 | regardless of changes to its pixels or deletion of this object. |
| 120 | */ |
| 121 | uint32_t getStableID() const { return fStableID; } |
| 122 | #endif |
| 123 | |
commit-bot@chromium.org | 0e8d0d6 | 2014-01-27 15:41:07 +0000 | [diff] [blame] | 124 | /** |
| 125 | * Call this if you have changed the contents of the pixels. This will in- |
| 126 | * turn cause a different generation ID value to be returned from |
| 127 | * getGenerationID(). |
commit-bot@chromium.org | 0e8d0d6 | 2014-01-27 15:41:07 +0000 | [diff] [blame] | 128 | */ |
reed@google.com | c1587f9 | 2014-01-28 16:05:39 +0000 | [diff] [blame] | 129 | void notifyPixelsChanged(); |
| 130 | |
| 131 | /** |
| 132 | * Change the info's AlphaType. Note that this does not automatically |
| 133 | * invalidate the generation ID. If the pixel values themselves have |
| 134 | * changed, then you must explicitly call notifyPixelsChanged() as well. |
| 135 | */ |
| 136 | void changeAlphaType(SkAlphaType at); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | |
| 138 | /** Returns true if this pixelref is marked as immutable, meaning that the |
| 139 | contents of its pixels will not change for the lifetime of the pixelref. |
| 140 | */ |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 141 | bool isImmutable() const { return fMutability != kMutable; } |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 142 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | /** Marks this pixelref is immutable, meaning that the contents of its |
| 144 | pixels will not change for the lifetime of the pixelref. This state can |
| 145 | be set on a pixelref, but it cannot be cleared once it is set. |
| 146 | */ |
| 147 | void setImmutable(); |
| 148 | |
| 149 | /** Return the optional URI string associated with this pixelref. May be |
| 150 | null. |
| 151 | */ |
| 152 | const char* getURI() const { return fURI.size() ? fURI.c_str() : NULL; } |
| 153 | |
| 154 | /** Copy a URI string to this pixelref, or clear the URI if the uri is null |
| 155 | */ |
| 156 | void setURI(const char uri[]) { |
| 157 | fURI.set(uri); |
| 158 | } |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 159 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | /** Copy a URI string to this pixelref |
| 161 | */ |
| 162 | void setURI(const char uri[], size_t len) { |
| 163 | fURI.set(uri, len); |
| 164 | } |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 165 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | /** Assign a URI string to this pixelref. |
| 167 | */ |
| 168 | void setURI(const SkString& uri) { fURI = uri; } |
| 169 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 170 | struct LockRequest { |
| 171 | SkISize fSize; |
| 172 | SkFilterQuality fQuality; |
| 173 | }; |
| 174 | |
| 175 | struct LockResult { |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 176 | LockResult() : fPixels(NULL), fCTable(NULL) {} |
| 177 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 178 | void (*fUnlockProc)(void* ctx); |
| 179 | void* fUnlockContext; |
| 180 | |
reed | 11e833d | 2015-05-30 09:20:29 -0700 | [diff] [blame] | 181 | const void* fPixels; |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 182 | SkColorTable* fCTable; // should be NULL unless colortype is kIndex8 |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 183 | size_t fRowBytes; |
| 184 | SkISize fSize; |
| 185 | |
| 186 | void unlock() { |
| 187 | if (fUnlockProc) { |
| 188 | fUnlockProc(fUnlockContext); |
| 189 | fUnlockProc = NULL; // can't unlock twice! |
| 190 | } |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | bool requestLock(const LockRequest&, LockResult*); |
| 195 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 196 | /** |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 197 | * If this can efficiently return YUV data, this should return true. |
| 198 | * Otherwise this returns false and does not modify any of the parameters. |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 199 | * |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 200 | * @param sizeInfo Output parameter indicating the sizes and required |
| 201 | * allocation widths of the Y, U, and V planes. |
| 202 | * @param colorSpace Output parameter. |
msarett | 095d31c | 2016-03-07 08:39:12 -0800 | [diff] [blame] | 203 | */ |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 204 | bool queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const { |
| 205 | return this->onQueryYUV8(sizeInfo, colorSpace); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Returns true on success and false on failure. |
| 210 | * Copies YUV data into the provided YUV planes. |
| 211 | * |
| 212 | * @param sizeInfo Needs to exactly match the values returned by the |
| 213 | * query, except the WidthBytes may be larger than the |
| 214 | * recommendation (but not smaller). |
| 215 | * @param planes Memory for each of the Y, U, and V planes. |
| 216 | */ |
| 217 | bool getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) { |
| 218 | return this->onGetYUV8Planes(sizeInfo, planes); |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 219 | } |
| 220 | |
bsalomon | 9d22fd6 | 2016-01-11 11:14:17 -0800 | [diff] [blame] | 221 | /** Populates dst with the pixels of this pixelRef, converting them to colorType. */ |
| 222 | bool readPixels(SkBitmap* dst, SkColorType colorType, const SkIRect* subset = NULL); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 223 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 224 | // Register a listener that may be called the next time our generation ID changes. |
| 225 | // |
| 226 | // We'll only call the listener if we're confident that we are the only SkPixelRef with this |
| 227 | // generation ID. If our generation ID changes and we decide not to call the listener, we'll |
| 228 | // never call it: you must add a new listener for each generation ID change. We also won't call |
| 229 | // the listener when we're certain no one knows what our generation ID is. |
| 230 | // |
| 231 | // This can be used to invalidate caches keyed by SkPixelRef generation ID. |
| 232 | struct GenIDChangeListener { |
| 233 | virtual ~GenIDChangeListener() {} |
| 234 | virtual void onChange() = 0; |
| 235 | }; |
| 236 | |
| 237 | // Takes ownership of listener. |
| 238 | void addGenIDChangeListener(GenIDChangeListener* listener); |
| 239 | |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 240 | // Call when this pixelref is part of the key to a resourcecache entry. This allows the cache |
| 241 | // to know automatically those entries can be purged when this pixelref is changed or deleted. |
| 242 | void notifyAddedToCache() { |
mtklein | 6101077 | 2015-02-25 08:27:41 -0800 | [diff] [blame] | 243 | fAddedToCache.store(true); |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 244 | } |
| 245 | |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 246 | virtual SkDiscardableMemory* diagnostic_only_getDiscardable() const { return NULL; } |
| 247 | |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 248 | /** |
| 249 | * Returns true if the pixels are generated on-the-fly (when required). |
| 250 | */ |
| 251 | bool isLazyGenerated() const { return this->onIsLazyGenerated(); } |
| 252 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 253 | protected: |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 254 | /** |
| 255 | * On success, returns true and fills out the LockRec for the pixels. On |
| 256 | * failure returns false and ignores the LockRec parameter. |
| 257 | * |
| 258 | * The caller will have already acquired a mutex for thread safety, so this |
| 259 | * method need not do that. |
| 260 | */ |
| 261 | virtual bool onNewLockPixels(LockRec*) = 0; |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 262 | |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 263 | /** |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 264 | * Balancing the previous successful call to onNewLockPixels. The locked |
| 265 | * pixel address will no longer be referenced, so the subclass is free to |
| 266 | * move or discard that memory. |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 267 | * |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 268 | * The caller will have already acquired a mutex for thread safety, so this |
| 269 | * method need not do that. |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 270 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | virtual void onUnlockPixels() = 0; |
| 272 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 273 | /** Default impl returns true */ |
| 274 | virtual bool onLockPixelsAreWritable() const; |
| 275 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 276 | /** |
| 277 | * For pixelrefs that don't have access to their raw pixels, they may be |
| 278 | * able to make a copy of them (e.g. if the pixels are on the GPU). |
| 279 | * |
| 280 | * The base class implementation returns false; |
| 281 | */ |
bsalomon | 9d22fd6 | 2016-01-11 11:14:17 -0800 | [diff] [blame] | 282 | virtual bool onReadPixels(SkBitmap* dst, SkColorType colorType, const SkIRect* subsetOrNull); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 283 | |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 284 | // default impl does nothing. |
| 285 | virtual void onNotifyPixelsChanged(); |
| 286 | |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 287 | virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const { |
| 288 | return false; |
| 289 | } |
| 290 | virtual bool onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) { |
| 291 | return false; |
| 292 | } |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 293 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 294 | /** |
| 295 | * Returns the size (in bytes) of the internally allocated memory. |
| 296 | * This should be implemented in all serializable SkPixelRef derived classes. |
| 297 | * SkBitmap::fPixelRefOffset + SkBitmap::getSafeSize() should never overflow this value, |
| 298 | * otherwise the rendering code may attempt to read memory out of bounds. |
| 299 | * |
| 300 | * @return default impl returns 0. |
| 301 | */ |
| 302 | virtual size_t getAllocatedSizeInBytes() const; |
| 303 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 304 | virtual bool onRequestLock(const LockRequest&, LockResult*); |
| 305 | |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 306 | virtual bool onIsLazyGenerated() const { return false; } |
| 307 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 308 | /** Return the mutex associated with this pixelref. This value is assigned |
| 309 | in the constructor, and cannot change during the lifetime of the object. |
| 310 | */ |
sclittle | d9f5d20 | 2016-05-04 18:23:30 -0700 | [diff] [blame] | 311 | SkBaseMutex* mutex() const { return &fMutex; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 312 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 313 | // only call from constructor. Flags this to always be locked, removing |
| 314 | // the need to grab the mutex and call onLockPixels/onUnlockPixels. |
| 315 | // 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] | 316 | void setPreLocked(void*, size_t rowBytes, SkColorTable*); |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 317 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 318 | private: |
mtklein | 7e6d9c0 | 2015-08-13 14:02:06 -0700 | [diff] [blame] | 319 | mutable SkMutex fMutex; |
reed@google.com | 33cc989 | 2014-01-02 18:07:46 +0000 | [diff] [blame] | 320 | |
commit-bot@chromium.org | 0e8d0d6 | 2014-01-27 15:41:07 +0000 | [diff] [blame] | 321 | // mostly const. fInfo.fAlpahType can be changed at runtime. |
reed@google.com | 33cc989 | 2014-01-02 18:07:46 +0000 | [diff] [blame] | 322 | const SkImageInfo fInfo; |
robertphillips@google.com | 0daa1ad | 2013-12-13 15:24:37 +0000 | [diff] [blame] | 323 | |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 324 | // LockRec is only valid if we're in a locked state (isLocked()) |
| 325 | LockRec fRec; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 326 | int fLockCount; |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 327 | |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 328 | bool lockPixelsInsideMutex(); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 329 | |
mtklein | 63d0024 | 2015-02-25 09:10:57 -0800 | [diff] [blame] | 330 | // Bottom bit indicates the Gen ID is unique. |
| 331 | bool genIDIsUnique() const { return SkToBool(fTaggedGenID.load() & 1); } |
| 332 | mutable SkAtomic<uint32_t> fTaggedGenID; |
| 333 | |
scroggo | f3ca41c | 2014-11-25 13:42:12 -0800 | [diff] [blame] | 334 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 335 | const uint32_t fStableID; |
| 336 | #endif |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 337 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 338 | SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owned |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 339 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | SkString fURI; |
| 341 | |
mtklein | 63d0024 | 2015-02-25 09:10:57 -0800 | [diff] [blame] | 342 | // Set true by caches when they cache content that's derived from the current pixels. |
| 343 | SkAtomic<bool> fAddedToCache; |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 344 | |
| 345 | enum { |
| 346 | kMutable, // PixelRefs begin mutable. |
| 347 | kTemporarilyImmutable, // Considered immutable, but can revert to mutable. |
| 348 | kImmutable, // Once set to this state, it never leaves. |
| 349 | } fMutability : 8; // easily fits inside a byte |
| 350 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 351 | // only ever set in constructor, const after that |
mtklein | 63d0024 | 2015-02-25 09:10:57 -0800 | [diff] [blame] | 352 | bool fPreLocked; |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 353 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 354 | void needsNewGenID(); |
| 355 | void callGenIDChangeListeners(); |
| 356 | |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 357 | void setTemporarilyImmutable(); |
| 358 | void restoreMutability(); |
| 359 | friend class SkSurface_Raster; // For the two methods above. |
| 360 | |
reed | 02d91d1 | 2015-07-30 20:13:43 -0700 | [diff] [blame] | 361 | bool isPreLocked() const { return fPreLocked; } |
| 362 | friend class SkImage_Raster; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 363 | friend class SkSpecialImage_Raster; |
reed | 02d91d1 | 2015-07-30 20:13:43 -0700 | [diff] [blame] | 364 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 365 | // When copying a bitmap to another with the same shape and config, we can safely |
| 366 | // clone the pixelref generation ID too, which makes them equivalent under caching. |
| 367 | friend class SkBitmap; // only for cloneGenID |
| 368 | void cloneGenID(const SkPixelRef&); |
| 369 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 370 | void setImmutableWithID(uint32_t genID); |
| 371 | friend class SkImage_Gpu; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 372 | friend class SkImageCacherator; |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 373 | friend class SkSpecialImage_Gpu; |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 374 | |
reed | ed45868 | 2014-07-14 09:21:31 -0700 | [diff] [blame] | 375 | typedef SkRefCnt INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 376 | }; |
| 377 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 378 | class SkPixelRefFactory : public SkRefCnt { |
| 379 | public: |
| 380 | /** |
| 381 | * Allocate a new pixelref matching the specified ImageInfo, allocating |
| 382 | * the memory for the pixels. If the ImageInfo requires a ColorTable, |
| 383 | * the pixelref will ref() the colortable. |
| 384 | * On failure return NULL. |
| 385 | */ |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 386 | virtual SkPixelRef* create(const SkImageInfo&, size_t rowBytes, SkColorTable*) = 0; |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 389 | #endif |