29 #include "freertos/FreeRTOS.h" 30 #include "freertos/task.h" 32 #include "soc/i2s_struct.h" 33 #include "soc/i2s_reg.h" 34 #include "driver/periph_ctrl.h" 35 #include "rom/lldesc.h" 37 #include "esp_spi_flash.h" 38 #include "esp_heap_caps.h" 62 static inline __attribute__((always_inline))
void VGA8_SETPIXELINROW(uint8_t * row,
int x,
int value) {
63 uint32_t * bits24 = (uint32_t*)(row + (x >> 3) * 3);
64 int shift = 21 - (x & 7) * 3;
65 *bits24 ^= ((value << shift) ^ *bits24) & (7 << shift);
68 static inline __attribute__((always_inline))
int VGA8_GETPIXELINROW(uint8_t * row,
int x) {
69 uint32_t * bits24 = (uint32_t*)(row + (x >> 3) * 3);
70 int shift = 21 - (x & 7) * 3;
71 return (*bits24 >> shift) & 7;
74 #define VGA8_INVERTPIXELINROW(row, x) *((uint32_t*)(row + ((x) >> 3) * 3)) ^= 7 << (21 - ((x) & 7) * 3) 76 static inline __attribute__((always_inline))
void VGA8_SETPIXEL(
int x,
int y,
int value) {
77 auto row = (uint8_t*) VGA8Controller::sgetScanline(y);
78 uint32_t * bits24 = (uint32_t*)(row + (x >> 3) * 3);
79 int shift = 21 - (x & 7) * 3;
80 *bits24 ^= ((value << shift) ^ *bits24) & (7 << shift);
83 #define VGA8_GETPIXEL(x, y) VGA8_GETPIXELINROW((uint8_t*)VGA8Controller::s_viewPort[(y)], (x)) 85 #define VGA8_INVERT_PIXEL(x, y) VGA8_INVERTPIXELINROW((uint8_t*)VGA8Controller::s_viewPort[(y)], (x)) 94 VGA8Controller * VGA8Controller::s_instance =
nullptr;
99 VGA8Controller::VGA8Controller()
103 m_packedPaletteIndexPair_to_signals = (uint16_t *) heap_caps_malloc(256 *
sizeof(uint16_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
107 VGA8Controller::~VGA8Controller()
109 heap_caps_free((
void *)m_packedPaletteIndexPair_to_signals);
113 void VGA8Controller::setupDefaultPalette()
129 m_palette[index] = color;
130 auto packed222 = RGB888toPackedRGB222(color);
131 for (
int i = 0; i < 8; ++i) {
132 m_packedPaletteIndexPair_to_signals[(index << 3) | i] &= 0xFF00;
133 m_packedPaletteIndexPair_to_signals[(index << 3) | i] |= (m_HVSync | packed222);
134 m_packedPaletteIndexPair_to_signals[(i << 3) | index] &= 0x00FF;
135 m_packedPaletteIndexPair_to_signals[(i << 3) | index] |= (m_HVSync | packed222) << 8;
140 void VGA8Controller::setPixelAt(PixelDesc
const & pixelDesc,
Rect & updateRect)
142 genericSetPixelAt(pixelDesc, updateRect,
143 [&] (
RGB888 const & color) {
return RGB888toPaletteIndex(color); },
151 void VGA8Controller::absDrawLine(
int X1,
int Y1,
int X2,
int Y2, RGB888 color)
153 genericAbsDrawLine(
X1,
Y1,
X2,
Y2, color,
154 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
155 [&] (
int Y,
int X1,
int X2, uint8_t colorIndex) { rawFillRow(
Y,
X1,
X2, colorIndex); },
156 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); },
158 [&] (
int X,
int Y) { VGA8_INVERT_PIXEL(
X,
Y); }
164 void VGA8Controller::rawFillRow(
int y,
int x1,
int x2, RGB888 color)
166 rawFillRow(y, x1, x2, RGB888toPaletteIndex(color));
171 void VGA8Controller::rawFillRow(
int y,
int x1,
int x2, uint8_t colorIndex)
173 uint8_t * row = (uint8_t*) m_viewPort[y];
174 for (; x1 <= x2; ++x1)
175 VGA8_SETPIXELINROW(row, x1, colorIndex);
180 void VGA8Controller::rawInvertRow(
int y,
int x1,
int x2)
182 auto row = m_viewPort[y];
183 for (
int x = x1; x <= x2; ++x)
184 VGA8_INVERTPIXELINROW(row, x);
188 void VGA8Controller::rawCopyRow(
int x1,
int x2,
int srcY,
int dstY)
190 auto srcRow = (uint8_t*) m_viewPort[srcY];
191 auto dstRow = (uint8_t*) m_viewPort[dstY];
192 for (; x1 <= x2; ++x1)
193 VGA8_SETPIXELINROW(dstRow, x1, VGA8_GETPIXELINROW(srcRow, x1));
197 void VGA8Controller::swapRows(
int yA,
int yB,
int x1,
int x2)
199 auto rowA = (uint8_t*) m_viewPort[yA];
200 auto rowB = (uint8_t*) m_viewPort[yB];
201 for (; x1 <= x2; ++x1) {
202 uint8_t a = VGA8_GETPIXELINROW(rowA, x1);
203 uint8_t b = VGA8_GETPIXELINROW(rowB, x1);
204 VGA8_SETPIXELINROW(rowA, x1, b);
205 VGA8_SETPIXELINROW(rowB, x1, a);
210 void VGA8Controller::drawEllipse(Size
const & size, Rect & updateRect)
212 genericDrawEllipse(size, updateRect,
213 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
219 void VGA8Controller::clear(Rect & updateRect)
221 hideSprites(updateRect);
222 uint8_t paletteIndex = RGB888toPaletteIndex(getActualBrushColor());
223 uint32_t pattern8 = (paletteIndex) | (paletteIndex << 3) | (paletteIndex << 6) | (paletteIndex << 9) | (paletteIndex << 12) | (paletteIndex << 15) | (paletteIndex << 18) | (paletteIndex << 21);
224 for (
int y = 0; y < m_viewPortHeight; ++y) {
225 auto dest = (uint8_t*) m_viewPort[y];
226 for (
int x = 0; x < m_viewPortWidth; x += 8, dest += 3)
227 *((uint32_t*)dest) = (*((uint32_t*)dest) & 0xFF000000) | pattern8;
234 void VGA8Controller::VScroll(
int scroll, Rect & updateRect)
236 genericVScroll(scroll, updateRect,
237 [&] (
int yA,
int yB,
int x1,
int x2) { swapRows(yA, yB, x1, x2); },
238 [&] (
int yA,
int yB) { tswap(m_viewPort[yA], m_viewPort[yB]); },
239 [&] (
int y,
int x1,
int x2, RGB888 color) { rawFillRow(y, x1, x2, color); }
245 void VGA8Controller::HScroll(
int scroll, Rect & updateRect)
247 hideSprites(updateRect);
248 uint8_t back = RGB888toPaletteIndex(getActualBrushColor());
250 int Y1 = paintState().scrollingRegion.Y1;
251 int Y2 = paintState().scrollingRegion.Y2;
252 int X1 = paintState().scrollingRegion.X1;
253 int X2 = paintState().scrollingRegion.X2;
256 bool HScrolllingRegionAligned = ((
X1 & 7) == 0 && (
width & 7) == 0);
260 for (
int y =
Y1; y <=
Y2; ++y) {
261 for (
int s = -scroll; s > 0;) {
262 if (HScrolllingRegionAligned && s >= 8) {
264 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 8 * 3;
266 auto sz =
width & ~7;
267 memmove(row, row + sc / 8 * 3, (sz - sc) / 8 * 3);
268 rawFillRow(y,
X2 - sc + 1,
X2, back);
272 auto row = (uint8_t*) m_viewPort[y];
273 for (
int x =
X1; x <=
X2 - s; ++x)
274 VGA8_SETPIXELINROW(row, x, VGA8_GETPIXELINROW(row, x + s));
276 rawFillRow(y,
X2 - s + 1,
X2, back);
281 }
else if (scroll > 0) {
283 for (
int y =
Y1; y <=
Y2; ++y) {
284 for (
int s = scroll; s > 0;) {
285 if (HScrolllingRegionAligned && s >= 8) {
287 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 8 * 3;
289 auto sz =
width & ~7;
290 memmove(row + sc / 8 * 3, row, (sz - sc) / 8 * 3);
291 rawFillRow(y,
X1,
X1 + sc - 1, back);
295 auto row = (uint8_t*) m_viewPort[y];
296 for (
int x =
X2 - s; x >=
X1; --x)
297 VGA8_SETPIXELINROW(row, x + s, VGA8_GETPIXELINROW(row, x));
299 rawFillRow(y,
X1,
X1 + s - 1, back);
308 void VGA8Controller::drawGlyph(Glyph
const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect)
310 genericDrawGlyph(glyph, glyphOptions, penColor, brushColor, updateRect,
311 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
312 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
318 void VGA8Controller::invertRect(Rect
const & rect, Rect & updateRect)
320 genericInvertRect(rect, updateRect,
321 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); }
326 void VGA8Controller::swapFGBG(Rect
const & rect, Rect & updateRect)
328 genericSwapFGBG(rect, updateRect,
329 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
330 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
339 void VGA8Controller::copyRect(Rect
const & source, Rect & updateRect)
341 genericCopyRect(source, updateRect,
342 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
350 void VGA8Controller::readScreen(Rect
const & rect, RGB888 * destBuf)
352 for (
int y = rect.Y1; y <= rect.Y2; ++y) {
353 auto row = (uint8_t*) m_viewPort[y];
354 for (
int x = rect.X1; x <= rect.X2; ++x, ++destBuf) {
355 const RGB222 v = m_palette[VGA8_GETPIXELINROW(row, x)];
356 *destBuf = RGB888(v.R * 85, v.G * 85, v.B * 85);
362 void VGA8Controller::rawDrawBitmap_Native(
int destX,
int destY, Bitmap
const * bitmap,
int X1,
int Y1,
int XCount,
int YCount)
364 genericRawDrawBitmap_Native(destX, destY, (uint8_t*) bitmap->data, bitmap->width,
X1,
Y1, XCount, YCount,
365 [&] (
int y) { return (uint8_t*) m_viewPort[y]; },
371 void VGA8Controller::rawDrawBitmap_Mask(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
373 auto foregroundColorIndex = RGB888toPaletteIndex(bitmap->foregroundColor);
374 genericRawDrawBitmap_Mask(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
375 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
377 [&] (uint8_t * row,
int x) { VGA8_SETPIXELINROW(row, x, foregroundColorIndex); }
382 void VGA8Controller::rawDrawBitmap_RGBA2222(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
384 genericRawDrawBitmap_RGBA2222(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
385 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
387 [&] (uint8_t * row,
int x, uint8_t src) { VGA8_SETPIXELINROW(row, x, RGB2222toPaletteIndex(src)); }
392 void VGA8Controller::rawDrawBitmap_RGBA8888(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
394 genericRawDrawBitmap_RGBA8888(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
395 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
396 [&] (uint8_t * row,
int x) {
return VGA8_GETPIXELINROW(row, x); },
397 [&] (uint8_t * row,
int x,
RGBA8888 const & src) { VGA8_SETPIXELINROW(row, x, RGB8888toPaletteIndex(src)); }
402 #pragma GCC optimize ("O2") 405 void IRAM_ATTR VGA8Controller::ISRHandler(
void * arg)
407 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 408 auto s1 = getCycleCount();
411 auto ctrl = (VGA8Controller *) arg;
413 if (I2S1.int_st.out_eof) {
415 auto const desc = (lldesc_t*) I2S1.out_eof_des_addr;
417 if (desc == s_frameResetDesc)
420 auto const width = ctrl->m_viewPortWidth;
421 auto const height = ctrl->m_viewPortHeight;
422 auto const packedPaletteIndexPair_to_signals = (uint16_t
const *) ctrl->m_packedPaletteIndexPair_to_signals;
423 auto const lines = ctrl->m_lines;
425 int scanLine = (s_scanLine + VGA8_LinesCount / 2) %
height;
427 auto lineIndex = scanLine & (VGA8_LinesCount - 1);
429 for (
int i = 0; i < VGA8_LinesCount / 2; ++i) {
431 auto src = (uint8_t
const *) s_viewPortVisible[scanLine];
432 auto dest = (uint16_t*) lines[lineIndex];
435 for (
int col = 0; col <
width; col += 16) {
437 auto w1 = *((uint16_t*)(src ));
438 auto w2 = *((uint16_t*)(src + 2));
439 auto w3 = *((uint16_t*)(src + 4));
443 auto src1 = w1 | (w2 << 16);
444 auto src2 = (w2 >> 8) | (w3 << 8);
446 auto v1 = packedPaletteIndexPair_to_signals[(src1 ) & 0x3f];
447 auto v2 = packedPaletteIndexPair_to_signals[(src1 >> 6) & 0x3f];
448 auto v3 = packedPaletteIndexPair_to_signals[(src1 >> 12) & 0x3f];
449 auto v4 = packedPaletteIndexPair_to_signals[(src1 >> 18) & 0x3f];
450 auto v5 = packedPaletteIndexPair_to_signals[(src2 ) & 0x3f];
451 auto v6 = packedPaletteIndexPair_to_signals[(src2 >> 6) & 0x3f];
452 auto v7 = packedPaletteIndexPair_to_signals[(src2 >> 12) & 0x3f];
453 auto v8 = packedPaletteIndexPair_to_signals[(src2 >> 18) & 0x3f];
473 s_scanLine += VGA8_LinesCount / 2;
475 if (scanLine >=
height && !ctrl->m_primitiveProcessingSuspended && spi_flash_cache_enabled()) {
478 vTaskNotifyGiveFromISR(ctrl->m_primitiveExecTask, NULL);
483 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 484 s_vgapalctrlcycles += getCycleCount() - s1;
487 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.