// Copyright 2019 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifdef UNSAFE_BUFFERS_BUILD // TODO(crbug.com/40285824): Remove this and convert code to safer constructs. #pragma allow_unsafe_buffers #endif #include <dawn/native/DawnNative.h> #include "build/build_config.h" #include "components/viz/test/test_gpu_service_holder.h" #include "gpu/command_buffer/client/client_shared_image.h" #include "gpu/command_buffer/client/shared_image_interface.h" #include "gpu/command_buffer/client/webgpu_implementation.h" #include "gpu/command_buffer/common/mailbox.h" #include "gpu/command_buffer/common/shared_image_usage.h" #include "gpu/command_buffer/service/shared_image/shared_image_format_service_utils.h" #include "gpu/command_buffer/service/webgpu_decoder.h" #include "gpu/command_buffer/tests/webgpu_test.h" #include "gpu/config/gpu_finch_features.h" #include "gpu/config/gpu_test_config.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" #include "ui/gfx/color_space.h" #include "ui/gl/gl_context.h" #include "ui/gl/gl_utils.h" #include "ui/gl/init/gl_factory.h" #define SKIP_TEST_IF(condition) … namespace gpu { namespace { class MockBufferMapCallback { … }; std::unique_ptr<testing::StrictMock<MockBufferMapCallback>> mock_buffer_map_callback; void ToMockBufferMapCallback(wgpu::MapAsyncStatus status, const char* message) { … } class MockUncapturedErrorCallback { … }; std::unique_ptr<testing::StrictMock<MockUncapturedErrorCallback>> mock_device_error_callback; void ToMockUncapturedErrorCallback(WGPUErrorType type, const char* message, void* userdata) { … } struct WebGPUMailboxTestParams : WebGPUTest::Options { … }; std::ostream& operator<<(std::ostream& os, const WebGPUMailboxTestParams& options) { … } uint32_t BytesPerTexel(viz::SharedImageFormat format) { … } } // namespace class WebGPUMailboxTest : public WebGPUTest, public testing::WithParamInterface<WebGPUMailboxTestParams> { … }; TEST_P(WebGPUMailboxTest, AssociateMailboxCmd) { … } // Test that AssociateMailbox with a bad mailbox produces an error texture. TEST_P(WebGPUMailboxTest, AssociateMailboxCmdBadMailboxMakesErrorTexture) { … } TEST_P(WebGPUMailboxTest, DissociateMailboxCmd) { … } // Tests using Associate/DissociateMailbox to share an image with Dawn. // For simplicity of the test the image is shared between a Dawn device and // itself: we render to it using the Dawn device, then re-associate it to a // Dawn texture and read back the values that were written. TEST_P(WebGPUMailboxTest, WriteToMailboxThenReadFromIt) { … } // Test that passing write usages when associating a mailbox fails if // the SharedImage associated with the mailbox doesn't have WEBGPU_WRITE access. TEST_P(WebGPUMailboxTest, PassWriteUsagesWhenAssociatingReadOnlyMailbox) { … } // Test that passing internal write usages when associating a mailbox fails if // the SharedImage associated with the mailbox doesn't have WEBGPU_WRITE access. TEST_P(WebGPUMailboxTest, PassInternalWriteUsagesWhenAssociatingReadOnlyMailbox) { … } // Test that passing WEBGPU_MAILBOX_DISCARD when associating a mailbox fails if // the SharedImage associated with the mailbox doesn't have WEBGPU_WRITE access. TEST_P(WebGPUMailboxTest, PassDiscardWhenAssociatingReadOnlyMailbox) { … } // Test that passing WEBGPU_MAILBOX_DISCARD when associating a mailbox fails if // the client doesn't pass a usage supporting lazy clearing. TEST_P(WebGPUMailboxTest, PassDiscardWhenAssociatingMailboxWithoutUsageSupportingClearing) { … } // Test that an uninitialized writable shared image is lazily cleared by Dawn // when it is accessed with an internal write usage supporting lazy clearing. TEST_P(WebGPUMailboxTest, ReadWritableUninitializedSharedImageWhenAccessedWithInternalWriteUsage) { … } // Test that an uninitialized writable shared image is lazily cleared by Dawn // when it is read if a usage supporting lazy clearing is passed. TEST_P(WebGPUMailboxTest, ReadWritableUninitializedSharedImageWithUsageSupportingLazyClearing) { … } // Test that an uninitialized writable shared image is lazily cleared by Dawn // when it is read if an internal usage supporting lazy clearing is passed. TEST_P( WebGPUMailboxTest, ReadWritableUninitializedSharedImageWithInternalUsageSupportingLazyClearing) { … } // Tests that using a shared image aftr it is dissociated produces an error. TEST_P(WebGPUMailboxTest, ErrorWhenUsingTextureAfterDissociate) { … } // This is a regression test for an issue when using multiple shared images // where a `ScopedAccess` was destroyed after it's `SharedImageRepresentation`. // The code was similar to the following. // // struct Pair { // unique_ptr<Representation> representation; // unique_ptr<Access> access; // }; // // base::flat_map<Key, Pair> map; // map.erase(some_iterator); // // In the Pair destructor C++ guarantees that `access` is destroyed before // `representation` but `erase` can move one element over another, causing // the move-assignment operator to be called. In this case the defaulted // move-assignment would first move `representation` then `access`. Causing // incorrect member destruction order for the move-to object. TEST_P(WebGPUMailboxTest, UseA_UseB_DestroyA_DestroyB) { … } // Regression test for a bug where the (id, generation) for associated shared // images was stored globally instead of per-device. This meant that of two // devices tried to create shared images with the same (id, generation) (which // is possible because they can be on different Dawn wires) they would conflict. TEST_P(WebGPUMailboxTest, AssociateOnTwoDevicesAtTheSameTime) { … } // Test that passing a descriptor to ReserveTexture produces a client-side // WGPUTexture that correctly reflects said descriptor. TEST_P(WebGPUMailboxTest, ReflectionOfDescriptor) { … } // Test that if some other GL context is current when // Associate/DissociateMailbox occurs, the operations do not fail. Some WebGPU // shared image backings rely on GL and need to be responsible for making the // context current. TEST_P(WebGPUMailboxTest, AssociateDissociateMailboxWhenNotCurrent) { … } INSTANTIATE_TEST_SUITE_P(…); } // namespace gpu