SkRecorder.h revision f920e468ac66a36c9653d1b11181480295044c7d
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 SkDrawableList : SkNoncopyable {
19public:
20    ~SkDrawableList();
21
22    int count() const { return fArray.count(); }
23    SkDrawable* const* begin() const { return fArray.begin(); }
24
25    void append(SkDrawable* 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<SkDrawable*> 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    void reset(SkRecord*, const SkRect& bounds);
43
44    SkDrawableList* getDrawableList() const { return fDrawableList.get(); }
45    SkDrawableList* detachDrawableList() { return fDrawableList.detach(); }
46
47    // Make SkRecorder forget entirely about its SkRecord*; all calls to SkRecorder will fail.
48    void forgetRecord();
49
50    void willSave() override;
51    SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SkCanvas::SaveFlags) override;
52    void willRestore() override {}
53    void didRestore() override;
54
55    void didConcat(const SkMatrix&) override;
56    void didSetMatrix(const SkMatrix&) override;
57
58    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
59    void onDrawDrawable(SkDrawable*) override;
60    void onDrawText(const void* text,
61                    size_t byteLength,
62                    SkScalar x,
63                    SkScalar y,
64                    const SkPaint& paint) override;
65    void onDrawPosText(const void* text,
66                       size_t byteLength,
67                       const SkPoint pos[],
68                       const SkPaint& paint) override;
69    void onDrawPosTextH(const void* text,
70                        size_t byteLength,
71                        const SkScalar xpos[],
72                        SkScalar constY,
73                        const SkPaint& paint) override;
74    void onDrawTextOnPath(const void* text,
75                          size_t byteLength,
76                          const SkPath& path,
77                          const SkMatrix* matrix,
78                          const SkPaint& paint) override;
79    void onDrawTextBlob(const SkTextBlob* blob,
80                        SkScalar x,
81                        SkScalar y,
82                        const SkPaint& paint) override;
83    void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
84                     const SkPoint texCoords[4], SkXfermode* xmode,
85                     const SkPaint& paint) override;
86
87    void onDrawPaint(const SkPaint&) override;
88    void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
89    void onDrawRect(const SkRect&, const SkPaint&) override;
90    void onDrawOval(const SkRect&, const SkPaint&) override;
91    void onDrawRRect(const SkRRect&, const SkPaint&) override;
92    void onDrawPath(const SkPath&, const SkPaint&) override;
93    void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
94    void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
95                          DrawBitmapRectFlags flags) override;
96    void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
97    void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
98                         const SkPaint*) override;
99    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
100                          const SkPaint*) override;
101    void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override;
102    void onDrawVertices(VertexMode vmode, int vertexCount,
103                        const SkPoint vertices[], const SkPoint texs[],
104                        const SkColor colors[], SkXfermode* xmode,
105                        const uint16_t indices[], int indexCount,
106                        const SkPaint&) override;
107
108    void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) override;
109    void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) override;
110    void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) override;
111    void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) override;
112
113    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
114
115    void beginCommentGroup(const char*) override;
116    void addComment(const char*, const char*) override;
117    void endCommentGroup() override;
118
119    SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override { return NULL; }
120
121private:
122    template <typename T>
123    T* copy(const T*);
124
125    template <typename T>
126    T* copy(const T[], size_t count);
127
128    SkIRect devBounds() const {
129        SkIRect devBounds;
130        this->getClipDeviceBounds(&devBounds);
131        return devBounds;
132    }
133
134    SkRecord* fRecord;
135
136    SkAutoTDelete<SkDrawableList> fDrawableList;
137};
138
139#endif//SkRecorder_DEFINED
140