qp_surface_rgb888.c (6065B)
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: rgb888 13 14 static inline void setpixel_rgb888(surface_painter_device_t *surface, uint16_t x, uint16_t y, rgb_t rgb888) { 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 (memcmp(&surface->rgbbuffer[y * w + x], &rgb888, sizeof(rgb_t)) != 0) { 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->rgbbuffer[y * w + x] = rgb888; 30 } 31 } 32 33 static inline void append_pixel_rgb888(surface_painter_device_t *surface, rgb_t rgb888) { 34 setpixel_rgb888(surface, surface->viewport.pixdata_x, surface->viewport.pixdata_y, rgb888); 35 qp_surface_increment_pixdata_location(&surface->viewport); 36 } 37 38 static inline void stream_pixdata_rgb888(surface_painter_device_t *surface, const rgb_t *data, uint32_t native_pixel_count) { 39 for (uint32_t pixel_counter = 0; pixel_counter < native_pixel_count; ++pixel_counter) { 40 append_pixel_rgb888(surface, data[pixel_counter]); 41 } 42 } 43 44 // Stream pixel data to the current write position in GRAM 45 static bool qp_surface_pixdata_rgb888(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_rgb888(surface, (const rgb_t *)pixel_data, native_pixel_count); 49 return true; 50 } 51 52 // Pixel colour conversion 53 static bool qp_surface_palette_convert_rgb888(painter_device_t device, int16_t palette_size, qp_pixel_t *palette) { 54 for (int16_t i = 0; i < palette_size; ++i) { 55 palette[i].rgb888 = hsv_to_rgb_nocie(palette[i].hsv888); 56 } 57 return true; 58 } 59 60 // Append pixels to the target location, keyed by the pixel index 61 static bool qp_surface_append_pixels_rgb888(painter_device_t device, uint8_t *target_buffer, qp_pixel_t *palette, uint32_t pixel_offset, uint32_t pixel_count, uint8_t *palette_indices) { 62 rgb_t *buf = (rgb_t *)target_buffer; 63 for (uint32_t i = 0; i < pixel_count; ++i) { 64 buf[pixel_offset + i] = palette[palette_indices[i]].rgb888; 65 } 66 return true; 67 } 68 69 static bool rgb888_target_pixdata_transfer(painter_driver_t *surface_driver, painter_driver_t *target_driver, uint16_t x, uint16_t y, bool entire_surface) { 70 surface_painter_device_t *surface_handle = (surface_painter_device_t *)surface_driver; 71 72 uint16_t l = entire_surface ? 0 : surface_handle->dirty.l; 73 uint16_t t = entire_surface ? 0 : surface_handle->dirty.t; 74 uint16_t r = entire_surface ? (surface_handle->base.panel_width - 1) : surface_handle->dirty.r; 75 uint16_t b = entire_surface ? (surface_handle->base.panel_height - 1) : surface_handle->dirty.b; 76 77 // Set the target drawing area 78 bool ok = qp_viewport((painter_device_t)target_driver, x + l, y + t, x + r, y + b); 79 if (!ok) { 80 qp_dprintf("rgb888_target_pixdata_transfer: fail (could not set target viewport)\n"); 81 return false; 82 } 83 84 // Housekeeping of the amount of pixels to transfer 85 uint32_t total_pixel_count = (8 * QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE) / surface_driver->native_bits_per_pixel; 86 uint32_t pixel_counter = 0; 87 rgb_t *target_buffer = (rgb_t *)qp_internal_global_pixdata_buffer; 88 89 // Fill the global pixdata area so that we can start transferring to the panel 90 for (uint16_t y = t; y <= b; ++y) { 91 for (uint16_t x = l; x <= r; ++x) { 92 // Update the target buffer 93 target_buffer[pixel_counter++] = surface_handle->rgbbuffer[y * surface_handle->base.panel_width + x]; 94 95 // If we've accumulated enough data, send it 96 if (pixel_counter == total_pixel_count) { 97 ok = qp_pixdata((painter_device_t)target_driver, qp_internal_global_pixdata_buffer, pixel_counter); 98 if (!ok) { 99 qp_dprintf("rgb888_target_pixdata_transfer: fail (could not stream pixdata to target)\n"); 100 return false; 101 } 102 // Reset the counter 103 pixel_counter = 0; 104 } 105 } 106 } 107 108 // If there's any leftover data, send it 109 if (pixel_counter > 0) { 110 ok = qp_pixdata((painter_device_t)target_driver, qp_internal_global_pixdata_buffer, pixel_counter); 111 if (!ok) { 112 qp_dprintf("rgb888_target_pixdata_transfer: fail (could not stream pixdata to target)\n"); 113 return false; 114 } 115 } 116 117 return true; 118 } 119 120 static bool qp_surface_append_pixdata_rgb888(painter_device_t device, uint8_t *target_buffer, uint32_t pixdata_offset, uint8_t pixdata_byte) { 121 target_buffer[pixdata_offset] = pixdata_byte; 122 return true; 123 } 124 125 const surface_painter_driver_vtable_t rgb888_surface_driver_vtable = { 126 .base = 127 { 128 .init = qp_surface_init, 129 .power = qp_surface_power, 130 .clear = qp_surface_clear, 131 .flush = qp_surface_flush, 132 .pixdata = qp_surface_pixdata_rgb888, 133 .viewport = qp_surface_viewport, 134 .palette_convert = qp_surface_palette_convert_rgb888, 135 .append_pixels = qp_surface_append_pixels_rgb888, 136 .append_pixdata = qp_surface_append_pixdata_rgb888, 137 }, 138 .target_pixdata_transfer = rgb888_target_pixdata_transfer, 139 }; 140 141 SURFACE_FACTORY_FUNCTION_IMPL(qp_make_rgb888_surface, rgb888_surface_driver_vtable, 24); 142 143 #endif // QUANTUM_PAINTER_SURFACE_ENABLE