Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColorFilter.h" |
| 11 | #include "include/core/SkData.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkRefCnt.h" |
| 15 | #include "include/core/SkSize.h" |
| 16 | #include "include/core/SkString.h" |
Brian Osman | ee426f2 | 2020-01-02 11:55:24 -0500 | [diff] [blame] | 17 | #include "include/effects/SkRuntimeEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "tools/Resources.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 19 | |
| 20 | #include <stddef.h> |
| 21 | #include <utility> |
| 22 | |
Brian Osman | 5240e18 | 2020-08-12 10:22:34 -0400 | [diff] [blame] | 23 | const char* gLumaSrc = R"( |
Brian Osman | 767f444 | 2020-08-13 16:59:48 -0400 | [diff] [blame] | 24 | in shader input; |
| 25 | half4 main() { |
| 26 | return dot(sample(input).rgb, half3(0.3, 0.6, 0.1)).000r; |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 27 | } |
| 28 | )"; |
| 29 | |
Brian Osman | 5240e18 | 2020-08-12 10:22:34 -0400 | [diff] [blame] | 30 | const char* gLumaSrcWithCoords = R"( |
Brian Osman | 767f444 | 2020-08-13 16:59:48 -0400 | [diff] [blame] | 31 | in shader input; |
| 32 | half4 main(float2 p) { |
| 33 | return dot(sample(input).rgb, half3(0.3, 0.6, 0.1)).000r; |
Brian Osman | 5240e18 | 2020-08-12 10:22:34 -0400 | [diff] [blame] | 34 | } |
| 35 | )"; |
| 36 | |
Mike Klein | 7485ffc | 2020-10-14 14:05:50 -0500 | [diff] [blame] | 37 | // A runtime effect with a small amount of control flow (if, else, etc., and |
| 38 | // early return) that can in principle still be reduced to a single basic block. |
| 39 | // Distilled from AOSP tone mapping shaders. |
| 40 | const char* gComplex = R"( |
| 41 | in shader input; |
| 42 | half4 main() { |
| 43 | half4 color = sample(input); |
| 44 | |
| 45 | half luma = dot(color.rgb, half3(0.3, 0.6, 0.1)); |
| 46 | |
| 47 | half scale = 0; |
| 48 | |
| 49 | if (luma < 0.33333) { |
| 50 | return half4(color.rgb * 0.5, color.a); |
| 51 | } else if (luma < 0.66666) { |
| 52 | scale = 0.166666 + 2.0 * (luma - 0.33333); |
| 53 | } else { |
| 54 | scale = 0.833333 + 0.5 * (luma - 0.66666); |
| 55 | } |
| 56 | |
| 57 | return half4(color.rgb * (scale/luma), color.a); |
| 58 | } |
| 59 | )"; |
| 60 | |
| 61 | |
| 62 | DEF_SIMPLE_GM(runtimecolorfilter, canvas, 256 * 4, 256) { |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 63 | auto img = GetResourceAsImage("images/mandrill_256.png"); |
| 64 | canvas->drawImage(img, 0, 0, nullptr); |
| 65 | |
Mike Klein | 7485ffc | 2020-10-14 14:05:50 -0500 | [diff] [blame] | 66 | for (auto src : { gLumaSrc, gLumaSrcWithCoords, gComplex }) { |
Brian Osman | 5240e18 | 2020-08-12 10:22:34 -0400 | [diff] [blame] | 67 | sk_sp<SkRuntimeEffect> effect = std::get<0>(SkRuntimeEffect::Make(SkString(src))); |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 68 | SkASSERT(effect); |
Brian Osman | 5240e18 | 2020-08-12 10:22:34 -0400 | [diff] [blame] | 69 | SkPaint p; |
Brian Osman | 767f444 | 2020-08-13 16:59:48 -0400 | [diff] [blame] | 70 | sk_sp<SkColorFilter> input = nullptr; |
| 71 | p.setColorFilter(effect->makeColorFilter(nullptr, &input, 1)); |
Brian Osman | 5240e18 | 2020-08-12 10:22:34 -0400 | [diff] [blame] | 72 | canvas->translate(256, 0); |
| 73 | canvas->drawImage(img, 0, 0, &p); |
| 74 | } |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 75 | } |