[Windows] Fix driver version detection of AMD RDNA+ GPU on Windows 10
[xbmc.git] / xbmc / windowing / gbm / GBMUtils.cpp
blob0ac2b854baeef14eb8076425af0486558087fadf
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "GBMUtils.h"
11 #include "utils/log.h"
13 #include <mutex>
15 using namespace KODI::WINDOWING::GBM;
17 namespace
19 std::once_flag flag;
22 bool CGBMUtils::CreateDevice(int fd)
24 auto device = gbm_create_device(fd);
25 if (!device)
27 CLog::Log(LOGERROR, "CGBMUtils::{} - failed to create device: {}", __FUNCTION__,
28 strerror(errno));
29 return false;
32 m_device.reset(new CGBMDevice(device));
34 return true;
37 CGBMUtils::CGBMDevice::CGBMDevice(gbm_device* device) : m_device(device)
41 bool CGBMUtils::CGBMDevice::CreateSurface(
42 int width, int height, uint32_t format, const uint64_t* modifiers, const int modifiers_count)
44 gbm_surface* surface{nullptr};
45 #if defined(HAS_GBM_MODIFIERS)
46 if (modifiers)
48 surface = gbm_surface_create_with_modifiers(m_device, width, height, format, modifiers,
49 modifiers_count);
51 #endif
52 if (!surface)
54 surface = gbm_surface_create(m_device, width, height, format,
55 GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
58 if (!surface)
60 CLog::Log(LOGERROR, "CGBMUtils::{} - failed to create surface: {}", __FUNCTION__,
61 strerror(errno));
62 return false;
65 CLog::Log(LOGDEBUG, "CGBMUtils::{} - created surface with size {}x{}", __FUNCTION__, width,
66 height);
68 m_surface.reset(new CGBMSurface(surface));
70 return true;
73 CGBMUtils::CGBMDevice::CGBMSurface::CGBMSurface(gbm_surface* surface) : m_surface(surface)
77 CGBMUtils::CGBMDevice::CGBMSurface::CGBMSurfaceBuffer& CGBMUtils::CGBMDevice::CGBMSurface::
78 LockFrontBuffer()
80 m_buffers.emplace(std::make_unique<CGBMSurfaceBuffer>(m_surface));
82 if (!static_cast<bool>(gbm_surface_has_free_buffers(m_surface)))
85 * We want to use call_once here because we want it to be logged the first time that
86 * we have to release buffers. This means that the maximum amount of buffers had been reached.
87 * For mesa this should be 4 buffers but it may vary across other implementations.
89 std::call_once(
90 flag, [this]() { CLog::Log(LOGDEBUG, "CGBMUtils - using {} buffers", m_buffers.size()); });
92 m_buffers.pop();
95 return *m_buffers.back();
98 CGBMUtils::CGBMDevice::CGBMSurface::CGBMSurfaceBuffer::CGBMSurfaceBuffer(gbm_surface* surface)
99 : m_surface(surface), m_buffer(gbm_surface_lock_front_buffer(surface))
103 CGBMUtils::CGBMDevice::CGBMSurface::CGBMSurfaceBuffer::~CGBMSurfaceBuffer()
105 if (m_surface && m_buffer)
106 gbm_surface_release_buffer(m_surface, m_buffer);