2020-04-14 13:54:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google LLC.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
2020-04-15 20:43:00 +00:00
|
|
|
#include "src/core/SkMatrixProvider.h"
|
2020-04-14 13:54:02 +00:00
|
|
|
#include "src/gpu/GrBitmapTextureMaker.h"
|
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#include "src/gpu/GrRenderTargetContextPriv.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
|
|
|
|
#include "src/gpu/ops/GrFillRectOp.h"
|
|
|
|
#include "tools/Resources.h"
|
|
|
|
|
|
|
|
class SampleMatrixVariableEffect : public GrFragmentProcessor {
|
|
|
|
public:
|
|
|
|
static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 2;
|
|
|
|
|
2020-04-28 17:55:02 +00:00
|
|
|
SampleMatrixVariableEffect(std::unique_ptr<GrFragmentProcessor> child, float xOffset,
|
|
|
|
float yOffset)
|
|
|
|
: INHERITED(CLASS_ID, kNone_OptimizationFlags)
|
|
|
|
, fXOffset(xOffset)
|
|
|
|
, fYOffset(yOffset) {
|
2020-04-14 13:54:02 +00:00
|
|
|
child->setSampleMatrix(SkSL::SampleMatrix(SkSL::SampleMatrix::Kind::kVariable));
|
|
|
|
this->registerChildProcessor(std::move(child));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* name() const override { return "SampleMatrixVariableEffect"; }
|
|
|
|
|
|
|
|
std::unique_ptr<GrFragmentProcessor> clone() const override {
|
|
|
|
SkASSERT(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onIsEqual(const GrFragmentProcessor& that) const override {
|
|
|
|
return this == &that;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
|
2020-04-28 17:55:02 +00:00
|
|
|
|
|
|
|
float fXOffset;
|
|
|
|
float fYOffset;
|
|
|
|
|
2020-04-14 13:54:02 +00:00
|
|
|
typedef GrFragmentProcessor INHERITED;
|
2020-04-28 17:55:02 +00:00
|
|
|
|
|
|
|
friend class GLSLSampleMatrixVariableEffect;
|
2020-04-14 13:54:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class GLSLSampleMatrixVariableEffect : public GrGLSLFragmentProcessor {
|
|
|
|
void emitCode(EmitArgs& args) override {
|
2020-04-28 17:55:02 +00:00
|
|
|
auto& smve = args.fFp.cast<SampleMatrixVariableEffect>();
|
2020-04-14 13:54:02 +00:00
|
|
|
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
|
|
|
|
SkString sample1 = this->invokeChildWithMatrix(0, args, "float3x3(1)");
|
2020-04-28 17:55:02 +00:00
|
|
|
SkString sample2 = this->invokeChildWithMatrix(0, args,
|
|
|
|
SkStringPrintf("float3x3(1, -1, 0, 1, 0, 0, "
|
|
|
|
"%g, %g, 1)",
|
|
|
|
smve.fXOffset,
|
|
|
|
smve.fYOffset).c_str());
|
2020-04-14 13:54:02 +00:00
|
|
|
fragBuilder->codeAppendf("%s = (%s + %s) / 2;\n", args.fOutputColor, sample1.c_str(),
|
|
|
|
sample2.c_str());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GrGLSLFragmentProcessor* SampleMatrixVariableEffect::onCreateGLSLInstance() const {
|
|
|
|
return new GLSLSampleMatrixVariableEffect();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GPU_GM(sample_matrix_variable, ctx, rtCtx, canvas, 512, 256) {
|
|
|
|
{
|
|
|
|
SkRect bounds = SkRect::MakeIWH(256, 256);
|
|
|
|
SkBitmap bmp;
|
|
|
|
GetResourceAsBitmap("images/mandrill_256.png", &bmp);
|
|
|
|
GrBitmapTextureMaker maker(ctx, bmp, GrImageTexGenPolicy::kDraw);
|
|
|
|
auto view = maker.view(GrMipMapped::kNo);
|
|
|
|
std::unique_ptr<GrFragmentProcessor> imgFP =
|
|
|
|
GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
|
|
|
|
imgFP->setSampleMatrix(SkSL::SampleMatrix::Kind::kVariable);
|
|
|
|
auto fp = std::unique_ptr<GrFragmentProcessor>(
|
2020-04-28 17:55:02 +00:00
|
|
|
new SampleMatrixVariableEffect(std::move(imgFP), -128, 256));
|
2020-04-14 13:54:02 +00:00
|
|
|
|
|
|
|
GrPaint paint;
|
|
|
|
paint.addCoverageFragmentProcessor(std::move(fp));
|
2020-05-29 13:54:07 +00:00
|
|
|
rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds);
|
2020-04-14 13:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
GrPaint paint;
|
|
|
|
SkRect bounds = SkRect::MakeLTRB(256, 0, 512, 256);
|
|
|
|
static constexpr SkColor colors[] = { 0xff00ff00, 0xffff00ff };
|
|
|
|
static constexpr SkScalar pos[] = { 0.0f, 1.0f };
|
|
|
|
const SkPoint pts[] = {{ 256, 0 }, { 512, 0 }};
|
|
|
|
|
|
|
|
auto shader = SkGradientShader::MakeLinear(pts, colors, pos,
|
|
|
|
SK_ARRAY_COUNT(colors),
|
|
|
|
SkTileMode::kRepeat);
|
|
|
|
SkMatrix matrix;
|
2020-04-15 20:43:00 +00:00
|
|
|
SkSimpleMatrixProvider matrixProvider(matrix);
|
2020-04-14 13:54:02 +00:00
|
|
|
GrColorInfo colorInfo;
|
2020-04-15 20:43:00 +00:00
|
|
|
GrFPArgs args(ctx, matrixProvider, kHigh_SkFilterQuality, &colorInfo);
|
2020-04-14 13:54:02 +00:00
|
|
|
std::unique_ptr<GrFragmentProcessor> gradientFP = as_SB(shader)->asFragmentProcessor(args);
|
|
|
|
gradientFP->setSampleMatrix(SkSL::SampleMatrix::Kind::kVariable);
|
|
|
|
auto fp = std::unique_ptr<GrFragmentProcessor>(
|
2020-04-28 17:55:02 +00:00
|
|
|
new SampleMatrixVariableEffect(std::move(gradientFP), -0.5, 1));
|
2020-04-14 13:54:02 +00:00
|
|
|
paint.addCoverageFragmentProcessor(std::move(fp));
|
2020-05-29 13:54:07 +00:00
|
|
|
rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds);
|
2020-04-14 13:54:02 +00:00
|
|
|
}
|
|
|
|
}
|