blob: 6d14c621aafb510dd5e13a6f01fa50f866c19992 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.com76f10a32014-02-05 15:32:21 +00007
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
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000022// These macros help with packing and unpacking a single byte value and
23// a 3 byte value into/out of a uint32_t
24#define MASK_24 0x00FFFFFF
25#define UNPACK_8_24(combined, small, large) \
26 small = (combined >> 24) & 0xFF; \
27 large = combined & MASK_24;
28#define PACK_8_24(small, large) ((small << 24) | large)
29
30
reed@android.com8a1c16f2008-12-17 15:59:43 +000031class SkPictureRecord : public SkCanvas {
32public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000033 SkPictureRecord(uint32_t recordFlags, SkBaseDevice*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 virtual ~SkPictureRecord();
35
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000036 virtual SkBaseDevice* setDevice(SkBaseDevice* device) SK_OVERRIDE;
junov@chromium.org4e6dfa52012-07-16 14:04:59 +000037
reed@google.com2d4297c2011-10-06 13:14:12 +000038 virtual int save(SaveFlags) SK_OVERRIDE;
39 virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
40 virtual void restore() SK_OVERRIDE;
41 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
42 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
43 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
44 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
45 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
46 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com071eef92011-10-12 11:52:53 +000047 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000048 virtual bool clipRRect(const SkRRect&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com071eef92011-10-12 11:52:53 +000049 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000050 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
51 virtual void clear(SkColor) SK_OVERRIDE;
52 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000054 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000055 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000056 virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000057 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000058 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000060 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +000061 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +000062 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +000063 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000065 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000066 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000067 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000069 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000070 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000071 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000072 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000073 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 virtual void drawPosTextH(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000075 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000076 virtual void drawTextOnPath(const void* text, size_t byteLength,
77 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +000078 const SkPaint&) SK_OVERRIDE;
79 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 virtual void drawVertices(VertexMode, int vertexCount,
81 const SkPoint vertices[], const SkPoint texs[],
82 const SkColor colors[], SkXfermode*,
83 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +000084 const SkPaint&) SK_OVERRIDE;
85 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +000086 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
87 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
88 virtual void endCommentGroup() SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000089 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
junov@chromium.org3f5ecd62013-01-22 18:01:26 +000091 void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&,
reed@google.com45954262012-12-07 17:14:40 +000092 SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 const SkTDArray<SkPicture* >& getPictureRefs() const {
95 return fPictureRefs;
96 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000097
junov@chromium.org4866cc02012-06-01 21:23:07 +000098 void setFlags(uint32_t recordFlags) {
99 fRecordFlags = recordFlags;
100 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101
102 const SkWriter32& writeStream() const {
103 return fWriter;
104 }
105
reed@google.comd86e7ab2012-09-27 20:31:31 +0000106 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000107 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +0000108
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +0000110 void handleOptimization(int opt);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000111 int recordRestoreOffsetPlaceholder(SkRegion::Op);
112 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +0000113
reed@google.comffacd3c2012-08-30 15:31:23 +0000114 SkTDArray<int32_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
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000120 /*
121 * Write the 'drawType' operation and chunk size to the skp. 'size'
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000122 * can potentially be increased if the chunk size needs its own storage
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000123 * location (i.e., it overflows 24 bits).
124 * Returns the start offset of the chunk. This is the location at which
125 * the opcode & size are stored.
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000126 * TODO: since we are handing the size into here we could call reserve
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000127 * and then return a pointer to the memory storage. This could decrease
128 * allocation overhead but could lead to more wasted space (the tail
129 * end of blocks could go unused). Possibly add a second addDraw that
130 * operates in this manner.
131 */
robertphillips@google.com8b169312013-10-15 17:47:36 +0000132 size_t addDraw(DrawType drawType, uint32_t* size) {
reed@google.com44699382013-10-31 17:28:30 +0000133 size_t offset = fWriter.bytesWritten();
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000134
reed@google.com97af1a62012-08-28 12:19:02 +0000135 this->predrawNotify();
136
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000137 #ifdef SK_DEBUG_TRACE
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 SkDebugf("add %s\n", DrawTypeToString(drawType));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000139 #endif
140
141 SkASSERT(0 != *size);
142 SkASSERT(((uint8_t) drawType) == drawType);
143
144 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
145 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
146 *size += 1;
147 fWriter.writeInt(*size);
148 } else {
149 fWriter.writeInt(PACK_8_24(drawType, *size));
150 }
151
152 return offset;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000153 }
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000154
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 void addInt(int value) {
156 fWriter.writeInt(value);
157 }
158 void addScalar(SkScalar scalar) {
159 fWriter.writeScalar(scalar);
160 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000161
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 void addBitmap(const SkBitmap& bitmap);
163 void addMatrix(const SkMatrix& matrix);
junov@chromium.orgf3b12232013-01-22 17:50:47 +0000164 const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
165 const SkFlatData* addPaintPtr(const SkPaint* paint);
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000166 void addFlatPaint(const SkFlatData* flatPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 void addPath(const SkPath& path);
168 void addPicture(SkPicture& picture);
169 void addPoint(const SkPoint& point);
170 void addPoints(const SkPoint pts[], int count);
171 void addRect(const SkRect& rect);
172 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000173 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 void addIRectPtr(const SkIRect* rect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000175 void addRRect(const SkRRect&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 void addRegion(const SkRegion& region);
177 void addText(const void* text, size_t byteLength);
178
junov@chromium.org4866cc02012-06-01 21:23:07 +0000179 int find(const SkBitmap& bitmap);
180
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181#ifdef SK_DEBUG_DUMP
182public:
183 void dumpMatrices();
184 void dumpPaints();
185#endif
186
187#ifdef SK_DEBUG_SIZE
188public:
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000189 size_t size() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 int bitmaps(size_t* size) const;
191 int matrices(size_t* size) const;
192 int paints(size_t* size) const;
193 int paths(size_t* size) const;
194 int regions(size_t* size) const;
195 size_t streamlen() const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000196
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 size_t fPointBytes, fRectBytes, fTextBytes;
198 int fPointWrites, fRectWrites, fTextWrites;
199#endif
200
201#ifdef SK_DEBUG_VALIDATE
202public:
robertphillips@google.com8b169312013-10-15 17:47:36 +0000203 void validate(size_t initialOffset, uint32_t size) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204private:
205 void validateBitmaps() const;
206 void validateMatrices() const;
207 void validatePaints() const;
208 void validatePaths() const;
209 void validateRegions() const;
210#else
211public:
robertphillips@google.com8b169312013-10-15 17:47:36 +0000212 void validate(size_t initialOffset, uint32_t size) const {
reed@google.com44699382013-10-31 17:28:30 +0000213 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000214 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215#endif
216
rileya@google.com9f5898d2012-09-11 20:21:44 +0000217protected:
reed@google.com76f10a32014-02-05 15:32:21 +0000218 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE;
219
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000220 // Return fontmetrics.fTop,fBottom in topbot[0,1], after they have been
221 // tweaked by paint.computeFastBounds().
222 static void ComputeFontMetricsTopBottom(const SkPaint& paint, SkScalar topbot[2]);
223
224 // Make sure that flat has fTopBot written.
225 static void WriteTopBot(const SkPaint& paint, const SkFlatData& flat) {
226 if (!flat.isTopBotWritten()) {
227 ComputeFontMetricsTopBottom(paint, flat.writableTopBot());
228 SkASSERT(flat.isTopBotWritten());
229 }
230 }
231 // Will return a cached version when possible.
232 const SkFlatData* getFlatPaintData(const SkPaint& paint);
233 /**
234 * SkBBoxRecord::drawPosTextH gets a flat paint and uses it,
235 * then it calls this, using the extra parameter, to avoid duplication.
236 */
237 void drawPosTextHImpl(const void* text, size_t byteLength,
238 const SkScalar xpos[], SkScalar constY,
239 const SkPaint& paint, const SkFlatData* flatPaintData);
rileya@google.com9f5898d2012-09-11 20:21:44 +0000240
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000241 int addPathToHeap(const SkPath& path); // does not write to ops stream
242
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000243 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000244 // restores to be deferred (e.g., if the MC state is being collapsed and
245 // only written out as needed).
246 void recordConcat(const SkMatrix& matrix);
247 int recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
248 int recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
249 int recordClipPath(int pathID, SkRegion::Op op, bool doAA);
250 int recordClipRegion(const SkRegion& region, SkRegion::Op op);
251 void recordSave(SaveFlags flags);
252 void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
253 void recordRestore();
254
rileya@google.com9f5898d2012-09-11 20:21:44 +0000255 // These are set to NULL in our constructor, but may be changed by
256 // subclasses, in which case they will be SkSafeUnref'd in our destructor.
257 SkBBoxHierarchy* fBoundingHierarchy;
258 SkPictureStateTree* fStateTree;
259
robertphillips@google.com801cee12012-10-19 19:06:11 +0000260 // Allocated in the constructor and managed by this class.
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000261 SkBitmapHeap* fBitmapHeap;
robertphillips@google.com801cee12012-10-19 19:06:11 +0000262
263private:
djsollen@google.com21830d92012-08-07 19:49:41 +0000264 SkChunkFlatController fFlattenableHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000265
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000266 SkPaintDictionary fPaints;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000267
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268 SkPathHeap* fPathHeap; // reference counted
269 SkWriter32 fWriter;
270
reed@android.com09b84a02009-06-26 20:22:26 +0000271 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272 SkTDArray<SkPicture*> fPictureRefs;
273
reed@android.comae814c82009-02-13 14:56:09 +0000274 uint32_t fRecordFlags;
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000275 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000276
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000278 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279
280 typedef SkCanvas INHERITED;
281};
282
283#endif