GCC Code Coverage Report


Directory: ./
File: overlays/PixelOverlayModel.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 740 999 74.1%
Functions: 56 65 86.2%
Branches: 790 1478 53.5%

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 LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include <sys/stat.h>
16 #include <atomic>
17 #include <unordered_map>
18
19 #include "fpp-json.h"
20 #include "Warnings.h"
21
22 #if __has_include(<sys/posix_shm.h>)
23 #include <sys/posix_shm.h>
24 #else
25 #include <limits.h>
26 #define PSHMNAMLEN NAME_MAX
27 #endif
28
29 #include <Magick++/Blob.h>
30 #include <Magick++/Color.h>
31 #include <Magick++/Geometry.h>
32 #include <Magick++/Image.h>
33 #include <sys/mman.h>
34 #include <sys/time.h>
35 #include <algorithm>
36 #include <cstdint>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <list>
41 #include <set>
42 #include <memory>
43 #include <mutex>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <string>
48 #include <time.h>
49 #include <unistd.h>
50 #include <utility>
51 #include <vector>
52
53 #include "../Plugins.h"
54 #include "../Sequence.h"
55 #include "../commands/Commands.h"
56 #include "../common.h"
57 #include "../effects.h"
58 #include "../log.h"
59 #include "../settings.h"
60
61 #include "PixelOverlay.h"
62 #include "PixelOverlayEffects.h"
63 #include "PixelOverlayModel.h"
64 #include "PixelOverlayModelSub.h"
65 #include "../channeloutput/channeloutputthread.h"
66
67 // The channel order a model reads and writes. This is the order of fppd's
68 // channel data, not of the wire: setPixelValue() stores r, then g, then b,
69 // then w into consecutive channels, and the output driver is what permutes
70 // those into the hardware's colour order. It therefore follows entirely from
71 // how many channels a node occupies.
72 1796 static const char* ChannelDataOrder(int channelsPerNode) {
73
3/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1780 times.
✓ Branch 3 taken 15 times.
1796 switch (channelsPerNode) {
74 1 case 1:
75 1 return "W";
76 ✗ case 2:
77 ✗ return "RG";
78 1780 case 3:
79 1780 return "RGB";
80 15 default:
81 15 return "RGBW";
82 }
83 }
84
85 // The shm names this fppd has mapped. fppd never destroys the models at exit
86 // (a restart execlp()s over itself, a stop ends in _exit(), and a crash or kill
87 // skips everything), so their destructors never unlink the blocks, and the next
88 // fppd mapped one with the last session's pixels still in it. A block that
89 // already exists is cleared the first time this fppd maps its name; a new one
90 // is zero already and is left untouched, so its pages aren't all committed in
91 // tmpfs. Cleared rather than unlinked: a program outside fppd that maps a
92 // model by name keeps writing to the same block across restarts, as it always
93 // has. A second live model under the same name (a duplicate, or a name cut
94 // short to PSHMNAMLEN) shares the block and doesn't clear the first one's.
95 static std::mutex s_modelMemoryNamesLock;
96 static std::set<std::string> s_modelMemoryNamesMapped;
97
98 // Both of a model's buffers (the channel data and the overlay buffer) are
99 // allocated the same way and with the same fallbacks, so they share this.
100 //
101 // The buffer is shared memory when it can be, because external programs read
102 // it; when it cannot be, a private anonymous mapping is just as good to us.
103 // Each step is checked, and the caller is told which one won so it can release
104 // the buffer the way it was allocated -- munmap() on a malloc()ed pointer is
105 // not a no-op.
106 //
107 // Returns nullptr only if every allocator failed, which means the box is out of
108 // memory. Callers must handle that: a model with no buffer draws nothing, which
109 // is survivable, and the alternative here used to be a wild write through a
110 // null pointer.
111 1927 static uint8_t* createModelMemory(const std::string& dataName, uint32_t size, bool& mapped) {
112 1927 mapped = true;
113
2/2
✓ Branch 0 taken 242 times.
✓ Branch 1 taken 1685 times.
1927 if (size < 8) {
114 // cannot mmap 0 size which some models happen to have
115 242 size = 8;
116 }
117
118 1927 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
119 // No name: private memory, for a model that has given its names up.
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1927 times.
1927 int f = dataName.empty() ? -1 : shm_open(dataName.c_str(), O_RDWR | O_CREAT, mode);
121 1927 bool existed = false;
122
2/2
✓ Branch 0 taken 1925 times.
✓ Branch 1 taken 2 times.
1927 if (f != -1) {
123 struct stat st;
124
3/4
✓ Branch 0 taken 1925 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 453 times.
✓ Branch 3 taken 1472 times.
1925 existed = fstat(f, &st) == 0 && st.st_size > 0;
125 1925 int rc = ftruncate(f, size);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1925 times.
1925 if (rc == -1) {
127 // if ftruncate fails, we need to completely reset
128 ✗ close(f);
129 ✗ shm_unlink(dataName.c_str());
130 ✗ existed = false;
131 ✗ f = shm_open(dataName.c_str(), O_RDWR | O_CREAT, mode);
132 ✗ if (f != -1 && ftruncate(f, size) == -1) {
133 ✗ close(f);
134 ✗ f = -1;
135 }
136 }
137 }
138
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1925 times.
1927 if (f == -1) {
139
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!dataName.empty()) {
140 2 LogWarn(VB_CHANNELOUT, "Could not create shared memory block for %s: %s\n",
141 dataName.c_str(), FPPstrerror(errno));
142 }
143 } else {
144 1925 uint8_t* data = (uint8_t*)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, f, 0);
145 1925 close(f);
146
1/2
✓ Branch 0 taken 1925 times.
✗ Branch 1 not taken.
1925 if (data != MAP_FAILED) {
147
1/2
✓ Branch 0 taken 1925 times.
✗ Branch 1 not taken.
1925 std::lock_guard<std::mutex> l(s_modelMemoryNamesLock);
148
7/8
✓ Branch 0 taken 1925 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1644 times.
✓ Branch 3 taken 281 times.
✓ Branch 4 taken 453 times.
✓ Branch 5 taken 1191 times.
✓ Branch 6 taken 453 times.
✓ Branch 7 taken 1472 times.
1925 if (s_modelMemoryNamesMapped.insert(dataName).second && existed) {
149 453 memset(data, 0, size);
150 }
151 1925 return data;
152 1925 }
153 ✗ LogWarn(VB_CHANNELOUT, "Could not mmap shared memory for %s: %s\n",
154 dataName.c_str(), FPPstrerror(errno));
155 ✗ shm_unlink(dataName.c_str());
156 }
157
158 // Private memory instead: external programs lose their view of this model,
159 // but fppd itself works normally. MAP_ANON has to be paired with one of
160 // MAP_PRIVATE/MAP_SHARED -- on its own mmap() returns EINVAL, so for as
161 // long as that pairing was missing this fallback could not succeed and
162 // every failure here landed on the unchecked malloc() below.
163 2 uint8_t* data = (uint8_t*)mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
164
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (data != MAP_FAILED) {
165 2 return data;
166 }
167
168 ✗ mapped = false;
169 ✗ data = (uint8_t*)malloc(size);
170 ✗ if (data) {
171 // mmap() hands back zeroed pages and callers rely on that; malloc() does not.
172 ✗ memset(data, 0, size);
173 } else {
174 ✗ LogErr(VB_CHANNELOUT, "Out of memory allocating %u bytes for %s\n", size, dataName.c_str());
175 }
176 ✗ return data;
177 }
178
179 // Serializes the lazy overlay buffer allocation (see getOverlayBuffer()) and
180 // guards s_namesReleased.
181 static std::mutex s_overlayBufferAllocLock;
182 // Models that have given up their shm names (releaseSharedMemoryNames()). A
183 // member would change the plugin-visible layout. The destructor erases its
184 // entry, so a new model at the same address starts with its names.
185 static std::set<const PixelOverlayModel*> s_namesReleased;
186
187 // The shm name for a model buffer, shortened where the system's limit demands it.
188 2760 static std::string modelMemoryName(const std::string& prefix, const std::string& shortPrefix,
189 const std::string& name) {
190 if (PSHMNAMLEN > 48) {
191
1/2
✓ Branch 0 taken 2760 times.
✗ Branch 1 not taken.
2760 return prefix + name;
192 }
193 // system doesn't allow very long shared memory names, we'll use a shortened form
194 std::string n = shortPrefix + name;
195 if (n.size() > PSHMNAMLEN) {
196 n = n.substr(0, PSHMNAMLEN);
197 }
198 return n;
199 }
200
201 // Model definitions come from model-overlays.json, which POST /api/models
202 // (xLights' FPP Connect) writes unchecked, and from xlights-submodels.json.
203 // jsoncpp's asInt()/asString() throw on the wrong type, and nothing above the
204 // constructor catches - at boot that was a crash on every start. These read a
205 // field the way a hand-edited file might hold it (a number, or a number in a
206 // string) and report anything else instead of throwing.
207 14462 bool PixelOverlayModel::configInt(const Json::Value& c, const char* key, int dflt, int& out) {
208
5/6
✓ Branch 0 taken 8853 times.
✓ Branch 1 taken 5609 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8853 times.
✓ Branch 4 taken 5609 times.
✓ Branch 5 taken 8853 times.
14462 if (!c.isMember(key) || c[key].isNull()) {
209 5609 out = dflt;
210 5609 return true;
211 }
212 8853 const Json::Value& v = c[key];
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8853 times.
8853 if (v.isBool()) {
214 ✗ out = v.asBool() ? 1 : 0;
215 ✗ return true;
216 }
217
2/2
✓ Branch 0 taken 8852 times.
✓ Branch 1 taken 1 times.
8853 if (v.isNumeric()) {
218 8852 double d = v.asDouble();
219
2/4
✓ Branch 0 taken 8852 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8852 times.
8852 if (d < INT_MIN || d > INT_MAX) {
220 ✗ return false;
221 }
222 8852 out = (int)d;
223 8852 return true;
224 }
225
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (v.isString()) {
226
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::string str = v.asString();
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (str.empty()) {
228 ✗ out = dflt;
229 ✗ return true;
230 }
231 1 char* end = nullptr;
232 1 errno = 0;
233 1 long l = strtol(str.c_str(), &end, 10);
234
2/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1 if (errno || *end != '\0' || l < INT_MIN || l > INT_MAX) {
235 1 return false;
236 }
237 ✗ out = (int)l;
238 ✗ return true;
239 1 }
240 ✗ return false;
241 }
242 10885 bool PixelOverlayModel::configString(const Json::Value& c, const char* key, std::string& out) {
243
5/6
✓ Branch 0 taken 8861 times.
✓ Branch 1 taken 2024 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8861 times.
✓ Branch 4 taken 2024 times.
✓ Branch 5 taken 8861 times.
10885 if (!c.isMember(key) || c[key].isNull()) {
244 2024 out.clear();
245 2024 return true;
246 }
247 8861 const Json::Value& v = c[key];
248
5/6
✓ Branch 0 taken 8859 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8859 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 8859 times.
8861 if (v.isObject() || v.isArray()) {
249 2 return false;
250 }
251
1/2
✓ Branch 0 taken 8859 times.
✗ Branch 1 not taken.
8859 out = v.asString();
252 8859 return true;
253 }
254
255 // Logs why and releases channelData, so isValid() is false and the manager
256 // drops the model. For constructors only.
257 7 void PixelOverlayModel::markInvalid(const std::string& why) {
258 7 LogErr(VB_CHANNELOUT, "Overlay model '%s' is not usable: %s\n", name.c_str(), why.c_str());
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (channelData) {
260 ✗ if (channelDataMapped) {
261 ✗ munmap(channelData, std::max(channelCount, 8));
262 ✗ shm_unlink(modelMemoryName("/FPP-Model-Data-", "/FPPMD-", name).c_str());
263 } else {
264 ✗ free(channelData);
265 }
266 ✗ channelData = nullptr;
267 }
268 7 }
269
270 // Largest buffer a model may describe, in cells (pixels). Above this the
271 // channel map alone would run to hundreds of MB.
272 static constexpr int64_t MAX_MODEL_CELLS = 4096 * 1024;
273
274 1798 PixelOverlayModel::PixelOverlayModel(const Json::Value& c) :
275
1/2
✓ Branch 0 taken 1798 times.
✗ Branch 1 not taken.
1798 config(c),
276 1798 overlayBufferData(nullptr),
277 1798 channelData(nullptr),
278 3596 runningEffect(nullptr) {
279 // Any definition this constructor cannot represent safely leaves
280 // channelData null and returns: isValid() is then false and the manager
281 // drops the model, with the reason in the log. Nothing below may throw.
282
2/4
✓ Branch 0 taken 1798 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1798 times.
1798 if (!config.isObject()) {
283 ✗ markInvalid("its definition is not a JSON object");
284 7 return;
285 }
286 1798 std::string orientation;
287 1798 std::string startCorner;
288 1798 int strings = 0;
289 1798 int sps = 0;
290 1798 int cfgWidth = 0;
291 1798 int cfgHeight = 0;
292 1798 int pixelSize = 0;
293
3/6
✓ Branch 0 taken 1798 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1797 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1797 times.
✗ Branch 5 not taken.
3595 if (!configString(config, "Name", name) || !configString(config, "Type", type) ||
294
2/4
✓ Branch 0 taken 1797 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1797 times.
✗ Branch 3 not taken.
1797 !configString(config, "BufferStyle", bufferStyle) ||
295
8/12
✓ Branch 0 taken 1797 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1797 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1797 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1797 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1797 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 1797 times.
3595 !configString(config, "Orientation", orientation) || !configString(config, "StartCorner", startCorner)) {
296
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 markInvalid("a text field (Name, Type, BufferStyle, Orientation or StartCorner) is not text");
297 1 return;
298 }
299
2/4
✓ Branch 0 taken 1797 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1797 times.
✗ Branch 3 not taken.
1797 name = normalizedName(name);
300
3/6
✓ Branch 0 taken 1797 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1796 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1796 times.
✗ Branch 5 not taken.
3593 if (!configInt(config, "StartChannel", 0, startChannel) || !configInt(config, "ChannelCount", 0, channelCount) ||
301
4/8
✓ Branch 0 taken 1796 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1796 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1796 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1796 times.
✗ Branch 7 not taken.
1796 !configInt(config, "StringCount", 0, strings) || !configInt(config, "StrandsPerString", 0, sps) ||
302
2/4
✓ Branch 0 taken 1796 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1796 times.
✗ Branch 3 not taken.
1796 !configInt(config, "ChannelCountPerNode", 3, channelsPerNode) ||
303
8/12
✓ Branch 0 taken 1796 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1796 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1796 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1796 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1796 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 1796 times.
5389 !configInt(config, "Width", 0, cfgWidth) || !configInt(config, "Height", 0, cfgHeight) ||
304
2/4
✓ Branch 0 taken 1796 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1796 times.
1796 !configInt(config, "PixelSize", 0, pixelSize)) {
305
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 markInvalid("a numeric field is not a number");
306 1 return;
307 }
308 1796 startChannel--; // need to be 0 based
309
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 1711 times.
1796 if (sps == 0) {
310 85 sps = 1;
311 }
312
313 // An empty or one-letter StartCorner used to index past the string.
314
4/4
✓ Branch 0 taken 1711 times.
✓ Branch 1 taken 85 times.
✓ Branch 2 taken 1139 times.
✓ Branch 3 taken 572 times.
1796 bool TtoB = startCorner.size() < 1 || startCorner[0] == 'T';
315
3/4
✓ Branch 0 taken 1711 times.
✓ Branch 1 taken 85 times.
✓ Branch 2 taken 1711 times.
✗ Branch 3 not taken.
1796 bool LtoR = startCorner.size() < 2 || startCorner[1] == 'L';
316
317
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1795 times.
1796 if (channelsPerNode == 0) {
318 1 channelsPerNode = 1;
319 }
320
3/4
✓ Branch 0 taken 1796 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 82 times.
✓ Branch 3 taken 1714 times.
1796 if (type == "FB") {
321 // The framebuffer copies channelData as packed RGB, width*height*3
322 // bytes; any other node width under- or over-ran it.
323 82 channelsPerNode = 3;
324 }
325
2/4
✓ Branch 0 taken 1796 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1796 times.
1796 if (channelsPerNode < 1 || channelsPerNode > 32) {
326 ✗ markInvalid("ChannelCountPerNode " + std::to_string(channelsPerNode) + " is out of range (1-32)");
327 ✗ return;
328 }
329
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1781 times.
1796 bytesPerPixel = (channelsPerNode >= 4) ? 4 : 3;
330
331 // A model addresses fppd's channel data, which is always R,G,B[,W] - the
332 // output driver is what applies the hardware wire order. So the order the
333 // model sees is fully determined by the node width and is derived here
334 // rather than stored: any ColorOrder in the saved config is overwritten.
335 // Exported read-only through the API so the UI can show it.
336
2/4
✓ Branch 0 taken 1796 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1796 times.
✗ Branch 3 not taken.
1796 config["ColorOrder"] = ChannelDataOrder(channelsPerNode);
337
338
3/4
✓ Branch 0 taken 1796 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✓ Branch 3 taken 1684 times.
1796 if (type != "Channel") {
339
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 111 times.
112 if (pixelSize > 1) {
340 1 width = cfgWidth / pixelSize;
341 1 height = cfgHeight / pixelSize;
342 } else {
343 111 width = cfgWidth;
344 111 height = cfgHeight;
345 }
346
3/6
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 112 times.
112 if (width < 1 || height < 1 || (int64_t)width * height > MAX_MODEL_CELLS) {
347 ✗ markInvalid("size " + std::to_string(width) + "x" + std::to_string(height) + " is out of range");
348 ✗ return;
349 }
350
2/4
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
112 config["ChannelCount"] = channelCount = width * height * channelsPerNode;
351
2/4
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
112 config["StringCount"] = strings = height;
352
2/4
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
112 config["StrandsPerString"] = sps = 1;
353
2/4
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
112 config["StartChannel"] = startChannel = 0;
354
3/6
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 112 times.
✗ Branch 5 not taken.
112 config["Orientation"] = orientation = "horizontal";
355
3/6
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 112 times.
✗ Branch 5 not taken.
112 config["StartCorner"] = startCorner = "TL";
356 112 TtoB = true;
357 112 LtoR = true;
358 } else {
359 // doOverlay() copies channelCount bytes to channels[startChannel], in
360 // a buffer that ends at FPPD_MAX_CHANNELS: StartChannel 0 wrote
361 // channels[-1], and a range past the end wrote past the buffer, every
362 // frame, on the channel output thread.
363
3/4
✓ Branch 0 taken 1682 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1682 times.
✗ Branch 3 not taken.
1684 if (startChannel < 0 || channelCount < 1 ||
364
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1681 times.
1682 (int64_t)startChannel + channelCount > FPPD_MAX_CHANNELS) {
365
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
9 markInvalid("channels " + std::to_string(startChannel + 1) + " + " + std::to_string(channelCount) +
366
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
12 " do not fit in 1-" + std::to_string(FPPD_MAX_CHANNELS));
367 3 return;
368 }
369
3/4
✓ Branch 0 taken 1679 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1679 times.
1681 if (strings < 0 || sps < 0) {
370
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 markInvalid("StringCount and StrandsPerString must not be negative");
371 2 return;
372 }
373 1679 int64_t h = (int64_t)strings * sps;
374
2/4
✓ Branch 0 taken 1679 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1679 times.
✗ Branch 3 not taken.
1679 height = (h < 1 || h > INT_MAX) ? 1 : (int)h;
375 // Round up: strings needn't be the same length (350 + 375 nodes on
376 // two strings), and truncating dropped the last nodes of the model.
377 // The spare cells of the last row/column are switched off below.
378 1679 width = channelCount / channelsPerNode;
379 1679 width = (width + height - 1) / height;
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1679 times.
1679 if (width == 0) {
381 ✗ width = 1;
382 }
383 }
384
385
4/8
✓ Branch 0 taken 1791 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1791 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1791 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1182 times.
✗ Branch 7 not taken.
5982 channelData = createModelMemory(modelMemoryName("/FPP-Model-Data-", "/FPPMD-", name),
386
1/2
✓ Branch 0 taken 609 times.
✗ Branch 1 not taken.
1791 channelCount, channelDataMapped);
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1791 times.
1791 if (!channelData) {
388 ✗ WarningHolder::AddWarning(WARNING_ID_MODEL_MEMORY, MODEL_MEMORY_WARNING);
389 }
390
391
7/10
✓ Branch 0 taken 1791 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1791 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1791 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1790 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1790 times.
1791 if (orientation == "V" || orientation == "vertical") {
392
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 channelMap.resize(width * height * bytesPerPixel);
393
394 1 std::swap(width, height);
395
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int x = 0; x < width; x++) {
396 2 int segment = x % sps;
397
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int y = 0; y < height; y++) {
398 // pos in the normal overlay buffer
399 6 int ppos = y * width + x;
400 // Relative Input Pixel first channel
401 6 int inCh = (ppos * bytesPerPixel);
402
403 // X position in output
404
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 int outX = (LtoR) ? x : width - x - 1;
405 // Y position in output
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 int outY = (TtoB == (segment % 2)) ? height - y - 1 : y;
407
408 // Relative Mapped Output Pixel first channel
409 6 int mpos = outX * height + outY;
410 6 int outCh = (mpos * channelsPerNode);
411
412
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
24 for (int cho = 0; cho < bytesPerPixel; cho++) {
413
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (cho < channelsPerNode) {
414 18 channelMap[inCh + cho] = outCh + cho;
415 } else {
416 ✗ channelMap[inCh + cho] = FPPD_OFF_CHANNEL;
417 }
418 }
419 }
420 }
421
7/10
✓ Branch 0 taken 1790 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1790 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1790 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 1785 times.
✓ Branch 8 taken 5 times.
✓ Branch 9 taken 1785 times.
1790 } else if (orientation == "C" || orientation == "custom") {
422 // Two formats supported:
423 // "data": dense format "n,n,n;n,n,n|n,n,n;n,n,n" (comma=col, semi=row, pipe=layer)
424 // "compressedData": sparse format "node,row,col[,layer];..." (only non-empty cells)
425 5 std::string compressed;
426 5 std::string dense;
427
5/10
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ 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 5 times.
5 if (!configString(config, "compressedData", compressed) || !configString(config, "data", dense)) {
428 ✗ markInvalid("custom model data is not text");
429 ✗ return;
430 }
431
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!compressed.empty()) {
432 // Parse compressed format: "node,row,col[,layer];node,row,col[,layer];..."
433 ✗ std::vector<std::string> entries = split(compressed, ';');
434
435 struct NodeEntry {
436 int node, row, col, layer;
437 };
438 ✗ std::vector<NodeEntry> nodes;
439 ✗ nodes.reserve(entries.size());
440
441 ✗ int maxRow = 0, maxCol = 0, maxLayer = 0;
442 ✗ for (auto& entry : entries) {
443 ✗ std::vector<std::string> parts = split(entry, ',');
444 ✗ if (parts.size() >= 3) {
445 NodeEntry ne;
446 ✗ ne.node = std::atoi(parts[0].c_str());
447 ✗ ne.row = std::atoi(parts[1].c_str());
448 ✗ ne.col = std::atoi(parts[2].c_str());
449 ✗ ne.layer = (parts.size() >= 4) ? std::atoi(parts[3].c_str()) : 0;
450 // Negative row/col/layer indexed channelMap before its
451 // start (a heap write), and huge ones overflowed the int
452 // size below; no real model is 4096 cells on a side.
453 ✗ if (ne.node > 0 && ne.node <= FPPD_MAX_CHANNEL_NUM &&
454 ✗ ne.row >= 0 && ne.row < 4096 && ne.col >= 0 && ne.col < 4096 &&
455 ✗ ne.layer >= 0 && ne.layer < 4096) {
456 ✗ nodes.push_back(ne);
457 ✗ maxRow = std::max(maxRow, ne.row);
458 ✗ maxCol = std::max(maxCol, ne.col);
459 ✗ maxLayer = std::max(maxLayer, ne.layer);
460 }
461 }
462 ✗ }
463
464 ✗ width = maxCol + 1;
465 ✗ int64_t allLayers = (int64_t)(maxRow + 1) * (maxLayer + 1);
466 ✗ height = allLayers > INT_MAX ? INT_MAX : (int)allLayers;
467 ✗ if (width < 1) width = 1;
468 ✗ if (height < 1) height = 1;
469 ✗ if ((int64_t)width * height > (600 * 600)) {
470 // Limit to first layer only
471 ✗ height = maxRow + 1;
472 ✗ if (height < 1) height = 1;
473 }
474 ✗ if ((int64_t)width * height > MAX_MODEL_CELLS) {
475 ✗ markInvalid("custom model is " + std::to_string(width) + "x" + std::to_string(height) + ", too large");
476 ✗ return;
477 }
478
479 ✗ channelMap.resize(width * height * bytesPerPixel);
480 // Initialize all to FPPD_OFF_CHANNEL
481 ✗ std::fill(channelMap.begin(), channelMap.end(), FPPD_OFF_CHANNEL);
482
483 ✗ int layerHeight = maxRow + 1;
484 ✗ for (auto& ne : nodes) {
485 ✗ int y = ne.row + ne.layer * layerHeight;
486 ✗ if (y >= height || ne.col >= width) {
487 ✗ continue;
488 }
489 ✗ for (int cho = 0; cho < bytesPerPixel; cho++) {
490 ✗ if (cho < channelsPerNode) {
491 ✗ channelMap[(y * width * bytesPerPixel) + (ne.col * bytesPerPixel) + cho] = (ne.node - 1) * channelsPerNode + cho;
492 }
493 }
494 }
495 ✗ } else {
496 // Parse dense format: "n,n,n;n,n,n|n,n,n;n,n,n"
497
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 std::string customData = ",;,";
498
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if (config.isMember("data")) {
499
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 customData = dense;
500 }
501 5 int endOfFirstLayer = 0;
502
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 std::vector<std::string> layers = split(customData, '|');
503 5 std::vector<std::vector<std::string>> allData;
504 5 width = 1;
505 5 height = 0; // counted from the rows below, not StringCount
506
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
11 for (auto& layer : layers) {
507
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 std::vector<std::string> lines = split(layer, ';');
508
2/2
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 7 times.
617 for (auto& l : lines) {
509
2/4
✓ Branch 0 taken 610 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 610 times.
✗ Branch 3 not taken.
610 allData.push_back(split(l, ','));
510 610 width = std::max(width, (int)allData.back().size());
511 610 height++;
512 }
513
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 if (endOfFirstLayer == 0) {
514 5 endOfFirstLayer = allData.size();
515 }
516 7 lines.clear();
517
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 if ((int64_t)width * height > (600 * 600)) {
518 1 height = endOfFirstLayer;
519
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 allData.erase(allData.begin() + endOfFirstLayer, allData.end());
520 1 break;
521 }
522
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 }
523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (height < 1) {
524 ✗ height = 1;
525 }
526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if ((int64_t)width * height > MAX_MODEL_CELLS) {
527 ✗ markInvalid("custom model is " + std::to_string(width) + "x" + std::to_string(height) + ", too large");
528 ✗ return;
529 }
530
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 channelMap.resize(width * height * bytesPerPixel);
531
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
15 for (int y = 0; y < height; y++) {
532
2/2
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 10 times.
730 for (int x = 0; x < width; x++) {
533 720 int node = -1;
534
5/6
✓ Branch 0 taken 720 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 698 times.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 698 times.
720 if (y < allData.size() && x < allData[y].size()) {
535 22 std::string& s = allData[y][x];
536
8/10
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 20 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 20 times.
22 if (s.empty() || s.length() > 10 || s == " ") {
537 2 node = -1;
538 } else {
539 20 node = std::atoi(s.c_str());
540
6/8
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 19 times.
20 if (node == 0 && s != "0") {
541 1 node = -1;
542 }
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (node > FPPD_MAX_CHANNEL_NUM) {
544 ✗ node = -1;
545 }
546 }
547 }
548
549
2/2
✓ Branch 0 taken 2160 times.
✓ Branch 1 taken 720 times.
2880 for (int cho = 0; cho < bytesPerPixel; cho++) {
550
3/4
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 2103 times.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
2160 if (node != -1 && cho < channelsPerNode) {
551 57 channelMap[(y * width * bytesPerPixel) + (x * bytesPerPixel) + cho] = (node - 1) * channelsPerNode + cho;
552 } else {
553 2103 channelMap[(y * width * bytesPerPixel) + (x * bytesPerPixel) + cho] = FPPD_OFF_CHANNEL;
554 }
555 }
556 }
557 }
558
3/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 }
559
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 } else {
560
1/2
✓ Branch 0 taken 1785 times.
✗ Branch 1 not taken.
1785 channelMap.resize(width * height * bytesPerPixel);
561
2/2
✓ Branch 0 taken 14866 times.
✓ Branch 1 taken 1785 times.
16651 for (int y = 0; y < height; y++) {
562 14866 int segment = y % sps;
563
2/2
✓ Branch 0 taken 1763917 times.
✓ Branch 1 taken 14866 times.
1778783 for (int x = 0; x < width; x++) {
564 // pos in the normal overlay buffer
565 1763917 int ppos = y * width + x;
566 // Input Pixel first channel
567 1763917 int inCh = (ppos * bytesPerPixel);
568
569 // X position in output
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1763917 times.
1763917 int outX = (LtoR == (segment % 2)) ? width - x - 1 : x;
571 // Y position in output
572
2/2
✓ Branch 0 taken 1744108 times.
✓ Branch 1 taken 19809 times.
1763917 int outY = (TtoB) ? y : height - y - 1;
573
574 // Relative Mapped Output Pixel first channel
575 1763917 int mpos = outY * width + outX;
576 1763917 int outCh = (mpos * channelsPerNode);
577
578
2/2
✓ Branch 0 taken 5294147 times.
✓ Branch 1 taken 1763917 times.
7058064 for (int cho = 0; cho < bytesPerPixel; cho++) {
579
2/2
✓ Branch 0 taken 5294087 times.
✓ Branch 1 taken 60 times.
5294147 if (cho < channelsPerNode) {
580 5294087 channelMap[inCh + cho] = outCh + cho;
581 } else {
582 60 channelMap[inCh + cho] = FPPD_OFF_CHANNEL;
583 }
584 }
585 }
586 }
587 }
588
589 // channelData is exactly channelCount bytes, but a custom model's node
590 // numbers come straight from the config and can address well past it.
591 // Every setData()/doOverlay() indexes channelData through channelMap, so
592 // enforce the bound once here rather than at each use.
593 // A grid model whose node count doesn't divide evenly by its strings is
594 // rounded up, so its spare cells land here too; those are expected and
595 // not worth a warning.
596 1791 int outOfRange = 0;
597
2/2
✓ Branch 0 taken 5296325 times.
✓ Branch 1 taken 1791 times.
5298116 for (auto& ch : channelMap) {
598
4/4
✓ Branch 0 taken 5294162 times.
✓ Branch 1 taken 2163 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 5294153 times.
5296325 if (ch != FPPD_OFF_CHANNEL && ch >= (uint32_t)channelCount) {
599 9 ch = FPPD_OFF_CHANNEL;
600 9 outOfRange++;
601 }
602 }
603
11/16
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1788 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 1 times.
✓ Branch 14 taken 2 times.
✓ Branch 15 taken 1789 times.
1791 if (outOfRange && type == "Channel" && orientation != "C" && orientation != "custom") {
604 2 int64_t padding = ((int64_t)width * height - channelCount / channelsPerNode) * std::min(channelsPerNode, bytesPerPixel);
605
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (outOfRange <= padding) {
606 2 outOfRange = 0;
607 }
608 }
609
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1790 times.
1791 if (outOfRange) {
610
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_CHANNELOUT, "Overlay model '%s': %d node channel(s) exceed the model's %d channels and were disabled\n",
611 name.c_str(), outOfRange, channelCount);
612 }
613
4/4
✓ Branch 0 taken 1791 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 1791 times.
✓ Branch 3 taken 7 times.
1963 }
614 3218 PixelOverlayModel::~PixelOverlayModel() {
615 {
616 // The running effect used to be left behind, so every output reload
617 // leaked whatever was running on an auto-created model (a WLED effect
618 // is a whole strip). Nothing else can reach it now: the effect thread
619 // was drained (removeAutoOverlayModel) or stopped (loadModelMap,
620 // shutdown) before the model is freed.
621 1646 std::unique_lock<std::recursive_mutex> l(effectLock);
622
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 803 times.
1646 delete runningEffect;
623 1646 runningEffect = nullptr;
624 1646 }
625 // A retired model can be freed long after its replacement took its name,
626 // so nothing here may act on the name: the shm names were unlinked at
627 // retirement if it was retired. Nor does it reset its submodels: the
628 // manager has reset them by parent name at retirement, and a full reload
629 // or shutdown frees them along with it. Resetting them here, by
630 // name, disabled the replacement's submodel, and took modelsLock on
631 // whatever thread dropped the last reference -- the effect thread holds a
632 // submodel's effectLock there, the reverse of the sanctioned order.
633 bool namesReleased;
634 {
635 1646 std::lock_guard<std::mutex> l(s_overlayBufferAllocLock);
636 1646 namesReleased = s_namesReleased.erase(this) != 0;
637 1646 }
638
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 7 times.
1646 if (channelData) {
639
1/2
✓ Branch 0 taken 816 times.
✗ Branch 1 not taken.
1632 if (channelDataMapped) {
640 1632 munmap(channelData, std::max(channelCount, 8));
641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 816 times.
1632 if (!namesReleased) {
642 ✗ shm_unlink(modelMemoryName("/FPP-Model-Data-", "/FPPMD-", name).c_str());
643 }
644 } else {
645 ✗ free(channelData);
646 }
647 }
648
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 807 times.
1646 if (overlayBufferData) {
649
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 if (overlayBufferMapped) {
650 32 munmap(overlayBufferData, width * height * bytesPerPixel + sizeof(OverlayBufferData));
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
32 if (!namesReleased) {
652 ✗ shm_unlink(modelMemoryName("/FPP-Model-Overlay-Buffer-", "/FPPMB-", name).c_str());
653 }
654 } else {
655 ✗ free(overlayBufferData);
656 }
657 }
658 3218 }
659
660 3602 std::string PixelOverlayModel::normalizedName(std::string name) {
661
3/6
✓ Branch 0 taken 3602 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3602 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3602 times.
✗ Branch 5 not taken.
10806 replaceAll(name, "/", "_");
662 3602 return name;
663 }
664
665 1799 bool PixelOverlayModel::configName(const Json::Value& c, std::string& out) {
666
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1798 times.
1799 if (!configString(c, "Name", out)) {
667 1 return false;
668 }
669
2/4
✓ Branch 0 taken 1798 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1798 times.
✗ Branch 3 not taken.
1798 out = normalizedName(out);
670 1798 return true;
671 }
672
673 ✗ void PixelOverlayModel::abandonSharedMemoryNames() {
674 ✗ std::lock_guard<std::mutex> l(s_overlayBufferAllocLock);
675 ✗ s_namesReleased.insert(this);
676 ✗ }
677
678 817 void PixelOverlayModel::releaseSharedMemoryNames() {
679 // Under the allocation lock, so a buffer allocated concurrently either
680 // exists to be unlinked here or sees the release and stays private.
681
1/2
✓ Branch 0 taken 817 times.
✗ Branch 1 not taken.
817 std::lock_guard<std::mutex> l(s_overlayBufferAllocLock);
682
2/4
✓ Branch 0 taken 817 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 817 times.
817 if (!s_namesReleased.insert(this).second) {
683 ✗ return;
684 }
685
2/4
✓ Branch 0 taken 817 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 817 times.
✗ Branch 3 not taken.
817 if (channelData && channelDataMapped) {
686
4/8
✓ Branch 0 taken 817 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 817 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 817 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 817 times.
✗ Branch 7 not taken.
3480 shm_unlink(modelMemoryName("/FPP-Model-Data-", "/FPPMD-", name).c_str());
687 }
688
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 801 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
817 if (overlayBufferData && overlayBufferMapped) {
689
4/8
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
80 shm_unlink(modelMemoryName("/FPP-Model-Overlay-Buffer-", "/FPPMB-", name).c_str());
690 }
691
1/2
✓ Branch 0 taken 817 times.
✗ Branch 1 not taken.
817 }
692
693 817 bool PixelOverlayModel::isAutoCreated() {
694
3/6
✓ Branch 0 taken 817 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 817 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 817 times.
✗ Branch 5 not taken.
817 return config.isMember("autoCreated") && config["autoCreated"].isBool() && config["autoCreated"].asBool();
695 }
696
697 // The state is set from the API, commands and effect threads, and read on
698 // the output thread and by anything reporting it, not all under one lock.
699 // atomic_ref rather than an atomic member: this class's layout is compiled
700 // into plugins (see the header).
701 1171646 PixelOverlayState PixelOverlayModel::getState() const {
702 1171646 return std::atomic_ref<PixelOverlayState>(const_cast<PixelOverlayState&>(state)).load(std::memory_order_relaxed);
703 }
704 240021 bool PixelOverlayModel::needsOutput() const {
705
6/8
✓ Branch 0 taken 240021 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 126704 times.
✓ Branch 3 taken 113317 times.
✓ Branch 4 taken 126704 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7372 times.
✓ Branch 7 taken 119332 times.
240021 return getState().getState() != PixelOverlayState::PixelState::Disabled || hasChildren();
706 }
707
708 342082 void PixelOverlayModel::setState(const PixelOverlayState& st) {
709
3/4
✓ Branch 0 taken 342082 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 116725 times.
✓ Branch 3 taken 225357 times.
342082 if (st == getState())
710 116725 return;
711
712 // Give the buffer a little time to flush before disabling -- but not on
713 // the output thread, which is what flushes it: an effect that ends by
714 // disabling its model after the next output (EFFECT_AFTER_NEXT_OUTPUT
715 // runs there) waited out the whole 100 ms, and every output stalled.
716
10/12
✓ Branch 0 taken 112247 times.
✓ Branch 1 taken 113110 times.
✓ Branch 2 taken 112247 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 462 times.
✓ Branch 5 taken 111785 times.
✓ Branch 6 taken 462 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 157 times.
✓ Branch 9 taken 305 times.
✓ Branch 10 taken 157 times.
✓ Branch 11 taken 225200 times.
225357 if (!st.getState() && needRefresh() && !IsChannelOutputThread()) {
717 157 int x = 0;
718
7/8
✓ Branch 0 taken 762 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 646 times.
✓ Branch 3 taken 116 times.
✓ Branch 4 taken 605 times.
✓ Branch 5 taken 41 times.
✓ Branch 6 taken 605 times.
✓ Branch 7 taken 157 times.
762 while (needRefresh() && (x++ < 10)) {
719
1/2
✓ Branch 0 taken 605 times.
✗ Branch 1 not taken.
605 usleep(10000);
720 }
721 }
722
723 225357 PixelOverlayState old = std::atomic_ref<PixelOverlayState>(state).exchange(st, std::memory_order_relaxed);
724
1/2
✓ Branch 0 taken 225357 times.
✗ Branch 1 not taken.
225357 PixelOverlayManager::INSTANCE.modelStateChanged(this, old, st);
725 }
726 // Copy-on-write publish of the child list. See the note on `children` in the
727 // header for why this must not take (or be called under) any overlay lock.
728 135117 void PixelOverlayModel::setChildState(const std::string& n, const PixelOverlayState& in, int ox, int oy, int w, int h) {
729 // flushChildren() indexes this model's channelMap with the child's
730 // rectangle on every frame, so the rectangle has to lie inside this model.
731 // A submodel saved against a parent whose geometry has since changed can
732 // point anywhere; refuse it here, where the list is written, the same way
733 // setData() refuses an out-of-bounds region.
734 135117 PixelOverlayState st = in;
735 // 64-bit sums: an XOffset near INT_MAX wrapped ox + w negative and passed.
736
8/12
✓ Branch 0 taken 78165 times.
✓ Branch 1 taken 56952 times.
✓ Branch 2 taken 78165 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 78165 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 78165 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 78165 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 135115 times.
213282 if (st.getState() && (ox < 0 || oy < 0 || w < 0 || h < 0 ||
737
3/4
✓ Branch 0 taken 78163 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 78163 times.
78165 (int64_t)ox + w > width || (int64_t)oy + h > height)) {
738
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogWarn(VB_CHANNELOUT, "Submodel '%s' (%dx%d @ %d,%d) lies outside its parent '%s' (%dx%d), ignoring it\n",
739 n.c_str(), w, h, ox, oy, name.c_str(), width, height);
740 2 st = PixelOverlayState(PixelOverlayState::Disabled);
741 }
742
743 135117 bool hadChildren = false;
744 135117 bool hasChildren = false;
745
746 {
747 // Leaf lock. Writers really are concurrent -- an HTTP/command thread
748 // holds modelsLock while an effect update thread holds only this
749 // model's effectLock -- so the read-copy-publish below has to be
750 // serialized or one update is lost. Nothing inside this scope calls
751 // out to code that takes another lock.
752
1/2
✓ Branch 0 taken 135117 times.
✗ Branch 1 not taken.
135117 std::lock_guard<std::mutex> lk(childrenWriteLock);
753
754
1/2
✓ Branch 0 taken 135117 times.
✗ Branch 1 not taken.
135117 auto cur = children.load();
755
4/4
✓ Branch 0 taken 135084 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 127741 times.
✓ Branch 3 taken 7343 times.
135117 hadChildren = cur && !cur->empty();
756
757
3/4
✓ Branch 0 taken 135084 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 135084 times.
✗ Branch 3 not taken.
135117 ChildModelStateList next = cur ? *cur : ChildModelStateList();
758 135117 bool changed = false;
759 135117 bool found = false;
760
761 135117 auto it = next.begin();
762
2/2
✓ Branch 0 taken 248322 times.
✓ Branch 1 taken 135117 times.
383439 while (it != next.end()) {
763
2/2
✓ Branch 0 taken 78143 times.
✓ Branch 1 taken 170179 times.
248322 if (it->name == n) {
764 78143 found = true;
765
2/2
✓ Branch 0 taken 56919 times.
✓ Branch 1 taken 21224 times.
78143 if (st.getState() == 0) {
766 56919 it = next.erase(it);
767 56919 changed = true;
768 } else {
769
2/4
✓ Branch 0 taken 21011 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21011 times.
✗ Branch 3 not taken.
42235 if (it->state != st || it->xoffset != ox || it->yoffset != oy ||
770
6/8
✓ Branch 0 taken 21011 times.
✓ Branch 1 taken 213 times.
✓ Branch 2 taken 21011 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 21011 times.
✓ Branch 6 taken 213 times.
✓ Branch 7 taken 21011 times.
42235 it->width != w || it->height != h) {
771 213 it->state = st;
772 213 it->xoffset = ox;
773 213 it->yoffset = oy;
774 213 it->width = w;
775 213 it->height = h;
776 213 changed = true;
777 }
778 21224 ++it;
779 }
780 } else {
781 170179 ++it;
782 }
783 }
784
785
6/6
✓ Branch 0 taken 56974 times.
✓ Branch 1 taken 78143 times.
✓ Branch 2 taken 56939 times.
✓ Branch 3 taken 35 times.
✓ Branch 4 taken 56939 times.
✓ Branch 5 taken 78178 times.
135117 if (!found && st.getState()) {
786 56939 ChildModelState cms;
787
1/2
✓ Branch 0 taken 56939 times.
✗ Branch 1 not taken.
56939 cms.name = n;
788 56939 cms.state = st;
789 56939 cms.xoffset = ox;
790 56939 cms.yoffset = oy;
791 56939 cms.width = w;
792 56939 cms.height = h;
793
1/2
✓ Branch 0 taken 56939 times.
✗ Branch 1 not taken.
56939 next.push_back(cms);
794 56939 changed = true;
795 56939 }
796
797 135117 hasChildren = !next.empty();
798
2/2
✓ Branch 0 taken 21046 times.
✓ Branch 1 taken 114071 times.
135117 if (!changed) {
799 // Nothing to publish -- and with the list unchanged, hadChildren
800 // and hasChildren agree, so neither transition below could fire.
801 21046 return;
802 }
803
2/4
✓ Branch 0 taken 114071 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 114071 times.
✗ Branch 3 not taken.
114071 children.store(std::make_shared<const ChildModelStateList>(std::move(next)));
804
6/6
✓ Branch 0 taken 114071 times.
✓ Branch 1 taken 21046 times.
✓ Branch 2 taken 114071 times.
✓ Branch 3 taken 21046 times.
✓ Branch 4 taken 114071 times.
✓ Branch 5 taken 21046 times.
177209 }
805
806 // Deliberately outside childrenWriteLock: modelStateChanged() takes
807 // activeModelsLock, which the channel output thread holds while it is
808 // inside the readers of `children`.
809
4/4
✓ Branch 0 taken 106729 times.
✓ Branch 1 taken 7342 times.
✓ Branch 2 taken 7322 times.
✓ Branch 3 taken 99407 times.
114071 if (hadChildren && !hasChildren) {
810
2/4
✓ Branch 0 taken 7322 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7322 times.
✗ Branch 3 not taken.
7322 PixelOverlayManager::INSTANCE.modelStateChanged(this, PixelOverlayState::Enabled, getState());
811
3/4
✓ Branch 0 taken 7342 times.
✓ Branch 1 taken 99407 times.
✓ Branch 2 taken 7342 times.
✗ Branch 3 not taken.
106749 } else if (!hadChildren && hasChildren) {
812
2/4
✓ Branch 0 taken 7342 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7342 times.
✗ Branch 3 not taken.
7342 PixelOverlayManager::INSTANCE.modelStateChanged(this, getState(), PixelOverlayState::Enabled);
813 }
814 }
815
816 1741 bool PixelOverlayModel::flushChildren(uint8_t* dst) {
817 // Snapshot: the list this iterates cannot be mutated or freed by a
818 // concurrent setChildState(), which publishes a whole new list instead.
819
1/2
✓ Branch 0 taken 1741 times.
✗ Branch 1 not taken.
1741 auto snapshot = children.load();
820
3/6
✓ Branch 0 taken 1741 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1741 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1741 times.
1741 if (!snapshot || snapshot->empty()) {
821 ✗ return false;
822 }
823
2/2
✓ Branch 0 taken 2087 times.
✓ Branch 1 taken 1741 times.
3828 for (auto& c : *snapshot) {
824 2087 int cst = c.state.getState();
825
2/2
✓ Branch 0 taken 40755 times.
✓ Branch 1 taken 2087 times.
42842 for (int y = 0; y < c.height; y++) {
826 40755 int yoff = (y + c.yoffset) * width * bytesPerPixel;
827
2/2
✓ Branch 0 taken 2287100 times.
✓ Branch 1 taken 40755 times.
2327855 for (int x = 0; x < c.width; x++) {
828 2287100 int off0 = (x + c.xoffset) * bytesPerPixel + yoff;
829
830
3/4
✓ Branch 0 taken 82324 times.
✓ Branch 1 taken 2203976 times.
✓ Branch 2 taken 800 times.
✗ Branch 3 not taken.
2287100 switch (cst) {
831 82324 case 1: // Opaque
832
2/2
✓ Branch 0 taken 247068 times.
✓ Branch 1 taken 82324 times.
329392 for (int ch = 0; ch < bytesPerPixel; ch++) {
833
1/2
✓ Branch 0 taken 247068 times.
✗ Branch 1 not taken.
247068 if (channelMap[off0 + ch] != FPPD_OFF_CHANNEL) {
834 247068 dst[channelMap[off0 + ch]] = channelData[channelMap[off0 + ch]];
835 }
836 }
837 82324 break;
838 2203976 case 2: // Transparent
839
2/2
✓ Branch 0 taken 6611928 times.
✓ Branch 1 taken 2203976 times.
8815904 for (int ch = 0; ch < bytesPerPixel; ch++) {
840
5/6
✓ Branch 0 taken 6611928 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2204752 times.
✓ Branch 3 taken 4407176 times.
✓ Branch 4 taken 2204752 times.
✓ Branch 5 taken 4407176 times.
6611928 if (channelMap[off0 + ch] != FPPD_OFF_CHANNEL && channelData[channelMap[off0 + ch]]) {
841 2204752 dst[channelMap[off0 + ch]] = channelData[channelMap[off0 + ch]];
842 }
843 }
844 2203976 break;
845 800 case 3: { // TransparentRGB
846 800 bool cp = false;
847
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 800 times.
3200 for (int ch = 0; ch < bytesPerPixel; ch++) {
848
3/4
✓ Branch 0 taken 2400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1576 times.
✓ Branch 3 taken 824 times.
2400 cp |= (channelMap[off0 + ch] != FPPD_OFF_CHANNEL && channelData[channelMap[off0 + ch]]);
849 }
850
1/2
✓ Branch 0 taken 800 times.
✗ Branch 1 not taken.
800 if (cp) {
851
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 800 times.
3200 for (int ch = 0; ch < bytesPerPixel; ch++) {
852
5/6
✓ Branch 0 taken 2400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1576 times.
✓ Branch 3 taken 824 times.
✓ Branch 4 taken 1576 times.
✓ Branch 5 taken 824 times.
2400 if (channelMap[off0 + ch] != FPPD_OFF_CHANNEL && channelData[channelMap[off0 + ch]]) {
853 1576 dst[channelMap[off0 + ch]] = channelData[channelMap[off0 + ch]];
854 }
855 }
856 }
857 800 break;
858 }
859 }
860 }
861 }
862 }
863 1741 return true;
864 1741 }
865
866 // A plain model owns one contiguous output range, so its channelData offset is
867 // simply relative to where that range starts (doOverlay() below memcpy's the
868 // whole thing to &channels[startChannel]).
869 432 uint32_t PixelOverlayModel::outputChannelForData(uint32_t dataOffset) const {
870 432 return static_cast<uint32_t>(startChannel) + dataOffset;
871 }
872 24 uint32_t PixelOverlayModel::outputChannelForCell(int x, int y) const {
873
4/8
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 24 times.
24 if (x < 0 || y < 0 || x >= width || y >= height) {
874 ✗ return FPPD_OFF_CHANNEL;
875 }
876 24 size_t off = ((size_t)y * width + x) * bytesPerPixel;
877
3/6
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
24 if (off >= channelMap.size() || channelMap[off] == FPPD_OFF_CHANNEL) {
878 ✗ return FPPD_OFF_CHANNEL;
879 }
880 24 return outputChannelForData(channelMap[off]);
881 }
882
883 /*
884 * Build (once) the polar view of this model's buffer.
885 *
886 * The join runs buffer cell -> channelData offset -> absolute output channel ->
887 * position, because the virtual display map is keyed by absolute channel and
888 * every model type can already say which channel one of its buffer cells ends
889 * up on (outputChannelForData, overridden by the scattering subclasses).
890 *
891 * Any failure returns nullptr and is remembered, so a model with no geometry
892 * costs one attempt rather than one per effect start. A submodel whose parent
893 * can't be resolved yet is not an attempt: it has no channels to join.
894 */
895 23 const PolarBufferMap* PixelOverlayModel::getPolarMap(PolarMode mode) {
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (mode == PolarMode::None) {
897 ✗ return nullptr;
898 }
899
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 PixelOverlayModelSub* sub = dynamic_cast<PixelOverlayModelSub*>(this);
900
5/8
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 23 times.
23 if (sub && !sub->resolveParent()) {
901 ✗ return nullptr;
902 }
903 // Rebuild when the layout has been re-uploaded since the map was built.
904 struct stat vst;
905 23 long long layoutVersion = 0;
906
5/8
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 21 times.
✓ Branch 7 taken 2 times.
46 if (stat(FPP_DIR_CONFIG("/virtualdisplaymap").c_str(), &vst) == 0) {
907 21 layoutVersion = (long long)vst.st_mtime;
908 }
909 23 auto key = std::make_pair(mode, layoutVersion);
910
911
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 auto cached = polarMaps.find(key);
912
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 22 times.
23 if (cached != polarMaps.end()) {
913
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 return cached->second.valid() ? &cached->second : nullptr;
914 }
915
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 PolarBufferMap& polarMap = polarMaps[key];
916
917 22 std::vector<std::pair<uint32_t, std::pair<float, float>>> vdm;
918
3/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 20 times.
22 if (!LoadVirtualDisplayPositions(vdm)) {
919
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogDebug(VB_CHANNELOUT,
920 "No config/virtualdisplaymap; polar buffer mapping unavailable for %s\n",
921 name.c_str());
922 2 return nullptr;
923 }
924
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 std::unordered_map<uint32_t, std::pair<float, float>> pos(vdm.begin(), vdm.end());
925
926 20 std::vector<PolarPixel> pixels;
927
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 pixels.reserve(static_cast<size_t>(width) * height);
928
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 20 times.
46 for (int y = 0; y < height; y++) {
929
2/2
✓ Branch 0 taken 488 times.
✓ Branch 1 taken 26 times.
514 for (int x = 0; x < width; x++) {
930 488 int off = (y * width + x) * bytesPerPixel;
931
3/6
✓ Branch 0 taken 488 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 488 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 488 times.
488 if (off < 0 || off >= (int)channelMap.size()) {
932 102 continue;
933 }
934 488 uint32_t cd = channelMap[off];
935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 488 times.
488 if (cd == FPPD_OFF_CHANNEL) {
936 ✗ continue; // a hole in the buffer
937 }
938
1/2
✓ Branch 0 taken 488 times.
✗ Branch 1 not taken.
488 uint32_t ch = outputChannelForData(cd);
939
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 480 times.
488 if (ch == FPPD_OFF_CHANNEL) {
940 8 continue;
941 }
942
1/2
✓ Branch 0 taken 480 times.
✗ Branch 1 not taken.
480 auto it = pos.find(ch);
943
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 386 times.
480 if (it == pos.end()) {
944 94 continue; // this node was never exported to the layout
945 }
946 772 pixels.push_back({ it->second.first, it->second.second,
947
1/2
✓ Branch 0 taken 386 times.
✗ Branch 1 not taken.
386 static_cast<uint32_t>(y * width + x) });
948 }
949 }
950
951
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 if (pixels.size() < 2) {
952
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 LogDebug(VB_CHANNELOUT,
953 "Model %s has %zu positioned pixels; polar buffer mapping unavailable\n",
954 name.c_str(), pixels.size());
955 4 return nullptr;
956 }
957
958
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 polarMap = BuildPolarBufferMap(pixels, mode);
959
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!polarMap.valid()) {
960 ✗ LogDebug(VB_CHANNELOUT, "Could not build a polar buffer for %s\n", name.c_str());
961 ✗ return nullptr;
962 }
963
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 LogDebug(VB_CHANNELOUT, "Polar buffer for %s: %d x %d from %zu positioned pixels\n",
964 name.c_str(), polarMap.width, polarMap.height, pixels.size());
965 16 return &polarMap;
966 22 }
967
968 24930 void PixelOverlayModel::doOverlay(uint8_t* channels) {
969
1/2
✓ Branch 0 taken 24930 times.
✗ Branch 1 not taken.
24930 int st = getState().getState();
970 24930 uint8_t* dst = &channels[startChannel];
971
6/6
✓ Branch 0 taken 1743 times.
✓ Branch 1 taken 23187 times.
✓ Branch 2 taken 1741 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1741 times.
✓ Branch 5 taken 23189 times.
24930 if (st == 0 && hasChildren()) {
972 // this model is disable, but we have children that are
973 // enabled. Thus, we need to apply their blending
974 // to the channel data.
975 1741 flushChildren(dst);
976 1741 dirtyBuffer = false;
977 1741 return;
978 }
979
2/2
✓ Branch 0 taken 369 times.
✓ Branch 1 taken 22385 times.
22754 if (((st == 2) || (st == 3)) &&
980
1/2
✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
804 (!IsEffectRunning()) &&
981
5/6
✓ Branch 0 taken 22754 times.
✓ Branch 1 taken 435 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 741 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 23189 times.
46441 (!sequence->IsSequenceRunning()) &&
982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 !PluginManager::INSTANCE.hasPlugins()) {
983 // there is nothing running that we would be overlaying so do a straight copy
984 ✗ st = 1;
985 }
986
987 23189 uint8_t* src = channelData;
988
4/4
✓ Branch 0 taken 22383 times.
✓ Branch 1 taken 435 times.
✓ Branch 2 taken 369 times.
✓ Branch 3 taken 2 times.
23189 switch (st) {
989 22383 case 1: // Active - Opaque
990 22383 memcpy(dst, src, channelCount);
991 22383 break;
992 435 case 2: // Active Transparent
993
2/2
✓ Branch 0 taken 65250 times.
✓ Branch 1 taken 435 times.
65685 for (int j = 0; j < channelCount; j++, src++, dst++) {
994
2/2
✓ Branch 0 taken 30208 times.
✓ Branch 1 taken 35042 times.
65250 if (*src) {
995 30208 *dst = *src;
996 }
997 }
998 435 break;
999 369 case 3: // Active Transparent RGB(W)
1000
2/2
✓ Branch 0 taken 18450 times.
✓ Branch 1 taken 369 times.
18819 for (int j = 0; j < channelCount; j += channelsPerNode, src += channelsPerNode, dst += channelsPerNode) {
1001 // A channel count that is not a whole number of nodes leaves a
1002 // short last node; reading and writing a full node there ran past
1003 // channelData and past the model's channels.
1004 18450 const int n = std::min(channelsPerNode, channelCount - j);
1005 18450 bool any = false;
1006
2/2
✓ Branch 0 taken 55350 times.
✓ Branch 1 taken 18450 times.
73800 for (int k = 0; k < n; k++) {
1007 55350 any |= src[k];
1008 }
1009
2/2
✓ Branch 0 taken 17292 times.
✓ Branch 1 taken 1158 times.
18450 if (any) {
1010
2/2
✓ Branch 0 taken 51876 times.
✓ Branch 1 taken 17292 times.
69168 for (int k = 0; k < n; k++) {
1011 51876 dst[k] = src[k];
1012 }
1013 }
1014 }
1015 369 break;
1016 }
1017
1018 23189 dirtyBuffer = false;
1019 }
1020
1021 110983 void PixelOverlayModel::setData(const uint8_t* data) {
1022
2/2
✓ Branch 0 taken 107968852 times.
✓ Branch 1 taken 110983 times.
108079835 for (int c = 0; c < (width * height * bytesPerPixel); c++) {
1023
2/2
✓ Branch 0 taken 107968831 times.
✓ Branch 1 taken 21 times.
107968852 if (channelMap[c] != FPPD_OFF_CHANNEL) {
1024 107968831 channelData[channelMap[c]] = data[c];
1025 }
1026 }
1027 110983 dirtyBuffer = true;
1028 110983 }
1029
1030 57382 void PixelOverlayModel::setData(const uint8_t* data, int xOffset, int yOffset, int w, int h, const PixelOverlayState& st) {
1031
1/2
✓ Branch 0 taken 57382 times.
✗ Branch 1 not taken.
57382 if (((int64_t)w + xOffset > width) ||
1032
2/4
✓ Branch 0 taken 57382 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57382 times.
✗ Branch 3 not taken.
57382 ((int64_t)h + yOffset > height) ||
1033
3/6
✓ Branch 0 taken 57382 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57382 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 57382 times.
57382 (xOffset < 0) || (yOffset < 0) || (w < 0) || (h < 0)) {
1034 ✗ LogErr(VB_CHANNELOUT, "Error: region is out of bounds, model is %dx%d, region is %dx%d @ %d,%d\n",
1035 width, height, w, h, xOffset, yOffset);
1036 ✗ return;
1037 }
1038
1039 170314 if ((w == width) &&
1040
2/4
✓ Branch 0 taken 55550 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55550 times.
✗ Branch 3 not taken.
55550 (h == height) &&
1041
1/2
✓ Branch 0 taken 55550 times.
✗ Branch 1 not taken.
55550 (xOffset == 0) &&
1042
5/6
✓ Branch 0 taken 55550 times.
✓ Branch 1 taken 1832 times.
✓ Branch 2 taken 55550 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55550 times.
✓ Branch 5 taken 1832 times.
112932 (yOffset == 0) &&
1043 55550 (st.getState() == 1)) {
1044 55550 setData(data);
1045 55550 return;
1046 }
1047
1048 1832 int cst = st.getState();
1049 1832 int rowWrap = (width - w) * bytesPerPixel;
1050 1832 int s = 0;
1051 1832 int c = (yOffset * width * bytesPerPixel) + (xOffset * bytesPerPixel);
1052
2/2
✓ Branch 0 taken 3965 times.
✓ Branch 1 taken 1832 times.
5797 for (int yPos = 0; yPos < h; yPos++) {
1053
2/2
✓ Branch 0 taken 31226 times.
✓ Branch 1 taken 3965 times.
35191 for (int xPos = 0; xPos < w; xPos++) {
1054
4/4
✓ Branch 0 taken 9656 times.
✓ Branch 1 taken 21570 times.
✓ Branch 2 taken 2048 times.
✓ Branch 3 taken 7608 times.
31226 if (cst == 1 || cst == 0) {
1055
2/2
✓ Branch 0 taken 70858 times.
✓ Branch 1 taken 23618 times.
94476 for (int i = 0; i < bytesPerPixel; i++, c++, s++) {
1056
1/2
✓ Branch 0 taken 70858 times.
✗ Branch 1 not taken.
70858 if (channelMap[c] != FPPD_OFF_CHANNEL) {
1057 70858 channelData[channelMap[c]] = data[s];
1058 }
1059 }
1060
2/2
✓ Branch 0 taken 4072 times.
✓ Branch 1 taken 3536 times.
31226 } else if (cst == 2) {
1061
2/2
✓ Branch 0 taken 12216 times.
✓ Branch 1 taken 4072 times.
16288 for (int i = 0; i < bytesPerPixel; i++, c++, s++) {
1062
5/6
✓ Branch 0 taken 8120 times.
✓ Branch 1 taken 4096 times.
✓ Branch 2 taken 8120 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8120 times.
✓ Branch 5 taken 4096 times.
12216 if (data[s] && channelMap[c] != FPPD_OFF_CHANNEL) {
1063 8120 channelData[channelMap[c]] = data[s];
1064 }
1065 }
1066
1/2
✓ Branch 0 taken 3536 times.
✗ Branch 1 not taken.
3536 } else if (cst == 3) {
1067 3536 bool b = false;
1068
2/2
✓ Branch 0 taken 10608 times.
✓ Branch 1 taken 3536 times.
14144 for (int i = 0; i < bytesPerPixel; i++) {
1069 10608 b |= data[s + i];
1070 }
1071
1/2
✓ Branch 0 taken 3536 times.
✗ Branch 1 not taken.
3536 if (b) {
1072
2/2
✓ Branch 0 taken 10608 times.
✓ Branch 1 taken 3536 times.
14144 for (int i = 0; i < bytesPerPixel; i++, c++, s++) {
1073
1/2
✓ Branch 0 taken 10608 times.
✗ Branch 1 not taken.
10608 if (channelMap[c] != FPPD_OFF_CHANNEL) {
1074 10608 channelData[channelMap[c]] = data[s];
1075 }
1076 }
1077 } else {
1078 ✗ s += bytesPerPixel;
1079 ✗ c += bytesPerPixel;
1080 }
1081 }
1082 }
1083 3965 c += rowWrap;
1084 }
1085 1832 dirtyBuffer = true;
1086 }
1087
1088 // Clip a srcW x srcH rect placed at (xOffset, yOffset) against a w x h model.
1089 // On success sx/sy are the first source pixel to read, dx/dy where it lands,
1090 // and cw/ch the visible size. Returns false when nothing is visible.
1091 55558 static bool clipBlitRect(int srcW, int srcH, int xOffset, int yOffset,
1092 int w, int h,
1093 int& sx, int& sy, int& dx, int& dy, int& cw, int& ch) {
1094 55558 sx = 0;
1095 55558 sy = 0;
1096 55558 dx = xOffset;
1097 55558 dy = yOffset;
1098 55558 cw = srcW;
1099 55558 ch = srcH;
1100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55558 times.
55558 if (dx < 0) {
1101 ✗ sx = -dx;
1102 ✗ cw += dx;
1103 ✗ dx = 0;
1104 }
1105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55558 times.
55558 if (dy < 0) {
1106 ✗ sy = -dy;
1107 ✗ ch += dy;
1108 ✗ dy = 0;
1109 }
1110
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 55557 times.
55558 if (cw > (w - dx)) {
1111 1 cw = w - dx;
1112 }
1113
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 55557 times.
55558 if (ch > (h - dy)) {
1114 1 ch = h - dy;
1115 }
1116
2/4
✓ Branch 0 taken 55558 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55558 times.
✗ Branch 3 not taken.
55558 return (cw > 0) && (ch > 0);
1117 }
1118
1119 // Expand one source pixel to the model's channel layout. srcBpp 1 is greyscale
1120 // (replicated to RGB), 3 is RGB, 4 is RGBW. When an RGBW source lands on an RGB
1121 // model the white is folded into RGB the same way getOverlayPixelValue() does,
1122 // so it dims rather than disappearing.
1123 30188722 static inline void convertPixel(const uint8_t* s, int srcBpp, uint8_t* d, int dstBpp) {
1124 30188722 uint8_t px[4] = { 0, 0, 0, 0 };
1125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30188722 times.
30188722 if (srcBpp == 1) {
1126 ✗ px[0] = px[1] = px[2] = s[0];
1127 } else {
1128 30188722 px[0] = s[0];
1129 30188722 px[1] = s[1];
1130 30188722 px[2] = s[2];
1131
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 30188706 times.
30188722 if (srcBpp > 3) {
1132 16 px[3] = s[3];
1133 }
1134 }
1135
3/4
✓ Branch 0 taken 30099568 times.
✓ Branch 1 taken 89154 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30099568 times.
30188722 if (dstBpp < 4 && px[3]) {
1136 ✗ px[0] = std::min(px[0] + px[3], 255);
1137 ✗ px[1] = std::min(px[1] + px[3], 255);
1138 ✗ px[2] = std::min(px[2] + px[3], 255);
1139 }
1140
3/4
✓ Branch 0 taken 90655320 times.
✓ Branch 1 taken 30188722 times.
✓ Branch 2 taken 90655320 times.
✗ Branch 3 not taken.
120844042 for (int i = 0; i < dstBpp && i < 4; i++) {
1141 90655320 d[i] = px[i];
1142 }
1143 30188722 }
1144
1145 55552 bool PixelOverlayModel::blitData(const uint8_t* src, int srcW, int srcH, int srcBpp,
1146 int xOffset, int yOffset, const PixelOverlayState& st) {
1147
5/10
✓ Branch 0 taken 55552 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55552 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55552 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 55552 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 55552 times.
55552 if (!src || srcW <= 0 || srcH <= 0 || srcBpp < 1 || srcBpp > 4) {
1148 ✗ return false;
1149 }
1150 int sx, sy, dx, dy, cw, ch;
1151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55552 times.
55552 if (!clipBlitRect(srcW, srcH, xOffset, yOffset, width, height, sx, sy, dx, dy, cw, ch)) {
1152 ✗ return false;
1153 }
1154
1155 // setData()'s rect form wants a tightly packed cw x ch buffer in the
1156 // model's own pixel format, so convert into a temporary rather than
1157 // duplicating its channelMap walk here.
1158 55552 const int bpp = bytesPerPixel;
1159
1/2
✓ Branch 0 taken 55552 times.
✗ Branch 1 not taken.
55552 std::vector<uint8_t> rect((size_t)cw * ch * bpp, 0);
1160
2/2
✓ Branch 0 taken 923317 times.
✓ Branch 1 taken 55552 times.
978869 for (int y = 0; y < ch; y++) {
1161 923317 const uint8_t* s = src + (((size_t)(sy + y) * srcW) + sx) * srcBpp;
1162 923317 uint8_t* d = &rect[(size_t)y * cw * bpp];
1163
2/2
✓ Branch 0 taken 30188464 times.
✓ Branch 1 taken 923317 times.
31111781 for (int x = 0; x < cw; x++, s += srcBpp, d += bpp) {
1164
1/2
✓ Branch 0 taken 30188464 times.
✗ Branch 1 not taken.
30188464 convertPixel(s, srcBpp, d, bpp);
1165 }
1166 }
1167
1/2
✓ Branch 0 taken 55552 times.
✗ Branch 1 not taken.
55552 setData(rect.data(), dx, dy, cw, ch, st);
1168 55552 return true;
1169 55552 }
1170
1171 6 bool PixelOverlayModel::blitOverlayBuffer(const uint8_t* src, int srcW, int srcH, int srcBpp,
1172 int xOffset, int yOffset) {
1173
5/10
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 6 times.
6 if (!src || srcW <= 0 || srcH <= 0 || srcBpp < 1 || srcBpp > 4) {
1174 ✗ return false;
1175 }
1176 int sx, sy, dx, dy, cw, ch;
1177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!clipBlitRect(srcW, srcH, xOffset, yOffset, width, height, sx, sy, dx, dy, cw, ch)) {
1178 ✗ return false;
1179 }
1180
1181 6 const int bpp = bytesPerPixel;
1182
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 uint8_t* buf = getOverlayBuffer();
1183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!buf) {
1184 ✗ return false;
1185 }
1186
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 6 times.
33 for (int y = 0; y < ch; y++) {
1187 27 const uint8_t* s = src + (((size_t)(sy + y) * srcW) + sx) * srcBpp;
1188 27 uint8_t* d = buf + ((size_t)(dy + y) * width + dx) * bpp;
1189
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 27 times.
285 for (int x = 0; x < cw; x++, s += srcBpp, d += bpp) {
1190
1/2
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
258 convertPixel(s, srcBpp, d, bpp);
1191 }
1192 }
1193
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 setOverlayBufferDirty(true);
1194 6 return true;
1195 }
1196
1197 // Sets absolute (0-based) channels startChannel..endChannel of this model to
1198 // value; -1 for either end means the model's own first or last channel. No
1199 // caller in FPP; protected, so kept for plugin subclasses. It used to index channelMap - laid out by
1200 // buffer cell, not by channel - with channel numbers, compute the end from
1201 // the shadowed startChannel parameter, and run an omitted end to -1.
1202 3 void PixelOverlayModel::setValue(uint8_t value, int startChannel, int endChannel) {
1203
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (!channelData || channelCount <= 0) {
1204 ✗ return;
1205 }
1206
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 int64_t start = (startChannel == -1) ? 0 : (int64_t)startChannel - this->startChannel;
1207
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 int64_t end = (endChannel == -1) ? channelCount - 1 : (int64_t)endChannel - this->startChannel;
1208 3 start = std::max<int64_t>(start, 0);
1209 3 end = std::min<int64_t>(end, channelCount - 1);
1210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (start > end) {
1211 ✗ return;
1212 }
1213 3 memset(channelData + start, value, (size_t)(end - start + 1));
1214 3 dirtyBuffer = true;
1215 }
1216 503817 void PixelOverlayModel::setPixelValue(int x, int y, int r, int g, int b) {
1217 503817 setPixelValue(x, y, r, g, b, 0);
1218 503817 }
1219 546565 void PixelOverlayModel::setPixelValue(int x, int y, int r, int g, int b, int w) {
1220
8/8
✓ Branch 0 taken 546563 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 546562 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 546561 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 546560 times.
546565 if (y >= height || x >= width || x < 0 || y < 0) {
1221 5 return;
1222 }
1223 546560 int c = (y * width * bytesPerPixel) + x * bytesPerPixel;
1224 546560 int mapSize = width * height * bytesPerPixel;
1225
2/4
✓ Branch 0 taken 546560 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 546560 times.
546560 if (c >= mapSize || c < 0) {
1226 ✗ return;
1227 }
1228
1/2
✓ Branch 0 taken 546560 times.
✗ Branch 1 not taken.
546560 if (channelMap[c] != FPPD_OFF_CHANNEL) {
1229 546560 channelData[channelMap[c]] = r;
1230 }
1231 546560 c++;
1232
4/8
✓ Branch 0 taken 546560 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 546560 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 546560 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 546560 times.
✗ Branch 7 not taken.
546560 if (channelsPerNode > 1 && c < mapSize && channelMap[c] != FPPD_OFF_CHANNEL) {
1233 546560 channelData[channelMap[c]] = g;
1234 }
1235 546560 c++;
1236
4/8
✓ Branch 0 taken 546560 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 546560 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 546560 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 546560 times.
✗ Branch 7 not taken.
546560 if (channelsPerNode > 2 && c < mapSize && channelMap[c] != FPPD_OFF_CHANNEL) {
1237 546560 channelData[channelMap[c]] = b;
1238 }
1239 546560 c++;
1240
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 546560 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 546560 times.
546560 if (channelsPerNode > 3 && c < mapSize && channelMap[c] != FPPD_OFF_CHANNEL) {
1241 ✗ channelData[channelMap[c]] = w;
1242 }
1243 546560 dirtyBuffer = true;
1244 }
1245 ✗ void PixelOverlayModel::getPixelValue(int x, int y, int& r, int& g, int& b) {
1246 ✗ int w = 0;
1247 ✗ getPixelValue(x, y, r, g, b, w);
1248 ✗ if (w) {
1249 ✗ r = std::min(r + w, 255);
1250 ✗ g = std::min(g + w, 255);
1251 ✗ b = std::min(b + w, 255);
1252 }
1253 ✗ }
1254 ✗ void PixelOverlayModel::getPixelValue(int x, int y, int& r, int& g, int& b, int& w) {
1255 ✗ if (y >= height || x >= width || x < 0 || y < 0) {
1256 ✗ return;
1257 }
1258 ✗ int c = (y * width * bytesPerPixel) + x * bytesPerPixel;
1259 ✗ int mapSize = width * height * bytesPerPixel;
1260 ✗ if (c >= mapSize || c < 0) {
1261 ✗ return;
1262 }
1263 ✗ if (channelMap[c] != FPPD_OFF_CHANNEL) {
1264 ✗ r = channelData[channelMap[c]];
1265 }
1266 ✗ c++;
1267 ✗ if (channelsPerNode > 1 && c < mapSize && channelMap[c] != FPPD_OFF_CHANNEL) {
1268 ✗ g = channelData[channelMap[c]];
1269 }
1270 ✗ c++;
1271 ✗ if (channelsPerNode > 2 && c < mapSize && channelMap[c] != FPPD_OFF_CHANNEL) {
1272 ✗ b = channelData[channelMap[c]];
1273 }
1274 ✗ c++;
1275 ✗ if (channelsPerNode > 3 && c < mapSize && channelMap[c] != FPPD_OFF_CHANNEL) {
1276 ✗ w = channelData[channelMap[c]];
1277 }
1278 }
1279
1280 ✗ void PixelOverlayModel::setScaledData(uint8_t* data, int w, int h) {
1281 ✗ float ydiff = (float)h / (float)height;
1282 ✗ float xdiff = (float)w / (float)width;
1283
1284 ✗ float newy = 0.0f;
1285 ✗ float newx = 0.0f;
1286 int x, y;
1287
1288 ✗ for (y = 0, newy = 0.0f; y < height; y++, newy += ydiff) {
1289 ✗ int srcY = newy;
1290 ✗ for (x = 0, newx = 0.0f; x < width; x++, newx += xdiff) {
1291 ✗ int srcX = newx;
1292
1293 ✗ int idx = y * width * bytesPerPixel + x * bytesPerPixel;
1294 ✗ int srcidx = srcY * w * bytesPerPixel + srcX * bytesPerPixel;
1295
1296 ✗ for (int ch = 0; ch < bytesPerPixel; ch++) {
1297 ✗ if (ch < channelsPerNode && channelMap[idx] != FPPD_OFF_CHANNEL) {
1298 ✗ channelData[channelMap[idx]] = data[srcidx];
1299 }
1300 ✗ idx++;
1301 ✗ srcidx++;
1302 }
1303 }
1304 }
1305 ✗ dirtyBuffer = true;
1306 ✗ }
1307 64 void PixelOverlayModel::clearData() {
1308 64 memset(channelData, 0, channelCount);
1309 64 dirtyBuffer = true;
1310 64 }
1311 ✗ void PixelOverlayModel::fillData(int r, int g, int b) {
1312 ✗ fillData(r, g, b, 0);
1313 ✗ }
1314 1203 void PixelOverlayModel::fillData(int r, int g, int b, int w) {
1315
2/2
✓ Branch 0 taken 3080 times.
✓ Branch 1 taken 1203 times.
4283 for (int y = 0; y < height; y++) {
1316
2/2
✓ Branch 0 taken 42748 times.
✓ Branch 1 taken 3080 times.
45828 for (int x = 0; x < width; x++) {
1317 42748 setPixelValue(x, y, r, g, b, w);
1318 }
1319 }
1320 1203 }
1321
1322 ✗ void PixelOverlayModel::getDataJson(Json::Value& v, bool rle) {
1323 ✗ int bpp = bytesPerPixel;
1324 ✗ if (rle) {
1325 ✗ std::vector<unsigned char> cur(bpp, 0);
1326 ✗ std::vector<unsigned char> last(bpp, 0);
1327 ✗ int count = 0;
1328 ✗ for (int c = 0; c < height * width * bpp; c += bpp) {
1329 ✗ for (int ch = 0; ch < bpp; ch++) {
1330 ✗ cur[ch] = (channelMap[c + ch] != FPPD_OFF_CHANNEL) ? channelData[channelMap[c + ch]] : 0;
1331 }
1332
1333 ✗ if (cur == last) {
1334 ✗ count++;
1335 } else {
1336 ✗ if (count) {
1337 ✗ v.append(count);
1338 ✗ for (int ch = 0; ch < bpp; ch++) {
1339 ✗ v.append(last[ch]);
1340 }
1341 }
1342 ✗ count = 1;
1343 ✗ last = cur;
1344 }
1345 }
1346
1347 ✗ if (count) {
1348 ✗ v.append(count);
1349 ✗ for (int ch = 0; ch < bpp; ch++) {
1350 ✗ v.append(cur[ch]);
1351 }
1352 }
1353 ✗ } else {
1354 ✗ for (int c = 0; c < height * width * bpp; c++) {
1355 ✗ unsigned char i = 0;
1356 ✗ if (channelMap[c] != FPPD_OFF_CHANNEL) {
1357 ✗ i = channelData[channelMap[c]];
1358 }
1359 ✗ v.append(i);
1360 }
1361 }
1362 ✗ }
1363
1364 501 void PixelOverlayModel::getData(std::vector<uint8_t>& out) {
1365 501 const int bpp = bytesPerPixel;
1366 501 const size_t sz = (size_t)width * height * bpp;
1367 501 out.resize(sz);
1368
2/2
✓ Branch 0 taken 1550134 times.
✓ Branch 1 taken 501 times.
1550635 for (size_t c = 0; c < sz; c++) {
1369
2/2
✓ Branch 0 taken 1548043 times.
✓ Branch 1 taken 2091 times.
1550134 out[c] = (channelMap[c] != FPPD_OFF_CHANNEL) ? channelData[channelMap[c]] : 0;
1370 }
1371 501 }
1372
1373 116697 bool PixelOverlayModel::needRefresh() {
1374
3/4
✓ Branch 0 taken 114336 times.
✓ Branch 1 taken 2361 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 114336 times.
116697 return (dirtyBuffer || overlayBufferIsDirty());
1375 }
1376
1377 517 void PixelOverlayModel::setBufferIsDirty(bool dirty) {
1378 517 dirtyBuffer = dirty;
1379 517 }
1380
1381 // Created on first use, which can be an HTTP thread and an effect thread at
1382 // once: both saw null, both created it, and one mapping was lost - along with
1383 // whatever that thread had already drawn into it. Serialized here (allocation
1384 // happens once per model, so one lock for all of them is fine; a member would
1385 // change the plugin-visible layout), and published with release so the
1386 // channel output thread, which reads the pointer unlocked, never sees it
1387 // before its header is filled in.
1388 3374895 uint8_t* PixelOverlayModel::getOverlayBuffer() {
1389 3374895 OverlayBufferData* ob = __atomic_load_n(&overlayBufferData, __ATOMIC_ACQUIRE);
1390
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 3374756 times.
3374895 if (!ob) {
1391
1/2
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
139 std::lock_guard<std::mutex> l(s_overlayBufferAllocLock);
1392 139 ob = overlayBufferData;
1393
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 3 times.
139 if (!ob) {
1394 136 uint32_t size = width * height * bytesPerPixel + sizeof(OverlayBufferData);
1395 // A retired model's name may be its replacement's by now.
1396
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 std::string shmName = s_namesReleased.count(this) ? std::string()
1397
8/24
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 136 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 136 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 136 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 136 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 136 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 136 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
544 : modelMemoryName("/FPP-Model-Overlay-Buffer-", "/FPPMB-", name);
1398
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 ob = (OverlayBufferData*)createModelMemory(shmName, size, overlayBufferMapped);
1399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (!ob) {
1400 // Nothing left to hand back. Callers check, because the alternative
1401 // was dereferencing this null a few instructions from here.
1402 ✗ WarningHolder::AddWarning(WARNING_ID_MODEL_MEMORY, MODEL_MEMORY_WARNING);
1403 ✗ return nullptr;
1404 }
1405 136 ob->width = width;
1406 136 ob->height = height;
1407 136 ob->flags = (bytesPerPixel << 8); // store bytesPerPixel in bits 8-15
1408 136 __atomic_store_n(&overlayBufferData, ob, __ATOMIC_RELEASE);
1409
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 }
1410
1/2
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
139 }
1411 3374895 return ob->data;
1412 }
1413
1414 4769 void PixelOverlayModel::clearOverlayBuffer() {
1415
1/2
✓ Branch 0 taken 4769 times.
✗ Branch 1 not taken.
4769 if (uint8_t* buf = getOverlayBuffer()) {
1416 4769 memset(buf, 0, width * height * bytesPerPixel);
1417 }
1418 4769 }
1419 38459 void PixelOverlayModel::fillOverlayBuffer(int r, int g, int b) {
1420 38459 fillOverlayBuffer(r, g, b, 0);
1421 38459 }
1422 41467 void PixelOverlayModel::fillOverlayBuffer(int r, int g, int b, int w) {
1423 41467 uint8_t* data = getOverlayBuffer();
1424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41467 times.
41467 if (!data) {
1425 ✗ return;
1426 }
1427
2/2
✓ Branch 0 taken 2436780 times.
✓ Branch 1 taken 41467 times.
2478247 for (int p = 0; p < (width * height); p++) {
1428 2436780 data[0] = r;
1429 2436780 data[1] = g;
1430 2436780 data[2] = b;
1431
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 2436730 times.
2436780 if (bytesPerPixel > 3) {
1432 50 data[3] = w;
1433 }
1434 2436780 data += bytesPerPixel;
1435 }
1436 }
1437
1438 ✗ void PixelOverlayModel::setOverlayPixelValue(int x, int y, int r, int g, int b) {
1439 ✗ setOverlayPixelValue(x, y, r, g, b, 0);
1440 ✗ }
1441 2548400 void PixelOverlayModel::setOverlayPixelValue(int x, int y, int r, int g, int b, int w) {
1442
4/8
✓ Branch 0 taken 2548400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2548400 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2548400 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2548400 times.
2548400 if (y >= height || x >= width || x < 0 || y < 0) {
1443 ✗ return;
1444 }
1445 2548400 int idx = y * width * bytesPerPixel + x * bytesPerPixel;
1446 2548400 uint8_t* buf = getOverlayBuffer();
1447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2548400 times.
2548400 if (!buf) {
1448 ✗ return;
1449 }
1450 2548400 buf[idx++] = r;
1451 2548400 buf[idx++] = g;
1452 2548400 buf[idx++] = b;
1453
2/2
✓ Branch 0 taken 1260 times.
✓ Branch 1 taken 2547140 times.
2548400 if (bytesPerPixel > 3) {
1454 1260 buf[idx] = w;
1455 }
1456 }
1457 ✗ void PixelOverlayModel::getOverlayPixelValue(int x, int y, int& r, int& g, int& b) {
1458 ✗ int w = 0;
1459 ✗ getOverlayPixelValue(x, y, r, g, b, w);
1460 ✗ if (w) {
1461 ✗ r = std::min(r + w, 255);
1462 ✗ g = std::min(g + w, 255);
1463 ✗ b = std::min(b + w, 255);
1464 }
1465 ✗ }
1466 775299 void PixelOverlayModel::getOverlayPixelValue(int x, int y, int& r, int& g, int& b, int& w) {
1467
4/8
✓ Branch 0 taken 775299 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 775299 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 775299 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 775299 times.
775299 if (y >= height || x >= width || x < 0 || y < 0) {
1468 ✗ r = g = b = w = 0;
1469 ✗ return;
1470 }
1471 775299 int idx = y * width * bytesPerPixel + x * bytesPerPixel;
1472 775299 uint8_t* buf = getOverlayBuffer();
1473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 775299 times.
775299 if (!buf) {
1474 ✗ r = g = b = w = 0;
1475 ✗ return;
1476 }
1477 775299 r = buf[idx++];
1478 775299 g = buf[idx++];
1479 775299 b = buf[idx++];
1480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 775299 times.
775299 w = (bytesPerPixel > 3) ? buf[idx] : 0;
1481 }
1482
1483 // Neither of these allocates the overlay buffer. One that nobody has written
1484 // to is all zeros, and copying it out blanked whatever the model's channels
1485 // held: PUT .../fill with RGB filled the channels directly (the model had no
1486 // overlay buffer yet), then flushed, and the model went black.
1487 56207 void PixelOverlayModel::flushOverlayBuffer() {
1488 56207 OverlayBufferData* ob = __atomic_load_n(&overlayBufferData, __ATOMIC_ACQUIRE);
1489
2/2
✓ Branch 0 taken 662 times.
✓ Branch 1 taken 55545 times.
56207 if (!ob) {
1490 662 return;
1491 }
1492 55545 setData(ob->data);
1493 55545 ob->flags &= ~0x1;
1494 }
1495
1496 148849 bool PixelOverlayModel::overlayBufferIsDirty() {
1497 // Allocated on first use from any thread (getOverlayBuffer()), so read the
1498 // pointer the way it is published.
1499 148849 OverlayBufferData* ob = __atomic_load_n(&overlayBufferData, __ATOMIC_ACQUIRE);
1500
4/4
✓ Branch 0 taken 18603 times.
✓ Branch 1 taken 130246 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 18601 times.
148849 return ob && (ob->flags & 0x1);
1501 }
1502
1503 6 void PixelOverlayModel::setOverlayBufferDirty(bool dirty) {
1504 6 OverlayBufferData* ob = __atomic_load_n(&overlayBufferData, __ATOMIC_ACQUIRE);
1505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!ob) {
1506 ✗ return;
1507 }
1508
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (dirty)
1509 6 ob->flags |= 0x1;
1510 else
1511 ✗ ob->flags &= ~0x1;
1512 }
1513
1514 ✗ void PixelOverlayModel::setOverlayBufferScaledData(uint8_t* data, int w, int h) {
1515 ✗ float ydiff = (float)h / (float)height;
1516 ✗ float xdiff = (float)w / (float)width;
1517 ✗ uint8_t* buf = getOverlayBuffer();
1518 ✗ if (!buf) {
1519 ✗ return;
1520 }
1521
1522 ✗ float newy = 0.0f;
1523 ✗ float newx = 0.0f;
1524 int x, y;
1525
1526 ✗ for (y = 0, newy = 0.0f; y < height; y++, newy += ydiff) {
1527 ✗ int srcY = newy;
1528 ✗ for (x = 0, newx = 0.0f; x < width; x++, newx += xdiff) {
1529 ✗ int srcX = newx;
1530
1531 ✗ int idx = y * width * bytesPerPixel + x * bytesPerPixel;
1532 ✗ int srcidx = srcY * w * bytesPerPixel + srcX * bytesPerPixel;
1533
1534 ✗ for (int ch = 0; ch < bytesPerPixel; ch++) {
1535 ✗ buf[idx + ch] = data[srcidx + ch];
1536 }
1537 }
1538 }
1539 }
1540
1541 // A file name the image directory can hold: no directory part, nothing
1542 // hidden or relative ("..", ".foo"), no control characters. fppd runs as
1543 // root, so "../../boot/firmware/config.txt" overwrote that file with PNG data.
1544 3 bool PixelOverlayModel::isPlainImageFileName(const std::string& f) {
1545
5/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2 times.
3 if (f.empty() || f[0] == '.') {
1546 1 return false;
1547 }
1548
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
22 for (unsigned char c : f) {
1549
4/8
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 20 times.
20 if (c == '/' || c == '\\' || c < 0x20 || c == 0x7F) {
1550 ✗ return false;
1551 }
1552 }
1553 2 return true;
1554 }
1555
1556 1 void PixelOverlayModel::saveOverlayAsImage(std::string filename) {
1557
5/10
✓ 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.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 if (filename != "" && !isPlainImageFileName(filename)) {
1558 ✗ LogWarn(VB_CHANNELOUT, "Not saving model \"%s\" as \"%s\": the name must be a plain file name\n",
1559 name.c_str(), filename.c_str());
1560 ✗ return;
1561 }
1562
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (width <= 0 || height <= 0) {
1563 ✗ return;
1564 }
1565
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 Magick::Blob blob;
1566
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 Magick::Image image(Magick::Geometry(width, height), Magick::Color("black"));
1567
1568
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (filename == "") {
1569 struct timeval tv;
1570 struct tm tm;
1571 char timeStr[32];
1572
1573 ✗ gettimeofday(&tv, nullptr);
1574 ✗ localtime_r(&tv.tv_sec, &tm);
1575
1576 ✗ snprintf(timeStr, sizeof(timeStr),
1577 "%4d%02d%02d_%02d%02d%02d",
1578 ✗ 1900 + tm.tm_year, tm.tm_mon + 1,
1579 tm.tm_mday, tm.tm_hour, tm.tm_min,
1580 tm.tm_sec);
1581
1582 ✗ filename = name + "-" + timeStr + ".png";
1583 }
1584
1585
4/8
✓ 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.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 filename = FPP_DIR_IMAGE("/") + filename;
1586
1587
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_CHANNELOUT, "Saving %dx%d PixelOverlayModel image to: %s\n",
1588 width, height, filename.c_str());
1589
1590 // Always save as RGB (3 bytes per pixel) for image compatibility
1591 // For RGBW models, blend the W channel into RGB so white pixels render correctly
1592
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::vector<uint8_t> data(width * height * 3, 0);
1593
1594
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for (int y = 0; y < height; y++) {
1595
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 5 times.
55 for (int x = 0; x < width; x++) {
1596 50 int mapIdx = (y * width + x) * bytesPerPixel;
1597 50 int imgIdx = (y * width + x) * 3;
1598
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 50 times.
200 for (int ch = 0; ch < 3; ch++) {
1599
3/6
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 150 times.
✗ Branch 5 not taken.
150 if (ch < bytesPerPixel && channelMap[mapIdx + ch] != FPPD_OFF_CHANNEL) {
1600 150 data[imgIdx + ch] = channelData[channelMap[mapIdx + ch]];
1601 }
1602 }
1603
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 50 times.
50 if (bytesPerPixel > 3 && channelMap[mapIdx + 3] != FPPD_OFF_CHANNEL) {
1604 ✗ uint8_t w = channelData[channelMap[mapIdx + 3]];
1605 ✗ if (w) {
1606 ✗ for (int ch = 0; ch < 3; ch++) {
1607 ✗ int v = data[imgIdx + ch] + w;
1608 ✗ data[imgIdx + ch] = (v > 255) ? 255 : v;
1609 }
1610 }
1611 }
1612 }
1613 }
1614
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 blob.update(&data[0], width * height * 3);
1615
1616 try {
1617
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 image.magick("RGB");
1618
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 image.depth(8);
1619
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 image.quiet(true);
1620
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 image.read(blob, Magick::Geometry(width, height));
1621
1622
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 image.write(filename.c_str());
1623 ✗ } catch (const std::exception& e) {
1624 ✗ LogErr(VB_CHANNELOUT, "Could not save model \"%s\" to %s: %s\n", name.c_str(), filename.c_str(), e.what());
1625 ✗ return;
1626 ✗ }
1627
1628
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 SetFilePerms(filename);
1629
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 }
1630
1631 64 void PixelOverlayModel::clear() {
1632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (__atomic_load_n(&overlayBufferData, __ATOMIC_ACQUIRE)) {
1633 ✗ clearOverlayBuffer();
1634 ✗ flushOverlayBuffer();
1635 } else {
1636 64 clearData();
1637 }
1638 64 }
1639 4210 void PixelOverlayModel::fill(int r, int g, int b) {
1640 4210 fill(r, g, b, 0);
1641 4210 }
1642 4210 void PixelOverlayModel::fill(int r, int g, int b, int w) {
1643
2/2
✓ Branch 0 taken 3007 times.
✓ Branch 1 taken 1203 times.
4210 if (__atomic_load_n(&overlayBufferData, __ATOMIC_ACQUIRE)) {
1644 3007 fillOverlayBuffer(r, g, b, w);
1645 3007 flushOverlayBuffer();
1646 } else {
1647 1203 fillData(r, g, b, w);
1648 }
1649 4210 }
1650
1651 9 int PixelOverlayModel::getStartChannel() const {
1652 9 return startChannel;
1653 }
1654 5 int PixelOverlayModel::getChannelCount() const {
1655 5 return channelCount;
1656 }
1657 426645 void PixelOverlayModel::toJson(Json::Value& result) {
1658 426645 result = config;
1659 426645 }
1660
1661 16960 int32_t PixelOverlayModel::updateRunningEffects() {
1662
1/2
✓ Branch 0 taken 16960 times.
✗ Branch 1 not taken.
16960 std::unique_lock<std::recursive_mutex> l(effectLock);
1663
2/2
✓ Branch 0 taken 16828 times.
✓ Branch 1 taken 132 times.
16960 if (runningEffect) {
1664
1/2
✓ Branch 0 taken 16828 times.
✗ Branch 1 not taken.
16828 int32_t v = runningEffect->update();
1665
2/2
✓ Branch 0 taken 329 times.
✓ Branch 1 taken 16499 times.
16828 if (v == 0) {
1666
1/2
✓ Branch 0 taken 329 times.
✗ Branch 1 not taken.
329 delete runningEffect;
1667 329 runningEffect = nullptr;
1668 }
1669 16828 return v;
1670 }
1671 132 return 0;
1672 16960 }
1673
1674 79244 void PixelOverlayModel::setRunningEffect(RunningEffect* ef, int32_t firstUpdateMS) {
1675
1/2
✓ Branch 0 taken 79244 times.
✗ Branch 1 not taken.
79244 std::unique_lock<std::recursive_mutex> l(effectLock);
1676
2/2
✓ Branch 0 taken 40855 times.
✓ Branch 1 taken 38389 times.
79244 if (runningEffect) {
1677
2/2
✓ Branch 0 taken 40736 times.
✓ Branch 1 taken 119 times.
40855 if (runningEffect != ef) {
1678
1/2
✓ Branch 0 taken 40736 times.
✗ Branch 1 not taken.
40736 delete runningEffect;
1679 }
1680
1/2
✓ Branch 0 taken 40855 times.
✗ Branch 1 not taken.
40855 PixelOverlayManager::INSTANCE.removePeriodicUpdate(this);
1681 }
1682 79244 runningEffect = ef;
1683
1/2
✓ Branch 0 taken 79244 times.
✗ Branch 1 not taken.
79244 PixelOverlayManager::INSTANCE.addPeriodicUpdate(firstUpdateMS, this);
1684 79244 }
1685
1686 137 void PixelOverlayModel::clearRunningEffect() {
1687
1/2
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
137 std::unique_lock<std::recursive_mutex> l(effectLock);
1688
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 122 times.
137 if (runningEffect) {
1689
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 delete runningEffect;
1690 15 runningEffect = nullptr;
1691
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 PixelOverlayManager::INSTANCE.removePeriodicUpdate(this);
1692 }
1693 137 }
1694
1695 41449 bool PixelOverlayModel::applyEffect(const std::string& autoState, const std::string& effect, const std::vector<std::string>& args) {
1696 41449 PixelOverlayEffect* pe = PixelOverlayEffect::GetPixelOverlayEffect(effect);
1697
2/2
✓ Branch 0 taken 41447 times.
✓ Branch 1 taken 2 times.
41449 if (pe) {
1698 // Every effect indexes args[] positionally with no bounds check of its
1699 // own, so a caller that supplies fewer than the effect declares walks
1700 // off the end -- "Overlay Model Effect,<model>,Enabled,Text" with no
1701 // further arguments is a one-line HTTP request that segfaults fppd in
1702 // TextEffect::apply's args[0]. Validate once here, at the single point
1703 // that dispatches to any effect, rather than bounds-checking every
1704 // effect body. The effect's own declared arg list is the authority,
1705 // and trailing args marked optional may legitimately be omitted.
1706 41447 size_t required = 0;
1707 41447 size_t idx = 0;
1708
2/2
✓ Branch 0 taken 17905 times.
✓ Branch 1 taken 41447 times.
59352 for (const auto& a : pe->args) {
1709 17905 ++idx;
1710
2/2
✓ Branch 0 taken 16505 times.
✓ Branch 1 taken 1400 times.
17905 if (!a.optional) {
1711 16505 required = idx;
1712 }
1713 }
1714
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 41443 times.
41447 if (args.size() < required) {
1715 4 LogErr(VB_CHANNELOUT,
1716 "Pixel Overlay effect '%s' needs at least %zu argument(s), %zu given -- ignoring\n",
1717 effect.c_str(), required, args.size());
1718 4 return false;
1719 }
1720 41443 return pe->apply(this, autoState, args);
1721 }
1722 2 LogWarn(VB_CHANNELOUT, "Pixel Overlay effect '%s' does not exist -- ignoring\n", effect.c_str());
1723 2 return false;
1724 }
1725