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