blob: d28544c48c7f55246c17a5cd8835a0a543003c12 [file] [log] [blame]
robertphillips@google.com770963f2014-04-18 18:04:41 +00001/*
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
Ben Wagnerd5148e32018-07-16 17:44:06 -040011#include "../private/SkNoncopyable.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000012#include "SkBBHFactory.h"
13#include "SkPicture.h"
14#include "SkRefCnt.h"
15
djsollendbb7b302014-06-17 13:42:59 -070016#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
17namespace android {
18 class Picture;
19};
20#endif
21
mtklein1bb5fec2016-08-01 13:17:47 -070022class GrContext;
robertphillips@google.com770963f2014-04-18 18:04:41 +000023class SkCanvas;
reed3cb38402015-02-06 08:36:15 -080024class SkDrawable;
Mike Reedc090c642017-05-16 10:39:06 -040025class SkMiniRecorder;
robertphillips643b8bd2014-06-08 05:55:05 -070026class SkPictureRecord;
mtklein887f3972014-06-17 12:08:15 -070027class SkRecord;
28class SkRecorder;
robertphillips@google.com770963f2014-04-18 18:04:41 +000029
30class SK_API SkPictureRecorder : SkNoncopyable {
31public:
Mike Kleinf22b6b52014-06-23 13:29:10 -040032 SkPictureRecorder();
robertphillips643b8bd2014-06-08 05:55:05 -070033 ~SkPictureRecorder();
34
robertphillips81f71b62014-11-11 04:54:49 -080035 enum RecordFlags {
djsollenaa6f7392015-11-17 06:18:31 -080036 // If you call drawPicture() or drawDrawable() on the recording canvas, this flag forces
37 // that object to playback its contents immediately rather than reffing the object.
robertphillips4c7e1172016-07-14 13:44:14 -070038 kPlaybackDrawPicture_RecordFlag = 1 << 0,
reed41c27e12016-07-06 09:29:16 -070039 };
40
41 enum FinishFlags {
robertphillips81f71b62014-11-11 04:54:49 -080042 };
43
robertphillips@google.com770963f2014-04-18 18:04:41 +000044 /** Returns the canvas that records the drawing commands.
reed78e27682014-11-19 08:04:34 -080045 @param bounds the cull rect used when recording this picture. Any drawing the falls outside
46 of this rect is undefined, and may be drawn or it may not.
robertphillips@google.com770963f2014-04-18 18:04:41 +000047 @param bbhFactory factory to create desired acceleration structure
48 @param recordFlags optional flags that control recording.
49 @return the canvas.
50 */
reed78e27682014-11-19 08:04:34 -080051 SkCanvas* beginRecording(const SkRect& bounds,
Ben Wagnera93a14a2017-08-28 10:34:05 -040052 SkBBHFactory* bbhFactory = nullptr,
reed7c4cdd22014-11-19 07:17:16 -080053 uint32_t recordFlags = 0);
reed7e76bff2014-11-19 06:59:41 -080054
reed78e27682014-11-19 08:04:34 -080055 SkCanvas* beginRecording(SkScalar width, SkScalar height,
Ben Wagnera93a14a2017-08-28 10:34:05 -040056 SkBBHFactory* bbhFactory = nullptr,
reed78e27682014-11-19 08:04:34 -080057 uint32_t recordFlags = 0) {
58 return this->beginRecording(SkRect::MakeWH(width, height), bbhFactory, recordFlags);
59 }
60
robertphillips@google.com770963f2014-04-18 18:04:41 +000061 /** Returns the recording canvas if one is active, or NULL if recording is
62 not active. This does not alter the refcnt on the canvas (if present).
63 */
robertphillips643b8bd2014-06-08 05:55:05 -070064 SkCanvas* getRecordingCanvas();
robertphillips@google.com770963f2014-04-18 18:04:41 +000065
reed1bdfd3f2014-11-24 14:41:51 -080066 /**
67 * Signal that the caller is done recording. This invalidates the canvas returned by
68 * beginRecording/getRecordingCanvas. Ownership of the object is passed to the caller, who
69 * must call unref() when they are done using it.
70 *
71 * The returned picture is immutable. If during recording drawables were added to the canvas,
72 * these will have been "drawn" into a recording canvas, so that this resulting picture will
73 * reflect their current state, but will not contain a live reference to the drawables
74 * themselves.
75 */
reed41c27e12016-07-06 09:29:16 -070076 sk_sp<SkPicture> finishRecordingAsPicture(uint32_t endFlags = 0);
reed1bdfd3f2014-11-24 14:41:51 -080077
78 /**
schenneyeeff8bb2015-07-07 14:27:10 -070079 * Signal that the caller is done recording, and update the cull rect to use for bounding
80 * box hierarchy (BBH) generation. The behavior is the same as calling
Yuqian Li46b83a42017-02-22 13:12:30 -050081 * finishRecordingAsPicture(), except that this method updates the cull rect initially passed
schenneyeeff8bb2015-07-07 14:27:10 -070082 * into beginRecording.
83 * @param cullRect the new culling rectangle to use as the overall bound for BBH generation
84 * and subsequent culling operations.
85 * @return the picture containing the recorded content.
86 */
reed41c27e12016-07-06 09:29:16 -070087 sk_sp<SkPicture> finishRecordingAsPictureWithCull(const SkRect& cullRect,
88 uint32_t endFlags = 0);
schenneyeeff8bb2015-07-07 14:27:10 -070089
90 /**
reed1bdfd3f2014-11-24 14:41:51 -080091 * Signal that the caller is done recording. This invalidates the canvas returned by
92 * beginRecording/getRecordingCanvas. Ownership of the object is passed to the caller, who
93 * must call unref() when they are done using it.
94 *
Yuqian Li46b83a42017-02-22 13:12:30 -050095 * Unlike finishRecordingAsPicture(), which returns an immutable picture, the returned drawable
reed1bdfd3f2014-11-24 14:41:51 -080096 * may contain live references to other drawables (if they were added to the recording canvas)
97 * and therefore this drawable will reflect the current state of those nested drawables anytime
98 * it is drawn or a new picture is snapped from it (by calling drawable->newPictureSnapshot()).
99 */
reed41c27e12016-07-06 09:29:16 -0700100 sk_sp<SkDrawable> finishRecordingAsDrawable(uint32_t endFlags = 0);
reed1bdfd3f2014-11-24 14:41:51 -0800101
robertphillips@google.com770963f2014-04-18 18:04:41 +0000102private:
mtklein887f3972014-06-17 12:08:15 -0700103 void reset();
104
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +0000105 /** Replay the current (partially recorded) operation stream into
106 canvas. This call doesn't close the current recording.
107 */
djsollendbb7b302014-06-17 13:42:59 -0700108#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollendbb7b302014-06-17 13:42:59 -0700109 friend class android::Picture;
110#endif
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +0000111 friend class SkPictureRecorderReplayTester; // for unit testing
commit-bot@chromium.org2246e322014-05-29 15:57:11 +0000112 void partialReplay(SkCanvas* canvas) const;
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +0000113
Mike Reed5df49342016-11-12 08:06:55 -0600114 bool fActivelyRecording;
115 uint32_t fFlags;
116 SkRect fCullRect;
117 sk_sp<SkBBoxHierarchy> fBBH;
Mike Reed5df49342016-11-12 08:06:55 -0600118 std::unique_ptr<SkRecorder> fRecorder;
Mike Reed5df49342016-11-12 08:06:55 -0600119 sk_sp<SkRecord> fRecord;
Mike Reedc090c642017-05-16 10:39:06 -0400120 std::unique_ptr<SkMiniRecorder> fMiniRecorder;
robertphillips@google.com770963f2014-04-18 18:04:41 +0000121
robertphillips@google.com770963f2014-04-18 18:04:41 +0000122 typedef SkNoncopyable INHERITED;
123};
124
robertphillips@google.com770963f2014-04-18 18:04:41 +0000125#endif