| 1 |
/* |
| 2 |
* Copyright (C) 2005-2020 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 "VideoBufferPoolDMA.h" |
| 10 |
|
| 11 |
#include "cores/VideoPlayer/Buffers/VideoBufferDMA.h" |
| 12 |
#include "utils/BufferObjectFactory.h" |
| 13 |
|
| 14 |
#include <mutex> |
| 15 |
|
| 16 |
#include <drm_fourcc.h> |
| 17 |
|
| 18 |
extern "C" |
| 19 |
{ |
| 20 |
#include <libavutil/pixfmt.h> |
| 21 |
} |
| 22 |
|
| 23 |
CVideoBufferPoolDMA::~CVideoBufferPoolDMA() |
| 24 |
{ |
| 25 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 26 |
|
| 27 |
for (auto buf : m_all) |
| 28 |
delete buf; |
| 29 |
} |
| 30 |
|
| 31 |
CVideoBuffer* CVideoBufferPoolDMA::Get() |
| 32 |
{ |
| 33 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 34 |
|
| 35 |
CVideoBufferDMA* buf = nullptr; |
| 36 |
if (!m_free.empty()) |
| 37 |
{ |
| 38 |
int idx = m_free.front(); |
| 39 |
m_free.pop_front(); |
| 40 |
m_used.push_back(idx); |
| 41 |
buf = m_all[idx]; |
| 42 |
} |
| 43 |
else |
| 44 |
{ |
| 45 |
int id = m_all.size(); |
| 46 |
buf = new CVideoBufferDMA(*this, id, m_fourcc, m_size); |
| 47 |
|
| 48 |
if (!buf->Alloc()) |
| 49 |
{ |
| 50 |
delete buf; |
| 51 |
return nullptr; |
| 52 |
} |
| 53 |
|
| 54 |
m_all.push_back(buf); |
| 55 |
m_used.push_back(id); |
| 56 |
} |
| 57 |
|
| 58 |
buf->Acquire(GetPtr()); |
| 59 |
return buf; |
| 60 |
} |
| 61 |
|
| 62 |
void CVideoBufferPoolDMA::Return(int id) |
| 63 |
{ |
| 64 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 65 |
|
| 66 |
m_all[id]->Unref(); |
| 67 |
auto it = m_used.begin(); |
| 68 |
while (it != m_used.end()) |
| 69 |
{ |
| 70 |
if (*it == id) |
| 71 |
{ |
| 72 |
m_used.erase(it); |
| 73 |
break; |
| 74 |
} |
| 75 |
else |
| 76 |
++it; |
| 77 |
} |
| 78 |
m_free.push_back(id); |
| 79 |
} |
| 80 |
|
| 81 |
void CVideoBufferPoolDMA::Configure(AVPixelFormat format, int size) |
| 82 |
{ |
| 83 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 84 |
|
| 85 |
m_fourcc = TranslateFormat(format); |
| 86 |
m_size = static_cast<uint64_t>(size); |
| 87 |
} |
| 88 |
|
| 89 |
bool CVideoBufferPoolDMA::IsConfigured() |
| 90 |
{ |
| 91 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 92 |
|
| 93 |
return (m_fourcc != 0 && m_size != 0); |
| 94 |
} |
| 95 |
|
| 96 |
bool CVideoBufferPoolDMA::IsCompatible(AVPixelFormat format, int size) |
| 97 |
{ |
| 98 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 99 |
|
| 100 |
if (m_fourcc != TranslateFormat(format) || m_size != static_cast<uint64_t>(size)) |
| 101 |
return false; |
| 102 |
|
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
void CVideoBufferPoolDMA::Released(CVideoBufferManager& videoBufferManager) |
| 107 |
{ |
| 108 |
if (!CBufferObjectFactory::CreateBufferObject(true)) |
| 109 |
return; |
| 110 |
|
| 111 |
videoBufferManager.RegisterPool(std::make_shared<CVideoBufferPoolDMA>()); |
| 112 |
} |
| 113 |
|
| 114 |
std::shared_ptr<IVideoBufferPool> CVideoBufferPoolDMA::CreatePool() |
| 115 |
{ |
| 116 |
return std::make_shared<CVideoBufferPoolDMA>(); |
| 117 |
} |
| 118 |
|
| 119 |
uint32_t CVideoBufferPoolDMA::TranslateFormat(AVPixelFormat format) |
| 120 |
{ |
| 121 |
switch (format) |
| 122 |
{ |
| 123 |
case AV_PIX_FMT_YUV420P: |
| 124 |
case AV_PIX_FMT_YUVJ420P: |
| 125 |
return DRM_FORMAT_YUV420; |
| 126 |
case AV_PIX_FMT_YUV422P: |
| 127 |
case AV_PIX_FMT_YUVJ422P: |
| 128 |
return DRM_FORMAT_YUV422; |
| 129 |
case AV_PIX_FMT_YUV444P: |
| 130 |
case AV_PIX_FMT_YUVJ444P: |
| 131 |
return DRM_FORMAT_YUV444; |
| 132 |
default: |
| 133 |
return 0; |
| 134 |
} |
| 135 |
} |