{"id":9609,"url":"https://patchwork.libcamera.org/api/patches/9609/?format=json","web_url":"https://patchwork.libcamera.org/patch/9609/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200915024623.30667-4-laurent.pinchart@ideasonboard.com>","date":"2020-09-15T02:46:22","name":"[libcamera-devel,v7,3/4] qcam: Add ViewFinderGL class to accelerate the format conversion","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"d3648d5b223c5d82365b4692c7d6633c4009c473","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/9609/mbox/","series":[{"id":1290,"url":"https://patchwork.libcamera.org/api/series/1290/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1290","date":"2020-09-15T02:46:19","name":"[libcamera-devel,v7,1/4] qcam: Add OpenGL shader code as Qt resource","version":7,"mbox":"https://patchwork.libcamera.org/series/1290/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/9609/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/9609/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id E31D2C3B5B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 15 Sep 2020 02:46:59 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AE2BC62DF5;\n\tTue, 15 Sep 2020 04:46:59 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4854062D5B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 15 Sep 2020 04:46:56 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id AA084FD8;\n\tTue, 15 Sep 2020 04:46:55 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"nUXdmaKZ\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1600138015;\n\tbh=x8IM0UmVSibea9vem8aAXgtvGFZk+36n7Zx/8uFvat0=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=nUXdmaKZ1bRkAOfmzVZC7yzfOuE+6ntf7O0336wvvxjeQNALQxBinKBngOgaq8nhh\n\tVgbTTI3ckkG89vWYwIEhV8UjPVw05L0jBC0AKlcrOmd905r8KxrvGM37zpzy4tDV9U\n\t7JqMMc68JpHIDZMeozotqWVMWLtrfzhogN84XghU=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org,\n\tShow Liu <show.liu@linaro.org>","Date":"Tue, 15 Sep 2020 05:46:22 +0300","Message-Id":"<20200915024623.30667-4-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20200915024623.30667-1-laurent.pinchart@ideasonboard.com>","References":"<20200915024623.30667-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v7 3/4] qcam: Add ViewFinderGL class to\n\taccelerate the format conversion","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From: Show Liu <show.liu@linaro.org>\n\nThe viewfinderGL accelerates the format conversion by using OpenGL ES\nshader.\n\nThe minimum Qt version is bumped to v5.4, as QOpenGLWidget wasn't\navailable before that.\n\nSigned-off-by: Show Liu <show.liu@linaro.org>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n meson.build                |   1 +\n src/qcam/meson.build       |  17 +-\n src/qcam/viewfinder_gl.cpp | 451 +++++++++++++++++++++++++++++++++++++\n src/qcam/viewfinder_gl.h   |  96 ++++++++\n 4 files changed, 563 insertions(+), 2 deletions(-)\n create mode 100644 src/qcam/viewfinder_gl.cpp\n create mode 100644 src/qcam/viewfinder_gl.h","diff":"diff --git a/meson.build b/meson.build\nindex 1ea35e92ed67..c58d458ca0ff 100644\n--- a/meson.build\n+++ b/meson.build\n@@ -26,6 +26,7 @@ libcamera_version = libcamera_git_version.split('+')[0]\n \n # Configure the build environment.\n cc = meson.get_compiler('c')\n+cxx = meson.get_compiler('cpp')\n config_h = configuration_data()\n \n if cc.has_header_symbol('execinfo.h', 'backtrace')\ndiff --git a/src/qcam/meson.build b/src/qcam/meson.build\nindex a4bad0a0659e..9bb48c0d06c5 100644\n--- a/src/qcam/meson.build\n+++ b/src/qcam/meson.build\n@@ -16,14 +16,14 @@ qcam_moc_headers = files([\n \n qcam_resources = files([\n     'assets/feathericons/feathericons.qrc',\n-    'assets/shader/shaders.qrc'\n ])\n \n qt5 = import('qt5')\n qt5_dep = dependency('qt5',\n                      method : 'pkg-config',\n                      modules : ['Core', 'Gui', 'Widgets'],\n-                     required : get_option('qcam'))\n+                     required : get_option('qcam'),\n+                     version : '>=5.4')\n \n if qt5_dep.found()\n     qcam_deps = [\n@@ -42,6 +42,19 @@ if qt5_dep.found()\n         ])\n     endif\n \n+    if cxx.has_header_symbol('QOpenGLWidget', 'QOpenGLWidget',\n+                             dependencies : qt5_dep, args : '-fPIC')\n+        qcam_sources += files([\n+            'viewfinder_gl.cpp',\n+        ])\n+        qcam_moc_headers += files([\n+            'viewfinder_gl.h',\n+        ])\n+        qcam_resources += files([\n+            'assets/shader/shaders.qrc'\n+        ])\n+    endif\n+\n     # gcc 9 introduced a deprecated-copy warning that is triggered by Qt until\n     # Qt 5.13. clang 10 introduced the same warning, but detects more issues\n     # that are not fixed in Qt yet. Disable the warning manually in both cases.\ndiff --git a/src/qcam/viewfinder_gl.cpp b/src/qcam/viewfinder_gl.cpp\nnew file mode 100644\nindex 000000000000..fbe21dcf1ad2\n--- /dev/null\n+++ b/src/qcam/viewfinder_gl.cpp\n@@ -0,0 +1,451 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Linaro\n+ *\n+ * viewfinderGL.cpp - OpenGL Viewfinder for rendering by OpenGL shader\n+ */\n+\n+#include \"viewfinder_gl.h\"\n+\n+#include <QImage>\n+\n+#include <libcamera/formats.h>\n+\n+static const QList<libcamera::PixelFormat> supportedFormats{\n+\tlibcamera::formats::NV12,\n+\tlibcamera::formats::NV21,\n+\tlibcamera::formats::NV16,\n+\tlibcamera::formats::NV61,\n+\tlibcamera::formats::NV24,\n+\tlibcamera::formats::NV42,\n+\tlibcamera::formats::YUV420,\n+\tlibcamera::formats::YVU420,\n+};\n+\n+ViewFinderGL::ViewFinderGL(QWidget *parent)\n+\t: QOpenGLWidget(parent), buffer_(nullptr), yuvData_(nullptr),\n+\t  fragmentShader_(nullptr), vertexShader_(nullptr),\n+\t  vertexBuffer_(QOpenGLBuffer::VertexBuffer),\n+\t  textureU_(QOpenGLTexture::Target2D),\n+\t  textureV_(QOpenGLTexture::Target2D),\n+\t  textureY_(QOpenGLTexture::Target2D)\n+{\n+}\n+\n+ViewFinderGL::~ViewFinderGL()\n+{\n+\tremoveShader();\n+}\n+\n+const QList<libcamera::PixelFormat> &ViewFinderGL::nativeFormats() const\n+{\n+\treturn supportedFormats;\n+}\n+\n+int ViewFinderGL::setFormat(const libcamera::PixelFormat &format,\n+\t\t\t    const QSize &size)\n+{\n+\t/* If the fragment is created remove it and create a new one. */\n+\tif (fragmentShader_) {\n+\t\tif (shaderProgram_.isLinked()) {\n+\t\t\tshaderProgram_.release();\n+\t\t\tshaderProgram_.removeShader(fragmentShader_);\n+\t\t\tdelete fragmentShader_;\n+\t\t}\n+\t}\n+\n+\tif (!selectFormat(format))\n+\t\treturn -1;\n+\n+\tformat_ = format;\n+\tsize_ = size;\n+\n+\tupdateGeometry();\n+\treturn 0;\n+}\n+\n+void ViewFinderGL::stop()\n+{\n+\tif (buffer_) {\n+\t\trenderComplete(buffer_);\n+\t\tbuffer_ = nullptr;\n+\t}\n+}\n+\n+QImage ViewFinderGL::getCurrentImage()\n+{\n+\tQMutexLocker locker(&mutex_);\n+\n+\treturn grabFramebuffer();\n+}\n+\n+void ViewFinderGL::render(libcamera::FrameBuffer *buffer, MappedBuffer *map)\n+{\n+\tif (buffer->planes().size() != 1) {\n+\t\tqWarning() << \"Multi-planar buffers are not supported\";\n+\t\treturn;\n+\t}\n+\n+\tif (buffer_)\n+\t\trenderComplete(buffer_);\n+\n+\tyuvData_ = static_cast<unsigned char *>(map->memory);\n+\tupdate();\n+\tbuffer_ = buffer;\n+}\n+\n+bool ViewFinderGL::selectFormat(const libcamera::PixelFormat &format)\n+{\n+\tbool ret = true;\n+\tswitch (format) {\n+\tcase libcamera::formats::NV12:\n+\t\thorzSubSample_ = 2;\n+\t\tvertSubSample_ = 2;\n+\t\tvertexShaderSrc_ = \":NV_vertex_shader.glsl\";\n+\t\tfragmentShaderSrc_ = \":NV_2_planes_UV_f.glsl\";\n+\t\tbreak;\n+\tcase libcamera::formats::NV21:\n+\t\thorzSubSample_ = 2;\n+\t\tvertSubSample_ = 2;\n+\t\tvertexShaderSrc_ = \":NV_vertex_shader.glsl\";\n+\t\tfragmentShaderSrc_ = \":NV_2_planes_VU_f.glsl\";\n+\t\tbreak;\n+\tcase libcamera::formats::NV16:\n+\t\thorzSubSample_ = 2;\n+\t\tvertSubSample_ = 1;\n+\t\tvertexShaderSrc_ = \":NV_vertex_shader.glsl\";\n+\t\tfragmentShaderSrc_ = \":NV_2_planes_UV_f.glsl\";\n+\t\tbreak;\n+\tcase libcamera::formats::NV61:\n+\t\thorzSubSample_ = 2;\n+\t\tvertSubSample_ = 1;\n+\t\tvertexShaderSrc_ = \":NV_vertex_shader.glsl\";\n+\t\tfragmentShaderSrc_ = \":NV_2_planes_VU_f.glsl\";\n+\t\tbreak;\n+\tcase libcamera::formats::NV24:\n+\t\thorzSubSample_ = 1;\n+\t\tvertSubSample_ = 1;\n+\t\tvertexShaderSrc_ = \":NV_vertex_shader.glsl\";\n+\t\tfragmentShaderSrc_ = \":NV_2_planes_UV_f.glsl\";\n+\t\tbreak;\n+\tcase libcamera::formats::NV42:\n+\t\thorzSubSample_ = 1;\n+\t\tvertSubSample_ = 1;\n+\t\tvertexShaderSrc_ = \":NV_vertex_shader.glsl\";\n+\t\tfragmentShaderSrc_ = \":NV_2_planes_VU_f.glsl\";\n+\t\tbreak;\n+\tcase libcamera::formats::YUV420:\n+\t\thorzSubSample_ = 2;\n+\t\tvertSubSample_ = 2;\n+\t\tvertexShaderSrc_ = \":NV_vertex_shader.glsl\";\n+\t\tfragmentShaderSrc_ = \":NV_3_planes_f.glsl\";\n+\t\tbreak;\n+\tcase libcamera::formats::YVU420:\n+\t\thorzSubSample_ = 2;\n+\t\tvertSubSample_ = 2;\n+\t\tvertexShaderSrc_ = \":NV_vertex_shader.glsl\";\n+\t\tfragmentShaderSrc_ = \":NV_3_planes_f.glsl\";\n+\t\tbreak;\n+\tdefault:\n+\t\tret = false;\n+\t\tqWarning() << \"[ViewFinderGL]:\"\n+\t\t\t   << \"format not supported.\";\n+\t\tbreak;\n+\t};\n+\n+\treturn ret;\n+}\n+\n+bool ViewFinderGL::createVertexShader()\n+{\n+\t/* Create Vertex Shader */\n+\tvertexShader_ = new QOpenGLShader(QOpenGLShader::Vertex, this);\n+\n+\t/* Compile the vertex shader */\n+\tif (!vertexShader_->compileSourceFile(vertexShaderSrc_)) {\n+\t\tqWarning() << \"[ViewFinderGL]:\" << vertexShader_->log();\n+\t\treturn false;\n+\t}\n+\n+\tshaderProgram_.addShader(vertexShader_);\n+\treturn true;\n+}\n+\n+bool ViewFinderGL::createFragmentShader()\n+{\n+\tint attributeVertex;\n+\tint attributeTexture;\n+\n+\t/* Create Fragment Shader */\n+\tfragmentShader_ = new QOpenGLShader(QOpenGLShader::Fragment, this);\n+\n+\t/* Compile the fragment shader */\n+\tif (!fragmentShader_->compileSourceFile(fragmentShaderSrc_)) {\n+\t\tqWarning() << \"[ViewFinderGL]:\" << fragmentShader_->log();\n+\t\treturn false;\n+\t}\n+\n+\tshaderProgram_.addShader(fragmentShader_);\n+\n+\t/* Link shader pipeline */\n+\tif (!shaderProgram_.link()) {\n+\t\tqWarning() << \"[ViewFinderGL]:\" << shaderProgram_.log();\n+\t\tclose();\n+\t}\n+\n+\t/* Bind shader pipeline for use */\n+\tif (!shaderProgram_.bind()) {\n+\t\tqWarning() << \"[ViewFinderGL]:\" << shaderProgram_.log();\n+\t\tclose();\n+\t}\n+\n+\tattributeVertex = shaderProgram_.attributeLocation(\"vertexIn\");\n+\tattributeTexture = shaderProgram_.attributeLocation(\"textureIn\");\n+\n+\tshaderProgram_.enableAttributeArray(attributeVertex);\n+\tshaderProgram_.setAttributeBuffer(attributeVertex,\n+\t\t\t\t\t  GL_FLOAT,\n+\t\t\t\t\t  0,\n+\t\t\t\t\t  2,\n+\t\t\t\t\t  2 * sizeof(GLfloat));\n+\n+\tshaderProgram_.enableAttributeArray(attributeTexture);\n+\tshaderProgram_.setAttributeBuffer(attributeTexture,\n+\t\t\t\t\t  GL_FLOAT,\n+\t\t\t\t\t  8 * sizeof(GLfloat),\n+\t\t\t\t\t  2,\n+\t\t\t\t\t  2 * sizeof(GLfloat));\n+\n+\ttextureUniformY_ = shaderProgram_.uniformLocation(\"tex_y\");\n+\ttextureUniformU_ = shaderProgram_.uniformLocation(\"tex_u\");\n+\ttextureUniformV_ = shaderProgram_.uniformLocation(\"tex_v\");\n+\n+\tif (!textureY_.isCreated())\n+\t\ttextureY_.create();\n+\n+\tif (!textureU_.isCreated())\n+\t\ttextureU_.create();\n+\n+\tif (!textureV_.isCreated())\n+\t\ttextureV_.create();\n+\n+\tid_y_ = textureY_.textureId();\n+\tid_u_ = textureU_.textureId();\n+\tid_v_ = textureV_.textureId();\n+\treturn true;\n+}\n+\n+void ViewFinderGL::configureTexture(unsigned int id)\n+{\n+\tglBindTexture(GL_TEXTURE_2D, id);\n+\tglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\n+\tglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\n+\tglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);\n+\tglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);\n+}\n+\n+void ViewFinderGL::removeShader()\n+{\n+\tif (shaderProgram_.isLinked()) {\n+\t\tshaderProgram_.release();\n+\t\tshaderProgram_.removeAllShaders();\n+\t}\n+\n+\tif (fragmentShader_)\n+\t\tdelete fragmentShader_;\n+\n+\tif (vertexShader_)\n+\t\tdelete vertexShader_;\n+}\n+\n+void ViewFinderGL::initializeGL()\n+{\n+\tinitializeOpenGLFunctions();\n+\tglEnable(GL_TEXTURE_2D);\n+\tglDisable(GL_DEPTH_TEST);\n+\n+\tstatic const GLfloat coordinates[2][4][2]{\n+\t\t{\n+\t\t\t/* Vertex coordinates */\n+\t\t\t{ -1.0f, -1.0f },\n+\t\t\t{ -1.0f, +1.0f },\n+\t\t\t{ +1.0f, +1.0f },\n+\t\t\t{ +1.0f, -1.0f },\n+\t\t},\n+\t\t{\n+\t\t\t/* Texture coordinates */\n+\t\t\t{ 0.0f, 1.0f },\n+\t\t\t{ 0.0f, 0.0f },\n+\t\t\t{ 1.0f, 0.0f },\n+\t\t\t{ 1.0f, 1.0f },\n+\t\t},\n+\t};\n+\n+\tvertexBuffer_.create();\n+\tvertexBuffer_.bind();\n+\tvertexBuffer_.allocate(coordinates, sizeof(coordinates));\n+\n+\t/* Create Vertex Shader */\n+\tif (!createVertexShader())\n+\t\tqWarning() << \"[ViewFinderGL]: create vertex shader failed.\";\n+\n+\tglClearColor(1.0f, 1.0f, 1.0f, 0.0f);\n+}\n+\n+void ViewFinderGL::doRender()\n+{\n+\tswitch (format_) {\n+\tcase libcamera::formats::NV12:\n+\tcase libcamera::formats::NV21:\n+\tcase libcamera::formats::NV16:\n+\tcase libcamera::formats::NV61:\n+\tcase libcamera::formats::NV24:\n+\tcase libcamera::formats::NV42:\n+\t\t/* Activate texture Y */\n+\t\tglActiveTexture(GL_TEXTURE0);\n+\t\tconfigureTexture(id_y_);\n+\t\tglTexImage2D(GL_TEXTURE_2D,\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     size_.width(),\n+\t\t\t     size_.height(),\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     GL_UNSIGNED_BYTE,\n+\t\t\t     yuvData_);\n+\t\tshaderProgram_.setUniformValue(textureUniformY_, 0);\n+\n+\t\t/* Activate texture UV/VU */\n+\t\tglActiveTexture(GL_TEXTURE1);\n+\t\tconfigureTexture(id_u_);\n+\t\tglTexImage2D(GL_TEXTURE_2D,\n+\t\t\t     0,\n+\t\t\t     GL_RG,\n+\t\t\t     size_.width() / horzSubSample_,\n+\t\t\t     size_.height() / vertSubSample_,\n+\t\t\t     0,\n+\t\t\t     GL_RG,\n+\t\t\t     GL_UNSIGNED_BYTE,\n+\t\t\t     (char *)yuvData_ + size_.width() * size_.height());\n+\t\tshaderProgram_.setUniformValue(textureUniformU_, 1);\n+\t\tbreak;\n+\n+\tcase libcamera::formats::YUV420:\n+\t\t/* Activate texture Y */\n+\t\tglActiveTexture(GL_TEXTURE0);\n+\t\tconfigureTexture(id_y_);\n+\t\tglTexImage2D(GL_TEXTURE_2D,\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     size_.width(),\n+\t\t\t     size_.height(),\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     GL_UNSIGNED_BYTE,\n+\t\t\t     yuvData_);\n+\t\tshaderProgram_.setUniformValue(textureUniformY_, 0);\n+\n+\t\t/* Activate texture U */\n+\t\tglActiveTexture(GL_TEXTURE1);\n+\t\tconfigureTexture(id_u_);\n+\t\tglTexImage2D(GL_TEXTURE_2D,\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     size_.width() / horzSubSample_,\n+\t\t\t     size_.height() / vertSubSample_,\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     GL_UNSIGNED_BYTE,\n+\t\t\t     (char *)yuvData_ + size_.width() * size_.height());\n+\t\tshaderProgram_.setUniformValue(textureUniformU_, 1);\n+\n+\t\t/* Activate texture V */\n+\t\tglActiveTexture(GL_TEXTURE2);\n+\t\tconfigureTexture(id_v_);\n+\t\tglTexImage2D(GL_TEXTURE_2D,\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     size_.width() / horzSubSample_,\n+\t\t\t     size_.height() / vertSubSample_,\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     GL_UNSIGNED_BYTE,\n+\t\t\t     (char *)yuvData_ + size_.width() * size_.height() * 5 / 4);\n+\t\tshaderProgram_.setUniformValue(textureUniformV_, 2);\n+\t\tbreak;\n+\n+\tcase libcamera::formats::YVU420:\n+\t\t/* Activate texture Y */\n+\t\tglActiveTexture(GL_TEXTURE0);\n+\t\tconfigureTexture(id_y_);\n+\t\tglTexImage2D(GL_TEXTURE_2D,\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     size_.width(),\n+\t\t\t     size_.height(),\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     GL_UNSIGNED_BYTE,\n+\t\t\t     yuvData_);\n+\t\tshaderProgram_.setUniformValue(textureUniformY_, 0);\n+\n+\t\t/* Activate texture V */\n+\t\tglActiveTexture(GL_TEXTURE2);\n+\t\tconfigureTexture(id_v_);\n+\t\tglTexImage2D(GL_TEXTURE_2D,\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     size_.width() / horzSubSample_,\n+\t\t\t     size_.height() / vertSubSample_,\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     GL_UNSIGNED_BYTE,\n+\t\t\t     (char *)yuvData_ + size_.width() * size_.height());\n+\t\tshaderProgram_.setUniformValue(textureUniformV_, 2);\n+\n+\t\t/* Activate texture U */\n+\t\tglActiveTexture(GL_TEXTURE1);\n+\t\tconfigureTexture(id_u_);\n+\t\tglTexImage2D(GL_TEXTURE_2D,\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     size_.width() / horzSubSample_,\n+\t\t\t     size_.height() / vertSubSample_,\n+\t\t\t     0,\n+\t\t\t     GL_RED,\n+\t\t\t     GL_UNSIGNED_BYTE,\n+\t\t\t     (char *)yuvData_ + size_.width() * size_.height() * 5 / 4);\n+\t\tshaderProgram_.setUniformValue(textureUniformU_, 1);\n+\t\tbreak;\n+\n+\tdefault:\n+\t\tbreak;\n+\t};\n+}\n+\n+void ViewFinderGL::paintGL()\n+{\n+\tif (!fragmentShader_)\n+\t\tif (!createFragmentShader()) {\n+\t\t\tqWarning() << \"[ViewFinderGL]:\"\n+\t\t\t\t   << \"create fragment shader failed.\";\n+\t\t}\n+\n+\tif (yuvData_) {\n+\t\tglClearColor(0.0, 0.0, 0.0, 1.0);\n+\t\tglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\n+\n+\t\tdoRender();\n+\t\tglDrawArrays(GL_TRIANGLE_FAN, 0, 4);\n+\t}\n+}\n+\n+void ViewFinderGL::resizeGL(int w, int h)\n+{\n+\tglViewport(0, 0, w, h);\n+}\n+\n+QSize ViewFinderGL::sizeHint() const\n+{\n+\treturn size_.isValid() ? size_ : QSize(640, 480);\n+}\ndiff --git a/src/qcam/viewfinder_gl.h b/src/qcam/viewfinder_gl.h\nnew file mode 100644\nindex 000000000000..69502b7a543e\n--- /dev/null\n+++ b/src/qcam/viewfinder_gl.h\n@@ -0,0 +1,96 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Linaro\n+ *\n+ * viewfinder_GL.h - OpenGL Viewfinder for rendering by OpenGL shader\n+ *\n+ */\n+#ifndef __VIEWFINDER_GL_H__\n+#define __VIEWFINDER_GL_H__\n+\n+#include <QImage>\n+#include <QMutex>\n+#include <QOpenGLBuffer>\n+#include <QOpenGLFunctions>\n+#include <QOpenGLShader>\n+#include <QOpenGLShaderProgram>\n+#include <QOpenGLTexture>\n+#include <QOpenGLWidget>\n+#include <QSize>\n+\n+#include <libcamera/buffer.h>\n+#include <libcamera/formats.h>\n+\n+#include \"viewfinder.h\"\n+\n+class ViewFinderGL : public QOpenGLWidget,\n+\t\t     public ViewFinder,\n+\t\t     protected QOpenGLFunctions\n+{\n+\tQ_OBJECT\n+\n+public:\n+\tViewFinderGL(QWidget *parent = nullptr);\n+\t~ViewFinderGL();\n+\n+\tconst QList<libcamera::PixelFormat> &nativeFormats() const override;\n+\n+\tint setFormat(const libcamera::PixelFormat &format, const QSize &size) override;\n+\tvoid render(libcamera::FrameBuffer *buffer, MappedBuffer *map) override;\n+\tvoid stop() override;\n+\n+\tQImage getCurrentImage() override;\n+\n+Q_SIGNALS:\n+\tvoid renderComplete(libcamera::FrameBuffer *buffer);\n+\n+protected:\n+\tvoid initializeGL() override;\n+\tvoid paintGL() override;\n+\tvoid resizeGL(int w, int h) override;\n+\tQSize sizeHint() const override;\n+\n+private:\n+\tbool selectFormat(const libcamera::PixelFormat &format);\n+\n+\tvoid configureTexture(unsigned int id);\n+\tbool createFragmentShader();\n+\tbool createVertexShader();\n+\tvoid removeShader();\n+\tvoid doRender();\n+\n+\t/* Captured image size, format and buffer */\n+\tlibcamera::FrameBuffer *buffer_;\n+\tlibcamera::PixelFormat format_;\n+\tQSize size_;\n+\tunsigned char *yuvData_;\n+\n+\t/* OpenGL components for rendering */\n+\tQOpenGLShader *fragmentShader_;\n+\tQOpenGLShader *vertexShader_;\n+\tQOpenGLShaderProgram shaderProgram_;\n+\n+\t/* Vertex buffer */\n+\tQOpenGLBuffer vertexBuffer_;\n+\n+\t/* Fragment and Vertex shader file name */\n+\tQString fragmentShaderSrc_;\n+\tQString vertexShaderSrc_;\n+\n+\t/* YUV texture planars and parameters */\n+\tGLuint id_u_;\n+\tGLuint id_v_;\n+\tGLuint id_y_;\n+\tGLuint textureUniformU_;\n+\tGLuint textureUniformV_;\n+\tGLuint textureUniformY_;\n+\tQOpenGLTexture textureU_;\n+\tQOpenGLTexture textureV_;\n+\tQOpenGLTexture textureY_;\n+\tunsigned int horzSubSample_;\n+\tunsigned int vertSubSample_;\n+\n+\tQMutex mutex_; /* Prevent concurrent access to image_ */\n+};\n+\n+#endif /* __VIEWFINDER_GL_H__ */\n","prefixes":["libcamera-devel","v7","3/4"]}