Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2007 The Android Open Source Project
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkPicture_DEFINED
      9 #define SkPicture_DEFINED
     10 
     11 #include "SkRefCnt.h"
     12 #include "SkRect.h"
     13 #include "SkTypes.h"
     14 
     15 class SkBigPicture;
     16 class SkCanvas;
     17 class SkData;
     18 struct SkDeserialProcs;
     19 class SkImage;
     20 class SkPictureData;
     21 class SkReadBuffer;
     22 class SkRefCntSet;
     23 struct SkSerialProcs;
     24 class SkStream;
     25 class SkTypefacePlayback;
     26 class SkWStream;
     27 class SkWriteBuffer;
     28 struct SkPictInfo;
     29 
     30 /** \class SkPicture
     31 
     32     An SkPicture records drawing commands made to a canvas to be played back at a later time.
     33     This base class handles serialization and a few other miscellany.
     34 */
     35 class SK_API SkPicture : public SkRefCnt {
     36 public:
     37     /**
     38      *  Recreate a picture that was serialized into a stream or data.
     39      */
     40 
     41     static sk_sp<SkPicture> MakeFromStream(SkStream*, const SkDeserialProcs* = nullptr);
     42     static sk_sp<SkPicture> MakeFromData(const SkData* data, const SkDeserialProcs* = nullptr);
     43     static sk_sp<SkPicture> MakeFromData(const void* data, size_t size,
     44                                          const SkDeserialProcs* = nullptr);
     45 
     46     /**
     47      *  Recreate a picture that was serialized into a buffer. If the creation requires bitmap
     48      *  decoding, the decoder must be set on the SkReadBuffer parameter by calling
     49      *  SkReadBuffer::setBitmapDecoder() before calling SkPicture::MakeFromBuffer().
     50      *  @param SkReadBuffer Serialized picture data.
     51      *  @return A new SkPicture representing the serialized data, or NULL if the buffer is
     52      *          invalid.
     53      */
     54     static sk_sp<SkPicture> MakeFromBuffer(SkReadBuffer&);
     55 
     56     /**
     57     *  Subclasses of this can be passed to playback(). During the playback
     58     *  of the picture, this callback will periodically be invoked. If its
     59     *  abort() returns true, then picture playback will be interrupted.
     60     *
     61     *  The resulting drawing is undefined, as there is no guarantee how often the
     62     *  callback will be invoked. If the abort happens inside some level of nested
     63     *  calls to save(), restore will automatically be called to return the state
     64     *  to the same level it was before the playback call was made.
     65     */
     66     class SK_API AbortCallback {
     67     public:
     68         AbortCallback() {}
     69         virtual ~AbortCallback() {}
     70         virtual bool abort() = 0;
     71     };
     72 
     73     /** Replays the drawing commands on the specified canvas. Note that
     74         this has the effect of unfurling this picture into the destination
     75         canvas. Using the SkCanvas::drawPicture entry point gives the destination
     76         canvas the option of just taking a ref.
     77         @param canvas the canvas receiving the drawing commands.
     78         @param callback a callback that allows interruption of playback
     79     */
     80     virtual void playback(SkCanvas*, AbortCallback* = nullptr) const = 0;
     81 
     82     /** Return a cull rect for this picture.
     83         Ops recorded into this picture that attempt to draw outside the cull might not be drawn.
     84      */
     85     virtual SkRect cullRect() const = 0;
     86 
     87     /** Returns a non-zero value unique among all pictures. */
     88     uint32_t uniqueID() const;
     89 
     90     sk_sp<SkData> serialize(const SkSerialProcs* = nullptr) const;
     91     void serialize(SkWStream*, const SkSerialProcs* = nullptr) const;
     92 
     93     /**
     94      * Return a placeholder SkPicture.
     95      * This placeholder does not draw anything itself.  It has a distinct uniqueID()
     96      * (just like all SkPictures) and will always be visible to SkSerialProcs.
     97      * @param cull the placeholder's dimensions
     98      */
     99     static sk_sp<SkPicture> MakePlaceholder(SkRect cull);
    100 
    101     /**
    102      *  Serialize to a buffer.
    103      */
    104     void flatten(SkWriteBuffer&) const;
    105 
    106     /** Return the approximate number of operations in this picture.  This
    107      *  number may be greater or less than the number of SkCanvas calls
    108      *  recorded: some calls may be recorded as more than one operation, or some
    109      *  calls may be optimized away.
    110      */
    111     virtual int approximateOpCount() const = 0;
    112 
    113     /** Returns the approximate byte size of this picture, not including large ref'd objects. */
    114     virtual size_t approximateBytesUsed() const = 0;
    115 
    116     // Returns NULL if this is not an SkBigPicture.
    117     virtual const SkBigPicture* asSkBigPicture() const { return nullptr; }
    118 
    119 private:
    120     // Subclass whitelist.
    121     SkPicture();
    122     friend class SkBigPicture;
    123     friend class SkEmptyPicture;
    124     template <typename> friend class SkMiniPicture;
    125 
    126     void serialize(SkWStream*, const SkSerialProcs*, SkRefCntSet* typefaces) const;
    127     static sk_sp<SkPicture> MakeFromStream(SkStream*, const SkDeserialProcs*, SkTypefacePlayback*);
    128     friend class SkPictureData;
    129 
    130     /** Return true if the SkStream/Buffer represents a serialized picture, and
    131      fills out SkPictInfo. After this function returns, the data source is not
    132      rewound so it will have to be manually reset before passing to
    133      MakeFromStream or MakeFromBuffer. Note, MakeFromStream and
    134      MakeFromBuffer perform this check internally so these entry points are
    135      intended for stand alone tools.
    136      If false is returned, SkPictInfo is unmodified.
    137      */
    138     static bool StreamIsSKP(SkStream*, SkPictInfo*);
    139     static bool BufferIsSKP(SkReadBuffer*, SkPictInfo*);
    140     friend bool SkPicture_StreamIsSKP(SkStream*, SkPictInfo*);
    141 
    142     friend struct SkPathCounter;
    143 
    144     // V35: Store SkRect (rather then width & height) in header
    145     // V36: Remove (obsolete) alphatype from SkColorTable
    146     // V37: Added shadow only option to SkDropShadowImageFilter (last version to record CLEAR)
    147     // V38: Added PictureResolution option to SkPictureImageFilter
    148     // V39: Added FilterLevel option to SkPictureImageFilter
    149     // V40: Remove UniqueID serialization from SkImageFilter.
    150     // V41: Added serialization of SkBitmapSource's filterQuality parameter
    151     // V42: Added a bool to SkPictureShader serialization to indicate did-we-serialize-a-picture?
    152     // V43: Added DRAW_IMAGE and DRAW_IMAGE_RECT opt codes to serialized data
    153     // V44: Move annotations from paint to drawAnnotation
    154     // V45: Add invNormRotation to SkLightingShader.
    155     // V46: Add drawTextRSXform
    156     // V47: Add occluder rect to SkBlurMaskFilter
    157     // V48: Read and write extended SkTextBlobs.
    158     // V49: Gradients serialized as SkColor4f + SkColorSpace
    159     // V50: SkXfermode -> SkBlendMode
    160     // V51: more SkXfermode -> SkBlendMode
    161     // V52: Remove SkTextBlob::fRunCount
    162     // V53: SaveLayerRec clip mask
    163     // V54: ComposeShader can use a Mode or a Lerp
    164     // V55: Drop blendmode[] from MergeImageFilter
    165     // V56: Add TileMode in SkBlurImageFilter.
    166     // V57: Sweep tiling info.
    167     // V58: No more 2pt conical flipping.
    168     // V59: No more LocalSpace option on PictureImageFilter
    169     // V60: Remove flags in picture header
    170     // V61: Change SkDrawPictureRec to take two colors rather than two alphas
    171 
    172     // Only SKPs within the min/current picture version range (inclusive) can be read.
    173     static const uint32_t     MIN_PICTURE_VERSION = 56;     // august 2017
    174     static const uint32_t CURRENT_PICTURE_VERSION = 61;
    175 
    176     static bool IsValidPictInfo(const SkPictInfo& info);
    177     static sk_sp<SkPicture> Forwardport(const SkPictInfo&,
    178                                         const SkPictureData*,
    179                                         SkReadBuffer* buffer);
    180 
    181     SkPictInfo createHeader() const;
    182     SkPictureData* backport() const;
    183 
    184     mutable uint32_t fUniqueID;
    185 };
    186 
    187 #endif
    188