Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2006 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 SkCanvas_DEFINED
      9 #define SkCanvas_DEFINED
     10 
     11 #include "SkTypes.h"
     12 #include "SkBitmap.h"
     13 #include "SkDeque.h"
     14 #include "SkClipStack.h"
     15 #include "SkPaint.h"
     16 #include "SkRefCnt.h"
     17 #include "SkPath.h"
     18 #include "SkRegion.h"
     19 #include "SkXfermode.h"
     20 
     21 #ifdef SK_SUPPORT_LEGACY_DRAWTEXT_VIRTUAL
     22     #define SK_LEGACY_DRAWTEXT_VIRTUAL  virtual
     23 #else
     24     #define SK_LEGACY_DRAWTEXT_VIRTUAL
     25 #endif
     26 
     27 class SkCanvasClipVisitor;
     28 class SkBaseDevice;
     29 class SkDraw;
     30 class SkDrawFilter;
     31 class SkMetaData;
     32 class SkPicture;
     33 class SkRRect;
     34 class SkSurface;
     35 class SkSurface_Base;
     36 class GrContext;
     37 class GrRenderTarget;
     38 
     39 /** \class SkCanvas
     40 
     41     A Canvas encapsulates all of the state about drawing into a device (bitmap).
     42     This includes a reference to the device itself, and a stack of matrix/clip
     43     values. For any given draw call (e.g. drawRect), the geometry of the object
     44     being drawn is transformed by the concatenation of all the matrices in the
     45     stack. The transformed geometry is clipped by the intersection of all of
     46     the clips in the stack.
     47 
     48     While the Canvas holds the state of the drawing device, the state (style)
     49     of the object being drawn is held by the Paint, which is provided as a
     50     parameter to each of the draw() methods. The Paint holds attributes such as
     51     color, typeface, textSize, strokeWidth, shader (e.g. gradients, patterns),
     52     etc.
     53 */
     54 class SK_API SkCanvas : public SkRefCnt {
     55 public:
     56     SK_DECLARE_INST_COUNT(SkCanvas)
     57 
     58     /**
     59      *  Attempt to allocate an offscreen raster canvas, matching the ImageInfo.
     60      *  On success, return a new canvas that will draw into that offscreen.
     61      *
     62      *  The caller can access the pixels after drawing into this canvas by
     63      *  calling readPixels() or peekPixels().
     64      *
     65      *  If the requested ImageInfo is opaque (either the colortype is
     66      *  intrinsically opaque like RGB_565, or the info's alphatype is kOpaque)
     67      *  then the pixel memory may be uninitialized. Otherwise, the pixel memory
     68      *  will be initialized to 0, which is interpreted as transparent.
     69      *
     70      *  On failure, return NULL. This can fail for several reasons:
     71      *  1. the memory allocation failed (e.g. request is too large)
     72      *  2. invalid ImageInfo (e.g. negative dimensions)
     73      *  3. unsupported ImageInfo for a canvas
     74      *      - kUnknown_SkColorType, kIndex_8_SkColorType
     75      *      - kIgnore_SkAlphaType
     76      *      - this list is not complete, so others may also be unsupported
     77      *
     78      *  Note: it is valid to request a supported ImageInfo, but with zero
     79      *  dimensions.
     80      */
     81     static SkCanvas* NewRaster(const SkImageInfo&);
     82 
     83     static SkCanvas* NewRasterN32(int width, int height) {
     84         return NewRaster(SkImageInfo::MakeN32Premul(width, height));
     85     }
     86 
     87     /**
     88      *  Attempt to allocate raster canvas, matching the ImageInfo, that will draw directly into the
     89      *  specified pixels. To access the pixels after drawing to them, the caller should call
     90      *  flush() or call peekPixels(...).
     91      *
     92      *  On failure, return NULL. This can fail for several reasons:
     93      *  1. invalid ImageInfo (e.g. negative dimensions)
     94      *  2. unsupported ImageInfo for a canvas
     95      *      - kUnknown_SkColorType, kIndex_8_SkColorType
     96      *      - kIgnore_SkAlphaType
     97      *      - this list is not complete, so others may also be unsupported
     98      *
     99      *  Note: it is valid to request a supported ImageInfo, but with zero
    100      *  dimensions.
    101      */
    102     static SkCanvas* NewRasterDirect(const SkImageInfo&, void*, size_t);
    103 
    104     static SkCanvas* NewRasterDirectN32(int width, int height, SkPMColor* pixels, size_t rowBytes) {
    105         return NewRasterDirect(SkImageInfo::MakeN32Premul(width, height), pixels, rowBytes);
    106     }
    107 
    108     /**
    109      *  Creates an empty canvas with no backing device/pixels, and zero
    110      *  dimensions.
    111      */
    112     SkCanvas();
    113 
    114     /**
    115      *  Creates a canvas of the specified dimensions, but explicitly not backed
    116      *  by any device/pixels. Typically this use used by subclasses who handle
    117      *  the draw calls in some other way.
    118      */
    119     SkCanvas(int width, int height);
    120 
    121     /** Construct a canvas with the specified device to draw into.
    122 
    123         @param device   Specifies a device for the canvas to draw into.
    124     */
    125     explicit SkCanvas(SkBaseDevice* device);
    126 
    127     /** Construct a canvas with the specified bitmap to draw into.
    128         @param bitmap   Specifies a bitmap for the canvas to draw into. Its
    129                         structure are copied to the canvas.
    130     */
    131     explicit SkCanvas(const SkBitmap& bitmap);
    132     virtual ~SkCanvas();
    133 
    134     SkMetaData& getMetaData();
    135 
    136     /**
    137      *  Return ImageInfo for this canvas. If the canvas is not backed by pixels
    138      *  (cpu or gpu), then the info's ColorType will be kUnknown_SkColorType.
    139      */
    140     SkImageInfo imageInfo() const;
    141 
    142     ///////////////////////////////////////////////////////////////////////////
    143 
    144     /**
    145      *  Trigger the immediate execution of all pending draw operations.
    146      */
    147     void flush();
    148 
    149     /**
    150      * Gets the size of the base or root layer in global canvas coordinates. The
    151      * origin of the base layer is always (0,0). The current drawable area may be
    152      * smaller (due to clipping or saveLayer).
    153      */
    154     SkISize getBaseLayerSize() const;
    155 
    156     /**
    157      *  DEPRECATED: call getBaseLayerSize
    158      */
    159     SkISize getDeviceSize() const { return this->getBaseLayerSize(); }
    160 
    161     /**
    162      *  DEPRECATED.
    163      *  Return the canvas' device object, which may be null. The device holds
    164      *  the bitmap of the pixels that the canvas draws into. The reference count
    165      *  of the returned device is not changed by this call.
    166      */
    167     SkBaseDevice* getDevice() const;
    168 
    169     /**
    170      *  saveLayer() can create another device (which is later drawn onto
    171      *  the previous device). getTopDevice() returns the top-most device current
    172      *  installed. Note that this can change on other calls like save/restore,
    173      *  so do not access this device after subsequent canvas calls.
    174      *  The reference count of the device is not changed.
    175      *
    176      * @param updateMatrixClip If this is true, then before the device is
    177      *        returned, we ensure that its has been notified about the current
    178      *        matrix and clip. Note: this happens automatically when the device
    179      *        is drawn to, but is optional here, as there is a small perf hit
    180      *        sometimes.
    181      */
    182 #ifndef SK_SUPPORT_LEGACY_GETTOPDEVICE
    183 private:
    184 #endif
    185     SkBaseDevice* getTopDevice(bool updateMatrixClip = false) const;
    186 public:
    187 
    188     /**
    189      *  Create a new surface matching the specified info, one that attempts to
    190      *  be maximally compatible when used with this canvas. If there is no matching Surface type,
    191      *  NULL is returned.
    192      */
    193     SkSurface* newSurface(const SkImageInfo&);
    194 
    195     /**
    196      * Return the GPU context of the device that is associated with the canvas.
    197      * For a canvas with non-GPU device, NULL is returned.
    198      */
    199     GrContext* getGrContext();
    200 
    201     ///////////////////////////////////////////////////////////////////////////
    202 
    203     /**
    204      *  If the canvas has writable pixels in its top layer (and is not recording to a picture
    205      *  or other non-raster target) and has direct access to its pixels (i.e. they are in
    206      *  local RAM) return the address of those pixels, and if not null,
    207      *  return the ImageInfo, rowBytes and origin. The returned address is only valid
    208      *  while the canvas object is in scope and unchanged. Any API calls made on
    209      *  canvas (or its parent surface if any) will invalidate the
    210      *  returned address (and associated information).
    211      *
    212      *  On failure, returns NULL and the info, rowBytes, and origin parameters are ignored.
    213      */
    214     void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = NULL);
    215 
    216     /**
    217      *  If the canvas has readable pixels in its base layer (and is not recording to a picture
    218      *  or other non-raster target) and has direct access to its pixels (i.e. they are in
    219      *  local RAM) return the const-address of those pixels, and if not null,
    220      *  return the ImageInfo and rowBytes. The returned address is only valid
    221      *  while the canvas object is in scope and unchanged. Any API calls made on
    222      *  canvas (or its parent surface if any) will invalidate the
    223      *  returned address (and associated information).
    224      *
    225      *  On failure, returns NULL and the info and rowBytes parameters are
    226      *  ignored.
    227      */
    228     const void* peekPixels(SkImageInfo* info, size_t* rowBytes);
    229 
    230     /**
    231      *  Copy the pixels from the base-layer into the specified buffer (pixels + rowBytes),
    232      *  converting them into the requested format (SkImageInfo). The base-layer pixels are read
    233      *  starting at the specified (srcX,srcY) location in the coordinate system of the base-layer.
    234      *
    235      *  The specified ImageInfo and (srcX,srcY) offset specifies a source rectangle
    236      *
    237      *      srcR.setXYWH(srcX, srcY, dstInfo.width(), dstInfo.height());
    238      *
    239      *  srcR is intersected with the bounds of the base-layer. If this intersection is not empty,
    240      *  then we have two sets of pixels (of equal size). Replace the dst pixels with the
    241      *  corresponding src pixels, performing any colortype/alphatype transformations needed
    242      *  (in the case where the src and dst have different colortypes or alphatypes).
    243      *
    244      *  This call can fail, returning false, for several reasons:
    245      *  - If srcR does not intersect the base-layer bounds.
    246      *  - If the requested colortype/alphatype cannot be converted from the base-layer's types.
    247      *  - If this canvas is not backed by pixels (e.g. picture or PDF)
    248      */
    249     bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
    250                     int srcX, int srcY);
    251 
    252     /**
    253      *  Helper for calling readPixels(info, ...). This call will check if bitmap has been allocated.
    254      *  If not, it will attempt to call allocPixels(). If this fails, it will return false. If not,
    255      *  it calls through to readPixels(info, ...) and returns its result.
    256      */
    257     bool readPixels(SkBitmap* bitmap, int srcX, int srcY);
    258 
    259     /**
    260      *  Helper for allocating pixels and then calling readPixels(info, ...). The bitmap is resized
    261      *  to the intersection of srcRect and the base-layer bounds. On success, pixels will be
    262      *  allocated in bitmap and true returned. On failure, false is returned and bitmap will be
    263      *  set to empty.
    264      */
    265     bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
    266 
    267     /**
    268      *  This method affects the pixels in the base-layer, and operates in pixel coordinates,
    269      *  ignoring the matrix and clip.
    270      *
    271      *  The specified ImageInfo and (x,y) offset specifies a rectangle: target.
    272      *
    273      *      target.setXYWH(x, y, info.width(), info.height());
    274      *
    275      *  Target is intersected with the bounds of the base-layer. If this intersection is not empty,
    276      *  then we have two sets of pixels (of equal size), the "src" specified by info+pixels+rowBytes
    277      *  and the "dst" by the canvas' backend. Replace the dst pixels with the corresponding src
    278      *  pixels, performing any colortype/alphatype transformations needed (in the case where the
    279      *  src and dst have different colortypes or alphatypes).
    280      *
    281      *  This call can fail, returning false, for several reasons:
    282      *  - If the src colortype/alphatype cannot be converted to the canvas' types
    283      *  - If this canvas is not backed by pixels (e.g. picture or PDF)
    284      */
    285     bool writePixels(const SkImageInfo&, const void* pixels, size_t rowBytes, int x, int y);
    286 
    287     /**
    288      *  Helper for calling writePixels(info, ...) by passing its pixels and rowbytes. If the bitmap
    289      *  is just wrapping a texture, returns false and does nothing.
    290      */
    291     bool writePixels(const SkBitmap& bitmap, int x, int y);
    292 
    293     ///////////////////////////////////////////////////////////////////////////
    294 
    295     enum SaveFlags {
    296         /** save the matrix state, restoring it on restore() */
    297         kMatrix_SaveFlag            = 0x01,
    298         /** save the clip state, restoring it on restore() */
    299         kClip_SaveFlag              = 0x02,
    300         /** the layer needs to support per-pixel alpha */
    301         kHasAlphaLayer_SaveFlag     = 0x04,
    302         /** the layer needs to support 8-bits per color component */
    303         kFullColorLayer_SaveFlag    = 0x08,
    304         /**
    305          *  the layer should clip against the bounds argument
    306          *
    307          *  if SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG is undefined, this is treated as always on.
    308          */
    309         kClipToLayer_SaveFlag       = 0x10,
    310 
    311         // helper masks for common choices
    312         kMatrixClip_SaveFlag        = 0x03,
    313 #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
    314         kARGB_NoClipLayer_SaveFlag  = 0x0F,
    315 #endif
    316         kARGB_ClipLayer_SaveFlag    = 0x1F
    317     };
    318 
    319     /** This call saves the current matrix, clip, and drawFilter, and pushes a
    320         copy onto a private stack. Subsequent calls to translate, scale,
    321         rotate, skew, concat or clipRect, clipPath, and setDrawFilter all
    322         operate on this copy.
    323         When the balancing call to restore() is made, the previous matrix, clip,
    324         and drawFilter are restored.
    325 
    326         @return The value to pass to restoreToCount() to balance this save()
    327     */
    328     int save();
    329 
    330     /** DEPRECATED - use save() instead.
    331 
    332         This behaves the same as save(), but it allows fine-grained control of
    333         which state bits to be saved (and subsequently restored).
    334 
    335         @param flags The flags govern what portion of the Matrix/Clip/drawFilter
    336                      state the save (and matching restore) effect. For example,
    337                      if only kMatrix is specified, then only the matrix state
    338                      will be pushed and popped. Likewise for the clip if kClip
    339                      is specified.  However, the drawFilter is always affected
    340                      by calls to save/restore.
    341         @return The value to pass to restoreToCount() to balance this save()
    342     */
    343     SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated")
    344     int save(SaveFlags flags);
    345 
    346     /** This behaves the same as save(), but in addition it allocates an
    347         offscreen bitmap. All drawing calls are directed there, and only when
    348         the balancing call to restore() is made is that offscreen transfered to
    349         the canvas (or the previous layer).
    350         @param bounds (may be null) This rect, if non-null, is used as a hint to
    351                       limit the size of the offscreen, and thus drawing may be
    352                       clipped to it, though that clipping is not guaranteed to
    353                       happen. If exact clipping is desired, use clipRect().
    354         @param paint (may be null) This is copied, and is applied to the
    355                      offscreen when restore() is called
    356         @return The value to pass to restoreToCount() to balance this save()
    357     */
    358     int saveLayer(const SkRect* bounds, const SkPaint* paint);
    359 
    360     /** DEPRECATED - use saveLayer(const SkRect*, const SkPaint*) instead.
    361 
    362         This behaves the same as saveLayer(const SkRect*, const SkPaint*),
    363         but it allows fine-grained control of which state bits to be saved
    364         (and subsequently restored).
    365 
    366         @param bounds (may be null) This rect, if non-null, is used as a hint to
    367                       limit the size of the offscreen, and thus drawing may be
    368                       clipped to it, though that clipping is not guaranteed to
    369                       happen. If exact clipping is desired, use clipRect().
    370         @param paint (may be null) This is copied, and is applied to the
    371                      offscreen when restore() is called
    372         @param flags  LayerFlags
    373         @return The value to pass to restoreToCount() to balance this save()
    374     */
    375     SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated")
    376     int saveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
    377 
    378     /** This behaves the same as save(), but in addition it allocates an
    379         offscreen bitmap. All drawing calls are directed there, and only when
    380         the balancing call to restore() is made is that offscreen transfered to
    381         the canvas (or the previous layer).
    382         @param bounds (may be null) This rect, if non-null, is used as a hint to
    383                       limit the size of the offscreen, and thus drawing may be
    384                       clipped to it, though that clipping is not guaranteed to
    385                       happen. If exact clipping is desired, use clipRect().
    386         @param alpha  This is applied to the offscreen when restore() is called.
    387         @return The value to pass to restoreToCount() to balance this save()
    388     */
    389     int saveLayerAlpha(const SkRect* bounds, U8CPU alpha);
    390 
    391     /** DEPRECATED - use saveLayerAlpha(const SkRect*, U8CPU) instead.
    392 
    393         This behaves the same as saveLayerAlpha(const SkRect*, U8CPU),
    394         but it allows fine-grained control of which state bits to be saved
    395         (and subsequently restored).
    396 
    397         @param bounds (may be null) This rect, if non-null, is used as a hint to
    398                       limit the size of the offscreen, and thus drawing may be
    399                       clipped to it, though that clipping is not guaranteed to
    400                       happen. If exact clipping is desired, use clipRect().
    401         @param alpha  This is applied to the offscreen when restore() is called.
    402         @param flags  LayerFlags
    403         @return The value to pass to restoreToCount() to balance this save()
    404     */
    405     SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated")
    406     int saveLayerAlpha(const SkRect* bounds, U8CPU alpha, SaveFlags flags);
    407 
    408     /** This call balances a previous call to save(), and is used to remove all
    409         modifications to the matrix/clip/drawFilter state since the last save
    410         call.
    411         It is an error to call restore() more times than save() was called.
    412     */
    413     void restore();
    414 
    415     /** Returns the number of matrix/clip states on the SkCanvas' private stack.
    416         This will equal # save() calls - # restore() calls + 1. The save count on
    417         a new canvas is 1.
    418     */
    419     int getSaveCount() const;
    420 
    421     /** Efficient way to pop any calls to save() that happened after the save
    422         count reached saveCount. It is an error for saveCount to be greater than
    423         getSaveCount(). To pop all the way back to the initial matrix/clip context
    424         pass saveCount == 1.
    425         @param saveCount    The number of save() levels to restore from
    426     */
    427     void restoreToCount(int saveCount);
    428 
    429     /** Returns true if drawing is currently going to a layer (from saveLayer)
    430      *  rather than to the root device.
    431      */
    432     virtual bool isDrawingToLayer() const;
    433 
    434     /** Preconcat the current matrix with the specified translation
    435         @param dx   The distance to translate in X
    436         @param dy   The distance to translate in Y
    437     */
    438     void translate(SkScalar dx, SkScalar dy);
    439 
    440     /** Preconcat the current matrix with the specified scale.
    441         @param sx   The amount to scale in X
    442         @param sy   The amount to scale in Y
    443     */
    444     void scale(SkScalar sx, SkScalar sy);
    445 
    446     /** Preconcat the current matrix with the specified rotation.
    447         @param degrees  The amount to rotate, in degrees
    448     */
    449     void rotate(SkScalar degrees);
    450 
    451     /** Preconcat the current matrix with the specified skew.
    452         @param sx   The amount to skew in X
    453         @param sy   The amount to skew in Y
    454     */
    455     void skew(SkScalar sx, SkScalar sy);
    456 
    457     /** Preconcat the current matrix with the specified matrix.
    458         @param matrix   The matrix to preconcatenate with the current matrix
    459     */
    460     void concat(const SkMatrix& matrix);
    461 
    462     /** Replace the current matrix with a copy of the specified matrix.
    463         @param matrix The matrix that will be copied into the current matrix.
    464     */
    465     void setMatrix(const SkMatrix& matrix);
    466 
    467     /** Helper for setMatrix(identity). Sets the current matrix to identity.
    468     */
    469     void resetMatrix();
    470 
    471     /**
    472      *  Modify the current clip with the specified rectangle.
    473      *  @param rect The rect to combine with the current clip
    474      *  @param op The region op to apply to the current clip
    475      *  @param doAntiAlias true if the clip should be antialiased
    476      */
    477     void clipRect(const SkRect& rect,
    478                   SkRegion::Op op = SkRegion::kIntersect_Op,
    479                   bool doAntiAlias = false);
    480 
    481     /**
    482      *  Modify the current clip with the specified SkRRect.
    483      *  @param rrect The rrect to combine with the current clip
    484      *  @param op The region op to apply to the current clip
    485      *  @param doAntiAlias true if the clip should be antialiased
    486      */
    487     void clipRRect(const SkRRect& rrect,
    488                    SkRegion::Op op = SkRegion::kIntersect_Op,
    489                    bool doAntiAlias = false);
    490 
    491     /**
    492      *  Modify the current clip with the specified path.
    493      *  @param path The path to combine with the current clip
    494      *  @param op The region op to apply to the current clip
    495      *  @param doAntiAlias true if the clip should be antialiased
    496      */
    497     void clipPath(const SkPath& path,
    498                   SkRegion::Op op = SkRegion::kIntersect_Op,
    499                   bool doAntiAlias = false);
    500 
    501     /** EXPERIMENTAL -- only used for testing
    502         Set to false to force clips to be hard, even if doAntiAlias=true is
    503         passed to clipRect or clipPath.
    504      */
    505     void setAllowSoftClip(bool allow) {
    506         fAllowSoftClip = allow;
    507     }
    508 
    509     /** EXPERIMENTAL -- only used for testing
    510         Set to simplify clip stack using path ops.
    511      */
    512     void setAllowSimplifyClip(bool allow) {
    513         fAllowSimplifyClip = allow;
    514     }
    515 
    516     /** Modify the current clip with the specified region. Note that unlike
    517         clipRect() and clipPath() which transform their arguments by the current
    518         matrix, clipRegion() assumes its argument is already in device
    519         coordinates, and so no transformation is performed.
    520         @param deviceRgn    The region to apply to the current clip
    521         @param op The region op to apply to the current clip
    522     */
    523     void clipRegion(const SkRegion& deviceRgn,
    524                     SkRegion::Op op = SkRegion::kIntersect_Op);
    525 
    526     /** Helper for clipRegion(rgn, kReplace_Op). Sets the current clip to the
    527         specified region. This does not intersect or in any other way account
    528         for the existing clip region.
    529         @param deviceRgn The region to copy into the current clip.
    530     */
    531     void setClipRegion(const SkRegion& deviceRgn) {
    532         this->clipRegion(deviceRgn, SkRegion::kReplace_Op);
    533     }
    534 
    535     /** Return true if the specified rectangle, after being transformed by the
    536         current matrix, would lie completely outside of the current clip. Call
    537         this to check if an area you intend to draw into is clipped out (and
    538         therefore you can skip making the draw calls).
    539         @param rect the rect to compare with the current clip
    540         @return true if the rect (transformed by the canvas' matrix) does not
    541                      intersect with the canvas' clip
    542     */
    543     bool quickReject(const SkRect& rect) const;
    544 
    545     /** Return true if the specified path, after being transformed by the
    546         current matrix, would lie completely outside of the current clip. Call
    547         this to check if an area you intend to draw into is clipped out (and
    548         therefore you can skip making the draw calls). Note, for speed it may
    549         return false even if the path itself might not intersect the clip
    550         (i.e. the bounds of the path intersects, but the path does not).
    551         @param path The path to compare with the current clip
    552         @return true if the path (transformed by the canvas' matrix) does not
    553                      intersect with the canvas' clip
    554     */
    555     bool quickReject(const SkPath& path) const;
    556 
    557     /** Return true if the horizontal band specified by top and bottom is
    558         completely clipped out. This is a conservative calculation, meaning
    559         that it is possible that if the method returns false, the band may still
    560         in fact be clipped out, but the converse is not true. If this method
    561         returns true, then the band is guaranteed to be clipped out.
    562         @param top  The top of the horizontal band to compare with the clip
    563         @param bottom The bottom of the horizontal and to compare with the clip
    564         @return true if the horizontal band is completely clipped out (i.e. does
    565                      not intersect the current clip)
    566     */
    567     bool quickRejectY(SkScalar top, SkScalar bottom) const {
    568         SkASSERT(top <= bottom);
    569 
    570 #ifndef SK_WILL_NEVER_DRAW_PERSPECTIVE_TEXT
    571         // TODO: add a hasPerspective method similar to getLocalClipBounds. This
    572         // would cache the SkMatrix::hasPerspective result. Alternatively, have
    573         // the MC stack just set a hasPerspective boolean as it is updated.
    574         if (this->getTotalMatrix().hasPerspective()) {
    575             // TODO: consider implementing some half-plane test between the
    576             // two Y planes and the device-bounds (i.e., project the top and
    577             // bottom Y planes and then determine if the clip bounds is completely
    578             // outside either one).
    579             return false;
    580         }
    581 #endif
    582 
    583         const SkRect& clipR = this->getLocalClipBounds();
    584         // In the case where the clip is empty and we are provided with a
    585         // negative top and positive bottom parameter then this test will return
    586         // false even though it will be clipped. We have chosen to exclude that
    587         // check as it is rare and would result double the comparisons.
    588         return top >= clipR.fBottom || bottom <= clipR.fTop;
    589     }
    590 
    591     /** Return the bounds of the current clip (in local coordinates) in the
    592         bounds parameter, and return true if it is non-empty. This can be useful
    593         in a way similar to quickReject, in that it tells you that drawing
    594         outside of these bounds will be clipped out.
    595     */
    596     virtual bool getClipBounds(SkRect* bounds) const;
    597 
    598     /** Return the bounds of the current clip, in device coordinates; returns
    599         true if non-empty. Maybe faster than getting the clip explicitly and
    600         then taking its bounds.
    601     */
    602     virtual bool getClipDeviceBounds(SkIRect* bounds) const;
    603 
    604 
    605     /** Fill the entire canvas' bitmap (restricted to the current clip) with the
    606         specified ARGB color, using the specified mode.
    607         @param a    the alpha component (0..255) of the color to fill the canvas
    608         @param r    the red component (0..255) of the color to fill the canvas
    609         @param g    the green component (0..255) of the color to fill the canvas
    610         @param b    the blue component (0..255) of the color to fill the canvas
    611         @param mode the mode to apply the color in (defaults to SrcOver)
    612     */
    613     void drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b,
    614                   SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode);
    615 
    616     /** Fill the entire canvas' bitmap (restricted to the current clip) with the
    617         specified color and mode.
    618         @param color    the color to draw with
    619         @param mode the mode to apply the color in (defaults to SrcOver)
    620     */
    621     void drawColor(SkColor color,
    622                    SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode);
    623 
    624     /**
    625      *  This erases the entire drawing surface to the specified color,
    626      *  irrespective of the clip. It does not blend with the previous pixels,
    627      *  but always overwrites them.
    628      *
    629      *  It is roughly equivalent to the following:
    630      *      canvas.save();
    631      *      canvas.clipRect(hugeRect, kReplace_Op);
    632      *      paint.setColor(color);
    633      *      paint.setXfermodeMode(kSrc_Mode);
    634      *      canvas.drawPaint(paint);
    635      *      canvas.restore();
    636      *  though it is almost always much more efficient.
    637      */
    638     virtual void clear(SkColor);
    639 
    640     /**
    641      * This makes the contents of the canvas undefined. Subsequent calls that
    642      * require reading the canvas contents will produce undefined results. Examples
    643      * include blending and readPixels. The actual implementation is backend-
    644      * dependent and one legal implementation is to do nothing. Like clear(), this
    645      * ignores the clip.
    646      *
    647      * This function should only be called if the caller intends to subsequently
    648      * draw to the canvas. The canvas may do real work at discard() time in order
    649      * to optimize performance on subsequent draws. Thus, if you call this and then
    650      * never draw to the canvas subsequently you may pay a perfomance penalty.
    651      */
    652     void discard() { this->onDiscard(); }
    653 
    654     /**
    655      *  Fill the entire canvas' bitmap (restricted to the current clip) with the
    656      *  specified paint.
    657      *  @param paint    The paint used to fill the canvas
    658      */
    659     virtual void drawPaint(const SkPaint& paint);
    660 
    661     enum PointMode {
    662         /** drawPoints draws each point separately */
    663         kPoints_PointMode,
    664         /** drawPoints draws each pair of points as a line segment */
    665         kLines_PointMode,
    666         /** drawPoints draws the array of points as a polygon */
    667         kPolygon_PointMode
    668     };
    669 
    670     /** Draw a series of points, interpreted based on the PointMode mode. For
    671         all modes, the count parameter is interpreted as the total number of
    672         points. For kLine mode, count/2 line segments are drawn.
    673         For kPoint mode, each point is drawn centered at its coordinate, and its
    674         size is specified by the paint's stroke-width. It draws as a square,
    675         unless the paint's cap-type is round, in which the points are drawn as
    676         circles.
    677         For kLine mode, each pair of points is drawn as a line segment,
    678         respecting the paint's settings for cap/join/width.
    679         For kPolygon mode, the entire array is drawn as a series of connected
    680         line segments.
    681         Note that, while similar, kLine and kPolygon modes draw slightly
    682         differently than the equivalent path built with a series of moveto,
    683         lineto calls, in that the path will draw all of its contours at once,
    684         with no interactions if contours intersect each other (think XOR
    685         xfermode). drawPoints always draws each element one at a time.
    686         @param mode     PointMode specifying how to draw the array of points.
    687         @param count    The number of points in the array
    688         @param pts      Array of points to draw
    689         @param paint    The paint used to draw the points
    690     */
    691     virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
    692                             const SkPaint& paint);
    693 
    694     /** Helper method for drawing a single point. See drawPoints() for a more
    695         details.
    696     */
    697     void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint);
    698 
    699     /** Draws a single pixel in the specified color.
    700         @param x        The X coordinate of which pixel to draw
    701         @param y        The Y coordiante of which pixel to draw
    702         @param color    The color to draw
    703     */
    704     void drawPoint(SkScalar x, SkScalar y, SkColor color);
    705 
    706     /** Draw a line segment with the specified start and stop x,y coordinates,
    707         using the specified paint. NOTE: since a line is always "framed", the
    708         paint's Style is ignored.
    709         @param x0    The x-coordinate of the start point of the line
    710         @param y0    The y-coordinate of the start point of the line
    711         @param x1    The x-coordinate of the end point of the line
    712         @param y1    The y-coordinate of the end point of the line
    713         @param paint The paint used to draw the line
    714     */
    715     void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1,
    716                   const SkPaint& paint);
    717 
    718     /** Draw the specified rectangle using the specified paint. The rectangle
    719         will be filled or stroked based on the Style in the paint.
    720         @param rect     The rect to be drawn
    721         @param paint    The paint used to draw the rect
    722     */
    723     virtual void drawRect(const SkRect& rect, const SkPaint& paint);
    724 
    725     /** Draw the specified rectangle using the specified paint. The rectangle
    726         will be filled or framed based on the Style in the paint.
    727         @param rect     The rect to be drawn
    728         @param paint    The paint used to draw the rect
    729     */
    730     void drawIRect(const SkIRect& rect, const SkPaint& paint) {
    731         SkRect r;
    732         r.set(rect);    // promotes the ints to scalars
    733         this->drawRect(r, paint);
    734     }
    735 
    736     /** Draw the specified rectangle using the specified paint. The rectangle
    737         will be filled or framed based on the Style in the paint.
    738         @param left     The left side of the rectangle to be drawn
    739         @param top      The top side of the rectangle to be drawn
    740         @param right    The right side of the rectangle to be drawn
    741         @param bottom   The bottom side of the rectangle to be drawn
    742         @param paint    The paint used to draw the rect
    743     */
    744     void drawRectCoords(SkScalar left, SkScalar top, SkScalar right,
    745                         SkScalar bottom, const SkPaint& paint);
    746 
    747     /** Draw the specified oval using the specified paint. The oval will be
    748         filled or framed based on the Style in the paint.
    749         @param oval     The rectangle bounds of the oval to be drawn
    750         @param paint    The paint used to draw the oval
    751     */
    752     virtual void drawOval(const SkRect& oval, const SkPaint&);
    753 
    754     /**
    755      *  Draw the specified RRect using the specified paint The rrect will be filled or stroked
    756      *  based on the Style in the paint.
    757      *
    758      *  @param rrect    The round-rect to draw
    759      *  @param paint    The paint used to draw the round-rect
    760      */
    761     virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint);
    762 
    763     /**
    764      *  Draw the annulus formed by the outer and inner rrects. The results
    765      *  are undefined if the outer does not contain the inner.
    766      */
    767     void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint&);
    768 
    769     /** Draw the specified circle using the specified paint. If radius is <= 0,
    770         then nothing will be drawn. The circle will be filled
    771         or framed based on the Style in the paint.
    772         @param cx       The x-coordinate of the center of the cirle to be drawn
    773         @param cy       The y-coordinate of the center of the cirle to be drawn
    774         @param radius   The radius of the cirle to be drawn
    775         @param paint    The paint used to draw the circle
    776     */
    777     void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius,
    778                     const SkPaint& paint);
    779 
    780     /** Draw the specified arc, which will be scaled to fit inside the
    781         specified oval. If the sweep angle is >= 360, then the oval is drawn
    782         completely. Note that this differs slightly from SkPath::arcTo, which
    783         treats the sweep angle mod 360.
    784         @param oval The bounds of oval used to define the shape of the arc
    785         @param startAngle Starting angle (in degrees) where the arc begins
    786         @param sweepAngle Sweep angle (in degrees) measured clockwise
    787         @param useCenter true means include the center of the oval. For filling
    788                          this will draw a wedge. False means just use the arc.
    789         @param paint    The paint used to draw the arc
    790     */
    791     void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
    792                  bool useCenter, const SkPaint& paint);
    793 
    794     /** Draw the specified round-rect using the specified paint. The round-rect
    795         will be filled or framed based on the Style in the paint.
    796         @param rect     The rectangular bounds of the roundRect to be drawn
    797         @param rx       The x-radius of the oval used to round the corners
    798         @param ry       The y-radius of the oval used to round the corners
    799         @param paint    The paint used to draw the roundRect
    800     */
    801     void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry,
    802                        const SkPaint& paint);
    803 
    804     /** Draw the specified path using the specified paint. The path will be
    805         filled or framed based on the Style in the paint.
    806         @param path     The path to be drawn
    807         @param paint    The paint used to draw the path
    808     */
    809     virtual void drawPath(const SkPath& path, const SkPaint& paint);
    810 
    811     /** Draw the specified bitmap, with its top/left corner at (x,y), using the
    812         specified paint, transformed by the current matrix. Note: if the paint
    813         contains a maskfilter that generates a mask which extends beyond the
    814         bitmap's original width/height, then the bitmap will be drawn as if it
    815         were in a Shader with CLAMP mode. Thus the color outside of the original
    816         width/height will be the edge color replicated.
    817 
    818         If a shader is present on the paint it will be ignored, except in the
    819         case where the bitmap is kAlpha_8_SkColorType. In that case, the color is
    820         generated by the shader.
    821 
    822         @param bitmap   The bitmap to be drawn
    823         @param left     The position of the left side of the bitmap being drawn
    824         @param top      The position of the top side of the bitmap being drawn
    825         @param paint    The paint used to draw the bitmap, or NULL
    826     */
    827     virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
    828                             const SkPaint* paint = NULL);
    829 
    830     enum DrawBitmapRectFlags {
    831         kNone_DrawBitmapRectFlag            = 0x0,
    832         /**
    833          *  When filtering is enabled, allow the color samples outside of
    834          *  the src rect (but still in the src bitmap) to bleed into the
    835          *  drawn portion
    836          */
    837         kBleed_DrawBitmapRectFlag           = 0x1,
    838     };
    839 
    840     /** Draw the specified bitmap, with the specified matrix applied (before the
    841         canvas' matrix is applied).
    842         @param bitmap   The bitmap to be drawn
    843         @param src      Optional: specify the subset of the bitmap to be drawn
    844         @param dst      The destination rectangle where the scaled/translated
    845                         image will be drawn
    846         @param paint    The paint used to draw the bitmap, or NULL
    847     */
    848     virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
    849                                       const SkRect& dst,
    850                                       const SkPaint* paint = NULL,
    851                                       DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag);
    852 
    853     void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst,
    854                         const SkPaint* paint = NULL) {
    855         this->drawBitmapRectToRect(bitmap, NULL, dst, paint, kNone_DrawBitmapRectFlag);
    856     }
    857 
    858     void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* isrc,
    859                         const SkRect& dst, const SkPaint* paint = NULL,
    860                         DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag) {
    861         SkRect realSrcStorage;
    862         SkRect* realSrcPtr = NULL;
    863         if (isrc) {
    864             realSrcStorage.set(*isrc);
    865             realSrcPtr = &realSrcStorage;
    866         }
    867         this->drawBitmapRectToRect(bitmap, realSrcPtr, dst, paint, flags);
    868     }
    869 
    870     virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
    871                                   const SkPaint* paint = NULL);
    872 
    873     /**
    874      *  Draw the bitmap stretched differentially to fit into dst.
    875      *  center is a rect within the bitmap, and logically divides the bitmap
    876      *  into 9 sections (3x3). For example, if the middle pixel of a [5x5]
    877      *  bitmap is the "center", then the center-rect should be [2, 2, 3, 3].
    878      *
    879      *  If the dst is >= the bitmap size, then...
    880      *  - The 4 corners are not stretched at all.
    881      *  - The sides are stretched in only one axis.
    882      *  - The center is stretched in both axes.
    883      * Else, for each axis where dst < bitmap,
    884      *  - The corners shrink proportionally
    885      *  - The sides (along the shrink axis) and center are not drawn
    886      */
    887     virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
    888                                 const SkRect& dst, const SkPaint* paint = NULL);
    889 
    890     /** Draw the specified bitmap, with its top/left corner at (x,y),
    891         NOT transformed by the current matrix. Note: if the paint
    892         contains a maskfilter that generates a mask which extends beyond the
    893         bitmap's original width/height, then the bitmap will be drawn as if it
    894         were in a Shader with CLAMP mode. Thus the color outside of the original
    895         width/height will be the edge color replicated.
    896         @param bitmap   The bitmap to be drawn
    897         @param left     The position of the left side of the bitmap being drawn
    898         @param top      The position of the top side of the bitmap being drawn
    899         @param paint    The paint used to draw the bitmap, or NULL
    900     */
    901     virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
    902                             const SkPaint* paint = NULL);
    903 
    904     /** Draw the text, with origin at (x,y), using the specified paint.
    905         The origin is interpreted based on the Align setting in the paint.
    906         @param text The text to be drawn
    907         @param byteLength   The number of bytes to read from the text parameter
    908         @param x        The x-coordinate of the origin of the text being drawn
    909         @param y        The y-coordinate of the origin of the text being drawn
    910         @param paint    The paint used for the text (e.g. color, size, style)
    911     */
    912     SK_LEGACY_DRAWTEXT_VIRTUAL void drawText(const void* text, size_t byteLength, SkScalar x,
    913                           SkScalar y, const SkPaint& paint);
    914 
    915     /** Draw the text, with each character/glyph origin specified by the pos[]
    916         array. The origin is interpreted by the Align setting in the paint.
    917         @param text The text to be drawn
    918         @param byteLength   The number of bytes to read from the text parameter
    919         @param pos      Array of positions, used to position each character
    920         @param paint    The paint used for the text (e.g. color, size, style)
    921         */
    922     SK_LEGACY_DRAWTEXT_VIRTUAL void drawPosText(const void* text, size_t byteLength,
    923                              const SkPoint pos[], const SkPaint& paint);
    924 
    925     /** Draw the text, with each character/glyph origin specified by the x
    926         coordinate taken from the xpos[] array, and the y from the constY param.
    927         The origin is interpreted by the Align setting in the paint.
    928         @param text The text to be drawn
    929         @param byteLength   The number of bytes to read from the text parameter
    930         @param xpos     Array of x-positions, used to position each character
    931         @param constY   The shared Y coordinate for all of the positions
    932         @param paint    The paint used for the text (e.g. color, size, style)
    933         */
    934     SK_LEGACY_DRAWTEXT_VIRTUAL void drawPosTextH(const void* text, size_t byteLength,
    935                               const SkScalar xpos[], SkScalar constY,
    936                               const SkPaint& paint);
    937 
    938     /** Draw the text, with origin at (x,y), using the specified paint, along
    939         the specified path. The paint's Align setting determins where along the
    940         path to start the text.
    941         @param text The text to be drawn
    942         @param byteLength   The number of bytes to read from the text parameter
    943         @param path         The path the text should follow for its baseline
    944         @param hOffset      The distance along the path to add to the text's
    945                             starting position
    946         @param vOffset      The distance above(-) or below(+) the path to
    947                             position the text
    948         @param paint        The paint used for the text
    949     */
    950     void drawTextOnPathHV(const void* text, size_t byteLength,
    951                           const SkPath& path, SkScalar hOffset,
    952                           SkScalar vOffset, const SkPaint& paint);
    953 
    954     /** Draw the text, with origin at (x,y), using the specified paint, along
    955         the specified path. The paint's Align setting determins where along the
    956         path to start the text.
    957         @param text The text to be drawn
    958         @param byteLength   The number of bytes to read from the text parameter
    959         @param path         The path the text should follow for its baseline
    960         @param matrix       (may be null) Applied to the text before it is
    961                             mapped onto the path
    962         @param paint        The paint used for the text
    963         */
    964     SK_LEGACY_DRAWTEXT_VIRTUAL void drawTextOnPath(const void* text, size_t byteLength,
    965                                 const SkPath& path, const SkMatrix* matrix,
    966                                 const SkPaint& paint);
    967 
    968     /** PRIVATE / EXPERIMENTAL -- do not call
    969         Perform back-end analysis/optimization of a picture. This may attach
    970         optimization data to the picture which can be used by a later
    971         drawPicture call.
    972         @param picture The recorded drawing commands to analyze/optimize
    973     */
    974     void EXPERIMENTAL_optimize(const SkPicture* picture);
    975 
    976     /** PRIVATE / EXPERIMENTAL -- do not call
    977         Purge all the discardable optimization information associated with
    978         'picture'. If NULL is passed in, purge all discardable information.
    979     */
    980     void EXPERIMENTAL_purge(const SkPicture* picture);
    981 
    982     /** Draw the picture into this canvas. This method effective brackets the
    983         playback of the picture's draw calls with save/restore, so the state
    984         of this canvas will be unchanged after this call.
    985         @param picture The recorded drawing commands to playback into this
    986                        canvas.
    987     */
    988     void drawPicture(const SkPicture* picture);
    989 
    990     enum VertexMode {
    991         kTriangles_VertexMode,
    992         kTriangleStrip_VertexMode,
    993         kTriangleFan_VertexMode
    994     };
    995 
    996     /** Draw the array of vertices, interpreted as triangles (based on mode).
    997 
    998         If both textures and vertex-colors are NULL, it strokes hairlines with
    999         the paint's color. This behavior is a useful debugging mode to visualize
   1000         the mesh.
   1001 
   1002         @param vmode How to interpret the array of vertices
   1003         @param vertexCount The number of points in the vertices array (and
   1004                     corresponding texs and colors arrays if non-null)
   1005         @param vertices Array of vertices for the mesh
   1006         @param texs May be null. If not null, specifies the coordinate
   1007                     in _texture_ space (not uv space) for each vertex.
   1008         @param colors May be null. If not null, specifies a color for each
   1009                       vertex, to be interpolated across the triangle.
   1010         @param xmode Used if both texs and colors are present. In this
   1011                     case the colors are combined with the texture using mode,
   1012                     before being drawn using the paint. If mode is null, then
   1013                     kModulate_Mode is used.
   1014         @param indices If not null, array of indices to reference into the
   1015                     vertex (texs, colors) array.
   1016         @param indexCount number of entries in the indices array (if not null)
   1017         @param paint Specifies the shader/texture if present.
   1018     */
   1019     virtual void drawVertices(VertexMode vmode, int vertexCount,
   1020                               const SkPoint vertices[], const SkPoint texs[],
   1021                               const SkColor colors[], SkXfermode* xmode,
   1022                               const uint16_t indices[], int indexCount,
   1023                               const SkPaint& paint);
   1024 
   1025     /** Send a blob of data to the canvas.
   1026         For canvases that draw, this call is effectively a no-op, as the data
   1027         is not parsed, but just ignored. However, this call exists for
   1028         subclasses like SkPicture's recording canvas, that can store the data
   1029         and then play it back later (via another call to drawData).
   1030      */
   1031     virtual void drawData(const void* data, size_t length) {
   1032         // do nothing. Subclasses may do something with the data
   1033     }
   1034 
   1035     /** Add comments. beginCommentGroup/endCommentGroup open/close a new group.
   1036         Each comment added via addComment is notionally attached to its
   1037         enclosing group. Top-level comments simply belong to no group.
   1038      */
   1039     virtual void beginCommentGroup(const char* description) {
   1040         // do nothing. Subclasses may do something
   1041     }
   1042     virtual void addComment(const char* kywd, const char* value) {
   1043         // do nothing. Subclasses may do something
   1044     }
   1045     virtual void endCommentGroup() {
   1046         // do nothing. Subclasses may do something
   1047     }
   1048 
   1049     /**
   1050      *  With this call the client asserts that subsequent draw operations (up to the
   1051      *  matching popCull()) are fully contained within the given bounding box. The assertion
   1052      *  is not enforced, but the information might be used to quick-reject command blocks,
   1053      *  so an incorrect bounding box may result in incomplete rendering.
   1054      */
   1055     void pushCull(const SkRect& cullRect);
   1056 
   1057     /**
   1058      *  Terminates the current culling block, and restores the previous one (if any).
   1059      */
   1060     void popCull();
   1061 
   1062     //////////////////////////////////////////////////////////////////////////
   1063 
   1064     /** Get the current filter object. The filter's reference count is not
   1065         affected. The filter is saved/restored, just like the matrix and clip.
   1066         @return the canvas' filter (or NULL).
   1067     */
   1068     SkDrawFilter* getDrawFilter() const;
   1069 
   1070     /** Set the new filter (or NULL). Pass NULL to clear any existing filter.
   1071         As a convenience, the parameter is returned. If an existing filter
   1072         exists, its refcnt is decrement. If the new filter is not null, its
   1073         refcnt is incremented. The filter is saved/restored, just like the
   1074         matrix and clip.
   1075         @param filter the new filter (or NULL)
   1076         @return the new filter
   1077     */
   1078     virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter);
   1079 
   1080     //////////////////////////////////////////////////////////////////////////
   1081 
   1082     /**
   1083      *  Return true if the current clip is empty (i.e. nothing will draw).
   1084      *  Note: this is not always a free call, so it should not be used
   1085      *  more often than necessary. However, once the canvas has computed this
   1086      *  result, subsequent calls will be cheap (until the clip state changes,
   1087      *  which can happen on any clip..() or restore() call.
   1088      */
   1089     virtual bool isClipEmpty() const;
   1090 
   1091     /**
   1092      *  Returns true if the current clip is just a (non-empty) rectangle.
   1093      *  Returns false if the clip is empty, or if it is complex.
   1094      */
   1095     virtual bool isClipRect() const;
   1096 
   1097     /** Return the current matrix on the canvas.
   1098         This does not account for the translate in any of the devices.
   1099         @return The current matrix on the canvas.
   1100     */
   1101     const SkMatrix& getTotalMatrix() const;
   1102 
   1103 #ifdef SK_SUPPORT_LEGACY_GETCLIPTYPE
   1104     enum ClipType {
   1105         kEmpty_ClipType = 0,
   1106         kRect_ClipType,
   1107         kComplex_ClipType
   1108     };
   1109     /** Returns a description of the total clip; may be cheaper than
   1110         getting the clip and querying it directly.
   1111     */
   1112     virtual ClipType getClipType() const;
   1113 #endif
   1114 
   1115 #ifdef SK_SUPPORT_LEGACY_GETTOTALCLIP
   1116     /** DEPRECATED -- need to move this guy to private/friend
   1117      *  Return the current device clip (concatenation of all clip calls).
   1118      *  This does not account for the translate in any of the devices.
   1119      *  @return the current device clip (concatenation of all clip calls).
   1120      */
   1121     const SkRegion& getTotalClip() const;
   1122 #endif
   1123 
   1124     /** Return the clip stack. The clip stack stores all the individual
   1125      *  clips organized by the save/restore frame in which they were
   1126      *  added.
   1127      *  @return the current clip stack ("list" of individual clip elements)
   1128      */
   1129     const SkClipStack* getClipStack() const {
   1130         return &fClipStack;
   1131     }
   1132 
   1133     typedef SkCanvasClipVisitor ClipVisitor;
   1134     /**
   1135      *  Replays the clip operations, back to front, that have been applied to
   1136      *  the canvas, calling the appropriate method on the visitor for each
   1137      *  clip. All clips have already been transformed into device space.
   1138      */
   1139     void replayClips(ClipVisitor*) const;
   1140 
   1141     ///////////////////////////////////////////////////////////////////////////
   1142 
   1143     /** After calling saveLayer(), there can be any number of devices that make
   1144         up the top-most drawing area. LayerIter can be used to iterate through
   1145         those devices. Note that the iterator is only valid until the next API
   1146         call made on the canvas. Ownership of all pointers in the iterator stays
   1147         with the canvas, so none of them should be modified or deleted.
   1148     */
   1149     class SK_API LayerIter /*: SkNoncopyable*/ {
   1150     public:
   1151         /** Initialize iterator with canvas, and set values for 1st device */
   1152         LayerIter(SkCanvas*, bool skipEmptyClips);
   1153         ~LayerIter();
   1154 
   1155         /** Return true if the iterator is done */
   1156         bool done() const { return fDone; }
   1157         /** Cycle to the next device */
   1158         void next();
   1159 
   1160         // These reflect the current device in the iterator
   1161 
   1162         SkBaseDevice*   device() const;
   1163         const SkMatrix& matrix() const;
   1164         const SkRegion& clip() const;
   1165         const SkPaint&  paint() const;
   1166         int             x() const;
   1167         int             y() const;
   1168 
   1169     private:
   1170         // used to embed the SkDrawIter object directly in our instance, w/o
   1171         // having to expose that class def to the public. There is an assert
   1172         // in our constructor to ensure that fStorage is large enough
   1173         // (though needs to be a compile-time-assert!). We use intptr_t to work
   1174         // safely with 32 and 64 bit machines (to ensure the storage is enough)
   1175         intptr_t          fStorage[32];
   1176         class SkDrawIter* fImpl;    // this points at fStorage
   1177         SkPaint           fDefaultPaint;
   1178         bool              fDone;
   1179     };
   1180 
   1181     // don't call
   1182     const SkRegion& internal_private_getTotalClip() const;
   1183     // don't call
   1184     void internal_private_getTotalClipAsPath(SkPath*) const;
   1185     // don't call
   1186     GrRenderTarget* internal_private_accessTopLayerRenderTarget();
   1187 
   1188 protected:
   1189     // default impl defers to getDevice()->newSurface(info)
   1190     virtual SkSurface* onNewSurface(const SkImageInfo&);
   1191 
   1192     // default impl defers to its device
   1193     virtual const void* onPeekPixels(SkImageInfo*, size_t* rowBytes);
   1194     virtual void* onAccessTopLayerPixels(SkImageInfo*, size_t* rowBytes);
   1195 
   1196     // Subclass save/restore notifiers.
   1197     // Overriders should call the corresponding INHERITED method up the inheritance chain.
   1198     // willSaveLayer()'s return value may suppress full layer allocation.
   1199     enum SaveLayerStrategy {
   1200         kFullLayer_SaveLayerStrategy,
   1201         kNoLayer_SaveLayerStrategy
   1202     };
   1203 
   1204     // Transitional, pending external clients cleanup.
   1205     virtual void willSave(SaveFlags) { this->willSave(); }
   1206 
   1207     virtual void willSave() {}
   1208     virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) {
   1209         return kFullLayer_SaveLayerStrategy;
   1210     }
   1211     virtual void willRestore() {}
   1212     virtual void didConcat(const SkMatrix&) {}
   1213     virtual void didSetMatrix(const SkMatrix&) {}
   1214 
   1215     virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&);
   1216 
   1217     virtual void onDrawText(const void* text, size_t byteLength, SkScalar x,
   1218                             SkScalar y, const SkPaint& paint);
   1219 
   1220     virtual void onDrawPosText(const void* text, size_t byteLength,
   1221                                const SkPoint pos[], const SkPaint& paint);
   1222 
   1223     virtual void onDrawPosTextH(const void* text, size_t byteLength,
   1224                                 const SkScalar xpos[], SkScalar constY,
   1225                                 const SkPaint& paint);
   1226 
   1227     virtual void onDrawTextOnPath(const void* text, size_t byteLength,
   1228                                   const SkPath& path, const SkMatrix* matrix,
   1229                                   const SkPaint& paint);
   1230 
   1231     enum ClipEdgeStyle {
   1232         kHard_ClipEdgeStyle,
   1233         kSoft_ClipEdgeStyle
   1234     };
   1235 
   1236     virtual void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle);
   1237     virtual void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle);
   1238     virtual void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle);
   1239     virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op);
   1240 
   1241     virtual void onDiscard();
   1242 
   1243     virtual void onDrawPicture(const SkPicture* picture);
   1244 
   1245     // Returns the canvas to be used by DrawIter. Default implementation
   1246     // returns this. Subclasses that encapsulate an indirect canvas may
   1247     // need to overload this method. The impl must keep track of this, as it
   1248     // is not released or deleted by the caller.
   1249     virtual SkCanvas* canvasForDrawIter();
   1250 
   1251     // Clip rectangle bounds. Called internally by saveLayer.
   1252     // returns false if the entire rectangle is entirely clipped out
   1253     // If non-NULL, The imageFilter parameter will be used to expand the clip
   1254     // and offscreen bounds for any margin required by the filter DAG.
   1255     bool clipRectBounds(const SkRect* bounds, SaveFlags flags,
   1256                         SkIRect* intersection,
   1257                         const SkImageFilter* imageFilter = NULL);
   1258 
   1259     // Called by child classes that override clipPath and clipRRect to only
   1260     // track fast conservative clip bounds, rather than exact clips.
   1261     void updateClipConservativelyUsingBounds(const SkRect&, SkRegion::Op,
   1262                                              bool inverseFilled);
   1263 
   1264     // notify our surface (if we have one) that we are about to draw, so it
   1265     // can perform copy-on-write or invalidate any cached images
   1266     void predrawNotify();
   1267 
   1268     virtual void onPushCull(const SkRect& cullRect);
   1269     virtual void onPopCull();
   1270 
   1271 private:
   1272     class MCRec;
   1273 
   1274     SkClipStack fClipStack;
   1275     SkDeque     fMCStack;
   1276     // points to top of stack
   1277     MCRec*      fMCRec;
   1278     // the first N recs that can fit here mean we won't call malloc
   1279     uint32_t    fMCRecStorage[32];
   1280 
   1281     int         fSaveLayerCount;    // number of successful saveLayer calls
   1282     int         fCullCount;         // number of active culls
   1283 
   1284     SkMetaData* fMetaData;
   1285 
   1286     SkSurface_Base*  fSurfaceBase;
   1287     SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; }
   1288     void setSurfaceBase(SkSurface_Base* sb) {
   1289         fSurfaceBase = sb;
   1290     }
   1291     friend class SkSurface_Base;
   1292     friend class SkSurface_Gpu;
   1293 
   1294     bool fDeviceCMDirty;            // cleared by updateDeviceCMCache()
   1295     void updateDeviceCMCache();
   1296 
   1297     friend class SkDrawIter;        // needs setupDrawForLayerDevice()
   1298     friend class AutoDrawLooper;
   1299     friend class SkLua;             // needs top layer size and offset
   1300     friend class SkDebugCanvas;     // needs experimental fAllowSimplifyClip
   1301     friend class SkDeferredDevice;  // needs getTopDevice()
   1302 
   1303     SkBaseDevice* createLayerDevice(const SkImageInfo&);
   1304 
   1305     SkBaseDevice* init(SkBaseDevice*);
   1306 
   1307     /**
   1308      *  DEPRECATED
   1309      *
   1310      *  Specify a device for this canvas to draw into. If it is not null, its
   1311      *  reference count is incremented. If the canvas was already holding a
   1312      *  device, its reference count is decremented. The new device is returned.
   1313      */
   1314     SkBaseDevice* setRootDevice(SkBaseDevice* device);
   1315 
   1316     /**
   1317      * Gets the size/origin of the top level layer in global canvas coordinates. We don't want this
   1318      * to be public because it exposes decisions about layer sizes that are internal to the canvas.
   1319      */
   1320     SkISize getTopLayerSize() const;
   1321     SkIPoint getTopLayerOrigin() const;
   1322 
   1323     // internal methods are not virtual, so they can safely be called by other
   1324     // canvas apis, without confusing subclasses (like SkPictureRecording)
   1325     void internalDrawBitmap(const SkBitmap&, const SkMatrix& m, const SkPaint* paint);
   1326     void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
   1327                                 const SkRect& dst, const SkPaint* paint,
   1328                                 DrawBitmapRectFlags flags);
   1329     void internalDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
   1330                                 const SkRect& dst, const SkPaint* paint);
   1331     void internalDrawPaint(const SkPaint& paint);
   1332     int internalSaveLayer(const SkRect* bounds, const SkPaint* paint,
   1333                           SaveFlags, bool justForImageFilter, SaveLayerStrategy strategy);
   1334     void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*);
   1335 
   1336     // shared by save() and saveLayer()
   1337     int internalSave(SaveFlags flags);
   1338     void internalRestore();
   1339     static void DrawRect(const SkDraw& draw, const SkPaint& paint,
   1340                          const SkRect& r, SkScalar textSize);
   1341     static void DrawTextDecorations(const SkDraw& draw, const SkPaint& paint,
   1342                                     const char text[], size_t byteLength,
   1343                                     SkScalar x, SkScalar y);
   1344 
   1345     /*  These maintain a cache of the clip bounds in local coordinates,
   1346         (converted to 2s-compliment if floats are slow).
   1347      */
   1348     mutable SkRect fCachedLocalClipBounds;
   1349     mutable bool   fCachedLocalClipBoundsDirty;
   1350     bool fAllowSoftClip;
   1351     bool fAllowSimplifyClip;
   1352 
   1353     const SkRect& getLocalClipBounds() const {
   1354         if (fCachedLocalClipBoundsDirty) {
   1355             if (!this->getClipBounds(&fCachedLocalClipBounds)) {
   1356                 fCachedLocalClipBounds.setEmpty();
   1357             }
   1358             fCachedLocalClipBoundsDirty = false;
   1359         }
   1360         return fCachedLocalClipBounds;
   1361     }
   1362 
   1363     class AutoValidateClip : ::SkNoncopyable {
   1364     public:
   1365         explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) {
   1366             fCanvas->validateClip();
   1367         }
   1368         ~AutoValidateClip() { fCanvas->validateClip(); }
   1369 
   1370     private:
   1371         const SkCanvas* fCanvas;
   1372     };
   1373 
   1374 #ifdef SK_DEBUG
   1375     // The cull stack rects are in device-space
   1376     SkTDArray<SkIRect> fCullStack;
   1377     void validateCull(const SkIRect&);
   1378     void validateClip() const;
   1379 #else
   1380     void validateClip() const {}
   1381 #endif
   1382 
   1383     typedef SkRefCnt INHERITED;
   1384 };
   1385 
   1386 /** Stack helper class to automatically call restoreToCount() on the canvas
   1387     when this object goes out of scope. Use this to guarantee that the canvas
   1388     is restored to a known state.
   1389 */
   1390 class SkAutoCanvasRestore : SkNoncopyable {
   1391 public:
   1392     SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) {
   1393         if (fCanvas) {
   1394             fSaveCount = canvas->getSaveCount();
   1395             if (doSave) {
   1396                 canvas->save();
   1397             }
   1398         }
   1399     }
   1400     ~SkAutoCanvasRestore() {
   1401         if (fCanvas) {
   1402             fCanvas->restoreToCount(fSaveCount);
   1403         }
   1404     }
   1405 
   1406     /**
   1407      *  Perform the restore now, instead of waiting for the destructor. Will
   1408      *  only do this once.
   1409      */
   1410     void restore() {
   1411         if (fCanvas) {
   1412             fCanvas->restoreToCount(fSaveCount);
   1413             fCanvas = NULL;
   1414         }
   1415     }
   1416 
   1417 private:
   1418     SkCanvas*   fCanvas;
   1419     int         fSaveCount;
   1420 };
   1421 #define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore)
   1422 
   1423 /** Stack helper class to automatically open and close a comment block
   1424  */
   1425 class SkAutoCommentBlock : SkNoncopyable {
   1426 public:
   1427     SkAutoCommentBlock(SkCanvas* canvas, const char* description) {
   1428         fCanvas = canvas;
   1429         if (NULL != fCanvas) {
   1430             fCanvas->beginCommentGroup(description);
   1431         }
   1432     }
   1433 
   1434     ~SkAutoCommentBlock() {
   1435         if (NULL != fCanvas) {
   1436             fCanvas->endCommentGroup();
   1437         }
   1438     }
   1439 
   1440 private:
   1441     SkCanvas* fCanvas;
   1442 };
   1443 #define SkAutoCommentBlock(...) SK_REQUIRE_LOCAL_VAR(SkAutoCommentBlock)
   1444 
   1445 /**
   1446  *  If the caller wants read-only access to the pixels in a canvas, it can just
   1447  *  call canvas->peekPixels(), since that is the fastest way to "peek" at the
   1448  *  pixels on a raster-backed canvas.
   1449  *
   1450  *  If the canvas has pixels, but they are not readily available to the CPU
   1451  *  (e.g. gpu-backed), then peekPixels() will fail, but readPixels() will
   1452  *  succeed (though be slower, since it will return a copy of the pixels).
   1453  *
   1454  *  SkAutoROCanvasPixels encapsulates these two techniques, trying first to call
   1455  *  peekPixels() (for performance), but if that fails, calling readPixels() and
   1456  *  storing the copy locally.
   1457  *
   1458  *  The caller must respect the restrictions associated with peekPixels(), since
   1459  *  that may have been called: The returned information is invalidated if...
   1460  *      - any API is called on the canvas (or its parent surface if present)
   1461  *      - the canvas goes out of scope
   1462  */
   1463 class SkAutoROCanvasPixels : SkNoncopyable {
   1464 public:
   1465     SkAutoROCanvasPixels(SkCanvas* canvas);
   1466 
   1467     // returns NULL on failure
   1468     const void* addr() const { return fAddr; }
   1469 
   1470     // undefined if addr() == NULL
   1471     size_t rowBytes() const { return fRowBytes; }
   1472 
   1473     // undefined if addr() == NULL
   1474     const SkImageInfo& info() const { return fInfo; }
   1475 
   1476     // helper that, if returns true, installs the pixels into the bitmap. Note
   1477     // that the bitmap may reference the address returned by peekPixels(), so
   1478     // the caller must respect the restrictions associated with peekPixels().
   1479     bool asROBitmap(SkBitmap*) const;
   1480 
   1481 private:
   1482     SkBitmap    fBitmap;    // used if peekPixels() fails
   1483     const void* fAddr;      // NULL on failure
   1484     SkImageInfo fInfo;
   1485     size_t      fRowBytes;
   1486 };
   1487 
   1488 static inline SkCanvas::SaveFlags operator|(const SkCanvas::SaveFlags lhs,
   1489                                             const SkCanvas::SaveFlags rhs) {
   1490     return static_cast<SkCanvas::SaveFlags>(static_cast<int>(lhs) | static_cast<int>(rhs));
   1491 }
   1492 
   1493 static inline SkCanvas::SaveFlags& operator|=(SkCanvas::SaveFlags& lhs,
   1494                                               const SkCanvas::SaveFlags rhs) {
   1495     lhs = lhs | rhs;
   1496     return lhs;
   1497 }
   1498 
   1499 class SkCanvasClipVisitor {
   1500 public:
   1501     virtual ~SkCanvasClipVisitor();
   1502     virtual void clipRect(const SkRect&, SkRegion::Op, bool antialias) = 0;
   1503     virtual void clipRRect(const SkRRect&, SkRegion::Op, bool antialias) = 0;
   1504     virtual void clipPath(const SkPath&, SkRegion::Op, bool antialias) = 0;
   1505 };
   1506 
   1507 #endif
   1508