Title: [275403] trunk/Source
Revision
275403
Author
[email protected]
Date
2021-04-01 21:42:15 -0700 (Thu, 01 Apr 2021)

Log Message

RemoteRenderingBackend should clean up CG resources after last Canvas is destroyed in Gpu process
https://bugs.webkit.org/show_bug.cgi?id=223885
<rdar://problem/75637356>

Patch by Kimmo Kinnunen <[email protected]> on 2021-04-01
Reviewed by Simon Fraser.

Source/WebCore/PAL:

Add SPI interface _MTLDevice for its _purgeDevice method.

* pal/spi/cocoa/MetalSPI.h:

Source/WebKit:

Release CoreGraphics resources (Metal device command buffers,
and other per-device resources) 1s after last
 - RemoteRenderingBackend drawable resource
 - RemoteGraphicsContextGL instance
 - RemoteMediaPlayerProxy instance
has been destroyed.

Adds ScopedRenderingResourcesRequest and
ScopedWebGLRenderingResourcesRequest to hold the intent to
use the global rendering resources. The names are called 'Request'
because the implementation is not guaranteed to do anything specific.
E.g. ResourcesRequest == "I'd like the resources to be available"
     ResourcesLock, ResourcesHolder, ...  = "The resources are guaranteed
     to be held."
The naming tries to be consistent with the similar class
ScopedHighPerformanceGPURequest.

For sequence like:
1) Visit a page with simple Context2D usage
2) Close the page (but leave the browser running)
This saves 2mb of IOAccelMemory at step 2 on a tested device.

* Configurations/WebKit.xcconfig:
Link WebKit to Metal in order to use the RemoteRenderingRequest
(_MTLCopyAllDevices symbol).

* GPUProcess/GPUConnectionToWebProcess.cpp:
(WebKit::GPUConnectionToWebProcess::didClose):
Clear the map containing RemoteGraphicsContextGL objects
when connection to WebContent process is closed.
This stops the RemoteGraphicsContextGL listening for IPC
and destroys them in scenarios where the connection is
closed but the GPUConnectionToWebProcess is staying alive
for some reason. In this patch, this would happen for example
during reload of the page.

* GPUProcess/graphics/RemoteGraphicsContextGL.cpp:
(WebKit::remoteGraphicsContextGLStreamWorkQueue):
(WebKit::RemoteGraphicsContextGL::RemoteGraphicsContextGL):
(WebKit::RemoteGraphicsContextGL::initialize):
(WebKit::RemoteGraphicsContextGL::stopListeningForIPC):
* GPUProcess/graphics/RemoteGraphicsContextGL.h:
Hold the ScopedWebGLRenderingResourcesRequest for
the duration of the RemoteGraphicsContextGL context lifetime.

* GPUProcess/graphics/RemoteRenderingBackend.cpp:
(WebKit::RemoteRenderingBackend::~RemoteRenderingBackend):
(WebKit::RemoteRenderingBackend::createImageBuffer):
(WebKit::RemoteRenderingBackend::releaseRemoteResource):
(WebKit::RemoteRenderingBackend::updateRenderingResourceRequest):
* GPUProcess/graphics/RemoteRenderingBackend.h:
Hold the ScopedRenderingResourcesRequest for the duration that
there are ImageBuffers or NativeImages in the RemoteResourceCache.

A RemoteRenderingBackend might exist without any drawable due to
a) Context2D being created and then destroyed
b) WebGL taking a reference to the RemoteRenderingBackend in order
to submit work in case the caller would request Image -> texture
upload.

In case of WebGL context being created and then destroyed, there is
a RRB instance alive but not doing any rendering. In these cases,
we want the rendering resources to be released and not held by
the empty RRB.

* GPUProcess/graphics/ScopedRenderingResourcesRequest.cpp: Copied from Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h.
(WebKit::ScopedRenderingResourcesRequest::scheduleFreeRenderingResources):
(WebKit::ScopedRenderingResourcesRequest::freeRenderingResources):
* GPUProcess/graphics/ScopedRenderingResourcesRequest.h: Added.
(WebKit::ScopedRenderingResourcesRequest::ScopedRenderingResourcesRequest):
(WebKit::ScopedRenderingResourcesRequest::~ScopedRenderingResourcesRequest):
(WebKit::ScopedRenderingResourcesRequest::operator=):
(WebKit::ScopedRenderingResourcesRequest::isRequested const):
(WebKit::ScopedRenderingResourcesRequest::acquire):
(WebKit::ScopedRenderingResourcesRequest::reset):
* GPUProcess/graphics/ScopedRenderingResourcesRequestCocoa.mm: Copied from Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h.
(WebKit::ScopedRenderingResourcesRequest::scheduleFreeRenderingResources):
(WebKit::ScopedRenderingResourcesRequest::freeRenderingResources):
Add a scoped holder for the rendering resources. Currently it purges
all Metal device memory.

* GPUProcess/graphics/ScopedWebGLRenderingResourcesRequest.cpp: Copied from Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h.
(WebKit::ScopedWebGLRenderingResourcesRequest::scheduleFreeWebGLRenderingResources):
(WebKit::ScopedWebGLRenderingResourcesRequest::freeWebGLRenderingResources):
* GPUProcess/graphics/ScopedWebGLRenderingResourcesRequest.h: Added.
(WebKit::ScopedWebGLRenderingResourcesRequest::ScopedWebGLRenderingResourcesRequest):
(WebKit::ScopedWebGLRenderingResourcesRequest::~ScopedWebGLRenderingResourcesRequest):
(WebKit::ScopedWebGLRenderingResourcesRequest::operator=):
(WebKit::ScopedWebGLRenderingResourcesRequest::isRequested const):
(WebKit::ScopedWebGLRenderingResourcesRequest::acquire):
(WebKit::ScopedWebGLRenderingResourcesRequest::reset):
* GPUProcess/graphics/ScopedWebGLRenderingResourcesRequestCocoa.cpp: Copied from Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h.
(WebKit::ScopedWebGLRenderingResourcesRequest::scheduleFreeWebGLRenderingResources):
(WebKit::ScopedWebGLRenderingResourcesRequest::freeWebGLRenderingResources):
Add a scoped holder for the WebGL global resources. Currently it frees
the ANGLE related global data. It also purges all Metal device memory
via RenderingResourcesRequest.

* GPUProcess/media/RemoteMediaPlayerProxy.cpp:
(WebKit::RemoteMediaPlayerProxy::RemoteMediaPlayerProxy):
(WebKit::RemoteMediaPlayerProxy::invalidate):
* GPUProcess/media/RemoteMediaPlayerProxy.h:
Hold RenderingResourcesRequest for the duration of a media player
existing in the GPU process. It's not known if media playback needs
Metal. At least some videos do not need it. However, this is erring
on the side of caution in order to avoid potential video frame skips
the video playback using Metal but then the devices being purged by
a Context2D or WebGL -related destruction.

* Sources.txt:
* SourcesCocoa.txt:
* WebKit.xcodeproj/project.pbxproj:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/PAL/ChangeLog (275402 => 275403)


--- trunk/Source/WebCore/PAL/ChangeLog	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebCore/PAL/ChangeLog	2021-04-02 04:42:15 UTC (rev 275403)
@@ -1,3 +1,15 @@
+2021-04-01  Kimmo Kinnunen  <[email protected]>
+
+        RemoteRenderingBackend should clean up CG resources after last Canvas is destroyed in Gpu process
+        https://bugs.webkit.org/show_bug.cgi?id=223885
+        <rdar://problem/75637356>
+
+        Reviewed by Simon Fraser.
+
+        Add SPI interface _MTLDevice for its _purgeDevice method.
+
+        * pal/spi/cocoa/MetalSPI.h:
+
 2021-04-01  Jean-Yves Avenard  <[email protected]>
 
         Previous artwork isn't cleared when NowPlaying info doesn't contain an artwork

Modified: trunk/Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h (275402 => 275403)


--- trunk/Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h	2021-04-02 04:42:15 UTC (rev 275403)
@@ -29,6 +29,8 @@
 
 #else
 
+#import <Foundation/NSObject.h>
+
 @protocol MTLDeviceSPI <MTLDevice>
 - (NSString*)vendorName;
 - (NSString*)familyName;
@@ -35,4 +37,8 @@
 - (NSString*)productName;
 @end
 
+@interface _MTLDevice : NSObject
+- (void)_purgeDevice;
+@end
+
 #endif

Modified: trunk/Source/WebKit/ChangeLog (275402 => 275403)


--- trunk/Source/WebKit/ChangeLog	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/ChangeLog	2021-04-02 04:42:15 UTC (rev 275403)
@@ -1,3 +1,125 @@
+2021-04-01  Kimmo Kinnunen  <[email protected]>
+
+        RemoteRenderingBackend should clean up CG resources after last Canvas is destroyed in Gpu process
+        https://bugs.webkit.org/show_bug.cgi?id=223885
+        <rdar://problem/75637356>
+
+        Reviewed by Simon Fraser.
+
+        Release CoreGraphics resources (Metal device command buffers,
+        and other per-device resources) 1s after last
+         - RemoteRenderingBackend drawable resource
+         - RemoteGraphicsContextGL instance
+         - RemoteMediaPlayerProxy instance
+        has been destroyed.
+
+        Adds ScopedRenderingResourcesRequest and
+        ScopedWebGLRenderingResourcesRequest to hold the intent to
+        use the global rendering resources. The names are called 'Request'
+        because the implementation is not guaranteed to do anything specific.
+        E.g. ResourcesRequest == "I'd like the resources to be available"
+             ResourcesLock, ResourcesHolder, ...  = "The resources are guaranteed
+             to be held."
+        The naming tries to be consistent with the similar class
+        ScopedHighPerformanceGPURequest.
+
+        For sequence like:
+        1) Visit a page with simple Context2D usage
+        2) Close the page (but leave the browser running)
+        This saves 2mb of IOAccelMemory at step 2 on a tested device.
+
+
+        * Configurations/WebKit.xcconfig:
+        Link WebKit to Metal in order to use the RemoteRenderingRequest
+        (_MTLCopyAllDevices symbol).
+
+        * GPUProcess/GPUConnectionToWebProcess.cpp:
+        (WebKit::GPUConnectionToWebProcess::didClose):
+        Clear the map containing RemoteGraphicsContextGL objects
+        when connection to WebContent process is closed.
+        This stops the RemoteGraphicsContextGL listening for IPC
+        and destroys them in scenarios where the connection is
+        closed but the GPUConnectionToWebProcess is staying alive
+        for some reason. In this patch, this would happen for example
+        during reload of the page.
+
+        * GPUProcess/graphics/RemoteGraphicsContextGL.cpp:
+        (WebKit::remoteGraphicsContextGLStreamWorkQueue):
+        (WebKit::RemoteGraphicsContextGL::RemoteGraphicsContextGL):
+        (WebKit::RemoteGraphicsContextGL::initialize):
+        (WebKit::RemoteGraphicsContextGL::stopListeningForIPC):
+        * GPUProcess/graphics/RemoteGraphicsContextGL.h:
+        Hold the ScopedWebGLRenderingResourcesRequest for
+        the duration of the RemoteGraphicsContextGL context lifetime.
+
+        * GPUProcess/graphics/RemoteRenderingBackend.cpp:
+        (WebKit::RemoteRenderingBackend::~RemoteRenderingBackend):
+        (WebKit::RemoteRenderingBackend::createImageBuffer):
+        (WebKit::RemoteRenderingBackend::releaseRemoteResource):
+        (WebKit::RemoteRenderingBackend::updateRenderingResourceRequest):
+        * GPUProcess/graphics/RemoteRenderingBackend.h:
+        Hold the ScopedRenderingResourcesRequest for the duration that
+        there are ImageBuffers or NativeImages in the RemoteResourceCache.
+
+        A RemoteRenderingBackend might exist without any drawable due to
+        a) Context2D being created and then destroyed
+        b) WebGL taking a reference to the RemoteRenderingBackend in order
+        to submit work in case the caller would request Image -> texture
+        upload.
+
+        In case of WebGL context being created and then destroyed, there is
+        a RRB instance alive but not doing any rendering. In these cases,
+        we want the rendering resources to be released and not held by
+        the empty RRB.
+
+        * GPUProcess/graphics/ScopedRenderingResourcesRequest.cpp: Copied from Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h.
+        (WebKit::ScopedRenderingResourcesRequest::scheduleFreeRenderingResources):
+        (WebKit::ScopedRenderingResourcesRequest::freeRenderingResources):
+        * GPUProcess/graphics/ScopedRenderingResourcesRequest.h: Added.
+        (WebKit::ScopedRenderingResourcesRequest::ScopedRenderingResourcesRequest):
+        (WebKit::ScopedRenderingResourcesRequest::~ScopedRenderingResourcesRequest):
+        (WebKit::ScopedRenderingResourcesRequest::operator=):
+        (WebKit::ScopedRenderingResourcesRequest::isRequested const):
+        (WebKit::ScopedRenderingResourcesRequest::acquire):
+        (WebKit::ScopedRenderingResourcesRequest::reset):
+        * GPUProcess/graphics/ScopedRenderingResourcesRequestCocoa.mm: Copied from Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h.
+        (WebKit::ScopedRenderingResourcesRequest::scheduleFreeRenderingResources):
+        (WebKit::ScopedRenderingResourcesRequest::freeRenderingResources):
+        Add a scoped holder for the rendering resources. Currently it purges
+        all Metal device memory.
+
+        * GPUProcess/graphics/ScopedWebGLRenderingResourcesRequest.cpp: Copied from Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h.
+        (WebKit::ScopedWebGLRenderingResourcesRequest::scheduleFreeWebGLRenderingResources):
+        (WebKit::ScopedWebGLRenderingResourcesRequest::freeWebGLRenderingResources):
+        * GPUProcess/graphics/ScopedWebGLRenderingResourcesRequest.h: Added.
+        (WebKit::ScopedWebGLRenderingResourcesRequest::ScopedWebGLRenderingResourcesRequest):
+        (WebKit::ScopedWebGLRenderingResourcesRequest::~ScopedWebGLRenderingResourcesRequest):
+        (WebKit::ScopedWebGLRenderingResourcesRequest::operator=):
+        (WebKit::ScopedWebGLRenderingResourcesRequest::isRequested const):
+        (WebKit::ScopedWebGLRenderingResourcesRequest::acquire):
+        (WebKit::ScopedWebGLRenderingResourcesRequest::reset):
+        * GPUProcess/graphics/ScopedWebGLRenderingResourcesRequestCocoa.cpp: Copied from Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h.
+        (WebKit::ScopedWebGLRenderingResourcesRequest::scheduleFreeWebGLRenderingResources):
+        (WebKit::ScopedWebGLRenderingResourcesRequest::freeWebGLRenderingResources):
+        Add a scoped holder for the WebGL global resources. Currently it frees
+        the ANGLE related global data. It also purges all Metal device memory
+        via RenderingResourcesRequest.
+
+        * GPUProcess/media/RemoteMediaPlayerProxy.cpp:
+        (WebKit::RemoteMediaPlayerProxy::RemoteMediaPlayerProxy):
+        (WebKit::RemoteMediaPlayerProxy::invalidate):
+        * GPUProcess/media/RemoteMediaPlayerProxy.h:
+        Hold RenderingResourcesRequest for the duration of a media player
+        existing in the GPU process. It's not known if media playback needs
+        Metal. At least some videos do not need it. However, this is erring
+        on the side of caution in order to avoid potential video frame skips
+        the video playback using Metal but then the devices being purged by
+        a Context2D or WebGL -related destruction.
+
+        * Sources.txt:
+        * SourcesCocoa.txt:
+        * WebKit.xcodeproj/project.pbxproj:
+
 2021-04-01  Kate Cheney  <[email protected]>
 
         Clear network website data when a user clears history/website data

Modified: trunk/Source/WebKit/Configurations/WebKit.xcconfig (275402 => 275403)


--- trunk/Source/WebKit/Configurations/WebKit.xcconfig	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/Configurations/WebKit.xcconfig	2021-04-02 04:42:15 UTC (rev 275403)
@@ -143,7 +143,7 @@
 WK_LIBNETWORKEXTENSION_LDFLAGS_macosx = -weak_library /usr/lib/libnetworkextension.dylib;
 WK_LIBNETWORKEXTENSION_LDFLAGS_tvos = ;
 
-FRAMEWORK_AND_LIBRARY_LDFLAGS = -lobjc -framework CFNetwork -framework CoreAudio -framework CoreFoundation -framework CoreGraphics -framework CoreText -framework Foundation -framework ImageIO -framework IOKit -framework IOSurface -framework WebKitLegacy -lnetwork $(WK_ACCESSIBILITY_LDFLAGS) $(WK_APPKIT_LDFLAGS) $(WK_BACKBOARD_SERVICES_LDFLAGS) $(WK_RUNNINGBOARD_SERVICES_LDFLAGS) $(WK_AUTHKIT_LDFLAGS) $(WK_CARBON_LDFLAGS) $(WK_CONTACTS_LDFLAGS) $(WK_CORE_PREDICTION_LDFLAGS) $(WK_CORE_SERVICES_LDFLAGS) $(WK_GRAPHICS_SERVICES_LDFLAGS) $(WK_LIBNETWORKEXTENSION_LDFLAGS) $(WK_LIBSANDBOX_LDFLAGS) $(WK_LIBWEBRTC_LDFLAGS) $(WK_MOBILE_CORE_SERVICES_LDFLAGS) $(WK_MOBILE_GESTALT_LDFLAGS) $(WK_PDFKIT_LDFLAGS) $(WK_SAFE_BROWSING_LDFLAGS) $(WK_SECURITY_INTERFACE_LDFLAGS) $(WK_UIKIT_LDFLAGS) $(WK_UNIFORM_TYPE_IDENTIFIERS_LDFLAGS) $(WK_URL_FORMATTING_LDFLAGS) $(WK_WEBINSPECTORUI_LDFLAGS);
+FRAMEWORK_AND_LIBRARY_LDFLAGS = -lobjc -framework CFNetwork -framework CoreAudio -framework CoreFoundation -framework CoreGraphics -framework CoreText -framework Foundation -framework ImageIO -framework IOKit -framework IOSurface -framework WebKitLegacy -lnetwork -framework Metal $(WK_ACCESSIBILITY_LDFLAGS) $(WK_APPKIT_LDFLAGS) $(WK_BACKBOARD_SERVICES_LDFLAGS) $(WK_RUNNINGBOARD_SERVICES_LDFLAGS) $(WK_AUTHKIT_LDFLAGS) $(WK_CARBON_LDFLAGS) $(WK_CONTACTS_LDFLAGS) $(WK_CORE_PREDICTION_LDFLAGS) $(WK_CORE_SERVICES_LDFLAGS) $(WK_GRAPHICS_SERVICES_LDFLAGS) $(WK_LIBNETWORKEXTENSION_LDFLAGS) $(WK_LIBSANDBOX_LDFLAGS) $(WK_LIBWEBRTC_LDFLAGS) $(WK_MOBILE_CORE_SERVICES_LDFLAGS) $(WK_MOBILE_GESTALT_LDFLAGS) $(WK_PDFKIT_LDFLAGS) $(WK_SAFE_BROWSING_LDFLAGS) $(WK_SECURITY_INTERFACE_LDFLAGS) $(WK_UIKIT_LDFLAGS) $(WK_UNIFORM_TYPE_IDENTIFIERS_LDFLAGS) $(WK_URL_FORMATTING_LDFLAGS) $(WK_WEBINSPECTORUI_LDFLAGS);
 
 // Prevent C++ standard library basic_stringstream, operator new, delete and their related exception types from being exported as weak symbols.
 UNEXPORTED_SYMBOL_LDFLAGS = -Wl,-unexported_symbol -Wl,__ZTISt9bad_alloc -Wl,-unexported_symbol -Wl,__ZTISt9exception -Wl,-unexported_symbol -Wl,__ZTSSt9bad_alloc -Wl,-unexported_symbol -Wl,__ZTSSt9exception -Wl,-unexported_symbol -Wl,__ZdlPvS_ -Wl,-unexported_symbol -Wl,__ZnwmPv -Wl,-unexported_symbol -Wl,__Znwm -Wl,-unexported_symbol -Wl,__ZNSt3__18functionIFvN7WebCore12PolicyActionEEEC2EOS4_ -Wl,-unexported_symbol -Wl,__ZNSt3__18functionIFvN7WebCore12PolicyActionEEEC1EOS4_ -Wl,-unexported_symbol -Wl,__ZNSt3__18functionIFvN7WebCore12PolicyActionEEEaSEDn -Wl,-unexported_symbol -Wl,__ZNKSt3__18functionIFvN7WebCore12PolicyActionEEEclES2_ -Wl,-unexported_symbol -Wl,__ZNSt3__18functionIFvN7WebCore12PolicyActionEEE4swapERS4_ -Wl,-unexported_symbol -Wl,__ZNSt3__18functionIFvN7WebCore12PolicyActionEEEC1ERKS4_ -Wl,-unexported_symbol -Wl,__ZNSt3__18functionIFvN7WebCore12PolicyActionEEEC2ERKS4_ -Wl,-unexported_symbol -Wl,__ZNSt3__18functionIFvN7WebCore12PolicyActionEE
 ED1Ev -Wl,-unexported_symbol -Wl,__ZNSt3__18functionIFvN7WebCore12PolicyActionEEED2Ev -Wl,-unexported_symbol -Wl,__ZNSt3__18functionIFvN7WebCore12PolicyActionEEEaSERKS4_ -Wl,-unexported_symbol -Wl,__ZTVNSt3__117bad_function_callE -Wl,-unexported_symbol -Wl,__ZTCNSt3__118basic_stringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE0_NS_13basic_istreamIcS2_EE -Wl,-unexported_symbol -Wl,__ZTCNSt3__118basic_stringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE0_NS_14basic_iostreamIcS2_EE -Wl,-unexported_symbol -Wl,__ZTCNSt3__118basic_stringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE16_NS_13basic_ostreamIcS2_EE -Wl,-unexported_symbol -Wl,__ZTTNSt3__118basic_stringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE -Wl,-unexported_symbol -Wl,__ZTVNSt3__115basic_stringbufIcNS_11char_traitsIcEENS_9allocatorIcEEEE -Wl,-unexported_symbol -Wl,__ZTVNSt3__118basic_stringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE -Wl,-unexported_symbol -Wl,__ZTCNSt3__118basic_stringstreamIcNS_11char_traitsIcEE
 NS_9allocatorIcEEEE8_NS_13basic_ostreamIcS2_EE;

Modified: trunk/Source/WebKit/GPUProcess/GPUConnectionToWebProcess.cpp (275402 => 275403)


--- trunk/Source/WebKit/GPUProcess/GPUConnectionToWebProcess.cpp	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/GPUProcess/GPUConnectionToWebProcess.cpp	2021-04-02 04:42:15 UTC (rev 275403)
@@ -237,6 +237,9 @@
     // to break the reference cycle by destroying them.
     m_remoteRenderingBackendMap.clear();
 
+    // RemoteGraphicsContextsGL objects are unneeded after connection closes.
+    m_remoteGraphicsContextGLMap.clear();
+
     gpuProcess().connectionToWebProcessClosed(connection);
     gpuProcess().removeGPUConnectionToWebProcess(*this); // May destroy |this|.
 }

Modified: trunk/Source/WebKit/GPUProcess/graphics/RemoteGraphicsContextGL.cpp (275402 => 275403)


--- trunk/Source/WebKit/GPUProcess/graphics/RemoteGraphicsContextGL.cpp	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/GPUProcess/graphics/RemoteGraphicsContextGL.cpp	2021-04-02 04:42:15 UTC (rev 275403)
@@ -46,21 +46,12 @@
 using namespace WebCore;
 
 // Currently we have one global WebGL processing instance.
-static IPC::StreamConnectionWorkQueue& remoteGraphicsContextGLStreamWorkQueue()
+IPC::StreamConnectionWorkQueue& remoteGraphicsContextGLStreamWorkQueue()
 {
     static NeverDestroyed<IPC::StreamConnectionWorkQueue> instance("RemoteGraphicsContextGL work queue");
     return instance.get();
 }
 
-static constexpr Seconds dispatchReleaseAllResourcesIfUnusedTimeout = 0.2_s;
-static unsigned remoteGraphicsContextCount;
-static void dispatchReleaseAllResourcesIfUnused()
-{
-#if PLATFORM(COCOA)
-    remoteGraphicsContextGLStreamWorkQueue().dispatch(GraphicsContextGLOpenGL::releaseAllResourcesIfUnused);
-#endif
-}
-
 #if !PLATFORM(COCOA)
 Ref<RemoteGraphicsContextGL> RemoteGraphicsContextGL::create(GPUConnectionToWebProcess& gpuConnectionToWebProcess, GraphicsContextGLAttributes&& attributes, GraphicsContextGLIdentifier graphicsContextGLIdentifier, RemoteRenderingBackend& renderingBackend, IPC::StreamConnectionBuffer&& stream)
 {
@@ -76,6 +67,7 @@
     , m_streamConnection(IPC::StreamServerConnection<RemoteGraphicsContextGL>::create(gpuConnectionToWebProcess.connection(), WTFMove(stream), remoteGraphicsContextGLStreamWorkQueue()))
     , m_graphicsContextGLIdentifier(graphicsContextGLIdentifier)
     , m_renderingBackend(makeRef(renderingBackend))
+    , m_renderingResourcesRequest(ScopedWebGLRenderingResourcesRequest::acquire())
 {
     ASSERT(RunLoop::isMain());
 }
@@ -89,7 +81,6 @@
 void RemoteGraphicsContextGL::initialize(GraphicsContextGLAttributes&& attributes)
 {
     ASSERT(RunLoop::isMain());
-    ++remoteGraphicsContextCount;
     remoteGraphicsContextGLStreamWorkQueue().dispatch([attributes = WTFMove(attributes), protectedThis = makeRef(*this)]() mutable {
         protectedThis->workQueueInitialize(WTFMove(attributes));
     });
@@ -102,10 +93,8 @@
     m_streamConnection->stopReceivingMessages(Messages::RemoteGraphicsContextGL::messageReceiverName(), m_graphicsContextGLIdentifier.toUInt64());
     remoteGraphicsContextGLStreamWorkQueue().dispatch([protectedThis = WTFMove(refFromConnection)]() {
         protectedThis->workQueueUninitialize();
+        protectedThis->m_renderingResourcesRequest = { };
     });
-    --remoteGraphicsContextCount;
-    if (!remoteGraphicsContextCount)
-        RunLoop::current().dispatchAfter(dispatchReleaseAllResourcesIfUnusedTimeout, dispatchReleaseAllResourcesIfUnused);
 }
 
 #if PLATFORM(MAC)

Modified: trunk/Source/WebKit/GPUProcess/graphics/RemoteGraphicsContextGL.h (275402 => 275403)


--- trunk/Source/WebKit/GPUProcess/graphics/RemoteGraphicsContextGL.h	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/GPUProcess/graphics/RemoteGraphicsContextGL.h	2021-04-02 04:42:15 UTC (rev 275403)
@@ -31,6 +31,7 @@
 #include "GPUConnectionToWebProcess.h"
 #include "GraphicsContextGLIdentifier.h"
 #include "RemoteRenderingBackend.h"
+#include "ScopedWebGLRenderingResourcesRequest.h"
 #include "StreamServerConnection.h"
 #include <WebCore/ExtensionsGL.h>
 #include <WebCore/GraphicsContextGLOpenGL.h>
@@ -110,8 +111,11 @@
     RefPtr<WebCore::GraphicsContextGLOpenGL> m_context;
     GraphicsContextGLIdentifier m_graphicsContextGLIdentifier;
     Ref<RemoteRenderingBackend> m_renderingBackend;
+    ScopedWebGLRenderingResourcesRequest m_renderingResourcesRequest;
 };
 
+IPC::StreamConnectionWorkQueue& remoteGraphicsContextGLStreamWorkQueue();
+
 } // namespace WebKit
 
 #endif

Modified: trunk/Source/WebKit/GPUProcess/graphics/RemoteRenderingBackend.cpp (275402 => 275403)


--- trunk/Source/WebKit/GPUProcess/graphics/RemoteRenderingBackend.cpp	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/GPUProcess/graphics/RemoteRenderingBackend.cpp	2021-04-02 04:42:15 UTC (rev 275403)
@@ -87,7 +87,8 @@
 RemoteRenderingBackend::~RemoteRenderingBackend()
 {
     // Make sure we destroy the ResourceCache on the WorkQueue since it gets populated on the WorkQueue.
-    m_workQueue->dispatch([remoteResourceCache = WTFMove(m_remoteResourceCache)] { });
+    // Make sure rendering resource request is released after destroying the cache.
+    m_workQueue->dispatch([renderingResourcesRequest = WTFMove(m_renderingResourcesRequest), remoteResourceCache = WTFMove(m_remoteResourceCache)] { });
 }
 
 void RemoteRenderingBackend::stopListeningForIPC()
@@ -166,6 +167,7 @@
     }
 
     m_remoteResourceCache.cacheImageBuffer(makeRef(*imageBuffer));
+    updateRenderingResourceRequest();
 
     if (m_pendingWakeupInfo && m_pendingWakeupInfo->shouldPerformWakeup(renderingResourceIdentifier))
         wakeUpAndApplyDisplayList(std::exchange(m_pendingWakeupInfo, WTF::nullopt)->arguments);
@@ -473,6 +475,7 @@
 {
     ASSERT(!RunLoop::isMain());
     m_remoteResourceCache.releaseRemoteResource(renderingResourceIdentifier);
+    updateRenderingResourceRequest();
 }
 
 void RemoteRenderingBackend::didCreateSharedDisplayListHandle(DisplayList::ItemBufferIdentifier identifier, const SharedMemory::IPCHandle& handle, RenderingResourceIdentifier destinationBufferIdentifier)
@@ -587,6 +590,16 @@
     return WTF::nullopt;
 }
 
+void RemoteRenderingBackend::updateRenderingResourceRequest()
+{
+    bool hasActiveDrawables = !m_remoteResourceCache.imageBuffers().isEmpty() || !m_remoteResourceCache.nativeImages().isEmpty();
+    bool hasActiveRequest = m_renderingResourcesRequest.isRequested();
+    if (hasActiveDrawables && !hasActiveRequest)
+        m_renderingResourcesRequest = ScopedRenderingResourcesRequest::acquire();
+    else if (!hasActiveDrawables && hasActiveRequest)
+        m_renderingResourcesRequest = { };
+}
+
 } // namespace WebKit
 
 #endif // ENABLE(GPU_PROCESS)

Modified: trunk/Source/WebKit/GPUProcess/graphics/RemoteRenderingBackend.h (275402 => 275403)


--- trunk/Source/WebKit/GPUProcess/graphics/RemoteRenderingBackend.h	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/GPUProcess/graphics/RemoteRenderingBackend.h	2021-04-02 04:42:15 UTC (rev 275403)
@@ -35,6 +35,7 @@
 #include "MessageSender.h"
 #include "RemoteResourceCache.h"
 #include "RenderingBackendIdentifier.h"
+#include "ScopedRenderingResourcesRequest.h"
 #include <WebCore/ColorSpace.h>
 #include <WebCore/DisplayList.h>
 #include <WebCore/DisplayListItems.h>
@@ -106,6 +107,7 @@
     RefPtr<WebCore::ImageBuffer> nextDestinationImageBufferAfterApplyingDisplayLists(WebCore::ImageBuffer& initialDestination, size_t initialOffset, DisplayListReaderHandle&, GPUProcessWakeupReason);
 
     Optional<SharedMemory::IPCHandle> updateSharedMemoryForGetImageDataHelper(size_t byteCount);
+    void updateRenderingResourceRequest();
 
     // IPC::MessageSender.
     IPC::Connection* messageSenderConnection() const override;
@@ -132,6 +134,7 @@
     void releaseRemoteResource(WebCore::RenderingResourceIdentifier);
     void didCreateSharedDisplayListHandle(WebCore::DisplayList::ItemBufferIdentifier, const SharedMemory::IPCHandle&, WebCore::RenderingResourceIdentifier destinationBufferIdentifier);
 
+
     struct PendingWakeupInformation {
         GPUProcessWakeupMessageArguments arguments;
         Optional<WebCore::RenderingResourceIdentifier> missingCachedResourceIdentifier;
@@ -157,6 +160,7 @@
     IPC::Semaphore m_resumeDisplayListSemaphore;
     IPC::Semaphore m_getImageDataSemaphore;
     RefPtr<SharedMemory> m_getImageDataSharedMemory;
+    ScopedRenderingResourcesRequest m_renderingResourcesRequest;
 };
 
 } // namespace WebKit

Copied: trunk/Source/WebKit/GPUProcess/graphics/ScopedRenderingResourcesRequest.cpp (from rev 275402, trunk/Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h) (0 => 275403)


--- trunk/Source/WebKit/GPUProcess/graphics/ScopedRenderingResourcesRequest.cpp	                        (rev 0)
+++ trunk/Source/WebKit/GPUProcess/graphics/ScopedRenderingResourcesRequest.cpp	2021-04-02 04:42:15 UTC (rev 275403)
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "ScopedRenderingResourcesRequest.h"
+
+namespace WebKit {
+
+std::atomic<unsigned> ScopedRenderingResourcesRequest::s_requests;
+
+#if !PLATFORM(COCOA)
+
+void ScopedRenderingResourcesRequest::scheduleFreeRenderingResources()
+{
+}
+
+void ScopedRenderingResourcesRequest::freeRenderingResources()
+{
+}
+
+#endif
+
+}

Added: trunk/Source/WebKit/GPUProcess/graphics/ScopedRenderingResourcesRequest.h (0 => 275403)


--- trunk/Source/WebKit/GPUProcess/graphics/ScopedRenderingResourcesRequest.h	                        (rev 0)
+++ trunk/Source/WebKit/GPUProcess/graphics/ScopedRenderingResourcesRequest.h	2021-04-02 04:42:15 UTC (rev 275403)
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <atomic>
+#include <utility>
+
+namespace WebKit {
+
+class ScopedRenderingResourcesRequest {
+public:
+    ScopedRenderingResourcesRequest() = default;
+    ScopedRenderingResourcesRequest(ScopedRenderingResourcesRequest&& other)
+        : m_requested(std::exchange(other.m_requested, false))
+    {
+    }
+    ~ScopedRenderingResourcesRequest()
+    {
+        reset();
+    }
+    ScopedRenderingResourcesRequest& operator=(ScopedRenderingResourcesRequest&& other)
+    {
+        if (this != &other) {
+            reset();
+            m_requested = std::exchange(other.m_requested, false);
+        }
+        return *this;
+    }
+    bool isRequested() const { return m_requested; }
+    void reset()
+    {
+        if (!m_requested)
+            return;
+        ASSERT(s_requests);
+        --s_requests;
+        if (!s_requests)
+            scheduleFreeRenderingResources();
+    }
+    static ScopedRenderingResourcesRequest acquire()
+    {
+        return { DidRequest };
+    }
+private:
+    static void scheduleFreeRenderingResources();
+    static void freeRenderingResources();
+    enum RequestState { DidRequest };
+    ScopedRenderingResourcesRequest(RequestState)
+        : m_requested(true)
+    {
+        ++s_requests;
+    }
+    static std::atomic<unsigned> s_requests;
+    bool m_requested { false };
+};
+
+}

Copied: trunk/Source/WebKit/GPUProcess/graphics/ScopedRenderingResourcesRequestCocoa.mm (from rev 275402, trunk/Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h) (0 => 275403)


--- trunk/Source/WebKit/GPUProcess/graphics/ScopedRenderingResourcesRequestCocoa.mm	                        (rev 0)
+++ trunk/Source/WebKit/GPUProcess/graphics/ScopedRenderingResourcesRequestCocoa.mm	2021-04-02 04:42:15 UTC (rev 275403)
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "ScopedRenderingResourcesRequest.h"
+
+#if PLATFORM(COCOA)
+
+#import <Metal/Metal.h>
+#import <objc/NSObjCRuntime.h>
+#import <pal/spi/cocoa/MetalSPI.h>
+#import <wtf/BlockObjCExceptions.h>
+#import <wtf/RetainPtr.h>
+#import <wtf/RunLoop.h>
+#import <wtf/Seconds.h>
+
+OBJC_PROTOCOL(MTLDevice);
+OBJC_PROTOCOL(MTLDeviceSPI);
+
+namespace WebKit {
+
+static constexpr Seconds freeRenderingResourcesTimeout = 1_s;
+static bool didScheduleFreeRenderingResources;
+
+void ScopedRenderingResourcesRequest::scheduleFreeRenderingResources()
+{
+    if (didScheduleFreeRenderingResources)
+        return;
+    RunLoop::main().dispatchAfter(freeRenderingResourcesTimeout, freeRenderingResources);
+    didScheduleFreeRenderingResources = true;
+}
+
+void ScopedRenderingResourcesRequest::freeRenderingResources()
+{
+    didScheduleFreeRenderingResources = false;
+    if (s_requests)
+        return;
+    BEGIN_BLOCK_OBJC_EXCEPTIONS
+#if PLATFORM(MAC)
+    auto devices = adoptNS(MTLCopyAllDevices());
+    for (id <MTLDevice> device : devices.get())
+        [(_MTLDevice *)device _purgeDevice];
+#else
+    RetainPtr<MTLDevice> devicePtr = adoptNS(MTLCreateSystemDefaultDevice());
+    [(_MTLDevice *)devicePtr.get() _purgeDevice];
+#endif
+    END_BLOCK_OBJC_EXCEPTIONS
+}
+
+}
+
+#endif

Copied: trunk/Source/WebKit/GPUProcess/graphics/ScopedWebGLRenderingResourcesRequest.cpp (from rev 275402, trunk/Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h) (0 => 275403)


--- trunk/Source/WebKit/GPUProcess/graphics/ScopedWebGLRenderingResourcesRequest.cpp	                        (rev 0)
+++ trunk/Source/WebKit/GPUProcess/graphics/ScopedWebGLRenderingResourcesRequest.cpp	2021-04-02 04:42:15 UTC (rev 275403)
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "ScopedWebGLRenderingResourcesRequest.h"
+
+namespace WebKit {
+
+std::atomic<unsigned> ScopedWebGLRenderingResourcesRequest::s_requests;
+
+#if !PLATFORM(COCOA)
+
+void ScopedWebGLRenderingResourcesRequest::scheduleFreeWebGLRenderingResources()
+{
+}
+
+void ScopedWebGLRenderingResourcesRequest::freeWebGLRenderingResources()
+{
+}
+
+#endif
+
+}

Added: trunk/Source/WebKit/GPUProcess/graphics/ScopedWebGLRenderingResourcesRequest.h (0 => 275403)


--- trunk/Source/WebKit/GPUProcess/graphics/ScopedWebGLRenderingResourcesRequest.h	                        (rev 0)
+++ trunk/Source/WebKit/GPUProcess/graphics/ScopedWebGLRenderingResourcesRequest.h	2021-04-02 04:42:15 UTC (rev 275403)
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include "ScopedRenderingResourcesRequest.h"
+#include <atomic>
+#include <wtf/StdLibExtras.h>
+
+namespace WebKit {
+
+class ScopedWebGLRenderingResourcesRequest {
+public:
+    ScopedWebGLRenderingResourcesRequest() = default;
+    ScopedWebGLRenderingResourcesRequest(ScopedWebGLRenderingResourcesRequest&& other)
+        : m_requested(std::exchange(other.m_requested, false))
+        , m_renderingResourcesRequest(WTFMove(other.m_renderingResourcesRequest))
+    {
+    }
+    ~ScopedWebGLRenderingResourcesRequest()
+    {
+        reset();
+    }
+    ScopedWebGLRenderingResourcesRequest& operator=(ScopedWebGLRenderingResourcesRequest&& other)
+    {
+        if (this != &other) {
+            reset();
+            m_requested = std::exchange(other.m_requested, false);
+            m_renderingResourcesRequest = WTFMove(other.m_renderingResourcesRequest);
+        }
+        return *this;
+    }
+    bool isRequested() const { return m_requested; }
+    void reset()
+    {
+        if (!m_requested)
+            return;
+        ASSERT(s_requests);
+        --s_requests;
+        if (!s_requests)
+            scheduleFreeWebGLRenderingResources();
+    }
+    static ScopedWebGLRenderingResourcesRequest acquire()
+    {
+        return { DidRequest };
+    }
+private:
+    static void scheduleFreeWebGLRenderingResources();
+    static void freeWebGLRenderingResources();
+    enum RequestState { DidRequest };
+    ScopedWebGLRenderingResourcesRequest(RequestState)
+        : m_requested(true)
+        , m_renderingResourcesRequest(ScopedRenderingResourcesRequest::acquire())
+    {
+        ++s_requests;
+    }
+    bool m_requested { false };
+    ScopedRenderingResourcesRequest m_renderingResourcesRequest;
+    static std::atomic<unsigned> s_requests;
+};
+
+}

Copied: trunk/Source/WebKit/GPUProcess/graphics/ScopedWebGLRenderingResourcesRequestCocoa.cpp (from rev 275402, trunk/Source/WebCore/PAL/pal/spi/cocoa/MetalSPI.h) (0 => 275403)


--- trunk/Source/WebKit/GPUProcess/graphics/ScopedWebGLRenderingResourcesRequestCocoa.cpp	                        (rev 0)
+++ trunk/Source/WebKit/GPUProcess/graphics/ScopedWebGLRenderingResourcesRequestCocoa.cpp	2021-04-02 04:42:15 UTC (rev 275403)
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "ScopedWebGLRenderingResourcesRequest.h"
+
+#if PLATFORM(COCOA)
+
+#include "RemoteGraphicsContextGL.h"
+#include "StreamConnectionWorkQueue.h"
+#include <WebCore/GraphicsContextGL.h>
+#include <wtf/RunLoop.h>
+#include <wtf/Seconds.h>
+
+namespace WebKit {
+
+static constexpr Seconds freeWebGLRenderingResourcesTimeout = 1_s;
+static bool didScheduleFreeWebGLRenderingResources;
+
+void ScopedWebGLRenderingResourcesRequest::scheduleFreeWebGLRenderingResources()
+{
+    if (didScheduleFreeWebGLRenderingResources)
+        return;
+    RunLoop::main().dispatchAfter(freeWebGLRenderingResourcesTimeout, freeWebGLRenderingResources);
+    didScheduleFreeWebGLRenderingResources = true;
+}
+
+void ScopedWebGLRenderingResourcesRequest::freeWebGLRenderingResources()
+{
+    didScheduleFreeWebGLRenderingResources = false;
+    if (s_requests)
+        return;
+    remoteGraphicsContextGLStreamWorkQueue().dispatch(GraphicsContextGLOpenGL::releaseAllResourcesIfUnused);
+}
+
+}
+
+#endif

Modified: trunk/Source/WebKit/GPUProcess/media/RemoteMediaPlayerProxy.cpp (275402 => 275403)


--- trunk/Source/WebKit/GPUProcess/media/RemoteMediaPlayerProxy.cpp	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/GPUProcess/media/RemoteMediaPlayerProxy.cpp	2021-04-02 04:42:15 UTC (rev 275403)
@@ -77,6 +77,7 @@
     , m_engineIdentifier(engineIdentifier)
     , m_updateCachedStateMessageTimer(RunLoop::main(), this, &RemoteMediaPlayerProxy::timerFired)
     , m_configuration(configuration)
+    , m_renderingResourcesRequest(ScopedRenderingResourcesRequest::acquire())
 #if !RELEASE_LOG_DISABLED
     , m_logger(manager.logger())
 #endif
@@ -100,6 +101,7 @@
         m_sandboxExtension->revoke();
         m_sandboxExtension = nullptr;
     }
+    m_renderingResourcesRequest = { };
 }
 
 void RemoteMediaPlayerProxy::getConfiguration(RemoteMediaPlayerConfiguration& configuration)

Modified: trunk/Source/WebKit/GPUProcess/media/RemoteMediaPlayerProxy.h (275402 => 275403)


--- trunk/Source/WebKit/GPUProcess/media/RemoteMediaPlayerProxy.h	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/GPUProcess/media/RemoteMediaPlayerProxy.h	2021-04-02 04:42:15 UTC (rev 275403)
@@ -35,6 +35,7 @@
 #include "RemoteMediaPlayerState.h"
 #include "RemoteMediaResourceIdentifier.h"
 #include "SandboxExtension.h"
+#include "ScopedRenderingResourcesRequest.h"
 #include "TrackPrivateRemoteIdentifier.h"
 #include <WebCore/Cookie.h>
 #include <WebCore/InbandTextTrackPrivate.h>
@@ -334,6 +335,7 @@
 #if ENABLE(WEB_AUDIO) && PLATFORM(COCOA)
     RefPtr<RemoteAudioSourceProviderProxy> m_remoteAudioSourceProvider;
 #endif
+    ScopedRenderingResourcesRequest m_renderingResourcesRequest;
 
 #if !RELEASE_LOG_DISABLED
     const Logger& m_logger;

Modified: trunk/Source/WebKit/Sources.txt (275402 => 275403)


--- trunk/Source/WebKit/Sources.txt	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/Sources.txt	2021-04-02 04:42:15 UTC (rev 275403)
@@ -28,6 +28,8 @@
 GPUProcess/graphics/RemoteRenderingBackend.cpp
 GPUProcess/graphics/RemoteGraphicsContextGL.cpp
 GPUProcess/graphics/RemoteResourceCache.cpp
+GPUProcess/graphics/ScopedRenderingResourcesRequest.cpp
+GPUProcess/graphics/ScopedWebGLRenderingResourcesRequest.cpp
 GPUProcess/media/RemoteAudioHardwareListenerProxy.cpp
 GPUProcess/media/RemoteAudioSessionProxy.cpp
 GPUProcess/media/RemoteAudioSessionProxyManager.cpp

Modified: trunk/Source/WebKit/SourcesCocoa.txt (275402 => 275403)


--- trunk/Source/WebKit/SourcesCocoa.txt	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/SourcesCocoa.txt	2021-04-02 04:42:15 UTC (rev 275403)
@@ -58,6 +58,8 @@
 
 GPUProcess/EntryPoint/Cocoa/XPCService/GPUServiceEntryPoint.mm
 GPUProcess/graphics/RemoteGraphicsContextGLCocoa.cpp
+GPUProcess/graphics/ScopedRenderingResourcesRequestCocoa.mm @no-unify
+GPUProcess/graphics/ScopedWebGLRenderingResourcesRequestCocoa.cpp
 GPUProcess/ios/GPUProcessIOS.mm
 GPUProcess/mac/GPUProcessMac.mm
 GPUProcess/media/RemoteAudioSourceProviderProxy.cpp

Modified: trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj (275402 => 275403)


--- trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj	2021-04-02 04:02:04 UTC (rev 275402)
+++ trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj	2021-04-02 04:42:15 UTC (rev 275403)
@@ -1337,6 +1337,7 @@
 		7B73124125CC8525003B2796 /* StreamServerConnection.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B73123825CC8524003B2796 /* StreamServerConnection.h */; };
 		7B73124225CC8525003B2796 /* StreamConnectionEncoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B73123925CC8525003B2796 /* StreamConnectionEncoder.h */; };
 		7BAB111025DD02B3008FC479 /* ScopedActiveMessageReceiveQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BAB110F25DD02B2008FC479 /* ScopedActiveMessageReceiveQueue.h */; };
+		7BCF70DE2615D06E00E4FB70 /* ScopedRenderingResourcesRequestCocoa.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7BCF70CB2614935E00E4FB70 /* ScopedRenderingResourcesRequestCocoa.mm */; };
 		7C065F2C1C8CD95F00C2D950 /* WebUserContentControllerDataTypes.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C065F2A1C8CD95F00C2D950 /* WebUserContentControllerDataTypes.h */; };
 		7C135AA9173B0BCA00586AE2 /* WKPluginInformation.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C135AA7173B0BCA00586AE2 /* WKPluginInformation.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		7C1BA33E1A4A0E600043E249 /* APIDictionary.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C1BA33C1A4A0E600043E249 /* APIDictionary.h */; };
@@ -4591,6 +4592,12 @@
 		7B90416A254AFEA7006EEB8C /* RemoteGraphicsContextGL.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RemoteGraphicsContextGL.h; sourceTree = "<group>"; };
 		7B90416D2550108C006EEB8C /* RemoteGraphicsContextGLFunctionsGenerated.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RemoteGraphicsContextGLFunctionsGenerated.h; sourceTree = "<group>"; };
 		7BAB110F25DD02B2008FC479 /* ScopedActiveMessageReceiveQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScopedActiveMessageReceiveQueue.h; sourceTree = "<group>"; };
+		7BCF70CA2614935E00E4FB70 /* ScopedRenderingResourcesRequest.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ScopedRenderingResourcesRequest.cpp; sourceTree = "<group>"; };
+		7BCF70CB2614935E00E4FB70 /* ScopedRenderingResourcesRequestCocoa.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ScopedRenderingResourcesRequestCocoa.mm; sourceTree = "<group>"; };
+		7BCF70CC2614935F00E4FB70 /* ScopedWebGLRenderingResourcesRequest.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ScopedWebGLRenderingResourcesRequest.cpp; sourceTree = "<group>"; };
+		7BCF70CD2614935F00E4FB70 /* ScopedRenderingResourcesRequest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScopedRenderingResourcesRequest.h; sourceTree = "<group>"; };
+		7BCF70CE2614935F00E4FB70 /* ScopedWebGLRenderingResourcesRequest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScopedWebGLRenderingResourcesRequest.h; sourceTree = "<group>"; };
+		7BCF70CF261497E600E4FB70 /* ScopedWebGLRenderingResourcesRequestCocoa.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ScopedWebGLRenderingResourcesRequestCocoa.cpp; sourceTree = "<group>"; };
 		7BE726572574F67200E85D98 /* RemoteGraphicsContextGLProxyFunctionsGenerated.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = RemoteGraphicsContextGLProxyFunctionsGenerated.cpp; sourceTree = "<group>"; };
 		7BE72668257680EF00E85D98 /* RemoteGraphicsContextGLCocoa.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = RemoteGraphicsContextGLCocoa.cpp; sourceTree = "<group>"; };
 		7C065F291C8CD95F00C2D950 /* WebUserContentControllerDataTypes.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebUserContentControllerDataTypes.cpp; sourceTree = "<group>"; };
@@ -8768,6 +8775,12 @@
 				550640A424071C2100AAE045 /* RemoteRenderingBackend.messages.in */,
 				726D56DD253A64810002EF90 /* RemoteResourceCache.cpp */,
 				726D56DE253A64810002EF90 /* RemoteResourceCache.h */,
+				7BCF70CA2614935E00E4FB70 /* ScopedRenderingResourcesRequest.cpp */,
+				7BCF70CD2614935F00E4FB70 /* ScopedRenderingResourcesRequest.h */,
+				7BCF70CB2614935E00E4FB70 /* ScopedRenderingResourcesRequestCocoa.mm */,
+				7BCF70CC2614935F00E4FB70 /* ScopedWebGLRenderingResourcesRequest.cpp */,
+				7BCF70CE2614935F00E4FB70 /* ScopedWebGLRenderingResourcesRequest.h */,
+				7BCF70CF261497E600E4FB70 /* ScopedWebGLRenderingResourcesRequestCocoa.cpp */,
 			);
 			path = graphics;
 			sourceTree = "<group>";
@@ -13984,6 +13997,7 @@
 				1DD2A66E2562021E00FF7B6F /* RemoteSourceBufferProxyMessageReceiver.cpp in Sources */,
 				1BBBE4A019B66C53006B7D81 /* RemoteWebInspectorUIMessageReceiver.cpp in Sources */,
 				A55BA8261BA25CFD007CD33D /* RemoteWebInspectorUIProxyMessageReceiver.cpp in Sources */,
+				7BCF70DE2615D06E00E4FB70 /* ScopedRenderingResourcesRequestCocoa.mm in Sources */,
 				E18E6917169B667B009B6670 /* SecItemShimProxyMessageReceiver.cpp in Sources */,
 				41DE7C6C22278F1E00532B65 /* ServiceWorkerFetchTask.cpp in Sources */,
 				2D92A787212B6AB100F493FD /* ShareableBitmap.cpp in Sources */,
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to