qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

qp_surface_rgb565.c (6251B)


      1 // Copyright 2022 Nick Brassel (@tzarc)
      2 // SPDX-License-Identifier: GPL-2.0-or-later
      3 
      4 #ifdef QUANTUM_PAINTER_SURFACE_ENABLE
      5 
      6 #    include "color.h"
      7 #    include "qp_draw.h"
      8 #    include "qp_surface_internal.h"
      9 #    include "qp_comms_dummy.h"
     10 
     11 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     12 // Surface driver impl: rgb565
     13 
     14 static inline void setpixel_rgb565(surface_painter_device_t *surface, uint16_t x, uint16_t y, uint16_t rgb565) {
     15     uint16_t w = surface->base.panel_width;
     16     uint16_t h = surface->base.panel_height;
     17 
     18     // Drop out if it's off-screen
     19     if (x >= w || y >= h) {
     20         return;
     21     }
     22 
     23     // Skip messing with the dirty info if the original value already matches
     24     if (surface->u16buffer[y * w + x] != rgb565) {
     25         // Update the dirty region
     26         qp_surface_update_dirty(&surface->dirty, x, y);
     27 
     28         // Update the pixel data in the buffer
     29         surface->u16buffer[y * w + x] = rgb565;
     30     }
     31 }
     32 
     33 static inline void append_pixel_rgb565(surface_painter_device_t *surface, uint16_t rgb565) {
     34     setpixel_rgb565(surface, surface->viewport.pixdata_x, surface->viewport.pixdata_y, rgb565);
     35     qp_surface_increment_pixdata_location(&surface->viewport);
     36 }
     37 
     38 static inline void stream_pixdata_rgb565(surface_painter_device_t *surface, const uint16_t *data, uint32_t native_pixel_count) {
     39     for (uint32_t pixel_counter = 0; pixel_counter < native_pixel_count; ++pixel_counter) {
     40         append_pixel_rgb565(surface, data[pixel_counter]);
     41     }
     42 }
     43 
     44 // Stream pixel data to the current write position in GRAM
     45 static bool qp_surface_pixdata_rgb565(painter_device_t device, const void *pixel_data, uint32_t native_pixel_count) {
     46     painter_driver_t         *driver  = (painter_driver_t *)device;
     47     surface_painter_device_t *surface = (surface_painter_device_t *)driver;
     48     stream_pixdata_rgb565(surface, (const uint16_t *)pixel_data, native_pixel_count);
     49     return true;
     50 }
     51 
     52 // Pixel colour conversion
     53 static bool qp_surface_palette_convert_rgb565_swapped(painter_device_t device, int16_t palette_size, qp_pixel_t *palette) {
     54     for (int16_t i = 0; i < palette_size; ++i) {
     55         rgb_t    rgb      = hsv_to_rgb_nocie(palette[i].hsv888);
     56         uint16_t rgb565   = (((uint16_t)rgb.r) >> 3) << 11 | (((uint16_t)rgb.g) >> 2) << 5 | (((uint16_t)rgb.b) >> 3);
     57         palette[i].rgb565 = __builtin_bswap16(rgb565);
     58     }
     59     return true;
     60 }
     61 
     62 // Append pixels to the target location, keyed by the pixel index
     63 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) {
     64     uint16_t *buf = (uint16_t *)target_buffer;
     65     for (uint32_t i = 0; i < pixel_count; ++i) {
     66         buf[pixel_offset + i] = palette[palette_indices[i]].rgb565;
     67     }
     68     return true;
     69 }
     70 
     71 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) {
     72     surface_painter_device_t *surface_handle = (surface_painter_device_t *)surface_driver;
     73 
     74     uint16_t l = entire_surface ? 0 : surface_handle->dirty.l;
     75     uint16_t t = entire_surface ? 0 : surface_handle->dirty.t;
     76     uint16_t r = entire_surface ? (surface_handle->base.panel_width - 1) : surface_handle->dirty.r;
     77     uint16_t b = entire_surface ? (surface_handle->base.panel_height - 1) : surface_handle->dirty.b;
     78 
     79     // Set the target drawing area
     80     bool ok = qp_viewport((painter_device_t)target_driver, x + l, y + t, x + r, y + b);
     81     if (!ok) {
     82         qp_dprintf("rgb565_target_pixdata_transfer: fail (could not set target viewport)\n");
     83         return false;
     84     }
     85 
     86     // Housekeeping of the amount of pixels to transfer
     87     uint32_t  total_pixel_count = (8 * QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE) / surface_driver->native_bits_per_pixel;
     88     uint32_t  pixel_counter     = 0;
     89     uint16_t *target_buffer     = (uint16_t *)qp_internal_global_pixdata_buffer;
     90 
     91     // Fill the global pixdata area so that we can start transferring to the panel
     92     for (uint16_t y = t; y <= b; ++y) {
     93         for (uint16_t x = l; x <= r; ++x) {
     94             // Update the target buffer
     95             target_buffer[pixel_counter++] = surface_handle->u16buffer[y * surface_handle->base.panel_width + x];
     96 
     97             // If we've accumulated enough data, send it
     98             if (pixel_counter == total_pixel_count) {
     99                 ok = qp_pixdata((painter_device_t)target_driver, qp_internal_global_pixdata_buffer, pixel_counter);
    100                 if (!ok) {
    101                     qp_dprintf("rgb565_target_pixdata_transfer: fail (could not stream pixdata to target)\n");
    102                     return false;
    103                 }
    104                 // Reset the counter
    105                 pixel_counter = 0;
    106             }
    107         }
    108     }
    109 
    110     // If there's any leftover data, send it
    111     if (pixel_counter > 0) {
    112         ok = qp_pixdata((painter_device_t)target_driver, qp_internal_global_pixdata_buffer, pixel_counter);
    113         if (!ok) {
    114             qp_dprintf("rgb565_target_pixdata_transfer: fail (could not stream pixdata to target)\n");
    115             return false;
    116         }
    117     }
    118 
    119     return true;
    120 }
    121 
    122 static bool qp_surface_append_pixdata_rgb565(painter_device_t device, uint8_t *target_buffer, uint32_t pixdata_offset, uint8_t pixdata_byte) {
    123     target_buffer[pixdata_offset] = pixdata_byte;
    124     return true;
    125 }
    126 
    127 const surface_painter_driver_vtable_t rgb565_surface_driver_vtable = {
    128     .base =
    129         {
    130             .init            = qp_surface_init,
    131             .power           = qp_surface_power,
    132             .clear           = qp_surface_clear,
    133             .flush           = qp_surface_flush,
    134             .pixdata         = qp_surface_pixdata_rgb565,
    135             .viewport        = qp_surface_viewport,
    136             .palette_convert = qp_surface_palette_convert_rgb565_swapped,
    137             .append_pixels   = qp_surface_append_pixels_rgb565,
    138             .append_pixdata  = qp_surface_append_pixdata_rgb565,
    139         },
    140     .target_pixdata_transfer = rgb565_target_pixdata_transfer,
    141 };
    142 
    143 SURFACE_FACTORY_FUNCTION_IMPL(qp_make_rgb565_surface, rgb565_surface_driver_vtable, 16);
    144 
    145 #endif // QUANTUM_PAINTER_SURFACE_ENABLE