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" 55 static inline __attribute__((always_inline))
void VGA4_SETPIXELINROW(uint8_t * row,
int x,
int value) {
57 int shift = 6 - (x & 3) * 2;
58 row[brow] ^= ((value << shift) ^ row[brow]) & (3 << shift);
61 static inline __attribute__((always_inline))
int VGA4_GETPIXELINROW(uint8_t * row,
int x) {
63 int shift = 6 - (x & 3) * 2;
64 return (row[brow] >> shift) & 3;
67 #define VGA4_INVERTPIXELINROW(row, x) (row)[(x) >> 2] ^= 3 << (6 - ((x) & 3) * 2) 69 static inline __attribute__((always_inline))
void VGA4_SETPIXEL(
int x,
int y,
int value) {
70 auto row = (uint8_t*) VGA4Controller::sgetScanline(y);
72 int shift = 6 - (x & 3) * 2;
73 row[brow] ^= ((value << shift) ^ row[brow]) & (3 << shift);
76 #define VGA4_GETPIXEL(x, y) VGA4_GETPIXELINROW((uint8_t*)VGA4Controller::s_viewPort[(y)], (x)) 78 #define VGA4_INVERT_PIXEL(x, y) VGA4_INVERTPIXELINROW((uint8_t*)VGA4Controller::s_viewPort[(y)], (x)) 87 VGA4Controller * VGA4Controller::s_instance =
nullptr;
91 VGA4Controller::VGA4Controller()
95 m_packedPaletteIndexQuad_to_signals = (uint32_t *) heap_caps_malloc(256 *
sizeof(uint32_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
99 VGA4Controller::~VGA4Controller()
101 heap_caps_free((
void *)m_packedPaletteIndexQuad_to_signals);
105 void VGA4Controller::setupDefaultPalette()
117 m_palette[index] = color;
118 auto packed222 = RGB888toPackedRGB222(color);
119 for (
int i = 0; i < 256; ++i) {
120 auto b = (uint8_t *) (m_packedPaletteIndexQuad_to_signals + i);
121 for (
int j = 0; j < 4; ++j) {
123 if (((i >> aj) & 3) == index) {
124 b[j ^ 2] = m_HVSync | packed222;
131 void VGA4Controller::setPixelAt(PixelDesc
const & pixelDesc,
Rect & updateRect)
133 genericSetPixelAt(pixelDesc, updateRect,
134 [&] (
RGB888 const & color) {
return RGB888toPaletteIndex(color); },
142 void VGA4Controller::absDrawLine(
int X1,
int Y1,
int X2,
int Y2, RGB888 color)
144 genericAbsDrawLine(
X1,
Y1,
X2,
Y2, color,
145 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
146 [&] (
int Y,
int X1,
int X2, uint8_t colorIndex) { rawFillRow(
Y,
X1,
X2, colorIndex); },
147 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); },
149 [&] (
int X,
int Y) { VGA4_INVERT_PIXEL(
X,
Y); }
155 void VGA4Controller::rawFillRow(
int y,
int x1,
int x2, RGB888 color)
157 rawFillRow(y, x1, x2, RGB888toPaletteIndex(color));
162 void VGA4Controller::rawFillRow(
int y,
int x1,
int x2, uint8_t colorIndex)
164 uint8_t * row = (uint8_t*) m_viewPort[y];
167 for (; x <= x2 && (x & 3) != 0; ++x) {
168 VGA4_SETPIXELINROW(row, x, colorIndex);
172 int sz = (x2 & ~3) - x;
173 memset((
void*)(row + x / 4), colorIndex | (colorIndex << 2) | (colorIndex << 4) | (colorIndex << 6), sz / 4);
177 for (; x <= x2; ++x) {
178 VGA4_SETPIXELINROW(row, x, colorIndex);
184 void VGA4Controller::rawInvertRow(
int y,
int x1,
int x2)
186 auto row = m_viewPort[y];
187 for (
int x = x1; x <= x2; ++x)
188 VGA4_INVERTPIXELINROW(row, x);
192 void VGA4Controller::rawCopyRow(
int x1,
int x2,
int srcY,
int dstY)
194 auto srcRow = (uint8_t*) m_viewPort[srcY];
195 auto dstRow = (uint8_t*) m_viewPort[dstY];
198 for (; x <= x2 && (x & 3) != 0; ++x) {
199 VGA4_SETPIXELINROW(dstRow, x, VGA4_GETPIXELINROW(srcRow, x));
202 auto src = (uint8_t*)(srcRow + x / 4);
203 auto dst = (uint8_t*)(dstRow + x / 4);
204 for (
int right = (x2 & ~3); x < right; x += 4)
207 for (x = (x2 & ~3); x <= x2; ++x) {
208 VGA4_SETPIXELINROW(dstRow, x, VGA4_GETPIXELINROW(srcRow, x));
213 void VGA4Controller::swapRows(
int yA,
int yB,
int x1,
int x2)
215 auto rowA = (uint8_t*) m_viewPort[yA];
216 auto rowB = (uint8_t*) m_viewPort[yB];
219 for (; x <= x2 && (x & 3) != 0; ++x) {
220 uint8_t a = VGA4_GETPIXELINROW(rowA, x);
221 uint8_t b = VGA4_GETPIXELINROW(rowB, x);
222 VGA4_SETPIXELINROW(rowA, x, b);
223 VGA4_SETPIXELINROW(rowB, x, a);
226 auto a = (uint8_t*)(rowA + x / 4);
227 auto b = (uint8_t*)(rowB + x / 4);
228 for (
int right = (x2 & ~3); x < right; x += 4)
231 for (x = (x2 & ~3); x <= x2; ++x) {
232 uint8_t a = VGA4_GETPIXELINROW(rowA, x);
233 uint8_t b = VGA4_GETPIXELINROW(rowB, x);
234 VGA4_SETPIXELINROW(rowA, x, b);
235 VGA4_SETPIXELINROW(rowB, x, a);
240 void VGA4Controller::drawEllipse(Size
const & size, Rect & updateRect)
242 genericDrawEllipse(size, updateRect,
243 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
249 void VGA4Controller::clear(Rect & updateRect)
251 hideSprites(updateRect);
252 uint8_t paletteIndex = RGB888toPaletteIndex(getActualBrushColor());
253 uint8_t pattern4 = paletteIndex | (paletteIndex << 2) | (paletteIndex << 4) | (paletteIndex << 6);
254 for (
int y = 0; y < m_viewPortHeight; ++y)
255 memset((uint8_t*) m_viewPort[y], pattern4, m_viewPortWidth / 4);
261 void VGA4Controller::VScroll(
int scroll, Rect & updateRect)
263 genericVScroll(scroll, updateRect,
264 [&] (
int yA,
int yB,
int x1,
int x2) { swapRows(yA, yB, x1, x2); },
265 [&] (
int yA,
int yB) { tswap(m_viewPort[yA], m_viewPort[yB]); },
266 [&] (
int y,
int x1,
int x2, RGB888 color) { rawFillRow(y, x1, x2, color); }
271 void VGA4Controller::HScroll(
int scroll, Rect & updateRect)
273 hideSprites(updateRect);
274 uint8_t back = RGB888toPaletteIndex(getActualBrushColor());
275 uint8_t back4 = back | (back << 2) | (back << 4) | (back << 6);
277 int Y1 = paintState().scrollingRegion.Y1;
278 int Y2 = paintState().scrollingRegion.Y2;
279 int X1 = paintState().scrollingRegion.X1;
280 int X2 = paintState().scrollingRegion.X2;
283 bool HScrolllingRegionAligned = ((
X1 & 3) == 0 && (
width & 3) == 0);
287 for (
int y =
Y1; y <=
Y2; ++y) {
288 if (HScrolllingRegionAligned) {
290 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 4;
291 for (
int s = -scroll; s > 0;) {
295 uint8_t prev = back4;
296 for (
int i = sz - 1; i >= 0; --i) {
297 uint8_t lowbits = prev >> (8 - s * 2);
299 row[i] = (row[i] << (s * 2)) | lowbits;
305 auto sz =
width & ~3;
306 memmove(row, row + sc / 4, (sz - sc) / 4);
307 rawFillRow(y,
X2 - sc + 1,
X2, back);
313 auto row = (uint8_t*) m_viewPort[y];
314 for (
int x =
X1; x <=
X2 + scroll; ++x)
315 VGA4_SETPIXELINROW(row, x, VGA4_GETPIXELINROW(row, x - scroll));
317 rawFillRow(y,
X2 + 1 + scroll,
X2, back);
320 }
else if (scroll > 0) {
322 for (
int y =
Y1; y <=
Y2; ++y) {
323 if (HScrolllingRegionAligned) {
325 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 4;
326 for (
int s = scroll; s > 0;) {
330 uint8_t prev = back4;
331 for (
int i = 0; i < sz; ++i) {
332 uint8_t highbits = prev << (8 - s * 2);
334 row[i] = (row[i] >> (s * 2)) | highbits;
340 auto sz =
width & ~3;
341 memmove(row + sc / 4, row, (sz - sc) / 4);
342 rawFillRow(y,
X1,
X1 + sc - 1, back);
348 auto row = (uint8_t*) m_viewPort[y];
349 for (
int x =
X2 - scroll; x >=
X1; --x)
350 VGA4_SETPIXELINROW(row, x + scroll, VGA4_GETPIXELINROW(row, x));
352 rawFillRow(y,
X1,
X1 + scroll - 1, back);
360 void VGA4Controller::drawGlyph(Glyph
const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect)
362 genericDrawGlyph(glyph, glyphOptions, penColor, brushColor, updateRect,
363 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
364 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
370 void VGA4Controller::invertRect(Rect
const & rect, Rect & updateRect)
372 genericInvertRect(rect, updateRect,
373 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); }
378 void VGA4Controller::swapFGBG(Rect
const & rect, Rect & updateRect)
380 genericSwapFGBG(rect, updateRect,
381 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
382 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
391 void VGA4Controller::copyRect(Rect
const & source, Rect & updateRect)
393 genericCopyRect(source, updateRect,
394 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
402 void VGA4Controller::readScreen(Rect
const & rect, RGB888 * destBuf)
404 for (
int y = rect.Y1; y <= rect.Y2; ++y) {
405 auto row = (uint8_t*) m_viewPort[y];
406 for (
int x = rect.X1; x <= rect.X2; ++x, ++destBuf) {
407 const RGB222 v = m_palette[VGA4_GETPIXELINROW(row, x)];
408 *destBuf = RGB888(v.R * 85, v.G * 85, v.B * 85);
414 void VGA4Controller::rawDrawBitmap_Native(
int destX,
int destY, Bitmap
const * bitmap,
int X1,
int Y1,
int XCount,
int YCount)
416 genericRawDrawBitmap_Native(destX, destY, (uint8_t*) bitmap->data, bitmap->width,
X1,
Y1, XCount, YCount,
417 [&] (
int y) { return (uint8_t*) m_viewPort[y]; },
423 void VGA4Controller::rawDrawBitmap_Mask(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
425 auto foregroundColorIndex = RGB888toPaletteIndex(bitmap->foregroundColor);
426 genericRawDrawBitmap_Mask(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
427 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
429 [&] (uint8_t * row,
int x) { VGA4_SETPIXELINROW(row, x, foregroundColorIndex); }
434 void VGA4Controller::rawDrawBitmap_RGBA2222(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
436 genericRawDrawBitmap_RGBA2222(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
437 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
439 [&] (uint8_t * row,
int x, uint8_t src) { VGA4_SETPIXELINROW(row, x, RGB2222toPaletteIndex(src)); }
444 void VGA4Controller::rawDrawBitmap_RGBA8888(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
446 genericRawDrawBitmap_RGBA8888(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
447 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
448 [&] (uint8_t * row,
int x) {
return VGA4_GETPIXELINROW(row, x); },
449 [&] (uint8_t * row,
int x,
RGBA8888 const & src) { VGA4_SETPIXELINROW(row, x, RGB8888toPaletteIndex(src)); }
454 #pragma GCC optimize ("O2") 457 void IRAM_ATTR VGA4Controller::ISRHandler(
void * arg)
459 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 460 auto s1 = getCycleCount();
463 auto ctrl = (VGA4Controller *) arg;
465 if (I2S1.int_st.out_eof) {
467 auto const desc = (lldesc_t*) I2S1.out_eof_des_addr;
469 if (desc == s_frameResetDesc)
472 auto const width = ctrl->m_viewPortWidth;
473 auto const height = ctrl->m_viewPortHeight;
474 auto const packedPaletteIndexQuad_to_signals = (uint32_t
const *) ctrl->m_packedPaletteIndexQuad_to_signals;
475 auto const lines = ctrl->m_lines;
477 int scanLine = (s_scanLine + VGA4_LinesCount / 2) %
height;
479 auto lineIndex = scanLine & (VGA4_LinesCount - 1);
481 for (
int i = 0; i < VGA4_LinesCount / 2; ++i) {
483 auto src = (uint8_t
const *) s_viewPortVisible[scanLine];
484 auto dest = (uint32_t*) lines[lineIndex];
487 for (
int col = 0; col <
width; col += 16) {
489 auto src1 = *(src + 0);
490 auto src2 = *(src + 1);
491 auto src3 = *(src + 2);
492 auto src4 = *(src + 3);
496 auto v1 = packedPaletteIndexQuad_to_signals[src1];
497 auto v2 = packedPaletteIndexQuad_to_signals[src2];
498 auto v3 = packedPaletteIndexQuad_to_signals[src3];
499 auto v4 = packedPaletteIndexQuad_to_signals[src4];
514 s_scanLine += VGA4_LinesCount / 2;
516 if (scanLine >=
height && !ctrl->m_primitiveProcessingSuspended && spi_flash_cache_enabled()) {
519 vTaskNotifyGiveFromISR(ctrl->m_primitiveExecTask, NULL);
524 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 525 s_vgapalctrlcycles += getCycleCount() - s1;
528 I2S1.int_clr.val = I2S1.int_st.val;
Represents a 24 bit RGB color.
This file contains fabgl::VGA4Controller definition.
This file contains fabgl::GPIOStream definition.
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.