FabGL
ESP32 Display Controller and Graphics Library
vga16controller.h
Go to the documentation of this file.
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 #pragma once
24 
25 
26 
34 #include <stdint.h>
35 #include <stddef.h>
36 #include <atomic>
37 
38 #include "rom/lldesc.h"
39 #include "driver/gpio.h"
40 
41 #include "freertos/FreeRTOS.h"
42 #include "freertos/queue.h"
43 
44 #include "fabglconf.h"
45 #include "fabutils.h"
46 #include "devdrivers/swgenerator.h"
47 #include "displaycontroller.h"
49 
50 
51 
52 #define VGA16_LinesCount 4
53 
54 //#define VGATextController_PERFORMANCE_CHECK
55 
56 
57 namespace fabgl {
58 
59 
60 
80 class VGA16Controller : public GenericBitmappedDisplayController {
81 
82 public:
83 
85 
86  // unwanted methods
87  VGA16Controller(VGA16Controller const&) = delete;
88  void operator=(VGA16Controller const&) = delete;
89 
90 
96  static VGA16Controller * instance() { return s_instance; }
97 
98 
115  void begin(gpio_num_t redGPIO, gpio_num_t greenGPIO, gpio_num_t blueGPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO);
116 
136  void begin(gpio_num_t red1GPIO, gpio_num_t red0GPIO, gpio_num_t green1GPIO, gpio_num_t green0GPIO, gpio_num_t blue1GPIO, gpio_num_t blue0GPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO);
137 
148  void begin();
149 
150  void end();
151 
152  // abstract method of BitmappedDisplayController
154 
155  // abstract method of BitmappedDisplayController
157 
165  uint8_t getBitsPerChannel() { return m_bitsPerChannel; }
166 
167  // abstract method of BitmappedDisplayController
169 
197  void setResolution(char const * modeline, int viewPortWidth = -1, int viewPortHeight = -1, bool doubleBuffered = false);
198 
199  void setResolution(VGATimings const& timings, int viewPortWidth = -1, int viewPortHeight = -1, bool doubleBuffered = false);
200 
201  VGATimings * getResolutionTimings() { return &m_timings; }
202 
203  // abstract method of BitmappedDisplayController
204  int getScreenWidth() { return m_timings.HVisibleArea; }
205 
206  // abstract method of BitmappedDisplayController
207  int getScreenHeight() { return m_timings.VVisibleArea; }
208 
214  int getViewPortCol() { return m_viewPortCol; }
215 
221  int getViewPortRow() { return m_viewPortRow; }
222 
223  // abstract method of BitmappedDisplayController
224  int getViewPortWidth() { return m_viewPortWidth; }
225 
226  // abstract method of BitmappedDisplayController
227  int getViewPortHeight() { return m_viewPortHeight; }
228 
242  void moveScreen(int offsetX, int offsetY);
243 
258  void shrinkScreen(int shrinkX, int shrinkY);
259 
260  static uint8_t * getScanline(int y) { return (uint8_t*) s_viewPort[y]; }
261 
262  void readScreen(Rect const & rect, RGB888 * destBuf);
263 
275  void setPaletteItem(int index, RGB888 const & color);
276 
288  void setProcessPrimitivesOnBlank(bool value) { m_processPrimitivesOnBlank = value; }
289 
290 private:
291 
292 
293 
294  void init(gpio_num_t VSyncGPIO);
295 
296  uint8_t packHVSync(bool HSync, bool VSync);
297  uint8_t preparePixel(RGB222 rgb) { return m_HVSync | (rgb.B << VGA_BLUE_BIT) | (rgb.G << VGA_GREEN_BIT) | (rgb.R << VGA_RED_BIT); }
298  uint8_t preparePixelWithSync(RGB222 rgb, bool HSync, bool VSync);
299 
300  uint8_t RGB888toPaletteIndex(RGB888 const & rgb);
301  uint8_t RGB2222toPaletteIndex(uint8_t value);
302  uint8_t RGB8888toPaletteIndex(RGBA8888 value);
303 
304 
305  void freeBuffers();
306  void fillHorizBuffers(int offsetX);
307  void fillVertBuffers(int offsetY);
308  int fill(uint8_t volatile * buffer, int startPos, int length, uint8_t red, uint8_t green, uint8_t blue, bool hsync, bool vsync);
309  void allocateViewPort();
310  void freeViewPort();
311  int calcRequiredDMABuffersCount(int viewPortHeight);
312 
313  // abstract method of BitmappedDisplayController
314  void setPixelAt(PixelDesc const & pixelDesc, Rect & updateRect);
315 
316  // abstract method of BitmappedDisplayController
317  void drawEllipse(Size const & size, Rect & updateRect);
318 
319  // abstract method of BitmappedDisplayController
320  void clear(Rect & updateRect);
321 
322  // abstract method of BitmappedDisplayController
323  void VScroll(int scroll, Rect & updateRect);
324 
325  // abstract method of BitmappedDisplayController
326  void HScroll(int scroll, Rect & updateRect);
327 
328  // abstract method of BitmappedDisplayController
329  void drawGlyph(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect);
330 
331  // abstract method of BitmappedDisplayController
332  void invertRect(Rect const & rect, Rect & updateRect);
333 
334  // abstract method of BitmappedDisplayController
335  void copyRect(Rect const & source, Rect & updateRect);
336 
337  // abstract method of BitmappedDisplayController
338  void swapFGBG(Rect const & rect, Rect & updateRect);
339 
340  // abstract method of BitmappedDisplayController
341  void swapBuffers();
342 
343  // abstract method of BitmappedDisplayController
344  void rawDrawBitmap_Native(int destX, int destY, Bitmap const * bitmap, int X1, int Y1, int XCount, int YCount);
345 
346  // abstract method of BitmappedDisplayController
347  void rawDrawBitmap_Mask(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
348 
349  // abstract method of BitmappedDisplayController
350  void rawDrawBitmap_RGBA2222(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
351 
352  // abstract method of BitmappedDisplayController
353  void rawDrawBitmap_RGBA8888(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
354 
355  // abstract method of BitmappedDisplayController
356  void rawFillRow(int y, int x1, int x2, RGB888 color);
357 
358  void rawFillRow(int y, int x1, int x2, uint8_t colorIndex);
359 
360  void rawInvertRow(int y, int x1, int x2);
361 
362  void rawCopyRow(int x1, int x2, int srcY, int dstY);
363 
364  void swapRows(int yA, int yB, int x1, int x2);
365 
366  // abstract method of BitmappedDisplayController
367  void absDrawLine(int X1, int Y1, int X2, int Y2, RGB888 color);
368 
369  // abstract method of BitmappedDisplayController
370  int getBitmapSavePixelSize() { return 1; }
371 
372  static void I2SInterrupt(void * arg);
373 
374  static void primitiveExecTask(void * arg);
375 
376  void calculateAvailableCyclesForDrawings();
377 
378  static void setupGPIO(gpio_num_t gpio, int bit, gpio_mode_t mode);
379 
380  void setupDefaultPalette();
381  void updateRGB2PaletteLUT();
382 
383  // DMA related methods
384  bool setDMABuffersCount(int buffersCount);
385  void setDMABufferBlank(int index, void volatile * address, int length);
386  void setDMABufferView(int index, int row, int scan);
387  void volatile * getDMABuffer(int index, int * length);
388 
389 
390  static VGA16Controller * s_instance;
391  static volatile int s_scanLine;
392  static lldesc_t volatile * s_frameResetDesc;
393 
394  intr_handle_t m_isr_handle;
395 
396  TaskHandle_t m_primitiveExecTask;
397 
398  volatile int m_primitiveProcessingSuspended; // 0 = enabled, >0 suspended
399 
400  GPIOStream m_GPIOStream;
401 
402  int m_bitsPerChannel; // 1 = 8 colors, 2 = 64 colors, set by begin()
403  VGATimings m_timings;
404  int16_t m_rawFrameHeight;
405 
406  volatile uint32_t m_primitiveExecTimeoutCycles; // Maximum time (in CPU cycles) available for primitives drawing
407 
408  // true = allowed time to process primitives is limited to the vertical blank. Slow, but avoid flickering
409  // false = allowed time is the half of an entire frame. Fast, but may flick
410  bool m_processPrimitivesOnBlank;
411 
412  // These buffers contains a full line, with FrontPorch, Sync, BackPorch and blank visible area, in the
413  // order specified by timings.HStartingBlock
414  volatile uint8_t * m_HBlankLine_withVSync;
415  volatile uint8_t * m_HBlankLine;
416  int16_t m_HLineSize;
417 
418  // contains H and V signals for visible line
419  volatile uint8_t m_HVSync;
420 
421  volatile int16_t m_viewPortCol;
422  volatile int16_t m_viewPortRow;
423  volatile int16_t m_viewPortWidth;
424  volatile int16_t m_viewPortHeight;
425 
426  // when double buffer is enabled the "drawing" view port is always m_viewPort, while the "visible" view port is always m_viewPortVisible
427  // when double buffer is not enabled then m_viewPort = m_viewPortVisible
428  static volatile uint8_t * * s_viewPort;
429  static volatile uint8_t * * s_viewPortVisible;
430 
431  volatile uint8_t * m_lines[VGA16_LinesCount];
432 
433  uint8_t * m_viewPortMemoryPool[FABGLIB_VIEWPORT_MEMORY_POOL_COUNT + 1]; // last allocated pool is nullptr
434 
435  lldesc_t volatile * m_DMABuffers;
436 
437  int m_DMABuffersCount;
438 
439  gpio_num_t m_VSyncGPIO;
440 
441  RGB222 m_palette[16];
442  uint8_t m_packedRGB222_to_PaletteIndex[64];
443  volatile uint16_t m_packedPaletteIndexPair_to_signals[256];
444 
445  volatile bool m_taskProcessingPrimitives;
446 
447 };
448 
449 
450 
451 } // end of namespace
452 
453 
454 
455 
456 
457 
458 
void shrinkScreen(int shrinkX, int shrinkY)
Reduces or expands screen size by the specified horizontal and vertical offset.
Definition: vga16controller.cpp:546
int getViewPortWidth()
Determines horizontal size of the viewport.
Definition: vga16controller.h:224
void suspendBackgroundPrimitiveExecution()
Suspends drawings.
Definition: vga16controller.cpp:188
int16_t VVisibleArea
Definition: vgacontroller.h:96
This file contains fabgl::GPIOStream definition.
int getScreenWidth()
Determines the screen width in pixels.
Definition: vga16controller.h:204
This file contains fabgl::BitmappedDisplayController definition.
NativePixelFormat
This enum defines the display controller native pixel format.
Definition: displaycontroller.h:419
int16_t Y1
Definition: fabutils.h:108
void setPaletteItem(int index, RGB888 const &color)
Determines color of specified palette item.
Definition: vga16controller.cpp:692
This file contains fabgl::VGAController definition.
void begin()
This is the 64 colors (8 GPIOs) initializer using default pinout.
Definition: vga16controller.cpp:152
void resumeBackgroundPrimitiveExecution()
Resumes drawings after suspendBackgroundPrimitiveExecution().
Definition: vga16controller.cpp:198
NativePixelFormat nativePixelFormat()
Represents the native pixel format used by this display.
Definition: vga16controller.h:168
uint8_t G
Definition: displaycontroller.h:273
Specifies the VGA timings. This is a modeline decoded.
Definition: vgacontroller.h:89
This file contains some utility classes and functions.
Definition: canvas.cpp:31
Represents the VGA 16 colors bitmapped controller.
Definition: vga16controller.h:80
uint8_t getBitsPerChannel()
Gets number of bits allocated for each channel.
Definition: vga16controller.h:165
uint8_t B
Definition: displaycontroller.h:274
int16_t X2
Definition: fabutils.h:109
This file contains FabGL library configuration settings, like number of supported colors...
int16_t Y2
Definition: fabutils.h:110
void moveScreen(int offsetX, int offsetY)
Moves screen by specified horizontal and vertical offset.
Definition: vga16controller.cpp:537
Represents a 6 bit RGB color.
Definition: displaycontroller.h:271
int getViewPortRow()
Determines vertical position of the viewport.
Definition: vga16controller.h:221
void setResolution(char const *modeline, int viewPortWidth=-1, int viewPortHeight=-1, bool doubleBuffered=false)
Sets current resolution using linux-like modeline.
Definition: vga16controller.cpp:204
int getViewPortHeight()
Determines vertical size of the viewport.
Definition: vga16controller.h:227
#define FABGLIB_VIEWPORT_MEMORY_POOL_COUNT
Definition: fabglconf.h:114
static VGA16Controller * instance()
Returns the singleton instance of VGA16Controller class.
Definition: vga16controller.h:96
uint8_t R
Definition: displaycontroller.h:272
void setProcessPrimitivesOnBlank(bool value)
Determines the maximum time allowed to process primitives.
Definition: vga16controller.h:288
int getViewPortCol()
Determines horizontal position of the viewport.
Definition: vga16controller.h:214
int getScreenHeight()
Determines the screen height in pixels.
Definition: vga16controller.h:207
int16_t HVisibleArea
Definition: vgacontroller.h:92
int16_t X1
Definition: fabutils.h:107