• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkCanvas.h"
10 #include "include/core/SkData.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkShader.h"
13 #include "include/core/SkSize.h"
14 #include "include/core/SkString.h"
15 #include "include/effects/SkRuntimeEffect.h"
16 
17 static const char* RUNTIME_FUNCTIONS_SRC = R"(
18     uniform half4 gColor;
19 
20     half scale(float x) {
21         return x / 255;
22     }
23 
24     half4 blackAndWhite(half4 raw) {
25         half value = raw.r * 0.22 + raw.g * 0.67 + raw.b * 0.11;
26         return half4(value.xxx, raw.a);
27     }
28 
29     half4 main(float2 p) {
30         return blackAndWhite(half4(scale(p.x), scale(p.y), gColor.b, 1));
31     }
32 )";
33 
34 class RuntimeFunctions : public skiagm::GM {
runAsBench() const35     bool runAsBench() const override { return true; }
36 
onShortName()37     SkString onShortName() override { return SkString("runtimefunctions"); }
38 
onISize()39     SkISize onISize() override { return {256, 256}; }
40 
onDraw(SkCanvas * canvas)41     void onDraw(SkCanvas* canvas) override {
42         sk_sp<SkRuntimeEffect> gEffect =
43                 SkRuntimeEffect::MakeForShader(SkString(RUNTIME_FUNCTIONS_SRC)).effect;
44         SkASSERT(gEffect);
45 
46         SkMatrix localM;
47         localM.setRotate(90, 128, 128);
48 
49         SkColor4f inputColor = { 1, 0, 0, 1 };
50         auto shader = gEffect->makeShader(SkData::MakeWithCopy(&inputColor, sizeof(inputColor)),
51                                           nullptr, 0, &localM, true);
52         SkPaint p;
53         p.setShader(std::move(shader));
54         canvas->drawRect({0, 0, 256, 256}, p);
55     }
56 };
57 
58 DEF_GM(return new RuntimeFunctions;)
59