2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2008 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkWriter32_DEFINED
|
|
|
|
#define SkWriter32_DEFINED
|
|
|
|
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
|
|
|
#include "SkScalar.h"
|
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkRect.h"
|
|
|
|
|
|
|
|
class SkStream;
|
|
|
|
class SkWStream;
|
|
|
|
|
|
|
|
class SkWriter32 : SkNoncopyable {
|
|
|
|
public:
|
Fix a batch of coverity defects, uninitialized class fields.
In SkClipStack::B2FIter::Clip, SkWriter32, SkClipStack::Rec, SkDeque::F2BIter, SkPDFShader::State
CID 15427,15433,15533,15532,16274,16740
Review URL: http://codereview.appspot.com/4630055
git-svn-id: http://skia.googlecode.com/svn/trunk@1669 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-06-21 22:26:39 +00:00
|
|
|
SkWriter32(size_t minSize)
|
|
|
|
: fMinSize(minSize),
|
|
|
|
fSize(0),
|
|
|
|
fSingleBlock(NULL),
|
|
|
|
fSingleBlockSize(0),
|
|
|
|
fHead(NULL),
|
|
|
|
fTail(NULL) {
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
~SkWriter32();
|
|
|
|
|
2011-05-03 21:26:46 +00:00
|
|
|
/**
|
|
|
|
* Returns the single block backing the writer, or NULL if the memory is
|
|
|
|
* to be dynamically allocated.
|
|
|
|
*/
|
|
|
|
void* getSingleBlock() const { return fSingleBlock; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify the single block to back the writer, rathern than dynamically
|
|
|
|
* allocating the memory. If block == NULL, then the writer reverts to
|
|
|
|
* dynamic allocation (and resets).
|
|
|
|
*/
|
|
|
|
void reset(void* block, size_t size);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
bool writeBool(bool value) {
|
|
|
|
this->writeInt(value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeInt(int32_t value) {
|
|
|
|
*(int32_t*)this->reserve(sizeof(value)) = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write8(int32_t value) {
|
|
|
|
*(int32_t*)this->reserve(sizeof(value)) = value & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write16(int32_t value) {
|
|
|
|
*(int32_t*)this->reserve(sizeof(value)) = value & 0xFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write32(int32_t value) {
|
|
|
|
*(int32_t*)this->reserve(sizeof(value)) = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeScalar(SkScalar value) {
|
|
|
|
*(SkScalar*)this->reserve(sizeof(value)) = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writePoint(const SkPoint& pt) {
|
|
|
|
*(SkPoint*)this->reserve(sizeof(pt)) = pt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeRect(const SkRect& rect) {
|
|
|
|
*(SkRect*)this->reserve(sizeof(rect)) = rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write count bytes (must be a multiple of 4)
|
|
|
|
void writeMul4(const void* values, size_t size) {
|
2011-04-26 17:49:03 +00:00
|
|
|
this->write(values, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write size bytes from values. size must be a multiple of 4, though
|
|
|
|
* values need not be 4-byte aligned.
|
|
|
|
*/
|
|
|
|
void write(const void* values, size_t size) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(SkAlign4(size) == size);
|
|
|
|
// if we could query how much is avail in the current block, we might
|
|
|
|
// copy that much, and then alloc the rest. That would reduce the waste
|
|
|
|
// in the current block
|
|
|
|
memcpy(this->reserve(size), values, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writePad(const void* src, size_t size);
|
2011-05-23 12:21:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a string to the writer, which can be retrieved with
|
|
|
|
* SkReader32::readString().
|
|
|
|
* The length can be specified, or if -1 is passed, it will be computed by
|
|
|
|
* calling strlen(). The length must be < 0xFFFF
|
|
|
|
*/
|
|
|
|
void writeString(const char* str, size_t len = (size_t)-1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the size (aligned to multiple of 4) need to write the string
|
|
|
|
* in a call to writeString(). If the length is not specified, it will be
|
|
|
|
* computed by calling strlen().
|
|
|
|
*/
|
|
|
|
static size_t WriteStringSize(const char* str, size_t len = (size_t)-1);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// return the current offset (will always be a multiple of 4)
|
|
|
|
uint32_t size() const { return fSize; }
|
|
|
|
void reset();
|
|
|
|
uint32_t* reserve(size_t size); // size MUST be multiple of 4
|
2011-04-26 17:49:03 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// return the address of the 4byte int at the specified offset (which must
|
|
|
|
// be a multiple of 4. This does not allocate any new space, so the returned
|
|
|
|
// address is only valid for 1 int.
|
|
|
|
uint32_t* peek32(size_t offset);
|
|
|
|
|
|
|
|
// copy into a single buffer (allocated by caller). Must be at least size()
|
|
|
|
void flatten(void* dst) const;
|
|
|
|
|
|
|
|
// read from the stream, and write up to length bytes. Return the actual
|
|
|
|
// number of bytes written.
|
|
|
|
size_t readFromStream(SkStream*, size_t length);
|
|
|
|
|
|
|
|
bool writeToStream(SkWStream*);
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t fMinSize;
|
|
|
|
uint32_t fSize;
|
2011-05-03 21:26:46 +00:00
|
|
|
|
|
|
|
char* fSingleBlock;
|
|
|
|
uint32_t fSingleBlockSize;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
struct Block;
|
|
|
|
Block* fHead;
|
|
|
|
Block* fTail;
|
2011-05-03 21:26:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
Block* newBlock(size_t bytes);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|