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 "SkBBHFactory.h"
12 #include "SkPicture.h"
13 #include "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 SkPictureRecord;
24 class SkRecord;
25 class SkRecorder;
26 
27 class SK_API SkPictureRecorder : SkNoncopyable {
28 public:
29     SkPictureRecorder();
30     ~SkPictureRecorder();
31 
32     enum RecordFlags {
33         // This flag indicates that, if some BHH is being computed, saveLayer
34         // information should also be extracted at the same time.
35         kComputeSaveLayerInfo_RecordFlag = 0x01
36     };
37 
38     /** Returns the canvas that records the drawing commands.
39         @param bounds the cull rect used when recording this picture. Any drawing the falls outside
40                       of this rect is undefined, and may be drawn or it may not.
41         @param bbhFactory factory to create desired acceleration structure
42         @param recordFlags optional flags that control recording.
43         @return the canvas.
44     */
45     SkCanvas* beginRecording(const SkRect& bounds,
46                              SkBBHFactory* bbhFactory = NULL,
47                              uint32_t recordFlags = 0);
48 
49     SkCanvas* beginRecording(SkScalar width, SkScalar height,
50                              SkBBHFactory* bbhFactory = NULL,
51                              uint32_t recordFlags = 0) {
52         return this->beginRecording(SkRect::MakeWH(width, height), bbhFactory, recordFlags);
53     }
54 
55     /** Returns the recording canvas if one is active, or NULL if recording is
56         not active. This does not alter the refcnt on the canvas (if present).
57     */
58     SkCanvas* getRecordingCanvas();
59 
60     /**
61      *  Signal that the caller is done recording. This invalidates the canvas returned by
62      *  beginRecording/getRecordingCanvas. Ownership of the object is passed to the caller, who
63      *  must call unref() when they are done using it.
64      *
65      *  The returned picture is immutable. If during recording drawables were added to the canvas,
66      *  these will have been "drawn" into a recording canvas, so that this resulting picture will
67      *  reflect their current state, but will not contain a live reference to the drawables
68      *  themselves.
69      */
70     SkPicture* SK_WARN_UNUSED_RESULT endRecordingAsPicture();
71 
72     /**
73      *  Signal that the caller is done recording. This invalidates the canvas returned by
74      *  beginRecording/getRecordingCanvas. Ownership of the object is passed to the caller, who
75      *  must call unref() when they are done using it.
76      *
77      *  Unlike endRecordingAsPicture(), which returns an immutable picture, the returned drawable
78      *  may contain live references to other drawables (if they were added to the recording canvas)
79      *  and therefore this drawable will reflect the current state of those nested drawables anytime
80      *  it is drawn or a new picture is snapped from it (by calling drawable->newPictureSnapshot()).
81      */
82     SkDrawable* SK_WARN_UNUSED_RESULT endRecordingAsDrawable();
83 
84     // Legacy API -- use endRecordingAsPicture instead.
endRecording()85     SkPicture* SK_WARN_UNUSED_RESULT endRecording() { return this->endRecordingAsPicture(); }
86 
87 private:
88     void reset();
89 
90     /** Replay the current (partially recorded) operation stream into
91         canvas. This call doesn't close the current recording.
92     */
93 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
94     friend class android::Picture;
95 #endif
96     friend class SkPictureRecorderReplayTester; // for unit testing
97     void partialReplay(SkCanvas* canvas) const;
98 
99     bool                          fActivelyRecording;
100     uint32_t                      fFlags;
101     SkRect                        fCullRect;
102     SkAutoTUnref<SkBBoxHierarchy> fBBH;
103     SkAutoTUnref<SkRecorder>      fRecorder;
104     SkAutoTUnref<SkRecord>        fRecord;
105 
106     typedef SkNoncopyable INHERITED;
107 };
108 
109 #endif
110