2013-08-21 19:27:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 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 14:59:30 +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/SkPoint3.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2020-07-01 16:55:01 +00:00
|
|
|
#include "include/gpu/GrRecordingContext.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/private/GrTypesPriv.h"
|
|
|
|
#include "include/private/SkColorData.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
2021-07-21 19:39:51 +00:00
|
|
|
#include "src/core/SkCanvasPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkGeometry.h"
|
|
|
|
#include "src/core/SkPointPriv.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "src/gpu/GrCaps.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "src/gpu/GrGeometryProcessor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrMemoryPool.h"
|
|
|
|
#include "src/gpu/GrOpFlushState.h"
|
2020-02-28 16:56:44 +00:00
|
|
|
#include "src/gpu/GrOpsRenderPass.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "src/gpu/GrPaint.h"
|
|
|
|
#include "src/gpu/GrProcessorAnalysis.h"
|
|
|
|
#include "src/gpu/GrProcessorSet.h"
|
2020-02-28 16:56:44 +00:00
|
|
|
#include "src/gpu/GrProgramInfo.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrRecordingContextPriv.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "src/gpu/GrUserStencilSettings.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/effects/GrBezierEffect.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
|
2019-06-03 20:46:19 +00:00
|
|
|
#include "src/gpu/geometry/GrPathUtils.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "src/gpu/ops/GrDrawOp.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/ops/GrMeshDrawOp.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "src/gpu/ops/GrOp.h"
|
2019-12-05 21:40:31 +00:00
|
|
|
#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
|
2021-07-28 19:13:20 +00:00
|
|
|
#include "src/gpu/v1/SurfaceDrawContext_v1.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
class GrAppliedClip;
|
2013-08-21 19:27:48 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
2015-02-11 21:45:50 +00:00
|
|
|
|
2017-07-14 14:12:26 +00:00
|
|
|
class BezierTestOp : public GrMeshDrawOp {
|
|
|
|
public:
|
|
|
|
FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
|
|
|
|
|
2019-06-24 00:07:38 +00:00
|
|
|
GrProcessorSet::Analysis finalize(
|
2021-04-22 18:57:28 +00:00
|
|
|
const GrCaps& caps, const GrAppliedClip* clip, GrClampType clampType) override {
|
2019-03-05 17:11:58 +00:00
|
|
|
return fProcessorSet.finalize(
|
|
|
|
fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip,
|
2021-04-22 18:57:28 +00:00
|
|
|
&GrUserStencilSettings::kUnused, caps, clampType, &fColor);
|
2017-07-14 14:12:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 13:23:58 +00:00
|
|
|
void visitProxies(const GrVisitProxyFunc& func) const override {
|
2020-03-18 12:13:53 +00:00
|
|
|
if (fProgramInfo) {
|
|
|
|
fProgramInfo->visitFPProxies(func);
|
|
|
|
} else {
|
|
|
|
fProcessorSet.visitProxies(func);
|
|
|
|
}
|
2017-09-13 17:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 14:12:26 +00:00
|
|
|
protected:
|
2020-06-17 16:45:57 +00:00
|
|
|
BezierTestOp(const SkRect& rect, const SkPMColor4f& color, int32_t classID)
|
2017-07-14 14:12:26 +00:00
|
|
|
: INHERITED(classID)
|
|
|
|
, fRect(rect)
|
|
|
|
, fColor(color)
|
|
|
|
, fProcessorSet(SkBlendMode::kSrc) {
|
2019-10-01 19:14:44 +00:00
|
|
|
this->setBounds(rect, HasAABloat::kYes, IsHairline::kNo);
|
2017-07-14 14:12:26 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 16:56:44 +00:00
|
|
|
virtual GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) = 0;
|
2019-12-05 21:40:31 +00:00
|
|
|
|
2020-03-12 16:07:19 +00:00
|
|
|
GrProgramInfo* programInfo() override { return fProgramInfo; }
|
|
|
|
|
2020-03-11 19:55:55 +00:00
|
|
|
void onCreateProgramInfo(const GrCaps* caps,
|
|
|
|
SkArenaAlloc* arena,
|
2020-11-19 18:41:26 +00:00
|
|
|
const GrSurfaceProxyView& writeView,
|
2021-07-13 19:26:39 +00:00
|
|
|
bool usesMSAASurface,
|
2020-03-11 19:55:55 +00:00
|
|
|
GrAppliedClip&& appliedClip,
|
2021-06-02 15:58:05 +00:00
|
|
|
const GrDstProxyView& dstProxyView,
|
2020-11-20 15:22:43 +00:00
|
|
|
GrXferBarrierFlags renderPassXferBarriers,
|
|
|
|
GrLoadOp colorLoadOp) override {
|
2020-02-28 16:56:44 +00:00
|
|
|
auto gp = this->makeGP(*caps, arena);
|
|
|
|
if (!gp) {
|
2020-03-11 19:55:55 +00:00
|
|
|
return;
|
2020-02-28 16:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GrPipeline::InputFlags flags = GrPipeline::InputFlags::kNone;
|
|
|
|
|
2020-04-01 20:22:00 +00:00
|
|
|
fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView,
|
2021-08-26 16:05:11 +00:00
|
|
|
usesMSAASurface,
|
2020-03-11 19:55:55 +00:00
|
|
|
std::move(appliedClip),
|
|
|
|
dstProxyView, gp,
|
|
|
|
std::move(fProcessorSet),
|
|
|
|
GrPrimitiveType::kTriangles,
|
2020-09-11 13:33:54 +00:00
|
|
|
renderPassXferBarriers,
|
2020-11-20 15:22:43 +00:00
|
|
|
colorLoadOp,
|
2020-03-11 19:55:55 +00:00
|
|
|
flags);
|
2020-02-28 16:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final {
|
|
|
|
if (!fProgramInfo) {
|
2020-03-11 19:55:55 +00:00
|
|
|
this->createProgramInfo(flushState);
|
2020-02-28 16:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fProgramInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-16 23:34:44 +00:00
|
|
|
flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
|
2021-03-22 18:48:09 +00:00
|
|
|
flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
|
2020-03-16 23:34:44 +00:00
|
|
|
flushState->drawMesh(*fMesh);
|
2017-07-14 14:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SkRect& rect() const { return fRect; }
|
2018-10-31 18:04:39 +00:00
|
|
|
const SkPMColor4f& color() const { return fColor; }
|
2017-07-14 14:12:26 +00:00
|
|
|
|
2020-02-28 16:56:44 +00:00
|
|
|
protected:
|
2020-03-16 15:25:50 +00:00
|
|
|
GrSimpleMesh* fMesh = nullptr; // filled in by the derived classes
|
2020-02-28 16:56:44 +00:00
|
|
|
|
2017-07-14 14:12:26 +00:00
|
|
|
private:
|
2020-02-28 16:56:44 +00:00
|
|
|
SkRect fRect;
|
|
|
|
SkPMColor4f fColor;
|
|
|
|
GrProcessorSet fProcessorSet;
|
|
|
|
GrProgramInfo* fProgramInfo = nullptr;
|
2017-07-14 14:12:26 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GrMeshDrawOp;
|
2017-07-14 14:12:26 +00:00
|
|
|
};
|
|
|
|
|
2013-08-21 19:27:48 +00:00
|
|
|
/**
|
|
|
|
* This GM directly exercises effects that draw Bezier curves in the GPU backend.
|
|
|
|
*/
|
2017-07-14 14:12:26 +00:00
|
|
|
class BezierConicTestOp : public BezierTestOp {
|
2017-06-08 19:12:02 +00:00
|
|
|
public:
|
|
|
|
DEFINE_OP_CLASS_ID
|
|
|
|
|
2020-02-28 16:56:44 +00:00
|
|
|
const char* name() const final { return "BezierConicTestOp"; }
|
2017-06-08 19:12:02 +00:00
|
|
|
|
2020-10-07 20:46:15 +00:00
|
|
|
static GrOp::Owner Make(GrRecordingContext* context,
|
|
|
|
const SkRect& rect,
|
|
|
|
const SkPMColor4f& color,
|
|
|
|
const SkMatrix& klm) {
|
|
|
|
return GrOp::Make<BezierConicTestOp>(context, rect, color, klm);
|
2017-06-08 19:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-07 20:46:15 +00:00
|
|
|
friend class ::GrOp; // for ctor
|
2018-06-12 14:11:12 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
BezierConicTestOp(const SkRect& rect, const SkPMColor4f& color, const SkMatrix& klm)
|
|
|
|
: INHERITED(rect, color, ClassID())
|
|
|
|
, fKLM(klm) {}
|
2017-07-14 14:12:26 +00:00
|
|
|
|
2017-06-08 19:12:02 +00:00
|
|
|
struct Vertex {
|
|
|
|
SkPoint fPosition;
|
|
|
|
float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
|
|
|
|
};
|
|
|
|
|
2020-02-28 16:56:44 +00:00
|
|
|
GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) final {
|
2020-06-17 16:45:57 +00:00
|
|
|
auto tmp = GrConicEffect::Make(arena, this->color(), SkMatrix::I(), caps, SkMatrix::I(),
|
|
|
|
false);
|
2020-02-28 16:56:44 +00:00
|
|
|
if (!tmp) {
|
|
|
|
return nullptr;
|
2019-11-20 21:08:10 +00:00
|
|
|
}
|
2020-02-28 16:56:44 +00:00
|
|
|
SkASSERT(tmp->vertexStride() == sizeof(Vertex));
|
|
|
|
return tmp;
|
|
|
|
}
|
2019-11-20 21:08:10 +00:00
|
|
|
|
2021-06-17 18:55:07 +00:00
|
|
|
void onPrepareDraws(GrMeshDrawTarget* target) final {
|
2018-08-07 14:02:38 +00:00
|
|
|
QuadHelper helper(target, sizeof(Vertex), 1);
|
|
|
|
Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices());
|
2017-06-08 19:12:02 +00:00
|
|
|
if (!verts) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-14 14:12:26 +00:00
|
|
|
SkRect rect = this->rect();
|
2020-02-28 16:56:44 +00:00
|
|
|
SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect, sizeof(Vertex));
|
2017-06-08 19:12:02 +00:00
|
|
|
for (int v = 0; v < 4; ++v) {
|
2017-10-18 15:46:18 +00:00
|
|
|
SkPoint3 pt3 = {verts[v].fPosition.x(), verts[v].fPosition.y(), 1.f};
|
|
|
|
fKLM.mapHomogeneousPoints((SkPoint3* ) verts[v].fKLM, &pt3, 1);
|
2017-06-08 19:12:02 +00:00
|
|
|
}
|
2019-02-26 18:13:22 +00:00
|
|
|
|
2020-02-28 16:56:44 +00:00
|
|
|
fMesh = helper.mesh();
|
2017-06-08 19:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkMatrix fKLM;
|
|
|
|
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kVertsPerCubic = 4;
|
|
|
|
inline static constexpr int kIndicesPerCubic = 6;
|
2017-06-08 19:12:02 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = BezierTestOp;
|
2017-06-08 19:12:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
/**
|
|
|
|
* This GM directly exercises effects that draw Bezier curves in the GPU backend.
|
|
|
|
*/
|
2019-02-07 22:23:36 +00:00
|
|
|
class BezierConicEffects : public GpuGM {
|
2013-08-23 18:05:01 +00:00
|
|
|
public:
|
|
|
|
BezierConicEffects() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
protected:
|
2019-11-21 18:16:21 +00:00
|
|
|
static const int kNumConics = 10;
|
|
|
|
static const int kCellWidth = 128;
|
|
|
|
static const int kCellHeight = 128;
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-08-23 18:05:01 +00:00
|
|
|
return SkString("bezier_conic_effects");
|
|
|
|
}
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2020-06-17 16:45:57 +00:00
|
|
|
return SkISize::Make(kCellWidth, kNumConics*kCellHeight);
|
2013-08-23 18:05:01 +00:00
|
|
|
}
|
2013-08-21 19:27:48 +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;
|
|
|
|
}
|
2019-11-21 18:16:21 +00:00
|
|
|
|
|
|
|
const SkScalar w = kCellWidth, h = kCellHeight;
|
|
|
|
const SkPMColor4f kOpaqueBlack = SkPMColor4f::FromBytes_RGBA(0xff000000);
|
|
|
|
|
|
|
|
const SkPoint baseControlPts[kNumConics][3] = {
|
|
|
|
{ { 0.31f * w, 0.01f * h}, { 0.48f * w, 0.74f * h }, { 0.19f * w, 0.33f * h } },
|
|
|
|
{ { 0.00f * w, 0.07f * h}, { 0.30f * w, 0.70f * h }, { 0.47f * w, 0.37f * h } },
|
|
|
|
{ { 0.15f * w, 0.23f * h}, { 0.49f * w, 0.87f * h }, { 0.85f * w, 0.66f * h } },
|
|
|
|
{ { 0.09f * w, 0.15f * h}, { 0.42f * w, 0.33f * h }, { 0.17f * w, 0.38f * h } },
|
|
|
|
{ { 0.98f * w, 0.54f * h}, { 0.83f * w, 0.91f * h }, { 0.62f * w, 0.40f * h } },
|
|
|
|
{ { 0.96f * w, 0.65f * h}, { 0.03f * w, 0.79f * h }, { 0.24f * w, 0.56f * h } },
|
|
|
|
{ { 0.57f * w, 0.12f * h}, { 0.33f * w, 0.67f * h }, { 0.59f * w, 0.33f * h } },
|
|
|
|
{ { 0.12f * w, 0.72f * h}, { 0.69f * w, 0.85f * h }, { 0.46f * w, 0.32f * h } },
|
|
|
|
{ { 0.27f * w, 0.49f * h}, { 0.41f * w, 0.02f * h }, { 0.11f * w, 0.42f * h } },
|
|
|
|
{ { 0.40f * w, 0.13f * h}, { 0.83f * w, 0.30f * h }, { 0.31f * w, 0.68f * h } },
|
2013-08-23 18:05:01 +00:00
|
|
|
};
|
2019-11-21 18:16:21 +00:00
|
|
|
const SkScalar weights[kNumConics] = { 0.62f, 0.01f, 0.95f, 1.48f, 0.37f,
|
|
|
|
0.66f, 0.15f, 0.14f, 0.61f, 1.4f };
|
|
|
|
|
|
|
|
SkPaint ctrlPtPaint;
|
|
|
|
ctrlPtPaint.setColor(SK_ColorRED);
|
|
|
|
|
|
|
|
SkPaint choppedPtPaint;
|
|
|
|
choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
|
|
|
|
|
|
|
|
SkPaint polyPaint;
|
|
|
|
polyPaint.setColor(0xffA0A0A0);
|
|
|
|
polyPaint.setStrokeWidth(0);
|
|
|
|
polyPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
SkPaint boundsPaint;
|
|
|
|
boundsPaint.setColor(0xff808080);
|
|
|
|
boundsPaint.setStrokeWidth(0);
|
|
|
|
boundsPaint.setStyle(SkPaint::kStroke_Style);
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2019-11-21 18:16:21 +00:00
|
|
|
|
|
|
|
for (int row = 0; row < kNumConics; ++row) {
|
2020-06-17 16:45:57 +00:00
|
|
|
SkScalar x = 0;
|
|
|
|
SkScalar y = row * h;
|
|
|
|
SkPoint controlPts[] = {
|
|
|
|
{x + baseControlPts[row][0].fX, y + baseControlPts[row][0].fY},
|
|
|
|
{x + baseControlPts[row][1].fX, y + baseControlPts[row][1].fY},
|
|
|
|
{x + baseControlPts[row][2].fX, y + baseControlPts[row][2].fY}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint);
|
|
|
|
}
|
2014-03-05 18:27:43 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
SkConic dst[4];
|
|
|
|
SkMatrix klm;
|
|
|
|
int cnt = ChopConic(controlPts, dst, weights[row]);
|
|
|
|
GrPathUtils::getConicKLM(controlPts, weights[row], &klm);
|
|
|
|
|
|
|
|
for (int c = 0; c < cnt; ++c) {
|
|
|
|
SkPoint* pts = dst[c].fPts;
|
2013-08-23 18:05:01 +00:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
2020-06-17 16:45:57 +00:00
|
|
|
canvas->drawCircle(pts[i], 3.f, choppedPtPaint);
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
SkRect bounds;
|
|
|
|
bounds.setBounds(pts, 3);
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
canvas->drawRect(bounds, boundsPaint);
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2021-07-21 19:39:51 +00:00
|
|
|
GrOp::Owner op = BezierConicTestOp::Make(rContext, bounds,
|
2020-10-07 20:46:15 +00:00
|
|
|
kOpaqueBlack, klm);
|
2021-07-21 19:39:51 +00:00
|
|
|
sdc->addDrawOp(std::move(op));
|
2013-08-23 18:05:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-21 19:39:51 +00:00
|
|
|
|
|
|
|
return DrawResult::kOk;
|
2013-08-23 18:05:01 +00:00
|
|
|
}
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
private:
|
|
|
|
// Uses the max curvature function for quads to estimate
|
|
|
|
// where to chop the conic. If the max curvature is not
|
|
|
|
// found along the curve segment it will return 1 and
|
|
|
|
// dst[0] is the original conic. If it returns 2 the dst[0]
|
|
|
|
// and dst[1] are the two new conics.
|
2019-11-21 18:16:21 +00:00
|
|
|
static int SplitConic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
|
2013-08-23 18:05:01 +00:00
|
|
|
SkScalar t = SkFindQuadMaxCurvature(src);
|
2018-07-24 07:08:31 +00:00
|
|
|
if (t == 0 || t == 1) {
|
2013-08-23 18:05:01 +00:00
|
|
|
if (dst) {
|
|
|
|
dst[0].set(src, weight);
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
2013-08-23 18:05:01 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if (dst) {
|
|
|
|
SkConic conic;
|
|
|
|
conic.set(src, weight);
|
2016-09-26 18:03:54 +00:00
|
|
|
if (!conic.chopAt(t, dst)) {
|
|
|
|
dst[0].set(src, weight);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
2013-08-23 18:05:01 +00:00
|
|
|
return 2;
|
2013-08-21 19:27:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 18:16:21 +00:00
|
|
|
// Calls SplitConic on the entire conic and then once more on each subsection.
|
2013-08-23 18:05:01 +00:00
|
|
|
// Most cases will result in either 1 conic (chop point is not within t range)
|
|
|
|
// or 3 points (split once and then one subsection is split again).
|
2019-11-21 18:16:21 +00:00
|
|
|
static int ChopConic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
|
2013-08-23 18:05:01 +00:00
|
|
|
SkConic dstTemp[2];
|
2019-11-21 18:16:21 +00:00
|
|
|
int conicCnt = SplitConic(src, dstTemp, weight);
|
2013-08-23 18:05:01 +00:00
|
|
|
if (2 == conicCnt) {
|
2019-11-21 18:16:21 +00:00
|
|
|
int conicCnt2 = SplitConic(dstTemp[0].fPts, dst, dstTemp[0].fW);
|
|
|
|
conicCnt = conicCnt2 + SplitConic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
|
2013-08-23 18:05:01 +00:00
|
|
|
} else {
|
|
|
|
dst[0] = dstTemp[0];
|
|
|
|
}
|
|
|
|
return conicCnt;
|
|
|
|
}
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2013-08-21 19:27:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2015-02-11 21:45:50 +00:00
|
|
|
|
2017-07-14 14:12:26 +00:00
|
|
|
class BezierQuadTestOp : public BezierTestOp {
|
2015-02-11 21:45:50 +00:00
|
|
|
public:
|
2016-12-01 14:36:50 +00:00
|
|
|
DEFINE_OP_CLASS_ID
|
2016-12-16 14:35:49 +00:00
|
|
|
const char* name() const override { return "BezierQuadTestOp"; }
|
2015-02-11 21:45:50 +00:00
|
|
|
|
2020-10-07 20:46:15 +00:00
|
|
|
static GrOp::Owner Make(GrRecordingContext* context,
|
|
|
|
const SkRect& rect,
|
|
|
|
const SkPMColor4f& color,
|
|
|
|
const GrPathUtils::QuadUVMatrix& devToUV) {
|
|
|
|
return GrOp::Make<BezierQuadTestOp>(context, rect, color, devToUV);
|
2015-02-11 21:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-07 20:46:15 +00:00
|
|
|
friend class ::GrOp; // for ctor
|
2018-06-12 14:11:12 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
BezierQuadTestOp(const SkRect& rect, const SkPMColor4f& color,
|
|
|
|
const GrPathUtils::QuadUVMatrix& devToUV)
|
|
|
|
: INHERITED(rect, color, ClassID())
|
|
|
|
, fDevToUV(devToUV) {}
|
2015-02-11 21:45:50 +00:00
|
|
|
|
|
|
|
struct Vertex {
|
|
|
|
SkPoint fPosition;
|
|
|
|
float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
|
|
|
|
};
|
|
|
|
|
2020-02-28 16:56:44 +00:00
|
|
|
GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) final {
|
2020-06-17 16:45:57 +00:00
|
|
|
auto tmp = GrQuadEffect::Make(arena, this->color(), SkMatrix::I(), caps, SkMatrix::I(),
|
|
|
|
false);
|
2020-02-28 16:56:44 +00:00
|
|
|
if (!tmp) {
|
|
|
|
return nullptr;
|
2019-11-20 21:08:10 +00:00
|
|
|
}
|
2020-02-28 16:56:44 +00:00
|
|
|
SkASSERT(tmp->vertexStride() == sizeof(Vertex));
|
|
|
|
return tmp;
|
|
|
|
}
|
2019-11-20 21:08:10 +00:00
|
|
|
|
2021-06-17 18:55:07 +00:00
|
|
|
void onPrepareDraws(GrMeshDrawTarget* target) final {
|
2018-08-07 14:02:38 +00:00
|
|
|
QuadHelper helper(target, sizeof(Vertex), 1);
|
|
|
|
Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices());
|
2015-05-05 14:49:49 +00:00
|
|
|
if (!verts) {
|
2015-03-05 22:33:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-14 14:12:26 +00:00
|
|
|
SkRect rect = this->rect();
|
2018-05-18 16:52:22 +00:00
|
|
|
SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect, sizeof(Vertex));
|
2018-12-26 21:48:25 +00:00
|
|
|
fDevToUV.apply(verts, 4, sizeof(Vertex), sizeof(SkPoint));
|
2020-02-28 16:56:44 +00:00
|
|
|
|
|
|
|
fMesh = helper.mesh();
|
2015-02-11 21:45:50 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:02:34 +00:00
|
|
|
GrPathUtils::QuadUVMatrix fDevToUV;
|
2015-02-11 21:45:50 +00:00
|
|
|
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kVertsPerCubic = 4;
|
|
|
|
inline static constexpr int kIndicesPerCubic = 6;
|
2015-02-11 21:45:50 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = BezierTestOp;
|
2015-02-11 21:45:50 +00:00
|
|
|
};
|
|
|
|
|
2013-08-23 18:05:01 +00:00
|
|
|
/**
|
|
|
|
* This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
|
|
|
|
*/
|
2019-02-07 22:23:36 +00:00
|
|
|
class BezierQuadEffects : public GpuGM {
|
2013-08-23 18:05:01 +00:00
|
|
|
public:
|
|
|
|
BezierQuadEffects() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-11-21 18:16:21 +00:00
|
|
|
static const int kNumQuads = 5;
|
|
|
|
static const int kCellWidth = 128;
|
|
|
|
static const int kCellHeight = 128;
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-08-23 18:05:01 +00:00
|
|
|
return SkString("bezier_quad_effects");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2020-06-17 16:45:57 +00:00
|
|
|
return SkISize::Make(kCellWidth, kNumQuads*kCellHeight);
|
2013-08-23 18:05:01 +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;
|
|
|
|
}
|
2019-11-21 18:16:21 +00:00
|
|
|
|
|
|
|
const SkScalar w = kCellWidth, h = kCellHeight;
|
|
|
|
const SkPMColor4f kOpaqueBlack = SkPMColor4f::FromBytes_RGBA(0xff000000);
|
|
|
|
|
|
|
|
const SkPoint baseControlPts[kNumQuads][3] = {
|
|
|
|
{ { 0.31f * w, 0.01f * h}, { 0.48f * w, 0.74f * h }, { 0.19f * w, 0.33f * h } },
|
|
|
|
{ { 0.00f * w, 0.07f * h}, { 0.30f * w, 0.70f * h }, { 0.47f * w, 0.37f * h } },
|
|
|
|
{ { 0.15f * w, 0.23f * h}, { 0.49f * w, 0.87f * h }, { 0.85f * w, 0.66f * h } },
|
|
|
|
{ { 0.09f * w, 0.15f * h}, { 0.42f * w, 0.33f * h }, { 0.17f * w, 0.38f * h } },
|
|
|
|
{ { 0.98f * w, 0.54f * h}, { 0.83f * w, 0.91f * h }, { 0.62f * w, 0.40f * h } },
|
2013-08-23 18:05:01 +00:00
|
|
|
};
|
|
|
|
|
2019-11-21 18:16:21 +00:00
|
|
|
SkPaint ctrlPtPaint;
|
|
|
|
ctrlPtPaint.setColor(SK_ColorRED);
|
|
|
|
|
|
|
|
SkPaint choppedPtPaint;
|
|
|
|
choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
|
|
|
|
|
|
|
|
SkPaint polyPaint;
|
|
|
|
polyPaint.setColor(0xffA0A0A0);
|
|
|
|
polyPaint.setStrokeWidth(0);
|
|
|
|
polyPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
SkPaint boundsPaint;
|
|
|
|
boundsPaint.setColor(0xff808080);
|
|
|
|
boundsPaint.setStrokeWidth(0);
|
|
|
|
boundsPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
for (int row = 0; row < kNumQuads; ++row) {
|
2020-06-17 16:45:57 +00:00
|
|
|
SkScalar x = 0;
|
|
|
|
SkScalar y = row * h;
|
|
|
|
SkPoint controlPts[] = {
|
|
|
|
{x + baseControlPts[row][0].fX, y + baseControlPts[row][0].fY},
|
|
|
|
{x + baseControlPts[row][1].fX, y + baseControlPts[row][1].fY},
|
|
|
|
{x + baseControlPts[row][2].fX, y + baseControlPts[row][2].fY}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint);
|
|
|
|
}
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
SkPoint chopped[5];
|
|
|
|
int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped);
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
for (int c = 0; c < cnt; ++c) {
|
|
|
|
SkPoint* pts = chopped + 2 * c;
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
canvas->drawCircle(pts[i], 3.f, choppedPtPaint);
|
|
|
|
}
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
SkRect bounds;
|
|
|
|
bounds.setBounds(pts, 3);
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
canvas->drawRect(bounds, boundsPaint);
|
2013-08-23 18:05:01 +00:00
|
|
|
|
2020-06-17 16:45:57 +00:00
|
|
|
GrPathUtils::QuadUVMatrix DevToUV(pts);
|
2015-02-11 21:03:15 +00:00
|
|
|
|
2021-07-21 19:39:51 +00:00
|
|
|
GrOp::Owner op = BezierQuadTestOp::Make(rContext, bounds,
|
2020-10-07 20:46:15 +00:00
|
|
|
kOpaqueBlack, DevToUV);
|
2021-07-21 19:39:51 +00:00
|
|
|
sdc->addDrawOp(std::move(op));
|
2013-08-23 18:05:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-21 19:39:51 +00:00
|
|
|
|
|
|
|
return DrawResult::kOk;
|
2013-08-23 18:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2013-08-23 18:05:01 +00:00
|
|
|
};
|
2013-08-21 19:27:48 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new BezierConicEffects;)
|
|
|
|
DEF_GM(return new BezierQuadEffects;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|