Skip to content

Commit 1bd3d2f

Browse files
committed
[WPE][Skia] Enable MSAA when available
https://bugs.webkit.org/show_bug.cgi?id=277800 Reviewed by Carlos Garcia Campos. Enable MSAA on WPE whenever possible. On x86 or x86_64, use eight samples to get an antialiasing result similar to non MSAA. On embedded devices, use four samples instead, as this is a nice compromise between quality and resource usage. On GTK keep it disabled by default. The user can overwrite this default value through the WEBKIT_MSAA_SAMPLE_COUNT env var. Even if MSAA is reported as available by GLES, Skia has a lot of internal checks that may disable it, so the best way to check whether we can use it with Skia is trying to create a texture backed SkSurface with a sample count of four and check whether it works. * Source/WebCore/platform/graphics/PlatformDisplay.h: * Source/WebCore/platform/graphics/skia/ImageBufferSkiaAcceleratedBackend.cpp: (WebCore::ImageBufferSkiaAcceleratedBackend::create): (WebCore::flushSurfaceIfNeeded): (WebCore::ImageBufferSkiaAcceleratedBackend::copyNativeImage): (WebCore::ImageBufferSkiaAcceleratedBackend::createNativeImageReference): * Source/WebCore/platform/graphics/skia/PlatformDisplaySkia.cpp: (WebCore::initializeMSAASampleCount): (WebCore::SkiaGLContext::sampleCount): (WebCore::SkiaGLContext::SkiaGLContext): (WebCore::PlatformDisplay::msaaSampleCount): * Source/WebCore/platform/graphics/skia/SkiaAcceleratedBufferPool.cpp: (WebCore::SkiaAcceleratedBufferPool::createAcceleratedBuffer): Canonical link: https://commits.webkit.org/282223@main
1 parent 5a46936 commit 1bd3d2f

4 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎Source/WebCore/platform/graphics/PlatformDisplay.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class PlatformDisplay {
145145
#if USE(SKIA)
146146
GLContext* skiaGLContext();
147147
GrDirectContext* skiaGrContext();
148+
unsigned msaaSampleCount();
148149
#endif
149150

150151
#if USE(ATSPI)

‎Source/WebCore/platform/graphics/skia/ImageBufferSkiaAcceleratedBackend.cpp‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ std::unique_ptr<ImageBufferSkiaAcceleratedBackend> ImageBufferSkiaAcceleratedBac
7474
RELEASE_ASSERT(grContext);
7575
auto imageInfo = SkImageInfo::Make(backendSize.width(), backendSize.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType, parameters.colorSpace.platformColorSpace());
7676
SkSurfaceProps properties = { 0, FontRenderOptions::singleton().subpixelOrder() };
77-
auto surface = SkSurfaces::RenderTarget(grContext, skgpu::Budgeted::kNo, imageInfo, 0, kTopLeft_GrSurfaceOrigin, &properties);
77+
auto surface = SkSurfaces::RenderTarget(grContext, skgpu::Budgeted::kNo, imageInfo, PlatformDisplay::sharedDisplay().msaaSampleCount(), kTopLeft_GrSurfaceOrigin, &properties);
7878
if (!surface || !surface->getCanvas())
7979
return nullptr;
8080

@@ -107,14 +107,30 @@ ImageBufferSkiaAcceleratedBackend::~ImageBufferSkiaAcceleratedBackend()
107107
#endif
108108
}
109109

110+
void flushSurfaceIfNeeded(SkSurface* surface)
111+
{
112+
// If we're using MSAA, we need to flush the surface before calling makeImageSnapshot(),
113+
// because that call doesn't force the MSAA resolution, which can produce outdated results
114+
// in the resulting SkImage.
115+
auto& display = PlatformDisplay::sharedDisplay();
116+
if (display.msaaSampleCount() > 0) {
117+
auto* glContext = display.skiaGLContext();
118+
if (!glContext || !glContext->makeContextCurrent())
119+
return;
120+
display.skiaGrContext()->flush(surface);
121+
}
122+
}
123+
110124
RefPtr<NativeImage> ImageBufferSkiaAcceleratedBackend::copyNativeImage()
111125
{
112126
// FIXME: do we have to do a explicit copy here?
127+
flushSurfaceIfNeeded(m_surface.get());
113128
return NativeImage::create(m_surface->makeImageSnapshot());
114129
}
115130

116131
RefPtr<NativeImage> ImageBufferSkiaAcceleratedBackend::createNativeImageReference()
117132
{
133+
flushSurfaceIfNeeded(m_surface.get());
118134
return NativeImage::create(m_surface->makeImageSnapshot());
119135
}
120136

‎Source/WebCore/platform/graphics/skia/PlatformDisplaySkia.cpp‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
#include "PlatformDisplay.h"
2828

2929
#if USE(SKIA)
30+
#include "FontRenderOptions.h"
3031
#include "GLContext.h"
32+
#include <skia/core/SkColorSpace.h>
3133
#include <skia/gpu/GrBackendSurface.h>
3234
#include <skia/gpu/ganesh/gl/GrGLBackendSurface.h>
3335
#include <skia/gpu/ganesh/gl/GrGLDirectContext.h>
@@ -36,6 +38,7 @@
3638
#include <wtf/NeverDestroyed.h>
3739
#include <wtf/RunLoop.h>
3840
#include <wtf/ThreadSafeWeakPtr.h>
41+
#include <wtf/text/StringToIntegerConversion.h>
3942
#include <wtf/threads/BinarySemaphore.h>
4043

4144
IGNORE_CLANG_WARNINGS_BEGIN("cast-align")
@@ -50,6 +53,21 @@ IGNORE_CLANG_WARNINGS_END
5053

5154
namespace WebCore {
5255

56+
#if PLATFORM(WPE)
57+
#if CPU(X86) || CPU(X86_64)
58+
// On x86 ot x86_64 we need at least 8 samples for the antialiasing result to be similar
59+
// to non MSAA.
60+
static const unsigned s_defaultSampleCount = 8;
61+
#else
62+
// On embedded, we sacrifice a bit of antialiasing quality to save memory and improve
63+
// performance.
64+
static const unsigned s_defaultSampleCount = 4;
65+
#endif
66+
#else
67+
// Disable MSAA by default.
68+
static const unsigned s_defaultSampleCount = 0;
69+
#endif
70+
5371
#if !(PLATFORM(PLAYSTATION) && USE(COORDINATED_GRAPHICS))
5472
static sk_sp<const GrGLInterface> skiaGLInterface()
5573
{
@@ -66,6 +84,32 @@ static sk_sp<const GrGLInterface> skiaGLInterface()
6684

6785
static thread_local RefPtr<SkiaGLContext> s_skiaGLContext;
6886

87+
unsigned initializeMSAASampleCount(GrDirectContext* grContext)
88+
{
89+
static std::once_flag onceFlag;
90+
static int sampleCount = s_defaultSampleCount;
91+
92+
std::call_once(onceFlag, [grContext] {
93+
// Let the user override the default sample count if they want to.
94+
String envString = String::fromLatin1(getenv("WEBKIT_MSAA_SAMPLE_COUNT"));
95+
if (!envString.isEmpty())
96+
sampleCount = parseInteger<unsigned>(envString).value_or(0);
97+
98+
// Skia checks internally whether MSAA is supported, but also disables it for several platforms where it
99+
// knows there are bugs. The only way to know whether our sample count will work is trying to create a
100+
// surface with that value and check whether it works.
101+
auto imageInfo = SkImageInfo::Make(512, 512, kRGBA_8888_SkColorType, kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
102+
SkSurfaceProps properties = { 0, FontRenderOptions::singleton().subpixelOrder() };
103+
auto surface = SkSurfaces::RenderTarget(grContext, skgpu::Budgeted::kNo, imageInfo, sampleCount, kTopLeft_GrSurfaceOrigin, &properties);
104+
105+
// If the creation of the surface failed, disable MSAA.
106+
if (!surface)
107+
sampleCount = 0;
108+
});
109+
110+
return sampleCount;
111+
}
112+
69113
class SkiaGLContext : public ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr<SkiaGLContext> {
70114
public:
71115
static Ref<SkiaGLContext> create(PlatformDisplay& display)
@@ -102,6 +146,11 @@ class SkiaGLContext : public ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr<Ski
102146
return m_skiaGrContext.get();
103147
}
104148

149+
unsigned sampleCount()
150+
{
151+
return m_sampleCount;
152+
}
153+
105154
private:
106155
explicit SkiaGLContext(PlatformDisplay& display)
107156
: m_runLoop(&RunLoop::current())
@@ -115,6 +164,8 @@ class SkiaGLContext : public ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr<Ski
115164
m_skiaGLContext = WTFMove(glContext);
116165
m_skiaGrContext = WTFMove(grContext);
117166
}
167+
168+
m_sampleCount = initializeMSAASampleCount(m_skiaGrContext.get());
118169
}
119170

120171
void invalidateOnCurrentThread()
@@ -128,6 +179,7 @@ class SkiaGLContext : public ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr<Ski
128179
std::unique_ptr<GLContext> m_skiaGLContext WTF_GUARDED_BY_LOCK(m_lock);
129180
sk_sp<GrDirectContext> m_skiaGrContext WTF_GUARDED_BY_LOCK(m_lock);
130181
mutable Lock m_lock;
182+
unsigned m_sampleCount { 0 };
131183
};
132184
#endif
133185

@@ -152,6 +204,11 @@ GrDirectContext* PlatformDisplay::skiaGrContext()
152204
return s_skiaGLContext->skiaGrContext();
153205
}
154206

207+
unsigned PlatformDisplay::msaaSampleCount()
208+
{
209+
return s_skiaGLContext->sampleCount();
210+
}
211+
155212
void PlatformDisplay::invalidateSkiaGLContexts()
156213
{
157214
auto contexts = WTFMove(m_skiaGLContexts);

‎Source/WebCore/platform/graphics/skia/SkiaAcceleratedBufferPool.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ RefPtr<Nicosia::Buffer> SkiaAcceleratedBufferPool::createAcceleratedBuffer(const
7373
RELEASE_ASSERT(grContext);
7474
auto imageInfo = SkImageInfo::Make(size.width(), size.height(), kRGBA_8888_SkColorType, kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
7575
SkSurfaceProps properties = { 0, FontRenderOptions::singleton().subpixelOrder() };
76-
auto surface = SkSurfaces::RenderTarget(grContext, skgpu::Budgeted::kNo, imageInfo, 0, kTopLeft_GrSurfaceOrigin, &properties);
76+
auto surface = SkSurfaces::RenderTarget(grContext, skgpu::Budgeted::kNo, imageInfo, PlatformDisplay::sharedDisplay().msaaSampleCount(), kTopLeft_GrSurfaceOrigin, &properties);
7777
if (!surface)
7878
return nullptr;
7979
return Nicosia::AcceleratedBuffer::create(WTFMove(surface), supportsAlpha ? Nicosia::Buffer::SupportsAlpha : Nicosia::Buffer::NoFlags);

0 commit comments

Comments
 (0)