Flutter iOS Embedder
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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  // The GrContext that is currently used by the overlay surfaces.
39  // We track this to know when the GrContext for the Flutter app has changed
40  // so we can update the overlay with the new context.
41  GrDirectContext* gr_context;
42 
43  void UpdateViewState(UIView* flutter_view, SkRect rect, int64_t view_id, int64_t overlay_id);
44 };
45 
46 /// @brief Storage for Overlay layers across frames.
47 ///
48 /// Note: this class does not synchronize access to its layers or any layer removal. As it
49 /// is currently used, layers must be created on the platform thread but other methods of
50 /// it are called on the raster thread. This is safe as overlay layers are only ever added
51 /// while the raster thread is latched.
53  public:
54  OverlayLayerPool() = default;
55 
56  ~OverlayLayerPool() = default;
57 
58  /// @brief Gets a layer from the pool if available.
59  ///
60  /// The layer is marked as used until [RecycleLayers] is called.
61  std::shared_ptr<OverlayLayer> GetNextLayer();
62 
63  /// @brief Create a new overlay layer.
64  ///
65  /// This method can only be called on the Platform thread.
66  void CreateLayer(GrDirectContext* gr_context,
67  const std::shared_ptr<IOSContext>& ios_context,
68  MTLPixelFormat pixel_format);
69 
70  /// @brief Removes unused layers from the pool. Returns the unused layers.
71  std::vector<std::shared_ptr<OverlayLayer>> RemoveUnusedLayers();
72 
73  /// @brief Marks the layers in the pool as available for reuse.
74  void RecycleLayers();
75 
76  /// @brief The count of layers currently in the pool.
77  size_t size() const;
78 
79  private:
80  OverlayLayerPool(const OverlayLayerPool&) = delete;
81  OverlayLayerPool& operator=(const OverlayLayerPool&) = delete;
82 
83  // The index of the entry in the layers_ vector that determines the beginning of the unused
84  // layers. For example, consider the following vector:
85  // _____
86  // | 0 |
87  /// |---|
88  /// | 1 | <-- available_layer_index_
89  /// |---|
90  /// | 2 |
91  /// |---|
92  ///
93  /// This indicates that entries starting from 1 can be reused meanwhile the entry at position 0
94  /// cannot be reused.
95  size_t available_layer_index_ = 0;
96  std::vector<std::shared_ptr<OverlayLayer>> layers_;
97 };
98 
99 } // namespace flutter
100 
101 #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:12
flutter::OverlayLayerPool::GetNextLayer
std::shared_ptr< OverlayLayer > GetNextLayer()
Gets a layer from the pool if available.
Definition: overlay_layer_pool.mm:45
flutter::OverlayLayer
State holder for a Flutter overlay layer.
Definition: overlay_layer_pool.h:22
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:55
flutter::OverlayLayerPool::size
size_t size() const
The count of layers currently in the pool.
Definition: overlay_layer_pool.mm:130
flutter::OverlayLayer::ios_surface
std::unique_ptr< IOSSurface > ios_surface
Definition: overlay_layer_pool.h:32
flutter::OverlayLayer::gr_context
GrDirectContext * gr_context
Definition: overlay_layer_pool.h:41
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:27
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:109
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:113
flutter::OverlayLayer::overlay_view
UIView * overlay_view
Definition: overlay_layer_pool.h:30
ios_context.h
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:52