Flutter Engine
The Flutter Engine
Classes | Enumerations | Functions
runtimeshader.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkData.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRRect.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/effects/SkGradientShader.h"
#include "include/effects/SkImageFilters.h"
#include "include/effects/SkRuntimeEffect.h"
#include "include/gpu/GrRecordingContext.h"
#include "src/base/SkRandom.h"
#include "src/core/SkColorSpacePriv.h"
#include "src/core/SkRuntimeEffectPriv.h"
#include "tools/DecodeUtils.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"

Go to the source code of this file.

Classes

class  RuntimeShaderGM
 
class  SimpleRT
 
class  ThresholdRT
 
class  SpiralRT
 
class  UnsharpRT
 
class  ColorCubeRT
 
class  ColorCubeColorFilterRT
 
class  ClipSuperRRect
 
class  LinearGradientRT
 

Enumerations

enum  RT_Flags { kAnimate_RTFlag = 0x1 , kBench_RTFlag = 0x2 , kColorFilter_RTFlag = 0x4 }
 

Functions

static sk_sp< SkShadermake_shader (sk_sp< SkImage > img, SkISize size)
 
static sk_sp< SkShadermake_threshold (SkISize size)
 
 DEF_SIMPLE_GM (child_sampling_rt, canvas, 256, 256)
 
static sk_sp< SkShadernormal_map_shader ()
 
static sk_sp< SkImagenormal_map_image ()
 
static sk_sp< SkShadernormal_map_image_shader ()
 
static sk_sp< SkShadernormal_map_raw_image_shader ()
 
static sk_sp< SkImagenormal_map_unpremul_image ()
 
static sk_sp< SkShadernormal_map_unpremul_image_shader ()
 
static sk_sp< SkShadernormal_map_raw_unpremul_image_shader ()
 
static sk_sp< SkShaderlit_shader (sk_sp< SkShader > normals)
 
static sk_sp< SkShaderlit_shader_linear (sk_sp< SkShader > normals)
 
 DEF_SIMPLE_GM (paint_alpha_normals_rt, canvas, 512, 512)
 
 DEF_SIMPLE_GM (raw_image_shader_normals_rt, canvas, 768, 512)
 
 DEF_SIMPLE_GM (lit_shader_linear_rt, canvas, 512, 256)
 
 DEF_SIMPLE_GM (local_matrix_shader_rt, canvas, 256, 256)
 
 DEF_SIMPLE_GM (null_child_rt, canvas, 150, 100)
 
 DEF_SIMPLE_GM_CAN_FAIL (deferred_shader_rt, canvas, errorMsg, 150, 50)
 
sk_sp< SkShaderpaint_color_shader ()
 
 DEF_SIMPLE_GM_CAN_FAIL (alpha_image_shader_rt, canvas, errorMsg, 350, 50)
 

Enumeration Type Documentation

◆ RT_Flags

enum RT_Flags
Enumerator
kAnimate_RTFlag 
kBench_RTFlag 
kColorFilter_RTFlag 

Definition at line 28 of file runtimeshader.cpp.

28 {
29 kAnimate_RTFlag = 0x1,
30 kBench_RTFlag = 0x2,
32};
@ kAnimate_RTFlag
@ kColorFilter_RTFlag
@ kBench_RTFlag

Function Documentation

◆ DEF_SIMPLE_GM() [1/6]

DEF_SIMPLE_GM ( child_sampling_rt  ,
canvas  ,
256  ,
256   
)

Definition at line 647 of file runtimeshader.cpp.

647 {
648 static constexpr char scale[] =
649 "uniform shader child;"
650 "half4 main(float2 xy) {"
651 " return child.eval(xy*0.1);"
652 "}";
653
654 SkPaint p;
655 p.setColor(SK_ColorRED);
656 p.setAntiAlias(true);
657 p.setStyle(SkPaint::kStroke_Style);
658 p.setStrokeWidth(1);
659
660 auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100));
661 surf->getCanvas()->drawLine(0, 0, 100, 100, p);
662 auto shader = surf->makeImageSnapshot()->makeShader(SkSamplingOptions(SkFilterMode::kLinear));
663
665 builder.child("child") = shader;
666 p.setShader(builder.makeShader());
667
668 canvas->drawPaint(p);
669}
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
static Result MakeForShader(SkString sksl, const Options &)
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SkSamplingOptions(SkFilterMode::kLinear))
const Scalar scale
static SkImageInfo MakeN32Premul(int width, int height)

◆ DEF_SIMPLE_GM() [2/6]

DEF_SIMPLE_GM ( lit_shader_linear_rt  ,
canvas  ,
512  ,
256   
)

Definition at line 831 of file runtimeshader.cpp.

831 {
832 // First, make an offscreen surface, so we can control the destination color space:
833 auto surfInfo = SkImageInfo::Make(512, 256,
834 kN32_SkColorType,
837 auto surface = canvas->makeSurface(surfInfo);
838 if (!surface) {
839 surface = SkSurfaces::Raster(surfInfo);
840 }
841
842 auto draw_shader = [](int x, int y, sk_sp<SkShader> shader, SkCanvas* canvas) {
843 SkPaint p;
844 p.setShader(shader);
845
846 canvas->save();
847 canvas->translate(x, y);
848 canvas->clipRect({0, 0, 256, 256});
849 canvas->drawPaint(p);
850 canvas->restore();
851 };
852
853 // We draw two lit spheres - one does math in the working space (so gamma-encoded). The second
854 // works in linear space, then converts to sRGB. This produces (more accurate) sharp falloff:
855 draw_shader(0, 0, lit_shader(normal_map_shader()), surface->getCanvas());
856 draw_shader(256, 0, lit_shader_linear(normal_map_shader()), surface->getCanvas());
857
858 // Now draw the offscreen surface back to our original canvas:
859 canvas->drawImage(surface->makeImageSnapshot(), 0, 0);
860}
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
static sk_sp< SkColorSpace > MakeSRGB()
VkSurfaceKHR surface
Definition: main.cc:49
double y
double x
static SkBitmap draw_shader(SkCanvas *canvas, sk_sp< SkShader > shader, bool allowRasterFallback=true)
static sk_sp< SkShader > lit_shader(sk_sp< SkShader > normals)
static sk_sp< SkShader > normal_map_shader()
static sk_sp< SkShader > lit_shader_linear(sk_sp< SkShader > normals)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)

◆ DEF_SIMPLE_GM() [3/6]

DEF_SIMPLE_GM ( local_matrix_shader_rt  ,
canvas  ,
256  ,
256   
)

Definition at line 863 of file runtimeshader.cpp.

863 {
864 SkString passthrough(R"(
865 uniform shader s;
866 half4 main(float2 p) { return s.eval(p); }
867 )");
868 auto [rte, error] = SkRuntimeEffect::MakeForShader(passthrough, {});
869 if (!rte) {
870 SkDebugf("%s\n", error.c_str());
871 return;
872 }
873
874 auto image = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
875 auto imgShader = image->makeShader(SkFilterMode::kNearest);
876
877 auto r = SkRect::MakeWH(image->width(), image->height());
878
879 auto lm = SkMatrix::RotateDeg(90.f, {image->width()/2.f, image->height()/2.f});
880
882
883 // image
884 paint.setShader(imgShader);
885 canvas->drawRect(r, paint);
886
887 // passthrough(image)
888 canvas->save();
889 canvas->translate(image->width(), 0);
890 paint.setShader(rte->makeShader(/* uniforms= */ nullptr, &imgShader, /* childCount= */ 1));
891 canvas->drawRect(r, paint);
892 canvas->restore();
893
894 // localmatrix(image)
895 canvas->save();
896 canvas->translate(0, image->height());
897 paint.setShader(imgShader->makeWithLocalMatrix(lm));
898 canvas->drawRect(r, paint);
899 canvas->restore();
900
901 // localmatrix(passthrough(image)) This was the bug.
902 canvas->save();
903 canvas->translate(image->width(), image->height());
904 paint.setShader(rte->makeShader(/* uniforms= */ nullptr, &imgShader, /* childCount= */ 1)
905 ->makeWithLocalMatrix(lm));
906 canvas->drawRect(r, paint);
907 canvas->restore();
908}
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
int width() const
Definition: SkImage.h:285
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
int height() const
Definition: SkImage.h:291
static SkMatrix RotateDeg(SkScalar deg)
Definition: SkMatrix.h:104
const Paint & paint
Definition: color_source.cc:38
const uint8_t uint32_t uint32_t GError ** error
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609

◆ DEF_SIMPLE_GM() [4/6]

DEF_SIMPLE_GM ( null_child_rt  ,
canvas  ,
150  ,
100   
)

Definition at line 910 of file runtimeshader.cpp.

910 {
911 using ChildPtr = SkRuntimeEffect::ChildPtr;
912
913 // Every swatch should evaluate to the same shade of purple.
914 // Paint with a shader evaluating a null shader.
915 // Point passed to eval() is ignored; transparent black is returned.
916 {
917 const SkString kEvalShader{R"(
918 uniform shader s;
919 half4 main(float2 p) { return s.eval(p) + half4(0.5, 0, 0.5, 1); }
920 )"};
921 auto [rtShader, error] = SkRuntimeEffect::MakeForShader(kEvalShader);
922 SkASSERT(rtShader);
923
925 ChildPtr children[1] = {ChildPtr{sk_sp<SkShader>{nullptr}}};
926 paint.setShader(rtShader->makeShader(/*uniforms=*/nullptr, children));
927 paint.setColor(SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00)); // green (ignored)
928 canvas->drawRect({0, 0, 48, 48}, paint);
929 canvas->translate(50, 0);
930 }
931 // Paint with a shader evaluating a null color filter.
932 // Color passed to eval() is returned; paint color is ignored.
933 {
934 const SkString kEvalColorFilter{R"(
935 uniform colorFilter cf;
936 half4 main(float2 p) { return cf.eval(half4(0.5, 0, 0.5, 1)); }
937 )"};
938 auto [rtShader, error] = SkRuntimeEffect::MakeForShader(kEvalColorFilter);
939 SkASSERT(rtShader);
940
942 ChildPtr children[1] = {ChildPtr{sk_sp<SkColorFilter>{nullptr}}};
943 paint.setShader(rtShader->makeShader(/*uniforms=*/nullptr, children));
944 paint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF)); // green (does not contribute)
945 canvas->drawRect({0, 0, 48, 48}, paint);
946 canvas->translate(50, 0);
947 }
948 // Paint with a shader evaluating a null blender.
949 // Colors passed to eval() are blended via src-over; paint color is ignored.
950 {
951 const SkString kEvalBlender{R"(
952 uniform blender b;
953 half4 main(float2 p) { return b.eval(half4(0.5, 0, 0, 0.5), half4(0, 0, 1, 1)); }
954 )"};
955 auto [rtShader, error] = SkRuntimeEffect::MakeForShader(kEvalBlender);
956 SkASSERT(rtShader);
957
959 ChildPtr children[1] = {ChildPtr{sk_sp<SkBlender>{nullptr}}};
960 paint.setShader(rtShader->makeShader(/*uniforms=*/nullptr, children));
961 paint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF)); // green (does not contribute)
962 canvas->drawRect({0, 0, 48, 48}, paint);
963 canvas->translate(50, 0);
964 }
965
966 canvas->translate(-150, 50);
967
968 // Paint with a color filter evaluating a null shader.
969 // Point passed to eval() is ignored; transparent black is returned.
970 {
971 const SkString kEvalShader{R"(
972 uniform shader s;
973 half4 main(half4 c) { return s.eval(float2(0)) + half4(0.5, 0, 0.5, 1); }
974 )"};
975 auto [rtFilter, error] = SkRuntimeEffect::MakeForColorFilter(kEvalShader);
976 SkASSERT(rtFilter);
977
979 ChildPtr children[1] = {ChildPtr{sk_sp<SkShader>{nullptr}}};
980 paint.setColorFilter(rtFilter->makeColorFilter(/*uniforms=*/nullptr, children));
981 paint.setColor(SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00)); // green (ignored)
982 canvas->drawRect({0, 0, 48, 48}, paint);
983 canvas->translate(50, 0);
984 }
985 // Paint with a color filter evaluating a null color filter.
986 // Color passed to eval() is returned; paint color is ignored.
987 {
988 const SkString kEvalColorFilter{R"(
989 uniform colorFilter cf;
990 half4 main(half4 c) { return cf.eval(half4(0.5, 0, 0.5, 1)); }
991 )"};
992 auto [rtFilter, error] = SkRuntimeEffect::MakeForColorFilter(kEvalColorFilter);
993 SkASSERT(rtFilter);
994
996 ChildPtr children[1] = {ChildPtr{sk_sp<SkColorFilter>{nullptr}}};
997 paint.setColorFilter(rtFilter->makeColorFilter(/*uniforms=*/nullptr, children));
998 paint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF)); // green (does not contribute)
999 canvas->drawRect({0, 0, 48, 48}, paint);
1000 canvas->translate(50, 0);
1001 }
#define SkASSERT(cond)
Definition: SkAssert.h:116
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
static Result MakeForColorFilter(SkString sksl, const Options &)

◆ DEF_SIMPLE_GM() [5/6]

DEF_SIMPLE_GM ( paint_alpha_normals_rt  ,
canvas  ,
512  ,
512   
)

Definition at line 754 of file runtimeshader.cpp.

754 {
755 // Various draws, with non-opaque paint alpha. This demonstrates several issues around how
756 // paint alpha is applied differently on CPU (globally, after all shaders) and GPU (per shader,
757 // inconsistently). See: skbug.com/11942
758 //
759 // When this works, it will be a demo of applying paint alpha to fade out a complex effect.
760 auto draw_shader = [=](int x, int y, sk_sp<SkShader> shader) {
761 SkPaint p;
762 p.setAlpha(164);
763 p.setShader(shader);
764
765 canvas->save();
766 canvas->translate(x, y);
767 canvas->clipRect({0, 0, 256, 256});
768 canvas->drawPaint(p);
769 canvas->restore();
770 };
771
774
777}
static sk_sp< SkShader > normal_map_image_shader()

◆ DEF_SIMPLE_GM() [6/6]

DEF_SIMPLE_GM ( raw_image_shader_normals_rt  ,
canvas  ,
768  ,
512   
)

Definition at line 779 of file runtimeshader.cpp.

779 {
780 // Demonstrates the utility of SkImage::makeRawShader, for non-color child shaders.
781
782 // First, make an offscreen surface, so we can control the destination color space:
783 auto surfInfo = SkImageInfo::Make(512, 512,
784 kN32_SkColorType,
786 SkColorSpace::MakeSRGB()->makeColorSpin());
787 auto surface = canvas->makeSurface(surfInfo);
788 if (!surface) {
789 surface = SkSurfaces::Raster(surfInfo);
790 }
791
792 auto draw_shader = [](int x, int y, sk_sp<SkShader> shader, SkCanvas* canvas) {
793 SkPaint p;
794 p.setShader(shader);
795
796 canvas->save();
797 canvas->translate(x, y);
798 canvas->clipRect({0, 0, 256, 256});
799 canvas->drawPaint(p);
800 canvas->restore();
801 };
802
804 rawNormals = normal_map_raw_image_shader();
805
806 // Draw our normal map as colors (will be color-rotated), and raw (untransformed)
807 draw_shader(0, 0, colorNormals, surface->getCanvas());
808 draw_shader(0, 256, rawNormals, surface->getCanvas());
809
810 // Now draw our lighting shader using the normal and raw versions of the normals as children.
811 // The top image will have the normals rotated (incorrectly), so the lighting is very dark.
812 draw_shader(256, 0, lit_shader(colorNormals), surface->getCanvas());
813 draw_shader(256, 256, lit_shader(rawNormals), surface->getCanvas());
814
815 // Now draw the offscreen surface back to our original canvas. If we do this naively, the image
816 // will be un-transformed back to the canvas' color space. That will have the effect of undoing
817 // the color spin on the upper-left, and APPLYING a color-spin on the bottom left. To preserve
818 // the intent of this GM (and make it draw consistently whether or not the original surface has
819 // a color space attached), we reinterpret the offscreen image as being in sRGB:
820 canvas->drawImage(
821 surface->makeImageSnapshot()->reinterpretColorSpace(SkColorSpace::MakeSRGB()), 0, 0);
822
823 // Finally, to demonstrate that raw unpremul image shaders don't premul, draw lighting two more
824 // times, with an unpremul normal map (containing ZERO in the alpha channel). THe top will
825 // premultiply the normals, resulting in totally dark lighting. The bottom will retain the RGB
826 // encoded normals, even with zero alpha:
829}
static sk_sp< SkShader > normal_map_raw_unpremul_image_shader()
static sk_sp< SkShader > normal_map_unpremul_image_shader()
static sk_sp< SkShader > normal_map_raw_image_shader()

◆ DEF_SIMPLE_GM_CAN_FAIL() [1/2]

DEF_SIMPLE_GM_CAN_FAIL ( alpha_image_shader_rt  ,
canvas  ,
errorMsg  ,
350  ,
50   
)

Definition at line 1044 of file runtimeshader.cpp.

1044 {});
1045 SkASSERT(shader);
1046
1047 SkPaint paint;
1048 paint.setShader(shader);
1049
1050 for (int i = 0; i < 3; ++i) {
1051 canvas->drawRect({0, 0, 50, 50}, paint);
1052 canvas->translate(50, 0);
1053 }
1054
1056}
1057
1059 SkBitmap bmp;
1063}
1064
1065DEF_SIMPLE_GM_CAN_FAIL(alpha_image_shader_rt, canvas, errorMsg, 350, 50) {
1066 if (!canvas->getSurface()) {
1067 // The only backend that really fails is DDL (because the color filter fails to evaluate on
1068 // the CPU when we do paint optimization). We can't identify DDL separate from other
1069 // recording backends, so skip this GM for all of them:
1070 *errorMsg = "Not supported in recording/DDL mode";
1072 }
1073
1074 // Skia typically applies the paint color (or input color, for more complex GPU-FP trees)
1075 // to alpha-only images. This is useful in trivial cases, but surprising and inconsistent in
1076 // more complex situations, especially when using SkSL.
1077 //
1078 // This GM checks that we suppress the paint-color tinting from SkSL, and always get {0,0,0,a}.
1080 auto paint_shader = paint_color_shader();
1081 SkRuntimeEffect::ChildPtr children[1] = { paint_shader };
1082
1083 SkPaint paint;
1084 paint.setColor({0.5f, 0, 0.5f, 1.0f});
1085
1086 auto rect = [&]() {
1087 canvas->drawRect({0, 0, 48, 48}, paint);
1088 canvas->translate(50, 0);
1089 };
1090
1091 // Two simple cases: just paint color, then the "paint color" shader.
1092 // These should both be PURPLE
1093 rect();
1094
1095 paint.setShader(paint_shader);
1096 rect();
1097
1098 // All remaining cases should be BLACK
1099
1100 // Shader that evaluates the "paint color" shader.
1101 // For color-filter and blender, we test them with and without an actual SkShader on the paint.
1102 // These should all be BLACK
1103 paint.setShader(
1104 SkRuntimeEffect::MakeForShader(SkString("uniform shader s;"
1105 "half4 main(float2 p) { return s.eval(p); }"))
1106 .effect->makeShader(/* uniforms= */ nullptr, children));
1107 rect();
1108
1109 // Color-filter that evaluates the "paint color" shader, with and without a shader on the paint
1110 paint.setShader(nullptr);
1112 SkString("uniform shader s;"
1113 "half4 main(half4 color) { return s.eval(float2(0)); }"))
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkBitmap.cpp:669
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
sk_sp< SkShader > create_checkerboard_shader(SkColor c1, SkColor c2, int size)
Definition: ToolUtils.cpp:150
DEF_SIMPLE_GM_CAN_FAIL(deferred_shader_rt, canvas, errorMsg, 150, 50)
sk_sp< SkShader > paint_color_shader()
static void checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)

◆ DEF_SIMPLE_GM_CAN_FAIL() [2/2]

DEF_SIMPLE_GM_CAN_FAIL ( deferred_shader_rt  ,
canvas  ,
errorMsg  ,
150  ,
50   
)

Definition at line 1003 of file runtimeshader.cpp.

1004 {
1005 const SkString kEvalBlender{R"(
1006 uniform blender b;
1007 half4 main(half4 c) { return b.eval(half4(0.5, 0, 0, 0.5), half4(0, 0, 1, 1)); }
1008 )"};
1009 auto [rtFilter, error] = SkRuntimeEffect::MakeForColorFilter(kEvalBlender);
1010 SkASSERT(rtFilter);
1011
1012 SkPaint paint;
1013 ChildPtr children[1] = {ChildPtr{sk_sp<SkBlender>{nullptr}}};
1014 paint.setColorFilter(rtFilter->makeColorFilter(/*uniforms=*/nullptr, children));
1015 paint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF)); // green (does not contribute)
1016 canvas->drawRect({0, 0, 48, 48}, paint);
1017 canvas->translate(50, 0);
1018 }
1019}
1020
1021DEF_SIMPLE_GM_CAN_FAIL(deferred_shader_rt, canvas, errorMsg, 150, 50) {
1022 // Skip this GM on recording devices. It actually works okay on serialize-8888, but pic-8888
1023 // does not. Ultimately, behavior on CPU is potentially strange (especially with SkRP), because
1024 // SkRP will build the shader more than once per draw.
1025 if (canvas->imageInfo().colorType() == kUnknown_SkColorType) {
1027 }
1028
1029 const SkString kShader{R"(
1030 uniform half4 color;
1031 half4 main(float2 p) { return color; }
1032 )"};
1033 auto [effect, error] = SkRuntimeEffect::MakeForShader(kShader);
1034 SkASSERT(effect);
1035
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20

◆ lit_shader()

static sk_sp< SkShader > lit_shader ( sk_sp< SkShader normals)
static

Definition at line 726 of file runtimeshader.cpp.

726 {
727 // Simple N-dot-L against a fixed, directional light:
728 static const char* kSrc = R"(
729 uniform shader normals;
730 half4 main(vec2 p) {
731 vec3 n = normalize(normals.eval(p).xyz * 2 - 1);
732 vec3 l = normalize(vec3(1, -1, 1));
733 return saturate(dot(n, l)).xxx1;
734 }
735 )";
737 return effect->makeShader(/* uniforms= */ nullptr, &normals, /* childCount= */ 1);
738}
static SkM44 normals(SkM44 m)
Definition: 3DSlide.cpp:78
sk_sp< SkShader > makeShader(sk_sp< const SkData > uniforms, sk_sp< SkShader > children[], size_t childCount, const SkMatrix *localMatrix=nullptr) const
sk_sp< SkRuntimeEffect > effect

◆ lit_shader_linear()

static sk_sp< SkShader > lit_shader_linear ( sk_sp< SkShader normals)
static

Definition at line 740 of file runtimeshader.cpp.

740 {
741 // Simple N-dot-L against a fixed, directional light, done in linear space:
742 static const char* kSrc = R"(
743 uniform shader normals;
744 half4 main(vec2 p) {
745 vec3 n = normalize(normals.eval(p).xyz * 2 - 1);
746 vec3 l = normalize(vec3(1, -1, 1));
747 return fromLinearSrgb(saturate(dot(n, l)).xxx).xxx1;
748 }
749 )";
751 return effect->makeShader(/* uniforms= */ nullptr, &normals, /* childCount= */ 1);
752}

◆ make_shader()

static sk_sp< SkShader > make_shader ( sk_sp< SkImage img,
SkISize  size 
)
static

Definition at line 92 of file runtimeshader.cpp.

92 {
93 SkMatrix scale = SkMatrix::Scale(size.width() / (float)img->width(),
94 size.height() / (float)img->height());
95 return img->makeShader(SkSamplingOptions(), scale);
96}
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition: SkMatrix.h:75
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259

◆ make_threshold()

static sk_sp< SkShader > make_threshold ( SkISize  size)
static

Definition at line 98 of file runtimeshader.cpp.

98 {
99 auto info = SkImageInfo::Make(size.width(), size.height(), kAlpha_8_SkColorType,
101 auto surf = SkSurfaces::Raster(info);
102 auto canvas = surf->getCanvas();
103
104 const SkScalar rad = 50;
107 paint.setAntiAlias(true);
108 paint.setShader(SkGradientShader::MakeRadial({0,0}, rad, colors, nullptr, 2, SkTileMode::kClamp));
109
110 SkPaint layerPaint;
111 const SkScalar sigma = 16.0f;
112 layerPaint.setImageFilter(SkImageFilters::Blur(sigma, sigma, nullptr));
113 canvas->saveLayer(nullptr, &layerPaint);
114
115 SkRandom rand;
116 for (int i = 0; i < 25; ++i) {
117 SkScalar x = rand.nextF() * size.width();
118 SkScalar y = rand.nextF() * size.height();
119 canvas->save();
120 canvas->translate(x, y);
121 canvas->drawCircle(0, 0, rad, paint);
122 canvas->restore();
123 }
124
125 canvas->restore(); // apply the blur
126
127 return surf->makeImageSnapshot()->makeShader(SkSamplingOptions());
128}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
uint32_t SkColor
Definition: SkColor.h:37
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
float nextF()
Definition: SkRandom.h:55
float SkScalar
Definition: extension.cpp:12
PODArray< SkColor > colors
Definition: SkRecords.h:276

◆ normal_map_image()

static sk_sp< SkImage > normal_map_image ( )
static

Definition at line 685 of file runtimeshader.cpp.

685 {
686 // Above, baked into an image:
687 auto info = SkImageInfo::Make(256, 256, kN32_SkColorType, kPremul_SkAlphaType);
689 SkPaint p;
690 p.setShader(normal_map_shader());
691 surface->getCanvas()->drawPaint(p);
692 return surface->makeImageSnapshot();
693}

◆ normal_map_image_shader()

static sk_sp< SkShader > normal_map_image_shader ( )
static

Definition at line 695 of file runtimeshader.cpp.

695 {
697}
static sk_sp< SkImage > normal_map_image()

◆ normal_map_raw_image_shader()

static sk_sp< SkShader > normal_map_raw_image_shader ( )
static

Definition at line 699 of file runtimeshader.cpp.

699 {
701}
sk_sp< SkShader > makeRawShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:207

◆ normal_map_raw_unpremul_image_shader()

static sk_sp< SkShader > normal_map_raw_unpremul_image_shader ( )
static

Definition at line 722 of file runtimeshader.cpp.

722 {
724}
static sk_sp< SkImage > normal_map_unpremul_image()

◆ normal_map_shader()

static sk_sp< SkShader > normal_map_shader ( )
static

Definition at line 671 of file runtimeshader.cpp.

671 {
672 // Produces a hemispherical normal:
673 static const char* kSrc = R"(
674 half4 main(vec2 p) {
675 p = (p / 256) * 2 - 1;
676 float p2 = dot(p, p);
677 vec3 v = (p2 > 1) ? vec3(0, 0, 1) : vec3(p, sqrt(1 - p2));
678 return (v * 0.5 + 0.5).xyz1;
679 }
680 )";
682 return effect->makeShader(nullptr, {});
683}

◆ normal_map_unpremul_image()

static sk_sp< SkImage > normal_map_unpremul_image ( )
static

Definition at line 703 of file runtimeshader.cpp.

703 {
704 auto image = normal_map_image();
705 SkPixmap pm;
707 SkBitmap bmp;
709 // Copy all pixels over, but set alpha to 0
710 for (int y = 0; y < pm.height(); y++) {
711 for (int x = 0; x < pm.width(); x++) {
712 *bmp.getAddr32(x, y) = *pm.addr32(x, y) & 0x00FFFFFF;
713 }
714 }
715 return bmp.asImage();
716}
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
kUnpremul_SkAlphaType
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
uint32_t * getAddr32(int x, int y) const
Definition: SkBitmap.h:1260
const SkImageInfo & imageInfo() const
Definition: SkImage.h:279
bool peekPixels(SkPixmap *pixmap) const
Definition: SkImage.cpp:34
const uint32_t * addr32() const
Definition: SkPixmap.h:352
int width() const
Definition: SkPixmap.h:160
int height() const
Definition: SkPixmap.h:166
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
Definition: SkImageInfo.h:466

◆ normal_map_unpremul_image_shader()

static sk_sp< SkShader > normal_map_unpremul_image_shader ( )
static

Definition at line 718 of file runtimeshader.cpp.

◆ paint_color_shader()

sk_sp< SkShader > paint_color_shader ( )

Definition at line 1037 of file runtimeshader.cpp.

1037 {
1038 auto result = SkData::MakeWithCopy(&color, sizeof(color));
1039 color = {color.fB, color.fR, color.fG, color.fA};
1040 return result;
1041 };
1042
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition: SkData.cpp:111
DlColor color
GAsyncResult * result