Converts a drawPaint through a rrect clip to a drawRRect in GrDrawContext.

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

Review-Url: https://codereview.chromium.org/2271053004
This commit is contained in:
bsalomon 2016-08-26 10:48:19 -07:00 committed by Commit bot
parent e3031b607c
commit cb31e51d93
8 changed files with 181 additions and 0 deletions

53
gm/rrectclipdrawpaint.cpp Normal file
View File

@ -0,0 +1,53 @@
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkPath.h"
#include "SkGradientShader.h"
// Exercises code in GrDrawContext that attempts to replace a rrect clip/draw paint with draw rrect.
DEF_SIMPLE_GM(rrect_clip_draw_paint, canvas, 256, 256) {
SkRRect rrect = SkRRect::MakeRectXY(SkRect::MakeXYWH(10.f, 10.f, 236.f, 236.f), 30.f, 40.f);
SkPaint p;
p.setColor(SK_ColorRED);
SkMatrix zoomOut;
zoomOut.setScale(0.7f, 0.7f, 128.f, 128.f);
const SkRect layerRect = SkRect::MakeWH(256.f, 256.f);
canvas->saveLayer(layerRect, nullptr);
canvas->clipRRect(rrect, SkRegion::kIntersect_Op, true);
canvas->drawPaint(p);
canvas->restore();
canvas->concat(zoomOut);
p.setColor(SK_ColorBLUE);
canvas->saveLayer(layerRect, nullptr);
canvas->clipRRect(rrect, SkRegion::kIntersect_Op, false);
canvas->drawPaint(p);
canvas->restore();
static constexpr SkPoint kPts[] = {{0.f, 0.f}, {256.f, 256.f}};
static constexpr SkColor kColors1[] = {SK_ColorCYAN, SK_ColorGREEN};
p.setShader(SkGradientShader::MakeLinear(kPts, kColors1, nullptr, 2,
SkShader::kClamp_TileMode));
canvas->concat(zoomOut);
canvas->saveLayer(layerRect, nullptr);
canvas->clipRRect(rrect, SkRegion::kIntersect_Op, true);
canvas->drawPaint(p);
canvas->restore();
static constexpr SkColor kColors2[] = {SK_ColorMAGENTA, SK_ColorGRAY};
p.setShader(SkGradientShader::MakeRadial({128.f, 128.f}, 128.f, kColors2, nullptr, 2,
SkShader::kClamp_TileMode));
canvas->concat(zoomOut);
canvas->saveLayer(layerRect, nullptr);
canvas->clipRRect(rrect, SkRegion::kIntersect_Op, false);
canvas->drawPaint(p);
canvas->restore();
}

View File

@ -364,6 +364,21 @@ public:
*/
bool isWideOpen() const { return this->getTopmostGenID() == kWideOpenGenID; }
/**
* This method quickly and conservatively determines whether the entire stack is equivalent to
* intersection with a rrect given a bounds, where the rrect must not contain the entire bounds.
*
* @param bounds A bounds on what will be drawn through the clip. The clip only need be
* equivalent to a intersection with a rrect for draws within the bounds. The
* returned rrect must intersect the bounds but need not be contained by the
* bounds.
* @param rrect If return is true rrect will contain the rrect equivalent to the stack.
* @param aa If return is true aa will indicate whether the equivalent rrect clip is
* antialiased.
* @return true if the stack is equivalent to a single rrect intersect clip, false otherwise.
*/
bool isRRect(const SkRect& bounds, SkRRect* rrect, bool* aa) const;
/**
* The generation ID has three reserved values to indicate special
* (potentially ignorable) cases

View File

@ -32,6 +32,21 @@ public:
virtual ~GrClip() {}
/**
* This method quickly and conservatively determines whether the entire clip is equivalent to
* intersection with a rrect. This will only return true if the rrect does not fully contain
* the render target bounds. Moreover, the returned rrect need not be contained by the render
* target bounds. We assume all draws will be implicitly clipped by the render target bounds.
*
* @param rtBounds The bounds of the render target that the clip will be applied to.
* @param rrect If return is true rrect will contain the rrect equivalent to the clip within
* rtBounds.
* @param aa If return is true aa will indicate whether the rrect clip is antialiased.
* @return true if the clip is equivalent to a single rrect, false otherwise.
*
*/
virtual bool isRRect(const SkRect& rtBounds, SkRRect* rrect, bool* aa) const = 0;
/**
* 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
@ -122,6 +137,7 @@ private:
bool apply(GrContext*, GrDrawContext*, bool, bool, GrAppliedClip*) const final {
return true;
}
bool isRRect(const SkRect&, SkRRect*, bool*) const override { return false; };
};
#endif

View File

@ -862,6 +862,50 @@ void SkClipStack::getConservativeBounds(int offsetX,
}
}
bool SkClipStack::isRRect(const SkRect& bounds, SkRRect* rrect, bool* aa) const {
// We limit to 5 elements. This means the back element will be bounds checked at most 4 times if
// it is an rrect.
int cnt = fDeque.count();
if (!cnt || cnt > 5) {
return false;
}
const Element* back = static_cast<const Element*>(fDeque.back());
if (back->getType() != SkClipStack::Element::kRect_Type &&
back->getType() != SkClipStack::Element::kRRect_Type) {
return false;
}
if (back->getOp() == SkRegion::kReplace_Op) {
*rrect = back->asRRect();
*aa = back->isAA();
return true;
}
if (back->getOp() == SkRegion::kIntersect_Op) {
SkRect backBounds;
if (!backBounds.intersect(bounds, back->asRRect().rect())) {
return false;
}
if (cnt > 1) {
SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart);
SkAssertResult(static_cast<const Element*>(iter.prev()) == back);
while (const Element* prior = (const Element*)iter.prev()) {
if ((prior->getOp() != SkRegion::kIntersect_Op &&
prior->getOp() != SkRegion::kReplace_Op) ||
!prior->contains(backBounds)) {
return false;
}
if (prior->getOp() == SkRegion::kReplace_Op) {
break;
}
}
}
*rrect = back->asRRect();
*aa = back->isAA();
return true;
}
return false;
}
int32_t SkClipStack::GetNextGenID() {
// TODO: handle overflow.
return sk_atomic_inc(&gGenID);

View File

@ -41,6 +41,27 @@ bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
SkIntToScalar(fOrigin.fY)));
}
bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, bool* aa) const {
if (!fStack) {
return false;
}
const SkRect* rtBounds = &origRTBounds;
SkRect tempRTBounds;
bool origin = fOrigin.fX || fOrigin.fY;
if (origin) {
tempRTBounds = origRTBounds;
tempRTBounds.offset(SkIntToScalar(fOrigin.fX), SkIntToScalar(fOrigin.fY));
rtBounds = &tempRTBounds;
}
if (fStack->isRRect(*rtBounds, rr, aa)) {
if (origin) {
rr->offset(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY));
}
return true;
}
return false;
}
void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
bool* isIntersectionOfRects) const {
if (!fStack) {

View File

@ -38,6 +38,8 @@ public:
bool apply(GrContext*, GrDrawContext*, bool useHWAA, bool hasUserStencilSettings,
GrAppliedClip* out) const final;
bool isRRect(const SkRect& rtBounds, SkRRect* rr, bool* aa) const override;
private:
static bool PathNeedsSWRenderer(GrContext* context,
bool hasUserStencilSettings,

View File

@ -251,6 +251,20 @@ void GrDrawContext::drawPaint(const GrClip& clip,
SkIntToScalar(fRenderTarget->height()));
SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
SkRRect rrect;
bool aaRRect;
// Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
// transformation for non-rect rrects. Rects caused a performance regression on an Android
// test that needs investigation. We also skip cases where there are fragment processors
// because they may depend on having correct local coords and this path draws in device space
// without a local matrix.
if (!paint->numTotalFragmentProcessors() &&
clip.isRRect(r, &rrect, &aaRRect) && !rrect.isRect()) {
paint.writable()->setAntiAlias(aaRRect);
this->drawRRect(GrNoClip(), *paint, SkMatrix::I(), rrect, GrStyle::SimpleFill());
return;
}
// by definition this fills the entire clip, no need for AA
if (paint->isAntiAlias()) {
paint.writable()->setAntiAlias(false);

View File

@ -39,6 +39,22 @@ public:
void getConservativeBounds(int width, int height, SkIRect* devResult,
bool* isIntersectionOfRects) const final;
bool isRRect(const SkRect& rtBounds, SkRRect* rr, bool* aa) const override {
if (fHasStencilClip) {
return false;
}
if (fScissorState.enabled()) {
SkRect rect = SkRect::Make(fScissorState.rect());
if (!rect.intersects(rtBounds)) {
return false;
}
rr->setRect(rect);
*aa = false;
return true;
}
return false;
};
private:
bool apply(GrContext*, GrDrawContext*, bool useHWAA, bool hasUserStencilSettings,
GrAppliedClip* out) const final;