Natasha Lee | 51af1b4 | 2020-10-12 22:32:33 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Dawn Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <gmock/gmock.h> |
| 16 | #include "tests/DawnTest.h" |
| 17 | |
| 18 | using namespace testing; |
| 19 | |
| 20 | class MockMapCallback { |
| 21 | public: |
| 22 | MOCK_METHOD(void, Call, (WGPUBufferMapAsyncStatus status, void* userdata)); |
| 23 | }; |
| 24 | |
| 25 | static std::unique_ptr<MockMapCallback> mockMapCallback; |
| 26 | static void ToMockMapCallback(WGPUBufferMapAsyncStatus status, void* userdata) { |
| 27 | EXPECT_EQ(status, WGPUBufferMapAsyncStatus_Success); |
| 28 | mockMapCallback->Call(status, userdata); |
| 29 | } |
| 30 | |
Corentin Wallez | c093db2 | 2021-02-25 13:17:01 +0000 | [diff] [blame] | 31 | class MockQueueWorkDoneCallback { |
| 32 | public: |
| 33 | MOCK_METHOD(void, Call, (WGPUQueueWorkDoneStatus status, void* userdata)); |
| 34 | }; |
| 35 | |
| 36 | static std::unique_ptr<MockQueueWorkDoneCallback> mockQueueWorkDoneCallback; |
| 37 | static void ToMockQueueWorkDone(WGPUQueueWorkDoneStatus status, void* userdata) { |
| 38 | mockQueueWorkDoneCallback->Call(status, userdata); |
| 39 | } |
| 40 | |
Natasha Lee | 51af1b4 | 2020-10-12 22:32:33 +0000 | [diff] [blame] | 41 | class QueueTimelineTests : public DawnTest { |
| 42 | protected: |
| 43 | void SetUp() override { |
| 44 | DawnTest::SetUp(); |
| 45 | |
| 46 | mockMapCallback = std::make_unique<MockMapCallback>(); |
Corentin Wallez | c093db2 | 2021-02-25 13:17:01 +0000 | [diff] [blame] | 47 | mockQueueWorkDoneCallback = std::make_unique<MockQueueWorkDoneCallback>(); |
Natasha Lee | 51af1b4 | 2020-10-12 22:32:33 +0000 | [diff] [blame] | 48 | |
| 49 | wgpu::BufferDescriptor descriptor; |
| 50 | descriptor.size = 4; |
| 51 | descriptor.usage = wgpu::BufferUsage::MapRead; |
| 52 | mMapReadBuffer = device.CreateBuffer(&descriptor); |
| 53 | } |
| 54 | |
| 55 | void TearDown() override { |
Natasha Lee | 51af1b4 | 2020-10-12 22:32:33 +0000 | [diff] [blame] | 56 | mockMapCallback = nullptr; |
Corentin Wallez | c093db2 | 2021-02-25 13:17:01 +0000 | [diff] [blame] | 57 | mockQueueWorkDoneCallback = nullptr; |
Natasha Lee | 51af1b4 | 2020-10-12 22:32:33 +0000 | [diff] [blame] | 58 | DawnTest::TearDown(); |
| 59 | } |
| 60 | |
| 61 | wgpu::Buffer mMapReadBuffer; |
| 62 | }; |
| 63 | |
Corentin Wallez | c093db2 | 2021-02-25 13:17:01 +0000 | [diff] [blame] | 64 | // Test that mMapReadBuffer.MapAsync callback happens before queue.OnWorkDone callback |
| 65 | // when queue.OnSubmittedWorkDone is called after mMapReadBuffer.MapAsync. The callback order should |
| 66 | // happen in the order the functions are called. |
| 67 | TEST_P(QueueTimelineTests, MapRead_OnWorkDone) { |
| 68 | testing::InSequence sequence; |
| 69 | EXPECT_CALL(*mockMapCallback, Call(WGPUBufferMapAsyncStatus_Success, this)).Times(1); |
| 70 | EXPECT_CALL(*mockQueueWorkDoneCallback, Call(WGPUQueueWorkDoneStatus_Success, this)).Times(1); |
| 71 | |
Zhaoming Jiang | 2a5b981 | 2021-10-18 05:30:39 +0000 | [diff] [blame] | 72 | mMapReadBuffer.MapAsync(wgpu::MapMode::Read, 0, wgpu::kWholeMapSize, ToMockMapCallback, this); |
Corentin Wallez | c093db2 | 2021-02-25 13:17:01 +0000 | [diff] [blame] | 73 | |
| 74 | queue.OnSubmittedWorkDone(0u, ToMockQueueWorkDone, this); |
| 75 | |
| 76 | WaitForAllOperations(); |
| 77 | mMapReadBuffer.Unmap(); |
| 78 | } |
| 79 | |
Corentin Wallez | c093db2 | 2021-02-25 13:17:01 +0000 | [diff] [blame] | 80 | // Test that queue.OnWorkDone callback happens before mMapReadBuffer.MapAsync callback when |
| 81 | // queue.Signal is called before mMapReadBuffer.MapAsync. The callback order should |
| 82 | // happen in the order the functions are called. |
| 83 | TEST_P(QueueTimelineTests, OnWorkDone_MapRead) { |
| 84 | testing::InSequence sequence; |
| 85 | EXPECT_CALL(*mockQueueWorkDoneCallback, Call(WGPUQueueWorkDoneStatus_Success, this)).Times(1); |
| 86 | EXPECT_CALL(*mockMapCallback, Call(WGPUBufferMapAsyncStatus_Success, this)).Times(1); |
| 87 | |
| 88 | queue.OnSubmittedWorkDone(0u, ToMockQueueWorkDone, this); |
| 89 | |
Zhaoming Jiang | 2a5b981 | 2021-10-18 05:30:39 +0000 | [diff] [blame] | 90 | mMapReadBuffer.MapAsync(wgpu::MapMode::Read, 0, wgpu::kWholeMapSize, ToMockMapCallback, this); |
Corentin Wallez | c093db2 | 2021-02-25 13:17:01 +0000 | [diff] [blame] | 91 | |
| 92 | WaitForAllOperations(); |
| 93 | mMapReadBuffer.Unmap(); |
| 94 | } |
| 95 | |
Natasha Lee | 51af1b4 | 2020-10-12 22:32:33 +0000 | [diff] [blame] | 96 | DAWN_INSTANTIATE_TEST(QueueTimelineTests, |
| 97 | D3D12Backend(), |
| 98 | MetalBackend(), |
| 99 | OpenGLBackend(), |
Stephen White | f31b78e | 2020-12-04 15:59:29 +0000 | [diff] [blame] | 100 | OpenGLESBackend(), |
Natasha Lee | 51af1b4 | 2020-10-12 22:32:33 +0000 | [diff] [blame] | 101 | VulkanBackend()); |