| Flutter iOS Embedder |

overlay_layer_pool.mm
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 #include "flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.h"
6
7 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h"
8 #import "flutter/shell/platform/darwin/ios/ios_surface.h"
9
10 namespace flutter {
11
12 OverlayLayer::OverlayLayer(UIView* overlay_view,
13 UIView* overlay_view_wrapper,
14 std::unique_ptr<IOSSurface> ios_surface,
15 std::unique_ptr<Surface> surface)
16 : overlay_view(overlay_view),
17 overlay_view_wrapper(overlay_view_wrapper),
18 ios_surface(std::move(ios_surface)),
19 surface(std::move(surface)) {};
20
21 void OverlayLayer::UpdateViewState(UIView* flutter_view,
22 DlRect rect,
23 int64_t view_id,
24 int64_t overlay_id) {
25 auto screenScale = [UIScreen mainScreen].scale;
26 // Set the size of the overlay view wrapper.
27 // This wrapper view masks the overlay view.
28 overlay_view_wrapper.frame =
29 CGRectMake(rect.GetX() / screenScale, rect.GetY() / screenScale,
30 rect.GetWidth() / screenScale, rect.GetHeight() / screenScale);
31 // Set a unique view identifier, so the overlay_view_wrapper can be identified in XCUITests.
32 overlay_view_wrapper.accessibilityIdentifier =
33 [NSString stringWithFormat:@"platform_view[%lld].overlay[%lld]", view_id, overlay_id];
34
35 // Set the size of the overlay view.
36 // This size is equal to the device screen size.
37 overlay_view.frame = [flutter_view convertRect:flutter_view.bounds toView:overlay_view_wrapper];
38 // Set a unique view identifier, so the overlay_view can be identified in XCUITests.
39 overlay_view.accessibilityIdentifier =
40 [NSString stringWithFormat:@"platform_view[%lld].overlay_view[%lld]", view_id, overlay_id];
41 }
42
43 // OverlayLayerPool
44 ////////////////////////////////////////////////////////
45
46 std::shared_ptr<OverlayLayer> OverlayLayerPool::GetNextLayer() {
47 std::shared_ptr<OverlayLayer> result;
48 if (available_layer_index_ < layers_.size()) {
49 result = layers_[available_layer_index_];
50 available_layer_index_++;
51 }
52
53 return result;
54 }
55
56 void OverlayLayerPool::CreateLayer(const std::shared_ptr<IOSContext>& ios_context,
57 MTLPixelFormat pixel_format) {
58 FML_DCHECK([[NSThread currentThread] isMainThread]);
59 std::shared_ptr<OverlayLayer> layer;
60 UIView* overlay_view;
61 UIView* overlay_view_wrapper;
62
63 CGFloat screenScale = [UIScreen mainScreen].scale;
64 overlay_view = [[FlutterOverlayView alloc] initWithContentsScale:screenScale
65 pixelFormat:pixel_format];
66 overlay_view_wrapper = [[FlutterOverlayView alloc] initWithContentsScale:screenScale
67 pixelFormat:pixel_format];
68
69 CALayer* ca_layer = overlay_view.layer;
70 std::unique_ptr<IOSSurface> ios_surface = IOSSurface::Create(ios_context, ca_layer);
71 std::unique_ptr<Surface> surface = ios_surface->CreateGPUSurface();
72
73 layer = std::make_shared<OverlayLayer>(overlay_view, overlay_view_wrapper, std::move(ios_surface),
74 std::move(surface));
75
76 // The overlay view wrapper masks the overlay view.
77 // This is required to keep the backing surface size unchanged between frames.
78 //
79 // Otherwise, changing the size of the overlay would require a new surface,
80 // which can be very expensive.
81 //
82 // This is the case of an animation in which the overlay size is changing in every frame.
83 //
84 // +------------------------+
85 // | overlay_view |
86 // | +--------------+ | +--------------+
87 // | | wrapper | | == mask => | overlay_view |
88 // | +--------------+ | +--------------+
89 // +------------------------+
90 layer->overlay_view_wrapper.clipsToBounds = YES;
91 [layer->overlay_view_wrapper addSubview:layer->overlay_view];
92
93 layers_.push_back(layer);
94 }
95
96 void OverlayLayerPool::RecycleLayers() {
97 available_layer_index_ = 0;
98 }
99
100 std::vector<std::shared_ptr<OverlayLayer>> OverlayLayerPool::RemoveUnusedLayers() {
101 std::vector<std::shared_ptr<OverlayLayer>> results;
102 for (size_t i = available_layer_index_; i < layers_.size(); i++) {
103 results.push_back(layers_[i]);
104 }
105 // Leave at least one overlay layer, to work around cases where scrolling
106 // platform views under an app bar continually adds and removes an
107 // overlay layer. This logic could be removed if https://github.com/flutter/flutter/issues/150646
108 // is fixed.
109 static constexpr size_t kLeakLayerCount = 1;
110 size_t erase_offset = std::max(available_layer_index_, kLeakLayerCount);
111 if (erase_offset < layers_.size()) {
112 layers_.erase(layers_.begin() + erase_offset, layers_.end());
113 }
114 return results;
115 }
116
117 size_t OverlayLayerPool::size() const {
118 return layers_.size();
119 }
120
121 } // namespace flutter
FlutterOverlayView.h
flutter::IOSSurface::Create
static std::unique_ptr< IOSSurface > Create(std::shared_ptr< IOSContext > context, CALayer *layer)
Definition: ios_surface.mm:18
flutter::OverlayLayerPool::RecycleLayers
void RecycleLayers()
Marks the layers in the pool as available for reuse.
Definition: overlay_layer_pool.mm:96
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:100
flutter::OverlayLayerPool::size
size_t size() const
The count of layers currently in the pool.
Definition: overlay_layer_pool.mm:117
flutter::OverlayLayerPool::GetNextLayer
std::shared_ptr< OverlayLayer > GetNextLayer()
Gets a layer from the pool if available.
Definition: overlay_layer_pool.mm:46
flutter::OverlayLayerPool::CreateLayer
void CreateLayer(const std::shared_ptr< IOSContext > &ios_context, MTLPixelFormat pixel_format)
Create a new overlay layer.
Definition: overlay_layer_pool.mm:56
FlutterOverlayView
Definition: FlutterOverlayView.h:23
ios_surface.h
flutter
Definition: accessibility_bridge.h:26
overlay_layer_pool.h
flutter::OverlayLayer::overlay_view_wrapper
UIView * overlay_view_wrapper
Definition: overlay_layer_pool.h:31
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::OverlayLayer::overlay_view
UIView * overlay_view
Definition: overlay_layer_pool.h:30
flutter::OverlayLayer::UpdateViewState
void UpdateViewState(UIView *flutter_view, DlRect rect, int64_t view_id, int64_t overlay_id)
Definition: overlay_layer_pool.mm:21