OpenToonz - An open-source full-featured 2D animation creation software
C
Autoclose: Cache Support
Abel Youcb50cc997ce2
diff --git a/toonz/sources/include/toonz/autoclose.h b/toonz/sources/include/toonz/autoclose.hindex 4550d86dd..8024b1a18 100644--- a/toonz/sources/include/toonz/autoclose.h+++ b/toonz/sources/include/toonz/autoclose.h@@ -4,6 +4,8 @@#define _TAUTOCLOSE_H_#include <memory>+#include <unordered_map>+#include <mutex>#include "tgeometry.h"#include "traster.h"@@ -28,17 +30,52 @@ public:// calcola i segmenti e li disegna sul rastervoid exec();+ void exec(std::string id);// non modifica il raster. Si limita a calcolare i segmentivoid compute(std::vector<Segment> &segments);// disegna sul raster i segmentivoid draw(const std::vector<Segment> &segments);+ static bool hasSegmentCache(const std::string &id) {+ std::lock_guard<std::mutex> lock(m_mutex);+ return m_cache.find(id) != m_cache.end();+ }++ static const std::vector<Segment> &getSegmentCache(const std::string &id) {+ std::lock_guard<std::mutex> lock(m_mutex);+ auto it = m_cache.find(id);+ if (it != m_cache.end()) {+ return it->second;+ }+ static const std::vector<Segment> empty;+ return empty;+ }++ static void setSegmentCache(const std::string &id,+ const std::vector<Segment> &segments) {+ std::lock_guard<std::mutex> lock(m_mutex);+ m_cache[id] = segments;+ }++ static void invalidateSegmentCache(const std::string &id) {+ std::lock_guard<std::mutex> lock(m_mutex);+ m_cache.erase(id);+ }++ static void clearSegmentCache() {+ std::lock_guard<std::mutex> lock(m_mutex);+ m_cache.clear();+ }private:class Imp;std::unique_ptr<Imp> m_imp;+ static std::unordered_map<std::string, std::vector<TAutocloser::Segment>>+ m_cache;+ static std::mutex m_mutex;+// not implementedTAutocloser();TAutocloser(const TAutocloser &a);diff --git a/toonz/sources/include/toonz/stagevisitor.h b/toonz/sources/include/toonz/stagevisitor.hindex 1af0eb513..f7e2e5b3f 100644--- a/toonz/sources/include/toonz/stagevisitor.h+++ b/toonz/sources/include/toonz/stagevisitor.h@@ -7,6 +7,7 @@#include "timage.h"#include "trastercm.h"#include "tgl.h"+#include <string>// TnzExt includes#include "ext/plasticvisualsettings.h"@@ -260,6 +261,7 @@ private:int m_maskLevel;bool m_singleColumnEnabled;bool m_checkFlags;+ std::string m_currentImageId; // Used to update the autoClose cache// darken blended view mode for viewing the non-cleanuped and stacked drawingsbool m_doRasterDarkenBlendedView;@@ -293,7 +295,7 @@ public:bool isSingleColumnEnabled() const { return m_singleColumnEnabled; }void setRasterDarkenBlendedView(bool on) { m_doRasterDarkenBlendedView = on; }-+ void setCurrentImageId(std::string id) { m_currentImageId = id; }std::vector<TStroke *> &getGuidedStrokes() { return m_guidedStrokes; }};diff --git a/toonz/sources/tnztools/tool.cpp b/toonz/sources/tnztools/tool.cppindex 3bbfe09f5..ff47314a4 100644--- a/toonz/sources/tnztools/tool.cpp+++ b/toonz/sources/tnztools/tool.cpp@@ -34,6 +34,7 @@#include "toonz/dpiscale.h"#include "toonz/palettecontroller.h"#include "toonz/tonionskinmaskhandle.h"+#include "toonz/autoclose.h"#include "toutputproperties.h"// TnzCore includes@@ -682,7 +683,7 @@ TTool::Application *TTool::getApplication() {//------------------------------------------------------------------------------/*! Notify change of current image: update icon and notify level change.+/*! Notify change of current image: update icon & Closer and notify level change.If current object is a spline commit spline changed.If current mode is EditingLevel touch current frame.*/@@ -699,6 +700,8 @@ void TTool::notifyImageChanged() {if (!sl) return;TFrameId fid = m_application->getCurrentFrame()->getFid();sl->touchFrame(fid);+ if (sl->getType() & TXshLevelType::RASTER_TYPE)+ TAutocloser::invalidateSegmentCache(sl->getImageId(fid));// sl->setDirtyFlag(true);IconGenerator::instance()->invalidate(sl, fid);IconGenerator::instance()->invalidateSceneIcon();@@ -721,6 +724,9 @@ void TTool::notifyImageChanged() {if (sl) {IconGenerator::instance()->invalidate(sl, cell.m_frameId);sl->touchFrame(cell.m_frameId);+ if (sl->getType() & TXshLevelType::RASTER_TYPE)+ TAutocloser::invalidateSegmentCache(+ sl->getImageId(cell.m_frameId, 0));IconGenerator::instance()->invalidateSceneIcon();}}@@ -730,7 +736,7 @@ void TTool::notifyImageChanged() {//------------------------------------------------------------------------------/*! Notify change of image in \b fid: update icon and notify level change.+/*! Notify change of image in \b fid: update Icon & Closer and notify level change.*/void TTool::notifyImageChanged(const TFrameId &fid) {onImageChanged();@@ -746,6 +752,8 @@ void TTool::notifyImageChanged(const TFrameId &fid) {sl->setDirtyFlag(true);IconGenerator::instance()->invalidate(sl, fid);IconGenerator::instance()->invalidateSceneIcon();+ if (sl->getType() & TXshLevelType::RASTER_TYPE)+ TAutocloser::invalidateSegmentCache(sl->getImageId(fid, 0));} else {int row = m_application->getCurrentFrame()->getFrame();int col = m_application->getCurrentColumn()->getColumnIndex();@@ -761,6 +769,8 @@ void TTool::notifyImageChanged(const TFrameId &fid) {if (sl->m_rasterizePli)sl->touchFrame(fid);sl->setDirtyFlag(true);+ if (sl->getType() & TXshLevelType::RASTER_TYPE)+ TAutocloser::invalidateSegmentCache(sl->getImageId(fid, 0));}}m_application->getCurrentLevel()->notifyLevelChange();diff --git a/toonz/sources/tnztools/toolutils.cpp b/toonz/sources/tnztools/toolutils.cppindex 4ec9856f2..1dcf1c118 100644--- a/toonz/sources/tnztools/toolutils.cpp+++ b/toonz/sources/tnztools/toolutils.cpp@@ -33,6 +33,8 @@#include "toonz/preferences.h"#include "toonz/palettecontroller.h"#include "toonz/txshchildlevel.h"+#include "toonz/stage2.h"+#include "toonz/autoclose.h"#include "toonzqt/tselectionhandle.h"#include "toonzqt/icongenerator.h"@@ -636,6 +638,8 @@ void ToolUtils::TToolUndo::notifyImageChanged() const {std::string id = m_level->getImageId(m_frameId) + "_rasterized";ImageManager::instance()->invalidate(id);}+ if(ToonzCheck::instance()->getChecks() & ToonzCheck::eAutoclose)+ TAutocloser::invalidateSegmentCache(m_level->getImageId(m_frameId));}//------------------------------------------------------------------------------------------diff --git a/toonz/sources/toonz/sceneviewer.cpp b/toonz/sources/toonz/sceneviewer.cppindex aa9d4034f..6d04de744 100644--- a/toonz/sources/toonz/sceneviewer.cpp+++ b/toonz/sources/toonz/sceneviewer.cpp@@ -2272,6 +2272,13 @@ void SceneViewer::drawScene() {}assert(glGetError() == 0);+ if (ToonzCheck::instance()->getChecks() & ToonzCheck::eAutoclose) {+ TXshSimpleLevel *sl = app->getCurrentLevel()->getSimpleLevel();+ if (sl && sl->getType() & TXshLevelType::RASTER_TYPE)+ painter.setCurrentImageId(sl->getImageId(+ app->getCurrentTool()->getTool()->getCurrentFid(),+ 0));+ }painter.flushRasterImages();TXshSimpleLevel::m_fillFullColorRaster = fillFullColorRaster;diff --git a/toonz/sources/toonzlib/autoclose.cpp b/toonz/sources/toonzlib/autoclose.cppindex d41317c50..c86d94460 100644--- a/toonz/sources/toonzlib/autoclose.cpp+++ b/toonz/sources/toonzlib/autoclose.cpp@@ -1127,6 +1127,10 @@ return 0;/*=============================================================================*/+std::unordered_map<std::string, std::vector<TAutocloser::Segment>>+ TAutocloser::m_cache;+std::mutex TAutocloser::m_mutex;+TAutocloser::TAutocloser(const TRasterP &r, int distance, double angle,int index, int opacity): m_imp(new Imp(r, distance, angle, index, opacity)) {}@@ -1139,6 +1143,13 @@ void TAutocloser::exec() {draw(segments);}+void TAutocloser::exec(std::string id) {+ std::vector<TAutocloser::Segment> segments;+ compute(segments);+ draw(segments);+ setSegmentCache(id, std::move(segments));+}+//...............................TAutocloser::~TAutocloser() {}diff --git a/toonz/sources/toonzlib/stagevisitor.cpp b/toonz/sources/toonzlib/stagevisitor.cppindex 85eaa86fe..081cb3806 100644--- a/toonz/sources/toonzlib/stagevisitor.cpp+++ b/toonz/sources/toonzlib/stagevisitor.cpp@@ -568,19 +568,27 @@ void RasterPainter::flushRasterImages() {m_nodes[i].m_palette->setFrame(m_nodes[i].m_frame);TPaletteP plt;- int styleIndex = -1;+ int gapCheckIndex = -1;if ((tc & ToonzCheck::eGap || tc & ToonzCheck::eAutoclose) &&m_nodes[i].m_isCurrentColumn) {srcCm = srcCm->clone();plt = m_nodes[i].m_palette->clone();- styleIndex = plt->addStyle(TPixel::Magenta);- if (tc & ToonzCheck::eAutoclose)- TAutocloser(srcCm, AutocloseDistance, AutocloseAngle, styleIndex,- AutocloseOpacity)- .exec();- if (tc & ToonzCheck::eGap)- AreaFiller(srcCm).rectFill(m_nodes[i].m_savebox, 1, true, true,- false);+ gapCheckIndex = plt->addStyle(TPixel::Magenta);+ if (tc & ToonzCheck::eAutoclose) {+ TAutocloser ac(srcCm, AutocloseDistance, AutocloseAngle,+ gapCheckIndex, AutocloseOpacity);+ if (ac.hasSegmentCache(m_currentImageId))+ ac.draw(ac.getSegmentCache(m_currentImageId));+ else+ ac.exec(m_currentImageId);+ if (tc & ToonzCheck::eGap) {+ AreaFiller(srcCm).rectFill(m_nodes[i].m_savebox, gapCheckIndex,+ true, true, false);+ }+ if (tc & ToonzCheck::eGap)+ AreaFiller(srcCm).rectFill(m_nodes[i].m_savebox, 1, true, true,+ false);+ }} elseplt = m_nodes[i].m_palette;@@ -610,7 +618,7 @@ void RasterPainter::flushRasterImages() {settings.m_transpCheckPaint);settings.m_isOnionSkin = m_nodes[i].m_onionMode != Node::eOnionSkinNone;- settings.m_gapCheckIndex = styleIndex;+ settings.m_gapCheckIndex = gapCheckIndex;TRop::quickPut(viewedRaster, srcCm, plt, aff, settings);}