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, MTLPixelFormat pixel_format);
62 
63  /// @brief Removes unused layers from the pool. Returns the unused layers.
64  std::vector<std::shared_ptr<OverlayLayer>> RemoveUnusedLayers();
65 
66  /// @brief Marks the layers in the pool as available for reuse.
67  void RecycleLayers();
68 
69  /// @brief The count of layers currently in the pool.
70  size_t size() const;
71 
72  private:
73  OverlayLayerPool(const OverlayLayerPool&) = delete;
74  OverlayLayerPool& operator=(const OverlayLayerPool&) = delete;
75 
76  // The index of the entry in the layers_ vector that determines the beginning of the unused
77  // layers. For example, consider the following vector:
78  // _____
79  // | 0 |
80  /// |---|
81  /// | 1 | <-- available_layer_index_
82  /// |---|
83  /// | 2 |
84  /// |---|
85  ///
86  /// This indicates that entries starting from 1 can be reused meanwhile the entry at position 0
87  /// cannot be reused.
88  size_t available_layer_index_ = 0;
89  std::vector<std::shared_ptr<OverlayLayer>> layers_;
90 };
91 
92 } // namespace flutter
93 
94 #endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_OVERLAY_LAYER_POOL_H_
Storage for Overlay layers across frames.
void RecycleLayers()
Marks the layers in the pool as available for reuse.
std::vector< std::shared_ptr< OverlayLayer > > RemoveUnusedLayers()
Removes unused layers from the pool. Returns the unused layers.
size_t size() const
The count of layers currently in the pool.
std::shared_ptr< OverlayLayer > GetNextLayer()
Gets a layer from the pool if available.
void CreateLayer(const std::shared_ptr< IOSContext > &ios_context, MTLPixelFormat pixel_format)
Create a new overlay layer.
State holder for a Flutter overlay layer.
std::unique_ptr< Surface > surface
void UpdateViewState(UIView *flutter_view, SkRect rect, int64_t view_id, int64_t overlay_id)
std::unique_ptr< IOSSurface > ios_surface
OverlayLayer(UIView *overlay_view, UIView *overlay_view_wrapper, std::unique_ptr< IOSSurface > ios_surface, std::unique_ptr< Surface > surface)