TinyGPU
Loading...
Searching...
No Matches
DisplayDriverSDL.h
Go to the documentation of this file.
1#pragma once
2#include <SDL.h>
3
4#include <cstdint>
5#include <cstdlib>
6#include <vector>
7
8#include "TinyGPU/Drivers/DisplayDriver.h"
9#include "TinyGPU/Color/RGB565.h"
10
11namespace tinygpu {
12
13/**
14 * @brief Display class for rendering a Surface<RGB565> using the SDL2 library.
15 * This class can be used to display the contents of a TinyGPU Surface<RGB565>
16 * on a desktop environment for testing and debugging purposes.
17 *
18 * writeData() drains the SDL event queue on every call and exits the process
19 * on SDL_QUIT (i.e. the window's close button), since sketches that never
20 * touch a TouchDriverSDL - the only other place that pumps events - would
21 * otherwise leave the window unresponsive to being closed: with no one
22 * calling SDL_PollEvent, the window manager's close request just piles up in
23 * SDL's queue and the OS reports the window as hung.
24 *
25 * The window can be larger than the display: setScale() (or the environment
26 * variable TINYGPU_SDL_SCALE, which takes precedence) opens it with that
27 * factor, e.g. 1.5 or 2, and it can be resized - the display is scaled to the
28 * window (letterboxed if the aspect ratio differs): with sharp pixels for a
29 * whole factor, smoothed otherwise. TouchDriverSDL maps the mouse back to
30 * display coordinates.
31 */
32template <typename RGB_T = RGB565>
33class DisplayDriverSDL : public DisplayDriver<RGB_T> {
34 public:
35 DisplayDriverSDL(size_t width, size_t height) : w_(width), h_(height) {}
36 DisplayDriverSDL(const ISurface<RGB_T>& surface)
37 : w_(surface.width()), h_(surface.height()) {}
38
39 size_t width() const override { return w_; }
40 size_t height() const override { return h_; }
41
42 /// Size of the window as a multiple of the display, e.g. 1.5 or 2 (call
43 /// before begin()).
44 void setScale(float scale) { scale_ = scale < 1.0f ? 1.0f : scale; }
45 float scale() const { return scale_; }
46
47 /// Title of the window (call before begin()).
48 void setTitle(const char* title) { title_ = title != nullptr ? title : ""; }
49
50 bool begin() override {
51 SDL_Init(SDL_INIT_VIDEO);
52 if (window_ == nullptr) {
53 const char* env = getenv("TINYGPU_SDL_SCALE");
54 if (env != nullptr && atof(env) >= 1.0) scale_ = static_cast<float>(atof(env));
55 // sharp pixels for a whole factor; smoothed otherwise (uneven pixels)
56 const bool whole = scale_ == static_cast<float>(static_cast<int>(scale_));
57 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, whole ? "0" : "1");
58 window_ = SDL_CreateWindow(title_, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
59 static_cast<int>(w_ * scale_ + 0.5f), static_cast<int>(h_ * scale_ + 0.5f),
60 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
61 if (window_ == nullptr) return false;
62 renderer_ = SDL_CreateRenderer(window_, -1, SDL_RENDERER_ACCELERATED);
63 if (renderer_ == nullptr) return false;
64 // draw in display pixels, scaled to the window
65 SDL_RenderSetLogicalSize(renderer_, static_cast<int>(w_), static_cast<int>(h_));
66 texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGB565,
67 SDL_TEXTUREACCESS_STREAMING, w_, h_);
68 if (texture_ == nullptr) return false;
69 }
70 return true;
71 }
72
73 public:
74 bool writeData(ISurface<RGB_T>& surface) override {
75 return writeData(surface, 0, 0);
76 }
77
78 bool writeData(ISurface<RGB_T>& surface, size_t x, size_t y) override {
79 static_assert(sizeof(RGB_T) == 2,
80 "DisplayDriverSDL assumes a 16bpp RGB_T (RGB565)");
81 SDL_Event event;
82 while (SDL_PollEvent(&event)) {
83 if (event.type == SDL_QUIT) {
84 exit(0);
85 }
86 }
87 setAddressWindow(x, y, surface.width(), surface.height());
88 SDL_Rect updateRect = {static_cast<int>(x), static_cast<int>(y),
89 static_cast<int>(surface.width()),
90 static_cast<int>(surface.height())};
91 // RGB_T (RGB565) values are stored byte-swapped to SPI/QSPI wire
92 // order (see RGB565.h), but SDL_PIXELFORMAT_RGB565 expects the
93 // conventional (native) bit layout - swap back into a scratch
94 // buffer before uploading, rather than mutating the surface itself.
95 const size_t n = surface.size();
96 if (scratch_.size() < n) scratch_.resize(n);
97 const uint16_t* src = reinterpret_cast<const uint16_t*>(surface.data());
98 uint16_t* dst = reinterpret_cast<uint16_t*>(scratch_.data());
99 const size_t pixelCount = n / sizeof(RGB_T);
100 for (size_t i = 0; i < pixelCount; ++i) {
101 dst[i] = RGB565::swapBytes(src[i]);
102 }
103 SDL_UpdateTexture(texture_, &updateRect, scratch_.data(),
104 surface.width() * sizeof(RGB_T));
105 SDL_Rect fullRect = {0, 0, static_cast<int>(w_), static_cast<int>(h_)};
106 SDL_RenderClear(renderer_);
107 SDL_RenderCopy(renderer_, texture_, NULL, &fullRect);
108 SDL_RenderPresent(renderer_);
109 return true;
110 }
111
112 void end() override {
113 SDL_DestroyTexture(texture_);
114 SDL_DestroyRenderer(renderer_);
115 SDL_DestroyWindow(window_);
116 SDL_Quit();
117 texture_ = nullptr;
118 renderer_ = nullptr;
119 window_ = nullptr;
120 }
121
122 protected:
125 float scale_ = 1.0f;
126 const char* title_ = "RGB565 Display";
127 SDL_Window* window_ = nullptr;
131
132 protected:
133 bool setAddressWindow(size_t x, size_t y, size_t w, size_t h) override {
134 return true;
135 }
136};
137
138} // namespace tinygpu
Display class for rendering a Surface<RGB565> using the SDL2 library. This class can be used to displ...
Definition: DisplayDriverSDL.h:33
float scale_
Definition: DisplayDriverSDL.h:125
size_t w_
Definition: DisplayDriverSDL.h:123
SDL_Texture * texture_
Definition: DisplayDriverSDL.h:129
DisplayDriverSDL(size_t width, size_t height)
Definition: DisplayDriverSDL.h:35
void end() override
Definition: DisplayDriverSDL.h:112
SDL_Window * window_
Definition: DisplayDriverSDL.h:127
size_t height() const override
Definition: DisplayDriverSDL.h:40
DisplayDriverSDL(const ISurface< RGB_T > &surface)
Definition: DisplayDriverSDL.h:36
std::vector< uint8_t > scratch_
Definition: DisplayDriverSDL.h:130
bool writeData(ISurface< RGB_T > &surface, size_t x, size_t y) override
Definition: DisplayDriverSDL.h:78
size_t h_
Definition: DisplayDriverSDL.h:124
size_t width() const override
Definition: DisplayDriverSDL.h:39
SDL_Renderer * renderer_
Definition: DisplayDriverSDL.h:128
bool begin() override
Definition: DisplayDriverSDL.h:50
void setTitle(const char *title)
Title of the window (call before begin()).
Definition: DisplayDriverSDL.h:48
bool setAddressWindow(size_t x, size_t y, size_t w, size_t h) override
Definition: DisplayDriverSDL.h:133
float scale() const
Definition: DisplayDriverSDL.h:45
const char * title_
Definition: DisplayDriverSDL.h:126
void setScale(float scale)
Definition: DisplayDriverSDL.h:44
bool writeData(ISurface< RGB_T > &surface) override
Definition: DisplayDriverSDL.h:74
Definition: DisplayDriver.h:49
RGB color stored in 16-bit RGB565 format, byte-swapped from the conventional bit layout.
Definition: RGB565.h:24
Definition: DSIBusESP32.h:19