Skip to content

Commit

Permalink
[WebGPU] Replace m_performWithMediaPlayerOnMainThread with the m_gpuC…
Browse files Browse the repository at this point in the history
…onnectionToWebProcess

https://bugs.webkit.org/show_bug.cgi?id=261418
<radar://115294252>

Reviewed by Kimmo Kinnunen.

Remove the m_performWithMediaPlayerOnMainThread callback so that the video cleanup
PR is smaller as this is an isolated change.

Instead of piping a callback from GPUConnectionToWebProcess, just send a reference
down to RemoteDevice.

* Source/WebKit/GPUProcess/GPUConnectionToWebProcess.cpp:
(WebKit::GPUConnectionToWebProcess::createRemoteGPU):
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteAdapter.cpp:
(WebKit::RemoteAdapter::RemoteAdapter):
(WebKit::RemoteAdapter::requestDevice):
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteAdapter.h:
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteDevice.cpp:
(WebKit::RemoteDevice::RemoteDevice):
(WebKit::RemoteDevice::importExternalTextureFromPixelBuffer):
(WebKit::populateConvertedDescriptor): Deleted.
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteDevice.h:
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteGPU.cpp:
(WebKit::RemoteGPU::RemoteGPU):
(WebKit::RemoteGPU::requestAdapter):
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteGPU.h:

Canonical link: https://commits.webkit.org/267874@main
  • Loading branch information
mwyrzykowski committed Sep 11, 2023
1 parent 5c073cc commit bb19a86
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 74 deletions.
17 changes: 1 addition & 16 deletions Source/WebKit/GPUProcess/GPUConnectionToWebProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,22 +681,7 @@ void GPUConnectionToWebProcess::createRemoteGPU(WebGPUIdentifier identifier, Ren
MESSAGE_CHECK(streamConnection);

auto addResult = m_remoteGPUMap.ensure(identifier, [&, streamConnection = WTFMove(streamConnection)] () mutable {
return IPC::ScopedActiveMessageReceiveQueue { RemoteGPU::create(
#if ENABLE(VIDEO) && PLATFORM(COCOA)
[&] (std::variant<WebCore::MediaPlayerIdentifier, WebKit::RemoteVideoFrameReference> identifier, Function<void(RefPtr<WebCore::VideoFrame>)>&& callback) mutable {
return WTF::switchOn(identifier, [&] (WebCore::MediaPlayerIdentifier i) {
performWithMediaPlayerOnMainThread(i, [&callback](auto& player) {
callback(player.videoFrameForCurrentTime());
});
}, [&] (WebKit::RemoteVideoFrameReference i) {
callback(videoFrameObjectHeap().get(RemoteVideoFrameReadReference(WTFMove(i))));
});
},
#else
[] () mutable {
},
#endif
identifier, *this, *renderingBackend, streamConnection.releaseNonNull()) };
return IPC::ScopedActiveMessageReceiveQueue { RemoteGPU::create(identifier, *this, *renderingBackend, streamConnection.releaseNonNull()) };
});
ASSERT_UNUSED(addResult, addResult.isNewEntry);
}
Expand Down
8 changes: 4 additions & 4 deletions Source/WebKit/GPUProcess/graphics/WebGPU/RemoteAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@

namespace WebKit {

RemoteAdapter::RemoteAdapter(PerformWithMediaPlayerOnMainThread& performWithMediaPlayerOnMainThread, WebCore::WebGPU::Adapter& adapter, WebGPU::ObjectHeap& objectHeap, Ref<IPC::StreamServerConnection>&& streamConnection, WebGPUIdentifier identifier)
RemoteAdapter::RemoteAdapter(GPUConnectionToWebProcess& gpuConnectionToWebProcess, WebCore::WebGPU::Adapter& adapter, WebGPU::ObjectHeap& objectHeap, Ref<IPC::StreamServerConnection>&& streamConnection, WebGPUIdentifier identifier)
: m_backing(adapter)
, m_objectHeap(objectHeap)
, m_streamConnection(WTFMove(streamConnection))
, m_performWithMediaPlayerOnMainThread(performWithMediaPlayerOnMainThread)
, m_gpuConnectionToWebProcess(gpuConnectionToWebProcess)
, m_identifier(identifier)
{
m_streamConnection->startReceivingMessages(*this, Messages::RemoteAdapter::messageReceiverName(), m_identifier.toUInt64());
Expand All @@ -72,14 +72,14 @@ void RemoteAdapter::requestDevice(const WebGPU::DeviceDescriptor& descriptor, We
return;
}

m_backing->requestDevice(*convertedDescriptor, [callback = WTFMove(callback), objectHeap = Ref { m_objectHeap }, streamConnection = m_streamConnection.copyRef(), identifier, queueIdentifier, &performWithMediaPlayerOnMainThread = m_performWithMediaPlayerOnMainThread] (RefPtr<WebCore::WebGPU::Device>&& devicePtr) mutable {
m_backing->requestDevice(*convertedDescriptor, [callback = WTFMove(callback), objectHeap = Ref { m_objectHeap }, streamConnection = m_streamConnection.copyRef(), identifier, queueIdentifier, &gpuConnectionToWebProcess = m_gpuConnectionToWebProcess] (RefPtr<WebCore::WebGPU::Device>&& devicePtr) mutable {
if (!devicePtr.get()) {
callback({ }, { });
return;
}

auto device = devicePtr.releaseNonNull();
auto remoteDevice = RemoteDevice::create(performWithMediaPlayerOnMainThread, device, objectHeap, WTFMove(streamConnection), identifier, queueIdentifier);
auto remoteDevice = RemoteDevice::create(gpuConnectionToWebProcess, device, objectHeap, WTFMove(streamConnection), identifier, queueIdentifier);
objectHeap->addObject(identifier, remoteDevice);
objectHeap->addObject(queueIdentifier, remoteDevice->queue());
const auto& features = device->features();
Expand Down
8 changes: 4 additions & 4 deletions Source/WebKit/GPUProcess/graphics/WebGPU/RemoteAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ struct SupportedLimits;
class RemoteAdapter final : public IPC::StreamMessageReceiver {
WTF_MAKE_FAST_ALLOCATED;
public:
static Ref<RemoteAdapter> create(PerformWithMediaPlayerOnMainThread& performWithMediaPlayerOnMainThread, WebCore::WebGPU::Adapter& adapter, WebGPU::ObjectHeap& objectHeap, Ref<IPC::StreamServerConnection>&& streamConnection, WebGPUIdentifier identifier)
static Ref<RemoteAdapter> create(GPUConnectionToWebProcess& gpuConnectionToWebProcess, WebCore::WebGPU::Adapter& adapter, WebGPU::ObjectHeap& objectHeap, Ref<IPC::StreamServerConnection>&& streamConnection, WebGPUIdentifier identifier)
{
return adoptRef(*new RemoteAdapter(performWithMediaPlayerOnMainThread, adapter, objectHeap, WTFMove(streamConnection), identifier));
return adoptRef(*new RemoteAdapter(gpuConnectionToWebProcess, adapter, objectHeap, WTFMove(streamConnection), identifier));
}

virtual ~RemoteAdapter();
Expand All @@ -65,7 +65,7 @@ class RemoteAdapter final : public IPC::StreamMessageReceiver {
private:
friend class WebGPU::ObjectHeap;

RemoteAdapter(PerformWithMediaPlayerOnMainThread&, WebCore::WebGPU::Adapter&, WebGPU::ObjectHeap&, Ref<IPC::StreamServerConnection>&&, WebGPUIdentifier);
RemoteAdapter(GPUConnectionToWebProcess&, WebCore::WebGPU::Adapter&, WebGPU::ObjectHeap&, Ref<IPC::StreamServerConnection>&&, WebGPUIdentifier);

RemoteAdapter(const RemoteAdapter&) = delete;
RemoteAdapter(RemoteAdapter&&) = delete;
Expand All @@ -82,7 +82,7 @@ class RemoteAdapter final : public IPC::StreamMessageReceiver {
Ref<WebCore::WebGPU::Adapter> m_backing;
WebGPU::ObjectHeap& m_objectHeap;
Ref<IPC::StreamServerConnection> m_streamConnection;
PerformWithMediaPlayerOnMainThread& m_performWithMediaPlayerOnMainThread;
GPUConnectionToWebProcess& m_gpuConnectionToWebProcess;
WebGPUIdentifier m_identifier;
};

Expand Down
44 changes: 20 additions & 24 deletions Source/WebKit/GPUProcess/graphics/WebGPU/RemoteDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,19 @@

namespace WebKit {

RemoteDevice::RemoteDevice(PerformWithMediaPlayerOnMainThread& performWithMediaPlayerOnMainThread, WebCore::WebGPU::Device& device, WebGPU::ObjectHeap& objectHeap, Ref<IPC::StreamServerConnection>&& streamConnection, WebGPUIdentifier identifier, WebGPUIdentifier queueIdentifier)
RemoteDevice::RemoteDevice(GPUConnectionToWebProcess& gpuConnectionToWebProcess, WebCore::WebGPU::Device& device, WebGPU::ObjectHeap& objectHeap, Ref<IPC::StreamServerConnection>&& streamConnection, WebGPUIdentifier identifier, WebGPUIdentifier queueIdentifier)
: m_backing(device)
, m_objectHeap(objectHeap)
, m_streamConnection(streamConnection.copyRef())
, m_identifier(identifier)
, m_queue(RemoteQueue::create(device.queue(), objectHeap, WTFMove(streamConnection), queueIdentifier))
, m_performWithMediaPlayerOnMainThread(performWithMediaPlayerOnMainThread)
#if ENABLE(VIDEO)
, m_videoFrameObjectHeap(gpuConnectionToWebProcess.videoFrameObjectHeap())
#if PLATFORM(COCOA)
, m_sharedVideoFrameReader(m_videoFrameObjectHeap.ptr(), gpuConnectionToWebProcess.webProcessIdentity())
#endif
#endif
, m_gpuConnectionToWebProcess(gpuConnectionToWebProcess)
{
m_streamConnection->startReceivingMessages(*this, Messages::RemoteDevice::messageReceiverName(), m_identifier.toUInt64());
}
Expand Down Expand Up @@ -153,26 +159,6 @@ void RemoteDevice::createSampler(const WebGPU::SamplerDescriptor& descriptor, We
m_objectHeap.addObject(identifier, remoteSampler);
}

static void populateConvertedDescriptor(auto mediaIdentifier, auto& convertedDescriptor, auto& performWithMediaPlayerOnMainThread)
{
#if ENABLE(VIDEO) && PLATFORM(COCOA)
using MediaPlayerOrVideoFrameResult = std::variant<WebCore::MediaPlayerIdentifier, WebKit::RemoteVideoFrameReference>;
MediaPlayerOrVideoFrameResult result = WTF::switchOn(mediaIdentifier, [] (WebCore::WebGPU::HTMLVideoElementIdentifier i) -> MediaPlayerOrVideoFrameResult {
return WebCore::MediaPlayerIdentifier(i.identifier);
}, [] (WebCore::WebGPU::WebCodecsVideoFrameIdentifier i) -> MediaPlayerOrVideoFrameResult {
return RemoteVideoFrameReference(RemoteVideoFrameIdentifier(i.identifier.first), i.identifier.second);
});

performWithMediaPlayerOnMainThread(result, [&convertedDescriptor](RefPtr<WebCore::VideoFrame> videoFrame) mutable {
convertedDescriptor->pixelBuffer = videoFrame ? videoFrame->pixelBuffer() : nullptr;
});
#else
UNUSED_PARAM(mediaIdentifier);
UNUSED_PARAM(convertedDescriptor);
UNUSED_PARAM(performWithMediaPlayerOnMainThread);
#endif
}

#if ENABLE(VIDEO) && PLATFORM(COCOA)
void RemoteDevice::setSharedVideoFrameSemaphore(IPC::Semaphore&& semaphore)
{
Expand Down Expand Up @@ -202,9 +188,19 @@ void RemoteDevice::importExternalTextureFromPixelBuffer(const WebGPU::ExternalTe
#if PLATFORM(COCOA) && ENABLE(VIDEO)
if (sharedBuffer)
convertedDescriptor->pixelBuffer = m_sharedVideoFrameReader.readBuffer(WTFMove(*sharedBuffer));
else
else {
WTF::switchOn(descriptor.mediaIdentifier, [&] (WebCore::WebGPU::HTMLVideoElementIdentifier i) {
m_gpuConnectionToWebProcess.performWithMediaPlayerOnMainThread(WebCore::MediaPlayerIdentifier(i.identifier), [&convertedDescriptor] (auto& player) mutable {
auto videoFrame = player.videoFrameForCurrentTime();
convertedDescriptor->pixelBuffer = videoFrame ? videoFrame->pixelBuffer() : nullptr;
});
}, [&] (WebCore::WebGPU::WebCodecsVideoFrameIdentifier i) {
auto readReference = RemoteVideoFrameReadReference(RemoteVideoFrameReference(RemoteVideoFrameIdentifier(i.identifier.first), i.identifier.second));
auto videoFrame = m_videoFrameObjectHeap.get().get(WTFMove(readReference));
convertedDescriptor->pixelBuffer = videoFrame ? videoFrame->pixelBuffer() : nullptr;
});
}
#endif
populateConvertedDescriptor(descriptor.mediaIdentifier, convertedDescriptor, m_performWithMediaPlayerOnMainThread);

if (!convertedDescriptor->pixelBuffer)
return;
Expand Down
25 changes: 14 additions & 11 deletions Source/WebKit/GPUProcess/graphics/WebGPU/RemoteDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#if ENABLE(GPU_PROCESS)

#include "GPUConnectionToWebProcess.h"
#include "RemoteQueue.h"
#include "RemoteVideoFrameIdentifier.h"
#include "SharedVideoFrame.h"
Expand All @@ -39,6 +40,10 @@
#include <wtf/Ref.h>
#include <wtf/text/WTFString.h>

#if ENABLE(VIDEO)
#include "RemoteVideoFrameObjectHeap.h"
#endif

typedef struct __CVBuffer* CVPixelBufferRef;

namespace WebCore::WebGPU {
Expand All @@ -60,6 +65,7 @@ namespace WebKit {

class RemoteGPU;
class SharedMemoryHandle;
struct SharedVideoFrame;

namespace WebGPU {
struct BindGroupDescriptor;
Expand All @@ -78,18 +84,12 @@ struct ShaderModuleDescriptor;
struct TextureDescriptor;
}

#if ENABLE(VIDEO) && PLATFORM(COCOA)
using PerformWithMediaPlayerOnMainThread = Function<void(std::variant<WebCore::MediaPlayerIdentifier, WebKit::RemoteVideoFrameReference>, Function<void(RefPtr<WebCore::VideoFrame>)>&&)>;
#else
using PerformWithMediaPlayerOnMainThread = Function<void()>;
#endif

class RemoteDevice final : public IPC::StreamMessageReceiver {
WTF_MAKE_FAST_ALLOCATED;
public:
static Ref<RemoteDevice> create(PerformWithMediaPlayerOnMainThread& performWithMediaPlayerOnMainThread, WebCore::WebGPU::Device& device, WebGPU::ObjectHeap& objectHeap, Ref<IPC::StreamServerConnection>&& streamConnection, WebGPUIdentifier identifier, WebGPUIdentifier queueIdentifier)
static Ref<RemoteDevice> create(GPUConnectionToWebProcess& gpuConnectionToWebProcess, WebCore::WebGPU::Device& device, WebGPU::ObjectHeap& objectHeap, Ref<IPC::StreamServerConnection>&& streamConnection, WebGPUIdentifier identifier, WebGPUIdentifier queueIdentifier)
{
return adoptRef(*new RemoteDevice(performWithMediaPlayerOnMainThread, device, objectHeap, WTFMove(streamConnection), identifier, queueIdentifier));
return adoptRef(*new RemoteDevice(gpuConnectionToWebProcess, device, objectHeap, WTFMove(streamConnection), identifier, queueIdentifier));
}

~RemoteDevice();
Expand All @@ -101,7 +101,7 @@ class RemoteDevice final : public IPC::StreamMessageReceiver {
private:
friend class WebGPU::ObjectHeap;

RemoteDevice(PerformWithMediaPlayerOnMainThread&, WebCore::WebGPU::Device&, WebGPU::ObjectHeap&, Ref<IPC::StreamServerConnection>&&, WebGPUIdentifier, WebGPUIdentifier queueIdentifier);
RemoteDevice(GPUConnectionToWebProcess&, WebCore::WebGPU::Device&, WebGPU::ObjectHeap&, Ref<IPC::StreamServerConnection>&&, WebGPUIdentifier, WebGPUIdentifier queueIdentifier);

RemoteDevice(const RemoteDevice&) = delete;
RemoteDevice(RemoteDevice&&) = delete;
Expand Down Expand Up @@ -150,10 +150,13 @@ class RemoteDevice final : public IPC::StreamMessageReceiver {
Ref<IPC::StreamServerConnection> m_streamConnection;
WebGPUIdentifier m_identifier;
Ref<RemoteQueue> m_queue;
PerformWithMediaPlayerOnMainThread& m_performWithMediaPlayerOnMainThread;
#if PLATFORM(COCOA) && ENABLE(VIDEO)
#if ENABLE(VIDEO)
Ref<RemoteVideoFrameObjectHeap> m_videoFrameObjectHeap;
#if PLATFORM(COCOA)
SharedVideoFrameReader m_sharedVideoFrameReader;
#endif
#endif
GPUConnectionToWebProcess& m_gpuConnectionToWebProcess;
};

} // namespace WebKit
Expand Down
7 changes: 3 additions & 4 deletions Source/WebKit/GPUProcess/graphics/WebGPU/RemoteGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@

namespace WebKit {

RemoteGPU::RemoteGPU(PerformWithMediaPlayerOnMainThread&& performWithMediaPlayerOnMainThread, WebGPUIdentifier identifier, GPUConnectionToWebProcess& gpuConnectionToWebProcess, RemoteRenderingBackend& renderingBackend, Ref<IPC::StreamServerConnection>&& streamConnection)
RemoteGPU::RemoteGPU(WebGPUIdentifier identifier, GPUConnectionToWebProcess& gpuConnectionToWebProcess, RemoteRenderingBackend& renderingBackend, Ref<IPC::StreamServerConnection>&& streamConnection)
: m_gpuConnectionToWebProcess(gpuConnectionToWebProcess)
, m_workQueue(IPC::StreamConnectionWorkQueue::create("WebGPU work queue"))
, m_streamConnection(WTFMove(streamConnection))
, m_objectHeap(WebGPU::ObjectHeap::create())
, m_performWithMediaPlayerOnMainThread(WTFMove(performWithMediaPlayerOnMainThread))
, m_identifier(identifier)
, m_renderingBackend(renderingBackend)
{
Expand Down Expand Up @@ -125,13 +124,13 @@ void RemoteGPU::requestAdapter(const WebGPU::RequestAdapterOptions& options, Web
return;
}

m_backing->requestAdapter(*convertedOptions, [callback = WTFMove(callback), objectHeap = m_objectHeap.copyRef(), streamConnection = Ref { *m_streamConnection }, identifier, &performWithMediaPlayerOnMainThread = m_performWithMediaPlayerOnMainThread] (RefPtr<WebCore::WebGPU::Adapter>&& adapter) mutable {
m_backing->requestAdapter(*convertedOptions, [callback = WTFMove(callback), objectHeap = m_objectHeap.copyRef(), streamConnection = Ref { *m_streamConnection }, identifier, &gpuConnectionToWebProcess = m_gpuConnectionToWebProcess] (RefPtr<WebCore::WebGPU::Adapter>&& adapter) mutable {
if (!adapter) {
callback(std::nullopt);
return;
}

auto remoteAdapter = RemoteAdapter::create(performWithMediaPlayerOnMainThread, *adapter, objectHeap, WTFMove(streamConnection), identifier);
auto remoteAdapter = RemoteAdapter::create(gpuConnectionToWebProcess, *adapter, objectHeap, WTFMove(streamConnection), identifier);
objectHeap->addObject(identifier, remoteAdapter);

auto name = adapter->name();
Expand Down
15 changes: 4 additions & 11 deletions Source/WebKit/GPUProcess/graphics/WebGPU/RemoteGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,12 @@ class ObjectHeap;
struct RequestAdapterOptions;
}

#if ENABLE(VIDEO) && PLATFORM(COCOA)
using PerformWithMediaPlayerOnMainThread = Function<void(std::variant<WebCore::MediaPlayerIdentifier, WebKit::RemoteVideoFrameReference>, Function<void(RefPtr<WebCore::VideoFrame>)>&&)>;
#else
using PerformWithMediaPlayerOnMainThread = Function<void()>;
#endif

class RemoteGPU final : public IPC::StreamMessageReceiver {
WTF_MAKE_FAST_ALLOCATED;
public:
static Ref<RemoteGPU> create(PerformWithMediaPlayerOnMainThread&& performWithMediaPlayerOnMainThread, WebGPUIdentifier identifier, GPUConnectionToWebProcess& gpuConnectionToWebProcess, RemoteRenderingBackend& renderingBackend, Ref<IPC::StreamServerConnection>&& serverConnection)
static Ref<RemoteGPU> create(WebGPUIdentifier identifier, GPUConnectionToWebProcess& gpuConnectionToWebProcess, RemoteRenderingBackend& renderingBackend, Ref<IPC::StreamServerConnection>&& serverConnection)
{
auto result = adoptRef(*new RemoteGPU(WTFMove(performWithMediaPlayerOnMainThread), identifier, gpuConnectionToWebProcess, renderingBackend, WTFMove(serverConnection)));
auto result = adoptRef(*new RemoteGPU(identifier, gpuConnectionToWebProcess, renderingBackend, WTFMove(serverConnection)));
result->initialize();
return result;
}
Expand All @@ -89,7 +83,7 @@ class RemoteGPU final : public IPC::StreamMessageReceiver {
private:
friend class WebGPU::ObjectHeap;

RemoteGPU(PerformWithMediaPlayerOnMainThread&&, WebGPUIdentifier, GPUConnectionToWebProcess&, RemoteRenderingBackend&, Ref<IPC::StreamServerConnection>&&);
RemoteGPU(WebGPUIdentifier, GPUConnectionToWebProcess&, RemoteRenderingBackend&, Ref<IPC::StreamServerConnection>&&);

RemoteGPU(const RemoteGPU&) = delete;
RemoteGPU(RemoteGPU&&) = delete;
Expand All @@ -115,12 +109,11 @@ class RemoteGPU final : public IPC::StreamMessageReceiver {

void createCompositorIntegration(WebGPUIdentifier);

WeakPtr<GPUConnectionToWebProcess> m_gpuConnectionToWebProcess;
GPUConnectionToWebProcess& m_gpuConnectionToWebProcess;
Ref<IPC::StreamConnectionWorkQueue> m_workQueue;
RefPtr<IPC::StreamServerConnection> m_streamConnection;
RefPtr<WebCore::WebGPU::GPU> m_backing WTF_GUARDED_BY_CAPABILITY(workQueue());
Ref<WebGPU::ObjectHeap> m_objectHeap WTF_GUARDED_BY_CAPABILITY(workQueue());
PerformWithMediaPlayerOnMainThread m_performWithMediaPlayerOnMainThread;
const WebGPUIdentifier m_identifier;
Ref<RemoteRenderingBackend> m_renderingBackend;
};
Expand Down

0 comments on commit bb19a86

Please sign in to comment.