blob: f3b414e104a46d52620da2cdcba69638ae9f8f91 [file] [log] [blame]
djsollen@google.com2b2ede32012-04-12 13:24:04 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef SkOrderedWriteBuffer_DEFINED
10#define SkOrderedWriteBuffer_DEFINED
11
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000012#include "SkFlattenableBuffers.h"
13
djsollen@google.com2b2ede32012-04-12 13:24:04 +000014#include "SkRefCnt.h"
djsollen@google.com21830d92012-08-07 19:49:41 +000015#include "SkBitmapHeap.h"
djsollen@google.com2b2ede32012-04-12 13:24:04 +000016#include "SkPath.h"
scroggo@google.com32ef1312013-02-22 22:04:19 +000017#include "SkPicture.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000018#include "SkWriter32.h"
19
junov@chromium.orgce65f382012-10-17 19:36:09 +000020class SkBitmap;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000021class SkFlattenable;
mike@reedtribe.org227b5162012-08-12 19:25:08 +000022class SkFactorySet;
23class SkNamedFactorySet;
24class SkRefCntSet;
djsollen@google.com2b2ede32012-04-12 13:24:04 +000025
26class SkOrderedWriteBuffer : public SkFlattenableWriteBuffer {
27public:
28 SkOrderedWriteBuffer(size_t minSize);
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +000029 SkOrderedWriteBuffer(size_t minSize, void* initialStorage, size_t storageSize);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000030 virtual ~SkOrderedWriteBuffer();
31
32 virtual bool isOrderedBinaryBuffer() SK_OVERRIDE { return true; }
33 virtual SkOrderedWriteBuffer* getOrderedBinaryBuffer() SK_OVERRIDE { return this; }
djsollen@google.com2b2ede32012-04-12 13:24:04 +000034
djsollen@google.comd2700ee2012-05-30 16:54:13 +000035 SkWriter32* getWriter32() { return &fWriter; }
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +000036 void reset(void* storage, size_t storageSize) { fWriter.reset(storage, storageSize); }
37
38 // Returns true if we've written only into the storage passed into constructor or reset.
39 // (You may be able to use this to avoid a call to writeToMemory.)
40 bool wroteOnlyToStorage() const { return fWriter.wroteOnlyToStorage(); }
djsollen@google.comd2700ee2012-05-30 16:54:13 +000041
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000042 void writeToMemory(void* dst) { fWriter.flatten(dst); }
43 uint32_t* reserve(size_t size) { return fWriter.reserve(size); }
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +000044
robertphillips@google.comadacc702013-10-14 21:53:24 +000045 size_t bytesWritten() const { return fWriter.bytesWritten(); }
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +000046 // Deprecated. Please call bytesWritten instead. TODO(mtklein): clean up
robertphillips@google.comadacc702013-10-14 21:53:24 +000047 size_t size() const { return this->bytesWritten(); }
djsollen@google.com2b2ede32012-04-12 13:24:04 +000048
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000049 virtual void writeByteArray(const void* data, size_t size) SK_OVERRIDE;
50 virtual void writeBool(bool value) SK_OVERRIDE;
51 virtual void writeFixed(SkFixed value) SK_OVERRIDE;
52 virtual void writeScalar(SkScalar value) SK_OVERRIDE;
53 virtual void writeScalarArray(const SkScalar* value, uint32_t count) SK_OVERRIDE;
54 virtual void writeInt(int32_t value) SK_OVERRIDE;
55 virtual void writeIntArray(const int32_t* value, uint32_t count) SK_OVERRIDE;
56 virtual void writeUInt(uint32_t value) SK_OVERRIDE;
57 virtual void write32(int32_t value) SK_OVERRIDE;
58 virtual void writeString(const char* value) SK_OVERRIDE;
59 virtual void writeEncodedString(const void* value, size_t byteLength,
60 SkPaint::TextEncoding encoding) SK_OVERRIDE;
djsollen@google.com2b2ede32012-04-12 13:24:04 +000061
robertphillips@google.comc2eae472013-10-21 12:26:10 +000062 virtual void writeFlattenable(const SkFlattenable* flattenable) SK_OVERRIDE;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000063 virtual void writeColor(const SkColor& color) SK_OVERRIDE;
64 virtual void writeColorArray(const SkColor* color, uint32_t count) SK_OVERRIDE;
65 virtual void writePoint(const SkPoint& point) SK_OVERRIDE;
66 virtual void writePointArray(const SkPoint* point, uint32_t count) SK_OVERRIDE;
67 virtual void writeMatrix(const SkMatrix& matrix) SK_OVERRIDE;
68 virtual void writeIRect(const SkIRect& rect)SK_OVERRIDE;
69 virtual void writeRect(const SkRect& rect) SK_OVERRIDE;
70 virtual void writeRegion(const SkRegion& region) SK_OVERRIDE;
71 virtual void writePath(const SkPath& path) SK_OVERRIDE;
72 virtual size_t writeStream(SkStream* stream, size_t length) SK_OVERRIDE;
djsollen@google.com2b2ede32012-04-12 13:24:04 +000073
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000074 virtual void writeBitmap(const SkBitmap& bitmap) SK_OVERRIDE;
75 virtual void writeTypeface(SkTypeface* typeface) SK_OVERRIDE;
76
77 virtual bool writeToStream(SkWStream*) SK_OVERRIDE;
78
79 SkFactorySet* setFactoryRecorder(SkFactorySet*);
80 SkNamedFactorySet* setNamedFactoryRecorder(SkNamedFactorySet*);
81
82 SkRefCntSet* getTypefaceRecorder() const { return fTFSet; }
83 SkRefCntSet* setTypefaceRecorder(SkRefCntSet*);
84
scroggo@google.com7c9d5392012-12-10 15:40:55 +000085 /**
86 * Set an SkBitmapHeap to store bitmaps rather than flattening.
87 *
88 * Incompatible with an EncodeBitmap function. If an EncodeBitmap function is set, setting an
89 * SkBitmapHeap will set the function to NULL in release mode and crash in debug.
90 */
91 void setBitmapHeap(SkBitmapHeap*);
djsollen@google.com2b2ede32012-04-12 13:24:04 +000092
scroggo@google.com5a7c6be2012-10-04 21:46:08 +000093 /**
scroggo@google.com1b1bcc32013-05-21 20:31:23 +000094 * Provide a function to encode an SkBitmap to an SkData. writeBitmap will attempt to use
scroggo@google.com7c9d5392012-12-10 15:40:55 +000095 * bitmapEncoder to store the SkBitmap. If the reader does not provide a function to decode, it
96 * will not be able to restore SkBitmaps, but will still be able to read the rest of the stream.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +000097 * bitmapEncoder will never be called with a NULL pixelRefOffset.
scroggo@google.com7c9d5392012-12-10 15:40:55 +000098 *
99 * Incompatible with the SkBitmapHeap. If an encoder is set fBitmapHeap will be set to NULL in
100 * release and crash in debug.
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000101 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000102 void setBitmapEncoder(SkPicture::EncodeBitmap bitmapEncoder);
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000103
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000104private:
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000105 SkFactorySet* fFactorySet;
106 SkNamedFactorySet* fNamedFactorySet;
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000107 SkWriter32 fWriter;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000108
djsollen@google.com21830d92012-08-07 19:49:41 +0000109 SkBitmapHeap* fBitmapHeap;
110 SkRefCntSet* fTFSet;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000111
scroggo@google.com32ef1312013-02-22 22:04:19 +0000112 SkPicture::EncodeBitmap fBitmapEncoder;
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000113
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000114 typedef SkFlattenableWriteBuffer INHERITED;
115};
116
117#endif // SkOrderedWriteBuffer_DEFINED