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