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