SkRecorder.h revision c54d8db4d169ea5f2af2a9a2349df007bd428475
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 drawPaint(const SkPaint& paint) SK_OVERRIDE;
49    void drawPoints(PointMode mode,
50                    size_t count,
51                    const SkPoint pts[],
52                    const SkPaint& paint) SK_OVERRIDE;
53    void drawRect(const SkRect& rect, const SkPaint& paint) SK_OVERRIDE;
54    void drawOval(const SkRect& oval, const SkPaint&) SK_OVERRIDE;
55    void drawRRect(const SkRRect& rrect, const SkPaint& paint) SK_OVERRIDE;
56    void drawPath(const SkPath& path, const SkPaint& paint) SK_OVERRIDE;
57    void drawBitmap(const SkBitmap& bitmap,
58                    SkScalar left,
59                    SkScalar top,
60                    const SkPaint* paint = NULL) SK_OVERRIDE;
61    void drawBitmapRectToRect(const SkBitmap& bitmap,
62                              const SkRect* src,
63                              const SkRect& dst,
64                              const SkPaint* paint = NULL,
65                              DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag) SK_OVERRIDE;
66    void drawBitmapNine(const SkBitmap& bitmap,
67                        const SkIRect& center,
68                        const SkRect& dst,
69                        const SkPaint* paint = NULL) SK_OVERRIDE;
70    virtual void drawImage(const SkImage* image, SkScalar left, SkScalar top,
71                           const SkPaint* paint = NULL) SK_OVERRIDE;
72    virtual void drawImageRect(const SkImage* image, const SkRect* src,
73                               const SkRect& dst,
74                               const SkPaint* paint = NULL) SK_OVERRIDE;
75    void drawSprite(const SkBitmap& bitmap,
76                    int left,
77                    int top,
78                    const SkPaint* paint = NULL) SK_OVERRIDE;
79    void drawVertices(VertexMode vmode,
80                      int vertexCount,
81                      const SkPoint vertices[],
82                      const SkPoint texs[],
83                      const SkColor colors[],
84                      SkXfermode* xmode,
85                      const uint16_t indices[],
86                      int indexCount,
87                      const SkPaint& paint) SK_OVERRIDE;
88
89    void willSave() SK_OVERRIDE;
90    SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SkCanvas::SaveFlags) SK_OVERRIDE;
91    void willRestore() SK_OVERRIDE {}
92    void didRestore() SK_OVERRIDE;
93
94    void didConcat(const SkMatrix&) SK_OVERRIDE;
95    void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
96
97    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
98    void onDrawDrawable(SkCanvasDrawable*) SK_OVERRIDE;
99    void onDrawText(const void* text,
100                    size_t byteLength,
101                    SkScalar x,
102                    SkScalar y,
103                    const SkPaint& paint) SK_OVERRIDE;
104    void onDrawPosText(const void* text,
105                       size_t byteLength,
106                       const SkPoint pos[],
107                       const SkPaint& paint) SK_OVERRIDE;
108    void onDrawPosTextH(const void* text,
109                        size_t byteLength,
110                        const SkScalar xpos[],
111                        SkScalar constY,
112                        const SkPaint& paint) SK_OVERRIDE;
113    void onDrawTextOnPath(const void* text,
114                          size_t byteLength,
115                          const SkPath& path,
116                          const SkMatrix* matrix,
117                          const SkPaint& paint) SK_OVERRIDE;
118    void onDrawTextBlob(const SkTextBlob* blob,
119                        SkScalar x,
120                        SkScalar y,
121                        const SkPaint& paint) SK_OVERRIDE;
122    void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
123                     const SkPoint texCoords[4], SkXfermode* xmode,
124                     const SkPaint& paint) SK_OVERRIDE;
125
126    void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) SK_OVERRIDE;
127    void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) SK_OVERRIDE;
128    void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) SK_OVERRIDE;
129    void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE;
130
131    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
132
133    void onPushCull(const SkRect& cullRect) SK_OVERRIDE;
134    void onPopCull() SK_OVERRIDE;
135
136    void beginCommentGroup(const char*) SK_OVERRIDE;
137    void addComment(const char*, const char*) SK_OVERRIDE;
138    void endCommentGroup() SK_OVERRIDE;
139    void drawData(const void*, size_t) SK_OVERRIDE;
140
141    bool isDrawingToLayer() const SK_OVERRIDE;
142    SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) SK_OVERRIDE { return NULL; }
143
144private:
145    template <typename T>
146    T* copy(const T*);
147
148    template <typename T>
149    T* copy(const T[], size_t count);
150
151    SkIRect devBounds() const {
152        SkIRect devBounds;
153        this->getClipDeviceBounds(&devBounds);
154        return devBounds;
155    }
156
157    SkRecord* fRecord;
158
159    int fSaveLayerCount;
160    SkTDArray<SkBool8> fSaveIsSaveLayer;
161    SkAutoTDelete<SkCanvasDrawableList> fDrawableList;
162};
163
164#endif//SkRecorder_DEFINED
165