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") 58 static inline __attribute__((always_inline))
void VGA2_SETPIXELINROW(uint8_t * row,
int x,
int value) {
60 row[brow] ^= (-value ^ row[brow]) & (0x80 >> (x & 7));
63 static 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)) 70 static 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)) 87 VGA2Controller * VGA2Controller::s_instance =
nullptr;
91 VGA2Controller::VGA2Controller()
95 m_packedPaletteIndexOctet_to_signals = (uint64_t *) heap_caps_malloc(256 *
sizeof(uint64_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
99 VGA2Controller::~VGA2Controller()
101 heap_caps_free((
void *)m_packedPaletteIndexOctet_to_signals);
105 void VGA2Controller::setupDefaultPalette()
115 m_palette[index] = color;
116 auto packed222 = RGB888toPackedRGB222(color);
117 for (
int i = 0; i < 256; ++i) {
118 auto b = (uint8_t *) (m_packedPaletteIndexOctet_to_signals + i);
119 for (
int j = 0; j < 8; ++j) {
121 if ((index == 0 && ((1 << aj) & i) == 0) || (index == 1 && ((1 << aj) & i) != 0)) {
122 b[j ^ 2] = m_HVSync | packed222;
129 void VGA2Controller::setPixelAt(PixelDesc
const & pixelDesc,
Rect & updateRect)
131 genericSetPixelAt(pixelDesc, updateRect,
132 [&] (
RGB888 const & color) {
return RGB888toPaletteIndex(color); },
140 void VGA2Controller::absDrawLine(
int X1,
int Y1,
int X2,
int Y2, RGB888 color)
142 genericAbsDrawLine(
X1,
Y1,
X2,
Y2, color,
143 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
144 [&] (
int Y,
int X1,
int X2, uint8_t colorIndex) { rawFillRow(
Y,
X1,
X2, colorIndex); },
145 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); },
147 [&] (
int X,
int Y) { VGA2_INVERT_PIXEL(
X,
Y); }
153 void VGA2Controller::rawFillRow(
int y,
int x1,
int x2, RGB888 color)
155 rawFillRow(y, x1, x2, RGB888toPaletteIndex(color));
160 void VGA2Controller::rawFillRow(
int y,
int x1,
int x2, uint8_t colorIndex)
162 uint8_t * row = (uint8_t*) m_viewPort[y];
165 for (; x <= x2 && (x & 7) != 0; ++x) {
166 VGA2_SETPIXELINROW(row, x, colorIndex);
170 int sz = (x2 & ~7) - x;
171 memset((
void*)(row + x / 8), colorIndex ? 0xFF : 0x00, sz / 8);
175 for (; x <= x2; ++x) {
176 VGA2_SETPIXELINROW(row, x, colorIndex);
182 void VGA2Controller::rawInvertRow(
int y,
int x1,
int x2)
184 auto row = m_viewPort[y];
185 for (
int x = x1; x <= x2; ++x)
186 VGA2_INVERTPIXELINROW(row, x);
190 void VGA2Controller::rawCopyRow(
int x1,
int x2,
int srcY,
int dstY)
192 auto srcRow = (uint8_t*) m_viewPort[srcY];
193 auto dstRow = (uint8_t*) m_viewPort[dstY];
196 for (; x <= x2 && (x & 7) != 0; ++x) {
197 VGA2_SETPIXELINROW(dstRow, x, VGA2_GETPIXELINROW(srcRow, x));
200 auto src = (uint8_t*)(srcRow + x / 8);
201 auto dst = (uint8_t*)(dstRow + x / 8);
202 for (
int right = (x2 & ~7); x < right; x += 8)
205 for (x = (x2 & ~7); x <= x2; ++x) {
206 VGA2_SETPIXELINROW(dstRow, x, VGA2_GETPIXELINROW(srcRow, x));
211 void VGA2Controller::swapRows(
int yA,
int yB,
int x1,
int x2)
213 auto rowA = (uint8_t*) m_viewPort[yA];
214 auto rowB = (uint8_t*) m_viewPort[yB];
217 for (; x <= x2 && (x & 7) != 0; ++x) {
218 uint8_t a = VGA2_GETPIXELINROW(rowA, x);
219 uint8_t b = VGA2_GETPIXELINROW(rowB, x);
220 VGA2_SETPIXELINROW(rowA, x, b);
221 VGA2_SETPIXELINROW(rowB, x, a);
224 auto a = (uint8_t*)(rowA + x / 8);
225 auto b = (uint8_t*)(rowB + x / 8);
226 for (
int right = (x2 & ~7); x < right; x += 8)
229 for (x = (x2 & ~7); x <= x2; ++x) {
230 uint8_t a = VGA2_GETPIXELINROW(rowA, x);
231 uint8_t b = VGA2_GETPIXELINROW(rowB, x);
232 VGA2_SETPIXELINROW(rowA, x, b);
233 VGA2_SETPIXELINROW(rowB, x, a);
238 void VGA2Controller::drawEllipse(Size
const & size, Rect & updateRect)
240 genericDrawEllipse(size, updateRect,
241 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
247 void VGA2Controller::clear(Rect & updateRect)
249 hideSprites(updateRect);
250 uint8_t paletteIndex = RGB888toPaletteIndex(getActualBrushColor());
251 uint8_t pattern8 = paletteIndex ? 0xFF : 0x00;
252 for (
int y = 0; y < m_viewPortHeight; ++y)
253 memset((uint8_t*) m_viewPort[y], pattern8, m_viewPortWidth / 8);
259 void VGA2Controller::VScroll(
int scroll, Rect & updateRect)
261 genericVScroll(scroll, updateRect,
262 [&] (
int yA,
int yB,
int x1,
int x2) { swapRows(yA, yB, x1, x2); },
263 [&] (
int yA,
int yB) { tswap(m_viewPort[yA], m_viewPort[yB]); },
264 [&] (
int y,
int x1,
int x2, RGB888 color) { rawFillRow(y, x1, x2, color); }
269 void VGA2Controller::HScroll(
int scroll, Rect & updateRect)
271 hideSprites(updateRect);
272 uint8_t back = RGB888toPaletteIndex(getActualBrushColor());
273 uint8_t back8 = back ? 0xFF : 0x00;
275 int Y1 = paintState().scrollingRegion.Y1;
276 int Y2 = paintState().scrollingRegion.Y2;
277 int X1 = paintState().scrollingRegion.X1;
278 int X2 = paintState().scrollingRegion.X2;
281 bool HScrolllingRegionAligned = ((
X1 & 7) == 0 && (
width & 7) == 0);
285 for (
int y =
Y1; y <=
Y2; ++y) {
286 if (HScrolllingRegionAligned) {
288 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 8;
289 for (
int s = -scroll; s > 0;) {
293 uint8_t prev = back8;
294 for (
int i = sz - 1; i >= 0; --i) {
295 uint8_t lowbits = prev >> (8 - s);
297 row[i] = (row[i] << s) | lowbits;
303 auto sz =
width & ~7;
304 memmove(row, row + sc / 8, (sz - sc) / 8);
305 rawFillRow(y,
X2 - sc + 1,
X2, back);
311 auto row = (uint8_t*) m_viewPort[y];
312 for (
int x =
X1; x <=
X2 + scroll; ++x)
313 VGA2_SETPIXELINROW(row, x, VGA2_GETPIXELINROW(row, x - scroll));
315 rawFillRow(y,
X2 + 1 + scroll,
X2, back);
318 }
else if (scroll > 0) {
320 for (
int y =
Y1; y <=
Y2; ++y) {
321 if (HScrolllingRegionAligned) {
323 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 8;
324 for (
int s = scroll; s > 0;) {
328 uint8_t prev = back8;
329 for (
int i = 0; i < sz; ++i) {
330 uint8_t highbits = prev << (8 - s);
332 row[i] = (row[i] >> s) | highbits;
338 auto sz =
width & ~7;
339 memmove(row + sc / 8, row, (sz - sc) / 8);
340 rawFillRow(y,
X1,
X1 + sc - 1, back);
346 auto row = (uint8_t*) m_viewPort[y];
347 for (
int x =
X2 - scroll; x >=
X1; --x)
348 VGA2_SETPIXELINROW(row, x + scroll, VGA2_GETPIXELINROW(row, x));
350 rawFillRow(y,
X1,
X1 + scroll - 1, back);
358 void VGA2Controller::drawGlyph(Glyph
const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect)
360 genericDrawGlyph(glyph, glyphOptions, penColor, brushColor, updateRect,
361 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
362 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
368 void VGA2Controller::invertRect(Rect
const & rect, Rect & updateRect)
370 genericInvertRect(rect, updateRect,
371 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); }
376 void VGA2Controller::swapFGBG(Rect
const & rect, Rect & updateRect)
378 genericSwapFGBG(rect, updateRect,
379 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
380 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
389 void VGA2Controller::copyRect(Rect
const & source, Rect & updateRect)
391 genericCopyRect(source, updateRect,
392 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
400 void VGA2Controller::readScreen(Rect
const & rect, RGB888 * destBuf)
402 for (
int y = rect.Y1; y <= rect.Y2; ++y) {
403 auto row = (uint8_t*) m_viewPort[y];
404 for (
int x = rect.X1; x <= rect.X2; ++x, ++destBuf) {
405 const RGB222 v = m_palette[VGA2_GETPIXELINROW(row, x)];
406 *destBuf = RGB888(v.R * 85, v.G * 85, v.B * 85);
412 void VGA2Controller::rawDrawBitmap_Native(
int destX,
int destY, Bitmap
const * bitmap,
int X1,
int Y1,
int XCount,
int YCount)
414 genericRawDrawBitmap_Native(destX, destY, (uint8_t*) bitmap->data, bitmap->width,
X1,
Y1, XCount, YCount,
415 [&] (
int y) { return (uint8_t*) m_viewPort[y]; },
421 void VGA2Controller::rawDrawBitmap_Mask(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
423 auto foregroundColorIndex = RGB888toPaletteIndex(bitmap->foregroundColor);
424 genericRawDrawBitmap_Mask(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
425 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
427 [&] (uint8_t * row,
int x) { VGA2_SETPIXELINROW(row, x, foregroundColorIndex); }
432 void VGA2Controller::rawDrawBitmap_RGBA2222(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
434 genericRawDrawBitmap_RGBA2222(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
435 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
437 [&] (uint8_t * row,
int x, uint8_t src) { VGA2_SETPIXELINROW(row, x, RGB2222toPaletteIndex(src)); }
442 void VGA2Controller::rawDrawBitmap_RGBA8888(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
444 genericRawDrawBitmap_RGBA8888(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
445 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
446 [&] (uint8_t * row,
int x) {
return VGA2_GETPIXELINROW(row, x); },
447 [&] (uint8_t * row,
int x,
RGBA8888 const & src) { VGA2_SETPIXELINROW(row, x, RGB8888toPaletteIndex(src)); }
452 void IRAM_ATTR VGA2Controller::ISRHandler(
void * arg)
454 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 455 auto s1 = getCycleCount();
458 auto ctrl = (VGA2Controller *) arg;
460 if (I2S1.int_st.out_eof) {
462 auto const desc = (lldesc_t*) I2S1.out_eof_des_addr;
464 if (desc == s_frameResetDesc)
467 auto const width = ctrl->m_viewPortWidth;
468 auto const height = ctrl->m_viewPortHeight;
469 auto const packedPaletteIndexOctet_to_signals = (uint64_t
const *) ctrl->m_packedPaletteIndexOctet_to_signals;
470 auto const lines = ctrl->m_lines;
472 int scanLine = (s_scanLine + VGA2_LinesCount / 2) %
height;
474 auto lineIndex = scanLine & (VGA2_LinesCount - 1);
476 for (
int i = 0; i < VGA2_LinesCount / 2; ++i) {
478 auto src = (uint8_t
const *) s_viewPortVisible[scanLine];
479 auto dest = (uint64_t*) lines[lineIndex];
482 for (
int col = 0; col <
width; col += 16) {
484 auto src1 = *(src + 0);
485 auto src2 = *(src + 1);
489 auto v1 = packedPaletteIndexOctet_to_signals[src1];
490 auto v2 = packedPaletteIndexOctet_to_signals[src2];
504 s_scanLine += VGA2_LinesCount / 2;
506 if (scanLine >=
height && !ctrl->m_primitiveProcessingSuspended && spi_flash_cache_enabled() && ctrl->m_primitiveExecTask) {
509 vTaskNotifyGiveFromISR(ctrl->m_primitiveExecTask, NULL);
514 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 515 s_vgapalctrlcycles += getCycleCount() - s1;
518 I2S1.int_clr.val = I2S1.int_st.val;
Represents a 24 bit RGB color.
This file contains fabgl::GPIOStream definition.
void setPaletteItem(int index, RGB888 const &color)
Determines color of specified palette item.
This file contains fabgl::VGA2Controller definition.
This file contains some utility classes and functions.
NativePixelFormat
This enum defines the display controller native pixel format.