Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / fpcoordinateoverride.cpp
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/GrCaps.h"
23 #include "src/gpu/ganesh/GrDirectContextPriv.h"
24 #include "src/gpu/ganesh/GrFragmentProcessor.h"
25 #include "src/gpu/ganesh/SkGr.h"
26 #include "src/gpu/ganesh/SurfaceFillContext.h"
27 #include "src/gpu/ganesh/effects/GrRRectEffect.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 "tools/Resources.h"
32 #include "tools/ToolUtils.h"
33
34 class SampleCoordEffect : public GrFragmentProcessor {
35 public:
36     inline static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 0;
37
38     SampleCoordEffect(std::unique_ptr<GrFragmentProcessor> child)
39         : INHERITED(CLASS_ID, kNone_OptimizationFlags) {
40         this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
41     }
42
43     const char* name() const override { return "SampleCoordEffect"; }
44
45     std::unique_ptr<GrFragmentProcessor> clone() const override {
46         SkASSERT(false);
47         return nullptr;
48     }
49
50     void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
51
52     bool onIsEqual(const GrFragmentProcessor&) const override {
53         SkASSERT(false);
54         return true;
55     }
56
57 private:
58     std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override;
59     using INHERITED = GrFragmentProcessor;
60 };
61
62 std::unique_ptr<GrFragmentProcessor::ProgramImpl> SampleCoordEffect::onMakeProgramImpl() const {
63     class Impl : public ProgramImpl {
64     public:
65         void emitCode(EmitArgs& args) override {
66             GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
67             SkString s1 = this->invokeChild(0, args, "float2(sk_FragCoord.x, sk_FragCoord.y)");
68             SkString s2 = this->invokeChild(0, args, "float2(sk_FragCoord.x, 512-sk_FragCoord.y)");
69             fragBuilder->codeAppendf("return (%s + %s) / 2;\n", s1.c_str(), s2.c_str());
70         }
71     };
72
73     return std::make_unique<Impl>();
74 }
75
76 DEF_SIMPLE_GPU_GM_BG(fpcoordinateoverride, rContext, canvas, 512, 512,
77                      ToolUtils::color_to_565(0xFF66AA99)) {
78
79     auto sfc = SkCanvasPriv::TopDeviceSurfaceFillContext(canvas);
80     if (!sfc) {
81         return;
82     }
83
84     SkBitmap bmp;
85     GetResourceAsBitmap("images/mandrill_512_q075.jpg", &bmp);
86     auto view = std::get<0>(GrMakeCachedBitmapProxyView(rContext, bmp, GrMipmapped::kNo));
87     if (!view) {
88         return;
89     }
90     std::unique_ptr<GrFragmentProcessor> imgFP =
91             GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
92     auto fp = std::unique_ptr<GrFragmentProcessor>(new SampleCoordEffect(std::move(imgFP)));
93
94     sfc->fillWithFP(std::move(fp));
95 }