[QP] Add support for OLED, variable framebuffer bpp (#19997)
Co-authored-by: Pablo Martínez <58857054+elpekenin@users.noreply.github.com> Co-authored-by: Dasky <32983009+daskygit@users.noreply.github.com> Fixup delta frame coordinates after #20296.
This commit is contained in:
parent
48d9140cfc
commit
8e614250b4
51 changed files with 1610 additions and 497 deletions
|
@ -1,284 +0,0 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#include "color.h"
|
||||
#include "qp_rgb565_surface.h"
|
||||
#include "qp_draw.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Common
|
||||
|
||||
// Device definition
|
||||
typedef struct rgb565_surface_painter_device_t {
|
||||
painter_driver_t base; // must be first, so it can be cast to/from the painter_device_t* type
|
||||
|
||||
// The target buffer
|
||||
uint16_t *buffer;
|
||||
|
||||
// Manually manage the viewport for streaming pixel data to the display
|
||||
uint16_t viewport_l;
|
||||
uint16_t viewport_t;
|
||||
uint16_t viewport_r;
|
||||
uint16_t viewport_b;
|
||||
|
||||
// Current write location to the display when streaming pixel data
|
||||
uint16_t pixdata_x;
|
||||
uint16_t pixdata_y;
|
||||
|
||||
// Maintain a dirty region so we can stream only what we need
|
||||
bool is_dirty;
|
||||
uint16_t dirty_l;
|
||||
uint16_t dirty_t;
|
||||
uint16_t dirty_r;
|
||||
uint16_t dirty_b;
|
||||
|
||||
} rgb565_surface_painter_device_t;
|
||||
|
||||
// Driver storage
|
||||
rgb565_surface_painter_device_t surface_drivers[RGB565_SURFACE_NUM_DEVICES] = {0};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helpers
|
||||
|
||||
static inline void increment_pixdata_location(rgb565_surface_painter_device_t *surface) {
|
||||
// Increment the X-position
|
||||
surface->pixdata_x++;
|
||||
|
||||
// If the x-coord has gone past the right-side edge, loop it back around and increment the y-coord
|
||||
if (surface->pixdata_x > surface->viewport_r) {
|
||||
surface->pixdata_x = surface->viewport_l;
|
||||
surface->pixdata_y++;
|
||||
}
|
||||
|
||||
// If the y-coord has gone past the bottom, loop it back to the top
|
||||
if (surface->pixdata_y > surface->viewport_b) {
|
||||
surface->pixdata_y = surface->viewport_t;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void setpixel(rgb565_surface_painter_device_t *surface, uint16_t x, uint16_t y, uint16_t rgb565) {
|
||||
// Skip messing with the dirty info if the original value already matches
|
||||
if (surface->buffer[y * surface->base.panel_width + x] != rgb565) {
|
||||
// Maintain dirty region
|
||||
if (surface->dirty_l > x) {
|
||||
surface->dirty_l = x;
|
||||
}
|
||||
if (surface->dirty_r < x) {
|
||||
surface->dirty_r = x;
|
||||
}
|
||||
if (surface->dirty_t > y) {
|
||||
surface->dirty_t = y;
|
||||
}
|
||||
if (surface->dirty_b < y) {
|
||||
surface->dirty_b = y;
|
||||
}
|
||||
|
||||
// Always dirty after a setpixel
|
||||
surface->is_dirty = true;
|
||||
|
||||
// Update the pixel data in the buffer
|
||||
surface->buffer[y * surface->base.panel_width + x] = rgb565;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void append_pixel(rgb565_surface_painter_device_t *surface, uint16_t rgb565) {
|
||||
setpixel(surface, surface->pixdata_x, surface->pixdata_y, rgb565);
|
||||
increment_pixdata_location(surface);
|
||||
}
|
||||
|
||||
static inline void stream_pixdata(rgb565_surface_painter_device_t *surface, const uint16_t *data, uint32_t native_pixel_count) {
|
||||
for (uint32_t pixel_counter = 0; pixel_counter < native_pixel_count; ++pixel_counter) {
|
||||
append_pixel(surface, data[pixel_counter]);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Driver vtable
|
||||
|
||||
static bool qp_rgb565_surface_init(painter_device_t device, painter_rotation_t rotation) {
|
||||
painter_driver_t * driver = (painter_driver_t *)device;
|
||||
rgb565_surface_painter_device_t *surface = (rgb565_surface_painter_device_t *)driver;
|
||||
memset(surface->buffer, 0, driver->panel_width * driver->panel_height * driver->native_bits_per_pixel / 8);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool qp_rgb565_surface_power(painter_device_t device, bool power_on) {
|
||||
// No-op.
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool qp_rgb565_surface_clear(painter_device_t device) {
|
||||
painter_driver_t *driver = (painter_driver_t *)device;
|
||||
driver->driver_vtable->init(device, driver->rotation); // Re-init the surface
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool qp_rgb565_surface_flush(painter_device_t device) {
|
||||
painter_driver_t * driver = (painter_driver_t *)device;
|
||||
rgb565_surface_painter_device_t *surface = (rgb565_surface_painter_device_t *)driver;
|
||||
surface->dirty_l = surface->dirty_t = UINT16_MAX;
|
||||
surface->dirty_r = surface->dirty_b = 0;
|
||||
surface->is_dirty = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool qp_rgb565_surface_viewport(painter_device_t device, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom) {
|
||||
painter_driver_t * driver = (painter_driver_t *)device;
|
||||
rgb565_surface_painter_device_t *surface = (rgb565_surface_painter_device_t *)driver;
|
||||
|
||||
// Set the viewport locations
|
||||
surface->viewport_l = left;
|
||||
surface->viewport_t = top;
|
||||
surface->viewport_r = right;
|
||||
surface->viewport_b = bottom;
|
||||
|
||||
// Reset the write location to the top left
|
||||
surface->pixdata_x = left;
|
||||
surface->pixdata_y = top;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Stream pixel data to the current write position in GRAM
|
||||
static bool qp_rgb565_surface_pixdata(painter_device_t device, const void *pixel_data, uint32_t native_pixel_count) {
|
||||
painter_driver_t * driver = (painter_driver_t *)device;
|
||||
rgb565_surface_painter_device_t *surface = (rgb565_surface_painter_device_t *)driver;
|
||||
stream_pixdata(surface, (const uint16_t *)pixel_data, native_pixel_count);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Pixel colour conversion
|
||||
static bool qp_rgb565_surface_palette_convert_rgb565_swapped(painter_device_t device, int16_t palette_size, qp_pixel_t *palette) {
|
||||
for (int16_t i = 0; i < palette_size; ++i) {
|
||||
RGB rgb = hsv_to_rgb_nocie((HSV){palette[i].hsv888.h, palette[i].hsv888.s, palette[i].hsv888.v});
|
||||
uint16_t rgb565 = (((uint16_t)rgb.r) >> 3) << 11 | (((uint16_t)rgb.g) >> 2) << 5 | (((uint16_t)rgb.b) >> 3);
|
||||
palette[i].rgb565 = __builtin_bswap16(rgb565);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Append pixels to the target location, keyed by the pixel index
|
||||
static bool qp_rgb565_surface_append_pixels_rgb565(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices) {
|
||||
uint16_t *buf = (uint16_t *)target_buffer;
|
||||
for (uint32_t i = 0; i < pixel_count; ++i) {
|
||||
buf[pixel_offset + i] = palette[palette_indices[i]].rgb565;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Append data to the target location
|
||||
static bool qp_rgb565_surface_append_pixdata(painter_device_t device, uint8_t *target_buffer, uint32_t pixdata_offset, uint8_t pixdata_byte) {
|
||||
target_buffer[pixdata_offset] = pixdata_byte;
|
||||
return true;
|
||||
}
|
||||
|
||||
const painter_driver_vtable_t rgb565_surface_driver_vtable = {
|
||||
.init = qp_rgb565_surface_init,
|
||||
.power = qp_rgb565_surface_power,
|
||||
.clear = qp_rgb565_surface_clear,
|
||||
.flush = qp_rgb565_surface_flush,
|
||||
.pixdata = qp_rgb565_surface_pixdata,
|
||||
.viewport = qp_rgb565_surface_viewport,
|
||||
.palette_convert = qp_rgb565_surface_palette_convert_rgb565_swapped,
|
||||
.append_pixels = qp_rgb565_surface_append_pixels_rgb565,
|
||||
.append_pixdata = qp_rgb565_surface_append_pixdata,
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Comms vtable
|
||||
|
||||
static bool qp_rgb565_surface_comms_init(painter_device_t device) {
|
||||
// No-op.
|
||||
return true;
|
||||
}
|
||||
static bool qp_rgb565_surface_comms_start(painter_device_t device) {
|
||||
// No-op.
|
||||
return true;
|
||||
}
|
||||
static void qp_rgb565_surface_comms_stop(painter_device_t device) {
|
||||
// No-op.
|
||||
}
|
||||
uint32_t qp_rgb565_surface_comms_send(painter_device_t device, const void *data, uint32_t byte_count) {
|
||||
// No-op.
|
||||
return byte_count;
|
||||
}
|
||||
|
||||
painter_comms_vtable_t rgb565_surface_driver_comms_vtable = {
|
||||
// These are all effective no-op's because they're not actually needed.
|
||||
.comms_init = qp_rgb565_surface_comms_init,
|
||||
.comms_start = qp_rgb565_surface_comms_start,
|
||||
.comms_stop = qp_rgb565_surface_comms_stop,
|
||||
.comms_send = qp_rgb565_surface_comms_send};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Factory function for creating a handle to an rgb565 surface
|
||||
|
||||
painter_device_t qp_rgb565_make_surface(uint16_t panel_width, uint16_t panel_height, void *buffer) {
|
||||
for (uint32_t i = 0; i < RGB565_SURFACE_NUM_DEVICES; ++i) {
|
||||
rgb565_surface_painter_device_t *driver = &surface_drivers[i];
|
||||
if (!driver->base.driver_vtable) {
|
||||
driver->base.driver_vtable = &rgb565_surface_driver_vtable;
|
||||
driver->base.comms_vtable = &rgb565_surface_driver_comms_vtable;
|
||||
driver->base.native_bits_per_pixel = 16; // RGB565
|
||||
driver->base.panel_width = panel_width;
|
||||
driver->base.panel_height = panel_height;
|
||||
driver->base.rotation = QP_ROTATION_0;
|
||||
driver->base.offset_x = 0;
|
||||
driver->base.offset_y = 0;
|
||||
driver->buffer = (uint16_t *)buffer;
|
||||
return (painter_device_t)driver;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Drawing routine to copy out the dirty region and send it to another device
|
||||
|
||||
bool qp_rgb565_surface_draw(painter_device_t surface, painter_device_t display, uint16_t x, uint16_t y) {
|
||||
painter_driver_t * surface_driver = (painter_driver_t *)surface;
|
||||
rgb565_surface_painter_device_t *surface_handle = (rgb565_surface_painter_device_t *)surface_driver;
|
||||
|
||||
// If we're not dirty... we're done.
|
||||
if (!surface_handle->is_dirty) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Set the target drawing area
|
||||
bool ok = qp_viewport(display, x + surface_handle->dirty_l, y + surface_handle->dirty_t, x + surface_handle->dirty_r, y + surface_handle->dirty_b);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Housekeeping of the amount of pixels to transfer
|
||||
uint32_t total_pixel_count = QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE / sizeof(uint16_t);
|
||||
uint32_t pixel_counter = 0;
|
||||
uint16_t *target_buffer = (uint16_t *)qp_internal_global_pixdata_buffer;
|
||||
|
||||
// Fill the global pixdata area so that we can start transferring to the panel
|
||||
for (uint16_t y = surface_handle->dirty_t; y <= surface_handle->dirty_b; ++y) {
|
||||
for (uint16_t x = surface_handle->dirty_l; x <= surface_handle->dirty_r; ++x) {
|
||||
// Update the target buffer
|
||||
target_buffer[pixel_counter++] = surface_handle->buffer[y * surface_handle->base.panel_width + x];
|
||||
|
||||
// If we've accumulated enough data, send it
|
||||
if (pixel_counter == total_pixel_count) {
|
||||
ok = qp_pixdata(display, qp_internal_global_pixdata_buffer, pixel_counter);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
// Reset the counter
|
||||
pixel_counter = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If there's any leftover data, send it
|
||||
if (pixel_counter > 0) {
|
||||
ok = qp_pixdata(display, qp_internal_global_pixdata_buffer, pixel_counter);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Clear the dirty info for the surface
|
||||
return qp_flush(surface);
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#include "qp_internal.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Quantum Painter RGB565 surface configurables (add to your keyboard's config.h)
|
||||
|
||||
#ifndef RGB565_SURFACE_NUM_DEVICES
|
||||
/**
|
||||
* @def This controls the maximum number of surface devices that Quantum Painter can use at any one time.
|
||||
* Increasing this number allows for multiple framebuffers to be used. Each requires its own RAM allocation.
|
||||
*/
|
||||
# define RGB565_SURFACE_NUM_DEVICES 1
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Forward declarations
|
||||
|
||||
#ifdef QUANTUM_PAINTER_RGB565_SURFACE_ENABLE
|
||||
/**
|
||||
* Factory method for an RGB565 surface (aka framebuffer).
|
||||
*
|
||||
* @param panel_width[in] the width of the display panel
|
||||
* @param panel_height[in] the height of the display panel
|
||||
* @param buffer[in] pointer to a preallocated buffer of size `(sizeof(uint16_t) * panel_width * panel_height)`
|
||||
* @return the device handle used with all drawing routines in Quantum Painter
|
||||
*/
|
||||
painter_device_t qp_rgb565_make_surface(uint16_t panel_width, uint16_t panel_height, void *buffer);
|
||||
|
||||
/**
|
||||
* Helper method to draw the dirty contents of the framebuffer to the target device.
|
||||
*
|
||||
* After successful completion, the dirty area is reset.
|
||||
*
|
||||
* @param surface[in] the surface to copy from
|
||||
* @param display[in] the display to copy into
|
||||
* @param x[in] the x-location of the original position of the framebuffer
|
||||
* @param y[in] the y-location of the original position of the framebuffer
|
||||
* @return whether the draw operation completed successfully
|
||||
*/
|
||||
bool qp_rgb565_surface_draw(painter_device_t surface, painter_device_t display, uint16_t x, uint16_t y);
|
||||
#endif // QUANTUM_PAINTER_RGB565_SURFACE_ENABLE
|
67
drivers/painter/generic/qp_surface.h
Normal file
67
drivers/painter/generic/qp_surface.h
Normal file
|
@ -0,0 +1,67 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
|
||||
#include "qp_internal.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Quantum Painter surface helpers
|
||||
|
||||
// Helper for determining buffer size required for a surface
|
||||
#define SURFACE_REQUIRED_BUFFER_BYTE_SIZE(w, h, bpp) ((((w) * (h) * (bpp)) + 7) / 8)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Quantum Painter surface configurables (add to your keyboard's config.h)
|
||||
|
||||
#ifndef SURFACE_NUM_DEVICES
|
||||
/**
|
||||
* @def This controls the maximum number of surface devices that Quantum Painter can use at any one time.
|
||||
* Increasing this number allows for multiple framebuffers to be used. Each requires its own RAM allocation.
|
||||
*/
|
||||
# define SURFACE_NUM_DEVICES 1
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Forward declarations
|
||||
|
||||
#ifdef QUANTUM_PAINTER_SURFACE_ENABLE
|
||||
|
||||
// Surface struct
|
||||
struct surface_painter_device_t;
|
||||
typedef struct surface_painter_device_t surface_painter_device_t;
|
||||
|
||||
/**
|
||||
* Factory method for an RGB565 surface (aka framebuffer).
|
||||
*
|
||||
* @param panel_width[in] the width of the display panel
|
||||
* @param panel_height[in] the height of the display panel
|
||||
* @param buffer[in] pointer to a preallocated uint8_t buffer of size `SURFACE_REQUIRED_BUFFER_BYTE_SIZE(panel_width, panel_height, 16)`
|
||||
* @return the device handle used with all drawing routines in Quantum Painter
|
||||
*/
|
||||
painter_device_t qp_make_rgb565_surface(uint16_t panel_width, uint16_t panel_height, void *buffer);
|
||||
|
||||
/**
|
||||
* Factory method for a 1bpp monochrome surface (aka framebuffer).
|
||||
*
|
||||
* @param panel_width[in] the width of the display panel
|
||||
* @param panel_height[in] the height of the display panel
|
||||
* @param buffer[in] pointer to a preallocated uint8_t buffer of size `SURFACE_REQUIRED_BUFFER_BYTE_SIZE(panel_width, panel_height, 1)`
|
||||
* @return the device handle used with all drawing routines in Quantum Painter
|
||||
*/
|
||||
painter_device_t qp_make_mono1bpp_surface(uint16_t panel_width, uint16_t panel_height, void *buffer);
|
||||
|
||||
/**
|
||||
* Helper method to draw the contents of the framebuffer to the target device.
|
||||
*
|
||||
* After successful completion, the dirty area is reset.
|
||||
*
|
||||
* @param surface[in] the surface to copy from
|
||||
* @param target[in] the target device to copy into
|
||||
* @param x[in] the x-location of the original position of the framebuffer
|
||||
* @param y[in] the y-location of the original position of the framebuffer
|
||||
* @param entire_surface[in] whether the entire surface should be drawn, instead of just the dirty region
|
||||
* @return whether the draw operation completed successfully
|
||||
*/
|
||||
bool qp_surface_draw(painter_device_t surface, painter_device_t target, uint16_t x, uint16_t y, bool entire_surface);
|
||||
|
||||
#endif // QUANTUM_PAINTER_SURFACE_ENABLE
|
141
drivers/painter/generic/qp_surface_common.c
Normal file
141
drivers/painter/generic/qp_surface_common.c
Normal file
|
@ -0,0 +1,141 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "color.h"
|
||||
#include "qp_draw.h"
|
||||
#include "qp_surface_internal.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Driver storage
|
||||
|
||||
surface_painter_device_t surface_drivers[SURFACE_NUM_DEVICES] = {0};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helpers
|
||||
|
||||
void qp_surface_increment_pixdata_location(surface_viewport_data_t *viewport) {
|
||||
// Increment the X-position
|
||||
viewport->pixdata_x++;
|
||||
|
||||
// If the x-coord has gone past the right-side edge, loop it back around and increment the y-coord
|
||||
if (viewport->pixdata_x > viewport->viewport_r) {
|
||||
viewport->pixdata_x = viewport->viewport_l;
|
||||
viewport->pixdata_y++;
|
||||
}
|
||||
|
||||
// If the y-coord has gone past the bottom, loop it back to the top
|
||||
if (viewport->pixdata_y > viewport->viewport_b) {
|
||||
viewport->pixdata_y = viewport->viewport_t;
|
||||
}
|
||||
}
|
||||
|
||||
void qp_surface_update_dirty(surface_dirty_data_t *dirty, uint16_t x, uint16_t y) {
|
||||
// Maintain dirty region
|
||||
if (dirty->l > x) {
|
||||
dirty->l = x;
|
||||
dirty->is_dirty = true;
|
||||
}
|
||||
if (dirty->r < x) {
|
||||
dirty->r = x;
|
||||
dirty->is_dirty = true;
|
||||
}
|
||||
if (dirty->t > y) {
|
||||
dirty->t = y;
|
||||
dirty->is_dirty = true;
|
||||
}
|
||||
if (dirty->b < y) {
|
||||
dirty->b = y;
|
||||
dirty->is_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Driver vtable
|
||||
|
||||
bool qp_surface_init(painter_device_t device, painter_rotation_t rotation) {
|
||||
painter_driver_t * driver = (painter_driver_t *)device;
|
||||
surface_painter_device_t *surface = (surface_painter_device_t *)driver;
|
||||
memset(surface->buffer, 0, SURFACE_REQUIRED_BUFFER_BYTE_SIZE(driver->panel_width, driver->panel_height, driver->native_bits_per_pixel));
|
||||
|
||||
surface->dirty.l = 0;
|
||||
surface->dirty.t = 0;
|
||||
surface->dirty.r = surface->base.panel_width - 1;
|
||||
surface->dirty.b = surface->base.panel_height - 1;
|
||||
surface->dirty.is_dirty = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool qp_surface_power(painter_device_t device, bool power_on) {
|
||||
// No-op.
|
||||
return true;
|
||||
}
|
||||
|
||||
bool qp_surface_clear(painter_device_t device) {
|
||||
painter_driver_t *driver = (painter_driver_t *)device;
|
||||
driver->driver_vtable->init(device, driver->rotation); // Re-init the surface
|
||||
return true;
|
||||
}
|
||||
|
||||
bool qp_surface_flush(painter_device_t device) {
|
||||
painter_driver_t * driver = (painter_driver_t *)device;
|
||||
surface_painter_device_t *surface = (surface_painter_device_t *)driver;
|
||||
surface->dirty.l = surface->dirty.t = UINT16_MAX;
|
||||
surface->dirty.r = surface->dirty.b = 0;
|
||||
surface->dirty.is_dirty = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool qp_surface_viewport(painter_device_t device, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom) {
|
||||
painter_driver_t * driver = (painter_driver_t *)device;
|
||||
surface_painter_device_t *surface = (surface_painter_device_t *)driver;
|
||||
|
||||
// Set the viewport locations
|
||||
surface->viewport.viewport_l = left;
|
||||
surface->viewport.viewport_t = top;
|
||||
surface->viewport.viewport_r = right;
|
||||
surface->viewport.viewport_b = bottom;
|
||||
|
||||
// Reset the write location to the top left
|
||||
surface->viewport.pixdata_x = left;
|
||||
surface->viewport.pixdata_y = top;
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Drawing routine to copy out the dirty region and send it to another device
|
||||
|
||||
bool qp_surface_draw(painter_device_t surface, painter_device_t target, uint16_t x, uint16_t y, bool entire_surface) {
|
||||
painter_driver_t * surface_driver = (painter_driver_t *)surface;
|
||||
surface_painter_device_t *surface_handle = (surface_painter_device_t *)surface_driver;
|
||||
painter_driver_t * target_driver = (painter_driver_t *)target;
|
||||
|
||||
// If we're not dirty... we're done.
|
||||
if (!surface_handle->dirty.is_dirty) {
|
||||
qp_dprintf("qp_surface_draw: ok (not dirty, skipping)\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
// If we have incompatible bit depths, drop out
|
||||
if (surface_driver->native_bits_per_pixel != target_driver->native_bits_per_pixel) {
|
||||
qp_dprintf("qp_surface_draw: fail (incompatible bpp: surface=%d, target=%d)\n", (int)surface_driver->native_bits_per_pixel, (int)target_driver->native_bits_per_pixel);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Offload to the pixdata transfer function
|
||||
surface_painter_driver_vtable_t *vtable = (surface_painter_driver_vtable_t *)surface_driver->driver_vtable;
|
||||
bool ok = vtable->target_pixdata_transfer(surface_driver, target_driver, x, y, entire_surface);
|
||||
if (!ok) {
|
||||
qp_dprintf("qp_surface_draw: fail (could not transfer pixel data)\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clear the dirty info for the surface
|
||||
ok = qp_flush(surface);
|
||||
if (!ok) {
|
||||
qp_dprintf("qp_surface_draw: fail (could not flush)\n");
|
||||
return false;
|
||||
}
|
||||
qp_dprintf("qp_surface_draw: ok\n");
|
||||
return true;
|
||||
}
|
119
drivers/painter/generic/qp_surface_internal.h
Normal file
119
drivers/painter/generic/qp_surface_internal.h
Normal file
|
@ -0,0 +1,119 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
|
||||
#ifdef QUANTUM_PAINTER_SURFACE_ENABLE
|
||||
|
||||
# include "qp_surface.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Internal declarations
|
||||
|
||||
// Surface vtable
|
||||
typedef struct surface_painter_driver_vtable_t {
|
||||
painter_driver_vtable_t base; // must be first, so it can be cast to/from the painter_driver_vtable_t* type
|
||||
|
||||
bool (*target_pixdata_transfer)(painter_driver_t *surface_driver, painter_driver_t *target_driver, uint16_t x, uint16_t y, bool entire_surface);
|
||||
} surface_painter_driver_vtable_t;
|
||||
|
||||
typedef struct surface_dirty_data_t {
|
||||
bool is_dirty;
|
||||
uint16_t l;
|
||||
uint16_t t;
|
||||
uint16_t r;
|
||||
uint16_t b;
|
||||
} surface_dirty_data_t;
|
||||
|
||||
typedef struct surface_viewport_data_t {
|
||||
// Manually manage the viewport for streaming pixel data to the display
|
||||
uint16_t viewport_l;
|
||||
uint16_t viewport_t;
|
||||
uint16_t viewport_r;
|
||||
uint16_t viewport_b;
|
||||
|
||||
// Current write location to the display when streaming pixel data
|
||||
uint16_t pixdata_x;
|
||||
uint16_t pixdata_y;
|
||||
} surface_viewport_data_t;
|
||||
|
||||
// Surface struct
|
||||
typedef struct surface_painter_device_t {
|
||||
painter_driver_t base; // must be first, so it can be cast to/from the painter_device_t* type
|
||||
|
||||
// The target buffer
|
||||
union {
|
||||
void * buffer;
|
||||
uint8_t * u8buffer;
|
||||
uint16_t *u16buffer;
|
||||
};
|
||||
|
||||
// Manually manage the viewport for streaming pixel data to the display
|
||||
surface_viewport_data_t viewport;
|
||||
|
||||
// Maintain a dirty region so we can stream only what we need
|
||||
surface_dirty_data_t dirty;
|
||||
} surface_painter_device_t;
|
||||
|
||||
/**
|
||||
* Factory method for an RGB565 surface (aka framebuffer). Accepts an external device table.
|
||||
*
|
||||
* @param device_table[in] the table of devices to use for instantiation
|
||||
* @param device_table_len[in] the length of the table of devices
|
||||
* @param panel_width[in] the width of the display panel
|
||||
* @param panel_height[in] the height of the display panel
|
||||
* @param buffer[in] pointer to a preallocated uint8_t buffer of size `SURFACE_REQUIRED_BUFFER_BYTE_SIZE(panel_width, panel_height, 16)`
|
||||
* @return the device handle used with all drawing routines in Quantum Painter
|
||||
*/
|
||||
painter_device_t qp_make_rgb565_surface_advanced(surface_painter_device_t *device_table, size_t device_table_len, uint16_t panel_width, uint16_t panel_height, void *buffer);
|
||||
|
||||
/**
|
||||
* Factory method for a 1bpp monochrome surface (aka framebuffer).
|
||||
*
|
||||
* @param device_table[in] the table of devices to use for instantiation
|
||||
* @param device_table_len[in] the length of the table of devices
|
||||
* @param panel_width[in] the width of the display panel
|
||||
* @param panel_height[in] the height of the display panel
|
||||
* @param buffer[in] pointer to a preallocated uint8_t buffer of size `SURFACE_REQUIRED_BUFFER_BYTE_SIZE(panel_width, panel_height, 16)`
|
||||
* @return the device handle used with all drawing routines in Quantum Painter
|
||||
*/
|
||||
painter_device_t qp_make_mono1bpp_surface_advanced(surface_painter_device_t *device_table, size_t device_table_len, uint16_t panel_width, uint16_t panel_height, void *buffer);
|
||||
|
||||
// Driver storage
|
||||
extern surface_painter_device_t surface_drivers[SURFACE_NUM_DEVICES];
|
||||
|
||||
// Surface common APIs
|
||||
bool qp_surface_init(painter_device_t device, painter_rotation_t rotation);
|
||||
bool qp_surface_power(painter_device_t device, bool power_on);
|
||||
bool qp_surface_clear(painter_device_t device);
|
||||
bool qp_surface_flush(painter_device_t device);
|
||||
bool qp_surface_viewport(painter_device_t device, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom);
|
||||
void qp_surface_increment_pixdata_location(surface_viewport_data_t *viewport);
|
||||
void qp_surface_update_dirty(surface_dirty_data_t *dirty, uint16_t x, uint16_t y);
|
||||
|
||||
#endif // QUANTUM_PAINTER_SURFACE_ENABLE
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Factory functions for creating a handle to a surface
|
||||
|
||||
#define SURFACE_FACTORY_FUNCTION_IMPL(function_name, vtable, bpp) \
|
||||
painter_device_t(function_name##_advanced)(surface_painter_device_t * device_table, size_t device_table_len, uint16_t panel_width, uint16_t panel_height, void *buffer) { \
|
||||
for (uint32_t i = 0; i < device_table_len; ++i) { \
|
||||
surface_painter_device_t *driver = &device_table[i]; \
|
||||
if (!driver->base.driver_vtable) { \
|
||||
driver->base.driver_vtable = (painter_driver_vtable_t *)&(vtable); \
|
||||
driver->base.native_bits_per_pixel = (bpp); \
|
||||
driver->base.comms_vtable = &dummy_comms_vtable; \
|
||||
driver->base.panel_width = panel_width; \
|
||||
driver->base.panel_height = panel_height; \
|
||||
driver->base.rotation = QP_ROTATION_0; \
|
||||
driver->base.offset_x = 0; \
|
||||
driver->base.offset_y = 0; \
|
||||
driver->buffer = buffer; \
|
||||
return (painter_device_t)driver; \
|
||||
} \
|
||||
} \
|
||||
return NULL; \
|
||||
} \
|
||||
painter_device_t(function_name)(uint16_t panel_width, uint16_t panel_height, void *buffer) { \
|
||||
return (function_name##_advanced)(surface_drivers, SURFACE_NUM_DEVICES, panel_width, panel_height, buffer); \
|
||||
}
|
113
drivers/painter/generic/qp_surface_mono1bpp.c
Normal file
113
drivers/painter/generic/qp_surface_mono1bpp.c
Normal file
|
@ -0,0 +1,113 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#ifdef QUANTUM_PAINTER_SURFACE_ENABLE
|
||||
|
||||
# include "color.h"
|
||||
# include "qp_draw.h"
|
||||
# include "qp_surface_internal.h"
|
||||
# include "qp_comms_dummy.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Surface driver impl: mono1bpp
|
||||
|
||||
static inline void setpixel_mono1bpp(surface_painter_device_t *surface, uint16_t x, uint16_t y, bool mono_pixel) {
|
||||
uint16_t w = surface->base.panel_width;
|
||||
uint16_t h = surface->base.panel_height;
|
||||
|
||||
// Drop out if it's off-screen
|
||||
if (x >= w || y >= h) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Figure out which location needs to be updated
|
||||
uint32_t pixel_num = y * w + x;
|
||||
uint32_t byte_offset = pixel_num / 8;
|
||||
uint8_t bit_offset = pixel_num % 8;
|
||||
bool curr_val = (surface->u8buffer[byte_offset] & (1 << bit_offset)) ? true : false;
|
||||
|
||||
// Skip messing with the dirty info if the original value already matches
|
||||
if (curr_val != mono_pixel) {
|
||||
// Update the dirty region
|
||||
qp_surface_update_dirty(&surface->dirty, x, y);
|
||||
|
||||
// Update the pixel data in the buffer
|
||||
if (mono_pixel) {
|
||||
surface->u8buffer[byte_offset] |= (1 << bit_offset);
|
||||
} else {
|
||||
surface->u8buffer[byte_offset] &= ~(1 << bit_offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void append_pixel_mono1bpp(surface_painter_device_t *surface, bool mono_pixel) {
|
||||
setpixel_mono1bpp(surface, surface->viewport.pixdata_x, surface->viewport.pixdata_y, mono_pixel);
|
||||
qp_surface_increment_pixdata_location(&surface->viewport);
|
||||
}
|
||||
|
||||
static inline void stream_pixdata_mono1bpp(surface_painter_device_t *surface, const uint8_t *data, uint32_t native_pixel_count) {
|
||||
for (uint32_t pixel_counter = 0; pixel_counter < native_pixel_count; ++pixel_counter) {
|
||||
uint32_t byte_offset = pixel_counter / 8;
|
||||
uint8_t bit_offset = pixel_counter % 8;
|
||||
append_pixel_mono1bpp(surface, (data[byte_offset] & (1 << bit_offset)) ? true : false);
|
||||
}
|
||||
}
|
||||
|
||||
// Stream pixel data to the current write position in GRAM
|
||||
static bool qp_surface_pixdata_mono1bpp(painter_device_t device, const void *pixel_data, uint32_t native_pixel_count) {
|
||||
painter_driver_t * driver = (painter_driver_t *)device;
|
||||
surface_painter_device_t *surface = (surface_painter_device_t *)driver;
|
||||
stream_pixdata_mono1bpp(surface, (const uint8_t *)pixel_data, native_pixel_count);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Pixel colour conversion
|
||||
static bool qp_surface_palette_convert_mono1bpp(painter_device_t device, int16_t palette_size, qp_pixel_t *palette) {
|
||||
for (int16_t i = 0; i < palette_size; ++i) {
|
||||
palette[i].mono = (palette[i].hsv888.v > 127) ? 1 : 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Append pixels to the target location, keyed by the pixel index
|
||||
static bool qp_surface_append_pixels_mono1bpp(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices) {
|
||||
for (uint32_t i = 0; i < pixel_count; ++i) {
|
||||
uint32_t pixel_num = pixel_offset + i;
|
||||
uint32_t byte_offset = pixel_num / 8;
|
||||
uint8_t bit_offset = pixel_num % 8;
|
||||
if (palette[palette_indices[i]].mono) {
|
||||
target_buffer[byte_offset] |= (1 << bit_offset);
|
||||
} else {
|
||||
target_buffer[byte_offset] &= ~(1 << bit_offset);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool mono1bpp_target_pixdata_transfer(painter_driver_t *surface_driver, painter_driver_t *target_driver, uint16_t x, uint16_t y, bool entire_surface) {
|
||||
return false; // Not yet supported.
|
||||
}
|
||||
|
||||
static bool qp_surface_append_pixdata_mono1bpp(painter_device_t device, uint8_t *target_buffer, uint32_t pixdata_offset, uint8_t pixdata_byte) {
|
||||
return false; // Just use 1bpp images.
|
||||
}
|
||||
|
||||
const surface_painter_driver_vtable_t mono1bpp_surface_driver_vtable = {
|
||||
.base =
|
||||
{
|
||||
.init = qp_surface_init,
|
||||
.power = qp_surface_power,
|
||||
.clear = qp_surface_clear,
|
||||
.flush = qp_surface_flush,
|
||||
.pixdata = qp_surface_pixdata_mono1bpp,
|
||||
.viewport = qp_surface_viewport,
|
||||
.palette_convert = qp_surface_palette_convert_mono1bpp,
|
||||
.append_pixels = qp_surface_append_pixels_mono1bpp,
|
||||
.append_pixdata = qp_surface_append_pixdata_mono1bpp,
|
||||
},
|
||||
.target_pixdata_transfer = mono1bpp_target_pixdata_transfer,
|
||||
};
|
||||
|
||||
SURFACE_FACTORY_FUNCTION_IMPL(qp_make_mono1bpp_surface, mono1bpp_surface_driver_vtable, 1);
|
||||
|
||||
#endif // QUANTUM_PAINTER_SURFACE_ENABLE
|
145
drivers/painter/generic/qp_surface_rgb565.c
Normal file
145
drivers/painter/generic/qp_surface_rgb565.c
Normal file
|
@ -0,0 +1,145 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#ifdef QUANTUM_PAINTER_SURFACE_ENABLE
|
||||
|
||||
# include "color.h"
|
||||
# include "qp_draw.h"
|
||||
# include "qp_surface_internal.h"
|
||||
# include "qp_comms_dummy.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Surface driver impl: rgb565
|
||||
|
||||
static inline void setpixel_rgb565(surface_painter_device_t *surface, uint16_t x, uint16_t y, uint16_t rgb565) {
|
||||
uint16_t w = surface->base.panel_width;
|
||||
uint16_t h = surface->base.panel_height;
|
||||
|
||||
// Drop out if it's off-screen
|
||||
if (x >= w || y >= h) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Skip messing with the dirty info if the original value already matches
|
||||
if (surface->u16buffer[y * w + x] != rgb565) {
|
||||
// Update the dirty region
|
||||
qp_surface_update_dirty(&surface->dirty, x, y);
|
||||
|
||||
// Update the pixel data in the buffer
|
||||
surface->u16buffer[y * w + x] = rgb565;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void append_pixel_rgb565(surface_painter_device_t *surface, uint16_t rgb565) {
|
||||
setpixel_rgb565(surface, surface->viewport.pixdata_x, surface->viewport.pixdata_y, rgb565);
|
||||
qp_surface_increment_pixdata_location(&surface->viewport);
|
||||
}
|
||||
|
||||
static inline void stream_pixdata_rgb565(surface_painter_device_t *surface, const uint16_t *data, uint32_t native_pixel_count) {
|
||||
for (uint32_t pixel_counter = 0; pixel_counter < native_pixel_count; ++pixel_counter) {
|
||||
append_pixel_rgb565(surface, data[pixel_counter]);
|
||||
}
|
||||
}
|
||||
|
||||
// Stream pixel data to the current write position in GRAM
|
||||
static bool qp_surface_pixdata_rgb565(painter_device_t device, const void *pixel_data, uint32_t native_pixel_count) {
|
||||
painter_driver_t * driver = (painter_driver_t *)device;
|
||||
surface_painter_device_t *surface = (surface_painter_device_t *)driver;
|
||||
stream_pixdata_rgb565(surface, (const uint16_t *)pixel_data, native_pixel_count);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Pixel colour conversion
|
||||
static bool qp_surface_palette_convert_rgb565_swapped(painter_device_t device, int16_t palette_size, qp_pixel_t *palette) {
|
||||
for (int16_t i = 0; i < palette_size; ++i) {
|
||||
RGB rgb = hsv_to_rgb_nocie((HSV){palette[i].hsv888.h, palette[i].hsv888.s, palette[i].hsv888.v});
|
||||
uint16_t rgb565 = (((uint16_t)rgb.r) >> 3) << 11 | (((uint16_t)rgb.g) >> 2) << 5 | (((uint16_t)rgb.b) >> 3);
|
||||
palette[i].rgb565 = __builtin_bswap16(rgb565);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Append pixels to the target location, keyed by the pixel index
|
||||
static bool qp_surface_append_pixels_rgb565(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices) {
|
||||
uint16_t *buf = (uint16_t *)target_buffer;
|
||||
for (uint32_t i = 0; i < pixel_count; ++i) {
|
||||
buf[pixel_offset + i] = palette[palette_indices[i]].rgb565;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool rgb565_target_pixdata_transfer(painter_driver_t *surface_driver, painter_driver_t *target_driver, uint16_t x, uint16_t y, bool entire_surface) {
|
||||
surface_painter_device_t *surface_handle = (surface_painter_device_t *)surface_driver;
|
||||
|
||||
uint16_t l = entire_surface ? 0 : surface_handle->dirty.l;
|
||||
uint16_t t = entire_surface ? 0 : surface_handle->dirty.t;
|
||||
uint16_t r = entire_surface ? (surface_handle->base.panel_width - 1) : surface_handle->dirty.r;
|
||||
uint16_t b = entire_surface ? (surface_handle->base.panel_height - 1) : surface_handle->dirty.b;
|
||||
|
||||
// Set the target drawing area
|
||||
bool ok = qp_viewport((painter_device_t)target_driver, x + l, y + t, x + r, y + b);
|
||||
if (!ok) {
|
||||
qp_dprintf("rgb565_target_pixdata_transfer: fail (could not set target viewport)\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Housekeeping of the amount of pixels to transfer
|
||||
uint32_t total_pixel_count = (8 * QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE) / surface_driver->native_bits_per_pixel;
|
||||
uint32_t pixel_counter = 0;
|
||||
uint16_t *target_buffer = (uint16_t *)qp_internal_global_pixdata_buffer;
|
||||
|
||||
// Fill the global pixdata area so that we can start transferring to the panel
|
||||
for (uint16_t y = t; y <= b; ++y) {
|
||||
for (uint16_t x = l; x <= r; ++x) {
|
||||
// Update the target buffer
|
||||
target_buffer[pixel_counter++] = surface_handle->u16buffer[y * surface_handle->base.panel_width + x];
|
||||
|
||||
// If we've accumulated enough data, send it
|
||||
if (pixel_counter == total_pixel_count) {
|
||||
ok = qp_pixdata((painter_device_t)target_driver, qp_internal_global_pixdata_buffer, pixel_counter);
|
||||
if (!ok) {
|
||||
qp_dprintf("rgb565_target_pixdata_transfer: fail (could not stream pixdata to target)\n");
|
||||
return false;
|
||||
}
|
||||
// Reset the counter
|
||||
pixel_counter = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If there's any leftover data, send it
|
||||
if (pixel_counter > 0) {
|
||||
ok = qp_pixdata((painter_device_t)target_driver, qp_internal_global_pixdata_buffer, pixel_counter);
|
||||
if (!ok) {
|
||||
qp_dprintf("rgb565_target_pixdata_transfer: fail (could not stream pixdata to target)\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool qp_surface_append_pixdata_rgb565(painter_device_t device, uint8_t *target_buffer, uint32_t pixdata_offset, uint8_t pixdata_byte) {
|
||||
target_buffer[pixdata_offset] = pixdata_byte;
|
||||
return true;
|
||||
}
|
||||
|
||||
const surface_painter_driver_vtable_t rgb565_surface_driver_vtable = {
|
||||
.base =
|
||||
{
|
||||
.init = qp_surface_init,
|
||||
.power = qp_surface_power,
|
||||
.clear = qp_surface_clear,
|
||||
.flush = qp_surface_flush,
|
||||
.pixdata = qp_surface_pixdata_rgb565,
|
||||
.viewport = qp_surface_viewport,
|
||||
.palette_convert = qp_surface_palette_convert_rgb565_swapped,
|
||||
.append_pixels = qp_surface_append_pixels_rgb565,
|
||||
.append_pixdata = qp_surface_append_pixdata_rgb565,
|
||||
},
|
||||
.target_pixdata_transfer = rgb565_target_pixdata_transfer,
|
||||
};
|
||||
|
||||
SURFACE_FACTORY_FUNCTION_IMPL(qp_make_rgb565_surface, rgb565_surface_driver_vtable, 16);
|
||||
|
||||
#endif // QUANTUM_PAINTER_SURFACE_ENABLE
|
Loading…
Add table
Add a link
Reference in a new issue