• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SkPictureRecorder_DEFINED
9 #define SkPictureRecorder_DEFINED
10 
11 #include "include/core/SkBBHFactory.h"
12 #include "include/core/SkPicture.h"
13 #include "include/core/SkRefCnt.h"
14 
15 #include <memory>
16 
17 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
18 namespace android {
19     class Picture;
20 };
21 #endif
22 
23 class SkCanvas;
24 class SkDrawable;
25 class SkPictureRecord;
26 class SkRecord;
27 class SkRecorder;
28 
29 class SK_API SkPictureRecorder {
30 public:
31     SkPictureRecorder();
32     ~SkPictureRecorder();
33 
34     enum FinishFlags {
35     };
36 
37     /** Returns the canvas that records the drawing commands.
38         @param bounds the cull rect used when recording this picture. Any drawing the falls outside
39                       of this rect is undefined, and may be drawn or it may not.
40         @param bbh         optional acceleration structure
41         @param recordFlags optional flags that control recording.
42         @return the canvas.
43     */
44     SkCanvas* beginRecording(const SkRect& bounds, sk_sp<SkBBoxHierarchy> bbh);
45 
46     SkCanvas* beginRecording(const SkRect& bounds, SkBBHFactory* bbhFactory = nullptr);
47 
48     SkCanvas* beginRecording(SkScalar width, SkScalar height,
49                              SkBBHFactory* bbhFactory = nullptr) {
50         return this->beginRecording(SkRect::MakeWH(width, height), bbhFactory);
51     }
52 
53     /** Returns the recording canvas if one is active, or NULL if recording is
54         not active. This does not alter the refcnt on the canvas (if present).
55     */
56     SkCanvas* getRecordingCanvas();
57 
58     /**
59      *  Signal that the caller is done recording. This invalidates the canvas returned by
60      *  beginRecording/getRecordingCanvas. Ownership of the object is passed to the caller, who
61      *  must call unref() when they are done using it.
62      *
63      *  The returned picture is immutable. If during recording drawables were added to the canvas,
64      *  these will have been "drawn" into a recording canvas, so that this resulting picture will
65      *  reflect their current state, but will not contain a live reference to the drawables
66      *  themselves.
67      */
68     sk_sp<SkPicture> finishRecordingAsPicture();
69 
70     /**
71      *  Signal that the caller is done recording, and update the cull rect to use for bounding
72      *  box hierarchy (BBH) generation. The behavior is the same as calling
73      *  finishRecordingAsPicture(), except that this method updates the cull rect initially passed
74      *  into beginRecording.
75      *  @param cullRect the new culling rectangle to use as the overall bound for BBH generation
76      *                  and subsequent culling operations.
77      *  @return the picture containing the recorded content.
78      */
79     sk_sp<SkPicture> finishRecordingAsPictureWithCull(const SkRect& cullRect);
80 
81     /**
82      *  Signal that the caller is done recording. This invalidates the canvas returned by
83      *  beginRecording/getRecordingCanvas. Ownership of the object is passed to the caller, who
84      *  must call unref() when they are done using it.
85      *
86      *  Unlike finishRecordingAsPicture(), which returns an immutable picture, the returned drawable
87      *  may contain live references to other drawables (if they were added to the recording canvas)
88      *  and therefore this drawable will reflect the current state of those nested drawables anytime
89      *  it is drawn or a new picture is snapped from it (by calling drawable->newPictureSnapshot()).
90      */
91     sk_sp<SkDrawable> finishRecordingAsDrawable();
92 
93 private:
94     void reset();
95 
96     /** Replay the current (partially recorded) operation stream into
97         canvas. This call doesn't close the current recording.
98     */
99 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
100     friend class android::Picture;
101 #endif
102     friend class SkPictureRecorderReplayTester; // for unit testing
103     void partialReplay(SkCanvas* canvas) const;
104 
105     bool                        fActivelyRecording;
106     SkRect                      fCullRect;
107     sk_sp<SkBBoxHierarchy>      fBBH;
108     std::unique_ptr<SkRecorder> fRecorder;
109     sk_sp<SkRecord>             fRecord;
110 
111     SkPictureRecorder(SkPictureRecorder&&) = delete;
112     SkPictureRecorder& operator=(SkPictureRecorder&&) = delete;
113 };
114 
115 #endif
116