Debian Codegraph @

video/out/opengl/hwdec_dxva2gldx.c

raw symbols SCIP
1 /*
2 * This file is part of mpv.
3 *
4 * mpv is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * mpv is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <d3d9.h>
19 #include <assert.h>
20
21 #include "common/common.h"
22 #include "osdep/windows_utils.h"
23 #include "video/out/gpu/hwdec.h"
24 #include "ra_gl.h"
25 #include "video/hwdec.h"
26 #include "video/d3d.h"
27
28 // for WGL_ACCESS_READ_ONLY_NV
29 #include <GL/wglext.h>
30
31 #define SHARED_SURFACE_D3DFMT D3DFMT_X8R8G8B8
32
33 struct priv_owner {
34 struct mp_hwdec_ctx hwctx;
35 IDirect3DDevice9Ex *device;
36 HANDLE device_h;
37 };
38
39 struct priv {
40 IDirect3DDevice9Ex *device;
41 HANDLE device_h;
42 IDirect3DSurface9 *rtarget;
43 HANDLE rtarget_h;
44 GLuint texture;
45 };
46
47 static void uninit(struct ra_hwdec *hw)
48 {
49 struct priv_owner *p = hw->priv;
50
51 hwdec_devices_remove(hw->devs, &p->hwctx);
52 av_buffer_unref(&p->hwctx.av_device_ref);
53
54 if (p->device)
55 IDirect3DDevice9Ex_Release(p->device);
56 }
57
58 static int init(struct ra_hwdec *hw)
59 {
60 struct priv_owner *p = hw->priv;
61 struct ra *ra = hw->ra_ctx->ra;
62
63 if (!ra_is_gl(ra))
64 return -1;
65 GL *gl = ra_gl_get(ra);
66 if (!(gl->mpgl_caps & MPGL_CAP_DXINTEROP))
67 return -1;
68
69 // AMD drivers won't open multiple dxinterop HANDLES on the same D3D device,
70 // so we request the one already in use by context_dxinterop
71 p->device_h = ra_get_native_resource(ra, "dxinterop_device_HANDLE");
72 if (!p->device_h)
73 return -1;
74
75 // But we also still need the actual D3D device
76 p->device = ra_get_native_resource(ra, "IDirect3DDevice9Ex");
77 if (!p->device)
78 return -1;
79 IDirect3DDevice9Ex_AddRef(p->device);
80
81 p->hwctx = (struct mp_hwdec_ctx){
82 .driver_name = hw->driver->name,
83 .av_device_ref = d3d9_wrap_device_ref((IDirect3DDevice9 *)p->device),
84 .hw_imgfmt = IMGFMT_DXVA2,
85 };
86
87 if (!p->hwctx.av_device_ref) {
88 MP_VERBOSE(hw, "Failed to create hwdevice_ctx\n");
89 return -1;
90 }
91
92 hwdec_devices_add(hw->devs, &p->hwctx);
93 return 0;
94 }
95
96 static void mapper_uninit(struct ra_hwdec_mapper *mapper)
97 {
98 struct priv *p = mapper->priv;
99 GL *gl = ra_gl_get(mapper->ra);
100
101 if (p->rtarget_h && p->device_h) {
102 if (!gl->DXUnlockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
103 MP_ERR(mapper, "Failed unlocking texture for access by OpenGL: %s\n",
104 mp_LastError_to_str());
105 }
106 }
107
108 if (p->rtarget_h) {
109 if (!gl->DXUnregisterObjectNV(p->device_h, p->rtarget_h)) {
110 MP_ERR(mapper, "Failed to unregister Direct3D surface with OpenGL: %s\n",
111 mp_LastError_to_str());
112 } else {
113 p->rtarget_h = 0;
114 }
115 }
116
117 gl->DeleteTextures(1, &p->texture);
118 p->texture = 0;
119
120 if (p->rtarget) {
121 IDirect3DSurface9_Release(p->rtarget);
122 p->rtarget = NULL;
123 }
124
125 ra_tex_free(mapper->ra, &mapper->tex[0]);
126 }
127
128 static int mapper_init(struct ra_hwdec_mapper *mapper)
129 {
130 struct priv_owner *p_owner = mapper->owner->priv;
131 struct priv *p = mapper->priv;
132 GL *gl = ra_gl_get(mapper->ra);
133 HRESULT hr;
134
135 p->device = p_owner->device;
136 p->device_h = p_owner->device_h;
137
138 HANDLE share_handle = NULL;
139 hr = IDirect3DDevice9Ex_CreateRenderTarget(
140 p->device,
141 mapper->src_params.w, mapper->src_params.h,
142 SHARED_SURFACE_D3DFMT, D3DMULTISAMPLE_NONE, 0, FALSE,
143 &p->rtarget, &share_handle);
144 if (FAILED(hr)) {
145 MP_ERR(mapper, "Failed creating offscreen Direct3D surface: %s\n",
146 mp_HRESULT_to_str(hr));
147 return -1;
148 }
149
150 if (share_handle &&
151 !gl->DXSetResourceShareHandleNV(p->rtarget, share_handle)) {
152 MP_ERR(mapper, "Failed setting Direct3D/OpenGL share handle for surface: %s\n",
153 mp_LastError_to_str());
154 return -1;
155 }
156
157 gl->GenTextures(1, &p->texture);
158 gl->BindTexture(GL_TEXTURE_2D, p->texture);
159 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
160 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
161 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
162 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
163 gl->BindTexture(GL_TEXTURE_2D, 0);
164
165 p->rtarget_h = gl->DXRegisterObjectNV(p->device_h, p->rtarget, p->texture,
166 GL_TEXTURE_2D,
167 WGL_ACCESS_READ_ONLY_NV);
168 if (!p->rtarget_h) {
169 MP_ERR(mapper, "Failed to register Direct3D surface with OpenGL: %s\n",
170 mp_LastError_to_str());
171 return -1;
172 }
173
174 if (!gl->DXLockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
175 MP_ERR(mapper, "Failed locking texture for access by OpenGL %s\n",
176 mp_LastError_to_str());
177 return -1;
178 }
179
180 struct ra_tex_params params = {
181 .dimensions = 2,
182 .w = mapper->src_params.w,
183 .h = mapper->src_params.h,
184 .d = 1,
185 .format = ra_find_unorm_format(mapper->ra, 1, 4),
186 .render_src = true,
187 .src_linear = true,
188 };
189 if (!params.format)
190 return -1;
191
192 mapper->tex[0] = ra_create_wrapped_tex(mapper->ra, &params, p->texture);
193 if (!mapper->tex[0])
194 return -1;
195
196 mapper->dst_params = mapper->src_params;
197 mapper->dst_params.imgfmt = IMGFMT_RGB0;
198 mapper->dst_params.hw_subfmt = 0;
199
200 return 0;
201 }
202
203 static int mapper_map(struct ra_hwdec_mapper *mapper)
204 {
205 struct priv *p = mapper->priv;
206 GL *gl = ra_gl_get(mapper->ra);
207 HRESULT hr;
208
209 if (!gl->DXUnlockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
210 MP_ERR(mapper, "Failed unlocking texture for access by OpenGL: %s\n",
211 mp_LastError_to_str());
212 return -1;
213 }
214
215 IDirect3DSurface9* hw_surface = (IDirect3DSurface9 *)mapper->src->planes[3];
216 RECT rc = {0, 0, mapper->src->w, mapper->src->h};
217 hr = IDirect3DDevice9Ex_StretchRect(p->device,
218 hw_surface, &rc,
219 p->rtarget, &rc,
220 D3DTEXF_NONE);
221 if (FAILED(hr)) {
222 MP_ERR(mapper, "Direct3D RGB conversion failed: %s", mp_HRESULT_to_str(hr));
223 return -1;
224 }
225
226 if (!gl->DXLockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
227 MP_ERR(mapper, "Failed locking texture for access by OpenGL: %s\n",
228 mp_LastError_to_str());
229 return -1;
230 }
231
232 return 0;
233 }
234
235 const struct ra_hwdec_driver ra_hwdec_dxva2gldx = {
236 .name = "dxva2-dxinterop",
237 .priv_size = sizeof(struct priv_owner),
238 .imgfmts = {IMGFMT_DXVA2, 0},
239 .device_type = AV_HWDEVICE_TYPE_DXVA2,
240 .init = init,
241 .uninit = uninit,
242 .mapper = &(const struct ra_hwdec_mapper_driver){
243 .priv_size = sizeof(struct priv),
244 .init = mapper_init,
245 .uninit = mapper_uninit,
246 .map = mapper_map,
247 },
248 };