esp-idf: Implement color_swap for RGB8 pixels

Use Olivier's great trick to rename color_swap_16 to color_swap via
union.
This commit is contained in:
Simon Hausmann 2025-01-08 10:32:32 +01:00 committed by Simon Hausmann
parent 8903124239
commit 5ef3fb8b26
2 changed files with 27 additions and 16 deletions

View file

@ -52,9 +52,12 @@ struct SlintPlatformConfiguration
std::optional<std::span<PixelType>> buffer2 = {};
slint::platform::SoftwareRenderer::RenderingRotation rotation =
slint::platform::SoftwareRenderer::RenderingRotation::NoRotation;
/// Swap the 2 bytes of RGB 565 pixels before sending to the display. Use this
/// if your CPU is little endian but the display expects big-endian.
bool color_swap_16 = false;
/// Swap the 2 bytes of RGB 565 pixels before sending to the display, or turn RGB into BGR. Use
/// this if your CPU is little endian but the display expects big-endian.
union {
bool color_swap_16;
bool color_swap = false;
};
};
template<typename... Args>

View file

@ -47,7 +47,7 @@ struct EspPlatform : public slint::platform::Platform
touch_handle(config.touch_handle),
buffer1(config.buffer1),
buffer2(config.buffer2),
color_swap_16(config.color_swap_16),
color_swap(config.color_swap),
rotation(config.rotation)
{
#if !defined(SLINT_FEATURE_EXPERIMENTAL)
@ -72,7 +72,7 @@ private:
esp_lcd_touch_handle_t touch_handle;
std::optional<std::span<PixelType>> buffer1;
std::optional<std::span<PixelType>> buffer2;
bool color_swap_16;
bool color_swap;
slint::platform::SoftwareRenderer::RenderingRotation rotation;
class EspWindowAdapter<PixelType> *m_window = nullptr;
@ -121,6 +121,19 @@ extern "C" bool on_vsync_event(esp_lcd_panel_handle_t panel,
}
#endif
namespace {
void swap_colors(slint::platform::Rgb565Pixel *pixel)
{
// Swap endianness to big endian
auto px = reinterpret_cast<uint16_t *>(pixel);
*px = (*px << 8) | (*px >> 8);
}
void swap_colors(slint::Rgb8Pixel *pixel)
{
std::swap(pixel->r, pixel->b);
}
}
template<typename PixelType>
void EspPlatform<PixelType>::run_event_loop()
{
@ -227,14 +240,11 @@ void EspPlatform<PixelType>::run_event_loop()
auto region = m_window->m_renderer.render(buffer1.value(),
rotated ? size.height : size.width);
if (color_swap_16) {
if (color_swap) {
for (auto [o, s] : region.rectangles()) {
for (int y = o.y; y < o.y + s.height; y++) {
for (int x = o.x; x < o.x + s.width; x++) {
// Swap endianness to big endian
auto px = reinterpret_cast<uint16_t *>(
&buffer1.value()[y * size.width + x]);
*px = (*px << 8) | (*px >> 8);
swap_colors(&buffer1.value()[y * size.width + x]);
}
}
}
@ -275,12 +285,10 @@ void EspPlatform<PixelType>::run_event_loop()
std::span<slint::platform::Rgb565Pixel> view { lb.get(),
line_end - line_start };
render_fn(view);
if (color_swap_16) {
if (color_swap) {
// Swap endianness to big endian
std::for_each(view.begin(), view.end(), [](auto &rgbpix) {
auto px = reinterpret_cast<uint16_t *>(&rgbpix);
*px = (*px << 8) | (*px >> 8);
});
std::for_each(view.begin(), view.end(),
[](auto &rgbpix) { swap_colors(&rgbpix); });
}
esp_lcd_panel_draw_bitmap(panel_handle, line_start, line_y, line_end,
line_y + 1, lb.get());
@ -358,7 +366,7 @@ void slint_esp_init(slint::PhysicalSize size, esp_lcd_panel_handle_t panel,
.buffer1 = std::nullopt,
.buffer2 = std::nullopt,
.rotation = rotation,
.color_swap_16 = false };
.color_swap = false };
slint_esp_init(config);
}
#endif