2019-08-29 20:10:13 +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/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "src/gpu/GrCaps.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-08-29 20:10:13 +00:00
|
|
|
#include "src/gpu/GrFragmentProcessor.h"
|
2020-12-09 21:37:04 +00:00
|
|
|
#include "src/gpu/GrSurfaceDrawContext.h"
|
2021-04-28 16:39:21 +00:00
|
|
|
#include "src/gpu/SkGr.h"
|
2019-08-29 20:10:13 +00:00
|
|
|
#include "src/gpu/effects/GrRRectEffect.h"
|
|
|
|
#include "src/gpu/effects/GrSkSLFP.h"
|
2019-11-26 21:20:22 +00:00
|
|
|
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
|
2019-08-29 20:10:13 +00:00
|
|
|
#include "src/gpu/ops/GrFillRectOp.h"
|
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
class SampleCoordEffect : public GrFragmentProcessor {
|
|
|
|
public:
|
|
|
|
static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0;
|
|
|
|
|
|
|
|
SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child)
|
|
|
|
: INHERITED(CLASS_ID, kNone_OptimizationFlags) {
|
2020-06-30 17:39:35 +00:00
|
|
|
this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
|
2019-08-29 20:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* name() const override { return "SampleCoordEffect"; }
|
|
|
|
|
|
|
|
std::unique_ptr<GrFragmentProcessor> clone() const override {
|
|
|
|
SkASSERT(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onIsEqual(const GrFragmentProcessor&) const override {
|
|
|
|
SkASSERT(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-02-23 15:07:05 +00:00
|
|
|
std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override;
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GrFragmentProcessor;
|
2019-08-29 20:10:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class GLSLSampleCoordEffect : public GrGLSLFragmentProcessor {
|
|
|
|
void emitCode(EmitArgs& args) override {
|
|
|
|
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
|
2020-01-24 19:52:10 +00:00
|
|
|
SkString sample1 = this->invokeChild(0, args, "float2(sk_FragCoord.x, sk_FragCoord.y)");
|
|
|
|
SkString sample2 = this->invokeChild(0, args, "float2(sk_FragCoord.x, 512-sk_FragCoord.y)");
|
2020-12-14 23:04:13 +00:00
|
|
|
fragBuilder->codeAppendf("return (%s + %s) / 2;\n", sample1.c_str(), sample2.c_str());
|
2019-08-29 20:10:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-23 15:07:05 +00:00
|
|
|
std::unique_ptr<GrGLSLFragmentProcessor> SampleCoordEffect::onMakeProgramImpl() const {
|
|
|
|
return std::make_unique<GLSLSampleCoordEffect>();
|
2019-08-29 20:10:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 18:40:57 +00:00
|
|
|
DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, ctx, sdCtx, canvas, 512, 512,
|
2019-08-29 20:10:13 +00:00
|
|
|
ToolUtils::color_to_565(0xFF66AA99)) {
|
|
|
|
SkRect bounds = SkRect::MakeIWH(512, 512);
|
|
|
|
|
|
|
|
SkBitmap bmp;
|
|
|
|
GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp);
|
2021-04-28 16:39:21 +00:00
|
|
|
auto view = std::get<0>(GrMakeCachedBitmapProxyView(ctx, bmp, GrMipmapped::kNo));
|
2020-03-09 19:18:35 +00:00
|
|
|
if (!view) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-23 00:09:27 +00:00
|
|
|
std::unique_ptr<GrFragmentProcessor> imgFP =
|
2020-02-05 15:45:39 +00:00
|
|
|
GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
|
2019-08-29 20:10:13 +00:00
|
|
|
auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP)));
|
|
|
|
|
|
|
|
GrPaint grPaint;
|
2020-07-21 18:39:40 +00:00
|
|
|
grPaint.setCoverageFragmentProcessor(std::move(fp));
|
2019-08-29 20:10:13 +00:00
|
|
|
|
2021-06-04 18:40:57 +00:00
|
|
|
sdCtx->addDrawOp(GrFillRectOp::MakeNonAARect(ctx, std::move(grPaint), SkMatrix::I(), bounds));
|
2019-08-29 20:10:13 +00:00
|
|
|
}
|