Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: include/core/SkWriter32.h

Issue 130913018: Templetized SkWriter32 readTAt() & overwriteTAt() (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Win build fix Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | src/core/SkPictureRecord.h » ('j') | src/core/SkPictureRecord.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // Read or (over)write a record at offset, which must be a multiple of 4.
83 uint32_t read32At(size_t offset) { return this->atOffset(offset); } 83 // These calls are valid only if the same record type has been previously wr itten
84 void write32At(size_t offset, uint32_t val) { this->atOffset(offset) = val; } 84 // atomically at the given offset.
mtklein 2014/02/06 21:38:16 Now that we're looking at it again, it might make
mtklein 2014/02/06 21:38:16 This atomically isn't necessarily congruent to any
f(malita) 2014/02/10 16:10:35 Sounds good.
f(malita) 2014/02/10 16:10:35 Good catch - I hadn't realized some of the writeXY
85 template<typename T>
86 inline const T& readTAt(size_t offset) const {
87 return this->atOffset<T>(offset);
88 }
89
90 template<typename T>
91 inline void writeTAt(size_t offset, T value) {
92 this->atOffset<T>(offset) = value;
93 }
94
95 template<typename T>
96 inline void writeT(const T& value) {
mtklein 2014/02/06 21:38:16 My only reservation about doing this is that it al
f(malita) 2014/02/10 16:10:35 Ah, nice - overloading is not shun upon in Skia. I
97 *(T*)this->reserve(sizeof(value)) = 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
95 void write8(int32_t value) { 109 void write8(int32_t value) {
96 *(int32_t*)this->reserve(sizeof(value)) = value & 0xFF; 110 *(int32_t*)this->reserve(sizeof(value)) = value & 0xFF;
97 } 111 }
98 112
99 void write16(int32_t value) { 113 void write16(int32_t value) {
100 *(int32_t*)this->reserve(sizeof(value)) = value & 0xFFFF; 114 *(int32_t*)this->reserve(sizeof(value)) = value & 0xFFFF;
101 } 115 }
102 116
103 void write32(int32_t value) { 117 void write32(int32_t value) {
104 *(int32_t*)this->reserve(sizeof(value)) = value; 118 *(int32_t*)this->reserve(sizeof(value)) = value;
105 } 119 }
106 120
107 void writePtr(void* value) {
108 *(void**)this->reserve(sizeof(value)) = value;
109 }
110
111 void writeScalar(SkScalar value) {
112 *(SkScalar*)this->reserve(sizeof(value)) = value;
113 }
114
115 void writePoint(const SkPoint& pt) {
116 *(SkPoint*)this->reserve(sizeof(pt)) = pt;
117 }
118
119 void writeRect(const SkRect& rect) {
120 *(SkRect*)this->reserve(sizeof(rect)) = rect;
121 }
122
123 void writeIRect(const SkIRect& rect) {
124 *(SkIRect*)this->reserve(sizeof(rect)) = rect;
125 }
126
127 void writeRRect(const SkRRect& rrect) { 121 void writeRRect(const SkRRect& rrect) {
128 rrect.writeToMemory(this->reserve(SkRRect::kSizeInMemory)); 122 rrect.writeToMemory(this->reserve(SkRRect::kSizeInMemory));
129 } 123 }
130 124
131 void writePath(const SkPath& path) { 125 void writePath(const SkPath& path) {
132 size_t size = path.writeToMemory(NULL); 126 size_t size = path.writeToMemory(NULL);
133 SkASSERT(SkAlign4(size) == size); 127 SkASSERT(SkAlign4(size) == size);
134 path.writeToMemory(this->reserve(size)); 128 path.writeToMemory(this->reserve(size));
135 } 129 }
136 130
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 && stream->write(fInternal.begin(), fInternal.bytes()); 225 && stream->write(fInternal.begin(), fInternal.bytes());
232 } 226 }
233 227
234 // read from the stream, and write up to length bytes. Return the actual 228 // read from the stream, and write up to length bytes. Return the actual
235 // number of bytes written. 229 // number of bytes written.
236 size_t readFromStream(SkStream* stream, size_t length) { 230 size_t readFromStream(SkStream* stream, size_t length) {
237 return stream->read(this->reservePad(length), length); 231 return stream->read(this->reservePad(length), length);
238 } 232 }
239 233
240 private: 234 private:
241 uint32_t& atOffset(size_t offset) { 235 template<typename T>
236 inline T& atOffset(size_t offset) const {
242 SkASSERT(SkAlign4(offset) == offset); 237 SkASSERT(SkAlign4(offset) == offset);
243 const int count = SkToInt(offset/4); 238 const int count = SkToInt(offset / 4);
244 SkASSERT(count < fCount); 239 SkASSERT(offset + sizeof(T) <= (unsigned)fCount << 2);
mtklein 2014/02/06 21:38:16 * 4 would be a bit more consistent with the rest o
f(malita) 2014/02/10 16:10:35 Will do.
245 240
246 if (count < this->externalCount()) { 241 if (count < this->externalCount()) {
247 return fExternal[count]; 242 // A record should not span fExternal->fInternal.
243 SkASSERT(offset + sizeof(T) <= (unsigned)this->externalCount() << 2) ;
244 return *(T*)&fExternal[count];
248 } 245 }
249 return fInternal[count - this->externalCount()]; 246 return *(T*)&fInternal[count - this->externalCount()];
250 } 247 }
251 248
252
253 // Number of uint32_t written into fExternal. <= fExternalLimit. 249 // Number of uint32_t written into fExternal. <= fExternalLimit.
254 int externalCount() const { return fCount - fInternal.count(); } 250 int externalCount() const { return fCount - fInternal.count(); }
255 251
256 int fCount; // Total number of uint32_t written. 252 int fCount; // Total number of uint32_t written.
257 int fExternalLimit; // Number of uint32_t we can write to fExter nal. 253 int fExternalLimit; // Number of uint32_t we can write to fExter nal.
258 uint32_t* fExternal; // Unmanaged memory block. 254 uint32_t* fExternal; // Unmanaged memory block.
259 SkTDArray<uint32_t> fInternal; // Managed memory block. 255 SkTDArray<uint32_t> fInternal; // Managed memory block.
260 }; 256 };
261 257
262 /** 258 /**
263 * Helper class to allocated SIZE bytes as part of the writer, and to provide 259 * 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. 260 * that storage to the constructor as its initial storage buffer.
265 * 261 *
266 * This wrapper ensures proper alignment rules are met for the storage. 262 * This wrapper ensures proper alignment rules are met for the storage.
267 */ 263 */
268 template <size_t SIZE> class SkSWriter32 : public SkWriter32 { 264 template <size_t SIZE> class SkSWriter32 : public SkWriter32 {
269 public: 265 public:
270 SkSWriter32() : SkWriter32(fData.fStorage, SIZE) {} 266 SkSWriter32() : SkWriter32(fData.fStorage, SIZE) {}
271 267
272 private: 268 private:
273 union { 269 union {
274 void* fPtrAlignment; 270 void* fPtrAlignment;
275 double fDoubleAlignment; 271 double fDoubleAlignment;
276 char fStorage[SIZE]; 272 char fStorage[SIZE];
277 } fData; 273 } fData;
278 }; 274 };
279 275
280 #endif 276 #endif
OLDNEW
« no previous file with comments | « no previous file | src/core/SkPictureRecord.h » ('j') | src/core/SkPictureRecord.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698