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