From 78e7aacd7dd8b404ba4e2450ccd407a6ced22bc8 Mon Sep 17 00:00:00 2001 From: Dominik Holland Date: Fri, 31 Aug 2018 16:14:03 +0200 Subject: Add initial camera application for Neptune 3 UI Change-Id: I04848271012128eef9654c3bd6a7bdcd868cf6f2 Reviewed-by: Bramastyo Harimukti Santoso --- com.pelagicore.camera/plugin/camera_plugin.cpp | 60 ++++++ com.pelagicore.camera/plugin/camera_plugin.h | 68 +++++++ com.pelagicore.camera/plugin/camerastream.cpp | 258 +++++++++++++++++++++++++ com.pelagicore.camera/plugin/camerastream.h | 109 +++++++++++ com.pelagicore.camera/plugin/plugin.pro | 19 ++ com.pelagicore.camera/plugin/qmldir | 2 + 6 files changed, 516 insertions(+) create mode 100644 com.pelagicore.camera/plugin/camera_plugin.cpp create mode 100644 com.pelagicore.camera/plugin/camera_plugin.h create mode 100644 com.pelagicore.camera/plugin/camerastream.cpp create mode 100644 com.pelagicore.camera/plugin/camerastream.h create mode 100644 com.pelagicore.camera/plugin/plugin.pro create mode 100644 com.pelagicore.camera/plugin/qmldir (limited to 'com.pelagicore.camera/plugin') diff --git a/com.pelagicore.camera/plugin/camera_plugin.cpp b/com.pelagicore.camera/plugin/camera_plugin.cpp new file mode 100644 index 0000000..05939e9 --- /dev/null +++ b/com.pelagicore.camera/plugin/camera_plugin.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Pelagicore AG +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtAuto Extra Apps. +** +** $QT_BEGIN_LICENSE:BSD-QTAS$ +** Commercial License Usage +** Licensees holding valid commercial Qt Automotive Suite licenses may use +** this file in accordance with the commercial license agreement provided +** with the Software or, alternatively, in accordance with the terms +** contained in a written agreement between you and The Qt Company. For +** licensing terms and conditions see https://www.qt.io/terms-conditions. +** For further information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +** SPDX-License-Identifier: BSD-3-Clause +** +****************************************************************************/ +#include "camera_plugin.h" +#include "camerastream.h" + +void CameraPlugin::registerTypes(const char* uri) +{ + Q_ASSERT(uri == QLatin1String("camera")); + + qmlRegisterType(uri, 0, 1, "CameraStream"); +} diff --git a/com.pelagicore.camera/plugin/camera_plugin.h b/com.pelagicore.camera/plugin/camera_plugin.h new file mode 100644 index 0000000..3dfd991 --- /dev/null +++ b/com.pelagicore.camera/plugin/camera_plugin.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Pelagicore AG +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtAuto Extra Apps. +** +** $QT_BEGIN_LICENSE:BSD-QTAS$ +** Commercial License Usage +** Licensees holding valid commercial Qt Automotive Suite licenses may use +** this file in accordance with the commercial license agreement provided +** with the Software or, alternatively, in accordance with the terms +** contained in a written agreement between you and The Qt Company. For +** licensing terms and conditions see https://www.qt.io/terms-conditions. +** For further information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +** SPDX-License-Identifier: BSD-3-Clause +** +****************************************************************************/ +#ifndef CAMERAPLUGIN_H +#define CAMERAPLUGIN_H + +#include +#include +#include + +class CameraPlugin : public QQmlExtensionPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") + +public: + void registerTypes(const char* uri) override; +}; + +#endif // CAMERAPLUGIN_H diff --git a/com.pelagicore.camera/plugin/camerastream.cpp b/com.pelagicore.camera/plugin/camerastream.cpp new file mode 100644 index 0000000..3a0d862 --- /dev/null +++ b/com.pelagicore.camera/plugin/camerastream.cpp @@ -0,0 +1,258 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Pelagicore AG +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtAuto Extra Apps. +** +** $QT_BEGIN_LICENSE:BSD-QTAS$ +** Commercial License Usage +** Licensees holding valid commercial Qt Automotive Suite licenses may use +** this file in accordance with the commercial license agreement provided +** with the Software or, alternatively, in accordance with the terms +** contained in a written agreement between you and The Qt Company. For +** licensing terms and conditions see https://www.qt.io/terms-conditions. +** For further information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +** SPDX-License-Identifier: BSD-3-Clause +** +****************************************************************************/ +#include "camerastream.h" +#include "QQuickWindow" +#include +#include +#include +#include +#include +#include + +#ifdef Q_OS_UNIX +#include +#include +#include +#include +#include +#include +#endif + +#define WIDTH 1280 +#define HEIGHT 1080 + +//-------------------------------------------- +#define SWAP_COLORS +//-------------------------------------------- + +CameraStream::CameraStream(QQuickItem* parent) + : QQuickItem(parent) + , m_enableBenchmarkFirstFrameRendered(true) +{ + m_renderTimer.setInterval(30); + m_renderTimer.setSingleShot(false); + + m_framecount = 0; + + m_fb = new uint8_t[WIDTH*HEIGHT*4]; + m_tfb = new uint8_t[WIDTH*HEIGHT*4]; + + connect(this, &QQuickItem::visibleChanged, this, &CameraStream::cameraVisibleChanged); + connect(&m_renderTimer, &QTimer::timeout, this, &QQuickItem::update); + + this->setFlag(QQuickItem::ItemHasContents, true); + + m_image = new QImage(m_fb, WIDTH, HEIGHT, QImage::Format_RGB888); + + m_renderTimer.start(); + + m_angle = 0; +} + +CameraStream::~CameraStream() +{ + delete m_fb; + delete m_tfb; + delete m_image; +} + +void CameraStream::cameraVisibleChanged() +{ + if (isVisible()) { + m_renderTimer.start(); + } else { + m_renderTimer.stop(); + } +} + +int CameraStream::angle() const +{ + return m_angle; +} + +void CameraStream::setAngle(int angle) +{ + if (angle < 0) + angle = 0; + if (angle >= NUM_CAMERAS) + angle = NUM_CAMERAS-1; + + if (m_angle != angle) { + m_angle = angle; +#ifdef Q_OS_UNIX + int f; + f = open("/dev/framegrabber", O_RDONLY); + if (f >= 0) { + ioctl(f, 0, m_angle); + close(f); + } +#endif + emit angleChanged(angle); + } +} + +int CameraStream::hdmiAngle() const +{ + return m_hdmi_angle; +} + +void CameraStream::setHdmiAngle(int angle) +{ + if (angle < 0) + angle = 0; + if (angle >= NUM_CAMERAS) + angle = NUM_CAMERAS-1; + + if (m_hdmi_angle != angle) { + m_hdmi_angle = angle; +#ifdef Q_OS_UNIX + int f; + f = open("/dev/framegrabber", O_RDONLY); + if (f >= 0) { + ioctl(f, 1, m_hdmi_angle); + close(f); + } +#endif + emit hdmiAngleChanged(angle); + } +} + +QString CameraStream::lastError() const +{ + return m_lastError; +} + +void CameraStream::firstFrameRenderBenchmark() +{ + emit firstFrameRendered(); + disconnect(this->window(), &QQuickWindow::frameSwapped, this, &CameraStream::firstFrameRenderBenchmark); +} + +QSGNode* CameraStream::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*) +{ + QQuickWindow *win = QQuickItem::window(); + + QSGSimpleTextureNode *textureNode = static_cast(oldNode); + if (!textureNode) + textureNode = new QSGSimpleTextureNode(); + + if (textureNode->texture()) { + delete textureNode->texture(); + } + + copyFrame(); + + QImage resizedImage = m_image->scaled(width(), height(), Qt::KeepAspectRatio); + auto texture = win->createTextureFromImage(resizedImage); + textureNode->setTexture(texture); + + textureNode->markDirty(QSGNode::DirtyMaterial); + + textureNode->setRect(0, 0, width(), height()); + + m_framecount++; + //enable capturing first frame swap signal after refreshing the first paint of this node. + if (m_enableBenchmarkFirstFrameRendered && isVisible()) { + connect(this->window(), &QQuickWindow::frameSwapped, this, &CameraStream::firstFrameRenderBenchmark); + m_enableBenchmarkFirstFrameRendered = false; + } + + return textureNode; +} + +void CameraStream::copyFrame() { +#ifdef ABGR + int x, y; + uint32_t tmp; +#endif + uint32_t *s, *t; + + s = reinterpret_cast(m_tfb); + t = reinterpret_cast(m_fb); + +#ifdef Q_OS_UNIX + int f; + f = open("/dev/framegrabber", O_RDONLY); + if (f >= 0) { + m_lastError.clear(); + ssize_t num; + num = read(f, m_tfb, WIDTH*HEIGHT*3); + if (num < WIDTH*HEIGHT*3) + qCritical() << "read() returned " << num; + close(f); + } else { + m_lastError = QLatin1String(strerror(errno)); + emit lastErrorChanged(m_lastError); + } +#else + m_lastError = QLatin1String("no camera support on windows"); + emit lastErrorChanged(m_lastError); +#endif + +#ifdef ABGR + for (y = 0; y < HEIGHT;y++) { + for (x = 0; x < WIDTH;x++) { + tmp = s[y * WIDTH + x]; +#ifdef SWAP_COLORS + tmp = + ((tmp & 0x00ff0000) >> 16) | + ((tmp & 0x000000ff) << 16) | + (tmp & 0x0000ff00); +#endif + tmp |= 0xff000000; + *t++ = tmp; + } + } +#else + memcpy(t, s, WIDTH*HEIGHT*3); +#endif +} diff --git a/com.pelagicore.camera/plugin/camerastream.h b/com.pelagicore.camera/plugin/camerastream.h new file mode 100644 index 0000000..e94481c --- /dev/null +++ b/com.pelagicore.camera/plugin/camerastream.h @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Pelagicore AG +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtAuto Extra Apps. +** +** $QT_BEGIN_LICENSE:BSD-QTAS$ +** Commercial License Usage +** Licensees holding valid commercial Qt Automotive Suite licenses may use +** this file in accordance with the commercial license agreement provided +** with the Software or, alternatively, in accordance with the terms +** contained in a written agreement between you and The Qt Company. For +** licensing terms and conditions see https://www.qt.io/terms-conditions. +** For further information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +** SPDX-License-Identifier: BSD-3-Clause +** +****************************************************************************/ +#ifndef CAMERASTREAM_H +#define CAMERASTREAM_H + +#include +#include +#include + +#define NUM_CAMERAS 4 + +class CameraStream : public QQuickItem +{ + Q_OBJECT + + Q_PROPERTY(int angle READ angle WRITE setAngle NOTIFY angleChanged) + Q_PROPERTY(int hdmi_angle READ hdmiAngle WRITE setHdmiAngle NOTIFY hdmiAngleChanged) + Q_PROPERTY(QString lastError READ lastError NOTIFY lastErrorChanged) + +public: + CameraStream(QQuickItem* parent = nullptr); + ~CameraStream() override; + + int angle() const; + void setAngle(int angle); + + int hdmiAngle() const; + void setHdmiAngle(int angle); + + QString lastError() const; + +signals: + void firstFrameRendered(); + void angleChanged(int mode); + void hdmiAngleChanged(int mode); + void lastErrorChanged(QString lastError); + +private slots: + void cameraVisibleChanged(); + void firstFrameRenderBenchmark(); + +protected: + QSGNode* updatePaintNode(QSGNode*, UpdatePaintNodeData*) override; + +private: + void copyFrame(); + + QTimer m_renderTimer; + + bool m_enableBenchmarkFirstFrameRendered; + int m_framecount; + uint8_t *m_fb; + uint8_t *m_tfb; + QImage *m_image; + int m_angle; + int m_hdmi_angle; + QString m_lastError; +}; + +#endif // CAMERASTREAM_H diff --git a/com.pelagicore.camera/plugin/plugin.pro b/com.pelagicore.camera/plugin/plugin.pro new file mode 100644 index 0000000..8988a37 --- /dev/null +++ b/com.pelagicore.camera/plugin/plugin.pro @@ -0,0 +1,19 @@ +TEMPLATE = lib +TARGET = camera +QT += qml quick +CONFIG += plugin + +TARGET = $$qtLibraryTarget($$TARGET) +uri = camera +load(qmlplugin) + +# Input +SOURCES += \ + camera_plugin.cpp \ + camerastream.cpp + +HEADERS += \ + camera_plugin.h \ + camerastream.h + +OTHER_FILES = qmldir diff --git a/com.pelagicore.camera/plugin/qmldir b/com.pelagicore.camera/plugin/qmldir new file mode 100644 index 0000000..ff5dd50 --- /dev/null +++ b/com.pelagicore.camera/plugin/qmldir @@ -0,0 +1,2 @@ +module camera +plugin camera . -- cgit v1.2.3