| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | /* | ||
| 3 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 4 | * 2013-2022 by the Falcon Player Developers. | ||
| 5 | * | ||
| 6 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 7 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 8 | * file for descriptions of what files are covered by each license. | ||
| 9 | * | ||
| 10 | * This source file is covered under the LGPL v2.1 as described in the | ||
| 11 | * included LICENSE.LGPL file. | ||
| 12 | */ | ||
| 13 | |||
| 14 | // PLUGIN ABI: native plugins compile against this header and are loaded with | ||
| 15 | // dlopen(), so parts of it are baked into binaries FPP did not build. | ||
| 16 | // - PixelOverlayModel's data layout: plugins call the inline getters | ||
| 17 | // (getName/getType/getWidth/getHeight/getSize/getRunningEffect/ | ||
| 18 | // getRunningEffectMutex), which read members at compiled-in offsets. | ||
| 19 | // - PixelOverlayModel's vtable (plugins call setState() etc. virtually). | ||
| 20 | // - PixelOverlayState, which plugins construct and pass by value. | ||
| 21 | // Any such change must bump FPP_PLUGIN_API_VERSION in Plugin.h. Without the | ||
| 22 | // bump, a plugin built against the previous header still loads and silently | ||
| 23 | // uses the old layout - FPP 10.2 shipped exactly that and crash-looped fppd. | ||
| 24 | // See .claude/PLUGIN-ABI.md for which plugins use what. | ||
| 25 | |||
| 26 | #include <list> | ||
| 27 | #include <map> | ||
| 28 | #include "fpp-json.h" | ||
| 29 | #include <memory> | ||
| 30 | #include <mutex> | ||
| 31 | #include <thread> | ||
| 32 | #include <vector> | ||
| 33 | |||
| 34 | #include "../AtomicSharedPtr.h" | ||
| 35 | #include "PolarBufferMap.h" | ||
| 36 | |||
| 37 | // Raised when a model's channel data or overlay buffer cannot be allocated at | ||
| 38 | // all -- shared memory, an anonymous mapping and malloc() all failed, which | ||
| 39 | // means the box is out of memory. Deliberately permanent: nothing rechecks the | ||
| 40 | // allocation, and the model stays unusable until fppd restarts. | ||
| 41 | constexpr int WARNING_ID_MODEL_MEMORY = 64; | ||
| 42 | constexpr const char* MODEL_MEMORY_WARNING = | ||
| 43 | "Out of memory allocating pixel overlay model buffers - some models will not display"; | ||
| 44 | |||
| 45 | class RunningEffect; | ||
| 46 | |||
| 47 | class PixelOverlayState { | ||
| 48 | public: | ||
| 49 | enum PixelState : uint8_t { | ||
| 50 | Disabled, | ||
| 51 | Enabled, | ||
| 52 | Transparent, | ||
| 53 | TransparentRGB | ||
| 54 | }; | ||
| 55 | |||
| 56 | 53567 | PixelOverlayState() : | |
| 57 | 53567 | state(PixelState::Disabled) {} | |
| 58 | 204994 | constexpr PixelOverlayState(PixelState v) : | |
| 59 | 204994 | state(v) {} | |
| 60 | 227115 | constexpr PixelOverlayState(int v) : | |
| 61 | 227115 | state((PixelState)v) {} | |
| 62 | 131500 | PixelOverlayState(const std::string& v) { | |
| 63 |
8/10✓ Branch 0 taken 131484 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 131484 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 131482 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 131482 times.
✓ Branch 8 taken 18 times.
✓ Branch 9 taken 131482 times.
|
131500 | if (v == "Disabled" || v == "false" || v == "False" || v == "0") { |
| 64 | 18 | state = PixelState::Disabled; | |
| 65 |
8/10✓ Branch 0 taken 51698 times.
✓ Branch 1 taken 79784 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 51692 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
✓ Branch 8 taken 131476 times.
✓ Branch 9 taken 6 times.
|
131482 | } else if (v == "Enabled" || v == "true" || v == "True" || v == "1") { |
| 66 | 131476 | state = PixelState::Enabled; | |
| 67 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | } else if (v == "Transparent") { |
| 68 | 4 | state = PixelState::Transparent; | |
| 69 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | } else if (v == "TransparentRGB" || v == "Transparent RGB") { |
| 70 | 2 | state = PixelState::TransparentRGB; | |
| 71 | } else { | ||
| 72 | ✗ | state = PixelState::Disabled; | |
| 73 | } | ||
| 74 | 131500 | } | |
| 75 | |||
| 76 | 1456353 | PixelState getState() const { return state; } | |
| 77 | 348682 | bool operator==(PixelOverlayState a) const { return state == a.state; } | |
| 78 | 21224 | bool operator!=(PixelOverlayState a) const { return state != a.state; } | |
| 79 | |||
| 80 | private: | ||
| 81 | PixelState state; | ||
| 82 | }; | ||
| 83 | |||
| 84 | class PixelOverlayModel { | ||
| 85 | public: | ||
| 86 | PixelOverlayModel(const Json::Value& config); | ||
| 87 | virtual ~PixelOverlayModel(); | ||
| 88 | |||
| 89 | 1031336 | const std::string& getName() const { return name; }; | |
| 90 | 59983 | const std::string& getType() const { return type; }; | |
| 91 | |||
| 92 | // How this model's BUFFER is laid out, when it is not the plain | ||
| 93 | // wiring-order rectangle. Read from the model definition's "BufferStyle" | ||
| 94 | // and reported in toJson(), so a producer (an xLights bridge script, a | ||
| 95 | // plugin) can declare a model whose axes already mean something and the UI | ||
| 96 | // can group and label it accordingly rather than filing it under whatever | ||
| 97 | // file it happened to arrive in. Empty for an ordinary model. | ||
| 98 | // | ||
| 99 | // Known values: "" (ordinary) and "polar" (x/y are radius and angle). | ||
| 100 | const std::string& getBufferStyle() const { return bufferStyle; } | ||
| 101 | |||
| 102 | 4003808 | int getWidth() const { return width; } | |
| 103 | 3995058 | int getHeight() const { return height; } | |
| 104 | 6171 | void getSize(int& w, int& h) const { | |
| 105 | 6171 | w = width; | |
| 106 | 6171 | h = height; | |
| 107 | 6171 | } | |
| 108 | |||
| 109 | PixelOverlayState getState() const; | ||
| 110 | // For the manager, when it retires this model (removeAutoOverlayModel()): | ||
| 111 | // gives up the shared memory names, which a replacement of the same name | ||
| 112 | // is about to take. The model may live on for a while in a reference | ||
| 113 | // holder's hands, and its destructor then leaves the names alone; a | ||
| 114 | // buffer it allocates after this is private memory. | ||
| 115 | void releaseSharedMemoryNames(); | ||
| 116 | // For the manager, when it drops a model it built but never registered | ||
| 117 | // because another live model has its name: gives up the names without | ||
| 118 | // unlinking them, so its destructor leaves the other model's shared | ||
| 119 | // memory alone. | ||
| 120 | void abandonSharedMemoryNames(); | ||
| 121 | |||
| 122 | // A model's name as the constructor makes it from a definition: "/" is | ||
| 123 | // not allowed in a name (it would be a path in a shm name or a file). | ||
| 124 | static std::string normalizedName(std::string name); | ||
| 125 | // The name a definition gives its model, read and cleaned exactly as the | ||
| 126 | // constructor does it; false if Name is an object or an array. | ||
| 127 | static bool configName(const Json::Value& c, std::string& out); | ||
| 128 | // The absolute output channel buffer cell (x, y)'s first byte lands on, | ||
| 129 | // or FPPD_OFF_CHANNEL for a hole or a cell outside the model. | ||
| 130 | uint32_t outputChannelForCell(int x, int y) const; | ||
| 131 | // Whether the channel output thread has to draw this model: its own state | ||
| 132 | // is enabled, or a submodel is enabled within it. The manager's active | ||
| 133 | // list is kept in step with this, not with individual state transitions. | ||
| 134 | bool needsOutput() const; | ||
| 135 | virtual void setState(const PixelOverlayState& state); | ||
| 136 | |||
| 137 | virtual void doOverlay(uint8_t* channels); | ||
| 138 | |||
| 139 | // A polar (radius x angle) view of this model's buffer, for the Radial/ | ||
| 140 | // Angular buffer mappings. Built on first use from the model's geometry in | ||
| 141 | // config/virtualdisplaymap and cached; the result is owned by the model. | ||
| 142 | // | ||
| 143 | // Returns nullptr whenever a polar layout cannot be built -- no layout file | ||
| 144 | // (a player that has never used xLights), this model absent from it, or too | ||
| 145 | // little geometry. Callers MUST fall back to the ordinary buffer mapping, | ||
| 146 | // never treat it as an error. | ||
| 147 | // | ||
| 148 | // Not thread safe against itself: call it from the command path that starts | ||
| 149 | // an effect, never from the channel output thread. | ||
| 150 | const PolarBufferMap* getPolarMap(PolarMode mode); | ||
| 151 | |||
| 152 | int getStartChannel() const; | ||
| 153 | int getChannelCount() const; | ||
| 154 | |||
| 155 | void toJson(Json::Value& v); | ||
| 156 | void getDataJson(Json::Value& v, bool rle = false); | ||
| 157 | // Binary equivalent of getDataJson(): the current pixel data as a flat | ||
| 158 | // width*height*bytesPerPixel buffer. | ||
| 159 | void getData(std::vector<uint8_t>& out); | ||
| 160 | |||
| 161 | // Access to the channelData. The channelData is a minimal array of bytes | ||
| 162 | // If the model is a "custom" model or using singleChannel nodes or similar, | ||
| 163 | // then the channelData will be significantly smaller than WxHxBPP | ||
| 164 | 2610959 | int getBytesPerPixel() const { return bytesPerPixel; } | |
| 165 | void saveOverlayAsImage(std::string filename = ""); | ||
| 166 | // True for a name saveOverlayAsImage() accepts: no directory part, | ||
| 167 | // nothing hidden or relative. | ||
| 168 | static bool isPlainImageFileName(const std::string& f); | ||
| 169 | virtual void setData(const uint8_t* data); // full pixel data, width*height*bytesPerPixel | ||
| 170 | virtual void setData(const uint8_t* data, int xOffset, int yOffset, int w, int h, const PixelOverlayState& st = PixelOverlayState(PixelOverlayState::Enabled)); | ||
| 171 | |||
| 172 | // Blit externally supplied pixel data onto the model. src is srcW*srcH | ||
| 173 | // pixels of srcBpp bytes each (1 = greyscale, 3 = RGB, 4 = RGBW); it is | ||
| 174 | // converted to this model's bytesPerPixel and CLIPPED to the model bounds, | ||
| 175 | // so a rect hanging off an edge draws the part that is visible instead of | ||
| 176 | // being rejected the way setData()'s rect form is. That is what lets a | ||
| 177 | // caller slide a sprite off the edge by reposting it at moving offsets. | ||
| 178 | // Returns false only when nothing landed inside the model at all. | ||
| 179 | // | ||
| 180 | // blitData() writes the output channel data (honoring st as the per-blit | ||
| 181 | // blend); blitOverlayBuffer() writes the mmapped overlay image buffer and | ||
| 182 | // marks it dirty, so the write composites into the same persistent canvas | ||
| 183 | // external mmap clients use and is flushed on the next output pass. | ||
| 184 | bool blitData(const uint8_t* src, int srcW, int srcH, int srcBpp, | ||
| 185 | int xOffset, int yOffset, const PixelOverlayState& st); | ||
| 186 | bool blitOverlayBuffer(const uint8_t* src, int srcW, int srcH, int srcBpp, | ||
| 187 | int xOffset, int yOffset); | ||
| 188 | |||
| 189 | void setScaledData(uint8_t* data, int w, int h); | ||
| 190 | void setPixelValue(int x, int y, int r, int g, int b); | ||
| 191 | void setPixelValue(int x, int y, int r, int g, int b, int w); | ||
| 192 | void getPixelValue(int x, int y, int& r, int& g, int& b); | ||
| 193 | void getPixelValue(int x, int y, int& r, int& g, int& b, int& w); | ||
| 194 | void clearData(); | ||
| 195 | void fillData(int r, int g, int b); | ||
| 196 | void fillData(int r, int g, int b, int w); | ||
| 197 | void setBufferIsDirty(bool dirty = true); | ||
| 198 | bool needRefresh(); | ||
| 199 | |||
| 200 | // False when the model's channel data could not be allocated at all. Such | ||
| 201 | // a model has no storage behind any of its accessors, so the manager drops | ||
| 202 | // it rather than registering one that cannot be indexed. | ||
| 203 | 1910 | bool isValid() const { return channelData != nullptr; } | |
| 204 | |||
| 205 | // The overlay buffer is a full continuous width*height*bytesPerPixel buffer | ||
| 206 | // that can be used to construct the frame as a full RGB(W) image prior to | ||
| 207 | // flushing to the channelData. The overlay buffer is also mmapped so | ||
| 208 | // external programs can have easy access to it. | ||
| 209 | uint8_t* getOverlayBuffer(); | ||
| 210 | void setOverlayBufferDirty(bool dirty = true); | ||
| 211 | bool overlayBufferIsDirty(); | ||
| 212 | void clearOverlayBuffer(); | ||
| 213 | void setOverlayBufferScaledData(uint8_t* data, int w, int h); | ||
| 214 | void fillOverlayBuffer(int r, int g, int b); | ||
| 215 | void fillOverlayBuffer(int r, int g, int b, int w); | ||
| 216 | void setOverlayPixelValue(int x, int y, int r, int g, int b); | ||
| 217 | void setOverlayPixelValue(int x, int y, int r, int g, int b, int w); | ||
| 218 | void getOverlayPixelValue(int x, int y, int& r, int& g, int& b); | ||
| 219 | void getOverlayPixelValue(int x, int y, int& r, int& g, int& b, int& w); | ||
| 220 | void flushOverlayBuffer(); | ||
| 221 | |||
| 222 | // Operate on both the overlay buffer (if mapped) and the channelData | ||
| 223 | void clear(); | ||
| 224 | void fill(int r, int g, int b); | ||
| 225 | void fill(int r, int g, int b, int w); | ||
| 226 | |||
| 227 | bool applyEffect(const std::string& autoState, const std::string& effect, const std::vector<std::string>& args); | ||
| 228 | void setRunningEffect(RunningEffect* r, int32_t firstUpdateMS); | ||
| 229 | |||
| 230 | // Retire the running effect WITHOUT clearing what it last drew. This is | ||
| 231 | // the "nothing takes over" case setRunningEffect() cannot express, and the | ||
| 232 | // Text effect needs it: a still, centred message writes its pixels | ||
| 233 | // directly, so an effect left repainting from an earlier run (a scroll, or | ||
| 234 | // an animated colour mode) would paint straight back over it. | ||
| 235 | void clearRunningEffect(); | ||
| 236 | |||
| 237 | 906782 | std::recursive_mutex& getRunningEffectMutex() { return effectLock; } | |
| 238 | 909149 | RunningEffect* getRunningEffect() const { return runningEffect; } // make sure you have the mutex locked | |
| 239 | int32_t updateRunningEffects(); | ||
| 240 | |||
| 241 | void setChildState(const std::string& n, const PixelOverlayState& state, int ox, int oy, int w, int h); | ||
| 242 | bool isAutoCreated(); | ||
| 243 | |||
| 244 | protected: | ||
| 245 | // Constructor helpers: read a config field without throwing (a number, | ||
| 246 | // or a number in a string; false for anything else), and reject the | ||
| 247 | // definition. See the constructor. | ||
| 248 | static bool configInt(const Json::Value& c, const char* key, int dflt, int& out); | ||
| 249 | static bool configString(const Json::Value& c, const char* key, std::string& out); | ||
| 250 | void markInvalid(const std::string& why); | ||
| 251 | |||
| 252 | void setValue(uint8_t v, int startChannel = -1, int endChannel = -1); | ||
| 253 | bool flushChildren(uint8_t* dst); | ||
| 254 | |||
| 255 | // The 0-based ABSOLUTE output channel a channelData offset ends up on, or | ||
| 256 | // FPPD_OFF_CHANNEL. A plain model owns a contiguous range so it is just an | ||
| 257 | // addition; a submodel or group scatters, so those override this. Used only | ||
| 258 | // to join buffer cells to their position in the virtual display map. | ||
| 259 | virtual uint32_t outputChannelForData(uint32_t dataOffset) const; | ||
| 260 | |||
| 261 | // Cached polar views, keyed by mode AND the layout file's mtime. | ||
| 262 | // | ||
| 263 | // One entry per key, never replaced or erased: a running effect holds a | ||
| 264 | // bare pointer into this for its lifetime, so rebuilding in place would | ||
| 265 | // dangle it. std::map node addresses are stable across inserts, which is | ||
| 266 | // the property relied on. An entry with valid() == false records | ||
| 267 | // "tried, cannot" so a model with no geometry costs one attempt. | ||
| 268 | // | ||
| 269 | // The mtime in the key is what makes a re-upload from xLights take effect: | ||
| 270 | // a model that already built a map would otherwise keep its stale layout | ||
| 271 | // until fppd restarted. A superseded entry is left behind rather than | ||
| 272 | // erased (a few KB, only when the layout actually changes mid-run) because | ||
| 273 | // an effect may still be pointing at it. | ||
| 274 | std::map<std::pair<PolarMode, long long>, PolarBufferMap> polarMaps; | ||
| 275 | |||
| 276 | Json::Value config; | ||
| 277 | std::string name; | ||
| 278 | std::string type; | ||
| 279 | int width, height; | ||
| 280 | std::string bufferStyle; | ||
| 281 | PixelOverlayState state; | ||
| 282 | int startChannel; | ||
| 283 | int channelCount; | ||
| 284 | int channelsPerNode; | ||
| 285 | int bytesPerPixel; // 3 for RGB, 4 for RGBW (overlay buffer stride) | ||
| 286 | |||
| 287 | std::vector<uint32_t> channelMap; | ||
| 288 | uint8_t* channelData; | ||
| 289 | |||
| 290 | volatile bool dirtyBuffer = false; | ||
| 291 | |||
| 292 | struct OverlayBufferData { | ||
| 293 | uint32_t width; | ||
| 294 | uint32_t height; | ||
| 295 | uint32_t flags; // bit 0: dirty, bits 8-15: bytesPerPixel (0 means 3 for backward compat) | ||
| 296 | uint8_t data[4]; | ||
| 297 | } __attribute__((__packed__)); | ||
| 298 | OverlayBufferData* overlayBufferData; | ||
| 299 | |||
| 300 | // How each buffer was allocated, so the destructor releases it the same | ||
| 301 | // way. munmap() on a malloc()ed pointer either fails outright or, if the | ||
| 302 | // allocation happened to be page aligned, unmaps memory the allocator still | ||
| 303 | // believes it owns. | ||
| 304 | bool overlayBufferMapped = true; | ||
| 305 | bool channelDataMapped = true; | ||
| 306 | |||
| 307 | std::recursive_mutex effectLock; | ||
| 308 | RunningEffect* runningEffect; | ||
| 309 | |||
| 310 | class ChildModelState { | ||
| 311 | public: | ||
| 312 | std::string name; | ||
| 313 | PixelOverlayState state = PixelOverlayState::Disabled; | ||
| 314 | int xoffset = 0; | ||
| 315 | int yoffset = 0; | ||
| 316 | int width = 0; | ||
| 317 | int height = 0; | ||
| 318 | }; | ||
| 319 | |||
| 320 | // The child list is copy-on-write, published through an AtomicSharedPtr, | ||
| 321 | // and DELIBERATELY not guarded by any of the overlay locks. | ||
| 322 | // | ||
| 323 | // Readers (flushChildren(), doOverlay(), the FB subclass) run on the | ||
| 324 | // channel output thread, which reaches them from doOverlays() holding | ||
| 325 | // activeModelsLock. The writer, setChildState(), is reached from | ||
| 326 | // PixelOverlayModelSub::setState() -- an HTTP/command thread under | ||
| 327 | // modelsLock, or an effect update thread under that model's effectLock. | ||
| 328 | // Guarding `children` with a lock that either side already holds is what | ||
| 329 | // makes this area deadlock: activeModelsLock -> X on the reader against | ||
| 330 | // modelsLock/effectLock -> X on the writer closes a cycle with the | ||
| 331 | // existing modelsLock -> activeModelsLock and modelsLock -> effectLock | ||
| 332 | // orders (see PixelOverlayModelSub::foundParent()). | ||
| 333 | // | ||
| 334 | // Copy-on-write sidesteps the ordering question entirely: a reader takes a | ||
| 335 | // snapshot and iterates a list nobody can mutate or free, so it needs no | ||
| 336 | // lock at all. Previously readers walked the live std::list while | ||
| 337 | // setChildState() erased nodes out from under them, which faulted in | ||
| 338 | // flushChildren() on a freed node's garbage width/height. | ||
| 339 | // | ||
| 340 | // childrenWriteLock serializes only the read-copy-publish sequence in | ||
| 341 | // setChildState() -- writers are genuinely concurrent (HTTP vs. effect | ||
| 342 | // thread), so without it one update can be lost. It is a LEAF lock: it is | ||
| 343 | // held across nothing but list copying and the store, never across a call | ||
| 344 | // that could reach another lock (notably not modelStateChanged(), which | ||
| 345 | // takes activeModelsLock). Keep it that way and it cannot participate in | ||
| 346 | // a cycle. | ||
| 347 | typedef std::list<ChildModelState> ChildModelStateList; | ||
| 348 | AtomicSharedPtr<const ChildModelStateList> children; | ||
| 349 | std::mutex childrenWriteLock; | ||
| 350 | |||
| 351 | // A null snapshot means "no children" -- models without submodels never | ||
| 352 | // allocate a list. | ||
| 353 | 133023 | bool hasChildren() const { | |
| 354 |
1/2✓ Branch 0 taken 133023 times.
✗ Branch 1 not taken.
|
133023 | auto snap = children.load(); |
| 355 |
4/4✓ Branch 0 taken 16369 times.
✓ Branch 1 taken 116654 times.
✓ Branch 2 taken 9113 times.
✓ Branch 3 taken 7256 times.
|
266046 | return snap && !snap->empty(); |
| 356 | 133023 | } | |
| 357 | }; | ||
| 358 |