| Flutter macOS Embedder |

FlutterCompositor.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 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h"
6 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMutatorView.h"
7
8 #include "flutter/fml/logging.h"
9
10 namespace flutter {
11
12 FlutterCompositor::FlutterCompositor(id<FlutterViewProvider> view_provider,
13 FlutterPlatformViewController* platform_view_controller)
14 : view_provider_(view_provider),
15 platform_view_controller_(platform_view_controller),
16 mutator_views_([NSMapTable strongToStrongObjectsMapTable]) {
17 FML_CHECK(view_provider != nullptr) << "view_provider cannot be nullptr";
18 }
19
20 bool FlutterCompositor::CreateBackingStore(const FlutterBackingStoreConfig* config,
21 FlutterBackingStore* backing_store_out) {
22 // TODO(dkwingsmt): This class only supports single-view for now. As more
23 // classes are gradually converted to multi-view, it should get the view ID
24 // from somewhere.
25 FlutterView* view = [view_provider_ viewForId:kFlutterImplicitViewId];
26 if (!view) {
27 return false;
28 }
29
30 CGSize size = CGSizeMake(config->size.width, config->size.height);
31 FlutterSurface* surface = [view.surfaceManager surfaceForSize:size];
32 memset(backing_store_out, 0, sizeof(FlutterBackingStore));
33 backing_store_out->struct_size = sizeof(FlutterBackingStore);
34 backing_store_out->type = kFlutterBackingStoreTypeMetal;
35 backing_store_out->metal.struct_size = sizeof(FlutterMetalBackingStore);
36 backing_store_out->metal.texture = surface.asFlutterMetalTexture;
37 return true;
38 }
39
40 bool FlutterCompositor::Present(FlutterViewId view_id,
41 const FlutterLayer** layers,
42 size_t layers_count) {
43 FlutterView* view = [view_provider_ viewForId:view_id];
44 if (!view) {
45 return false;
46 }
47
48 NSMutableArray* surfaces = [NSMutableArray array];
49 for (size_t i = 0; i < layers_count; i++) {
50 const FlutterLayer* layer = layers[i];
51 if (layer->type == kFlutterLayerContentTypeBackingStore) {
52 FlutterSurface* surface =
53 [FlutterSurface fromFlutterMetalTexture:&layer->backing_store->metal.texture];
54
55 if (surface) {
56 FlutterSurfacePresentInfo* info = [[FlutterSurfacePresentInfo alloc] init];
57 info.surface = surface;
58 info.offset = CGPointMake(layer->offset.x, layer->offset.y);
59 info.zIndex = i;
60 FlutterBackingStorePresentInfo* present_info = layer->backing_store_present_info;
61 if (present_info != nullptr && present_info->paint_region != nullptr) {
62 auto paint_region = present_info->paint_region;
63 // Safe because the size of FlutterRect is not expected to change.
64 info.paintRegion = std::vector<FlutterRect>(
65 paint_region->rects, paint_region->rects + paint_region->rects_count);
66 }
67 [surfaces addObject:info];
68 }
69 }
70 }
71
72 [view.surfaceManager present:surfaces
73 notify:^{
74 PresentPlatformViews(view, layers, layers_count);
75 }];
76
77 return true;
78 }
79
80 void FlutterCompositor::PresentPlatformViews(FlutterView* default_base_view,
81 const FlutterLayer** layers,
82 size_t layers_count) {
83 FML_DCHECK([[NSThread currentThread] isMainThread])
84 << "Must be on the main thread to present platform views";
85
86 // Active mutator views for this frame.
87 NSMutableArray<FlutterMutatorView*>* present_mutators = [NSMutableArray array];
88
89 for (size_t i = 0; i < layers_count; i++) {
90 FlutterLayer* layer = (FlutterLayer*)layers[i];
91 if (layer->type == kFlutterLayerContentTypePlatformView) {
92 [present_mutators addObject:PresentPlatformView(default_base_view, layer, i)];
93 }
94 }
95
96 NSMutableArray<FlutterMutatorView*>* obsolete_mutators =
97 [NSMutableArray arrayWithArray:[mutator_views_ objectEnumerator].allObjects];
98 [obsolete_mutators removeObjectsInArray:present_mutators];
99
100 for (FlutterMutatorView* mutator in obsolete_mutators) {
101 [mutator_views_ removeObjectForKey:mutator.platformView];
102 [mutator removeFromSuperview];
103 }
104
105 [platform_view_controller_ disposePlatformViews];
106 }
107
108 FlutterMutatorView* FlutterCompositor::PresentPlatformView(FlutterView* default_base_view,
109 const FlutterLayer* layer,
110 size_t layer_position) {
111 FML_DCHECK([[NSThread currentThread] isMainThread])
112 << "Must be on the main thread to present platform views";
113
114 int64_t platform_view_id = layer->platform_view->identifier;
115 NSView* platform_view = [platform_view_controller_ platformViewWithID:platform_view_id];
116
117 FML_DCHECK(platform_view) << "Platform view not found for id: " << platform_view_id;
118
119 FlutterMutatorView* container = [mutator_views_ objectForKey:platform_view];
120
121 if (!container) {
122 container = [[FlutterMutatorView alloc] initWithPlatformView:platform_view];
123 [mutator_views_ setObject:container forKey:platform_view];
124 [default_base_view addSubview:container];
125 }
126
127 container.layer.zPosition = layer_position;
128 [container applyFlutterLayer:layer];
129
130 return container;
131 }
132
133 } // namespace flutter
FlutterMutatorView.h
FlutterView::surfaceManager
FlutterSurfaceManager * surfaceManager
Definition: FlutterView.h:66
FlutterMutatorView
Definition: FlutterMutatorView.h:11
-[FlutterSurface asFlutterMetalTexture]
FlutterMetalTexture asFlutterMetalTexture()
Definition: FlutterSurface.mm:45
FlutterPlatformViewController
Definition: FlutterPlatformViewController.h:14
kFlutterImplicitViewId
constexpr FlutterViewId kFlutterImplicitViewId
Definition: FlutterView.h:23
flutter::FlutterCompositor::Present
bool Present(FlutterViewId view_id, const FlutterLayer **layers, size_t layers_count)
Definition: FlutterCompositor.mm:40
FlutterSurface
Definition: FlutterSurface.h:13
flutter::FlutterCompositor::CreateBackingStore
bool CreateBackingStore(const FlutterBackingStoreConfig *config, FlutterBackingStore *backing_store_out)
Definition: FlutterCompositor.mm:20
flutter::FlutterCompositor::FlutterCompositor
FlutterCompositor(id< FlutterViewProvider > view_provider, FlutterPlatformViewController *platform_views_controller)
Definition: FlutterCompositor.mm:12
FlutterSurfacePresentInfo::surface
FlutterSurface * surface
Definition: FlutterSurfaceManager.h:17
flutter
Definition: AccessibilityBridgeMac.h:16
FlutterSurfacePresentInfo::offset
CGPoint offset
Definition: FlutterSurfaceManager.h:18
FlutterSurfacePresentInfo
Definition: FlutterSurfaceManager.h:15
FlutterSurfacePresentInfo::zIndex
size_t zIndex
Definition: FlutterSurfaceManager.h:19
FlutterView
Definition: FlutterView.h:45
FlutterSurfacePresentInfo::paintRegion
std::vector< FlutterRect > paintRegion
Definition: FlutterSurfaceManager.h:20
FlutterCompositor.h
FlutterViewId
int64_t FlutterViewId
Definition: FlutterView.h:12