Skip to content

Commit

Permalink
Manage images with unique_ptr
Browse files Browse the repository at this point in the history
ThreadPool does not really support unique_ptr, so spare it for now.
  • Loading branch information
tobiolo committed Oct 31, 2024
1 parent e685337 commit 542ec7f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 34 deletions.
19 changes: 9 additions & 10 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -2243,7 +2242,7 @@ struct Document {
}

void SetImageBM(Cell *c, std::vector<uint8_t> &&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) {
Expand Down
17 changes: 6 additions & 11 deletions src/myframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
17 changes: 5 additions & 12 deletions src/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct System {
Evaluator ev;
wxString clipboardcopy;
unique_ptr<Cell> cellclipboard;
std::vector<Image *> imagelist;
std::vector<unique_ptr<Image>> imagelist;
std::vector<int> loadimageids;
uchar versionlastloaded {0};
wxLongLong fakelasteditonload;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<Image>(hash, sc, std::move(idv), iti));
return imagelist.size() - 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 542ec7f

Please sign in to comment.