From 2d3271403bb85407f775bd4fe75ccc96542b0244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Markovi=C4=87?= Date: Wed, 31 Dec 2025 15:11:42 +0100 Subject: [PATCH] Metal backend resource for MacOS fixes (#4020) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- platform/macos/src/MLNMapView+Impl.h | 7 +++++++ platform/macos/src/MLNMapView+Metal.h | 1 + platform/macos/src/MLNMapView+Metal.mm | 15 +++++++++++++-- platform/macos/src/MLNMapView.h | 11 +++++++++++ platform/macos/src/MLNMapView.mm | 7 +++++++ 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/platform/macos/src/MLNMapView+Impl.h b/platform/macos/src/MLNMapView+Impl.h index 07fd08cc2..d314baf9c 100644 --- a/platform/macos/src/MLNMapView+Impl.h +++ b/platform/macos/src/MLNMapView+Impl.h @@ -2,6 +2,8 @@ #import #import +#import "MLNBackendResource.h" + @class MLNMapView; typedef struct _CGLContextObject* CGLContextObj; @@ -20,6 +22,11 @@ class MLNMapViewImpl : public mbgl::MapObserver { virtual CGLContextObj getCGLContextObj() { return nullptr; } +#if MLN_RENDER_BACKEND_METAL + // Returns the backend resource for Metal rendering in custom layers + virtual MLNBackendResource* getObject() { return nullptr; } +#endif + // Called by the view delegate when it's time to render. void render(); diff --git a/platform/macos/src/MLNMapView+Metal.h b/platform/macos/src/MLNMapView+Metal.h index f6d12e66c..55a6adb43 100644 --- a/platform/macos/src/MLNMapView+Metal.h +++ b/platform/macos/src/MLNMapView+Metal.h @@ -36,6 +36,7 @@ class MLNMapViewMetalImpl final : public MLNMapViewImpl, mbgl::gfx::RendererBackend& getRendererBackend() override { return *this; } mbgl::PremultipliedImage readStillImage() override; + MLNBackendResource* getObject() override; private: bool presentsWithTransaction = false; diff --git a/platform/macos/src/MLNMapView+Metal.mm b/platform/macos/src/MLNMapView+Metal.mm index 5c4759cbd..8513803c6 100644 --- a/platform/macos/src/MLNMapView+Metal.mm +++ b/platform/macos/src/MLNMapView+Metal.mm @@ -44,8 +44,10 @@ public: commandQueue = [mtlView.device newCommandQueue]; } - commandBuffer = [commandQueue commandBuffer]; - commandBufferPtr = NS::RetainPtr((__bridge MTL::CommandBuffer*)commandBuffer); + if (!commandBuffer) { + commandBuffer = [commandQueue commandBuffer]; + commandBufferPtr = NS::RetainPtr((__bridge MTL::CommandBuffer*)commandBuffer); + } } const mbgl::mtl::RendererBackend& getBackend() const override { return backend; } @@ -158,3 +160,12 @@ mbgl::PremultipliedImage MLNMapViewMetalImpl::readStillImage() { // return readFramebuffer(mapView.framebufferSize); // TODO: RendererBackend::readFramebuffer return {}; } + +MLNBackendResource* MLNMapViewMetalImpl::getObject() { + auto& resource = getResource(); + + return [[MLNBackendResource alloc] initWithMTKView:resource.mtlView + device:resource.mtlView.device + renderPassDescriptor:resource.mtlView.currentRenderPassDescriptor + commandBuffer:resource.commandBuffer]; +} diff --git a/platform/macos/src/MLNMapView.h b/platform/macos/src/MLNMapView.h index 389474131..1f12769fe 100644 --- a/platform/macos/src/MLNMapView.h +++ b/platform/macos/src/MLNMapView.h @@ -2,6 +2,7 @@ #import #import +#import "MLNBackendResource.h" #import "MLNFoundation.h" #import "MLNGeometry.h" #import "MLNMapOptions.h" @@ -1336,6 +1337,16 @@ around the returned camera object if it were set as the receiver’s camera. */ - (void)clearActionJournalLog; +#if MLN_RENDER_BACKEND_METAL +/** + Returns the Metal backend resource for use in custom style layers. + + This property provides access to the Metal device, command buffer, and other + resources needed for custom Metal rendering in MLNCustomStyleLayer subclasses. + */ +- (MLNBackendResource *)backendResource; +#endif + @end NS_ASSUME_NONNULL_END diff --git a/platform/macos/src/MLNMapView.mm b/platform/macos/src/MLNMapView.mm index 6dc79ba88..cb5afa550 100644 --- a/platform/macos/src/MLNMapView.mm +++ b/platform/macos/src/MLNMapView.mm @@ -48,6 +48,7 @@ #import #import "MLNMapView+Impl.h" +#import "MLNBackendResource.h" #import "NSBundle+MLNAdditions.h" #import "NSDate+MLNAdditions.h" #import "NSProcessInfo+MLNAdditions.h" @@ -3314,4 +3315,10 @@ public: actionJournal->clearLog(); } +#if MLN_RENDER_BACKEND_METAL +- (MLNBackendResource *)backendResource { + return _mbglView->getObject(); +} +#endif + @end