Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | List of all members
Demo Class Referencefinal

Public Member Functions

bool init (std::string canvasSelector, int width, int height)
 
void setKind (DemoKind kind)
 
void draw (int timestamp)
 
void drawSolidColor (SkPaint *paint)
 
void drawGradient (SkPaint *paint)
 
void drawRuntimeEffect (SkPaint *paint, int timestamp)
 

Detailed Description

Definition at line 72 of file bindings.cpp.

Member Function Documentation

◆ draw()

void Demo::draw ( int  timestamp)
inline

Definition at line 123 of file bindings.cpp.

123 {
124 GrDawnRenderTargetInfo rtInfo;
125 rtInfo.fTextureView = fCanvasSwapChain.GetCurrentTextureView();
126 rtInfo.fFormat = wgpu::TextureFormat::BGRA8Unorm;
127 rtInfo.fLevelCount = 1;
128 GrBackendRenderTarget backendRenderTarget(fWidth, fHeight, 1, 8, rtInfo);
129 SkSurfaceProps surfaceProps(0, kRGB_H_SkPixelGeometry);
130
132 backendRenderTarget,
134 kN32_SkColorType,
135 nullptr,
136 &surfaceProps);
137
139 if (fDemoKind == DemoKind::SOLID_COLOR) {
141 } else if (fDemoKind == DemoKind::GRADIENT) {
143 } else if (fDemoKind == DemoKind::RUNTIME_EFFECT) {
144 drawRuntimeEffect(&paint, timestamp);
145 }
146
147 // Schedule the recorded commands and wait until the GPU has executed them.
148 surface->getCanvas()->drawPaint(paint);
149 fContext->flushAndSubmit(surface, true);
150 fFrameCount++;
151 }
@ kTopLeft_GrSurfaceOrigin
Definition GrTypes.h:148
@ kRGB_H_SkPixelGeometry
@ RUNTIME_EFFECT
void drawGradient(SkPaint *paint)
Definition bindings.cpp:158
void drawSolidColor(SkPaint *paint)
Definition bindings.cpp:153
void drawRuntimeEffect(SkPaint *paint, int timestamp)
Definition bindings.cpp:173
void flushAndSubmit(GrSyncCpu sync=GrSyncCpu::kNo)
T * get() const
Definition SkRefCnt.h:303
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
SK_API sk_sp< SkSurface > WrapBackendRenderTarget(GrRecordingContext *context, const GrBackendRenderTarget &backendRenderTarget, GrSurfaceOrigin origin, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, RenderTargetReleaseProc releaseProc=nullptr, ReleaseContext releaseContext=nullptr)

◆ drawGradient()

void Demo::drawGradient ( SkPaint paint)
inline

Definition at line 158 of file bindings.cpp.

158 {
159 bool flipColor = fFrameCount % 2 == 0;
160 SkColor colors1[2] = {SK_ColorMAGENTA, SK_ColorCYAN};
161 SkColor colors2[2] = {SK_ColorCYAN, SK_ColorMAGENTA};
162
163 float x = (float)fWidth / 2.f;
164 float y = (float)fHeight / 2.f;
166 std::min(x, y),
167 flipColor ? colors1 : colors2,
168 nullptr,
169 2,
171 }
constexpr SkColor SK_ColorMAGENTA
Definition SkColor.h:147
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
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)
double y
double x
static constexpr SkPoint Make(float x, float y)

◆ drawRuntimeEffect()

void Demo::drawRuntimeEffect ( SkPaint paint,
int  timestamp 
)
inline

Definition at line 173 of file bindings.cpp.

173 {
174 DemoUniforms uniforms;
175 uniforms.width = fWidth;
176 uniforms.height = fHeight;
177 uniforms.time = static_cast<float>(timestamp) / 1000.f;
178
179 sk_sp<SkData> uniformData = SkData::MakeWithCopy(&uniforms, sizeof(uniforms));
180 sk_sp<SkShader> shader = fEffect->makeShader(std::move(uniformData), /*children=*/{});
181 paint->setShader(shader);
182 }
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition SkData.cpp:111
sk_sp< SkShader > makeShader(sk_sp< const SkData > uniforms, sk_sp< SkShader > children[], size_t childCount, const SkMatrix *localMatrix=nullptr) const

◆ drawSolidColor()

void Demo::drawSolidColor ( SkPaint paint)
inline

Definition at line 153 of file bindings.cpp.

153 {
154 bool flipColor = fFrameCount % 2 == 0;
155 paint->setColor(flipColor ? SK_ColorCYAN : SK_ColorMAGENTA);
156 }

◆ init()

bool Demo::init ( std::string  canvasSelector,
int  width,
int  height 
)
inline

Definition at line 74 of file bindings.cpp.

74 {
75 GrContextOptions ctxOpts;
76
77 wgpu::Device device = wgpu::Device::Acquire(emscripten_webgpu_get_device());
78 sk_sp<GrDirectContext> context = GrDirectContext::MakeDawn(device, ctxOpts);
79 if (!context) {
80 SkDebugf("Could not create GrDirectContext\n");
81 return false;
82 }
83
84 const char* sksl =
85 "uniform float2 iResolution;"
86 "uniform float iTime;"
87 "vec2 d;"
88 "float b(float a) {"
89 " return step(max(d.x, d.y), a);"
90 "}"
91 "half4 main(float2 C) {"
92 " vec4 O = vec4(0);"
93 " C.y = iResolution.y - C.y;"
94 " for (float i = 0; i < 3; ++i) {"
95 " vec2 U = C.yx / iResolution.yx;"
96 " U.y -= .5;"
97 " U.x = U.x * .4 + U.y * U.y;"
98 " U.y += U.x * sin(-iTime * 9. + i * 2. + U.x * 25.) * .2;"
99 " U.x -= asin(sin(U.y * 34.))/20.;"
100 " d = abs(U);"
101 " O += .3 * vec4(.8 * b(.3) + b(.2), b(.2), b(.1), -1.);"
102 " }"
103 " return O.xyz1;"
104 "}";
105
106 auto [effect, err] = SkRuntimeEffect::MakeForShader(SkString(sksl));
107 if (!effect) {
108 SkDebugf("Failed to compile SkSL: %s\n", err.c_str());
109 return false;
110 }
111
112 fWidth = width;
113 fHeight = height;
114 fCanvasSwapChain = getSwapChainForCanvas(device, canvasSelector, width, height);
115 fContext = context;
116 fEffect = effect;
117
118 return true;
119 }
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static wgpu::SwapChain getSwapChainForCanvas(wgpu::Device device, std::string canvasSelector, int width, int height)
Definition bindings.cpp:39
static Result MakeForShader(SkString sksl, const Options &)
VkDevice device
Definition main.cc:53
int32_t height
int32_t width

◆ setKind()

void Demo::setKind ( DemoKind  kind)
inline

Definition at line 121 of file bindings.cpp.

121{ fDemoKind = kind; }

The documentation for this class was generated from the following file: