/
loader.cpp
683 lines (619 loc) · 32.7 KB
/
loader.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
* This file is a part of KNOSSOS.
*
* (C) Copyright 2007-2018
* Max-Planck-Gesellschaft zur Foerderung der Wissenschaften e.V.
*
* KNOSSOS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 of
* the License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* For further information, visit https://knossostool.org
* or contact knossos-team@mpimf-heidelberg.mpg.de
*/
#include "loader.h"
#include "network.h"
#include "segmentation/segmentation.h"
#include "session.h"
#include "skeleton/skeletonizer.h"
#include "stateInfo.h"
#include "viewer.h"
#include "widgets/mainwindow.h"
#include <quazip.h>
#include <quazipfile.h>
#include <snappy.h>
#include <QFile>
#include <QFuture>
#include <QImage>
#include <QMutexLocker>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QtConcurrent>
#include <cmath>
#include <fstream>
#include <stdexcept>
//generalizing this needs polymorphic lambdas or return type deduction
auto currentlyVisibleWrap = [](const Coordinate & center, const Dataset & dataset){
return [¢er, &dataset](const Coordinate & coord){
return currentlyVisible(coord, center, state->M, dataset.cubeEdgeLength * dataset.magnification);
};
};
auto insideCurrentSupercubeWrap = [](const Coordinate & center, const Dataset & dataset){
return [center, dataset](const CoordOfCube & coord){
return insideCurrentSupercube(coord.cube2Global(dataset.cubeEdgeLength, dataset.magnification), center, state->M, dataset.cubeEdgeLength * dataset.magnification);
};
};
bool currentlyVisibleWrapWrap(const Coordinate & center, const Coordinate & coord) {// only for use from main thread
return currentlyVisibleWrap(center, Dataset::current())(coord);
}
void Loader::Controller::suspendLoader() {
++loadingNr;
workerThread.quit();
workerThread.wait();
if (worker != nullptr) {
worker->abortDownloadsFinishDecompression();
}
}
Loader::Controller::~Controller() {
suspendLoader();
}
void Loader::Controller::unloadCurrentMagnification() {
++loadingNr;
emit unloadCurrentMagnificationSignal();
}
void Loader::Controller::markOcCubeAsModified(const CoordOfCube &cubeCoord, const int magnification) {
emit markOcCubeAsModifiedSignal(cubeCoord, magnification);
state->viewer->window->notifyUnsavedChanges();
state->viewer->reslice_notify_all(worker.get()->snappyLayerId, cubeCoord.cube2Global(Dataset::current().cubeEdgeLength, magnification));
}
decltype(Loader::Worker::snappyCache) Loader::Controller::getAllModifiedCubes() {
if (worker != nullptr) {
QMutexLocker locker(&worker->snappyMutex);
//signal to run in loader thread
QTimer::singleShot(0, worker.get(), &Loader::Worker::flushIntoSnappyCache);
worker->snappyFlushCondition.wait(&worker->snappyMutex);
return worker->snappyCache;
} else {
return decltype(Loader::Worker::snappyCache)();//{} is not working
}
}
bool Loader::Controller::isFinished() {
return worker != nullptr ? worker->isFinished.load() : true;//no loader == done?
}
void Loader::Worker::CalcLoadOrderMetric(float halfSc, floatCoordinate currentMetricPos, const UserMoveType userMoveType, const floatCoordinate & direction, float *metrics) {
const auto INNER_MULT_VECTOR = [](const floatCoordinate v) {
return v.x * v.y * v.z;
};
const auto CALC_VECTOR_NORM = [](const floatCoordinate v) {
return std::sqrt(std::pow(v.x, 2) + std::pow(v.y, 2) + std::pow(v.z, 2));
};
const auto CALC_DOT_PRODUCT = [](const floatCoordinate a, const floatCoordinate b){
return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
};
const auto CALC_POINT_DISTANCE_FROM_PLANE = [CALC_VECTOR_NORM, CALC_DOT_PRODUCT](const floatCoordinate point, const floatCoordinate plane){
return std::abs(CALC_DOT_PRODUCT(point, plane)) / CALC_VECTOR_NORM(plane);
};
float distance_from_plane, distance_from_origin, dot_product;
int i = 0;
distance_from_origin = CALC_VECTOR_NORM(currentMetricPos);
switch (userMoveType) {
case USERMOVE_HORIZONTAL:
case USERMOVE_DRILL:
distance_from_plane = CALC_POINT_DISTANCE_FROM_PLANE(currentMetricPos, direction);
dot_product = CALC_DOT_PRODUCT(currentMetricPos, direction);
if (USERMOVE_HORIZONTAL == userMoveType) {
metrics[i++] = (0 == distance_from_plane ? -1.0 : 1.0);
metrics[i++] = (0 == INNER_MULT_VECTOR(currentMetricPos) ? -1.0 : 1.0);
}
else {
metrics[i++] = (( (distance_from_plane <= 1) || (distance_from_origin <= halfSc) ) ? -1.0 : 1.0);
metrics[i++] = (distance_from_plane > 1 ? 1.0 : -1.0);
metrics[i++] = (dot_product < 0 ? 1.0 : -1.0);
metrics[i++] = distance_from_plane;
}
break;
case USERMOVE_NEUTRAL:
// Priorities are XY->ZY->XZ
metrics[i++] = (0 == currentMetricPos.z ? -1.0 : 1.0);
metrics[i++] = (0 == currentMetricPos.x ? -1.0 : 1.0);
metrics[i++] = (0 == currentMetricPos.y ? -1.0 : 1.0);
break;
default:
break;
}
metrics[i++] = distance_from_origin;
currentMaxMetric = std::max(this->currentMaxMetric, i);
}
struct LO_Element {
CoordOfCube coordinate;
Coordinate offset;
float loadOrderMetrics[LL_METRIC_NUM];
};
std::vector<CoordOfCube> Loader::Worker::DcoiFromPos(const CoordOfCube & currentOrigin, const UserMoveType userMoveType, const floatCoordinate & direction) {
const float floatHalfSc = state->M / 2.;
const int halfSc = std::floor(floatHalfSc);
const int cubeElemCount = state->cubeSetElements;
int i = 0;
currentMaxMetric = 0;
std::vector<LO_Element> DcArray(cubeElemCount);
for (int x = -halfSc; x < halfSc + 1; ++x) {
for (int y = -halfSc; y < halfSc + 1; ++y) {
for (int z = -halfSc; z < halfSc + 1; ++z) {
DcArray[i].coordinate = {currentOrigin.x + x, currentOrigin.y + y, currentOrigin.z + z};
DcArray[i].offset = {x, y, z};
floatCoordinate currentMetricPos(x, y, z);
CalcLoadOrderMetric(floatHalfSc, currentMetricPos, userMoveType, direction, &DcArray[i].loadOrderMetrics[0]);
++i;
}
}
}
std::sort(std::begin(DcArray), std::begin(DcArray) + cubeElemCount, [&](const LO_Element & elem_a, const LO_Element & elem_b){
for (int metric_index = 0; metric_index < currentMaxMetric; ++metric_index) {
float m_a = elem_a.loadOrderMetrics[metric_index];
float m_b = elem_b.loadOrderMetrics[metric_index];
if (m_a != m_b) {
return (m_a - m_b) < 0;
}
//If equal just continue to next comparison level
}
return false;
});
std::vector<CoordOfCube> cubes;
for (int i = 0; i < cubeElemCount; ++i) {
cubes.emplace_back(DcArray[i].coordinate);
}
return cubes;
}
Loader::Worker::Worker(const decltype(datasets) & layers)
: slotDownload(static_cast<std::size_t>(layers.size())), slotDecompression(static_cast<std::size_t>(layers.size()))
, slotChunk(static_cast<std::size_t>(layers.size())), freeSlots(static_cast<std::size_t>(layers.size()))
, datasets{layers}, snappyLayerId{Segmentation::singleton().layerId}
, OcModifiedCacheQueue(static_cast<std::size_t>(std::log2(layers.front().highestAvailableMag)+1))
, snappyCache(static_cast<std::size_t>(std::log2(layers.front().highestAvailableMag)+1))
{
qnam.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);// default is manual redirect
state->cube2Pointer.clear();
state->cube2Pointer.shrink_to_fit();
// freeSlots[] are lists of pointers to locations that
// can hold data or overlay cubes. Whenever we want to load a new
// datacube, we load it into a location from this list. Whenever a
// datacube in memory becomes invalid, we add the pointer to its
// memory location back into this list.
for (std::size_t layerId{0}; layerId < layers.size(); ++layerId) {
state->cube2Pointer.emplace_back(std::log2(layers[layerId].highestAvailableMag)+1);
if (!datasets[layerId].allocationEnabled) {
continue;
}
const auto overlayFactor = layers[layerId].isOverlay() ? OBJID_BYTES : 1;
qDebug() << "Allocating" << state->cubeSetBytes * overlayFactor / 1024. / 1024. << "MiB for cubes.";
for (size_t i = 0; i < state->cubeSetBytes * overlayFactor; i += state->cubeBytes * overlayFactor) {
slotChunk[layerId].emplace_back(state->cubeBytes * overlayFactor, 0);// zero init chunk of chars
freeSlots[layerId].emplace_back(slotChunk[layerId].back().data());// append newest element
}
}
}
Loader::Worker::~Worker() {
abortDownloadsFinishDecompression();
if (state->quitSignal) {
return;//state is dead already
}
QMutexLocker locker(&state->protectCube2Pointer);
for (auto & layer : state->cube2Pointer) {
for (auto & elem : layer) {
elem.clear();
}
}
}
template<typename CubeHash, typename Slots, typename Keep>
void unloadCubes(CubeHash & loadedCubes, Slots & freeSlots, Keep keep) {
unloadCubes(loadedCubes, freeSlots, keep, [](const CoordOfCube &, void *){});
}
template<typename CubeHash, typename Slots, typename Keep, typename UnloadHook>
void unloadCubes(CubeHash & loadedCubes, Slots & freeSlots, Keep keep, UnloadHook todo) {
for (auto it = std::begin(loadedCubes); it != std::end(loadedCubes);) {
if (!keep(it->first)) {
todo(CoordOfCube(it->first.x, it->first.y, it->first.z), it->second);
freeSlots.emplace_back(it->second);
it = loadedCubes.erase(it);
} else {
++it;
}
}
}
void Loader::Worker::unloadCurrentMagnification() {
for (std::size_t layerId{0}; layerId < datasets.size(); ++layerId) {
abortDownloadsFinishDecompression(layerId, [](const Coordinate &){return false;});
QMutexLocker locker(&state->protectCube2Pointer);
if (loaderMagnification >= state->cube2Pointer[layerId].size()) {
continue;
}
for (auto &elem : state->cube2Pointer[layerId][loaderMagnification]) {
const auto cubeCoord = elem.first;
const auto remSlotPtr = elem.second;
if (layerId == snappyLayerId) {
if (OcModifiedCacheQueue[loaderMagnification].find(cubeCoord) != std::end(OcModifiedCacheQueue[loaderMagnification])) {
snappyCacheBackupRaw(cubeCoord, remSlotPtr);
//remove from work queue
OcModifiedCacheQueue[loaderMagnification].erase(cubeCoord);
}
}
freeSlots[layerId].emplace_back(remSlotPtr);
}
state->cube2Pointer[layerId][loaderMagnification].clear();
}
}
void Loader::Worker::markOcCubeAsModified(const CoordOfCube &cubeCoord, const int magnification) {
OcModifiedCacheQueue[static_cast<std::size_t>(std::log2(magnification))].emplace(cubeCoord);
}
void Loader::Worker::snappyCacheSupplySnappy(const CoordOfCube cubeCoord, const int magnification, const std::string cube) {
const auto cubeMagnification = static_cast<std::size_t>(std::log2(magnification));
if (cubeMagnification >= snappyCache.size()) {
qWarning() << QObject::tr("ignored snappy cube (%1, %2, %3) for higher than available mag %4 ((log2(%4) = %5) ≥ %6)")
.arg(cubeCoord.x).arg(cubeCoord.y).arg(cubeCoord.z).arg(magnification).arg(cubeMagnification).arg(snappyCache.size());
return;
}
snappyCache[cubeMagnification].emplace(std::piecewise_construct, std::forward_as_tuple(cubeCoord), std::forward_as_tuple(cube));
if (cubeMagnification == loaderMagnification) {//unload if currently loaded
const auto globalCoord = cubeCoord.cube2Global(datasets.front().cubeEdgeLength, magnification);
auto downloadIt = slotDownload[snappyLayerId].find(globalCoord);
if (downloadIt != std::end(slotDownload[snappyLayerId])) {
downloadIt->second->abort();
}
auto decompressionIt = slotDecompression[snappyLayerId].find(globalCoord);
if (decompressionIt != std::end(slotDecompression[snappyLayerId])) {
decompressionIt->second->waitForFinished();
}
QMutexLocker locker(&state->protectCube2Pointer);
const auto coord = cubeCoord;
auto cubePtr = Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, snappyLayerId, loaderMagnification, coord);
if (cubePtr != nullptr) {
freeSlots[snappyLayerId].emplace_back(cubePtr);
state->cube2Pointer[snappyLayerId][loaderMagnification].erase(coord);
}
}
}
void Loader::Worker::snappyCacheBackupRaw(const CoordOfCube & cubeCoord, const void * cube) {
//insert empty string into snappy cache
auto snappyIt = snappyCache[loaderMagnification].emplace(std::piecewise_construct, std::forward_as_tuple(cubeCoord), std::forward_as_tuple()).first;
//compress cube into the new string
snappy::Compress(reinterpret_cast<const char *>(cube), OBJID_BYTES * state->cubeBytes, &snappyIt->second);
}
void Loader::Worker::snappyCacheClear() {
if (snappyLayerId >= state->cube2Pointer.size()) {
return;
}
//unload all modified cubes
for (std::size_t mag = 0; mag < OcModifiedCacheQueue.size(); ++mag) {
unloadCubes(state->cube2Pointer[snappyLayerId][mag], freeSlots[snappyLayerId], [this, mag](const CoordOfCube & cubeCoord){
const bool unflushed = OcModifiedCacheQueue[mag].find(cubeCoord) != std::end(OcModifiedCacheQueue[mag]);
const bool flushed = snappyCache[mag].find(cubeCoord) != std::end(snappyCache[mag]);
return !unflushed && !flushed;//only keep cubes which are neither in snappy cache nor in modified queue
});
OcModifiedCacheQueue[mag].clear();
snappyCache[mag].clear();
}
state->viewer->loader_notify();//a bit of a detour…
}
void Loader::Worker::flushIntoSnappyCache() {
QMutexLocker locker(&snappyMutex);
for (std::size_t mag = 0; mag < OcModifiedCacheQueue.size(); ++mag) {
for (const auto & cubeCoord : OcModifiedCacheQueue[mag]) {
state->protectCube2Pointer.lock();
auto cube = Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, snappyLayerId, mag, {cubeCoord.x, cubeCoord.y, cubeCoord.z});
state->protectCube2Pointer.unlock();
if (cube != nullptr) {
snappyCacheBackupRaw(cubeCoord, cube);
}
}
//clear work queue
OcModifiedCacheQueue[mag].clear();
}
snappyFlushCondition.wakeAll();
}
void Loader::Worker::moveToThread(QThread *targetThread) {
qnam.moveToThread(targetThread);
QObject::moveToThread(targetThread);
}
template<typename Downloads, typename Func>
void abortDownloads(Downloads & downloads, Func keep) {
std::vector<Coordinate> abortQueue;
for (auto && elem : downloads) {
if (!keep(elem.first)) {
abortQueue.emplace_back(elem.first);
}
}
for (auto && elem : abortQueue) {
downloads[elem]->abort();//abort running downloads
}
}
template<typename Decomp, typename Func>
void finishDecompression(Decomp & decompressions, Func keep) {
for (auto && elem : decompressions) {
if (!keep(elem.first)) {
//elem.second->cancel();
elem.second->waitForFinished();
}
}
}
void Loader::Worker::abortDownloadsFinishDecompression() {
for (std::size_t layerId{0}; layerId < datasets.size(); ++layerId) {
abortDownloadsFinishDecompression(layerId, [](const Coordinate &){return false;});
}
}
template<typename Func>
void Loader::Worker::abortDownloadsFinishDecompression(std::size_t layerId, Func keep) {
abortDownloads(slotDownload[layerId], keep);
finishDecompression(slotDecompression[layerId], keep);
}
std::pair<bool, void*> decompressCube(void * currentSlot, QIODevice & reply, const std::size_t layerId, const Dataset dataset, decltype(state->cube2Pointer)::value_type::value_type & cubeHash, const Coordinate globalCoord) {
if (!reply.isOpen()) {// sanity check, finished replies with no error should be ready for reading (https://bugreports.qt.io/browse/QTBUG-45944)
return {false, currentSlot};
}
QThread::currentThread()->setPriority(QThread::IdlePriority);
bool success = false;
auto data = reply.read(reply.bytesAvailable());//readAll can be very slow – https://bugreports.qt.io/browse/QTBUG-45926
const std::size_t availableSize = data.size();
if (dataset.type == Dataset::CubeType::RAW_UNCOMPRESSED) {
const std::size_t expectedSize = state->cubeBytes;
if (availableSize == expectedSize) {
std::copy(std::begin(data), std::end(data), reinterpret_cast<std::uint8_t *>(currentSlot));
success = true;
}
} else if (dataset.type == Dataset::CubeType::RAW_JPG || dataset.type == Dataset::CubeType::RAW_J2K || dataset.type == Dataset::CubeType::RAW_JP2_6 || dataset.type == Dataset::CubeType::RAW_PNG) {
const auto image = QImage::fromData(data).convertToFormat(QImage::Format_Indexed8);
const qint64 expectedSize = state->cubeBytes;
if (image.byteCount() == expectedSize) {
std::copy(image.bits(), image.bits() + image.byteCount(), reinterpret_cast<std::uint8_t *>(currentSlot));
success = true;
}
} else if (dataset.type == Dataset::CubeType::SEGMENTATION_UNCOMPRESSED_16) {
const std::size_t expectedSize = state->cubeBytes * OBJID_BYTES / 4;
if (availableSize == expectedSize) {
boost::multi_array_ref<uint16_t, 1> dataRef(reinterpret_cast<uint16_t *>(data.data()), boost::extents[std::pow(dataset.cubeEdgeLength, 3)]);
boost::multi_array_ref<uint64_t, 1> slotRef(reinterpret_cast<uint64_t *>(currentSlot), boost::extents[std::pow(dataset.cubeEdgeLength, 3)]);
std::copy(std::begin(dataRef), std::end(dataRef), std::begin(slotRef));
success = true;
}
} else if (dataset.type == Dataset::CubeType::SEGMENTATION_UNCOMPRESSED_64) {
const std::size_t expectedSize = state->cubeBytes * OBJID_BYTES;
if (availableSize == expectedSize) {
std::copy(std::begin(data), std::end(data), reinterpret_cast<std::uint64_t *>(currentSlot));
success = true;
}
} else if (dataset.type == Dataset::CubeType::SEGMENTATION_SZ_ZIP) {
QBuffer buffer(&data);
QuaZip archive(&buffer);//QuaZip needs a random access QIODevice
if (archive.open(QuaZip::mdUnzip)) {
archive.goToFirstFile();
QuaZipFile file(&archive);
if (file.open(QIODevice::ReadOnly)) {
auto data = file.readAll();
std::size_t uncompressedSize;
snappy::GetUncompressedLength(data.data(), data.size(), &uncompressedSize);
const std::size_t expectedSize = state->cubeBytes * OBJID_BYTES;
if (uncompressedSize == expectedSize) {
success = snappy::RawUncompress(data.data(), data.size(), reinterpret_cast<char*>(currentSlot));
}
}
archive.close();
}
} else {
qDebug() << "unsupported format";
}
if (success) {
state->protectCube2Pointer.lock();
cubeHash[globalCoord.cube(dataset.cubeEdgeLength, dataset.magnification)] = currentSlot;
state->protectCube2Pointer.unlock();
state->viewer->reslice_notify_all(layerId, globalCoord);
}
return {success, currentSlot};
}
void Loader::Worker::cleanup(const Coordinate center) {
for (std::size_t layerId{0}; layerId < datasets.size(); ++layerId) {
abortDownloadsFinishDecompression(layerId, currentlyVisibleWrap(center, datasets[layerId]));
if (loaderMagnification >= state->cube2Pointer[layerId].size()) {
continue;
}
QMutexLocker locker(&state->protectCube2Pointer);
unloadCubes(state->cube2Pointer[layerId][loaderMagnification], freeSlots[layerId], insideCurrentSupercubeWrap(center, datasets[layerId])
, [this, layerId](const CoordOfCube & cubeCoord, void * remSlotPtr){
if (datasets[layerId].isOverlay()) {// TODO is it the snappy layer?
if (OcModifiedCacheQueue[loaderMagnification].find(cubeCoord) != std::end(OcModifiedCacheQueue[loaderMagnification])) {
snappyCacheBackupRaw(cubeCoord, remSlotPtr);
//remove from work queue
OcModifiedCacheQueue[loaderMagnification].erase(cubeCoord);
}
}
});
}
}
void Loader::Controller::startLoading(const Coordinate & center, const UserMoveType userMoveType, const floatCoordinate & direction) {
if (worker != nullptr) {
worker->isFinished = false;
emit loadSignal(++loadingNr, center, userMoveType, direction, Dataset::datasets);
}
}
void Loader::Worker::broadcastProgress(bool startup) {
std::size_t count{0};
for (std::size_t layerId{0}; layerId < datasets.size(); ++layerId) {
count += slotDownload[layerId].size() + slotDecompression[layerId].size();
}
isFinished = count == 0;
emit progress(startup, count);
}
void Loader::Worker::downloadAndLoadCubes(const unsigned int loadingNr, const Coordinate center, const UserMoveType userMoveType, const floatCoordinate & direction, const Dataset::list_t & changedDatasets) {
datasets = changedDatasets;
cleanup(center);
decltype(Dataset::magnification) magnification = datasets.front().magnification;
loaderMagnification = static_cast<std::size_t>(std::log2(magnification));
const auto cubeEdgeLen = datasets.front().cubeEdgeLength;
const auto Dcoi = DcoiFromPos(center.cube(cubeEdgeLen, magnification), userMoveType, direction);//datacubes of interest prioritized around the current position
//split dcoi into slice planes and rest
std::vector<std::pair<std::size_t, Coordinate>> allCubes;
for (auto && todo : Dcoi) {
const Coordinate globalCoord = todo.cube2Global(cubeEdgeLen, magnification);
QMutexLocker locker(&state->protectCube2Pointer);
for (std::size_t layerId{0}; layerId < datasets.size(); ++layerId) {
// only queue downloads which are necessary
if (Coordinate2BytePtr_hash_get_or_fail(state->cube2Pointer, layerId, loaderMagnification, globalCoord.cube(cubeEdgeLen, magnification)) == nullptr) {
allCubes.emplace_back(layerId, globalCoord);
}
}
}
auto startDownload = [this, center](const std::size_t layerId, const Dataset dataset, const Coordinate globalCoord, decltype(slotDownload)::value_type & downloads
, decltype(slotDecompression)::value_type & decompressions, decltype(freeSlots)::value_type & freeSlots, decltype(state->cube2Pointer)::value_type::value_type & cubeHash){
if (dataset.isOverlay()) {
auto snappyIt = snappyCache[loaderMagnification].find(globalCoord.cube(dataset.cubeEdgeLength, dataset.magnification));
if (snappyIt != std::end(snappyCache[loaderMagnification])) {
if (!freeSlots.empty()) {
auto downloadIt = downloads.find(globalCoord);
if (downloadIt != std::end(downloads)) {
downloadIt->second->abort();
}
auto decompressionIt = decompressions.find(globalCoord);
if (decompressionIt != std::end(decompressions)) {
decompressionIt->second->waitForFinished();
}
const auto cubeCoord = globalCoord.cube(dataset.cubeEdgeLength, dataset.magnification);
state->protectCube2Pointer.lock();
const auto currentSlotIt = cubeHash.find(cubeCoord);
auto * currentSlot = currentSlotIt != std::end(cubeHash) ? currentSlotIt->second : freeSlots.front();
cubeHash.erase(cubeCoord);
state->protectCube2Pointer.unlock();
if (currentSlot == freeSlots.front()) {
freeSlots.pop_front();
}
//directly uncompress snappy cube into the OC slot
const auto success = snappy::RawUncompress(snappyIt->second.c_str(), snappyIt->second.size(), reinterpret_cast<char*>(currentSlot));
if (success) {
state->protectCube2Pointer.lock();
cubeHash[globalCoord.cube(dataset.cubeEdgeLength, dataset.magnification)] = currentSlot;
state->protectCube2Pointer.unlock();
state->viewer->reslice_notify_all(layerId, globalCoord);
} else {
freeSlots.emplace_back(currentSlot);
qCritical() << layerId << globalCoord << "snappy extract failed" << snappyIt->second.size();
}
} else {
qCritical() << layerId << globalCoord << "no slots for snappy extract" << cubeHash.size() << freeSlots.size();
}
return;
}
}
QUrl dcUrl = dataset.apiSwitch(globalCoord);
state->protectCube2Pointer.lock();
const bool cubeNotAlreadyLoaded = cubeHash.count(globalCoord.cube(dataset.cubeEdgeLength, dataset.magnification)) == 0;
state->protectCube2Pointer.unlock();
const bool cubeNotDownloading = downloads.find(globalCoord) == std::end(downloads);
const bool cubeNotDecompressing = decompressions.find(globalCoord) == std::end(decompressions);
if (cubeNotAlreadyLoaded && cubeNotDownloading && cubeNotDecompressing) {
if (dataset.type == Dataset::CubeType::SNAPPY) {
if (!freeSlots.empty()) {
auto * currentSlot = freeSlots.front();
freeSlots.pop_front();
std::fill(reinterpret_cast<std::uint8_t *>(currentSlot), reinterpret_cast<std::uint8_t *>(currentSlot) + state->cubeBytes * (dataset.isOverlay() ? OBJID_BYTES : 1), 0);
state->protectCube2Pointer.lock();
cubeHash[globalCoord.cube(dataset.cubeEdgeLength, dataset.magnification)] = currentSlot;
state->protectCube2Pointer.unlock();
state->viewer->reslice_notify_all(layerId, globalCoord);
} else {
qCritical() << layerId << globalCoord << "no slots for snappy extract" << cubeHash.size() << freeSlots.size();
}
return;
}
//transform googles oauth2 token from query item to request header
QUrlQuery originalQuery(dcUrl);
auto reducedQuery = originalQuery;
reducedQuery.removeQueryItem("access_token");
dcUrl.setQuery(reducedQuery);
auto request = QNetworkRequest(dcUrl);
if (originalQuery.hasQueryItem("access_token")) {
const auto authorization = QString("Bearer ") + originalQuery.queryItemValue("access_token");
request.setRawHeader("Authorization", authorization.toUtf8());
}
QByteArray payload;
if (dataset.api == Dataset::API::WebKnossos) {
request.setRawHeader("Content-Type", "application/json");
payload = QString{R"json([{"position":[%1,%2,%3],"zoomStep":%4,"cubeSize":%5,"fourBit":false}])json"}.arg(globalCoord.x).arg(globalCoord.y).arg(globalCoord.z).arg(static_cast<std::size_t>(std::log2(dataset.magnification))).arg(dataset.cubeEdgeLength).toUtf8();
}
//request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
//request.setAttribute(QNetworkRequest::SpdyAllowedAttribute, true);
if (globalCoord == center.cube(dataset.cubeEdgeLength, dataset.magnification).cube2Global(dataset.cubeEdgeLength, dataset.magnification)) {
//the first download usually finishes last (which is a bug) so we put it alone in the high priority bucket
request.setPriority(QNetworkRequest::HighPriority);
}
auto * reply = dataset.api == Dataset::API::WebKnossos ? qnam.post(request, payload) : qnam.get(request);
reply->setParent(nullptr);//reparent, so it don’t gets destroyed with qnam
downloads[globalCoord] = reply;
broadcastProgress(true);
QObject::connect(reply, &QNetworkReply::finished, [this, layerId, dataset, reply, globalCoord, &downloads, &decompressions, &freeSlots, &cubeHash](){
if (freeSlots.empty()) {
qCritical() << layerId << globalCoord << static_cast<int>(dataset.type) << "no slots for decompression" << cubeHash.size() << freeSlots.size();
reply->deleteLater();
downloads.erase(globalCoord);
broadcastProgress();
return;
}
if (reply->error() == QNetworkReply::NoError) {
auto * currentSlot = freeSlots.front();
freeSlots.pop_front();
auto * watcher = new QFutureWatcher<DecompressionResult>;
QObject::connect(watcher, &QFutureWatcher<DecompressionResult>::finished, [this, reply, dataset, layerId, &freeSlots, &decompressions, globalCoord, watcher, currentSlot](){
if (!watcher->isCanceled()) {
auto result = watcher->result();
if (!result.first) {//decompression unsuccessful
qCritical() << layerId << globalCoord << static_cast<int>(dataset.type) << "decompression failed → no fill";
freeSlots.emplace_back(result.second);
}
} else {
qCritical() << layerId << globalCoord << static_cast<int>(dataset.type) << "future canceled";
freeSlots.emplace_back(currentSlot);
}
reply->deleteLater();
decompressions.erase(globalCoord);
broadcastProgress();
});
decompressions[globalCoord].reset(watcher);
downloads.erase(globalCoord);
watcher->setFuture(QtConcurrent::run(&decompressionPool, std::bind(&decompressCube, currentSlot, std::ref(*reply), layerId, dataset, std::ref(cubeHash), globalCoord)));
} else {
if (reply->error() == QNetworkReply::ContentNotFoundError) {//404 → fill
auto * currentSlot = freeSlots.front();
freeSlots.pop_front();
std::fill(reinterpret_cast<std::uint8_t *>(currentSlot), reinterpret_cast<std::uint8_t *>(currentSlot) + state->cubeBytes * (dataset.isOverlay() ? OBJID_BYTES : 1), 0);
state->protectCube2Pointer.lock();
cubeHash[globalCoord.cube(dataset.cubeEdgeLength, dataset.magnification)] = currentSlot;
state->protectCube2Pointer.unlock();
state->viewer->reslice_notify_all(layerId, globalCoord);
} else {
if (reply->error() != QNetworkReply::OperationCanceledError) {
qCritical() << layerId << globalCoord << static_cast<int>(dataset.type) << reply->errorString() << reply->readAll();
}
}
reply->deleteLater();
downloads.erase(globalCoord);
broadcastProgress();
}
});
}
};
const auto workaroundProcessLocalImmediately = datasets[0].url.scheme() == "file" ? [](){QCoreApplication::processEvents();} : [](){};
for (auto [layerId, globalCoord] : allCubes) {
if (loadingNr == Loader::Controller::singleton().loadingNr) {
if (datasets[layerId].loadingEnabled) {
try {
startDownload(layerId, datasets[layerId], globalCoord, slotDownload[layerId], slotDecompression[layerId], freeSlots[layerId], state->cube2Pointer.at(layerId).at(loaderMagnification));
} catch (const std::out_of_range &) {}
workaroundProcessLocalImmediately();//https://bugreports.qt.io/browse/QTBUG-45925
}
}
}
}