skia2/include/gpu/GrClip.h
csmartdalton 28341fad84 Implement difference clip rects with window rectangles
Plumbs the pipeline for window rectangles and uses them for a very
basic implementation of difference clip rects. This puts a common
Blink pattern on fast path, but we will still eventually need to make
more comprehensive use of window rectangles during clipping.

GTX 960 perf result:
                              gpu                     glinst4                 glinst16
desk_jsfiddlebigcar.skp       0.254 -> 0.177 [70%]    0.279 -> 0.197 [71%]    0.577 -> 0.196 [34%]
keymobi_sfgate_com_.skp       0.697 -> 0.513 [74%]    0.766 -> 0.451 [59%]    0.769 -> 0.597 [78%]
keymobi_blogger.skp           0.406 -> 0.314 [77%]    0.436 -> 0.292 [67%]    0.696 -> 0.319 [46%]
desk_pokemonwiki.skp          0.121 -> 0.098 [81%]     0.13 -> 0.105 [81%]    0.216 -> 0.097 [45%]
desk_wikipedia.skp            0.121 -> 0.098 [81%]     0.13 -> 0.104 [80%]    0.199 -> 0.104 [52%]
keymobi_androidpolice_co...   0.443 -> 0.382 [86%]    0.447 -> 0.398 [89%]    0.444 -> 0.396 [89%]
keymobi_booking_com_sear...   1 .15 ->  1.03 [90%]     1.17 ->  1.06 [91%]     1.17 ->  1.05 [90%]
keymobi_theverge_com.skp      0.417 -> 0.396 [95%]    0.426 -> 0.405 [95%]    0.429 ->   0.4 [93%]

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2251573002

Review-Url: https://codereview.chromium.org/2251573002
2016-08-17 10:00:22 -07:00

154 lines
6.5 KiB
C++

/*
* 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 GrAppliedClip;
class GrDrawContext;
/**
* 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 bool quickContains(const SkRRect& rrect) const {
return this->quickContains(rrect.getBounds());
}
virtual void getConservativeBounds(int width, int height, SkIRect* devResult,
bool* isIntersectionOfRects = nullptr) const = 0;
virtual bool apply(GrContext*, GrDrawContext*, 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*, bool, bool, GrAppliedClip*) 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() : fHasStencilClip(false) {}
GrFixedClip(const SkIRect& scissorRect)
: fScissorState(scissorRect)
, fHasStencilClip(false) {}
void reset() {
fScissorState.setDisabled();
fHasStencilClip = false;
}
void reset(const SkIRect& scissorRect) {
fScissorState.set(scissorRect);
fHasStencilClip = false;
}
void enableStencilClip() { fHasStencilClip = true; }
void disableStencilClip() { fHasStencilClip = false; }
bool quickContains(const SkRect&) const final;
void getConservativeBounds(int width, int height, SkIRect* devResult,
bool* isIntersectionOfRects) const final;
private:
bool apply(GrContext*, GrDrawContext*, bool useHWAA, bool hasUserStencilSettings,
GrAppliedClip* out) const final;
GrScissorState fScissorState;
bool fHasStencilClip;
};
#endif