32#include "freertos/FreeRTOS.h"
33#include "freertos/task.h"
35#include "soc/i2s_struct.h"
36#include "soc/i2s_reg.h"
37#include "driver/periph_ctrl.h"
39#include "esp_spi_flash.h"
40#include "esp_heap_caps.h"
48#pragma GCC optimize ("O2")
59static inline __attribute__((always_inline))
void VGA16_SETPIXELINROW(uint8_t * row,
int x,
int value) {
61 row[brow] = (x & 1) ? ((row[brow] & 0xf0) | value) : ((row[brow] & 0x0f) | (value << 4));
64static inline __attribute__((always_inline))
int VGA16_GETPIXELINROW(uint8_t * row,
int x) {
66 return ((x & 1) ? (row[brow] & 0x0f) : ((row[brow] & 0xf0) >> 4));
69#define VGA16_INVERTPIXELINROW(row, x) (row)[(x) >> 1] ^= (0xf0 >> (((x) & 1) << 2))
71static inline __attribute__((always_inline))
void VGA16_SETPIXEL(
int x,
int y,
int value) {
72 auto row = (uint8_t*) VGA16Controller::sgetScanline(y);
74 row[brow] = (x & 1) ? ((row[brow] & 0xf0) | value) : ((row[brow] & 0x0f) | (value << 4));
77#define VGA16_GETPIXEL(x, y) VGA16_GETPIXELINROW((uint8_t*)VGA16Controller::s_viewPort[(y)], (x))
79#define VGA16_INVERT_PIXEL(x, y) VGA16_INVERTPIXELINROW((uint8_t*)VGA16Controller::s_viewPort[(y)], (x))
82#define VGA16_COLUMNSQUANTUM 16
89VGA16Controller * VGA16Controller::s_instance =
nullptr;
93VGA16Controller::VGA16Controller()
100void VGA16Controller::setupDefaultPalette()
102 for (
int colorIndex = 0; colorIndex < 16; ++colorIndex) {
103 RGB888 rgb888((
Color)colorIndex);
104 setPaletteItem(colorIndex, rgb888);
109void VGA16Controller::setPaletteItem(
int index,
RGB888 const & color)
112 m_palette[index] = color;
113 auto packed222 = RGB888toPackedRGB222(color);
114 for (
int i = 0; i < 16; ++i) {
115 m_packedPaletteIndexPair_to_signals[(index << 4) | i] &= 0xFF00;
116 m_packedPaletteIndexPair_to_signals[(index << 4) | i] |= (m_HVSync | packed222);
117 m_packedPaletteIndexPair_to_signals[(i << 4) | index] &= 0x00FF;
118 m_packedPaletteIndexPair_to_signals[(i << 4) | index] |= (m_HVSync | packed222) << 8;
123void VGA16Controller::setPixelAt(PixelDesc
const & pixelDesc,
Rect & updateRect)
125 genericSetPixelAt(pixelDesc, updateRect,
126 [&] (
RGB888 const & color) {
return RGB888toPaletteIndex(color); },
134void VGA16Controller::absDrawLine(
int X1,
int Y1,
int X2,
int Y2, RGB888 color)
136 genericAbsDrawLine(
X1,
Y1,
X2,
Y2, color,
137 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
138 [&] (
int Y,
int X1,
int X2, uint8_t colorIndex) { rawFillRow(
Y,
X1,
X2, colorIndex); },
139 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); },
141 [&] (
int X,
int Y) { VGA16_INVERT_PIXEL(
X,
Y); }
147void VGA16Controller::rawFillRow(
int y,
int x1,
int x2, RGB888 color)
149 rawFillRow(y, x1, x2, RGB888toPaletteIndex(color));
154void VGA16Controller::rawFillRow(
int y,
int x1,
int x2, uint8_t colorIndex)
156 uint8_t * row = (uint8_t*) m_viewPort[y];
159 for (; x <= x2 && (x & 3) != 0; ++x) {
160 VGA16_SETPIXELINROW(row, x, colorIndex);
164 int sz = (x2 & ~3) - x;
165 memset((
void*)(row + x / 2), colorIndex | (colorIndex << 4), sz / 2);
169 for (; x <= x2; ++x) {
170 VGA16_SETPIXELINROW(row, x, colorIndex);
176void VGA16Controller::rawInvertRow(
int y,
int x1,
int x2)
178 auto row = m_viewPort[y];
179 for (
int x = x1; x <= x2; ++x)
180 VGA16_INVERTPIXELINROW(row, x);
184void VGA16Controller::rawCopyRow(
int x1,
int x2,
int srcY,
int dstY)
186 auto srcRow = (uint8_t*) m_viewPort[srcY];
187 auto dstRow = (uint8_t*) m_viewPort[dstY];
190 for (; x <= x2 && (x & 3) != 0; ++x) {
191 VGA16_SETPIXELINROW(dstRow, x, VGA16_GETPIXELINROW(srcRow, x));
194 auto src = (uint16_t*)(srcRow + x / 2);
195 auto dst = (uint16_t*)(dstRow + x / 2);
196 for (
int right = (x2 & ~3); x < right; x += 4)
199 for (x = (x2 & ~3); x <= x2; ++x) {
200 VGA16_SETPIXELINROW(dstRow, x, VGA16_GETPIXELINROW(srcRow, x));
205void VGA16Controller::swapRows(
int yA,
int yB,
int x1,
int x2)
207 auto rowA = (uint8_t*) m_viewPort[yA];
208 auto rowB = (uint8_t*) m_viewPort[yB];
211 for (; x <= x2 && (x & 3) != 0; ++x) {
212 uint8_t a = VGA16_GETPIXELINROW(rowA, x);
213 uint8_t b = VGA16_GETPIXELINROW(rowB, x);
214 VGA16_SETPIXELINROW(rowA, x, b);
215 VGA16_SETPIXELINROW(rowB, x, a);
218 auto a = (uint16_t*)(rowA + x / 2);
219 auto b = (uint16_t*)(rowB + x / 2);
220 for (
int right = (x2 & ~3); x < right; x += 4)
223 for (x = (x2 & ~3); x <= x2; ++x) {
224 uint8_t a = VGA16_GETPIXELINROW(rowA, x);
225 uint8_t b = VGA16_GETPIXELINROW(rowB, x);
226 VGA16_SETPIXELINROW(rowA, x, b);
227 VGA16_SETPIXELINROW(rowB, x, a);
232void VGA16Controller::drawEllipse(Size
const & size, Rect & updateRect)
234 genericDrawEllipse(size, updateRect,
235 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
241void VGA16Controller::clear(Rect & updateRect)
243 hideSprites(updateRect);
244 uint8_t paletteIndex = RGB888toPaletteIndex(getActualBrushColor());
245 uint8_t pattern = paletteIndex | (paletteIndex << 4);
246 for (
int y = 0; y < m_viewPortHeight; ++y)
247 memset((uint8_t*) m_viewPort[y], pattern, m_viewPortWidth / 2);
253void VGA16Controller::VScroll(
int scroll, Rect & updateRect)
255 genericVScroll(scroll, updateRect,
256 [&] (
int yA,
int yB,
int x1,
int x2) { swapRows(yA, yB, x1, x2); },
257 [&] (
int yA,
int yB) { tswap(m_viewPort[yA], m_viewPort[yB]); },
258 [&] (
int y,
int x1,
int x2, RGB888 color) { rawFillRow(y, x1, x2, color); }
263void VGA16Controller::HScroll(
int scroll, Rect & updateRect)
265 hideSprites(updateRect);
266 uint8_t back4 = RGB888toPaletteIndex(getActualBrushColor());
268 int Y1 = paintState().scrollingRegion.Y1;
269 int Y2 = paintState().scrollingRegion.Y2;
270 int X1 = paintState().scrollingRegion.X1;
271 int X2 = paintState().scrollingRegion.X2;
274 bool HScrolllingRegionAligned = ((
X1 & 3) == 0 && (
width & 3) == 0);
278 for (
int y =
Y1; y <=
Y2; ++y) {
279 if (HScrolllingRegionAligned) {
281 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 2;
282 for (
int s = -scroll; s > 0;) {
286 auto sz =
width & ~1;
287 memmove(row, row + sc / 2, (sz - sc) / 2);
288 rawFillRow(y,
X2 - sc + 1,
X2, back4);
296 auto w = (uint16_t *) (row +
width / 2) - 1;
297 for (
int i = 0; i <
width; i += 4) {
298 const uint16_t p4 = *w;
299 *w-- = (p4 << 4 & 0xf000) | (prev << 8 & 0x0f00) | (p4 << 4 & 0x00f0) | (p4 >> 12 & 0x000f);
300 prev = p4 >> 4 & 0x000f;
307 auto row = (uint8_t*) m_viewPort[y];
308 for (
int x =
X1; x <=
X2 + scroll; ++x)
309 VGA16_SETPIXELINROW(row, x, VGA16_GETPIXELINROW(row, x - scroll));
311 rawFillRow(y,
X2 + 1 + scroll,
X2, back4);
314 }
else if (scroll > 0) {
316 for (
int y =
Y1; y <=
Y2; ++y) {
317 if (HScrolllingRegionAligned) {
319 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 2;
320 for (
int s = scroll; s > 0;) {
324 auto sz =
width & ~1;
325 memmove(row + sc / 2, row, (sz - sc) / 2);
326 rawFillRow(y,
X1,
X1 + sc - 1, back4);
334 auto w = (uint16_t *) row;
335 for (
int i = 0; i <
width; i += 4) {
336 const uint16_t p4 = *w;
337 *w++ = (p4 << 12 & 0xf000) | (p4 >> 4 & 0x0f00) | (prev << 4) | (p4 >> 4 & 0x000f);
338 prev = p4 >> 8 & 0x000f;
345 auto row = (uint8_t*) m_viewPort[y];
346 for (
int x =
X2 - scroll; x >=
X1; --x)
347 VGA16_SETPIXELINROW(row, x + scroll, VGA16_GETPIXELINROW(row, x));
349 rawFillRow(y,
X1,
X1 + scroll - 1, back4);
357void VGA16Controller::drawGlyph(Glyph
const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect)
359 genericDrawGlyph(glyph, glyphOptions, penColor, brushColor, updateRect,
360 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
361 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
367void VGA16Controller::invertRect(Rect
const & rect, Rect & updateRect)
369 genericInvertRect(rect, updateRect,
370 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); }
377 genericSwapFGBG(rect, updateRect,
378 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
379 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
388void VGA16Controller::copyRect(Rect
const & source, Rect & updateRect)
390 genericCopyRect(source, updateRect,
391 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
399void VGA16Controller::readScreen(Rect
const & rect, RGB888 * destBuf)
401 for (
int y = rect.Y1; y <= rect.Y2; ++y) {
402 auto row = (uint8_t*) m_viewPort[y];
403 for (
int x = rect.X1; x <= rect.X2; ++x, ++destBuf) {
404 const RGB222 v = m_palette[VGA16_GETPIXELINROW(row, x)];
405 *destBuf = RGB888(v.R * 85, v.G * 85, v.B * 85);
411void VGA16Controller::rawDrawBitmap_Native(
int destX,
int destY, Bitmap
const * bitmap,
int X1,
int Y1,
int XCount,
int YCount)
413 genericRawDrawBitmap_Native(destX, destY, (uint8_t*) bitmap->data, bitmap->width,
X1,
Y1, XCount, YCount,
414 [&] (
int y) { return (uint8_t*) m_viewPort[y]; },
420void VGA16Controller::rawDrawBitmap_Mask(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
422 auto foregroundColorIndex = RGB888toPaletteIndex(bitmap->foregroundColor);
423 genericRawDrawBitmap_Mask(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
424 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
426 [&] (uint8_t * row,
int x) { VGA16_SETPIXELINROW(row, x, foregroundColorIndex); }
431void VGA16Controller::rawDrawBitmap_RGBA2222(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
433 genericRawDrawBitmap_RGBA2222(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
434 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
436 [&] (uint8_t * row,
int x, uint8_t src) { VGA16_SETPIXELINROW(row, x, RGB2222toPaletteIndex(src)); }
441void VGA16Controller::rawDrawBitmap_RGBA8888(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
443 genericRawDrawBitmap_RGBA8888(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
444 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
445 [&] (uint8_t * row,
int x) {
return VGA16_GETPIXELINROW(row, x); },
446 [&] (uint8_t * row,
int x,
RGBA8888 const & src) { VGA16_SETPIXELINROW(row, x, RGB8888toPaletteIndex(src)); }
451void IRAM_ATTR VGA16Controller::ISRHandler(
void * arg)
453 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK
454 auto s1 = getCycleCount();
457 auto ctrl = (VGA16Controller *) arg;
459 if (I2S1.int_st.out_eof) {
461 auto const desc = (lldesc_t*) I2S1.out_eof_des_addr;
463 if (desc == s_frameResetDesc)
466 auto const width = ctrl->m_viewPortWidth;
467 auto const height = ctrl->m_viewPortHeight;
468 auto const packedPaletteIndexPair_to_signals = (uint16_t
const *) ctrl->m_packedPaletteIndexPair_to_signals;
469 auto const lines = ctrl->m_lines;
471 int scanLine = (s_scanLine + VGA16_LinesCount / 2) %
height;
473 auto lineIndex = scanLine & (VGA16_LinesCount - 1);
475 for (
int i = 0; i < VGA16_LinesCount / 2; ++i) {
477 auto src = (uint8_t
const *) s_viewPortVisible[scanLine];
478 auto dest = (uint16_t*) lines[lineIndex];
481 for (
int col = 0; col <
width; col += 16) {
483 auto src1 = *(src + 0);
484 auto src2 = *(src + 1);
485 auto src3 = *(src + 2);
486 auto src4 = *(src + 3);
487 auto src5 = *(src + 4);
488 auto src6 = *(src + 5);
489 auto src7 = *(src + 6);
490 auto src8 = *(src + 7);
494 auto v1 = packedPaletteIndexPair_to_signals[src1];
495 auto v2 = packedPaletteIndexPair_to_signals[src2];
496 auto v3 = packedPaletteIndexPair_to_signals[src3];
497 auto v4 = packedPaletteIndexPair_to_signals[src4];
498 auto v5 = packedPaletteIndexPair_to_signals[src5];
499 auto v6 = packedPaletteIndexPair_to_signals[src6];
500 auto v7 = packedPaletteIndexPair_to_signals[src7];
501 auto v8 = packedPaletteIndexPair_to_signals[src8];
521 s_scanLine += VGA16_LinesCount / 2;
523 if (scanLine >=
height && !ctrl->m_primitiveProcessingSuspended && spi_flash_cache_enabled() && ctrl->m_primitiveExecTask) {
526 vTaskNotifyGiveFromISR(ctrl->m_primitiveExecTask, NULL);
531 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK
532 s_vgapalctrlcycles += getCycleCount() - s1;
535 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.
Color
This enum defines named colors.
Represents a 24 bit RGB color.
This file contains fabgl::GPIOStream definition.
This file contains fabgl::VGA16Controller definition.