2 * Copyright (C) 2005-2020 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.
9 #include "DumbBufferObject.h"
11 #include "ServiceBroker.h"
12 #include "utils/BufferObjectFactory.h"
13 #include "utils/log.h"
14 #include "windowing/gbm/WinSystemGbm.h"
15 #include "windowing/gbm/WinSystemGbmEGLContext.h"
17 #include <drm_fourcc.h>
22 #include "PlatformDefs.h"
24 using namespace KODI::WINDOWING::GBM
;
26 std::unique_ptr
<CBufferObject
> CDumbBufferObject::Create()
28 return std::make_unique
<CDumbBufferObject
>();
31 void CDumbBufferObject::Register()
33 CBufferObjectFactory::RegisterBufferObject(CDumbBufferObject::Create
);
36 CDumbBufferObject::CDumbBufferObject()
38 auto winSystem
= static_cast<CWinSystemGbmEGLContext
*>(CServiceBroker::GetWinSystem());
40 m_device
= winSystem
->GetDrm()->GetFileDescriptor();
43 CDumbBufferObject::~CDumbBufferObject()
46 DestroyBufferObject();
49 bool CDumbBufferObject::CreateBufferObject(uint32_t format
, uint32_t width
, uint32_t height
)
58 case DRM_FORMAT_ARGB1555
:
59 case DRM_FORMAT_RGB565
:
62 case DRM_FORMAT_ARGB8888
:
66 throw std::runtime_error("CDumbBufferObject: pixel format not implemented");
69 struct drm_mode_create_dumb create_dumb
{};
70 create_dumb
.height
= height
;
71 create_dumb
.width
= width
;
72 create_dumb
.bpp
= bpp
;
74 int ret
= drmIoctl(m_device
, DRM_IOCTL_MODE_CREATE_DUMB
, &create_dumb
);
77 CLog::Log(LOGERROR
, "CDumbBufferObject::{} - ioctl DRM_IOCTL_MODE_CREATE_DUMB failed, errno={}",
78 __FUNCTION__
, strerror(errno
));
82 m_size
= create_dumb
.size
;
83 m_stride
= create_dumb
.pitch
;
85 ret
= drmPrimeHandleToFD(m_device
, create_dumb
.handle
, 0, &m_fd
);
88 CLog::Log(LOGERROR
, "CDumbBufferObject::{} - failed to get fd from prime handle, errno={}",
89 __FUNCTION__
, strerror(errno
));
96 void CDumbBufferObject::DestroyBufferObject()
101 int ret
= close(m_fd
);
103 CLog::Log(LOGERROR
, "CDumbBufferObject::{} - close failed, errno={}", __FUNCTION__
,
111 uint8_t* CDumbBufferObject::GetMemory()
118 CLog::Log(LOGDEBUG
, "CDumbBufferObject::{} - already mapped fd={} map={}", __FUNCTION__
, m_fd
,
124 int ret
= drmPrimeFDToHandle(m_device
, m_fd
, &handle
);
127 CLog::Log(LOGERROR
, "CDumbBufferObject::{} - failed to get handle from prime fd, errno={}",
128 __FUNCTION__
, strerror(errno
));
132 struct drm_mode_map_dumb map_dumb
{};
133 map_dumb
.handle
= handle
;
135 ret
= drmIoctl(m_device
, DRM_IOCTL_MODE_MAP_DUMB
, &map_dumb
);
138 CLog::Log(LOGERROR
, "CDumbBufferObject::{} - ioctl DRM_IOCTL_MODE_MAP_DUMB failed, errno={}",
139 __FUNCTION__
, strerror(errno
));
143 m_offset
= map_dumb
.offset
;
145 m_map
= static_cast<uint8_t*>(mmap(nullptr, m_size
, PROT_WRITE
, MAP_SHARED
, m_device
, m_offset
));
146 if (m_map
== MAP_FAILED
)
148 CLog::Log(LOGERROR
, "CDumbBufferObject::{} - mmap failed, errno={}", __FUNCTION__
,
156 void CDumbBufferObject::ReleaseMemory()
161 int ret
= munmap(m_map
, m_size
);
163 CLog::Log(LOGERROR
, "CDumbBufferObject::{} - munmap failed, errno={}", __FUNCTION__
,