| 1 |
/* |
| 2 |
* Copyright (C) 2007-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "VaapiEGL.h" |
| 10 |
|
| 11 |
#include "cores/VideoPlayer/DVDCodecs/Video/VAAPI.h" |
| 12 |
#include "utils/EGLUtils.h" |
| 13 |
#include "utils/log.h" |
| 14 |
|
| 15 |
#include <drm_fourcc.h> |
| 16 |
#include <va/va_drmcommon.h> |
| 17 |
|
| 18 |
#define HAVE_VAEXPORTSURFACHEHANDLE VA_CHECK_VERSION(1, 1, 0) |
| 19 |
|
| 20 |
using namespace VAAPI; |
| 21 |
|
| 22 |
void CVaapi1Texture::Init(InteropInfo &interop) |
| 23 |
{ |
| 24 |
m_interop = interop; |
| 25 |
} |
| 26 |
|
| 27 |
bool CVaapi1Texture::Map(CVaapiRenderPicture *pic) |
| 28 |
{ |
| 29 |
VAStatus status; |
| 30 |
|
| 31 |
if (m_vaapiPic) |
| 32 |
return true; |
| 33 |
|
| 34 |
vaSyncSurface(pic->vadsp, pic->procPic.videoSurface); |
| 35 |
|
| 36 |
status = vaDeriveImage(pic->vadsp, pic->procPic.videoSurface, &m_glSurface.vaImage); |
| 37 |
if (status != VA_STATUS_SUCCESS) |
| 38 |
{ |
| 39 |
CLog::Log(LOGERROR, "CVaapiTexture::{} - Error: {}({})", __FUNCTION__, vaErrorStr(status), |
| 40 |
status); |
| 41 |
return false; |
| 42 |
} |
| 43 |
memset(&m_glSurface.vBufInfo, 0, sizeof(m_glSurface.vBufInfo)); |
| 44 |
m_glSurface.vBufInfo.mem_type = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME; |
| 45 |
status = vaAcquireBufferHandle(pic->vadsp, m_glSurface.vaImage.buf, &m_glSurface.vBufInfo); |
| 46 |
if (status != VA_STATUS_SUCCESS) |
| 47 |
{ |
| 48 |
CLog::Log(LOGERROR, "CVaapiTexture::{} - Error: {}({})", __FUNCTION__, vaErrorStr(status), |
| 49 |
status); |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
m_texWidth = m_glSurface.vaImage.width; |
| 54 |
m_texHeight = m_glSurface.vaImage.height; |
| 55 |
|
| 56 |
GLint attribs[23], *attrib; |
| 57 |
|
| 58 |
switch (m_glSurface.vaImage.format.fourcc) |
| 59 |
{ |
| 60 |
case VA_FOURCC('N','V','1','2'): |
| 61 |
{ |
| 62 |
attrib = attribs; |
| 63 |
*attrib++ = EGL_LINUX_DRM_FOURCC_EXT; |
| 64 |
*attrib++ = fourcc_code('R', '8', ' ', ' '); |
| 65 |
*attrib++ = EGL_WIDTH; |
| 66 |
*attrib++ = m_glSurface.vaImage.width; |
| 67 |
*attrib++ = EGL_HEIGHT; |
| 68 |
*attrib++ = m_glSurface.vaImage.height; |
| 69 |
*attrib++ = EGL_DMA_BUF_PLANE0_FD_EXT; |
| 70 |
*attrib++ = (intptr_t)m_glSurface.vBufInfo.handle; |
| 71 |
*attrib++ = EGL_DMA_BUF_PLANE0_OFFSET_EXT; |
| 72 |
*attrib++ = m_glSurface.vaImage.offsets[0]; |
| 73 |
*attrib++ = EGL_DMA_BUF_PLANE0_PITCH_EXT; |
| 74 |
*attrib++ = m_glSurface.vaImage.pitches[0]; |
| 75 |
*attrib++ = EGL_NONE; |
| 76 |
m_glSurface.eglImageY = m_interop.eglCreateImageKHR(m_interop.eglDisplay, |
| 77 |
EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, (EGLClientBuffer)NULL, |
| 78 |
attribs); |
| 79 |
if (!m_glSurface.eglImageY) |
| 80 |
{ |
| 81 |
EGLint err = eglGetError(); |
| 82 |
CLog::Log(LOGERROR, "failed to import VA buffer NV12 into EGL image: {}", err); |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
attrib = attribs; |
| 87 |
*attrib++ = EGL_LINUX_DRM_FOURCC_EXT; |
| 88 |
*attrib++ = fourcc_code('G', 'R', '8', '8'); |
| 89 |
*attrib++ = EGL_WIDTH; |
| 90 |
*attrib++ = (m_glSurface.vaImage.width + 1) >> 1; |
| 91 |
*attrib++ = EGL_HEIGHT; |
| 92 |
*attrib++ = (m_glSurface.vaImage.height + 1) >> 1; |
| 93 |
*attrib++ = EGL_DMA_BUF_PLANE0_FD_EXT; |
| 94 |
*attrib++ = (intptr_t)m_glSurface.vBufInfo.handle; |
| 95 |
*attrib++ = EGL_DMA_BUF_PLANE0_OFFSET_EXT; |
| 96 |
*attrib++ = m_glSurface.vaImage.offsets[1]; |
| 97 |
*attrib++ = EGL_DMA_BUF_PLANE0_PITCH_EXT; |
| 98 |
*attrib++ = m_glSurface.vaImage.pitches[1]; |
| 99 |
*attrib++ = EGL_NONE; |
| 100 |
m_glSurface.eglImageVU = m_interop.eglCreateImageKHR(m_interop.eglDisplay, |
| 101 |
EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, (EGLClientBuffer)NULL, |
| 102 |
attribs); |
| 103 |
if (!m_glSurface.eglImageVU) |
| 104 |
{ |
| 105 |
EGLint err = eglGetError(); |
| 106 |
CLog::Log(LOGERROR, "failed to import VA buffer NV12 into EGL image: {}", err); |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
glGenTextures(1, &m_textureY); |
| 111 |
glBindTexture(m_interop.textureTarget, m_textureY); |
| 112 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 113 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 114 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 115 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 116 |
m_interop.glEGLImageTargetTexture2DOES(m_interop.textureTarget, m_glSurface.eglImageY); |
| 117 |
|
| 118 |
glGenTextures(1, &m_textureVU); |
| 119 |
glBindTexture(m_interop.textureTarget, m_textureVU); |
| 120 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 121 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 122 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 123 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 124 |
m_interop.glEGLImageTargetTexture2DOES(m_interop.textureTarget, m_glSurface.eglImageVU); |
| 125 |
|
| 126 |
glBindTexture(m_interop.textureTarget, 0); |
| 127 |
|
| 128 |
break; |
| 129 |
} |
| 130 |
case VA_FOURCC('P','0','1','0'): |
| 131 |
{ |
| 132 |
attrib = attribs; |
| 133 |
*attrib++ = EGL_LINUX_DRM_FOURCC_EXT; |
| 134 |
*attrib++ = fourcc_code('R', '1', '6', ' '); |
| 135 |
*attrib++ = EGL_WIDTH; |
| 136 |
*attrib++ = m_glSurface.vaImage.width; |
| 137 |
*attrib++ = EGL_HEIGHT; |
| 138 |
*attrib++ = m_glSurface.vaImage.height; |
| 139 |
*attrib++ = EGL_DMA_BUF_PLANE0_FD_EXT; |
| 140 |
*attrib++ = (intptr_t)m_glSurface.vBufInfo.handle; |
| 141 |
*attrib++ = EGL_DMA_BUF_PLANE0_OFFSET_EXT; |
| 142 |
*attrib++ = m_glSurface.vaImage.offsets[0]; |
| 143 |
*attrib++ = EGL_DMA_BUF_PLANE0_PITCH_EXT; |
| 144 |
*attrib++ = m_glSurface.vaImage.pitches[0]; |
| 145 |
*attrib++ = EGL_NONE; |
| 146 |
m_glSurface.eglImageY = m_interop.eglCreateImageKHR(m_interop.eglDisplay, |
| 147 |
EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, (EGLClientBuffer)NULL, |
| 148 |
attribs); |
| 149 |
if (!m_glSurface.eglImageY) |
| 150 |
{ |
| 151 |
EGLint err = eglGetError(); |
| 152 |
CLog::Log(LOGERROR, "failed to import VA buffer P010 into EGL image: {}", err); |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
attrib = attribs; |
| 157 |
*attrib++ = EGL_LINUX_DRM_FOURCC_EXT; |
| 158 |
*attrib++ = fourcc_code('G', 'R', '3', '2'); |
| 159 |
*attrib++ = EGL_WIDTH; |
| 160 |
*attrib++ = (m_glSurface.vaImage.width + 1) >> 1; |
| 161 |
*attrib++ = EGL_HEIGHT; |
| 162 |
*attrib++ = (m_glSurface.vaImage.height + 1) >> 1; |
| 163 |
*attrib++ = EGL_DMA_BUF_PLANE0_FD_EXT; |
| 164 |
*attrib++ = (intptr_t)m_glSurface.vBufInfo.handle; |
| 165 |
*attrib++ = EGL_DMA_BUF_PLANE0_OFFSET_EXT; |
| 166 |
*attrib++ = m_glSurface.vaImage.offsets[1]; |
| 167 |
*attrib++ = EGL_DMA_BUF_PLANE0_PITCH_EXT; |
| 168 |
*attrib++ = m_glSurface.vaImage.pitches[1]; |
| 169 |
*attrib++ = EGL_NONE; |
| 170 |
m_glSurface.eglImageVU = m_interop.eglCreateImageKHR(m_interop.eglDisplay, |
| 171 |
EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, (EGLClientBuffer)NULL, |
| 172 |
attribs); |
| 173 |
if (!m_glSurface.eglImageVU) |
| 174 |
{ |
| 175 |
EGLint err = eglGetError(); |
| 176 |
CLog::Log(LOGERROR, "failed to import VA buffer P010 into EGL image: {}", err); |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
glGenTextures(1, &m_textureY); |
| 181 |
glBindTexture(m_interop.textureTarget, m_textureY); |
| 182 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 183 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 184 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 185 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 186 |
m_interop.glEGLImageTargetTexture2DOES(m_interop.textureTarget, m_glSurface.eglImageY); |
| 187 |
|
| 188 |
glGenTextures(1, &m_textureVU); |
| 189 |
glBindTexture(m_interop.textureTarget, m_textureVU); |
| 190 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 191 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 192 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 193 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 194 |
m_interop.glEGLImageTargetTexture2DOES(m_interop.textureTarget, m_glSurface.eglImageVU); |
| 195 |
|
| 196 |
glBindTexture(m_interop.textureTarget, 0); |
| 197 |
|
| 198 |
break; |
| 199 |
} |
| 200 |
case VA_FOURCC('B','G','R','A'): |
| 201 |
{ |
| 202 |
attrib = attribs; |
| 203 |
*attrib++ = EGL_DRM_BUFFER_FORMAT_MESA; |
| 204 |
*attrib++ = EGL_DRM_BUFFER_FORMAT_ARGB32_MESA; |
| 205 |
*attrib++ = EGL_WIDTH; |
| 206 |
*attrib++ = m_glSurface.vaImage.width; |
| 207 |
*attrib++ = EGL_HEIGHT; |
| 208 |
*attrib++ = m_glSurface.vaImage.height; |
| 209 |
*attrib++ = EGL_DRM_BUFFER_STRIDE_MESA; |
| 210 |
*attrib++ = m_glSurface.vaImage.pitches[0] / 4; |
| 211 |
*attrib++ = EGL_NONE; |
| 212 |
m_glSurface.eglImage = m_interop.eglCreateImageKHR(m_interop.eglDisplay, EGL_NO_CONTEXT, |
| 213 |
EGL_DRM_BUFFER_MESA, |
| 214 |
(EGLClientBuffer)m_glSurface.vBufInfo.handle, |
| 215 |
attribs); |
| 216 |
if (!m_glSurface.eglImage) |
| 217 |
{ |
| 218 |
EGLint err = eglGetError(); |
| 219 |
CLog::Log(LOGERROR, "failed to import VA buffer BGRA into EGL image: {}", err); |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
glGenTextures(1, &m_texture); |
| 224 |
glBindTexture(m_interop.textureTarget, m_texture); |
| 225 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 226 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 227 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 228 |
glTexParameteri(m_interop.textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 229 |
|
| 230 |
m_interop.glEGLImageTargetTexture2DOES(m_interop.textureTarget, m_glSurface.eglImage); |
| 231 |
|
| 232 |
glBindTexture(m_interop.textureTarget, 0); |
| 233 |
|
| 234 |
break; |
| 235 |
} |
| 236 |
default: |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
m_vaapiPic = pic; |
| 241 |
m_vaapiPic->Acquire(); |
| 242 |
return true; |
| 243 |
} |
| 244 |
|
| 245 |
void CVaapi1Texture::Unmap() |
| 246 |
{ |
| 247 |
if (!m_vaapiPic) |
| 248 |
return; |
| 249 |
|
| 250 |
if (m_glSurface.vaImage.image_id == VA_INVALID_ID) |
| 251 |
return; |
| 252 |
|
| 253 |
m_interop.eglDestroyImageKHR(m_interop.eglDisplay, m_glSurface.eglImageY); |
| 254 |
m_interop.eglDestroyImageKHR(m_interop.eglDisplay, m_glSurface.eglImageVU); |
| 255 |
|
| 256 |
VAStatus status; |
| 257 |
status = vaReleaseBufferHandle(m_vaapiPic->vadsp, m_glSurface.vaImage.buf); |
| 258 |
if (status != VA_STATUS_SUCCESS) |
| 259 |
{ |
| 260 |
CLog::Log(LOGERROR, "VAAPI::{} - Error: {}({})", __FUNCTION__, vaErrorStr(status), status); |
| 261 |
} |
| 262 |
|
| 263 |
status = vaDestroyImage(m_vaapiPic->vadsp, m_glSurface.vaImage.image_id); |
| 264 |
if (status != VA_STATUS_SUCCESS) |
| 265 |
{ |
| 266 |
CLog::Log(LOGERROR, "VAAPI::{} - Error: {}({})", __FUNCTION__, vaErrorStr(status), status); |
| 267 |
} |
| 268 |
|
| 269 |
m_glSurface.vaImage.image_id = VA_INVALID_ID; |
| 270 |
|
| 271 |
glDeleteTextures(1, &m_textureY); |
| 272 |
glDeleteTextures(1, &m_textureVU); |
| 273 |
|
| 274 |
m_vaapiPic->Release(); |
| 275 |
m_vaapiPic = nullptr; |
| 276 |
} |
| 277 |
|
| 278 |
GLuint CVaapi1Texture::GetTextureY() |
| 279 |
{ |
| 280 |
return m_textureY; |
| 281 |
} |
| 282 |
|
| 283 |
GLuint CVaapi1Texture::GetTextureVU() |
| 284 |
{ |
| 285 |
return m_textureVU; |
| 286 |
} |
| 287 |
|
| 288 |
CSizeInt CVaapi1Texture::GetTextureSize() |
| 289 |
{ |
| 290 |
return {m_texWidth, m_texHeight}; |
| 291 |
} |
| 292 |
|
| 293 |
void CVaapi1Texture::TestInterop(VADisplay vaDpy, EGLDisplay eglDisplay, bool &general, bool &deepColor) |
| 294 |
{ |
| 295 |
general = false; |
| 296 |
deepColor = false; |
| 297 |
|
| 298 |
PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR"); |
| 299 |
PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR"); |
| 300 |
if (!eglCreateImageKHR || !eglDestroyImageKHR) |
| 301 |
{ |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
int width = 1920; |
| 306 |
int height = 1080; |
| 307 |
|
| 308 |
// create surfaces |
| 309 |
VASurfaceID surface; |
| 310 |
VAStatus status; |
| 311 |
VAImage image; |
| 312 |
VABufferInfo bufferInfo; |
| 313 |
|
| 314 |
if (vaCreateSurfaces(vaDpy, VA_RT_FORMAT_YUV420, |
| 315 |
width, height, |
| 316 |
&surface, 1, NULL, 0) != VA_STATUS_SUCCESS) |
| 317 |
{ |
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
// check interop |
| 322 |
|
| 323 |
status = vaDeriveImage(vaDpy, surface, &image); |
| 324 |
if (status == VA_STATUS_SUCCESS) |
| 325 |
{ |
| 326 |
memset(&bufferInfo, 0, sizeof(bufferInfo)); |
| 327 |
bufferInfo.mem_type = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME; |
| 328 |
status = vaAcquireBufferHandle(vaDpy, image.buf, &bufferInfo); |
| 329 |
if (status == VA_STATUS_SUCCESS) |
| 330 |
{ |
| 331 |
EGLImageKHR eglImage; |
| 332 |
EGLint attribs[] = { |
| 333 |
EGL_LINUX_DRM_FOURCC_EXT, DRM_FORMAT_R8, |
| 334 |
EGL_WIDTH, image.width, |
| 335 |
EGL_HEIGHT, image.height, |
| 336 |
EGL_DMA_BUF_PLANE0_FD_EXT, static_cast<EGLint> (bufferInfo.handle), |
| 337 |
EGL_DMA_BUF_PLANE0_OFFSET_EXT, static_cast<EGLint> (image.offsets[0]), |
| 338 |
EGL_DMA_BUF_PLANE0_PITCH_EXT, static_cast<EGLint> (image.pitches[0]), |
| 339 |
EGL_NONE |
| 340 |
}; |
| 341 |
|
| 342 |
eglImage = eglCreateImageKHR(eglDisplay, |
| 343 |
EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, (EGLClientBuffer)NULL, |
| 344 |
attribs); |
| 345 |
if (eglImage) |
| 346 |
{ |
| 347 |
eglDestroyImageKHR(eglDisplay, eglImage); |
| 348 |
general = true; |
| 349 |
} |
| 350 |
} |
| 351 |
vaDestroyImage(vaDpy, image.image_id); |
| 352 |
} |
| 353 |
vaDestroySurfaces(vaDpy, &surface, 1); |
| 354 |
|
| 355 |
if (general) |
| 356 |
{ |
| 357 |
deepColor = TestInteropDeepColor(vaDpy, eglDisplay); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
bool CVaapi1Texture::TestInteropDeepColor(VADisplay vaDpy, EGLDisplay eglDisplay) |
| 362 |
{ |
| 363 |
bool ret = false; |
| 364 |
|
| 365 |
PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR"); |
| 366 |
PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR"); |
| 367 |
if (!eglCreateImageKHR || !eglDestroyImageKHR) |
| 368 |
{ |
| 369 |
return false; |
| 370 |
} |
| 371 |
|
| 372 |
int width = 1920; |
| 373 |
int height = 1080; |
| 374 |
|
| 375 |
// create surfaces |
| 376 |
VASurfaceID surface; |
| 377 |
VAStatus status; |
| 378 |
VAImage image; |
| 379 |
VABufferInfo bufferInfo; |
| 380 |
|
| 381 |
VASurfaceAttrib attribs = {}; |
| 382 |
attribs.flags = VA_SURFACE_ATTRIB_SETTABLE; |
| 383 |
attribs.type = VASurfaceAttribPixelFormat; |
| 384 |
attribs.value.type = VAGenericValueTypeInteger; |
| 385 |
attribs.value.value.i = VA_FOURCC_P010; |
| 386 |
|
| 387 |
if (vaCreateSurfaces(vaDpy, VA_RT_FORMAT_YUV420_10BPP, |
| 388 |
width, height, |
| 389 |
&surface, 1, &attribs, 1) != VA_STATUS_SUCCESS) |
| 390 |
{ |
| 391 |
return false; |
| 392 |
} |
| 393 |
|
| 394 |
// check interop |
| 395 |
status = vaDeriveImage(vaDpy, surface, &image); |
| 396 |
if (status == VA_STATUS_SUCCESS) |
| 397 |
{ |
| 398 |
memset(&bufferInfo, 0, sizeof(bufferInfo)); |
| 399 |
bufferInfo.mem_type = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME; |
| 400 |
status = vaAcquireBufferHandle(vaDpy, image.buf, &bufferInfo); |
| 401 |
if (status == VA_STATUS_SUCCESS) |
| 402 |
{ |
| 403 |
EGLImageKHR eglImage; |
| 404 |
EGLint attribs[] = { |
| 405 |
EGL_LINUX_DRM_FOURCC_EXT, DRM_FORMAT_GR1616, |
| 406 |
EGL_WIDTH, (image.width + 1) >> 1, |
| 407 |
EGL_HEIGHT, (image.height + 1) >> 1, |
| 408 |
EGL_DMA_BUF_PLANE0_FD_EXT, static_cast<EGLint> (bufferInfo.handle), |
| 409 |
EGL_DMA_BUF_PLANE0_OFFSET_EXT, static_cast<EGLint> (image.offsets[1]), |
| 410 |
EGL_DMA_BUF_PLANE0_PITCH_EXT, static_cast<EGLint> (image.pitches[1]), |
| 411 |
EGL_NONE |
| 412 |
}; |
| 413 |
|
| 414 |
eglImage = eglCreateImageKHR(eglDisplay, |
| 415 |
EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, (EGLClientBuffer)NULL, |
| 416 |
attribs); |
| 417 |
if (eglImage) |
| 418 |
{ |
| 419 |
eglDestroyImageKHR(eglDisplay, eglImage); |
| 420 |
ret = true; |
| 421 |
} |
| 422 |
|
| 423 |
} |
| 424 |
vaDestroyImage(vaDpy, image.image_id); |
| 425 |
} |
| 426 |
|
| 427 |
vaDestroySurfaces(vaDpy, &surface, 1); |
| 428 |
|
| 429 |
return ret; |
| 430 |
} |
| 431 |
|
| 432 |
void CVaapi2Texture::Init(InteropInfo& interop) |
| 433 |
{ |
| 434 |
m_interop = interop; |
| 435 |
m_hasPlaneModifiers = CEGLUtils::HasExtension(m_interop.eglDisplay, "EGL_EXT_image_dma_buf_import_modifiers"); |
| 436 |
} |
| 437 |
|
| 438 |
bool CVaapi2Texture::Map(CVaapiRenderPicture* pic) |
| 439 |
{ |
| 440 |
#if HAVE_VAEXPORTSURFACHEHANDLE |
| 441 |
if (m_vaapiPic) |
| 442 |
return true; |
| 443 |
|
| 444 |
m_vaapiPic = pic; |
| 445 |
m_vaapiPic->Acquire(); |
| 446 |
|
| 447 |
VAStatus status; |
| 448 |
|
| 449 |
VADRMPRIMESurfaceDescriptor surface; |
| 450 |
|
| 451 |
status = vaExportSurfaceHandle(pic->vadsp, pic->procPic.videoSurface, |
| 452 |
VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2, |
| 453 |
VA_EXPORT_SURFACE_READ_ONLY | VA_EXPORT_SURFACE_SEPARATE_LAYERS, |
| 454 |
&surface); |
| 455 |
|
| 456 |
if (status != VA_STATUS_SUCCESS) |
| 457 |
{ |
| 458 |
CLog::Log(LOGWARNING, "CVaapi2Texture::Map: vaExportSurfaceHandle failed - Error: {} ({})", |
| 459 |
vaErrorStr(status), status); |
| 460 |
return false; |
| 461 |
} |
| 462 |
|
| 463 |
// Remember fds to close them later |
| 464 |
if (surface.num_objects > m_drmFDs.size()) |
| 465 |
throw std::logic_error("Too many fds returned by vaExportSurfaceHandle"); |
| 466 |
|
| 467 |
for (uint32_t object = 0; object < surface.num_objects; object++) |
| 468 |
{ |
| 469 |
m_drmFDs[object].attach(surface.objects[object].fd); |
| 470 |
} |
| 471 |
|
| 472 |
status = vaSyncSurface(pic->vadsp, pic->procPic.videoSurface); |
| 473 |
if (status != VA_STATUS_SUCCESS) |
| 474 |
{ |
| 475 |
CLog::Log(LOGERROR, "CVaapi2Texture::Map: vaSyncSurface - Error: {} ({})", vaErrorStr(status), |
| 476 |
status); |
| 477 |
return false; |
| 478 |
} |
| 479 |
|
| 480 |
m_textureSize.Set(pic->DVDPic.iWidth, pic->DVDPic.iHeight); |
| 481 |
|
| 482 |
for (uint32_t layerNo = 0; layerNo < surface.num_layers; layerNo++) |
| 483 |
{ |
| 484 |
int plane = 0; |
| 485 |
auto const& layer = surface.layers[layerNo]; |
| 486 |
if (layer.num_planes != 1) |
| 487 |
{ |
| 488 |
CLog::Log(LOGDEBUG, |
| 489 |
"CVaapi2Texture::Map: DRM-exported layer has {} planes - only 1 supported", |
| 490 |
layer.num_planes); |
| 491 |
return false; |
| 492 |
} |
| 493 |
auto const& object = surface.objects[layer.object_index[plane]]; |
| 494 |
|
| 495 |
MappedTexture* texture{}; |
| 496 |
EGLint width{m_textureSize.Width()}; |
| 497 |
EGLint height{m_textureSize.Height()}; |
| 498 |
|
| 499 |
switch (surface.num_layers) |
| 500 |
{ |
| 501 |
case 2: |
| 502 |
switch (layerNo) |
| 503 |
{ |
| 504 |
case 0: |
| 505 |
texture = &m_y; |
| 506 |
break; |
| 507 |
case 1: |
| 508 |
texture = &m_vu; |
| 509 |
if (surface.fourcc == VA_FOURCC_NV12 || surface.fourcc == VA_FOURCC_P010 || surface.fourcc == VA_FOURCC_P016) |
| 510 |
{ |
| 511 |
// Adjust w/h for 4:2:0 subsampling on UV plane |
| 512 |
width = (width + 1) >> 1; |
| 513 |
height = (height + 1) >> 1; |
| 514 |
} |
| 515 |
break; |
| 516 |
default: |
| 517 |
throw std::logic_error("Impossible layer number"); |
| 518 |
} |
| 519 |
break; |
| 520 |
default: |
| 521 |
CLog::Log(LOGDEBUG, |
| 522 |
"CVaapi2Texture::Map: DRM-exported surface {} layers - only 2 supported", |
| 523 |
surface.num_layers); |
| 524 |
return false; |
| 525 |
} |
| 526 |
|
| 527 |
CEGLAttributes<8> attribs; // 6 static + 2 modifiers |
| 528 |
attribs.Add({{EGL_LINUX_DRM_FOURCC_EXT, static_cast<EGLint>(layer.drm_format)}, |
| 529 |
{EGL_WIDTH, width}, |
| 530 |
{EGL_HEIGHT, height}, |
| 531 |
{EGL_DMA_BUF_PLANE0_FD_EXT, object.fd}, |
| 532 |
{EGL_DMA_BUF_PLANE0_OFFSET_EXT, static_cast<EGLint>(layer.offset[plane])}, |
| 533 |
{EGL_DMA_BUF_PLANE0_PITCH_EXT, static_cast<EGLint>(layer.pitch[plane])}}); |
| 534 |
|
| 535 |
if (m_hasPlaneModifiers) |
| 536 |
{ |
| 537 |
attribs.Add({{EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, static_cast<EGLint>(object.drm_format_modifier)}, |
| 538 |
{EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, static_cast<EGLint>(object.drm_format_modifier >> 32)}}); |
| 539 |
} |
| 540 |
|
| 541 |
texture->eglImage = m_interop.eglCreateImageKHR(m_interop.eglDisplay, |
| 542 |
EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, nullptr, |
| 543 |
attribs.Get()); |
| 544 |
if (!texture->eglImage) |
| 545 |
{ |
| 546 |
CEGLUtils::Log(LOGERROR, "Failed to import VA DRM surface into EGL image"); |
| 547 |
return false; |
| 548 |
} |
| 549 |
|
| 550 |
glGenTextures(1, &texture->glTexture); |
| 551 |
glBindTexture(m_interop.textureTarget, texture->glTexture); |
| 552 |
m_interop.glEGLImageTargetTexture2DOES(m_interop.textureTarget, texture->eglImage); |
| 553 |
glBindTexture(m_interop.textureTarget, 0); |
| 554 |
} |
| 555 |
|
| 556 |
return true; |
| 557 |
#else |
| 558 |
return false; |
| 559 |
#endif |
| 560 |
} |
| 561 |
|
| 562 |
void CVaapi2Texture::Unmap() |
| 563 |
{ |
| 564 |
if (!m_vaapiPic) |
| 565 |
return; |
| 566 |
|
| 567 |
for (auto texture : {&m_y, &m_vu}) |
| 568 |
{ |
| 569 |
if (texture->eglImage != EGL_NO_IMAGE_KHR) |
| 570 |
{ |
| 571 |
m_interop.eglDestroyImageKHR(m_interop.eglDisplay, texture->eglImage); |
| 572 |
texture->eglImage = EGL_NO_IMAGE_KHR; |
| 573 |
glDeleteTextures(1, &texture->glTexture); |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
for (auto& fd : m_drmFDs) |
| 578 |
{ |
| 579 |
fd.reset(); |
| 580 |
} |
| 581 |
|
| 582 |
m_vaapiPic->Release(); |
| 583 |
m_vaapiPic = nullptr; |
| 584 |
} |
| 585 |
|
| 586 |
GLuint CVaapi2Texture::GetTextureY() |
| 587 |
{ |
| 588 |
return m_y.glTexture; |
| 589 |
} |
| 590 |
|
| 591 |
GLuint CVaapi2Texture::GetTextureVU() |
| 592 |
{ |
| 593 |
return m_vu.glTexture; |
| 594 |
} |
| 595 |
|
| 596 |
CSizeInt CVaapi2Texture::GetTextureSize() |
| 597 |
{ |
| 598 |
return m_textureSize; |
| 599 |
} |
| 600 |
|
| 601 |
bool CVaapi2Texture::TestEsh(VADisplay vaDpy, EGLDisplay eglDisplay, std::uint32_t rtFormat, std::int32_t pixelFormat) |
| 602 |
{ |
| 603 |
#if HAVE_VAEXPORTSURFACHEHANDLE |
| 604 |
int width = 1920; |
| 605 |
int height = 1080; |
| 606 |
|
| 607 |
PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC) eglGetProcAddress("eglCreateImageKHR"); |
| 608 |
PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC) eglGetProcAddress("eglDestroyImageKHR"); |
| 609 |
if (!eglCreateImageKHR || !eglDestroyImageKHR) |
| 610 |
{ |
| 611 |
return false; |
| 612 |
} |
| 613 |
|
| 614 |
// create surfaces |
| 615 |
VASurfaceID surface; |
| 616 |
VAStatus status; |
| 617 |
|
| 618 |
VASurfaceAttrib attribs = {}; |
| 619 |
attribs.flags = VA_SURFACE_ATTRIB_SETTABLE; |
| 620 |
attribs.type = VASurfaceAttribPixelFormat; |
| 621 |
attribs.value.type = VAGenericValueTypeInteger; |
| 622 |
attribs.value.value.i = pixelFormat; |
| 623 |
|
| 624 |
if (vaCreateSurfaces(vaDpy, rtFormat, |
| 625 |
width, height, |
| 626 |
&surface, 1, &attribs, 1) != VA_STATUS_SUCCESS) |
| 627 |
{ |
| 628 |
return false; |
| 629 |
} |
| 630 |
|
| 631 |
// check interop |
| 632 |
VADRMPRIMESurfaceDescriptor drmPrimeSurface; |
| 633 |
status = vaExportSurfaceHandle(vaDpy, surface, |
| 634 |
VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2, |
| 635 |
VA_EXPORT_SURFACE_READ_ONLY | VA_EXPORT_SURFACE_SEPARATE_LAYERS, |
| 636 |
&drmPrimeSurface); |
| 637 |
|
| 638 |
bool result = false; |
| 639 |
|
| 640 |
if (status == VA_STATUS_SUCCESS) |
| 641 |
{ |
| 642 |
auto const& layer = drmPrimeSurface.layers[0]; |
| 643 |
auto const& object = drmPrimeSurface.objects[layer.object_index[0]]; |
| 644 |
EGLint attribs[] = { |
| 645 |
EGL_LINUX_DRM_FOURCC_EXT, static_cast<EGLint>(drmPrimeSurface.layers[0].drm_format), |
| 646 |
EGL_WIDTH, width, |
| 647 |
EGL_HEIGHT, height, |
| 648 |
EGL_DMA_BUF_PLANE0_FD_EXT, static_cast<EGLint>(object.fd), |
| 649 |
EGL_DMA_BUF_PLANE0_OFFSET_EXT, static_cast<EGLint>(layer.offset[0]), |
| 650 |
EGL_DMA_BUF_PLANE0_PITCH_EXT, static_cast<EGLint>(layer.pitch[0]), |
| 651 |
EGL_NONE}; |
| 652 |
|
| 653 |
EGLImageKHR eglImage = eglCreateImageKHR(eglDisplay, |
| 654 |
EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, nullptr, |
| 655 |
attribs); |
| 656 |
if (eglImage) |
| 657 |
{ |
| 658 |
eglDestroyImageKHR(eglDisplay, eglImage); |
| 659 |
result = true; |
| 660 |
} |
| 661 |
|
| 662 |
for (uint32_t object = 0; object < drmPrimeSurface.num_objects; object++) |
| 663 |
{ |
| 664 |
close(drmPrimeSurface.objects[object].fd); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
vaDestroySurfaces(vaDpy, &surface, 1); |
| 669 |
|
| 670 |
return result; |
| 671 |
#else |
| 672 |
return false; |
| 673 |
#endif |
| 674 |
} |
| 675 |
|
| 676 |
void CVaapi2Texture::TestInterop(VADisplay vaDpy, EGLDisplay eglDisplay, bool& general, bool& deepColor) |
| 677 |
{ |
| 678 |
general = false; |
| 679 |
deepColor = false; |
| 680 |
|
| 681 |
general = TestInteropGeneral(vaDpy, eglDisplay); |
| 682 |
if (general) |
| 683 |
{ |
| 684 |
deepColor = TestEsh(vaDpy, eglDisplay, VA_RT_FORMAT_YUV420_10BPP, VA_FOURCC_P010); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
bool CVaapi2Texture::TestInteropGeneral(VADisplay vaDpy, EGLDisplay eglDisplay) |
| 689 |
{ |
| 690 |
return TestEsh(vaDpy, eglDisplay, VA_RT_FORMAT_YUV420, VA_FOURCC_NV12); |
| 691 |
} |