-
Notifications
You must be signed in to change notification settings - Fork 16
/
hagl_hal_double.c
207 lines (173 loc) · 6.3 KB
/
hagl_hal_double.c
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
/*
MIT License
Copyright (c) 2019-2021 Mika Tuupola
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-cut-
This file is part of the MIPI DCS HAL for HAGL graphics library:
https://github.com/tuupola/hagl_esp_mipi/
SPDX-License-Identifier: MIT
-cut-
This is the HAL used when double buffering is enabled. The GRAM of the
display driver chip is the framebuffer. The memory allocated by this HAL
is the back buffer. Total two buffers.
Note that all coordinates are already clipped in the main library itself.
HAL does not need to validate the coordinates, they can alway be assumed
valid.
*/
#include "sdkconfig.h"
#include "hagl_hal.h"
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <esp_log.h>
#include <esp_heap_caps.h>
#include <string.h>
#include <mipi_display.h>
#include <hagl/bitmap.h>
#include <hagl.h>
#ifdef CONFIG_HAGL_HAL_USE_DOUBLE_BUFFERING
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
static SemaphoreHandle_t mutex;
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
// static uint8_t *buffer;
static hagl_bitmap_t bb;
static spi_device_handle_t spi;
static const char *TAG = "hagl_esp_mipi";
static size_t
flush(void *self)
{
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
size_t size = 0;
/* Flush the whole back buffer with locking. */
xSemaphoreTake(mutex, portMAX_DELAY);
size = mipi_display_write(spi, 0, 0, bb.width, bb.height, (uint8_t *) bb.buffer);
xSemaphoreGive(mutex);
return size;
#else
/* Flush the whole back buffer. */
return mipi_display_write(spi, 0, 0, bb.width, bb.height, (uint8_t *) bb.buffer);
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
}
static void
put_pixel(void *self, int16_t x0, int16_t y0, hagl_color_t color)
{
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
xSemaphoreTake(mutex, portMAX_DELAY);
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
bb.put_pixel(&bb, x0, y0, color);
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
xSemaphoreGive(mutex);
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
}
static hagl_color_t
get_pixel(void *self, int16_t x0, int16_t y0)
{
return bb.get_pixel(&bb, x0, y0);
}
static void
blit(void *self, int16_t x0, int16_t y0, hagl_bitmap_t *src)
{
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
xSemaphoreTake(mutex, portMAX_DELAY);
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
bb.blit(&bb, x0, y0, src);
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
xSemaphoreGive(mutex);
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
}
static void
scale_blit(void *self, uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, hagl_bitmap_t *src)
{
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
xSemaphoreTake(mutex, portMAX_DELAY);
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
bb.scale_blit(&bb, x0, y0, w, h, src);
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
xSemaphoreGive(mutex);
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
}
static void
hline(void *self, int16_t x0, int16_t y0, uint16_t width, hagl_color_t color)
{
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
xSemaphoreTake(mutex, portMAX_DELAY);
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
bb.hline(&bb, x0, y0, width, color);
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
xSemaphoreGive(mutex);
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
}
static void
vline(void *self, int16_t x0, int16_t y0, uint16_t height, hagl_color_t color)
{
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
xSemaphoreTake(mutex, portMAX_DELAY);
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
bb.vline(&bb, x0, y0, height, color);
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
xSemaphoreGive(mutex);
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
}
// void hagl_hal_clear_screen()
// {
// #ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
// xSemaphoreTake(mutex, portMAX_DELAY);
// #endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
// hagl_color_t *ptr = (hagl_color_t *) buffer;
// size_t count = DISPLAY_WIDTH * DISPLAY_HEIGHT;
// while (--count) {
// *ptr++ = 0x0000;
// }
// #ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
// xSemaphoreGive(mutex);
// #endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
// }
void
hagl_hal_init(hagl_backend_t *backend)
{
mipi_display_init(&spi);
#ifdef CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING
mutex = xSemaphoreCreateMutex();
#endif /* CONFIG_HAGL_HAL_LOCK_WHEN_FLUSHING */
ESP_LOGI(
TAG, "Largest (MALLOC_CAP_DMA | MALLOC_CAP_32BIT) block before init: %d",
heap_caps_get_largest_free_block(MALLOC_CAP_DMA | MALLOC_CAP_32BIT)
);
backend->buffer = (uint8_t *) heap_caps_malloc(
BITMAP_SIZE(DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_DEPTH),
MALLOC_CAP_DMA
);
if (NULL == backend->buffer) {
ESP_LOGE(TAG, "NO BUFFER");
};
ESP_LOGI(
TAG, "Largest (MALLOC_CAP_DMA | MALLOC_CAP_32BIT) block after init: %d",
heap_caps_get_largest_free_block(MALLOC_CAP_DMA | MALLOC_CAP_32BIT)
);
heap_caps_print_heap_info(MALLOC_CAP_DMA | MALLOC_CAP_32BIT);
backend->width = MIPI_DISPLAY_WIDTH;
backend->height = MIPI_DISPLAY_HEIGHT;
backend->depth = MIPI_DISPLAY_DEPTH;
backend->put_pixel = put_pixel;
backend->get_pixel = get_pixel;
backend->hline = hline;
backend->vline = vline;
backend->blit = blit;
backend->scale_blit = scale_blit;
backend->flush = flush;
hagl_bitmap_init(&bb, backend->width, backend->height, backend->depth, backend->buffer);
}
#endif /* CONFIG_HAGL_HAL_USE_DOUBLE_BUFFERING */