SkRecorder.h revision e5de130788c8637d2f7df9ddb0241b78e04d5882
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 "SkCanvas.h"
13#include "SkMiniRecorder.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 : public SkCanvas {
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    SkDrawableList* detachDrawableList() { return fDrawableList.release(); }
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 didTranslateZ(SkScalar) override;
64
65    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
66    void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
67    void onDrawText(const void* text,
68                    size_t byteLength,
69                    SkScalar x,
70                    SkScalar y,
71                    const SkPaint& paint) override;
72    void onDrawPosText(const void* text,
73                       size_t byteLength,
74                       const SkPoint pos[],
75                       const SkPaint& paint) override;
76    void onDrawPosTextH(const void* text,
77                        size_t byteLength,
78                        const SkScalar xpos[],
79                        SkScalar constY,
80                        const SkPaint& paint) override;
81    void onDrawTextOnPath(const void* text,
82                          size_t byteLength,
83                          const SkPath& path,
84                          const SkMatrix* matrix,
85                          const SkPaint& paint) override;
86    void onDrawTextRSXform(const void* text,
87                           size_t byteLength,
88                           const SkRSXform[],
89                           const SkRect* cull,
90                           const SkPaint& paint) override;
91    void onDrawTextBlob(const SkTextBlob* blob,
92                        SkScalar x,
93                        SkScalar y,
94                        const SkPaint& paint) override;
95    void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
96                     const SkPoint texCoords[4], SkXfermode* xmode,
97                     const SkPaint& paint) override;
98
99    void onDrawPaint(const SkPaint&) override;
100    void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
101    void onDrawRect(const SkRect&, const SkPaint&) override;
102    void onDrawOval(const SkRect&, const SkPaint&) override;
103    void onDrawRRect(const SkRRect&, const SkPaint&) override;
104    void onDrawPath(const SkPath&, const SkPaint&) override;
105    void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
106    void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
107                          SrcRectConstraint) override;
108    void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
109    void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
110                         const SkPaint*, SrcRectConstraint) override;
111    void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
112                         const SkPaint*) override;
113    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
114                          const SkPaint*) override;
115    void onDrawVertices(VertexMode vmode, int vertexCount,
116                        const SkPoint vertices[], const SkPoint texs[],
117                        const SkColor colors[], SkXfermode* xmode,
118                        const uint16_t indices[], int indexCount,
119                        const SkPaint&) override;
120    void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
121                     int count, SkXfermode::Mode, const SkRect* cull, const SkPaint*) override;
122
123    void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) override;
124    void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) override;
125    void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) override;
126    void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) override;
127
128    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
129    void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
130
131    sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
132
133    void flushMiniRecorder();
134
135private:
136    template <typename T>
137    T* copy(const T*);
138
139    template <typename T>
140    T* copy(const T[], size_t count);
141
142    SkIRect devBounds() const {
143        SkIRect devBounds;
144        this->getClipDeviceBounds(&devBounds);
145        return devBounds;
146    }
147
148    DrawPictureMode fDrawPictureMode;
149    size_t fApproxBytesUsedBySubPictures;
150    SkRecord* fRecord;
151    SkAutoTDelete<SkDrawableList> fDrawableList;
152
153    SkMiniRecorder* fMiniRecorder;
154};
155
156#endif//SkRecorder_DEFINED
157