| 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 <d3d11.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 |
#ifndef EGL_D3D_TEXTURE_SUBRESOURCE_ID_ANGLE |
| 36 |
#define EGL_D3D_TEXTURE_SUBRESOURCE_ID_ANGLE 0x33AB |
| 37 |
#endif |
| 38 |
|
| 39 |
struct priv_owner { |
| 40 |
struct mp_hwdec_ctx hwctx; |
| 41 |
|
| 42 |
ID3D11Device *d3d11_device; |
| 43 |
EGLDisplay egl_display; |
| 44 |
|
| 45 |
|
| 46 |
EGLStreamKHR (EGLAPIENTRY *CreateStreamKHR)(EGLDisplay dpy, |
| 47 |
const EGLint *attrib_list); |
| 48 |
EGLBoolean (EGLAPIENTRY *DestroyStreamKHR)(EGLDisplay dpy, |
| 49 |
EGLStreamKHR stream); |
| 50 |
|
| 51 |
|
| 52 |
EGLBoolean (EGLAPIENTRY *StreamConsumerAcquireKHR) |
| 53 |
(EGLDisplay dpy, EGLStreamKHR stream); |
| 54 |
EGLBoolean (EGLAPIENTRY *StreamConsumerReleaseKHR) |
| 55 |
(EGLDisplay dpy, EGLStreamKHR stream); |
| 56 |
|
| 57 |
|
| 58 |
EGLBoolean (EGLAPIENTRY *StreamConsumerGLTextureExternalAttribsNV) |
| 59 |
(EGLDisplay dpy, EGLStreamKHR stream, EGLAttrib *attrib_list); |
| 60 |
|
| 61 |
|
| 62 |
EGLBoolean (EGLAPIENTRY *CreateStreamProducerD3DTextureANGLE) |
| 63 |
(EGLDisplay dpy, EGLStreamKHR stream, const EGLAttrib *attrib_list); |
| 64 |
EGLBoolean (EGLAPIENTRY *StreamPostD3DTextureANGLE) |
| 65 |
(EGLDisplay dpy, EGLStreamKHR stream, void *texture, |
| 66 |
const EGLAttrib *attrib_list); |
| 67 |
}; |
| 68 |
|
| 69 |
struct priv { |
| 70 |
EGLStreamKHR egl_stream; |
| 71 |
GLuint gl_textures[2]; |
| 72 |
}; |
| 73 |
|
| 74 |
static void uninit(struct ra_hwdec *hw) |
| 75 |
{ |
| 76 |
struct priv_owner *p = hw->priv; |
| 77 |
|
| 78 |
hwdec_devices_remove(hw->devs, &p->hwctx); |
| 79 |
|
| 80 |
if (p->d3d11_device) |
| 81 |
ID3D11Device_Release(p->d3d11_device); |
| 82 |
} |
| 83 |
|
| 84 |
static int init(struct ra_hwdec *hw) |
| 85 |
{ |
| 86 |
struct priv_owner *p = hw->priv; |
| 87 |
HRESULT hr; |
| 88 |
|
| 89 |
if (!ra_is_gl(hw->ra_ctx->ra)) |
| 90 |
return -1; |
| 91 |
if (!angle_load()) |
| 92 |
return -1; |
| 93 |
|
| 94 |
EGLDisplay egl_display = eglGetCurrentDisplay(); |
| 95 |
if (!egl_display) |
| 96 |
return -1; |
| 97 |
|
| 98 |
if (!eglGetCurrentContext()) |
| 99 |
return -1; |
| 100 |
|
| 101 |
GL *gl = ra_gl_get(hw->ra_ctx->ra); |
| 102 |
|
| 103 |
const char *exts = eglQueryString(egl_display, EGL_EXTENSIONS); |
| 104 |
if (!gl_check_extension(exts, "EGL_ANGLE_d3d_share_handle_client_buffer") || |
| 105 |
!gl_check_extension(exts, "EGL_ANGLE_stream_producer_d3d_texture") || |
| 106 |
!(gl_check_extension(gl->extensions, "GL_OES_EGL_image_external_essl3") || |
| 107 |
gl->es == 200) || |
| 108 |
!gl_check_extension(exts, "EGL_EXT_device_query") || |
| 109 |
!(gl->mpgl_caps & MPGL_CAP_TEX_RG)) |
| 110 |
return -1; |
| 111 |
|
| 112 |
p->egl_display = egl_display; |
| 113 |
|
| 114 |
p->CreateStreamKHR = (void *)eglGetProcAddress("eglCreateStreamKHR"); |
| 115 |
p->DestroyStreamKHR = (void *)eglGetProcAddress("eglDestroyStreamKHR"); |
| 116 |
p->StreamConsumerAcquireKHR = |
| 117 |
(void *)eglGetProcAddress("eglStreamConsumerAcquireKHR"); |
| 118 |
p->StreamConsumerReleaseKHR = |
| 119 |
(void *)eglGetProcAddress("eglStreamConsumerReleaseKHR"); |
| 120 |
p->StreamConsumerGLTextureExternalAttribsNV = |
| 121 |
(void *)eglGetProcAddress("eglStreamConsumerGLTextureExternalAttribsNV"); |
| 122 |
p->CreateStreamProducerD3DTextureANGLE = |
| 123 |
(void *)eglGetProcAddress("eglCreateStreamProducerD3DTextureANGLE"); |
| 124 |
p->StreamPostD3DTextureANGLE = |
| 125 |
(void *)eglGetProcAddress("eglStreamPostD3DTextureANGLE"); |
| 126 |
|
| 127 |
if (!p->CreateStreamKHR || !p->DestroyStreamKHR || |
| 128 |
!p->StreamConsumerAcquireKHR || !p->StreamConsumerReleaseKHR || |
| 129 |
!p->StreamConsumerGLTextureExternalAttribsNV || |
| 130 |
!p->CreateStreamProducerD3DTextureANGLE || |
| 131 |
!p->StreamPostD3DTextureANGLE) |
| 132 |
{ |
| 133 |
MP_ERR(hw, "Failed to load some EGLStream functions.\n"); |
| 134 |
goto fail; |
| 135 |
} |
| 136 |
|
| 137 |
static const char *es2_exts[] = {"GL_NV_EGL_stream_consumer_external", 0}; |
| 138 |
static const char *es3_exts[] = {"GL_NV_EGL_stream_consumer_external", |
| 139 |
"GL_OES_EGL_image_external_essl3", 0}; |
| 140 |
hw->glsl_extensions = gl->es == 200 ? es2_exts : es3_exts; |
| 141 |
|
| 142 |
PFNEGLQUERYDISPLAYATTRIBEXTPROC p_eglQueryDisplayAttribEXT = |
| 143 |
(void *)eglGetProcAddress("eglQueryDisplayAttribEXT"); |
| 144 |
PFNEGLQUERYDEVICEATTRIBEXTPROC p_eglQueryDeviceAttribEXT = |
| 145 |
(void *)eglGetProcAddress("eglQueryDeviceAttribEXT"); |
| 146 |
if (!p_eglQueryDisplayAttribEXT || !p_eglQueryDeviceAttribEXT) |
| 147 |
goto fail; |
| 148 |
|
| 149 |
EGLAttrib device = 0; |
| 150 |
if (!p_eglQueryDisplayAttribEXT(egl_display, EGL_DEVICE_EXT, &device)) |
| 151 |
goto fail; |
| 152 |
EGLAttrib d3d_device = 0; |
| 153 |
if (!p_eglQueryDeviceAttribEXT((EGLDeviceEXT)device, |
| 154 |
EGL_D3D11_DEVICE_ANGLE, &d3d_device)) |
| 155 |
{ |
| 156 |
MP_ERR(hw, "Could not get EGL_D3D11_DEVICE_ANGLE from ANGLE.\n"); |
| 157 |
goto fail; |
| 158 |
} |
| 159 |
|
| 160 |
p->d3d11_device = (ID3D11Device *)d3d_device; |
| 161 |
if (!p->d3d11_device) |
| 162 |
goto fail; |
| 163 |
ID3D11Device_AddRef(p->d3d11_device); |
| 164 |
|
| 165 |
if (!d3d11_check_decoding(p->d3d11_device)) { |
| 166 |
MP_VERBOSE(hw, "D3D11 video decoding not supported on this system.\n"); |
| 167 |
goto fail; |
| 168 |
} |
| 169 |
|
| 170 |
ID3D10Multithread *multithread; |
| 171 |
hr = ID3D11Device_QueryInterface(p->d3d11_device, &IID_ID3D10Multithread, |
| 172 |
(void **)&multithread); |
| 173 |
if (FAILED(hr)) { |
| 174 |
MP_ERR(hw, "Failed to get Multithread interface: %s\n", |
| 175 |
mp_HRESULT_to_str(hr)); |
| 176 |
goto fail; |
| 177 |
} |
| 178 |
ID3D10Multithread_SetMultithreadProtected(multithread, TRUE); |
| 179 |
ID3D10Multithread_Release(multithread); |
| 180 |
|
| 181 |
static const int subfmts[] = {IMGFMT_NV12, IMGFMT_P010, 0}; |
| 182 |
p->hwctx = (struct mp_hwdec_ctx){ |
| 183 |
.driver_name = hw->driver->name, |
| 184 |
.av_device_ref = d3d11_wrap_device_ref(p->d3d11_device), |
| 185 |
.supported_formats = subfmts, |
| 186 |
.hw_imgfmt = IMGFMT_D3D11, |
| 187 |
}; |
| 188 |
|
| 189 |
if (!p->hwctx.av_device_ref) { |
| 190 |
MP_VERBOSE(hw, "Failed to create hwdevice_ctx\n"); |
| 191 |
return -1; |
| 192 |
} |
| 193 |
|
| 194 |
hwdec_devices_add(hw->devs, &p->hwctx); |
| 195 |
|
| 196 |
return 0; |
| 197 |
fail: |
| 198 |
return -1; |
| 199 |
} |
| 200 |
|
| 201 |
static void mapper_uninit(struct ra_hwdec_mapper *mapper) |
| 202 |
{ |
| 203 |
struct priv_owner *o = mapper->owner->priv; |
| 204 |
struct priv *p = mapper->priv; |
| 205 |
GL *gl = ra_gl_get(mapper->ra); |
| 206 |
|
| 207 |
if (p->egl_stream) |
| 208 |
o->DestroyStreamKHR(o->egl_display, p->egl_stream); |
| 209 |
p->egl_stream = 0; |
| 210 |
|
| 211 |
gl->DeleteTextures(2, p->gl_textures); |
| 212 |
} |
| 213 |
|
| 214 |
static int mapper_init(struct ra_hwdec_mapper *mapper) |
| 215 |
{ |
| 216 |
struct priv_owner *o = mapper->owner->priv; |
| 217 |
struct priv *p = mapper->priv; |
| 218 |
GL *gl = ra_gl_get(mapper->ra); |
| 219 |
|
| 220 |
struct ra_imgfmt_desc desc = {0}; |
| 221 |
|
| 222 |
ra_get_imgfmt_desc(mapper->ra, mapper->src_params.hw_subfmt, &desc); |
| 223 |
|
| 224 |
|
| 225 |
if ((mapper->src_params.hw_subfmt != IMGFMT_NV12 && |
| 226 |
mapper->src_params.hw_subfmt != IMGFMT_P010) || |
| 227 |
desc.num_planes < 1 || desc.num_planes > 2) |
| 228 |
{ |
| 229 |
MP_FATAL(mapper, "Format not supported.\n"); |
| 230 |
return -1; |
| 231 |
} |
| 232 |
|
| 233 |
mapper->dst_params = mapper->src_params; |
| 234 |
mapper->dst_params.imgfmt = mapper->src_params.hw_subfmt; |
| 235 |
mapper->dst_params.hw_subfmt = 0; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
int texunits = 0; |
| 240 |
int num_planes = desc.num_planes; |
| 241 |
int gl_target = GL_TEXTURE_EXTERNAL_OES; |
| 242 |
|
| 243 |
p->egl_stream = o->CreateStreamKHR(o->egl_display, (EGLint[]){EGL_NONE}); |
| 244 |
if (!p->egl_stream) |
| 245 |
goto fail; |
| 246 |
|
| 247 |
EGLAttrib attrs[(2 + 2 + 1) * 2] = { |
| 248 |
EGL_COLOR_BUFFER_TYPE, EGL_YUV_BUFFER_EXT, |
| 249 |
EGL_YUV_NUMBER_OF_PLANES_EXT, num_planes, |
| 250 |
}; |
| 251 |
|
| 252 |
for (int n = 0; n < num_planes; n++) { |
| 253 |
gl->ActiveTexture(GL_TEXTURE0 + texunits + n); |
| 254 |
gl->GenTextures(1, &p->gl_textures[n]); |
| 255 |
gl->BindTexture(gl_target, p->gl_textures[n]); |
| 256 |
gl->TexParameteri(gl_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 257 |
gl->TexParameteri(gl_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 258 |
gl->TexParameteri(gl_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 259 |
gl->TexParameteri(gl_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 260 |
attrs[(2 + n) * 2 + 0] = EGL_YUV_PLANE0_TEXTURE_UNIT_NV + n; |
| 261 |
attrs[(2 + n) * 2 + 1] = texunits + n; |
| 262 |
} |
| 263 |
|
| 264 |
attrs[(2 + num_planes) * 2 + 0] = EGL_NONE; |
| 265 |
|
| 266 |
if (!o->StreamConsumerGLTextureExternalAttribsNV(o->egl_display, p->egl_stream, |
| 267 |
attrs)) |
| 268 |
goto fail; |
| 269 |
|
| 270 |
if (!o->CreateStreamProducerD3DTextureANGLE(o->egl_display, p->egl_stream, |
| 271 |
(EGLAttrib[]){EGL_NONE})) |
| 272 |
goto fail; |
| 273 |
|
| 274 |
for (int n = 0; n < num_planes; n++) { |
| 275 |
gl->ActiveTexture(GL_TEXTURE0 + texunits + n); |
| 276 |
gl->BindTexture(gl_target, 0); |
| 277 |
} |
| 278 |
gl->ActiveTexture(GL_TEXTURE0); |
| 279 |
return 0; |
| 280 |
fail: |
| 281 |
gl->ActiveTexture(GL_TEXTURE0); |
| 282 |
MP_ERR(mapper, "Failed to create EGLStream\n"); |
| 283 |
return -1; |
| 284 |
} |
| 285 |
|
| 286 |
static int mapper_map(struct ra_hwdec_mapper *mapper) |
| 287 |
{ |
| 288 |
struct priv_owner *o = mapper->owner->priv; |
| 289 |
struct priv *p = mapper->priv; |
| 290 |
|
| 291 |
ID3D11Texture2D *d3d_tex = (void *)mapper->src->planes[0]; |
| 292 |
int d3d_subindex = (intptr_t)mapper->src->planes[1]; |
| 293 |
if (!d3d_tex) |
| 294 |
return -1; |
| 295 |
|
| 296 |
EGLAttrib attrs[] = { |
| 297 |
EGL_D3D_TEXTURE_SUBRESOURCE_ID_ANGLE, d3d_subindex, |
| 298 |
EGL_NONE, |
| 299 |
}; |
| 300 |
if (!o->StreamPostD3DTextureANGLE(o->egl_display, p->egl_stream, |
| 301 |
(void *)d3d_tex, attrs)) |
| 302 |
{ |
| 303 |
|
| 304 |
attrs[0] = attrs[0] == 0x33AB ? 0x3AAB : 0x33AB; |
| 305 |
if (!o->StreamPostD3DTextureANGLE(o->egl_display, p->egl_stream, |
| 306 |
(void *)d3d_tex, attrs)) |
| 307 |
return -1; |
| 308 |
} |
| 309 |
|
| 310 |
if (!o->StreamConsumerAcquireKHR(o->egl_display, p->egl_stream)) |
| 311 |
return -1; |
| 312 |
|
| 313 |
D3D11_TEXTURE2D_DESC texdesc; |
| 314 |
ID3D11Texture2D_GetDesc(d3d_tex, &texdesc); |
| 315 |
|
| 316 |
for (int n = 0; n < 2; n++) { |
| 317 |
struct ra_tex_params params = { |
| 318 |
.dimensions = 2, |
| 319 |
.w = texdesc.Width / (n ? 2 : 1), |
| 320 |
.h = texdesc.Height / (n ? 2 : 1), |
| 321 |
.d = 1, |
| 322 |
.format = ra_find_unorm_format(mapper->ra, 1, n ? 2 : 1), |
| 323 |
.render_src = true, |
| 324 |
.src_linear = true, |
| 325 |
.external_oes = true, |
| 326 |
}; |
| 327 |
if (!params.format) |
| 328 |
return -1; |
| 329 |
|
| 330 |
mapper->tex[n] = ra_create_wrapped_tex(mapper->ra, ¶ms, |
| 331 |
p->gl_textures[n]); |
| 332 |
if (!mapper->tex[n]) |
| 333 |
return -1; |
| 334 |
} |
| 335 |
|
| 336 |
return 0; |
| 337 |
} |
| 338 |
|
| 339 |
static void mapper_unmap(struct ra_hwdec_mapper *mapper) |
| 340 |
{ |
| 341 |
struct priv_owner *o = mapper->owner->priv; |
| 342 |
struct priv *p = mapper->priv; |
| 343 |
|
| 344 |
for (int n = 0; n < 2; n++) |
| 345 |
ra_tex_free(mapper->ra, &mapper->tex[n]); |
| 346 |
if (p->egl_stream) |
| 347 |
o->StreamConsumerReleaseKHR(o->egl_display, p->egl_stream); |
| 348 |
} |
| 349 |
|
| 350 |
const struct ra_hwdec_driver ra_hwdec_d3d11egl = { |
| 351 |
.name = "d3d11-egl", |
| 352 |
.priv_size = sizeof(struct priv_owner), |
| 353 |
.imgfmts = {IMGFMT_D3D11, 0}, |
| 354 |
.device_type = AV_HWDEVICE_TYPE_D3D11VA, |
| 355 |
.init = init, |
| 356 |
.uninit = uninit, |
| 357 |
.mapper = &(const struct ra_hwdec_mapper_driver){ |
| 358 |
.priv_size = sizeof(struct priv), |
| 359 |
.init = mapper_init, |
| 360 |
.uninit = mapper_uninit, |
| 361 |
.map = mapper_map, |
| 362 |
.unmap = mapper_unmap, |
| 363 |
}, |
| 364 |
}; |