Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2006 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 SkDraw_DEFINED
     11 #define SkDraw_DEFINED
     12 
     13 #include "SkCanvas.h"
     14 #include "SkMask.h"
     15 #include "SkPaint.h"
     16 
     17 class SkBitmap;
     18 class SkBounder;
     19 class SkClipStack;
     20 class SkBaseDevice;
     21 class SkMatrix;
     22 class SkPath;
     23 class SkRegion;
     24 class SkRasterClip;
     25 struct SkDrawProcs;
     26 struct SkRect;
     27 class SkRRect;
     28 
     29 class SkDraw {
     30 public:
     31     SkDraw();
     32     SkDraw(const SkDraw& src);
     33 
     34     void    drawPaint(const SkPaint&) const;
     35     void    drawPoints(SkCanvas::PointMode, size_t count, const SkPoint[],
     36                        const SkPaint&, bool forceUseDevice = false) const;
     37     void    drawRect(const SkRect&, const SkPaint&) const;
     38     void    drawRRect(const SkRRect&, const SkPaint&) const;
     39     /**
     40      *  To save on mallocs, we allow a flag that tells us that srcPath is
     41      *  mutable, so that we don't have to make copies of it as we transform it.
     42      *
     43      *  If prePathMatrix is not null, it should logically be applied before any
     44      *  stroking or other effects. If there are no effects on the paint that
     45      *  affect the geometry/rasterization, then the pre matrix can just be
     46      *  pre-concated with the current matrix.
     47      */
     48     void    drawPath(const SkPath& path, const SkPaint& paint,
     49                      const SkMatrix* prePathMatrix, bool pathIsMutable) const {
     50         this->drawPath(path, paint, prePathMatrix, pathIsMutable, false);
     51     }
     52 
     53     void drawPath(const SkPath& path, const SkPaint& paint) const {
     54         this->drawPath(path, paint, NULL, false, false);
     55     }
     56 
     57     void    drawBitmap(const SkBitmap&, const SkMatrix&, const SkPaint&) const;
     58     void    drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const;
     59     void    drawText(const char text[], size_t byteLength, SkScalar x,
     60                      SkScalar y, const SkPaint& paint) const;
     61     void    drawPosText(const char text[], size_t byteLength,
     62                         const SkScalar pos[], SkScalar constY,
     63                         int scalarsPerPosition, const SkPaint& paint) const;
     64     void    drawTextOnPath(const char text[], size_t byteLength,
     65                         const SkPath&, const SkMatrix*, const SkPaint&) const;
     66     void    drawVertices(SkCanvas::VertexMode mode, int count,
     67                          const SkPoint vertices[], const SkPoint textures[],
     68                          const SkColor colors[], SkXfermode* xmode,
     69                          const uint16_t indices[], int ptCount,
     70                          const SkPaint& paint) const;
     71 
     72     /**
     73      *  Overwrite the target with the path's coverage (i.e. its mask).
     74      *  Will overwrite the entire device, so it need not be zero'd first.
     75      *
     76      *  Only device A8 is supported right now.
     77      */
     78     void drawPathCoverage(const SkPath& src, const SkPaint& paint) const {
     79         this->drawPath(src, paint, NULL, false, true);
     80     }
     81 
     82     /** Helper function that creates a mask from a path and an optional maskfilter.
     83         Note however, that the resulting mask will not have been actually filtered,
     84         that must be done afterwards (by calling filterMask). The maskfilter is provided
     85         solely to assist in computing the mask's bounds (if the mode requests that).
     86     */
     87     static bool DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
     88                            const SkMaskFilter*, const SkMatrix* filterMatrix,
     89                            SkMask* mask, SkMask::CreateMode mode,
     90                            SkPaint::Style style);
     91 
     92     enum RectType {
     93         kHair_RectType,
     94         kFill_RectType,
     95         kStroke_RectType,
     96         kPath_RectType
     97     };
     98 
     99     /**
    100      *  Based on the paint's style, strokeWidth, and the matrix, classify how
    101      *  to draw the rect. If no special-case is available, returns
    102      *  kPath_RectType.
    103      *
    104      *  Iff RectType == kStroke_RectType, then strokeSize is set to the device
    105      *  width and height of the stroke.
    106      */
    107     static RectType ComputeRectType(const SkPaint&, const SkMatrix&,
    108                                     SkPoint* strokeSize);
    109 
    110     void    drawText_asPaths(const char text[], size_t byteLength,
    111                              SkScalar x, SkScalar y, const SkPaint&) const;
    112     void    drawPosText_asPaths(const char text[], size_t byteLength,
    113                                 const SkScalar pos[], SkScalar constY,
    114                                 int scalarsPerPosition, const SkPaint&) const;
    115 
    116 private:
    117     void    drawDevMask(const SkMask& mask, const SkPaint&) const;
    118     void    drawBitmapAsMask(const SkBitmap&, const SkPaint&) const;
    119 
    120     void    drawPath(const SkPath&, const SkPaint&, const SkMatrix* preMatrix,
    121                      bool pathIsMutable, bool drawCoverage) const;
    122 
    123     /**
    124      *  Return the current clip bounds, in local coordinates, with slop to account
    125      *  for antialiasing or hairlines (i.e. device-bounds outset by 1, and then
    126      *  run through the inverse of the matrix).
    127      *
    128      *  If the matrix cannot be inverted, or the current clip is empty, return
    129      *  false and ignore bounds parameter.
    130      */
    131     bool SK_WARN_UNUSED_RESULT
    132     computeConservativeLocalClipBounds(SkRect* bounds) const;
    133 
    134     static bool ShouldDrawTextAsPaths(const SkPaint&, const SkMatrix&);
    135 
    136 public:
    137     const SkBitmap* fBitmap;        // required
    138     const SkMatrix* fMatrix;        // required
    139     const SkRegion* fClip;          // DEPRECATED
    140     const SkRasterClip* fRC;        // required
    141 
    142     const SkClipStack* fClipStack;  // optional
    143     SkBaseDevice*   fDevice;        // optional
    144     SkBounder*      fBounder;       // optional
    145     SkDrawProcs*    fProcs;         // optional
    146 
    147 #ifdef SK_DEBUG
    148     void validate() const;
    149 #else
    150     void validate() const {}
    151 #endif
    152 };
    153 
    154 #endif
    155