2012-12-17 21:48:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkRRect.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/gpu/GrContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/private/GrSharedEnums.h"
|
|
|
|
#include "include/private/GrTypesPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrCaps.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/GrFragmentProcessor.h"
|
|
|
|
#include "src/gpu/GrPaint.h"
|
|
|
|
#include "src/gpu/GrRenderTargetContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrRenderTargetContextPriv.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/effects/GrRRectEffect.h"
|
|
|
|
#include "src/gpu/ops/GrDrawOp.h"
|
|
|
|
#include "src/gpu/ops/GrFillRectOp.h"
|
2012-12-17 21:48:19 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
2012-12-17 21:48:19 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class RRectGM : public GM {
|
|
|
|
public:
|
2014-03-04 16:25:34 +00:00
|
|
|
enum Type {
|
|
|
|
kBW_Draw_Type,
|
|
|
|
kAA_Draw_Type,
|
|
|
|
kBW_Clip_Type,
|
|
|
|
kAA_Clip_Type,
|
|
|
|
kEffect_Type,
|
|
|
|
};
|
2016-01-08 21:48:43 +00:00
|
|
|
RRectGM(Type type) : fType(type) { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
2018-08-16 14:17:03 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2012-12-17 21:48:19 +00:00
|
|
|
this->setUpRRects();
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-12-17 21:48:19 +00:00
|
|
|
SkString name("rrect");
|
2014-03-04 16:25:34 +00:00
|
|
|
switch (fType) {
|
|
|
|
case kBW_Draw_Type:
|
|
|
|
name.append("_draw_bw");
|
|
|
|
break;
|
|
|
|
case kAA_Draw_Type:
|
|
|
|
name.append("_draw_aa");
|
|
|
|
break;
|
|
|
|
case kBW_Clip_Type:
|
|
|
|
name.append("_clip_bw");
|
|
|
|
break;
|
|
|
|
case kAA_Clip_Type:
|
|
|
|
name.append("_clip_aa");
|
|
|
|
break;
|
|
|
|
case kEffect_Type:
|
|
|
|
name.append("_effect");
|
|
|
|
break;
|
2012-12-17 21:48:19 +00:00
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(kImageWidth, kImageHeight); }
|
2012-12-17 21:48:19 +00:00
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2016-10-27 18:47:55 +00:00
|
|
|
GrRenderTargetContext* renderTargetContext =
|
|
|
|
canvas->internal_private_accessTopLayerRenderTargetContext();
|
2018-06-12 14:11:12 +00:00
|
|
|
GrContext* context = canvas->getGrContext();
|
2019-02-07 23:20:09 +00:00
|
|
|
if (kEffect_Type == fType && (!renderTargetContext || !context)) {
|
|
|
|
*errorMsg = kErrorMsg_DrawSkippedGpuOnly;
|
|
|
|
return DrawResult::kSkip;
|
2018-06-12 14:11:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 16:25:34 +00:00
|
|
|
SkPaint paint;
|
|
|
|
if (kAA_Draw_Type == fType) {
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
}
|
2014-03-05 03:02:06 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
const SkRect kMaxTileBound = SkRect::MakeWH(SkIntToScalar(kTileX),
|
|
|
|
SkIntToScalar(kTileY));
|
2014-03-10 19:33:16 +00:00
|
|
|
#ifdef SK_DEBUG
|
2016-09-01 18:24:54 +00:00
|
|
|
const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth),
|
|
|
|
SkIntToScalar(kImageHeight));
|
2014-03-10 19:33:16 +00:00
|
|
|
#endif
|
2012-12-17 21:48:19 +00:00
|
|
|
|
2017-11-10 16:58:19 +00:00
|
|
|
int lastEdgeType = (kEffect_Type == fType) ? (int) GrClipEdgeType::kLast: 0;
|
2014-03-10 19:33:16 +00:00
|
|
|
|
|
|
|
int y = 1;
|
2014-03-10 19:51:46 +00:00
|
|
|
for (int et = 0; et <= lastEdgeType; ++et) {
|
2014-03-10 19:33:16 +00:00
|
|
|
int x = 1;
|
|
|
|
for (int curRRect = 0; curRRect < kNumRRects; ++curRRect) {
|
|
|
|
bool drew = true;
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
SkASSERT(kMaxTileBound.contains(fRRects[curRRect].getBounds()));
|
|
|
|
SkRect imageSpaceBounds = fRRects[curRRect].getBounds();
|
|
|
|
imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
SkASSERT(kMaxImageBound.contains(imageSpaceBounds));
|
|
|
|
#endif
|
2012-12-17 21:48:19 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
2014-03-04 16:25:34 +00:00
|
|
|
if (kEffect_Type == fType) {
|
2014-03-10 19:33:16 +00:00
|
|
|
SkRRect rrect = fRRects[curRRect];
|
2014-03-04 16:25:34 +00:00
|
|
|
rrect.offset(SkIntToScalar(x), SkIntToScalar(y));
|
2017-11-09 19:51:17 +00:00
|
|
|
GrClipEdgeType edgeType = (GrClipEdgeType) et;
|
2018-10-15 20:09:54 +00:00
|
|
|
const auto& caps = *renderTargetContext->caps()->shaderCaps();
|
2020-06-17 17:53:37 +00:00
|
|
|
auto [success, fp] = GrRRectEffect::Make(/*inputFP=*/nullptr,
|
|
|
|
edgeType, rrect, caps);
|
|
|
|
if (success) {
|
2017-01-11 18:42:54 +00:00
|
|
|
GrPaint grPaint;
|
|
|
|
grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
|
2016-06-23 21:07:00 +00:00
|
|
|
grPaint.addCoverageFragmentProcessor(std::move(fp));
|
2018-10-16 19:19:28 +00:00
|
|
|
grPaint.setColor4f({ 0, 0, 0, 1.f });
|
2014-03-04 16:25:34 +00:00
|
|
|
|
|
|
|
SkRect bounds = rrect.getBounds();
|
|
|
|
bounds.outset(2.f, 2.f);
|
2014-03-05 03:02:06 +00:00
|
|
|
|
2017-05-08 14:43:33 +00:00
|
|
|
renderTargetContext->priv().testingOnly_addDrawOp(
|
2019-05-29 18:43:13 +00:00
|
|
|
GrFillRectOp::MakeNonAARect(context, std::move(grPaint),
|
|
|
|
SkMatrix::I(), bounds));
|
2014-03-10 19:33:16 +00:00
|
|
|
} else {
|
|
|
|
drew = false;
|
2014-03-04 16:25:34 +00:00
|
|
|
}
|
|
|
|
} else if (kBW_Clip_Type == fType || kAA_Clip_Type == fType) {
|
|
|
|
bool aaClip = (kAA_Clip_Type == fType);
|
2016-09-21 18:15:07 +00:00
|
|
|
canvas->clipRRect(fRRects[curRRect], aaClip);
|
2012-12-17 21:48:19 +00:00
|
|
|
canvas->drawRect(kMaxTileBound, paint);
|
|
|
|
} else {
|
2014-03-10 19:33:16 +00:00
|
|
|
canvas->drawRRect(fRRects[curRRect], paint);
|
2012-12-17 21:48:19 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2014-03-10 19:33:16 +00:00
|
|
|
if (drew) {
|
|
|
|
x = x + kTileX;
|
|
|
|
if (x > kImageWidth) {
|
|
|
|
x = 1;
|
|
|
|
y += kTileY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (x != 1) {
|
|
|
|
y += kTileY;
|
2012-12-17 21:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2012-12-17 21:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setUpRRects() {
|
2012-12-18 02:03:03 +00:00
|
|
|
// each RRect must fit in a 0x0 -> (kTileX-2)x(kTileY-2) block. These will be tiled across
|
2012-12-17 21:48:19 +00:00
|
|
|
// the screen in kTileX x kTileY tiles. The extra empty pixels on each side are for AA.
|
|
|
|
|
|
|
|
// simple cases
|
|
|
|
fRRects[0].setRect(SkRect::MakeWH(kTileX-2, kTileY-2));
|
|
|
|
fRRects[1].setOval(SkRect::MakeWH(kTileX-2, kTileY-2));
|
|
|
|
fRRects[2].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 10);
|
2014-02-19 15:18:05 +00:00
|
|
|
fRRects[3].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 5);
|
|
|
|
// small circular corners are an interesting test case for gpu clipping
|
|
|
|
fRRects[4].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 1, 1);
|
|
|
|
fRRects[5].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.5f, 0.5f);
|
|
|
|
fRRects[6].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.2f, 0.2f);
|
2012-12-17 21:48:19 +00:00
|
|
|
|
|
|
|
// The first complex case needs special handling since it is a square
|
|
|
|
fRRects[kNumSimpleCases].setRectRadii(SkRect::MakeWH(kTileY-2, kTileY-2), gRadii[0]);
|
2012-12-24 13:56:17 +00:00
|
|
|
for (size_t i = 1; i < SK_ARRAY_COUNT(gRadii); ++i) {
|
2012-12-17 21:48:19 +00:00
|
|
|
fRRects[kNumSimpleCases+i].setRectRadii(SkRect::MakeWH(kTileX-2, kTileY-2), gRadii[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-03-04 16:25:34 +00:00
|
|
|
Type fType;
|
2014-03-05 03:02:06 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kImageWidth = 640;
|
|
|
|
static constexpr int kImageHeight = 480;
|
2012-12-17 21:48:19 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kTileX = 80;
|
|
|
|
static constexpr int kTileY = 40;
|
2012-12-17 21:48:19 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kNumSimpleCases = 7;
|
|
|
|
static constexpr int kNumComplexCases = 35;
|
2012-12-17 21:58:02 +00:00
|
|
|
static const SkVector gRadii[kNumComplexCases][4];
|
2012-12-17 21:48:19 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kNumRRects = kNumSimpleCases + kNumComplexCases;
|
2012-12-17 21:48:19 +00:00
|
|
|
SkRRect fRRects[kNumRRects];
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Radii for the various test cases. Order is UL, UR, LR, LL
|
|
|
|
const SkVector RRectGM::gRadii[kNumComplexCases][4] = {
|
|
|
|
// a circle
|
|
|
|
{ { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY } },
|
|
|
|
|
|
|
|
// odd ball cases
|
|
|
|
{ { 8, 8 }, { 32, 32 }, { 8, 8 }, { 32, 32 } },
|
|
|
|
{ { 16, 8 }, { 8, 16 }, { 16, 8 }, { 8, 16 } },
|
|
|
|
{ { 0, 0 }, { 16, 16 }, { 8, 8 }, { 32, 32 } },
|
|
|
|
|
|
|
|
// UL
|
|
|
|
{ { 30, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 30, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 15, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
|
|
|
|
// UR
|
|
|
|
{ { 0, 0 }, { 30, 30 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 30, 15 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 15, 30 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
|
|
|
|
// LR
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 30, 30 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 30, 15 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 15, 30 }, { 0, 0 } },
|
|
|
|
|
|
|
|
// LL
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 30 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 15 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 30 } },
|
|
|
|
|
|
|
|
// over-sized radii
|
|
|
|
{ { 0, 0 }, { 100, 400 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 400, 400 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 400, 400 }, { 400, 400 }, { 400, 400 }, { 400, 400 } },
|
2014-02-21 22:41:56 +00:00
|
|
|
|
|
|
|
// circular corner tabs
|
|
|
|
{ { 0, 0 }, { 20, 20 }, { 20, 20 }, { 0, 0 } },
|
|
|
|
{ { 20, 20 }, { 20, 20 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 20, 20 }, { 20, 20 } },
|
|
|
|
{ { 20, 20 }, { 0, 0 }, { 0, 0 }, { 20, 20 } },
|
2014-03-11 15:54:51 +00:00
|
|
|
|
2014-03-24 14:53:09 +00:00
|
|
|
// small radius circular corner tabs
|
|
|
|
{ { 0, 0 }, { 0.2f, 0.2f }, { 0.2f, 0.2f }, { 0, 0 } },
|
|
|
|
{ { 0.3f, 0.3f }, { 0.3f, .3f }, { 0, 0 }, { 0, 0 } },
|
|
|
|
|
2014-03-11 15:54:51 +00:00
|
|
|
// single circular corner cases
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 15 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 15, 15 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 15, 15 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 15, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
|
2014-03-13 18:01:05 +00:00
|
|
|
|
|
|
|
// nine patch elliptical
|
|
|
|
{ { 5, 7 }, { 8, 7 }, { 8, 12 }, { 5, 12 } },
|
|
|
|
{ { 0, 7 }, { 8, 7 }, { 8, 12 }, { 0, 12 } },
|
2014-03-24 14:53:09 +00:00
|
|
|
|
|
|
|
// nine patch elliptical, small radii
|
|
|
|
{ { 0.4f, 7 }, { 8, 7 }, { 8, 12 }, { 0.4f, 12 } },
|
|
|
|
{ { 0.4f, 0.4f }, { 8, 0.4f }, { 8, 12 }, { 0.4f, 12 } },
|
|
|
|
{ { 20, 0.4f }, { 18, 0.4f }, { 18, 0.4f }, { 20, 0.4f } },
|
|
|
|
{ { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f } },
|
|
|
|
|
2012-12-17 21:48:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-03-04 16:25:34 +00:00
|
|
|
DEF_GM( return new RRectGM(RRectGM::kAA_Draw_Type); )
|
|
|
|
DEF_GM( return new RRectGM(RRectGM::kBW_Draw_Type); )
|
|
|
|
DEF_GM( return new RRectGM(RRectGM::kAA_Clip_Type); )
|
|
|
|
DEF_GM( return new RRectGM(RRectGM::kBW_Clip_Type); )
|
|
|
|
DEF_GM( return new RRectGM(RRectGM::kEffect_Type); )
|
2012-12-17 21:48:19 +00:00
|
|
|
|
|
|
|
}
|