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") 59 static 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));
64 static 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)) 71 static 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)) 88 VGA16Controller * VGA16Controller::s_instance =
nullptr;
92 VGA16Controller::VGA16Controller()
99 void VGA16Controller::setupDefaultPalette()
101 for (
int colorIndex = 0; colorIndex < 16; ++colorIndex) {
102 RGB888 rgb888((
Color)colorIndex);
111 m_palette[index] = color;
112 auto packed222 = RGB888toPackedRGB222(color);
113 for (
int i = 0; i < 16; ++i) {
114 m_packedPaletteIndexPair_to_signals[(index << 4) | i] &= 0xFF00;
115 m_packedPaletteIndexPair_to_signals[(index << 4) | i] |= (m_HVSync | packed222);
116 m_packedPaletteIndexPair_to_signals[(i << 4) | index] &= 0x00FF;
117 m_packedPaletteIndexPair_to_signals[(i << 4) | index] |= (m_HVSync | packed222) << 8;
122 void VGA16Controller::setPixelAt(PixelDesc
const & pixelDesc,
Rect & updateRect)
124 genericSetPixelAt(pixelDesc, updateRect,
125 [&] (
RGB888 const & color) {
return RGB888toPaletteIndex(color); },
133 void VGA16Controller::absDrawLine(
int X1,
int Y1,
int X2,
int Y2, RGB888 color)
135 genericAbsDrawLine(
X1,
Y1,
X2,
Y2, color,
136 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
137 [&] (
int Y,
int X1,
int X2, uint8_t colorIndex) { rawFillRow(
Y,
X1,
X2, colorIndex); },
138 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); },
140 [&] (
int X,
int Y) { VGA16_INVERT_PIXEL(
X,
Y); }
146 void VGA16Controller::rawFillRow(
int y,
int x1,
int x2, RGB888 color)
148 rawFillRow(y, x1, x2, RGB888toPaletteIndex(color));
153 void VGA16Controller::rawFillRow(
int y,
int x1,
int x2, uint8_t colorIndex)
155 uint8_t * row = (uint8_t*) m_viewPort[y];
158 for (; x <= x2 && (x & 3) != 0; ++x) {
159 VGA16_SETPIXELINROW(row, x, colorIndex);
163 int sz = (x2 & ~3) - x;
164 memset((
void*)(row + x / 2), colorIndex | (colorIndex << 4), sz / 2);
168 for (; x <= x2; ++x) {
169 VGA16_SETPIXELINROW(row, x, colorIndex);
175 void VGA16Controller::rawInvertRow(
int y,
int x1,
int x2)
177 auto row = m_viewPort[y];
178 for (
int x = x1; x <= x2; ++x)
179 VGA16_INVERTPIXELINROW(row, x);
183 void VGA16Controller::rawCopyRow(
int x1,
int x2,
int srcY,
int dstY)
185 auto srcRow = (uint8_t*) m_viewPort[srcY];
186 auto dstRow = (uint8_t*) m_viewPort[dstY];
189 for (; x <= x2 && (x & 3) != 0; ++x) {
190 VGA16_SETPIXELINROW(dstRow, x, VGA16_GETPIXELINROW(srcRow, x));
193 auto src = (uint16_t*)(srcRow + x / 2);
194 auto dst = (uint16_t*)(dstRow + x / 2);
195 for (
int right = (x2 & ~3); x < right; x += 4)
198 for (x = (x2 & ~3); x <= x2; ++x) {
199 VGA16_SETPIXELINROW(dstRow, x, VGA16_GETPIXELINROW(srcRow, x));
204 void VGA16Controller::swapRows(
int yA,
int yB,
int x1,
int x2)
206 auto rowA = (uint8_t*) m_viewPort[yA];
207 auto rowB = (uint8_t*) m_viewPort[yB];
210 for (; x <= x2 && (x & 3) != 0; ++x) {
211 uint8_t a = VGA16_GETPIXELINROW(rowA, x);
212 uint8_t b = VGA16_GETPIXELINROW(rowB, x);
213 VGA16_SETPIXELINROW(rowA, x, b);
214 VGA16_SETPIXELINROW(rowB, x, a);
217 auto a = (uint16_t*)(rowA + x / 2);
218 auto b = (uint16_t*)(rowB + x / 2);
219 for (
int right = (x2 & ~3); x < right; x += 4)
222 for (x = (x2 & ~3); x <= x2; ++x) {
223 uint8_t a = VGA16_GETPIXELINROW(rowA, x);
224 uint8_t b = VGA16_GETPIXELINROW(rowB, x);
225 VGA16_SETPIXELINROW(rowA, x, b);
226 VGA16_SETPIXELINROW(rowB, x, a);
231 void VGA16Controller::drawEllipse(Size
const & size, Rect & updateRect)
233 genericDrawEllipse(size, updateRect,
234 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
240 void VGA16Controller::clear(Rect & updateRect)
242 hideSprites(updateRect);
243 uint8_t paletteIndex = RGB888toPaletteIndex(getActualBrushColor());
244 uint8_t pattern = paletteIndex | (paletteIndex << 4);
245 for (
int y = 0; y < m_viewPortHeight; ++y)
246 memset((uint8_t*) m_viewPort[y], pattern, m_viewPortWidth / 2);
252 void VGA16Controller::VScroll(
int scroll, Rect & updateRect)
254 genericVScroll(scroll, updateRect,
255 [&] (
int yA,
int yB,
int x1,
int x2) { swapRows(yA, yB, x1, x2); },
256 [&] (
int yA,
int yB) { tswap(m_viewPort[yA], m_viewPort[yB]); },
257 [&] (
int y,
int x1,
int x2, RGB888 color) { rawFillRow(y, x1, x2, color); }
262 void VGA16Controller::HScroll(
int scroll, Rect & updateRect)
264 hideSprites(updateRect);
265 uint8_t back4 = RGB888toPaletteIndex(getActualBrushColor());
267 int Y1 = paintState().scrollingRegion.Y1;
268 int Y2 = paintState().scrollingRegion.Y2;
269 int X1 = paintState().scrollingRegion.X1;
270 int X2 = paintState().scrollingRegion.X2;
273 bool HScrolllingRegionAligned = ((
X1 & 3) == 0 && (
width & 3) == 0);
277 for (
int y =
Y1; y <=
Y2; ++y) {
278 if (HScrolllingRegionAligned) {
280 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 2;
281 for (
int s = -scroll; s > 0;) {
285 auto sz =
width & ~1;
286 memmove(row, row + sc / 2, (sz - sc) / 2);
287 rawFillRow(y,
X2 - sc + 1,
X2, back4);
295 auto w = (uint16_t *) (row +
width / 2) - 1;
296 for (
int i = 0; i <
width; i += 4) {
297 const uint16_t p4 = *w;
298 *w-- = (p4 << 4 & 0xf000) | (prev << 8 & 0x0f00) | (p4 << 4 & 0x00f0) | (p4 >> 12 & 0x000f);
299 prev = p4 >> 4 & 0x000f;
306 auto row = (uint8_t*) m_viewPort[y];
307 for (
int x =
X1; x <=
X2 + scroll; ++x)
308 VGA16_SETPIXELINROW(row, x, VGA16_GETPIXELINROW(row, x - scroll));
310 rawFillRow(y,
X2 + 1 + scroll,
X2, back4);
313 }
else if (scroll > 0) {
315 for (
int y =
Y1; y <=
Y2; ++y) {
316 if (HScrolllingRegionAligned) {
318 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 2;
319 for (
int s = scroll; s > 0;) {
323 auto sz =
width & ~1;
324 memmove(row + sc / 2, row, (sz - sc) / 2);
325 rawFillRow(y,
X1,
X1 + sc - 1, back4);
333 auto w = (uint16_t *) row;
334 for (
int i = 0; i <
width; i += 4) {
335 const uint16_t p4 = *w;
336 *w++ = (p4 << 12 & 0xf000) | (p4 >> 4 & 0x0f00) | (prev << 4) | (p4 >> 4 & 0x000f);
337 prev = p4 >> 8 & 0x000f;
344 auto row = (uint8_t*) m_viewPort[y];
345 for (
int x =
X2 - scroll; x >=
X1; --x)
346 VGA16_SETPIXELINROW(row, x + scroll, VGA16_GETPIXELINROW(row, x));
348 rawFillRow(y,
X1,
X1 + scroll - 1, back4);
356 void VGA16Controller::drawGlyph(Glyph
const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect)
358 genericDrawGlyph(glyph, glyphOptions, penColor, brushColor, updateRect,
359 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
360 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
366 void VGA16Controller::invertRect(Rect
const & rect, Rect & updateRect)
368 genericInvertRect(rect, updateRect,
369 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); }
374 void VGA16Controller::swapFGBG(Rect
const & rect, Rect & updateRect)
376 genericSwapFGBG(rect, updateRect,
377 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
378 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
387 void VGA16Controller::copyRect(Rect
const & source, Rect & updateRect)
389 genericCopyRect(source, updateRect,
390 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
398 void VGA16Controller::readScreen(Rect
const & rect, RGB888 * destBuf)
400 for (
int y = rect.Y1; y <= rect.Y2; ++y) {
401 auto row = (uint8_t*) m_viewPort[y];
402 for (
int x = rect.X1; x <= rect.X2; ++x, ++destBuf) {
403 const RGB222 v = m_palette[VGA16_GETPIXELINROW(row, x)];
404 *destBuf = RGB888(v.R * 85, v.G * 85, v.B * 85);
410 void VGA16Controller::rawDrawBitmap_Native(
int destX,
int destY, Bitmap
const * bitmap,
int X1,
int Y1,
int XCount,
int YCount)
412 genericRawDrawBitmap_Native(destX, destY, (uint8_t*) bitmap->data, bitmap->width,
X1,
Y1, XCount, YCount,
413 [&] (
int y) { return (uint8_t*) m_viewPort[y]; },
419 void VGA16Controller::rawDrawBitmap_Mask(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
421 auto foregroundColorIndex = RGB888toPaletteIndex(bitmap->foregroundColor);
422 genericRawDrawBitmap_Mask(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
423 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
425 [&] (uint8_t * row,
int x) { VGA16_SETPIXELINROW(row, x, foregroundColorIndex); }
430 void VGA16Controller::rawDrawBitmap_RGBA2222(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
432 genericRawDrawBitmap_RGBA2222(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
433 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
435 [&] (uint8_t * row,
int x, uint8_t src) { VGA16_SETPIXELINROW(row, x, RGB2222toPaletteIndex(src)); }
440 void VGA16Controller::rawDrawBitmap_RGBA8888(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
442 genericRawDrawBitmap_RGBA8888(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
443 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
444 [&] (uint8_t * row,
int x) {
return VGA16_GETPIXELINROW(row, x); },
445 [&] (uint8_t * row,
int x,
RGBA8888 const & src) { VGA16_SETPIXELINROW(row, x, RGB8888toPaletteIndex(src)); }
450 void IRAM_ATTR VGA16Controller::ISRHandler(
void * arg)
452 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 453 auto s1 = getCycleCount();
456 auto ctrl = (VGA16Controller *) arg;
458 if (I2S1.int_st.out_eof) {
460 auto const desc = (lldesc_t*) I2S1.out_eof_des_addr;
462 if (desc == s_frameResetDesc)
465 auto const width = ctrl->m_viewPortWidth;
466 auto const height = ctrl->m_viewPortHeight;
467 auto const packedPaletteIndexPair_to_signals = (uint16_t
const *) ctrl->m_packedPaletteIndexPair_to_signals;
468 auto const lines = ctrl->m_lines;
470 int scanLine = (s_scanLine + VGA16_LinesCount / 2) %
height;
472 auto lineIndex = scanLine & (VGA16_LinesCount - 1);
474 for (
int i = 0; i < VGA16_LinesCount / 2; ++i) {
476 auto src = (uint8_t
const *) s_viewPortVisible[scanLine];
477 auto dest = (uint16_t*) lines[lineIndex];
480 for (
int col = 0; col <
width; col += 16) {
482 auto src1 = *(src + 0);
483 auto src2 = *(src + 1);
484 auto src3 = *(src + 2);
485 auto src4 = *(src + 3);
486 auto src5 = *(src + 4);
487 auto src6 = *(src + 5);
488 auto src7 = *(src + 6);
489 auto src8 = *(src + 7);
493 auto v1 = packedPaletteIndexPair_to_signals[src1];
494 auto v2 = packedPaletteIndexPair_to_signals[src2];
495 auto v3 = packedPaletteIndexPair_to_signals[src3];
496 auto v4 = packedPaletteIndexPair_to_signals[src4];
497 auto v5 = packedPaletteIndexPair_to_signals[src5];
498 auto v6 = packedPaletteIndexPair_to_signals[src6];
499 auto v7 = packedPaletteIndexPair_to_signals[src7];
500 auto v8 = packedPaletteIndexPair_to_signals[src8];
520 s_scanLine += VGA16_LinesCount / 2;
522 if (scanLine >=
height && !ctrl->m_primitiveProcessingSuspended && spi_flash_cache_enabled() && ctrl->m_primitiveExecTask) {
525 vTaskNotifyGiveFromISR(ctrl->m_primitiveExecTask, NULL);
530 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 531 s_vgapalctrlcycles += getCycleCount() - s1;
534 I2S1.int_clr.val = I2S1.int_st.val;
Represents a 24 bit RGB color.
This file contains fabgl::VGA16Controller definition.
This file contains fabgl::GPIOStream definition.
Color
This enum defines named colors.
void setPaletteItem(int index, RGB888 const &color)
Determines color of specified palette item.
This file contains some utility classes and functions.
NativePixelFormat
This enum defines the display controller native pixel format.