33#include "freertos/FreeRTOS.h"
34#include "freertos/task.h"
36#include "soc/i2s_struct.h"
37#include "soc/i2s_reg.h"
38#include "driver/periph_ctrl.h"
40#include "esp_spi_flash.h"
41#include "esp_heap_caps.h"
49#pragma GCC optimize ("O2")
63volatile uint8_t * * VGAPalettedController::s_viewPort;
64volatile uint8_t * * VGAPalettedController::s_viewPortVisible;
65lldesc_t
volatile * VGAPalettedController::s_frameResetDesc;
66volatile int VGAPalettedController::s_scanLine;
71VGAPalettedController::VGAPalettedController(
int linesCount,
int columnsQuantum,
NativePixelFormat nativePixelFormat,
int viewPortRatioDiv,
int viewPortRatioMul, intr_handler_t isrHandler)
72 : m_linesCount(linesCount),
73 m_columnsQuantum(columnsQuantum),
74 m_nativePixelFormat(nativePixelFormat),
75 m_viewPortRatioDiv(viewPortRatioDiv),
76 m_viewPortRatioMul(viewPortRatioMul),
77 m_isrHandler(isrHandler)
79 m_lines = (
volatile uint8_t**) heap_caps_malloc(
sizeof(uint8_t*) * m_linesCount, MALLOC_CAP_32BIT | MALLOC_CAP_INTERNAL);
80 m_palette = (RGB222*) heap_caps_malloc(
sizeof(RGB222) * getPaletteSize(), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
84VGAPalettedController::~VGAPalettedController()
86 heap_caps_free(m_palette);
87 heap_caps_free(m_lines);
91void VGAPalettedController::init()
93 VGABaseController::init();
95 m_doubleBufferOverDMA =
false;
96 m_taskProcessingPrimitives =
false;
97 m_processPrimitivesOnBlank =
false;
98 m_primitiveExecTask =
nullptr;
102void VGAPalettedController::end()
104 if (m_primitiveExecTask) {
105 vTaskDelete(m_primitiveExecTask);
106 m_primitiveExecTask =
nullptr;
107 m_taskProcessingPrimitives =
false;
109 VGABaseController::end();
113void VGAPalettedController::suspendBackgroundPrimitiveExecution()
115 VGABaseController::suspendBackgroundPrimitiveExecution();
116 while (m_taskProcessingPrimitives)
121void VGAPalettedController::checkViewPortSize()
123 m_viewPortHeight &= ~(m_linesCount - 1);
124 m_viewPortWidth &= ~(m_columnsQuantum - 1);
128void VGAPalettedController::allocateViewPort()
130 VGABaseController::allocateViewPort(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL, m_viewPortWidth / m_viewPortRatioDiv * m_viewPortRatioMul);
132 for (
int i = 0; i < m_linesCount; ++i)
133 m_lines[i] = (uint8_t*) heap_caps_malloc(m_viewPortWidth, MALLOC_CAP_DMA);
137void VGAPalettedController::freeViewPort()
139 VGABaseController::freeViewPort();
141 for (
int i = 0; i < m_linesCount; ++i) {
142 heap_caps_free((
void*)m_lines[i]);
143 m_lines[i] =
nullptr;
148void VGAPalettedController::setResolution(VGATimings
const& timings,
int viewPortWidth,
int viewPortHeight,
bool doubleBuffered)
150 VGABaseController::setResolution(timings, viewPortWidth, viewPortHeight, doubleBuffered);
152 s_viewPort = m_viewPort;
153 s_viewPortVisible = m_viewPortVisible;
156 for (
int i = 0; i < m_viewPortHeight; ++i)
157 memset((
void*)(m_viewPort[i]), 0, m_viewPortWidth / m_viewPortRatioDiv * m_viewPortRatioMul);
159 setupDefaultPalette();
160 updateRGB2PaletteLUT();
162 calculateAvailableCyclesForDrawings();
168 if (m_isr_handle ==
nullptr) {
171 I2S1.int_clr.val = 0xFFFFFFFF;
172 I2S1.int_ena.out_eof = 1;
175 if (m_primitiveExecTask ==
nullptr) {
179 resumeBackgroundPrimitiveExecution();
183void VGAPalettedController::onSetupDMABuffer(lldesc_t
volatile * buffer,
bool isStartOfVertFrontPorch,
int scan,
bool isVisible,
int visibleRow)
186 buffer->buf = (uint8_t *) m_lines[visibleRow % m_linesCount];
189 if ((scan == 0 && (visibleRow % (m_linesCount / 2)) == 0)) {
191 s_frameResetDesc = buffer;
198int VGAPalettedController::getPaletteSize()
200 switch (nativePixelFormat()) {
201 case NativePixelFormat::PALETTE2:
203 case NativePixelFormat::PALETTE4:
205 case NativePixelFormat::PALETTE8:
207 case NativePixelFormat::PALETTE16:
216void VGAPalettedController::updateRGB2PaletteLUT()
218 auto paletteSize = getPaletteSize();
219 for (
int r = 0; r < 4; ++r)
220 for (
int g = 0; g < 4; ++g)
221 for (
int b = 0; b < 4; ++b) {
223 rgb222_to_hsv(r, g, b, &H1, &S1, &V1);
225 int bestDst = 1000000000;
226 for (
int i = 0; i < paletteSize; ++i) {
228 rgb222_to_hsv(m_palette[i].
R, m_palette[i].
G, m_palette[i].
B, &H2, &S2, &V2);
232 int dst = AH * AH + AS * AS + AV * AV;
233 if (dst <= bestDst) {
240 m_packedRGB222_to_PaletteIndex[r | (g << 2) | (b << 4)] = bestIdx;
246void VGAPalettedController::calculateAvailableCyclesForDrawings()
250 if (m_processPrimitivesOnBlank) {
252 availtime_us = ceil(1000000.0 / m_timings.frequency * m_timings.scanCount * m_HLineSize * (m_linesCount / 2 + m_timings.VFrontPorch + m_timings.VSyncPulse + m_timings.VBackPorch + m_viewPortRow));
255 availtime_us = ceil(1000000.0 / m_timings.frequency * m_timings.scanCount * m_HLineSize * (m_timings.VVisibleArea + m_timings.VFrontPorch + m_timings.VSyncPulse + m_timings.VBackPorch));
259 m_primitiveExecTimeoutCycles = getCPUFrequencyMHz() * availtime_us;
265void VGAPalettedController::primitiveExecTask(
void * arg)
267 auto ctrl = (VGAPalettedController *) arg;
270 if (!ctrl->m_primitiveProcessingSuspended) {
271 auto startCycle = ctrl->backgroundPrimitiveTimeoutEnabled() ? getCycleCount() : 0;
272 Rect updateRect = Rect(SHRT_MAX, SHRT_MAX, SHRT_MIN, SHRT_MIN);
273 ctrl->m_taskProcessingPrimitives =
true;
276 if (ctrl->getPrimitive(&prim, 0) ==
false)
278 ctrl->execPrimitive(prim, updateRect,
false);
279 if (ctrl->m_primitiveProcessingSuspended)
281 }
while (!ctrl->backgroundPrimitiveTimeoutEnabled() || (startCycle + ctrl->m_primitiveExecTimeoutCycles > getCycleCount()));
282 ctrl->showSprites(updateRect);
283 ctrl->m_taskProcessingPrimitives =
false;
287 ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
293void VGAPalettedController::swapBuffers()
295 VGABaseController::swapBuffers();
296 s_viewPort = m_viewPort;
297 s_viewPortVisible = m_viewPortVisible;
#define FABGLIB_VGAPALETTEDCONTROLLER_PRIMTASK_PRIORITY
#define FABGLIB_VIDEO_CPUINTENSIVE_TASKS_CORE
#define FABGLIB_VGAPALETTEDCONTROLLER_PRIMTASK_STACK_SIZE
This file contains some utility classes and functions.
NativePixelFormat
This enum defines the display controller native pixel format.
This file contains fabgl::GPIOStream definition.
This file contains fabgl::VGAPalettedController definition.