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
|
|
|
|
|
2015-08-19 18:56:48 +00:00
|
|
|
#include "../private/SkTemplates.h"
|
2014-03-12 17:04:28 +00:00
|
|
|
#include "SkData.h"
|
2014-01-14 20:51:26 +00:00
|
|
|
#include "SkMatrix.h"
|
2012-06-08 18:30:46 +00:00
|
|
|
#include "SkPath.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkPoint.h"
|
2012-12-12 20:48:18 +00:00
|
|
|
#include "SkRRect.h"
|
2014-01-14 20:51:26 +00:00
|
|
|
#include "SkRect.h"
|
2012-04-12 13:24:04 +00:00
|
|
|
#include "SkRegion.h"
|
2014-01-14 20:51:26 +00:00
|
|
|
#include "SkScalar.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTypes.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-06-26 15:01:14 +00:00
|
|
|
class SK_API SkWriter32 : SkNoncopyable {
|
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.
|
2014-01-14 20:51:26 +00:00
|
|
|
*
|
|
|
|
* SkWriter32 will try to back reserve and write calls with this external storage until the
|
|
|
|
* first time an allocation doesn't fit. From then it will use dynamically allocated storage.
|
|
|
|
* This used to be optional behavior, but pipe now relies on it.
|
2012-04-24 21:12:39 +00:00
|
|
|
*/
|
2014-02-13 18:35:54 +00:00
|
|
|
SkWriter32(void* external = NULL, size_t externalBytes = 0) {
|
2014-01-14 20:51:26 +00:00
|
|
|
this->reset(external, externalBytes);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-08-30 14:03:21 +00:00
|
|
|
// return the current offset (will always be a multiple of 4)
|
2014-02-11 10:17:02 +00:00
|
|
|
size_t bytesWritten() const { return fUsed; }
|
2013-10-31 17:28:30 +00:00
|
|
|
|
|
|
|
SK_ATTR_DEPRECATED("use bytesWritten")
|
2013-10-14 21:53:24 +00:00
|
|
|
size_t size() const { return this->bytesWritten(); }
|
2012-08-30 14:03:21 +00:00
|
|
|
|
2014-01-14 20:51:26 +00:00
|
|
|
void reset(void* external = NULL, size_t externalBytes = 0) {
|
|
|
|
SkASSERT(SkIsAlign4((uintptr_t)external));
|
|
|
|
SkASSERT(SkIsAlign4(externalBytes));
|
2014-02-11 10:17:02 +00:00
|
|
|
|
|
|
|
fData = (uint8_t*)external;
|
|
|
|
fCapacity = externalBytes;
|
|
|
|
fUsed = 0;
|
|
|
|
fExternal = external;
|
2013-07-24 20:37:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 10:17:02 +00:00
|
|
|
// Returns the current buffer.
|
|
|
|
// The pointer may be invalidated by any future write calls.
|
2014-01-14 20:51:26 +00:00
|
|
|
const uint32_t* contiguousArray() const {
|
2014-02-11 10:17:02 +00:00
|
|
|
return (uint32_t*)fData;
|
2014-01-14 20:51:26 +00:00
|
|
|
}
|
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);
|
2014-02-11 10:17:02 +00:00
|
|
|
size_t offset = fUsed;
|
|
|
|
size_t totalRequired = fUsed + size;
|
|
|
|
if (totalRequired > fCapacity) {
|
2014-02-11 18:22:04 +00:00
|
|
|
this->growToAtLeast(totalRequired);
|
2014-01-14 20:51:26 +00:00
|
|
|
}
|
2014-02-11 10:17:02 +00:00
|
|
|
fUsed = totalRequired;
|
|
|
|
return (uint32_t*)(fData + offset);
|
2014-01-14 20:51:26 +00:00
|
|
|
}
|
2012-12-22 02:02:33 +00:00
|
|
|
|
2014-02-11 22:38:51 +00:00
|
|
|
/**
|
|
|
|
* Read a T record at offset, which must be a multiple of 4. Only legal if the record
|
2014-03-12 17:04:28 +00:00
|
|
|
* was written atomically using the write methods below.
|
2014-02-11 22:38:51 +00:00
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
const T& readTAt(size_t offset) const {
|
2014-02-11 10:17:02 +00:00
|
|
|
SkASSERT(SkAlign4(offset) == offset);
|
|
|
|
SkASSERT(offset < fUsed);
|
2014-02-11 22:38:51 +00:00
|
|
|
return *(T*)(fData + offset);
|
2014-02-11 10:17:02 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 22:38:51 +00:00
|
|
|
/**
|
|
|
|
* Overwrite a T record at offset, which must be a multiple of 4. Only legal if the record
|
2014-03-12 17:04:28 +00:00
|
|
|
* was written atomically using the write methods below.
|
2014-02-11 22:38:51 +00:00
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
void overwriteTAt(size_t offset, const T& value) {
|
2014-02-11 10:17:02 +00:00
|
|
|
SkASSERT(SkAlign4(offset) == offset);
|
|
|
|
SkASSERT(offset < fUsed);
|
2014-02-11 22:38:51 +00:00
|
|
|
*(T*)(fData + offset) = value;
|
2014-02-11 10:17:02 +00:00
|
|
|
}
|
2012-08-30 14:38:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
bool writeBool(bool value) {
|
2014-01-14 20:51:26 +00:00
|
|
|
this->write32(value);
|
2008-12-17 15:59:43 +00:00
|
|
|
return value;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void writeInt(int32_t value) {
|
2014-01-14 20:51:26 +00:00
|
|
|
this->write32(value);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
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
|
|
|
|
2014-01-14 20:51:26 +00:00
|
|
|
void writePtr(void* value) {
|
|
|
|
*(void**)this->reserve(sizeof(value)) = value;
|
2012-06-12 20:47:53 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-08-29 20:20:40 +00:00
|
|
|
void writeIRect(const SkIRect& rect) {
|
|
|
|
*(SkIRect*)this->reserve(sizeof(rect)) = rect;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
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.
|
|
|
|
*/
|
2014-01-14 20:51:26 +00:00
|
|
|
uint32_t* reservePad(size_t size) {
|
2014-02-11 22:38:51 +00:00
|
|
|
size_t alignedSize = SkAlign4(size);
|
|
|
|
uint32_t* p = this->reserve(alignedSize);
|
|
|
|
if (alignedSize != size) {
|
|
|
|
SkASSERT(alignedSize >= 4);
|
|
|
|
p[alignedSize / 4 - 1] = 0;
|
2014-01-14 20:51:26 +00:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Write size bytes from src, and pad to 4 byte alignment with zeroes.
|
|
|
|
*/
|
2014-01-14 20:51:26 +00:00
|
|
|
void writePad(const void* src, size_t size) {
|
|
|
|
memcpy(this->reservePad(size), src, 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
|
2013-08-15 21:01:32 +00:00
|
|
|
* calling strlen(). The length must be < max size_t.
|
|
|
|
*
|
|
|
|
* If you write NULL, it will be read as "".
|
2011-05-23 12:21:05 +00:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
2012-08-30 14:03:21 +00:00
|
|
|
/**
|
|
|
|
* Move the cursor back to offset bytes from the beginning.
|
2014-01-31 16:22:57 +00:00
|
|
|
* offset must be a multiple of 4 no greater than size().
|
2012-08-30 14:03:21 +00:00
|
|
|
*/
|
2014-01-14 20:51:26 +00:00
|
|
|
void rewindToOffset(size_t offset) {
|
|
|
|
SkASSERT(SkAlign4(offset) == offset);
|
2014-02-11 10:17:02 +00:00
|
|
|
SkASSERT(offset <= bytesWritten());
|
|
|
|
fUsed = offset;
|
2014-01-14 20:51:26 +00:00
|
|
|
}
|
2012-08-30 14:03:21 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// copy into a single buffer (allocated by caller). Must be at least size()
|
2014-01-14 20:51:26 +00:00
|
|
|
void flatten(void* dst) const {
|
2014-02-11 10:17:02 +00:00
|
|
|
memcpy(dst, fData, fUsed);
|
2014-01-14 20:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool writeToStream(SkWStream* stream) const {
|
2014-02-11 10:17:02 +00:00
|
|
|
return stream->write(fData, fUsed);
|
2014-01-14 20:51:26 +00:00
|
|
|
}
|
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.
|
2014-01-14 20:51:26 +00:00
|
|
|
size_t readFromStream(SkStream* stream, size_t length) {
|
|
|
|
return stream->read(this->reservePad(length), length);
|
2012-12-21 15:36:33 +00:00
|
|
|
}
|
2012-04-24 21:12:39 +00:00
|
|
|
|
2014-03-12 17:04:28 +00:00
|
|
|
/**
|
|
|
|
* Captures a snapshot of the data as it is right now, and return it.
|
|
|
|
*/
|
|
|
|
SkData* snapshotAsData() const;
|
2014-01-14 20:51:26 +00:00
|
|
|
private:
|
2014-02-11 10:17:02 +00:00
|
|
|
void growToAtLeast(size_t size);
|
2012-08-30 14:03:21 +00:00
|
|
|
|
2014-02-13 18:35:54 +00:00
|
|
|
uint8_t* fData; // Points to either fInternal or fExternal.
|
|
|
|
size_t fCapacity; // Number of bytes we can write to fData.
|
|
|
|
size_t fUsed; // Number of bytes written.
|
|
|
|
void* fExternal; // Unmanaged memory block.
|
|
|
|
SkAutoTMalloc<uint8_t> fInternal; // Managed memory block.
|
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:
|
2014-02-11 18:22:04 +00:00
|
|
|
SkSWriter32() { this->reset(); }
|
|
|
|
|
|
|
|
void reset() {this->INHERITED::reset(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;
|
2014-02-11 18:22:04 +00:00
|
|
|
|
|
|
|
typedef SkWriter32 INHERITED;
|
2012-06-12 20:47:53 +00:00
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|