blob: ab56f9d48d9ac02c4513525492324d4439d4ac35 [file] [log] [blame]
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +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 SkWriteBuffer_DEFINED
10#define SkWriteBuffer_DEFINED
11
12#include "SkBitmapHeap.h"
13#include "SkData.h"
14#include "SkPath.h"
15#include "SkPicture.h"
16#include "SkRefCnt.h"
17#include "SkWriter32.h"
18
19class SkBitmap;
20class SkFactorySet;
21class SkFlattenable;
22class SkNamedFactorySet;
23class SkRefCntSet;
24
25class SkWriteBuffer {
26public:
27 SkWriteBuffer();
28 SkWriteBuffer(void* initialStorage, size_t storageSize);
29 ~SkWriteBuffer();
30
31 enum Flags {
32 kCrossProcess_Flag = 1 << 0,
33 kValidation_Flag = 1 << 1,
34 };
35 void setFlags(uint32_t flags) { fFlags = flags; }
36 uint32_t getFlags() const { return fFlags; }
37
38 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
39 bool isCrossProcess() const {
40 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
41 }
42
43 SkWriter32* getWriter32() { return &fWriter; }
44 void reset(void* storage = NULL, size_t storageSize = 0) {
45 fWriter.reset(storage, storageSize);
46 }
47
48 uint32_t* reserve(size_t size) { return fWriter.reserve(size); }
49
50 size_t bytesWritten() const { return fWriter.bytesWritten(); }
51
52 void writeByteArray(const void* data, size_t size);
53 void writeDataAsByteArray(SkData* data) { this->writeByteArray(data->data(), data->size()); }
54 void writeBool(bool value);
55 void writeFixed(SkFixed value);
56 void writeScalar(SkScalar value);
57 void writeScalarArray(const SkScalar* value, uint32_t count);
58 void writeInt(int32_t value);
59 void writeIntArray(const int32_t* value, uint32_t count);
60 void writeUInt(uint32_t value);
61 void write32(int32_t value);
62 void writeString(const char* value);
63 void writeEncodedString(const void* value, size_t byteLength, SkPaint::TextEncoding encoding);
64 void writeFunctionPtr(void* ptr) { this->writeByteArray(&ptr, sizeof(ptr)); }
65
66 void writeFlattenable(const SkFlattenable* flattenable);
67 void writeColor(const SkColor& color);
68 void writeColorArray(const SkColor* color, uint32_t count);
69 void writePoint(const SkPoint& point);
70 void writePointArray(const SkPoint* point, uint32_t count);
71 void writeMatrix(const SkMatrix& matrix);
72 void writeIRect(const SkIRect& rect);
73 void writeRect(const SkRect& rect);
74 void writeRegion(const SkRegion& region);
75 void writePath(const SkPath& path);
76 size_t writeStream(SkStream* stream, size_t length);
77 void writeBitmap(const SkBitmap& bitmap);
78 void writeTypeface(SkTypeface* typeface);
79 void writePaint(const SkPaint& paint) { paint.flatten(*this); }
80
81 bool writeToStream(SkWStream*);
82 void writeToMemory(void* dst) { fWriter.flatten(dst); }
83
84 SkFactorySet* setFactoryRecorder(SkFactorySet*);
85 SkNamedFactorySet* setNamedFactoryRecorder(SkNamedFactorySet*);
86
87 SkRefCntSet* getTypefaceRecorder() const { return fTFSet; }
88 SkRefCntSet* setTypefaceRecorder(SkRefCntSet*);
89
90 /**
91 * Set an SkBitmapHeap to store bitmaps rather than flattening.
92 *
93 * Incompatible with an EncodeBitmap function. If an EncodeBitmap function is set, setting an
94 * SkBitmapHeap will set the function to NULL in release mode and crash in debug.
95 */
96 void setBitmapHeap(SkBitmapHeap*);
97
98 /**
99 * Provide a function to encode an SkBitmap to an SkData. writeBitmap will attempt to use
100 * bitmapEncoder to store the SkBitmap. If the reader does not provide a function to decode, it
101 * will not be able to restore SkBitmaps, but will still be able to read the rest of the stream.
102 * bitmapEncoder will never be called with a NULL pixelRefOffset.
103 *
104 * Incompatible with the SkBitmapHeap. If an encoder is set fBitmapHeap will be set to NULL in
105 * release and crash in debug.
106 */
107 void setBitmapEncoder(SkPicture::EncodeBitmap bitmapEncoder);
108
109private:
110 uint32_t fFlags;
111 SkFactorySet* fFactorySet;
112 SkNamedFactorySet* fNamedFactorySet;
113 SkWriter32 fWriter;
114
115 SkBitmapHeap* fBitmapHeap;
116 SkRefCntSet* fTFSet;
117
118 SkPicture::EncodeBitmap fBitmapEncoder;
119};
120
121#endif // SkWriteBuffer_DEFINED