1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
gpu / command_buffer / service / dxgi_shared_handle_manager_unittest.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/service/dxgi_shared_handle_manager.h"
#include <windows.h>
#include <d3d11_1.h>
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gl/gl_angle_util_win.h"
namespace gpu {
namespace {
class DXGISharedHandleManagerTest : public testing::Test {
protected:
void SetUp() override {
d3d11_device_ = gl::QueryD3D11DeviceObjectFromANGLE();
dxgi_shared_handle_manager_ =
base::MakeRefCounted<DXGISharedHandleManager>();
}
bool ShouldSkipTest() const { return !d3d11_device_; }
Microsoft::WRL::ComPtr<ID3D11Texture2D> CreateTexture() {
D3D11_TEXTURE2D_DESC desc;
desc.Width = 1;
desc.Height = 1;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
desc.CPUAccessFlags = 0;
desc.MiscFlags =
D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED;
Microsoft::WRL::ComPtr<ID3D11Texture2D> d3d11_texture;
HRESULT hr = d3d11_device_->CreateTexture2D(&desc, nullptr, &d3d11_texture);
EXPECT_EQ(hr, S_OK);
return d3d11_texture;
}
base::win::ScopedHandle CreateSharedHandle(
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& d3d11_texture) {
Microsoft::WRL::ComPtr<IDXGIResource1> dxgi_resource;
HRESULT hr = d3d11_texture.As(&dxgi_resource);
EXPECT_EQ(hr, S_OK);
HANDLE shared_handle;
hr = dxgi_resource->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE,
nullptr, &shared_handle);
EXPECT_EQ(hr, S_OK);
return base::win::ScopedHandle(shared_handle);
}
Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_;
scoped_refptr<DXGISharedHandleManager> dxgi_shared_handle_manager_;
};
TEST_F(DXGISharedHandleManagerTest, LookupByToken) {
if (ShouldSkipTest())
return;
auto d3d11_texture = CreateTexture();
ASSERT_TRUE(d3d11_texture);
constexpr int kNumHandles = 5;
auto orig_token = gfx::DXGIHandleToken();
base::win::ScopedHandle orig_handle = CreateSharedHandle(d3d11_texture);
ASSERT_TRUE(orig_handle.IsValid());
scoped_refptr<DXGISharedHandleState> orig_state =
dxgi_shared_handle_manager_->GetOrCreateSharedHandleState(
orig_token, std::move(orig_handle), d3d11_device_);
ASSERT_NE(orig_state, nullptr);
EXPECT_EQ(dxgi_shared_handle_manager_->GetSharedHandleMapSizeForTesting(),
1u);
for (int i = 0; i < kNumHandles - 1; i++) {
HANDLE handle;
::DuplicateHandle(::GetCurrentProcess(), orig_state->GetSharedHandle(),
::GetCurrentProcess(), &handle,
/*dwDesiredAccess=*/0,
/*bInerhitHandle=*/FALSE, DUPLICATE_SAME_ACCESS);
base::win::ScopedHandle new_handle(handle);
ASSERT_TRUE(new_handle.IsValid());
scoped_refptr<DXGISharedHandleState> state =
dxgi_shared_handle_manager_->GetOrCreateSharedHandleState(
orig_token, std::move(new_handle), d3d11_device_);
EXPECT_EQ(state, orig_state);
EXPECT_EQ(dxgi_shared_handle_manager_->GetSharedHandleMapSizeForTesting(),
1u);
}
orig_state = nullptr;
EXPECT_EQ(dxgi_shared_handle_manager_->GetSharedHandleMapSizeForTesting(),
0u);
}
TEST_F(DXGISharedHandleManagerTest, LookupByTokenMultiThread) {
if (ShouldSkipTest())
return;
auto d3d11_texture = CreateTexture();
ASSERT_TRUE(d3d11_texture);
constexpr int kNumHandles = 101;
auto orig_token = gfx::DXGIHandleToken();
base::win::ScopedHandle orig_handle = CreateSharedHandle(d3d11_texture);
ASSERT_TRUE(orig_handle.IsValid());
scoped_refptr<DXGISharedHandleState> orig_state =
dxgi_shared_handle_manager_->GetOrCreateSharedHandleState(
orig_token, std::move(orig_handle), d3d11_device_);
ASSERT_NE(orig_state, nullptr);
EXPECT_EQ(dxgi_shared_handle_manager_->GetSharedHandleMapSizeForTesting(),
1u);
base::Lock lock;
base::ConditionVariable cv(&lock);
base::AutoLock auto_lock(lock);
int remaining_handles = kNumHandles - 1;
for (int i = 0; i < remaining_handles; i++) {
base::ThreadPool::PostTask(
FROM_HERE, base::BindLambdaForTesting([&] {
HANDLE handle;
::DuplicateHandle(::GetCurrentProcess(),
orig_state->GetSharedHandle(),
::GetCurrentProcess(), &handle,
/*dwDesiredAccess=*/0,
/*bInerhitHandle=*/FALSE, DUPLICATE_SAME_ACCESS);
base::win::ScopedHandle new_handle(handle);
ASSERT_TRUE(new_handle.IsValid());
scoped_refptr<DXGISharedHandleState> state =
dxgi_shared_handle_manager_->GetOrCreateSharedHandleState(
orig_token, std::move(new_handle), d3d11_device_);
EXPECT_EQ(state, orig_state);
EXPECT_EQ(
dxgi_shared_handle_manager_->GetSharedHandleMapSizeForTesting(),
1u);
state = nullptr;
EXPECT_EQ(
dxgi_shared_handle_manager_->GetSharedHandleMapSizeForTesting(),
1u);
base::AutoLock auto_lock(lock);
remaining_handles--;
cv.Signal();
}));
}
while (remaining_handles > 0)
cv.Wait();
EXPECT_EQ(dxgi_shared_handle_manager_->GetSharedHandleMapSizeForTesting(),
1u);
orig_state = nullptr;
EXPECT_EQ(dxgi_shared_handle_manager_->GetSharedHandleMapSizeForTesting(),
0u);
}
} // anonymous namespace
} // namespace gpu