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")
58static inline __attribute__((always_inline))
void VGA2_SETPIXELINROW(uint8_t * row,
int x,
int value) {
60 row[brow] ^= (-value ^ row[brow]) & (0x80 >> (x & 7));
63static inline __attribute__((always_inline))
int VGA2_GETPIXELINROW(uint8_t * row,
int x) {
65 return (row[brow] & (0x80 >> (x & 7))) != 0;
68#define VGA2_INVERTPIXELINROW(row, x) (row)[(x) >> 3] ^= (0x80 >> ((x) & 7))
70static inline __attribute__((always_inline))
void VGA2_SETPIXEL(
int x,
int y,
int value) {
71 auto row = (uint8_t*) VGA2Controller::sgetScanline(y);
73 row[brow] ^= (-value ^ row[brow]) & (0x80 >> (x & 7));
76#define VGA2_GETPIXEL(x, y) VGA2_GETPIXELINROW((uint8_t*)VGA2Controller::s_viewPort[(y)], (x))
78#define VGA2_INVERT_PIXEL(x, y) VGA2_INVERTPIXELINROW((uint8_t*)VGA2Controller::s_viewPort[(y)], (x))
81#define VGA2_COLUMNSQUANTUM 16
89VGA2Controller * VGA2Controller::s_instance =
nullptr;
93VGA2Controller::VGA2Controller()
97 m_packedPaletteIndexOctet_to_signals = (uint64_t *) heap_caps_malloc(256 *
sizeof(uint64_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
101VGA2Controller::~VGA2Controller()
103 heap_caps_free((
void *)m_packedPaletteIndexOctet_to_signals);
107void VGA2Controller::setupDefaultPalette()
109 setPaletteItem(0, RGB888(0, 0, 0));
110 setPaletteItem(1, RGB888(255, 255, 255));
114void VGA2Controller::setPaletteItem(
int index,
RGB888 const & color)
117 m_palette[index] = color;
118 auto packed222 = RGB888toPackedRGB222(color);
119 for (
int i = 0; i < 256; ++i) {
120 auto b = (uint8_t *) (m_packedPaletteIndexOctet_to_signals + i);
121 for (
int j = 0; j < 8; ++j) {
123 if ((index == 0 && ((1 << aj) & i) == 0) || (index == 1 && ((1 << aj) & i) != 0)) {
124 b[j ^ 2] = m_HVSync | packed222;
131void VGA2Controller::setPixelAt(PixelDesc
const & pixelDesc,
Rect & updateRect)
133 genericSetPixelAt(pixelDesc, updateRect,
134 [&] (
RGB888 const & color) {
return RGB888toPaletteIndex(color); },
142void VGA2Controller::absDrawLine(
int X1,
int Y1,
int X2,
int Y2, RGB888 color)
144 genericAbsDrawLine(
X1,
Y1,
X2,
Y2, color,
145 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
146 [&] (
int Y,
int X1,
int X2, uint8_t colorIndex) { rawFillRow(
Y,
X1,
X2, colorIndex); },
147 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); },
149 [&] (
int X,
int Y) { VGA2_INVERT_PIXEL(
X,
Y); }
155void VGA2Controller::rawFillRow(
int y,
int x1,
int x2, RGB888 color)
157 rawFillRow(y, x1, x2, RGB888toPaletteIndex(color));
162void VGA2Controller::rawFillRow(
int y,
int x1,
int x2, uint8_t colorIndex)
164 uint8_t * row = (uint8_t*) m_viewPort[y];
167 for (; x <= x2 && (x & 7) != 0; ++x) {
168 VGA2_SETPIXELINROW(row, x, colorIndex);
172 int sz = (x2 & ~7) - x;
173 memset((
void*)(row + x / 8), colorIndex ? 0xFF : 0x00, sz / 8);
177 for (; x <= x2; ++x) {
178 VGA2_SETPIXELINROW(row, x, colorIndex);
184void VGA2Controller::rawInvertRow(
int y,
int x1,
int x2)
186 auto row = m_viewPort[y];
187 for (
int x = x1; x <= x2; ++x)
188 VGA2_INVERTPIXELINROW(row, x);
192void VGA2Controller::rawCopyRow(
int x1,
int x2,
int srcY,
int dstY)
194 auto srcRow = (uint8_t*) m_viewPort[srcY];
195 auto dstRow = (uint8_t*) m_viewPort[dstY];
198 for (; x <= x2 && (x & 7) != 0; ++x) {
199 VGA2_SETPIXELINROW(dstRow, x, VGA2_GETPIXELINROW(srcRow, x));
202 auto src = (uint8_t*)(srcRow + x / 8);
203 auto dst = (uint8_t*)(dstRow + x / 8);
204 for (
int right = (x2 & ~7); x < right; x += 8)
207 for (x = (x2 & ~7); x <= x2; ++x) {
208 VGA2_SETPIXELINROW(dstRow, x, VGA2_GETPIXELINROW(srcRow, x));
213void VGA2Controller::swapRows(
int yA,
int yB,
int x1,
int x2)
215 auto rowA = (uint8_t*) m_viewPort[yA];
216 auto rowB = (uint8_t*) m_viewPort[yB];
219 for (; x <= x2 && (x & 7) != 0; ++x) {
220 uint8_t a = VGA2_GETPIXELINROW(rowA, x);
221 uint8_t b = VGA2_GETPIXELINROW(rowB, x);
222 VGA2_SETPIXELINROW(rowA, x, b);
223 VGA2_SETPIXELINROW(rowB, x, a);
226 auto a = (uint8_t*)(rowA + x / 8);
227 auto b = (uint8_t*)(rowB + x / 8);
228 for (
int right = (x2 & ~7); x < right; x += 8)
231 for (x = (x2 & ~7); x <= x2; ++x) {
232 uint8_t a = VGA2_GETPIXELINROW(rowA, x);
233 uint8_t b = VGA2_GETPIXELINROW(rowB, x);
234 VGA2_SETPIXELINROW(rowA, x, b);
235 VGA2_SETPIXELINROW(rowB, x, a);
240void VGA2Controller::drawEllipse(Size
const & size, Rect & updateRect)
242 genericDrawEllipse(size, updateRect,
243 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
249void VGA2Controller::clear(Rect & updateRect)
251 hideSprites(updateRect);
252 uint8_t paletteIndex = RGB888toPaletteIndex(getActualBrushColor());
253 uint8_t pattern8 = paletteIndex ? 0xFF : 0x00;
254 for (
int y = 0; y < m_viewPortHeight; ++y)
255 memset((uint8_t*) m_viewPort[y], pattern8, m_viewPortWidth / 8);
261void VGA2Controller::VScroll(
int scroll, Rect & updateRect)
263 genericVScroll(scroll, updateRect,
264 [&] (
int yA,
int yB,
int x1,
int x2) { swapRows(yA, yB, x1, x2); },
265 [&] (
int yA,
int yB) { tswap(m_viewPort[yA], m_viewPort[yB]); },
266 [&] (
int y,
int x1,
int x2, RGB888 color) { rawFillRow(y, x1, x2, color); }
271void VGA2Controller::HScroll(
int scroll, Rect & updateRect)
273 hideSprites(updateRect);
274 uint8_t back = RGB888toPaletteIndex(getActualBrushColor());
275 uint8_t back8 = back ? 0xFF : 0x00;
277 int Y1 = paintState().scrollingRegion.Y1;
278 int Y2 = paintState().scrollingRegion.Y2;
279 int X1 = paintState().scrollingRegion.X1;
280 int X2 = paintState().scrollingRegion.X2;
283 bool HScrolllingRegionAligned = ((
X1 & 7) == 0 && (
width & 7) == 0);
287 for (
int y =
Y1; y <=
Y2; ++y) {
288 if (HScrolllingRegionAligned) {
290 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 8;
291 for (
int s = -scroll; s > 0;) {
295 uint8_t prev = back8;
296 for (
int i = sz - 1; i >= 0; --i) {
297 uint8_t lowbits = prev >> (8 - s);
299 row[i] = (row[i] << s) | lowbits;
305 auto sz =
width & ~7;
306 memmove(row, row + sc / 8, (sz - sc) / 8);
307 rawFillRow(y,
X2 - sc + 1,
X2, back);
313 auto row = (uint8_t*) m_viewPort[y];
314 for (
int x =
X1; x <=
X2 + scroll; ++x)
315 VGA2_SETPIXELINROW(row, x, VGA2_GETPIXELINROW(row, x - scroll));
317 rawFillRow(y,
X2 + 1 + scroll,
X2, back);
320 }
else if (scroll > 0) {
322 for (
int y =
Y1; y <=
Y2; ++y) {
323 if (HScrolllingRegionAligned) {
325 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 8;
326 for (
int s = scroll; s > 0;) {
330 uint8_t prev = back8;
331 for (
int i = 0; i < sz; ++i) {
332 uint8_t highbits = prev << (8 - s);
334 row[i] = (row[i] >> s) | highbits;
340 auto sz =
width & ~7;
341 memmove(row + sc / 8, row, (sz - sc) / 8);
342 rawFillRow(y,
X1,
X1 + sc - 1, back);
348 auto row = (uint8_t*) m_viewPort[y];
349 for (
int x =
X2 - scroll; x >=
X1; --x)
350 VGA2_SETPIXELINROW(row, x + scroll, VGA2_GETPIXELINROW(row, x));
352 rawFillRow(y,
X1,
X1 + scroll - 1, back);
360void VGA2Controller::drawGlyph(Glyph
const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect)
362 genericDrawGlyph(glyph, glyphOptions, penColor, brushColor, updateRect,
363 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
364 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
370void VGA2Controller::invertRect(Rect
const & rect, Rect & updateRect)
372 genericInvertRect(rect, updateRect,
373 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); }
380 genericSwapFGBG(rect, updateRect,
381 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
382 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
391void VGA2Controller::copyRect(Rect
const & source, Rect & updateRect)
393 genericCopyRect(source, updateRect,
394 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
402void VGA2Controller::readScreen(Rect
const & rect, RGB888 * destBuf)
404 for (
int y = rect.Y1; y <= rect.Y2; ++y) {
405 auto row = (uint8_t*) m_viewPort[y];
406 for (
int x = rect.X1; x <= rect.X2; ++x, ++destBuf) {
407 const RGB222 v = m_palette[VGA2_GETPIXELINROW(row, x)];
408 *destBuf = RGB888(v.R * 85, v.G * 85, v.B * 85);
414void VGA2Controller::rawDrawBitmap_Native(
int destX,
int destY, Bitmap
const * bitmap,
int X1,
int Y1,
int XCount,
int YCount)
416 genericRawDrawBitmap_Native(destX, destY, (uint8_t*) bitmap->data, bitmap->width,
X1,
Y1, XCount, YCount,
417 [&] (
int y) { return (uint8_t*) m_viewPort[y]; },
423void VGA2Controller::rawDrawBitmap_Mask(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
425 auto foregroundColorIndex = RGB888toPaletteIndex(bitmap->foregroundColor);
426 genericRawDrawBitmap_Mask(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
427 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
429 [&] (uint8_t * row,
int x) { VGA2_SETPIXELINROW(row, x, foregroundColorIndex); }
434void VGA2Controller::rawDrawBitmap_RGBA2222(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
436 genericRawDrawBitmap_RGBA2222(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
437 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
439 [&] (uint8_t * row,
int x, uint8_t src) { VGA2_SETPIXELINROW(row, x, RGB2222toPaletteIndex(src)); }
444void VGA2Controller::rawDrawBitmap_RGBA8888(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
446 genericRawDrawBitmap_RGBA8888(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
447 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
448 [&] (uint8_t * row,
int x) {
return VGA2_GETPIXELINROW(row, x); },
449 [&] (uint8_t * row,
int x,
RGBA8888 const & src) { VGA2_SETPIXELINROW(row, x, RGB8888toPaletteIndex(src)); }
454void IRAM_ATTR VGA2Controller::ISRHandler(
void * arg)
456 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK
457 auto s1 = getCycleCount();
460 auto ctrl = (VGA2Controller *) arg;
462 if (I2S1.int_st.out_eof) {
464 auto const desc = (lldesc_t*) I2S1.out_eof_des_addr;
466 if (desc == s_frameResetDesc)
469 auto const width = ctrl->m_viewPortWidth;
470 auto const height = ctrl->m_viewPortHeight;
471 auto const packedPaletteIndexOctet_to_signals = (uint64_t
const *) ctrl->m_packedPaletteIndexOctet_to_signals;
472 auto const lines = ctrl->m_lines;
474 int scanLine = (s_scanLine + VGA2_LinesCount / 2) %
height;
476 auto lineIndex = scanLine & (VGA2_LinesCount - 1);
478 for (
int i = 0; i < VGA2_LinesCount / 2; ++i) {
480 auto src = (uint8_t
const *) s_viewPortVisible[scanLine];
481 auto dest = (uint64_t*) lines[lineIndex];
484 for (
int col = 0; col <
width; col += 16) {
486 auto src1 = *(src + 0);
487 auto src2 = *(src + 1);
491 auto v1 = packedPaletteIndexOctet_to_signals[src1];
492 auto v2 = packedPaletteIndexOctet_to_signals[src2];
506 s_scanLine += VGA2_LinesCount / 2;
508 if (scanLine >=
height && !ctrl->m_primitiveProcessingSuspended && spi_flash_cache_enabled() && ctrl->m_primitiveExecTask) {
511 vTaskNotifyGiveFromISR(ctrl->m_primitiveExecTask, NULL);
516 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK
517 s_vgapalctrlcycles += getCycleCount() - s1;
520 I2S1.int_clr.val = I2S1.int_st.val;
This file contains some utility classes and functions.
NativePixelFormat
This enum defines the display controller native pixel format.
Represents a 24 bit RGB color.
This file contains fabgl::GPIOStream definition.
This file contains fabgl::VGA2Controller definition.