[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [49870] trunk/blender/source/blender: Fix for [#32220] regression - DistortionCache is never freed.

Jeroen Bakker j.bakker at atmind.nl
Mon Aug 13 12:56:37 CEST 2012


Revision: 49870
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=49870
Author:   jbakker
Date:     2012-08-13 10:56:36 +0000 (Mon, 13 Aug 2012)
Log Message:
-----------
Fix for [#32220] regression - DistortionCache is never freed.

 * at max 10 cache items will be available. Items will be removed by
latest usage.
 * number of cached items can be adjusted in code
 * added deinitialization of compositor when blender exists.
 * updated scons and cmake build files

Modified Paths:
--------------
    trunk/blender/source/blender/compositor/COM_compositor.h
    trunk/blender/source/blender/compositor/intern/COM_compositor.cpp
    trunk/blender/source/blender/compositor/operations/COM_MovieDistortionOperation.cpp
    trunk/blender/source/blender/compositor/operations/COM_MovieDistortionOperation.h
    trunk/blender/source/blender/windowmanager/CMakeLists.txt
    trunk/blender/source/blender/windowmanager/SConscript
    trunk/blender/source/blender/windowmanager/intern/wm_init_exit.c

Modified: trunk/blender/source/blender/compositor/COM_compositor.h
===================================================================
--- trunk/blender/source/blender/compositor/COM_compositor.h	2012-08-13 09:47:45 UTC (rev 49869)
+++ trunk/blender/source/blender/compositor/COM_compositor.h	2012-08-13 10:56:36 UTC (rev 49870)
@@ -300,6 +300,11 @@
 void COM_execute(RenderData *rd, bNodeTree *editingtree, int rendering);
 
 /**
+ * @brief Deinitialize the compositor caches and allocated memory.
+ */
+void COM_deinitialize();
+
+/**
  * @brief Return a list of highlighted bnodes pointers.
  * @return 
  */

Modified: trunk/blender/source/blender/compositor/intern/COM_compositor.cpp
===================================================================
--- trunk/blender/source/blender/compositor/intern/COM_compositor.cpp	2012-08-13 09:47:45 UTC (rev 49869)
+++ trunk/blender/source/blender/compositor/intern/COM_compositor.cpp	2012-08-13 10:56:36 UTC (rev 49870)
@@ -32,23 +32,24 @@
 #include "COM_ExecutionSystem.h"
 #include "COM_WorkScheduler.h"
 #include "OCL_opencl.h"
+#include "COM_MovieDistortionOperation.h"
 
-static ThreadMutex compositorMutex;
+static ThreadMutex s_compositorMutex;
 static char is_compositorMutex_init = FALSE;
 void COM_execute(RenderData *rd, bNodeTree *editingtree, int rendering)
 {
 	if (is_compositorMutex_init == FALSE) { /// TODO: move to blender startup phase
-		memset(&compositorMutex, 0, sizeof(compositorMutex));
-		BLI_mutex_init(&compositorMutex);
+		memset(&s_compositorMutex, 0, sizeof(s_compositorMutex));
+		BLI_mutex_init(&s_compositorMutex);
 		OCL_init();
 		WorkScheduler::initialize(); ///TODO: call workscheduler.deinitialize somewhere
 		is_compositorMutex_init = TRUE;
 	}
-	BLI_mutex_lock(&compositorMutex);
+	BLI_mutex_lock(&s_compositorMutex);
 	if (editingtree->test_break(editingtree->tbh)) {
 		// during editing multiple calls to this method can be triggered.
 		// make sure one the last one will be doing the work.
-		BLI_mutex_unlock(&compositorMutex);
+		BLI_mutex_unlock(&s_compositorMutex);
 		return;
 
 	}
@@ -67,7 +68,7 @@
 		if (editingtree->test_break(editingtree->tbh)) {
 			// during editing multiple calls to this method can be triggered.
 			// make sure one the last one will be doing the work.
-			BLI_mutex_unlock(&compositorMutex);
+			BLI_mutex_unlock(&s_compositorMutex);
 			return;
 		}
 	}
@@ -77,5 +78,18 @@
 	system->execute();
 	delete system;
 
-	BLI_mutex_unlock(&compositorMutex);
+	BLI_mutex_unlock(&s_compositorMutex);
 }
+
+void COM_deinitialize() 
+{
+	if (is_compositorMutex_init)
+	{
+		BLI_mutex_lock(&s_compositorMutex);
+		deintializeDistortionCache();
+		WorkScheduler::deinitialize();
+		is_compositorMutex_init = FALSE;
+		BLI_mutex_unlock(&s_compositorMutex);
+		BLI_mutex_end(&s_compositorMutex);
+	}
+}

Modified: trunk/blender/source/blender/compositor/operations/COM_MovieDistortionOperation.cpp
===================================================================
--- trunk/blender/source/blender/compositor/operations/COM_MovieDistortionOperation.cpp	2012-08-13 09:47:45 UTC (rev 49869)
+++ trunk/blender/source/blender/compositor/operations/COM_MovieDistortionOperation.cpp	2012-08-13 10:56:36 UTC (rev 49870)
@@ -31,6 +31,15 @@
 
 vector<DistortionCache *> s_cache;
 
+void deintializeDistortionCache(void) 
+{
+	while (s_cache.size()>0)
+	{
+		DistortionCache * cache = s_cache.back();
+		s_cache.pop_back();
+		delete cache;
+	}
+}
 
 MovieDistortionOperation::MovieDistortionOperation(bool distortion) : NodeOperation()
 {
@@ -52,12 +61,14 @@
 		BKE_movieclip_user_set_frame(&clipUser, this->m_framenumber);
 		BKE_movieclip_get_size(this->m_movieClip, &clipUser, &calibration_width, &calibration_height);
 
-		for (unsigned int i = 0; i < s_cache.size(); i++) {
+		for (unsigned int i = 0; i < s_cache.size(); i++) 
+		{
 			DistortionCache *c = (DistortionCache *)s_cache[i];
 			if (c->isCacheFor(this->m_movieClip, this->m_width, this->m_height,
 			                  calibration_width, calibration_height, this->m_distortion))
 			{
 				this->m_cache = c;
+				this->m_cache->updateLastUsage();
 				return;
 			}
 		}
@@ -75,6 +86,21 @@
 {
 	this->m_inputOperation = NULL;
 	this->m_movieClip = NULL;
+	while (s_cache.size() > COM_DISTORTIONCACHE_MAXSIZE) 
+	{
+		double minTime = PIL_check_seconds_timer();
+		vector<DistortionCache*>::iterator minTimeIterator = s_cache.begin();
+		for (vector<DistortionCache*>::iterator it = s_cache.begin(); it < s_cache.end(); it ++) 
+		{
+			DistortionCache * cache = *it;
+			if (cache->getTimeLastUsage()<minTime) 
+			{
+				minTime = cache->getTimeLastUsage();
+				minTimeIterator = it;
+			}
+		}
+		s_cache.erase(minTimeIterator);
+	}
 }
 
 

Modified: trunk/blender/source/blender/compositor/operations/COM_MovieDistortionOperation.h
===================================================================
--- trunk/blender/source/blender/compositor/operations/COM_MovieDistortionOperation.h	2012-08-13 09:47:45 UTC (rev 49869)
+++ trunk/blender/source/blender/compositor/operations/COM_MovieDistortionOperation.h	2012-08-13 10:56:36 UTC (rev 49870)
@@ -27,8 +27,11 @@
 #include "DNA_movieclip_types.h"
 extern "C" {
 	#include "BKE_tracking.h"
+	#include "PIL_time.h"
 }
 
+#define COM_DISTORTIONCACHE_MAXSIZE 10
+
 class DistortionCache {
 private:
 	float m_k1;
@@ -44,6 +47,8 @@
 	bool m_inverted;
 	float *m_buffer;
 	int *m_bufferCalculated;
+	double timeLastUsage;
+	
 public:
 	DistortionCache(MovieClip *movieclip, int width, int height, int calibration_width, int calibration_height, bool inverted) {
 		this->m_k1 = movieclip->tracking.camera.k1;
@@ -62,7 +67,29 @@
 		for (int i = 0; i < this->m_width * this->m_height; i++) {
 			this->m_bufferCalculated[i] = 0;
 		}
+		this->updateLastUsage();
 	}
+	
+	~DistortionCache() {
+		if (this->m_buffer) {
+			delete[] this->m_buffer;
+			this->m_buffer = NULL;
+		}
+		
+		if (this->m_bufferCalculated) {
+			delete[] this->m_bufferCalculated;
+			this->m_bufferCalculated = NULL;
+		}
+	}
+	
+	void updateLastUsage() {
+		this->timeLastUsage = PIL_check_seconds_timer();
+	}
+	
+	inline double getTimeLastUsage() {
+		return this->timeLastUsage;
+	}
+
 	bool isCacheFor(MovieClip *movieclip, int width, int height, int calibration_width, int claibration_height, bool inverted) {
 		return this->m_k1 == movieclip->tracking.camera.k1 &&
 		       this->m_k2 == movieclip->tracking.camera.k2 &&
@@ -139,4 +166,6 @@
 	void setFramenumber(int framenumber) { this->m_framenumber = framenumber; }
 };
 
+void deintializeDistortionCache(void);
+
 #endif

Modified: trunk/blender/source/blender/windowmanager/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/windowmanager/CMakeLists.txt	2012-08-13 09:47:45 UTC (rev 49869)
+++ trunk/blender/source/blender/windowmanager/CMakeLists.txt	2012-08-13 10:56:36 UTC (rev 49870)
@@ -36,6 +36,8 @@
 	../makesdna
 	../makesrna
 	../nodes
+	../compositor
+	../opencl
 	../render/extern/include
 	../../gameengine/BlenderRoutines
 	../../../intern/elbeem/extern

Modified: trunk/blender/source/blender/windowmanager/SConscript
===================================================================
--- trunk/blender/source/blender/windowmanager/SConscript	2012-08-13 09:47:45 UTC (rev 49869)
+++ trunk/blender/source/blender/windowmanager/SConscript	2012-08-13 10:56:36 UTC (rev 49870)
@@ -10,7 +10,7 @@
 incs = '. ../editors/include ../python ../makesdna ../blenlib ../blenkernel'
 incs += ' ../nodes ../imbuf ../blenloader ../render/extern/include'
 incs += ' ../radiosity/extern/include'
-incs += ' ../makesrna ../gpu ../blenfont ../bmesh'
+incs += ' ../makesrna ../gpu ../blenfont ../bmesh ../compositor'
 
 incs += ' #/intern/guardedalloc #/intern/memutil #/intern/ghost'
 incs += ' #/intern/elbeem #/extern/glew/include'

Modified: trunk/blender/source/blender/windowmanager/intern/wm_init_exit.c
===================================================================
--- trunk/blender/source/blender/windowmanager/intern/wm_init_exit.c	2012-08-13 09:47:45 UTC (rev 49869)
+++ trunk/blender/source/blender/windowmanager/intern/wm_init_exit.c	2012-08-13 10:56:36 UTC (rev 49870)
@@ -113,6 +113,7 @@
 
 #include "BKE_depsgraph.h"
 #include "BKE_sound.h"
+#include "COM_compositor.h"
 
 static void wm_init_reports(bContext *C)
 {
@@ -211,7 +212,6 @@
 #ifdef WITH_COMPOSITOR
 	if (1) {
 		extern void *COM_linker_hack;
-		extern void *COM_execute;
 		COM_linker_hack = COM_execute;
 	}
 #endif
@@ -414,6 +414,10 @@
 	BKE_sequencer_free_clipboard(); /* sequencer.c */
 	BKE_tracking_clipboard_free();
 		
+#ifdef WITH_COMPOSITOR
+	COM_deinitialize();
+#endif
+	
 	free_blender();  /* blender.c, does entire library and spacetypes */
 //	free_matcopybuf();
 	free_anim_copybuf();




More information about the Bf-blender-cvs mailing list