• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SkWriter32_DEFINED
18 #define SkWriter32_DEFINED
19 
20 #include "SkTypes.h"
21 
22 #include "SkScalar.h"
23 #include "SkPoint.h"
24 #include "SkRect.h"
25 
26 class SkStream;
27 class SkWStream;
28 
29 class SkWriter32 : SkNoncopyable {
30 public:
SkWriter32(size_t minSize)31     SkWriter32(size_t minSize) {
32         fMinSize = minSize;
33         fSize = 0;
34         fHead = fTail = NULL;
35     }
36     ~SkWriter32();
37 
writeBool(bool value)38     bool writeBool(bool value) {
39         this->writeInt(value);
40         return value;
41     }
42 
writeInt(int32_t value)43     void writeInt(int32_t value) {
44         *(int32_t*)this->reserve(sizeof(value)) = value;
45     }
46 
write8(int32_t value)47     void write8(int32_t value) {
48         *(int32_t*)this->reserve(sizeof(value)) = value & 0xFF;
49     }
50 
write16(int32_t value)51     void write16(int32_t value) {
52         *(int32_t*)this->reserve(sizeof(value)) = value & 0xFFFF;
53     }
54 
write32(int32_t value)55     void write32(int32_t value) {
56         *(int32_t*)this->reserve(sizeof(value)) = value;
57     }
58 
writeScalar(SkScalar value)59     void writeScalar(SkScalar value) {
60         *(SkScalar*)this->reserve(sizeof(value)) = value;
61     }
62 
writePoint(const SkPoint & pt)63     void writePoint(const SkPoint& pt) {
64         *(SkPoint*)this->reserve(sizeof(pt)) = pt;
65     }
66 
writeRect(const SkRect & rect)67     void writeRect(const SkRect& rect) {
68         *(SkRect*)this->reserve(sizeof(rect)) = rect;
69     }
70 
71     // write count bytes (must be a multiple of 4)
writeMul4(const void * values,size_t size)72     void writeMul4(const void* values, size_t size) {
73         SkASSERT(SkAlign4(size) == size);
74         // if we could query how much is avail in the current block, we might
75         // copy that much, and then alloc the rest. That would reduce the waste
76         // in the current block
77         memcpy(this->reserve(size), values, size);
78     }
79 
80     void writePad(const void* src, size_t size);
81 
82     // return the current offset (will always be a multiple of 4)
size()83     uint32_t  size() const { return fSize; }
84     void      reset();
85     uint32_t* reserve(size_t size); // size MUST be multiple of 4
86 
87     // return the address of the 4byte int at the specified offset (which must
88     // be a multiple of 4. This does not allocate any new space, so the returned
89     // address is only valid for 1 int.
90     uint32_t* peek32(size_t offset);
91 
92     // copy into a single buffer (allocated by caller). Must be at least size()
93     void flatten(void* dst) const;
94 
95     // read from the stream, and write up to length bytes. Return the actual
96     // number of bytes written.
97     size_t readFromStream(SkStream*, size_t length);
98 
99     bool writeToStream(SkWStream*);
100 
101 private:
102     size_t      fMinSize;
103     uint32_t    fSize;
104 
105     struct Block;
106     Block*  fHead;
107     Block*  fTail;
108 
109     Block* newBlock(size_t bytes);
110 };
111 
112 #endif
113