| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <assert.h> |
| 19 |
#include <windows.h> |
| 20 |
#include <d3d9.h> |
| 21 |
|
| 22 |
#include <EGL/egl.h> |
| 23 |
#include <EGL/eglext.h> |
| 24 |
|
| 25 |
#include "angle_dynamic.h" |
| 26 |
|
| 27 |
#include "common/common.h" |
| 28 |
#include "osdep/timer.h" |
| 29 |
#include "osdep/windows_utils.h" |
| 30 |
#include "video/out/gpu/hwdec.h" |
| 31 |
#include "ra_gl.h" |
| 32 |
#include "video/hwdec.h" |
| 33 |
#include "video/d3d.h" |
| 34 |
|
| 35 |
struct priv_owner { |
| 36 |
struct mp_hwdec_ctx hwctx; |
| 37 |
IDirect3D9Ex *d3d9ex; |
| 38 |
IDirect3DDevice9Ex *device9ex; |
| 39 |
|
| 40 |
EGLDisplay egl_display; |
| 41 |
EGLConfig egl_config; |
| 42 |
EGLint alpha; |
| 43 |
}; |
| 44 |
|
| 45 |
struct priv { |
| 46 |
IDirect3DDevice9Ex *device9ex; |
| 47 |
IDirect3DQuery9 *query9; |
| 48 |
IDirect3DTexture9 *texture9; |
| 49 |
IDirect3DSurface9 *surface9; |
| 50 |
|
| 51 |
EGLDisplay egl_display; |
| 52 |
EGLSurface egl_surface; |
| 53 |
|
| 54 |
GLuint gl_texture; |
| 55 |
}; |
| 56 |
|
| 57 |
static void uninit(struct ra_hwdec *hw) |
| 58 |
{ |
| 59 |
struct priv_owner *p = hw->priv; |
| 60 |
|
| 61 |
hwdec_devices_remove(hw->devs, &p->hwctx); |
| 62 |
av_buffer_unref(&p->hwctx.av_device_ref); |
| 63 |
|
| 64 |
if (p->device9ex) |
| 65 |
IDirect3DDevice9Ex_Release(p->device9ex); |
| 66 |
|
| 67 |
if (p->d3d9ex) |
| 68 |
IDirect3D9Ex_Release(p->d3d9ex); |
| 69 |
} |
| 70 |
|
| 71 |
static int init(struct ra_hwdec *hw) |
| 72 |
{ |
| 73 |
struct priv_owner *p = hw->priv; |
| 74 |
HRESULT hr; |
| 75 |
|
| 76 |
if (!ra_is_gl(hw->ra_ctx->ra)) |
| 77 |
return -1; |
| 78 |
if (!angle_load()) |
| 79 |
return -1; |
| 80 |
|
| 81 |
d3d_load_dlls(); |
| 82 |
|
| 83 |
EGLDisplay egl_display = eglGetCurrentDisplay(); |
| 84 |
if (!egl_display) |
| 85 |
return -1; |
| 86 |
|
| 87 |
if (!eglGetCurrentContext()) |
| 88 |
return -1; |
| 89 |
|
| 90 |
const char *exts = eglQueryString(egl_display, EGL_EXTENSIONS); |
| 91 |
if (!gl_check_extension(exts, "EGL_ANGLE_d3d_share_handle_client_buffer")) { |
| 92 |
return -1; |
| 93 |
} |
| 94 |
|
| 95 |
p->egl_display = egl_display; |
| 96 |
|
| 97 |
if (!d3d9_dll) { |
| 98 |
MP_FATAL(hw, "Failed to load \"d3d9.dll\": %s\n", |
| 99 |
mp_LastError_to_str()); |
| 100 |
goto fail; |
| 101 |
} |
| 102 |
|
| 103 |
HRESULT (WINAPI *Direct3DCreate9Ex)(UINT SDKVersion, IDirect3D9Ex **ppD3D); |
| 104 |
Direct3DCreate9Ex = (void *)GetProcAddress(d3d9_dll, "Direct3DCreate9Ex"); |
| 105 |
if (!Direct3DCreate9Ex) { |
| 106 |
MP_FATAL(hw, "Direct3D 9Ex not supported\n"); |
| 107 |
goto fail; |
| 108 |
} |
| 109 |
|
| 110 |
hr = Direct3DCreate9Ex(D3D_SDK_VERSION, &p->d3d9ex); |
| 111 |
if (FAILED(hr)) { |
| 112 |
MP_FATAL(hw, "Couldn't create Direct3D9Ex: %s\n", |
| 113 |
mp_HRESULT_to_str(hr)); |
| 114 |
goto fail; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
D3DPRESENT_PARAMETERS present_params = { |
| 121 |
.BackBufferWidth = 1, |
| 122 |
.BackBufferHeight = 1, |
| 123 |
.BackBufferFormat = D3DFMT_UNKNOWN, |
| 124 |
.BackBufferCount = 1, |
| 125 |
.SwapEffect = D3DSWAPEFFECT_DISCARD, |
| 126 |
.hDeviceWindow = NULL, |
| 127 |
.Windowed = TRUE, |
| 128 |
.Flags = D3DPRESENTFLAG_VIDEO, |
| 129 |
.FullScreen_RefreshRateInHz = 0, |
| 130 |
.PresentationInterval = 0, |
| 131 |
}; |
| 132 |
hr = IDirect3D9Ex_CreateDeviceEx(p->d3d9ex, |
| 133 |
D3DADAPTER_DEFAULT, |
| 134 |
D3DDEVTYPE_HAL, |
| 135 |
NULL, |
| 136 |
D3DCREATE_FPU_PRESERVE | |
| 137 |
D3DCREATE_HARDWARE_VERTEXPROCESSING | |
| 138 |
D3DCREATE_DISABLE_PSGP_THREADING | |
| 139 |
D3DCREATE_MULTITHREADED, |
| 140 |
&present_params, |
| 141 |
NULL, |
| 142 |
&p->device9ex); |
| 143 |
if (FAILED(hr)) { |
| 144 |
MP_FATAL(hw, "Failed to create Direct3D9Ex device: %s\n", |
| 145 |
mp_HRESULT_to_str(hr)); |
| 146 |
goto fail; |
| 147 |
} |
| 148 |
|
| 149 |
EGLint attrs[] = { |
| 150 |
EGL_BUFFER_SIZE, 32, |
| 151 |
EGL_RED_SIZE, 8, |
| 152 |
EGL_GREEN_SIZE, 8, |
| 153 |
EGL_BLUE_SIZE, 8, |
| 154 |
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
| 155 |
EGL_ALPHA_SIZE, 0, |
| 156 |
EGL_NONE |
| 157 |
}; |
| 158 |
EGLint count; |
| 159 |
if (!eglChooseConfig(p->egl_display, attrs, &p->egl_config, 1, &count) || |
| 160 |
!count) { |
| 161 |
MP_ERR(hw, "Failed to get EGL surface configuration\n"); |
| 162 |
goto fail; |
| 163 |
} |
| 164 |
|
| 165 |
if (!eglGetConfigAttrib(p->egl_display, p->egl_config, |
| 166 |
EGL_BIND_TO_TEXTURE_RGBA, &p->alpha)) { |
| 167 |
MP_FATAL(hw, "Failed to query EGL surface alpha\n"); |
| 168 |
goto fail; |
| 169 |
} |
| 170 |
|
| 171 |
struct mp_image_params dummy_params = { |
| 172 |
.imgfmt = IMGFMT_DXVA2, |
| 173 |
.w = 256, |
| 174 |
.h = 256, |
| 175 |
}; |
| 176 |
struct ra_hwdec_mapper *mapper = ra_hwdec_mapper_create(hw, &dummy_params); |
| 177 |
if (!mapper) |
| 178 |
goto fail; |
| 179 |
ra_hwdec_mapper_free(&mapper); |
| 180 |
|
| 181 |
p->hwctx = (struct mp_hwdec_ctx){ |
| 182 |
.driver_name = hw->driver->name, |
| 183 |
.av_device_ref = d3d9_wrap_device_ref((IDirect3DDevice9 *)p->device9ex), |
| 184 |
.hw_imgfmt = IMGFMT_DXVA2, |
| 185 |
}; |
| 186 |
|
| 187 |
if (!p->hwctx.av_device_ref) { |
| 188 |
MP_VERBOSE(hw, "Failed to create hwdevice_ctx\n"); |
| 189 |
goto fail; |
| 190 |
} |
| 191 |
|
| 192 |
hwdec_devices_add(hw->devs, &p->hwctx); |
| 193 |
|
| 194 |
return 0; |
| 195 |
fail: |
| 196 |
return -1; |
| 197 |
} |
| 198 |
|
| 199 |
static void mapper_uninit(struct ra_hwdec_mapper *mapper) |
| 200 |
{ |
| 201 |
struct priv *p = mapper->priv; |
| 202 |
GL *gl = ra_gl_get(mapper->ra); |
| 203 |
|
| 204 |
ra_tex_free(mapper->ra, &mapper->tex[0]); |
| 205 |
gl->DeleteTextures(1, &p->gl_texture); |
| 206 |
|
| 207 |
if (p->egl_display && p->egl_surface) { |
| 208 |
eglReleaseTexImage(p->egl_display, p->egl_surface, EGL_BACK_BUFFER); |
| 209 |
eglDestroySurface(p->egl_display, p->egl_surface); |
| 210 |
} |
| 211 |
|
| 212 |
if (p->surface9) |
| 213 |
IDirect3DSurface9_Release(p->surface9); |
| 214 |
|
| 215 |
if (p->texture9) |
| 216 |
IDirect3DTexture9_Release(p->texture9); |
| 217 |
|
| 218 |
if (p->query9) |
| 219 |
IDirect3DQuery9_Release(p->query9); |
| 220 |
} |
| 221 |
|
| 222 |
static int mapper_init(struct ra_hwdec_mapper *mapper) |
| 223 |
{ |
| 224 |
struct priv_owner *p_owner = mapper->owner->priv; |
| 225 |
struct priv *p = mapper->priv; |
| 226 |
GL *gl = ra_gl_get(mapper->ra); |
| 227 |
HRESULT hr; |
| 228 |
|
| 229 |
p->device9ex = p_owner->device9ex; |
| 230 |
p->egl_display = p_owner->egl_display; |
| 231 |
|
| 232 |
hr = IDirect3DDevice9_CreateQuery(p->device9ex, D3DQUERYTYPE_EVENT, |
| 233 |
&p->query9); |
| 234 |
if (FAILED(hr)) { |
| 235 |
MP_FATAL(mapper, "Failed to create Direct3D query interface: %s\n", |
| 236 |
mp_HRESULT_to_str(hr)); |
| 237 |
goto fail; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
hr = IDirect3DQuery9_Issue(p->query9, D3DISSUE_END); |
| 242 |
if (FAILED(hr)) { |
| 243 |
MP_FATAL(mapper, "Failed to issue Direct3D END test query: %s\n", |
| 244 |
mp_HRESULT_to_str(hr)); |
| 245 |
goto fail; |
| 246 |
} |
| 247 |
|
| 248 |
HANDLE share_handle = NULL; |
| 249 |
hr = IDirect3DDevice9Ex_CreateTexture(p->device9ex, |
| 250 |
mapper->src_params.w, |
| 251 |
mapper->src_params.h, |
| 252 |
1, D3DUSAGE_RENDERTARGET, |
| 253 |
p_owner->alpha ? |
| 254 |
D3DFMT_A8R8G8B8 : D3DFMT_X8R8G8B8, |
| 255 |
D3DPOOL_DEFAULT, |
| 256 |
&p->texture9, |
| 257 |
&share_handle); |
| 258 |
if (FAILED(hr)) { |
| 259 |
MP_ERR(mapper, "Failed to create Direct3D9 texture: %s\n", |
| 260 |
mp_HRESULT_to_str(hr)); |
| 261 |
goto fail; |
| 262 |
} |
| 263 |
|
| 264 |
hr = IDirect3DTexture9_GetSurfaceLevel(p->texture9, 0, &p->surface9); |
| 265 |
if (FAILED(hr)) { |
| 266 |
MP_ERR(mapper, "Failed to get Direct3D9 surface from texture: %s\n", |
| 267 |
mp_HRESULT_to_str(hr)); |
| 268 |
goto fail; |
| 269 |
} |
| 270 |
|
| 271 |
EGLint attrib_list[] = { |
| 272 |
EGL_WIDTH, mapper->src_params.w, |
| 273 |
EGL_HEIGHT, mapper->src_params.h, |
| 274 |
EGL_TEXTURE_FORMAT, p_owner->alpha ? EGL_TEXTURE_RGBA : EGL_TEXTURE_RGB, |
| 275 |
EGL_TEXTURE_TARGET, EGL_TEXTURE_2D, |
| 276 |
EGL_NONE |
| 277 |
}; |
| 278 |
p->egl_surface = eglCreatePbufferFromClientBuffer( |
| 279 |
p->egl_display, EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE, |
| 280 |
share_handle, p_owner->egl_config, attrib_list); |
| 281 |
if (p->egl_surface == EGL_NO_SURFACE) { |
| 282 |
MP_ERR(mapper, "Failed to create EGL surface\n"); |
| 283 |
goto fail; |
| 284 |
} |
| 285 |
|
| 286 |
gl->GenTextures(1, &p->gl_texture); |
| 287 |
gl->BindTexture(GL_TEXTURE_2D, p->gl_texture); |
| 288 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 289 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 290 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 291 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 292 |
gl->BindTexture(GL_TEXTURE_2D, 0); |
| 293 |
|
| 294 |
struct ra_tex_params params = { |
| 295 |
.dimensions = 2, |
| 296 |
.w = mapper->src_params.w, |
| 297 |
.h = mapper->src_params.h, |
| 298 |
.d = 1, |
| 299 |
.format = ra_find_unorm_format(mapper->ra, 1, p_owner->alpha ? 4 : 3), |
| 300 |
.render_src = true, |
| 301 |
.src_linear = true, |
| 302 |
}; |
| 303 |
if (!params.format) |
| 304 |
goto fail; |
| 305 |
|
| 306 |
mapper->tex[0] = ra_create_wrapped_tex(mapper->ra, ¶ms, p->gl_texture); |
| 307 |
if (!mapper->tex[0]) |
| 308 |
goto fail; |
| 309 |
|
| 310 |
mapper->dst_params = mapper->src_params; |
| 311 |
mapper->dst_params.imgfmt = IMGFMT_RGB0; |
| 312 |
mapper->dst_params.hw_subfmt = 0; |
| 313 |
return 0; |
| 314 |
fail: |
| 315 |
return -1; |
| 316 |
} |
| 317 |
|
| 318 |
static int mapper_map(struct ra_hwdec_mapper *mapper) |
| 319 |
{ |
| 320 |
struct priv *p = mapper->priv; |
| 321 |
GL *gl = ra_gl_get(mapper->ra); |
| 322 |
|
| 323 |
HRESULT hr; |
| 324 |
RECT rc = {0, 0, mapper->src->w, mapper->src->h}; |
| 325 |
IDirect3DSurface9* hw_surface = (IDirect3DSurface9 *)mapper->src->planes[3]; |
| 326 |
hr = IDirect3DDevice9Ex_StretchRect(p->device9ex, |
| 327 |
hw_surface, &rc, |
| 328 |
p->surface9, &rc, |
| 329 |
D3DTEXF_NONE); |
| 330 |
if (FAILED(hr)) { |
| 331 |
MP_ERR(mapper, "Direct3D RGB conversion failed: %s\n", |
| 332 |
mp_HRESULT_to_str(hr)); |
| 333 |
return -1; |
| 334 |
} |
| 335 |
|
| 336 |
hr = IDirect3DQuery9_Issue(p->query9, D3DISSUE_END); |
| 337 |
if (FAILED(hr)) { |
| 338 |
MP_ERR(mapper, "Failed to issue Direct3D END query\n"); |
| 339 |
return -1; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
const int max_retries = 8; |
| 346 |
const int64_t wait_ns = MP_TIME_MS_TO_NS(1); |
| 347 |
int retries = 0; |
| 348 |
while (true) { |
| 349 |
hr = IDirect3DQuery9_GetData(p->query9, NULL, 0, D3DGETDATA_FLUSH); |
| 350 |
if (FAILED(hr)) { |
| 351 |
MP_ERR(mapper, "Failed to query Direct3D flush state\n"); |
| 352 |
return -1; |
| 353 |
} else if (hr == S_FALSE) { |
| 354 |
if (++retries > max_retries) { |
| 355 |
MP_VERBOSE(mapper, "Failed to flush frame after %lld ms\n", |
| 356 |
(long long)MP_TIME_MS_TO_NS(wait_ns * max_retries)); |
| 357 |
break; |
| 358 |
} |
| 359 |
mp_sleep_ns(wait_ns); |
| 360 |
} else { |
| 361 |
break; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
gl->BindTexture(GL_TEXTURE_2D, p->gl_texture); |
| 366 |
eglBindTexImage(p->egl_display, p->egl_surface, EGL_BACK_BUFFER); |
| 367 |
gl->BindTexture(GL_TEXTURE_2D, 0); |
| 368 |
|
| 369 |
return 0; |
| 370 |
} |
| 371 |
|
| 372 |
const struct ra_hwdec_driver ra_hwdec_dxva2egl = { |
| 373 |
.name = "dxva2-egl", |
| 374 |
.priv_size = sizeof(struct priv_owner), |
| 375 |
.imgfmts = {IMGFMT_DXVA2, 0}, |
| 376 |
.device_type = AV_HWDEVICE_TYPE_DXVA2, |
| 377 |
.init = init, |
| 378 |
.uninit = uninit, |
| 379 |
.mapper = &(const struct ra_hwdec_mapper_driver){ |
| 380 |
.priv_size = sizeof(struct priv), |
| 381 |
.init = mapper_init, |
| 382 |
.uninit = mapper_uninit, |
| 383 |
.map = mapper_map, |
| 384 |
}, |
| 385 |
}; |