Flutter macOS Embedder
flutter::FlutterCompositor Class Reference

#include <FlutterCompositor.h>

Public Member Functions

 FlutterCompositor (id< FlutterViewProvider > view_provider, FlutterTimeConverter *time_converter, FlutterPlatformViewController *platform_views_controller)
 
 ~FlutterCompositor ()=default
 
void AddView (FlutterViewId view_id)
 
void RemoveView (FlutterViewId view_id)
 
bool CreateBackingStore (const FlutterBackingStoreConfig *config, FlutterBackingStore *backing_store_out)
 
bool Present (FlutterViewIdentifier view_id, const FlutterLayer **layers, size_t layers_count)
 
size_t DebugNumViews ()
 

Detailed Description

Definition at line 36 of file FlutterCompositor.h.

Constructor & Destructor Documentation

◆ FlutterCompositor()

flutter::FlutterCompositor::FlutterCompositor ( id< FlutterViewProvider view_provider,
FlutterTimeConverter time_converter,
FlutterPlatformViewController platform_views_controller 
)

Definition at line 35 of file FlutterCompositor.mm.

38  : view_provider_(view_provider),
39  time_converter_(time_converter),
40  platform_view_controller_(platform_view_controller) {
41  FML_CHECK(view_provider != nullptr) << "view_provider cannot be nullptr";
42 }

◆ ~FlutterCompositor()

flutter::FlutterCompositor::~FlutterCompositor ( )
default

Member Function Documentation

◆ AddView()

void flutter::FlutterCompositor::AddView ( FlutterViewId  view_id)

Definition at line 44 of file FlutterCompositor.mm.

44  {
45  dispatch_assert_queue(dispatch_get_main_queue());
46  presenters_.try_emplace(view_id);
47 }

◆ CreateBackingStore()

bool flutter::FlutterCompositor::CreateBackingStore ( const FlutterBackingStoreConfig *  config,
FlutterBackingStore *  backing_store_out 
)

Definition at line 54 of file FlutterCompositor.mm.

55  {
56  FlutterView* view = [view_provider_ viewForIdentifier:config->view_id];
57  if (!view) {
58  return false;
59  }
60 
61  CGSize size = CGSizeMake(config->size.width, config->size.height);
62  FlutterSurface* surface = [view.surfaceManager surfaceForSize:size];
63  memset(backing_store_out, 0, sizeof(FlutterBackingStore));
64  backing_store_out->struct_size = sizeof(FlutterBackingStore);
65  backing_store_out->type = kFlutterBackingStoreTypeMetal;
66  backing_store_out->metal.struct_size = sizeof(FlutterMetalBackingStore);
67  backing_store_out->metal.texture = surface.asFlutterMetalTexture;
68  return true;
69 }

References FlutterSurface::asFlutterMetalTexture, and FlutterView::surfaceManager.

◆ DebugNumViews()

size_t flutter::FlutterCompositor::DebugNumViews ( )

Definition at line 134 of file FlutterCompositor.mm.

134  {
135  return presenters_.size();
136 }

◆ Present()

bool flutter::FlutterCompositor::Present ( FlutterViewIdentifier  view_id,
const FlutterLayer **  layers,
size_t  layers_count 
)

Definition at line 71 of file FlutterCompositor.mm.

73  {
74  FlutterView* view = [view_provider_ viewForIdentifier:view_id];
75  if (!view) {
76  return false;
77  }
78 
79  NSMutableArray* surfaces = [NSMutableArray array];
80  for (size_t i = 0; i < layers_count; i++) {
81  const FlutterLayer* layer = layers[i];
82  if (layer->type == kFlutterLayerContentTypeBackingStore) {
83  FlutterSurface* surface =
84  [FlutterSurface fromFlutterMetalTexture:&layer->backing_store->metal.texture];
85 
86  if (surface) {
88  info.surface = surface;
89  info.offset = CGPointMake(layer->offset.x, layer->offset.y);
90  info.zIndex = i;
91  FlutterBackingStorePresentInfo* present_info = layer->backing_store_present_info;
92  if (present_info != nullptr && present_info->paint_region != nullptr) {
93  auto paint_region = present_info->paint_region;
94  // Safe because the size of FlutterRect is not expected to change.
95  info.paintRegion = std::vector<FlutterRect>(
96  paint_region->rects, paint_region->rects + paint_region->rects_count);
97  }
98  [surfaces addObject:info];
99  }
100  }
101  }
102 
103  CFTimeInterval presentation_time = 0;
104 
105  if (layers_count > 0 && layers[0]->presentation_time != 0) {
106  presentation_time = [time_converter_ engineTimeToCAMediaTime:layers[0]->presentation_time];
107  }
108 
109  // Notify block below may be called asynchronously, hence the need to copy
110  // the layer information instead of passing the original pointers from embedder.
111  auto layers_copy = std::make_shared<std::vector<LayerVariant>>(CopyLayers(layers, layers_count));
112 
113  [view.surfaceManager presentSurfaces:surfaces
114  atTime:presentation_time
115  notify:^{
116  // Accessing presenters_ here does not need a
117  // lock to avoid race condition against
118  // AddView and RemoveView, since all three
119  // take place on the platform thread. (The
120  // macOS API requires platform view presenting
121  // to take place on the platform thread,
122  // enforced by `FlutterThreadSynchronizer`.)
123  dispatch_assert_queue(dispatch_get_main_queue());
124  auto found_presenter = presenters_.find(view_id);
125  if (found_presenter != presenters_.end()) {
126  found_presenter->second.PresentPlatformViews(
127  view, *layers_copy, platform_view_controller_);
128  }
129  }];
130 
131  return true;
132 }

References FlutterSurfacePresentInfo::offset, FlutterSurfacePresentInfo::paintRegion, FlutterSurfacePresentInfo::surface, FlutterView::surfaceManager, and FlutterSurfacePresentInfo::zIndex.

◆ RemoveView()

void flutter::FlutterCompositor::RemoveView ( FlutterViewId  view_id)

Definition at line 49 of file FlutterCompositor.mm.

49  {
50  dispatch_assert_queue(dispatch_get_main_queue());
51  presenters_.erase(view_id);
52 }

The documentation for this class was generated from the following files:
FlutterView::surfaceManager
FlutterSurfaceManager * surfaceManager
Definition: FlutterView.h:57
-[FlutterSurface asFlutterMetalTexture]
FlutterMetalTexture asFlutterMetalTexture()
Definition: FlutterSurface.mm:59
FlutterSurface
Definition: FlutterSurface.h:16
FlutterSurfacePresentInfo::surface
FlutterSurface * surface
Definition: FlutterSurfaceManager.h:20
FlutterSurfacePresentInfo::offset
CGPoint offset
Definition: FlutterSurfaceManager.h:21
FlutterSurfacePresentInfo
Definition: FlutterSurfaceManager.h:18
FlutterSurfacePresentInfo::zIndex
size_t zIndex
Definition: FlutterSurfaceManager.h:22
FlutterView
Definition: FlutterView.h:35
FlutterSurfacePresentInfo::paintRegion
std::vector< FlutterRect > paintRegion
Definition: FlutterSurfaceManager.h:23