• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 #ifndef SkRuntimeEffectPriv_DEFINED
9 #define SkRuntimeEffectPriv_DEFINED
10 
11 #include "include/effects/SkRuntimeEffect.h"
12 
13 // These internal APIs for creating runtime effects vary from the public API in two ways:
14 //
15 //     1) they're used in contexts where it's not useful to receive an error message;
16 //     2) they're cached.
17 //
18 // Users of the public SkRuntimeEffect::Make*() can of course cache however they like themselves;
19 // keeping these APIs private means users will not be forced into our cache or cache policy.
20 
21 sk_sp<SkRuntimeEffect> SkMakeCachedRuntimeEffect(SkRuntimeEffect::Result (*make)(SkString sksl),
22                                                  SkString sksl);
23 
SkMakeCachedRuntimeEffect(SkRuntimeEffect::Result (* make)(SkString),const char * sksl)24 inline sk_sp<SkRuntimeEffect> SkMakeCachedRuntimeEffect(SkRuntimeEffect::Result (*make)(SkString),
25                                                         const char* sksl) {
26     return SkMakeCachedRuntimeEffect(make, SkString{sksl});
27 }
28 
29 // This is mostly from skvm's rgb->hsl code, with some GPU-related finesse pulled from
30 // GrHighContrastFilterEffect.fp, see next comment.
31 constexpr char kRGB_to_HSL_sksl[] =
32     "half3 rgb_to_hsl(half3 c) {"
33         "half mx = max(max(c.r,c.g),c.b),"
34         "     mn = min(min(c.r,c.g),c.b),"
35         "      d = mx-mn,                "
36         "   invd = 1.0 / d,              "
37         " g_lt_b = c.g < c.b ? 6.0 : 0.0;"
38 
39         // We'd prefer to write these tests like `mx == c.r`, but on some GPUs max(x,y) is
40         // not always equal to either x or y.  So we use long form, c.r >= c.g && c.r >= c.b.
41         "half h = (1/6.0) * (mx == mn                 ? 0.0 :"
42         "     /*mx==c.r*/    c.r >= c.g && c.r >= c.b ? invd * (c.g - c.b) + g_lt_b :"
43         "     /*mx==c.g*/    c.g >= c.b               ? invd * (c.b - c.r) + 2.0  "
44         "     /*mx==c.b*/                             : invd * (c.r - c.g) + 4.0);"
45 
46         "half sum = mx+mn,"
47         "       l = sum * 0.5,"
48         "       s = mx == mn ? 0.0"
49         "                    : d / (l > 0.5 ? 2.0 - sum : sum);"
50         "return half3(h,s,l);"
51     "}";
52 
53 //This is straight out of GrHSLToRGBFilterEffect.fp.
54 constexpr char kHSL_to_RGB_sksl[] =
55     "half3 hsl_to_rgb(half3 hsl) {"
56         "half  C = (1 - abs(2 * hsl.z - 1)) * hsl.y;"
57         "half3 p = hsl.xxx + half3(0, 2/3.0, 1/3.0);"
58         "half3 q = saturate(abs(fract(p) * 6 - 3) - 1);"
59         "return (q - 0.5) * C + hsl.z;"
60     "}";
61 
62 #endif
63