Skip to content

Commit

Permalink
Adopt API for layer hosting
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=268562
rdar://121747472

Reviewed by Brent Fulgham.

Adopt BrowserEngineKit API for layer hosting. This is also a speculative fix for a crash seen when
calling [_SEHostingView setHandle:], since the forward declared handle property was incorrectly
declared. This is possibly fixed in this patch by using the proper system headers instead of
using an incorrect property declaration.

* Source/WebKit/GPUProcess/media/cocoa/RemoteMediaPlayerProxyCocoa.mm:
(WebKit::RemoteMediaPlayerProxy::setVideoLayerSizeFenced):
* Source/WebKit/Platform/cocoa/LayerHostingContext.h:
(WebKit::LayerHostingContext::hostable const):
* Source/WebKit/Platform/cocoa/LayerHostingContext.mm:
(WebKit::LayerHostingContext::createForExternalHostingProcess):
(WebKit::LayerHostingContext::xpcRepresentation const):
(WebKit::LayerHostingContext::createHostingUpdateCoordinator):
(WebKit::LayerHostingContext::createHostingHandle):
* Source/WebKit/Platform/spi/Cocoa/ExtensionKitSPI.h:
* Source/WebKit/UIProcess/Cocoa/VideoPresentationManagerProxy.mm:
(WebKit::VideoPresentationManagerProxy::createLayerHostViewWithID):
(WebKit::VideoPresentationManagerProxy::setVideoLayerFrame):

Canonical link: https://commits.webkit.org/273950@main
  • Loading branch information
pvollan committed Feb 1, 2024
1 parent 944eaf1 commit ecb4508
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
#import <wtf/MachSendRight.h>

#if USE(EXTENSIONKIT)
#import "ExtensionKitSoftLink.h"
#import <BrowserEngineKit/BELayerHierarchy.h>
#import <BrowserEngineKit/BELayerHierarchyHandle.h>
#import <BrowserEngineKit/BELayerHierarchyHostingTransactionCoordinator.h>
#endif

namespace WebKit {
Expand Down Expand Up @@ -108,13 +110,13 @@
ALWAYS_LOG(LOGIDENTIFIER, size.width(), "x", size.height());

#if USE(EXTENSIONKIT)
RetainPtr<_SEHostingUpdateCoordinator> hostingUpdateCoordinator;
RetainPtr<BELayerHierarchyHostingTransactionCoordinator> hostingUpdateCoordinator;
#endif

if (m_inlineLayerHostingContext) {
#if USE(EXTENSIONKIT)
hostingUpdateCoordinator = LayerHostingContext::createHostingUpdateCoordinator(machSendRight.sendRight());
[hostingUpdateCoordinator addHostable:m_inlineLayerHostingContext->hostable().get()];
[hostingUpdateCoordinator addLayerHierarchy:m_inlineLayerHostingContext->hostable().get()];
#else
m_inlineLayerHostingContext->setFencePort(machSendRight.sendRight());
#endif
Expand Down
14 changes: 7 additions & 7 deletions Source/WebKit/Platform/cocoa/LayerHostingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ OBJC_CLASS CALayer;
OBJC_CLASS CAContext;

#if USE(EXTENSIONKIT)
OBJC_CLASS _SEHostable;
OBJC_CLASS _SEHostingHandle;
OBJC_CLASS _SEHostingUpdateCoordinator;
OBJC_CLASS BELayerHierarchy;
OBJC_CLASS BELayerHierarchyHandle;
OBJC_CLASS BELayerHierarchyHostingTransactionCoordinator;
#endif

namespace WTF {
Expand Down Expand Up @@ -114,10 +114,10 @@ class LayerHostingContext {

#if USE(EXTENSIONKIT)
OSObjectPtr<xpc_object_t> xpcRepresentation() const;
RetainPtr<_SEHostable> hostable() const { return m_hostable; }
RetainPtr<BELayerHierarchy> hostable() const { return m_hostable; }

static RetainPtr<_SEHostingHandle> createHostingHandle(uint64_t pid, uint64_t contextID);
static RetainPtr<_SEHostingUpdateCoordinator> createHostingUpdateCoordinator(mach_port_t sendRight);
static RetainPtr<BELayerHierarchyHandle> createHostingHandle(uint64_t pid, uint64_t contextID);
static RetainPtr<BELayerHierarchyHostingTransactionCoordinator> createHostingUpdateCoordinator(mach_port_t sendRight);
#endif

private:
Expand All @@ -128,7 +128,7 @@ class LayerHostingContext {
LayerHostingContextID m_cachedContextID;
RetainPtr<CAContext> m_context;
#if USE(EXTENSIONKIT)
RetainPtr<_SEHostable> m_hostable;
RetainPtr<BELayerHierarchy> m_hostable;
#endif
};

Expand Down
34 changes: 22 additions & 12 deletions Source/WebKit/Platform/cocoa/LayerHostingContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@

#if USE(EXTENSIONKIT)
#import "ExtensionKitSPI.h"

SOFT_LINK_FRAMEWORK_OPTIONAL(ServiceExtensions);
SOFT_LINK_CLASS_OPTIONAL(ServiceExtensions, _SEHostable);
SOFT_LINK_CLASS_OPTIONAL(ServiceExtensions, _SEHostingHandle);
SOFT_LINK_CLASS_OPTIONAL(ServiceExtensions, _SEHostingUpdateCoordinator);
#import <BrowserEngineKit/BELayerHierarchy.h>
#import <BrowserEngineKit/BELayerHierarchyHandle.h>
#import <BrowserEngineKit/BELayerHierarchyHostingTransactionCoordinator.h>

SOFT_LINK_FRAMEWORK_OPTIONAL(BrowserEngineKit);
SOFT_LINK_CLASS_OPTIONAL(BrowserEngineKit, BELayerHierarchy);
SOFT_LINK_CLASS_OPTIONAL(BrowserEngineKit, BELayerHierarchyHandle);
SOFT_LINK_CLASS_OPTIONAL(BrowserEngineKit, BELayerHierarchyHostingTransactionCoordinator);
#endif

namespace WebKit {
Expand Down Expand Up @@ -78,8 +81,7 @@
};
#if USE(EXTENSIONKIT)
if (options.useHostable) {
// Note: this reads like a leak, but the object returned from createHostableWithOptions should not be adopted.
layerHostingContext->m_hostable = [get_SEHostableClass() createHostableWithOptions:contextOptions error:nil];
layerHostingContext->m_hostable = [getBELayerHierarchyClass() layerHierarchyWithOptions:contextOptions error:nil];
return layerHostingContext;
}
#endif
Expand Down Expand Up @@ -215,22 +217,30 @@
{
if (!m_hostable)
return nullptr;
return [[m_hostable handle] xpcRepresentation];
return [[m_hostable handle] createXPCRepresentation];
}

RetainPtr<_SEHostingUpdateCoordinator> LayerHostingContext::createHostingUpdateCoordinator(mach_port_t sendRight)
RetainPtr<BELayerHierarchyHostingTransactionCoordinator> LayerHostingContext::createHostingUpdateCoordinator(mach_port_t sendRight)
{
auto xpcRepresentation = adoptOSObject(xpc_dictionary_create(nullptr, nullptr, 0));
xpc_dictionary_set_mach_send(xpcRepresentation.get(), machPortKey, sendRight);
return adoptNS([alloc_SEHostingUpdateCoordinatorInstance() initFromXPCRepresentation:xpcRepresentation.get()]);
NSError* error = nil;
auto coordinator = [getBELayerHierarchyHostingTransactionCoordinatorClass() coordinatorWithXPCRepresentation:xpcRepresentation.get() error:&error];
if (error)
NSLog(@"Could not create update coordinator, error = %@", error);
return coordinator;
}

RetainPtr<_SEHostingHandle> LayerHostingContext::createHostingHandle(uint64_t pid, uint64_t contextID)
RetainPtr<BELayerHierarchyHandle> LayerHostingContext::createHostingHandle(uint64_t pid, uint64_t contextID)
{
auto xpcRepresentation = adoptOSObject(xpc_dictionary_create(nullptr, nullptr, 0));
xpc_dictionary_set_uint64(xpcRepresentation.get(), processIDKey, pid);
xpc_dictionary_set_uint64(xpcRepresentation.get(), contextIDKey, contextID);
return adoptNS([alloc_SEHostingHandleInstance() initFromXPCRepresentation:xpcRepresentation.get()]);
NSError* error = nil;
auto handle = [getBELayerHierarchyHandleClass() handleWithXPCRepresentation:xpcRepresentation.get() error:&error];
if (error)
NSLog(@"Could not create layer hierarchy handle, error = %@", error);
return handle;
}
#endif

Expand Down
40 changes: 17 additions & 23 deletions Source/WebKit/Platform/spi/Cocoa/ExtensionKitSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#if __has_include(<BrowserEngineKit/BELayerHierarchy_Private.h>)
#import <BrowserEngineKit/BELayerHierarchy_Private.h>
#else
#import <BrowserEngineKit/BELayerHierarchy.h>
@class CAContext;

NS_ASSUME_NONNULL_BEGIN

@interface BELayerHierarchy ()
+ (nullable BELayerHierarchy *)layerHierarchyWithOptions:(NSDictionary *)options error:(NSError **)error NS_REFINED_FOR_SWIFT;
- (instancetype)initWithContext:(CAContext *)context;
@end

NS_ASSUME_NONNULL_END

#endif

#if __has_include(<ServiceExtensions/ServiceExtensions_Private.h>)
#import <ServiceExtensions/ServiceExtensions_Private.h>
#else
Expand Down Expand Up @@ -116,29 +133,6 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) NSString *mediaEnvironment;
@end

@interface _SEHostingHandle: NSObject
-(instancetype)initFromXPCRepresentation:(xpc_object_t)xpcRepresentation;
-(xpc_object_t)xpcRepresentation;
@end

@interface _SEHostable: NSObject
+(_SEHostable*)createHostableWithOptions:(NSDictionary*)dict error:(NSError**)error;
@property (nonatomic, readonly) _SEHostingHandle* handle;
@property (nonatomic, strong) CALayer *layer;
@end

@interface _SEHostingView: UIView
@property (nonatomic, retain) _SEHostingHandle* handle;
@end

@interface _SEHostingUpdateCoordinator : NSObject
-(instancetype)initFromXPCRepresentation:(xpc_object_t)xpcRepresentation;
-(xpc_object_t)xpcRepresentation;
-(void)addHostable:(_SEHostable*)hostable;
-(void)addHostingView:(_SEHostingView*)hostingView;
-(void)commit;
@end

NS_ASSUME_NONNULL_END

#endif // USE(EXTENSIONKIT)
13 changes: 7 additions & 6 deletions Source/WebKit/UIProcess/Cocoa/VideoPresentationManagerProxy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
#endif

#if USE(EXTENSIONKIT)
#import "ExtensionKitSoftLink.h"
#import <BrowserEngineKit/BELayerHierarchyHostingTransactionCoordinator.h>
#import <BrowserEngineKit/BELayerHierarchyHostingView.h>
#endif

#define MESSAGE_CHECK(assertion) MESSAGE_CHECK_BASE(assertion, m_page->process().connection())
Expand All @@ -76,7 +77,7 @@ @implementation WKLayerHostView {
#endif
#if USE(EXTENSIONKIT)
@public
RetainPtr<_SEHostingView> _hostingView;
RetainPtr<BELayerHierarchyHostingView> _hostingView;
#endif
}

Expand Down Expand Up @@ -726,7 +727,7 @@ - (BOOL)prefersStatusBarHidden
model->setLayerHostView(view);

#if USE(EXTENSIONKIT)
auto hostingView = adoptNS([alloc_SEHostingViewInstance() init]);
auto hostingView = adoptNS([[BELayerHierarchyHostingView alloc] init]);
view->_hostingView = hostingView;
[view addSubview:hostingView.get()];
auto layer = [hostingView layer];
Expand Down Expand Up @@ -1219,9 +1220,9 @@ - (BOOL)prefersStatusBarHidden
#if USE(EXTENSIONKIT)
RetainPtr<WKLayerHostView> view = static_cast<WKLayerHostView*>(model->layerHostView());
if (view && view->_hostingView) {
auto hostingUpdateCoordinator = adoptNS([alloc_SEHostingUpdateCoordinatorInstance() init]);
[hostingUpdateCoordinator addHostingView:view->_hostingView.get()];
OSObjectPtr<xpc_object_t> xpcRepresentationHostingCoordinator = [hostingUpdateCoordinator xpcRepresentation];
auto hostingUpdateCoordinator = [BELayerHierarchyHostingTransactionCoordinator coordinatorWithError:nil];
[hostingUpdateCoordinator addLayerHierarchyHostingView:view->_hostingView.get()];
OSObjectPtr<xpc_object_t> xpcRepresentationHostingCoordinator = [hostingUpdateCoordinator createXPCRepresentation];
fenceSendRight = MachSendRight::adopt(xpc_dictionary_copy_mach_send(xpcRepresentationHostingCoordinator.get(), machPortKey));
m_page->send(Messages::VideoPresentationManager::SetVideoLayerFrameFenced(contextId, frame, WTFMove(fenceSendRight)));
[hostingUpdateCoordinator commit];
Expand Down

0 comments on commit ecb4508

Please sign in to comment.