SkRecorder.h revision 1bdfd3f4f09e47364f76d3f08177b1ce844ac786
1/*
2 * Copyright 2014 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 */
7
8#ifndef SkRecorder_DEFINED
9#define SkRecorder_DEFINED
10
11#include "SkCanvas.h"
12#include "SkRecord.h"
13#include "SkRecords.h"
14#include "SkTDArray.h"
15
16class SkBBHFactory;
17
18class SkCanvasDrawableList : SkNoncopyable {
19public:
20    ~SkCanvasDrawableList();
21
22    int count() const { return fArray.count(); }
23    SkCanvasDrawable* const* begin() const { return fArray.begin(); }
24
25    void append(SkCanvasDrawable* drawable);
26
27    // Return a new or ref'd array of pictures that were snapped from our drawables.
28    SkPicture::SnapshotArray* newDrawableSnapshot();
29
30private:
31    SkTDArray<SkCanvasDrawable*> fArray;
32};
33
34// SkRecorder provides an SkCanvas interface for recording into an SkRecord.
35
36class SkRecorder : public SkCanvas {
37public:
38    // Does not take ownership of the SkRecord.
39    SkRecorder(SkRecord*, int width, int height);   // legacy version
40    SkRecorder(SkRecord*, const SkRect& bounds);
41
42    SkCanvasDrawableList* getDrawableList() const { return fDrawableList.get(); }
43    SkCanvasDrawableList* detachDrawableList() { return fDrawableList.detach(); }
44
45    // Make SkRecorder forget entirely about its SkRecord*; all calls to SkRecorder will fail.
46    void forgetRecord();
47
48    void clear(SkColor) SK_OVERRIDE;
49    void drawPaint(const SkPaint& paint) SK_OVERRIDE;
50    void drawPoints(PointMode mode,
51                    size_t count,
52                    const SkPoint pts[],
53                    const SkPaint& paint) SK_OVERRIDE;
54    void drawRect(const SkRect& rect, const SkPaint& paint) SK_OVERRIDE;
55    void drawOval(const SkRect& oval, const SkPaint&) SK_OVERRIDE;
56    void drawRRect(const SkRRect& rrect, const SkPaint& paint) SK_OVERRIDE;
57    void drawPath(const SkPath& path, const SkPaint& paint) SK_OVERRIDE;
58    void drawBitmap(const SkBitmap& bitmap,
59                    SkScalar left,
60                    SkScalar top,
61                    const SkPaint* paint = NULL) SK_OVERRIDE;
62    void drawBitmapRectToRect(const SkBitmap& bitmap,
63                              const SkRect* src,
64                              const SkRect& dst,
65                              const SkPaint* paint = NULL,
66                              DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag) SK_OVERRIDE;
67    void drawBitmapMatrix(const SkBitmap& bitmap,
68                          const SkMatrix& m,
69                          const SkPaint* paint = NULL) SK_OVERRIDE;
70    void drawBitmapNine(const SkBitmap& bitmap,
71                        const SkIRect& center,
72                        const SkRect& dst,
73                        const SkPaint* paint = NULL) SK_OVERRIDE;
74    virtual void drawImage(const SkImage* image, SkScalar left, SkScalar top,
75                           const SkPaint* paint = NULL) SK_OVERRIDE;
76    virtual void drawImageRect(const SkImage* image, const SkRect* src,
77                               const SkRect& dst,
78                               const SkPaint* paint = NULL) SK_OVERRIDE;
79    void drawSprite(const SkBitmap& bitmap,
80                    int left,
81                    int top,
82                    const SkPaint* paint = NULL) SK_OVERRIDE;
83    void drawVertices(VertexMode vmode,
84                      int vertexCount,
85                      const SkPoint vertices[],
86                      const SkPoint texs[],
87                      const SkColor colors[],
88                      SkXfermode* xmode,
89                      const uint16_t indices[],
90                      int indexCount,
91                      const SkPaint& paint) SK_OVERRIDE;
92
93    void willSave() SK_OVERRIDE;
94    SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SkCanvas::SaveFlags) SK_OVERRIDE;
95    void willRestore() SK_OVERRIDE {}
96    void didRestore() SK_OVERRIDE;
97
98    void didConcat(const SkMatrix&) SK_OVERRIDE;
99    void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
100
101    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
102    void onDrawDrawable(SkCanvasDrawable*) SK_OVERRIDE;
103    void onDrawText(const void* text,
104                    size_t byteLength,
105                    SkScalar x,
106                    SkScalar y,
107                    const SkPaint& paint) SK_OVERRIDE;
108    void onDrawPosText(const void* text,
109                       size_t byteLength,
110                       const SkPoint pos[],
111                       const SkPaint& paint) SK_OVERRIDE;
112    void onDrawPosTextH(const void* text,
113                        size_t byteLength,
114                        const SkScalar xpos[],
115                        SkScalar constY,
116                        const SkPaint& paint) SK_OVERRIDE;
117    void onDrawTextOnPath(const void* text,
118                          size_t byteLength,
119                          const SkPath& path,
120                          const SkMatrix* matrix,
121                          const SkPaint& paint) SK_OVERRIDE;
122    void onDrawTextBlob(const SkTextBlob* blob,
123                        SkScalar x,
124                        SkScalar y,
125                        const SkPaint& paint) SK_OVERRIDE;
126    void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
127                     const SkPoint texCoords[4], SkXfermode* xmode,
128                     const SkPaint& paint) SK_OVERRIDE;
129
130    void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) SK_OVERRIDE;
131    void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) SK_OVERRIDE;
132    void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) SK_OVERRIDE;
133    void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE;
134
135    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
136
137    void onPushCull(const SkRect& cullRect) SK_OVERRIDE;
138    void onPopCull() SK_OVERRIDE;
139
140    void beginCommentGroup(const char*) SK_OVERRIDE;
141    void addComment(const char*, const char*) SK_OVERRIDE;
142    void endCommentGroup() SK_OVERRIDE;
143    void drawData(const void*, size_t) SK_OVERRIDE;
144
145    bool isDrawingToLayer() const SK_OVERRIDE;
146    SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) SK_OVERRIDE { return NULL; }
147
148private:
149    template <typename T>
150    T* copy(const T*);
151
152    template <typename T>
153    T* copy(const T[], size_t count);
154
155    SkIRect devBounds() const {
156        SkIRect devBounds;
157        this->getClipDeviceBounds(&devBounds);
158        return devBounds;
159    }
160
161    SkRecord* fRecord;
162
163    int fSaveLayerCount;
164    SkTDArray<SkBool8> fSaveIsSaveLayer;
165    SkAutoTDelete<SkCanvasDrawableList> fDrawableList;
166};
167
168#endif//SkRecorder_DEFINED
169