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