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 { |
| 41 | kReturnNullForEmpty_FinishFlag = 1 << 0, // no draw-ops will return nullptr |
robertphillips | 81f71b6 | 2014-11-11 04:54:49 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 44 | /** Returns the canvas that records the drawing commands. |
reed | 78e2768 | 2014-11-19 08:04:34 -0800 | [diff] [blame] | 45 | @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.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 47 | @param bbhFactory factory to create desired acceleration structure |
| 48 | @param recordFlags optional flags that control recording. |
| 49 | @return the canvas. |
| 50 | */ |
reed | 78e2768 | 2014-11-19 08:04:34 -0800 | [diff] [blame] | 51 | SkCanvas* beginRecording(const SkRect& bounds, |
reed | 7e76bff | 2014-11-19 06:59:41 -0800 | [diff] [blame] | 52 | SkBBHFactory* bbhFactory = NULL, |
reed | 7c4cdd2 | 2014-11-19 07:17:16 -0800 | [diff] [blame] | 53 | uint32_t recordFlags = 0); |
reed | 7e76bff | 2014-11-19 06:59:41 -0800 | [diff] [blame] | 54 | |
reed | 78e2768 | 2014-11-19 08:04:34 -0800 | [diff] [blame] | 55 | SkCanvas* beginRecording(SkScalar width, SkScalar height, |
| 56 | SkBBHFactory* bbhFactory = NULL, |
| 57 | uint32_t recordFlags = 0) { |
| 58 | return this->beginRecording(SkRect::MakeWH(width, height), bbhFactory, recordFlags); |
| 59 | } |
| 60 | |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 61 | /** 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 | */ |
robertphillips | 643b8bd | 2014-06-08 05:55:05 -0700 | [diff] [blame] | 64 | SkCanvas* getRecordingCanvas(); |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 65 | |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 66 | /** |
| 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 | */ |
reed | 41c27e1 | 2016-07-06 09:29:16 -0700 | [diff] [blame] | 76 | sk_sp<SkPicture> finishRecordingAsPicture(uint32_t endFlags = 0); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 79 | * 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 |
| 81 | * endRecordingAsPicture(), except that this method updates the cull rect initially passed |
| 82 | * 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 | */ |
reed | 41c27e1 | 2016-07-06 09:29:16 -0700 | [diff] [blame] | 87 | sk_sp<SkPicture> finishRecordingAsPictureWithCull(const SkRect& cullRect, |
| 88 | uint32_t endFlags = 0); |
schenney | eeff8bb | 2015-07-07 14:27:10 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 91 | * 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 | * |
| 95 | * Unlike endRecordingAsPicture(), which returns an immutable picture, the returned drawable |
| 96 | * 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 | */ |
reed | 41c27e1 | 2016-07-06 09:29:16 -0700 | [diff] [blame] | 100 | sk_sp<SkDrawable> finishRecordingAsDrawable(uint32_t endFlags = 0); |
reed | 1bdfd3f | 2014-11-24 14:41:51 -0800 | [diff] [blame] | 101 | |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 102 | private: |
mtklein | 887f397 | 2014-06-17 12:08:15 -0700 | [diff] [blame] | 103 | void reset(); |
| 104 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 105 | /** Replay the current (partially recorded) operation stream into |
| 106 | canvas. This call doesn't close the current recording. |
| 107 | */ |
djsollen | dbb7b30 | 2014-06-17 13:42:59 -0700 | [diff] [blame] | 108 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
djsollen | dbb7b30 | 2014-06-17 13:42:59 -0700 | [diff] [blame] | 109 | friend class android::Picture; |
| 110 | #endif |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 111 | friend class SkPictureRecorderReplayTester; // for unit testing |
commit-bot@chromium.org | 2246e32 | 2014-05-29 15:57:11 +0000 | [diff] [blame] | 112 | void partialReplay(SkCanvas* canvas) const; |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 113 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame^] | 114 | bool fActivelyRecording; |
| 115 | uint32_t fFlags; |
| 116 | SkRect fCullRect; |
| 117 | sk_sp<SkBBoxHierarchy> fBBH; |
| 118 | #ifdef SK_SUPPORT_LEGACY_CANVAS_IS_REFCNT |
| 119 | sk_sp<SkRecorder> fRecorder; |
| 120 | #else |
| 121 | std::unique_ptr<SkRecorder> fRecorder; |
| 122 | #endif |
| 123 | sk_sp<SkRecord> fRecord; |
| 124 | SkMiniRecorder fMiniRecorder; |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 125 | |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 126 | typedef SkNoncopyable INHERITED; |
| 127 | }; |
| 128 | |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 129 | #endif |