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_H_
6 #define FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_H_
7 
8 #include <unordered_map>
9 
10 #include "flutter/flow/embedded_views.h"
11 #include "flutter/flow/rtree.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 #include "third_party/skia/include/core/SkPictureRecorder.h"
17 
18 namespace flutter {
19 
20 //------------------------------------------------------------------------------
21 /// Allows to embed Android views into a Flutter application.
22 ///
23 /// This class calls Java methods via |PlatformViewAndroidJNI| to manage the
24 /// lifecycle of the Android view corresponding to |flutter::PlatformViewLayer|.
25 ///
26 /// It also orchestrates overlay surfaces. These are Android views
27 /// that render above (by Z order) the Android view corresponding to
28 /// |flutter::PlatformViewLayer|.
29 ///
31  public:
33  std::shared_ptr<AndroidContext> android_context,
34  std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
35  std::shared_ptr<AndroidSurfaceFactory> surface_factory);
36 
37  // |ExternalViewEmbedder|
39  int view_id,
40  std::unique_ptr<flutter::EmbeddedViewParams> params) override;
41 
42  // |ExternalViewEmbedder|
43  SkCanvas* CompositeEmbeddedView(int view_id) override;
44 
45  // |ExternalViewEmbedder|
46  std::vector<SkCanvas*> GetCurrentCanvases() override;
47 
48  // |ExternalViewEmbedder|
49  void SubmitFrame(GrDirectContext* context,
50  std::unique_ptr<SurfaceFrame> frame) override;
51 
52  // |ExternalViewEmbedder|
54  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) override;
55 
56  // |ExternalViewEmbedder|
57  SkCanvas* GetRootCanvas() override;
58 
59  // |ExternalViewEmbedder|
60  void BeginFrame(
61  SkISize frame_size,
62  GrDirectContext* context,
63  double device_pixel_ratio,
64  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) override;
65 
66  // |ExternalViewEmbedder|
67  void CancelFrame() override;
68 
69  // |ExternalViewEmbedder|
70  void EndFrame(
71  bool should_resubmit_frame,
72  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) override;
73 
74  bool SupportsDynamicThreadMerging() override;
75 
76  // Gets the rect based on the device pixel ratio of a platform view displayed
77  // on the screen.
78  SkRect GetViewRect(int view_id) const;
79 
80  private:
81  static const int kMaxLayerAllocations = 2;
82 
83  // The number of frames the rasterizer task runner will continue
84  // to run on the platform thread after no platform view is rendered.
85  //
86  // Note: this is an arbitrary number that attempts to account for cases
87  // where the platform view might be momentarily off the screen.
88  static const int kDefaultMergedLeaseDuration = 10;
89 
90  // Provides metadata to the Android surfaces.
91  const std::shared_ptr<AndroidContext> android_context_;
92 
93  // Allows to call methods in Java.
94  const std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
95 
96  // Allows to create surfaces.
97  const std::shared_ptr<AndroidSurfaceFactory> surface_factory_;
98 
99  // Holds surfaces. Allows to recycle surfaces or allocate new ones.
100  const std::unique_ptr<SurfacePool> surface_pool_;
101 
102  // The size of the root canvas.
103  SkISize frame_size_;
104 
105  // The pixel ratio used to determinate the size of a platform view layer
106  // relative to the device layout system.
107  double device_pixel_ratio_;
108 
109  // The order of composition. Each entry contains a unique id for the platform
110  // view.
111  std::vector<int64_t> composition_order_;
112 
113  // The platform view's picture recorder keyed off the platform view id, which
114  // contains any subsequent operation until the next platform view or the end
115  // of the last leaf node in the layer tree.
116  std::unordered_map<int64_t, std::unique_ptr<SkPictureRecorder>>
117  picture_recorders_;
118 
119  // The params for a platform view, which contains the size, position and
120  // mutation stack.
121  std::unordered_map<int64_t, EmbeddedViewParams> view_params_;
122 
123  // The r-tree that captures the operations for the picture recorders.
124  std::unordered_map<int64_t, sk_sp<RTree>> view_rtrees_;
125 
126  // The number of platform views in the previous frame.
127  int64_t previous_frame_view_count_;
128 
129  // Resets the state.
130  void Reset();
131 
132  // Whether the layer tree in the current frame has platform layers.
133  bool FrameHasPlatformLayers();
134 
135  // Creates a Surface when needed or recycles an existing one.
136  // Finally, draws the picture on the frame's canvas.
137  std::unique_ptr<SurfaceFrame> CreateSurfaceIfNeeded(GrDirectContext* context,
138  int64_t view_id,
139  sk_sp<SkPicture> picture,
140  const SkRect& rect);
141 };
142 
143 } // namespace flutter
144 
145 #endif // FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_H_
PostPrerollResult PostPrerollAction(fml::RefPtr< fml::RasterThreadMerger > raster_thread_merger) override
void BeginFrame(SkISize frame_size, GrDirectContext *context, double device_pixel_ratio, fml::RefPtr< fml::RasterThreadMerger > raster_thread_merger) override
SkCanvas * CompositeEmbeddedView(int view_id) override
void SubmitFrame(GrDirectContext *context, std::unique_ptr< SurfaceFrame > frame) override
std::vector< SkCanvas * > GetCurrentCanvases() override
void PrerollCompositeEmbeddedView(int view_id, std::unique_ptr< flutter::EmbeddedViewParams > params) override
void EndFrame(bool should_resubmit_frame, fml::RefPtr< fml::RasterThreadMerger > raster_thread_merger) override
AndroidExternalViewEmbedder(std::shared_ptr< AndroidContext > android_context, std::shared_ptr< PlatformViewAndroidJNI > jni_facade, std::shared_ptr< AndroidSurfaceFactory > surface_factory)