|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Kai Ninomiya <kainino@chromium.org> |
| 3 | +Date: Wed, 11 Mar 2026 14:52:44 -0700 |
| 4 | +Subject: [M146] Increment WebGL context generation number on context restore |
| 5 | + |
| 6 | +Objects created while the context is lost should not be valid to use |
| 7 | +after the context is restored. |
| 8 | +- Replace number_of_context_losses_ with a "context generation number" |
| 9 | + which increments on both context loss and context restore. |
| 10 | + - Technically, it would make sense to increment it only on context |
| 11 | + restore, but just in case any logic is relying on the current |
| 12 | + behavior, increment it in both places. |
| 13 | + - It's uint64_t just in case someone figures out how to increment it 4 |
| 14 | + billion times. |
| 15 | +- Remove unused WebGLRenderingContextBase::number_of_context_losses_, |
| 16 | + left over from before it was moved into WebGLContextObjectSupport. |
| 17 | + |
| 18 | +(cherry picked from commit c1433740f3ea902fd6b15d63c4865ad60a3761f9) |
| 19 | + |
| 20 | +Bug: 485935305 |
| 21 | +Change-Id: I1007217c8e69cfb8de4f117e0b7845ca574579c4 |
| 22 | +Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7630664 |
| 23 | +Reviewed-by: Kenneth Russell <kbr@chromium.org> |
| 24 | +Commit-Queue: Kai Ninomiya <kainino@chromium.org> |
| 25 | +Cr-Original-Commit-Position: refs/heads/main@{#1593726} |
| 26 | +Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7658823 |
| 27 | +Auto-Submit: Kai Ninomiya <kainino@chromium.org> |
| 28 | +Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> |
| 29 | +Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> |
| 30 | +Cr-Commit-Position: refs/branch-heads/7680@{#2370} |
| 31 | +Cr-Branched-From: 76b7d80e5cda23fe6537eed26d68c92e995c7f39-refs/heads/main@{#1582197} |
| 32 | + |
| 33 | +diff --git a/third_party/blink/renderer/modules/webgl/webgl_context_object_support.cc b/third_party/blink/renderer/modules/webgl/webgl_context_object_support.cc |
| 34 | +index 6a3b1416354e7993e7a9ebd25c4ca08593105d9a..83941f8163a5e9425f2df8fd3bb98e1fd37537ad 100644 |
| 35 | +--- a/third_party/blink/renderer/modules/webgl/webgl_context_object_support.cc |
| 36 | ++++ b/third_party/blink/renderer/modules/webgl/webgl_context_object_support.cc |
| 37 | +@@ -22,7 +22,10 @@ WebGLContextObjectSupport::WebGLContextObjectSupport( |
| 38 | + |
| 39 | + void WebGLContextObjectSupport::OnContextLost() { |
| 40 | + DCHECK(!is_lost_); |
| 41 | +- number_of_context_losses_++; |
| 42 | ++ // Invalidate all past objects. |
| 43 | ++ // (It may not be strictly necessary to do this here, since it's also done in |
| 44 | ++ // OnContextRestored, but we did it historically, and there's no harm in it.) |
| 45 | ++ context_generation_++; |
| 46 | + is_lost_ = true; |
| 47 | + gles2_interface_ = nullptr; |
| 48 | + extensions_enabled_.reset(); |
| 49 | +@@ -31,6 +34,8 @@ void WebGLContextObjectSupport::OnContextLost() { |
| 50 | + void WebGLContextObjectSupport::OnContextRestored( |
| 51 | + gpu::gles2::GLES2Interface* gl) { |
| 52 | + DCHECK(is_lost_); |
| 53 | ++ // Invalidate all past objects. |
| 54 | ++ context_generation_++; |
| 55 | + is_lost_ = false; |
| 56 | + gles2_interface_ = gl; |
| 57 | + } |
| 58 | +diff --git a/third_party/blink/renderer/modules/webgl/webgl_context_object_support.h b/third_party/blink/renderer/modules/webgl/webgl_context_object_support.h |
| 59 | +index 907866bb21acf9647d1c0ecd791e642e96b734fc..ba8b79f8bb9db12058614982a625baaff5546af7 100644 |
| 60 | +--- a/third_party/blink/renderer/modules/webgl/webgl_context_object_support.h |
| 61 | ++++ b/third_party/blink/renderer/modules/webgl/webgl_context_object_support.h |
| 62 | +@@ -33,10 +33,10 @@ class MODULES_EXPORT WebGLContextObjectSupport : public ScriptWrappable { |
| 63 | + bool IsWebGL2() const { return is_webgl2_; } |
| 64 | + bool IsLost() const { return is_lost_; } |
| 65 | + |
| 66 | +- // How many context losses there were, to check whether a WebGLObject was |
| 67 | +- // created since the last context resoration or before that (and hence invalid |
| 68 | +- // to use). |
| 69 | +- uint32_t NumberOfContextLosses() const { return number_of_context_losses_; } |
| 70 | ++ // Which "generation" the context is on (essentially, how many times it has |
| 71 | ++ // been restored), to check whether a WebGLObject was created since the last |
| 72 | ++ // context restoration, or before that (and hence invalid to use). |
| 73 | ++ uint64_t GetContextGeneration() const { return context_generation_; } |
| 74 | + |
| 75 | + bool ExtensionEnabled(WebGLExtensionName name) const { |
| 76 | + return extensions_enabled_.test(name); |
| 77 | +@@ -65,7 +65,7 @@ class MODULES_EXPORT WebGLContextObjectSupport : public ScriptWrappable { |
| 78 | + std::bitset<kWebGLExtensionNameCount> extensions_enabled_ = {}; |
| 79 | + raw_ptr<gpu::gles2::GLES2Interface> gles2_interface_ = nullptr; |
| 80 | + |
| 81 | +- uint32_t number_of_context_losses_ = 0; |
| 82 | ++ uint64_t context_generation_ = 0; |
| 83 | + bool is_lost_ = true; |
| 84 | + bool is_webgl2_; |
| 85 | + }; |
| 86 | +diff --git a/third_party/blink/renderer/modules/webgl/webgl_object.cc b/third_party/blink/renderer/modules/webgl/webgl_object.cc |
| 87 | +index 9d984de0073796f23a5038bfc0a51ec676179765..07e0a9a4aa3406a1298a677a3159edadc5f2cbb5 100644 |
| 88 | +--- a/third_party/blink/renderer/modules/webgl/webgl_object.cc |
| 89 | ++++ b/third_party/blink/renderer/modules/webgl/webgl_object.cc |
| 90 | +@@ -33,9 +33,9 @@ namespace blink { |
| 91 | + |
| 92 | + WebGLObject::WebGLObject(WebGLContextObjectSupport* context) |
| 93 | + : context_(context), |
| 94 | +- cached_number_of_context_losses_(std::numeric_limits<uint32_t>::max()) { |
| 95 | ++ context_generation_at_creation_(std::numeric_limits<uint64_t>::max()) { |
| 96 | + if (context_) { |
| 97 | +- cached_number_of_context_losses_ = context->NumberOfContextLosses(); |
| 98 | ++ context_generation_at_creation_ = context->GetContextGeneration(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | +@@ -46,7 +46,7 @@ bool WebGLObject::Validate(const WebGLContextObjectSupport* context) const { |
| 103 | + // the objects they ever created, so there's no way to invalidate them |
| 104 | + // eagerly during context loss. The invalidation is discovered lazily. |
| 105 | + return (context == context_ && context_ != nullptr && |
| 106 | +- cached_number_of_context_losses_ == context->NumberOfContextLosses()); |
| 107 | ++ context_generation_at_creation_ == context->GetContextGeneration()); |
| 108 | + } |
| 109 | + |
| 110 | + void WebGLObject::SetObject(GLuint object) { |
| 111 | +@@ -71,7 +71,7 @@ void WebGLObject::DeleteObject(gpu::gles2::GLES2Interface* gl) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | +- if (context_->NumberOfContextLosses() != cached_number_of_context_losses_) { |
| 116 | ++ if (context_->GetContextGeneration() != context_generation_at_creation_) { |
| 117 | + // This object has been invalidated. |
| 118 | + return; |
| 119 | + } |
| 120 | +diff --git a/third_party/blink/renderer/modules/webgl/webgl_object.h b/third_party/blink/renderer/modules/webgl/webgl_object.h |
| 121 | +index bb56df0f99e8e8432e03442feb9302b8dde27d01..97caa90e34288911b1a827e60c2569544d2b8f69 100644 |
| 122 | +--- a/third_party/blink/renderer/modules/webgl/webgl_object.h |
| 123 | ++++ b/third_party/blink/renderer/modules/webgl/webgl_object.h |
| 124 | +@@ -123,9 +123,9 @@ class WebGLObject : public ScriptWrappable { |
| 125 | + |
| 126 | + GLuint object_ = 0; |
| 127 | + |
| 128 | +- // This was the number of context losses of the object's associated |
| 129 | +- // WebGLContext at the time this object was created. |
| 130 | +- uint32_t cached_number_of_context_losses_; |
| 131 | ++ // The context generation number of the associated WebGLContext when the |
| 132 | ++ // object was created, to prevent reuse in later generations. |
| 133 | ++ uint64_t context_generation_at_creation_; |
| 134 | + |
| 135 | + unsigned attachment_count_ = 0; |
| 136 | + |
| 137 | +diff --git a/third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.h b/third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.h |
| 138 | +index 060563a9955a8564d176177fc389c4f98aa64e9f..f24221cb2f47cfde515179ff945c13756487ebfc 100644 |
| 139 | +--- a/third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.h |
| 140 | ++++ b/third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.h |
| 141 | +@@ -2073,8 +2073,6 @@ class MODULES_EXPORT WebGLRenderingContextBase |
| 142 | + |
| 143 | + bool has_been_drawn_to_ = false; |
| 144 | + |
| 145 | +- uint32_t number_of_context_losses_ = 0; |
| 146 | +- |
| 147 | + // Tracks if the context has ever called glBeginPixelLocalStorageANGLE. If it |
| 148 | + // has, we need to start using the pixel local storage interrupt mechanism |
| 149 | + // when we take over the client's context. |
0 commit comments