SkPicture.h revision 34342f6f5127122ecc32166dcffa7f3d2a45c387
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 SK_DECLARE_INST_COUNT(SkPicture) 30 31 /** The constructor prepares the picture to record. 32 @param width the width of the virtual device the picture records. 33 @param height the height of the virtual device the picture records. 34 */ 35 SkPicture(); 36 /** Make a copy of the contents of src. If src records more drawing after 37 this call, those elements will not appear in this picture. 38 */ 39 SkPicture(const SkPicture& src); 40 /** 41 * Recreate a picture that was serialized into a stream. If an error occurs 42 * the picture will be "empty" : width and height == 0 43 */ 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 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 /* When a draw operation is recorded that has a bitmap parameter, it 64 may be unsafe to defer rendering if source bitmap may be written to 65 between the time of recording and the time of executing the draw 66 operation. This flag specifies that SkPicture should serialize a 67 snapshot of any source bitmaps that reside in RAM and are not 68 marked as immutable, making the draw operation safe for deferral. 69 */ 70 kFlattenMutableNonTexturePixelRefs_RecordingFlag = 0x02 71 }; 72 73 /** Returns the canvas that records the drawing commands. 74 @param width the base width for the picture, as if the recording 75 canvas' bitmap had this width. 76 @param height the base width for the picture, as if the recording 77 canvas' bitmap had this height. 78 @param recordFlags optional flags that control recording. 79 @return the picture canvas. 80 */ 81 SkCanvas* beginRecording(int width, int height, uint32_t recordFlags = 0); 82 83 /** Returns the recording canvas if one is active, or NULL if recording is 84 not active. This does not alter the refcnt on the canvas (if present). 85 */ 86 SkCanvas* getRecordingCanvas() const; 87 /** Signal that the caller is done recording. This invalidates the canvas 88 returned by beginRecording/getRecordingCanvas, and prepares the picture 89 for drawing. Note: this happens implicitly the first time the picture 90 is drawn. 91 */ 92 void endRecording(); 93 94 /** Returns true if any draw commands have been recorded since the last 95 call to beginRecording. 96 */ 97 bool hasRecorded() const; 98 99 /** Returns true if a snapshot of the specified bitmap will be flattened 100 whaen a draw operation using the bitmap is recorded. 101 */ 102 bool willFlattenPixelsOnRecord(const SkBitmap&) const; 103 104 /** Replays the drawing commands on the specified canvas. This internally 105 calls endRecording() if that has not already been called. 106 @param surface the canvas receiving the drawing commands. 107 */ 108 void draw(SkCanvas* surface); 109 110 /** Return the width of the picture's recording canvas. This 111 value reflects what was passed to setSize(), and does not necessarily 112 reflect the bounds of what has been recorded into the picture. 113 @return the width of the picture's recording canvas 114 */ 115 int width() const { return fWidth; } 116 117 /** Return the height of the picture's recording canvas. This 118 value reflects what was passed to setSize(), and does not necessarily 119 reflect the bounds of what has been recorded into the picture. 120 @return the height of the picture's recording canvas 121 */ 122 int height() const { return fHeight; } 123 124 void serialize(SkWStream*) const; 125 126 /** Signals that the caller is prematurely done replaying the drawing 127 commands. This can be called from a canvas virtual while the picture 128 is drawing. Has no effect if the picture is not drawing. 129 */ 130 void abortPlayback(); 131 132private: 133 int fWidth, fHeight; 134 SkPictureRecord* fRecord; 135 SkPicturePlayback* fPlayback; 136 137 friend class SkFlatPicture; 138 friend class SkPicturePlayback; 139 140 typedef SkRefCnt INHERITED; 141}; 142 143class SkAutoPictureRecord : SkNoncopyable { 144public: 145 SkAutoPictureRecord(SkPicture* pict, int width, int height, 146 uint32_t recordingFlags = 0) { 147 fPicture = pict; 148 fCanvas = pict->beginRecording(width, height, recordingFlags); 149 } 150 ~SkAutoPictureRecord() { 151 fPicture->endRecording(); 152 } 153 154 /** Return the canvas to draw into for recording into the picture. 155 */ 156 SkCanvas* getRecordingCanvas() const { return fCanvas; } 157 158private: 159 SkPicture* fPicture; 160 SkCanvas* fCanvas; 161}; 162 163 164#endif 165