blob: 3ae82d014aee1d659912faa155e81c85dd59a240 [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"
19
20class SkBitmap;
djsollen0b17d6c2014-11-13 12:52:35 -080021class SkBitmapHeap;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000022class SkFactorySet;
23class SkFlattenable;
24class SkNamedFactorySet;
25class SkRefCntSet;
26
27class SkWriteBuffer {
28public:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000029 enum Flags {
30 kCrossProcess_Flag = 1 << 0,
31 kValidation_Flag = 1 << 1,
32 };
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000033
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +000034 SkWriteBuffer(uint32_t flags = 0);
35 SkWriteBuffer(void* initialStorage, size_t storageSize, uint32_t flags = 0);
36 ~SkWriteBuffer();
37
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000038 bool isCrossProcess() const {
39 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
40 }
41
42 SkWriter32* getWriter32() { return &fWriter; }
43 void reset(void* storage = NULL, size_t storageSize = 0) {
44 fWriter.reset(storage, storageSize);
45 }
46
47 uint32_t* reserve(size_t size) { return fWriter.reserve(size); }
48
49 size_t bytesWritten() const { return fWriter.bytesWritten(); }
50
51 void writeByteArray(const void* data, size_t size);
52 void writeDataAsByteArray(SkData* data) { this->writeByteArray(data->data(), data->size()); }
53 void writeBool(bool value);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000054 void writeScalar(SkScalar value);
55 void writeScalarArray(const SkScalar* value, uint32_t count);
56 void writeInt(int32_t value);
57 void writeIntArray(const int32_t* value, uint32_t count);
58 void writeUInt(uint32_t value);
59 void write32(int32_t value);
60 void writeString(const char* value);
61 void writeEncodedString(const void* value, size_t byteLength, SkPaint::TextEncoding encoding);
commit-bot@chromium.org39426e22014-04-16 16:24:08 +000062 void writeFunctionPtr(void* ptr) { fWriter.writePtr(ptr); }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000063
64 void writeFlattenable(const SkFlattenable* flattenable);
65 void writeColor(const SkColor& color);
66 void writeColorArray(const SkColor* color, uint32_t count);
67 void writePoint(const SkPoint& point);
68 void writePointArray(const SkPoint* point, uint32_t count);
69 void writeMatrix(const SkMatrix& matrix);
70 void writeIRect(const SkIRect& rect);
71 void writeRect(const SkRect& rect);
72 void writeRegion(const SkRegion& region);
73 void writePath(const SkPath& path);
74 size_t writeStream(SkStream* stream, size_t length);
75 void writeBitmap(const SkBitmap& bitmap);
reed871872f2015-06-22 12:48:26 -070076 void writeImage(const SkImage*);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000077 void writeTypeface(SkTypeface* typeface);
78 void writePaint(const SkPaint& paint) { paint.flatten(*this); }
79
80 bool writeToStream(SkWStream*);
81 void writeToMemory(void* dst) { fWriter.flatten(dst); }
82
83 SkFactorySet* setFactoryRecorder(SkFactorySet*);
84 SkNamedFactorySet* setNamedFactoryRecorder(SkNamedFactorySet*);
85
86 SkRefCntSet* getTypefaceRecorder() const { return fTFSet; }
87 SkRefCntSet* setTypefaceRecorder(SkRefCntSet*);
88
89 /**
90 * Set an SkBitmapHeap to store bitmaps rather than flattening.
91 *
scroggo895c43b2014-12-11 10:53:58 -080092 * Incompatible with an SkPixelSerializer. If an SkPixelSerializer is set,
93 * setting an SkBitmapHeap will set the SkPixelSerializer to NULL in release
94 * and crash in debug.
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000095 */
96 void setBitmapHeap(SkBitmapHeap*);
97
98 /**
scroggo895c43b2014-12-11 10:53:58 -080099 * Set an SkPixelSerializer to store an encoded representation of pixels,
100 * e.g. SkBitmaps.
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000101 *
scroggo895c43b2014-12-11 10:53:58 -0800102 * Calls ref() on the serializer.
103 *
104 * TODO: Encode SkImage pixels as well.
105 *
106 * Incompatible with the SkBitmapHeap. If an encoder is set fBitmapHeap will
107 * be set to NULL in release and crash in debug.
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000108 */
scroggo895c43b2014-12-11 10:53:58 -0800109 void setPixelSerializer(SkPixelSerializer*);
reed871872f2015-06-22 12:48:26 -0700110 SkPixelSerializer* getPixelSerializer() const { return fPixelSerializer; }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000111
112private:
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000113 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
114
115 const uint32_t fFlags;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000116 SkFactorySet* fFactorySet;
117 SkNamedFactorySet* fNamedFactorySet;
118 SkWriter32 fWriter;
119
120 SkBitmapHeap* fBitmapHeap;
121 SkRefCntSet* fTFSet;
122
scroggo895c43b2014-12-11 10:53:58 -0800123 SkAutoTUnref<SkPixelSerializer> fPixelSerializer;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000124};
125
126#endif // SkWriteBuffer_DEFINED