Changes to partial update during rendering
[blender.git] / source / blender / editors / render / render_view.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
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  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/render/render_view.c
26  *  \ingroup edrend
27  */
28
29 #include <string.h>
30 #include <stddef.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_utildefines.h"
36
37 #include "DNA_scene_types.h"
38 #include "DNA_userdef_types.h"
39
40 #include "BKE_blender.h"
41 #include "BKE_context.h"
42 #include "BKE_image.h"
43 #include "BKE_global.h"
44 #include "BKE_main.h"
45 #include "BKE_node.h"
46 #include "BKE_report.h"
47 #include "BKE_screen.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "ED_screen.h"
53
54 #include "wm_window.h"
55
56 #include "render_intern.h"
57
58 /*********************** utilities for finding areas *************************/
59
60 /* returns biggest area that is not uv/image editor. Note that it uses buttons */
61 /* window as the last possible alternative.                                                                        */
62 /* would use BKE_screen_find_big_area(...) but this is too specific            */
63 static ScrArea *biggest_non_image_area(bContext *C)
64 {
65         bScreen *sc = CTX_wm_screen(C);
66         ScrArea *sa, *big = NULL;
67         int size, maxsize = 0, bwmaxsize = 0;
68         short foundwin = 0;
69
70         for (sa = sc->areabase.first; sa; sa = sa->next) {
71                 if (sa->winx > 30 && sa->winy > 30) {
72                         size = sa->winx * sa->winy;
73                         if (sa->spacetype == SPACE_BUTS) {
74                                 if (foundwin == 0 && size > bwmaxsize) {
75                                         bwmaxsize = size;
76                                         big = sa;
77                                 }
78                         }
79                         else if (sa->spacetype != SPACE_IMAGE && size > maxsize) {
80                                 maxsize = size;
81                                 big = sa;
82                                 foundwin = 1;
83                         }
84                 }
85         }
86
87         return big;
88 }
89
90 static ScrArea *find_area_showing_r_result(bContext *C, Scene *scene, wmWindow **win)
91 {
92         wmWindowManager *wm = CTX_wm_manager(C);
93         ScrArea *sa = NULL;
94         SpaceImage *sima;
95
96         /* find an imagewindow showing render result */
97         for (*win = wm->windows.first; *win; *win = (*win)->next) {
98                 if ((*win)->screen->scene == scene) {
99                         for (sa = (*win)->screen->areabase.first; sa; sa = sa->next) {
100                                 if (sa->spacetype == SPACE_IMAGE) {
101                                         sima = sa->spacedata.first;
102                                         if (sima->image && sima->image->type == IMA_TYPE_R_RESULT)
103                                                 break;
104                                 }
105                         }
106                         if (sa)
107                                 break;
108                 }
109         }
110         
111         return sa;
112 }
113
114 static ScrArea *find_area_image_empty(bContext *C)
115 {
116         bScreen *sc = CTX_wm_screen(C);
117         ScrArea *sa;
118         SpaceImage *sima;
119
120         /* find an imagewindow showing render result */
121         for (sa = sc->areabase.first; sa; sa = sa->next) {
122                 if (sa->spacetype == SPACE_IMAGE) {
123                         sima = sa->spacedata.first;
124                         if (!sima->image)
125                                 break;
126                 }
127         }
128
129         return sa;
130 }
131
132 /********************** open image editor for render *************************/
133
134 /* new window uses x,y to set position */
135 ScrArea *render_view_open(bContext *C, int mx, int my)
136 {
137         wmWindow *win = CTX_wm_window(C);
138         Scene *scene = CTX_data_scene(C);
139         ScrArea *sa = NULL;
140         SpaceImage *sima;
141         int area_was_image = 0;
142
143         if (scene->r.displaymode == R_OUTPUT_NONE)
144                 return NULL;
145         
146         if (scene->r.displaymode == R_OUTPUT_WINDOW) {
147                 rcti rect;
148                 int sizex, sizey;
149
150                 sizex = 10 + (scene->r.xsch * scene->r.size) / 100;
151                 sizey = 40 + (scene->r.ysch * scene->r.size) / 100;
152
153                 /* arbitrary... miniature image window views don't make much sense */
154                 if (sizex < 320) sizex = 320;
155                 if (sizey < 256) sizey = 256;
156
157                 /* some magic to calculate postition */
158                 /* pixelsize: mouse coords are in U.pixelsize units :/ */
159                 rect.xmin = (mx / U.pixelsize) + win->posx - sizex / 2;
160                 rect.ymin = (my / U.pixelsize) + win->posy - sizey / 2;
161                 rect.xmax = rect.xmin + sizex;
162                 rect.ymax = rect.ymin + sizey;
163
164                 /* changes context! */
165                 WM_window_open_temp(C, &rect, WM_WINDOW_RENDER);
166
167                 sa = CTX_wm_area(C);
168         }
169         else if (scene->r.displaymode == R_OUTPUT_SCREEN) {
170                 if (CTX_wm_area(C) && CTX_wm_area(C)->spacetype == SPACE_IMAGE)
171                         area_was_image = 1;
172
173                 /* this function returns with changed context */
174                 sa = ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_IMAGE);
175         }
176
177         if (!sa) {
178                 sa = find_area_showing_r_result(C, scene, &win);
179                 if (sa == NULL)
180                         sa = find_area_image_empty(C);
181                 
182                 /* if area found in other window, we make that one show in front */
183                 if (win && win != CTX_wm_window(C))
184                         wm_window_raise(win);
185
186                 if (sa == NULL) {
187                         /* find largest open non-image area */
188                         sa = biggest_non_image_area(C);
189                         if (sa) {
190                                 ED_area_newspace(C, sa, SPACE_IMAGE);
191                                 sima = sa->spacedata.first;
192
193                                 /* makes ESC go back to prev space */
194                                 sima->flag |= SI_PREVSPACE;
195                         }
196                         else {
197                                 /* use any area of decent size */
198                                 sa = BKE_screen_find_big_area(CTX_wm_screen(C), -1, 0);
199                                 if (sa->spacetype != SPACE_IMAGE) {
200                                         // XXX newspace(sa, SPACE_IMAGE);
201                                         sima = sa->spacedata.first;
202
203                                         /* makes ESC go back to prev space */
204                                         sima->flag |= SI_PREVSPACE;
205                                 }
206                         }
207                 }
208         }
209         sima = sa->spacedata.first;
210
211         /* get the correct image, and scale it */
212         sima->image = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
213
214
215         /* if we're rendering to full screen, set appropriate hints on image editor
216          * so it can restore properly on pressing esc */
217         if (sa->full) {
218                 sima->flag |= SI_FULLWINDOW;
219
220                 /* Tell the image editor to revert to previous space in space list on close
221                  * _only_ if it wasn't already an image editor when the render was invoked */
222                 if (area_was_image == 0)
223                         sima->flag |= SI_PREVSPACE;
224                 else {
225                         /* Leave it alone so the image editor will just go back from
226                          * full screen to the original tiled setup */
227                 }
228         }
229
230         return sa;
231 }
232
233 /*************************** cancel render viewer **********************/
234
235 static int render_view_cancel_exec(bContext *C, wmOperator *UNUSED(op))
236 {
237         wmWindow *win = CTX_wm_window(C);
238         ScrArea *sa = CTX_wm_area(C);
239         SpaceImage *sima = sa->spacedata.first;
240
241         /* test if we have a temp screen in front */
242         if (CTX_wm_window(C)->screen->temp) {
243                 wm_window_lower(CTX_wm_window(C));
244                 return OPERATOR_FINISHED;
245         }
246         /* determine if render already shows */
247         else if (sima->flag & SI_PREVSPACE) {
248                 sima->flag &= ~SI_PREVSPACE;
249
250                 if (sima->flag & SI_FULLWINDOW) {
251                         sima->flag &= ~SI_FULLWINDOW;
252                         ED_screen_full_prevspace(C, sa);
253                 }
254                 else
255                         ED_area_prevspace(C, sa);
256
257                 return OPERATOR_FINISHED;
258         }
259         else if (sima->flag & SI_FULLWINDOW) {
260                 sima->flag &= ~SI_FULLWINDOW;
261                 ED_screen_full_toggle(C, win, sa);
262                 return OPERATOR_FINISHED;
263         }
264
265         return OPERATOR_PASS_THROUGH;
266 }
267
268 void RENDER_OT_view_cancel(struct wmOperatorType *ot)
269 {
270         /* identifiers */
271         ot->name = "Cancel Render View";
272         ot->description = "Cancel show render view";
273         ot->idname = "RENDER_OT_view_cancel";
274
275         /* api callbacks */
276         ot->exec = render_view_cancel_exec;
277         ot->poll = ED_operator_image_active;
278 }
279
280 /************************* show render viewer *****************/
281
282 static int render_view_show_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
283 {
284         wmWindow *wincur = CTX_wm_window(C);
285         
286         /* test if we have currently a temp screen active */
287         if (wincur->screen->temp) {
288                 wm_window_lower(wincur);
289         }
290         else {
291                 wmWindow *win, *winshow;
292                 ScrArea *sa = find_area_showing_r_result(C, CTX_data_scene(C), &winshow);
293                 
294                 /* is there another window on current scene showing result? */
295                 for (win = CTX_wm_manager(C)->windows.first; win; win = win->next) {
296                         bScreen *sc = win->screen;
297                         if ((sc->temp && ((ScrArea *)sc->areabase.first)->spacetype == SPACE_IMAGE) ||
298                             (win == winshow && winshow != wincur))
299                         {
300                                 wm_window_raise(win);
301                                 return OPERATOR_FINISHED;
302                         }
303                 }
304                 
305                 /* determine if render already shows */
306                 if (sa) {
307                         /* but don't close it when rendering */
308                         if (G.is_rendering == FALSE) {
309                                 SpaceImage *sima = sa->spacedata.first;
310
311                                 if (sima->flag & SI_PREVSPACE) {
312                                         sima->flag &= ~SI_PREVSPACE;
313
314                                         if (sima->flag & SI_FULLWINDOW) {
315                                                 sima->flag &= ~SI_FULLWINDOW;
316                                                 ED_screen_full_prevspace(C, sa);
317                                         }
318                                         else if (sima->next) {
319                                                 /* workaround for case of double prevspace, render window
320                                                  * with a file browser on top of it (same as in ED_area_prevspace) */
321                                                 if (sima->next->spacetype == SPACE_FILE && sima->next->next)
322                                                         ED_area_newspace(C, sa, sima->next->next->spacetype);
323                                                 else
324                                                         ED_area_newspace(C, sa, sima->next->spacetype);
325                                                 ED_area_tag_redraw(sa);
326                                         }
327                                 }
328                         }
329                 }
330                 else {
331                         render_view_open(C, event->x, event->y);
332                 }
333         }
334
335         return OPERATOR_FINISHED;
336 }
337
338 void RENDER_OT_view_show(struct wmOperatorType *ot)
339 {
340         /* identifiers */
341         ot->name = "Show/Hide Render View";
342         ot->description = "Toggle show render view";
343         ot->idname = "RENDER_OT_view_show";
344
345         /* api callbacks */
346         ot->invoke = render_view_show_invoke;
347         ot->poll = ED_operator_screenactive;
348 }
349