blob: abaa22dd9d7feda0b63d15169044e5799ec71b7e [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;
reed@google.com4ed0fb72012-12-12 20:48:18 +000039 virtual bool clipRRect(const SkRRect&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com071eef92011-10-12 11:52:53 +000040 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000041 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
42 virtual void clear(SkColor) SK_OVERRIDE;
43 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000045 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000046 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
47 virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
48 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000049 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000051 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +000052 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
53 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000055 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000056 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000057 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000059 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000060 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000061 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000062 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000063 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 virtual void drawPosTextH(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000065 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000066 virtual void drawTextOnPath(const void* text, size_t byteLength,
67 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +000068 const SkPaint&) SK_OVERRIDE;
69 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 virtual void drawVertices(VertexMode, int vertexCount,
71 const SkPoint vertices[], const SkPoint texs[],
72 const SkColor colors[], SkXfermode*,
73 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +000074 const SkPaint&) SK_OVERRIDE;
75 virtual void drawData(const void*, size_t) SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000076 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000077
reed@google.com45954262012-12-07 17:14:40 +000078 void addFontMetricsTopBottom(const SkPaint& paint, int paintIndex,
79 SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000080
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 const SkTDArray<SkPicture* >& getPictureRefs() const {
82 return fPictureRefs;
83 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000084
junov@chromium.org4866cc02012-06-01 21:23:07 +000085 void setFlags(uint32_t recordFlags) {
86 fRecordFlags = recordFlags;
87 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
89 const SkWriter32& writeStream() const {
90 return fWriter;
91 }
92
reed@google.comd86e7ab2012-09-27 20:31:31 +000093 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +000094 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096private:
junov@chromium.orge3dbedb2012-07-09 16:03:55 +000097 void recordRestoreOffsetPlaceholder(SkRegion::Op);
98 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(
99 uint32_t restoreOffset);
100
reed@google.comffacd3c2012-08-30 15:31:23 +0000101 SkTDArray<int32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000102 int fFirstSavedLayerIndex;
103 enum {
104 kNoSavedLayerIndex = -1
105 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106
107 void addDraw(DrawType drawType) {
reed@google.com97af1a62012-08-28 12:19:02 +0000108 this->predrawNotify();
109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110#ifdef SK_DEBUG_TRACE
111 SkDebugf("add %s\n", DrawTypeToString(drawType));
112#endif
113 fWriter.writeInt(drawType);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000114 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 void addInt(int value) {
116 fWriter.writeInt(value);
117 }
118 void addScalar(SkScalar scalar) {
119 fWriter.writeScalar(scalar);
120 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 void addBitmap(const SkBitmap& bitmap);
123 void addMatrix(const SkMatrix& matrix);
124 void addMatrixPtr(const SkMatrix* matrix);
reed@google.com45954262012-12-07 17:14:40 +0000125 int addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
126 int addPaintPtr(const SkPaint* paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 void addPath(const SkPath& path);
128 void addPicture(SkPicture& picture);
129 void addPoint(const SkPoint& point);
130 void addPoints(const SkPoint pts[], int count);
131 void addRect(const SkRect& rect);
132 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000133 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 void addIRectPtr(const SkIRect* rect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000135 void addRRect(const SkRRect&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 void addRegion(const SkRegion& region);
137 void addText(const void* text, size_t byteLength);
138
junov@chromium.org4866cc02012-06-01 21:23:07 +0000139 int find(const SkBitmap& bitmap);
140
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141#ifdef SK_DEBUG_DUMP
142public:
143 void dumpMatrices();
144 void dumpPaints();
145#endif
146
147#ifdef SK_DEBUG_SIZE
148public:
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000149 size_t size() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 int bitmaps(size_t* size) const;
151 int matrices(size_t* size) const;
152 int paints(size_t* size) const;
153 int paths(size_t* size) const;
154 int regions(size_t* size) const;
155 size_t streamlen() const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000156
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 size_t fPointBytes, fRectBytes, fTextBytes;
158 int fPointWrites, fRectWrites, fTextWrites;
159#endif
160
161#ifdef SK_DEBUG_VALIDATE
162public:
163 void validate() const;
164private:
165 void validateBitmaps() const;
166 void validateMatrices() const;
167 void validatePaints() const;
168 void validatePaths() const;
169 void validateRegions() const;
170#else
171public:
172 void validate() const {}
173#endif
174
rileya@google.com9f5898d2012-09-11 20:21:44 +0000175protected:
176
177 // These are set to NULL in our constructor, but may be changed by
178 // subclasses, in which case they will be SkSafeUnref'd in our destructor.
179 SkBBoxHierarchy* fBoundingHierarchy;
180 SkPictureStateTree* fStateTree;
181
robertphillips@google.com801cee12012-10-19 19:06:11 +0000182 // Allocated in the constructor and managed by this class.
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000183 SkBitmapHeap* fBitmapHeap;
robertphillips@google.com801cee12012-10-19 19:06:11 +0000184
185private:
djsollen@google.com21830d92012-08-07 19:49:41 +0000186 SkChunkFlatController fFlattenableHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000187
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000188 SkMatrixDictionary fMatrices;
189 SkPaintDictionary fPaints;
190 SkRegionDictionary fRegions;
191
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 SkPathHeap* fPathHeap; // reference counted
193 SkWriter32 fWriter;
194
reed@android.com09b84a02009-06-26 20:22:26 +0000195 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 SkTDArray<SkPicture*> fPictureRefs;
197
reed@android.comae814c82009-02-13 14:56:09 +0000198 uint32_t fRecordFlags;
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000199 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000200
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000202 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203
204 typedef SkCanvas INHERITED;
205};
206
207#endif