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" 50 #pragma GCC optimize ("O2") 65 static inline __attribute__((always_inline))
void VGA8_SETPIXELINROW(uint8_t * row,
int x,
int value) {
66 uint32_t * bits24 = (uint32_t*)(row + (x >> 3) * 3);
67 int shift = 21 - (x & 7) * 3;
68 *bits24 ^= ((value << shift) ^ *bits24) & (7 << shift);
71 static inline __attribute__((always_inline))
int VGA8_GETPIXELINROW(uint8_t * row,
int x) {
72 uint32_t * bits24 = (uint32_t*)(row + (x >> 3) * 3);
73 int shift = 21 - (x & 7) * 3;
74 return (*bits24 >> shift) & 7;
77 #define VGA8_INVERTPIXELINROW(row, x) *((uint32_t*)(row + ((x) >> 3) * 3)) ^= 7 << (21 - ((x) & 7) * 3) 79 static inline __attribute__((always_inline))
void VGA8_SETPIXEL(
int x,
int y,
int value) {
80 auto row = (uint8_t*) VGA8Controller::sgetScanline(y);
81 uint32_t * bits24 = (uint32_t*)(row + (x >> 3) * 3);
82 int shift = 21 - (x & 7) * 3;
83 *bits24 ^= ((value << shift) ^ *bits24) & (7 << shift);
86 #define VGA8_GETPIXEL(x, y) VGA8_GETPIXELINROW((uint8_t*)VGA8Controller::s_viewPort[(y)], (x)) 88 #define VGA8_INVERT_PIXEL(x, y) VGA8_INVERTPIXELINROW((uint8_t*)VGA8Controller::s_viewPort[(y)], (x)) 97 VGA8Controller * VGA8Controller::s_instance =
nullptr;
102 VGA8Controller::VGA8Controller()
106 m_packedPaletteIndexPair_to_signals = (uint16_t *) heap_caps_malloc(256 *
sizeof(uint16_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
110 VGA8Controller::~VGA8Controller()
112 heap_caps_free((
void *)m_packedPaletteIndexPair_to_signals);
116 void VGA8Controller::setupDefaultPalette()
132 m_palette[index] = color;
133 auto packed222 = RGB888toPackedRGB222(color);
134 for (
int i = 0; i < 8; ++i) {
135 m_packedPaletteIndexPair_to_signals[(index << 3) | i] &= 0xFF00;
136 m_packedPaletteIndexPair_to_signals[(index << 3) | i] |= (m_HVSync | packed222);
137 m_packedPaletteIndexPair_to_signals[(i << 3) | index] &= 0x00FF;
138 m_packedPaletteIndexPair_to_signals[(i << 3) | index] |= (m_HVSync | packed222) << 8;
143 void VGA8Controller::setPixelAt(PixelDesc
const & pixelDesc,
Rect & updateRect)
145 genericSetPixelAt(pixelDesc, updateRect,
146 [&] (
RGB888 const & color) {
return RGB888toPaletteIndex(color); },
154 void VGA8Controller::absDrawLine(
int X1,
int Y1,
int X2,
int Y2, RGB888 color)
156 genericAbsDrawLine(
X1,
Y1,
X2,
Y2, color,
157 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
158 [&] (
int Y,
int X1,
int X2, uint8_t colorIndex) { rawFillRow(
Y,
X1,
X2, colorIndex); },
159 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); },
161 [&] (
int X,
int Y) { VGA8_INVERT_PIXEL(
X,
Y); }
167 void VGA8Controller::rawFillRow(
int y,
int x1,
int x2, RGB888 color)
169 rawFillRow(y, x1, x2, RGB888toPaletteIndex(color));
174 void VGA8Controller::rawFillRow(
int y,
int x1,
int x2, uint8_t colorIndex)
176 uint8_t * row = (uint8_t*) m_viewPort[y];
177 for (; x1 <= x2; ++x1)
178 VGA8_SETPIXELINROW(row, x1, colorIndex);
183 void VGA8Controller::rawInvertRow(
int y,
int x1,
int x2)
185 auto row = m_viewPort[y];
186 for (
int x = x1; x <= x2; ++x)
187 VGA8_INVERTPIXELINROW(row, x);
191 void VGA8Controller::rawCopyRow(
int x1,
int x2,
int srcY,
int dstY)
193 auto srcRow = (uint8_t*) m_viewPort[srcY];
194 auto dstRow = (uint8_t*) m_viewPort[dstY];
195 for (; x1 <= x2; ++x1)
196 VGA8_SETPIXELINROW(dstRow, x1, VGA8_GETPIXELINROW(srcRow, x1));
200 void VGA8Controller::swapRows(
int yA,
int yB,
int x1,
int x2)
202 auto rowA = (uint8_t*) m_viewPort[yA];
203 auto rowB = (uint8_t*) m_viewPort[yB];
204 for (; x1 <= x2; ++x1) {
205 uint8_t a = VGA8_GETPIXELINROW(rowA, x1);
206 uint8_t b = VGA8_GETPIXELINROW(rowB, x1);
207 VGA8_SETPIXELINROW(rowA, x1, b);
208 VGA8_SETPIXELINROW(rowB, x1, a);
213 void VGA8Controller::drawEllipse(Size
const & size, Rect & updateRect)
215 genericDrawEllipse(size, updateRect,
216 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
222 void VGA8Controller::clear(Rect & updateRect)
224 hideSprites(updateRect);
225 uint8_t paletteIndex = RGB888toPaletteIndex(getActualBrushColor());
226 uint32_t pattern8 = (paletteIndex) | (paletteIndex << 3) | (paletteIndex << 6) | (paletteIndex << 9) | (paletteIndex << 12) | (paletteIndex << 15) | (paletteIndex << 18) | (paletteIndex << 21);
227 for (
int y = 0; y < m_viewPortHeight; ++y) {
228 auto dest = (uint8_t*) m_viewPort[y];
229 for (
int x = 0; x < m_viewPortWidth; x += 8, dest += 3)
230 *((uint32_t*)dest) = (*((uint32_t*)dest) & 0xFF000000) | pattern8;
237 void VGA8Controller::VScroll(
int scroll, Rect & updateRect)
239 genericVScroll(scroll, updateRect,
240 [&] (
int yA,
int yB,
int x1,
int x2) { swapRows(yA, yB, x1, x2); },
241 [&] (
int yA,
int yB) { tswap(m_viewPort[yA], m_viewPort[yB]); },
242 [&] (
int y,
int x1,
int x2, RGB888 color) { rawFillRow(y, x1, x2, color); }
248 void VGA8Controller::HScroll(
int scroll, Rect & updateRect)
250 hideSprites(updateRect);
251 uint8_t back = RGB888toPaletteIndex(getActualBrushColor());
253 int Y1 = paintState().scrollingRegion.Y1;
254 int Y2 = paintState().scrollingRegion.Y2;
255 int X1 = paintState().scrollingRegion.X1;
256 int X2 = paintState().scrollingRegion.X2;
259 bool HScrolllingRegionAligned = ((
X1 & 7) == 0 && (
width & 7) == 0);
263 for (
int y =
Y1; y <=
Y2; ++y) {
264 for (
int s = -scroll; s > 0;) {
265 if (HScrolllingRegionAligned && s >= 8) {
267 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 8 * 3;
269 auto sz =
width & ~7;
270 memmove(row, row + sc / 8 * 3, (sz - sc) / 8 * 3);
271 rawFillRow(y,
X2 - sc + 1,
X2, back);
275 auto row = (uint8_t*) m_viewPort[y];
276 for (
int x =
X1; x <=
X2 - s; ++x)
277 VGA8_SETPIXELINROW(row, x, VGA8_GETPIXELINROW(row, x + s));
279 rawFillRow(y,
X2 - s + 1,
X2, back);
284 }
else if (scroll > 0) {
286 for (
int y =
Y1; y <=
Y2; ++y) {
287 for (
int s = scroll; s > 0;) {
288 if (HScrolllingRegionAligned && s >= 8) {
290 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 8 * 3;
292 auto sz =
width & ~7;
293 memmove(row + sc / 8 * 3, row, (sz - sc) / 8 * 3);
294 rawFillRow(y,
X1,
X1 + sc - 1, back);
298 auto row = (uint8_t*) m_viewPort[y];
299 for (
int x =
X2 - s; x >=
X1; --x)
300 VGA8_SETPIXELINROW(row, x + s, VGA8_GETPIXELINROW(row, x));
302 rawFillRow(y,
X1,
X1 + s - 1, back);
311 void VGA8Controller::drawGlyph(Glyph
const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect)
313 genericDrawGlyph(glyph, glyphOptions, penColor, brushColor, updateRect,
314 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
315 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
321 void VGA8Controller::invertRect(Rect
const & rect, Rect & updateRect)
323 genericInvertRect(rect, updateRect,
324 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); }
329 void VGA8Controller::swapFGBG(Rect
const & rect, Rect & updateRect)
331 genericSwapFGBG(rect, updateRect,
332 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
333 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
342 void VGA8Controller::copyRect(Rect
const & source, Rect & updateRect)
344 genericCopyRect(source, updateRect,
345 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
353 void VGA8Controller::readScreen(Rect
const & rect, RGB888 * destBuf)
355 for (
int y = rect.Y1; y <= rect.Y2; ++y) {
356 auto row = (uint8_t*) m_viewPort[y];
357 for (
int x = rect.X1; x <= rect.X2; ++x, ++destBuf) {
358 const RGB222 v = m_palette[VGA8_GETPIXELINROW(row, x)];
359 *destBuf = RGB888(v.R * 85, v.G * 85, v.B * 85);
365 void VGA8Controller::rawDrawBitmap_Native(
int destX,
int destY, Bitmap
const * bitmap,
int X1,
int Y1,
int XCount,
int YCount)
367 genericRawDrawBitmap_Native(destX, destY, (uint8_t*) bitmap->data, bitmap->width,
X1,
Y1, XCount, YCount,
368 [&] (
int y) { return (uint8_t*) m_viewPort[y]; },
374 void VGA8Controller::rawDrawBitmap_Mask(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
376 auto foregroundColorIndex = RGB888toPaletteIndex(bitmap->foregroundColor);
377 genericRawDrawBitmap_Mask(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
378 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
380 [&] (uint8_t * row,
int x) { VGA8_SETPIXELINROW(row, x, foregroundColorIndex); }
385 void VGA8Controller::rawDrawBitmap_RGBA2222(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
387 genericRawDrawBitmap_RGBA2222(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
388 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
390 [&] (uint8_t * row,
int x, uint8_t src) { VGA8_SETPIXELINROW(row, x, RGB2222toPaletteIndex(src)); }
395 void VGA8Controller::rawDrawBitmap_RGBA8888(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
397 genericRawDrawBitmap_RGBA8888(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
398 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
399 [&] (uint8_t * row,
int x) {
return VGA8_GETPIXELINROW(row, x); },
400 [&] (uint8_t * row,
int x,
RGBA8888 const & src) { VGA8_SETPIXELINROW(row, x, RGB8888toPaletteIndex(src)); }
405 void VGA8Controller::directSetPixel(
int x,
int y,
int value)
407 VGA8_SETPIXEL(x, y, value);
411 void IRAM_ATTR VGA8Controller::ISRHandler(
void * arg)
413 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 414 auto s1 = getCycleCount();
417 auto ctrl = (VGA8Controller *) arg;
419 if (I2S1.int_st.out_eof) {
421 auto const desc = (lldesc_t*) I2S1.out_eof_des_addr;
423 if (desc == s_frameResetDesc)
426 auto const width = ctrl->m_viewPortWidth;
427 auto const height = ctrl->m_viewPortHeight;
428 auto const packedPaletteIndexPair_to_signals = (uint16_t
const *) ctrl->m_packedPaletteIndexPair_to_signals;
429 auto const lines = ctrl->m_lines;
431 int scanLine = (s_scanLine + VGA8_LinesCount / 2) %
height;
433 auto lineIndex = scanLine & (VGA8_LinesCount - 1);
435 for (
int i = 0; i < VGA8_LinesCount / 2; ++i) {
437 auto src = (uint8_t
const *) s_viewPortVisible[scanLine];
438 auto dest = (uint16_t*) lines[lineIndex];
441 for (
int col = 0; col <
width; col += 16) {
443 auto w1 = *((uint16_t*)(src ));
444 auto w2 = *((uint16_t*)(src + 2));
445 auto w3 = *((uint16_t*)(src + 4));
449 auto src1 = w1 | (w2 << 16);
450 auto src2 = (w2 >> 8) | (w3 << 8);
452 auto v1 = packedPaletteIndexPair_to_signals[(src1 ) & 0x3f];
453 auto v2 = packedPaletteIndexPair_to_signals[(src1 >> 6) & 0x3f];
454 auto v3 = packedPaletteIndexPair_to_signals[(src1 >> 12) & 0x3f];
455 auto v4 = packedPaletteIndexPair_to_signals[(src1 >> 18) & 0x3f];
456 auto v5 = packedPaletteIndexPair_to_signals[(src2 ) & 0x3f];
457 auto v6 = packedPaletteIndexPair_to_signals[(src2 >> 6) & 0x3f];
458 auto v7 = packedPaletteIndexPair_to_signals[(src2 >> 12) & 0x3f];
459 auto v8 = packedPaletteIndexPair_to_signals[(src2 >> 18) & 0x3f];
479 s_scanLine += VGA8_LinesCount / 2;
481 if (scanLine >=
height && !ctrl->m_primitiveProcessingSuspended && spi_flash_cache_enabled() && ctrl->m_primitiveExecTask) {
484 vTaskNotifyGiveFromISR(ctrl->m_primitiveExecTask, NULL);
489 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 490 s_vgapalctrlcycles += getCycleCount() - s1;
493 I2S1.int_clr.val = I2S1.int_st.val;
Represents a 24 bit RGB color.
This file contains fabgl::GPIOStream definition.
This file contains some utility classes and functions.
void setPaletteItem(int index, RGB888 const &color)
Determines color of specified palette item.
NativePixelFormat
This enum defines the display controller native pixel format.
This file contains fabgl::VGA8Controller definition.