aboutsummaryrefslogtreecommitdiffhomepage
path: root/video/out/opengl/hwdec_dxva2gldx.c
blob: 4cd8c1c0b2b9cfdf36e7800e12d5ef38a2588225 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * This file is part of mpv.
 *
 * mpv is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * mpv is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <d3d9.h>
#include <assert.h>

#include "common/common.h"
#include "osdep/windows_utils.h"
#include "hwdec.h"
#include "video/hwdec.h"

// for  WGL_ACCESS_READ_ONLY_NV
#include <GL/wglext.h>

#define SHARED_SURFACE_D3DFMT D3DFMT_X8R8G8B8
#define SHARED_SURFACE_MPFMT  IMGFMT_RGB0
struct priv {
    struct mp_hwdec_ctx hwctx;
    IDirect3DDevice9Ex *device;
    HANDLE device_h;

    IDirect3DSurface9 *rtarget;
    HANDLE rtarget_h;
    GLuint texture;
};

static void destroy_objects(struct gl_hwdec *hw)
{
    struct priv *p = hw->priv;
    GL *gl = hw->gl;

    if (p->rtarget_h && p->device_h) {
        if (!gl->DXUnlockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
            MP_ERR(hw, "Failed unlocking texture for access by OpenGL: %s\n",
                   mp_LastError_to_str());
        }
    }

    if (p->rtarget_h) {
        if (!gl->DXUnregisterObjectNV(p->device_h, p->rtarget_h)) {
            MP_ERR(hw, "Failed to unregister Direct3D surface with OpenGL: %s\n",
                   mp_LastError_to_str());
        } else {
            p->rtarget_h = 0;
        }
    }

    gl->DeleteTextures(1, &p->texture);
    p->texture = 0;

    if (p->rtarget) {
        IDirect3DSurface9_Release(p->rtarget);
        p->rtarget = NULL;
    }
}

static void destroy(struct gl_hwdec *hw)
{
    struct priv *p = hw->priv;
    destroy_objects(hw);

    hwdec_devices_remove(hw->devs, &p->hwctx);

    if (p->device)
        IDirect3DDevice9Ex_Release(p->device);
}

static int create(struct gl_hwdec *hw)
{
    GL *gl = hw->gl;
    if (!gl->MPGetNativeDisplay || !(gl->mpgl_caps & MPGL_CAP_DXINTEROP))
        return -1;

    struct priv *p = talloc_zero(hw, struct priv);
    hw->priv = p;

    // AMD drivers won't open multiple dxinterop HANDLES on the same D3D device,
    // so we request the one already in use by context_dxinterop
    p->device_h = gl->MPGetNativeDisplay("dxinterop_device_HANDLE");
    if (!p->device_h)
        return -1;

    // But we also still need the actual D3D device
    p->device = gl->MPGetNativeDisplay("IDirect3DDevice9Ex");
    if (!p->device)
        return -1;
    IDirect3DDevice9Ex_AddRef(p->device);

    p->hwctx = (struct mp_hwdec_ctx){
        .type = HWDEC_DXVA2,
        .driver_name = hw->driver->name,
        .ctx = (IDirect3DDevice9 *)p->device,
    };
    hwdec_devices_add(hw->devs, &p->hwctx);
    return 0;
}

static int reinit(struct gl_hwdec *hw, struct mp_image_params *params)
{
    struct priv *p = hw->priv;
    GL *gl = hw->gl;
    HRESULT hr;

    destroy_objects(hw);

    HANDLE share_handle = NULL;
    hr = IDirect3DDevice9Ex_CreateRenderTarget(
        p->device,
        params->w, params->h,
        SHARED_SURFACE_D3DFMT, D3DMULTISAMPLE_NONE, 0, FALSE,
        &p->rtarget, &share_handle);
    if (FAILED(hr)) {
        MP_ERR(hw, "Failed creating offscreen Direct3D surface: %s\n",
               mp_HRESULT_to_str(hr));
        goto fail;
    }

    if (share_handle &&
        !gl->DXSetResourceShareHandleNV(p->rtarget, share_handle)) {
        MP_ERR(hw, "Failed setting Direct3D/OpenGL share handle for surface: %s\n",
               mp_LastError_to_str());
        goto fail;
    }

    gl->GenTextures(1, &p->texture);
    gl->BindTexture(GL_TEXTURE_2D, p->texture);
    gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    gl->BindTexture(GL_TEXTURE_2D, 0);

    p->rtarget_h = gl->DXRegisterObjectNV(p->device_h, p->rtarget, p->texture,
                                          GL_TEXTURE_2D,
                                          WGL_ACCESS_READ_ONLY_NV);
    if (!p->rtarget_h) {
        MP_ERR(hw, "Failed to register Direct3D surface with OpenGL: %s\n",
               mp_LastError_to_str());
        goto fail;
    }

    if (!gl->DXLockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
        MP_ERR(hw, "Failed locking texture for access by OpenGL %s\n",
               mp_LastError_to_str());
        goto fail;
    }

    params->imgfmt = SHARED_SURFACE_MPFMT;

    return 0;
fail:
    destroy_objects(hw);
    return -1;
}

static int map_frame(struct gl_hwdec *hw, struct mp_image *hw_image,
                     struct gl_hwdec_frame *out_frame)
{
    assert(hw_image && hw_image->imgfmt == hw->driver->imgfmt);
    GL *gl = hw->gl;
    struct priv *p = hw->priv;
    HRESULT hr;

    if (!gl->DXUnlockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
        MP_ERR(hw, "Failed unlocking texture for access by OpenGL: %s\n",
               mp_LastError_to_str());
        return -1;
    }

    IDirect3DSurface9* hw_surface = (IDirect3DSurface9 *)hw_image->planes[3];
    RECT rc = {0, 0, hw_image->w, hw_image->h};
    hr = IDirect3DDevice9Ex_StretchRect(p->device,
                                        hw_surface, &rc,
                                        p->rtarget, &rc,
                                        D3DTEXF_NONE);
    if (FAILED(hr)) {
        MP_ERR(hw, "Direct3D RGB conversion failed: %s", mp_HRESULT_to_str(hr));
        return -1;
    }

    if (!gl->DXLockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
        MP_ERR(hw, "Failed locking texture for access by OpenGL: %s\n",
               mp_LastError_to_str());
        return -1;
    }

    *out_frame = (struct gl_hwdec_frame){
        .planes = {
            {
                .gl_texture = p->texture,
                .gl_target = GL_TEXTURE_2D,
                .tex_w = hw_image->w,
                .tex_h = hw_image->h,
            },
        },
    };
    return 0;
}

const struct gl_hwdec_driver gl_hwdec_dxva2gldx = {
    .name = "dxva2-dxinterop",
    .api = HWDEC_DXVA2,
    .imgfmt = IMGFMT_DXVA2,
    .create = create,
    .reinit = reinit,
    .map_frame = map_frame,
    .destroy = destroy,
};