reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 22 | class SkCanvas; |
| 23 | class SkPicturePlayback; |
| 24 | class SkPictureRecord; |
| 25 | class SkStream; |
| 26 | class 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 | */ |
| 33 | class SkPicture : public SkRefCnt { |
| 34 | public: |
| 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 | |
| 95 | private: |
| 96 | int fWidth, fHeight; |
| 97 | SkPictureRecord* fRecord; |
| 98 | SkPicturePlayback* fPlayback; |
| 99 | |
| 100 | friend class SkFlatPicture; |
| 101 | friend class SkPicturePlayback; |
| 102 | }; |
| 103 | |
| 104 | class SkAutoPictureRecord : SkNoncopyable { |
| 105 | public: |
| 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 | |
| 118 | private: |
| 119 | SkPicture* fPicture; |
| 120 | SkCanvas* fCanvas; |
| 121 | }; |
| 122 | |
| 123 | |
| 124 | #endif |