blob: d8df43afd7fd4f97d566d5e7805e3d9249f10155 [file] [log] [blame]
Corentin Wallez339bd9d2019-03-28 12:57:11 +00001// 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
20using namespace testing;
21using namespace dawn_wire;
22
23class WireInjectTextureTests : public WireTest {
24 public:
Corentin Wallez0ae00a12019-03-28 17:12:47 +000025 WireInjectTextureTests() {
Corentin Wallez339bd9d2019-03-28 12:57:11 +000026 }
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.
32TEST_F(WireInjectTextureTests, CallAfterReserveInject) {
33 ReservedTexture reservation = GetWireClient()->ReserveTexture(device);
34
Corentin Wallez45b51f52019-10-28 22:15:47 +000035 WGPUTexture apiTexture = api.GetNewTexture();
Corentin Wallez339bd9d2019-03-28 12:57:11 +000036 EXPECT_CALL(api, TextureReference(apiTexture));
Austin Eng8ba0a012021-01-13 20:58:18 +000037 ASSERT_TRUE(GetWireServer()->InjectTexture(apiTexture, reservation.id, reservation.generation,
38 reservation.deviceId, reservation.deviceGeneration));
Corentin Wallez339bd9d2019-03-28 12:57:11 +000039
Corentin Wallez45b51f52019-10-28 22:15:47 +000040 wgpuTextureCreateView(reservation.texture, nullptr);
41 WGPUTextureView apiDummyView = api.GetNewTextureView();
Kai Ninomiya4078ed82019-08-27 17:56:23 +000042 EXPECT_CALL(api, TextureCreateView(apiTexture, nullptr)).WillOnce(Return(apiDummyView));
Corentin Wallez339bd9d2019-03-28 12:57:11 +000043 FlushClient();
44}
45
46// Test that reserve correctly returns different IDs each time.
47TEST_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.
56TEST_F(WireInjectTextureTests, InjectExistingID) {
57 ReservedTexture reservation = GetWireClient()->ReserveTexture(device);
58
Corentin Wallez45b51f52019-10-28 22:15:47 +000059 WGPUTexture apiTexture = api.GetNewTexture();
Corentin Wallez339bd9d2019-03-28 12:57:11 +000060 EXPECT_CALL(api, TextureReference(apiTexture));
Austin Eng8ba0a012021-01-13 20:58:18 +000061 ASSERT_TRUE(GetWireServer()->InjectTexture(apiTexture, reservation.id, reservation.generation,
62 reservation.deviceId, reservation.deviceGeneration));
Corentin Wallez339bd9d2019-03-28 12:57:11 +000063
64 // ID already in use, call fails.
Austin Eng8ba0a012021-01-13 20:58:18 +000065 ASSERT_FALSE(GetWireServer()->InjectTexture(apiTexture, reservation.id, reservation.generation,
66 reservation.deviceId,
67 reservation.deviceGeneration));
Corentin Wallez339bd9d2019-03-28 12:57:11 +000068}
69
70// Test that the server only borrows the texture and does a single reference-release
71TEST_F(WireInjectTextureTests, InjectedTextureLifetime) {
72 ReservedTexture reservation = GetWireClient()->ReserveTexture(device);
73
74 // Injecting the texture adds a reference
Corentin Wallez45b51f52019-10-28 22:15:47 +000075 WGPUTexture apiTexture = api.GetNewTexture();
Corentin Wallez339bd9d2019-03-28 12:57:11 +000076 EXPECT_CALL(api, TextureReference(apiTexture));
Austin Eng8ba0a012021-01-13 20:58:18 +000077 ASSERT_TRUE(GetWireServer()->InjectTexture(apiTexture, reservation.id, reservation.generation,
78 reservation.deviceId, reservation.deviceGeneration));
Corentin Wallez339bd9d2019-03-28 12:57:11 +000079
80 // Releasing the texture removes a single reference.
Corentin Wallez45b51f52019-10-28 22:15:47 +000081 wgpuTextureRelease(reservation.texture);
Corentin Wallez339bd9d2019-03-28 12:57:11 +000082 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 Eng45ce1fd2021-01-22 00:25:05 +000089
90// Test that a texture reservation can be reclaimed. This is necessary to
91// avoid leaking ObjectIDs for reservations that are never injected.
92TEST_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}