robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 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 | |
mtklein | 0867943 | 2015-07-29 13:31:22 -0700 | [diff] [blame] | 11 | #include "../private/SkMiniRecorder.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 12 | #include "SkBBHFactory.h" |
| 13 | #include "SkPicture.h" |
| 14 | #include "SkRefCnt.h" |
| 15 | |
djsollen | dbb7b30 | 2014-06-17 13:42:59 -0700 | [diff] [blame] | 16 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 17 | namespace android { |
| 18 | class Picture; |
| 19 | }; |
| 20 | #endif |
| 21 | |
mtklein | 1bb5fec | 2016-08-01 13:17:47 -0700 | [diff] [blame] | 22 | class GrContext; |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 23 | class SkCanvas; |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 24 | class SkDrawable; |
robertphillips | 643b8bd | 2014-06-08 05:55:05 -0700 | [diff] [blame] | 25 | class SkPictureRecord; |
mtklein | 887f397 | 2014-06-17 12:08:15 -0700 | [diff] [blame] | 26 | class SkRecord; |
| 27 | class SkRecorder; |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 28 | |
| 29 | class SK_API SkPictureRecorder : SkNoncopyable { |
| 30 | public: |
Mike Klein | f22b6b5 | 2014-06-23 13:29:10 -0400 | [diff] [blame] | 31 | SkPictureRecorder(); |
robertphillips | 643b8bd | 2014-06-08 05:55:05 -0700 | [diff] [blame] | 32 | ~SkPictureRecorder(); |
| 33 | |
robertphillips | 81f71b6 | 2014-11-11 04:54:49 -0800 | [diff] [blame] | 34 | enum RecordFlags { |
djsollen | aa6f739 | 2015-11-17 06:18:31 -0800 | [diff] [blame] | 35 | // 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. |
robertphillips | 4c7e117 | 2016-07-14 13:44:14 -0700 | [diff] [blame] | 37 | kPlaybackDrawPicture_RecordFlag = 1 << 0, |
reed | 41c27e1 | 2016-07-06 09:29:16 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | enum FinishFlags { |
robertphillips | 81f71b6 | 2014-11-11 04:54:49 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 43 | /** Returns the canvas that records the drawing commands. |
reed | 78e2768 | 2014-11-19 08:04:34 -0800 | [diff] [blame] | 44 | @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.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 46 | @param bbhFactory factory to create desired acceleration structure |
| 47 | @param recordFlags optional flags that control recording. |
| 48 | @return the canvas. |
| 49 | */ |
reed | 78e2768 | 2014-11-19 08:04:34 -0800 | [diff] [blame] | 50 | SkCanvas* beginRecording(const SkRect& bounds, |
reed | 7e76bff | 2014-11-19 06:59:41 -0800 | [diff] [blame] | 51 | SkBBHFactory* bbhFactory = NULL, |
reed | 7c4cdd2 | 2014-11-19 07:17:16 -0800 | [diff] [blame] | 52 | uint32_t recordFlags = 0); |
reed | 7e76bff | 2014-11-19 06:59:41 -0800 | [diff] [blame] | 53 | |
reed | 78e2768 | 2014-11-19 08:04:34 -0800 | [diff] [blame] | 54 | 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.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 60 | /** 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 | */ |
robertphillips | 643b8bd | 2014-06-08 05:55:05 -0700 | [diff] [blame] | 63 | SkCanvas* getRecordingCanvas(); |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 64 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 65 | /** |
| 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 | */ |
reed | 41c27e1 | 2016-07-06 09:29:16 -0700 | [diff] [blame] | 75 | sk_sp<SkPicture> finishRecordingAsPicture(uint32_t endFlags = 0); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 76 | |
| 77 | /** |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 78 | * 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 Li | 46b83a4 | 2017-02-22 13:12:30 -0500 | [diff] [blame] | 80 | * finishRecordingAsPicture(), except that this method updates the cull rect initially passed |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 81 | * 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 | */ |
reed | 41c27e1 | 2016-07-06 09:29:16 -0700 | [diff] [blame] | 86 | sk_sp<SkPicture> finishRecordingAsPictureWithCull(const SkRect& cullRect, |
| 87 | uint32_t endFlags = 0); |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 88 | |
| 89 | /** |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 90 | * 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 Li | 46b83a4 | 2017-02-22 13:12:30 -0500 | [diff] [blame] | 94 | * Unlike finishRecordingAsPicture(), which returns an immutable picture, the returned drawable |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 95 | * 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 | */ |
reed | 41c27e1 | 2016-07-06 09:29:16 -0700 | [diff] [blame] | 99 | sk_sp<SkDrawable> finishRecordingAsDrawable(uint32_t endFlags = 0); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 100 | |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 101 | private: |
mtklein | 887f397 | 2014-06-17 12:08:15 -0700 | [diff] [blame] | 102 | void reset(); |
| 103 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 104 | /** Replay the current (partially recorded) operation stream into |
| 105 | canvas. This call doesn't close the current recording. |
| 106 | */ |
djsollen | dbb7b30 | 2014-06-17 13:42:59 -0700 | [diff] [blame] | 107 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
djsollen | dbb7b30 | 2014-06-17 13:42:59 -0700 | [diff] [blame] | 108 | friend class android::Picture; |
| 109 | #endif |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 110 | friend class SkPictureRecorderReplayTester; // for unit testing |
commit-bot@chromium.org | 2246e32 | 2014-05-29 15:57:11 +0000 | [diff] [blame] | 111 | void partialReplay(SkCanvas* canvas) const; |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 112 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 113 | bool fActivelyRecording; |
| 114 | uint32_t fFlags; |
| 115 | SkRect fCullRect; |
| 116 | sk_sp<SkBBoxHierarchy> fBBH; |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 117 | std::unique_ptr<SkRecorder> fRecorder; |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 118 | sk_sp<SkRecord> fRecord; |
| 119 | SkMiniRecorder fMiniRecorder; |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 120 | |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 121 | typedef SkNoncopyable INHERITED; |
| 122 | }; |
| 123 | |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 124 | #endif |