Fix #35634: weight paint did not do z-buffer culling anymore on Windows after recent...
[blender.git] / source / blender / compositor / intern / COM_compositor.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor: 
19  *              Jeroen Bakker 
20  *              Monique Dewanchand
21  */
22
23
24 extern "C" {
25 #include "BKE_node.h"
26 #include "BLI_threads.h"
27 }
28 #include "BKE_main.h"
29 #include "BKE_global.h"
30
31 #include "COM_compositor.h"
32 #include "COM_ExecutionSystem.h"
33 #include "COM_WorkScheduler.h"
34 #include "OCL_opencl.h"
35 #include "COM_MovieDistortionOperation.h"
36
37 static ThreadMutex s_compositorMutex;
38 static char is_compositorMutex_init = FALSE;
39
40 static void intern_freeCompositorCaches()
41 {
42         deintializeDistortionCache();
43 }
44
45 void COM_execute(RenderData *rd, bNodeTree *editingtree, int rendering,
46                  const ColorManagedViewSettings *viewSettings,
47                  const ColorManagedDisplaySettings *displaySettings)
48 {
49         /* initialize mutex, TODO this mutex init is actually not thread safe and
50          * should be done somewhere as part of blender startup, all the other
51          * initializations can be done lazily */
52         if (is_compositorMutex_init == FALSE) {
53                 BLI_mutex_init(&s_compositorMutex);
54                 is_compositorMutex_init = TRUE;
55         }
56
57         BLI_mutex_lock(&s_compositorMutex);
58
59         if (editingtree->test_break(editingtree->tbh)) {
60                 // during editing multiple calls to this method can be triggered.
61                 // make sure one the last one will be doing the work.
62                 BLI_mutex_unlock(&s_compositorMutex);
63                 return;
64         }
65
66         /* Make sure node tree has previews.
67          * Don't create previews in advance, this is done when adding preview operations.
68          * Reserved preview size is determined by render output for now.
69          */
70         float aspect = rd->xsch > 0 ? (float)rd->ysch / (float)rd->xsch : 1.0f;
71         BKE_node_preview_init_tree(editingtree, COM_PREVIEW_SIZE, (int)(COM_PREVIEW_SIZE * aspect), FALSE);
72
73         /* initialize workscheduler, will check if already done. TODO deinitialize somewhere */
74         bool use_opencl = (editingtree->flag & NTREE_COM_OPENCL) != 0;
75         WorkScheduler::initialize(use_opencl);
76
77         /* set progress bar to 0% and status to init compositing */
78         editingtree->progress(editingtree->prh, 0.0);
79
80         bool twopass = (editingtree->flag & NTREE_TWO_PASS) > 0 && !rendering;
81         /* initialize execution system */
82         if (twopass) {
83                 ExecutionSystem *system = new ExecutionSystem(rd, editingtree, rendering, twopass, viewSettings, displaySettings);
84                 system->execute();
85                 delete system;
86                 
87                 if (editingtree->test_break(editingtree->tbh)) {
88                         // during editing multiple calls to this method can be triggered.
89                         // make sure one the last one will be doing the work.
90                         BLI_mutex_unlock(&s_compositorMutex);
91                         return;
92                 }
93         }
94
95         ExecutionSystem *system = new ExecutionSystem(rd, editingtree, rendering, false,
96                                                       viewSettings, displaySettings);
97         system->execute();
98         delete system;
99
100         BLI_mutex_unlock(&s_compositorMutex);
101 }
102
103 static void UNUSED_FUNCTION(COM_freeCaches)()
104 {
105         if (is_compositorMutex_init) {
106                 BLI_mutex_lock(&s_compositorMutex);
107                 intern_freeCompositorCaches();
108                 BLI_mutex_unlock(&s_compositorMutex);
109         }
110 }
111
112 void COM_deinitialize()
113 {
114         if (is_compositorMutex_init) {
115                 BLI_mutex_lock(&s_compositorMutex);
116                 intern_freeCompositorCaches();
117                 WorkScheduler::deinitialize();
118                 is_compositorMutex_init = FALSE;
119                 BLI_mutex_unlock(&s_compositorMutex);
120                 BLI_mutex_end(&s_compositorMutex);
121         }
122 }