Flutter Engine
The Flutter Engine
external_view_embedder.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_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_EXTERNAL_VIEW_EMBEDDER_H_
6#define FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_EXTERNAL_VIEW_EMBEDDER_H_
7
8#include <unordered_map>
9
10#include "flutter/common/task_runners.h"
11#include "flutter/flow/embedded_views.h"
12#include "flutter/shell/platform/android/context/android_context.h"
13#include "flutter/shell/platform/android/external_view_embedder/surface_pool.h"
14#include "flutter/shell/platform/android/jni/platform_view_android_jni.h"
15#include "flutter/shell/platform/android/surface/android_surface.h"
16
17namespace flutter {
18
19//------------------------------------------------------------------------------
20/// Allows to embed Android views into a Flutter application.
21///
22/// This class calls Java methods via |PlatformViewAndroidJNI| to manage the
23/// lifecycle of the Android view corresponding to |flutter::PlatformViewLayer|.
24///
25/// It also orchestrates overlay surfaces. These are Android views
26/// that render above (by Z order) the Android view corresponding to
27/// |flutter::PlatformViewLayer|.
28///
30 public:
32 const AndroidContext& android_context,
33 std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
34 std::shared_ptr<AndroidSurfaceFactory> surface_factory,
35 const TaskRunners& task_runners);
36
37 // |ExternalViewEmbedder|
39 int64_t view_id,
40 std::unique_ptr<flutter::EmbeddedViewParams> params) override;
41
42 // |ExternalViewEmbedder|
43 DlCanvas* CompositeEmbeddedView(int64_t view_id) override;
44
45 // |ExternalViewEmbedder|
47 int64_t flutter_view_id,
48 GrDirectContext* context,
49 const std::shared_ptr<impeller::AiksContext>& aiks_context,
50 std::unique_ptr<SurfaceFrame> frame) override;
51
52 // |ExternalViewEmbedder|
54 const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger)
55 override;
56
57 // |ExternalViewEmbedder|
58 DlCanvas* GetRootCanvas() override;
59
60 // |ExternalViewEmbedder|
61 void BeginFrame(GrDirectContext* context,
63 raster_thread_merger) override;
64
65 // |ExternalViewEmbedder|
66 void PrepareFlutterView(SkISize frame_size,
67 double device_pixel_ratio) override;
68
69 // |ExternalViewEmbedder|
70 void CancelFrame() override;
71
72 // |ExternalViewEmbedder|
73 void EndFrame(bool should_resubmit_frame,
75 raster_thread_merger) override;
76
77 bool SupportsDynamicThreadMerging() override;
78
79 void Teardown() override;
80
81 // Gets the rect based on the device pixel ratio of a platform view displayed
82 // on the screen.
83 SkRect GetViewRect(int64_t view_id) const;
84
85 private:
86 // The number of frames the rasterizer task runner will continue
87 // to run on the platform thread after no platform view is rendered.
88 //
89 // Note: this is an arbitrary number that attempts to account for cases
90 // where the platform view might be momentarily off the screen.
91 static const int kDefaultMergedLeaseDuration = 10;
92
93 // Provides metadata to the Android surfaces.
94 const AndroidContext& android_context_;
95
96 // Allows to call methods in Java.
97 const std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
98
99 // Allows to create surfaces.
100 const std::shared_ptr<AndroidSurfaceFactory> surface_factory_;
101
102 // Holds surfaces. Allows to recycle surfaces or allocate new ones.
103 const std::unique_ptr<SurfacePool> surface_pool_;
104
105 // The task runners.
106 const TaskRunners task_runners_;
107
108 // The size of the root canvas.
109 SkISize frame_size_;
110
111 // The pixel ratio used to determinate the size of a platform view layer
112 // relative to the device layout system.
113 double device_pixel_ratio_;
114
115 // The order of composition. Each entry contains a unique id for the platform
116 // view.
117 std::vector<int64_t> composition_order_;
118
119 // The |EmbedderViewSlice| implementation keyed off the platform view id,
120 // which contains any subsequent operations until the next platform view or
121 // the end of the last leaf node in the layer tree.
122 std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>> slices_;
123
124 // The params for a platform view, which contains the size, position and
125 // mutation stack.
126 std::unordered_map<int64_t, EmbeddedViewParams> view_params_;
127
128 // The number of platform views in the previous frame.
129 int64_t previous_frame_view_count_;
130
131 // Destroys the surfaces created from the surface factory.
132 // This method schedules a task on the platform thread, and waits for
133 // the task until it completes.
134 void DestroySurfaces();
135
136 // Resets the state.
137 void Reset();
138
139 // Whether the layer tree in the current frame has platform layers.
140 bool FrameHasPlatformLayers();
141
142 // Creates a Surface when needed or recycles an existing one.
143 // Finally, draws the picture on the frame's canvas.
144 std::unique_ptr<SurfaceFrame> CreateSurfaceIfNeeded(GrDirectContext* context,
145 int64_t view_id,
146 EmbedderViewSlice* slice,
147 const SkRect& rect);
148};
149
150} // namespace flutter
151
152#endif // FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_EXTERNAL_VIEW_EMBEDDER_H_
Holds state that is shared across Android surfaces.
void EndFrame(bool should_resubmit_frame, const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
PostPrerollResult PostPrerollAction(const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
void PrerollCompositeEmbeddedView(int64_t view_id, std::unique_ptr< flutter::EmbeddedViewParams > params) override
void SubmitFlutterView(int64_t flutter_view_id, GrDirectContext *context, const std::shared_ptr< impeller::AiksContext > &aiks_context, std::unique_ptr< SurfaceFrame > frame) override
DlCanvas * CompositeEmbeddedView(int64_t view_id) override
void BeginFrame(GrDirectContext *context, const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
AndroidExternalViewEmbedder(const AndroidContext &android_context, std::shared_ptr< PlatformViewAndroidJNI > jni_facade, std::shared_ptr< AndroidSurfaceFactory > surface_factory, const TaskRunners &task_runners)
SkRect GetViewRect(int64_t view_id) const
void PrepareFlutterView(SkISize frame_size, double device_pixel_ratio) override
Developer-facing API for rendering anything within the engine.
Definition: dl_canvas.h:38
const EmbeddedViewParams * params
double frame
Definition: examples.cpp:31
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
Definition: SkSize.h:16