1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

VideoSurface: aspect ratio

This commit is contained in:
krepa098 2014-10-11 20:53:40 +02:00
parent 9df5ab02ec
commit 1d4055858f
4 changed files with 165 additions and 104 deletions

View File

@ -24,10 +24,7 @@ AVForm::AVForm() :
bodyUI = new Ui::AVSettings;
bodyUI->setupUi(this);
//cam->setVideoMode(cam->getBestVideoMode());
camView = new VideoSurface(Camera::getInstance(), this);
bodyUI->CamViewLayout->addWidget(camView);
bodyUI->CamVideoSurface->setSource(Camera::getInstance());
}
AVForm::~AVForm()

View File

@ -6,14 +6,14 @@
<rect>
<x>0</x>
<y>0</y>
<width>394</width>
<height>391</height>
<width>390</width>
<height>387</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
@ -56,92 +56,111 @@
<property name="title">
<string>Video settings</string>
</property>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Modes</string>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout" stretch="0">
<item>
<widget class="QFrame" name="CamFrame">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>1</horstretch>
<verstretch>99</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="VideoSurface" name="CamVideoSurface" native="true"/>
</item>
</layout>
</widget>
</item>
</layout>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="videoModescomboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<item>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Hue</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSlider" name="HueSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Brightness</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSlider" name="BrightnessSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Saturation</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QSlider" name="SaturationSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Contrast</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QSlider" name="ContrastSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Preview</string>
</property>
</widget>
</item>
<item row="5" column="1">
<layout class="QVBoxLayout" name="CamViewLayout"/>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Modes</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="videoModescomboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Hue</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSlider" name="HueSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Brightness</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSlider" name="BrightnessSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Saturation</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QSlider" name="SaturationSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Contrast</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QSlider" name="ContrastSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
@ -161,6 +180,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>VideoSurface</class>
<extends>QWidget</extends>
<header>widget/videosurface.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -22,17 +22,24 @@
#include <QOpenGLShaderProgram>
#include <QDebug>
VideoSurface::VideoSurface(VideoSource *Source, QWidget* parent)
VideoSurface::VideoSurface(QWidget* parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
, source(Source)
, source(nullptr)
, pbo(nullptr)
, program(nullptr)
, textureId(0)
, pboAllocSize(0)
, uploadFrame(false)
, hasSubscribed(false)
, lastWidth(0)
{
setFixedSize(source->resolution());
}
VideoSurface::VideoSurface(VideoSource *Source, QWidget* parent)
: VideoSurface(parent)
{
source = Source;
}
VideoSurface::~VideoSurface()
@ -43,12 +50,18 @@ VideoSurface::~VideoSurface()
if (textureId != 0)
glDeleteTextures(1, &textureId);
source->unsubscribe();
if (source && hasSubscribed)
source->unsubscribe();
}
void VideoSurface::setSource(VideoSource *src)
{
source = src;
}
void VideoSurface::hideEvent(QHideEvent *ev)
{
if (hasSubscribed)
if (source && hasSubscribed)
{
source->unsubscribe();
hasSubscribed = false;
@ -60,7 +73,7 @@ void VideoSurface::hideEvent(QHideEvent *ev)
void VideoSurface::showEvent(QShowEvent *ev)
{
if (!hasSubscribed)
if (source && !hasSubscribed)
{
source->subscribe();
hasSubscribed = true;
@ -70,6 +83,11 @@ void VideoSurface::showEvent(QShowEvent *ev)
QGLWidget::showEvent(ev);
}
QSize VideoSurface::sizeHint() const
{
return QGLWidget::sizeHint();
}
void VideoSurface::initializeGL()
{
@ -77,9 +95,12 @@ void VideoSurface::initializeGL()
void VideoSurface::paintGL()
{
if (!source)
return;
if (!pbo)
{
qDebug() << "Creating pbo, program";
qDebug() << "VideoSurface: Init";
// pbo
pbo = new QOpenGLBuffer(QOpenGLBuffer::PixelUnpackBuffer);
@ -95,6 +116,8 @@ void VideoSurface::paintGL()
" gl_Position = vec4(vertices.xy,0.0,1.0);"
" coords = vertices.xy*vec2(0.5,0.5)+vec2(0.5,0.5);"
"}");
// brg frag-shader
program->addShaderFromSourceCode(QOpenGLShader::Fragment,
"uniform sampler2D texture0;"
"varying vec2 coords;"
@ -109,7 +132,7 @@ void VideoSurface::paintGL()
if (res != source->resolution())
{
qDebug() << "Change resolution " << res << " to " << source->resolution();
qDebug() << "VideoSurface: Change resolution from " << res << " to " << source->resolution();
res = source->resolution();
// a texture used to render the pbo (has the match the pixelformat of the source)
@ -118,7 +141,7 @@ void VideoSurface::paintGL()
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, res.width(), res.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
setFixedSize(res);
updateGeometry();
}
@ -130,7 +153,7 @@ void VideoSurface::paintGL()
if (pboAllocSize != frameBytes && frameBytes > 0)
{
qDebug() << "Resize pbo " << frameBytes << "bytes (was" << pboAllocSize << ") res " << source->resolution();
qDebug() << "VideoSurface: Resize pbo " << frameBytes << "bytes (before" << pboAllocSize << ")";
pbo->bind();
pbo->allocate(frameBytes);
@ -169,11 +192,21 @@ void VideoSurface::paintGL()
program->setAttributeArray(0, GL_FLOAT, values, 2);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, width(), height());
// keep aspect ratio
float aspectRatio = float(res.width()) / float(res.height());
if (width() < float(height()) * aspectRatio)
{
float h = float(width()) / aspectRatio;
glViewport(0, (height() - h)*0.5f, width(), h);
}
else
{
float w = float(height()) * float(aspectRatio);
glViewport((width() - w)*0.5f, 0, w, height());
}
program->bind();
program->enableAttributeArray(0);
@ -195,3 +228,4 @@ void VideoSurface::updateGL()
}

View File

@ -29,11 +29,14 @@ class VideoSurface : public QGLWidget
Q_OBJECT
public:
VideoSurface(QWidget* parent=0);
VideoSurface(VideoSource* source, QWidget* parent=0);
~VideoSurface();
void setSource(VideoSource* src);
virtual void hideEvent(QHideEvent* ev);
virtual void showEvent(QShowEvent* ev);
virtual QSize sizeHint() const;
// QGLWidget interface
protected:
@ -52,7 +55,7 @@ private:
QSize res;
bool uploadFrame;
bool hasSubscribed;
mutable int lastWidth;
};
#endif // SELFCAMVIEW_H