SkPicture.h revision 4866cc0afb7571309d9fdecb221d919f663054c0
1
2/*
3 * Copyright 2007 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkPicture_DEFINED
11#define SkPicture_DEFINED
12
13#include "SkRefCnt.h"
14
15class SkBitmap;
16class SkCanvas;
17class SkPicturePlayback;
18class SkPictureRecord;
19class SkStream;
20class SkWStream;
21
22/** \class SkPicture
23
24    The SkPicture class records the drawing commands made to a canvas, to
25    be played back at a later time.
26*/
27class SK_API SkPicture : public SkRefCnt {
28public:
29    /** The constructor prepares the picture to record.
30        @param width the width of the virtual device the picture records.
31        @param height the height of the virtual device the picture records.
32    */
33    SkPicture();
34    /** Make a copy of the contents of src. If src records more drawing after
35        this call, those elements will not appear in this picture.
36    */
37    SkPicture(const SkPicture& src);
38    explicit SkPicture(SkStream*);
39    virtual ~SkPicture();
40
41    /**
42     *  Swap the contents of the two pictures. Guaranteed to succeed.
43     */
44    void swap(SkPicture& other);
45
46    enum RecordingFlags {
47        /*  This flag specifies that when clipPath() is called, the path will
48            be faithfully recorded, but the recording canvas' current clip will
49            only see the path's bounds. This speeds up the recording process
50            without compromising the fidelity of the playback. The only side-
51            effect for recording is that calling getTotalClip() or related
52            clip-query calls will reflect the path's bounds, not the actual
53            path.
54         */
55        kUsePathBoundsForClip_RecordingFlag = 0x01,
56
57        /*  When a draw operation is recorded that has a bitmap parameter, it
58            may be unsafe to defer rendering if source bitmap may be written to
59            between the time of recording and the time of executing the draw
60            operation. This flag specifies that SkPicture should serialize a
61            snapshot of any source bitmaps that reside in RAM and are not
62            marked as immutable, making the draw operation safe for deferral.
63         */
64        kFlattenMutableNonTexturePixelRefs_RecordingFlag = 0x02
65    };
66
67    /** Returns the canvas that records the drawing commands.
68        @param width the base width for the picture, as if the recording
69                     canvas' bitmap had this width.
70        @param height the base width for the picture, as if the recording
71                     canvas' bitmap had this height.
72        @param recordFlags optional flags that control recording.
73        @return the picture canvas.
74    */
75    SkCanvas* beginRecording(int width, int height, uint32_t recordFlags = 0);
76
77    /** Returns the recording canvas if one is active, or NULL if recording is
78        not active. This does not alter the refcnt on the canvas (if present).
79    */
80    SkCanvas* getRecordingCanvas() const;
81    /** Signal that the caller is done recording. This invalidates the canvas
82        returned by beginRecording/getRecordingCanvas, and prepares the picture
83        for drawing. Note: this happens implicitly the first time the picture
84        is drawn.
85    */
86    void endRecording();
87
88    /** Returns true if any draw commands have been recorded since the last
89        call to beginRecording.
90    */
91    bool hasRecorded() const;
92
93    /** Returns true if a snapshot of the specified bitmap will be flattened
94        whaen a draw operation using the bitmap is recorded.
95    */
96    bool willFlattenPixelsOnRecord(const SkBitmap&) const;
97
98    /** Replays the drawing commands on the specified canvas. This internally
99        calls endRecording() if that has not already been called.
100        @param surface the canvas receiving the drawing commands.
101    */
102    void draw(SkCanvas* surface);
103
104    /** Return the width of the picture's recording canvas. This
105        value reflects what was passed to setSize(), and does not necessarily
106        reflect the bounds of what has been recorded into the picture.
107        @return the width of the picture's recording canvas
108    */
109    int width() const { return fWidth; }
110
111    /** Return the height of the picture's recording canvas. This
112        value reflects what was passed to setSize(), and does not necessarily
113        reflect the bounds of what has been recorded into the picture.
114        @return the height of the picture's recording canvas
115    */
116    int height() const { return fHeight; }
117
118    void serialize(SkWStream*) const;
119
120    /** Signals that the caller is prematurely done replaying the drawing
121        commands. This can be called from a canvas virtual while the picture
122        is drawing. Has no effect if the picture is not drawing.
123    */
124    void abortPlayback();
125
126private:
127    int fWidth, fHeight;
128    SkPictureRecord* fRecord;
129    SkPicturePlayback* fPlayback;
130
131    friend class SkFlatPicture;
132    friend class SkPicturePlayback;
133};
134
135class SkAutoPictureRecord : SkNoncopyable {
136public:
137    SkAutoPictureRecord(SkPicture* pict, int width, int height,
138                        uint32_t recordingFlags = 0) {
139        fPicture = pict;
140        fCanvas = pict->beginRecording(width, height, recordingFlags);
141    }
142    ~SkAutoPictureRecord() {
143        fPicture->endRecording();
144    }
145
146    /** Return the canvas to draw into for recording into the picture.
147    */
148    SkCanvas* getRecordingCanvas() const { return fCanvas; }
149
150private:
151    SkPicture*  fPicture;
152    SkCanvas*   fCanvas;
153};
154
155
156#endif
157