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 #include "SkPixmap.h"
17 #include "SkStrokeRec.h"
18 #include "SkVertices.h"
19 #include "SkScalerContext.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 SkDrawProcs;
30 struct SkRect;
31 class SkRRect;
32 struct SkInitOnceData;
33 
34 class SkDraw {
35 public:
36     SkDraw();
37 
38     void    drawPaint(const SkPaint&) const;
39     void    drawPoints(SkCanvas::PointMode, size_t count, const SkPoint[],
40                        const SkPaint&, SkBaseDevice*) const;
41     void    drawRect(const SkRect& prePaintRect, const SkPaint&, const SkMatrix* paintMatrix,
42                      const SkRect* postPaintRect) const;
drawRect(const SkRect & rect,const SkPaint & paint)43     void    drawRect(const SkRect& rect, const SkPaint& paint) const {
44         this->drawRect(rect, paint, nullptr, nullptr);
45     }
46     void    drawRRect(const SkRRect&, const SkPaint&) const;
47     /**
48      *  To save on mallocs, we allow a flag that tells us that srcPath is
49      *  mutable, so that we don't have to make copies of it as we transform it.
50      *
51      *  If prePathMatrix is not null, it should logically be applied before any
52      *  stroking or other effects. If there are no effects on the paint that
53      *  affect the geometry/rasterization, then the pre matrix can just be
54      *  pre-concated with the current matrix.
55      */
56     void    drawPath(const SkPath& path, const SkPaint& paint,
57                      const SkMatrix* prePathMatrix = nullptr, bool pathIsMutable = false) const {
58         this->drawPath(path, paint, prePathMatrix, pathIsMutable, false);
59     }
60 
61     /* If dstOrNull is null, computes a dst by mapping the bitmap's bounds through the matrix. */
62     void    drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
63                        const SkPaint&) const;
64     void    drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const;
65     void    drawText(const char text[], size_t byteLength, SkScalar x,
66                      SkScalar y, const SkPaint& paint, const SkSurfaceProps*) const;
67     void    drawPosText(const char text[], size_t byteLength,
68                         const SkScalar pos[], int scalarsPerPosition,
69                         const SkPoint& offset, const SkPaint&, const SkSurfaceProps*) const;
70     void    drawVertices(SkVertices::VertexMode mode, int count,
71                          const SkPoint vertices[], const SkPoint textures[],
72                          const SkColor colors[], SkBlendMode bmode,
73                          const uint16_t indices[], int ptCount,
74                          const SkPaint& paint) const;
75 
76     /**
77      *  Overwrite the target with the path's coverage (i.e. its mask).
78      *  Will overwrite the entire device, so it need not be zero'd first.
79      *
80      *  Only device A8 is supported right now.
81      */
82     void drawPathCoverage(const SkPath& src, const SkPaint& paint,
83                           SkBlitter* customBlitter = nullptr) const {
84         bool isHairline = paint.getStyle() == SkPaint::kStroke_Style &&
85                           paint.getStrokeWidth() > 0;
86         this->drawPath(src, paint, nullptr, false, !isHairline, customBlitter);
87     }
88 
89     /** Helper function that creates a mask from a path and an optional maskfilter.
90         Note however, that the resulting mask will not have been actually filtered,
91         that must be done afterwards (by calling filterMask). The maskfilter is provided
92         solely to assist in computing the mask's bounds (if the mode requests that).
93     */
94     static bool DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
95                            const SkMaskFilter*, const SkMatrix* filterMatrix,
96                            SkMask* mask, SkMask::CreateMode mode,
97                            SkStrokeRec::InitStyle style);
98 
99     void drawDevMask(const SkMask& mask, const SkPaint&) const;
100 
101     enum RectType {
102         kHair_RectType,
103         kFill_RectType,
104         kStroke_RectType,
105         kPath_RectType
106     };
107 
108     /**
109      *  Based on the paint's style, strokeWidth, and the matrix, classify how
110      *  to draw the rect. If no special-case is available, returns
111      *  kPath_RectType.
112      *
113      *  Iff RectType == kStroke_RectType, then strokeSize is set to the device
114      *  width and height of the stroke.
115      */
116     static RectType ComputeRectType(const SkPaint&, const SkMatrix&,
117                                     SkPoint* strokeSize);
118 
119     static bool ShouldDrawTextAsPaths(const SkPaint&, const SkMatrix&, SkScalar sizeLimit = 1024);
120     void        drawText_asPaths(const char text[], size_t byteLength, SkScalar x, SkScalar y,
121                                  const SkPaint&) const;
122     void        drawPosText_asPaths(const char text[], size_t byteLength, const SkScalar pos[],
123                                     int scalarsPerPosition, const SkPoint& offset,
124                                     const SkPaint&, const SkSurfaceProps*) const;
125     static SkScalar ComputeResScaleForStroking(const SkMatrix& );
126 private:
127     void    drawBitmapAsMask(const SkBitmap&, const SkPaint&) const;
128 
129     void    drawPath(const SkPath&, const SkPaint&, const SkMatrix* preMatrix,
130                      bool pathIsMutable, bool drawCoverage,
131                      SkBlitter* customBlitter = nullptr, SkInitOnceData* iData = nullptr) const;
132 
133     void drawLine(const SkPoint[2], const SkPaint&) const;
134     void drawDevPath(const SkPath& devPath, const SkPaint& paint, bool drawCoverage,
135                      SkBlitter* customBlitter, bool doFill, SkInitOnceData* iData = nullptr) const;
136     /**
137      *  Return the current clip bounds, in local coordinates, with slop to account
138      *  for antialiasing or hairlines (i.e. device-bounds outset by 1, and then
139      *  run through the inverse of the matrix).
140      *
141      *  If the matrix cannot be inverted, or the current clip is empty, return
142      *  false and ignore bounds parameter.
143      */
144     bool SK_WARN_UNUSED_RESULT
145     computeConservativeLocalClipBounds(SkRect* bounds) const;
146 
147     /** Returns the current setting for using fake gamma and contrast. */
148     SkScalerContextFlags SK_WARN_UNUSED_RESULT scalerContextFlags() const;
149 
150 public:
151     SkPixmap        fDst;
152     const SkMatrix* fMatrix;        // required
153     const SkRasterClip* fRC;        // required
154 
155 #ifdef SK_DEBUG
156     void validate() const;
157 #else
validate()158     void validate() const {}
159 #endif
160 
161     friend class SkThreadedBMPDevice; // to access private method drawPath
162 };
163 
164 #endif
165