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