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 "SkBigPicture.h"
12#include "SkMiniRecorder.h"
13#include "SkNoDrawCanvas.h"
14#include "SkRecord.h"
15#include "SkRecords.h"
16#include "SkTDArray.h"
17
18class SkBBHFactory;
19
20class SkDrawableList : SkNoncopyable {
21public:
22    SkDrawableList() {}
23    ~SkDrawableList();
24
25    int count() const { return fArray.count(); }
26    SkDrawable* const* begin() const { return fArray.begin(); }
27
28    void append(SkDrawable* drawable);
29
30    // Return a new or ref'd array of pictures that were snapped from our drawables.
31    SkBigPicture::SnapshotArray* newDrawableSnapshot();
32
33private:
34    SkTDArray<SkDrawable*> fArray;
35};
36
37// SkRecorder provides an SkCanvas interface for recording into an SkRecord.
38
39class SkRecorder final : public SkNoDrawCanvas {
40public:
41    // Does not take ownership of the SkRecord.
42    SkRecorder(SkRecord*, int width, int height, SkMiniRecorder* = nullptr);   // legacy version
43    SkRecorder(SkRecord*, const SkRect& bounds, SkMiniRecorder* = nullptr);
44
45    enum DrawPictureMode { Record_DrawPictureMode, Playback_DrawPictureMode };
46    void reset(SkRecord*, const SkRect& bounds, DrawPictureMode, SkMiniRecorder* = nullptr);
47
48    size_t approxBytesUsedBySubPictures() const { return fApproxBytesUsedBySubPictures; }
49
50    SkDrawableList* getDrawableList() const { return fDrawableList.get(); }
51    std::unique_ptr<SkDrawableList> detachDrawableList() { return std::move(fDrawableList); }
52
53    // Make SkRecorder forget entirely about its SkRecord*; all calls to SkRecorder will fail.
54    void forgetRecord();
55
56    void willSave() override;
57    SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
58    void willRestore() override {}
59    void didRestore() override;
60
61    void didConcat(const SkMatrix&) override;
62    void didSetMatrix(const SkMatrix&) override;
63    void didTranslate(SkScalar, SkScalar) override;
64
65#ifdef SK_EXPERIMENTAL_SHADOWING
66    void didTranslateZ(SkScalar) override;
67#else
68    void didTranslateZ(SkScalar);
69#endif
70
71    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
72    void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
73    void onDrawText(const void* text,
74                    size_t byteLength,
75                    SkScalar x,
76                    SkScalar y,
77                    const SkPaint& paint) override;
78    void onDrawPosText(const void* text,
79                       size_t byteLength,
80                       const SkPoint pos[],
81                       const SkPaint& paint) override;
82    void onDrawPosTextH(const void* text,
83                        size_t byteLength,
84                        const SkScalar xpos[],
85                        SkScalar constY,
86                        const SkPaint& paint) override;
87    void onDrawTextOnPath(const void* text,
88                          size_t byteLength,
89                          const SkPath& path,
90                          const SkMatrix* matrix,
91                          const SkPaint& paint) override;
92    void onDrawTextRSXform(const void* text,
93                           size_t byteLength,
94                           const SkRSXform[],
95                           const SkRect* cull,
96                           const SkPaint& paint) override;
97    void onDrawTextBlob(const SkTextBlob* blob,
98                        SkScalar x,
99                        SkScalar y,
100                        const SkPaint& paint) override;
101    void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
102                     const SkPoint texCoords[4], SkBlendMode,
103                     const SkPaint& paint) override;
104
105    void onDrawPaint(const SkPaint&) override;
106    void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
107    void onDrawRect(const SkRect&, const SkPaint&) override;
108    void onDrawRegion(const SkRegion&, const SkPaint&) override;
109    void onDrawOval(const SkRect&, const SkPaint&) override;
110    void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
111    void onDrawRRect(const SkRRect&, const SkPaint&) override;
112    void onDrawPath(const SkPath&, const SkPaint&) override;
113    void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
114    void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
115                          SrcRectConstraint) override;
116    void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
117    void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
118                         const SkPaint*, SrcRectConstraint) override;
119    void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
120                         const SkPaint*) override;
121    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
122                          const SkPaint*) override;
123    void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst,
124                            const SkPaint*) override;
125    void onDrawBitmapLattice(const SkBitmap&, const Lattice& lattice, const SkRect& dst,
126                             const SkPaint*) override;
127    void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
128    void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
129                     int count, SkBlendMode, const SkRect* cull, const SkPaint*) override;
130
131    void onClipRect(const SkRect& rect, SkClipOp, ClipEdgeStyle) override;
132    void onClipRRect(const SkRRect& rrect, SkClipOp, ClipEdgeStyle) override;
133    void onClipPath(const SkPath& path, SkClipOp, ClipEdgeStyle) override;
134    void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override;
135
136    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
137
138#ifdef SK_EXPERIMENTAL_SHADOWING
139    void onDrawShadowedPicture(const SkPicture*,
140                               const SkMatrix*,
141                               const SkPaint*,
142                               const SkShadowParams& params) override;
143#else
144    void onDrawShadowedPicture(const SkPicture*,
145                               const SkMatrix*,
146                               const SkPaint*,
147                               const SkShadowParams& params);
148#endif
149
150    void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
151
152    sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
153
154    void flushMiniRecorder();
155
156private:
157    template <typename T>
158    T* copy(const T*);
159
160    template <typename T>
161    T* copy(const T[], size_t count);
162
163    DrawPictureMode fDrawPictureMode;
164    size_t fApproxBytesUsedBySubPictures;
165    SkRecord* fRecord;
166    std::unique_ptr<SkDrawableList> fDrawableList;
167
168    SkMiniRecorder* fMiniRecorder;
169};
170
171#endif//SkRecorder_DEFINED
172