• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 class SkBitmap;
21 class SkClipStack;
22 class SkBaseDevice;
23 class SkBlitter;
24 class SkMatrix;
25 class SkPath;
26 class SkRegion;
27 class SkRasterClip;
28 struct SkDrawProcs;
29 struct SkRect;
30 class SkRRect;
31 
32 class SkDraw {
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;
drawRect(const SkRect & rect,const SkPaint & paint)41     void    drawRect(const SkRect& rect, const SkPaint& paint) const {
42         this->drawRect(rect, paint, NULL, NULL);
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      */
drawPath(const SkPath & path,const SkPaint & paint,const SkMatrix * prePathMatrix,bool pathIsMutable)54     void    drawPath(const SkPath& path, const SkPaint& paint,
55                      const SkMatrix* prePathMatrix, bool pathIsMutable) const {
56         this->drawPath(path, paint, prePathMatrix, pathIsMutable, false);
57     }
58 
59     void drawPath(const SkPath& path, const SkPaint& paint,
60                   SkBlitter* customBlitter = NULL) const {
61         this->drawPath(path, paint, NULL, false, false, customBlitter);
62     }
63 
64     /* If dstOrNull is null, computes a dst by mapping the bitmap's bounds through the matrix. */
65     void    drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
66                        const SkPaint&) const;
67     void    drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const;
68     void    drawText(const char text[], size_t byteLength, SkScalar x,
69                      SkScalar y, const SkPaint& paint, const SkSurfaceProps*) const;
70     void    drawPosText(const char text[], size_t byteLength,
71                         const SkScalar pos[], int scalarsPerPosition,
72                         const SkPoint& offset, const SkPaint&, const SkSurfaceProps*) const;
73     void    drawVertices(SkVertices::VertexMode mode, int count,
74                          const SkPoint vertices[], const SkPoint textures[],
75                          const SkColor colors[], SkBlendMode bmode,
76                          const uint16_t indices[], int ptCount,
77                          const SkPaint& paint) const;
78 
79     /**
80      *  Overwrite the target with the path's coverage (i.e. its mask).
81      *  Will overwrite the entire device, so it need not be zero'd first.
82      *
83      *  Only device A8 is supported right now.
84      */
85     void drawPathCoverage(const SkPath& src, const SkPaint& paint,
86                           SkBlitter* customBlitter = NULL) const {
87         bool isHairline = paint.getStyle() == SkPaint::kStroke_Style &&
88                           paint.getStrokeWidth() > 0;
89         this->drawPath(src, paint, NULL, false, !isHairline, customBlitter);
90     }
91 
92     /** Helper function that creates a mask from a path and an optional maskfilter.
93         Note however, that the resulting mask will not have been actually filtered,
94         that must be done afterwards (by calling filterMask). The maskfilter is provided
95         solely to assist in computing the mask's bounds (if the mode requests that).
96     */
97     static bool DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
98                            const SkMaskFilter*, const SkMatrix* filterMatrix,
99                            SkMask* mask, SkMask::CreateMode mode,
100                            SkStrokeRec::InitStyle style);
101 
102     void drawDevMask(const SkMask& mask, const SkPaint&) const;
103 
104     enum RectType {
105         kHair_RectType,
106         kFill_RectType,
107         kStroke_RectType,
108         kPath_RectType
109     };
110 
111     /**
112      *  Based on the paint's style, strokeWidth, and the matrix, classify how
113      *  to draw the rect. If no special-case is available, returns
114      *  kPath_RectType.
115      *
116      *  Iff RectType == kStroke_RectType, then strokeSize is set to the device
117      *  width and height of the stroke.
118      */
119     static RectType ComputeRectType(const SkPaint&, const SkMatrix&,
120                                     SkPoint* strokeSize);
121 
122     static bool ShouldDrawTextAsPaths(const SkPaint&, const SkMatrix&);
123     void        drawText_asPaths(const char text[], size_t byteLength, SkScalar x, SkScalar y,
124                                  const SkPaint&) const;
125     void        drawPosText_asPaths(const char text[], size_t byteLength, const SkScalar pos[],
126                                     int scalarsPerPosition, const SkPoint& offset,
127                                     const SkPaint&, const SkSurfaceProps*) const;
128     static SkScalar ComputeResScaleForStroking(const SkMatrix& );
129 private:
130     void    drawBitmapAsMask(const SkBitmap&, const SkPaint&) const;
131 
132     void    drawPath(const SkPath&, const SkPaint&, const SkMatrix* preMatrix,
133                      bool pathIsMutable, bool drawCoverage,
134                      SkBlitter* customBlitter = NULL) const;
135 
136     void drawLine(const SkPoint[2], const SkPaint&) const;
137     void drawDevPath(const SkPath& devPath, const SkPaint& paint, bool drawCoverage,
138                      SkBlitter* customBlitter, bool doFill) const;
139     /**
140      *  Return the current clip bounds, in local coordinates, with slop to account
141      *  for antialiasing or hairlines (i.e. device-bounds outset by 1, and then
142      *  run through the inverse of the matrix).
143      *
144      *  If the matrix cannot be inverted, or the current clip is empty, return
145      *  false and ignore bounds parameter.
146      */
147     bool SK_WARN_UNUSED_RESULT
148     computeConservativeLocalClipBounds(SkRect* bounds) const;
149 
150     /** Returns the current setting for using fake gamma and contrast. */
151     uint32_t SK_WARN_UNUSED_RESULT scalerContextFlags() const;
152 
153 public:
154     SkPixmap        fDst;
155     const SkMatrix* fMatrix;        // required
156     const SkRasterClip* fRC;        // required
157 
158 #ifdef SK_DEBUG
159     void validate() const;
160 #else
validate()161     void validate() const {}
162 #endif
163 };
164 
165 #endif
166