FabGL
ESP32 Display Controller and Graphics Library
vga8controller.cpp
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019-2020 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 
24 #include <alloca.h>
25 #include <stdarg.h>
26 #include <math.h>
27 #include <string.h>
28 
29 #include "freertos/FreeRTOS.h"
30 #include "freertos/task.h"
31 
32 #include "soc/i2s_struct.h"
33 #include "soc/i2s_reg.h"
34 #include "driver/periph_ctrl.h"
35 #include "rom/lldesc.h"
36 #include "soc/rtc.h"
37 #include "esp_spi_flash.h"
38 #include "esp_heap_caps.h"
39 
40 #include "fabutils.h"
41 #include "vga8controller.h"
42 #include "devdrivers/swgenerator.h"
43 
44 
45 
46 
47 
48 
49 
50 
51 namespace fabgl {
52 
53 /*
54 To improve drawing and rendering speed pixels order is a bit oddy because we want to pack pixels (3 bits) into a uint32_t and ESP32 is little-endian.
55 8 pixels (24 bits) are packed in 3 bytes:
56 bytes: 0 1 2 ...
57 bits: 76543210 76543210 76543210 ...
58 pixels: 55666777 23334445 00011122 ...
59 bits24: 0 1...
60 */
61 
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); // x / 8 * 3
64  int shift = 21 - (x & 7) * 3;
65  *bits24 ^= ((value << shift) ^ *bits24) & (7 << shift);
66 }
67 
68 static inline __attribute__((always_inline)) int VGA8_GETPIXELINROW(uint8_t * row, int x) {
69  uint32_t * bits24 = (uint32_t*)(row + (x >> 3) * 3); // x / 8 * 3
70  int shift = 21 - (x & 7) * 3;
71  return (*bits24 >> shift) & 7;
72 }
73 
74 #define VGA8_INVERTPIXELINROW(row, x) *((uint32_t*)(row + ((x) >> 3) * 3)) ^= 7 << (21 - ((x) & 7) * 3)
75 
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); // x / 8 * 3
79  int shift = 21 - (x & 7) * 3;
80  *bits24 ^= ((value << shift) ^ *bits24) & (7 << shift);
81 }
82 
83 #define VGA8_GETPIXEL(x, y) VGA8_GETPIXELINROW((uint8_t*)VGA8Controller::s_viewPort[(y)], (x))
84 
85 #define VGA8_INVERT_PIXEL(x, y) VGA8_INVERTPIXELINROW((uint8_t*)VGA8Controller::s_viewPort[(y)], (x))
86 
87 
88 
89 
90 /*************************************************************************************/
91 /* VGA8Controller definitions */
92 
93 
94 VGA8Controller * VGA8Controller::s_instance = nullptr;
95 
96 
97 
98 
99 VGA8Controller::VGA8Controller()
100  : VGAPalettedController(VGA8_LinesCount, NativePixelFormat::PALETTE8, 8, 3, ISRHandler)
101 {
102  s_instance = this;
103  m_packedPaletteIndexPair_to_signals = (uint16_t *) heap_caps_malloc(256 * sizeof(uint16_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
104 }
105 
106 
107 VGA8Controller::~VGA8Controller()
108 {
109  heap_caps_free((void *)m_packedPaletteIndexPair_to_signals);
110 }
111 
112 
113 void VGA8Controller::setupDefaultPalette()
114 {
115  setPaletteItem(0, RGB888(0, 0, 0)); // 0: black
116  setPaletteItem(1, RGB888(128, 0, 0)); // 1: red
117  setPaletteItem(2, RGB888(0, 128, 0)); // 2: green
118  setPaletteItem(3, RGB888(0, 0, 128)); // 3: blue
119  setPaletteItem(4, RGB888(255, 0, 0)); // 4: bright red
120  setPaletteItem(5, RGB888(0, 255, 0)); // 5: bright green
121  setPaletteItem(6, RGB888(0, 0, 255)); // 6: bright blue
122  setPaletteItem(7, RGB888(255, 255, 255)); // 7: white
123 }
124 
125 
126 void VGA8Controller::setPaletteItem(int index, RGB888 const & color)
127 {
128  index %= 8;
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;
136  }
137 }
138 
139 
140 void VGA8Controller::setPixelAt(PixelDesc const & pixelDesc, Rect & updateRect)
141 {
142  genericSetPixelAt(pixelDesc, updateRect,
143  [&] (RGB888 const & color) { return RGB888toPaletteIndex(color); },
144  VGA8_SETPIXEL
145  );
146 }
147 
148 
149 // coordinates are absolute values (not relative to origin)
150 // line clipped on current absolute clipping rectangle
151 void VGA8Controller::absDrawLine(int X1, int Y1, int X2, int Y2, RGB888 color)
152 {
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); },
157  VGA8_SETPIXEL,
158  [&] (int X, int Y) { VGA8_INVERT_PIXEL(X, Y); }
159  );
160 }
161 
162 
163 // parameters not checked
164 void VGA8Controller::rawFillRow(int y, int x1, int x2, RGB888 color)
165 {
166  rawFillRow(y, x1, x2, RGB888toPaletteIndex(color));
167 }
168 
169 
170 // parameters not checked
171 void VGA8Controller::rawFillRow(int y, int x1, int x2, uint8_t colorIndex)
172 {
173  uint8_t * row = (uint8_t*) m_viewPort[y];
174  for (; x1 <= x2; ++x1)
175  VGA8_SETPIXELINROW(row, x1, colorIndex);
176 }
177 
178 
179 // parameters not checked
180 void VGA8Controller::rawInvertRow(int y, int x1, int x2)
181 {
182  auto row = m_viewPort[y];
183  for (int x = x1; x <= x2; ++x)
184  VGA8_INVERTPIXELINROW(row, x);
185 }
186 
187 
188 void VGA8Controller::rawCopyRow(int x1, int x2, int srcY, int dstY)
189 {
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));
194 }
195 
196 
197 void VGA8Controller::swapRows(int yA, int yB, int x1, int x2)
198 {
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);
206  }
207 }
208 
209 
210 void VGA8Controller::drawEllipse(Size const & size, Rect & updateRect)
211 {
212  genericDrawEllipse(size, updateRect,
213  [&] (RGB888 const & color) { return RGB888toPaletteIndex(color); },
214  VGA8_SETPIXEL
215  );
216 }
217 
218 
219 void VGA8Controller::clear(Rect & updateRect)
220 {
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;
228  }
229 }
230 
231 
232 // scroll < 0 -> scroll UP
233 // scroll > 0 -> scroll DOWN
234 void VGA8Controller::VScroll(int scroll, Rect & updateRect)
235 {
236  genericVScroll(scroll, updateRect,
237  [&] (int yA, int yB, int x1, int x2) { swapRows(yA, yB, x1, x2); }, // swapRowsCopying
238  [&] (int yA, int yB) { tswap(m_viewPort[yA], m_viewPort[yB]); }, // swapRowsPointers
239  [&] (int y, int x1, int x2, RGB888 color) { rawFillRow(y, x1, x2, color); } // rawFillRow
240  );
241 }
242 
243 
244 // todo: optimize!
245 void VGA8Controller::HScroll(int scroll, Rect & updateRect)
246 {
247  hideSprites(updateRect);
248  uint8_t back = RGB888toPaletteIndex(getActualBrushColor());
249 
250  int Y1 = paintState().scrollingRegion.Y1;
251  int Y2 = paintState().scrollingRegion.Y2;
252  int X1 = paintState().scrollingRegion.X1;
253  int X2 = paintState().scrollingRegion.X2;
254 
255  int width = X2 - X1 + 1;
256  bool HScrolllingRegionAligned = ((X1 & 7) == 0 && (width & 7) == 0); // 8 pixels aligned
257 
258  if (scroll < 0) {
259  // scroll left
260  for (int y = Y1; y <= Y2; ++y) {
261  for (int s = -scroll; s > 0;) {
262  if (HScrolllingRegionAligned && s >= 8) {
263  // scroll left by multiplies of 8
264  uint8_t * row = (uint8_t*) (m_viewPort[y]) + X1 / 8 * 3;
265  auto sc = s & ~7;
266  auto sz = width & ~7;
267  memmove(row, row + sc / 8 * 3, (sz - sc) / 8 * 3);
268  rawFillRow(y, X2 - sc + 1, X2, back);
269  s -= sc;
270  } else {
271  // unaligned horizontal scrolling region, fallback to slow version
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));
275  // fill right area with brush color
276  rawFillRow(y, X2 - s + 1, X2, back);
277  s = 0;
278  }
279  }
280  }
281  } else if (scroll > 0) {
282  // scroll right
283  for (int y = Y1; y <= Y2; ++y) {
284  for (int s = scroll; s > 0;) {
285  if (HScrolllingRegionAligned && s >= 8) {
286  // scroll right by multiplies of 8
287  uint8_t * row = (uint8_t*) (m_viewPort[y]) + X1 / 8 * 3;
288  auto sc = s & ~7;
289  auto sz = width & ~7;
290  memmove(row + sc / 8 * 3, row, (sz - sc) / 8 * 3);
291  rawFillRow(y, X1, X1 + sc - 1, back);
292  s -= sc;
293  } else {
294  // unaligned horizontal scrolling region, fallback to slow version
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));
298  // fill left area with brush color
299  rawFillRow(y, X1, X1 + s - 1, back);
300  s = 0;
301  }
302  }
303  }
304  }
305 }
306 
307 
308 void VGA8Controller::drawGlyph(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect)
309 {
310  genericDrawGlyph(glyph, glyphOptions, penColor, brushColor, updateRect,
311  [&] (RGB888 const & color) { return RGB888toPaletteIndex(color); },
312  [&] (int y) { return (uint8_t*) m_viewPort[y]; },
313  VGA8_SETPIXELINROW
314  );
315 }
316 
317 
318 void VGA8Controller::invertRect(Rect const & rect, Rect & updateRect)
319 {
320  genericInvertRect(rect, updateRect,
321  [&] (int Y, int X1, int X2) { rawInvertRow(Y, X1, X2); }
322  );
323 }
324 
325 
326 void VGA8Controller::swapFGBG(Rect const & rect, Rect & updateRect)
327 {
328  genericSwapFGBG(rect, updateRect,
329  [&] (RGB888 const & color) { return RGB888toPaletteIndex(color); },
330  [&] (int y) { return (uint8_t*) m_viewPort[y]; },
331  VGA8_GETPIXELINROW,
332  VGA8_SETPIXELINROW
333  );
334 }
335 
336 
337 // Slow operation!
338 // supports overlapping of source and dest rectangles
339 void VGA8Controller::copyRect(Rect const & source, Rect & updateRect)
340 {
341  genericCopyRect(source, updateRect,
342  [&] (int y) { return (uint8_t*) m_viewPort[y]; },
343  VGA8_GETPIXELINROW,
344  VGA8_SETPIXELINROW
345  );
346 }
347 
348 
349 // no bounds check is done!
350 void VGA8Controller::readScreen(Rect const & rect, RGB888 * destBuf)
351 {
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); // 85 x 3 = 255
357  }
358  }
359 }
360 
361 
362 void VGA8Controller::rawDrawBitmap_Native(int destX, int destY, Bitmap const * bitmap, int X1, int Y1, int XCount, int YCount)
363 {
364  genericRawDrawBitmap_Native(destX, destY, (uint8_t*) bitmap->data, bitmap->width, X1, Y1, XCount, YCount,
365  [&] (int y) { return (uint8_t*) m_viewPort[y]; }, // rawGetRow
366  VGA8_SETPIXELINROW
367  );
368 }
369 
370 
371 void VGA8Controller::rawDrawBitmap_Mask(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount)
372 {
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]; }, // rawGetRow
376  VGA8_GETPIXELINROW,
377  [&] (uint8_t * row, int x) { VGA8_SETPIXELINROW(row, x, foregroundColorIndex); } // rawSetPixelInRow
378  );
379 }
380 
381 
382 void VGA8Controller::rawDrawBitmap_RGBA2222(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount)
383 {
384  genericRawDrawBitmap_RGBA2222(destX, destY, bitmap, (uint8_t*)saveBackground, X1, Y1, XCount, YCount,
385  [&] (int y) { return (uint8_t*) m_viewPort[y]; }, // rawGetRow
386  VGA8_GETPIXELINROW,
387  [&] (uint8_t * row, int x, uint8_t src) { VGA8_SETPIXELINROW(row, x, RGB2222toPaletteIndex(src)); } // rawSetPixelInRow
388  );
389 }
390 
391 
392 void VGA8Controller::rawDrawBitmap_RGBA8888(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount)
393 {
394  genericRawDrawBitmap_RGBA8888(destX, destY, bitmap, (uint8_t*)saveBackground, X1, Y1, XCount, YCount,
395  [&] (int y) { return (uint8_t*) m_viewPort[y]; }, // rawGetRow
396  [&] (uint8_t * row, int x) { return VGA8_GETPIXELINROW(row, x); }, // rawGetPixelInRow
397  [&] (uint8_t * row, int x, RGBA8888 const & src) { VGA8_SETPIXELINROW(row, x, RGB8888toPaletteIndex(src)); } // rawSetPixelInRow
398  );
399 }
400 
401 
402 #pragma GCC optimize ("O2")
403 
404 
405 void IRAM_ATTR VGA8Controller::ISRHandler(void * arg)
406 {
407  #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK
408  auto s1 = getCycleCount();
409  #endif
410 
411  auto ctrl = (VGA8Controller *) arg;
412 
413  if (I2S1.int_st.out_eof) {
414 
415  auto const desc = (lldesc_t*) I2S1.out_eof_des_addr;
416 
417  if (desc == s_frameResetDesc)
418  s_scanLine = 0;
419 
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;
424 
425  int scanLine = (s_scanLine + VGA8_LinesCount / 2) % height;
426 
427  auto lineIndex = scanLine & (VGA8_LinesCount - 1);
428 
429  for (int i = 0; i < VGA8_LinesCount / 2; ++i) {
430 
431  auto src = (uint8_t const *) s_viewPortVisible[scanLine];
432  auto dest = (uint16_t*) lines[lineIndex];
433 
434  // optimizazion warn: horizontal resolution must be a multiple of 16!
435  for (int col = 0; col < width; col += 16) {
436 
437  auto w1 = *((uint16_t*)(src )); // hi A:23334445, lo A:55666777
438  auto w2 = *((uint16_t*)(src + 2)); // hi B:55666777, lo A:00011122
439  auto w3 = *((uint16_t*)(src + 4)); // hi B:00011122, lo B:23334445
440 
441  PSRAM_HACK;
442 
443  auto src1 = w1 | (w2 << 16);
444  auto src2 = (w2 >> 8) | (w3 << 8);
445 
446  auto v1 = packedPaletteIndexPair_to_signals[(src1 ) & 0x3f]; // pixels 0, 1
447  auto v2 = packedPaletteIndexPair_to_signals[(src1 >> 6) & 0x3f]; // pixels 2, 3
448  auto v3 = packedPaletteIndexPair_to_signals[(src1 >> 12) & 0x3f]; // pixels 4, 5
449  auto v4 = packedPaletteIndexPair_to_signals[(src1 >> 18) & 0x3f]; // pixels 6, 7
450  auto v5 = packedPaletteIndexPair_to_signals[(src2 ) & 0x3f]; // pixels 8, 9
451  auto v6 = packedPaletteIndexPair_to_signals[(src2 >> 6) & 0x3f]; // pixels 10, 11
452  auto v7 = packedPaletteIndexPair_to_signals[(src2 >> 12) & 0x3f]; // pixels 12, 13
453  auto v8 = packedPaletteIndexPair_to_signals[(src2 >> 18) & 0x3f]; // pixels 14, 15
454 
455  *(dest + 2) = v1;
456  *(dest + 3) = v2;
457  *(dest + 0) = v3;
458  *(dest + 1) = v4;
459  *(dest + 6) = v5;
460  *(dest + 7) = v6;
461  *(dest + 4) = v7;
462  *(dest + 5) = v8;
463 
464  dest += 8;
465  src += 6;
466 
467  }
468 
469  ++lineIndex;
470  ++scanLine;
471  }
472 
473  s_scanLine += VGA8_LinesCount / 2;
474 
475  if (scanLine >= height && !ctrl->m_primitiveProcessingSuspended && spi_flash_cache_enabled()) {
476  // vertical sync, unlock primitive execution task
477  // warn: don't use vTaskSuspendAll() in primitive drawing, otherwise vTaskNotifyGiveFromISR may be blocked and screen will flick!
478  vTaskNotifyGiveFromISR(ctrl->m_primitiveExecTask, NULL);
479  }
480 
481  }
482 
483  #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK
484  s_vgapalctrlcycles += getCycleCount() - s1;
485  #endif
486 
487  I2S1.int_clr.val = I2S1.int_st.val;
488 }
489 
490 
491 
492 
493 
494 } // end of namespace
int16_t X2
Definition: fabutils.h:150
Represents a 24 bit RGB color.
int16_t Y2
Definition: fabutils.h:151
int16_t Y1
Definition: fabutils.h:149
int16_t Y
This file contains fabgl::GPIOStream definition.
int16_t X1
Definition: fabutils.h:148
This file contains some utility classes and functions.
Definition: canvas.cpp:31
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.
Represents a rectangle.
Definition: fabutils.h:191
int16_t X
uint8_t height
uint8_t width