Skip to content

Commit 0e21750

Browse files
committed
[Skia] Use CoordinatedPlatformLayerBufferNativeImage in ImageBufferSkiaAcceleratedBackend
https://bugs.webkit.org/show_bug.cgi?id=279437 Reviewed by Miguel Gomez. It simplifies the code and avoids the context switch to copy the texture. * Source/WebCore/platform/graphics/skia/ImageBufferSkiaAcceleratedBackend.cpp: (WebCore::ImageBufferSkiaAcceleratedBackend::ImageBufferSkiaAcceleratedBackend): (ImageBufferSkiaAcceleratedBackend::~ImageBufferSkiaAcceleratedBackend): (ImageBufferSkiaAcceleratedBackend::copyNativeImage): (ImageBufferSkiaAcceleratedBackend::createNativeImageReference): (ImageBufferSkiaAcceleratedBackend::layerContentsDisplayDelegate const): (WebCore::ImageBufferSkiaAcceleratedBackend::~ImageBufferSkiaAcceleratedBackend): Deleted. (WebCore::flushSurfaceIfNeeded): Deleted. (WebCore::ImageBufferSkiaAcceleratedBackend::copyNativeImage): Deleted. (WebCore::ImageBufferSkiaAcceleratedBackend::createNativeImageReference): Deleted. (WebCore::ImageBufferSkiaAcceleratedBackend::getPixelBuffer): Deleted. (WebCore::ImageBufferSkiaAcceleratedBackend::putPixelBuffer): Deleted. (WebCore::ImageBufferSkiaAcceleratedBackend::layerContentsDisplayDelegate const): Deleted. (WebCore::ImageBufferSkiaAcceleratedBackend::swapBuffersIfNeeded): Deleted. * Source/WebCore/platform/graphics/skia/ImageBufferSkiaAcceleratedBackend.h: Canonical link: https://commits.webkit.org/283460@main
1 parent b09245a commit 0e21750

2 files changed

Lines changed: 17 additions & 65 deletions

File tree

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

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#if USE(COORDINATED_GRAPHICS)
4444
#include "BitmapTexture.h"
45+
#include "CoordinatedPlatformLayerBufferNativeImage.h"
4546
#include "CoordinatedPlatformLayerBufferRGB.h"
4647
#include "GLFence.h"
4748
#include "GraphicsLayerContentsDisplayDelegateTextureMapper.h"
@@ -89,15 +90,15 @@ ImageBufferSkiaAcceleratedBackend::ImageBufferSkiaAcceleratedBackend(const Param
8990
if (parameters.purpose == RenderingPurpose::Canvas) {
9091
auto proxy = TextureMapperPlatformLayerProxyGL::create(TextureMapperPlatformLayerProxy::ContentType::Canvas);
9192
proxy->setSwapBuffersFunction([this](TextureMapperPlatformLayerProxy& proxy) {
92-
if (!swapBuffersIfNeeded())
93-
return;
94-
95-
auto fence = GLFence::create();
96-
if (!PlatformDisplay::sharedDisplay().skiaGLContext()->makeContextCurrent())
93+
auto image = createNativeImageReference();
94+
if (!image)
9795
return;
9896

9997
Locker locker { proxy.lock() };
100-
auto layerBuffer = CoordinatedPlatformLayerBufferRGB::create(m_texture.front->id(), m_texture.front->size(), { TextureMapperFlags::ShouldBlend }, WTFMove(fence));
98+
OptionSet<TextureMapperFlags> flags;
99+
if (image->hasAlpha())
100+
flags.add(TextureMapperFlags::ShouldBlend);
101+
auto layerBuffer = CoordinatedPlatformLayerBufferNativeImage::create(image.releaseNonNull(), flags, GLFence::create());
101102
downcast<TextureMapperPlatformLayerProxyGL>(proxy).pushNextBuffer(WTFMove(layerBuffer));
102103
});
103104
m_layerContentsDisplayDelegate = GraphicsLayerContentsDisplayDelegateTextureMapper::create(WTFMove(proxy));
@@ -110,38 +111,26 @@ ImageBufferSkiaAcceleratedBackend::~ImageBufferSkiaAcceleratedBackend()
110111
#if USE(COORDINATED_GRAPHICS)
111112
if (m_layerContentsDisplayDelegate)
112113
static_cast<GraphicsLayerContentsDisplayDelegateTextureMapper*>(m_layerContentsDisplayDelegate.get())->proxy().setSwapBuffersFunction(nullptr);
113-
if (m_texture.back || m_texture.front) {
114-
GLContext::ScopedGLContextCurrent scopedContext(*PlatformDisplay::sharedDisplay().sharingGLContext());
115-
m_texture.back = nullptr;
116-
m_texture.front = nullptr;
117-
}
118114
#endif
119115
}
120116

121-
void flushSurfaceIfNeeded(SkSurface* surface)
117+
RefPtr<NativeImage> ImageBufferSkiaAcceleratedBackend::copyNativeImage()
118+
{
119+
// SkSurface uses a copy-on-write mechanism for makeImageSnapshot(), so it's
120+
// always safe to return the SkImage without copying.
121+
return createNativeImageReference();
122+
}
123+
124+
RefPtr<NativeImage> ImageBufferSkiaAcceleratedBackend::createNativeImageReference()
122125
{
123126
// If we're using MSAA, we need to flush the surface before calling makeImageSnapshot(),
124127
// because that call doesn't force the MSAA resolution, which can produce outdated results
125128
// in the resulting SkImage.
126129
auto& display = PlatformDisplay::sharedDisplay();
127130
if (display.msaaSampleCount() > 0) {
128-
auto* glContext = display.skiaGLContext();
129-
if (!glContext || !glContext->makeContextCurrent())
130-
return;
131-
display.skiaGrContext()->flush(surface);
131+
if (display.skiaGLContext()->makeContextCurrent())
132+
display.skiaGrContext()->flush(m_surface.get());
132133
}
133-
}
134-
135-
RefPtr<NativeImage> ImageBufferSkiaAcceleratedBackend::copyNativeImage()
136-
{
137-
// FIXME: do we have to do a explicit copy here?
138-
flushSurfaceIfNeeded(m_surface.get());
139-
return NativeImage::create(m_surface->makeImageSnapshot());
140-
}
141-
142-
RefPtr<NativeImage> ImageBufferSkiaAcceleratedBackend::createNativeImageReference()
143-
{
144-
flushSurfaceIfNeeded(m_surface.get());
145134
return NativeImage::create(m_surface->makeImageSnapshot());
146135
}
147136

@@ -182,38 +171,6 @@ RefPtr<GraphicsLayerContentsDisplayDelegate> ImageBufferSkiaAcceleratedBackend::
182171
{
183172
return m_layerContentsDisplayDelegate;
184173
}
185-
186-
bool ImageBufferSkiaAcceleratedBackend::swapBuffersIfNeeded()
187-
{
188-
auto& display = PlatformDisplay::sharedDisplay();
189-
if (!display.skiaGLContext()->makeContextCurrent())
190-
return false;
191-
192-
auto* grContext = display.skiaGrContext();
193-
RELEASE_ASSERT(grContext);
194-
grContext->flushAndSubmit(m_surface.get(), GLFence::isSupported() ? GrSyncCpu::kNo : GrSyncCpu::kYes);
195-
196-
auto texture = SkSurfaces::GetBackendTexture(m_surface.get(), SkSurface::BackendHandleAccess::kFlushRead);
197-
ASSERT(texture.isValid());
198-
GrGLTextureInfo textureInfo;
199-
bool retrievedTextureInfo = GrBackendTextures::GetGLTextureInfo(texture, &textureInfo);
200-
ASSERT_UNUSED(retrievedTextureInfo, retrievedTextureInfo);
201-
std::unique_ptr<GLFence> fence = GLFence::create();
202-
203-
// Switch to the sharing context for the texture copy.
204-
if (!display.sharingGLContext()->makeContextCurrent())
205-
return false;
206-
207-
auto info = m_surface->imageInfo();
208-
IntSize textureSize(info.width(), info.height());
209-
if (!m_texture.back)
210-
m_texture.back = BitmapTexture::create(textureSize, BitmapTexture::Flags::SupportsAlpha);
211-
fence->serverWait();
212-
m_texture.back->copyFromExternalTexture(textureInfo.fID);
213-
std::swap(m_texture.back, m_texture.front);
214-
215-
return true;
216-
}
217174
#endif
218175

219176
} // namespace WebCore

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,8 @@ class ImageBufferSkiaAcceleratedBackend final : public ImageBufferSkiaSurfaceBac
5656

5757
#if USE(COORDINATED_GRAPHICS)
5858
RefPtr<GraphicsLayerContentsDisplayDelegate> layerContentsDisplayDelegate() const final;
59-
bool swapBuffersIfNeeded();
6059

6160
RefPtr<GraphicsLayerContentsDisplayDelegate> m_layerContentsDisplayDelegate;
62-
struct {
63-
RefPtr<BitmapTexture> back;
64-
RefPtr<BitmapTexture> front;
65-
} m_texture;
6661
#endif
6762
};
6863

0 commit comments

Comments
 (0)