Compositor: Speedup movie (un)distortion operation
[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
29 #include "BLT_translation.h"
30
31 #include "BKE_scene.h"
32
33 #include "COM_compositor.h"
34 #include "COM_ExecutionSystem.h"
35 #include "COM_WorkScheduler.h"
36 #include "clew.h"
37 #include "COM_MovieDistortionOperation.h"
38
39 static ThreadMutex s_compositorMutex;
40 static bool is_compositorMutex_init = false;
41
42 void COM_execute(RenderData *rd, Scene *scene, bNodeTree *editingtree, int rendering,
43                  const ColorManagedViewSettings *viewSettings,
44                  const ColorManagedDisplaySettings *displaySettings,
45                  const char *viewName)
46 {
47         /* initialize mutex, TODO this mutex init is actually not thread safe and
48          * should be done somewhere as part of blender startup, all the other
49          * initializations can be done lazily */
50         if (is_compositorMutex_init == false) {
51                 BLI_mutex_init(&s_compositorMutex);
52                 is_compositorMutex_init = true;
53         }
54
55         BLI_mutex_lock(&s_compositorMutex);
56
57         if (editingtree->test_break(editingtree->tbh)) {
58                 // during editing multiple calls to this method can be triggered.
59                 // make sure one the last one will be doing the work.
60                 BLI_mutex_unlock(&s_compositorMutex);
61                 return;
62         }
63
64         /* Make sure node tree has previews.
65          * Don't create previews in advance, this is done when adding preview operations.
66          * Reserved preview size is determined by render output for now.
67          */
68         float aspect = rd->xsch > 0 ? (float)rd->ysch / (float)rd->xsch : 1.0f;
69         BKE_node_preview_init_tree(editingtree, COM_PREVIEW_SIZE, (int)(COM_PREVIEW_SIZE * aspect), false);
70
71         /* initialize workscheduler, will check if already done. TODO deinitialize somewhere */
72         bool use_opencl = (editingtree->flag & NTREE_COM_OPENCL) != 0;
73         WorkScheduler::initialize(use_opencl, BKE_render_num_threads(rd));
74
75         /* set progress bar to 0% and status to init compositing */
76         editingtree->progress(editingtree->prh, 0.0);
77         editingtree->stats_draw(editingtree->sdh, IFACE_("Compositing"));
78
79         bool twopass = (editingtree->flag & NTREE_TWO_PASS) > 0 && !rendering;
80         /* initialize execution system */
81         if (twopass) {
82                 ExecutionSystem *system = new ExecutionSystem(rd, scene, editingtree, rendering, twopass, viewSettings, displaySettings, viewName);
83                 system->execute();
84                 delete system;
85                 
86                 if (editingtree->test_break(editingtree->tbh)) {
87                         // during editing multiple calls to this method can be triggered.
88                         // make sure one the last one will be doing the work.
89                         BLI_mutex_unlock(&s_compositorMutex);
90                         return;
91                 }
92         }
93
94         ExecutionSystem *system = new ExecutionSystem(rd, scene, editingtree, rendering, false,
95                                                       viewSettings, displaySettings, viewName);
96         system->execute();
97         delete system;
98
99         BLI_mutex_unlock(&s_compositorMutex);
100 }
101
102 void COM_deinitialize()
103 {
104         if (is_compositorMutex_init) {
105                 BLI_mutex_lock(&s_compositorMutex);
106                 WorkScheduler::deinitialize();
107                 is_compositorMutex_init = false;
108                 BLI_mutex_unlock(&s_compositorMutex);
109                 BLI_mutex_end(&s_compositorMutex);
110         }
111 }