blob: a19a084534fca07438732e4f64ac00365edd2b6a [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
junov@chromium.org4e6dfa52012-07-16 14:04:59 +000024 virtual SkDevice* setDevice(SkDevice* device) SK_OVERRIDE;
25
reed@google.com2d4297c2011-10-06 13:14:12 +000026 virtual int save(SaveFlags) SK_OVERRIDE;
27 virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
28 virtual void restore() SK_OVERRIDE;
29 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
30 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
31 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
32 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
33 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
34 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com071eef92011-10-12 11:52:53 +000035 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
36 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000037 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
38 virtual void clear(SkColor) SK_OVERRIDE;
39 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000041 const SkPaint&) SK_OVERRIDE;
42 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
43 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000045 const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
reed@google.com2d4297c2011-10-06 13:14:12 +000047 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000049 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000050 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000051 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000053 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000054 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000055 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000056 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000057 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 virtual void drawPosTextH(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000059 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000060 virtual void drawTextOnPath(const void* text, size_t byteLength,
61 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +000062 const SkPaint&) SK_OVERRIDE;
63 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 virtual void drawVertices(VertexMode, int vertexCount,
65 const SkPoint vertices[], const SkPoint texs[],
66 const SkColor colors[], SkXfermode*,
67 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +000068 const SkPaint&) SK_OVERRIDE;
69 virtual void drawData(const void*, size_t) SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000070 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000071
reed@google.com9efd9a02012-01-30 15:41:43 +000072 void addFontMetricsTopBottom(const SkPaint& paint, SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 const SkTDArray<SkPicture* >& getPictureRefs() const {
75 return fPictureRefs;
76 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000077
junov@chromium.org4866cc02012-06-01 21:23:07 +000078 void setFlags(uint32_t recordFlags) {
79 fRecordFlags = recordFlags;
80 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000081
82 const SkWriter32& writeStream() const {
83 return fWriter;
84 }
85
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +000086 void endRecording();
reed@android.com8a1c16f2008-12-17 15:59:43 +000087private:
junov@chromium.orge3dbedb2012-07-09 16:03:55 +000088 void recordRestoreOffsetPlaceholder(SkRegion::Op);
89 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(
90 uint32_t restoreOffset);
91
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 SkTDArray<uint32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000093 int fFirstSavedLayerIndex;
94 enum {
95 kNoSavedLayerIndex = -1
96 };
reed@android.com8a1c16f2008-12-17 15:59:43 +000097
98 void addDraw(DrawType drawType) {
reed@google.com97af1a62012-08-28 12:19:02 +000099 this->predrawNotify();
100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101#ifdef SK_DEBUG_TRACE
102 SkDebugf("add %s\n", DrawTypeToString(drawType));
103#endif
104 fWriter.writeInt(drawType);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000105 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 void addInt(int value) {
107 fWriter.writeInt(value);
108 }
109 void addScalar(SkScalar scalar) {
110 fWriter.writeScalar(scalar);
111 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 void addBitmap(const SkBitmap& bitmap);
114 void addMatrix(const SkMatrix& matrix);
115 void addMatrixPtr(const SkMatrix* matrix);
116 void addPaint(const SkPaint& paint);
117 void addPaintPtr(const SkPaint* paint);
118 void addPath(const SkPath& path);
119 void addPicture(SkPicture& picture);
120 void addPoint(const SkPoint& point);
121 void addPoints(const SkPoint pts[], int count);
122 void addRect(const SkRect& rect);
123 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000124 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 void addIRectPtr(const SkIRect* rect);
126 void addRegion(const SkRegion& region);
127 void addText(const void* text, size_t byteLength);
128
junov@chromium.org4866cc02012-06-01 21:23:07 +0000129 int find(const SkBitmap& bitmap);
130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131#ifdef SK_DEBUG_DUMP
132public:
133 void dumpMatrices();
134 void dumpPaints();
135#endif
136
137#ifdef SK_DEBUG_SIZE
138public:
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000139 size_t size() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 int bitmaps(size_t* size) const;
141 int matrices(size_t* size) const;
142 int paints(size_t* size) const;
143 int paths(size_t* size) const;
144 int regions(size_t* size) const;
145 size_t streamlen() const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 size_t fPointBytes, fRectBytes, fTextBytes;
148 int fPointWrites, fRectWrites, fTextWrites;
149#endif
150
151#ifdef SK_DEBUG_VALIDATE
152public:
153 void validate() const;
154private:
155 void validateBitmaps() const;
156 void validateMatrices() const;
157 void validatePaints() const;
158 void validatePaths() const;
159 void validateRegions() const;
160#else
161public:
162 void validate() const {}
163#endif
164
165private:
djsollen@google.com21830d92012-08-07 19:49:41 +0000166 SkBitmapHeap fBitmapHeap;
167 SkChunkFlatController fFlattenableHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000168
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000169 SkMatrixDictionary fMatrices;
170 SkPaintDictionary fPaints;
171 SkRegionDictionary fRegions;
172
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 SkPathHeap* fPathHeap; // reference counted
174 SkWriter32 fWriter;
175
reed@android.com09b84a02009-06-26 20:22:26 +0000176 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 SkTDArray<SkPicture*> fPictureRefs;
178
reed@android.comae814c82009-02-13 14:56:09 +0000179 uint32_t fRecordFlags;
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000180 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000181
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000183 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184
185 typedef SkCanvas INHERITED;
186};
187
188#endif