From 1d4055858fb72d54cee752a2973915fd778fb7ed Mon Sep 17 00:00:00 2001 From: krepa098 Date: Sat, 11 Oct 2014 20:53:40 +0200 Subject: [PATCH] VideoSurface: aspect ratio --- src/widget/form/settings/avform.cpp | 5 +- src/widget/form/settings/avsettings.ui | 201 ++++++++++++++----------- src/widget/videosurface.cpp | 58 +++++-- src/widget/videosurface.h | 5 +- 4 files changed, 165 insertions(+), 104 deletions(-) diff --git a/src/widget/form/settings/avform.cpp b/src/widget/form/settings/avform.cpp index 082097b5c..36f1433f4 100644 --- a/src/widget/form/settings/avform.cpp +++ b/src/widget/form/settings/avform.cpp @@ -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() diff --git a/src/widget/form/settings/avsettings.ui b/src/widget/form/settings/avsettings.ui index 20ebbf43e..0bd7080bb 100644 --- a/src/widget/form/settings/avsettings.ui +++ b/src/widget/form/settings/avsettings.ui @@ -6,14 +6,14 @@ 0 0 - 394 - 391 + 390 + 387 Form - + @@ -56,92 +56,111 @@ Video settings - - - QFormLayout::ExpandingFieldsGrow - - - - - Modes - - + + + + + + + + 1 + 99 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + + + + + - - - - - 0 - 0 - + + + + QFormLayout::ExpandingFieldsGrow - - - - - - Hue - - - - - - - Qt::Horizontal - - - - - - - Brightness - - - - - - - Qt::Horizontal - - - - - - - Saturation - - - - - - - Qt::Horizontal - - - - - - - Contrast - - - - - - - Qt::Horizontal - - - - - - - Preview - - - - - + + + + Modes + + + + + + + + 0 + 0 + + + + + + + + Hue + + + + + + + Qt::Horizontal + + + + + + + Brightness + + + + + + + Qt::Horizontal + + + + + + + Saturation + + + + + + + Qt::Horizontal + + + + + + + Contrast + + + + + + + Qt::Horizontal + + + + @@ -161,6 +180,14 @@ + + + VideoSurface + QWidget +
widget/videosurface.h
+ 1 +
+
diff --git a/src/widget/videosurface.cpp b/src/widget/videosurface.cpp index bd49b49c0..4a31bb052 100644 --- a/src/widget/videosurface.cpp +++ b/src/widget/videosurface.cpp @@ -22,17 +22,24 @@ #include #include -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() } + diff --git a/src/widget/videosurface.h b/src/widget/videosurface.h index 7de304682..ae2bb4178 100644 --- a/src/widget/videosurface.h +++ b/src/widget/videosurface.h @@ -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