logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

qp_surface_mono1bpp.c (4687B)


  1. // Copyright 2022 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #ifdef QUANTUM_PAINTER_SURFACE_ENABLE
  4. # include "color.h"
  5. # include "qp_draw.h"
  6. # include "qp_surface_internal.h"
  7. # include "qp_comms_dummy.h"
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. // Surface driver impl: mono1bpp
  10. static inline void setpixel_mono1bpp(surface_painter_device_t *surface, uint16_t x, uint16_t y, bool mono_pixel) {
  11. uint16_t w = surface->base.panel_width;
  12. uint16_t h = surface->base.panel_height;
  13. // Drop out if it's off-screen
  14. if (x >= w || y >= h) {
  15. return;
  16. }
  17. // Figure out which location needs to be updated
  18. uint32_t pixel_num = y * w + x;
  19. uint32_t byte_offset = pixel_num / 8;
  20. uint8_t bit_offset = pixel_num % 8;
  21. bool curr_val = (surface->u8buffer[byte_offset] & (1 << bit_offset)) ? true : false;
  22. // Skip messing with the dirty info if the original value already matches
  23. if (curr_val != mono_pixel) {
  24. // Update the dirty region
  25. qp_surface_update_dirty(&surface->dirty, x, y);
  26. // Update the pixel data in the buffer
  27. if (mono_pixel) {
  28. surface->u8buffer[byte_offset] |= (1 << bit_offset);
  29. } else {
  30. surface->u8buffer[byte_offset] &= ~(1 << bit_offset);
  31. }
  32. }
  33. }
  34. static inline void append_pixel_mono1bpp(surface_painter_device_t *surface, bool mono_pixel) {
  35. setpixel_mono1bpp(surface, surface->viewport.pixdata_x, surface->viewport.pixdata_y, mono_pixel);
  36. qp_surface_increment_pixdata_location(&surface->viewport);
  37. }
  38. static inline void stream_pixdata_mono1bpp(surface_painter_device_t *surface, const uint8_t *data, uint32_t native_pixel_count) {
  39. for (uint32_t pixel_counter = 0; pixel_counter < native_pixel_count; ++pixel_counter) {
  40. uint32_t byte_offset = pixel_counter / 8;
  41. uint8_t bit_offset = pixel_counter % 8;
  42. append_pixel_mono1bpp(surface, (data[byte_offset] & (1 << bit_offset)) ? true : false);
  43. }
  44. }
  45. // Stream pixel data to the current write position in GRAM
  46. static bool qp_surface_pixdata_mono1bpp(painter_device_t device, const void *pixel_data, uint32_t native_pixel_count) {
  47. painter_driver_t * driver = (painter_driver_t *)device;
  48. surface_painter_device_t *surface = (surface_painter_device_t *)driver;
  49. stream_pixdata_mono1bpp(surface, (const uint8_t *)pixel_data, native_pixel_count);
  50. return true;
  51. }
  52. // Pixel colour conversion
  53. static bool qp_surface_palette_convert_mono1bpp(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].mono = (palette[i].hsv888.v > 127) ? 1 : 0;
  56. }
  57. return true;
  58. }
  59. // Append pixels to the target location, keyed by the pixel index
  60. 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) {
  61. for (uint32_t i = 0; i < pixel_count; ++i) {
  62. uint32_t pixel_num = pixel_offset + i;
  63. uint32_t byte_offset = pixel_num / 8;
  64. uint8_t bit_offset = pixel_num % 8;
  65. if (palette[palette_indices[i]].mono) {
  66. target_buffer[byte_offset] |= (1 << bit_offset);
  67. } else {
  68. target_buffer[byte_offset] &= ~(1 << bit_offset);
  69. }
  70. }
  71. return true;
  72. }
  73. 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) {
  74. return false; // Not yet supported.
  75. }
  76. static bool qp_surface_append_pixdata_mono1bpp(painter_device_t device, uint8_t *target_buffer, uint32_t pixdata_offset, uint8_t pixdata_byte) {
  77. return false; // Just use 1bpp images.
  78. }
  79. const surface_painter_driver_vtable_t mono1bpp_surface_driver_vtable = {
  80. .base =
  81. {
  82. .init = qp_surface_init,
  83. .power = qp_surface_power,
  84. .clear = qp_surface_clear,
  85. .flush = qp_surface_flush,
  86. .pixdata = qp_surface_pixdata_mono1bpp,
  87. .viewport = qp_surface_viewport,
  88. .palette_convert = qp_surface_palette_convert_mono1bpp,
  89. .append_pixels = qp_surface_append_pixels_mono1bpp,
  90. .append_pixdata = qp_surface_append_pixdata_mono1bpp,
  91. },
  92. .target_pixdata_transfer = mono1bpp_target_pixdata_transfer,
  93. };
  94. SURFACE_FACTORY_FUNCTION_IMPL(qp_make_mono1bpp_surface, mono1bpp_surface_driver_vtable, 1);
  95. #endif // QUANTUM_PAINTER_SURFACE_ENABLE