| 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 "fpp-json.h" | ||
| 16 | |||
| 17 | #include <algorithm> | ||
| 18 | #include <cstring> | ||
| 19 | |||
| 20 | #include "../Sequence.h" // FPPD_OFF_CHANNEL | ||
| 21 | #include "../log.h" | ||
| 22 | #include "../common_mini.h" | ||
| 23 | |||
| 24 | #include "PixelOverlay.h" | ||
| 25 | #include "PixelOverlayModelSub.h" | ||
| 26 | |||
| 27 | 30 | PixelOverlayModelSub::PixelOverlayModelSub(const Json::Value& c) : | |
| 28 | 30 | PixelOverlayModel(c) { | |
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (!isValid()) { |
| 30 | // The base class rejected the definition; the manager drops it. | ||
| 31 | ✗ | return; | |
| 32 | } | ||
| 33 | // Same type tolerance as the base class: these come from the same | ||
| 34 | // unchecked files, and asInt()/asString() throw. | ||
| 35 | 30 | std::string parentName, subType, grid; | |
| 36 | 30 | int parentStart = 0; | |
| 37 |
3/6✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
|
60 | if (!configInt(config, "XOffset", 0, xOffset) || !configInt(config, "YOffset", 0, yOffset) || |
| 38 |
2/4✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
|
30 | !configInt(config, "ParentStartChannel", 0, parentStart) || |
| 39 |
6/12✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 30 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 30 times.
|
90 | !configString(config, "Parent", parentName) || !configString(config, "SubType", subType) || |
| 40 |
2/4✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
|
30 | !configString(config, "Grid", grid)) { |
| 41 | ✗ | markInvalid("XOffset, YOffset, ParentStartChannel, Parent, SubType or Grid has the wrong type"); | |
| 42 | ✗ | return; | |
| 43 | } | ||
| 44 | |||
| 45 |
3/4✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 25 times.
|
30 | if (config.isMember("SubType")) { |
| 46 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | std::string subType = config["SubType"].asString(); |
| 47 |
3/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
|
5 | if (subType == "grid") { |
| 48 | 3 | gridMode = true; | |
| 49 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | buildGrid(); |
| 50 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | } else if (subType == "channelgrid") { |
| 51 | 2 | gridMode = true; | |
| 52 | 2 | channelGridMode = true; | |
| 53 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | buildChannelGrid(); |
| 54 | } | ||
| 55 | 5 | } | |
| 56 |
3/6✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
|
30 | } |
| 57 | |||
| 58 | // Channel-grid (model group) build: each grid cell is a '&'-separated list of | ||
| 59 | // 1-based absolute start channels (the member pixels binned into that cell). We | ||
| 60 | // index outputCells by the channelData slot the base assigned to each cell, so | ||
| 61 | // the scatter reads the right buffer color. | ||
| 62 | 2 | void PixelOverlayModelSub::buildChannelGrid() { | |
| 63 | 2 | const int cpn = channelsPerNode; | |
| 64 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | outputCells.assign((size_t)width * height, std::vector<uint32_t>()); |
| 65 | |||
| 66 |
5/14✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
2 | std::string grid = config.isMember("Grid") ? config["Grid"].asString() : ""; |
| 67 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | std::vector<std::string> rows = split(grid, ';'); |
| 68 | |||
| 69 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (int y = 0; y < height; y++) { |
| 70 | 4 | std::vector<std::string> cells; | |
| 71 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (y < (int)rows.size()) { |
| 72 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | cells = split(rows[y], ','); |
| 73 | } | ||
| 74 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
|
16 | for (int x = 0; x < width; x++) { |
| 75 |
6/6✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 8 times.
|
12 | if (x >= (int)cells.size() || cells[x].empty()) { |
| 76 | 4 | continue; // hole | |
| 77 | } | ||
| 78 | 8 | int buf = (y * width + x) * bytesPerPixel; | |
| 79 |
3/6✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
|
8 | if (buf < 0 || buf >= (int)channelMap.size()) { |
| 80 | ✗ | continue; | |
| 81 | } | ||
| 82 | 8 | uint32_t cd = channelMap[buf]; | |
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (cd == FPPD_OFF_CHANNEL) { |
| 84 | ✗ | continue; | |
| 85 | } | ||
| 86 | 8 | int slot = cd / cpn; | |
| 87 |
3/6✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
|
8 | if (slot < 0 || slot >= (int)outputCells.size()) { |
| 88 | ✗ | continue; | |
| 89 | } | ||
| 90 |
3/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 8 times.
|
22 | for (auto& chStr : split(cells[x], '&')) { |
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (chStr.empty()) { |
| 92 | ✗ | continue; | |
| 93 | } | ||
| 94 | 14 | int ch1 = std::atoi(chStr.c_str()); // 1-based absolute start channel | |
| 95 | // Same bound as buildGrid(): scattered to on the output thread. | ||
| 96 |
2/4✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
14 | if (ch1 > 0 && (int64_t)ch1 - 1 + cpn <= FPPD_MAX_CHANNELS) { |
| 97 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | outputCells[slot].push_back((uint32_t)(ch1 - 1)); |
| 98 | } | ||
| 99 | 8 | } | |
| 100 | } | ||
| 101 | 4 | } | |
| 102 | 2 | } | |
| 103 | |||
| 104 | // Build outputMap: for each populated grid cell, resolve the parent node number | ||
| 105 | // to the absolute (0-based) output channel base. The base class has already | ||
| 106 | // built a dense width*height buffer + channelMap, so we index outputMap by the | ||
| 107 | // channelData slot the base assigned to each cell, making this robust to the | ||
| 108 | // base orientation mapping. | ||
| 109 | 3 | void PixelOverlayModelSub::buildGrid() { | |
| 110 | 3 | int parentStart0 = 0; | |
| 111 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | configInt(config, "ParentStartChannel", 0, parentStart0); // type checked in the constructor |
| 112 | 3 | parentStart0--; | |
| 113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (parentStart0 < 0) { |
| 114 | ✗ | parentStart0 = 0; | |
| 115 | } | ||
| 116 | 3 | const int cpn = channelsPerNode; | |
| 117 | |||
| 118 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | outputMap.assign((size_t)width * height, FPPD_OFF_CHANNEL); |
| 119 | |||
| 120 |
5/14✓ 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.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 3 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
3 | std::string grid = config.isMember("Grid") ? config["Grid"].asString() : ""; |
| 121 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | std::vector<std::string> rows = split(grid, ';'); |
| 122 | |||
| 123 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3 times.
|
11 | for (int y = 0; y < height; y++) { |
| 124 | 8 | std::vector<std::string> cells; | |
| 125 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (y < (int)rows.size()) { |
| 126 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | cells = split(rows[y], ','); |
| 127 | } | ||
| 128 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 8 times.
|
48 | for (int x = 0; x < width; x++) { |
| 129 | 40 | int node = -1; | |
| 130 |
6/6✓ Branch 0 taken 34 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 32 times.
✓ Branch 5 taken 8 times.
|
40 | if (x < (int)cells.size() && !cells[x].empty()) { |
| 131 | 32 | node = std::atoi(cells[x].c_str()); | |
| 132 | } | ||
| 133 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 32 times.
|
40 | if (node <= 0) { |
| 134 | 8 | continue; // hole | |
| 135 | } | ||
| 136 | 32 | int buf = (y * width + x) * bytesPerPixel; | |
| 137 |
3/6✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 32 times.
|
32 | if (buf < 0 || buf >= (int)channelMap.size()) { |
| 138 | ✗ | continue; | |
| 139 | } | ||
| 140 | 32 | uint32_t cd = channelMap[buf]; | |
| 141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (cd == FPPD_OFF_CHANNEL) { |
| 142 | ✗ | continue; | |
| 143 | } | ||
| 144 | 32 | int slot = cd / cpn; | |
| 145 | // doGridOverlay() writes cpn channels at this base on the output | ||
| 146 | // thread, so it has to lie inside the channel buffer; a stale or | ||
| 147 | // corrupt grid could name any node. | ||
| 148 | 32 | int64_t ob = (int64_t)parentStart0 + (int64_t)(node - 1) * cpn; | |
| 149 |
4/8✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
✗ Branch 7 not taken.
|
32 | if (slot >= 0 && slot < (int)outputMap.size() && ob + cpn <= FPPD_MAX_CHANNELS) { |
| 150 | 32 | outputMap[slot] = (uint32_t)ob; | |
| 151 | } | ||
| 152 | } | ||
| 153 | 8 | } | |
| 154 | 3 | } | |
| 155 | |||
| 156 | ✗ | PixelOverlayModelSub::~PixelOverlayModelSub() { | |
| 157 | ✗ | } | |
| 158 | |||
| 159 | // Resolve the parent model, caching the pointer. | ||
| 160 | // | ||
| 161 | // MUST NOT be called from the channel output thread. The output thread reaches | ||
| 162 | // doOverlay() from PixelOverlayManager::doOverlays() with activeModelsLock | ||
| 163 | // already held, and every HTTP handler takes modelsLock before | ||
| 164 | // activeModelsLock (via setState() -> modelStateChanged()), so a lookup from | ||
| 165 | // that side would invert them. Callers are setState() below, and | ||
| 166 | // resolveParent() from the manager when this model or its parent is added | ||
| 167 | // and from getPolarMap() (effect start). The resolution itself only | ||
| 168 | // try-locks modelsLock: setState() can also run on the effect update threads | ||
| 169 | // (an effect stopping calls model->setState() under this model's | ||
| 170 | // effectLock), where BLOCKING on modelsLock is the reverse of the | ||
| 171 | // HTTP/command paths' modelsLock -> effectLock order and deadlocks outright -- | ||
| 172 | // the same shape 37c8f92a8 fixed one lock over. The try-acquire never blocks: | ||
| 173 | // from the manager paths (which already hold modelsLock recursively) it always | ||
| 174 | // succeeds, and a contended effect-side resolve just stays unresolved until | ||
| 175 | // the next setState(). | ||
| 176 | 152123 | bool PixelOverlayModelSub::foundParent() { | |
| 177 | { | ||
| 178 |
1/2✓ Branch 0 taken 152123 times.
✗ Branch 1 not taken.
|
152123 | std::lock_guard<std::mutex> l(parentLock); |
| 179 |
2/2✓ Branch 0 taken 135119 times.
✓ Branch 1 taken 17004 times.
|
152123 | if (parent) |
| 180 | 135119 | return true; | |
| 181 |
2/2✓ Branch 0 taken 17004 times.
✓ Branch 1 taken 135119 times.
|
152123 | } |
| 182 | |||
| 183 | 17004 | PixelOverlayManager& mgr = PixelOverlayManager::INSTANCE; | |
| 184 | 17004 | std::unique_lock<std::recursive_mutex> lock(mgr.modelsLock, std::try_to_lock); | |
| 185 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 16973 times.
|
17004 | if (!lock.owns_lock()) { |
| 186 | // modelsLock contended and not ours; do not log -- the parent may be | ||
| 187 | // perfectly valid, we just can't look it up without risking deadlock. | ||
| 188 | 31 | return false; | |
| 189 | } | ||
| 190 |
3/6✓ Branch 0 taken 16973 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16973 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16973 times.
✗ Branch 5 not taken.
|
16973 | PixelOverlayModel* p = mgr.getModelLocked(config["Parent"].asString()); |
| 191 | |||
| 192 | // Refuse a parent chain that leads back here: a submodel naming itself | ||
| 193 | // (the Parent picker lists every model, this one included) or a loop of | ||
| 194 | // them. doOverlay() calls parent->setData() then parent->doOverlay(), and | ||
| 195 | // with this model somewhere up the chain that never returned - the channel | ||
| 196 | // output thread recursed until its stack overflowed. Grid submodels end | ||
| 197 | // the walk: they scatter to output channels and never call their parent. | ||
| 198 | 16973 | constexpr int MAX_SUBMODEL_DEPTH = 16; | |
| 199 | 16973 | PixelOverlayModel* a = p; | |
| 200 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 16928 times.
|
16977 | for (int depth = 0; a; depth++) { |
| 201 |
3/4✓ Branch 0 taken 43 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 43 times.
|
49 | if (a == this || depth >= MAX_SUBMODEL_DEPTH) { |
| 202 |
3/6✓ 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.
|
6 | LogErr(VB_CHANNELOUT, "Submodel \"%s\" cannot use \"%s\" as its parent: the parent chain leads back to it\n", |
| 203 | name.c_str(), config["Parent"].asString().c_str()); | ||
| 204 | 6 | return false; | |
| 205 | } | ||
| 206 |
1/2✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
|
43 | PixelOverlayModelSub* s = dynamic_cast<PixelOverlayModelSub*>(a); |
| 207 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
43 | if (!s || s->gridMode) { |
| 208 | break; | ||
| 209 | } | ||
| 210 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
4 | a = mgr.getModelLocked(s->config["Parent"].asString()); |
| 211 | } | ||
| 212 | // Where the chain ends; a grid submodel there ends it at no framebuffer. | ||
| 213 |
5/6✓ Branch 0 taken 39 times.
✓ Branch 1 taken 16928 times.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 36 times.
|
16967 | bool toFB = a && a->getType() == "FB"; |
| 214 | |||
| 215 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 16928 times.
|
16967 | if (p) { |
| 216 | // Under modelsLock, so a reload can't retire it (and resetParent() | ||
| 217 | // this model) between the lookup and here. | ||
| 218 |
1/2✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 | std::lock_guard<std::mutex> l(parentLock); |
| 219 | 39 | parent = p; | |
| 220 |
1/2✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 | parentRef = mgr.models[p->getName()].owner; |
| 221 | 39 | fbRoot = toFB; | |
| 222 | 39 | return true; | |
| 223 | 39 | } | |
| 224 | |||
| 225 |
3/6✓ Branch 0 taken 16928 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16928 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16928 times.
✗ Branch 5 not taken.
|
16928 | LogErr(VB_CHANNELOUT, "Unable to find parent \"%s\" for submodel \"%s\"\n", |
| 226 | config["Parent"].asString().c_str(), name.c_str()); | ||
| 227 | 16928 | return false; | |
| 228 | 17004 | } | |
| 229 | 152097 | void PixelOverlayModelSub::setState(const PixelOverlayState& st) { | |
| 230 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 152079 times.
|
152097 | if (gridMode) { |
| 231 | // Grid submodels scatter directly to the parent's output channels; they | ||
| 232 | // never dereference `parent`, so they need no resolution at all, and | ||
| 233 | // they do not register as a rectangular child of the parent. | ||
| 234 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | PixelOverlayModel::setState(st); |
| 235 | 18 | return; | |
| 236 | } | ||
| 237 | |||
| 238 | // Resolve BEFORE the base class publishes this model into the manager's | ||
| 239 | // active list. From the moment it lands there the channel output thread can | ||
| 240 | // call doOverlay() on it, and resolving from that side takes modelsLock | ||
| 241 | // while doOverlays() holds activeModelsLock -- the inversion described on | ||
| 242 | // foundParent(). Doing it here is free: setState() runs under modelsLock | ||
| 243 | // on the manager's paths, so this is a recursive re-acquire there. | ||
| 244 | // | ||
| 245 | // Not every path holds modelsLock, though: an effect that disables its | ||
| 246 | // model when it finishes (Stop Effects, Text/Image with disableWhenDone, | ||
| 247 | // WLED autoDisable) gets here from the effect or output thread, while an | ||
| 248 | // output reload's removeAutoOverlayModel() can retire the parent and | ||
| 249 | // resetParent() this model. So the parent is used through a share of its | ||
| 250 | // ownership taken here: it stays alive for the call even if retired | ||
| 251 | // meanwhile (a retired model can't be made active again), and the call | ||
| 252 | // needs no modelsLock, so it is never dropped or delayed because a | ||
| 253 | // command or reload happens to hold that lock. | ||
| 254 |
1/2✓ Branch 0 taken 152079 times.
✗ Branch 1 not taken.
|
152079 | foundParent(); |
| 255 | 152079 | std::shared_ptr<PixelOverlayModel> p; | |
| 256 | { | ||
| 257 |
1/2✓ Branch 0 taken 152079 times.
✗ Branch 1 not taken.
|
152079 | std::lock_guard<std::mutex> l(parentLock); |
| 258 | 152079 | p = parentRef.lock(); | |
| 259 | 152079 | } | |
| 260 | |||
| 261 |
1/2✓ Branch 0 taken 152079 times.
✗ Branch 1 not taken.
|
152079 | PixelOverlayModel::setState(st); |
| 262 | |||
| 263 |
2/2✓ Branch 0 taken 135117 times.
✓ Branch 1 taken 16962 times.
|
152079 | if (p) { |
| 264 |
1/2✓ Branch 0 taken 135117 times.
✗ Branch 1 not taken.
|
135117 | p->setChildState(name, st, xOffset, yOffset, width, height); |
| 265 | } | ||
| 266 | 152079 | } | |
| 267 | 25 | void PixelOverlayModelSub::resetParent() { | |
| 268 | // Disable first: that takes this model out of the active list under | ||
| 269 | // activeModelsLock, which waits out a doOverlay() already reading | ||
| 270 | // `parent` on the channel output thread. Only then is it safe to clear. | ||
| 271 | // useLock then waits out one from a VirtualMatrix's SendData(), which | ||
| 272 | // the active list does not cover, so the parent is never freed (or its | ||
| 273 | // display handed to a replacement) under a draw. | ||
| 274 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | PixelOverlayModel::setState(PixelOverlayState(PixelOverlayState::Disabled)); |
| 275 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | std::lock_guard<std::mutex> use(useLock); |
| 276 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | std::lock_guard<std::mutex> l(parentLock); |
| 277 | 25 | parent = nullptr; | |
| 278 | 25 | parentRef.reset(); | |
| 279 | 25 | fbRoot = false; | |
| 280 | 25 | } | |
| 281 | 16 | bool PixelOverlayModelSub::readyForPassThrough() { | |
| 282 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | if (fbRoot) { |
| 283 | 16 | passThroughWarned = false; | |
| 284 | 16 | return true; | |
| 285 | } | ||
| 286 | ✗ | if (!passThroughWarned.exchange(true)) { | |
| 287 | bool found; | ||
| 288 | { | ||
| 289 | ✗ | std::lock_guard<std::mutex> l(parentLock); | |
| 290 | ✗ | found = parent != nullptr; | |
| 291 | ✗ | } | |
| 292 | // Not found is also the moment of an output reload, between the old | ||
| 293 | // parent going and its replacement arriving, so only that is quiet. | ||
| 294 | ✗ | if (found) { | |
| 295 | ✗ | LogWarn(VB_CHANNELOUT, "Submodel \"%s\" is not drawn: its parent \"%s\" is not a framebuffer\n", | |
| 296 | name.c_str(), getParentName().c_str()); | ||
| 297 | } else { | ||
| 298 | ✗ | LogInfo(VB_CHANNELOUT, "Submodel \"%s\" is not drawn: its parent \"%s\" has not been found\n", | |
| 299 | name.c_str(), getParentName().c_str()); | ||
| 300 | } | ||
| 301 | } | ||
| 302 | ✗ | return false; | |
| 303 | } | ||
| 304 | 131 | std::string PixelOverlayModelSub::getParentName() const { | |
| 305 | 131 | return config["Parent"].asString(); | |
| 306 | } | ||
| 307 | |||
| 308 | 80 | uint32_t PixelOverlayModelSub::outputChannelForData(uint32_t dataOffset) const { | |
| 309 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 56 times.
|
80 | if (!gridMode) { |
| 310 | // A rectangular submodel draws through its parent, so its cells land | ||
| 311 | // wherever the parent's do at the offset cell; the base class's | ||
| 312 | // startChannel arithmetic is meaningless here (startChannel is forced | ||
| 313 | // to 0 for a submodel), and joined the polar layout against channels | ||
| 314 | // 0..N. channelData is node-major at channelsPerNode. | ||
| 315 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | if (!parent || channelsPerNode <= 0) { |
| 316 | ✗ | return FPPD_OFF_CHANNEL; | |
| 317 | } | ||
| 318 | 24 | int node = dataOffset / channelsPerNode; | |
| 319 | 24 | int k = dataOffset % channelsPerNode; | |
| 320 | 24 | uint32_t ch = parent->outputChannelForCell(node % width + xOffset, node / width + yOffset); | |
| 321 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | return ch == FPPD_OFF_CHANNEL ? ch : ch + k; |
| 322 | } | ||
| 323 | 56 | int slot = dataOffset / channelsPerNode; | |
| 324 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | if (channelGridMode) { |
| 325 | // A cell may feed several member pixels; they are one buffer cell and | ||
| 326 | // therefore one position, so the first is the representative one. | ||
| 327 | ✗ | if (slot < 0 || slot >= (int)outputCells.size() || outputCells[slot].empty()) { | |
| 328 | ✗ | return FPPD_OFF_CHANNEL; | |
| 329 | } | ||
| 330 | ✗ | return outputCells[slot][0]; | |
| 331 | } | ||
| 332 |
3/6✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 56 times.
|
56 | if (slot < 0 || slot >= (int)outputMap.size()) { |
| 333 | ✗ | return FPPD_OFF_CHANNEL; | |
| 334 | } | ||
| 335 | 56 | return outputMap[slot]; | |
| 336 | } | ||
| 337 | |||
| 338 | 4828 | void PixelOverlayModelSub::doOverlay(uint8_t* channels) { | |
| 339 |
2/4✓ Branch 0 taken 4828 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4828 times.
|
4828 | if (PixelOverlayModel::overlayBufferIsDirty()) |
| 340 | ✗ | flushOverlayBuffer(); | |
| 341 | |||
| 342 |
2/2✓ Branch 0 taken 117 times.
✓ Branch 1 taken 4711 times.
|
4828 | if (gridMode) { |
| 343 |
1/2✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
|
117 | doGridOverlay(channels); |
| 344 | 2998 | return; | |
| 345 | } | ||
| 346 | |||
| 347 | // The parent already resolved, NOT foundParent(): this runs on the channel | ||
| 348 | // output thread, where the lookup inverts the locks (see foundParent()), | ||
| 349 | // and retrying every frame would spam the log. None means it does not | ||
| 350 | // exist (yet); setState() or the VirtualMatrix reports that once. | ||
| 351 |
1/2✓ Branch 0 taken 4711 times.
✗ Branch 1 not taken.
|
4711 | std::lock_guard<std::mutex> use(useLock); |
| 352 | PixelOverlayModel* p; | ||
| 353 | { | ||
| 354 |
1/2✓ Branch 0 taken 4711 times.
✗ Branch 1 not taken.
|
4711 | std::lock_guard<std::mutex> l(parentLock); |
| 355 | 4711 | p = parent; | |
| 356 | 4711 | } | |
| 357 |
2/2✓ Branch 0 taken 947 times.
✓ Branch 1 taken 3764 times.
|
4711 | if (!p) |
| 358 | 947 | return; | |
| 359 | // A submodel that no longer fits its parent (the parent's geometry changed | ||
| 360 | // after the submodel was saved) can't be drawn: setChildState() warned | ||
| 361 | // about it once. Asking the parent anyway logged "region is out of | ||
| 362 | // bounds" on every frame an effect touched it. | ||
| 363 |
1/2✓ Branch 0 taken 3764 times.
✗ Branch 1 not taken.
|
3764 | if (xOffset < 0 || yOffset < 0 || |
| 364 |
5/6✓ Branch 0 taken 3764 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3688 times.
✓ Branch 3 taken 76 times.
✓ Branch 4 taken 76 times.
✓ Branch 5 taken 3688 times.
|
11216 | (int64_t)xOffset + width > p->getWidth() || |
| 365 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3688 times.
|
3688 | (int64_t)yOffset + height > p->getHeight()) { |
| 366 | 76 | dirtyBuffer = false; | |
| 367 | 76 | return; | |
| 368 | } | ||
| 369 | |||
| 370 |
1/2✓ Branch 0 taken 3688 times.
✗ Branch 1 not taken.
|
3688 | dirtyBuffer = dirtyBuffer | p->needRefresh(); |
| 371 |
2/2✓ Branch 0 taken 1858 times.
✓ Branch 1 taken 1830 times.
|
3688 | if (!dirtyBuffer) |
| 372 | 1858 | return; | |
| 373 | |||
| 374 | // channelData is packed at this model's node width, but the parent reads | ||
| 375 | // the region at its own bytes per pixel. A UI-made submodel is 3 channels | ||
| 376 | // per node, so on an RGBW parent that read ran a third past the end of | ||
| 377 | // channelData (and shifted every colour). Repack when they differ; this | ||
| 378 | // runs only on the channel output thread, hence the thread_local. | ||
| 379 | 1830 | const uint8_t* data = channelData; | |
| 380 | 1830 | const int pb = p->getBytesPerPixel(); | |
| 381 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1829 times.
|
1830 | if (pb != channelsPerNode) { |
| 382 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | static thread_local std::vector<uint8_t> repacked; |
| 383 | 1 | const size_t px = (size_t)width * height; | |
| 384 | 1 | const int n = std::min(pb, channelsPerNode); | |
| 385 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | repacked.assign(px * pb, 0); |
| 386 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | for (size_t i = 0; i < px; i++) { |
| 387 | 4 | memcpy(&repacked[i * pb], &channelData[i * channelsPerNode], n); | |
| 388 | } | ||
| 389 | 1 | data = repacked.data(); | |
| 390 | } | ||
| 391 |
1/2✓ Branch 0 taken 1830 times.
✗ Branch 1 not taken.
|
1830 | p->setData(data, xOffset, yOffset, width, height, state); |
| 392 |
1/2✓ Branch 0 taken 1830 times.
✗ Branch 1 not taken.
|
1830 | p->doOverlay(channels); |
| 393 | 1830 | dirtyBuffer = false; | |
| 394 |
2/2✓ Branch 0 taken 1830 times.
✓ Branch 1 taken 2881 times.
|
4711 | } |
| 395 | |||
| 396 | // Scatter each populated grid cell's color from channelData (dense, indexed by | ||
| 397 | // the base class buffer slot) to the parent's absolute output channel, honoring | ||
| 398 | // the overlay state. Unlike a normal model's doOverlay() this never does a | ||
| 399 | // bulk copy, so sibling pixels that are not part of the submodel are untouched. | ||
| 400 | // Scatter one node's color (src, cpn bytes) to one output channel base (dst), | ||
| 401 | // honoring the overlay state. | ||
| 402 | 1698 | static inline void scatterNode(uint8_t* dst, const uint8_t* src, int cpn, int st) { | |
| 403 |
3/4✓ Branch 0 taken 1522 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 88 times.
✗ Branch 3 not taken.
|
1698 | switch (st) { |
| 404 | 1522 | case 1: // Opaque | |
| 405 | 1522 | memcpy(dst, src, cpn); | |
| 406 | 1522 | break; | |
| 407 | 88 | case 2: // Transparent (per-channel) | |
| 408 |
2/2✓ Branch 0 taken 264 times.
✓ Branch 1 taken 88 times.
|
352 | for (int k = 0; k < cpn; k++) { |
| 409 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 208 times.
|
264 | if (src[k]) { |
| 410 | 56 | dst[k] = src[k]; | |
| 411 | } | ||
| 412 | } | ||
| 413 | 88 | break; | |
| 414 | 88 | case 3: { // Transparent RGB(W) (per-node) | |
| 415 | 88 | bool any = false; | |
| 416 |
2/2✓ Branch 0 taken 264 times.
✓ Branch 1 taken 88 times.
|
352 | for (int k = 0; k < cpn; k++) { |
| 417 | 264 | any |= src[k]; | |
| 418 | } | ||
| 419 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 32 times.
|
88 | if (any) { |
| 420 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 56 times.
|
224 | for (int k = 0; k < cpn; k++) { |
| 421 | 168 | dst[k] = src[k]; | |
| 422 | } | ||
| 423 | } | ||
| 424 | 88 | break; | |
| 425 | } | ||
| 426 | } | ||
| 427 | 1698 | } | |
| 428 | |||
| 429 | 117 | void PixelOverlayModelSub::doGridOverlay(uint8_t* channels) { | |
| 430 |
1/2✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
|
117 | int st = getState().getState(); |
| 431 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
|
117 | if (st == 0) { |
| 432 | ✗ | dirtyBuffer = false; | |
| 433 | ✗ | return; | |
| 434 | } | ||
| 435 | 117 | const int cpn = channelsPerNode; | |
| 436 | |||
| 437 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 87 times.
|
117 | if (channelGridMode) { |
| 438 | // Model group: each cell may map to multiple member channels. | ||
| 439 | 30 | const int slots = (int)outputCells.size(); | |
| 440 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 30 times.
|
210 | for (int slot = 0; slot < slots; slot++) { |
| 441 | 180 | const std::vector<uint32_t>& chans = outputCells[slot]; | |
| 442 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 120 times.
|
180 | if (chans.empty()) { |
| 443 | 60 | continue; | |
| 444 | } | ||
| 445 | 120 | uint8_t* src = &channelData[slot * cpn]; | |
| 446 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 120 times.
|
330 | for (uint32_t ob : chans) { |
| 447 | 210 | scatterNode(&channels[ob], src, cpn, st); | |
| 448 | } | ||
| 449 | } | ||
| 450 | 30 | dirtyBuffer = false; | |
| 451 | 30 | return; | |
| 452 | } | ||
| 453 | |||
| 454 | 87 | const int slots = (int)outputMap.size(); | |
| 455 |
2/2✓ Branch 0 taken 1776 times.
✓ Branch 1 taken 87 times.
|
1863 | for (int slot = 0; slot < slots; slot++) { |
| 456 | 1776 | uint32_t ob = outputMap[slot]; | |
| 457 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 1488 times.
|
1776 | if (ob == FPPD_OFF_CHANNEL) { |
| 458 | 288 | continue; | |
| 459 | } | ||
| 460 | 1488 | scatterNode(&channels[ob], &channelData[slot * cpn], cpn, st); | |
| 461 | } | ||
| 462 | 87 | dirtyBuffer = false; | |
| 463 | } | ||
| 464 | |||
| 465 | 37572 | void PixelOverlayModelSub::setData(const uint8_t* data) { | |
| 466 | // Don't call foundParent() here to avoid potential deadlock | ||
| 467 | // when called from flushOverlayBuffer() while holding runningEffectMutex. | ||
| 468 | // The parent will be resolved in doOverlay() before data is needed. | ||
| 469 | 37572 | PixelOverlayModel::setData(data); | |
| 470 | |||
| 471 | 37572 | dirtyBuffer = true; | |
| 472 | 37572 | } | |
| 473 |