Changeset - r25725:e1fbdd2a5f81
[Not reviewed]
master
0 1 0
Patric Stout - 3 years ago 2021-06-17 08:38:04
truebrain@openttd.org
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.
1 file changed with 7 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/gfx.cpp
Show inline comments
 
@@ -48,12 +48,14 @@ PauseMode _pause_mode;
 
Palette _cur_palette;
 

	
 
static byte _stringwidth_table[FS_END][224]; ///< Cache containing width of often used characters. @see GetCharacterWidth()
 
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);
 

	
 
static ReusableBuffer<uint8> _cursor_backup;
 

	
 
ZoomLevel _gui_zoom; ///< GUI Zoom level
 
@@ -1194,12 +1196,13 @@ static void GfxMainBlitter(const Sprite 
 
}
 

	
 
void DoPaletteAnimations();
 

	
 
void GfxInitPalettes()
 
{
 
	std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
 
	memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
 
	DoPaletteAnimations();
 
}
 

	
 
/**
 
 * Copy the current palette if the palette was updated.
 
@@ -1209,12 +1212,14 @@ void GfxInitPalettes()
 
 * @param local_palette The location to copy the palette to.
 
 * @param force_copy Whether to ignore if there is an update for the palette.
 
 * @return True iff a copy was done.
 
 */
 
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;
 
	_cur_palette.count_dirty = 0;
 

	
 
	if (force_copy) {
 
@@ -1227,12 +1232,14 @@ bool CopyPalette(Palette &local_palette,
 

	
 
#define EXTR(p, q) (((uint16)(palette_animation_counter * (p)) * (q)) >> 16)
 
#define EXTR2(p, q) (((uint16)(~palette_animation_counter * (p)) * (q)) >> 16)
 

	
 
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;
 

	
 
	Blitter *blitter = BlitterFactory::GetCurrentBlitter();
 
	const Colour *s;
0 comments (0 inline, 0 general)