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"
|
2012-06-08 18:30:46 +00:00
|
|
|
#include "SkPath.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkRect.h"
|
2012-12-12 20:48:18 +00:00
|
|
|
#include "SkRRect.h"
|
2012-04-12 13:24:04 +00:00
|
|
|
#include "SkMatrix.h"
|
|
|
|
#include "SkRegion.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
class SkStream;
|
|
|
|
class SkWStream;
|
|
|
|
|
|
|
|
class SkWriter32 : SkNoncopyable {
|
2012-12-21 15:36:33 +00:00
|
|
|
struct BlockHeader;
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-04-24 21:12:39 +00:00
|
|
|
/**
|
|
|
|
* The caller can specify an initial block of storage, which the caller manages.
|
|
|
|
* SkWriter32 will not attempt to free this in its destructor. It is up to the
|
|
|
|
* implementation to decide if, and how much, of the storage to utilize, and it
|
|
|
|
* is possible that it may be ignored entirely.
|
|
|
|
*/
|
|
|
|
SkWriter32(size_t minSize, void* initialStorage, size_t storageSize);
|
|
|
|
|
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)
|
2012-12-24 13:56:17 +00:00
|
|
|
: fHead(NULL)
|
|
|
|
, fTail(NULL)
|
|
|
|
, fMinSize(minSize)
|
|
|
|
, fSize(0)
|
|
|
|
, fWrittenBeforeLastBlock(0)
|
|
|
|
{}
|
2012-04-24 21:12:39 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
~SkWriter32();
|
|
|
|
|
2012-08-30 14:03:21 +00:00
|
|
|
// return the current offset (will always be a multiple of 4)
|
|
|
|
uint32_t bytesWritten() const { return fSize; }
|
|
|
|
// DEPRECATED: use byetsWritten instead
|
|
|
|
uint32_t size() const { return this->bytesWritten(); }
|
|
|
|
|
|
|
|
void reset();
|
2012-12-22 02:02:33 +00:00
|
|
|
|
2012-12-21 15:36:33 +00:00
|
|
|
// size MUST be multiple of 4
|
|
|
|
uint32_t* reserve(size_t size) {
|
|
|
|
SkASSERT(SkAlign4(size) == size);
|
2012-12-22 02:02:33 +00:00
|
|
|
|
2012-12-21 15:36:33 +00:00
|
|
|
Block* block = fTail;
|
|
|
|
if (NULL == block || block->available() < size) {
|
|
|
|
block = this->doReserve(size);
|
|
|
|
}
|
|
|
|
fSize += size;
|
|
|
|
return block->alloc(size);
|
|
|
|
}
|
2012-08-30 14:38:00 +00:00
|
|
|
|
2012-12-21 15:36:33 +00:00
|
|
|
void reset(void* storage, size_t size);
|
2012-04-24 21:12:39 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
bool writeBool(bool value) {
|
|
|
|
this->writeInt(value);
|
|
|
|
return value;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void writeInt(int32_t value) {
|
|
|
|
*(int32_t*)this->reserve(sizeof(value)) = value;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void write8(int32_t value) {
|
|
|
|
*(int32_t*)this->reserve(sizeof(value)) = value & 0xFF;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void write16(int32_t value) {
|
|
|
|
*(int32_t*)this->reserve(sizeof(value)) = value & 0xFFFF;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void write32(int32_t value) {
|
|
|
|
*(int32_t*)this->reserve(sizeof(value)) = value;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-06-12 20:47:53 +00:00
|
|
|
void writePtr(void* ptr) {
|
|
|
|
// Since we "know" that we're always 4-byte aligned, we can tell the
|
|
|
|
// compiler that here, by assigning to an int32 ptr.
|
|
|
|
int32_t* addr = (int32_t*)this->reserve(sizeof(void*));
|
|
|
|
if (4 == sizeof(void*)) {
|
|
|
|
*(void**)addr = ptr;
|
|
|
|
} else {
|
|
|
|
memcpy(addr, &ptr, sizeof(void*));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void writeScalar(SkScalar value) {
|
|
|
|
*(SkScalar*)this->reserve(sizeof(value)) = value;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void writePoint(const SkPoint& pt) {
|
|
|
|
*(SkPoint*)this->reserve(sizeof(pt)) = pt;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void writeRect(const SkRect& rect) {
|
|
|
|
*(SkRect*)this->reserve(sizeof(rect)) = rect;
|
|
|
|
}
|
2012-12-13 02:01:33 +00:00
|
|
|
|
2012-12-12 20:48:18 +00:00
|
|
|
void writeRRect(const SkRRect& rrect) {
|
|
|
|
rrect.writeToMemory(this->reserve(SkRRect::kSizeInMemory));
|
|
|
|
}
|
2012-12-13 02:01:33 +00:00
|
|
|
|
2012-06-08 18:30:46 +00:00
|
|
|
void writePath(const SkPath& path) {
|
|
|
|
size_t size = path.writeToMemory(NULL);
|
|
|
|
SkASSERT(SkAlign4(size) == size);
|
|
|
|
path.writeToMemory(this->reserve(size));
|
|
|
|
}
|
|
|
|
|
2012-04-12 13:24:04 +00:00
|
|
|
void writeMatrix(const SkMatrix& matrix) {
|
2012-06-08 18:30:46 +00:00
|
|
|
size_t size = matrix.writeToMemory(NULL);
|
2012-04-12 13:24:04 +00:00
|
|
|
SkASSERT(SkAlign4(size) == size);
|
2012-06-08 18:30:46 +00:00
|
|
|
matrix.writeToMemory(this->reserve(size));
|
2012-04-12 13:24:04 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-04-12 13:24:04 +00:00
|
|
|
void writeRegion(const SkRegion& rgn) {
|
2012-06-08 18:30:46 +00:00
|
|
|
size_t size = rgn.writeToMemory(NULL);
|
2012-04-12 13:24:04 +00:00
|
|
|
SkASSERT(SkAlign4(size) == size);
|
2012-06-08 18:30:46 +00:00
|
|
|
rgn.writeToMemory(this->reserve(size));
|
2012-04-12 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
Add the ability to provide function pointers to SkPicture serialization
and deserialization for encoding and decoding bitmaps.
Remove kForceFlattenBitmapPixels_Flag, which is no longer used.
When an SkOrderedReadBuffer needs to read a bitmap, if it does not
have an image decoder, use a dummy bitmap.
In GM, add a tolerance option for color differences, used when
testing picture serialization, so it can assume two images are the
same even though PNG encoding/decoding may have resulted in small
differences.
Create dummy implementations for SkImageDecoder and SkImageEncoder
functions in SkImageDecoder_empty so that a project that does not
want to include the images project it can still build.
Allow ports to build without images project.
In Mac's image encoder, copy 4444 to 8888 before encoding.
Add SkWriter32::reservePad, to provide a pointer to write non 4 byte
aligned data, padded with zeroes.
In bench_ and render_ pictures, pass decode function to SkPicture
creation from a stream.
BUG=https://code.google.com/p/skia/issues/detail?id=842
Review URL: https://codereview.appspot.com/6551071
git-svn-id: http://skia.googlecode.com/svn/trunk@5818 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-10-04 21:46:08 +00:00
|
|
|
/**
|
|
|
|
* Reserve size bytes. Does not need to be 4 byte aligned. The remaining space (if any) will be
|
|
|
|
* filled in with zeroes.
|
|
|
|
*/
|
|
|
|
uint32_t* reservePad(size_t size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write size bytes from src, and pad to 4 byte alignment with zeroes.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
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 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);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-08-30 14:03:21 +00:00
|
|
|
/**
|
|
|
|
* Move the cursor back to offset bytes from the beginning.
|
|
|
|
* This has the same restrictions as peek32: offset must be <= size() and
|
|
|
|
* offset must be a multiple of 4.
|
|
|
|
*/
|
|
|
|
void rewindToOffset(size_t offset);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// copy into a single buffer (allocated by caller). Must be at least size()
|
|
|
|
void flatten(void* dst) const;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// read from the stream, and write up to length bytes. Return the actual
|
|
|
|
// number of bytes written.
|
|
|
|
size_t readFromStream(SkStream*, size_t length);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
bool writeToStream(SkWStream*);
|
|
|
|
|
|
|
|
private:
|
2012-12-21 15:36:33 +00:00
|
|
|
struct Block {
|
|
|
|
Block* fNext;
|
|
|
|
char* fBasePtr;
|
|
|
|
size_t fSizeOfBlock; // total space allocated (after this)
|
|
|
|
size_t fAllocatedSoFar; // space used so far
|
|
|
|
|
|
|
|
size_t available() const { return fSizeOfBlock - fAllocatedSoFar; }
|
|
|
|
char* base() { return fBasePtr; }
|
|
|
|
const char* base() const { return fBasePtr; }
|
|
|
|
|
|
|
|
uint32_t* alloc(size_t size) {
|
|
|
|
SkASSERT(SkAlign4(size) == size);
|
|
|
|
SkASSERT(this->available() >= size);
|
|
|
|
void* ptr = this->base() + fAllocatedSoFar;
|
|
|
|
fAllocatedSoFar += size;
|
|
|
|
SkASSERT(fAllocatedSoFar <= fSizeOfBlock);
|
|
|
|
return (uint32_t*)ptr;
|
|
|
|
}
|
2011-05-03 21:26:46 +00:00
|
|
|
|
2012-12-21 15:36:33 +00:00
|
|
|
uint32_t* peek32(size_t offset) {
|
|
|
|
SkASSERT(offset <= fAllocatedSoFar + 4);
|
|
|
|
void* ptr = this->base() + offset;
|
|
|
|
return (uint32_t*)ptr;
|
|
|
|
}
|
2012-12-22 02:02:33 +00:00
|
|
|
|
2012-12-21 15:36:33 +00:00
|
|
|
void rewind() {
|
|
|
|
fNext = NULL;
|
|
|
|
fAllocatedSoFar = 0;
|
|
|
|
// keep fSizeOfBlock as is
|
|
|
|
}
|
2012-12-22 02:02:33 +00:00
|
|
|
|
2012-12-21 15:36:33 +00:00
|
|
|
static Block* Create(size_t size) {
|
|
|
|
SkASSERT(SkIsAlign4(size));
|
|
|
|
Block* block = (Block*)sk_malloc_throw(sizeof(Block) + size);
|
|
|
|
block->fNext = NULL;
|
|
|
|
block->fBasePtr = (char*)(block + 1);
|
|
|
|
block->fSizeOfBlock = size;
|
|
|
|
block->fAllocatedSoFar = 0;
|
|
|
|
return block;
|
|
|
|
}
|
2012-04-24 21:12:39 +00:00
|
|
|
|
2012-12-21 15:36:33 +00:00
|
|
|
Block* initFromStorage(void* storage, size_t size) {
|
|
|
|
SkASSERT(SkIsAlign4((intptr_t)storage));
|
|
|
|
SkASSERT(SkIsAlign4(size));
|
|
|
|
Block* block = this;
|
|
|
|
block->fNext = NULL;
|
|
|
|
block->fBasePtr = (char*)storage;
|
|
|
|
block->fSizeOfBlock = size;
|
|
|
|
block->fAllocatedSoFar = 0;
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MIN_BLOCKSIZE = sizeof(SkWriter32::Block) + sizeof(intptr_t)
|
|
|
|
};
|
2012-12-22 02:02:33 +00:00
|
|
|
|
2012-12-21 15:36:33 +00:00
|
|
|
Block fExternalBlock;
|
|
|
|
Block* fHead;
|
|
|
|
Block* fTail;
|
|
|
|
size_t fMinSize;
|
|
|
|
uint32_t fSize;
|
2012-12-07 17:48:04 +00:00
|
|
|
// sum of bytes written in all blocks *before* fTail
|
|
|
|
uint32_t fWrittenBeforeLastBlock;
|
|
|
|
|
2012-12-21 15:36:33 +00:00
|
|
|
bool isHeadExternallyAllocated() const {
|
|
|
|
return fHead == &fExternalBlock;
|
|
|
|
}
|
2012-04-24 21:12:39 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
Block* newBlock(size_t bytes);
|
2012-12-22 02:02:33 +00:00
|
|
|
|
2012-12-21 15:36:33 +00:00
|
|
|
// only call from reserve()
|
|
|
|
Block* doReserve(size_t bytes);
|
2012-08-30 14:03:21 +00:00
|
|
|
|
|
|
|
SkDEBUGCODE(void validate() const;)
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2012-06-12 20:47:53 +00:00
|
|
|
/**
|
|
|
|
* Helper class to allocated SIZE bytes as part of the writer, and to provide
|
|
|
|
* that storage to the constructor as its initial storage buffer.
|
|
|
|
*
|
|
|
|
* This wrapper ensures proper alignment rules are met for the storage.
|
|
|
|
*/
|
|
|
|
template <size_t SIZE> class SkSWriter32 : public SkWriter32 {
|
|
|
|
public:
|
|
|
|
SkSWriter32(size_t minSize) : SkWriter32(minSize, fData.fStorage, SIZE) {}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-06-12 20:47:53 +00:00
|
|
|
private:
|
|
|
|
union {
|
|
|
|
void* fPtrAlignment;
|
|
|
|
double fDoubleAlignment;
|
|
|
|
char fStorage[SIZE];
|
|
|
|
} fData;
|
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|