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