xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/vl/vl_winsys_win32.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © Microsoft Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 #include "pipe-loader/pipe_loader.h"
24 #include "pipe/p_screen.h"
25 
26 #include "util/u_memory.h"
27 #include "vl/vl_winsys.h"
28 
29 #include "gallium/winsys/sw/gdi/gdi_sw_winsys.h"
30 #include "gallium/drivers/d3d12/d3d12_public.h"
31 #include <unknwn.h>
32 
33 struct vl_win32_screen
34 {
35    struct vl_screen base;
36    LUID *adapter_luid;
37    struct sw_winsys* winsys;
38 };
39 
40 static void
vl_win32_screen_destroy(struct vl_screen * vscreen)41 vl_win32_screen_destroy(struct vl_screen *vscreen)
42 {
43    if (vscreen == NULL)
44       return;
45 
46    struct vl_win32_screen* w32screen = (struct vl_win32_screen*) vscreen;
47    if (w32screen->winsys)
48       w32screen->winsys->destroy(w32screen->winsys);
49 
50    if (vscreen->pscreen)
51       vscreen->pscreen->destroy(vscreen->pscreen);
52 
53    if (vscreen->dev)
54       pipe_loader_release(&vscreen->dev, 1);
55 
56    FREE(vscreen);
57 }
58 
59 struct vl_screen *
vl_win32_screen_create(LUID * adapter)60 vl_win32_screen_create(LUID *adapter)
61 {
62    struct vl_win32_screen *vscreen = CALLOC_STRUCT(vl_win32_screen);
63    if (!vscreen)
64       return NULL;
65 
66    vscreen->winsys = gdi_create_sw_winsys(gdi_sw_acquire_hdc_by_value, gdi_sw_release_hdc_by_value);
67    if (!vscreen->winsys)
68       goto release_pipe;
69 
70    /* If adapter is null, d3d12_create_dxcore_screen will choose one */
71    vscreen->base.pscreen = d3d12_create_dxcore_screen(vscreen->winsys, adapter);
72 
73    if (!vscreen->base.pscreen)
74       goto release_pipe;
75 
76    vscreen->adapter_luid = adapter;
77 
78    vscreen->base.destroy = vl_win32_screen_destroy;
79    vscreen->base.get_private = NULL;
80    vscreen->base.texture_from_drawable = NULL;
81    vscreen->base.get_dirty_area = NULL;
82 
83    return &vscreen->base;
84 
85 release_pipe:
86    vl_win32_screen_destroy(&vscreen->base);
87    return NULL;
88 }
89 
90 struct vl_screen *
vl_win32_screen_create_from_d3d12_device(IUnknown * d3d12_device,struct sw_winsys * winsys)91 vl_win32_screen_create_from_d3d12_device(IUnknown* d3d12_device, struct sw_winsys* winsys)
92 {
93    struct vl_win32_screen *vscreen = CALLOC_STRUCT(vl_win32_screen);
94    if (!vscreen)
95       return NULL;
96 
97    vscreen->base.pscreen = d3d12_create_dxcore_screen_from_d3d12_device(winsys, d3d12_device, &vscreen->adapter_luid);
98 
99    if (!vscreen->base.pscreen)
100       goto release_pipe;
101 
102    vscreen->base.destroy = vl_win32_screen_destroy;
103    vscreen->base.get_private = NULL;
104    vscreen->base.texture_from_drawable = NULL;
105    vscreen->base.get_dirty_area = NULL;
106 
107    return &vscreen->base;
108 
109 release_pipe:
110    vl_win32_screen_destroy(&vscreen->base);
111    return NULL;
112 }
113