Skip to content

Commit 8ba6cec

Browse files
[Skia] Fix GrDirectContext management for GPU resources
https://bugs.webkit.org/show_bug.cgi?id=304568 Reviewed by Carlos Garcia Campos. WebKit stores GrDirectContext instances in thread_local statics within PlatformDisplay. Operations on GPU-backed resources must use the context that created them, not the current thread's context obtained via PlatformDisplay::sharedDisplay().skiaGrContext(). The GL contexts used are sharing contexts, also stored in thread_local statics. Therefore we can safely make the current thread's GL context current before flushing with the correct GrDirectContext. For NativeImage, store the GrDirectContext at creation time and access it via grContext(). The following places previously used the shared display's context incorrectly: - GraphicsContextGLSkia::extractImage() for readPixels on texture-backed images - VideoFrameGStreamer::fromNativeImage() for readPixels - NativeImageSkia::singlePixelSolidColor() for readPixels - ImageBufferUtilitiesSkia::encodeAcceleratedImage() for encoding - CoordinatedPlatformLayerBufferNativeImage for flushAndSubmit Storing a pointer to GrDirectContext is safe, as the GrDirectContext lives as long as PlatformDisplay::clearGLContexts() is called, which only happens upon process destruction for the Gtk/WPE ports. For SkSurface, use surface->recordingContext()->asDirectContext() to obtain the surface's own context. The following places previously used the shared display's context incorrectly: - ImageBufferSkiaAcceleratedBackend::createNativeImageReference() for flush - CoordinatedTileBuffer::completePainting() for flushAndSubmit Note: When checking a SkImage for validity to decide if we need texture rewrapping, we need to use the destination context (from current thread) not the source image's context. Covered by existing tests (when run with SKIA_DEBUG=ON builds). We cannot yet turn on SKIA_DEBUG in our debug builds, as there are other kind of assertions that need to be fixed first. * Source/WebCore/platform/graphics/ImageBuffer.cpp: (WebCore::ImageBuffer::toData): * Source/WebCore/platform/graphics/NativeImage.cpp: * Source/WebCore/platform/graphics/NativeImage.h: (WebCore::NativeImage::grContext const): * Source/WebCore/platform/graphics/gstreamer/VideoFrameGStreamer.cpp: (WebCore::VideoFrame::fromNativeImage): * Source/WebCore/platform/graphics/skia/GraphicsContextGLSkia.cpp: (WebCore::GraphicsContextGLImageExtractor::extractImage): * Source/WebCore/platform/graphics/skia/GraphicsContextSkia.cpp: (WebCore::GraphicsContextSkia::drawNativeImage): (WebCore::GraphicsContextSkia::setupFillSource): (WebCore::GraphicsContextSkia::setupStrokeSource): (WebCore::GraphicsContextSkia::clipToImageBuffer): (WebCore::GraphicsContextSkia::drawPattern): (WebCore::createFenceAfterFlush): (WebCore::GraphicsContextSkia::createAcceleratedRenderingFence): (WebCore::GraphicsContextSkia::trackAcceleratedRenderingFenceIfNeeded): (WebCore::createAcceleratedRenderingFenceInternal): Deleted. * Source/WebCore/platform/graphics/skia/GraphicsContextSkia.h: * Source/WebCore/platform/graphics/skia/ImageBufferSkiaAcceleratedBackend.cpp: (WebCore::ImageBufferSkiaAcceleratedBackend::createNativeImageReference): * Source/WebCore/platform/graphics/skia/ImageBufferUtilitiesSkia.cpp: (WebCore::encodeAcceleratedImage): (WebCore::encodeData): * Source/WebCore/platform/graphics/skia/ImageBufferUtilitiesSkia.h: * Source/WebCore/platform/graphics/skia/NativeImageSkia.cpp: (WebCore::NativeImage::create): (WebCore::NativeImage::createTransient): (WebCore::NativeImage::NativeImage): (WebCore::NativeImage::singlePixelSolidColor const): * Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedPlatformLayerBufferNativeImage.cpp: (WebCore::CoordinatedPlatformLayerBufferNativeImage::CoordinatedPlatformLayerBufferNativeImage): * Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedTileBuffer.cpp: (WebCore::CoordinatedAcceleratedTileBuffer::completePainting): Canonical link: https://commits.webkit.org/304898@main
1 parent 02d6be0 commit 8ba6cec

13 files changed

+132
-55
lines changed

‎Source/WebCore/platform/graphics/ImageBuffer.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,11 @@ Vector<uint8_t> ImageBuffer::toData(Ref<ImageBuffer> source, const String& mimeT
497497
RefPtr<NativeImage> image = MIMETypeRegistry::isJPEGMIMEType(mimeType) ? copyImageBufferToOpaqueNativeImage(WTF::move(source), preserveResolution) : copyImageBufferToNativeImage(WTF::move(source), DontCopyBackingStore, preserveResolution);
498498
if (!image)
499499
return { };
500+
#if USE(SKIA)
501+
return encodeData(*image, mimeType, quality);
502+
#elif USE(CG) || USE(CAIRO)
500503
return encodeData(image->platformImage().get(), mimeType, quality);
504+
#endif
501505
}
502506

503507
RefPtr<PixelBuffer> ImageBuffer::getPixelBuffer(const PixelBufferFormat& destinationFormat, const IntRect& sourceRect, const ImageBufferAllocator& allocator) const

‎Source/WebCore/platform/graphics/NativeImage.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace WebCore {
3636

3737
WTF_MAKE_TZONE_ALLOCATED_IMPL(NativeImage);
3838

39-
#if !USE(CG)
39+
#if !USE(CG) && !USE(SKIA)
4040
RefPtr<NativeImage> NativeImage::create(PlatformImagePtr&& platformImage)
4141
{
4242
if (!platformImage)
@@ -50,11 +50,13 @@ RefPtr<NativeImage> NativeImage::createTransient(PlatformImagePtr&& image)
5050
}
5151
#endif
5252

53+
#if !USE(SKIA)
5354
NativeImage::NativeImage(PlatformImagePtr&& platformImage)
5455
: m_platformImage(WTF::move(platformImage))
5556
{
5657
computeHeadroom();
5758
}
59+
#endif
5860

5961
NativeImage::~NativeImage()
6062
{

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#include <wtf/TZoneMalloc.h>
3636
#include <wtf/UniqueRef.h>
3737

38+
#if USE(SKIA)
39+
class GrDirectContext;
40+
#endif
41+
3842
namespace WebCore {
3943

4044
class Color;
@@ -49,9 +53,15 @@ class NativeImage : public ThreadSafeRefCounted<NativeImage>, public CanMakeThre
4953
WTF_MAKE_TZONE_ALLOCATED(NativeImage);
5054
WTF_OVERRIDE_DELETE_FOR_CHECKED_PTR(NativeImage);
5155
public:
56+
#if USE(SKIA)
57+
static WEBCORE_EXPORT RefPtr<NativeImage> create(PlatformImagePtr&&, GrDirectContext* = nullptr);
58+
// Creates a NativeImage that is intended to be drawn once or only few times. Signals the platform to avoid generating any caches for the image.
59+
static WEBCORE_EXPORT RefPtr<NativeImage> createTransient(PlatformImagePtr&&, GrDirectContext* = nullptr);
60+
#else
5261
static WEBCORE_EXPORT RefPtr<NativeImage> create(PlatformImagePtr&&);
5362
// Creates a NativeImage that is intended to be drawn once or only few times. Signals the platform to avoid generating any caches for the image.
5463
static WEBCORE_EXPORT RefPtr<NativeImage> createTransient(PlatformImagePtr&&);
64+
#endif
5565

5666
WEBCORE_EXPORT virtual ~NativeImage();
5767

@@ -71,6 +81,10 @@ class NativeImage : public ThreadSafeRefCounted<NativeImage>, public CanMakeThre
7181
uint64_t uniqueID() const;
7282
#endif
7383

84+
#if USE(SKIA)
85+
GrDirectContext* grContext() const { return m_grContext; }
86+
#endif
87+
7488
void addObserver(WeakRef<RenderingResourceObserver>&& observer)
7589
{
7690
m_observers.add(WTF::move(observer));
@@ -82,14 +96,21 @@ class NativeImage : public ThreadSafeRefCounted<NativeImage>, public CanMakeThre
8296
}
8397

8498
protected:
99+
#if USE(SKIA)
100+
WEBCORE_EXPORT NativeImage(PlatformImagePtr&&, GrDirectContext* = nullptr);
101+
#else
85102
WEBCORE_EXPORT NativeImage(PlatformImagePtr&&);
103+
#endif
86104

87105
void computeHeadroom();
88106

89107
mutable PlatformImagePtr m_platformImage;
90108
mutable Headroom m_headroom { Headroom::None };
91109
mutable WeakHashSet<RenderingResourceObserver> m_observers;
92110
RenderingResourceIdentifier m_renderingResourceIdentifier { RenderingResourceIdentifier::generate() };
111+
#if USE(SKIA)
112+
GrDirectContext* m_grContext { nullptr };
113+
#endif
93114
};
94115

95116
} // namespace WebCore

‎Source/WebCore/platform/graphics/gstreamer/VideoFrameGStreamer.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ RefPtr<VideoFrame> VideoFrame::fromNativeImage(NativeImage& image)
145145
return nullptr;
146146

147147
auto data = SkData::MakeUninitialized(size);
148-
GrDirectContext* grContext = PlatformDisplay::sharedDisplay().skiaGrContext();
148+
auto* grContext = image.grContext();
149149
if (!platformImage->readPixels(grContext, imageInfo, static_cast<uint8_t*>(data->writable_data()), strides[0], 0, 0))
150150
return nullptr;
151151

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "BitmapImage.h"
3131
#include "GLContext.h"
3232
#include "GraphicsContextGLImageExtractor.h"
33+
#include "NativeImage.h"
3334
#include "NotImplemented.h"
3435
#include "PixelBuffer.h"
3536
#include "PlatformDisplay.h"
@@ -48,18 +49,22 @@ GraphicsContextGLImageExtractor::~GraphicsContextGLImageExtractor() = default;
4849

4950
bool GraphicsContextGLImageExtractor::extractImage(bool premultiplyAlpha, bool ignoreGammaAndColorProfile, bool ignoreNativeImageAlphaPremultiplication)
5051
{
51-
PlatformImagePtr platformImage;
52+
RefPtr<NativeImage> nativeImage;
5253
bool hasAlpha = !m_image->currentFrameKnownToBeOpaque();
5354
if ((ignoreGammaAndColorProfile || (hasAlpha && !premultiplyAlpha)) && m_image->data()) {
5455
auto image = BitmapImage::create(nullptr, AlphaOption::NotPremultiplied, ignoreGammaAndColorProfile ? GammaAndColorProfileOption::Ignored : GammaAndColorProfileOption::Applied);
5556
image->setData(m_image->data(), true);
5657
if (!image->frameCount())
5758
return false;
5859

59-
platformImage = image->currentNativeImage()->platformImage();
60+
nativeImage = image->currentNativeImage();
6061
} else
61-
platformImage = m_image->currentNativeImage()->platformImage();
62+
nativeImage = m_image->currentNativeImage();
6263

64+
if (!nativeImage)
65+
return false;
66+
67+
auto platformImage = nativeImage->platformImage();
6368
if (!platformImage)
6469
return false;
6570

@@ -101,7 +106,7 @@ bool GraphicsContextGLImageExtractor::extractImage(bool premultiplyAlpha, bool i
101106
if (!PlatformDisplay::sharedDisplay().skiaGLContext()->makeContextCurrent())
102107
return false;
103108

104-
GrDirectContext* grContext = PlatformDisplay::sharedDisplay().skiaGrContext();
109+
auto* grContext = nativeImage->grContext();
105110
if (!platformImage->readPixels(grContext, imageInfo, static_cast<uint8_t*>(data->writable_data()), bytesPerRow, 0, 0))
106111
return false;
107112

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

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "NativeImage.h"
3838
#include "NotImplemented.h"
3939
#include "PathSegment.h"
40+
#include "Pattern.h"
4041
#include "PlatformDisplay.h"
4142
#include "ProcessCapabilities.h"
4243
#include "SkiaPaintingEngine.h"
@@ -302,6 +303,9 @@ void GraphicsContextSkia::drawNativeImage(NativeImage& nativeImage, const FloatR
302303
if (image->isTextureBacked()) {
303304
auto* glContext = PlatformDisplay::sharedDisplay().skiaGLContext();
304305
if (glContext && glContext->makeContextCurrent()) {
306+
// Use the destination context (current thread's context), not nativeImage.grContext()
307+
// (source context). For cross-thread transfers, we must check validity against the
308+
// destination context and rewrap the texture for use in this context.
305309
auto* grContext = PlatformDisplay::sharedDisplay().skiaGrContext();
306310
RELEASE_ASSERT(grContext);
307311

@@ -310,7 +314,7 @@ void GraphicsContextSkia::drawNativeImage(NativeImage& nativeImage, const FloatR
310314
if (!image->isValid(grContext->asRecorder())) {
311315
// Ensure any pending GPU operations on the source image are complete before
312316
// accessing its backend texture for rewrapping.
313-
if (auto fence = createAcceleratedRenderingFence(image))
317+
if (auto fence = createAcceleratedRenderingFence(nativeImage.platformImage(), nativeImage.grContext()))
314318
fence->serverWait();
315319

316320
GrBackendTexture backendTexture;
@@ -333,13 +337,13 @@ void GraphicsContextSkia::drawNativeImage(NativeImage& nativeImage, const FloatR
333337
imageRasterCopy = imageInThisThread ? imageInThisThread->makeRasterImage() : image->makeRasterImage();
334338
imageForDrawing = imageRasterCopy.get();
335339
} else
336-
trackAcceleratedRenderingFenceIfNeeded(imageInThisThread ? imageInThisThread : image);
340+
trackAcceleratedRenderingFenceIfNeeded(imageInThisThread ? imageInThisThread : image, nativeImage.grContext());
337341
}
338342
inExtraTransparencyLayer = drawOutsetShadow(paint, [&](const SkPaint& paint) {
339343
m_canvas.drawImageRect(imageForDrawing, normalizedSrcRect, normalizedDestRect, toSkSamplingOptions(m_state.imageInterpolationQuality()), &paint, clampingConstraint);
340344
});
341345
} else
342-
trackAcceleratedRenderingFenceIfNeeded(imageInThisThread ? imageInThisThread : image);
346+
trackAcceleratedRenderingFenceIfNeeded(imageInThisThread ? imageInThisThread : image, nativeImage.grContext());
343347

344348
m_canvas.drawImageRect(imageForDrawing, normalizedSrcRect, normalizedDestRect, toSkSamplingOptions(m_state.imageInterpolationQuality()), &paint, clampingConstraint);
345349
if (inExtraTransparencyLayer)
@@ -611,7 +615,7 @@ void GraphicsContextSkia::setupFillSource(SkPaint& paint)
611615
if (auto fillPattern = fillBrush().pattern()) {
612616
paint.setShader(fillPattern->createPlatformPattern({ }, toSkSamplingOptions(imageInterpolationQuality())));
613617
paint.setAlphaf(alpha());
614-
trackAcceleratedRenderingFenceIfNeeded(paint);
618+
trackAcceleratedRenderingFenceIfNeeded(*fillPattern);
615619
} else if (auto fillGradient = fillBrush().gradient())
616620
paint.setShader(fillGradient->shader(alpha(), fillBrush().gradientSpaceTransform()));
617621
else
@@ -636,7 +640,7 @@ void GraphicsContextSkia::setupStrokeSource(SkPaint& paint)
636640
{
637641
if (auto strokePattern = strokeBrush().pattern()) {
638642
paint.setShader(strokePattern->createPlatformPattern({ }, toSkSamplingOptions(imageInterpolationQuality())));
639-
trackAcceleratedRenderingFenceIfNeeded(paint);
643+
trackAcceleratedRenderingFenceIfNeeded(*strokePattern);
640644
} else if (auto strokeGradient = strokeBrush().gradient())
641645
paint.setShader(strokeGradient->shader(alpha(), strokeBrush().gradientSpaceTransform()));
642646
else
@@ -719,7 +723,7 @@ void GraphicsContextSkia::clipToImageBuffer(ImageBuffer& buffer, const FloatRect
719723
{
720724
if (auto nativeImage = nativeImageForDrawing(buffer)) {
721725
auto image = nativeImage->platformImage();
722-
trackAcceleratedRenderingFenceIfNeeded(image);
726+
trackAcceleratedRenderingFenceIfNeeded(image, nativeImage->grContext());
723727
m_canvas.clipShader(image->makeShader(SkTileMode::kDecal, SkTileMode::kDecal, { }, SkMatrix::Translate(SkFloatToScalar(destRect.x()), SkFloatToScalar(destRect.y()))));
724728
}
725729
}
@@ -1098,7 +1102,7 @@ void GraphicsContextSkia::drawPattern(NativeImage& nativeImage, const FloatRect&
10981102
repeatY = imageSampledRect.y() < 0 || std::trunc(imageSampledRect.bottom()) > size.height();
10991103
}
11001104
paint.setShader(image->makeShader(repeatX ? SkTileMode::kRepeat : SkTileMode::kClamp, repeatY ? SkTileMode::kRepeat : SkTileMode::kClamp, samplingOptions, &shaderMatrix));
1101-
trackAcceleratedRenderingFenceIfNeeded(image);
1105+
trackAcceleratedRenderingFenceIfNeeded(image, nativeImage.grContext());
11021106
} else {
11031107
auto tileFloatRectWithSpacing = FloatRect(0, 0, tileRect.width() + spacing.width() / patternTransform.a(), tileRect.height() + spacing.height() / patternTransform.d());
11041108
if (image->isTextureBacked()) {
@@ -1107,9 +1111,11 @@ void GraphicsContextSkia::drawPattern(NativeImage& nativeImage, const FloatRect&
11071111
auto clipRect = enclosingIntRect(tileFloatRectWithSpacing);
11081112
if (auto surface = createAcceleratedSurface({ clipRect.width(), clipRect.height() })) {
11091113
surface->getCanvas()->drawImageRect(image, tileRect, dstRect, samplingOptions, nullptr, SkCanvas::kStrict_SrcRectConstraint);
1114+
auto* recordingContext = surface->recordingContext();
1115+
auto* grContext = recordingContext ? recordingContext->asDirectContext() : nullptr;
11101116
auto tileImage = surface->makeImageSnapshot();
11111117
paint.setShader(tileImage->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, samplingOptions, &shaderMatrix));
1112-
trackAcceleratedRenderingFenceIfNeeded(tileImage);
1118+
trackAcceleratedRenderingFenceIfNeeded(tileImage, grContext);
11131119
}
11141120
} else {
11151121
auto dstRect = SkRect::MakeWH(tileRect.width(), tileRect.height());
@@ -1140,18 +1146,8 @@ SkiaImageToFenceMap GraphicsContextSkia::endRecording()
11401146
return WTF::move(m_imageToFenceMap);
11411147
}
11421148

1143-
template<typename T>
1144-
inline std::unique_ptr<GLFence> createAcceleratedRenderingFenceInternal(T object)
1149+
static std::unique_ptr<GLFence> createFenceAfterFlush(GrDirectContext* grContext)
11451150
{
1146-
auto* glContext = PlatformDisplay::sharedDisplay().skiaGLContext();
1147-
if (!glContext || !glContext->makeContextCurrent())
1148-
return nullptr;
1149-
1150-
auto* grContext = PlatformDisplay::sharedDisplay().skiaGrContext();
1151-
RELEASE_ASSERT(grContext);
1152-
1153-
grContext->flush(object);
1154-
11551151
auto& glDisplay = PlatformDisplay::sharedDisplay().glDisplay();
11561152
if (GLFence::isSupported(glDisplay)) {
11571153
grContext->submit(GrSyncCpu::kNo);
@@ -1166,38 +1162,54 @@ inline std::unique_ptr<GLFence> createAcceleratedRenderingFenceInternal(T object
11661162

11671163
std::unique_ptr<GLFence> GraphicsContextSkia::createAcceleratedRenderingFence(SkSurface* surface)
11681164
{
1169-
return createAcceleratedRenderingFenceInternal<SkSurface*>(surface);
1165+
auto* glContext = PlatformDisplay::sharedDisplay().skiaGLContext();
1166+
if (!glContext || !glContext->makeContextCurrent())
1167+
return nullptr;
1168+
1169+
auto* recordingContext = surface->recordingContext();
1170+
auto* grContext = recordingContext ? recordingContext->asDirectContext() : nullptr;
1171+
if (!grContext)
1172+
return nullptr;
1173+
1174+
grContext->flush(surface);
1175+
return createFenceAfterFlush(grContext);
11701176
}
11711177

1172-
std::unique_ptr<GLFence> GraphicsContextSkia::createAcceleratedRenderingFence(const sk_sp<SkImage>& image)
1178+
std::unique_ptr<GLFence> GraphicsContextSkia::createAcceleratedRenderingFence(const sk_sp<SkImage>& image, GrDirectContext* grContext)
11731179
{
1174-
return createAcceleratedRenderingFenceInternal<const sk_sp<SkImage>>(image);
1180+
auto* glContext = PlatformDisplay::sharedDisplay().skiaGLContext();
1181+
if (!glContext || !glContext->makeContextCurrent())
1182+
return nullptr;
1183+
1184+
if (!grContext)
1185+
return nullptr;
1186+
1187+
grContext->flush(image);
1188+
return createFenceAfterFlush(grContext);
11751189
}
11761190

1177-
void GraphicsContextSkia::trackAcceleratedRenderingFenceIfNeeded(const sk_sp<SkImage>& image)
1191+
void GraphicsContextSkia::trackAcceleratedRenderingFenceIfNeeded(const sk_sp<SkImage>& image, GrDirectContext* grContext)
11781192
{
11791193
if (m_contextMode != ContextMode::RecordingMode)
11801194
return;
11811195

11821196
if (!image || !image->isTextureBacked())
11831197
return;
11841198

1185-
if (auto fence = createAcceleratedRenderingFence(image))
1199+
if (auto fence = createAcceleratedRenderingFence(image, grContext))
11861200
m_imageToFenceMap.add(image.get(), WTF::move(fence));
11871201
}
11881202

1189-
void GraphicsContextSkia::trackAcceleratedRenderingFenceIfNeeded(SkPaint& paint)
1203+
void GraphicsContextSkia::trackAcceleratedRenderingFenceIfNeeded(Pattern& pattern)
11901204
{
11911205
if (m_contextMode != ContextMode::RecordingMode)
11921206
return;
11931207

1194-
auto* shader = paint.getShader();
1195-
auto* image = shader ? shader->isAImage(nullptr, nullptr) : nullptr;
1196-
if (!image || !image->isTextureBacked())
1208+
auto nativeImage = pattern.tileNativeImage();
1209+
if (!nativeImage)
11971210
return;
11981211

1199-
if (auto fence = createAcceleratedRenderingFence(sk_ref_sp(image)))
1200-
m_imageToFenceMap.add(image, WTF::move(fence));
1212+
trackAcceleratedRenderingFenceIfNeeded(nativeImage->platformImage(), nativeImage->grContext());
12011213
}
12021214

12031215
void GraphicsContextSkia::drawSkiaText(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, bool enableAntialias, bool isVertical)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class SkSurface;
4141

4242
namespace WebCore {
4343

44+
class Pattern;
45+
4446
using SkiaImageToFenceMap = HashMap<const SkImage*, std::unique_ptr<GLFence>>;
4547

4648
class WEBCORE_EXPORT GraphicsContextSkia final : public GraphicsContext {
@@ -117,7 +119,7 @@ class WEBCORE_EXPORT GraphicsContextSkia final : public GraphicsContext {
117119
void drawSkiaText(const sk_sp<SkTextBlob>&, SkScalar, SkScalar, bool, bool);
118120

119121
static std::unique_ptr<GLFence> createAcceleratedRenderingFence(SkSurface*);
120-
static std::unique_ptr<GLFence> createAcceleratedRenderingFence(const sk_sp<SkImage>&);
122+
static std::unique_ptr<GLFence> createAcceleratedRenderingFence(const sk_sp<SkImage>&, GrDirectContext*);
121123

122124
private:
123125
enum class ContextMode : bool {
@@ -126,8 +128,8 @@ class WEBCORE_EXPORT GraphicsContextSkia final : public GraphicsContext {
126128
};
127129

128130
bool makeGLContextCurrentIfNeeded() const;
129-
void trackAcceleratedRenderingFenceIfNeeded(const sk_sp<SkImage>&);
130-
void trackAcceleratedRenderingFenceIfNeeded(SkPaint&);
131+
void trackAcceleratedRenderingFenceIfNeeded(const sk_sp<SkImage>&, GrDirectContext*);
132+
void trackAcceleratedRenderingFenceIfNeeded(Pattern&);
131133

132134
void setupFillSource(SkPaint&);
133135
void setupStrokeSource(SkPaint&);

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,17 @@ RefPtr<NativeImage> ImageBufferSkiaAcceleratedBackend::copyNativeImage()
158158

159159
RefPtr<NativeImage> ImageBufferSkiaAcceleratedBackend::createNativeImageReference()
160160
{
161+
auto* recordingContext = m_surface->recordingContext();
162+
auto* grContext = recordingContext ? recordingContext->asDirectContext() : nullptr;
163+
161164
// If we're using MSAA, we need to flush the surface before calling makeImageSnapshot(),
162165
// because that call doesn't force the MSAA resolution, which can produce outdated results
163166
// in the resulting SkImage.
164167
auto& display = PlatformDisplay::sharedDisplay();
165-
if (display.msaaSampleCount() > 0) {
166-
if (display.skiaGLContext()->makeContextCurrent())
167-
display.skiaGrContext()->flush(m_surface.get());
168-
}
169-
return NativeImage::create(m_surface->makeImageSnapshot());
168+
if (grContext && display.msaaSampleCount() > 0 && display.skiaGLContext()->makeContextCurrent())
169+
grContext->flush(m_surface.get());
170+
171+
return NativeImage::create(m_surface->makeImageSnapshot(), grContext);
170172
}
171173

172174
void ImageBufferSkiaAcceleratedBackend::getPixelBuffer(const IntRect& srcRect, PixelBuffer& destination)

0 commit comments

Comments
 (0)