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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 void SkPixelRef::setMutex(SkBaseMutex* mutex) { | 75 void SkPixelRef::setMutex(SkBaseMutex* mutex) { |
76 if (NULL == mutex) { | 76 if (NULL == mutex) { |
77 mutex = get_default_mutex(); | 77 mutex = get_default_mutex(); |
78 } | 78 } |
79 fMutex = mutex; | 79 fMutex = mutex; |
80 } | 80 } |
81 | 81 |
82 // just need a > 0 value, so pick a funny one to aid in debugging | 82 // just need a > 0 value, so pick a funny one to aid in debugging |
83 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 | 83 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 |
84 | 84 |
85 SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) { | 85 SkPixelRef::SkPixelRef(const SkImageInfo& info) { |
86 this->setMutex(mutex); | 86 this->setMutex(NULL); |
87 fInfo = info; | 87 fInfo = info; |
88 fPixels = NULL; | 88 fRec.zero(); |
89 fColorTable = NULL; // we do not track ownership of this | |
90 fLockCount = 0; | 89 fLockCount = 0; |
91 this->needsNewGenID(); | 90 this->needsNewGenID(); |
92 fIsImmutable = false; | 91 fIsImmutable = false; |
93 fPreLocked = false; | 92 fPreLocked = false; |
94 } | 93 } |
95 | 94 |
96 SkPixelRef::SkPixelRef(const SkImageInfo& info) { | 95 SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) { |
97 this->setMutex(NULL); | 96 this->setMutex(mutex); |
98 fInfo = info; | 97 fInfo = info; |
99 fPixels = NULL; | 98 fRec.zero(); |
100 fColorTable = NULL; // we do not track ownership of this | |
101 fLockCount = 0; | 99 fLockCount = 0; |
102 this->needsNewGenID(); | 100 this->needsNewGenID(); |
103 fIsImmutable = false; | 101 fIsImmutable = false; |
104 fPreLocked = false; | 102 fPreLocked = false; |
105 } | 103 } |
106 | 104 |
107 #ifdef SK_SUPPORT_LEGACY_PIXELREF_CONSTRUCTOR | |
108 // THIS GUY IS DEPRECATED -- don't use me! | |
109 SkPixelRef::SkPixelRef(SkBaseMutex* mutex) { | |
110 this->setMutex(mutex); | |
111 // Fill with dummy values. | |
112 sk_bzero(&fInfo, sizeof(fInfo)); | |
113 fPixels = NULL; | |
114 fColorTable = NULL; // we do not track ownership of this | |
115 fLockCount = 0; | |
116 this->needsNewGenID(); | |
117 fIsImmutable = false; | |
118 fPreLocked = false; | |
119 } | |
120 #endif | |
121 | |
122 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) | 105 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) |
123 : INHERITED(buffer) { | 106 : INHERITED(buffer) { |
124 this->setMutex(mutex); | 107 this->setMutex(mutex); |
125 fPixels = NULL; | 108 |
126 fColorTable = NULL; // we do not track ownership of this | 109 fInfo.unflatten(buffer); |
| 110 fRec.zero(); |
127 fLockCount = 0; | 111 fLockCount = 0; |
128 fIsImmutable = buffer.readBool(); | 112 fIsImmutable = buffer.readBool(); |
129 fGenerationID = buffer.readUInt(); | 113 fGenerationID = buffer.readUInt(); |
130 fUniqueGenerationID = false; // Conservatively assuming the original still
exists. | 114 fUniqueGenerationID = false; // Conservatively assuming the original still
exists. |
131 fPreLocked = false; | 115 fPreLocked = false; |
132 } | 116 } |
133 | 117 |
134 SkPixelRef::~SkPixelRef() { | 118 SkPixelRef::~SkPixelRef() { |
135 this->callGenIDChangeListeners(); | 119 this->callGenIDChangeListeners(); |
136 } | 120 } |
137 | 121 |
138 void SkPixelRef::needsNewGenID() { | 122 void SkPixelRef::needsNewGenID() { |
139 fGenerationID = 0; | 123 fGenerationID = 0; |
140 fUniqueGenerationID = false; | 124 fUniqueGenerationID = false; |
141 } | 125 } |
142 | 126 |
143 void SkPixelRef::cloneGenID(const SkPixelRef& that) { | 127 void SkPixelRef::cloneGenID(const SkPixelRef& that) { |
144 // This is subtle. We must call that.getGenerationID() to make sure its gen
ID isn't 0. | 128 // This is subtle. We must call that.getGenerationID() to make sure its gen
ID isn't 0. |
145 this->fGenerationID = that.getGenerationID(); | 129 this->fGenerationID = that.getGenerationID(); |
146 this->fUniqueGenerationID = false; | 130 this->fUniqueGenerationID = false; |
147 that.fUniqueGenerationID = false; | 131 that.fUniqueGenerationID = false; |
148 } | 132 } |
149 | 133 |
150 void SkPixelRef::setPreLocked(void* pixels, SkColorTable* ctable) { | 134 void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctabl
e) { |
151 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED | 135 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED |
152 // only call me in your constructor, otherwise fLockCount tracking can get | 136 // only call me in your constructor, otherwise fLockCount tracking can get |
153 // out of sync. | 137 // out of sync. |
154 fPixels = pixels; | 138 fRec.fPixels = pixels; |
155 fColorTable = ctable; | 139 fRec.fColorTable = ctable; |
| 140 fRec.fRowBytes = rowBytes; |
156 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; | 141 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; |
157 fPreLocked = true; | 142 fPreLocked = true; |
158 #endif | 143 #endif |
159 } | 144 } |
160 | 145 |
161 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { | 146 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { |
162 this->INHERITED::flatten(buffer); | 147 this->INHERITED::flatten(buffer); |
| 148 |
| 149 fInfo.flatten(buffer); |
163 buffer.writeBool(fIsImmutable); | 150 buffer.writeBool(fIsImmutable); |
164 // We write the gen ID into the picture for within-process recording. This | 151 // We write the gen ID into the picture for within-process recording. This |
165 // is safe since the same genID will never refer to two different sets of | 152 // is safe since the same genID will never refer to two different sets of |
166 // pixels (barring overflow). However, each process has its own "namespace" | 153 // pixels (barring overflow). However, each process has its own "namespace" |
167 // of genIDs. So for cross-process recording we write a zero which will | 154 // of genIDs. So for cross-process recording we write a zero which will |
168 // trigger assignment of a new genID in playback. | 155 // trigger assignment of a new genID in playback. |
169 if (buffer.isCrossProcess()) { | 156 if (buffer.isCrossProcess()) { |
170 buffer.writeUInt(0); | 157 buffer.writeUInt(0); |
171 } else { | 158 } else { |
172 buffer.writeUInt(fGenerationID); | 159 buffer.writeUInt(fGenerationID); |
173 fUniqueGenerationID = false; // Conservative, a copy is probably about
to exist. | 160 fUniqueGenerationID = false; // Conservative, a copy is probably about
to exist. |
174 } | 161 } |
175 } | 162 } |
176 | 163 |
177 void SkPixelRef::lockPixels() { | 164 bool SkPixelRef::lockPixels(LockRec* rec) { |
178 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 165 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
179 | 166 |
180 if (!fPreLocked) { | 167 if (!fPreLocked) { |
181 SkAutoMutexAcquire ac(*fMutex); | 168 SkAutoMutexAcquire ac(*fMutex); |
| 169 |
| 170 if (1 == ++fLockCount) { |
| 171 SkASSERT(fRec.isZero()); |
182 | 172 |
183 if (1 == ++fLockCount) { | 173 LockRec rec; |
184 fPixels = this->onLockPixels(&fColorTable); | 174 if (!this->onNewLockPixels(&rec)) { |
185 // If onLockPixels failed, it will return NULL | 175 return false; |
186 if (NULL == fPixels) { | |
187 fColorTable = NULL; | |
188 } | 176 } |
| 177 SkASSERT(!rec.isZero()); // else why did onNewLock return true? |
| 178 fRec = rec; |
189 } | 179 } |
190 } | 180 } |
| 181 *rec = fRec; |
| 182 return true; |
| 183 } |
| 184 |
| 185 bool SkPixelRef::lockPixels() { |
| 186 LockRec rec; |
| 187 return this->lockPixels(&rec); |
191 } | 188 } |
192 | 189 |
193 void SkPixelRef::unlockPixels() { | 190 void SkPixelRef::unlockPixels() { |
194 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 191 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
195 | 192 |
196 if (!fPreLocked) { | 193 if (!fPreLocked) { |
197 SkAutoMutexAcquire ac(*fMutex); | 194 SkAutoMutexAcquire ac(*fMutex); |
198 | 195 |
199 SkASSERT(fLockCount > 0); | 196 SkASSERT(fLockCount > 0); |
200 if (0 == --fLockCount) { | 197 if (0 == --fLockCount) { |
201 // don't call onUnlockPixels unless onLockPixels succeeded | 198 // don't call onUnlockPixels unless onLockPixels succeeded |
202 if (fPixels) { | 199 if (fRec.fPixels) { |
203 this->onUnlockPixels(); | 200 this->onUnlockPixels(); |
204 fPixels = NULL; | 201 fRec.zero(); |
205 fColorTable = NULL; | |
206 } else { | 202 } else { |
207 SkASSERT(NULL == fColorTable); | 203 SkASSERT(fRec.isZero()); |
208 } | 204 } |
209 } | 205 } |
210 } | 206 } |
211 } | 207 } |
212 | 208 |
213 bool SkPixelRef::lockPixelsAreWritable() const { | 209 bool SkPixelRef::lockPixelsAreWritable() const { |
214 return this->onLockPixelsAreWritable(); | 210 return this->onLockPixelsAreWritable(); |
215 } | 211 } |
216 | 212 |
217 bool SkPixelRef::onLockPixelsAreWritable() const { | 213 bool SkPixelRef::onLockPixelsAreWritable() const { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 SkData* SkPixelRef::onRefEncodedData() { | 275 SkData* SkPixelRef::onRefEncodedData() { |
280 return NULL; | 276 return NULL; |
281 } | 277 } |
282 | 278 |
283 size_t SkPixelRef::getAllocatedSizeInBytes() const { | 279 size_t SkPixelRef::getAllocatedSizeInBytes() const { |
284 return 0; | 280 return 0; |
285 } | 281 } |
286 | 282 |
287 /////////////////////////////////////////////////////////////////////////////// | 283 /////////////////////////////////////////////////////////////////////////////// |
288 | 284 |
| 285 #ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS |
| 286 |
| 287 void* SkPixelRef::onLockPixels(SkColorTable** ctable) { |
| 288 return NULL; |
| 289 } |
| 290 |
| 291 bool SkPixelRef::onNewLockPixels(LockRec* rec) { |
| 292 SkColorTable* ctable; |
| 293 void* pixels = this->onLockPixels(&ctable); |
| 294 if (!pixels) { |
| 295 return false; |
| 296 } |
| 297 |
| 298 rec->fPixels = pixels; |
| 299 rec->fColorTable = ctable; |
| 300 rec->fRowBytes = 0; // callers don't currently need this (thank goodness) |
| 301 return true; |
| 302 } |
| 303 |
| 304 #endif |
| 305 |
| 306 /////////////////////////////////////////////////////////////////////////////// |
| 307 |
289 #ifdef SK_BUILD_FOR_ANDROID | 308 #ifdef SK_BUILD_FOR_ANDROID |
290 void SkPixelRef::globalRef(void* data) { | 309 void SkPixelRef::globalRef(void* data) { |
291 this->ref(); | 310 this->ref(); |
292 } | 311 } |
293 | 312 |
294 void SkPixelRef::globalUnref() { | 313 void SkPixelRef::globalUnref() { |
295 this->unref(); | 314 this->unref(); |
296 } | 315 } |
297 #endif | 316 #endif |
OLD | NEW |