aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/gpu/GrClip.h
blob: 5f0a881653cf04a007434e68d5cc9af7312ed378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright 2010 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrClip_DEFINED
#define GrClip_DEFINED

#include "GrFragmentProcessor.h"
#include "GrTypesPriv.h"

class GrDrawContext;

/**
 * Produced by GrClip. It provides a set of modifications to the drawing state that are used to
 * create the final GrPipeline for a GrBatch.
 */
class GrAppliedClip : public SkNoncopyable {
public:
    GrAppliedClip() : fHasStencilClip(false), fDeviceBounds(SkRect::MakeLargest()) {}
    GrFragmentProcessor* getClipCoverageFragmentProcessor() const {
        return fClipCoverageFP.get();
    }
    const GrScissorState& scissorState() const { return fScissorState; }
    bool hasStencilClip() const { return fHasStencilClip; }

    void makeStencil(bool hasStencil, const SkRect& deviceBounds) {
        fClipCoverageFP = nullptr;
        fScissorState.setDisabled();
        fHasStencilClip = hasStencil;
        fDeviceBounds = deviceBounds;
    }

    /**
     * The device bounds of the clip defaults to the scissor rect, but a tighter bounds (based
     * on the known effect of the stencil values) can be provided.
     */
    void makeScissoredStencil(const SkIRect& scissor, const SkRect* deviceBounds = nullptr) {
        fClipCoverageFP = nullptr;
        fScissorState.set(scissor);
        fHasStencilClip = true;
        if (deviceBounds) {
            fDeviceBounds = *deviceBounds;
            SkASSERT(scissor.contains(*deviceBounds));
        } else {
            fDeviceBounds = SkRect::Make(scissor);
        }
    }

    void makeFPBased(sk_sp<GrFragmentProcessor> fp, const SkRect& deviceBounds) {
        fClipCoverageFP = fp;
        fScissorState.setDisabled();
        fHasStencilClip = false;
        fDeviceBounds = deviceBounds;
    }

    void makeScissored(SkIRect& scissor) {
        fClipCoverageFP.reset();
        fScissorState.set(scissor);
        fHasStencilClip = false;
        fDeviceBounds = SkRect::Make(scissor);
    }

    /**
     * The device bounds of the clip defaults to the scissor rect, but a tighter bounds (based
     * on the known effect of the fragment processor) can be provided.
     */
    void makeScissoredFPBased(sk_sp<GrFragmentProcessor> fp, const SkIRect& scissor,
                              const SkRect* deviceBounds = nullptr) {
        fClipCoverageFP = fp;
        fScissorState.set(scissor);
        fHasStencilClip = false;
        if (deviceBounds) {
            fDeviceBounds = *deviceBounds;
            SkASSERT(scissor.contains(*deviceBounds));
        } else {
            fDeviceBounds = SkRect::Make(scissor);
        }
    }

    /**
     * Returns the device bounds of the applied clip. Ideally this considers the combined effect of
     * all clipping techniques in play (scissor, stencil, and/or coverage fp).
     */
    const SkRect& deviceBounds() const { return fDeviceBounds; }

private:
    sk_sp<GrFragmentProcessor> fClipCoverageFP;
    GrScissorState             fScissorState;
    bool                       fHasStencilClip;
    SkRect                     fDeviceBounds;
    typedef SkNoncopyable INHERITED;
};

/**
 * GrClip is an abstract base class for applying a clip. It constructs a clip mask if necessary, and
 * fills out a GrAppliedClip instructing the caller on how to set up the draw state.
 */
class GrClip {
public:
    virtual bool quickContains(const SkRect&) const = 0;
    virtual void getConservativeBounds(int width, int height, SkIRect* devResult,
                                       bool* isIntersectionOfRects = nullptr) const = 0;
    virtual bool apply(GrContext*,
                       GrDrawContext*,
                       const SkRect* devBounds,
                       bool useHWAA,
                       bool hasUserStencilSettings,
                       GrAppliedClip* out) const = 0;

    virtual ~GrClip() {}

    /**
     * This is the maximum distance that a draw may extend beyond a clip's boundary and still count
     * count as "on the other side". We leave some slack because floating point rounding error is
     * likely to blame. The rationale for 1e-3 is that in the coverage case (and barring unexpected
     * rounding), as long as coverage stays within 0.5 * 1/256 of its intended value it shouldn't
     * have any effect on the final pixel values.
     */
    constexpr static SkScalar kBoundsTolerance = 1e-3f;

    /**
     * Returns true if the given query bounds count as entirely inside the clip.
     *
     * @param innerClipBounds   device-space rect contained by the clip (SkRect or SkIRect).
     * @param queryBounds       device-space bounds of the query region.
     */
    template<typename TRect> constexpr static bool IsInsideClip(const TRect& innerClipBounds,
                                                                const SkRect& queryBounds) {
        return innerClipBounds.fRight - innerClipBounds.fLeft > kBoundsTolerance &&
               innerClipBounds.fBottom - innerClipBounds.fTop > kBoundsTolerance &&
               innerClipBounds.fLeft < queryBounds.fLeft + kBoundsTolerance &&
               innerClipBounds.fTop < queryBounds.fTop + kBoundsTolerance &&
               innerClipBounds.fRight > queryBounds.fRight - kBoundsTolerance &&
               innerClipBounds.fBottom > queryBounds.fBottom - kBoundsTolerance;
    }

    /**
     * Returns true if the given query bounds count as entirely outside the clip.
     *
     * @param outerClipBounds   device-space rect that contains the clip (SkRect or SkIRect).
     * @param queryBounds       device-space bounds of the query region.
     */
    template<typename TRect> constexpr static bool IsOutsideClip(const TRect& outerClipBounds,
                                                                 const SkRect& queryBounds) {
        return outerClipBounds.fRight - outerClipBounds.fLeft <= kBoundsTolerance ||
               outerClipBounds.fBottom - outerClipBounds.fTop <= kBoundsTolerance ||
               outerClipBounds.fLeft >= queryBounds.fRight - kBoundsTolerance ||
               outerClipBounds.fTop >= queryBounds.fBottom - kBoundsTolerance ||
               outerClipBounds.fRight <= queryBounds.fLeft + kBoundsTolerance ||
               outerClipBounds.fBottom <= queryBounds.fTop + kBoundsTolerance;
    }

    /**
     * Returns the minimal integer rect that counts as containing a given set of bounds.
     */
    static SkIRect GetPixelIBounds(const SkRect& bounds) {
        return SkIRect::MakeLTRB(SkScalarFloorToInt(bounds.fLeft + kBoundsTolerance),
                                 SkScalarFloorToInt(bounds.fTop + kBoundsTolerance),
                                 SkScalarCeilToInt(bounds.fRight - kBoundsTolerance),
                                 SkScalarCeilToInt(bounds.fBottom - kBoundsTolerance));
    }

    /**
     * Returns the minimal pixel-aligned rect that counts as containing a given set of bounds.
     */
    static SkRect GetPixelBounds(const SkRect& bounds) {
        return SkRect::MakeLTRB(SkScalarFloorToScalar(bounds.fLeft + kBoundsTolerance),
                                SkScalarFloorToScalar(bounds.fTop + kBoundsTolerance),
                                SkScalarCeilToScalar(bounds.fRight - kBoundsTolerance),
                                SkScalarCeilToScalar(bounds.fBottom - kBoundsTolerance));
    }

    /**
     * Returns true if the given rect counts as aligned with pixel boundaries.
     */
    static bool IsPixelAligned(const SkRect& rect) {
        return SkScalarAbs(SkScalarRoundToScalar(rect.fLeft) - rect.fLeft) <= kBoundsTolerance &&
               SkScalarAbs(SkScalarRoundToScalar(rect.fTop) - rect.fTop) <= kBoundsTolerance &&
               SkScalarAbs(SkScalarRoundToScalar(rect.fRight) - rect.fRight) <= kBoundsTolerance &&
               SkScalarAbs(SkScalarRoundToScalar(rect.fBottom) - rect.fBottom) <= kBoundsTolerance;
    }
};

/**
 * Specialized implementation for no clip.
 */
class GrNoClip final : public GrClip {
private:
    bool quickContains(const SkRect&) const final { return true; }
    void getConservativeBounds(int width, int height, SkIRect* devResult,
                               bool* isIntersectionOfRects) const final;
    bool apply(GrContext*,
               GrDrawContext*,
               const SkRect* /* devBounds */,
               bool /* useHWAA */,
               bool /* hasUserStencilSettings */,
               GrAppliedClip* /* out */) const final { return true; }
};

/**
 * GrFixedClip is a clip that can be represented by fixed-function hardware. It never modifies the
 * stencil buffer itself, but can be configured to use whatever clip is already there.
 */
class GrFixedClip final : public GrClip {
public:
    GrFixedClip() : fDeviceBounds(SkRect::MakeLargest()), fHasStencilClip(false) {}
    GrFixedClip(const SkIRect& scissorRect)
        : fScissorState(scissorRect)
        , fDeviceBounds(SkRect::Make(scissorRect))
        , fHasStencilClip(false) {}

    void reset() {
        fScissorState.setDisabled();
        fDeviceBounds.setLargest();
        fHasStencilClip = false;
    }

    void reset(const SkIRect& scissorRect) {
        fScissorState.set(scissorRect);
        fDeviceBounds = SkRect::Make(scissorRect);
        fHasStencilClip = false;
    }

    /**
     * Enables stenciling. The stencil bounds is the device space bounds where the stencil test
     * may pass.
     */
    void enableStencilClip(const SkRect& stencilBounds) {
        fHasStencilClip = true;
        fDeviceBounds = stencilBounds;
        if (fScissorState.enabled()) {
            const SkIRect& s = fScissorState.rect();
            fDeviceBounds.fLeft   = SkTMax(fDeviceBounds.fLeft,   SkIntToScalar(s.fLeft));
            fDeviceBounds.fTop    = SkTMax(fDeviceBounds.fTop,    SkIntToScalar(s.fTop));
            fDeviceBounds.fRight  = SkTMin(fDeviceBounds.fRight,  SkIntToScalar(s.fRight));
            fDeviceBounds.fBottom = SkTMin(fDeviceBounds.fBottom, SkIntToScalar(s.fBottom));
        }
    }

    void disableStencilClip() {
        fHasStencilClip = false;
        if (fScissorState.enabled()) {
            fDeviceBounds = SkRect::Make(fScissorState.rect());
        } else {
            fDeviceBounds.setLargest();
        }
    }

    const GrScissorState& scissorState() const { return fScissorState; }
    bool hasStencilClip() const { return fHasStencilClip; }

    bool quickContains(const SkRect&) const final;
    void getConservativeBounds(int width, int height, SkIRect* devResult,
                               bool* isIntersectionOfRects) const final;

private:
    bool apply(GrContext*,
               GrDrawContext*,
               const SkRect* devBounds,
               bool useHWAA,
               bool hasUserStencilSettings,
               GrAppliedClip* out) const final;

    GrScissorState   fScissorState;
    SkRect           fDeviceBounds;
    bool             fHasStencilClip;
};

#endif