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