skia2/gm/convexpolyeffect.cpp
Adlai Holler 2940538ecd Reland "Remove more GrContext imports & usage"
This reverts commit 75c5168b41.

Reason for revert: Reverted image-cacherator test, for now

Original change's description:
> Revert "Remove more GrContext imports & usage"
>
> This reverts commit dd1395526d.
>
> Reason for revert: Broke chrome roll
>
> Original change's description:
> > Remove more GrContext imports & usage
> >
> > Sanity is coming soon!
> >
> > Change-Id: I109ebeef9efd7dbf4d76a13e1c05df36d59affbc
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/303661
> > Commit-Queue: Adlai Holler <adlai@google.com>
> > Reviewed-by: Robert Phillips <robertphillips@google.com>
>
> TBR=robertphillips@google.com,adlai@google.com
>
> Change-Id: I20d770058d4b54193b6cd2fdc9ca5a1e09f84309
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/304056
> Reviewed-by: Adlai Holler <adlai@google.com>
> Commit-Queue: Adlai Holler <adlai@google.com>

TBR=robertphillips@google.com,adlai@google.com


Change-Id: I940b9f74f7caaa8b4201c241f2a6242b7a24d2a4
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/304062
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: Adlai Holler <adlai@google.com>
2020-07-21 11:36:05 +00:00

210 lines
6.9 KiB
C++

/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
// This test only works with the GPU backend.
#include "gm/gm.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPath.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/private/GrSharedEnums.h"
#include "include/private/GrTypesPriv.h"
#include "src/core/SkTLList.h"
#include "src/gpu/GrFragmentProcessor.h"
#include "src/gpu/GrPaint.h"
#include "src/gpu/GrRenderTargetContext.h"
#include "src/gpu/GrRenderTargetContextPriv.h"
#include "src/gpu/effects/GrConvexPolyEffect.h"
#include "tools/gpu/TestOps.h"
#include <memory>
#include <utility>
class GrAppliedClip;
namespace skiagm {
/**
* This GM directly exercises a GrProcessor that draws convex polygons.
*/
class ConvexPolyEffect : public GpuGM {
public:
ConvexPolyEffect() {
this->setBGColor(0xFFFFFFFF);
}
protected:
SkString onShortName() override {
return SkString("convex_poly_effect");
}
SkISize onISize() override {
return SkISize::Make(720, 800);
}
void onOnceBeforeDraw() override {
SkPath tri;
tri.moveTo(5.f, 5.f);
tri.lineTo(100.f, 20.f);
tri.lineTo(15.f, 100.f);
fPaths.addToTail(tri);
fPaths.addToTail(SkPath())->reverseAddPath(tri);
tri.close();
fPaths.addToTail(tri);
SkPath ngon;
constexpr SkScalar kRadius = 50.f;
const SkPoint center = { kRadius, kRadius };
for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) {
SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges;
SkPoint point = { SkScalarCos(angle), SkScalarSin(angle) };
point.scale(kRadius);
point = center + point;
if (0 == i) {
ngon.moveTo(point);
} else {
ngon.lineTo(point);
}
}
fPaths.addToTail(ngon);
SkMatrix scaleM;
scaleM.setScale(1.1f, 0.4f);
ngon.transform(scaleM);
fPaths.addToTail(ngon);
SkPath linePath;
linePath.moveTo(5.f, 5.f);
linePath.lineTo(6.f, 6.f);
fPaths.addToTail(linePath);
// integer edges
fRects.addToTail(SkRect::MakeLTRB(5.f, 1.f, 30.f, 25.f));
// half-integer edges
fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 24.5f));
// vertically/horizontally thin rects that cover pixel centers
fRects.addToTail(SkRect::MakeLTRB(5.25f, 0.5f, 5.75f, 24.5f));
fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 0.75f));
// vertically/horizontally thin rects that don't cover pixel centers
fRects.addToTail(SkRect::MakeLTRB(5.55f, 0.5f, 5.75f, 24.5f));
fRects.addToTail(SkRect::MakeLTRB(5.5f, .05f, 29.5f, .25f));
// small in x and y
fRects.addToTail(SkRect::MakeLTRB(5.05f, .55f, 5.45f, .85f));
// inverted in x and y
fRects.addToTail(SkRect::MakeLTRB(100.f, 50.5f, 5.f, 0.5f));
}
void onDraw(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
SkCanvas* canvas) override {
SkScalar y = 0;
static constexpr SkScalar kDX = 12.f;
static constexpr SkScalar kOutset = 5.f;
for (PathList::Iter iter(fPaths, PathList::Iter::kHead_IterStart);
iter.get();
iter.next()) {
const SkPath* path = iter.get();
SkScalar x = 0;
for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
const SkMatrix m = SkMatrix::Translate(x, y);
SkPath p;
path->transform(m, &p);
GrClipEdgeType edgeType = (GrClipEdgeType) et;
auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, p);
if (!success) {
continue;
}
GrPaint grPaint;
grPaint.setColor4f({ 0, 0, 0, 1.f });
grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
grPaint.addCoverageFragmentProcessor(std::move(fp));
auto rect = p.getBounds().makeOutset(kOutset, kOutset);
auto op = sk_gpu_test::test_ops::MakeRect(context, std::move(grPaint), rect);
renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
x += SkScalarCeilToScalar(path->getBounds().width() + kDX);
}
// Draw AA and non AA paths using normal API for reference.
canvas->save();
canvas->translate(x, y);
SkPaint paint;
canvas->drawPath(*path, paint);
canvas->translate(path->getBounds().width() + 10.f, 0);
paint.setAntiAlias(true);
canvas->drawPath(*path, paint);
canvas->restore();
y += SkScalarCeilToScalar(path->getBounds().height() + 20.f);
}
for (RectList::Iter iter(fRects, RectList::Iter::kHead_IterStart);
iter.get();
iter.next()) {
SkScalar x = 0;
for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
SkRect rect = iter.get()->makeOffset(x, y);
GrClipEdgeType edgeType = (GrClipEdgeType) et;
auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, rect);
if (!success) {
continue;
}
GrPaint grPaint;
grPaint.setColor4f({ 0, 0, 0, 1.f });
grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
grPaint.addCoverageFragmentProcessor(std::move(fp));
auto drawRect = rect.makeOutset(kOutset, kOutset);
auto op = sk_gpu_test::test_ops::MakeRect(context, std::move(grPaint), drawRect);
renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
x += SkScalarCeilToScalar(rect.width() + kDX);
}
// Draw rect without and with AA using normal API for reference
canvas->save();
canvas->translate(x, y);
SkPaint paint;
canvas->drawRect(*iter.get(), paint);
x += SkScalarCeilToScalar(iter.get()->width() + kDX);
paint.setAntiAlias(true);
canvas->drawRect(*iter.get(), paint);
canvas->restore();
y += SkScalarCeilToScalar(iter.get()->height() + 20.f);
}
}
private:
typedef SkTLList<SkPath, 1> PathList;
typedef SkTLList<SkRect, 1> RectList;
PathList fPaths;
RectList fRects;
typedef GM INHERITED;
};
DEF_GM(return new ConvexPolyEffect;)
}