Flutter iOS Embedder
overlay_layer_pool.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_DARWIN_IOS_FRAMEWORK_SOURCE_OVERLAY_LAYER_POOL_H_
6 #define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_OVERLAY_LAYER_POOL_H_
7 
8 #include <Metal/Metal.h>
9 #include <memory>
10 
11 #import <UIKit/UIKit.h>
12 
13 #include "flow/surface.h"
14 #include "fml/platform/darwin/scoped_nsobject.h"
15 
17 
18 namespace flutter {
19 
20 class IOSSurface;
21 
22 /// @brief State holder for a Flutter overlay layer.
23 struct OverlayLayer {
24  OverlayLayer(const fml::scoped_nsobject<UIView>& overlay_view,
25  const fml::scoped_nsobject<UIView>& overlay_view_wrapper,
26  std::unique_ptr<IOSSurface> ios_surface,
27  std::unique_ptr<Surface> surface);
28 
29  ~OverlayLayer() = default;
30 
31  fml::scoped_nsobject<UIView> overlay_view;
32  fml::scoped_nsobject<UIView> overlay_view_wrapper;
33  std::unique_ptr<IOSSurface> ios_surface;
34  std::unique_ptr<Surface> surface;
35 
36  // Whether a frame for this layer was submitted.
38 
39  // The GrContext that is currently used by the overlay surfaces.
40  // We track this to know when the GrContext for the Flutter app has changed
41  // so we can update the overlay with the new context.
42  GrDirectContext* gr_context;
43 
44  void UpdateViewState(UIView* flutter_view, SkRect rect, int64_t view_id, int64_t overlay_id);
45 };
46 
47 /// @brief Storage for Overlay layers across frames.
48 ///
49 /// Note: this class does not synchronize access to its layers or any layer removal. As it
50 /// is currently used, layers must be created on the platform thread but other methods of
51 /// it are called on the raster thread. This is safe as overlay layers are only ever added
52 /// while the raster thread is latched.
54  public:
55  OverlayLayerPool() = default;
56 
57  ~OverlayLayerPool() = default;
58 
59  /// @brief Gets a layer from the pool if available.
60  ///
61  /// The layer is marked as used until [RecycleLayers] is called.
62  std::shared_ptr<OverlayLayer> GetNextLayer();
63 
64  /// @brief Create a new overlay layer.
65  ///
66  /// This method can only be called on the Platform thread.
67  void CreateLayer(GrDirectContext* gr_context,
68  const std::shared_ptr<IOSContext>& ios_context,
69  MTLPixelFormat pixel_format);
70 
71  /// @brief Removes unused layers from the pool. Returns the unused layers.
72  std::vector<std::shared_ptr<OverlayLayer>> RemoveUnusedLayers();
73 
74  /// @brief Marks the layers in the pool as available for reuse.
75  void RecycleLayers();
76 
77  /// @brief The count of layers currently in the pool.
78  size_t size() const;
79 
80  private:
81  OverlayLayerPool(const OverlayLayerPool&) = delete;
82  OverlayLayerPool& operator=(const OverlayLayerPool&) = delete;
83 
84  // The index of the entry in the layers_ vector that determines the beginning of the unused
85  // layers. For example, consider the following vector:
86  // _____
87  // | 0 |
88  /// |---|
89  /// | 1 | <-- available_layer_index_
90  /// |---|
91  /// | 2 |
92  /// |---|
93  ///
94  /// This indicates that entries starting from 1 can be reused meanwhile the entry at position 0
95  /// cannot be reused.
96  size_t available_layer_index_ = 0;
97  std::vector<std::shared_ptr<OverlayLayer>> layers_;
98 };
99 
100 } // namespace flutter
101 
102 #endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_OVERLAY_LAYER_POOL_H_
flutter::OverlayLayerPool::GetNextLayer
std::shared_ptr< OverlayLayer > GetNextLayer()
Gets a layer from the pool if available.
Definition: overlay_layer_pool.mm:47
flutter::OverlayLayer
State holder for a Flutter overlay layer.
Definition: overlay_layer_pool.h:23
flutter::OverlayLayerPool::~OverlayLayerPool
~OverlayLayerPool()=default
flutter::OverlayLayerPool::CreateLayer
void CreateLayer(GrDirectContext *gr_context, const std::shared_ptr< IOSContext > &ios_context, MTLPixelFormat pixel_format)
Create a new overlay layer.
Definition: overlay_layer_pool.mm:57
flutter::OverlayLayerPool::size
size_t size() const
The count of layers currently in the pool.
Definition: overlay_layer_pool.mm:132
flutter::OverlayLayer::ios_surface
std::unique_ptr< IOSSurface > ios_surface
Definition: overlay_layer_pool.h:33
flutter::OverlayLayer::gr_context
GrDirectContext * gr_context
Definition: overlay_layer_pool.h:42
flutter::OverlayLayer::~OverlayLayer
~OverlayLayer()=default
flutter::OverlayLayer::surface
std::unique_ptr< Surface > surface
Definition: overlay_layer_pool.h:34
flutter
Definition: accessibility_bridge.h:28
flutter::OverlayLayer::did_submit_last_frame
bool did_submit_last_frame
Definition: overlay_layer_pool.h:37
flutter::OverlayLayerPool::RecycleLayers
void RecycleLayers()
Marks the layers in the pool as available for reuse.
Definition: overlay_layer_pool.mm:111
flutter::OverlayLayerPool::RemoveUnusedLayers
std::vector< std::shared_ptr< OverlayLayer > > RemoveUnusedLayers()
Removes unused layers from the pool. Returns the unused layers.
Definition: overlay_layer_pool.mm:115
flutter::OverlayLayer::OverlayLayer
OverlayLayer(const fml::scoped_nsobject< UIView > &overlay_view, const fml::scoped_nsobject< UIView > &overlay_view_wrapper, std::unique_ptr< IOSSurface > ios_surface, std::unique_ptr< Surface > surface)
Definition: overlay_layer_pool.mm:12
flutter::OverlayLayer::overlay_view
fml::scoped_nsobject< UIView > overlay_view
Definition: overlay_layer_pool.h:31
ios_context.h
flutter::OverlayLayer::overlay_view_wrapper
fml::scoped_nsobject< UIView > overlay_view_wrapper
Definition: overlay_layer_pool.h:32
flutter::OverlayLayer::UpdateViewState
void UpdateViewState(UIView *flutter_view, SkRect rect, int64_t view_id, int64_t overlay_id)
Definition: overlay_layer_pool.mm:21
flutter::OverlayLayerPool::OverlayLayerPool
OverlayLayerPool()=default
flutter::OverlayLayerPool
Storage for Overlay layers across frames.
Definition: overlay_layer_pool.h:53