2014-01-30 18:15:51 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
|
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/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/GrTypesPriv.h"
|
2021-07-21 19:39:51 +00:00
|
|
|
#include "src/core/SkCanvasPriv.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/GrFragmentProcessor.h"
|
|
|
|
#include "src/gpu/GrPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/effects/GrConvexPolyEffect.h"
|
2021-08-11 19:43:50 +00:00
|
|
|
#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
|
2021-07-28 19:13:20 +00:00
|
|
|
#include "src/gpu/v1/SurfaceDrawContext_v1.h"
|
2021-02-24 15:19:11 +00:00
|
|
|
#include "tools/gpu/TestOps.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
class GrAppliedClip;
|
2014-01-30 18:15:51 +00:00
|
|
|
|
2016-04-01 13:06:20 +00:00
|
|
|
namespace skiagm {
|
2015-02-11 21:45:50 +00:00
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* This GM directly exercises a GrProcessor that draws convex polygons.
|
2014-01-30 18:15:51 +00:00
|
|
|
*/
|
2019-02-07 22:23:36 +00:00
|
|
|
class ConvexPolyEffect : public GpuGM {
|
2014-01-30 18:15:51 +00:00
|
|
|
public:
|
|
|
|
ConvexPolyEffect() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-01-30 18:15:51 +00:00
|
|
|
return SkString("convex_poly_effect");
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:55:22 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(720, 550); }
|
2014-01-30 18:15:51 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2014-01-30 18:15:51 +00:00
|
|
|
SkPath tri;
|
|
|
|
tri.moveTo(5.f, 5.f);
|
|
|
|
tri.lineTo(100.f, 20.f);
|
|
|
|
tri.lineTo(15.f, 100.f);
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2021-08-10 16:27:25 +00:00
|
|
|
fPaths.push_back(tri);
|
|
|
|
fPaths.emplace_back();
|
|
|
|
fPaths.back().reverseAddPath(tri);
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
tri.close();
|
2021-08-10 16:27:25 +00:00
|
|
|
fPaths.push_back(tri);
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
SkPath ngon;
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kRadius = 50.f;
|
2014-01-30 18:15:51 +00:00
|
|
|
const SkPoint center = { kRadius, kRadius };
|
|
|
|
for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) {
|
|
|
|
SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges;
|
2019-04-02 14:59:28 +00:00
|
|
|
SkPoint point = { SkScalarCos(angle), SkScalarSin(angle) };
|
2014-01-30 18:15:51 +00:00
|
|
|
point.scale(kRadius);
|
|
|
|
point = center + point;
|
|
|
|
if (0 == i) {
|
|
|
|
ngon.moveTo(point);
|
|
|
|
} else {
|
|
|
|
ngon.lineTo(point);
|
|
|
|
}
|
|
|
|
}
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2021-08-10 16:27:25 +00:00
|
|
|
fPaths.push_back(ngon);
|
2014-01-30 18:15:51 +00:00
|
|
|
SkMatrix scaleM;
|
|
|
|
scaleM.setScale(1.1f, 0.4f);
|
|
|
|
ngon.transform(scaleM);
|
2021-08-10 16:27:25 +00:00
|
|
|
fPaths.push_back(ngon);
|
2014-02-09 03:02:01 +00:00
|
|
|
|
2016-03-28 22:04:45 +00:00
|
|
|
SkPath linePath;
|
|
|
|
linePath.moveTo(5.f, 5.f);
|
|
|
|
linePath.lineTo(6.f, 6.f);
|
2021-08-10 16:27:25 +00:00
|
|
|
fPaths.push_back(linePath);
|
2014-01-30 18:15:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 19:39:51 +00:00
|
|
|
DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
|
|
|
|
auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
|
|
|
|
if (!sdc) {
|
|
|
|
*errorMsg = kErrorMsg_DrawSkippedGpuOnly;
|
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
SkScalar y = 0;
|
2019-11-22 21:56:36 +00:00
|
|
|
static constexpr SkScalar kDX = 12.f;
|
|
|
|
static constexpr SkScalar kOutset = 5.f;
|
|
|
|
|
2021-08-10 16:27:25 +00:00
|
|
|
for (const SkPath& path : fPaths) {
|
2014-01-30 18:15:51 +00:00
|
|
|
SkScalar x = 0;
|
|
|
|
|
2017-11-10 16:58:19 +00:00
|
|
|
for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
|
2020-05-21 16:11:27 +00:00
|
|
|
const SkMatrix m = SkMatrix::Translate(x, y);
|
2014-01-30 18:15:51 +00:00
|
|
|
SkPath p;
|
2021-08-10 16:27:25 +00:00
|
|
|
path.transform(m, &p);
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2017-11-09 19:51:17 +00:00
|
|
|
GrClipEdgeType edgeType = (GrClipEdgeType) et;
|
2020-06-16 20:34:12 +00:00
|
|
|
auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, p);
|
|
|
|
if (!success) {
|
2014-03-05 18:27:43 +00:00
|
|
|
continue;
|
2014-01-30 18:15:51 +00:00
|
|
|
}
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2021-02-24 15:19:11 +00:00
|
|
|
GrPaint grPaint;
|
|
|
|
grPaint.setColor4f({ 0, 0, 0, 1.f });
|
|
|
|
grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
|
|
|
|
grPaint.setCoverageFragmentProcessor(std::move(fp));
|
|
|
|
auto rect = p.getBounds().makeOutset(kOutset, kOutset);
|
2021-07-21 19:39:51 +00:00
|
|
|
auto op = sk_gpu_test::test_ops::MakeRect(rContext, std::move(grPaint), rect);
|
|
|
|
sdc->addDrawOp(std::move(op));
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2021-08-10 16:27:25 +00:00
|
|
|
x += SkScalarCeilToScalar(path.getBounds().width() + kDX);
|
2014-01-30 18:15:51 +00:00
|
|
|
}
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2014-01-30 18:15:51 +00:00
|
|
|
// Draw AA and non AA paths using normal API for reference.
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
SkPaint paint;
|
2021-08-10 16:27:25 +00:00
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
canvas->translate(path.getBounds().width() + 10.f, 0);
|
2014-01-30 18:15:51 +00:00
|
|
|
paint.setAntiAlias(true);
|
2021-08-10 16:27:25 +00:00
|
|
|
canvas->drawPath(path, paint);
|
2014-01-30 18:15:51 +00:00
|
|
|
canvas->restore();
|
2014-01-31 03:01:59 +00:00
|
|
|
|
2021-08-10 16:27:25 +00:00
|
|
|
y += SkScalarCeilToScalar(path.getBounds().height() + 20.f);
|
2014-02-08 19:31:05 +00:00
|
|
|
}
|
2021-07-21 19:39:51 +00:00
|
|
|
|
|
|
|
return DrawResult::kOk;
|
2014-01-30 18:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-08-10 16:27:25 +00:00
|
|
|
std::vector<SkPath> fPaths;
|
2014-02-09 03:02:01 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2014-01-30 18:15:51 +00:00
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new ConvexPolyEffect;)
|
2021-07-21 19:39:51 +00:00
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|