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