From 28a1dd6fab2b33b0f875bdd7e7b5c2331a271a40 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Wed, 31 Mar 2021 19:14:04 -0700 Subject: [PATCH 1/2] Fix texture corruption on Windows Notify Skia that we've updated texture handles within the current binding such that Skia invalidates any assumptions about previous context modifications that it had made. This fixes a texture corruption issue reported in https://github.com/flutter/flutter/issues/78648 --- shell/platform/embedder/embedder.cc | 25 ++++++++++++++++--- .../embedder/embedder_external_texture_gl.cc | 2 ++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 9c0b7ee2c3732..f1f9d3994a52f 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -446,6 +446,26 @@ static sk_sp MakeSkSurfaceFromBackingStore( texture_info.fID = texture->name; texture_info.fFormat = texture->format; + struct Captures { + VoidCallback destruction_callback; + void* user_data; + GrDirectContext* gr_direct_context; + }; + auto captures = std::make_unique(); + captures->destruction_callback = texture->destruction_callback; + captures->user_data = texture->user_data; + captures->gr_direct_context = context; + auto release_proc = [](void* context) { + auto captures = reinterpret_cast(context); + if (captures->gr_direct_context) { + captures->gr_direct_context->flushAndSubmit(); + captures->gr_direct_context->resetContext(kAll_GrBackendState); + } + if (captures->destruction_callback) { + captures->destruction_callback(captures->user_data); + } + }; + GrBackendTexture backend_texture(config.size.width, // config.size.height, // GrMipMapped::kNo, // @@ -462,9 +482,8 @@ static sk_sp MakeSkSurfaceFromBackingStore( kN32_SkColorType, // color type SkColorSpace::MakeSRGB(), // color space &surface_properties, // surface properties - static_cast( - texture->destruction_callback), // release proc - texture->user_data // release context + release_proc, // release proc + captures.release() // release context ); if (!surface) { diff --git a/shell/platform/embedder/embedder_external_texture_gl.cc b/shell/platform/embedder/embedder_external_texture_gl.cc index 0b5ed43c78028..45996a40d5792 100644 --- a/shell/platform/embedder/embedder_external_texture_gl.cc +++ b/shell/platform/embedder/embedder_external_texture_gl.cc @@ -48,6 +48,8 @@ sk_sp EmbedderExternalTextureGL::ResolveTexture( int64_t texture_id, GrDirectContext* context, const SkISize& size) { + context->flushAndSubmit(); + context->resetContext(kAll_GrBackendState); std::unique_ptr texture = external_texture_callback_(texture_id, size.width(), size.height()); From 41c966ca0f7f0b16c57d1bc38ef13b59f5bd2900 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Mon, 5 Apr 2021 11:44:37 -0700 Subject: [PATCH 2/2] Revert changes to destruction_callback Out of concerns with GrDirectContext lifetime, reverting this bit. --- shell/platform/embedder/embedder.cc | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index f1f9d3994a52f..9c0b7ee2c3732 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -446,26 +446,6 @@ static sk_sp MakeSkSurfaceFromBackingStore( texture_info.fID = texture->name; texture_info.fFormat = texture->format; - struct Captures { - VoidCallback destruction_callback; - void* user_data; - GrDirectContext* gr_direct_context; - }; - auto captures = std::make_unique(); - captures->destruction_callback = texture->destruction_callback; - captures->user_data = texture->user_data; - captures->gr_direct_context = context; - auto release_proc = [](void* context) { - auto captures = reinterpret_cast(context); - if (captures->gr_direct_context) { - captures->gr_direct_context->flushAndSubmit(); - captures->gr_direct_context->resetContext(kAll_GrBackendState); - } - if (captures->destruction_callback) { - captures->destruction_callback(captures->user_data); - } - }; - GrBackendTexture backend_texture(config.size.width, // config.size.height, // GrMipMapped::kNo, // @@ -482,8 +462,9 @@ static sk_sp MakeSkSurfaceFromBackingStore( kN32_SkColorType, // color type SkColorSpace::MakeSRGB(), // color space &surface_properties, // surface properties - release_proc, // release proc - captures.release() // release context + static_cast( + texture->destruction_callback), // release proc + texture->user_data // release context ); if (!surface) {