OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkMallocPixelRef.h" | 8 #include "SkMallocPixelRef.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkFlattenableBuffers.h" | 10 #include "SkReadBuffer.h" |
| 11 #include "SkWriteBuffer.h" |
11 | 12 |
12 // assumes ptr was allocated via sk_malloc | 13 // assumes ptr was allocated via sk_malloc |
13 static void sk_free_releaseproc(void* ptr, void*) { | 14 static void sk_free_releaseproc(void* ptr, void*) { |
14 sk_free(ptr); | 15 sk_free(ptr); |
15 } | 16 } |
16 | 17 |
17 static bool is_valid(const SkImageInfo& info, SkColorTable* ctable) { | 18 static bool is_valid(const SkImageInfo& info, SkColorTable* ctable) { |
18 if (info.fWidth < 0 || | 19 if (info.fWidth < 0 || |
19 info.fHeight < 0 || | 20 info.fHeight < 0 || |
20 (unsigned)info.fColorType > (unsigned)kLastEnum_SkColorType || | 21 (unsigned)info.fColorType > (unsigned)kLastEnum_SkColorType || |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 } | 195 } |
195 | 196 |
196 void SkMallocPixelRef::onUnlockPixels() { | 197 void SkMallocPixelRef::onUnlockPixels() { |
197 // nothing to do | 198 // nothing to do |
198 } | 199 } |
199 | 200 |
200 size_t SkMallocPixelRef::getAllocatedSizeInBytes() const { | 201 size_t SkMallocPixelRef::getAllocatedSizeInBytes() const { |
201 return this->info().getSafeSize(fRB); | 202 return this->info().getSafeSize(fRB); |
202 } | 203 } |
203 | 204 |
204 void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { | 205 void SkMallocPixelRef::flatten(SkWriteBuffer& buffer) const { |
205 this->INHERITED::flatten(buffer); | 206 this->INHERITED::flatten(buffer); |
206 | 207 |
207 buffer.write32(SkToU32(fRB)); | 208 buffer.write32(SkToU32(fRB)); |
208 | 209 |
209 // TODO: replace this bulk write with a chunky one that can trim off any | 210 // TODO: replace this bulk write with a chunky one that can trim off any |
210 // trailing bytes on each scanline (in case rowbytes > width*size) | 211 // trailing bytes on each scanline (in case rowbytes > width*size) |
211 size_t size = this->info().getSafeSize(fRB); | 212 size_t size = this->info().getSafeSize(fRB); |
212 buffer.writeByteArray(fStorage, size); | 213 buffer.writeByteArray(fStorage, size); |
213 buffer.writeBool(fCTable != NULL); | 214 buffer.writeBool(fCTable != NULL); |
214 if (fCTable) { | 215 if (fCTable) { |
215 fCTable->writeToBuffer(buffer); | 216 fCTable->writeToBuffer(buffer); |
216 } | 217 } |
217 } | 218 } |
218 | 219 |
219 SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer) | 220 SkMallocPixelRef::SkMallocPixelRef(SkReadBuffer& buffer) |
220 : INHERITED(buffer, NULL) | 221 : INHERITED(buffer, NULL) |
221 , fReleaseProc(sk_free_releaseproc) | 222 , fReleaseProc(sk_free_releaseproc) |
222 , fReleaseProcContext(NULL) | 223 , fReleaseProcContext(NULL) |
223 { | 224 { |
224 fRB = buffer.read32(); | 225 fRB = buffer.read32(); |
225 size_t size = buffer.isValid() ? this->info().getSafeSize(fRB) : 0; | 226 size_t size = buffer.isValid() ? this->info().getSafeSize(fRB) : 0; |
226 if (buffer.validateAvailable(size)) { | 227 if (buffer.validateAvailable(size)) { |
227 fStorage = sk_malloc_throw(size); | 228 fStorage = sk_malloc_throw(size); |
228 buffer.readByteArray(fStorage, size); | 229 buffer.readByteArray(fStorage, size); |
229 } else { | 230 } else { |
230 fStorage = NULL; | 231 fStorage = NULL; |
231 } | 232 } |
232 | 233 |
233 if (buffer.readBool()) { | 234 if (buffer.readBool()) { |
234 fCTable = SkNEW_ARGS(SkColorTable, (buffer)); | 235 fCTable = SkNEW_ARGS(SkColorTable, (buffer)); |
235 } else { | 236 } else { |
236 fCTable = NULL; | 237 fCTable = NULL; |
237 } | 238 } |
238 | 239 |
239 this->setPreLocked(fStorage, fRB, fCTable); | 240 this->setPreLocked(fStorage, fRB, fCTable); |
240 } | 241 } |
241 | 242 |
242 /////////////////////////////////////////////////////////////////////////////// | 243 /////////////////////////////////////////////////////////////////////////////// |
243 | 244 |
244 SkPixelRef* SkMallocPixelRef::PRFactory::create(const SkImageInfo& info, | 245 SkPixelRef* SkMallocPixelRef::PRFactory::create(const SkImageInfo& info, |
245 SkColorTable* ctable) { | 246 SkColorTable* ctable) { |
246 return SkMallocPixelRef::NewAllocate(info, info.minRowBytes(), ctable); | 247 return SkMallocPixelRef::NewAllocate(info, info.minRowBytes(), ctable); |
247 } | 248 } |
OLD | NEW |