Skip to content

Commit

Permalink
[Skia] Check if glFence is available before using it
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=271376

Reviewed by Miguel Gomez.

If not available we need to call GrDirectContext::submit() with
GrSyncCpu=kYes. This patch adds a helper class GLFence to wrap it, that
can be extended in the future to support nvidia fences if needed too.

* Source/WebCore/Headers.cmake:
* Source/WebCore/PlatformPlayStation.cmake:
* Source/WebCore/SourcesGTK.txt:
* Source/WebCore/SourcesWPE.txt:
* Source/WebCore/platform/graphics/egl/GLContext.cpp:
(WebCore::GLContext::glExtensions const):
* Source/WebCore/platform/graphics/egl/GLContext.h:
* Source/WebCore/platform/graphics/egl/GLFence.cpp: Added.
(WebCore::GLFence::create):
(WebCore::GLFence::GLFence):
(WebCore::GLFence::~GLFence):
(WebCore::GLFence::wait):
* Source/WebCore/platform/graphics/egl/GLFence.h: Added.
* Source/WebCore/platform/graphics/nicosia/NicosiaBuffer.cpp:
(Nicosia::AcceleratedBuffer::beginPainting):
(Nicosia::AcceleratedBuffer::completePainting):
(Nicosia::AcceleratedBuffer::waitUntilPaintingComplete):
(Nicosia::AcceleratedBuffer::~AcceleratedBuffer): Deleted.
* Source/WebCore/platform/graphics/nicosia/NicosiaBuffer.h:

Canonical link: https://commits.webkit.org/276463@main
  • Loading branch information
carlosgcampos committed Mar 21, 2024
1 parent 9b4ec97 commit c4b3e26
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 18 deletions.
1 change: 1 addition & 0 deletions Source/WebCore/Headers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2105,6 +2105,7 @@ set(WebCore_PRIVATE_FRAMEWORK_HEADERS

platform/graphics/egl/GLContext.h
platform/graphics/egl/GLContextWrapper.h
platform/graphics/egl/GLFence.h

platform/graphics/filters/DistantLightSource.h
platform/graphics/filters/FEBlend.h
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/PlatformPlayStation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ list(APPEND WebCore_SOURCES
platform/graphics/egl/GLContext.cpp
platform/graphics/egl/GLContextLibWPE.cpp
platform/graphics/egl/GLContextWrapper.cpp
platform/graphics/egl/GLFence.cpp

platform/graphics/libwpe/PlatformDisplayLibWPE.cpp

Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/SourcesGTK.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ platform/graphics/angle/PlatformDisplayANGLE.cpp @no-unify

platform/graphics/egl/GLContext.cpp @no-unify
platform/graphics/egl/GLContextWrapper.cpp @no-unify
platform/graphics/egl/GLFence.cpp @no-unify
platform/graphics/egl/PlatformDisplaySurfaceless.cpp @no-unify

platform/graphics/gbm/GBMBufferSwapchain.cpp
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/SourcesWPE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ platform/graphics/wpe/SystemFontDatabaseWPE.cpp
platform/graphics/egl/GLContext.cpp @no-unify
platform/graphics/egl/GLContextLibWPE.cpp @no-unify
platform/graphics/egl/GLContextWrapper.cpp @no-unify
platform/graphics/egl/GLFence.cpp @no-unify
platform/graphics/egl/PlatformDisplaySurfaceless.cpp @no-unify

platform/graphics/gbm/GBMBufferSwapchain.cpp
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/platform/graphics/egl/GLContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ const GLContext::GLExtensions& GLContext::glExtensions() const
const char* extensionsString = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
m_glExtensions.OES_texture_npot = isExtensionSupported(extensionsString, "GL_OES_texture_npot");
m_glExtensions.EXT_unpack_subimage = isExtensionSupported(extensionsString, "GL_EXT_unpack_subimage");
m_glExtensions.APPLE_sync = isExtensionSupported(extensionsString, "GL_APPLE_sync");

This comment has been minimized.

Copy link
@aperezdc

aperezdc Mar 21, 2024

Contributor

Maybe we would want to support EGL_KHR_fence_sync (when GL_OES_EGL_sync is also present) as well.

});
return m_glExtensions;
}
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/platform/graphics/egl/GLContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class GLContext final : public GLContextWrapper {
struct GLExtensions {
bool OES_texture_npot { false };
bool EXT_unpack_subimage { false };
bool APPLE_sync { false };
};
const GLExtensions& glExtensions() const;

Expand Down
65 changes: 65 additions & 0 deletions Source/WebCore/platform/graphics/egl/GLFence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2024 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

#include "config.h"
#include "GLFence.h"

#include "GLContext.h"

#if USE(LIBEPOXY)
#include <epoxy/gl.h>
#else
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#endif

namespace WebCore {

std::unique_ptr<GLFence> GLFence::create()
{
auto* context = GLContext::current();
if (!context)
return nullptr;

if (context->version() >= 300 || context->glExtensions().APPLE_sync) {
if (auto* sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0))
return makeUnique<GLFence>(sync);
return nullptr;
}

return nullptr;
}

GLFence::GLFence(GLsync sync)
: m_sync(sync)
{
}

GLFence::~GLFence()
{
if (m_sync)
glDeleteSync(m_sync);
}

unsigned GLFence::wait(FlushCommands flushCommands)
{
return glClientWaitSync(m_sync, flushCommands == FlushCommands::Yes ? GL_SYNC_FLUSH_COMMANDS_BIT : 0, GL_TIMEOUT_IGNORED);
}

} // namespace WebCore
44 changes: 44 additions & 0 deletions Source/WebCore/platform/graphics/egl/GLFence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2024 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

#pragma once

#include <wtf/FastMalloc.h>
#include <wtf/Noncopyable.h>

typedef struct __GLsync* GLsync;

namespace WebCore {

class GLFence {
WTF_MAKE_NONCOPYABLE(GLFence);
WTF_MAKE_FAST_ALLOCATED;
public:
WEBCORE_EXPORT static std::unique_ptr<GLFence> create();
explicit GLFence(GLsync);
~GLFence();

enum class FlushCommands : bool { No, Yes };
unsigned wait(FlushCommands);

private:
GLsync m_sync;
};

} // namespace WebCore
25 changes: 10 additions & 15 deletions Source/WebCore/platform/graphics/nicosia/NicosiaBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#if USE(SKIA)
#include "FontRenderOptions.h"
#include "GLFence.h"
#include "PlatformDisplay.h"
#include <skia/core/SkCanvas.h>
#include <skia/core/SkImage.h>
Expand Down Expand Up @@ -140,11 +141,7 @@ AcceleratedBuffer::AcceleratedBuffer(sk_sp<SkSurface>&& surface, Flags flags)
m_surface = WTFMove(surface);
}

AcceleratedBuffer::~AcceleratedBuffer()
{
if (m_sync)
glDeleteSync(m_sync);
}
AcceleratedBuffer::~AcceleratedBuffer() = default;

WebCore::IntSize AcceleratedBuffer::size() const
{
Expand All @@ -153,11 +150,6 @@ WebCore::IntSize AcceleratedBuffer::size() const

void AcceleratedBuffer::beginPainting()
{
if (m_sync) {
glDeleteSync(m_sync);
m_sync = nullptr;
}

m_surface->getCanvas()->save();
m_surface->getCanvas()->clear(SkColors::kTransparent);
}
Expand All @@ -167,9 +159,9 @@ void AcceleratedBuffer::completePainting()
m_surface->getCanvas()->restore();

auto* grContext = WebCore::PlatformDisplay::sharedDisplayForCompositing().skiaGrContext();
// FIXME: check if glFence is available and if not pass GrSyncCpu::kYes.
grContext->flushAndSubmit(m_surface.get(), GrSyncCpu::kNo);
m_sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
grContext->flush(m_surface.get());
m_fence = WebCore::GLFence::create();
grContext->submit(m_fence ? GrSyncCpu::kNo : GrSyncCpu::kYes);

auto texture = SkSurfaces::GetBackendTexture(m_surface.get(), SkSurface::BackendHandleAccess::kFlushRead);
ASSERT(texture.isValid());
Expand All @@ -182,8 +174,11 @@ void AcceleratedBuffer::completePainting()

void AcceleratedBuffer::waitUntilPaintingComplete()
{
if (m_sync)
glClientWaitSync(m_sync, 0, GL_TIMEOUT_IGNORED);
if (!m_fence)
return;

m_fence->wait(WebCore::GLFence::FlushCommands::No);
m_fence = nullptr;
}
#endif

Expand Down
8 changes: 5 additions & 3 deletions Source/WebCore/platform/graphics/nicosia/NicosiaBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
IGNORE_CLANG_WARNINGS_BEGIN("cast-align")
#include <skia/core/SkSurface.h>
IGNORE_CLANG_WARNINGS_END

typedef struct __GLsync* GLsync;
#endif

namespace WebCore {
class GLFence;
}

namespace Nicosia {

class Buffer : public ThreadSafeRefCounted<Buffer> {
Expand Down Expand Up @@ -139,7 +141,7 @@ class AcceleratedBuffer final : public Buffer {
void waitUntilPaintingComplete() final;

unsigned m_textureID { 0 };
GLsync m_sync { nullptr };
std::unique_ptr<WebCore::GLFence> m_fence;
};
#endif

Expand Down

0 comments on commit c4b3e26

Please sign in to comment.