blob: 87e931fb490526f1bcb9fdb10988f90e96868fbb [file] [log] [blame]
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8// This test only works with the GPU backend.
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkBlendMode.h"
12#include "include/core/SkCanvas.h"
13#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkPath.h"
16#include "include/core/SkPoint.h"
17#include "include/core/SkRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkScalar.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/private/GrTypesPriv.h"
Robert Phillips7a0d3c32021-07-21 15:39:51 -040022#include "src/core/SkCanvasPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "src/gpu/GrFragmentProcessor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "src/gpu/GrPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/effects/GrConvexPolyEffect.h"
Robert Phillips06273bc2021-08-11 15:43:50 -040026#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040027#include "src/gpu/v1/SurfaceDrawContext_v1.h"
Brian Salomon80313c82021-02-24 10:19:11 -050028#include "tools/gpu/TestOps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040029
30#include <memory>
31#include <utility>
32
33class GrAppliedClip;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000034
bsalomon342bfc22016-04-01 06:06:20 -070035namespace skiagm {
joshualitt95964c62015-02-11 13:45:50 -080036
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000037/**
joshualittb0a8a372014-09-23 09:50:21 -070038 * This GM directly exercises a GrProcessor that draws convex polygons.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000039 */
Chris Dalton3a778372019-02-07 15:23:36 -070040class ConvexPolyEffect : public GpuGM {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000041public:
42 ConvexPolyEffect() {
43 this->setBGColor(0xFFFFFFFF);
44 }
45
46protected:
mtklein36352bf2015-03-25 18:17:31 -070047 SkString onShortName() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000048 return SkString("convex_poly_effect");
49 }
50
Brian Salomon066f4112021-02-23 10:55:22 -050051 SkISize onISize() override { return SkISize::Make(720, 550); }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000052
mtklein36352bf2015-03-25 18:17:31 -070053 void onOnceBeforeDraw() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000054 SkPath tri;
55 tri.moveTo(5.f, 5.f);
56 tri.lineTo(100.f, 20.f);
57 tri.lineTo(15.f, 100.f);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +000058
Michael Ludwigb23630c2021-08-10 12:27:25 -040059 fPaths.push_back(tri);
60 fPaths.emplace_back();
61 fPaths.back().reverseAddPath(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +000062
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000063 tri.close();
Michael Ludwigb23630c2021-08-10 12:27:25 -040064 fPaths.push_back(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +000065
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000066 SkPath ngon;
mtkleindbfd7ab2016-09-01 11:24:54 -070067 constexpr SkScalar kRadius = 50.f;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000068 const SkPoint center = { kRadius, kRadius };
69 for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) {
70 SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges;
Brian Osman4428f2c2019-04-02 10:59:28 -040071 SkPoint point = { SkScalarCos(angle), SkScalarSin(angle) };
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000072 point.scale(kRadius);
73 point = center + point;
74 if (0 == i) {
75 ngon.moveTo(point);
76 } else {
77 ngon.lineTo(point);
78 }
79 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +000080
Michael Ludwigb23630c2021-08-10 12:27:25 -040081 fPaths.push_back(ngon);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000082 SkMatrix scaleM;
83 scaleM.setScale(1.1f, 0.4f);
84 ngon.transform(scaleM);
Michael Ludwigb23630c2021-08-10 12:27:25 -040085 fPaths.push_back(ngon);
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +000086
bsalomon7888de02016-03-28 15:04:45 -070087 SkPath linePath;
88 linePath.moveTo(5.f, 5.f);
89 linePath.lineTo(6.f, 6.f);
Michael Ludwigb23630c2021-08-10 12:27:25 -040090 fPaths.push_back(linePath);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000091 }
92
Robert Phillips7a0d3c32021-07-21 15:39:51 -040093 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
94 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
95 if (!sdc) {
96 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
97 return DrawResult::kSkip;
98 }
99
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000100 SkScalar y = 0;
Brian Salomone21af502019-11-22 16:56:36 -0500101 static constexpr SkScalar kDX = 12.f;
102 static constexpr SkScalar kOutset = 5.f;
103
Michael Ludwigb23630c2021-08-10 12:27:25 -0400104 for (const SkPath& path : fPaths) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000105 SkScalar x = 0;
106
Ethan Nicholas1706f842017-11-10 11:58:19 -0500107 for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
Mike Reed1f607332020-05-21 12:11:27 -0400108 const SkMatrix m = SkMatrix::Translate(x, y);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000109 SkPath p;
Michael Ludwigb23630c2021-08-10 12:27:25 -0400110 path.transform(m, &p);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000111
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500112 GrClipEdgeType edgeType = (GrClipEdgeType) et;
John Stilesf08a82b2020-06-16 16:34:12 -0400113 auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, p);
114 if (!success) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000115 continue;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000116 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000117
Brian Salomon80313c82021-02-24 10:19:11 -0500118 GrPaint grPaint;
119 grPaint.setColor4f({ 0, 0, 0, 1.f });
120 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
121 grPaint.setCoverageFragmentProcessor(std::move(fp));
122 auto rect = p.getBounds().makeOutset(kOutset, kOutset);
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400123 auto op = sk_gpu_test::test_ops::MakeRect(rContext, std::move(grPaint), rect);
124 sdc->addDrawOp(std::move(op));
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000125
Michael Ludwigb23630c2021-08-10 12:27:25 -0400126 x += SkScalarCeilToScalar(path.getBounds().width() + kDX);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000127 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000128
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000129 // Draw AA and non AA paths using normal API for reference.
130 canvas->save();
131 canvas->translate(x, y);
132 SkPaint paint;
Michael Ludwigb23630c2021-08-10 12:27:25 -0400133 canvas->drawPath(path, paint);
134 canvas->translate(path.getBounds().width() + 10.f, 0);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000135 paint.setAntiAlias(true);
Michael Ludwigb23630c2021-08-10 12:27:25 -0400136 canvas->drawPath(path, paint);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000137 canvas->restore();
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000138
Michael Ludwigb23630c2021-08-10 12:27:25 -0400139 y += SkScalarCeilToScalar(path.getBounds().height() + 20.f);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000140 }
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400141
142 return DrawResult::kOk;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000143 }
144
145private:
Michael Ludwigb23630c2021-08-10 12:27:25 -0400146 std::vector<SkPath> fPaths;
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +0000147
John Stiles7571f9e2020-09-02 22:42:33 -0400148 using INHERITED = GM;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000149};
150
halcanary385fe4d2015-08-26 13:07:48 -0700151DEF_GM(return new ConvexPolyEffect;)
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400152
John Stilesa6841be2020-08-06 14:11:56 -0400153} // namespace skiagm