| 1 |
/* |
| 2 |
* Copyright 2019 Google LLC. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkImageInfo.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkShader.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkString.h" |
| 19 |
#include "include/core/SkTileMode.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "src/core/SkCanvasPriv.h" |
| 22 |
#include "src/gpu/ganesh/GrCanvas.h" |
| 23 |
#include "src/gpu/ganesh/GrCaps.h" |
| 24 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 25 |
#include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 26 |
#include "src/gpu/ganesh/SkGr.h" |
| 27 |
#include "src/gpu/ganesh/SurfaceFillContext.h" |
| 28 |
#include "src/gpu/ganesh/effects/GrSkSLFP.h" |
| 29 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 30 |
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h" |
| 31 |
#include "src/gpu/ganesh/image/GrMippedBitmap.h" |
| 32 |
#include "tools/DecodeUtils.h" |
| 33 |
#include "tools/Resources.h" |
| 34 |
#include "tools/ToolUtils.h" |
| 35 |
|
| 36 |
class SampleCoordEffect : public GrFragmentProcessor { |
| 37 |
public: |
| 38 |
inline static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0; |
| 39 |
|
| 40 |
SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child) |
| 41 |
: INHERITED(CLASS_ID, kNone_OptimizationFlags) { |
| 42 |
this->registerChild(std::move(child), SkSL::SampleUsage::Explicit()); |
| 43 |
} |
| 44 |
|
| 45 |
const char* name() const override { return "SampleCoordEffect"; } |
| 46 |
|
| 47 |
std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 48 |
SkASSERT(false); |
| 49 |
return nullptr; |
| 50 |
} |
| 51 |
|
| 52 |
void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {} |
| 53 |
|
| 54 |
bool onIsEqual(const GrFragmentProcessor&) const override { |
| 55 |
SkASSERT(false); |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
private: |
| 60 |
std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override; |
| 61 |
using INHERITED = GrFragmentProcessor; |
| 62 |
}; |
| 63 |
|
| 64 |
std::unique_ptr<GrFragmentProcessor::ProgramImpl> SampleCoordEffect::onMakeProgramImpl() const { |
| 65 |
class Impl : public ProgramImpl { |
| 66 |
public: |
| 67 |
void emitCode(EmitArgs& args) override { |
| 68 |
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 69 |
SkString s1 = this->invokeChild(0, args, "float2(sk_FragCoord.x, sk_FragCoord.y)"); |
| 70 |
SkString s2 = this->invokeChild(0, args, "float2(sk_FragCoord.x, 512-sk_FragCoord.y)"); |
| 71 |
fragBuilder->codeAppendf("return (%s + %s) / 2;\n", s1.c_str(), s2.c_str()); |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
return std::make_unique<Impl>(); |
| 76 |
} |
| 77 |
|
| 78 |
DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, rContext, canvas, 512, 512, |
| 79 |
ToolUtils::color_to_565(0xFF66AA99)) { |
| 80 |
|
| 81 |
auto sfc = skgpu::ganesh::TopDeviceSurfaceFillContext(canvas); |
| 82 |
if (!sfc) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
SkBitmap bmp; |
| 87 |
ToolUtils::GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp); |
| 88 |
auto bitmap = GrMippedBitmap(bmp); |
| 89 |
auto view = std::get<0>(GrMakeCachedBitmapProxyView( |
| 90 |
rContext, bitmap, /*label=*/"FpCoordinateOverride", skgpu::Mipmapped::kNo)); |
| 91 |
if (!view) { |
| 92 |
return; |
| 93 |
} |
| 94 |
std::unique_ptr<GrFragmentProcessor> imgFP = |
| 95 |
GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix()); |
| 96 |
auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP))); |
| 97 |
|
| 98 |
sfc->fillWithFP(std::move(fp)); |
| 99 |
} |