Skip to content

Commit a3755dc

Browse files
author
Mark Kendall
committed
VAAPI: Refactor VAAPIContext for plain X11 displays.
1 parent a0f824e commit a3755dc

File tree

3 files changed

+63
-35
lines changed

3 files changed

+63
-35
lines changed

mythtv/libs/libmythtv/vaapicontext.cpp

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ VAProfile preferredProfile(MythCodecID codec)
7676
class VAAPIDisplay
7777
{
7878
protected:
79-
VAAPIDisplay() : m_va_disp(NULL), m_x_disp(NULL), m_display(NULL),
80-
m_ref_count(0), m_driver(QString()) { }
79+
VAAPIDisplay(VAAPIDisplayType display_type)
80+
: m_va_disp_type(display_type),
81+
m_va_disp(NULL), m_x_disp(NULL),
82+
m_ref_count(0), m_driver(QString()) { }
8183
public:
8284
~VAAPIDisplay()
8385
{
@@ -96,40 +98,45 @@ class VAAPIDisplay
9698

9799
bool Create(void)
98100
{
99-
MythMainWindow *mw = GetMythMainWindow();
100-
if (!mw)
101-
return false;
102-
103-
MythRenderOpenGL *gl =
104-
static_cast<MythRenderOpenGL*>(mw->GetRenderDevice());
105-
106-
if (!gl)
107-
{
108-
LOG(VB_PLAYBACK, LOG_ERR, LOC +
109-
QString("Failed to get OpenGL context - you must use the "
110-
"OpenGL UI painter for VAAPI support."));
111-
return false;
112-
}
113-
gl->makeCurrent();
114-
m_display = glXGetCurrentDisplay();
115-
gl->doneCurrent();
116-
117101
m_x_disp = OpenMythXDisplay();
118102
if (!m_x_disp)
119103
return false;
120104

121105
MythXLocker locker(m_x_disp);
122-
int major_ver, minor_ver;
123106

124-
//m_va_disp = vaGetDisplayGLX(m_x_disp->GetDisplay());
125-
m_va_disp = vaGetDisplayGLX(m_display);
107+
if (m_va_disp_type == kVADisplayGLX)
108+
{
109+
MythMainWindow *mw = GetMythMainWindow();
110+
if (!mw)
111+
return false;
112+
MythRenderOpenGL *gl =
113+
static_cast<MythRenderOpenGL*>(mw->GetRenderDevice());
114+
if (!gl)
115+
{
116+
LOG(VB_PLAYBACK, LOG_ERR, LOC +
117+
QString("Failed to get OpenGL context - you must use the "
118+
"OpenGL UI painter for VAAPI GLX support."));
119+
return false;
120+
}
121+
122+
gl->makeCurrent();
123+
Display *display = glXGetCurrentDisplay();
124+
gl->doneCurrent();
125+
126+
m_va_disp = vaGetDisplayGLX(display);
127+
}
128+
else
129+
{
130+
m_va_disp = vaGetDisplay(m_x_disp->GetDisplay());
131+
}
126132

127133
if (!m_va_disp)
128134
{
129135
LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to create VADisplay");
130136
return false;
131137
}
132138

139+
int major_ver, minor_ver;
133140
INIT_ST;
134141
va_status = vaInitialize(m_va_disp, &major_ver, &minor_ver);
135142
CHECK_ST;
@@ -148,7 +155,9 @@ class VAAPIDisplay
148155
if (ok)
149156
{
150157
UpRef();
151-
LOG(VB_PLAYBACK, LOG_INFO, LOC + "Created VAAPI GLX display");
158+
LOG(VB_PLAYBACK, LOG_INFO, LOC +
159+
QString("Created VAAPI %1 display")
160+
.arg(m_va_disp_type == kVADisplayGLX ? "GLX" : "X11"));
152161
}
153162
return ok;
154163
}
@@ -181,15 +190,21 @@ class VAAPIDisplay
181190
return ret;
182191
}
183192

184-
static VAAPIDisplay* GetDisplay(void)
193+
static VAAPIDisplay* GetDisplay(VAAPIDisplayType display_type)
185194
{
186195
if (gVAAPIDisplay)
187196
{
197+
if (gVAAPIDisplay->m_va_disp_type != display_type)
198+
{
199+
LOG(VB_GENERAL, LOG_ERR, "Already have a VAAPI display "
200+
"of a different type - aborting");
201+
return NULL;
202+
}
188203
gVAAPIDisplay->UpRef();
189204
return gVAAPIDisplay;
190205
}
191206

192-
gVAAPIDisplay = new VAAPIDisplay();
207+
gVAAPIDisplay = new VAAPIDisplay(display_type);
193208
if (gVAAPIDisplay && gVAAPIDisplay->Create())
194209
return gVAAPIDisplay;
195210

@@ -199,9 +214,9 @@ class VAAPIDisplay
199214
}
200215

201216
static VAAPIDisplay *gVAAPIDisplay;
217+
VAAPIDisplayType m_va_disp_type;
202218
void *m_va_disp;
203219
MythXDisplay *m_x_disp;
204-
Display *m_display;
205220
int m_ref_count;
206221
QString m_driver;
207222
};
@@ -212,7 +227,7 @@ bool VAAPIContext::IsFormatAccelerated(QSize size, MythCodecID codec,
212227
PixelFormat &pix_fmt)
213228
{
214229
bool result = false;
215-
VAAPIContext *ctx = new VAAPIContext(codec);
230+
VAAPIContext *ctx = new VAAPIContext(kVADisplayX11, codec);
216231
if (ctx && ctx->CreateDisplay(size))
217232
{
218233
pix_fmt = ctx->GetPixelFormat();
@@ -222,8 +237,10 @@ bool VAAPIContext::IsFormatAccelerated(QSize size, MythCodecID codec,
222237
return result;
223238
}
224239

225-
VAAPIContext::VAAPIContext(MythCodecID codec)
226-
: m_codec(codec),
240+
VAAPIContext::VAAPIContext(VAAPIDisplayType display_type,
241+
MythCodecID codec)
242+
: m_dispType(display_type),
243+
m_codec(codec),
227244
m_display(NULL),
228245
m_vaProfile(VAProfileMPEG2Main)/* ?? */,
229246
m_vaEntrypoint(VAEntrypointEncSlice),
@@ -280,7 +297,7 @@ bool VAAPIContext::CreateDisplay(QSize size)
280297
{
281298
m_size = size;
282299
bool ok = true;
283-
m_display = VAAPIDisplay::GetDisplay();
300+
m_display = VAAPIDisplay::GetDisplay(m_dispType);
284301
CREATE_CHECK(!m_size.isEmpty(), "Invalid size");
285302
CREATE_CHECK(m_display != NULL, "Invalid display");
286303
CREATE_CHECK(InitDisplay(), "Invalid VADisplay");
@@ -619,7 +636,7 @@ uint8_t* VAAPIContext::GetSurfaceIDPointer(void* buf)
619636
bool VAAPIContext::CopySurfaceToTexture(const void* buf, uint texture,
620637
uint texture_type, FrameScanType scan)
621638
{
622-
if (!buf)
639+
if (!buf || (m_dispType != kVADisplayGLX))
623640
return false;
624641

625642
const vaapi_surface *surf = (vaapi_surface*)buf;
@@ -643,6 +660,9 @@ bool VAAPIContext::CopySurfaceToTexture(const void* buf, uint texture,
643660

644661
void* VAAPIContext::GetGLXSurface(uint texture, uint texture_type)
645662
{
663+
if (m_dispType != kVADisplayGLX)
664+
return NULL;
665+
646666
if (m_glxSurfaces.contains(texture))
647667
return m_glxSurfaces.value(texture);
648668

@@ -667,7 +687,7 @@ void* VAAPIContext::GetGLXSurface(uint texture, uint texture_type)
667687

668688
void VAAPIContext::ClearGLXSurfaces(void)
669689
{
670-
if (!m_display)
690+
if (!m_display || (m_dispType != kVADisplayGLX))
671691
return;
672692

673693
MythXLocker locker(m_display->m_x_disp);

mythtv/libs/libmythtv/vaapicontext.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
extern "C" {
55
#include "libavcodec/vaapi.h"
66
}
7+
#include "va/va_x11.h"
78
#include "va/va_glx.h"
89
#include "videocolourspace.h"
910

@@ -15,12 +16,18 @@ struct vaapi_surface
1516
class VAAPIDisplay;
1617
class OpenGLVideo;
1718

19+
enum VAAPIDisplayType
20+
{
21+
kVADisplayX11,
22+
kVADisplayGLX,
23+
};
24+
1825
class VAAPIContext
1926
{
2027
public:
2128
static bool IsFormatAccelerated(QSize size, MythCodecID codec,
2229
PixelFormat &pix_fmt);
23-
VAAPIContext(MythCodecID codec);
30+
VAAPIContext(VAAPIDisplayType display_type, MythCodecID codec);
2431
~VAAPIContext();
2532

2633
bool CreateDisplay(QSize size);
@@ -43,6 +50,7 @@ class VAAPIContext
4350
void InitPictureAttributes(VideoColourSpace &colourspace);
4451
int SetPictureAttribute(PictureAttribute attribute, int newValue);
4552

53+
VAAPIDisplayType m_dispType;
4654
vaapi_context m_ctx;
4755
MythCodecID m_codec;
4856
QSize m_size;

mythtv/libs/libmythtv/videoout_openglvaapi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ bool VideoOutputOpenGLVAAPI::CreateVAAPIContext(QSize size)
124124
if (m_ctx)
125125
DeleteVAAPIContext();
126126

127-
m_ctx = new VAAPIContext(video_codec_id);
127+
m_ctx = new VAAPIContext(kVADisplayGLX, video_codec_id);
128128
if (m_ctx && m_ctx->CreateDisplay(size) && m_ctx->CreateBuffers())
129129
{
130130
int num_buffers = m_ctx->GetNumBuffers();

0 commit comments

Comments
 (0)