diff --git a/app/config/config.cpp b/app/config/config.cpp index 52258a73a..474c08160 100644 --- a/app/config/config.cpp +++ b/app/config/config.cpp @@ -76,6 +76,7 @@ void Config::SetDefaults() config_map_["DiskCachePath"] = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation); config_map_["DiskCacheSize"] = 20.0; + config_map_["ClearDiskCacheOnClose"] = false; config_map_["DefaultSequenceWidth"] = 1920; config_map_["DefaultSequenceHeight"] = 1080; diff --git a/app/core.cpp b/app/core.cpp index e85f6e26e..9fc916734 100644 --- a/app/core.cpp +++ b/app/core.cpp @@ -43,6 +43,7 @@ #include "project/item/footage/footage.h" #include "project/item/sequence/sequence.h" #include "render/colormanager.h" +#include "render/diskmanager.h" #include "task/import/import.h" #include "task/taskmanager.h" #include "ui/style/style.h" @@ -129,6 +130,8 @@ void Core::Stop() AudioManager::DestroyInstance(); + DiskManager::DestroyInstance(); + NodeFactory::Destroy(); delete main_window_; @@ -376,6 +379,9 @@ void Core::StartGUI(bool full_screen) // Initialize audio service AudioManager::CreateInstance(); + // Initialize disk service + DiskManager::CreateInstance(); + // Connect the PanelFocusManager to the application's focus change signal connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), diff --git a/app/dialog/preferences/tabs/preferencesdisktab.cpp b/app/dialog/preferences/tabs/preferencesdisktab.cpp index 08fba125a..ff8d9474b 100644 --- a/app/dialog/preferences/tabs/preferencesdisktab.cpp +++ b/app/dialog/preferences/tabs/preferencesdisktab.cpp @@ -42,6 +42,12 @@ PreferencesDiskTab::PreferencesDiskTab() connect(clear_cache_btn, &QPushButton::clicked, this, &PreferencesDiskTab::ClearDiskCache); layout->addWidget(clear_cache_btn, row, 1, 1, 2); + row++; + + clear_disk_cache_ = new QCheckBox(tr("Automatically clear disk cache on close")); + clear_disk_cache_->setChecked(Config::Current()["ClearDiskCacheOnClose"].toBool()); + layout->addWidget(clear_disk_cache_, row, 1, 1, 2); + outer_layout->addStretch(); } @@ -49,6 +55,7 @@ void PreferencesDiskTab::Accept() { Config::Current()["DiskCachePath"] = disk_cache_location_->text(); Config::Current()["DiskCacheSize"] = maximum_cache_slider_->GetValue(); + Config::Current()["ClearDiskCacheOnClose"] = clear_disk_cache_->isChecked(); } void PreferencesDiskTab::DiskCacheLineEditChanged() diff --git a/app/dialog/preferences/tabs/preferencesdisktab.h b/app/dialog/preferences/tabs/preferencesdisktab.h index 37a2eae01..fc2862511 100644 --- a/app/dialog/preferences/tabs/preferencesdisktab.h +++ b/app/dialog/preferences/tabs/preferencesdisktab.h @@ -1,6 +1,7 @@ #ifndef PREFERENCESDISKTAB_H #define PREFERENCESDISKTAB_H +#include #include #include "preferencestab.h" @@ -19,6 +20,8 @@ private: FloatSlider* maximum_cache_slider_; + QCheckBox* clear_disk_cache_; + private slots: void DiskCacheLineEditChanged(); diff --git a/app/render/backend/videorenderbackend.cpp b/app/render/backend/videorenderbackend.cpp index ea99687a0..fc8f43341 100644 --- a/app/render/backend/videorenderbackend.cpp +++ b/app/render/backend/videorenderbackend.cpp @@ -28,6 +28,7 @@ #include #include "common/timecodefunctions.h" +#include "render/diskmanager.h" #include "render/pixelservice.h" #include "videorenderworker.h" @@ -181,6 +182,8 @@ const char *VideoRenderBackend::GetCachedFrame(const rational &time) auto in = OIIO::ImageInput::open(fn.toStdString()); if (in) { + DiskManager::instance()->Accessed(frame_hash); + in->read_image(PixelService::GetPixelFormatInfo(params_.format()).oiio_desc, cache_frame_load_buffer_.data()); in->close(); @@ -244,6 +247,11 @@ void VideoRenderBackend::ThreadCompletedDownload(NodeDependency dep, qint64 job_ SetFrameHash(dep, hash, job_time); + // Register frame with the disk manager + if (operating_mode_ & VideoRenderWorker::kDownloadOnly) { + DiskManager::instance()->CreatedFile(frame_cache()->CachePathName(hash), hash); + } + QList hashes_with_time = frame_cache()->FramesWithHash(hash); foreach (const rational& t, hashes_with_time) { diff --git a/app/render/diskmanager.cpp b/app/render/diskmanager.cpp index f210bac2b..f06f3631d 100644 --- a/app/render/diskmanager.cpp +++ b/app/render/diskmanager.cpp @@ -13,13 +13,14 @@ DiskManager* DiskManager::instance_ = nullptr; DiskManager::DiskManager() : consumption_(0) { - // Try to load any current cache index from file QFile cache_index_file(QDir(GetMediaCacheLocation()).filePath("index")); if (cache_index_file.open(QFile::ReadOnly)) { QDataStream ds(&cache_index_file); + ds >> consumption_; + while (!cache_index_file.atEnd()) { HashTime h; @@ -30,28 +31,32 @@ DiskManager::DiskManager() : disk_data_.append(h); } - } else { - qWarning() << "Failed to read cache index"; } - } DiskManager::~DiskManager() { - // Save current cache index - QFile cache_index_file(QDir(GetMediaCacheLocation()).filePath("index")); - - if (cache_index_file.open(QFile::WriteOnly)) { - QDataStream ds(&cache_index_file); - - foreach (const HashTime& h, disk_data_) { - ds << h.file_name; - ds << h.hash; - ds << h.access_time; - ds << h.file_size; - } + if (Config::Current()["ClearDiskCacheOnClose"].toBool()) { + // Clear all cache data + QDir(GetMediaCacheLocation()).removeRecursively(); } else { - qWarning() << "Failed to write cache index"; + // Save current cache index + QFile cache_index_file(QDir(GetMediaCacheLocation()).filePath("index")); + + if (cache_index_file.open(QFile::WriteOnly)) { + QDataStream ds(&cache_index_file); + + ds << consumption_; + + foreach (const HashTime& h, disk_data_) { + ds << h.file_name; + ds << h.hash; + ds << h.access_time; + ds << h.file_size; + } + } else { + qWarning() << "Failed to write cache index"; + } } } diff --git a/app/window/mainwindow/mainwindow.cpp b/app/window/mainwindow/mainwindow.cpp index c69ea445c..04e49f26f 100644 --- a/app/window/mainwindow/mainwindow.cpp +++ b/app/window/mainwindow/mainwindow.cpp @@ -143,20 +143,11 @@ void MainWindow::ProjectOpen(Project* p) SetDefaultLayout(); } -// FIXME: Test code -#include -#include "common/filefunctions.h" -// End test code - void MainWindow::closeEvent(QCloseEvent *e) { PanelManager::instance()->DeleteAllPanels(); - // FIXME: Test code - We have no cache management and the cache is very much testing only, so we delete it on close - // as to not clog up HDD space - QDir(GetMediaCacheLocation()).removeRecursively(); - //QDir(GetMediaIndexLocation()).removeRecursively(); - // End test code + QMainWindow::closeEvent(e); }