blob: 7e5f086c7b74cc42b4f6464627c351a0f3cb2b81 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SkPictureRecord_DEFINED
9#define SkPictureRecord_DEFINED
10
11#include "SkCanvas.h"
12#include "SkFlattenable.h"
13#include "SkPathHeap.h"
14#include "SkPicture.h"
15#include "SkPictureFlat.h"
16#include "SkTemplates.h"
17#include "SkWriter32.h"
18
19class SkPictureRecord : public SkCanvas {
20public:
reed@android.comae814c82009-02-13 14:56:09 +000021 SkPictureRecord(uint32_t recordFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 virtual ~SkPictureRecord();
23
reed@google.com2d4297c2011-10-06 13:14:12 +000024 virtual int save(SaveFlags) SK_OVERRIDE;
25 virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
26 virtual void restore() SK_OVERRIDE;
27 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
28 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
29 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
30 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
31 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
32 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com071eef92011-10-12 11:52:53 +000033 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
34 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000035 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
36 virtual void clear(SkColor) SK_OVERRIDE;
37 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000039 const SkPaint&) SK_OVERRIDE;
40 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
41 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000043 const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
reed@google.com2d4297c2011-10-06 13:14:12 +000045 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000047 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000048 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000049 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000051 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000052 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000053 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000054 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000055 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 virtual void drawPosTextH(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000057 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000058 virtual void drawTextOnPath(const void* text, size_t byteLength,
59 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +000060 const SkPaint&) SK_OVERRIDE;
61 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 virtual void drawVertices(VertexMode, int vertexCount,
63 const SkPoint vertices[], const SkPoint texs[],
64 const SkColor colors[], SkXfermode*,
65 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +000066 const SkPaint&) SK_OVERRIDE;
67 virtual void drawData(const void*, size_t) SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000068 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000069
reed@google.com9efd9a02012-01-30 15:41:43 +000070 void addFontMetricsTopBottom(const SkPaint& paint, SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000071
djsollen@google.comd2700ee2012-05-30 16:54:13 +000072 const SkBitmapDictionary& getBitmaps() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 return fBitmaps;
74 }
djsollen@google.comd2700ee2012-05-30 16:54:13 +000075 const SkMatrixDictionary& getMatrices() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 return fMatrices;
77 }
djsollen@google.comd2700ee2012-05-30 16:54:13 +000078 const SkPaintDictionary& getPaints() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 return fPaints;
80 }
81 const SkTDArray<SkPicture* >& getPictureRefs() const {
82 return fPictureRefs;
83 }
djsollen@google.comd2700ee2012-05-30 16:54:13 +000084 const SkRegionDictionary& getRegions() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 return fRegions;
86 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 void reset();
junov@chromium.org4866cc02012-06-01 21:23:07 +000089 void setFlags(uint32_t recordFlags) {
90 fRecordFlags = recordFlags;
91 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000092
93 const SkWriter32& writeStream() const {
94 return fWriter;
95 }
96
junov@chromium.org4866cc02012-06-01 21:23:07 +000097 bool shouldFlattenPixels(const SkBitmap&) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000098private:
junov@chromium.org4866cc02012-06-01 21:23:07 +000099 struct PixelRefDictionaryEntry {
100 uint32_t fKey; // SkPixelRef GenerationID.
101 uint32_t fIndex; // Index of corresponding flattened bitmap in fBitmaps.
102 bool operator < (const PixelRefDictionaryEntry& other) const {
103 return this->fKey < other.fKey;
104 }
105 bool operator != (const PixelRefDictionaryEntry& other) const {
106 return this->fKey != other.fKey;
107 }
108 };
109
junov@chromium.orge3dbedb2012-07-09 16:03:55 +0000110 void recordRestoreOffsetPlaceholder(SkRegion::Op);
111 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(
112 uint32_t restoreOffset);
113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 SkTDArray<uint32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000115 int fFirstSavedLayerIndex;
116 enum {
117 kNoSavedLayerIndex = -1
118 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119
120 void addDraw(DrawType drawType) {
121#ifdef SK_DEBUG_TRACE
122 SkDebugf("add %s\n", DrawTypeToString(drawType));
123#endif
124 fWriter.writeInt(drawType);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000125 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 void addInt(int value) {
127 fWriter.writeInt(value);
128 }
129 void addScalar(SkScalar scalar) {
130 fWriter.writeScalar(scalar);
131 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 void addBitmap(const SkBitmap& bitmap);
134 void addMatrix(const SkMatrix& matrix);
135 void addMatrixPtr(const SkMatrix* matrix);
136 void addPaint(const SkPaint& paint);
137 void addPaintPtr(const SkPaint* paint);
138 void addPath(const SkPath& path);
139 void addPicture(SkPicture& picture);
140 void addPoint(const SkPoint& point);
141 void addPoints(const SkPoint pts[], int count);
142 void addRect(const SkRect& rect);
143 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000144 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 void addIRectPtr(const SkIRect* rect);
146 void addRegion(const SkRegion& region);
147 void addText(const void* text, size_t byteLength);
148
junov@chromium.org4866cc02012-06-01 21:23:07 +0000149 int find(const SkBitmap& bitmap);
150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151#ifdef SK_DEBUG_DUMP
152public:
153 void dumpMatrices();
154 void dumpPaints();
155#endif
156
157#ifdef SK_DEBUG_SIZE
158public:
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000159 size_t size() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 int bitmaps(size_t* size) const;
161 int matrices(size_t* size) const;
162 int paints(size_t* size) const;
163 int paths(size_t* size) const;
164 int regions(size_t* size) const;
165 size_t streamlen() const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000166
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 size_t fPointBytes, fRectBytes, fTextBytes;
168 int fPointWrites, fRectWrites, fTextWrites;
169#endif
170
171#ifdef SK_DEBUG_VALIDATE
172public:
173 void validate() const;
174private:
175 void validateBitmaps() const;
176 void validateMatrices() const;
177 void validatePaints() const;
178 void validatePaths() const;
179 void validateRegions() const;
180#else
181public:
182 void validate() const {}
183#endif
184
185private:
186 SkChunkAlloc fHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000187
188 SkTDArray<PixelRefDictionaryEntry> fPixelRefDictionary;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000189 SkBitmapDictionary fBitmaps;
190 SkMatrixDictionary fMatrices;
191 SkPaintDictionary fPaints;
192 SkRegionDictionary fRegions;
193
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 SkPathHeap* fPathHeap; // reference counted
195 SkWriter32 fWriter;
196
reed@android.com09b84a02009-06-26 20:22:26 +0000197 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 SkTDArray<SkPicture*> fPictureRefs;
199
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +0000200 SkRefCntSet fRCSet;
201 SkRefCntSet fTFSet;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000202
reed@android.comae814c82009-02-13 14:56:09 +0000203 uint32_t fRecordFlags;
204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000206 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207
208 typedef SkCanvas INHERITED;
209};
210
211#endif