Skip to content

Commit

Permalink
Make sure mutex is aquired whenever using the running effect
Browse files Browse the repository at this point in the history
  • Loading branch information
dkulp committed Nov 17, 2023
1 parent 03d71aa commit 93f17aa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/overlays/PixelOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ HTTP_RESPONSE_CONST std::shared_ptr<httpserver::http_response> PixelOverlayManag
PixelOverlayModel* m = models[mn];
m->toJson(model);
model["isActive"] = (int)m->getState().getState();
std::unique_lock<std::mutex> lock(m->getRunningEffectMutex());
if (m->getRunningEffect()) {
model["effectName"] = m->getRunningEffect()->name();
model["isLocked"] = true;
Expand All @@ -473,6 +474,7 @@ HTTP_RESPONSE_CONST std::shared_ptr<httpserver::http_response> PixelOverlayManag
std::unique_lock<std::mutex> lock(modelsLock);
auto m = getModel(p3);
if (m) {
std::unique_lock<std::mutex> lock(m->getRunningEffectMutex());
if (p4 == "data") {
Json::Value data;
m->getDataJson(data, p5 == "rle");
Expand Down
17 changes: 10 additions & 7 deletions src/overlays/PixelOverlayEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,13 +611,6 @@ class TextEffect : public PixelOverlayEffect {
}
image2.modifyImage();

TextMovementEffect* ef = dynamic_cast<TextMovementEffect*>(m->getRunningEffect());
if (ef == nullptr) {
ef = new TextMovementEffect(m);
ef->x = (int)x;
ef->y = (int)y;
}

const MagickLib::PixelPacket* pixel_cache = image2.getConstPixels(0, 0, image2.columns(), image2.rows());
uint8_t* newData = (uint8_t*)malloc(image2.columns() * image2.rows() * 3);
for (int yi = 0; yi < image2.rows(); yi++) {
Expand All @@ -639,6 +632,14 @@ class TextEffect : public PixelOverlayEffect {
np[2] = b;
}
}

std::unique_lock<std::mutex> lock(m->getRunningEffectMutex());
TextMovementEffect* ef = dynamic_cast<TextMovementEffect*>(m->getRunningEffect());
if (ef == nullptr) {
ef = new TextMovementEffect(m);
ef->x = (int)x;
ef->y = (int)y;
}
ef->speed = pixelsPerSecond;
ef->disableWhenDone = disableWhenDone;
ef->direction = position;
Expand All @@ -651,6 +652,8 @@ class TextEffect : public PixelOverlayEffect {
ef->imageDataCols = image2.columns();
ef->imageDataRows = image2.rows();
ef->copyImageData(ef->x, ef->y);
lock.unlock();

m->setRunningEffect(ef, t);
free(old);
}
Expand Down
4 changes: 3 additions & 1 deletion src/overlays/PixelOverlayModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ class PixelOverlayModel {

bool applyEffect(const std::string& autoState, const std::string& effect, const std::vector<std::string>& args);
void setRunningEffect(RunningEffect* r, int32_t firstUpdateMS);
RunningEffect* getRunningEffect() const { return runningEffect; }

std::mutex& getRunningEffectMutex() { return effectLock; }
RunningEffect* getRunningEffect() const { return runningEffect; } // make sure you have the mutex locked
int32_t updateRunningEffects();

void setChildState(const std::string& n, const PixelOverlayState& state, int ox, int oy, int w, int h);
Expand Down

0 comments on commit 93f17aa

Please sign in to comment.