|
4 | 4 |
|
5 | 5 | #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h" |
6 | 6 | #include "flutter/fml/logging.h" |
| 7 | +#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.h" |
7 | 8 |
|
8 | 9 | namespace flutter { |
9 | 10 |
|
10 | | -FlutterCompositor::FlutterCompositor(id<FlutterViewProvider> view_provider) |
11 | | - : view_provider_(view_provider) { |
| 11 | +FlutterCompositor::FlutterCompositor(id<FlutterViewProvider> view_provider, |
| 12 | + FlutterPlatformViewController* platform_views_controller, |
| 13 | + id<MTLDevice> mtl_device) |
| 14 | + : view_provider_(view_provider), |
| 15 | + mtl_device_(mtl_device), |
| 16 | + platform_views_controller_(platform_views_controller) { |
12 | 17 | FML_CHECK(view_provider != nullptr) << "FlutterViewProvider* cannot be nullptr"; |
13 | 18 | } |
14 | 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 = GetView(kFlutterDefaultViewId); |
| 26 | + if (!view) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + CGSize size = CGSizeMake(config->size.width, config->size.height); |
| 31 | + |
| 32 | + backing_store_out->metal.struct_size = sizeof(FlutterMetalBackingStore); |
| 33 | + backing_store_out->metal.texture.struct_size = sizeof(FlutterMetalTexture); |
| 34 | + |
| 35 | + if (GetFrameStatus() != FrameStatus::kStarted) { |
| 36 | + StartFrame(); |
| 37 | + // If the backing store is for the first layer, return the MTLTexture for the |
| 38 | + // FlutterView. |
| 39 | + FlutterMetalRenderBackingStore* backingStore = |
| 40 | + reinterpret_cast<FlutterMetalRenderBackingStore*>([view backingStoreForSize:size]); |
| 41 | + backing_store_out->metal.texture.texture = |
| 42 | + (__bridge FlutterMetalTextureHandle)backingStore.texture; |
| 43 | + } else { |
| 44 | + FlutterIOSurfaceHolder* io_surface_holder = [[FlutterIOSurfaceHolder alloc] init]; |
| 45 | + [io_surface_holder recreateIOSurfaceWithSize:size]; |
| 46 | + auto texture_descriptor = |
| 47 | + [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm |
| 48 | + width:size.width |
| 49 | + height:size.height |
| 50 | + mipmapped:NO]; |
| 51 | + texture_descriptor.usage = |
| 52 | + MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget | MTLTextureUsageShaderWrite; |
| 53 | + |
| 54 | + backing_store_out->metal.texture.texture = (__bridge_retained FlutterMetalTextureHandle) |
| 55 | + [mtl_device_ newTextureWithDescriptor:texture_descriptor |
| 56 | + iosurface:[io_surface_holder ioSurface] |
| 57 | + plane:0]; |
| 58 | + |
| 59 | + backing_store_out->metal.texture.user_data = (__bridge_retained void*)io_surface_holder; |
| 60 | + } |
| 61 | + |
| 62 | + backing_store_out->type = kFlutterBackingStoreTypeMetal; |
| 63 | + backing_store_out->metal.texture.destruction_callback = [](void* user_data) { |
| 64 | + if (user_data != nullptr) { |
| 65 | + CFRelease(user_data); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + return true; |
| 70 | +} |
| 71 | + |
| 72 | +bool FlutterCompositor::Present(const FlutterLayer** layers, size_t layers_count) { |
| 73 | + // TODO(dkwingsmt): This class only supports single-view for now. As more |
| 74 | + // classes are gradually converted to multi-view, it should get the view ID |
| 75 | + // from somewhere. |
| 76 | + FlutterView* view = GetView(kFlutterDefaultViewId); |
| 77 | + if (!view) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + SetFrameStatus(FrameStatus::kPresenting); |
| 82 | + |
| 83 | + bool has_flutter_content = false; |
| 84 | + for (size_t i = 0; i < layers_count; ++i) { |
| 85 | + const auto* layer = layers[i]; |
| 86 | + FlutterBackingStore* backing_store = const_cast<FlutterBackingStore*>(layer->backing_store); |
| 87 | + |
| 88 | + switch (layer->type) { |
| 89 | + case kFlutterLayerContentTypeBackingStore: { |
| 90 | + if (backing_store->metal.texture.user_data) { |
| 91 | + FlutterIOSurfaceHolder* io_surface_holder = |
| 92 | + (__bridge FlutterIOSurfaceHolder*)backing_store->metal.texture.user_data; |
| 93 | + IOSurfaceRef io_surface = [io_surface_holder ioSurface]; |
| 94 | + InsertCALayerForIOSurface(view, io_surface); |
| 95 | + } |
| 96 | + has_flutter_content = true; |
| 97 | + break; |
| 98 | + } |
| 99 | + case kFlutterLayerContentTypePlatformView: { |
| 100 | + PresentPlatformView(view, layer, i); |
| 101 | + break; |
| 102 | + } |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + return EndFrame(has_flutter_content); |
| 107 | +} |
| 108 | + |
| 109 | +void FlutterCompositor::PresentPlatformView(FlutterView* default_base_view, |
| 110 | + const FlutterLayer* layer, |
| 111 | + size_t layer_position) { |
| 112 | + // TODO (https://github.com/flutter/flutter/issues/96668) |
| 113 | + // once the issue is fixed, this check will pass. |
| 114 | + FML_DCHECK([[NSThread currentThread] isMainThread]) |
| 115 | + << "Must be on the main thread to present platform views"; |
| 116 | + |
| 117 | + int64_t platform_view_id = layer->platform_view->identifier; |
| 118 | + NSView* platform_view = [platform_views_controller_ platformViewWithID:platform_view_id]; |
| 119 | + |
| 120 | + FML_DCHECK(platform_view) << "Platform view not found for id: " << platform_view_id; |
| 121 | + |
| 122 | + CGFloat scale = [[NSScreen mainScreen] backingScaleFactor]; |
| 123 | + platform_view.frame = CGRectMake(layer->offset.x / scale, layer->offset.y / scale, |
| 124 | + layer->size.width / scale, layer->size.height / scale); |
| 125 | + if (platform_view.superview == nil) { |
| 126 | + [default_base_view addSubview:platform_view]; |
| 127 | + } |
| 128 | + platform_view.layer.zPosition = layer_position; |
| 129 | +} |
| 130 | + |
| 131 | +bool FlutterCompositor::CollectBackingStore(const FlutterBackingStore* backing_store) { |
| 132 | + // If we allocated this MTLTexture ourselves, user_data is not null, and we will need |
| 133 | + // to release it manually. |
| 134 | + if (backing_store->metal.texture.user_data != nullptr && |
| 135 | + backing_store->metal.texture.texture != nullptr) { |
| 136 | + CFRelease(backing_store->metal.texture.texture); |
| 137 | + } |
| 138 | + return true; |
| 139 | +} |
| 140 | + |
15 | 141 | void FlutterCompositor::SetPresentCallback( |
16 | 142 | const FlutterCompositor::PresentCallback& present_callback) { |
17 | 143 | present_callback_ = present_callback; |
|
0 commit comments