Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 The Android Open Source Project |
| 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 | 8 |
| 9 | 9 |
| 10 #ifndef SkWriter32_DEFINED | 10 #ifndef SkWriter32_DEFINED |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 if (fInternal.isEmpty() && this->externalCount() + count <= fExternalLim it) { | 72 if (fInternal.isEmpty() && this->externalCount() + count <= fExternalLim it) { |
| 73 p = fExternal + fCount; | 73 p = fExternal + fCount; |
| 74 } else { | 74 } else { |
| 75 p = fInternal.append(count); | 75 p = fInternal.append(count); |
| 76 } | 76 } |
| 77 | 77 |
| 78 fCount += count; | 78 fCount += count; |
| 79 return p; | 79 return p; |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Read or write 4 bytes at offset, which must be a multiple of 4 <= size(). | 82 /** |
| 83 uint32_t read32At(size_t offset) { return this->atOffset(offset); } | 83 * Read a T record at offset, which must be a multiple of 4. Only legal if the record |
| 84 void write32At(size_t offset, uint32_t val) { this->atOffset(offset) = val; } | 84 * was writtern atomically using the write methods below. |
| 85 */ | |
| 86 template<typename T> | |
| 87 const T& readTAt(size_t offset) const { | |
| 88 return this->atOffset<T>(offset); | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Overwrite a T record at offset, which must be a multiple of 4. Only lega l if the record | |
| 93 * was writtern atomically using the write methods below. | |
| 94 */ | |
| 95 template<typename T> | |
| 96 void overwriteTAt(size_t offset, const T& value) { | |
| 97 this->atOffset<T>(offset) = value; | |
| 98 } | |
| 85 | 99 |
| 86 bool writeBool(bool value) { | 100 bool writeBool(bool value) { |
| 87 this->write32(value); | 101 this->write32(value); |
| 88 return value; | 102 return value; |
| 89 } | 103 } |
| 90 | 104 |
| 91 void writeInt(int32_t value) { | 105 void writeInt(int32_t value) { |
| 92 this->write32(value); | 106 this->write32(value); |
| 93 } | 107 } |
| 94 | 108 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 // TODO: If we're going to spill from fExternal to fInternal, we might w ant to fill | 174 // TODO: If we're going to spill from fExternal to fInternal, we might w ant to fill |
| 161 // fExternal as much as possible before writing to fInternal. | 175 // fExternal as much as possible before writing to fInternal. |
| 162 memcpy(this->reserve(size), values, size); | 176 memcpy(this->reserve(size), values, size); |
| 163 } | 177 } |
| 164 | 178 |
| 165 /** | 179 /** |
| 166 * Reserve size bytes. Does not need to be 4 byte aligned. The remaining sp ace (if any) will be | 180 * Reserve size bytes. Does not need to be 4 byte aligned. The remaining sp ace (if any) will be |
| 167 * filled in with zeroes. | 181 * filled in with zeroes. |
| 168 */ | 182 */ |
| 169 uint32_t* reservePad(size_t size) { | 183 uint32_t* reservePad(size_t size) { |
| 170 uint32_t* p = this->reserve(SkAlign4(size)); | 184 size_t alignedSize = SkAlign4(size); |
| 171 uint8_t* tail = (uint8_t*)p + size; | 185 uint32_t* p = this->reserve(alignedSize); |
| 172 switch (SkAlign4(size) - size) { | 186 if (alignedSize != size) { |
| 173 default: SkDEBUGFAIL("SkAlign4(x) - x should always be 0, 1, 2, or 3 ."); | 187 SkASSERT(alignedSize >= 4); |
| 174 case 3: *tail++ = 0x00; // fallthrough is intentional | 188 p[alignedSize / 4 - 1] = 0; |
|
mtklein
2014/02/11 13:18:10
Duh. I don't know why I wrote the old one to be s
f(malita)
2014/02/11 14:09:36
Yeah, I kept the conditional mainly for handling r
| |
| 175 case 2: *tail++ = 0x00; // fallthrough is intentional | |
| 176 case 1: *tail++ = 0x00; | |
| 177 case 0: ;/*nothing to do*/ | |
| 178 } | 189 } |
| 179 return p; | 190 return p; |
| 180 } | 191 } |
| 181 | 192 |
| 182 /** | 193 /** |
| 183 * Write size bytes from src, and pad to 4 byte alignment with zeroes. | 194 * Write size bytes from src, and pad to 4 byte alignment with zeroes. |
| 184 */ | 195 */ |
| 185 void writePad(const void* src, size_t size) { | 196 void writePad(const void* src, size_t size) { |
| 186 memcpy(this->reservePad(size), src, size); | 197 memcpy(this->reservePad(size), src, size); |
| 187 } | 198 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 && stream->write(fInternal.begin(), fInternal.bytes()); | 242 && stream->write(fInternal.begin(), fInternal.bytes()); |
| 232 } | 243 } |
| 233 | 244 |
| 234 // read from the stream, and write up to length bytes. Return the actual | 245 // read from the stream, and write up to length bytes. Return the actual |
| 235 // number of bytes written. | 246 // number of bytes written. |
| 236 size_t readFromStream(SkStream* stream, size_t length) { | 247 size_t readFromStream(SkStream* stream, size_t length) { |
| 237 return stream->read(this->reservePad(length), length); | 248 return stream->read(this->reservePad(length), length); |
| 238 } | 249 } |
| 239 | 250 |
| 240 private: | 251 private: |
| 241 uint32_t& atOffset(size_t offset) { | 252 template<typename T> |
| 253 T& atOffset(size_t offset) const { | |
| 242 SkASSERT(SkAlign4(offset) == offset); | 254 SkASSERT(SkAlign4(offset) == offset); |
| 243 const int count = SkToInt(offset/4); | 255 const int count = SkToInt(offset / 4); |
| 244 SkASSERT(count < fCount); | 256 SkASSERT(offset + sizeof(T) <= (unsigned)fCount * 4); |
| 245 | 257 |
| 246 if (count < this->externalCount()) { | 258 if (count < this->externalCount()) { |
| 247 return fExternal[count]; | 259 // A record should not span fExternal->fInternal. |
| 260 SkASSERT(offset + sizeof(T) <= (unsigned)this->externalCount() * 4); | |
| 261 return *(T*)&fExternal[count]; | |
| 248 } | 262 } |
| 249 return fInternal[count - this->externalCount()]; | 263 return *(T*)&fInternal[count - this->externalCount()]; |
| 250 } | 264 } |
| 251 | 265 |
| 252 | |
| 253 // Number of uint32_t written into fExternal. <= fExternalLimit. | 266 // Number of uint32_t written into fExternal. <= fExternalLimit. |
| 254 int externalCount() const { return fCount - fInternal.count(); } | 267 int externalCount() const { return fCount - fInternal.count(); } |
| 255 | 268 |
| 256 int fCount; // Total number of uint32_t written. | 269 int fCount; // Total number of uint32_t written. |
| 257 int fExternalLimit; // Number of uint32_t we can write to fExter nal. | 270 int fExternalLimit; // Number of uint32_t we can write to fExter nal. |
| 258 uint32_t* fExternal; // Unmanaged memory block. | 271 uint32_t* fExternal; // Unmanaged memory block. |
| 259 SkTDArray<uint32_t> fInternal; // Managed memory block. | 272 SkTDArray<uint32_t> fInternal; // Managed memory block. |
| 260 }; | 273 }; |
| 261 | 274 |
| 262 /** | 275 /** |
| 263 * Helper class to allocated SIZE bytes as part of the writer, and to provide | 276 * Helper class to allocated SIZE bytes as part of the writer, and to provide |
| 264 * that storage to the constructor as its initial storage buffer. | 277 * that storage to the constructor as its initial storage buffer. |
| 265 * | 278 * |
| 266 * This wrapper ensures proper alignment rules are met for the storage. | 279 * This wrapper ensures proper alignment rules are met for the storage. |
| 267 */ | 280 */ |
| 268 template <size_t SIZE> class SkSWriter32 : public SkWriter32 { | 281 template <size_t SIZE> class SkSWriter32 : public SkWriter32 { |
| 269 public: | 282 public: |
| 270 SkSWriter32() : SkWriter32(fData.fStorage, SIZE) {} | 283 SkSWriter32() : SkWriter32(fData.fStorage, SIZE) {} |
| 271 | 284 |
| 272 private: | 285 private: |
| 273 union { | 286 union { |
| 274 void* fPtrAlignment; | 287 void* fPtrAlignment; |
| 275 double fDoubleAlignment; | 288 double fDoubleAlignment; |
| 276 char fStorage[SIZE]; | 289 char fStorage[SIZE]; |
| 277 } fData; | 290 } fData; |
| 278 }; | 291 }; |
| 279 | 292 |
| 280 #endif | 293 #endif |
| OLD | NEW |