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 "SkGlyphRunPainter.h"
     15 #include "SkMask.h"
     16 #include "SkPaint.h"
     17 #include "SkPixmap.h"
     18 #include "SkStrokeRec.h"
     19 #include "SkVertices.h"
     20 
     21 class SkBitmap;
     22 class SkClipStack;
     23 class SkBaseDevice;
     24 class SkBlitter;
     25 class SkMatrix;
     26 class SkPath;
     27 class SkRegion;
     28 class SkRasterClip;
     29 struct SkRect;
     30 class SkRRect;
     31 
     32 class SkDraw : public SkGlyphRunListPainter::BitmapDevicePainter {
     33 public:
     34     SkDraw();
     35 
     36     void    drawPaint(const SkPaint&) const;
     37     void    drawPoints(SkCanvas::PointMode, size_t count, const SkPoint[],
     38                        const SkPaint&, SkBaseDevice*) const;
     39     void    drawRect(const SkRect& prePaintRect, const SkPaint&, const SkMatrix* paintMatrix,
     40                      const SkRect* postPaintRect) const;
     41     void    drawRect(const SkRect& rect, const SkPaint& paint) const {
     42         this->drawRect(rect, paint, nullptr, nullptr);
     43     }
     44     void    drawRRect(const SkRRect&, const SkPaint&) const;
     45     /**
     46      *  To save on mallocs, we allow a flag that tells us that srcPath is
     47      *  mutable, so that we don't have to make copies of it as we transform it.
     48      *
     49      *  If prePathMatrix is not null, it should logically be applied before any
     50      *  stroking or other effects. If there are no effects on the paint that
     51      *  affect the geometry/rasterization, then the pre matrix can just be
     52      *  pre-concated with the current matrix.
     53      */
     54     void    drawPath(const SkPath& path, const SkPaint& paint,
     55                      const SkMatrix* prePathMatrix = nullptr, bool pathIsMutable = false) const {
     56         this->drawPath(path, paint, prePathMatrix, pathIsMutable, false);
     57     }
     58 
     59     /* If dstOrNull is null, computes a dst by mapping the bitmap's bounds through the matrix. */
     60     void    drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
     61                        const SkPaint&) const;
     62     void    drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const;
     63     void    drawGlyphRunList(const SkGlyphRunList& glyphRunList,
     64                              SkGlyphRunListPainter* glyphPainter) const;
     65     void    drawVertices(SkVertices::VertexMode mode, int vertexCount,
     66                          const SkPoint vertices[], const SkPoint textures[],
     67                          const SkColor colors[], const SkVertices::BoneIndices boneIndices[],
     68                          const SkVertices::BoneWeights boneWeights[], SkBlendMode bmode,
     69                          const uint16_t indices[], int ptCount,
     70                          const SkPaint& paint, const SkVertices::Bone bones[], int boneCount) 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,
     79                           SkBlitter* customBlitter = nullptr) const {
     80         bool isHairline = paint.getStyle() == SkPaint::kStroke_Style &&
     81                           paint.getStrokeWidth() > 0;
     82         this->drawPath(src, paint, nullptr, false, !isHairline, customBlitter);
     83     }
     84 
     85     void paintPaths(SkSpan<const SkPathPos> pathsAndPositions,
     86                     SkScalar scale,
     87                     const SkPaint& paint) const override;
     88 
     89     void paintMasks(SkSpan<const SkMask> masks, const SkPaint& paint) const override;
     90 
     91     static bool ComputeMaskBounds(const SkRect& devPathBounds, const SkIRect* clipBounds,
     92                                   const SkMaskFilter* filter, const SkMatrix* filterMatrix,
     93                                   SkIRect* bounds);
     94 
     95     /** Helper function that creates a mask from a path and an optional maskfilter.
     96         Note however, that the resulting mask will not have been actually filtered,
     97         that must be done afterwards (by calling filterMask). The maskfilter is provided
     98         solely to assist in computing the mask's bounds (if the mode requests that).
     99     */
    100     static bool DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
    101                            const SkMaskFilter*, const SkMatrix* filterMatrix,
    102                            SkMask* mask, SkMask::CreateMode mode,
    103                            SkStrokeRec::InitStyle style);
    104 
    105     void drawDevMask(const SkMask& mask, const SkPaint&) const;
    106 
    107     enum RectType {
    108         kHair_RectType,
    109         kFill_RectType,
    110         kStroke_RectType,
    111         kPath_RectType
    112     };
    113 
    114     /**
    115      *  Based on the paint's style, strokeWidth, and the matrix, classify how
    116      *  to draw the rect. If no special-case is available, returns
    117      *  kPath_RectType.
    118      *
    119      *  Iff RectType == kStroke_RectType, then strokeSize is set to the device
    120      *  width and height of the stroke.
    121      */
    122     static RectType ComputeRectType(const SkPaint&, const SkMatrix&,
    123                                     SkPoint* strokeSize);
    124 
    125     static bool ShouldDrawTextAsPaths(const SkFont&, const SkPaint&, const SkMatrix&,
    126                                       SkScalar sizeLimit = 1024);
    127 
    128     static SkScalar ComputeResScaleForStroking(const SkMatrix& );
    129 private:
    130     void drawBitmapAsMask(const SkBitmap&, const SkPaint&) const;
    131 
    132     void drawPath(const SkPath&,
    133                   const SkPaint&,
    134                   const SkMatrix* preMatrix,
    135                   bool pathIsMutable,
    136                   bool drawCoverage,
    137                   SkBlitter* customBlitter = nullptr) const;
    138 
    139     void drawLine(const SkPoint[2], const SkPaint&) const;
    140 
    141     void drawDevPath(const SkPath& devPath,
    142                      const SkPaint& paint,
    143                      bool drawCoverage,
    144                      SkBlitter* customBlitter,
    145                      bool doFill) const;
    146     /**
    147      *  Return the current clip bounds, in local coordinates, with slop to account
    148      *  for antialiasing or hairlines (i.e. device-bounds outset by 1, and then
    149      *  run through the inverse of the matrix).
    150      *
    151      *  If the matrix cannot be inverted, or the current clip is empty, return
    152      *  false and ignore bounds parameter.
    153      */
    154     bool SK_WARN_UNUSED_RESULT computeConservativeLocalClipBounds(SkRect* bounds) const;
    155 
    156 public:
    157     SkPixmap        fDst;
    158     const SkMatrix* fMatrix{nullptr};        // required
    159     const SkRasterClip* fRC{nullptr};        // required
    160 
    161     // optional, will be same dimensions as fDst if present
    162     const SkPixmap* fCoverage{nullptr};
    163 
    164 #ifdef SK_DEBUG
    165     void validate() const;
    166 #else
    167     void validate() const {}
    168 #endif
    169 };
    170 
    171 #endif
    172