Skip to content

Commit

Permalink
GPUP WebGL spends too much time allocating IOSurfaces
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=255860
rdar://108444431

Reviewed by Simon Fraser.

Creating MachSendRight out of IOSurface guarantees that the surface
is marked as in use for the lifetime of the surface. Trust this to
recycle the display buffers as drawing buffers in the GPUP mode.

Increase the held buffers to 3 surfaces per context. Otherwise the
buffers are never recycled, rather always reallocated. The new frame
is always being drawn while the previous frame is still being
composited.

Removes GraphicsContextGLIOSurfaceSwapChain, it is not a useful
abstraction. This was used temporarily when all the OpenGL variants
used the same implementation files.

Renames incorrect naming "DisplayBuffer" to "DrawingBuffer". The code
actually meant "current drawing buffer that will be display buffer
in the future".

* Source/WebCore/SourcesCocoa.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/platform/graphics/angle/GraphicsContextGLANGLE.cpp:
(WebCore::GraphicsContextGLANGLE::reshapeFBOs):
* Source/WebCore/platform/graphics/angle/GraphicsContextGLANGLE.h:
* Source/WebCore/platform/graphics/cocoa/GraphicsContextGLCocoa.h:
* Source/WebCore/platform/graphics/cocoa/GraphicsContextGLCocoa.mm:
(WebCore::GraphicsContextGLCocoa::~GraphicsContextGLCocoa):
(WebCore::GraphicsContextGLANGLE::~GraphicsContextGLANGLE):
(WebCore::GraphicsContextGLCocoa::reshapeDrawingBuffer):
(WebCore::GraphicsContextGLCocoa::setDrawingBufferColorSpace):
(WebCore::GraphicsContextGLCocoa::displayBuffer):
(WebCore::GraphicsContextGLCocoa::drawingBuffer):
(WebCore::GraphicsContextGLCocoa::bindNextDrawingBuffer):
(WebCore::GraphicsContextGLCocoa::freeDrawingBuffers):
(WebCore::GraphicsContextGLCocoa::prepareForDisplay):
(WebCore::GraphicsContextGLCocoa::readCompositedResults):
(WebCore::GraphicsContextGLCocoa::paintCompositedResultsToVideoFrame):
(WebCore::GraphicsContextGLCocoa::withDrawingBufferAsNativeImage):
(WebCore::GraphicsContextGLCocoa::withDisplayBufferAsNativeImage):
(WebCore::GraphicsContextGLCocoa::markDisplayBufferInUse): Deleted.
(WebCore::GraphicsContextGLCocoa::reshapeDisplayBufferBacking): Deleted.
(WebCore::GraphicsContextGLCocoa::allocateAndBindDisplayBufferBacking): Deleted.
(WebCore::GraphicsContextGLCocoa::bindDisplayBufferBacking): Deleted.
(WebCore::GraphicsContextGLANGLE::readCompositedResults): Deleted.
* Source/WebCore/platform/graphics/cocoa/GraphicsContextGLIOSurfaceSwapChain.cpp: Removed.
* Source/WebCore/platform/graphics/cocoa/GraphicsContextGLIOSurfaceSwapChain.h: Removed.
* Source/WebCore/platform/graphics/cocoa/WebProcessGraphicsContextGLCocoa.mm:
* Source/WebCore/platform/graphics/gbm/GraphicsContextGLFallback.cpp:
(WebCore::GraphicsContextGLFallback::reshapeDrawingBuffer):
(WebCore::GraphicsContextGLFallback::reshapeDisplayBufferBacking): Deleted.
* Source/WebCore/platform/graphics/gbm/GraphicsContextGLFallback.h:
* Source/WebCore/platform/graphics/gbm/GraphicsContextGLGBM.cpp:
(WebCore::GraphicsContextGLGBM::reshapeDrawingBuffer):
(WebCore::GraphicsContextGLGBM::reshapeDisplayBufferBacking): Deleted.
* Source/WebCore/platform/graphics/gbm/GraphicsContextGLGBM.h:
* Source/WebCore/platform/graphics/texmap/GraphicsContextGLTextureMapperANGLE.cpp:
(WebCore::GraphicsContextGLTextureMapperANGLE::reshapeDrawingBuffer):
(WebCore::GraphicsContextGLTextureMapperANGLE::reshapeDisplayBufferBacking): Deleted.
* Source/WebCore/platform/graphics/texmap/GraphicsContextGLTextureMapperANGLE.h:
* Source/WebKit/GPUProcess/graphics/RemoteGraphicsContextGLCocoa.cpp:
(WebKit::RemoteGraphicsContextGLCocoa::prepareForDisplay):
* Tools/TestWebKitAPI/Tests/WebCore/cocoa/TestGraphicsContextGLCocoa.mm:
(TestWebKitAPI::TEST_P):

Canonical link: https://commits.webkit.org/263496@main
  • Loading branch information
kkinnunen-apple committed Apr 28, 2023
1 parent 3bfd9bb commit 3b9112f
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 346 deletions.
2 changes: 0 additions & 2 deletions Source/WebCore/PlatformMac.cmake
Expand Up @@ -319,7 +319,6 @@ list(APPEND WebCore_SOURCES
platform/graphics/cocoa/FontPlatformDataCocoa.mm
platform/graphics/cocoa/GraphicsContextCocoa.mm
platform/graphics/cocoa/GraphicsContextGLCocoa.mm
platform/graphics/cocoa/GraphicsContextGLIOSurfaceSwapChain.cpp
platform/graphics/cocoa/IntRectCocoa.mm
platform/graphics/cocoa/IOSurface.mm
platform/graphics/cocoa/IOSurfacePoolCocoa.mm
Expand Down Expand Up @@ -671,7 +670,6 @@ list(APPEND WebCore_PRIVATE_FRAMEWORK_HEADERS
platform/graphics/cocoa/FontFamilySpecificationCoreText.h
platform/graphics/cocoa/FontFamilySpecificationCoreTextCache.h
platform/graphics/cocoa/GraphicsContextGLCocoa.h
platform/graphics/cocoa/GraphicsContextGLIOSurfaceSwapChain.h
platform/graphics/cocoa/IOSurface.h
platform/graphics/cocoa/MediaPlaybackTargetContext.h
platform/graphics/cocoa/MediaPlayerPrivateWebM.h
Expand Down
1 change: 0 additions & 1 deletion Source/WebCore/SourcesCocoa.txt
Expand Up @@ -419,7 +419,6 @@ platform/graphics/cocoa/FontFamilySpecificationCoreText.cpp
platform/graphics/cocoa/FontFamilySpecificationCoreTextCache.cpp
platform/graphics/cocoa/FontPlatformDataCocoa.mm
platform/graphics/cocoa/GraphicsContextCocoa.mm
platform/graphics/cocoa/GraphicsContextGLIOSurfaceSwapChain.cpp
platform/graphics/cocoa/HEVCUtilitiesCocoa.mm
platform/graphics/cocoa/IOSurface.mm
platform/graphics/cocoa/IOSurfacePoolCocoa.mm
Expand Down
6 changes: 0 additions & 6 deletions Source/WebCore/WebCore.xcodeproj/project.pbxproj
Expand Up @@ -2674,7 +2674,6 @@
7B10339E2549721700C8C1AC /* GraphicsContextGLCV.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B10339D2549720100C8C1AC /* GraphicsContextGLCV.h */; settings = {ATTRIBUTES = (Private, ); }; };
7B10339F2549721E00C8C1AC /* GraphicsContextGLCVCocoa.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B10339C2549720100C8C1AC /* GraphicsContextGLCVCocoa.h */; settings = {ATTRIBUTES = (Private, ); }; };
7B6094DD270C44BD0064835C /* ANGLEUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BB34A1625345CB200029D08 /* ANGLEUtilities.h */; settings = {ATTRIBUTES = (Private, ); }; };
7B6DC81925712E9200380C70 /* GraphicsContextGLIOSurfaceSwapChain.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B6DC81725712E9200380C70 /* GraphicsContextGLIOSurfaceSwapChain.h */; settings = {ATTRIBUTES = (Private, ); }; };
7B720B0C2743DBA70008ABED /* GraphicsLayerContentsDisplayDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B720B0A2743DBA60008ABED /* GraphicsLayerContentsDisplayDelegate.h */; settings = {ATTRIBUTES = (Private, ); }; };
7B7311FB25C092B7003B2796 /* ScopedHighPerformanceGPURequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B7311FA25C092B7003B2796 /* ScopedHighPerformanceGPURequest.h */; settings = {ATTRIBUTES = (Private, ); }; };
7B8ED5F026F0C13D008AC023 /* CVUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B8ED5ED26F0C123008AC023 /* CVUtilities.h */; settings = {ATTRIBUTES = (Private, ); }; };
Expand Down Expand Up @@ -12620,8 +12619,6 @@
7B434D1329DB0FD00062E8D1 /* PlatformCALayerDelegatedContents.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlatformCALayerDelegatedContents.h; sourceTree = "<group>"; };
7B5A3DAB27ABF8C4006C6F97 /* VideoFrame.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = VideoFrame.cpp; sourceTree = "<group>"; };
7B5A3DAD27ABF8C4006C6F97 /* VideoFrame.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VideoFrame.h; sourceTree = "<group>"; };
7B6DC81525712E9100380C70 /* GraphicsContextGLIOSurfaceSwapChain.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GraphicsContextGLIOSurfaceSwapChain.cpp; sourceTree = "<group>"; };
7B6DC81725712E9200380C70 /* GraphicsContextGLIOSurfaceSwapChain.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GraphicsContextGLIOSurfaceSwapChain.h; sourceTree = "<group>"; };
7B720B0A2743DBA60008ABED /* GraphicsLayerContentsDisplayDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GraphicsLayerContentsDisplayDelegate.h; sourceTree = "<group>"; };
7B7311FA25C092B7003B2796 /* ScopedHighPerformanceGPURequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScopedHighPerformanceGPURequest.h; sourceTree = "<group>"; };
7B8ED5ED26F0C123008AC023 /* CVUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CVUtilities.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -31360,8 +31357,6 @@
B277B4030B22F37C0004BEC6 /* GraphicsContextCocoa.mm */,
7BDDA31D274F9DA50038659E /* GraphicsContextGLCocoa.h */,
7BDDA31C274F9DA40038659E /* GraphicsContextGLCocoa.mm */,
7B6DC81525712E9100380C70 /* GraphicsContextGLIOSurfaceSwapChain.cpp */,
7B6DC81725712E9200380C70 /* GraphicsContextGLIOSurfaceSwapChain.h */,
CDA595962146DF7800A84185 /* HEVCUtilitiesCocoa.h */,
CDA595972146DF7800A84185 /* HEVCUtilitiesCocoa.mm */,
075CC6C228EE99E100EAAF61 /* IconCocoa.mm */,
Expand Down Expand Up @@ -37276,7 +37271,6 @@
7B10339F2549721E00C8C1AC /* GraphicsContextGLCVCocoa.h in Headers */,
3ABD156A297A289300AF9D76 /* GraphicsContextGLEnums.h in Headers */,
7BB34A48253776CA00029D08 /* GraphicsContextGLImageExtractor.h in Headers */,
7B6DC81925712E9200380C70 /* GraphicsContextGLIOSurfaceSwapChain.h in Headers */,
7B95CFEB2754FF6E000060CE /* GraphicsContextGLState.h in Headers */,
722AF2DF27E1C2060078D997 /* GraphicsContextState.h in Headers */,
722AF2DE27E1C2060078D997 /* GraphicsContextStateSaver.h in Headers */,
Expand Down
Expand Up @@ -267,7 +267,7 @@ bool GraphicsContextGLANGLE::reshapeFBOs(const IntSize& size)
// resize regular FBO
GL_BindFramebuffer(GL_FRAMEBUFFER, m_fbo);

if (!reshapeDisplayBufferBacking()) {
if (!reshapeDrawingBuffer()) {
RELEASE_LOG(WebGL, "Fatal: Unable to allocate backing store of size %d x %d", width, height);
forceContextLost();
return true;
Expand Down
20 changes: 2 additions & 18 deletions Source/WebCore/platform/graphics/angle/GraphicsContextGLANGLE.h
Expand Up @@ -34,12 +34,6 @@
#include <memory>
#include <wtf/Function.h>

#if PLATFORM(COCOA)
#include "GraphicsContextGLIOSurfaceSwapChain.h"
#include "IOSurface.h"
#include "ProcessIdentity.h"
#endif

namespace WebCore {

// Base class for GraphicsContextGL contexts that use ANGLE.
Expand Down Expand Up @@ -384,7 +378,7 @@ class WEBCORE_EXPORT GraphicsContextGLANGLE : public GraphicsContextGL {
void checkGPUStatus();

RefPtr<PixelBuffer> readRenderingResults();
RefPtr<PixelBuffer> readCompositedResults();
virtual RefPtr<PixelBuffer> readCompositedResults() = 0;
RefPtr<PixelBuffer> readPixelsForPaintResults();

bool reshapeFBOs(const IntSize&);
Expand All @@ -394,7 +388,7 @@ class WEBCORE_EXPORT GraphicsContextGLANGLE : public GraphicsContextGL {
#if PLATFORM(COCOA)
static bool makeCurrent(GCGLDisplay, GCGLContext);
#endif
virtual bool reshapeDisplayBufferBacking() = 0;
virtual bool reshapeDrawingBuffer() = 0;

static void platformReleaseThreadResources();

Expand Down Expand Up @@ -430,16 +424,6 @@ class WEBCORE_EXPORT GraphicsContextGLANGLE : public GraphicsContextGL {
GCGLDisplay m_displayObj { nullptr };
GCGLContext m_contextObj { nullptr };
GCGLConfig m_configObj { nullptr };

#if PLATFORM(COCOA)
// FIXME: Move these to GraphicsContextGLCocoa.
GraphicsContextGLIOSurfaceSwapChain m_swapChain;
// Backing store for the the buffer which is eventually used for display.
// When preserveDrawingBuffer == false, this is the drawing buffer backing store.
// When preserveDrawingBuffer == true, this is blitted to during display prepare.
std::unique_ptr<IOSurface> m_displayBufferBacking;
void* m_displayBufferPbuffer { nullptr };
#endif
};


Expand Down
29 changes: 21 additions & 8 deletions Source/WebCore/platform/graphics/cocoa/GraphicsContextGLCocoa.h
Expand Up @@ -29,7 +29,8 @@

#include "GraphicsContextGLANGLE.h"
#include "IOSurface.h"
#include <wtf/BlockPtr.h>
#include "ProcessIdentity.h"
#include <array>

#if PLATFORM(MAC)
#include "ScopedHighPerformanceGPURequest.h"
Expand All @@ -44,7 +45,6 @@ OBJC_CLASS MTLSharedEventListener;
namespace WebCore {

class GraphicsLayerContentsDisplayDelegate;
class ProcessIdentity;

#if ENABLE(VIDEO)
class GraphicsContextGLCVCocoa;
Expand All @@ -58,9 +58,7 @@ class WEBCORE_EXPORT GraphicsContextGLCocoa : public GraphicsContextGLANGLE {
public:
static RefPtr<GraphicsContextGLCocoa> create(WebCore::GraphicsContextGLAttributes&&, ProcessIdentity&& resourceOwner);
~GraphicsContextGLCocoa();

IOSurface* displayBuffer();
void markDisplayBufferInUse();
IOSurface* displayBufferSurface();

enum class PbufferAttachmentUsage { Read, Write, ReadWrite };
// Returns a handle which, if non-null, must be released via the
Expand Down Expand Up @@ -91,6 +89,7 @@ class WEBCORE_EXPORT GraphicsContextGLCocoa : public GraphicsContextGLANGLE {
#if ENABLE(MEDIA_STREAM) || ENABLE(WEB_CODECS)
RefPtr<VideoFrame> paintCompositedResultsToVideoFrame() final;
#endif
RefPtr<PixelBuffer> readCompositedResults() final;
void setContextVisibility(bool) final;
void setDrawingBufferColorSpace(const DestinationColorSpace&) final;
void prepareForDisplay() override;
Expand All @@ -112,9 +111,20 @@ class WEBCORE_EXPORT GraphicsContextGLCocoa : public GraphicsContextGLANGLE {
bool platformInitializeContext() final;
bool platformInitialize() final;
void invalidateKnownTextureContent(GCGLuint) final;
bool reshapeDisplayBufferBacking() final;
bool allocateAndBindDisplayBufferBacking();
bool bindDisplayBufferBacking(std::unique_ptr<IOSurface> backing, void* pbuffer);
bool reshapeDrawingBuffer() final;

// IOSurface backing store for an image of a texture.
// When preserveDrawingBuffer == false, this is the drawing buffer backing store.
// When preserveDrawingBuffer == true, this is blitted to during display prepare.
struct IOSurfacePbuffer {
std::unique_ptr<IOSurface> surface;
void* pbuffer { nullptr };
operator bool() const { return !!surface; }
};
IOSurfacePbuffer& drawingBuffer();
IOSurfacePbuffer& displayBuffer();
bool bindNextDrawingBuffer();
void freeDrawingBuffers();

// Inserts new fence that will invoke `signal` from a background thread when completed.
// If not possible, calls the `signal`.
Expand All @@ -136,6 +146,9 @@ class WEBCORE_EXPORT GraphicsContextGLCocoa : public GraphicsContextGLANGLE {
RetainPtr<MTLSharedEventListener> m_finishedMetalSharedEventListener;
RetainPtr<id> m_finishedMetalSharedEvent; // FIXME: Remove all C++ includees and use id<MTLSharedEvent>.

static constexpr size_t maxReusedDrawingBuffers { 3 };
size_t m_currentDrawingBufferIndex { 0 };
std::array<IOSurfacePbuffer, maxReusedDrawingBuffers> m_drawingBuffers;
friend class GraphicsContextGLCVCocoa;
};

Expand Down

0 comments on commit 3b9112f

Please sign in to comment.