File: tests_comp_client_opengl.cpp

package info (click to toggle)
monado 25.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,708 kB
  • sloc: cpp: 175,132; ansic: 141,570; python: 2,913; java: 753; xml: 735; sh: 403; javascript: 255; makefile: 58
file content (131 lines) | stat: -rw-r--r-- 4,183 bytes parent folder | download
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
// Copyright 2022, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
 * @file
 * @brief OpenGL tests.
 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
 */


#include "mock/mock_compositor.h"
#include "client/comp_gl_client.h"

#include "catch_amalgamated.hpp"
#include "util/u_handles.h"
#include "ogl/ogl_api.h"

#include "xrt/xrt_config_os.h"
#include "xrt/xrt_config_vulkan.h"
#include "xrt/xrt_deleters.hpp"

#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>

#if defined(XRT_OS_WINDOWS)
#include "client/comp_gl_win32_client.h"
#elif defined(VK_USE_PLATFORM_XLIB_XRANDR_EXT)
#include "client/comp_gl_xlib_client.h"
#else
#error "not implemented"
#endif

#include <stdint.h>

TEST_CASE("opengl_client_compositor", "[.][needgpu]")
{
	xrt_compositor_native *xcn = mock_create_native_compositor();
	struct mock_compositor *mc = mock_compositor(&(xcn->base));

	REQUIRE(SDL_Init(SDL_INIT_VIDEO) == 0);

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
	auto *window = SDL_CreateWindow("Tests", 100, 100, 320, 240, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
	REQUIRE(window);
	SDL_SysWMinfo info{};
	SDL_VERSION(&info.version);
	REQUIRE(SDL_GetWindowWMInfo(window, &info));

	auto context = SDL_GL_CreateContext(window);

	SDL_GL_MakeCurrent(window, context);

#if defined(XRT_OS_WINDOWS)
	HDC hDC = info.info.win.hdc;
	HGLRC hGLRC = wglGetCurrentContext();
	struct client_gl_win32_compositor *c = client_gl_win32_compositor_create(xcn, hDC, hGLRC);
#elif defined(VK_USE_PLATFORM_XLIB_XRANDR_EXT)
	Display *display = info.info.x11.display;
	struct client_gl_xlib_compositor *c =
	    client_gl_xlib_compositor_create(xcn, display, 0, nullptr, nullptr, (GLXContext)context);
#else
#error "not implemented"
#endif
	REQUIRE(c);
	struct xrt_compositor *xc = &c->base.base.base;

	SECTION("Swapchain create and import")
	{
		struct Data
		{
			bool nativeCreateCalled = false;

			bool nativeImportCalled = false;
		} data;
		mc->userdata = &data;
		mc->compositor_hooks.create_swapchain =
		    [](struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc,
		       const struct xrt_swapchain_create_info *info, struct xrt_swapchain **out_xsc) {
			    auto *data = static_cast<Data *>(mc->userdata);
			    data->nativeCreateCalled = true;
			    return XRT_SUCCESS;
		    };
		mc->compositor_hooks.import_swapchain =
		    [](struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc,
		       const struct xrt_swapchain_create_info *info, struct xrt_image_native *native_images,
		       uint32_t image_count, struct xrt_swapchain **out_xscc) {
			    auto *data = static_cast<Data *>(mc->userdata);
			    data->nativeImportCalled = true;
			    // need to release the native handles to avoid leaks
			    for (uint32_t i = 0; i < image_count; ++i) {
				    u_graphics_buffer_unref(&native_images[i].handle);
			    }
			    return XRT_SUCCESS;
		    };
		xrt_swapchain_create_info xsci{};
		xsci.format = GL_SRGB8_ALPHA8;
		xsci.bits = (xrt_swapchain_usage_bits)(XRT_SWAPCHAIN_USAGE_COLOR | XRT_SWAPCHAIN_USAGE_SAMPLED);
		xsci.sample_count = 1;
		xsci.width = 800;
		xsci.height = 600;
		xsci.face_count = 1;
		xsci.array_size = 1;
		xsci.mip_count = 1;
		SECTION("Swapchain Create")
		{
			struct xrt_swapchain *xsc = nullptr;
			// This will fail because the mock compositor doesn't actually import, but it will get far
			// enough to trigger our hook and update the flag.
			xrt_comp_create_swapchain(xc, &xsci, &xsc);
			// OpenGL should always create using the native compositor
			CHECK_FALSE(data.nativeImportCalled);
			CHECK(data.nativeCreateCalled);
			xrt_swapchain_reference(&xsc, nullptr);
		}
	}

	xrt_comp_destroy(&xc);
	xrt_comp_native_destroy(&xcn);

	SDL_GL_DeleteContext(context);
	SDL_DestroyWindow(window);
	SDL_Quit();
}