Clean process of initialization of style images, do not use manager pointer(could be released)

This commit is contained in:
Filip Sykala 2022-03-23 08:58:46 +01:00
parent 501f6f021f
commit 05b6d3578d
4 changed files with 82 additions and 45 deletions

View file

@ -13,9 +13,14 @@ using namespace Slic3r;
using namespace Slic3r::GUI;
CreateFontStyleImagesJob::CreateFontStyleImagesJob(StyleImagesData &&input)
CreateFontStyleImagesJob::CreateFontStyleImagesJob(
FontManager::StyleImagesData &&input)
: m_input(std::move(input))
{}
{
assert(m_input.result != nullptr);
assert(!m_input.styles.empty());
assert(m_input.max_width > 1);
}
void CreateFontStyleImagesJob::process(Ctl &ctl)
{
@ -24,7 +29,7 @@ void CreateFontStyleImagesJob::process(Ctl &ctl)
std::vector<double> scales(m_input.styles.size());
images = std::vector<FontManager::StyleImage>(m_input.styles.size());
for (StyleImagesData::Item &item : m_input.styles) {
for (auto &item : m_input.styles) {
size_t index = &item - &m_input.styles.front();
ExPolygons &shapes = name_shapes[index];
shapes = Emboss::text2shapes(item.font, item.text.c_str(), item.prop);
@ -124,20 +129,12 @@ void CreateFontStyleImagesJob::finalize(bool canceled, std::exception_ptr &)
// set up texture id
void *texture_id = (void *) (intptr_t) tex_id;
for (FontManager::StyleImage &image : images) {
for (FontManager::StyleImage &image : images)
image.texture_id = texture_id;
size_t index = &image - &images.front();
StyleImagesData::Item &style = m_input.styles[index];
// find manager image and copy to it
for (auto& it: m_input.mng->m_font_list) {
if (it.font_item.name != style.text ||
!(it.font_item.prop == style.prop))
continue;
it.image = image;
break;
}
}
// move to result
m_input.result->styles = std::move(m_input.styles);
m_input.result->images = std::move(images);
// bind default texture
GLuint no_texture_id = 0;

View file

@ -9,29 +9,6 @@
namespace Slic3r::GUI {
/// <summary>
/// Data needed to create Font Style Images
/// </summary>
struct StyleImagesData
{
struct Item
{
Emboss::FontFileWithCache font;
std::string text;
FontProp prop;
};
using Items = std::vector<Item>;
// Keep styles to render
Items styles;
// maximal width in pixels of image
int max_width;
// is used in finalize to set result
// and I Can't proof of alive
FontManager *mng;
};
/// <summary>
/// Create texture with name of styles written by its style
@ -39,7 +16,7 @@ struct StyleImagesData
/// </summary>
class CreateFontStyleImagesJob : public Job
{
StyleImagesData m_input;
FontManager::StyleImagesData m_input;
// Output data
// texture size
@ -50,7 +27,7 @@ class CreateFontStyleImagesJob : public Job
std::vector<FontManager::StyleImage> images;
public:
CreateFontStyleImagesJob(StyleImagesData &&input);
CreateFontStyleImagesJob(FontManager::StyleImagesData &&input);
void process(Ctl &ctl) override;
void finalize(bool canceled, std::exception_ptr &) override;
};

View file

@ -15,6 +15,7 @@ FontManager::FontManager(const ImWchar *language_glyph_range)
: m_imgui_init_glyph_range(language_glyph_range)
, m_font_selected(std::numeric_limits<size_t>::max())
, m_exist_style_images(false)
, m_temp_style_images(nullptr)
{}
FontManager::~FontManager() {
@ -382,6 +383,36 @@ void FontManager::init_style_images(int max_width) {
// check already initialized
if (m_exist_style_images) return;
// check is initializing
if (m_temp_style_images != nullptr) {
// is initialization finished
if (!m_temp_style_images->styles.empty()) {
assert(m_temp_style_images->images.size() ==
m_temp_style_images->styles.size());
// copy images into styles
for (FontManager::StyleImage &image : m_temp_style_images->images){
size_t index = &image - &m_temp_style_images->images.front();
StyleImagesData::Item &style = m_temp_style_images->styles[index];
// find style in font list and copy to it
for (auto &it : m_font_list) {
if (it.font_item.name != style.text ||
!(it.font_item.prop == style.prop))
continue;
it.image = image;
break;
}
}
m_temp_style_images = nullptr;
m_exist_style_images = true;
return;
}
// in process of initialization inside of job
return;
}
// create job for init images
m_temp_style_images = std::make_shared<StyleImagesData::StyleImages>();
StyleImagesData::Items styles;
styles.reserve(m_font_list.size());
for (Item &item : m_font_list) {
@ -396,10 +427,8 @@ void FontManager::init_style_images(int max_width) {
}
auto &worker = wxGetApp().plater()->get_ui_job_worker();
StyleImagesData data{styles, max_width, this};
queue_job(worker, std::make_unique<CreateFontStyleImagesJob>(std::move(data)));
m_exist_style_images = true;
StyleImagesData data{std::move(styles), max_width, m_temp_style_images};
queue_job(worker, std::make_unique<CreateFontStyleImagesJob>(std::move(data)));
}
void FontManager::free_style_images() {

View file

@ -17,7 +17,7 @@ namespace Slic3r::GUI {
/// </summary>
class FontManager
{
friend class CreateFontStyleImagesJob;
friend class CreateFontStyleImagesJob; // access to StyleImagesData
public:
FontManager(const ImWchar *language_glyph_range);
@ -196,6 +196,40 @@ private:
std::vector<Item> m_font_list;
size_t m_font_selected; // index to m_font_list
/// <summary>
/// Keep data needed to create Font Style Images in Job
/// </summary>
struct StyleImagesData
{
struct Item
{
Emboss::FontFileWithCache font;
std::string text;
FontProp prop;
};
using Items = std::vector<Item>;
// Keep styles to render
Items styles;
// Maximal width in pixels of image
int max_width;
/// <summary>
/// Result of job
/// </summary>
struct StyleImages
{
// vector of inputs
StyleImagesData::Items styles;
// job output
std::vector<FontManager::StyleImage> images;
};
// place to store result in main thread in Finalize
std::shared_ptr<StyleImages> result;
};
std::shared_ptr<StyleImagesData::StyleImages> m_temp_style_images;
bool m_exist_style_images;
// store all font GLImages