Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 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/SkColorFilter.h" |
| 11 | #include "include/core/SkData.h" |
| 12 | #include "include/core/SkImage.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkRefCnt.h" |
| 15 | #include "include/core/SkSize.h" |
| 16 | #include "include/core/SkString.h" |
| 17 | #include "src/core/SkColorFilterPriv.h" |
| 18 | #include "src/core/SkReadBuffer.h" |
| 19 | #include "src/core/SkWriteBuffer.h" |
| 20 | #include "src/shaders/SkRTShader.h" |
| 21 | #include "tools/Resources.h" |
| 22 | |
| 23 | #include <stddef.h> |
| 24 | |
| 25 | static const char* RUNTIME_FUNCTIONS_SRC = R"( |
Ethan Nicholas | 31cff27 | 2019-09-26 13:04:48 -0400 | [diff] [blame^] | 26 | layout(ctype=SkRect) uniform half4 gColor; |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 27 | |
| 28 | half scale(float x) { |
| 29 | return half(x) / 255; |
| 30 | } |
| 31 | |
| 32 | half4 blackAndWhite(half4 raw) { |
| 33 | half value = raw.r * 0.22 + raw.g * 0.67 + raw.b * 0.11; |
| 34 | return half4(half3(value), raw.a); |
| 35 | } |
| 36 | |
| 37 | void main(float x, float y, inout half4 color) { |
| 38 | color = blackAndWhite(half4(scale(x), scale(y), gColor.b, 1)); |
| 39 | } |
| 40 | )"; |
| 41 | |
| 42 | static sk_sp<SkShader> gShader; |
| 43 | |
| 44 | class RuntimeFunctions : public skiagm::GM { |
| 45 | sk_sp<SkData> fData; |
| 46 | |
| 47 | bool runAsBench() const override { return true; } |
| 48 | |
| 49 | SkString onShortName() override { return SkString("runtimefunctions"); } |
| 50 | |
| 51 | SkISize onISize() override { return {256, 256}; } |
| 52 | |
| 53 | void onOnceBeforeDraw() override { |
| 54 | // use global to pass gl persistent cache test in dm |
| 55 | if (!gShader) { |
| 56 | SkMatrix localM; |
| 57 | localM.setRotate(90, 128, 128); |
| 58 | |
| 59 | fData = SkData::MakeUninitialized(sizeof(SkColor4f)); |
| 60 | SkColor4f* c = (SkColor4f*)fData->writable_data(); |
| 61 | *c = {1, 0, 0, 1}; |
| 62 | gShader = SkRuntimeShaderFactory(SkString(RUNTIME_FUNCTIONS_SRC), |
| 63 | true).make(fData, &localM); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void onDraw(SkCanvas* canvas) override { |
| 68 | SkPaint p; |
| 69 | p.setShader(gShader); |
| 70 | canvas->drawRect({0, 0, 256, 256}, p); |
| 71 | } |
| 72 | }; |
| 73 | DEF_GM(return new RuntimeFunctions;) |