32#include "freertos/FreeRTOS.h"
33#include "freertos/task.h"
40#pragma GCC optimize ("O2")
54volatile uint8_t * * CVBSPalettedController::s_viewPort;
55volatile uint8_t * * CVBSPalettedController::s_viewPortVisible;
60CVBSPalettedController::CVBSPalettedController(
int columnsQuantum,
NativePixelFormat nativePixelFormat,
int viewPortRatioDiv,
int viewPortRatioMul)
61 : CVBSBaseController(),
62 m_columnsQuantum(columnsQuantum),
63 m_nativePixelFormat(nativePixelFormat),
64 m_viewPortRatioDiv(viewPortRatioDiv),
65 m_viewPortRatioMul(viewPortRatioMul)
67 m_palette = (RGB222*) heap_caps_malloc(
sizeof(RGB222) * getPaletteSize(), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
71CVBSPalettedController::~CVBSPalettedController()
73 heap_caps_free(m_palette);
77void CVBSPalettedController::init()
79 CVBSBaseController::init();
81 m_taskProcessingPrimitives =
false;
82 m_processPrimitivesOnBlank =
false;
83 m_primitiveExecTask =
nullptr;
87void CVBSPalettedController::end()
89 if (m_primitiveExecTask) {
90 vTaskDelete(m_primitiveExecTask);
91 m_primitiveExecTask =
nullptr;
92 m_taskProcessingPrimitives =
false;
94 CVBSBaseController::end();
98void CVBSPalettedController::suspendBackgroundPrimitiveExecution()
100 CVBSBaseController::suspendBackgroundPrimitiveExecution();
101 while (m_taskProcessingPrimitives)
106void CVBSPalettedController::checkViewPortSize()
108 m_viewPortWidth &= ~(m_columnsQuantum - 1);
112void CVBSPalettedController::allocateViewPort()
114 CVBSBaseController::allocateViewPort(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL, m_viewPortWidth / m_viewPortRatioDiv * m_viewPortRatioMul);
118void CVBSPalettedController::freeViewPort()
120 CVBSBaseController::freeViewPort();
124void CVBSPalettedController::setResolution(CVBSParams
const * params,
int viewPortWidth,
int viewPortHeight,
bool doubleBuffered)
126 CVBSBaseController::setResolution(params, viewPortWidth, viewPortHeight, doubleBuffered);
128 s_viewPort = m_viewPort;
129 s_viewPortVisible = m_viewPortVisible;
132 for (
int i = 0; i < m_viewPortHeight; ++i)
133 memset((
void*)(m_viewPort[i]), 0, m_viewPortWidth / m_viewPortRatioDiv * m_viewPortRatioMul);
135 setupDefaultPalette();
136 updateRGB2PaletteLUT();
138 calculateAvailableCyclesForDrawings();
140 if (m_primitiveExecTask ==
nullptr) {
144 resumeBackgroundPrimitiveExecution();
150int CVBSPalettedController::getPaletteSize()
152 switch (nativePixelFormat()) {
153 case NativePixelFormat::PALETTE2:
155 case NativePixelFormat::PALETTE4:
157 case NativePixelFormat::PALETTE8:
159 case NativePixelFormat::PALETTE16:
168void CVBSPalettedController::updateRGB2PaletteLUT()
170 auto paletteSize = getPaletteSize();
171 for (
int r = 0; r < 4; ++r)
172 for (
int g = 0; g < 4; ++g)
173 for (
int b = 0; b < 4; ++b) {
175 rgb222_to_hsv(r, g, b, &H1, &S1, &V1);
177 int bestDst = 1000000000;
178 for (
int i = 0; i < paletteSize; ++i) {
180 rgb222_to_hsv(m_palette[i].
R, m_palette[i].
G, m_palette[i].
B, &H2, &S2, &V2);
184 int dst = AH * AH + AS * AS + AV * AV;
185 if (dst <= bestDst) {
192 m_packedRGB222_to_PaletteIndex[r | (g << 2) | (b << 4)] = bestIdx;
198void CVBSPalettedController::calculateAvailableCyclesForDrawings()
202 if (m_processPrimitivesOnBlank) {
204 availtime_us = params()->blankLines * params()->line_us;
207 availtime_us = params()->fieldLines * params()->line_us;
210 availtime_us = params()->blankLines * params()->line_us;
212 m_primitiveExecTimeoutCycles = getCPUFrequencyMHz() * availtime_us;
218void CVBSPalettedController::primitiveExecTask(
void * arg)
220 auto ctrl = (CVBSPalettedController *) arg;
223 if (!ctrl->m_primitiveProcessingSuspended) {
224 auto startCycle = ctrl->backgroundPrimitiveTimeoutEnabled() ? getCycleCount() : 0;
225 Rect updateRect = Rect(SHRT_MAX, SHRT_MAX, SHRT_MIN, SHRT_MIN);
226 ctrl->m_taskProcessingPrimitives =
true;
229 if (ctrl->getPrimitive(&prim, 0) ==
false)
231 ctrl->execPrimitive(prim, updateRect,
false);
232 if (ctrl->m_primitiveProcessingSuspended)
234 }
while (!ctrl->backgroundPrimitiveTimeoutEnabled() || (startCycle + ctrl->m_primitiveExecTimeoutCycles > getCycleCount()));
235 ctrl->showSprites(updateRect);
236 ctrl->m_taskProcessingPrimitives =
false;
240 ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
246void CVBSPalettedController::swapBuffers()
248 CVBSBaseController::swapBuffers();
249 s_viewPort = m_viewPort;
250 s_viewPortVisible = m_viewPortVisible;
This file contains fabgl::CVBSPalettedController definition.
#define FABGLIB_VGAPALETTEDCONTROLLER_PRIMTASK_PRIORITY
#define FABGLIB_VGAPALETTEDCONTROLLER_PRIMTASK_STACK_SIZE
This file contains some utility classes and functions.
NativePixelFormat
This enum defines the display controller native pixel format.