From 6e410dcef55d55a40305baa574ace61d5f7fe86b Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Sat, 26 May 2018 11:26:58 +0800 Subject: [PATCH] camera: refactor (add qt_camera_base) --- src/citra_qt/CMakeLists.txt | 4 +- src/citra_qt/camera/qt_camera_base.cpp | 57 ++++++++++++++++++++ src/citra_qt/camera/qt_camera_base.h | 36 +++++++++++++ src/citra_qt/camera/qt_camera_factory.cpp | 24 --------- src/citra_qt/camera/qt_camera_factory.h | 18 ------- src/citra_qt/camera/qt_multimedia_camera.cpp | 31 ++--------- src/citra_qt/camera/qt_multimedia_camera.h | 14 ++--- src/citra_qt/camera/still_image_camera.cpp | 32 ++--------- src/citra_qt/camera/still_image_camera.h | 14 ++--- 9 files changed, 107 insertions(+), 123 deletions(-) create mode 100644 src/citra_qt/camera/qt_camera_base.cpp create mode 100644 src/citra_qt/camera/qt_camera_base.h delete mode 100644 src/citra_qt/camera/qt_camera_factory.cpp delete mode 100644 src/citra_qt/camera/qt_camera_factory.h diff --git a/src/citra_qt/CMakeLists.txt b/src/citra_qt/CMakeLists.txt index 4e0fcdbeb0..7b0d0eaac9 100644 --- a/src/citra_qt/CMakeLists.txt +++ b/src/citra_qt/CMakeLists.txt @@ -13,8 +13,8 @@ add_executable(citra-qt camera/camera_util.h camera/still_image_camera.cpp camera/still_image_camera.h - camera/qt_camera_factory.cpp - camera/qt_camera_factory.h + camera/qt_camera_base.cpp + camera/qt_camera_base.h camera/qt_multimedia_camera.cpp camera/qt_multimedia_camera.h citra-qt.rc diff --git a/src/citra_qt/camera/qt_camera_base.cpp b/src/citra_qt/camera/qt_camera_base.cpp new file mode 100644 index 0000000000..65bf2361c9 --- /dev/null +++ b/src/citra_qt/camera/qt_camera_base.cpp @@ -0,0 +1,57 @@ +// Copyright 2018 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include "citra_qt/camera/camera_util.h" +#include "citra_qt/camera/qt_camera_base.h" + +namespace Camera { + +QtCameraInterface::QtCameraInterface(const Service::CAM::Flip& flip) { + using namespace Service::CAM; + flip_horizontal = basic_flip_horizontal = (flip == Flip::Horizontal) || (flip == Flip::Reverse); + flip_vertical = basic_flip_vertical = (flip == Flip::Vertical) || (flip == Flip::Reverse); +} + +void QtCameraInterface::SetFormat(Service::CAM::OutputFormat output_format) { + output_rgb = output_format == Service::CAM::OutputFormat::RGB565; +} + +void QtCameraInterface::SetResolution(const Service::CAM::Resolution& resolution) { + width = resolution.width; + height = resolution.height; +} + +void QtCameraInterface::SetFlip(Service::CAM::Flip flip) { + using namespace Service::CAM; + flip_horizontal = basic_flip_horizontal ^ (flip == Flip::Horizontal || flip == Flip::Reverse); + flip_vertical = basic_flip_vertical ^ (flip == Flip::Vertical || flip == Flip::Reverse); +} + +void QtCameraInterface::SetEffect(Service::CAM::Effect effect) { + if (effect != Service::CAM::Effect::None) { + NGLOG_ERROR(Service_CAM, "Unimplemented effect {}", static_cast(effect)); + } +} + +std::vector QtCameraInterface::ReceiveFrame() { + return CameraUtil::ProcessImage(QtReceiveFrame(), width, height, output_rgb, flip_horizontal, + flip_vertical); +} + +std::unique_ptr QtCameraFactory::CreatePreview( + const std::string& config, int width, int height, const Service::CAM::Flip& flip) const { + std::unique_ptr camera = Create(config, flip); + + if (camera->IsPreviewAvailable()) { + return camera; + } + QMessageBox::critical( + nullptr, QObject::tr("Error"), + (config.empty() ? QObject::tr("Couldn't load the camera") + : QObject::tr("Couldn't load %1").arg(QString::fromStdString(config)))); + return nullptr; +} + +} // namespace Camera diff --git a/src/citra_qt/camera/qt_camera_base.h b/src/citra_qt/camera/qt_camera_base.h new file mode 100644 index 0000000000..e66c3bb46a --- /dev/null +++ b/src/citra_qt/camera/qt_camera_base.h @@ -0,0 +1,36 @@ +// Copyright 2018 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include "core/frontend/camera/factory.h" + +namespace Camera { + +// Base class for camera interfaces of citra_qt +class QtCameraInterface : public CameraInterface { +public: + QtCameraInterface(const Service::CAM::Flip& flip); + void SetResolution(const Service::CAM::Resolution&) override; + void SetFlip(Service::CAM::Flip) override; + void SetEffect(Service::CAM::Effect) override; + void SetFormat(Service::CAM::OutputFormat) override; + std::vector ReceiveFrame() override; + virtual QImage QtReceiveFrame() = 0; + +private: + int width, height; + bool output_rgb; + bool flip_horizontal, flip_vertical; + bool basic_flip_horizontal, basic_flip_vertical; +}; + +// Base class for camera factories of citra_qt +class QtCameraFactory : public CameraFactory { + std::unique_ptr CreatePreview(const std::string& config, int width, int height, + const Service::CAM::Flip& flip) const override; +}; + +} // namespace Camera diff --git a/src/citra_qt/camera/qt_camera_factory.cpp b/src/citra_qt/camera/qt_camera_factory.cpp deleted file mode 100644 index 063a600df3..0000000000 --- a/src/citra_qt/camera/qt_camera_factory.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2018 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include -#include "citra_qt/camera/qt_camera_factory.h" - -namespace Camera { - -std::unique_ptr QtCameraFactory::CreatePreview( - const std::string& config, int width, int height, const Service::CAM::Flip& flip) const { - std::unique_ptr camera = Create(config, flip); - - if (camera->IsPreviewAvailable()) { - return camera; - } - QMessageBox::critical( - nullptr, QObject::tr("Error"), - (config.empty() ? QObject::tr("Couldn't load the camera") - : QObject::tr("Couldn't load %1").arg(QString::fromStdString(config)))); - return nullptr; -} - -} // namespace Camera diff --git a/src/citra_qt/camera/qt_camera_factory.h b/src/citra_qt/camera/qt_camera_factory.h deleted file mode 100644 index 9e590ca66d..0000000000 --- a/src/citra_qt/camera/qt_camera_factory.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2018 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include "core/frontend/camera/factory.h" - -namespace Camera { - -// Base class for camera factories of citra_qt -class QtCameraFactory : public CameraFactory { - std::unique_ptr CreatePreview(const std::string& config, int width, int height, - const Service::CAM::Flip& flip) const override; -}; - -} // namespace Camera diff --git a/src/citra_qt/camera/qt_multimedia_camera.cpp b/src/citra_qt/camera/qt_multimedia_camera.cpp index 5f2137835c..abf2ff25e6 100644 --- a/src/citra_qt/camera/qt_multimedia_camera.cpp +++ b/src/citra_qt/camera/qt_multimedia_camera.cpp @@ -48,16 +48,13 @@ bool QtCameraSurface::present(const QVideoFrame& frame) { QtMultimediaCamera::QtMultimediaCamera(const std::string& camera_name, const Service::CAM::Flip& flip) - : handler(QtMultimediaCameraHandler::GetHandler()) { + : QtCameraInterface(flip), handler(QtMultimediaCameraHandler::GetHandler()) { if (handler->thread() == QThread::currentThread()) { handler->CreateCamera(camera_name); } else { QMetaObject::invokeMethod(handler.get(), "CreateCamera", Qt::BlockingQueuedConnection, Q_ARG(const std::string&, camera_name)); } - using namespace Service::CAM; - flip_horizontal = basic_flip_horizontal = (flip == Flip::Horizontal) || (flip == Flip::Reverse); - flip_vertical = basic_flip_vertical = (flip == Flip::Vertical) || (flip == Flip::Reverse); } QtMultimediaCamera::~QtMultimediaCamera() { @@ -77,10 +74,6 @@ void QtMultimediaCamera::StopCapture() { handler->StopCamera(); } -void QtMultimediaCamera::SetFormat(Service::CAM::OutputFormat output_format) { - output_rgb = output_format == Service::CAM::OutputFormat::RGB565; -} - void QtMultimediaCamera::SetFrameRate(Service::CAM::FrameRate frame_rate) { const std::array FrameRateList = { /* Rate_15 */ QCamera::FrameRateRange(15, 15), @@ -104,27 +97,9 @@ void QtMultimediaCamera::SetFrameRate(Service::CAM::FrameRate frame_rate) { handler->settings.setMinimumFrameRate(framerate.maximumFrameRate); } -void QtMultimediaCamera::SetResolution(const Service::CAM::Resolution& resolution) { - width = resolution.width; - height = resolution.height; -} - -void QtMultimediaCamera::SetFlip(Service::CAM::Flip flip) { - using namespace Service::CAM; - flip_horizontal = basic_flip_horizontal ^ (flip == Flip::Horizontal || flip == Flip::Reverse); - flip_vertical = basic_flip_vertical ^ (flip == Flip::Vertical || flip == Flip::Reverse); -} - -void QtMultimediaCamera::SetEffect(Service::CAM::Effect effect) { - if (effect != Service::CAM::Effect::None) { - NGLOG_ERROR(Service_CAM, "Unimplemented effect {}", static_cast(effect)); - } -} - -std::vector QtMultimediaCamera::ReceiveFrame() { +QImage QtMultimediaCamera::QtReceiveFrame() { QMutexLocker locker(&handler->camera_surface.mutex); - return CameraUtil::ProcessImage(handler->camera_surface.current_frame, width, height, - output_rgb, flip_horizontal, flip_vertical); + return handler->camera_surface.current_frame; } bool QtMultimediaCamera::IsPreviewAvailable() { diff --git a/src/citra_qt/camera/qt_multimedia_camera.h b/src/citra_qt/camera/qt_multimedia_camera.h index 580834275b..41024214cb 100644 --- a/src/citra_qt/camera/qt_multimedia_camera.h +++ b/src/citra_qt/camera/qt_multimedia_camera.h @@ -13,7 +13,7 @@ #include #include #include "citra_qt/camera/camera_util.h" -#include "citra_qt/camera/qt_camera_factory.h" +#include "citra_qt/camera/qt_camera_base.h" #include "core/frontend/camera/interface.h" class GMainWindow; @@ -36,26 +36,18 @@ private: class QtMultimediaCameraHandler; /// This class is only an interface. It just calls QtMultimediaCameraHandler. -class QtMultimediaCamera final : public CameraInterface { +class QtMultimediaCamera final : public QtCameraInterface { public: QtMultimediaCamera(const std::string& camera_name, const Service::CAM::Flip& flip); ~QtMultimediaCamera(); void StartCapture() override; void StopCapture() override; - void SetResolution(const Service::CAM::Resolution&) override; - void SetFlip(Service::CAM::Flip) override; - void SetEffect(Service::CAM::Effect) override; - void SetFormat(Service::CAM::OutputFormat) override; void SetFrameRate(Service::CAM::FrameRate frame_rate) override; - std::vector ReceiveFrame() override; + QImage QtReceiveFrame() override; bool IsPreviewAvailable() override; private: std::shared_ptr handler; - int width, height; - bool output_rgb; - bool flip_horizontal, flip_vertical; - bool basic_flip_horizontal, basic_flip_vertical; }; class QtMultimediaCameraFactory final : public QtCameraFactory { diff --git a/src/citra_qt/camera/still_image_camera.cpp b/src/citra_qt/camera/still_image_camera.cpp index 7e54fad3ee..2b9f4e54bc 100644 --- a/src/citra_qt/camera/still_image_camera.cpp +++ b/src/citra_qt/camera/still_image_camera.cpp @@ -10,40 +10,14 @@ namespace Camera { StillImageCamera::StillImageCamera(QImage image_, const Service::CAM::Flip& flip) - : image(std::move(image_)) { - using namespace Service::CAM; - flip_horizontal = basic_flip_horizontal = (flip == Flip::Horizontal) || (flip == Flip::Reverse); - flip_vertical = basic_flip_vertical = (flip == Flip::Vertical) || (flip == Flip::Reverse); -} + : QtCameraInterface(flip), image(std::move(image_)) {} void StillImageCamera::StartCapture() {} void StillImageCamera::StopCapture() {} -void StillImageCamera::SetFormat(Service::CAM::OutputFormat output_format) { - output_rgb = output_format == Service::CAM::OutputFormat::RGB565; -} - -void StillImageCamera::SetResolution(const Service::CAM::Resolution& resolution) { - width = resolution.width; - height = resolution.height; -} - -void StillImageCamera::SetFlip(Service::CAM::Flip flip) { - using namespace Service::CAM; - flip_horizontal = basic_flip_horizontal ^ (flip == Flip::Horizontal || flip == Flip::Reverse); - flip_vertical = basic_flip_vertical ^ (flip == Flip::Vertical || flip == Flip::Reverse); -} - -void StillImageCamera::SetEffect(Service::CAM::Effect effect) { - if (effect != Service::CAM::Effect::None) { - NGLOG_ERROR(Service_CAM, "Unimplemented effect {}", static_cast(effect)); - } -} - -std::vector StillImageCamera::ReceiveFrame() { - return CameraUtil::ProcessImage(image, width, height, output_rgb, flip_horizontal, - flip_vertical); +QImage StillImageCamera::QtReceiveFrame() { + return image; } bool StillImageCamera::IsPreviewAvailable() { diff --git a/src/citra_qt/camera/still_image_camera.h b/src/citra_qt/camera/still_image_camera.h index e9016981bd..11011622fc 100644 --- a/src/citra_qt/camera/still_image_camera.h +++ b/src/citra_qt/camera/still_image_camera.h @@ -7,30 +7,22 @@ #include #include #include "citra_qt/camera/camera_util.h" -#include "citra_qt/camera/qt_camera_factory.h" +#include "citra_qt/camera/qt_camera_base.h" #include "core/frontend/camera/interface.h" namespace Camera { -class StillImageCamera final : public CameraInterface { +class StillImageCamera final : public QtCameraInterface { public: StillImageCamera(QImage image, const Service::CAM::Flip& flip); void StartCapture() override; void StopCapture() override; - void SetResolution(const Service::CAM::Resolution&) override; - void SetFlip(Service::CAM::Flip) override; - void SetEffect(Service::CAM::Effect) override; - void SetFormat(Service::CAM::OutputFormat) override; void SetFrameRate(Service::CAM::FrameRate frame_rate) override {} - std::vector ReceiveFrame() override; + QImage QtReceiveFrame() override; bool IsPreviewAvailable() override; private: QImage image; - int width, height; - bool output_rgb; - bool flip_horizontal, flip_vertical; - bool basic_flip_horizontal, basic_flip_vertical; }; class StillImageCameraFactory final : public QtCameraFactory {