blob: 96809bf45c1f8673bbae980629040d37765e7209 [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();
89
90 const SkWriter32& writeStream() const {
91 return fWriter;
92 }
93
94private:
95 SkTDArray<uint32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000096 int fFirstSavedLayerIndex;
97 enum {
98 kNoSavedLayerIndex = -1
99 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100
101 void addDraw(DrawType drawType) {
102#ifdef SK_DEBUG_TRACE
103 SkDebugf("add %s\n", DrawTypeToString(drawType));
104#endif
105 fWriter.writeInt(drawType);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000106 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 void addInt(int value) {
108 fWriter.writeInt(value);
109 }
110 void addScalar(SkScalar scalar) {
111 fWriter.writeScalar(scalar);
112 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 void addBitmap(const SkBitmap& bitmap);
115 void addMatrix(const SkMatrix& matrix);
116 void addMatrixPtr(const SkMatrix* matrix);
117 void addPaint(const SkPaint& paint);
118 void addPaintPtr(const SkPaint* paint);
119 void addPath(const SkPath& path);
120 void addPicture(SkPicture& picture);
121 void addPoint(const SkPoint& point);
122 void addPoints(const SkPoint pts[], int count);
123 void addRect(const SkRect& rect);
124 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000125 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 void addIRectPtr(const SkIRect* rect);
127 void addRegion(const SkRegion& region);
128 void addText(const void* text, size_t byteLength);
129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130#ifdef SK_DEBUG_DUMP
131public:
132 void dumpMatrices();
133 void dumpPaints();
134#endif
135
136#ifdef SK_DEBUG_SIZE
137public:
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000138 size_t size() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 int bitmaps(size_t* size) const;
140 int matrices(size_t* size) const;
141 int paints(size_t* size) const;
142 int paths(size_t* size) const;
143 int regions(size_t* size) const;
144 size_t streamlen() const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 size_t fPointBytes, fRectBytes, fTextBytes;
147 int fPointWrites, fRectWrites, fTextWrites;
148#endif
149
150#ifdef SK_DEBUG_VALIDATE
151public:
152 void validate() const;
153private:
154 void validateBitmaps() const;
155 void validateMatrices() const;
156 void validatePaints() const;
157 void validatePaths() const;
158 void validateRegions() const;
159#else
160public:
161 void validate() const {}
162#endif
163
164private:
165 SkChunkAlloc fHeap;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000166 SkBitmapDictionary fBitmaps;
167 SkMatrixDictionary fMatrices;
168 SkPaintDictionary fPaints;
169 SkRegionDictionary fRegions;
170
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 SkPathHeap* fPathHeap; // reference counted
172 SkWriter32 fWriter;
173
reed@android.com09b84a02009-06-26 20:22:26 +0000174 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 SkTDArray<SkPicture*> fPictureRefs;
176
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +0000177 SkRefCntSet fRCSet;
178 SkRefCntSet fTFSet;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000179
reed@android.comae814c82009-02-13 14:56:09 +0000180 uint32_t fRecordFlags;
181
reed@google.com45482d12011-08-29 19:02:39 +0000182 // helper function to handle save/restore culling offsets
183 void recordOffsetForRestore(SkRegion::Op op);
184
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000186 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187
188 typedef SkCanvas INHERITED;
189};
190
191#endif