Fix bug initializing auxiliary table for findBestfit() from multiple threads

This could happen if we generated thumbnails for GIF files from
multiple threads when showing the File > Open dialog for the first
time (calling findBestfit() from multiple threads).
This commit is contained in:
David Capello 2022-04-19 17:07:37 -03:00
parent 82a0ec5199
commit 03e0bc26c9
4 changed files with 12 additions and 6 deletions

View File

@ -1,5 +1,5 @@
// Aseprite Document Library
// Copyright (c) 2020 Igara Studio S.A.
// Copyright (c) 2020-2022 Igara Studio S.A.
// Copyright (c) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
@ -207,7 +207,7 @@ static uint32_t* col_diff_r;
static uint32_t* col_diff_b;
static uint32_t* col_diff_a;
static void initBestfit()
void Palette::initBestfit()
{
col_diff.resize(4*128, 0);
col_diff_g = &col_diff[128*0];
@ -230,9 +230,7 @@ int Palette::findBestfit(int r, int g, int b, int a, int mask_index) const
ASSERT(g >= 0 && g <= 255);
ASSERT(b >= 0 && b <= 255);
ASSERT(a >= 0 && a <= 255);
if (col_diff.empty())
initBestfit();
ASSERT(!col_diff.empty());
r >>= 3;
g >>= 3;

View File

@ -22,6 +22,8 @@ namespace doc {
class Palette : public Object {
public:
static void initBestfit();
Palette(frame_t frame, int ncolors);
Palette(const Palette& palette);
Palette(const Palette& palette, const Remap& remap);

View File

@ -1,4 +1,5 @@
// Aseprite Document Library
// Copyright (c) 2022 Igara Studo S.A.
// Copyright (c) 2001-2015 David Capello
//
// This file is released under the terms of the MIT license.
@ -191,6 +192,9 @@ TEST(Remap, BetweenPalettesDontChangeMaskForced)
TEST(Remap, BetweenPalettesNonInvertible)
{
// create_remap_to_change_palette() uses findBestfit()
doc::Palette::initBestfit();
Palette a(frame_t(0), 4);
Palette b(frame_t(0), 3);

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019-2021 Igara Studio S.A.
// Copyright (C) 2019-2022 Igara Studio S.A.
// Copyright (C) 2001-2016 David Capello
//
// This program is distributed under the terms of
@ -17,6 +17,7 @@
#include "base/exception.h"
#include "base/memory.h"
#include "base/system_console.h"
#include "doc/palette.h"
#include "os/error.h"
#include "os/system.h"
#include "ver/info.h"
@ -93,6 +94,7 @@ int app_main(int argc, char* argv[])
base::SystemConsole systemConsole;
app::AppOptions options(argc, const_cast<const char**>(argv));
os::SystemRef system(os::make_system());
doc::Palette::initBestfit();
app::App app;
#if ENABLE_SENTRY