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"
|
2022-03-09 13:23:14 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-08-29 20:10:13 +00:00
|
|
|
#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"
|
2021-07-21 19:39:51 +00:00
|
|
|
#include "src/core/SkCanvasPriv.h"
|
2022-04-07 15:20:24 +00:00
|
|
|
#include "src/gpu/ganesh/GrCaps.h"
|
|
|
|
#include "src/gpu/ganesh/GrDirectContextPriv.h"
|
|
|
|
#include "src/gpu/ganesh/GrFragmentProcessor.h"
|
|
|
|
#include "src/gpu/ganesh/SkGr.h"
|
|
|
|
#include "src/gpu/ganesh/SurfaceFillContext.h"
|
|
|
|
#include "src/gpu/ganesh/effects/GrRRectEffect.h"
|
|
|
|
#include "src/gpu/ganesh/effects/GrSkSLFP.h"
|
|
|
|
#include "src/gpu/ganesh/effects/GrTextureEffect.h"
|
|
|
|
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
|
2019-08-29 20:10:13 +00:00
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
class SampleCoordEffect : public GrFragmentProcessor {
|
|
|
|
public:
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0;
|
2019-08-29 20:10:13 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:37:56 +00:00
|
|
|
void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
|
2019-08-29 20:10:13 +00:00
|
|
|
|
|
|
|
bool onIsEqual(const GrFragmentProcessor&) const override {
|
|
|
|
SkASSERT(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-08-09 15:23:04 +00:00
|
|
|
std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override;
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GrFragmentProcessor;
|
2019-08-29 20:10:13 +00:00
|
|
|
};
|
|
|
|
|
2021-08-09 15:23:04 +00:00
|
|
|
std::unique_ptr<GrFragmentProcessor::ProgramImpl> SampleCoordEffect::onMakeProgramImpl() const {
|
2021-08-10 17:56:13 +00:00
|
|
|
class Impl : public ProgramImpl {
|
|
|
|
public:
|
|
|
|
void emitCode(EmitArgs& args) override {
|
|
|
|
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
|
|
|
|
SkString s1 = this->invokeChild(0, args, "float2(sk_FragCoord.x, sk_FragCoord.y)");
|
|
|
|
SkString s2 = this->invokeChild(0, args, "float2(sk_FragCoord.x, 512-sk_FragCoord.y)");
|
|
|
|
fragBuilder->codeAppendf("return (%s + %s) / 2;\n", s1.c_str(), s2.c_str());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::make_unique<Impl>();
|
2019-08-29 20:10:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 19:39:51 +00:00
|
|
|
DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, rContext, canvas, 512, 512,
|
2019-08-29 20:10:13 +00:00
|
|
|
ToolUtils::color_to_565(0xFF66AA99)) {
|
2021-07-21 19:39:51 +00:00
|
|
|
|
|
|
|
auto sfc = SkCanvasPriv::TopDeviceSurfaceFillContext(canvas);
|
2021-07-30 14:59:25 +00:00
|
|
|
if (!sfc) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-21 19:39:51 +00:00
|
|
|
|
2019-08-29 20:10:13 +00:00
|
|
|
SkBitmap bmp;
|
|
|
|
GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp);
|
2021-07-07 18:15:12 +00:00
|
|
|
auto view = std::get<0>(GrMakeCachedBitmapProxyView(rContext, 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)));
|
|
|
|
|
2021-07-21 19:39:51 +00:00
|
|
|
sfc->fillWithFP(std::move(fp));
|
2019-08-29 20:10:13 +00:00
|
|
|
}
|