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