-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageBrowserAsyncThumbLoader.cpp
More file actions
163 lines (143 loc) · 4.96 KB
/
Copy pathImageBrowserAsyncThumbLoader.cpp
File metadata and controls
163 lines (143 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "ImageBrowserAsyncThumbLoader.h"
#include "CommonUtil.h"
#include <deque>
#include <mutex>
#include <thread>
#include <unordered_map>
namespace
{
bool PathEqualsInsensitive(const Floar::VirtualPath& a, const Floar::VirtualPath& b)
{
return CommonUtil::PathEqualsInsensitive(
a.hostPath, a.archiveInnerPath,
b.hostPath, b.archiveInnerPath);
}
std::mutex g_asyncThumbListMutex;
std::unordered_map<std::wstring, std::deque<AsyncThumbListChunkPayload>> g_asyncThumbListChunksByBrowser {};
std::unordered_map<std::wstring, HANDLE> g_asyncThumbReadyEventByBrowser {};
}
void ImageBrowserAsyncThumbLoader::RegisterBrowser(const std::wstring& browserName, HANDLE readyEvent)
{
std::lock_guard<std::mutex> lock(g_asyncThumbListMutex);
g_asyncThumbReadyEventByBrowser[browserName] = readyEvent;
}
void ImageBrowserAsyncThumbLoader::UnregisterBrowser(const std::wstring& browserName)
{
std::lock_guard<std::mutex> lock(g_asyncThumbListMutex);
g_asyncThumbListChunksByBrowser.erase(browserName);
g_asyncThumbReadyEventByBrowser.erase(browserName);
}
void ImageBrowserAsyncThumbLoader::EnqueueChunk(const std::wstring& browserName, AsyncThumbListChunkPayload&& payload)
{
HANDLE eventHandle = nullptr;
{
std::lock_guard<std::mutex> lock(g_asyncThumbListMutex);
auto& queue = g_asyncThumbListChunksByBrowser[browserName];
queue.push_back(std::move(payload));
auto eventIt = g_asyncThumbReadyEventByBrowser.find(browserName);
if (eventIt != g_asyncThumbReadyEventByBrowser.end())
{
eventHandle = eventIt->second;
}
}
if (eventHandle != nullptr)
{
SetEvent(eventHandle);
}
}
std::deque<AsyncThumbListChunkPayload> ImageBrowserAsyncThumbLoader::DequeueChunks(
const std::wstring& browserName,
HANDLE readyEvent)
{
std::deque<AsyncThumbListChunkPayload> chunks {};
{
std::lock_guard<std::mutex> lock(g_asyncThumbListMutex);
auto it = g_asyncThumbListChunksByBrowser.find(browserName);
if (it != g_asyncThumbListChunksByBrowser.end())
{
chunks = std::move(it->second);
g_asyncThumbListChunksByBrowser.erase(it);
}
}
if (readyEvent != nullptr && chunks.empty())
{
ResetEvent(readyEvent);
}
return chunks;
}
void ImageBrowserAsyncThumbLoader::StartEnumerate(
const Floar::VirtualPath& folder,
const std::function<void(std::vector<Floar::VirtualFileEntry>&&, bool completed)>& onChunk)
{
std::thread([folder, onChunk]()
{
if (!onChunk)
{
return;
}
if (!folder.IsInArchive() && !folder.IsArchiveFile())
{
// Fewer, larger chunks reduce progressive UI rebuild overhead on large folders.
constexpr size_t kBatchSize = 256;
std::vector<Floar::VirtualFileEntry> batch {};
batch.reserve(kBatchSize);
Floar::VirtualFileSystem::EnumerateFilesystemDirectory(folder.hostPath, [&](Floar::VirtualFileEntry&& entry)
{
batch.push_back(std::move(entry));
if (batch.size() >= kBatchSize)
{
onChunk(std::move(batch), false);
batch.clear();
batch.reserve(kBatchSize);
}
});
// Empty (enumeration failed/no entries) or the final partial batch either way.
onChunk(std::move(batch), true);
return;
}
// Archive listing is currently produced as a single snapshot.
onChunk(Floar::VirtualFileSystem::ListDirectory(folder), true);
}).detach();
}
bool ImageBrowserAsyncThumbLoader::AcceptChunk(
const AsyncThumbListChunkPayload& chunk,
unsigned long long currentRequestId,
const Floar::VirtualPath& currentFolder,
bool currentShowNavItems)
{
return chunk.requestId == currentRequestId &&
PathEqualsInsensitive(chunk.folder, currentFolder) &&
chunk.showNavItems == currentShowNavItems;
}
bool ImageBrowserAsyncThumbLoader::ApplyChunkToProgressive(
const AsyncThumbListChunkPayload& chunk,
std::vector<Floar::VirtualFileEntry>& progressiveListedEntries,
bool& progressiveUiDirty,
bool& progressiveLoadCompleted)
{
bool changed = false;
if (!chunk.batch.empty())
{
progressiveListedEntries.insert(
progressiveListedEntries.end(),
chunk.batch.begin(),
chunk.batch.end());
progressiveUiDirty = true;
changed = true;
}
if (chunk.completed)
{
progressiveLoadCompleted = true;
progressiveUiDirty = true;
changed = true;
}
return changed;
}
bool ImageBrowserAsyncThumbLoader::ShouldApplyNow(
bool progressiveLoadCompleted,
unsigned long long nowMs,
unsigned long long lastApplyMs,
unsigned long long applyIntervalMs)
{
return progressiveLoadCompleted || (nowMs - lastApplyMs >= applyIntervalMs);
}