blob: ffce619052aa62c336b0caf57dff55d5fe2b3097 [file] [log] [blame]
Brian Osman2e29ab52019-09-20 12:19:11 -04001/*
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"
Brian Osman2e29ab52019-09-20 12:19:11 -040010#include "include/core/SkData.h"
Brian Osman2e29ab52019-09-20 12:19:11 -040011#include "include/core/SkPaint.h"
Brian Osman2e29ab52019-09-20 12:19:11 -040012#include "include/core/SkSize.h"
13#include "include/core/SkString.h"
Brian Osmanee426f22020-01-02 11:55:24 -050014#include "include/effects/SkRuntimeEffect.h"
Brian Osman2e29ab52019-09-20 12:19:11 -040015
16static const char* RUNTIME_FUNCTIONS_SRC = R"(
Brian Osman9d10abe2019-12-13 16:31:40 -050017 uniform half4 gColor;
Brian Osman2e29ab52019-09-20 12:19:11 -040018
19 half scale(float x) {
20 return half(x) / 255;
21 }
22
23 half4 blackAndWhite(half4 raw) {
24 half value = raw.r * 0.22 + raw.g * 0.67 + raw.b * 0.11;
25 return half4(half3(value), raw.a);
26 }
27
28 void main(float x, float y, inout half4 color) {
29 color = blackAndWhite(half4(scale(x), scale(y), gColor.b, 1));
30 }
31)";
32
Brian Osman2e29ab52019-09-20 12:19:11 -040033class RuntimeFunctions : public skiagm::GM {
Brian Osman2e29ab52019-09-20 12:19:11 -040034 bool runAsBench() const override { return true; }
35
36 SkString onShortName() override { return SkString("runtimefunctions"); }
37
38 SkISize onISize() override { return {256, 256}; }
39
Brian Osman2e29ab52019-09-20 12:19:11 -040040 void onDraw(SkCanvas* canvas) override {
Brian Osman93de1622019-12-26 08:43:05 -050041 // static to pass gl persistent cache test in dm
42 static sk_sp<SkRuntimeEffect> gEffect =
43 std::get<0>(SkRuntimeEffect::Make(SkString(RUNTIME_FUNCTIONS_SRC)));
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);
Brian Osman2e29ab52019-09-20 12:19:11 -040052 SkPaint p;
Brian Osman93de1622019-12-26 08:43:05 -050053 p.setShader(std::move(shader));
Brian Osman2e29ab52019-09-20 12:19:11 -040054 canvas->drawRect({0, 0, 256, 256}, p);
55 }
56};
Brian Osman93de1622019-12-26 08:43:05 -050057
Brian Osman2e29ab52019-09-20 12:19:11 -040058DEF_GM(return new RuntimeFunctions;)