epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkPixelRef.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 9 | #include "SkFlattenableBuffers.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkThread.h" |
| 11 | |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 12 | SK_DEFINE_INST_COUNT(SkPixelRef) |
| 13 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 14 | // must be a power-of-2. undef to just use 1 mutex |
| 15 | #define PIXELREF_MUTEX_RING_COUNT 32 |
| 16 | |
| 17 | #ifdef PIXELREF_MUTEX_RING_COUNT |
| 18 | static int32_t gPixelRefMutexRingIndex; |
| 19 | static SK_DECLARE_MUTEX_ARRAY(gPixelRefMutexRing, PIXELREF_MUTEX_RING_COUNT); |
| 20 | #else |
| 21 | SK_DECLARE_STATIC_MUTEX(gPixelRefMutex); |
| 22 | #endif |
| 23 | |
caryclark@google.com | 803eceb | 2012-06-06 12:09:34 +0000 | [diff] [blame] | 24 | static SkBaseMutex* get_default_mutex() { |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 25 | #ifdef PIXELREF_MUTEX_RING_COUNT |
| 26 | // atomic_inc might be overkill here. It may be fine if once in a while |
| 27 | // we hit a race-condition and two subsequent calls get the same index... |
| 28 | int index = sk_atomic_inc(&gPixelRefMutexRingIndex); |
| 29 | return &gPixelRefMutexRing[index & (PIXELREF_MUTEX_RING_COUNT - 1)]; |
| 30 | #else |
| 31 | return &gPixelRefMutex; |
| 32 | #endif |
| 33 | } |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 36 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 37 | int32_t SkNextPixelRefGenerationID() { |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 38 | static int32_t gPixelRefGenerationID; |
| 39 | // do a loop in case our global wraps around, as we never want to |
| 40 | // return a 0 |
| 41 | int32_t genID; |
| 42 | do { |
| 43 | genID = sk_atomic_inc(&gPixelRefGenerationID) + 1; |
| 44 | } while (0 == genID); |
| 45 | return genID; |
| 46 | } |
| 47 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 48 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 50 | void SkPixelRef::setMutex(SkBaseMutex* mutex) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | if (NULL == mutex) { |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 52 | mutex = get_default_mutex(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | } |
| 54 | fMutex = mutex; |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // just need a > 0 value, so pick a funny one to aid in debugging |
| 58 | #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 |
| 59 | |
| 60 | SkPixelRef::SkPixelRef(SkBaseMutex* mutex) : fPreLocked(false) { |
| 61 | this->setMutex(mutex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | fPixels = NULL; |
| 63 | fColorTable = NULL; // we do not track ownership of this |
| 64 | fLockCount = 0; |
| 65 | fGenerationID = 0; // signal to rebuild |
| 66 | fIsImmutable = false; |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 67 | fPreLocked = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | } |
| 69 | |
djsollen@google.com | 5370cd9 | 2012-03-28 20:47:01 +0000 | [diff] [blame] | 70 | SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) |
| 71 | : INHERITED(buffer) { |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 72 | this->setMutex(mutex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | fPixels = NULL; |
| 74 | fColorTable = NULL; // we do not track ownership of this |
| 75 | fLockCount = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | fIsImmutable = buffer.readBool(); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 77 | fGenerationID = buffer.readUInt(); |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 78 | fPreLocked = false; |
| 79 | } |
| 80 | |
| 81 | void SkPixelRef::setPreLocked(void* pixels, SkColorTable* ctable) { |
| 82 | // only call me in your constructor, otherwise fLockCount tracking can get |
| 83 | // out of sync. |
| 84 | fPixels = pixels; |
| 85 | fColorTable = ctable; |
| 86 | fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; |
| 87 | fPreLocked = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | } |
| 89 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 90 | void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { |
djsollen@google.com | 5370cd9 | 2012-03-28 20:47:01 +0000 | [diff] [blame] | 91 | this->INHERITED::flatten(buffer); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | buffer.writeBool(fIsImmutable); |
bsalomon@google.com | 7aa876b | 2012-06-21 20:13:04 +0000 | [diff] [blame] | 93 | // We write the gen ID into the picture for within-process recording. This |
| 94 | // is safe since the same genID will never refer to two different sets of |
| 95 | // pixels (barring overflow). However, each process has its own "namespace" |
| 96 | // of genIDs. So for cross-process recording we write a zero which will |
| 97 | // trigger assignment of a new genID in playback. |
| 98 | if (buffer.isCrossProcess()) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 99 | buffer.writeUInt(0); |
bsalomon@google.com | 7aa876b | 2012-06-21 20:13:04 +0000 | [diff] [blame] | 100 | } else { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 101 | buffer.writeUInt(fGenerationID); |
bsalomon@google.com | 7aa876b | 2012-06-21 20:13:04 +0000 | [diff] [blame] | 102 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void SkPixelRef::lockPixels() { |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 106 | SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 107 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 108 | if (!fPreLocked) { |
| 109 | SkAutoMutexAcquire ac(*fMutex); |
| 110 | |
| 111 | if (1 == ++fLockCount) { |
| 112 | fPixels = this->onLockPixels(&fColorTable); |
| 113 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | void SkPixelRef::unlockPixels() { |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 118 | SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 119 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 120 | if (!fPreLocked) { |
| 121 | SkAutoMutexAcquire ac(*fMutex); |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 122 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 123 | SkASSERT(fLockCount > 0); |
| 124 | if (0 == --fLockCount) { |
| 125 | this->onUnlockPixels(); |
| 126 | fPixels = NULL; |
| 127 | fColorTable = NULL; |
| 128 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 132 | bool SkPixelRef::lockPixelsAreWritable() const { |
| 133 | return this->onLockPixelsAreWritable(); |
| 134 | } |
| 135 | |
| 136 | bool SkPixelRef::onLockPixelsAreWritable() const { |
| 137 | return true; |
| 138 | } |
| 139 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | uint32_t SkPixelRef::getGenerationID() const { |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 141 | if (0 == fGenerationID) { |
| 142 | fGenerationID = SkNextPixelRefGenerationID(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | } |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 144 | return fGenerationID; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void SkPixelRef::notifyPixelsChanged() { |
reed@android.com | 3eab80c | 2009-03-24 18:47:35 +0000 | [diff] [blame] | 148 | #ifdef SK_DEBUG |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | if (fIsImmutable) { |
| 150 | SkDebugf("========== notifyPixelsChanged called on immutable pixelref"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | } |
reed@android.com | 3eab80c | 2009-03-24 18:47:35 +0000 | [diff] [blame] | 152 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | // this signals us to recompute this next time around |
| 154 | fGenerationID = 0; |
| 155 | } |
| 156 | |
| 157 | void SkPixelRef::setImmutable() { |
| 158 | fIsImmutable = true; |
| 159 | } |
| 160 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 161 | bool SkPixelRef::readPixels(SkBitmap* dst, const SkIRect* subset) { |
| 162 | return this->onReadPixels(dst, subset); |
| 163 | } |
| 164 | |
| 165 | bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
| 166 | return false; |
| 167 | } |
| 168 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | /////////////////////////////////////////////////////////////////////////////// |
| 170 | |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 171 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 172 | void SkPixelRef::globalRef(void* data) { |
| 173 | this->ref(); |
| 174 | } |
| 175 | |
| 176 | void SkPixelRef::globalUnref() { |
| 177 | this->unref(); |
| 178 | } |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 179 | #endif |