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