From 542ec7f548bc40aefb6157ef0eb7d710952e8d1f Mon Sep 17 00:00:00 2001 From: Tobias Predel Date: Thu, 31 Oct 2024 13:11:57 +0100 Subject: [PATCH] Manage images with unique_ptr ThreadPool does not really support unique_ptr, so spare it for now. --- src/document.h | 19 +++++++++---------- src/myframe.h | 17 ++++++----------- src/system.h | 17 +++++------------ src/text.h | 2 +- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/src/document.h b/src/document.h index 9496fcac..df77ab41 100644 --- a/src/document.h +++ b/src/document.h @@ -175,15 +175,14 @@ struct Document { sos.Write8(ocs ? drawpath.size() : 0); // zoom level RefreshImageRefCount(true); int realindex = 0; - loopv(i, sys->imagelist) { - Image &image = *sys->imagelist[i]; - if (image.trefc) { - fos.PutC(image.image_type); - sos.WriteDouble(image.display_scale); - wxInt64 imagelen(image.image_data.size()); + for (const auto &uimg : sys->imagelist) { + if (uimg->trefc) { + fos.PutC(uimg->image_type); + sos.WriteDouble(uimg->display_scale); + wxInt64 imagelen(uimg->image_data.size()); sos.Write64(imagelen); - fos.Write(image.image_data.data(), imagelen); - image.savedindex = realindex++; + fos.Write(uimg->image_data.data(), imagelen); + uimg->savedindex = realindex++; } } @@ -805,7 +804,7 @@ struct Document { } void RefreshImageRefCount(bool includefolded) { - loopv(i, sys->imagelist) sys->imagelist[i]->trefc = 0; + for (const auto &uimg : sys->imagelist) uimg->trefc = 0; rootgrid->ImageRefCount(includefolded); } @@ -2243,7 +2242,7 @@ struct Document { } void SetImageBM(Cell *c, std::vector &&idv, double sc) { - c->text.image = sys->imagelist[sys->AddImageToList(sc, std::move(idv), 'I')]; + c->text.image = sys->imagelist[sys->AddImageToList(sc, std::move(idv), 'I')].get(); } bool LoadImageIntoCell(const wxString &fn, Cell *c, double sc) { diff --git a/src/myframe.h b/src/myframe.h index 7629983a..5189482a 100644 --- a/src/myframe.h +++ b/src/myframe.h @@ -1149,17 +1149,12 @@ struct MyFrame : wxFrame { { // block all other events until we finished preparing wxEventBlocker blocker(this); wxBusyCursor wait; - { - ThreadPool pool(std::thread::hardware_concurrency()); - loopv(i, sys->imagelist) { - pool.enqueue( - [](Image *img) { - img->bm_display = wxNullBitmap; - img->Display(); - }, - sys->imagelist[i]); - } - } // wait until all tasks are finished + + for (const auto &uimg : sys->imagelist) { + uimg->bm_display = wxNullBitmap; + uimg->Display(); + } + RenderFolderIcon(); if (nb) { loop(i, nb->GetPageCount()) { diff --git a/src/system.h b/src/system.h index 50667259..bea27be7 100644 --- a/src/system.h +++ b/src/system.h @@ -67,7 +67,7 @@ struct System { Evaluator ev; wxString clipboardcopy; unique_ptr cellclipboard; - std::vector imagelist; + std::vector> imagelist; std::vector loadimageids; uchar versionlastloaded {0}; wxLongLong fakelasteditonload; @@ -368,16 +368,9 @@ struct System { done: doc->RefreshImageRefCount(false); - { - ThreadPool pool(std::thread::hardware_concurrency()); - for (auto *image : sys->imagelist) { - pool.enqueue( - [](Image *img) { - if (img->trefc) img->Display(); - }, - image); - } - } // wait until all tasks are finished + for (const auto &uimg : sys->imagelist) { + if (uimg->trefc) uimg->Display(); + } FileUsed(filename, doc); doc->Refresh(); @@ -620,7 +613,7 @@ struct System { loopv(i, imagelist) { if (imagelist[i]->hash == hash) return i; } - imagelist.push_back(new Image(hash, sc, std::move(idv), iti)); + imagelist.push_back(make_unique(hash, sc, std::move(idv), iti)); return imagelist.size() - 1; } diff --git a/src/text.h b/src/text.h index 0be4e376..bd165a2f 100644 --- a/src/text.h +++ b/src/text.h @@ -472,7 +472,7 @@ struct Text { relsize = dis.Read32(); int i = dis.Read32(); - image = i >= 0 ? sys->imagelist[sys->loadimageids[i]] : nullptr; + image = i >= 0 ? sys->imagelist[sys->loadimageids[i]].get() : nullptr; if (sys->versionlastloaded >= 7) stylebits = dis.Read32();