• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EMBEDDER_H_
6 #define FLUTTER_EMBEDDER_H_
7 
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <memory>
12 #if defined(__cplusplus)
13 #include <functional>
14 extern "C" {
15 #endif
16 
17 #ifndef FLUTTER_EXPORT
18 #define FLUTTER_EXPORT
19 #endif  // FLUTTER_EXPORT
20 
21 #ifdef FLUTTER_API_SYMBOL_PREFIX
22 #define FLUTTER_EMBEDDING_CONCAT(a, b) a##b
23 #define FLUTTER_EMBEDDING_ADD_PREFIX(symbol, prefix) \
24   FLUTTER_EMBEDDING_CONCAT(prefix, symbol)
25 #define FLUTTER_API_SYMBOL(symbol) \
26   FLUTTER_EMBEDDING_ADD_PREFIX(symbol, FLUTTER_API_SYMBOL_PREFIX)
27 #else
28 #define FLUTTER_API_SYMBOL(symbol) symbol
29 #endif
30 
31 #define FLUTTER_ENGINE_VERSION 1
32 
33 typedef enum {
34   kSuccess = 0,
35   kInvalidLibraryVersion,
36   kInvalidArguments,
37   kInternalInconsistency,
38 } FlutterEngineResult;
39 
40 typedef enum {
41   kOpenGL,
42   kSoftware,
43 } FlutterRendererType;
44 
45 typedef enum {
46   // Text has unknown text direction.
47   kFlutterTextDirectionUnknown = 0,
48   // Text is read from right to left.
49   kFlutterTextDirectionRTL = 1,
50   // Text is read from left to right.
51   kFlutterTextDirectionLTR = 2,
52 } FlutterTextDirection;
53 
54 typedef struct _FlutterEngine* FLUTTER_API_SYMBOL(FlutterEngine);
55 
56 typedef struct {
57   // horizontal scale factor
58   double scaleX;
59   // horizontal skew factor
60   double skewX;
61   // horizontal translation
62   double transX;
63   // vertical skew factor
64   double skewY;
65   // vertical scale factor
66   double scaleY;
67   // vertical translation
68   double transY;
69   // input x-axis perspective factor
70   double pers0;
71   // input y-axis perspective factor
72   double pers1;
73   // perspective scale factor
74   double pers2;
75 } FlutterTransformation;
76 
77 typedef void (*VoidCallback)(void* /* user data */);
78 
79 typedef enum {
80   // Specifies an OpenGL texture target type. Textures are specified using
81   // the FlutterOpenGLTexture struct.
82   kFlutterOpenGLTargetTypeTexture,
83   // Specifies an OpenGL frame-buffer target type. Framebuffers are specified
84   // using the FlutterOpenGLFramebuffer struct.
85   kFlutterOpenGLTargetTypeFramebuffer,
86 } FlutterOpenGLTargetType;
87 
88 typedef struct {
89   // Target texture of the active texture unit (example GL_TEXTURE_2D).
90   uint32_t target;
91   // The name of the texture.
92   uint32_t name;
93   // The texture format (example GL_RGBA8).
94   uint32_t format;
95   // User data to be returned on the invocation of the destruction callback.
96   void* user_data;
97   // Callback invoked (on an engine managed thread) that asks the embedder to
98   // collect the texture.
99   VoidCallback destruction_callback;
100 } FlutterOpenGLTexture;
101 
102 typedef struct {
103   // The target of the color attachment of the frame-buffer. For example,
104   // GL_TEXTURE_2D or GL_RENDERBUFFER. In case of ambiguity when dealing with
105   // Window bound frame-buffers, 0 may be used.
106   uint32_t target;
107 
108   // The name of the framebuffer.
109   uint32_t name;
110 
111   // User data to be returned on the invocation of the destruction callback.
112   void* user_data;
113 
114   // Callback invoked (on an engine managed thread) that asks the embedder to
115   // collect the framebuffer.
116   VoidCallback destruction_callback;
117 } FlutterOpenGLFramebuffer;
118 
119 // ACE PC preivew
120 using IdleCallback = std::function<void(int64_t)>;
121 typedef bool (*UserBoolCallback)(const void*, const size_t, const int32_t, const int32_t);
122 #ifdef USE_GLFW_WINDOW
123 namespace flutter {
124   class PointerDataPacket;
125 }
126 
127 using HandleTouchEventCallback =
128     std::function<bool(std::unique_ptr<flutter::PointerDataPacket>&)>;
129 #endif
130 
131 typedef bool (*BoolCallback)(void* /* user data */);
132 typedef FlutterTransformation (*TransformationCallback)(void* /* user data */);
133 typedef uint32_t (*UIntCallback)(void* /* user data */);
134 typedef bool (*SoftwareSurfacePresentCallback)(void* /* user data */,
135                                                const void* /* allocation */,
136                                                size_t /* row bytes */,
137                                                size_t /* height */);
138 typedef void* (*ProcResolver)(void* /* user data */, const char* /* name */);
139 typedef bool (*TextureFrameCallback)(void* /* user data */,
140                                      int64_t /* texture identifier */,
141                                      size_t /* width */,
142                                      size_t /* height */,
143                                      FlutterOpenGLTexture* /* texture out */);
144 typedef void (*VsyncCallback)(void* /* user data */, intptr_t /* baton */);
145 
146 typedef struct {
147   // The size of this struct. Must be sizeof(FlutterOpenGLRendererConfig).
148   size_t struct_size;
149   BoolCallback make_current;
150   BoolCallback clear_current;
151   BoolCallback present;
152   UserBoolCallback send_current_surface;  // for ACE PC preivew
153   UIntCallback fbo_callback;
154   // This is an optional callback. Flutter will ask the emebdder to create a GL
155   // context current on a background thread. If the embedder is able to do so,
156   // Flutter will assume that this context is in the same sharegroup as the main
157   // rendering context and use this context for asynchronous texture uploads.
158   // Though optional, it is recommended that all embedders set this callback as
159   // it will lead to better performance in texture handling.
160   BoolCallback make_resource_current;
161   // By default, the renderer config assumes that the FBO does not change for
162   // the duration of the engine run. If this argument is true, the
163   // engine will ask the embedder for an updated FBO target (via an fbo_callback
164   // invocation) after a present call.
165   bool fbo_reset_after_present;
166   // The transformation to apply to the render target before any rendering
167   // operations. This callback is optional.
168   TransformationCallback surface_transformation;
169   ProcResolver gl_proc_resolver;
170   // When the embedder specifies that a texture has a frame available, the
171   // engine will call this method (on an internal engine managed thread) so that
172   // external texture details can be supplied to the engine for subsequent
173   // composition.
174   TextureFrameCallback gl_external_texture_frame_callback;
175 } FlutterOpenGLRendererConfig;
176 
177 typedef struct {
178   // The size of this struct. Must be sizeof(FlutterSoftwareRendererConfig).
179   size_t struct_size;
180   // The callback presented to the embedder to present a fully populated buffer
181   // to the user. The pixel format of the buffer is the native 32-bit RGBA
182   // format. The buffer is owned by the Flutter engine and must be copied in
183   // this callback if needed.
184   SoftwareSurfacePresentCallback surface_present_callback;
185 } FlutterSoftwareRendererConfig;
186 
187 typedef struct {
188   FlutterRendererType type;
189   union {
190     FlutterOpenGLRendererConfig open_gl;
191     FlutterSoftwareRendererConfig software;
192   };
193 } FlutterRendererConfig;
194 
195 typedef struct {
196   // The size of this struct. Must be sizeof(FlutterWindowMetricsEvent).
197   size_t struct_size;
198   // Physical width of the window.
199   size_t width;
200   // Physical height of the window.
201   size_t height;
202   // Scale factor for the physical screen.
203   double pixel_ratio;
204 } FlutterWindowMetricsEvent;
205 
206 // The phase of the pointer event.
207 typedef enum {
208   kCancel,
209   // The pointer, which must have been down (see kDown), is now up.
210   //
211   // For touch, this means that the pointer is no longer in contact with the
212   // screen. For a mouse, it means the last button was released. Note that if
213   // any other buttons are still pressed when one button is released, that
214   // should be sent as a kMove rather than a kUp.
215   kUp,
216   // The pointer, which must have been been up, is now down.
217   //
218   // For touch, this means that the pointer has come into contact with the
219   // screen. For a mouse, it means a button is now pressed. Note that if any
220   // other buttons are already pressed when a new button is pressed, that should
221   // be sent as a kMove rather than a kDown.
222   kDown,
223   // The pointer moved while down.
224   //
225   // This is also used for changes in button state that don't cause a kDown or
226   // kUp, such as releasing one of two pressed buttons.
227   kMove,
228   // The pointer is now sending input to Flutter. For instance, a mouse has
229   // entered the area where the Flutter content is displayed.
230   //
231   // A pointer should always be added before sending any other events.
232   kAdd,
233   // The pointer is no longer sending input to Flutter. For instance, a mouse
234   // has left the area where the Flutter content is displayed.
235   //
236   // A removed pointer should no longer send events until sending a new kAdd.
237   kRemove,
238   // The pointer moved while up.
239   kHover,
240 } FlutterPointerPhase;
241 
242 // The device type that created a pointer event.
243 typedef enum {
244   kFlutterPointerDeviceKindMouse = 1,
245   kFlutterPointerDeviceKindTouch,
246 } FlutterPointerDeviceKind;
247 
248 // Flags for the |buttons| field of |FlutterPointerEvent| when |device_kind|
249 // is |kFlutterPointerDeviceKindMouse|.
250 typedef enum {
251   kFlutterPointerButtonMousePrimary = 1 << 0,
252   kFlutterPointerButtonMouseSecondary = 1 << 1,
253   kFlutterPointerButtonMouseMiddle = 1 << 2,
254   kFlutterPointerButtonMouseBack = 1 << 3,
255   kFlutterPointerButtonMouseForward = 1 << 4,
256   // If a mouse has more than five buttons, send higher bit shifted values
257   // corresponding to the button number: 1 << 5 for the 6th, etc.
258 } FlutterPointerMouseButtons;
259 
260 // The type of a pointer signal.
261 typedef enum {
262   kFlutterPointerSignalKindNone,
263   kFlutterPointerSignalKindScroll,
264 } FlutterPointerSignalKind;
265 
266 typedef struct {
267   // The size of this struct. Must be sizeof(FlutterPointerEvent).
268   size_t struct_size;
269   FlutterPointerPhase phase;
270   size_t timestamp;  // in microseconds.
271   double x;
272   double y;
273   // An optional device identifier. If this is not specified, it is assumed that
274   // the embedder has no multi-touch capability.
275   int32_t device;
276   FlutterPointerSignalKind signal_kind;
277   double scroll_delta_x;
278   double scroll_delta_y;
279   // The type of the device generating this event.
280   // Backwards compatibility note: If this is not set, the device will be
281   // treated as a mouse, with the primary button set for |kDown| and |kMove|.
282   // If set explicitly to |kFlutterPointerDeviceKindMouse|, you must set the
283   // correct buttons.
284   FlutterPointerDeviceKind device_kind;
285   // The buttons currently pressed, if any.
286   int64_t buttons;
287 } FlutterPointerEvent;
288 
289 struct _FlutterPlatformMessageResponseHandle;
290 typedef struct _FlutterPlatformMessageResponseHandle
291     FlutterPlatformMessageResponseHandle;
292 
293 typedef struct {
294   // The size of this struct. Must be sizeof(FlutterPlatformMessage).
295   size_t struct_size;
296   const char* channel;
297   const uint8_t* message;
298   size_t message_size;
299   // The response handle on which to invoke
300   // |FlutterEngineSendPlatformMessageResponse| when the response is ready.
301   // |FlutterEngineSendPlatformMessageResponse| must be called for all messages
302   // received by the embedder. Failure to call
303   // |FlutterEngineSendPlatformMessageResponse| will cause a memory leak. It is
304   // not safe to send multiple responses on a single response object.
305   const FlutterPlatformMessageResponseHandle* response_handle;
306 } FlutterPlatformMessage;
307 
308 typedef void (*FlutterPlatformMessageCallback)(
309     const FlutterPlatformMessage* /* message*/,
310     void* /* user data */);
311 
312 typedef void (*FlutterDataCallback)(const uint8_t* /* data */,
313                                     size_t /* size */,
314                                     void* /* user data */);
315 
316 typedef struct {
317   double left;
318   double top;
319   double right;
320   double bottom;
321 } FlutterRect;
322 
323 typedef struct _FlutterTaskRunner* FlutterTaskRunner;
324 
325 typedef struct {
326   FlutterTaskRunner runner;
327   uint64_t task;
328 } FlutterTask;
329 
330 typedef void (*FlutterTaskRunnerPostTaskCallback)(
331     FlutterTask /* task */,
332     uint64_t /* target time nanos */,
333     void* /* user data */);
334 
335 // An interface used by the Flutter engine to execute tasks at the target time
336 // on a specified thread. There should be a 1-1 relationship between a thread
337 // and a task runner. It is undefined behavior to run a task on a thread that is
338 // not associated with its task runner.
339 typedef struct {
340   // The size of this struct. Must be sizeof(FlutterTaskRunnerDescription).
341   size_t struct_size;
342   void* user_data;
343   // May be called from any thread. Should return true if tasks posted on the
344   // calling thread will be run on that same thread.
345   //
346   // This field is required.
347   BoolCallback runs_task_on_current_thread_callback;
348   // May be called from any thread. The given task should be executed by the
349   // embedder on the thread associated with that task runner by calling
350   // |FlutterEngineRunTask| at the given target time. The system monotonic clock
351   // should be used for the target time. The target time is the absolute time
352   // from epoch (NOT a delta) at which the task must be returned back to the
353   // engine on the correct thread. If the embedder needs to calculate a delta,
354   // |FlutterEngineGetCurrentTime| may be called and the difference used as the
355   // delta.
356   //
357   // This field is required.
358   FlutterTaskRunnerPostTaskCallback post_task_callback;
359   /// A unique identifier for the task runner. If multiple task runners service
360   /// tasks on the same thread, their identifiers must match.
361   size_t identifier;
362 } FlutterTaskRunnerDescription;
363 
364 typedef struct {
365   // The size of this struct. Must be sizeof(FlutterCustomTaskRunners).
366   size_t struct_size;
367   // Specify the task runner for the thread on which the |FlutterEngineRun|
368   /// call is made. The same task runner description can be specified for both
369   /// the render and platform task runners. This makes the Flutter engine use
370   /// the same thread for both task runners.
371   const FlutterTaskRunnerDescription* platform_task_runner;
372   /// Specify the task runner for the thread on which the render tasks will be
373   /// run. The same task runner description can be specified for both the render
374   /// and platform task runners. This makes the Flutter engine use the same
375   /// thread for both task runners.
376   const FlutterTaskRunnerDescription* render_task_runner;
377 } FlutterCustomTaskRunners;
378 
379 typedef struct {
380   // The type of the OpenGL backing store. Currently, it can either be a texture
381   // or a framebuffer.
382   FlutterOpenGLTargetType type;
383   union {
384     // A texture for Flutter to render into.
385     FlutterOpenGLTexture texture;
386     // A framebuffer for Flutter to render into. The embedder must ensure that
387     // the framebuffer is complete.
388     FlutterOpenGLFramebuffer framebuffer;
389   };
390 } FlutterOpenGLBackingStore;
391 
392 typedef struct {
393   // A pointer to the raw bytes of the allocation described by this software
394   // backing store.
395   const void* allocation;
396   // The number of bytes in a single row of the allocation.
397   size_t row_bytes;
398   // The number of rows in the allocation.
399   size_t height;
400   // A baton that is not interpreted by the engine in any way. It will be given
401   // back to the embedder in the destruction callback below. Embedder resources
402   // may be associated with this baton.
403   void* user_data;
404   // The callback invoked by the engine when it no longer needs this backing
405   // store.
406   VoidCallback destruction_callback;
407 } FlutterSoftwareBackingStore;
408 
409 // The identifier of the platform view. This identifier is specified by the
410 // application when a platform view is added to the scene via the
411 // `SceneBuilder.addPlatformView` call.
412 typedef int64_t FlutterPlatformViewIdentifier;
413 
414 typedef struct {
415   // The size of this struct. Must be sizeof(FlutterPlatformView).
416   size_t struct_size;
417   // The identifier of this platform view. This identifier is specified by the
418   // application when a platform view is added to the scene via the
419   // `SceneBuilder.addPlatformView` call.
420   FlutterPlatformViewIdentifier identifier;
421 } FlutterPlatformView;
422 
423 typedef enum {
424   // Specifies an OpenGL backing store. Can either be an OpenGL texture or
425   // framebuffer.
426   kFlutterBackingStoreTypeOpenGL,
427   // Specified an software allocation for Flutter to render into using the CPU.
428   kFlutterBackingStoreTypeSoftware,
429 } FlutterBackingStoreType;
430 
431 typedef struct {
432   // The size of this struct. Must be sizeof(FlutterBackingStore).
433   size_t struct_size;
434   // A baton that is not interpreted by the engine in any way. The embedder may
435   // use this to associate resources that are tied to the lifecycle of the
436   // |FlutterBackingStore|.
437   void* user_data;
438   // Specifies the type of backing store.
439   FlutterBackingStoreType type;
440   // Indicates if this backing store was updated since the last time it was
441   // associated with a presented layer.
442   bool did_update;
443   union {
444     // The description of the OpenGL backing store.
445     FlutterOpenGLBackingStore open_gl;
446     // The description of the software backing store.
447     FlutterSoftwareBackingStore software;
448   };
449 } FlutterBackingStore;
450 
451 typedef struct {
452   double x;
453   double y;
454 } FlutterPoint;
455 
456 typedef struct {
457   double width;
458   double height;
459 } FlutterSize;
460 
461 typedef struct {
462   // The size of this struct. Must be sizeof(FlutterBackingStoreConfig).
463   size_t struct_size;
464   // The size of the render target the engine expects to render into.
465   FlutterSize size;
466 } FlutterBackingStoreConfig;
467 
468 typedef enum {
469   // Indicates that the contents of this layer are rendered by Flutter into a
470   // backing store.
471   kFlutterLayerContentTypeBackingStore,
472   // Indicates that the contents of this layer are determined by the embedder.
473   kFlutterLayerContentTypePlatformView,
474 } FlutterLayerContentType;
475 
476 typedef struct {
477   // This size of this struct. Must be sizeof(FlutterLayer).
478   size_t struct_size;
479   // Each layer displays contents in one way or another. The type indicates
480   // whether those contents are specified by Flutter or the embedder.
481   FlutterLayerContentType type;
482   union {
483     // Indicates that the contents of this layer are rendered by Flutter into a
484     // backing store.
485     const FlutterBackingStore* backing_store;
486     // Indicates that the contents of this layer are determined by the embedder.
487     const FlutterPlatformView* platform_view;
488   };
489   // The offset of this layer (in physical pixels) relative to the top left of
490   // the root surface used by the engine.
491   FlutterPoint offset;
492   // The size of the layer (in physical pixels).
493   FlutterSize size;
494 } FlutterLayer;
495 
496 typedef bool (*FlutterBackingStoreCreateCallback)(
497     const FlutterBackingStoreConfig* config,
498     FlutterBackingStore* backing_store_out,
499     void* user_data);
500 
501 typedef bool (*FlutterBackingStoreCollectCallback)(
502     const FlutterBackingStore* renderer,
503     void* user_data);
504 
505 typedef bool (*FlutterLayersPresentCallback)(const FlutterLayer** layers,
506                                              size_t layers_count,
507                                              void* user_data);
508 
509 typedef struct {
510   // This size of this struct. Must be sizeof(FlutterCompositor).
511   size_t struct_size;
512   // A baton that in not interpreted by the engine in any way. If it passed back
513   // to the embedder in  |FlutterCompositor.create_backing_store_callback|,
514   // |FlutterCompositor.collect_backing_store_callback| and
515   // |FlutterCompositor.present_layers_callback|
516   void* user_data;
517   // A callback invoked by the engine to obtain a backing store for a specific
518   // |FlutterLayer|.
519   //
520   // On ABI stability: Callers must take care to restrict access within
521   // |FlutterBackingStore::struct_size| when specifying a new backing store to
522   // the engine. This only matters if the embedder expects to be used with
523   // engines older than the version whose headers it used during compilation.
524   FlutterBackingStoreCreateCallback create_backing_store_callback;
525   // A callback invoked by the engine to release the backing store. The embedder
526   // may collect any resources associated with the backing store.
527   FlutterBackingStoreCollectCallback collect_backing_store_callback;
528   // Callback invoked by the engine to composite the contents of each layer onto
529   // the screen.
530   FlutterLayersPresentCallback present_layers_callback;
531 } FlutterCompositor;
532 
533 typedef struct {
534   // The size of this struct. Must be sizeof(FlutterProjectArgs).
535   size_t struct_size;
536 
537   // A callback that gets invoked by the engine when it attempts to wait for a
538   // platform vsync event. The engine will give the platform a baton that needs
539   // to be returned back to the engine via |FlutterEngineOnVsync|. All batons
540   // must be retured to the engine before initializing a
541   // |FlutterEngineShutdown|. Not doing the same will result in a memory leak.
542   // While the call to |FlutterEngineOnVsync| must occur on the thread that made
543   // the call to |FlutterEngineRun|, the engine will make this callback on an
544   // internal engine-managed thread. If the components accessed on the embedder
545   // are not thread safe, the appropriate re-threading must be done.
546   VsyncCallback vsync_callback;
547 
548   // Typically the Flutter engine create and manages its internal threads. This
549   // optional argument allows for the specification of task runner interfaces to
550   // event loops managed by the embedder on threads it creates.
551   const FlutterCustomTaskRunners* custom_task_runners;
552 
553   // Typically, Flutter renders the layer hierarchy into a single root surface.
554   // However, when embedders need to interleave their own contents within the
555   // Flutter layer hierarchy, their applications can push platform views within
556   // the Flutter scene. This is done using the `SceneBuilder.addPlatformView`
557   // call. When this happens, the Flutter rasterizer divides the effective view
558   // hierarchy into multiple layers. Each layer gets its own backing store and
559   // Flutter renders into the same. Once the layers contents have been
560   // fulfilled, the embedder is asked to composite these layers on-screen. At
561   // this point, it can interleave its own contents within the effective
562   // hierarchy. The interface for the specification of these layer backing
563   // stores and the hooks to listen for the composition of layers on-screen can
564   // be controlled using this field. This field is completely optional. In its
565   // absence, platforms views in the scene are ignored and Flutter renders to
566   // the root surface as normal.
567   const FlutterCompositor* compositor;
568 } FlutterProjectArgs;
569 
570 //------------------------------------------------------------------------------
571 /// @brief      Initialize and run a Flutter engine instance and return a handle
572 ///             to it. This is a convenience method for the the pair of calls to
573 ///             `FlutterEngineInitialize` and `FlutterEngineRunInitialized`.
574 ///
575 /// @note       This method of running a Flutter engine works well except in
576 ///             cases where the embedder specifies custom task runners via
577 ///             `FlutterProjectArgs::custom_task_runners`. In such cases, the
578 ///             engine may need the embedder to post tasks back to it before
579 ///             `FlutterEngineRun` has returned. Embedders can only post tasks
580 ///             to the engine if they have a handle to the engine. In such
581 ///             cases, embedders are advised to get the engine handle via the
582 ///             `FlutterInitializeCall`. Then they can call
583 ///             `FlutterEngineRunInitialized` knowing that they will be able to
584 ///             service custom tasks on other threads with the engine handle.
585 ///
586 /// @param[in]  version    The Flutter embedder API version. Must be
587 ///                        FLUTTER_ENGINE_VERSION.
588 /// @param[in]  config     The renderer configuration.
589 /// @param[in]  args       The Flutter project arguments.
590 /// @param      user_data  A user data baton passed back to embedders in
591 ///                        callbacks.
592 /// @param[out] engine_out The engine handle on successful engine creation.
593 ///
594 /// @return     The result of the call to run the Flutter engine.
595 ///
596 FLUTTER_EXPORT
597 FlutterEngineResult FlutterEngineRun(const FlutterRendererConfig* config,
598                                      const FlutterProjectArgs* args,
599                                      void* user_data,
600                                      FLUTTER_API_SYMBOL(FlutterEngine) *
601                                          engine_out);
602 
603 //------------------------------------------------------------------------------
604 /// @brief      Shuts down a Flutter engine instance. The engine handle is no
605 ///             longer valid for any calls in the embedder API after this point.
606 ///             Making additional calls with this handle is undefined behavior.
607 ///
608 /// @note       This de-initializes the Flutter engine instance (via an implicit
609 ///             call to `FlutterEngineDeinitialize`) if necessary.
610 ///
611 /// @param[in]  engine  The Flutter engine instance to collect.
612 ///
613 /// @return     The result of the call to shutdown the Flutter engine instance.
614 ///
615 FLUTTER_EXPORT
616 FlutterEngineResult FlutterEngineShutdown(FLUTTER_API_SYMBOL(FlutterEngine)
617                                               engine);
618 //------------------------------------------------------------------------------
619 /// @brief      Initialize a Flutter engine instance. This does not run the
620 ///             Flutter application code till the `FlutterEngineRunInitialized`
621 ///             call is made. Besides Flutter application code, no tasks are
622 ///             scheduled on embedder managed task runners either. This allows
623 ///             embedders providing custom task runners to the Flutter engine to
624 ///             obtain a handle to the Flutter engine before the engine can post
625 ///             tasks on these task runners.
626 ///
627 /// @param[in]  version    The Flutter embedder API version. Must be
628 ///                        FLUTTER_ENGINE_VERSION.
629 /// @param[in]  config     The renderer configuration.
630 /// @param[in]  args       The Flutter project arguments.
631 /// @param      user_data  A user data baton passed back to embedders in
632 ///                        callbacks.
633 /// @param[out] engine_out The engine handle on successful engine creation.
634 ///
635 /// @return     The result of the call to initialize the Flutter engine.
636 ///
637 FLUTTER_EXPORT
638 FlutterEngineResult FlutterEngineInitialize(const FlutterRendererConfig* config,
639                                             const FlutterProjectArgs* args,
640                                             void* user_data,
641                                             FLUTTER_API_SYMBOL(FlutterEngine) *
642                                                 engine_out);
643 
644 //------------------------------------------------------------------------------
645 /// @brief      Stops running the Flutter engine instance. After this call, the
646 ///             embedder is also guaranteed that no more calls to post tasks
647 ///             onto custom task runners specified by the embedder are made. The
648 ///             Flutter engine handle still needs to be collected via a call to
649 ///             `FlutterEngineShutdown`.
650 ///
651 /// @param[in]  engine    The running engine instance to de-initialize.
652 ///
653 /// @return     The result of the call to de-initialize the Flutter engine.
654 ///
655 FLUTTER_EXPORT
656 FlutterEngineResult FlutterEngineDeinitialize(FLUTTER_API_SYMBOL(FlutterEngine)
657                                                   engine);
658 
659 //------------------------------------------------------------------------------
660 /// @brief      Runs an initialized engine instance. An engine can be
661 ///             initialized via `FlutterEngineInitialize`. An initialized
662 ///             instance can only be run once. During and after this call,
663 ///             custom task runners supplied by the embedder are expected to
664 ///             start servicing tasks.
665 ///
666 /// @param[in]  engine  An initialized engine instance that has not previously
667 ///                     been run.
668 ///
669 /// @return     The result of the call to run the initialized Flutter
670 ///             engine instance.
671 ///
672 FLUTTER_EXPORT
673 FlutterEngineResult FlutterEngineRunInitialized(
674     FLUTTER_API_SYMBOL(FlutterEngine) engine);
675 
676 FLUTTER_EXPORT
677 FlutterEngineResult FlutterEngineSetIdleNotificationCallback(
678     FLUTTER_API_SYMBOL(FlutterEngine) engine,
679     const IdleCallback& idle_notification_callback);
680 
681 FLUTTER_EXPORT
682 FlutterEngineResult FlutterEngineSendWindowMetricsEvent(
683     FLUTTER_API_SYMBOL(FlutterEngine) engine,
684     const FlutterWindowMetricsEvent* event);
685 
686 FLUTTER_EXPORT
687 FlutterEngineResult FlutterEngineSendPointerEvent(
688     FLUTTER_API_SYMBOL(FlutterEngine) engine,
689     const FlutterPointerEvent* events,
690     size_t events_count);
691 
692 FLUTTER_EXPORT
693 FlutterEngineResult FlutterEngineSendPlatformMessage(
694     FLUTTER_API_SYMBOL(FlutterEngine) engine,
695     const FlutterPlatformMessage* message);
696 
697 // Creates a platform message response handle that allows the embedder to set a
698 // native callback for a response to a message. This handle may be set on the
699 // |response_handle| field of any |FlutterPlatformMessage| sent to the engine.
700 //
701 // The handle must be collected via a call to
702 // |FlutterPlatformMessageReleaseResponseHandle|. This may be done immediately
703 // after a call to |FlutterEngineSendPlatformMessage| with a platform message
704 // whose response handle contains the handle created using this call. In case a
705 // handle is created but never sent in a message, the release call must still be
706 // made. Not calling release on the handle results in a small memory leak.
707 //
708 // The user data baton passed to the data callback is the one specified in this
709 // call as the third argument.
710 FLUTTER_EXPORT
711 FlutterEngineResult FlutterPlatformMessageCreateResponseHandle(
712     FLUTTER_API_SYMBOL(FlutterEngine) engine,
713     FlutterDataCallback data_callback,
714     void* user_data,
715     FlutterPlatformMessageResponseHandle** response_out);
716 
717 // Collects the handle created using
718 // |FlutterPlatformMessageCreateResponseHandle|.
719 FLUTTER_EXPORT
720 FlutterEngineResult FlutterPlatformMessageReleaseResponseHandle(
721     FLUTTER_API_SYMBOL(FlutterEngine) engine,
722     FlutterPlatformMessageResponseHandle* response);
723 
724 FLUTTER_EXPORT
725 FlutterEngineResult FlutterEngineSendPlatformMessageResponse(
726     FLUTTER_API_SYMBOL(FlutterEngine) engine,
727     const FlutterPlatformMessageResponseHandle* handle,
728     const uint8_t* data,
729     size_t data_length);
730 
731 // This API is only meant to be used by platforms that need to flush tasks on a
732 // message loop not controlled by the Flutter engine. This API will be
733 // deprecated soon.
734 FLUTTER_EXPORT
735 FlutterEngineResult __FlutterEngineFlushPendingTasksNow();
736 
737 // Register an external texture with a unique (per engine) identifier. Only
738 // rendering backends that support external textures accept external texture
739 // registrations. After the external texture is registered, the application can
740 // mark that a frame is available by calling
741 // |FlutterEngineMarkExternalTextureFrameAvailable|.
742 FLUTTER_EXPORT
743 FlutterEngineResult FlutterEngineRegisterExternalTexture(
744     FLUTTER_API_SYMBOL(FlutterEngine) engine,
745     int64_t texture_identifier);
746 
747 // Unregister a previous texture registration.
748 FLUTTER_EXPORT
749 FlutterEngineResult FlutterEngineUnregisterExternalTexture(
750     FLUTTER_API_SYMBOL(FlutterEngine) engine,
751     int64_t texture_identifier);
752 
753 // Mark that a new texture frame is available for a given texture identifier.
754 FLUTTER_EXPORT
755 FlutterEngineResult FlutterEngineMarkExternalTextureFrameAvailable(
756     FLUTTER_API_SYMBOL(FlutterEngine) engine,
757     int64_t texture_identifier);
758 
759 // Notify the engine that a vsync event occurred. A baton passed to the
760 // platform via the vsync callback must be returned. This call must be made on
761 // the thread on which the call to |FlutterEngineRun| was made.
762 //
763 // |frame_start_time_nanos| is the point at which the vsync event occurred or
764 // will occur. If the time point is in the future, the engine will wait till
765 // that point to begin its frame workload. The system monotonic clock is used as
766 // the timebase.
767 //
768 // |frame_target_time_nanos| is the point at which the embedder anticipates the
769 // next vsync to occur. This is a hint the engine uses to schedule Dart VM
770 // garbage collection in periods in which the various threads are most likely to
771 // be idle. For example, for a 60Hz display, embedders should add 16.6 * 1e6 to
772 // the frame time field. The system monotonic clock is used as the timebase.
773 //
774 // That frame timepoints are in nanoseconds.
775 FLUTTER_EXPORT
776 FlutterEngineResult FlutterEngineOnVsync(FLUTTER_API_SYMBOL(FlutterEngine)
777                                              engine,
778                                          intptr_t baton,
779                                          uint64_t frame_start_time_nanos,
780                                          uint64_t frame_target_time_nanos);
781 
782 // A profiling utility. Logs a trace duration begin event to the timeline. If
783 // the timeline is unavailable or disabled, this has no effect. Must be
784 // balanced with an duration end event (via
785 // |FlutterEngineTraceEventDurationEnd|) with the same name on the same thread.
786 // Can be called on any thread. Strings passed into the function will NOT be
787 // copied when added to the timeline. Only string literals may be passed in.
788 FLUTTER_EXPORT
789 void FlutterEngineTraceEventDurationBegin(const char* name);
790 
791 // A profiling utility. Logs a trace duration end event to the timeline. If the
792 // timeline is unavailable or disabled, this has no effect. This call must be
793 // preceded by a trace duration begin call (via
794 // |FlutterEngineTraceEventDurationBegin|) with the same name on the same
795 // thread. Can be called on any thread. Strings passed into the function will
796 // NOT be copied when added to the timeline. Only string literals may be passed
797 // in.
798 FLUTTER_EXPORT
799 void FlutterEngineTraceEventDurationEnd(const char* name);
800 
801 // A profiling utility. Logs a trace duration instant event to the timeline. If
802 // the timeline is unavailable or disabled, this has no effect. Can be called
803 // on any thread. Strings passed into the function will NOT be copied when added
804 // to the timeline. Only string literals may be passed in.
805 FLUTTER_EXPORT
806 void FlutterEngineTraceEventInstant(const char* name);
807 
808 // Posts a task onto the Flutter render thread. Typically, this may be called
809 // from any thread as long as a |FlutterEngineShutdown| on the specific engine
810 // has not already been initiated.
811 FLUTTER_EXPORT
812 FlutterEngineResult FlutterEnginePostRenderThreadTask(
813     FLUTTER_API_SYMBOL(FlutterEngine) engine,
814     VoidCallback callback,
815     void* callback_data);
816 
817 // Get the current time in nanoseconds from the clock used by the flutter
818 // engine. This is the system monotonic clock.
819 FLUTTER_EXPORT
820 uint64_t FlutterEngineGetCurrentTime();
821 
822 // Inform the engine to run the specified task. This task has been given to
823 // the engine via the |FlutterTaskRunnerDescription.post_task_callback|. This
824 // call must only be made at the target time specified in that callback. Running
825 // the task before that time is undefined behavior.
826 FLUTTER_EXPORT
827 FlutterEngineResult FlutterEngineRunTask(FLUTTER_API_SYMBOL(FlutterEngine)
828                                              engine,
829                                          const FlutterTask* task);
830 
831 #ifdef USE_GLFW_WINDOW
832 FLUTTER_EXPORT
833 void FlutterEngineRegisterHandleTouchEventCallback(HandleTouchEventCallback&& callback);
834 #endif
835 
836 #if defined(__cplusplus)
837 }  // extern "C"
838 #endif
839 
840 #endif  // FLUTTER_EMBEDDER_H_
841