blob: fa614c95162a56d2ec64570e4c15ae8ae3b5843a [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
52 /** Returns the canvas that records the drawing commands.
53 @return the picture canvas.
54 */
55 SkCanvas* beginRecording(int width, int height);
56 /** Returns the recording canvas if one is active, or NULL if recording is
57 not active. This does not alter the refcnt on the canvas (if present).
58 */
59 SkCanvas* getRecordingCanvas() const;
60 /** Signal that the caller is done recording. This invalidates the canvas
61 returned by beginRecording/getRecordingCanvas, and prepares the picture
62 for drawing. Note: this happens implicitly the first time the picture
63 is drawn.
64 */
65 void endRecording();
66
67 /** Replays the drawing commands on the specified canvas. This internally
68 calls endRecording() if that has not already been called.
69 @param surface the canvas receiving the drawing commands.
70 */
71 void draw(SkCanvas* surface);
72
73 /** Return the width of the picture's recording canvas. This
74 value reflects what was passed to setSize(), and does not necessarily
75 reflect the bounds of what has been recorded into the picture.
76 @return the width of the picture's recording canvas
77 */
78 int width() const { return fWidth; }
79
80 /** Return the height of the picture's recording canvas. This
81 value reflects what was passed to setSize(), and does not necessarily
82 reflect the bounds of what has been recorded into the picture.
83 @return the height of the picture's recording canvas
84 */
85 int height() const { return fHeight; }
86
87 void serialize(SkWStream*) const;
88
89 /** Signals that the caller is prematurely done replaying the drawing
90 commands. This can be called from a canvas virtual while the picture
91 is drawing. Has no effect if the picture is not drawing.
92 */
93 void abortPlayback();
94
95private:
96 int fWidth, fHeight;
97 SkPictureRecord* fRecord;
98 SkPicturePlayback* fPlayback;
99
100 friend class SkFlatPicture;
101 friend class SkPicturePlayback;
102};
103
104class SkAutoPictureRecord : SkNoncopyable {
105public:
106 SkAutoPictureRecord(SkPicture* pict, int width, int height) {
107 fPicture = pict;
108 fCanvas = pict->beginRecording(width, height);
109 }
110 ~SkAutoPictureRecord() {
111 fPicture->endRecording();
112 }
113
114 /** Return the canvas to draw into for recording into the picture.
115 */
116 SkCanvas* getRecordingCanvas() const { return fCanvas; }
117
118private:
119 SkPicture* fPicture;
120 SkCanvas* fCanvas;
121};
122
123
124#endif