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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 // return a 0 | 68 // return a 0 |
69 int32_t genID; | 69 int32_t genID; |
70 do { | 70 do { |
71 genID = sk_atomic_inc(&gPixelRefGenerationID) + 1; | 71 genID = sk_atomic_inc(&gPixelRefGenerationID) + 1; |
72 } while (0 == genID); | 72 } while (0 == genID); |
73 return genID; | 73 return genID; |
74 } | 74 } |
75 | 75 |
76 /////////////////////////////////////////////////////////////////////////////// | 76 /////////////////////////////////////////////////////////////////////////////// |
77 | 77 |
78 static void mark_invalid(SkImageInfo* info) { | |
79 info->fWidth = -1; | |
80 } | |
81 | |
82 static bool is_invalid(const SkImageInfo& info) { | |
83 return info.fWidth < 0; | |
84 } | |
85 | |
86 /////////////////////////////////////////////////////////////////////////////// | |
87 | |
78 void SkPixelRef::setMutex(SkBaseMutex* mutex) { | 88 void SkPixelRef::setMutex(SkBaseMutex* mutex) { |
79 if (NULL == mutex) { | 89 if (NULL == mutex) { |
80 mutex = get_default_mutex(); | 90 mutex = get_default_mutex(); |
81 } | 91 } |
82 fMutex = mutex; | 92 fMutex = mutex; |
83 } | 93 } |
84 | 94 |
85 // just need a > 0 value, so pick a funny one to aid in debugging | 95 // just need a > 0 value, so pick a funny one to aid in debugging |
86 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 | 96 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 |
87 | 97 |
88 SkPixelRef::SkPixelRef(SkBaseMutex* mutex) { | 98 SkPixelRef::SkPixelRef(SkBaseMutex* mutex) { |
89 this->setMutex(mutex); | 99 this->setMutex(mutex); |
90 fPixels = NULL; | 100 mark_invalid(&fInfo); |
91 fColorTable = NULL; // we do not track ownership of this | 101 fRec.zero(); |
92 fLockCount = 0; | 102 fLockCount = 0; |
93 this->needsNewGenID(); | 103 this->needsNewGenID(); |
94 fIsImmutable = false; | 104 fIsImmutable = false; |
95 fPreLocked = false; | 105 fPreLocked = false; |
96 } | 106 } |
97 | 107 |
98 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) | 108 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) |
99 : INHERITED(buffer) { | 109 : INHERITED(buffer) { |
100 this->setMutex(mutex); | 110 this->setMutex(mutex); |
101 fPixels = NULL; | 111 mark_invalid(&fInfo); |
102 fColorTable = NULL; // we do not track ownership of this | 112 fRec.zero(); |
103 fLockCount = 0; | 113 fLockCount = 0; |
104 fIsImmutable = buffer.readBool(); | 114 fIsImmutable = buffer.readBool(); |
105 fGenerationID = buffer.readUInt(); | 115 fGenerationID = buffer.readUInt(); |
106 fUniqueGenerationID = false; // Conservatively assuming the original still exists. | 116 fUniqueGenerationID = false; // Conservatively assuming the original still exists. |
107 fPreLocked = false; | 117 fPreLocked = false; |
108 } | 118 } |
109 | 119 |
110 SkPixelRef::~SkPixelRef() { | 120 SkPixelRef::~SkPixelRef() { |
111 this->callGenIDChangeListeners(); | 121 this->callGenIDChangeListeners(); |
112 } | 122 } |
113 | 123 |
114 void SkPixelRef::needsNewGenID() { | 124 void SkPixelRef::needsNewGenID() { |
115 fGenerationID = 0; | 125 fGenerationID = 0; |
116 fUniqueGenerationID = false; | 126 fUniqueGenerationID = false; |
117 } | 127 } |
118 | 128 |
119 void SkPixelRef::cloneGenID(const SkPixelRef& that) { | 129 void SkPixelRef::cloneGenID(const SkPixelRef& that) { |
120 // This is subtle. We must call that.getGenerationID() to make sure its gen ID isn't 0. | 130 // This is subtle. We must call that.getGenerationID() to make sure its gen ID isn't 0. |
121 this->fGenerationID = that.getGenerationID(); | 131 this->fGenerationID = that.getGenerationID(); |
122 this->fUniqueGenerationID = false; | 132 this->fUniqueGenerationID = false; |
123 that.fUniqueGenerationID = false; | 133 that.fUniqueGenerationID = false; |
124 } | 134 } |
125 | 135 |
126 void SkPixelRef::setPreLocked(void* pixels, SkColorTable* ctable) { | 136 void SkPixelRef::setPreLocked(const SkImageInfo& info, void* pixels, |
137 size_t rowBytes, SkColorTable* ctable) { | |
127 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED | 138 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED |
128 // only call me in your constructor, otherwise fLockCount tracking can get | 139 // only call me in your constructor, otherwise fLockCount tracking can get |
129 // out of sync. | 140 // out of sync. |
130 fPixels = pixels; | 141 fInfo = info; |
131 fColorTable = ctable; | 142 fRec.fPixels = pixels; |
143 fRec.fColorTable = ctable; | |
144 fRec.fRowBytes = rowBytes; | |
132 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; | 145 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; |
133 fPreLocked = true; | 146 fPreLocked = true; |
134 #endif | 147 #endif |
135 } | 148 } |
136 | 149 |
137 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { | 150 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { |
138 this->INHERITED::flatten(buffer); | 151 this->INHERITED::flatten(buffer); |
139 buffer.writeBool(fIsImmutable); | 152 buffer.writeBool(fIsImmutable); |
140 // We write the gen ID into the picture for within-process recording. This | 153 // 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 | 154 // 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" | 155 // pixels (barring overflow). However, each process has its own "namespace" |
143 // of genIDs. So for cross-process recording we write a zero which will | 156 // of genIDs. So for cross-process recording we write a zero which will |
144 // trigger assignment of a new genID in playback. | 157 // trigger assignment of a new genID in playback. |
145 if (buffer.isCrossProcess()) { | 158 if (buffer.isCrossProcess()) { |
146 buffer.writeUInt(0); | 159 buffer.writeUInt(0); |
147 } else { | 160 } else { |
148 buffer.writeUInt(fGenerationID); | 161 buffer.writeUInt(fGenerationID); |
149 fUniqueGenerationID = false; // Conservative, a copy is probably about to exist. | 162 fUniqueGenerationID = false; // Conservative, a copy is probably about to exist. |
150 } | 163 } |
151 } | 164 } |
152 | 165 |
153 void SkPixelRef::lockPixels() { | 166 bool SkPixelRef::getInfo(SkImageInfo* info) { |
167 #ifdef SK_DEBUG | |
168 if (fPreLocked) { | |
169 SkASSERT(!is_invalid(fInfo)); | |
scroggo
2013/12/04 22:47:38
This seems redundant.
reed1
2013/12/05 13:32:39
I don't think we assert elsewhere that prelocked m
scroggo
2013/12/05 13:45:34
Not directly, but just below this we assert that i
| |
170 } | |
171 #endif | |
172 | |
173 if (is_invalid(fInfo)) { | |
174 SkASSERT(!fPreLocked); | |
175 | |
176 SkAutoMutexAcquire ac(*fMutex); | |
177 | |
178 SkImageInfo tmpInfo; | |
scroggo
2013/12/04 22:47:38
This one is just in case the implementation of onG
reed1
2013/12/05 13:32:39
Exactly.
Also, we are experimenting with making t
| |
179 if (!this->onGetInfo(&tmpInfo)) { | |
180 return false; | |
181 } | |
182 fInfo = tmpInfo; | |
183 } | |
184 *info = fInfo; | |
185 return true; | |
186 } | |
187 | |
188 bool SkPixelRef::lockPixels(LockRec* rec) { | |
154 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 189 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
155 | 190 |
156 if (!fPreLocked) { | 191 if (!fPreLocked) { |
157 SkAutoMutexAcquire ac(*fMutex); | 192 SkAutoMutexAcquire ac(*fMutex); |
158 | 193 |
159 if (1 == ++fLockCount) { | 194 if (1 == ++fLockCount) { |
160 fPixels = this->onLockPixels(&fColorTable); | 195 LockRec rec; |
196 if (!this->onNewLockPixels(&rec)) { | |
197 return false; | |
198 } | |
199 fRec = rec; | |
161 } | 200 } |
162 } | 201 } |
202 *rec = fRec; | |
203 return true; | |
204 } | |
205 | |
206 void SkPixelRef::lockPixels() { | |
207 LockRec rec; | |
208 (void)this->lockPixels(&rec); | |
163 } | 209 } |
164 | 210 |
165 void SkPixelRef::unlockPixels() { | 211 void SkPixelRef::unlockPixels() { |
166 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 212 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
167 | 213 |
168 if (!fPreLocked) { | 214 if (!fPreLocked) { |
169 SkAutoMutexAcquire ac(*fMutex); | 215 SkAutoMutexAcquire ac(*fMutex); |
170 | 216 |
171 SkASSERT(fLockCount > 0); | 217 SkASSERT(fLockCount > 0); |
172 if (0 == --fLockCount) { | 218 if (0 == --fLockCount) { |
173 this->onUnlockPixels(); | 219 this->onUnlockPixels(); |
174 fPixels = NULL; | 220 fRec.zero(); |
175 fColorTable = NULL; | |
176 } | 221 } |
177 } | 222 } |
178 } | 223 } |
179 | 224 |
180 bool SkPixelRef::lockPixelsAreWritable() const { | 225 bool SkPixelRef::lockPixelsAreWritable() const { |
181 return this->onLockPixelsAreWritable(); | 226 return this->onLockPixelsAreWritable(); |
182 } | 227 } |
183 | 228 |
184 bool SkPixelRef::onLockPixelsAreWritable() const { | 229 bool SkPixelRef::onLockPixelsAreWritable() const { |
185 return true; | 230 return true; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { | 287 bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
243 return false; | 288 return false; |
244 } | 289 } |
245 | 290 |
246 SkData* SkPixelRef::onRefEncodedData() { | 291 SkData* SkPixelRef::onRefEncodedData() { |
247 return NULL; | 292 return NULL; |
248 } | 293 } |
249 | 294 |
250 /////////////////////////////////////////////////////////////////////////////// | 295 /////////////////////////////////////////////////////////////////////////////// |
251 | 296 |
297 #ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS | |
298 | |
299 void* SkPixelRef::onLockPixels(SkColorTable** ctable) { | |
300 return NULL; | |
301 } | |
302 | |
303 bool SkPixelRef::onGetInfo(SkImageInfo* info) { | |
304 return false; | |
305 } | |
306 | |
307 bool SkPixelRef::onNewLockPixels(LockRec* rec) { | |
308 SkColorTable* ctable; | |
309 void* pixels = this->onLockPixels(&ctable); | |
310 if (!pixels) { | |
311 return false; | |
312 } | |
313 | |
314 rec->fPixels = pixels; | |
315 rec->fColorTable = ctable; | |
316 rec->fRowBytes = 0; // callers don't currently need this (thank goodness) | |
317 return true; | |
318 } | |
319 | |
320 #endif | |
321 | |
322 /////////////////////////////////////////////////////////////////////////////// | |
323 | |
252 #ifdef SK_BUILD_FOR_ANDROID | 324 #ifdef SK_BUILD_FOR_ANDROID |
253 void SkPixelRef::globalRef(void* data) { | 325 void SkPixelRef::globalRef(void* data) { |
254 this->ref(); | 326 this->ref(); |
255 } | 327 } |
256 | 328 |
257 void SkPixelRef::globalUnref() { | 329 void SkPixelRef::globalUnref() { |
258 this->unref(); | 330 this->unref(); |
259 } | 331 } |
260 #endif | 332 #endif |
OLD | NEW |