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 | |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 23 | const char* gNoop = R"( |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 24 | half4 main(half4 color) { |
| 25 | return color; |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 26 | } |
| 27 | )"; |
| 28 | |
Brian Osman | 5240e18 | 2020-08-12 10:22:34 -0400 | [diff] [blame] | 29 | const char* gLumaSrc = R"( |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 30 | half4 main(half4 color) { |
| 31 | return dot(color.rgb, half3(0.3, 0.6, 0.1)).000r; |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 32 | } |
| 33 | )"; |
| 34 | |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 35 | // Build up the same effect with increasingly complex control flow syntax. |
| 36 | // All of these are semantically equivalent and can be reduced in principle to one basic block. |
| 37 | |
| 38 | // Simplest to run; hardest to write? |
| 39 | const char* gTernary = R"( |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 40 | half4 main(half4 color) { |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 41 | half luma = dot(color.rgb, half3(0.3, 0.6, 0.1)); |
Mike Klein | 7485ffc | 2020-10-14 14:05:50 -0500 | [diff] [blame] | 42 | |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 43 | half scale = luma < 0.33333 ? 0.5 |
| 44 | : luma < 0.66666 ? (0.166666 + 2.0 * (luma - 0.33333)) / luma |
| 45 | : /* else */ (0.833333 + 0.5 * (luma - 0.66666)) / luma; |
| 46 | return half4(color.rgb * scale, color.a); |
| 47 | } |
| 48 | )"; |
| 49 | |
| 50 | // Uses conditional if statements but no early return. |
| 51 | const char* gIfs = R"( |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 52 | half4 main(half4 color) { |
Mike Klein | 7485ffc | 2020-10-14 14:05:50 -0500 | [diff] [blame] | 53 | half luma = dot(color.rgb, half3(0.3, 0.6, 0.1)); |
| 54 | |
| 55 | half scale = 0; |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 56 | if (luma < 0.33333) { |
| 57 | scale = 0.5; |
| 58 | } else if (luma < 0.66666) { |
| 59 | scale = (0.166666 + 2.0 * (luma - 0.33333)) / luma; |
| 60 | } else { |
| 61 | scale = (0.833333 + 0.5 * (luma - 0.66666)) / luma; |
| 62 | } |
| 63 | return half4(color.rgb * scale, color.a); |
| 64 | } |
| 65 | )"; |
Mike Klein | 7485ffc | 2020-10-14 14:05:50 -0500 | [diff] [blame] | 66 | |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 67 | // Distilled from AOSP tone mapping shaders, more like what people tend to write. |
| 68 | const char* gEarlyReturn = R"( |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 69 | half4 main(half4 color) { |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 70 | half luma = dot(color.rgb, half3(0.3, 0.6, 0.1)); |
| 71 | |
| 72 | half scale = 0; |
Mike Klein | 7485ffc | 2020-10-14 14:05:50 -0500 | [diff] [blame] | 73 | if (luma < 0.33333) { |
| 74 | return half4(color.rgb * 0.5, color.a); |
| 75 | } else if (luma < 0.66666) { |
| 76 | scale = 0.166666 + 2.0 * (luma - 0.33333); |
| 77 | } else { |
| 78 | scale = 0.833333 + 0.5 * (luma - 0.66666); |
| 79 | } |
Mike Klein | 7485ffc | 2020-10-14 14:05:50 -0500 | [diff] [blame] | 80 | return half4(color.rgb * (scale/luma), color.a); |
| 81 | } |
| 82 | )"; |
| 83 | |
| 84 | |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 85 | DEF_SIMPLE_GM(runtimecolorfilter, canvas, 256 * 3, 256 * 2) { |
| 86 | sk_sp<SkImage> img = GetResourceAsImage("images/mandrill_256.png"); |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 87 | |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 88 | auto draw_filter = [&](const char* src) { |
Brian Osman | f6123f1 | 2021-04-15 13:52:43 -0400 | [diff] [blame] | 89 | auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString(src)); |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 90 | if (!effect) { |
| 91 | SkDebugf("%s\n%s\n", src, err.c_str()); |
| 92 | } |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 93 | SkASSERT(effect); |
Brian Osman | 5240e18 | 2020-08-12 10:22:34 -0400 | [diff] [blame] | 94 | SkPaint p; |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 95 | p.setColorFilter(effect->makeColorFilter(nullptr)); |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 96 | canvas->drawImage(img, 0, 0, SkSamplingOptions(), &p); |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 97 | canvas->translate(256, 0); |
| 98 | }; |
| 99 | |
Brian Osman | f6123f1 | 2021-04-15 13:52:43 -0400 | [diff] [blame] | 100 | for (const char* src : { gNoop, gLumaSrc }) { |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 101 | draw_filter(src); |
| 102 | } |
Brian Osman | f6123f1 | 2021-04-15 13:52:43 -0400 | [diff] [blame] | 103 | canvas->translate(-256*2, 256); |
Mike Klein | a0f5452 | 2020-10-14 16:31:36 -0500 | [diff] [blame] | 104 | for (const char* src : { gTernary, gIfs, gEarlyReturn}) { |
| 105 | draw_filter(src); |
Brian Osman | 5240e18 | 2020-08-12 10:22:34 -0400 | [diff] [blame] | 106 | } |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 107 | } |