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