blob: be7bad5aef3d59afcee2dff01cf54a8a52c7cd10 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkPicture_DEFINED
18#define SkPicture_DEFINED
19
20#include "SkRefCnt.h"
21
22class SkCanvas;
23class SkPicturePlayback;
24class SkPictureRecord;
25class SkStream;
26class SkWStream;
27
28/** \class SkPicture
29
30 The SkPicture class records the drawing commands made to a canvas, to
31 be played back at a later time.
32*/
33class SkPicture : public SkRefCnt {
34public:
35 /** The constructor prepares the picture to record.
36 @param width the width of the virtual device the picture records.
37 @param height the height of the virtual device the picture records.
38 */
39 SkPicture();
40 /** Make a copy of the contents of src. If src records more drawing after
41 this call, those elements will not appear in this picture.
42 */
43 SkPicture(const SkPicture& src);
44 explicit SkPicture(SkStream*);
45 virtual ~SkPicture();
46
47 /**
48 * Swap the contents of the two pictures. Guaranteed to succeed.
49 */
50 void swap(SkPicture& other);
51
reed@android.comae814c82009-02-13 14:56:09 +000052 enum RecordingFlags {
53 /* This flag specifies that when clipPath() is called, the path will
54 be faithfully recorded, but the recording canvas' current clip will
55 only see the path's bounds. This speeds up the recording process
56 without compromising the fidelity of the playback. The only side-
57 effect for recording is that calling getTotalClip() or related
58 clip-query calls will reflect the path's bounds, not the actual
59 path.
60 */
61 kUsePathBoundsForClip_RecordingFlag = 0x01
62 };
63
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 /** Returns the canvas that records the drawing commands.
reed@android.comae814c82009-02-13 14:56:09 +000065 @param width the base width for the picture, as if the recording
66 canvas' bitmap had this width.
67 @param height the base width for the picture, as if the recording
68 canvas' bitmap had this height.
69 @param recordFlags optional flags that control recording.
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 @return the picture canvas.
71 */
reed@android.comae814c82009-02-13 14:56:09 +000072 SkCanvas* beginRecording(int width, int height, uint32_t recordFlags = 0);
73
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 /** Returns the recording canvas if one is active, or NULL if recording is
75 not active. This does not alter the refcnt on the canvas (if present).
76 */
77 SkCanvas* getRecordingCanvas() const;
78 /** Signal that the caller is done recording. This invalidates the canvas
79 returned by beginRecording/getRecordingCanvas, and prepares the picture
80 for drawing. Note: this happens implicitly the first time the picture
81 is drawn.
82 */
83 void endRecording();
84
85 /** Replays the drawing commands on the specified canvas. This internally
86 calls endRecording() if that has not already been called.
87 @param surface the canvas receiving the drawing commands.
88 */
89 void draw(SkCanvas* surface);
90
91 /** Return the width of the picture's recording canvas. This
92 value reflects what was passed to setSize(), and does not necessarily
93 reflect the bounds of what has been recorded into the picture.
94 @return the width of the picture's recording canvas
95 */
96 int width() const { return fWidth; }
97
98 /** Return the height of the picture's recording canvas. This
99 value reflects what was passed to setSize(), and does not necessarily
100 reflect the bounds of what has been recorded into the picture.
101 @return the height of the picture's recording canvas
102 */
103 int height() const { return fHeight; }
104
105 void serialize(SkWStream*) const;
106
107 /** Signals that the caller is prematurely done replaying the drawing
108 commands. This can be called from a canvas virtual while the picture
109 is drawing. Has no effect if the picture is not drawing.
110 */
111 void abortPlayback();
112
113private:
114 int fWidth, fHeight;
115 SkPictureRecord* fRecord;
116 SkPicturePlayback* fPlayback;
117
118 friend class SkFlatPicture;
119 friend class SkPicturePlayback;
120};
121
122class SkAutoPictureRecord : SkNoncopyable {
123public:
reed@android.comae814c82009-02-13 14:56:09 +0000124 SkAutoPictureRecord(SkPicture* pict, int width, int height,
125 uint32_t recordingFlags = 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 fPicture = pict;
reed@android.comae814c82009-02-13 14:56:09 +0000127 fCanvas = pict->beginRecording(width, height, recordingFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 }
129 ~SkAutoPictureRecord() {
130 fPicture->endRecording();
131 }
132
133 /** Return the canvas to draw into for recording into the picture.
134 */
135 SkCanvas* getRecordingCanvas() const { return fCanvas; }
136
137private:
138 SkPicture* fPicture;
139 SkCanvas* fCanvas;
140};
141
142
143#endif