summaryrefslogtreecommitdiffstats
path: root/drivers/painter/generic/qp_rgb565_surface.c
blob: 9c283e06872d68dd74be0b76c44ac225407429f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// 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);
}