blob: 77e011416fb9fd8c7909e2f2af762d08dde4a79b [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
rileya@google.com9f5898d2012-09-11 20:21:44 +000019class SkPictureStateTree;
20class SkBBoxHierarchy;
21
reed@android.com8a1c16f2008-12-17 15:59:43 +000022class SkPictureRecord : public SkCanvas {
23public:
reed@google.comd86e7ab2012-09-27 20:31:31 +000024 SkPictureRecord(uint32_t recordFlags, SkDevice*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 virtual ~SkPictureRecord();
26
junov@chromium.org4e6dfa52012-07-16 14:04:59 +000027 virtual SkDevice* setDevice(SkDevice* device) SK_OVERRIDE;
28
reed@google.com2d4297c2011-10-06 13:14:12 +000029 virtual int save(SaveFlags) SK_OVERRIDE;
30 virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
31 virtual void restore() SK_OVERRIDE;
32 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
33 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
34 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
35 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
36 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
37 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com071eef92011-10-12 11:52:53 +000038 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
39 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000040 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
41 virtual void clear(SkColor) SK_OVERRIDE;
42 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000044 const SkPaint&) SK_OVERRIDE;
45 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
46 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000048 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +000049 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
50 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000052 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000053 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000054 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000056 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000057 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000058 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000059 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000060 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 virtual void drawPosTextH(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000062 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000063 virtual void drawTextOnPath(const void* text, size_t byteLength,
64 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +000065 const SkPaint&) SK_OVERRIDE;
66 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 virtual void drawVertices(VertexMode, int vertexCount,
68 const SkPoint vertices[], const SkPoint texs[],
69 const SkColor colors[], SkXfermode*,
70 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +000071 const SkPaint&) SK_OVERRIDE;
72 virtual void drawData(const void*, size_t) SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000073 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
reed@google.com9efd9a02012-01-30 15:41:43 +000075 void addFontMetricsTopBottom(const SkPaint& paint, SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000076
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 const SkTDArray<SkPicture* >& getPictureRefs() const {
78 return fPictureRefs;
79 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000080
junov@chromium.org4866cc02012-06-01 21:23:07 +000081 void setFlags(uint32_t recordFlags) {
82 fRecordFlags = recordFlags;
83 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000084
85 const SkWriter32& writeStream() const {
86 return fWriter;
87 }
88
reed@google.comd86e7ab2012-09-27 20:31:31 +000089 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +000090 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +000091
reed@android.com8a1c16f2008-12-17 15:59:43 +000092private:
junov@chromium.orge3dbedb2012-07-09 16:03:55 +000093 void recordRestoreOffsetPlaceholder(SkRegion::Op);
94 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(
95 uint32_t restoreOffset);
96
reed@google.comffacd3c2012-08-30 15:31:23 +000097 SkTDArray<int32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000098 int fFirstSavedLayerIndex;
99 enum {
100 kNoSavedLayerIndex = -1
101 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102
103 void addDraw(DrawType drawType) {
reed@google.com97af1a62012-08-28 12:19:02 +0000104 this->predrawNotify();
105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106#ifdef SK_DEBUG_TRACE
107 SkDebugf("add %s\n", DrawTypeToString(drawType));
108#endif
109 fWriter.writeInt(drawType);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000110 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 void addInt(int value) {
112 fWriter.writeInt(value);
113 }
114 void addScalar(SkScalar scalar) {
115 fWriter.writeScalar(scalar);
116 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000117
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 void addBitmap(const SkBitmap& bitmap);
119 void addMatrix(const SkMatrix& matrix);
120 void addMatrixPtr(const SkMatrix* matrix);
121 void addPaint(const SkPaint& paint);
122 void addPaintPtr(const SkPaint* paint);
123 void addPath(const SkPath& path);
124 void addPicture(SkPicture& picture);
125 void addPoint(const SkPoint& point);
126 void addPoints(const SkPoint pts[], int count);
127 void addRect(const SkRect& rect);
128 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000129 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 void addIRectPtr(const SkIRect* rect);
131 void addRegion(const SkRegion& region);
132 void addText(const void* text, size_t byteLength);
133
junov@chromium.org4866cc02012-06-01 21:23:07 +0000134 int find(const SkBitmap& bitmap);
135
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136#ifdef SK_DEBUG_DUMP
137public:
138 void dumpMatrices();
139 void dumpPaints();
140#endif
141
142#ifdef SK_DEBUG_SIZE
143public:
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000144 size_t size() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 int bitmaps(size_t* size) const;
146 int matrices(size_t* size) const;
147 int paints(size_t* size) const;
148 int paths(size_t* size) const;
149 int regions(size_t* size) const;
150 size_t streamlen() const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 size_t fPointBytes, fRectBytes, fTextBytes;
153 int fPointWrites, fRectWrites, fTextWrites;
154#endif
155
156#ifdef SK_DEBUG_VALIDATE
157public:
158 void validate() const;
159private:
160 void validateBitmaps() const;
161 void validateMatrices() const;
162 void validatePaints() const;
163 void validatePaths() const;
164 void validateRegions() const;
165#else
166public:
167 void validate() const {}
168#endif
169
rileya@google.com9f5898d2012-09-11 20:21:44 +0000170protected:
171
172 // These are set to NULL in our constructor, but may be changed by
173 // subclasses, in which case they will be SkSafeUnref'd in our destructor.
174 SkBBoxHierarchy* fBoundingHierarchy;
175 SkPictureStateTree* fStateTree;
176
robertphillips@google.com801cee12012-10-19 19:06:11 +0000177 // Allocated in the constructor and managed by this class.
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000178 SkBitmapHeap* fBitmapHeap;
robertphillips@google.com801cee12012-10-19 19:06:11 +0000179
180private:
djsollen@google.com21830d92012-08-07 19:49:41 +0000181 SkChunkFlatController fFlattenableHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000182
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000183 SkMatrixDictionary fMatrices;
184 SkPaintDictionary fPaints;
185 SkRegionDictionary fRegions;
186
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 SkPathHeap* fPathHeap; // reference counted
188 SkWriter32 fWriter;
189
reed@android.com09b84a02009-06-26 20:22:26 +0000190 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 SkTDArray<SkPicture*> fPictureRefs;
192
reed@android.comae814c82009-02-13 14:56:09 +0000193 uint32_t fRecordFlags;
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000194 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000195
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000197 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198
199 typedef SkCanvas INHERITED;
200};
201
202#endif