GCC Code Coverage Report


Directory: ./
File: channeloutput/VirtualDisplay.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 132 156 84.6%
Functions: 9 9 100.0%
Branches: 130 266 48.9%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the GPL v2 as described in the
10 * included LICENSE.GPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include "fpp-json.h"
16
17
18 #include <sys/ioctl.h>
19 #include <sys/mman.h>
20 #include <cmath>
21 #include <fcntl.h>
22
23 #include "../common.h"
24 #include "../log.h"
25
26 #include "VirtualDisplay.h"
27 #include "ChannelOutputSetup.h"
28 #include "overlays/PixelOverlay.h"
29 #include "overlays/PixelOverlayModelSub.h"
30
31 #include "Plugin.h"
32 class VirtualDisplayPlugin : public FPPPlugins::Plugin, public FPPPlugins::ChannelOutputPlugin {
33 public:
34 9 VirtualDisplayPlugin() :
35
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
27 FPPPlugins::Plugin("VirtualDisplay") {
36 9 }
37 22 virtual ChannelOutput* createChannelOutput(unsigned int startChannel, unsigned int channelCount) override {
38
2/6
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
44 return new VirtualDisplayOutput(startChannel, channelCount);
39 }
40 };
41
42 extern "C" {
43 9 FPPPlugins::Plugin* createPlugin() {
44
2/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
18 return new VirtualDisplayPlugin();
45 }
46 }
47
48 /*
49 *
50 */
51 22 VirtualDisplayOutput::VirtualDisplayOutput(unsigned int startChannel,
52 22 unsigned int channelCount) :
53 22 VirtualDisplayBaseOutput(startChannel, channelCount) {
54
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 LogDebug(VB_CHANNELOUT, "VirtualDisplayOutput::VirtualDisplayOutput(%u, %u)\n",
55 startChannel, channelCount);
56 22 }
57
58 /*
59 *
60 */
61 88 VirtualDisplayOutput::~VirtualDisplayOutput() {
62 44 LogDebug(VB_CHANNELOUT, "VirtualDisplayOutput::~VirtualDisplayOutput()\n");
63
64 44 Close();
65 88 }
66
67 /*
68 *
69 */
70 22 int VirtualDisplayOutput::Init(Json::Value config) {
71 22 LogDebug(VB_CHANNELOUT, "VirtualDisplayOutput::Init()\n");
72
73 // Check for old config names
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (config.isMember("ModelName"))
75 ✗ m_modelName = config["ModelName"].asString();
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (config.isMember("PixelSize"))
77 ✗ m_pixelSize = config["PixelSize"].asInt();
78
79 // Check for new config names
80
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (config.isMember("modelName"))
81
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
22 m_modelName = config["modelName"].asString();
82
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (config.isMember("pixelSize"))
83 22 m_pixelSize = config["pixelSize"].asInt();
84
85
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 5 times.
22 if (m_modelName == "") {
86 17 int width = 0;
87 17 int height = 0;
88 17 std::string device;
89
90
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if ((config.isMember("width")) &&
91
4/8
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 17 times.
✗ Branch 7 not taken.
34 (config.isMember("height")) &&
92
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 (config.isMember("device"))) {
93
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 width = config["width"].asInt();
94
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 height = config["height"].asInt();
95
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 device = config["device"].asString();
96
97
3/6
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
51 replaceStart(device, "/dev/");
98
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 m_modelName = "FB - ";
99
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 m_modelName += device;
100
101
7/14
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 17 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 17 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 17 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 17 times.
✗ Branch 13 not taken.
17 if (width && height && (device != "") && !PixelOverlayManager::INSTANCE.getModel(m_modelName)) {
102
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 Json::Value val;
103
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 val["Name"] = m_modelName;
104
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 val["Type"] = "FB";
105
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 val["Width"] = width;
106
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 val["Height"] = height;
107
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 val["PixelSize"] = 1;
108
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 val["Device"] = device;
109
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 val["autoCreated"] = true;
110
111
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 PixelOverlayManager::INSTANCE.addModel(val);
112 17 modelCreated = true;
113 17 } else {
114 ✗ LogErr(VB_CHANNELOUT, "Empty Pixel Overlay Model name\n");
115 ✗ AddOutputConfigWarning(35, "Virtual Matrix output has an empty Pixel Overlay Model name");
116 ✗ return 0;
117 }
118 } else {
119 ✗ LogErr(VB_CHANNELOUT, "Empty Pixel Overlay Model name\n");
120 ✗ AddOutputConfigWarning(35, "Virtual Matrix output has an empty Pixel Overlay Model name");
121 ✗ return 0;
122 }
123
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 }
124
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (m_modelName == "") {
126 ✗ LogErr(VB_CHANNELOUT, "Empty Pixel Overlay Model name\n");
127 ✗ AddOutputConfigWarning(35, "Virtual Matrix output has an empty Pixel Overlay Model name");
128 ✗ return 0;
129 }
130
131 22 m_model = PixelOverlayManager::INSTANCE.getModel(m_modelName);
132
133
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 21 times.
22 if (!m_model) {
134 1 LogErr(VB_CHANNELOUT, "Invalid Pixel Overlay Model: '%s'\n", m_modelName.c_str());
135
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 AddOutputConfigWarning(35, "Invalid Pixel Overlay Model: '" + m_modelName + "'");
136 1 return 0;
137 }
138
139 // the display is drawn as 3 byte RGB, and setData() below reads the
140 // model's own bytes per pixel from it (4 for an RGBW submodel)
141
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19 times.
21 if (m_model->getBytesPerPixel() != 3) {
142 2 LogErr(VB_CHANNELOUT, "Pixel Overlay Model '%s' is not RGB\n", m_modelName.c_str());
143
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 AddOutputConfigWarning(35, "Virtual Display: model '" + m_modelName + "' is not an RGB model");
144 2 m_model = nullptr;
145 2 return 0;
146 }
147
148 19 m_width = m_model->getWidth();
149 19 m_height = m_model->getHeight();
150
151
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
19 config["width"] = m_width;
152
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
19 config["height"] = m_height;
153
154
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 if (!VirtualDisplayBaseOutput::Init(config))
155 ✗ return 0;
156
157 19 size_t bufBytes = virtualDisplayBufferBytes(m_width, m_height, m_bytesPerPixel);
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (bufBytes == 0) {
159 ✗ LogErr(VB_CHANNELOUT, "Invalid virtual display dimensions %dx%d\n",
160 m_width, m_height);
161 ✗ return 0;
162 }
163 19 m_virtualDisplay = (unsigned char*)malloc(bufBytes);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!m_virtualDisplay) {
165 ✗ LogErr(VB_CHANNELOUT, "Unable to malloc buffer\n");
166 ✗ return 0;
167 }
168
169 19 memset(m_virtualDisplay, 0, bufBytes);
170
171 19 int result = InitializePixelMap();
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!result)
173 ✗ return 0;
174
175 // Put the background image onto the model and always call doOverlay
176 // to ensure display is enabled (even if model state is not Disabled).
177 // Only for a framebuffer model: doOverlay() writes the model's channels
178 // at its start channel into the buffer it is given, and a submodel
179 // forwards to its parent at the parent's channels - far past the end of
180 // this display-sized buffer.
181 19 m_model->setData(m_virtualDisplay);
182
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 if (m_model->getType() == "FB") {
183 18 m_model->doOverlay(m_virtualDisplay);
184 }
185
186
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
19 m_modelListenerName = "VirtualDisplay" + std::to_string((uintptr_t)this);
187
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 PixelOverlayManager::INSTANCE.addModelListener(m_modelName, m_modelListenerName,
188 19 [this](PixelOverlayModel* m) {
189
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 std::lock_guard<std::mutex> lock(m_modelLock);
190 // only follow a replacement this display still fits
191
6/10
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 5 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 10 times.
10 if (m && (m->getWidth() != m_width || m->getHeight() != m_height || m->getBytesPerPixel() != 3)) {
192 ✗ LogWarn(VB_CHANNELOUT, "VirtualDisplay: model '%s' changed shape; detaching\n", m_modelName.c_str());
193 ✗ m = nullptr;
194 }
195 10 m_model = m;
196 10 });
197 19 return 1;
198 }
199
200 /*
201 *
202 */
203 41 int VirtualDisplayOutput::Close(void) {
204 41 LogDebug(VB_CHANNELOUT, "VirtualDisplayOutput::Close()\n");
205
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 22 times.
41 if (!m_modelListenerName.empty()) {
206 19 PixelOverlayManager::INSTANCE.removeModelListener(m_modelName, m_modelListenerName);
207 19 m_modelListenerName.clear();
208 }
209 {
210
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 std::lock_guard<std::mutex> lock(m_modelLock);
211 41 m_model = nullptr;
212 41 }
213
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 7 times.
41 if (modelCreated) {
214 34 PixelOverlayManager::INSTANCE.removeAutoOverlayModel(m_modelName);
215 }
216
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 22 times.
41 if (m_virtualDisplay) {
217 19 free(m_virtualDisplay);
218 19 m_virtualDisplay = nullptr;
219 }
220
221 41 return VirtualDisplayBaseOutput::Close();
222 }
223
224 517 int VirtualDisplayOutput::SendData(unsigned char* channelData) {
225
1/2
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
517 LogExcess(VB_CHANNELOUT, "VirtualDisplayOutput::SendData(%p)\n",
226 channelData);
227
1/2
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
517 std::lock_guard<std::mutex> modelLock(m_modelLock);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 517 times.
517 if (!m_model) {
229 ✗ return m_channelCount;
230 }
231 517 unsigned char r = 0;
232 517 unsigned char g = 0;
233 517 unsigned char b = 0;
234 517 int stride = m_width * m_bytesPerPixel;
235 VirtualDisplayPixel pixel;
236
237 517 int tx = 0;
238 517 int ty = 0;
239
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 510 times.
517 int radius = m_pixelSize >= 2 ? m_pixelSize / 2 : 1;
240
241 // Pre-calculate radius squared to avoid sqrt() in hot loop
242 517 int radiusSquared = radius * radius;
243
244
2/2
✓ Branch 0 taken 1167 times.
✓ Branch 1 taken 517 times.
1684 for (int i = 0; i < m_pixels.size(); i++) {
245 1167 pixel = m_pixels[i];
246
247
1/2
✓ Branch 0 taken 1167 times.
✗ Branch 1 not taken.
1167 GetPixelRGB(pixel, channelData, r, g, b);
248
249 // Optimize: For single pixel (radius=1), skip the expensive nested loops
250
2/2
✓ Branch 0 taken 1160 times.
✓ Branch 1 taken 7 times.
1167 if (radius == 1) {
251
1/2
✓ Branch 0 taken 1160 times.
✗ Branch 1 not taken.
1160 m_model->setPixelValue(pixel.xs, pixel.ys, r, g, b);
252 } else {
253
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 7 times.
35 for (int xo = 0 - radius; xo < radius; xo++) {
254
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
140 for (int yo = 0 - radius; yo < radius; yo++) {
255 // Replace expensive sqrt() with integer multiplication
256
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 77 times.
112 if ((xo * xo + yo * yo) > radiusSquared)
257 35 continue;
258
259 77 tx = pixel.xs + xo;
260 77 ty = pixel.ys + yo;
261
262
4/8
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 77 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 77 times.
77 if ((tx < 0) || (ty < 0) || (tx > (m_width - 1)) || (ty > (m_height - 1)))
263 ✗ continue;
264
265
1/2
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
77 m_model->setPixelValue(tx, ty, r, g, b);
266 }
267 }
268 }
269 }
270
1/2
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
517 m_model->setBufferIsDirty(true);
271
2/4
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 517 times.
✗ Branch 3 not taken.
517 if (m_model->getState() == 0) {
272
1/2
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
517 PixelOverlayModelSub* sub = dynamic_cast<PixelOverlayModelSub*>(m_model);
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 517 times.
517 if (sub) {
274 // a submodel's parent writes at its own absolute channels, so it
275 // needs the whole sequence buffer, not a display-sized one; and
276 // only a framebuffer parent draws here, after the outputs ahead
277 // of this one have sent theirs (as FBMatrix::SendData())
278 ✗ if (sub->readyForPassThrough()) {
279 ✗ m_model->doOverlay(channelData - m_startChannel);
280 }
281
2/4
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 517 times.
517 } else if (m_model->getType() != "FB") {
282 ✗ m_model->doOverlay(channelData - m_startChannel);
283
2/4
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 517 times.
✗ Branch 3 not taken.
517 } else if (uint8_t* buf = m_model->getOverlayBuffer()) {
284
1/2
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
517 m_model->doOverlay(buf);
285 }
286 }
287 517 return m_channelCount;
288 517 }
289