Home | History | Annotate | Download | only in core
      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 
     15 class SkCanvas;
     16 class SkPicturePlayback;
     17 class SkPictureRecord;
     18 class SkStream;
     19 class SkWStream;
     20 
     21 /** \class SkPicture
     22 
     23     The SkPicture class records the drawing commands made to a canvas, to
     24     be played back at a later time.
     25 */
     26 class SK_API SkPicture : public SkRefCnt {
     27 public:
     28     /** The constructor prepares the picture to record.
     29         @param width the width of the virtual device the picture records.
     30         @param height the height of the virtual device the picture records.
     31     */
     32     SkPicture();
     33     /** Make a copy of the contents of src. If src records more drawing after
     34         this call, those elements will not appear in this picture.
     35     */
     36     SkPicture(const SkPicture& src);
     37     explicit SkPicture(SkStream*);
     38     virtual ~SkPicture();
     39 
     40     /**
     41      *  Swap the contents of the two pictures. Guaranteed to succeed.
     42      */
     43     void swap(SkPicture& other);
     44 
     45     enum RecordingFlags {
     46         /*  This flag specifies that when clipPath() is called, the path will
     47             be faithfully recorded, but the recording canvas' current clip will
     48             only see the path's bounds. This speeds up the recording process
     49             without compromising the fidelity of the playback. The only side-
     50             effect for recording is that calling getTotalClip() or related
     51             clip-query calls will reflect the path's bounds, not the actual
     52             path.
     53          */
     54         kUsePathBoundsForClip_RecordingFlag = 0x01
     55     };
     56 
     57     /** Returns the canvas that records the drawing commands.
     58         @param width the base width for the picture, as if the recording
     59                      canvas' bitmap had this width.
     60         @param height the base width for the picture, as if the recording
     61                      canvas' bitmap had this height.
     62         @param recordFlags optional flags that control recording.
     63         @return the picture canvas.
     64     */
     65     SkCanvas* beginRecording(int width, int height, uint32_t recordFlags = 0);
     66 
     67     /** Returns the recording canvas if one is active, or NULL if recording is
     68         not active. This does not alter the refcnt on the canvas (if present).
     69     */
     70     SkCanvas* getRecordingCanvas() const;
     71     /** Signal that the caller is done recording. This invalidates the canvas
     72         returned by beginRecording/getRecordingCanvas, and prepares the picture
     73         for drawing. Note: this happens implicitly the first time the picture
     74         is drawn.
     75     */
     76     void endRecording();
     77 
     78     /** Replays the drawing commands on the specified canvas. This internally
     79         calls endRecording() if that has not already been called.
     80         @param surface the canvas receiving the drawing commands.
     81     */
     82     void draw(SkCanvas* surface);
     83 
     84     /** Return the width of the picture's recording canvas. This
     85         value reflects what was passed to setSize(), and does not necessarily
     86         reflect the bounds of what has been recorded into the picture.
     87         @return the width of the picture's recording canvas
     88     */
     89     int width() const { return fWidth; }
     90 
     91     /** Return the height 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 height of the picture's recording canvas
     95     */
     96     int height() const { return fHeight; }
     97 
     98     void serialize(SkWStream*) const;
     99 
    100     /** Signals that the caller is prematurely done replaying the drawing
    101         commands. This can be called from a canvas virtual while the picture
    102         is drawing. Has no effect if the picture is not drawing.
    103     */
    104     void abortPlayback();
    105 
    106 private:
    107     int fWidth, fHeight;
    108     SkPictureRecord* fRecord;
    109     SkPicturePlayback* fPlayback;
    110 
    111     friend class SkFlatPicture;
    112     friend class SkPicturePlayback;
    113 };
    114 
    115 class SkAutoPictureRecord : SkNoncopyable {
    116 public:
    117     SkAutoPictureRecord(SkPicture* pict, int width, int height,
    118                         uint32_t recordingFlags = 0) {
    119         fPicture = pict;
    120         fCanvas = pict->beginRecording(width, height, recordingFlags);
    121     }
    122     ~SkAutoPictureRecord() {
    123         fPicture->endRecording();
    124     }
    125 
    126     /** Return the canvas to draw into for recording into the picture.
    127     */
    128     SkCanvas* getRecordingCanvas() const { return fCanvas; }
    129 
    130 private:
    131     SkPicture*  fPicture;
    132     SkCanvas*   fCanvas;
    133 };
    134 
    135 
    136 #endif
    137