Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
dart_ui.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/lib/ui/dart_ui.h"
6
7#include <mutex>
8#include <string_view>
9
10#include "flutter/common/constants.h"
11#include "flutter/common/settings.h"
12#include "flutter/fml/build_config.h"
13#include "flutter/lib/ui/compositing/scene.h"
14#include "flutter/lib/ui/compositing/scene_builder.h"
15#include "flutter/lib/ui/dart_runtime_hooks.h"
16#include "flutter/lib/ui/isolate_name_server/isolate_name_server_natives.h"
17#include "flutter/lib/ui/painting/canvas.h"
18#include "flutter/lib/ui/painting/codec.h"
19#include "flutter/lib/ui/painting/color_filter.h"
20#include "flutter/lib/ui/painting/engine_layer.h"
21#include "flutter/lib/ui/painting/fragment_program.h"
22#include "flutter/lib/ui/painting/fragment_shader.h"
23#include "flutter/lib/ui/painting/gradient.h"
24#include "flutter/lib/ui/painting/image.h"
25#include "flutter/lib/ui/painting/image_descriptor.h"
26#include "flutter/lib/ui/painting/image_filter.h"
27#include "flutter/lib/ui/painting/image_shader.h"
28#include "flutter/lib/ui/painting/immutable_buffer.h"
29#include "flutter/lib/ui/painting/path.h"
30#include "flutter/lib/ui/painting/path_measure.h"
31#include "flutter/lib/ui/painting/picture.h"
32#include "flutter/lib/ui/painting/picture_recorder.h"
33#include "flutter/lib/ui/painting/vertices.h"
34#include "flutter/lib/ui/semantics/semantics_update.h"
35#include "flutter/lib/ui/semantics/semantics_update_builder.h"
36#include "flutter/lib/ui/semantics/string_attribute.h"
37#include "flutter/lib/ui/text/font_collection.h"
38#include "flutter/lib/ui/text/paragraph.h"
39#include "flutter/lib/ui/text/paragraph_builder.h"
40#include "flutter/lib/ui/window/platform_configuration.h"
41#include "flutter/lib/ui/window/platform_isolate.h"
45
46#ifdef IMPELLER_ENABLE_3D
47#include "flutter/lib/ui/painting/scene/scene_node.h"
48#include "flutter/lib/ui/painting/scene/scene_shader.h"
49#endif // IMPELLER_ENABLE_3D
50
51using tonic::ToDart;
52
53namespace flutter {
54
59
60// List of native static functions used as @Native functions.
61// Items are tuples of ('function_name', 'parameter_count'), where:
62// 'function_name' is the fully qualified name of the native function.
63// 'parameter_count' is the number of parameters the function has.
64//
65// These are used to:
66// - Instantiate FfiDispatcher templates to automatically create FFI Native
67// bindings.
68// If the name does not match a native function, the template will fail to
69// instatiate, resulting in a compile time error.
70// - Resolve the native function pointer associated with an @Native function.
71// If there is a mismatch between name or parameter count an @Native is
72// trying to resolve, an exception will be thrown.
73#define FFI_FUNCTION_LIST(V) \
74 /* Constructors */ \
75 V(Canvas::Create) \
76 V(ColorFilter::Create) \
77 V(FragmentProgram::Create) \
78 V(ReusableFragmentShader::Create) \
79 V(Gradient::Create) \
80 V(ImageFilter::Create) \
81 V(ImageShader::Create) \
82 V(ParagraphBuilder::Create) \
83 V(PathMeasure::Create) \
84 V(Path::Create) \
85 V(PictureRecorder::Create) \
86 V(SceneBuilder::Create) \
87 V(SemanticsUpdateBuilder::Create) \
88 /* Other */ \
89 V(FontCollection::LoadFontFromList) \
90 V(ImageDescriptor::initEncoded) \
91 V(ImmutableBuffer::init) \
92 V(ImmutableBuffer::initFromAsset) \
93 V(ImmutableBuffer::initFromFile) \
94 V(ImageDescriptor::initRaw) \
95 V(IsolateNameServerNatives::LookupPortByName) \
96 V(IsolateNameServerNatives::RegisterPortWithName) \
97 V(IsolateNameServerNatives::RemovePortNameMapping) \
98 V(NativeStringAttribute::initLocaleStringAttribute) \
99 V(NativeStringAttribute::initSpellOutStringAttribute) \
100 V(PlatformConfigurationNativeApi::DefaultRouteName) \
101 V(PlatformConfigurationNativeApi::ScheduleFrame) \
102 V(PlatformConfigurationNativeApi::EndWarmUpFrame) \
103 V(PlatformConfigurationNativeApi::Render) \
104 V(PlatformConfigurationNativeApi::UpdateSemantics) \
105 V(PlatformConfigurationNativeApi::SetNeedsReportTimings) \
106 V(PlatformConfigurationNativeApi::SetIsolateDebugName) \
107 V(PlatformConfigurationNativeApi::RequestDartPerformanceMode) \
108 V(PlatformConfigurationNativeApi::GetPersistentIsolateData) \
109 V(PlatformConfigurationNativeApi::ComputePlatformResolvedLocale) \
110 V(PlatformConfigurationNativeApi::SendPlatformMessage) \
111 V(PlatformConfigurationNativeApi::RespondToPlatformMessage) \
112 V(PlatformConfigurationNativeApi::GetRootIsolateToken) \
113 V(PlatformConfigurationNativeApi::RegisterBackgroundIsolate) \
114 V(PlatformConfigurationNativeApi::SendPortPlatformMessage) \
115 V(PlatformConfigurationNativeApi::SendChannelUpdate) \
116 V(PlatformConfigurationNativeApi::GetScaledFontSize) \
117 V(PlatformIsolateNativeApi::IsRunningOnPlatformThread) \
118 V(PlatformIsolateNativeApi::Spawn) \
119 V(DartRuntimeHooks::Logger_PrintDebugString) \
120 V(DartRuntimeHooks::Logger_PrintString) \
121 V(DartRuntimeHooks::ScheduleMicrotask) \
122 V(DartRuntimeHooks::GetCallbackHandle) \
123 V(DartRuntimeHooks::GetCallbackFromHandle) \
124 V(DartPluginRegistrant_EnsureInitialized) \
125 V(Vertices::init)
126
127// List of native instance methods used as @Native functions.
128// Items are tuples of ('class_name', 'method_name', 'parameter_count'), where:
129// 'class_name' is the name of the class containing the method.
130// 'method_name' is the name of the method.
131// 'parameter_count' is the number of parameters the method has including the
132// implicit `this` parameter.
133//
134// These are used to:
135// - Instantiate FfiDispatcher templates to automatically create FFI Native
136// bindings.
137// If the name does not match a native function, the template will fail to
138// instatiate, resulting in a compile time error.
139// - Resolve the native function pointer associated with an @Native function.
140// If there is a mismatch between names or parameter count an @Native is
141// trying to resolve, an exception will be thrown.
142#define FFI_METHOD_LIST(V) \
143 V(Canvas, clipPath) \
144 V(Canvas, clipRect) \
145 V(Canvas, clipRRect) \
146 V(Canvas, drawArc) \
147 V(Canvas, drawAtlas) \
148 V(Canvas, drawCircle) \
149 V(Canvas, drawColor) \
150 V(Canvas, drawDRRect) \
151 V(Canvas, drawImage) \
152 V(Canvas, drawImageNine) \
153 V(Canvas, drawImageRect) \
154 V(Canvas, drawLine) \
155 V(Canvas, drawOval) \
156 V(Canvas, drawPaint) \
157 V(Canvas, drawPath) \
158 V(Canvas, drawPicture) \
159 V(Canvas, drawPoints) \
160 V(Canvas, drawRRect) \
161 V(Canvas, drawRect) \
162 V(Canvas, drawShadow) \
163 V(Canvas, drawVertices) \
164 V(Canvas, getDestinationClipBounds) \
165 V(Canvas, getLocalClipBounds) \
166 V(Canvas, getSaveCount) \
167 V(Canvas, getTransform) \
168 V(Canvas, restore) \
169 V(Canvas, restoreToCount) \
170 V(Canvas, rotate) \
171 V(Canvas, save) \
172 V(Canvas, saveLayer) \
173 V(Canvas, saveLayerWithoutBounds) \
174 V(Canvas, scale) \
175 V(Canvas, skew) \
176 V(Canvas, transform) \
177 V(Canvas, translate) \
178 V(Codec, dispose) \
179 V(Codec, frameCount) \
180 V(Codec, getNextFrame) \
181 V(Codec, repetitionCount) \
182 V(ColorFilter, initLinearToSrgbGamma) \
183 V(ColorFilter, initMatrix) \
184 V(ColorFilter, initMode) \
185 V(ColorFilter, initSrgbToLinearGamma) \
186 V(EngineLayer, dispose) \
187 V(FragmentProgram, initFromAsset) \
188 V(ReusableFragmentShader, Dispose) \
189 V(ReusableFragmentShader, SetImageSampler) \
190 V(ReusableFragmentShader, ValidateSamplers) \
191 V(Gradient, initLinear) \
192 V(Gradient, initRadial) \
193 V(Gradient, initSweep) \
194 V(Gradient, initTwoPointConical) \
195 V(Image, dispose) \
196 V(Image, width) \
197 V(Image, height) \
198 V(Image, toByteData) \
199 V(Image, colorSpace) \
200 V(ImageDescriptor, bytesPerPixel) \
201 V(ImageDescriptor, dispose) \
202 V(ImageDescriptor, height) \
203 V(ImageDescriptor, instantiateCodec) \
204 V(ImageDescriptor, width) \
205 V(ImageFilter, initBlur) \
206 V(ImageFilter, initDilate) \
207 V(ImageFilter, initErode) \
208 V(ImageFilter, initColorFilter) \
209 V(ImageFilter, initComposeFilter) \
210 V(ImageFilter, initMatrix) \
211 V(ImageShader, dispose) \
212 V(ImageShader, initWithImage) \
213 V(ImmutableBuffer, dispose) \
214 V(ImmutableBuffer, length) \
215 V(ParagraphBuilder, addPlaceholder) \
216 V(ParagraphBuilder, addText) \
217 V(ParagraphBuilder, build) \
218 V(ParagraphBuilder, pop) \
219 V(ParagraphBuilder, pushStyle) \
220 V(Paragraph, alphabeticBaseline) \
221 V(Paragraph, computeLineMetrics) \
222 V(Paragraph, didExceedMaxLines) \
223 V(Paragraph, dispose) \
224 V(Paragraph, getClosestGlyphInfo) \
225 V(Paragraph, getGlyphInfoAt) \
226 V(Paragraph, getLineBoundary) \
227 V(Paragraph, getLineMetricsAt) \
228 V(Paragraph, getLineNumberAt) \
229 V(Paragraph, getNumberOfLines) \
230 V(Paragraph, getPositionForOffset) \
231 V(Paragraph, getRectsForPlaceholders) \
232 V(Paragraph, getRectsForRange) \
233 V(Paragraph, getWordBoundary) \
234 V(Paragraph, height) \
235 V(Paragraph, ideographicBaseline) \
236 V(Paragraph, layout) \
237 V(Paragraph, longestLine) \
238 V(Paragraph, maxIntrinsicWidth) \
239 V(Paragraph, minIntrinsicWidth) \
240 V(Paragraph, paint) \
241 V(Paragraph, width) \
242 V(PathMeasure, setPath) \
243 V(PathMeasure, getLength) \
244 V(PathMeasure, getPosTan) \
245 V(PathMeasure, getSegment) \
246 V(PathMeasure, isClosed) \
247 V(PathMeasure, nextContour) \
248 V(Path, addArc) \
249 V(Path, addOval) \
250 V(Path, addPath) \
251 V(Path, addPathWithMatrix) \
252 V(Path, addPolygon) \
253 V(Path, addRRect) \
254 V(Path, addRect) \
255 V(Path, arcTo) \
256 V(Path, arcToPoint) \
257 V(Path, clone) \
258 V(Path, close) \
259 V(Path, conicTo) \
260 V(Path, contains) \
261 V(Path, cubicTo) \
262 V(Path, extendWithPath) \
263 V(Path, extendWithPathAndMatrix) \
264 V(Path, getBounds) \
265 V(Path, getFillType) \
266 V(Path, lineTo) \
267 V(Path, moveTo) \
268 V(Path, op) \
269 V(Path, quadraticBezierTo) \
270 V(Path, relativeArcToPoint) \
271 V(Path, relativeConicTo) \
272 V(Path, relativeCubicTo) \
273 V(Path, relativeLineTo) \
274 V(Path, relativeMoveTo) \
275 V(Path, relativeQuadraticBezierTo) \
276 V(Path, reset) \
277 V(Path, setFillType) \
278 V(Path, shift) \
279 V(Path, transform) \
280 V(PictureRecorder, endRecording) \
281 V(Picture, GetAllocationSize) \
282 V(Picture, dispose) \
283 V(Picture, toImage) \
284 V(Picture, toImageSync) \
285 V(SceneBuilder, addPerformanceOverlay) \
286 V(SceneBuilder, addPicture) \
287 V(SceneBuilder, addPlatformView) \
288 V(SceneBuilder, addRetained) \
289 V(SceneBuilder, addTexture) \
290 V(SceneBuilder, build) \
291 V(SceneBuilder, pop) \
292 V(SceneBuilder, pushBackdropFilter) \
293 V(SceneBuilder, pushClipPath) \
294 V(SceneBuilder, pushClipRRect) \
295 V(SceneBuilder, pushClipRect) \
296 V(SceneBuilder, pushColorFilter) \
297 V(SceneBuilder, pushImageFilter) \
298 V(SceneBuilder, pushOffset) \
299 V(SceneBuilder, pushOpacity) \
300 V(SceneBuilder, pushShaderMask) \
301 V(SceneBuilder, pushTransformHandle) \
302 V(SceneBuilder, setCheckerboardOffscreenLayers) \
303 V(SceneBuilder, setCheckerboardRasterCacheImages) \
304 V(SceneBuilder, setRasterizerTracingThreshold) \
305 V(Scene, dispose) \
306 V(Scene, toImage) \
307 V(Scene, toImageSync) \
308 V(SemanticsUpdateBuilder, build) \
309 V(SemanticsUpdateBuilder, updateCustomAction) \
310 V(SemanticsUpdateBuilder, updateNode) \
311 V(SemanticsUpdate, dispose) \
312 V(Vertices, dispose)
313
314#ifdef IMPELLER_ENABLE_3D
315
316#define FFI_FUNCTION_LIST_3D(V) \
317 V(SceneNode::Create) \
318 V(SceneShader::Create)
319
320#define FFI_METHOD_LIST_3D(V) \
321 V(SceneNode, initFromAsset) \
322 V(SceneNode, initFromTransform) \
323 V(SceneNode, AddChild) \
324 V(SceneNode, SetTransform) \
325 V(SceneNode, SetAnimationState) \
326 V(SceneNode, SeekAnimation) \
327 V(SceneShader, SetCameraTransform) \
328 V(SceneShader, Dispose)
329
330#endif // IMPELLER_ENABLE_3D
331
332#define FFI_FUNCTION_INSERT(FUNCTION) \
333 g_function_dispatchers.insert(std::make_pair( \
334 std::string_view(#FUNCTION), \
335 reinterpret_cast<void*>( \
336 tonic::FfiDispatcher<void, decltype(&FUNCTION), &FUNCTION>::Call)));
337
338#define FFI_METHOD_INSERT(CLASS, METHOD) \
339 g_function_dispatchers.insert( \
340 std::make_pair(std::string_view(#CLASS "::" #METHOD), \
341 reinterpret_cast<void*>( \
342 tonic::FfiDispatcher<CLASS, decltype(&CLASS::METHOD), \
343 &CLASS::METHOD>::Call)));
344
345namespace {
346
347std::once_flag g_dispatchers_init_flag;
348std::unordered_map<std::string_view, void*> g_function_dispatchers;
349
350void* ResolveFfiNativeFunction(const char* name, uintptr_t args) {
351 auto it = g_function_dispatchers.find(name);
352 return (it != g_function_dispatchers.end()) ? it->second : nullptr;
353}
354
355void InitDispatcherMap() {
358
359#ifdef IMPELLER_ENABLE_3D
360 FFI_FUNCTION_LIST_3D(FFI_FUNCTION_INSERT)
361 FFI_METHOD_LIST_3D(FFI_METHOD_INSERT)
362#endif // IMPELLER_ENABLE_3D
363}
364
365} // anonymous namespace
366
367void DartUI::InitForIsolate(const Settings& settings) {
368 std::call_once(g_dispatchers_init_flag, InitDispatcherMap);
369
370 auto dart_ui = Dart_LookupLibrary(ToDart("dart:ui"));
371 if (Dart_IsError(dart_ui)) {
372 Dart_PropagateError(dart_ui);
373 }
374
375 // Set up FFI Native resolver for dart:ui.
377 Dart_SetFfiNativeResolver(dart_ui, ResolveFfiNativeFunction);
378 if (Dart_IsError(result)) {
380 }
381
382 if (settings.enable_impeller) {
383 result = Dart_SetField(dart_ui, ToDart("_impellerEnabled"), Dart_True());
384 if (Dart_IsError(result)) {
386 }
387 }
388
389 if (settings.enable_platform_isolates) {
390 result =
391 Dart_SetField(dart_ui, ToDart("_platformIsolatesEnabled"), Dart_True());
392 if (Dart_IsError(result)) {
394 }
395 }
396
397 result = Dart_SetField(dart_ui, ToDart("_implicitViewId"),
399 if (Dart_IsError(result)) {
401 }
402}
403
404} // namespace flutter
static void InitForIsolate(const Settings &settings)
Definition dart_ui.cc:367
DART_EXPORT Dart_Handle Dart_SetFfiNativeResolver(Dart_Handle library, Dart_FfiNativeResolver resolver)
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
DART_EXPORT void Dart_PropagateError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_True(void)
DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url)
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_SetField(Dart_Handle container, Dart_Handle name, Dart_Handle value)
DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
#define FFI_FUNCTION_LIST(V)
Definition dart_ui.cc:73
#define FFI_FUNCTION_INSERT(FUNCTION)
Definition dart_ui.cc:332
#define FFI_METHOD_LIST(V)
Definition dart_ui.cc:142
#define FFI_METHOD_INSERT(CLASS, METHOD)
Definition dart_ui.cc:338
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
constexpr int64_t kFlutterImplicitViewId
Definition constants.h:35
DEF_SWITCHES_START aot vmservice shared library name
Definition switches.h:32
CanvasGradient Gradient
Definition dart_ui.cc:57
CanvasPathMeasure PathMeasure
Definition dart_ui.cc:56
CanvasImage Image
Definition dart_ui.cc:55
CanvasPath Path
Definition dart_ui.cc:58
Dart_Handle ToDart(const T &object)