Corentin Wallez | 339bd9d | 2019-03-28 12:57:11 +0000 | [diff] [blame] | 1 | // Copyright 2019 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 "tests/unittests/wire/WireTest.h" |
| 16 | |
| 17 | #include "dawn_wire/WireClient.h" |
| 18 | #include "dawn_wire/WireServer.h" |
| 19 | |
| 20 | using namespace testing; |
| 21 | using namespace dawn_wire; |
| 22 | |
| 23 | class WireInjectTextureTests : public WireTest { |
| 24 | public: |
Corentin Wallez | 0ae00a1 | 2019-03-28 17:12:47 +0000 | [diff] [blame] | 25 | WireInjectTextureTests() { |
Corentin Wallez | 339bd9d | 2019-03-28 12:57:11 +0000 | [diff] [blame] | 26 | } |
| 27 | ~WireInjectTextureTests() override = default; |
| 28 | }; |
| 29 | |
| 30 | // Test that reserving and injecting a texture makes calls on the client object forward to the |
| 31 | // server object correctly. |
| 32 | TEST_F(WireInjectTextureTests, CallAfterReserveInject) { |
| 33 | ReservedTexture reservation = GetWireClient()->ReserveTexture(device); |
| 34 | |
Corentin Wallez | 45b51f5 | 2019-10-28 22:15:47 +0000 | [diff] [blame] | 35 | WGPUTexture apiTexture = api.GetNewTexture(); |
Corentin Wallez | 339bd9d | 2019-03-28 12:57:11 +0000 | [diff] [blame] | 36 | EXPECT_CALL(api, TextureReference(apiTexture)); |
Austin Eng | 8ba0a01 | 2021-01-13 20:58:18 +0000 | [diff] [blame] | 37 | ASSERT_TRUE(GetWireServer()->InjectTexture(apiTexture, reservation.id, reservation.generation, |
| 38 | reservation.deviceId, reservation.deviceGeneration)); |
Corentin Wallez | 339bd9d | 2019-03-28 12:57:11 +0000 | [diff] [blame] | 39 | |
Corentin Wallez | 45b51f5 | 2019-10-28 22:15:47 +0000 | [diff] [blame] | 40 | wgpuTextureCreateView(reservation.texture, nullptr); |
| 41 | WGPUTextureView apiDummyView = api.GetNewTextureView(); |
Kai Ninomiya | 4078ed8 | 2019-08-27 17:56:23 +0000 | [diff] [blame] | 42 | EXPECT_CALL(api, TextureCreateView(apiTexture, nullptr)).WillOnce(Return(apiDummyView)); |
Corentin Wallez | 339bd9d | 2019-03-28 12:57:11 +0000 | [diff] [blame] | 43 | FlushClient(); |
| 44 | } |
| 45 | |
| 46 | // Test that reserve correctly returns different IDs each time. |
| 47 | TEST_F(WireInjectTextureTests, ReserveDifferentIDs) { |
| 48 | ReservedTexture reservation1 = GetWireClient()->ReserveTexture(device); |
| 49 | ReservedTexture reservation2 = GetWireClient()->ReserveTexture(device); |
| 50 | |
| 51 | ASSERT_NE(reservation1.id, reservation2.id); |
| 52 | ASSERT_NE(reservation1.texture, reservation2.texture); |
| 53 | } |
| 54 | |
| 55 | // Test that injecting the same id without a destroy first fails. |
| 56 | TEST_F(WireInjectTextureTests, InjectExistingID) { |
| 57 | ReservedTexture reservation = GetWireClient()->ReserveTexture(device); |
| 58 | |
Corentin Wallez | 45b51f5 | 2019-10-28 22:15:47 +0000 | [diff] [blame] | 59 | WGPUTexture apiTexture = api.GetNewTexture(); |
Corentin Wallez | 339bd9d | 2019-03-28 12:57:11 +0000 | [diff] [blame] | 60 | EXPECT_CALL(api, TextureReference(apiTexture)); |
Austin Eng | 8ba0a01 | 2021-01-13 20:58:18 +0000 | [diff] [blame] | 61 | ASSERT_TRUE(GetWireServer()->InjectTexture(apiTexture, reservation.id, reservation.generation, |
| 62 | reservation.deviceId, reservation.deviceGeneration)); |
Corentin Wallez | 339bd9d | 2019-03-28 12:57:11 +0000 | [diff] [blame] | 63 | |
| 64 | // ID already in use, call fails. |
Austin Eng | 8ba0a01 | 2021-01-13 20:58:18 +0000 | [diff] [blame] | 65 | ASSERT_FALSE(GetWireServer()->InjectTexture(apiTexture, reservation.id, reservation.generation, |
| 66 | reservation.deviceId, |
| 67 | reservation.deviceGeneration)); |
Corentin Wallez | 339bd9d | 2019-03-28 12:57:11 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // Test that the server only borrows the texture and does a single reference-release |
| 71 | TEST_F(WireInjectTextureTests, InjectedTextureLifetime) { |
| 72 | ReservedTexture reservation = GetWireClient()->ReserveTexture(device); |
| 73 | |
| 74 | // Injecting the texture adds a reference |
Corentin Wallez | 45b51f5 | 2019-10-28 22:15:47 +0000 | [diff] [blame] | 75 | WGPUTexture apiTexture = api.GetNewTexture(); |
Corentin Wallez | 339bd9d | 2019-03-28 12:57:11 +0000 | [diff] [blame] | 76 | EXPECT_CALL(api, TextureReference(apiTexture)); |
Austin Eng | 8ba0a01 | 2021-01-13 20:58:18 +0000 | [diff] [blame] | 77 | ASSERT_TRUE(GetWireServer()->InjectTexture(apiTexture, reservation.id, reservation.generation, |
| 78 | reservation.deviceId, reservation.deviceGeneration)); |
Corentin Wallez | 339bd9d | 2019-03-28 12:57:11 +0000 | [diff] [blame] | 79 | |
| 80 | // Releasing the texture removes a single reference. |
Corentin Wallez | 45b51f5 | 2019-10-28 22:15:47 +0000 | [diff] [blame] | 81 | wgpuTextureRelease(reservation.texture); |
Corentin Wallez | 339bd9d | 2019-03-28 12:57:11 +0000 | [diff] [blame] | 82 | EXPECT_CALL(api, TextureRelease(apiTexture)); |
| 83 | FlushClient(); |
| 84 | |
| 85 | // Deleting the server doesn't release a second reference. |
| 86 | DeleteServer(); |
| 87 | Mock::VerifyAndClearExpectations(&api); |
| 88 | } |
Austin Eng | 45ce1fd | 2021-01-22 00:25:05 +0000 | [diff] [blame] | 89 | |
| 90 | // Test that a texture reservation can be reclaimed. This is necessary to |
| 91 | // avoid leaking ObjectIDs for reservations that are never injected. |
| 92 | TEST_F(WireInjectTextureTests, ReclaimTextureReservation) { |
| 93 | // Test that doing a reservation and full release is an error. |
| 94 | { |
| 95 | ReservedTexture reservation = GetWireClient()->ReserveTexture(device); |
| 96 | wgpuTextureRelease(reservation.texture); |
| 97 | FlushClient(false); |
| 98 | } |
| 99 | |
| 100 | // Test that doing a reservation and then reclaiming it recycles the ID. |
| 101 | { |
| 102 | ReservedTexture reservation1 = GetWireClient()->ReserveTexture(device); |
| 103 | GetWireClient()->ReclaimTextureReservation(reservation1); |
| 104 | |
| 105 | ReservedTexture reservation2 = GetWireClient()->ReserveTexture(device); |
| 106 | |
| 107 | // The ID is the same, but the generation is still different. |
| 108 | ASSERT_EQ(reservation1.id, reservation2.id); |
| 109 | ASSERT_NE(reservation1.generation, reservation2.generation); |
| 110 | |
| 111 | // No errors should occur. |
| 112 | FlushClient(); |
| 113 | } |
| 114 | } |