[Blink] Move CanvasResourceProviderPassThrough into WebGL context
Nothing outside of the context uses it, and we are working to remove it
entirely.
Other than updating comments, the only minor code change is inlining
the body of IsGMBAllowed() [1] at its callsite.
[1] https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/graphics/canvas_resource_provider.cc;l=105-112?q=IsGMBAllowed&ss=chromium
Bug: 352263194
Change-Id: I29987e578c12418f7470757620a68c884adcae1a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6662559
Commit-Queue: Colin Blundell <[email protected]>
Reviewed-by: Vasiliy Telezhnikov <[email protected]>
Reviewed-by: Kentaro Hara <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1477730}
diff --git a/third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.cc b/third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.cc
index 50f49136..1b6208e1 100644
--- a/third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.cc
+++ b/third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.cc
@@ -230,6 +230,110 @@
constexpr base::TimeDelta kDurationBetweenRestoreAttempts = base::Seconds(1);
const int kMaxGLErrorsAllowedToConsole = 256;
+// This ResourceProvider is used for low-latency WebGL to pass the drawing
+// buffer's SharedImage directly through to the canvas via
+// ExternalCanvasResource for use cases such as compositing and snapshotting.
+class CanvasResourceProviderPassThrough final : public CanvasResourceProvider {
+ public:
+ CanvasResourceProviderPassThrough(
+ gfx::Size size,
+ viz::SharedImageFormat format,
+ SkAlphaType alpha_type,
+ const gfx::ColorSpace& color_space,
+ base::WeakPtr<WebGraphicsContext3DProviderWrapper>
+ context_provider_wrapper,
+ CanvasResourceHost* resource_host)
+ : CanvasResourceProvider(kPassThrough,
+ size,
+ format,
+ alpha_type,
+ color_space,
+ std::move(context_provider_wrapper),
+ resource_host) {}
+
+ ~CanvasResourceProviderPassThrough() override = default;
+ bool IsValid() const final { return true; }
+ bool IsAccelerated() const final { return true; }
+ bool SupportsDirectCompositing() const override { return true; }
+ bool IsSingleBuffered() const override { return true; }
+
+ private:
+ void ImportResource(
+ scoped_refptr<ExternalCanvasResource>&& resource) override {
+ resource_ = resource;
+ }
+
+ scoped_refptr<CanvasResource> ProduceCanvasResource(FlushReason) final {
+ return resource_;
+ }
+
+ sk_sp<SkSurface> CreateSkSurface() const override { NOTREACHED(); }
+
+ scoped_refptr<StaticBitmapImage> Snapshot(FlushReason,
+ ImageOrientation) override {
+ if (IsGpuContextLost() || !resource_) {
+ return nullptr;
+ }
+ return resource_->Bitmap();
+ }
+
+ private:
+ scoped_refptr<ExternalCanvasResource> resource_;
+};
+
+std::unique_ptr<CanvasResourceProvider> CreatePassThroughProvider(
+ gfx::Size size,
+ viz::SharedImageFormat format,
+ SkAlphaType alpha_type,
+ const gfx::ColorSpace& color_space,
+ base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper,
+ CanvasResourceHost* resource_host) {
+ // SharedGpuContext::IsGpuCompositingEnabled can potentially replace the
+ // context_provider_wrapper, so it's important to call that first as it can
+ // invalidate the weak pointer.
+ if (!SharedGpuContext::IsGpuCompositingEnabled() ||
+ !context_provider_wrapper) {
+ return nullptr;
+ }
+
+ const auto& capabilities =
+ context_provider_wrapper->ContextProvider().GetCapabilities();
+ if (size.width() > capabilities.max_texture_size ||
+ size.height() > capabilities.max_texture_size) {
+ return nullptr;
+ }
+
+ const auto& shared_image_capabilities =
+ context_provider_wrapper->ContextProvider()
+ .SharedImageInterface()
+ ->GetCapabilities();
+
+ const gfx::BufferFormat buffer_format =
+ viz::SinglePlaneSharedImageFormatToBufferFormat(format);
+ bool gmb_allowed =
+ gpu::IsImageSizeValidForGpuMemoryBufferFormat(size, buffer_format) &&
+ gpu::IsImageFromGpuMemoryBufferFormatSupported(buffer_format,
+ capabilities);
+
+ // Either swap_chain or gpu memory buffer should be enabled for this be used.
+ // TODO(crbug.com/404887530) : Remove or Rename `gmb_allowed` since
+ // CanvasResourceProvider no longer uses GMBs.
+ if (!shared_image_capabilities.shared_image_swap_chain && !gmb_allowed) {
+ return nullptr;
+ }
+
+ // Note: Unlike other CanvasResourceProvider subclasses, a
+ // CanvasResourceProviderPassThrough instance is always valid and does not
+ // require clearing as part of initialization (both of these being due to the
+ // fact that it simply delegates the internal parts of the resource to the
+ // drawing buffer).
+ auto provider = std::make_unique<CanvasResourceProviderPassThrough>(
+ size, format, alpha_type, color_space, context_provider_wrapper,
+ resource_host);
+ CHECK(provider->IsValid());
+ return provider;
+}
+
base::Lock& WebGLContextLimitLock() {
DEFINE_THREAD_SAFE_STATIC_LOCAL(base::Lock, lock, ());
return lock;
@@ -1949,7 +2053,7 @@
// If either SwapChain is enabled or WebGLImage mode is enabled, we can
// try a passthrough provider.
DCHECK(Host()->LowLatencyEnabled());
- provider = CanvasResourceProvider::CreatePassThroughProvider(
+ provider = CreatePassThroughProvider(
Host()->Size(), format, alpha_type, color_space,
SharedGpuContext::ContextProviderWrapper(), Host());
}
diff --git a/third_party/blink/renderer/platform/graphics/canvas_resource_provider.cc b/third_party/blink/renderer/platform/graphics/canvas_resource_provider.cc
index e2401ad..78b769f 100644
--- a/third_party/blink/renderer/platform/graphics/canvas_resource_provider.cc
+++ b/third_party/blink/renderer/platform/graphics/canvas_resource_provider.cc
@@ -1024,57 +1024,6 @@
base::BindOnce(&NotifyGpuContextLostTask, CreateWeakPtr()));
};
-// This ResourceProvider is meant to be used with an imported external
-// CanvasResource, and all drawing and lifetime logic must be kept at a higher
-// level.
-class CanvasResourceProviderPassThrough final : public CanvasResourceProvider {
- public:
- CanvasResourceProviderPassThrough(
- gfx::Size size,
- viz::SharedImageFormat format,
- SkAlphaType alpha_type,
- const gfx::ColorSpace& color_space,
- base::WeakPtr<WebGraphicsContext3DProviderWrapper>
- context_provider_wrapper,
- CanvasResourceHost* resource_host)
- : CanvasResourceProvider(kPassThrough,
- size,
- format,
- alpha_type,
- color_space,
- std::move(context_provider_wrapper),
- resource_host) {}
-
- ~CanvasResourceProviderPassThrough() override = default;
- bool IsValid() const final { return true; }
- bool IsAccelerated() const final { return true; }
- bool SupportsDirectCompositing() const override { return true; }
- bool IsSingleBuffered() const override { return true; }
-
- private:
- void ImportResource(
- scoped_refptr<ExternalCanvasResource>&& resource) override {
- resource_ = resource;
- }
-
- scoped_refptr<CanvasResource> ProduceCanvasResource(FlushReason) final {
- return resource_;
- }
-
- sk_sp<SkSurface> CreateSkSurface() const override { NOTREACHED(); }
-
- scoped_refptr<StaticBitmapImage> Snapshot(FlushReason,
- ImageOrientation) override {
- if (IsGpuContextLost() || !resource_) {
- return nullptr;
- }
- return resource_->Bitmap();
- }
-
- private:
- scoped_refptr<ExternalCanvasResource> resource_;
-};
-
// * Renders to back buffer of a shared image swap chain.
// * Presents swap chain and exports front buffer mailbox to compositor to
// support low latency mode.
@@ -1403,51 +1352,6 @@
}
std::unique_ptr<CanvasResourceProvider>
-CanvasResourceProvider::CreatePassThroughProvider(
- gfx::Size size,
- viz::SharedImageFormat format,
- SkAlphaType alpha_type,
- const gfx::ColorSpace& color_space,
- base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper,
- CanvasResourceHost* resource_host) {
- // SharedGpuContext::IsGpuCompositingEnabled can potentially replace the
- // context_provider_wrapper, so it's important to call that first as it can
- // invalidate the weak pointer.
- if (!SharedGpuContext::IsGpuCompositingEnabled() || !context_provider_wrapper)
- return nullptr;
-
- const auto& capabilities =
- context_provider_wrapper->ContextProvider().GetCapabilities();
- if (size.width() > capabilities.max_texture_size ||
- size.height() > capabilities.max_texture_size) {
- return nullptr;
- }
-
- const auto& shared_image_capabilities =
- context_provider_wrapper->ContextProvider()
- .SharedImageInterface()
- ->GetCapabilities();
- // Either swap_chain or gpu memory buffer should be enabled for this be used.
- // TODO(crbug.com/404887530) : Remove or Rename IsGMBAllowed() since
- // CanvasResourceProvider no longer uses GMBs.
- if (!shared_image_capabilities.shared_image_swap_chain &&
- !IsGMBAllowed(size, format, capabilities)) {
- return nullptr;
- }
-
- // Note: Unlike other CanvasResourceProvider subclasses, a
- // CanvasResourceProviderPassThrough instance is always valid and does not
- // require clearing as part of initialization (both of these being due to the
- // fact that it simply delegates the internal parts of the resource to other
- // classes).
- auto provider = std::make_unique<CanvasResourceProviderPassThrough>(
- size, format, alpha_type, color_space, context_provider_wrapper,
- resource_host);
- CHECK(provider->IsValid());
- return provider;
-}
-
-std::unique_ptr<CanvasResourceProvider>
CanvasResourceProvider::CreateSwapChainProvider(
gfx::Size size,
viz::SharedImageFormat format,
diff --git a/third_party/blink/renderer/platform/graphics/canvas_resource_provider.h b/third_party/blink/renderer/platform/graphics/canvas_resource_provider.h
index cb986e37..3498777 100644
--- a/third_party/blink/renderer/platform/graphics/canvas_resource_provider.h
+++ b/third_party/blink/renderer/platform/graphics/canvas_resource_provider.h
@@ -146,14 +146,6 @@
gpu::SharedImageUsageSet shared_image_usage_flags = {},
CanvasResourceHost* resource_host = nullptr);
- static std::unique_ptr<CanvasResourceProvider> CreatePassThroughProvider(
- gfx::Size size,
- viz::SharedImageFormat format,
- SkAlphaType alpha_type,
- const gfx::ColorSpace& color_space,
- base::WeakPtr<WebGraphicsContext3DProviderWrapper>,
- CanvasResourceHost* resource_host = nullptr);
-
static std::unique_ptr<CanvasResourceProvider> CreateSwapChainProvider(
gfx::Size size,
viz::SharedImageFormat format,
diff --git a/third_party/blink/renderer/platform/graphics/canvas_resource_provider_test.cc b/third_party/blink/renderer/platform/graphics/canvas_resource_provider_test.cc
index a9d206f..a686720 100644
--- a/third_party/blink/renderer/platform/graphics/canvas_resource_provider_test.cc
+++ b/third_party/blink/renderer/platform/graphics/canvas_resource_provider_test.cc
@@ -692,26 +692,6 @@
EXPECT_TRUE(!provider || !provider->IsValid());
}
-TEST_F(CanvasResourceProviderTest, DimensionsExceedMaxTextureSize_PassThrough) {
- auto provider = CanvasResourceProvider::CreatePassThroughProvider(
- gfx::Size(kMaxTextureSize - 1, kMaxTextureSize), GetN32FormatForCanvas(),
- kPremul_SkAlphaType, gfx::ColorSpace::CreateSRGB(),
- context_provider_wrapper_);
- EXPECT_TRUE(provider->SupportsDirectCompositing());
- provider = CanvasResourceProvider::CreatePassThroughProvider(
- gfx::Size(kMaxTextureSize, kMaxTextureSize), GetN32FormatForCanvas(),
- kPremul_SkAlphaType, gfx::ColorSpace::CreateSRGB(),
- context_provider_wrapper_);
- EXPECT_TRUE(provider->SupportsDirectCompositing());
- provider = CanvasResourceProvider::CreatePassThroughProvider(
- gfx::Size(kMaxTextureSize + 1, kMaxTextureSize), GetN32FormatForCanvas(),
- kPremul_SkAlphaType, gfx::ColorSpace::CreateSRGB(),
- context_provider_wrapper_);
- // The CanvasResourceProvider for PassThrough should not be created or valid
- // if the texture size is greater than the maximum value
- EXPECT_TRUE(!provider || !provider->IsValid());
-}
-
TEST_F(CanvasResourceProviderTest, CanvasResourceProviderDirect2DSwapChain) {
const gfx::Size kSize(10, 10);
const SkImageInfo kInfo =
diff --git a/third_party/blink/tools/blinkpy/presubmit/audit_non_blink_usage.py b/third_party/blink/tools/blinkpy/presubmit/audit_non_blink_usage.py
index 692b6c46..31d903e 100755
--- a/third_party/blink/tools/blinkpy/presubmit/audit_non_blink_usage.py
+++ b/third_party/blink/tools/blinkpy/presubmit/audit_non_blink_usage.py
@@ -1647,9 +1647,13 @@
'paths': [
'third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.cc',
],
- # This class needs access to a GPU driver bug workaround entry.
+ # This class needs access to various GPU-related functionality.
'allowed': [
+ 'gfx::BufferFormat',
'gpu::ENABLE_WEBGL_TIMER_QUERY_EXTENSIONS',
+ 'gpu::IsImageFromGpuMemoryBufferFormatSupported',
+ 'gpu::IsImageSizeValidForGpuMemoryBufferFormat',
+ 'viz::SinglePlaneSharedImageFormatToBufferFormat',
],
},
{