Merging r49451 through r49462 from trunk into soc-2011-tomato
[blender.git] / source / blender / compositor / operations / COM_ViewerBaseOperation.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 #include "COM_ViewerBaseOperation.h"
24 #include "COM_SocketConnection.h"
25 #include "BLI_listbase.h"
26 #include "BKE_image.h"
27 #include "WM_api.h"
28 #include "WM_types.h"
29 #include "PIL_time.h"
30 #include "BLI_utildefines.h"
31 #include "BLI_math_color.h"
32
33 extern "C" {
34         #include "MEM_guardedalloc.h"
35         #include "IMB_imbuf.h"
36         #include "IMB_imbuf_types.h"
37         #include "IMB_colormanagement.h"
38 }
39
40
41 ViewerBaseOperation::ViewerBaseOperation() : NodeOperation()
42 {
43         this->setImage(NULL);
44         this->setImageUser(NULL);
45         this->m_outputBuffer = NULL;
46         this->m_depthBuffer = NULL;
47         this->m_outputBufferDisplay = NULL;
48         this->m_active = false;
49         this->m_doColorManagement = true;
50         this->m_doDepthBuffer = false;
51 }
52
53 void ViewerBaseOperation::initExecution()
54 {
55         if (isActiveViewerOutput()) {
56                 initImage();
57         }
58 }
59
60 void ViewerBaseOperation::initImage()
61 {
62         Image *anImage = this->m_image;
63         ImBuf *ibuf = BKE_image_acquire_ibuf(anImage, this->m_imageUser, &this->m_lock);
64
65         if (!ibuf) return;
66         BLI_lock_thread(LOCK_DRAW_IMAGE);
67         if (ibuf->x != (int)getWidth() || ibuf->y != (int)getHeight()) {
68
69                 imb_freerectImBuf(ibuf);
70                 imb_freerectfloatImBuf(ibuf);
71                 IMB_freezbuffloatImBuf(ibuf);
72                 ibuf->x = getWidth();
73                 ibuf->y = getHeight();
74                 imb_addrectImBuf(ibuf);
75                 imb_addrectfloatImBuf(ibuf);
76                 anImage->ok = IMA_OK_LOADED;
77
78                 IMB_display_buffer_invalidate(ibuf);
79
80                 BLI_unlock_thread(LOCK_DRAW_IMAGE);
81         }
82
83         if (m_doDepthBuffer) 
84         {
85                 addzbuffloatImBuf(ibuf);
86         }
87         BLI_unlock_thread(LOCK_DRAW_IMAGE);
88
89         /* now we combine the input with ibuf */
90         this->m_outputBuffer = ibuf->rect_float;
91         this->m_outputBufferDisplay = (unsigned char *)ibuf->rect;
92
93         /* needed for display buffer update
94          *
95          * no need to lock / reference the image buffer because it's seems
96          * to be the single place which changes buffers of viewer image
97          * which is this node
98          */
99         this->m_ibuf = ibuf;
100
101         if (m_doDepthBuffer)
102         {
103                 this->m_depthBuffer = ibuf->zbuf_float;
104         }
105
106         BKE_image_release_ibuf(this->m_image, this->m_lock);
107 }
108 void ViewerBaseOperation:: updateImage(rcti *rect)
109 {
110         IMB_partial_display_buffer_update(this->m_ibuf, this->m_outputBuffer, getWidth(), 0, 0,
111                                           rect->xmin, rect->ymin, rect->xmax, rect->ymax);
112
113         WM_main_add_notifier(NC_WINDOW | ND_DRAW, NULL);
114 }
115
116 void ViewerBaseOperation::deinitExecution()
117 {
118         this->m_outputBuffer = NULL;
119 }
120
121 const CompositorPriority ViewerBaseOperation::getRenderPriority() const
122 {
123         if (this->isActiveViewerOutput()) {
124                 return COM_PRIORITY_HIGH;
125         }
126         else {
127                 return COM_PRIORITY_LOW;
128         }
129 }