summaryrefslogtreecommitdiff
path: root/src/gfx.cpp
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-06-17 10:38:04 +0200
committerPatric Stout <github@truebrain.nl>2021-06-26 20:28:05 +0200
commit0013673fafc74034df77ce0588ea6d568f120b45 (patch)
tree91029f6731966e2fb4136b7c5d2bb78f2cd5c110 /src/gfx.cpp
parent74186998a2ab7f45053aa771d179dd8aa24f0d86 (diff)
downloadopenttd-0013673fafc74034df77ce0588ea6d568f120b45.tar.xz
Change: prevent palette updates during copying to the video driver
ThreadSanitizer rightfully notices that the game-thread could update the palette while the draw-thread is copying it for local use. The odds of this are very small, but nevertheless, it does carry a very good point. It wouldn't hurt the application in any way, but it might cause visual glitches on the screen.
Diffstat (limited to 'src/gfx.cpp')
-rw-r--r--src/gfx.cpp7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/gfx.cpp b/src/gfx.cpp
index 46870dc42..9ccb036d2 100644
--- a/src/gfx.cpp
+++ b/src/gfx.cpp
@@ -51,6 +51,8 @@ static byte _stringwidth_table[FS_END][224]; ///< Cache containing width of ofte
DrawPixelInfo *_cur_dpi;
byte _colour_gradient[COLOUR_END][8];
+static std::recursive_mutex _palette_mutex; ///< To coordinate access to _cur_palette.
+
static void GfxMainBlitterViewport(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = nullptr, SpriteID sprite_id = SPR_CURSOR_MOUSE);
static void GfxMainBlitter(const Sprite *sprite, int x, int y, BlitterMode mode, const SubSprite *sub = nullptr, SpriteID sprite_id = SPR_CURSOR_MOUSE, ZoomLevel zoom = ZOOM_LVL_NORMAL);
@@ -1197,6 +1199,7 @@ void DoPaletteAnimations();
void GfxInitPalettes()
{
+ std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
DoPaletteAnimations();
}
@@ -1212,6 +1215,8 @@ void GfxInitPalettes()
*/
bool CopyPalette(Palette &local_palette, bool force_copy)
{
+ std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
+
if (!force_copy && _cur_palette.count_dirty == 0) return false;
local_palette = _cur_palette;
@@ -1230,6 +1235,8 @@ bool CopyPalette(Palette &local_palette, bool force_copy)
void DoPaletteAnimations()
{
+ std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
+
/* Animation counter for the palette animation. */
static int palette_animation_counter = 0;
palette_animation_counter += 8;