Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
platform_view.h
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#ifndef FLUTTER_SHELL_COMMON_PLATFORM_VIEW_H_
6#define FLUTTER_SHELL_COMMON_PLATFORM_VIEW_H_
7
8#include <functional>
9#include <memory>
10
11#include "flutter/common/graphics/texture.h"
12#include "flutter/common/task_runners.h"
13#include "flutter/flow/embedded_views.h"
14#include "flutter/flow/surface.h"
15#include "flutter/fml/macros.h"
16#include "flutter/fml/mapping.h"
17#include "flutter/fml/memory/weak_ptr.h"
18#include "flutter/lib/ui/semantics/custom_accessibility_action.h"
19#include "flutter/lib/ui/semantics/semantics_node.h"
20#include "flutter/lib/ui/window/key_data_packet.h"
21#include "flutter/lib/ui/window/platform_message.h"
22#include "flutter/lib/ui/window/pointer_data_packet.h"
23#include "flutter/lib/ui/window/pointer_data_packet_converter.h"
24#include "flutter/lib/ui/window/viewport_metrics.h"
25#include "flutter/shell/common/platform_message_handler.h"
26#include "flutter/shell/common/pointer_data_dispatcher.h"
27#include "flutter/shell/common/vsync_waiter.h"
29
30namespace impeller {
31
32class Context;
33
34} // namespace impeller
35
36namespace flutter {
37
38//------------------------------------------------------------------------------
39/// @brief Platform views are created by the shell on the platform task
40/// runner. Unless explicitly specified, all platform view methods
41/// are called on the platform task runner as well. Platform views
42/// are usually sub-classed on a per platform basis and the bulk of
43/// the window system integration happens using that subclass. Since
44/// most platform window toolkits are usually only safe to access on
45/// a single "main" thread, any interaction that requires access to
46/// the underlying platform's window toolkit is routed through the
47/// platform view associated with that shell. This involves
48/// operations like settings up and tearing down the render surface,
49/// platform messages, interacting with accessibility features on
50/// the platform, input events, etc.
51///
53 public:
54 using AddViewCallback = std::function<void(bool added)>;
55 using RemoveViewCallback = std::function<void(bool removed)>;
56 //----------------------------------------------------------------------------
57 /// @brief Used to forward events from the platform view to interested
58 /// subsystems. This forwarding is done by the shell which sets
59 /// itself up as the delegate of the platform view.
60 ///
61 class Delegate {
62 public:
65 using KeyDataResponse = std::function<void(bool)>;
66 //--------------------------------------------------------------------------
67 /// @brief Notifies the delegate that the platform view was created
68 /// with the given render surface. This surface is platform
69 /// (iOS, Android) and client-rendering API (OpenGL, Software,
70 /// Metal, Vulkan) specific. This is usually a sign to the
71 /// rasterizer to set up and begin rendering to that surface.
72 ///
73 /// @param[in] surface The surface
74 ///
75 virtual void OnPlatformViewCreated(std::unique_ptr<Surface> surface) = 0;
76
77 //--------------------------------------------------------------------------
78 /// @brief Notifies the delegate that the platform view was destroyed.
79 /// This is usually a sign to the rasterizer to suspend
80 /// rendering a previously configured surface and collect any
81 /// intermediate resources.
82 ///
83 virtual void OnPlatformViewDestroyed() = 0;
84
85 //--------------------------------------------------------------------------
86 /// @brief Notifies the delegate that the platform needs to schedule a
87 /// frame to regenerate the layer tree and redraw the surface.
88 ///
89 virtual void OnPlatformViewScheduleFrame() = 0;
90
91 /// @brief Allocate resources for a new non-implicit view and inform
92 /// Dart about the view, and on success, schedules a new frame.
93 ///
94 /// After the operation, |callback| should be invoked with whether
95 /// the operation is successful.
96 ///
97 /// Adding |kFlutterImplicitViewId| or an existing view ID should
98 /// result in failure.
99 ///
100 /// @param[in] view_id The view ID of the new view.
101 /// @param[in] viewport_metrics The initial viewport metrics for the view.
102 /// @param[in] callback The callback that's invoked once the shell
103 /// has attempted to add the view.
104 ///
105 virtual void OnPlatformViewAddView(int64_t view_id,
106 const ViewportMetrics& viewport_metrics,
108
109 /// @brief Deallocate resources for a removed view and inform
110 /// Dart about the removal.
111 ///
112 /// After the operation, |callback| should be invoked with whether
113 /// the operation is successful.
114 ///
115 /// Removing |kFlutterImplicitViewId| or an non-existent view ID
116 /// should result in failure.
117 ///
118 /// @param[in] view_id The view ID of the view to be removed.
119 /// @param[in] callback The callback that's invoked once the shell has
120 /// attempted to remove the view.
121 ///
122 virtual void OnPlatformViewRemoveView(int64_t view_id,
124
125 //--------------------------------------------------------------------------
126 /// @brief Notifies the delegate that the specified callback needs to
127 /// be invoked after the rasterizer is done rendering the next
128 /// frame. This callback will be called on the render thread and
129 /// it is caller responsibility to perform any re-threading as
130 /// necessary. Due to the asynchronous nature of rendering in
131 /// Flutter, embedders usually add a placeholder over the
132 /// contents in which Flutter is going to render when Flutter is
133 /// first initialized. This callback may be used as a signal to
134 /// remove that placeholder.
135 ///
136 /// @attention The callback will be invoked on the render thread and not
137 /// the calling thread.
138 ///
139 /// @param[in] closure The callback to execute on the next frame.
140 ///
142 const fml::closure& closure) = 0;
143
144 //--------------------------------------------------------------------------
145 /// @brief Notifies the delegate the viewport metrics of a view have
146 /// been updated. The rasterizer will need to be reconfigured to
147 /// render the frame in the updated viewport metrics.
148 ///
149 /// @param[in] view_id The ID for the view that `metrics` describes.
150 /// @param[in] metrics The updated viewport metrics.
151 ///
153 int64_t view_id,
154 const ViewportMetrics& metrics) = 0;
155
156 //--------------------------------------------------------------------------
157 /// @brief Notifies the delegate that the platform has dispatched a
158 /// platform message from the embedder to the Flutter
159 /// application. This message must be forwarded to the running
160 /// isolate hosted by the engine on the UI thread.
161 ///
162 /// @param[in] message The platform message to dispatch to the running
163 /// root isolate.
164 ///
166 std::unique_ptr<PlatformMessage> message) = 0;
167
168 //--------------------------------------------------------------------------
169 /// @brief Notifies the delegate that the platform view has encountered
170 /// a pointer event. This pointer event needs to be forwarded to
171 /// the running root isolate hosted by the engine on the UI
172 /// thread.
173 ///
174 /// @param[in] packet The pointer data packet containing multiple pointer
175 /// events.
176 ///
178 std::unique_ptr<PointerDataPacket> packet) = 0;
179
180 //--------------------------------------------------------------------------
181 /// @brief Notifies the delegate that the platform view has encountered
182 /// an accessibility related action on the specified node. This
183 /// event must be forwarded to the running root isolate hosted
184 /// by the engine on the UI thread.
185 ///
186 /// @param[in] node_id The identifier of the accessibility node.
187 /// @param[in] action The accessibility related action performed on the
188 /// node of the specified ID.
189 /// @param[in] args An optional list of argument that apply to the
190 /// specified action.
191 ///
193 int32_t node_id,
196
197 //--------------------------------------------------------------------------
198 /// @brief Notifies the delegate that the embedder has expressed an
199 /// opinion about whether the accessibility tree needs to be
200 /// enabled or disabled. This information needs to be forwarded
201 /// to the root isolate running on the UI thread.
202 ///
203 /// @param[in] enabled Whether the accessibility tree is enabled or
204 /// disabled.
205 ///
206 virtual void OnPlatformViewSetSemanticsEnabled(bool enabled) = 0;
207
208 //--------------------------------------------------------------------------
209 /// @brief Notifies the delegate that the embedder has expressed an
210 /// opinion about the features to enable in the accessibility
211 /// tree.
212 ///
213 /// The engine does not care about the accessibility feature
214 /// flags as all it does is forward this information from the
215 /// embedder to the framework. However, curious readers may
216 /// refer to `AccessibilityFeatures` in `window.dart` for
217 /// currently supported accessibility feature flags.
218 ///
219 /// @param[in] flags The features to enable in the accessibility tree.
220 ///
222
223 //--------------------------------------------------------------------------
224 /// @brief Notifies the delegate that the embedder has specified a
225 /// texture that it want the rasterizer to composite within the
226 /// Flutter layer tree. All textures must have a unique
227 /// identifier. When the rasterizer encounters an external
228 /// texture within its hierarchy, it gives the embedder a chance
229 /// to update that texture on the raster thread before it
230 /// composites the same on-screen.
231 ///
232 /// @param[in] texture The texture that is being updated by the embedder
233 /// but composited by Flutter in its own hierarchy.
234 ///
236 std::shared_ptr<Texture> texture) = 0;
237
238 //--------------------------------------------------------------------------
239 /// @brief Notifies the delegate that the embedder will no longer
240 /// attempt to composite the specified texture within the layer
241 /// tree. This allows the rasterizer to collect associated
242 /// resources.
243 ///
244 /// @param[in] texture_id The identifier of the texture to unregister. If
245 /// the texture has not been previously registered,
246 /// this call does nothing.
247 ///
249
250 //--------------------------------------------------------------------------
251 /// @brief Notifies the delegate that the embedder has updated the
252 /// contents of the texture with the specified identifier.
253 /// Typically, Flutter will only render a frame if there is an
254 /// updated layer tree. However, in cases where the layer tree
255 /// is static but one of the externally composited textures has
256 /// been updated by the embedder, the embedder needs to notify
257 /// the rasterizer to render a new frame. In such cases, the
258 /// existing layer tree may be reused with the frame composited
259 /// with all updated external textures.
260 ///
261 /// @param[in] texture_id The identifier of the texture that has been
262 /// updated.
263 ///
265 int64_t texture_id) = 0;
266
267 //--------------------------------------------------------------------------
268 /// @brief Loads the dart shared library into the dart VM. When the
269 /// dart library is loaded successfully, the dart future
270 /// returned by the originating loadLibrary() call completes.
271 ///
272 /// The Dart compiler may generate separate shared libraries
273 /// files called 'loading units' when libraries are imported
274 /// as deferred. Each of these shared libraries are identified
275 /// by a unique loading unit id. Callers should open and resolve
276 /// a SymbolMapping from the shared library. The Mappings should
277 /// be moved into this method, as ownership will be assumed by
278 /// the dart root isolate after successful loading and released
279 /// after shutdown of the root isolate. The loading unit may not
280 /// be used after isolate shutdown. If loading fails, the
281 /// mappings will be released.
282 ///
283 /// This method is paired with a RequestDartDeferredLibrary
284 /// invocation that provides the embedder with the loading unit
285 /// id of the deferred library to load.
286 ///
287 ///
288 /// @param[in] loading_unit_id The unique id of the deferred library's
289 /// loading unit.
290 ///
291 /// @param[in] snapshot_data Dart snapshot data of the loading unit's
292 /// shared library.
293 ///
294 /// @param[in] snapshot_data Dart snapshot instructions of the loading
295 /// unit's shared library.
296 ///
298 intptr_t loading_unit_id,
299 std::unique_ptr<const fml::Mapping> snapshot_data,
300 std::unique_ptr<const fml::Mapping> snapshot_instructions) = 0;
301
302 //--------------------------------------------------------------------------
303 /// @brief Indicates to the dart VM that the request to load a deferred
304 /// library with the specified loading unit id has failed.
305 ///
306 /// The dart future returned by the initiating loadLibrary()
307 /// call will complete with an error.
308 ///
309 /// @param[in] loading_unit_id The unique id of the deferred library's
310 /// loading unit, as passed in by
311 /// RequestDartDeferredLibrary.
312 ///
313 /// @param[in] error_message The error message that will appear in the
314 /// dart Future.
315 ///
316 /// @param[in] transient A transient error is a failure due to
317 /// temporary conditions such as no network.
318 /// Transient errors allow the dart VM to
319 /// re-request the same deferred library and
320 /// loading_unit_id again. Non-transient
321 /// errors are permanent and attempts to
322 /// re-request the library will instantly
323 /// complete with an error.
324 virtual void LoadDartDeferredLibraryError(intptr_t loading_unit_id,
325 const std::string error_message,
326 bool transient) = 0;
327
328 //--------------------------------------------------------------------------
329 /// @brief Replaces the asset resolver handled by the engine's
330 /// AssetManager of the specified `type` with
331 /// `updated_asset_resolver`. The matching AssetResolver is
332 /// removed and replaced with `updated_asset_resolvers`.
333 ///
334 /// AssetResolvers should be updated when the existing resolver
335 /// becomes obsolete and a newer one becomes available that
336 /// provides updated access to the same type of assets as the
337 /// existing one. This update process is meant to be performed
338 /// at runtime.
339 ///
340 /// If a null resolver is provided, nothing will be done. If no
341 /// matching resolver is found, the provided resolver will be
342 /// added to the end of the AssetManager resolvers queue. The
343 /// replacement only occurs with the first matching resolver.
344 /// Any additional matching resolvers are untouched.
345 ///
346 /// @param[in] updated_asset_resolver The asset resolver to replace the
347 /// resolver of matching type with.
348 ///
349 /// @param[in] type The type of AssetResolver to update. Only resolvers of
350 /// the specified type will be replaced by the updated
351 /// resolver.
352 ///
354 std::unique_ptr<AssetResolver> updated_asset_resolver,
356
357 //--------------------------------------------------------------------------
358 /// @brief Called by the platform view on the platform thread to get
359 /// the settings object associated with the platform view
360 /// instance.
361 ///
362 /// @return The settings.
363 ///
364 virtual const Settings& OnPlatformViewGetSettings() const = 0;
365 };
366
367 //----------------------------------------------------------------------------
368 /// @brief Creates a platform view with the specified delegate and task
369 /// runner. The base class by itself does not do much but is
370 /// suitable for use in test environments where full platform
371 /// integration may not be necessary. The platform view may only
372 /// be created, accessed and destroyed on the platform task
373 /// runner.
374 ///
375 /// @param delegate The delegate. This is typically the shell.
376 /// @param[in] task_runners The task runners used by this platform view.
377 ///
378 explicit PlatformView(Delegate& delegate, const TaskRunners& task_runners);
379
380 //----------------------------------------------------------------------------
381 /// @brief Destroys the platform view. The platform view is owned by the
382 /// shell and will be destroyed by the same on the platform tasks
383 /// runner.
384 ///
385 virtual ~PlatformView();
386
387 //----------------------------------------------------------------------------
388 /// @brief Invoked by the shell to obtain a platform specific vsync
389 /// waiter. It is optional for platforms to override this method
390 /// and provide a custom vsync waiter because a timer based
391 /// fall-back waiter is used by default. However, it is highly
392 /// recommended that platform provide their own Vsync waiter as
393 /// the timer based fall-back will not render frames aligned with
394 /// vsync boundaries.
395 ///
396 /// @attention If a timer based fall-back is used, a warning is logged to the
397 /// console. In case this method is overridden in a subclass, it
398 /// must return a valid vsync waiter. Returning null will lead to
399 /// internal errors. If a valid vsync waiter cannot be returned,
400 /// subclasses should just call the based class method instead.
401 ///
402 /// @return A vsync waiter. If is an internal error to return a null
403 /// waiter.
404 ///
405 virtual std::unique_ptr<VsyncWaiter> CreateVSyncWaiter();
406
407 //----------------------------------------------------------------------------
408 /// @brief Used by embedders to dispatch a platform message to a
409 /// running root isolate hosted by the engine. If an isolate is
410 /// not running, the message is dropped. If there is no one on the
411 /// other side listening on the channel, the message is dropped.
412 /// When a platform message is dropped, any response handles
413 /// associated with that message will be dropped as well. All
414 /// users of platform messages must assume that message may not be
415 /// delivered and/or their response handles may not be invoked.
416 /// Platform messages are not buffered.
417 ///
418 /// For embedders that wish to respond to platform message
419 /// directed from the framework to the embedder, the
420 /// `HandlePlatformMessage` method may be overridden.
421 ///
422 /// @see HandlePlatformMessage()
423 ///
424 /// @param[in] message The platform message to deliver to the root isolate.
425 ///
426 void DispatchPlatformMessage(std::unique_ptr<PlatformMessage> message);
427
428 //----------------------------------------------------------------------------
429 /// @brief Overridden by embedders to perform actions in response to
430 /// platform messages sent from the framework to the embedder.
431 /// Default implementation of this method simply returns an empty
432 /// response.
433 ///
434 /// Embedders that wish to send platform messages to the framework
435 /// may use the `DispatchPlatformMessage` method. This method is
436 /// for messages that go the other way.
437 ///
438 /// @see DispatchPlatformMessage()
439 ///
440 /// @param[in] message The message
441 ///
442 virtual void HandlePlatformMessage(std::unique_ptr<PlatformMessage> message);
443
444 //----------------------------------------------------------------------------
445 /// @brief Used by embedders to dispatch an accessibility action to a
446 /// running isolate hosted by the engine.
447 ///
448 /// @param[in] node_id The identifier of the accessibility node on which to
449 /// perform the action.
450 /// @param[in] action The action
451 /// @param[in] args The arguments
452 ///
453 void DispatchSemanticsAction(int32_t node_id,
456
457 //----------------------------------------------------------------------------
458 /// @brief Used by embedder to notify the running isolate hosted by the
459 /// engine on the UI thread that the accessibility tree needs to
460 /// be generated.
461 ///
462 /// @attention Subclasses may choose to override this method to perform
463 /// platform specific functions. However, they must call the base
464 /// class method at some point in their implementation.
465 ///
466 /// @param[in] enabled Whether the accessibility tree needs to be generated.
467 ///
468 virtual void SetSemanticsEnabled(bool enabled);
469
470 //----------------------------------------------------------------------------
471 /// @brief Used by the embedder to specify the features to enable in the
472 /// accessibility tree generated by the isolate. This information
473 /// is forwarded to the root isolate hosted by the engine on the
474 /// UI thread.
475 ///
476 /// The engine does not care about the accessibility feature flags
477 /// as all it does is forward this information from the embedder
478 /// to the framework. However, curious readers may refer to
479 /// `AccessibilityFeatures` in `window.dart` for currently
480 /// supported accessibility feature flags.
481 ///
482 /// @attention Subclasses may choose to override this method to perform
483 /// platform specific functions. However, they must call the base
484 /// class method at some point in their implementation.
485 ///
486 /// @param[in] flags The features to enable in the accessibility tree.
487 ///
488 virtual void SetAccessibilityFeatures(int32_t flags);
489
490 //----------------------------------------------------------------------------
491 /// @brief Used by the framework to tell the embedder to apply the
492 /// specified semantics node updates. The default implementation
493 /// of this method does nothing.
494 ///
495 /// @see SemanticsNode, SemticsNodeUpdates,
496 /// CustomAccessibilityActionUpdates
497 ///
498 /// @param[in] updates A map with the stable semantics node identifier as
499 /// key and the node properties as the value.
500 /// @param[in] actions A map with the stable semantics node identifier as
501 /// key and the custom node action as the value.
502 ///
503 virtual void UpdateSemantics(SemanticsNodeUpdates updates,
505
506 //----------------------------------------------------------------------------
507 /// @brief Used by the framework to tell the embedder that it has
508 /// registered a listener on a given channel.
509 ///
510 /// @param[in] name The name of the channel on which the listener has
511 /// set or cleared a listener.
512 /// @param[in] listening True if a listener has been set, false if it has
513 /// been cleared.
514 ///
515 virtual void SendChannelUpdate(const std::string& name, bool listening);
516
517 //----------------------------------------------------------------------------
518 /// @brief Used by embedders to specify the updated viewport metrics for
519 /// a view. In response to this call, on the raster thread, the
520 /// rasterizer may need to be reconfigured to the updated viewport
521 /// dimensions. On the UI thread, the framework may need to start
522 /// generating a new frame for the updated viewport metrics as
523 /// well.
524 ///
525 /// @param[in] view_id The ID for the view that `metrics` describes.
526 /// @param[in] metrics The updated viewport metrics.
527 ///
528 void SetViewportMetrics(int64_t view_id, const ViewportMetrics& metrics);
529
530 //----------------------------------------------------------------------------
531 /// @brief Used by embedders to notify the shell that a platform view
532 /// has been created. This notification is used to create a
533 /// rendering surface and pick the client rendering API to use to
534 /// render into this surface. No frames will be scheduled or
535 /// rendered before this call. The surface must remain valid till
536 /// the corresponding call to NotifyDestroyed.
537 ///
538 void NotifyCreated();
539
540 //----------------------------------------------------------------------------
541 /// @brief Used by embedders to notify the shell that the platform view
542 /// has been destroyed. This notification used to collect the
543 /// rendering surface and all associated resources. Frame
544 /// scheduling is also suspended.
545 ///
546 /// @attention Subclasses may choose to override this method to perform
547 /// platform specific functions. However, they must call the base
548 /// class method at some point in their implementation.
549 ///
550 virtual void NotifyDestroyed();
551
552 //----------------------------------------------------------------------------
553 /// @brief Used by embedders to schedule a frame. In response to this
554 /// call, the framework may need to start generating a new frame.
555 ///
556 void ScheduleFrame();
557
558 /// @brief Used by embedders to notify the shell of a new non-implicit view.
559 ///
560 /// This method notifies the shell to allocate resources and inform
561 /// Dart about the view, and on success, schedules a new frame.
562 /// Finally, it invokes |callback| with whether the operation is
563 /// successful.
564 ///
565 /// This operation is asynchronous; avoid using the view until
566 /// |callback| returns true. Callers should prepare resources for the
567 /// view (if any) in advance but be ready to clean up on failure.
568 ///
569 /// The callback is called on a different thread.
570 ///
571 /// Do not use for implicit views, which are added internally during
572 /// shell initialization. Adding |kFlutterImplicitViewId| or an
573 /// existing view ID will fail, indicated by |callback| returning
574 /// false.
575 ///
576 /// @param[in] view_id The view ID of the new view.
577 /// @param[in] viewport_metrics The initial viewport metrics for the view.
578 /// @param[in] callback The callback that's invoked once the shell
579 /// has attempted to add the view.
580 ///
581 void AddView(int64_t view_id,
582 const ViewportMetrics& viewport_metrics,
584
585 /// @brief Used by embedders to notify the shell of a removed non-implicit
586 /// view.
587 ///
588 /// This method notifies the shell to deallocate resources and inform
589 /// Dart about the removal. Finally, it invokes |callback| with
590 /// whether the operation is successful.
591 ///
592 /// This operation is asynchronous. The embedder should not deallocate
593 /// resources until the |callback| is invoked.
594 ///
595 /// The callback is called on a different thread.
596 ///
597 /// Do not use for implicit views, which are never removed throughout
598 /// the lifetime of the app.
599 /// Removing |kFlutterImplicitViewId| or an
600 /// non-existent view ID will fail, indicated by |callback| returning
601 /// false.
602 ///
603 /// @param[in] view_id The view ID of the view to be removed.
604 /// @param[in] callback The callback that's invoked once the shell has
605 /// attempted to remove the view.
606 ///
607 void RemoveView(int64_t view_id, RemoveViewCallback callback);
608
609 //----------------------------------------------------------------------------
610 /// @brief Used by the shell to obtain a Skia GPU context that is capable
611 /// of operating on the IO thread. The context must be in the same
612 /// share-group as the Skia GPU context used on the render thread.
613 /// This context will always be used on the IO thread. Because it
614 /// is in the same share-group as the separate render thread
615 /// context, any GPU resources uploaded in this context will be
616 /// visible to the render thread context (synchronization of GPU
617 /// resources is managed by Skia).
618 ///
619 /// If such context cannot be created on the IO thread, callers
620 /// may return `nullptr`. This will mean that all texture uploads
621 /// will be queued onto the render thread which will cause
622 /// performance issues. When this context is `nullptr`, an error
623 /// is logged to the console. It is highly recommended that all
624 /// platforms provide a resource context.
625 ///
626 /// @attention Unlike all other methods on the platform view, this will be
627 /// called on IO task runner.
628 ///
629 /// @return The Skia GPU context that is in the same share-group as the
630 /// main render thread GPU context. May be `nullptr` in case such
631 /// a context cannot be created.
632 ///
634
635 virtual std::shared_ptr<impeller::Context> GetImpellerContext() const;
636
637 //----------------------------------------------------------------------------
638 /// @brief Used by the shell to notify the embedder that the resource
639 /// context previously obtained via a call to
640 /// `CreateResourceContext()` is being collected. The embedder
641 /// is free to collect an platform specific resources
642 /// associated with this context.
643 ///
644 /// @attention Unlike all other methods on the platform view, this will be
645 /// called on IO task runner.
646 ///
647 virtual void ReleaseResourceContext() const;
648
649 //--------------------------------------------------------------------------
650 /// @brief Returns a platform-specific PointerDataDispatcherMaker so the
651 /// `Engine` can construct the PointerDataPacketDispatcher based
652 /// on platforms.
654
655 //----------------------------------------------------------------------------
656 /// @brief Returns a weak pointer to the platform view. Since the
657 /// platform view may only be created, accessed and destroyed
658 /// on the platform thread, any access to the platform view
659 /// from a non-platform task runner needs a weak pointer to
660 /// the platform view along with a reference to the platform
661 /// task runner. A task must be posted to the platform task
662 /// runner with the weak pointer captured in the same. The
663 /// platform view method may only be called in the posted task
664 /// once the weak pointer validity has been checked. This
665 /// method is used by callers to obtain that weak pointer.
666 ///
667 /// @return The weak pointer to the platform view.
668 ///
670
671 //----------------------------------------------------------------------------
672 /// @brief Gives embedders a chance to react to a "cold restart" of the
673 /// running isolate. The default implementation of this method
674 /// does nothing.
675 ///
676 /// While a "hot restart" patches a running isolate, a "cold
677 /// restart" restarts the root isolate in a running shell.
678 ///
679 virtual void OnPreEngineRestart() const;
680
681 //----------------------------------------------------------------------------
682 /// @brief Sets a callback that gets executed when the rasterizer renders
683 /// the next frame. Due to the asynchronous nature of
684 /// rendering in Flutter, embedders usually add a placeholder
685 /// over the contents in which Flutter is going to render when
686 /// Flutter is first initialized. This callback may be used as
687 /// a signal to remove that placeholder. The callback is
688 /// executed on the render task runner and not the platform
689 /// task runner. It is the embedder's responsibility to
690 /// re-thread as necessary.
691 ///
692 /// @attention The callback is executed on the render task runner and not the
693 /// platform task runner. Embedders must re-thread as necessary.
694 ///
695 /// @param[in] closure The callback to execute on the render thread when the
696 /// next frame gets rendered.
697 ///
698 void SetNextFrameCallback(const fml::closure& closure);
699
700 //----------------------------------------------------------------------------
701 /// @brief Dispatches pointer events from the embedder to the
702 /// framework. Each pointer data packet may contain multiple
703 /// pointer input events. Each call to this method wakes up
704 /// the UI thread.
705 ///
706 /// @param[in] packet The pointer data packet to dispatch to the framework.
707 ///
708 void DispatchPointerDataPacket(std::unique_ptr<PointerDataPacket> packet);
709
710 //--------------------------------------------------------------------------
711 /// @brief Used by the embedder to specify a texture that it wants the
712 /// rasterizer to composite within the Flutter layer tree. All
713 /// textures must have a unique identifier. When the
714 /// rasterizer encounters an external texture within its
715 /// hierarchy, it gives the embedder a chance to update that
716 /// texture on the raster thread before it composites the same
717 /// on-screen.
718 ///
719 /// @attention This method must only be called once per texture. When the
720 /// texture is updated, calling `MarkTextureFrameAvailable`
721 /// with the specified texture identifier is sufficient to
722 /// make Flutter re-render the frame with the updated texture
723 /// composited in-line.
724 ///
725 /// @see UnregisterTexture, MarkTextureFrameAvailable
726 ///
727 /// @param[in] texture The texture that is being updated by the embedder
728 /// but composited by Flutter in its own hierarchy.
729 ///
730 void RegisterTexture(std::shared_ptr<flutter::Texture> texture);
731
732 //--------------------------------------------------------------------------
733 /// @brief Used by the embedder to notify the rasterizer that it will
734 /// no longer attempt to composite the specified texture within
735 /// the layer tree. This allows the rasterizer to collect
736 /// associated resources.
737 ///
738 /// @attention This call must only be called once per texture identifier.
739 ///
740 /// @see RegisterTexture, MarkTextureFrameAvailable
741 ///
742 /// @param[in] texture_id The identifier of the texture to unregister. If
743 /// the texture has not been previously registered,
744 /// this call does nothing.
745 ///
746 void UnregisterTexture(int64_t texture_id);
747
748 //--------------------------------------------------------------------------
749 /// @brief Used by the embedder to notify the rasterizer that the context
750 /// of the previously registered texture have been updated.
751 /// Typically, Flutter will only render a frame if there is an
752 /// updated layer tree. However, in cases where the layer tree
753 /// is static but one of the externally composited textures
754 /// has been updated by the embedder, the embedder needs to
755 /// notify the rasterizer to render a new frame. In such
756 /// cases, the existing layer tree may be reused with the
757 /// frame re-composited with all updated external textures.
758 /// Unlike the calls to register and unregister the texture,
759 /// this call must be made each time a new texture frame is
760 /// available.
761 ///
762 /// @see RegisterTexture, UnregisterTexture
763 ///
764 /// @param[in] texture_id The identifier of the texture that has been
765 /// updated.
766 ///
768
769 //--------------------------------------------------------------------------
770 /// @brief Directly invokes platform-specific APIs to compute the
771 /// locale the platform would have natively resolved to.
772 ///
773 /// @param[in] supported_locale_data The vector of strings that represents
774 /// the locales supported by the app.
775 /// Each locale consists of three
776 /// strings: languageCode, countryCode,
777 /// and scriptCode in that order.
778 ///
779 /// @return A vector of 3 strings languageCode, countryCode, and
780 /// scriptCode that represents the locale selected by the
781 /// platform. Empty strings mean the value was unassigned. Empty
782 /// vector represents a null locale.
783 ///
784 virtual std::unique_ptr<std::vector<std::string>>
786 const std::vector<std::string>& supported_locale_data);
787
788 virtual std::shared_ptr<ExternalViewEmbedder> CreateExternalViewEmbedder();
789
790 //--------------------------------------------------------------------------
791 /// @brief Invoked when the dart VM requests that a deferred library
792 /// be loaded. Notifies the engine that the deferred library
793 /// identified by the specified loading unit id should be
794 /// downloaded and loaded into the Dart VM via
795 /// `LoadDartDeferredLibrary`
796 ///
797 /// Upon encountering errors or otherwise failing to load a
798 /// loading unit with the specified id, the failure should be
799 /// directly reported to dart by calling
800 /// `LoadDartDeferredLibraryFailure` to ensure the waiting dart
801 /// future completes with an error.
802 ///
803 /// @param[in] loading_unit_id The unique id of the deferred library's
804 /// loading unit. This id is to be passed
805 /// back into LoadDartDeferredLibrary
806 /// in order to identify which deferred
807 /// library to load.
808 ///
809 virtual void RequestDartDeferredLibrary(intptr_t loading_unit_id);
810
811 //--------------------------------------------------------------------------
812 /// @brief Loads the Dart shared library into the Dart VM. When the
813 /// Dart library is loaded successfully, the Dart future
814 /// returned by the originating loadLibrary() call completes.
815 ///
816 /// The Dart compiler may generate separate shared libraries
817 /// files called 'loading units' when libraries are imported
818 /// as deferred. Each of these shared libraries are identified
819 /// by a unique loading unit id. Callers should open and resolve
820 /// a SymbolMapping from the shared library. The Mappings should
821 /// be moved into this method, as ownership will be assumed by the
822 /// dart isolate after successful loading and released after
823 /// shutdown of the dart isolate. If loading fails, the mappings
824 /// will naturally go out of scope.
825 ///
826 /// This method is paired with a RequestDartDeferredLibrary
827 /// invocation that provides the embedder with the loading unit id
828 /// of the deferred library to load.
829 ///
830 ///
831 /// @param[in] loading_unit_id The unique id of the deferred library's
832 /// loading unit, as passed in by
833 /// RequestDartDeferredLibrary.
834 ///
835 /// @param[in] snapshot_data Dart snapshot data of the loading unit's
836 /// shared library.
837 ///
838 /// @param[in] snapshot_data Dart snapshot instructions of the loading
839 /// unit's shared library.
840 ///
841 virtual void LoadDartDeferredLibrary(
842 intptr_t loading_unit_id,
843 std::unique_ptr<const fml::Mapping> snapshot_data,
844 std::unique_ptr<const fml::Mapping> snapshot_instructions);
845
846 //--------------------------------------------------------------------------
847 /// @brief Indicates to the dart VM that the request to load a deferred
848 /// library with the specified loading unit id has failed.
849 ///
850 /// The dart future returned by the initiating loadLibrary() call
851 /// will complete with an error.
852 ///
853 /// @param[in] loading_unit_id The unique id of the deferred library's
854 /// loading unit, as passed in by
855 /// RequestDartDeferredLibrary.
856 ///
857 /// @param[in] error_message The error message that will appear in the
858 /// dart Future.
859 ///
860 /// @param[in] transient A transient error is a failure due to
861 /// temporary conditions such as no network.
862 /// Transient errors allow the dart VM to
863 /// re-request the same deferred library and
864 /// loading_unit_id again. Non-transient
865 /// errors are permanent and attempts to
866 /// re-request the library will instantly
867 /// complete with an error.
868 ///
869 virtual void LoadDartDeferredLibraryError(intptr_t loading_unit_id,
870 const std::string error_message,
871 bool transient);
872
873 //--------------------------------------------------------------------------
874 /// @brief Replaces the asset resolver handled by the engine's
875 /// AssetManager of the specified `type` with
876 /// `updated_asset_resolver`. The matching AssetResolver is
877 /// removed and replaced with `updated_asset_resolvers`.
878 ///
879 /// AssetResolvers should be updated when the existing resolver
880 /// becomes obsolete and a newer one becomes available that
881 /// provides updated access to the same type of assets as the
882 /// existing one. This update process is meant to be performed
883 /// at runtime.
884 ///
885 /// If a null resolver is provided, nothing will be done. If no
886 /// matching resolver is found, the provided resolver will be
887 /// added to the end of the AssetManager resolvers queue. The
888 /// replacement only occurs with the first matching resolver.
889 /// Any additional matching resolvers are untouched.
890 ///
891 /// @param[in] updated_asset_resolver The asset resolver to replace the
892 /// resolver of matching type with.
893 ///
894 /// @param[in] type The type of AssetResolver to update. Only resolvers of
895 /// the specified type will be replaced by the updated
896 /// resolver.
897 ///
898 virtual void UpdateAssetResolverByType(
899 std::unique_ptr<AssetResolver> updated_asset_resolver,
901
902 //--------------------------------------------------------------------------
903 /// @brief Creates an object that produces surfaces suitable for raster
904 /// snapshotting. The rasterizer will request this surface if no
905 /// on screen surface is currently available when an application
906 /// requests a snapshot, e.g. if `Scene.toImage` or
907 /// `Picture.toImage` are called while the application is in the
908 /// background.
909 ///
910 /// Not all backends support this kind of surface usage, and the
911 /// default implementation returns nullptr. Platforms should
912 /// override this if they can support GPU operations in the
913 /// background and support GPU resource context usage.
914 ///
915 virtual std::unique_ptr<SnapshotSurfaceProducer>
917
918 //--------------------------------------------------------------------------
919 /// @brief Specifies a delegate that will receive PlatformMessages from
920 /// Flutter to the host platform.
921 ///
922 /// @details If this returns `null` that means PlatformMessages should be sent
923 /// to the PlatformView. That is to protect legacy behavior, any embedder
924 /// that wants to support executing Platform Channel handlers on background
925 /// threads should be returning a thread-safe PlatformMessageHandler instead.
926 virtual std::shared_ptr<PlatformMessageHandler> GetPlatformMessageHandler()
927 const;
928
929 //----------------------------------------------------------------------------
930 /// @brief Get the settings for this platform view instance.
931 ///
932 /// @return The settings.
933 ///
934 const Settings& GetSettings() const;
935
936 //--------------------------------------------------------------------------
937 /// @brief Synchronously invokes platform-specific APIs to apply the
938 /// system text scaling on the given unscaled font size.
939 ///
940 /// Platforms that support this feature (currently it's only
941 /// implemented for Android SDK level 34+) will send a valid
942 /// configuration_id to potential callers, before this method can
943 /// be called.
944 ///
945 /// @param[in] unscaled_font_size The unscaled font size specified by the
946 /// app developer. The value is in logical
947 /// pixels, and is guaranteed to be finite and
948 /// non-negative.
949 /// @param[in] configuration_id The unique id of the configuration to use
950 /// for computing the scaled font size.
951 ///
952 /// @return The scaled font size in logical pixels, or -1 if the given
953 /// configuration_id did not match a valid configuration.
954 ///
955 virtual double GetScaledFontSize(double unscaled_font_size,
956 int configuration_id) const;
957
958 protected:
959 // This is the only method called on the raster task runner.
960 virtual std::unique_ptr<Surface> CreateRenderingSurface();
961
966
967 private:
969};
970
971} // namespace flutter
972
973#endif // FLUTTER_SHELL_COMMON_PLATFORM_VIEW_H_
AssetResolverType
Identifies the type of AssetResolver an instance is.
Used to forward events from the platform view to interested subsystems. This forwarding is done by th...
virtual void OnPlatformViewAddView(int64_t view_id, const ViewportMetrics &viewport_metrics, AddViewCallback callback)=0
Allocate resources for a new non-implicit view and inform Dart about the view, and on success,...
virtual void OnPlatformViewSetNextFrameCallback(const fml::closure &closure)=0
Notifies the delegate that the specified callback needs to be invoked after the rasterizer is done re...
virtual void UpdateAssetResolverByType(std::unique_ptr< AssetResolver > updated_asset_resolver, AssetResolver::AssetResolverType type)=0
Replaces the asset resolver handled by the engine's AssetManager of the specified type with updated_a...
virtual void OnPlatformViewUnregisterTexture(int64_t texture_id)=0
Notifies the delegate that the embedder will no longer attempt to composite the specified texture wit...
virtual void OnPlatformViewRegisterTexture(std::shared_ptr< Texture > texture)=0
Notifies the delegate that the embedder has specified a texture that it want the rasterizer to compos...
virtual void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr< PointerDataPacket > packet)=0
Notifies the delegate that the platform view has encountered a pointer event. This pointer event need...
virtual void OnPlatformViewRemoveView(int64_t view_id, RemoveViewCallback callback)=0
Deallocate resources for a removed view and inform Dart about the removal.
virtual void OnPlatformViewSetViewportMetrics(int64_t view_id, const ViewportMetrics &metrics)=0
Notifies the delegate the viewport metrics of a view have been updated. The rasterizer will need to b...
virtual void OnPlatformViewSetAccessibilityFeatures(int32_t flags)=0
Notifies the delegate that the embedder has expressed an opinion about the features to enable in the ...
virtual void OnPlatformViewDispatchSemanticsAction(int32_t node_id, SemanticsAction action, fml::MallocMapping args)=0
Notifies the delegate that the platform view has encountered an accessibility related action on the s...
virtual void OnPlatformViewSetSemanticsEnabled(bool enabled)=0
Notifies the delegate that the embedder has expressed an opinion about whether the accessibility tree...
virtual void OnPlatformViewCreated(std::unique_ptr< Surface > surface)=0
Notifies the delegate that the platform view was created with the given render surface....
virtual const Settings & OnPlatformViewGetSettings() const =0
Called by the platform view on the platform thread to get the settings object associated with the pla...
std::function< void(bool)> KeyDataResponse
virtual void OnPlatformViewScheduleFrame()=0
Notifies the delegate that the platform needs to schedule a frame to regenerate the layer tree and re...
PlatformView::AddViewCallback AddViewCallback
virtual void LoadDartDeferredLibrary(intptr_t loading_unit_id, std::unique_ptr< const fml::Mapping > snapshot_data, std::unique_ptr< const fml::Mapping > snapshot_instructions)=0
Loads the dart shared library into the dart VM. When the dart library is loaded successfully,...
virtual void OnPlatformViewDestroyed()=0
Notifies the delegate that the platform view was destroyed. This is usually a sign to the rasterizer ...
virtual void LoadDartDeferredLibraryError(intptr_t loading_unit_id, const std::string error_message, bool transient)=0
Indicates to the dart VM that the request to load a deferred library with the specified loading unit ...
virtual void OnPlatformViewDispatchPlatformMessage(std::unique_ptr< PlatformMessage > message)=0
Notifies the delegate that the platform has dispatched a platform message from the embedder to the Fl...
virtual void OnPlatformViewMarkTextureFrameAvailable(int64_t texture_id)=0
Notifies the delegate that the embedder has updated the contents of the texture with the specified id...
PlatformView::RemoveViewCallback RemoveViewCallback
Platform views are created by the shell on the platform task runner. Unless explicitly specified,...
void DispatchPlatformMessage(std::unique_ptr< PlatformMessage > message)
Used by embedders to dispatch a platform message to a running root isolate hosted by the engine....
virtual ~PlatformView()
Destroys the platform view. The platform view is owned by the shell and will be destroyed by the same...
std::function< void(bool removed)> RemoveViewCallback
virtual void SetAccessibilityFeatures(int32_t flags)
Used by the embedder to specify the features to enable in the accessibility tree generated by the iso...
void MarkTextureFrameAvailable(int64_t texture_id)
Used by the embedder to notify the rasterizer that the context of the previously registered texture h...
virtual void NotifyDestroyed()
Used by embedders to notify the shell that the platform view has been destroyed. This notification us...
virtual std::unique_ptr< VsyncWaiter > CreateVSyncWaiter()
Invoked by the shell to obtain a platform specific vsync waiter. It is optional for platforms to over...
virtual void UpdateAssetResolverByType(std::unique_ptr< AssetResolver > updated_asset_resolver, AssetResolver::AssetResolverType type)
Replaces the asset resolver handled by the engine's AssetManager of the specified type with updated_a...
void AddView(int64_t view_id, const ViewportMetrics &viewport_metrics, AddViewCallback callback)
Used by embedders to notify the shell of a new non-implicit view.
void DispatchSemanticsAction(int32_t node_id, SemanticsAction action, fml::MallocMapping args)
Used by embedders to dispatch an accessibility action to a running isolate hosted by the engine.
virtual void LoadDartDeferredLibrary(intptr_t loading_unit_id, std::unique_ptr< const fml::Mapping > snapshot_data, std::unique_ptr< const fml::Mapping > snapshot_instructions)
Loads the Dart shared library into the Dart VM. When the Dart library is loaded successfully,...
virtual sk_sp< GrDirectContext > CreateResourceContext() const
Used by the shell to obtain a Skia GPU context that is capable of operating on the IO thread....
virtual std::shared_ptr< ExternalViewEmbedder > CreateExternalViewEmbedder()
virtual std::unique_ptr< std::vector< std::string > > ComputePlatformResolvedLocales(const std::vector< std::string > &supported_locale_data)
Directly invokes platform-specific APIs to compute the locale the platform would have natively resolv...
std::function< void(bool added)> AddViewCallback
virtual void ReleaseResourceContext() const
Used by the shell to notify the embedder that the resource context previously obtained via a call to ...
virtual void RequestDartDeferredLibrary(intptr_t loading_unit_id)
Invoked when the dart VM requests that a deferred library be loaded. Notifies the engine that the def...
PlatformView::Delegate & delegate_
void SetNextFrameCallback(const fml::closure &closure)
Sets a callback that gets executed when the rasterizer renders the next frame. Due to the asynchronou...
virtual PointerDataDispatcherMaker GetDispatcherMaker()
Returns a platform-specific PointerDataDispatcherMaker so the Engine can construct the PointerDataPac...
void NotifyCreated()
Used by embedders to notify the shell that a platform view has been created. This notification is use...
virtual std::unique_ptr< SnapshotSurfaceProducer > CreateSnapshotSurfaceProducer()
Creates an object that produces surfaces suitable for raster snapshotting. The rasterizer will reques...
virtual void SendChannelUpdate(const std::string &name, bool listening)
Used by the framework to tell the embedder that it has registered a listener on a given channel.
fml::WeakPtr< PlatformView > GetWeakPtr() const
Returns a weak pointer to the platform view. Since the platform view may only be created,...
void RemoveView(int64_t view_id, RemoveViewCallback callback)
Used by embedders to notify the shell of a removed non-implicit view.
virtual void HandlePlatformMessage(std::unique_ptr< PlatformMessage > message)
Overridden by embedders to perform actions in response to platform messages sent from the framework t...
void SetViewportMetrics(int64_t view_id, const ViewportMetrics &metrics)
Used by embedders to specify the updated viewport metrics for a view. In response to this call,...
virtual std::unique_ptr< Surface > CreateRenderingSurface()
void RegisterTexture(std::shared_ptr< flutter::Texture > texture)
Used by the embedder to specify a texture that it wants the rasterizer to composite within the Flutte...
void DispatchPointerDataPacket(std::unique_ptr< PointerDataPacket > packet)
Dispatches pointer events from the embedder to the framework. Each pointer data packet may contain mu...
virtual void LoadDartDeferredLibraryError(intptr_t loading_unit_id, const std::string error_message, bool transient)
Indicates to the dart VM that the request to load a deferred library with the specified loading unit ...
void ScheduleFrame()
Used by embedders to schedule a frame. In response to this call, the framework may need to start gene...
virtual void UpdateSemantics(SemanticsNodeUpdates updates, CustomAccessibilityActionUpdates actions)
Used by the framework to tell the embedder to apply the specified semantics node updates....
virtual double GetScaledFontSize(double unscaled_font_size, int configuration_id) const
Synchronously invokes platform-specific APIs to apply the system text scaling on the given unscaled f...
void UnregisterTexture(int64_t texture_id)
Used by the embedder to notify the rasterizer that it will no longer attempt to composite the specifi...
fml::WeakPtrFactory< PlatformView > weak_factory_
virtual void OnPreEngineRestart() const
Gives embedders a chance to react to a "cold restart" of the running isolate. The default implementat...
virtual std::shared_ptr< PlatformMessageHandler > GetPlatformMessageHandler() const
Specifies a delegate that will receive PlatformMessages from Flutter to the host platform.
const Settings & GetSettings() const
Get the settings for this platform view instance.
const TaskRunners task_runners_
PointerDataPacketConverter pointer_data_packet_converter_
virtual void SetSemanticsEnabled(bool enabled)
Used by embedder to notify the running isolate hosted by the engine on the UI thread that the accessi...
virtual std::shared_ptr< impeller::Context > GetImpellerContext() const
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition mapping.h:144
VkSurfaceKHR surface
Definition main.cc:49
FlutterSemanticsFlag flags
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
Win32Message message
FlTexture * texture
std::unordered_map< int32_t, SemanticsNode > SemanticsNodeUpdates
std::unordered_map< int32_t, CustomAccessibilityAction > CustomAccessibilityActionUpdates
std::function< std::unique_ptr< PointerDataDispatcher >(PointerDataDispatcher::Delegate &)> PointerDataDispatcherMaker
Signature for constructing PointerDataDispatcher.
DEF_SWITCHES_START aot vmservice shared library name
Definition switches.h:32
std::function< void()> closure
Definition closure.h:14
int64_t texture_id