SkDrawProcs.h revision 868074b50b0fc3e460d2aa97c1096827fe0a1935
1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkDrawProcs_DEFINED
9#define SkDrawProcs_DEFINED
10
11#include "SkBlitter.h"
12#include "SkDraw.h"
13
14class SkAAClip;
15class SkBlitter;
16
17struct SkDraw1Glyph {
18    const SkDraw* fDraw;
19    const SkRegion* fClip;
20    const SkAAClip* fAAClip;
21    SkBlitter* fBlitter;
22    SkGlyphCache* fCache;
23    const SkPaint* fPaint;
24    SkIRect fClipBounds;
25    /** Half the sampling frequency of the rasterized glyph in x. */
26    SkFixed fHalfSampleX;
27    /** Half the sampling frequency of the rasterized glyph in y. */
28    SkFixed fHalfSampleY;
29
30    /** Draws one glyph.
31     *
32     *  The x and y are pre-biased, so implementations may just truncate them.
33     *  i.e. half the sampling frequency has been added.
34     *  e.g. 1/2 or 1/(2^(SkGlyph::kSubBits+1)) has already been added.
35     *  This added bias can be found in fHalfSampleX,Y.
36     */
37    typedef void (*Proc)(const SkDraw1Glyph&, SkFixed x, SkFixed y, const SkGlyph&);
38
39    Proc init(const SkDraw* draw, SkBlitter* blitter, SkGlyphCache* cache,
40              const SkPaint&);
41
42    // call this instead of fBlitter->blitMask() since this wrapper will handle
43    // the case when the mask is ARGB32_Format
44    //
45    void blitMask(const SkMask& mask, const SkIRect& clip) const {
46        if (SkMask::kARGB32_Format == mask.fFormat) {
47            this->blitMaskAsSprite(mask);
48        } else {
49            fBlitter->blitMask(mask, clip);
50        }
51    }
52
53    // mask must be kARGB32_Format
54    void blitMaskAsSprite(const SkMask& mask) const;
55};
56
57struct SkDrawProcs {
58    SkDraw1Glyph::Proc  fD1GProc;
59};
60
61bool SkDrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix&,
62                                   SkScalar* coverage);
63
64/**
65 *  If the current paint is set to stroke and the stroke-width when applied to
66 *  the matrix is <= 1.0, then this returns true, and sets coverage (simulating
67 *  a stroke by drawing a hairline with partial coverage). If any of these
68 *  conditions are false, then this returns false and coverage is ignored.
69 */
70inline bool SkDrawTreatAsHairline(const SkPaint& paint, const SkMatrix& matrix,
71                                  SkScalar* coverage) {
72    if (SkPaint::kStroke_Style != paint.getStyle()) {
73        return false;
74    }
75
76    SkScalar strokeWidth = paint.getStrokeWidth();
77    if (0 == strokeWidth) {
78        *coverage = SK_Scalar1;
79        return true;
80    }
81
82    if (!paint.isAntiAlias()) {
83        return false;
84    }
85
86    return SkDrawTreatAAStrokeAsHairline(strokeWidth, matrix, coverage);
87}
88
89#endif
90