Skip to content

Commit

Permalink
Fix flickering
Browse files Browse the repository at this point in the history
The fix should lower GPU pressure too.
  • Loading branch information
alexzk1 committed Jul 21, 2024
1 parent 44016f8 commit ed271ab
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
36 changes: 36 additions & 0 deletions cpp/drawables.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <optional>
#include <map>
#include <tuple>

#include "json.hpp"

Expand Down Expand Up @@ -52,6 +53,8 @@ namespace draw_task
std::string command;

drawmode_t drawmode{drawmode_t::idk};
//Anti-flickering field,
bool already_rendered{false};

// common
int x{0};
Expand All @@ -64,6 +67,15 @@ namespace draw_task
std::string text;
std::string size;
std::optional<int> fontSize{std::nullopt};
bool operator==(const drawtext_t& other) const
{
static const auto tie = [](const drawtext_t& val)
{
return std::tie(val.text, val.size, val.fontSize);
};

return tie(*this) == tie(other);
}
} text;

struct drawshape_t
Expand All @@ -74,8 +86,27 @@ namespace draw_task
int w{0};
int h{0};
json vect;

bool operator==(const drawshape_t& other) const
{
static const auto tie = [](const drawshape_t& val)
{
return std::tie(val.shape, val.fill, val.w, val.h, val.vect);
};

return tie(*this) == tie(other);
}
} shape;

bool IsEqualStoredData(const drawitem_t& other) const
{
static const auto tie = [](const drawitem_t& item)
{
return std::tie(item.id, item.drawmode, item.color, item.text, item.shape);
};
return tie(*this) == tie(other);
}

bool isExpired() const
{
return ttl.isExpired();
Expand All @@ -85,6 +116,11 @@ namespace draw_task
{
return !command.empty();
}

void SetAlreadyRendered()
{
already_rendered = true;
}
};

using draw_items_t = std::map<std::string, drawitem_t>;
Expand Down
53 changes: 47 additions & 6 deletions cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#include <csignal>
#include <mutex>
#include <map>
#include <memory>
#include <thread>
#include <functional>
#include <string>
#include <stdexcept>

#include "socket.hh"
#include "json_message.hh"
Expand Down Expand Up @@ -125,7 +130,24 @@ int main(int argc, char* argv[])
if ((!(*should_close_ptr)))
{
incoming_draws.merge(allDraws);
allDraws.clear();

//Anti-flickering support.
if (!allDraws.empty())
{
for (const auto& old : allDraws)
{
const auto it = incoming_draws.find(old.first);
if (it != incoming_draws.end())
{
if (it->second.IsEqualStoredData(old.second))
{
it->second.SetAlreadyRendered();
}
}
}
allDraws.clear();
}

std::swap(allDraws, incoming_draws);
}
}
Expand Down Expand Up @@ -173,6 +195,7 @@ int main(int argc, char* argv[])
},
};

bool window_was_hidden = false;
while (serverThread)
{
std::this_thread::sleep_for(100ms);
Expand All @@ -182,10 +205,9 @@ int main(int argc, char* argv[])
targetAppActive = programName.empty()
|| utility::strcontains(drawer.getFocusedWindowBinaryPath(), programName);
}

drawer.cleanFrame();
{
std::lock_guard grd(*mut);
bool skip_render = true;
for(auto iter = allDraws.begin(); iter != allDraws.end(); )
{
const bool isCommand = iter->second.isCommand();
Expand All @@ -197,13 +219,17 @@ int main(int argc, char* argv[])
{
if (cmd_iter->second())
{
skip_render = false;
break;
}
}
}

skip_render = skip_render && iter->second.already_rendered;

if (isCommand || iter->second.isExpired())
{
skip_render = false;
iter = allDraws.erase(iter);
}
else
Expand All @@ -214,13 +240,28 @@ int main(int argc, char* argv[])

if (targetAppActive && !commandHideLayer)
{
for (const auto& drawitem : allDraws)
if (!skip_render || window_was_hidden)
{
drawer.cleanFrame();
for (auto& drawitem : allDraws)
{
drawer.draw(drawitem.second);
drawitem.second.SetAlreadyRendered();
}
drawer.flushFrame();
}
window_was_hidden = false;
}
else
{
if (!window_was_hidden)
{
drawer.draw(drawitem.second);
drawer.cleanFrame();
drawer.flushFrame();
}
window_was_hidden = true;
}
}
drawer.flushFrame();
}
}
catch(...)
Expand Down

0 comments on commit ed271ab

Please sign in to comment.