OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SkPixelRef.h" | 8 #include "SkPixelRef.h" |
9 #include "SkFlattenableBuffers.h" | 9 #include "SkFlattenableBuffers.h" |
10 #include "SkThread.h" | 10 #include "SkThread.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 void SkPixelRef::setMutex(SkBaseMutex* mutex) { | 78 void SkPixelRef::setMutex(SkBaseMutex* mutex) { |
79 if (NULL == mutex) { | 79 if (NULL == mutex) { |
80 mutex = get_default_mutex(); | 80 mutex = get_default_mutex(); |
81 } | 81 } |
82 fMutex = mutex; | 82 fMutex = mutex; |
83 } | 83 } |
84 | 84 |
85 // just need a > 0 value, so pick a funny one to aid in debugging | 85 // just need a > 0 value, so pick a funny one to aid in debugging |
86 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 | 86 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 |
87 | 87 |
88 SkPixelRef::SkPixelRef(SkBaseMutex* mutex) { | 88 SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) { |
89 this->setMutex(mutex); | 89 this->setMutex(mutex); |
90 fPixels = NULL; | 90 fInfo = info; |
91 fColorTable = NULL; // we do not track ownership of this | 91 fRec.zero(); |
92 fLockCount = 0; | 92 fLockCount = 0; |
93 this->needsNewGenID(); | 93 this->needsNewGenID(); |
94 fIsImmutable = false; | 94 fIsImmutable = false; |
95 fPreLocked = false; | 95 fPreLocked = false; |
96 } | 96 } |
97 | 97 |
98 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) | 98 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) |
99 : INHERITED(buffer) { | 99 : INHERITED(buffer) { |
100 this->setMutex(mutex); | 100 this->setMutex(mutex); |
101 fPixels = NULL; | 101 |
102 fColorTable = NULL; // we do not track ownership of this | 102 fInfo.unflatten(buffer); |
| 103 fRec.zero(); |
103 fLockCount = 0; | 104 fLockCount = 0; |
104 fIsImmutable = buffer.readBool(); | 105 fIsImmutable = buffer.readBool(); |
105 fGenerationID = buffer.readUInt(); | 106 fGenerationID = buffer.readUInt(); |
106 fUniqueGenerationID = false; // Conservatively assuming the original still
exists. | 107 fUniqueGenerationID = false; // Conservatively assuming the original still
exists. |
107 fPreLocked = false; | 108 fPreLocked = false; |
108 } | 109 } |
109 | 110 |
110 SkPixelRef::~SkPixelRef() { | 111 SkPixelRef::~SkPixelRef() { |
111 this->callGenIDChangeListeners(); | 112 this->callGenIDChangeListeners(); |
112 } | 113 } |
113 | 114 |
114 void SkPixelRef::needsNewGenID() { | 115 void SkPixelRef::needsNewGenID() { |
115 fGenerationID = 0; | 116 fGenerationID = 0; |
116 fUniqueGenerationID = false; | 117 fUniqueGenerationID = false; |
117 } | 118 } |
118 | 119 |
119 void SkPixelRef::cloneGenID(const SkPixelRef& that) { | 120 void SkPixelRef::cloneGenID(const SkPixelRef& that) { |
120 // This is subtle. We must call that.getGenerationID() to make sure its gen
ID isn't 0. | 121 // This is subtle. We must call that.getGenerationID() to make sure its gen
ID isn't 0. |
121 this->fGenerationID = that.getGenerationID(); | 122 this->fGenerationID = that.getGenerationID(); |
122 this->fUniqueGenerationID = false; | 123 this->fUniqueGenerationID = false; |
123 that.fUniqueGenerationID = false; | 124 that.fUniqueGenerationID = false; |
124 } | 125 } |
125 | 126 |
126 void SkPixelRef::setPreLocked(void* pixels, SkColorTable* ctable) { | 127 void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctabl
e) { |
127 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED | 128 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED |
128 // only call me in your constructor, otherwise fLockCount tracking can get | 129 // only call me in your constructor, otherwise fLockCount tracking can get |
129 // out of sync. | 130 // out of sync. |
130 fPixels = pixels; | 131 fRec.fPixels = pixels; |
131 fColorTable = ctable; | 132 fRec.fColorTable = ctable; |
| 133 fRec.fRowBytes = rowBytes; |
132 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; | 134 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; |
133 fPreLocked = true; | 135 fPreLocked = true; |
134 #endif | 136 #endif |
135 } | 137 } |
136 | 138 |
137 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { | 139 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { |
138 this->INHERITED::flatten(buffer); | 140 this->INHERITED::flatten(buffer); |
| 141 |
| 142 fInfo.flatten(buffer); |
139 buffer.writeBool(fIsImmutable); | 143 buffer.writeBool(fIsImmutable); |
140 // We write the gen ID into the picture for within-process recording. This | 144 // We write the gen ID into the picture for within-process recording. This |
141 // is safe since the same genID will never refer to two different sets of | 145 // is safe since the same genID will never refer to two different sets of |
142 // pixels (barring overflow). However, each process has its own "namespace" | 146 // pixels (barring overflow). However, each process has its own "namespace" |
143 // of genIDs. So for cross-process recording we write a zero which will | 147 // of genIDs. So for cross-process recording we write a zero which will |
144 // trigger assignment of a new genID in playback. | 148 // trigger assignment of a new genID in playback. |
145 if (buffer.isCrossProcess()) { | 149 if (buffer.isCrossProcess()) { |
146 buffer.writeUInt(0); | 150 buffer.writeUInt(0); |
147 } else { | 151 } else { |
148 buffer.writeUInt(fGenerationID); | 152 buffer.writeUInt(fGenerationID); |
149 fUniqueGenerationID = false; // Conservative, a copy is probably about
to exist. | 153 fUniqueGenerationID = false; // Conservative, a copy is probably about
to exist. |
150 } | 154 } |
151 } | 155 } |
152 | 156 |
153 void SkPixelRef::lockPixels() { | 157 bool SkPixelRef::lockPixels(LockRec* rec) { |
154 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 158 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
155 | 159 |
156 if (!fPreLocked) { | 160 if (!fPreLocked) { |
157 SkAutoMutexAcquire ac(*fMutex); | 161 SkAutoMutexAcquire ac(*fMutex); |
158 | 162 |
159 if (1 == ++fLockCount) { | 163 if (1 == ++fLockCount) { |
160 fPixels = this->onLockPixels(&fColorTable); | 164 LockRec rec; |
| 165 if (!this->onNewLockPixels(&rec)) { |
| 166 return false; |
| 167 } |
| 168 fRec = rec; |
161 } | 169 } |
162 } | 170 } |
| 171 *rec = fRec; |
| 172 return true; |
| 173 } |
| 174 |
| 175 bool SkPixelRef::lockPixels() { |
| 176 LockRec rec; |
| 177 return this->lockPixels(&rec); |
163 } | 178 } |
164 | 179 |
165 void SkPixelRef::unlockPixels() { | 180 void SkPixelRef::unlockPixels() { |
166 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 181 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
167 | 182 |
168 if (!fPreLocked) { | 183 if (!fPreLocked) { |
169 SkAutoMutexAcquire ac(*fMutex); | 184 SkAutoMutexAcquire ac(*fMutex); |
170 | 185 |
171 SkASSERT(fLockCount > 0); | 186 SkASSERT(fLockCount > 0); |
172 if (0 == --fLockCount) { | 187 if (0 == --fLockCount) { |
173 this->onUnlockPixels(); | 188 this->onUnlockPixels(); |
174 fPixels = NULL; | 189 fRec.zero(); |
175 fColorTable = NULL; | |
176 } | 190 } |
177 } | 191 } |
178 } | 192 } |
179 | 193 |
180 bool SkPixelRef::lockPixelsAreWritable() const { | 194 bool SkPixelRef::lockPixelsAreWritable() const { |
181 return this->onLockPixelsAreWritable(); | 195 return this->onLockPixelsAreWritable(); |
182 } | 196 } |
183 | 197 |
184 bool SkPixelRef::onLockPixelsAreWritable() const { | 198 bool SkPixelRef::onLockPixelsAreWritable() const { |
185 return true; | 199 return true; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { | 256 bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
243 return false; | 257 return false; |
244 } | 258 } |
245 | 259 |
246 SkData* SkPixelRef::onRefEncodedData() { | 260 SkData* SkPixelRef::onRefEncodedData() { |
247 return NULL; | 261 return NULL; |
248 } | 262 } |
249 | 263 |
250 /////////////////////////////////////////////////////////////////////////////// | 264 /////////////////////////////////////////////////////////////////////////////// |
251 | 265 |
| 266 #ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS |
| 267 |
| 268 void* SkPixelRef::onLockPixels(SkColorTable** ctable) { |
| 269 return NULL; |
| 270 } |
| 271 |
| 272 bool SkPixelRef::onNewLockPixels(LockRec* rec) { |
| 273 SkColorTable* ctable; |
| 274 void* pixels = this->onLockPixels(&ctable); |
| 275 if (!pixels) { |
| 276 return false; |
| 277 } |
| 278 |
| 279 rec->fPixels = pixels; |
| 280 rec->fColorTable = ctable; |
| 281 rec->fRowBytes = 0; // callers don't currently need this (thank goodness) |
| 282 return true; |
| 283 } |
| 284 |
| 285 #endif |
| 286 |
| 287 /////////////////////////////////////////////////////////////////////////////// |
| 288 |
252 #ifdef SK_BUILD_FOR_ANDROID | 289 #ifdef SK_BUILD_FOR_ANDROID |
253 void SkPixelRef::globalRef(void* data) { | 290 void SkPixelRef::globalRef(void* data) { |
254 this->ref(); | 291 this->ref(); |
255 } | 292 } |
256 | 293 |
257 void SkPixelRef::globalUnref() { | 294 void SkPixelRef::globalUnref() { |
258 this->unref(); | 295 this->unref(); |
259 } | 296 } |
260 #endif | 297 #endif |
OLD | NEW |