blob: d898b91cca548ba5894016a2201e41d21cade99b [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
mtklein08679432015-07-29 13:31:22 -070011#include "../private/SkMiniRecorder.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;
robertphillips643b8bd2014-06-08 05:55:05 -070025class SkPictureRecord;
mtklein887f3972014-06-17 12:08:15 -070026class SkRecord;
27class SkRecorder;
robertphillips@google.com770963f2014-04-18 18:04:41 +000028
29class SK_API SkPictureRecorder : SkNoncopyable {
30public:
Mike Kleinf22b6b52014-06-23 13:29:10 -040031 SkPictureRecorder();
robertphillips643b8bd2014-06-08 05:55:05 -070032 ~SkPictureRecorder();
33
robertphillips81f71b62014-11-11 04:54:49 -080034 enum RecordFlags {
djsollenaa6f7392015-11-17 06:18:31 -080035 // If you call drawPicture() or drawDrawable() on the recording canvas, this flag forces
36 // that object to playback its contents immediately rather than reffing the object.
robertphillips4c7e1172016-07-14 13:44:14 -070037 kPlaybackDrawPicture_RecordFlag = 1 << 0,
reed41c27e12016-07-06 09:29:16 -070038 };
39
40 enum FinishFlags {
robertphillips81f71b62014-11-11 04:54:49 -080041 };
42
robertphillips@google.com770963f2014-04-18 18:04:41 +000043 /** Returns the canvas that records the drawing commands.
reed78e27682014-11-19 08:04:34 -080044 @param bounds the cull rect used when recording this picture. Any drawing the falls outside
45 of this rect is undefined, and may be drawn or it may not.
robertphillips@google.com770963f2014-04-18 18:04:41 +000046 @param bbhFactory factory to create desired acceleration structure
47 @param recordFlags optional flags that control recording.
48 @return the canvas.
49 */
reed78e27682014-11-19 08:04:34 -080050 SkCanvas* beginRecording(const SkRect& bounds,
reed7e76bff2014-11-19 06:59:41 -080051 SkBBHFactory* bbhFactory = NULL,
reed7c4cdd22014-11-19 07:17:16 -080052 uint32_t recordFlags = 0);
reed7e76bff2014-11-19 06:59:41 -080053
reed78e27682014-11-19 08:04:34 -080054 SkCanvas* beginRecording(SkScalar width, SkScalar height,
55 SkBBHFactory* bbhFactory = NULL,
56 uint32_t recordFlags = 0) {
57 return this->beginRecording(SkRect::MakeWH(width, height), bbhFactory, recordFlags);
58 }
59
robertphillips@google.com770963f2014-04-18 18:04:41 +000060 /** Returns the recording canvas if one is active, or NULL if recording is
61 not active. This does not alter the refcnt on the canvas (if present).
62 */
robertphillips643b8bd2014-06-08 05:55:05 -070063 SkCanvas* getRecordingCanvas();
robertphillips@google.com770963f2014-04-18 18:04:41 +000064
reed1bdfd3f2014-11-24 14:41:51 -080065 /**
66 * Signal that the caller is done recording. This invalidates the canvas returned by
67 * beginRecording/getRecordingCanvas. Ownership of the object is passed to the caller, who
68 * must call unref() when they are done using it.
69 *
70 * The returned picture is immutable. If during recording drawables were added to the canvas,
71 * these will have been "drawn" into a recording canvas, so that this resulting picture will
72 * reflect their current state, but will not contain a live reference to the drawables
73 * themselves.
74 */
reed41c27e12016-07-06 09:29:16 -070075 sk_sp<SkPicture> finishRecordingAsPicture(uint32_t endFlags = 0);
reed1bdfd3f2014-11-24 14:41:51 -080076
77 /**
schenneyeeff8bb2015-07-07 14:27:10 -070078 * Signal that the caller is done recording, and update the cull rect to use for bounding
79 * box hierarchy (BBH) generation. The behavior is the same as calling
Yuqian Li46b83a42017-02-22 13:12:30 -050080 * finishRecordingAsPicture(), except that this method updates the cull rect initially passed
schenneyeeff8bb2015-07-07 14:27:10 -070081 * into beginRecording.
82 * @param cullRect the new culling rectangle to use as the overall bound for BBH generation
83 * and subsequent culling operations.
84 * @return the picture containing the recorded content.
85 */
reed41c27e12016-07-06 09:29:16 -070086 sk_sp<SkPicture> finishRecordingAsPictureWithCull(const SkRect& cullRect,
87 uint32_t endFlags = 0);
schenneyeeff8bb2015-07-07 14:27:10 -070088
89 /**
reed1bdfd3f2014-11-24 14:41:51 -080090 * Signal that the caller is done recording. This invalidates the canvas returned by
91 * beginRecording/getRecordingCanvas. Ownership of the object is passed to the caller, who
92 * must call unref() when they are done using it.
93 *
Yuqian Li46b83a42017-02-22 13:12:30 -050094 * Unlike finishRecordingAsPicture(), which returns an immutable picture, the returned drawable
reed1bdfd3f2014-11-24 14:41:51 -080095 * may contain live references to other drawables (if they were added to the recording canvas)
96 * and therefore this drawable will reflect the current state of those nested drawables anytime
97 * it is drawn or a new picture is snapped from it (by calling drawable->newPictureSnapshot()).
98 */
reed41c27e12016-07-06 09:29:16 -070099 sk_sp<SkDrawable> finishRecordingAsDrawable(uint32_t endFlags = 0);
reed1bdfd3f2014-11-24 14:41:51 -0800100
robertphillips@google.com770963f2014-04-18 18:04:41 +0000101private:
mtklein887f3972014-06-17 12:08:15 -0700102 void reset();
103
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +0000104 /** Replay the current (partially recorded) operation stream into
105 canvas. This call doesn't close the current recording.
106 */
djsollendbb7b302014-06-17 13:42:59 -0700107#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollendbb7b302014-06-17 13:42:59 -0700108 friend class android::Picture;
109#endif
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +0000110 friend class SkPictureRecorderReplayTester; // for unit testing
commit-bot@chromium.org2246e322014-05-29 15:57:11 +0000111 void partialReplay(SkCanvas* canvas) const;
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +0000112
Mike Reed5df49342016-11-12 08:06:55 -0600113 bool fActivelyRecording;
114 uint32_t fFlags;
115 SkRect fCullRect;
116 sk_sp<SkBBoxHierarchy> fBBH;
Mike Reed5df49342016-11-12 08:06:55 -0600117 std::unique_ptr<SkRecorder> fRecorder;
Mike Reed5df49342016-11-12 08:06:55 -0600118 sk_sp<SkRecord> fRecord;
119 SkMiniRecorder fMiniRecorder;
robertphillips@google.com770963f2014-04-18 18:04:41 +0000120
robertphillips@google.com770963f2014-04-18 18:04:41 +0000121 typedef SkNoncopyable INHERITED;
122};
123
robertphillips@google.com770963f2014-04-18 18:04:41 +0000124#endif