Differentiate texture view defaulted from texture labels

Bug: 425906323
Change-Id: Ica582cb3604740d6c8c7c2161d1e0259cece858a
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/248274
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Fr <beaufort.francois@gmail.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/dawn/native/Device.cpp b/src/dawn/native/Device.cpp
index fb14074..e59fcdf 100644
--- a/src/dawn/native/Device.cpp
+++ b/src/dawn/native/Device.cpp
@@ -2259,6 +2259,11 @@
 
     TextureViewDescriptor desc;
     DAWN_TRY_ASSIGN(desc, GetTextureViewDescriptorWithDefaults(texture, descriptorOrig));
+    std::string generatedLabel;
+    if (descriptorOrig == nullptr) {
+        generatedLabel = absl::StrFormat("defaulted from %s", texture);
+        desc.label = {generatedLabel.data(), generatedLabel.length()};
+    }
 
     UnpackedPtr<TextureViewDescriptor> descriptor;
     if (IsValidationEnabled()) {
diff --git a/src/dawn/native/Texture.cpp b/src/dawn/native/Texture.cpp
index e84c5d3..e35c86a 100644
--- a/src/dawn/native/Texture.cpp
+++ b/src/dawn/native/Texture.cpp
@@ -1035,6 +1035,9 @@
     // Drop all the cache references to TextureViews.
     mTextureViewCache = nullptr;
 
+    // Clear the default view associated with the texture.
+    mDefaultView = nullptr;
+
     // Destroy all of the views associated with the texture as well.
     mTextureViews.Destroy();
 }
@@ -1073,6 +1076,7 @@
     // Drop all the additional references to TextureViews that we were holding as a part of the
     // cache.
     mTextureViewCache = nullptr;
+    mDefaultView = nullptr;
 }
 
 std::string TextureBase::GetSizeLabel() const {
@@ -1391,6 +1395,9 @@
 
 ResultOrError<Ref<TextureViewBase>> TextureBase::CreateView(
     const TextureViewDescriptor* descriptor) {
+    if (descriptor == nullptr) {
+        return GetOrCreateDefaultView();
+    }
     return GetDevice()->CreateTextureView(this, descriptor);
 }
 
@@ -1461,6 +1468,20 @@
     return byteSize;
 }
 
+ResultOrError<Ref<TextureViewBase>> TextureBase::GetOrCreateDefaultView() {
+    // Texture view caching is not enabled, so don't cache the default view.
+    if (!mTextureViewCache) {
+        return GetDevice()->CreateTextureView(this, nullptr);
+    }
+
+    // Lazily initialize and cache a default view when asked for it.
+    if (!mDefaultView) {
+        DAWN_TRY_ASSIGN(mDefaultView, GetDevice()->CreateTextureView(this, nullptr));
+    }
+    DAWN_ASSERT(mDefaultView);
+    return mDefaultView;
+}
+
 void TextureBase::APIDestroy() {
     Destroy();
 }
diff --git a/src/dawn/native/Texture.h b/src/dawn/native/Texture.h
index 81c8a32..96d2903 100644
--- a/src/dawn/native/Texture.h
+++ b/src/dawn/native/Texture.h
@@ -239,6 +239,8 @@
 
     std::string GetSizeLabel() const;
 
+    ResultOrError<Ref<TextureViewBase>> GetOrCreateDefaultView();
+
     void WillAddFirstExternalRef() override;
     void WillDropLastExternalRef() override;
 
@@ -256,6 +258,7 @@
     TextureState mState;
     wgpu::TextureFormat mFormatEnumForReflection;
 
+    Ref<TextureViewBase> mDefaultView;
     // Textures track texture views created from them so that they can be destroyed when the texture
     // is destroyed.
     ApiObjectList mTextureViews;
diff --git a/src/dawn/tests/unittests/validation/LabelTests.cpp b/src/dawn/tests/unittests/validation/LabelTests.cpp
index ff8f0cc..9026254 100644
--- a/src/dawn/tests/unittests/validation/LabelTests.cpp
+++ b/src/dawn/tests/unittests/validation/LabelTests.cpp
@@ -519,11 +519,11 @@
 
     wgpu::Texture texture = device.CreateTexture(&descriptor);
 
-    // The label should be empty if one was not set.
+    // The label should be generated if no one was not set.
     {
         wgpu::TextureView textureView = texture.CreateView();
         std::string readbackLabel = native::GetObjectLabelForTesting(textureView.Get());
-        ASSERT_TRUE(readbackLabel.empty());
+        ASSERT_EQ(readbackLabel.rfind("defaulted from [Texture (unlabeled", 0), 0U);
     }
 
     // Test setting a label through API