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 VGA4_SETPIXELINROW(uint8_t * row,
int x,
int value) {
60 int shift = 6 - (x & 3) * 2;
61 row[brow] ^= ((value << shift) ^ row[brow]) & (3 << shift);
64 static inline __attribute__((always_inline))
int VGA4_GETPIXELINROW(uint8_t * row,
int x) {
66 int shift = 6 - (x & 3) * 2;
67 return (row[brow] >> shift) & 3;
70 #define VGA4_INVERTPIXELINROW(row, x) (row)[(x) >> 2] ^= 3 << (6 - ((x) & 3) * 2) 72 static inline __attribute__((always_inline))
void VGA4_SETPIXEL(
int x,
int y,
int value) {
73 auto row = (uint8_t*) VGA4Controller::sgetScanline(y);
75 int shift = 6 - (x & 3) * 2;
76 row[brow] ^= ((value << shift) ^ row[brow]) & (3 << shift);
79 #define VGA4_GETPIXEL(x, y) VGA4_GETPIXELINROW((uint8_t*)VGA4Controller::s_viewPort[(y)], (x)) 81 #define VGA4_INVERT_PIXEL(x, y) VGA4_INVERTPIXELINROW((uint8_t*)VGA4Controller::s_viewPort[(y)], (x)) 90 VGA4Controller * VGA4Controller::s_instance =
nullptr;
94 VGA4Controller::VGA4Controller()
98 m_packedPaletteIndexQuad_to_signals = (uint32_t *) heap_caps_malloc(256 *
sizeof(uint32_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
102 VGA4Controller::~VGA4Controller()
104 heap_caps_free((
void *)m_packedPaletteIndexQuad_to_signals);
108 void VGA4Controller::setupDefaultPalette()
120 m_palette[index] = color;
121 auto packed222 = RGB888toPackedRGB222(color);
122 for (
int i = 0; i < 256; ++i) {
123 auto b = (uint8_t *) (m_packedPaletteIndexQuad_to_signals + i);
124 for (
int j = 0; j < 4; ++j) {
126 if (((i >> aj) & 3) == index) {
127 b[j ^ 2] = m_HVSync | packed222;
134 void VGA4Controller::setPixelAt(PixelDesc
const & pixelDesc,
Rect & updateRect)
136 genericSetPixelAt(pixelDesc, updateRect,
137 [&] (
RGB888 const & color) {
return RGB888toPaletteIndex(color); },
145 void VGA4Controller::absDrawLine(
int X1,
int Y1,
int X2,
int Y2, RGB888 color)
147 genericAbsDrawLine(
X1,
Y1,
X2,
Y2, color,
148 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
149 [&] (
int Y,
int X1,
int X2, uint8_t colorIndex) { rawFillRow(
Y,
X1,
X2, colorIndex); },
150 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); },
152 [&] (
int X,
int Y) { VGA4_INVERT_PIXEL(
X,
Y); }
158 void VGA4Controller::rawFillRow(
int y,
int x1,
int x2, RGB888 color)
160 rawFillRow(y, x1, x2, RGB888toPaletteIndex(color));
165 void VGA4Controller::rawFillRow(
int y,
int x1,
int x2, uint8_t colorIndex)
167 uint8_t * row = (uint8_t*) m_viewPort[y];
170 for (; x <= x2 && (x & 3) != 0; ++x) {
171 VGA4_SETPIXELINROW(row, x, colorIndex);
175 int sz = (x2 & ~3) - x;
176 memset((
void*)(row + x / 4), colorIndex | (colorIndex << 2) | (colorIndex << 4) | (colorIndex << 6), sz / 4);
180 for (; x <= x2; ++x) {
181 VGA4_SETPIXELINROW(row, x, colorIndex);
187 void VGA4Controller::rawInvertRow(
int y,
int x1,
int x2)
189 auto row = m_viewPort[y];
190 for (
int x = x1; x <= x2; ++x)
191 VGA4_INVERTPIXELINROW(row, x);
195 void VGA4Controller::rawCopyRow(
int x1,
int x2,
int srcY,
int dstY)
197 auto srcRow = (uint8_t*) m_viewPort[srcY];
198 auto dstRow = (uint8_t*) m_viewPort[dstY];
201 for (; x <= x2 && (x & 3) != 0; ++x) {
202 VGA4_SETPIXELINROW(dstRow, x, VGA4_GETPIXELINROW(srcRow, x));
205 auto src = (uint8_t*)(srcRow + x / 4);
206 auto dst = (uint8_t*)(dstRow + x / 4);
207 for (
int right = (x2 & ~3); x < right; x += 4)
210 for (x = (x2 & ~3); x <= x2; ++x) {
211 VGA4_SETPIXELINROW(dstRow, x, VGA4_GETPIXELINROW(srcRow, x));
216 void VGA4Controller::swapRows(
int yA,
int yB,
int x1,
int x2)
218 auto rowA = (uint8_t*) m_viewPort[yA];
219 auto rowB = (uint8_t*) m_viewPort[yB];
222 for (; x <= x2 && (x & 3) != 0; ++x) {
223 uint8_t a = VGA4_GETPIXELINROW(rowA, x);
224 uint8_t b = VGA4_GETPIXELINROW(rowB, x);
225 VGA4_SETPIXELINROW(rowA, x, b);
226 VGA4_SETPIXELINROW(rowB, x, a);
229 auto a = (uint8_t*)(rowA + x / 4);
230 auto b = (uint8_t*)(rowB + x / 4);
231 for (
int right = (x2 & ~3); x < right; x += 4)
234 for (x = (x2 & ~3); x <= x2; ++x) {
235 uint8_t a = VGA4_GETPIXELINROW(rowA, x);
236 uint8_t b = VGA4_GETPIXELINROW(rowB, x);
237 VGA4_SETPIXELINROW(rowA, x, b);
238 VGA4_SETPIXELINROW(rowB, x, a);
243 void VGA4Controller::drawEllipse(Size
const & size, Rect & updateRect)
245 genericDrawEllipse(size, updateRect,
246 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
252 void VGA4Controller::clear(Rect & updateRect)
254 hideSprites(updateRect);
255 uint8_t paletteIndex = RGB888toPaletteIndex(getActualBrushColor());
256 uint8_t pattern4 = paletteIndex | (paletteIndex << 2) | (paletteIndex << 4) | (paletteIndex << 6);
257 for (
int y = 0; y < m_viewPortHeight; ++y)
258 memset((uint8_t*) m_viewPort[y], pattern4, m_viewPortWidth / 4);
264 void VGA4Controller::VScroll(
int scroll, Rect & updateRect)
266 genericVScroll(scroll, updateRect,
267 [&] (
int yA,
int yB,
int x1,
int x2) { swapRows(yA, yB, x1, x2); },
268 [&] (
int yA,
int yB) { tswap(m_viewPort[yA], m_viewPort[yB]); },
269 [&] (
int y,
int x1,
int x2, RGB888 color) { rawFillRow(y, x1, x2, color); }
274 void VGA4Controller::HScroll(
int scroll, Rect & updateRect)
276 hideSprites(updateRect);
277 uint8_t back = RGB888toPaletteIndex(getActualBrushColor());
278 uint8_t back4 = back | (back << 2) | (back << 4) | (back << 6);
280 int Y1 = paintState().scrollingRegion.Y1;
281 int Y2 = paintState().scrollingRegion.Y2;
282 int X1 = paintState().scrollingRegion.X1;
283 int X2 = paintState().scrollingRegion.X2;
286 bool HScrolllingRegionAligned = ((
X1 & 3) == 0 && (
width & 3) == 0);
290 for (
int y =
Y1; y <=
Y2; ++y) {
291 if (HScrolllingRegionAligned) {
293 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 4;
294 for (
int s = -scroll; s > 0;) {
298 uint8_t prev = back4;
299 for (
int i = sz - 1; i >= 0; --i) {
300 uint8_t lowbits = prev >> (8 - s * 2);
302 row[i] = (row[i] << (s * 2)) | lowbits;
308 auto sz =
width & ~3;
309 memmove(row, row + sc / 4, (sz - sc) / 4);
310 rawFillRow(y,
X2 - sc + 1,
X2, back);
316 auto row = (uint8_t*) m_viewPort[y];
317 for (
int x =
X1; x <=
X2 + scroll; ++x)
318 VGA4_SETPIXELINROW(row, x, VGA4_GETPIXELINROW(row, x - scroll));
320 rawFillRow(y,
X2 + 1 + scroll,
X2, back);
323 }
else if (scroll > 0) {
325 for (
int y =
Y1; y <=
Y2; ++y) {
326 if (HScrolllingRegionAligned) {
328 uint8_t * row = (uint8_t*) (m_viewPort[y]) +
X1 / 4;
329 for (
int s = scroll; s > 0;) {
333 uint8_t prev = back4;
334 for (
int i = 0; i < sz; ++i) {
335 uint8_t highbits = prev << (8 - s * 2);
337 row[i] = (row[i] >> (s * 2)) | highbits;
343 auto sz =
width & ~3;
344 memmove(row + sc / 4, row, (sz - sc) / 4);
345 rawFillRow(y,
X1,
X1 + sc - 1, back);
351 auto row = (uint8_t*) m_viewPort[y];
352 for (
int x =
X2 - scroll; x >=
X1; --x)
353 VGA4_SETPIXELINROW(row, x + scroll, VGA4_GETPIXELINROW(row, x));
355 rawFillRow(y,
X1,
X1 + scroll - 1, back);
363 void VGA4Controller::drawGlyph(Glyph
const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect)
365 genericDrawGlyph(glyph, glyphOptions, penColor, brushColor, updateRect,
366 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
367 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
373 void VGA4Controller::invertRect(Rect
const & rect, Rect & updateRect)
375 genericInvertRect(rect, updateRect,
376 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); }
381 void VGA4Controller::swapFGBG(Rect
const & rect, Rect & updateRect)
383 genericSwapFGBG(rect, updateRect,
384 [&] (RGB888
const & color) {
return RGB888toPaletteIndex(color); },
385 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
394 void VGA4Controller::copyRect(Rect
const & source, Rect & updateRect)
396 genericCopyRect(source, updateRect,
397 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
405 void VGA4Controller::readScreen(Rect
const & rect, RGB888 * destBuf)
407 for (
int y = rect.Y1; y <= rect.Y2; ++y) {
408 auto row = (uint8_t*) m_viewPort[y];
409 for (
int x = rect.X1; x <= rect.X2; ++x, ++destBuf) {
410 const RGB222 v = m_palette[VGA4_GETPIXELINROW(row, x)];
411 *destBuf = RGB888(v.R * 85, v.G * 85, v.B * 85);
417 void VGA4Controller::rawDrawBitmap_Native(
int destX,
int destY, Bitmap
const * bitmap,
int X1,
int Y1,
int XCount,
int YCount)
419 genericRawDrawBitmap_Native(destX, destY, (uint8_t*) bitmap->data, bitmap->width,
X1,
Y1, XCount, YCount,
420 [&] (
int y) { return (uint8_t*) m_viewPort[y]; },
426 void VGA4Controller::rawDrawBitmap_Mask(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
428 auto foregroundColorIndex = RGB888toPaletteIndex(bitmap->foregroundColor);
429 genericRawDrawBitmap_Mask(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
430 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
432 [&] (uint8_t * row,
int x) { VGA4_SETPIXELINROW(row, x, foregroundColorIndex); }
437 void VGA4Controller::rawDrawBitmap_RGBA2222(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
439 genericRawDrawBitmap_RGBA2222(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
440 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
442 [&] (uint8_t * row,
int x, uint8_t src) { VGA4_SETPIXELINROW(row, x, RGB2222toPaletteIndex(src)); }
447 void VGA4Controller::rawDrawBitmap_RGBA8888(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
449 genericRawDrawBitmap_RGBA8888(destX, destY, bitmap, (uint8_t*)saveBackground,
X1,
Y1, XCount, YCount,
450 [&] (
int y) {
return (uint8_t*) m_viewPort[y]; },
451 [&] (uint8_t * row,
int x) {
return VGA4_GETPIXELINROW(row, x); },
452 [&] (uint8_t * row,
int x,
RGBA8888 const & src) { VGA4_SETPIXELINROW(row, x, RGB8888toPaletteIndex(src)); }
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() && ctrl->m_primitiveExecTask) {
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.