blob: c931ad3fd294bf784f7c5939e3a8d755b9ba6b50 [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
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000012#include "SkData.h"
msarette8597a42016-03-24 10:41:47 -070013#include "SkImage.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000014#include "SkPath.h"
15#include "SkPicture.h"
scroggo895c43b2014-12-11 10:53:58 -080016#include "SkPixelSerializer.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000017#include "SkRefCnt.h"
18#include "SkWriter32.h"
msaretta3b3b232016-04-22 12:43:07 -070019#include "../private/SkTHash.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000020
21class SkBitmap;
djsollen0b17d6c2014-11-13 12:52:35 -080022class SkBitmapHeap;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000023class SkFactorySet;
24class SkFlattenable;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000025class SkRefCntSet;
26
27class SkWriteBuffer {
28public:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000029 enum Flags {
30 kCrossProcess_Flag = 1 << 0,
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000031 };
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000032
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +000033 SkWriteBuffer(uint32_t flags = 0);
34 SkWriteBuffer(void* initialStorage, size_t storageSize, uint32_t flags = 0);
35 ~SkWriteBuffer();
36
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000037 bool isCrossProcess() const {
msaretta3b3b232016-04-22 12:43:07 -070038 return SkToBool(fFlags & kCrossProcess_Flag);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000039 }
40
41 SkWriter32* getWriter32() { return &fWriter; }
42 void reset(void* storage = NULL, size_t storageSize = 0) {
43 fWriter.reset(storage, storageSize);
44 }
45
46 uint32_t* reserve(size_t size) { return fWriter.reserve(size); }
47
48 size_t bytesWritten() const { return fWriter.bytesWritten(); }
49
50 void writeByteArray(const void* data, size_t size);
51 void writeDataAsByteArray(SkData* data) { this->writeByteArray(data->data(), data->size()); }
52 void writeBool(bool value);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000053 void writeScalar(SkScalar value);
54 void writeScalarArray(const SkScalar* value, uint32_t count);
55 void writeInt(int32_t value);
56 void writeIntArray(const int32_t* value, uint32_t count);
57 void writeUInt(uint32_t value);
58 void write32(int32_t value);
59 void writeString(const char* value);
60 void writeEncodedString(const void* value, size_t byteLength, SkPaint::TextEncoding encoding);
commit-bot@chromium.org39426e22014-04-16 16:24:08 +000061 void writeFunctionPtr(void* ptr) { fWriter.writePtr(ptr); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000062
63 void writeFlattenable(const SkFlattenable* flattenable);
64 void writeColor(const SkColor& color);
65 void writeColorArray(const SkColor* color, uint32_t count);
66 void writePoint(const SkPoint& point);
67 void writePointArray(const SkPoint* point, uint32_t count);
68 void writeMatrix(const SkMatrix& matrix);
69 void writeIRect(const SkIRect& rect);
70 void writeRect(const SkRect& rect);
71 void writeRegion(const SkRegion& region);
72 void writePath(const SkPath& path);
73 size_t writeStream(SkStream* stream, size_t length);
74 void writeBitmap(const SkBitmap& bitmap);
reed871872f2015-06-22 12:48:26 -070075 void writeImage(const SkImage*);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000076 void writeTypeface(SkTypeface* typeface);
77 void writePaint(const SkPaint& paint) { paint.flatten(*this); }
78
79 bool writeToStream(SkWStream*);
80 void writeToMemory(void* dst) { fWriter.flatten(dst); }
81
82 SkFactorySet* setFactoryRecorder(SkFactorySet*);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000083
84 SkRefCntSet* getTypefaceRecorder() const { return fTFSet; }
85 SkRefCntSet* setTypefaceRecorder(SkRefCntSet*);
86
87 /**
88 * Set an SkBitmapHeap to store bitmaps rather than flattening.
89 *
scroggo895c43b2014-12-11 10:53:58 -080090 * Incompatible with an SkPixelSerializer. If an SkPixelSerializer is set,
91 * setting an SkBitmapHeap will set the SkPixelSerializer to NULL in release
92 * and crash in debug.
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000093 */
94 void setBitmapHeap(SkBitmapHeap*);
95
96 /**
scroggo895c43b2014-12-11 10:53:58 -080097 * Set an SkPixelSerializer to store an encoded representation of pixels,
98 * e.g. SkBitmaps.
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000099 *
scroggo895c43b2014-12-11 10:53:58 -0800100 * Calls ref() on the serializer.
101 *
102 * TODO: Encode SkImage pixels as well.
103 *
104 * Incompatible with the SkBitmapHeap. If an encoder is set fBitmapHeap will
105 * be set to NULL in release and crash in debug.
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000106 */
scroggo895c43b2014-12-11 10:53:58 -0800107 void setPixelSerializer(SkPixelSerializer*);
reed871872f2015-06-22 12:48:26 -0700108 SkPixelSerializer* getPixelSerializer() const { return fPixelSerializer; }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000109
110private:
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000111 const uint32_t fFlags;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000112 SkFactorySet* fFactorySet;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000113 SkWriter32 fWriter;
114
115 SkBitmapHeap* fBitmapHeap;
116 SkRefCntSet* fTFSet;
117
scroggo895c43b2014-12-11 10:53:58 -0800118 SkAutoTUnref<SkPixelSerializer> fPixelSerializer;
msaretta3b3b232016-04-22 12:43:07 -0700119
120 // Only used if we do not have an fFactorySet
121 SkTHashMap<SkString, uint32_t> fFlattenableDict;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000122};
123
124#endif // SkWriteBuffer_DEFINED