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