[v3,3/4] apps: cam: sdl_texture: Add `SDLTexture1Plane`
diff mbox series

Message ID 20250428084418.1251085-4-barnabas.pocze@ideasonboard.com
State Superseded
Headers show
Series
  • apps: cam: sdl_sink: Support more RGB and YUV formats
Related show

Commit Message

Barnabás Pőcze April 28, 2025, 8:44 a.m. UTC
`SDLTextureYUYV` uses `SDL_PIXELFORMAT_YUY2`, which is a single plane
format. To support other single plane formats, replace `SDLTextureYUYV`
with `SDLTexture1Plane` that can be instantiated with an arbitrary SDL
pixel format and that uses `SDL_UpdateTexture()` to update the texture
using exactly a single plane.

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/apps/cam/meson.build            |  1 +
 src/apps/cam/sdl_sink.cpp           |  3 ++-
 src/apps/cam/sdl_texture_1plane.cpp | 17 +++++++++++++++++
 src/apps/cam/sdl_texture_1plane.h   | 18 ++++++++++++++++++
 src/apps/cam/sdl_texture_yuv.cpp    | 10 ----------
 src/apps/cam/sdl_texture_yuv.h      |  7 -------
 6 files changed, 38 insertions(+), 18 deletions(-)
 create mode 100644 src/apps/cam/sdl_texture_1plane.cpp
 create mode 100644 src/apps/cam/sdl_texture_1plane.h

Patch
diff mbox series

diff --git a/src/apps/cam/meson.build b/src/apps/cam/meson.build
index c70ca3cd2..2833c86e9 100644
--- a/src/apps/cam/meson.build
+++ b/src/apps/cam/meson.build
@@ -34,6 +34,7 @@  if libsdl2.found()
     cam_sources += files([
         'sdl_sink.cpp',
         'sdl_texture.cpp',
+        'sdl_texture_1plane.cpp',
         'sdl_texture_yuv.cpp',
     ])
 
diff --git a/src/apps/cam/sdl_sink.cpp b/src/apps/cam/sdl_sink.cpp
index 8355dd5ed..b295675dc 100644
--- a/src/apps/cam/sdl_sink.cpp
+++ b/src/apps/cam/sdl_sink.cpp
@@ -22,6 +22,7 @@ 
 #include "../common/event_loop.h"
 #include "../common/image.h"
 
+#include "sdl_texture_1plane.h"
 #ifdef HAVE_LIBJPEG
 #include "sdl_texture_mjpg.h"
 #endif
@@ -74,7 +75,7 @@  int SDLSink::configure(const libcamera::CameraConfiguration &config)
 		break;
 #endif
 	case libcamera::formats::YUYV:
-		texture_ = std::make_unique<SDLTextureYUYV>(rect_, cfg.stride);
+		texture_ = std::make_unique<SDLTexture1Plane>(rect_, SDL_PIXELFORMAT_YUY2, cfg.stride);
 		break;
 	default:
 		std::cerr << "Unsupported pixel format "
diff --git a/src/apps/cam/sdl_texture_1plane.cpp b/src/apps/cam/sdl_texture_1plane.cpp
new file mode 100644
index 000000000..b97015bc1
--- /dev/null
+++ b/src/apps/cam/sdl_texture_1plane.cpp
@@ -0,0 +1,17 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2025, Ideas on Board Oy
+ *
+ * SDL single plane textures
+ */
+
+#include "sdl_texture_1plane.h"
+
+#include <assert.h>
+
+void SDLTexture1Plane::update(libcamera::Span<const libcamera::Span<const uint8_t>> data)
+{
+	assert(data.size() == 1);
+	assert(data[0].size_bytes() == std::size_t(rect_.h) * std::size_t(stride_));
+	SDL_UpdateTexture(ptr_, nullptr, data[0].data(), stride_);
+}
diff --git a/src/apps/cam/sdl_texture_1plane.h b/src/apps/cam/sdl_texture_1plane.h
new file mode 100644
index 000000000..795e1fa4f
--- /dev/null
+++ b/src/apps/cam/sdl_texture_1plane.h
@@ -0,0 +1,18 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2025, Ideas on Board Oy
+ *
+ * SDL single plane textures
+ */
+
+#pragma once
+
+#include "sdl_texture.h"
+
+class SDLTexture1Plane final : public SDLTexture
+{
+public:
+	using SDLTexture::SDLTexture;
+
+	void update(libcamera::Span<const libcamera::Span<const uint8_t>> data) override;
+};
diff --git a/src/apps/cam/sdl_texture_yuv.cpp b/src/apps/cam/sdl_texture_yuv.cpp
index 7e2ce3f49..bed297d28 100644
--- a/src/apps/cam/sdl_texture_yuv.cpp
+++ b/src/apps/cam/sdl_texture_yuv.cpp
@@ -21,13 +21,3 @@  void SDLTextureNV12::update(libcamera::Span<const libcamera::Span<const uint8_t>
 			    data[1].data(), stride_);
 }
 #endif
-
-SDLTextureYUYV::SDLTextureYUYV(const SDL_Rect &rect, unsigned int stride)
-	: SDLTexture(rect, SDL_PIXELFORMAT_YUY2, stride)
-{
-}
-
-void SDLTextureYUYV::update(libcamera::Span<const libcamera::Span<const uint8_t>> data)
-{
-	SDL_UpdateTexture(ptr_, nullptr, data[0].data(), stride_);
-}
diff --git a/src/apps/cam/sdl_texture_yuv.h b/src/apps/cam/sdl_texture_yuv.h
index db877f503..c271f901b 100644
--- a/src/apps/cam/sdl_texture_yuv.h
+++ b/src/apps/cam/sdl_texture_yuv.h
@@ -17,10 +17,3 @@  public:
 	void update(libcamera::Span<const libcamera::Span<const uint8_t>> data) override;
 };
 #endif
-
-class SDLTextureYUYV : public SDLTexture
-{
-public:
-	SDLTextureYUYV(const SDL_Rect &rect, unsigned int stride);
-	void update(libcamera::Span<const libcamera::Span<const uint8_t>> data) override;
-};