Skip to content
Snippets Groups Projects
Commit 96a01310 authored by Guillaume Roguez's avatar Guillaume Roguez
Browse files

video: decrease renderer overhead

Refs #70057
parent e7793429
Branches
No related tags found
No related merge requests found
...@@ -77,7 +77,7 @@ public: ...@@ -77,7 +77,7 @@ public:
//Attributes //Attributes
QString m_ShmPath ; QString m_ShmPath ;
int fd ; int m_fd ;
SHMHeader* m_pShmArea ; SHMHeader* m_pShmArea ;
unsigned m_ShmAreaLen ; unsigned m_ShmAreaLen ;
uint m_BufferGen ; uint m_BufferGen ;
...@@ -99,6 +99,7 @@ public: ...@@ -99,6 +99,7 @@ public:
bool shmLock (); bool shmLock ();
void shmUnlock (); void shmUnlock ();
bool renderToBitmap(); bool renderToBitmap();
bool resizeShm();
private: private:
Video::ShmRenderer* q_ptr; Video::ShmRenderer* q_ptr;
...@@ -110,7 +111,7 @@ private Q_SLOTS: ...@@ -110,7 +111,7 @@ private Q_SLOTS:
} }
Video::ShmRendererPrivate::ShmRendererPrivate(Video::ShmRenderer* parent) : QObject(parent), q_ptr(parent), Video::ShmRendererPrivate::ShmRendererPrivate(Video::ShmRenderer* parent) : QObject(parent), q_ptr(parent),
fd(-1),m_fpsC(0),m_Fps(0), m_fd(-1),m_fpsC(0),m_Fps(0),
m_pShmArea((SHMHeader*)MAP_FAILED), m_ShmAreaLen(0), m_BufferGen(0), m_pShmArea((SHMHeader*)MAP_FAILED), m_ShmAreaLen(0), m_BufferGen(0),
m_pTimer(nullptr) m_pTimer(nullptr)
#ifdef DEBUG_FPS #ifdef DEBUG_FPS
...@@ -134,12 +135,15 @@ Video::ShmRenderer::~ShmRenderer() ...@@ -134,12 +135,15 @@ Video::ShmRenderer::~ShmRenderer()
} }
///Get the data from shared memory and transform it into a QByteArray ///Get the data from shared memory and transform it into a QByteArray
bool Video::ShmRendererPrivate::renderToBitmap() bool
Video::ShmRendererPrivate::renderToBitmap()
{ {
QMutexLocker locker {q_ptr->mutex()}; QMutexLocker locker {q_ptr->mutex()};
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
if (!q_ptr->isRendering()) auto& renderer = static_cast<Video::Renderer*>(q_ptr)->d_ptr;
auto& frame = renderer->otherFrame();
if (frame.isEmpty())
return false; return false;
if (!shmLock()) if (!shmLock())
...@@ -161,13 +165,11 @@ bool Video::ShmRendererPrivate::renderToBitmap() ...@@ -161,13 +165,11 @@ bool Video::ShmRendererPrivate::renderToBitmap()
if (not m_pShmArea->frameSize) if (not m_pShmArea->frameSize)
return false; return false;
if (!q_ptr->resizeShm()) { if (!resizeShm()) {
qDebug() << "Could not resize shared memory"; qDebug() << "Could not resize shared memory";
return false; return false;
} }
auto& renderer = static_cast<Video::Renderer*>(q_ptr)->d_ptr;
auto& frame = renderer->otherFrame();
if ((unsigned)frame.size() != m_pShmArea->frameSize) if ((unsigned)frame.size() != m_pShmArea->frameSize)
frame.resize(m_pShmArea->frameSize); frame.resize(m_pShmArea->frameSize);
std::copy_n(m_pShmArea->data + m_pShmArea->readOffset, m_pShmArea->frameSize, frame.data()); std::copy_n(m_pShmArea->data + m_pShmArea->readOffset, m_pShmArea->frameSize, frame.data());
...@@ -192,16 +194,42 @@ bool Video::ShmRendererPrivate::renderToBitmap() ...@@ -192,16 +194,42 @@ bool Video::ShmRendererPrivate::renderToBitmap()
#endif #endif
} }
///Resize the shared memory
bool
Video::ShmRendererPrivate::resizeShm()
{
const auto areaSize = sizeof(SHMHeader) + 2 * m_pShmArea->frameSize + 15;
if (m_ShmAreaLen == areaSize)
return true;
shmUnlock();
if (::munmap(m_pShmArea, m_ShmAreaLen)) {
qDebug() << "Could not unmap shared area:" << strerror(errno);
return false;
}
m_pShmArea = (SHMHeader*) ::mmap(nullptr, areaSize,
PROT_READ | PROT_WRITE,
MAP_SHARED, m_fd, 0);
if (m_pShmArea == MAP_FAILED) {
qDebug() << "Could not remap shared area";
return false;
}
m_ShmAreaLen = areaSize;
return shmLock();
}
///Connect to the shared memory ///Connect to the shared memory
bool Video::ShmRenderer::startShm() bool Video::ShmRenderer::startShm()
{ {
if (d_ptr->fd != -1) { if (d_ptr->m_fd != -1) {
qDebug() << "fd must be -1"; qDebug() << "fd must be -1";
return false; return false;
} }
d_ptr->fd = ::shm_open(d_ptr->m_ShmPath.toLatin1(), O_RDWR, 0); d_ptr->m_fd = ::shm_open(d_ptr->m_ShmPath.toLatin1(), O_RDWR, 0);
if (d_ptr->fd < 0) { if (d_ptr->m_fd < 0) {
qDebug() << "could not open shm area " << d_ptr->m_ShmPath qDebug() << "could not open shm area " << d_ptr->m_ShmPath
<< ", shm_open failed:" << strerror(errno); << ", shm_open failed:" << strerror(errno);
return false; return false;
...@@ -210,7 +238,7 @@ bool Video::ShmRenderer::startShm() ...@@ -210,7 +238,7 @@ bool Video::ShmRenderer::startShm()
const auto areaSize = sizeof(SHMHeader); const auto areaSize = sizeof(SHMHeader);
d_ptr->m_pShmArea = (SHMHeader*) ::mmap(nullptr, areaSize, d_ptr->m_pShmArea = (SHMHeader*) ::mmap(nullptr, areaSize,
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_SHARED, d_ptr->fd, 0); MAP_SHARED, d_ptr->m_fd, 0);
if (d_ptr->m_pShmArea == MAP_FAILED) { if (d_ptr->m_pShmArea == MAP_FAILED) {
qDebug() << "Could not remap shared area"; qDebug() << "Could not remap shared area";
return false; return false;
...@@ -225,11 +253,11 @@ bool Video::ShmRenderer::startShm() ...@@ -225,11 +253,11 @@ bool Video::ShmRenderer::startShm()
///Disconnect from the shared memory ///Disconnect from the shared memory
void Video::ShmRenderer::stopShm() void Video::ShmRenderer::stopShm()
{ {
if (d_ptr->fd < 0) if (d_ptr->m_fd < 0)
return; return;
::close(d_ptr->fd); ::close(d_ptr->m_fd);
d_ptr->fd = -1; d_ptr->m_fd = -1;
if (d_ptr->m_pShmArea == MAP_FAILED) if (d_ptr->m_pShmArea == MAP_FAILED)
return; return;
...@@ -239,31 +267,6 @@ void Video::ShmRenderer::stopShm() ...@@ -239,31 +267,6 @@ void Video::ShmRenderer::stopShm()
d_ptr->m_pShmArea = (SHMHeader*) MAP_FAILED; d_ptr->m_pShmArea = (SHMHeader*) MAP_FAILED;
} }
///Resize the shared memory
bool Video::ShmRenderer::resizeShm()
{
const auto areaSize = sizeof(SHMHeader) + 2 * d_ptr->m_pShmArea->frameSize + 15;
if (d_ptr->m_ShmAreaLen == areaSize)
return true;
d_ptr->shmUnlock();
if (::munmap(d_ptr->m_pShmArea, d_ptr->m_ShmAreaLen)) {
qDebug() << "Could not unmap shared area:" << strerror(errno);
return false;
}
d_ptr->m_pShmArea = (SHMHeader*) ::mmap(nullptr, areaSize,
PROT_READ | PROT_WRITE,
MAP_SHARED, d_ptr->fd, 0);
if (d_ptr->m_pShmArea == MAP_FAILED) {
qDebug() << "Could not remap shared area";
return false;
}
d_ptr->m_ShmAreaLen = areaSize;
return d_ptr->shmLock();
}
///Lock the memory while the copy is being made ///Lock the memory while the copy is being made
bool Video::ShmRendererPrivate::shmLock() bool Video::ShmRendererPrivate::shmLock()
{ {
...@@ -301,7 +304,7 @@ void Video::ShmRendererPrivate::timedEvents() ...@@ -301,7 +304,7 @@ void Video::ShmRendererPrivate::timedEvents()
} }
m_fpsC++; m_fpsC++;
emit q_ptr->frameUpdated(); //emit q_ptr->frameUpdated();
} }
/*else { /*else {
qDebug() << "Frame dropped"; qDebug() << "Frame dropped";
......
...@@ -48,7 +48,6 @@ class LIB_EXPORT ShmRenderer : public Renderer { ...@@ -48,7 +48,6 @@ class LIB_EXPORT ShmRenderer : public Renderer {
virtual ~ShmRenderer(); virtual ~ShmRenderer();
//Mutators //Mutators
bool resizeShm();
void stopShm (); void stopShm ();
bool startShm (); bool startShm ();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment