The open source OpenXR runtime
1// Copyright 2022, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Direct3D 11 tests.
6 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
7 */
8
9
10#include "mock/mock_compositor.h"
11#include "client/comp_d3d11_client.h"
12
13#include "catch_amalgamated.hpp"
14#include "util/u_handles.h"
15
16#include <d3d/d3d_dxgi_helpers.hpp>
17#include <d3d/d3d_d3d11_helpers.hpp>
18#include <stdint.h>
19#include <util/u_win32_com_guard.hpp>
20
21#include <d3d11_4.h>
22
23
24using namespace xrt::auxiliary::d3d;
25using namespace xrt::auxiliary::d3d::d3d11;
26using namespace xrt::auxiliary::util;
27
28TEST_CASE("client_compositor", "[.][needgpu]")
29{
30 xrt_compositor_native *xcn = mock_create_native_compositor();
31 struct mock_compositor *mc = mock_compositor(&(xcn->base));
32
33 ComGuard comGuard;
34
35 wil::com_ptr<ID3D11Device> device;
36 wil::com_ptr<ID3D11DeviceContext> context;
37 std::tie(device, context) = createDevice();
38 struct xrt_compositor_d3d11 *xcd3d = client_d3d11_compositor_create(xcn, device.get());
39 struct xrt_compositor *xc = &xcd3d->base;
40
41 SECTION("Swapchain create and import")
42 {
43 struct Data
44 {
45 bool nativeCreateCalled = false;
46
47 bool nativeImportCalled = false;
48 } data;
49 mc->userdata = &data;
50 mc->compositor_hooks.create_swapchain =
51 [](struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc,
52 const struct xrt_swapchain_create_info *info, struct xrt_swapchain **out_xsc) {
53 auto *data = static_cast<Data *>(mc->userdata);
54 data->nativeCreateCalled = true;
55 return XRT_SUCCESS;
56 };
57 mc->compositor_hooks.import_swapchain =
58 [](struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc,
59 const struct xrt_swapchain_create_info *info, struct xrt_image_native *native_images,
60 uint32_t image_count, struct xrt_swapchain **out_xscc) {
61 auto *data = static_cast<Data *>(mc->userdata);
62 data->nativeImportCalled = true;
63 // need to release the native handles to avoid leaks
64 for (uint32_t i = 0; i < image_count; ++i) {
65 if (!native_images[i].is_dxgi_handle) {
66 u_graphics_buffer_unref(&native_images[i].handle);
67 }
68 }
69 return XRT_SUCCESS;
70 };
71 xrt_swapchain_create_info xsci{};
72 xsci.format = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
73 xsci.bits = (xrt_swapchain_usage_bits)(XRT_SWAPCHAIN_USAGE_COLOR | XRT_SWAPCHAIN_USAGE_SAMPLED);
74 xsci.sample_count = 1;
75 xsci.width = 800;
76 xsci.height = 600;
77 xsci.face_count = 1;
78 xsci.array_size = 1;
79 xsci.mip_count = 1;
80 SECTION("Swapchain Create")
81 {
82 struct xrt_swapchain *xsc = nullptr;
83 // This will fail because the mock compositor doesn't actually import, but it will get far
84 // enough to trigger our hook and update the flag.
85 xrt_comp_create_swapchain(xc, &xsci, &xsc);
86 // D3D always imports into the native compositor
87 CHECK(data.nativeImportCalled);
88 CHECK_FALSE(data.nativeCreateCalled);
89 xrt_swapchain_reference(&xsc, nullptr);
90 }
91 }
92
93 xrt_comp_destroy(&xc);
94 xrt_comp_native_destroy(&xcn);
95}