2.5: Space Image ported back
[blender-staging.git] / source / blender / editors / space_image / image_render.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2009
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <stdlib.h>
29
30 #include "DNA_image_types.h"
31 #include "DNA_screen_types.h"
32 #include "DNA_space_types.h"
33
34 #include "BKE_context.h"
35 #include "BKE_global.h"
36 #include "BKE_image.h"
37
38 #include "BIF_gl.h"
39 #include "BIF_glutil.h"
40
41 #include "RE_pipeline.h"
42
43 #define HEADER_HEIGHT 18
44
45 /* *********************** render callbacks ***************** */
46
47 /* set on initialize render, only one render output to imagewindow can exist, so the global isnt dangerous yet :) */
48 static ScrArea *image_area= NULL;
49
50 /* can get as well the full picture, as the parts while rendering */
51 static void imagewindow_progress(ScrArea *sa, RenderResult *rr, volatile rcti *renrect)
52 {
53         SpaceImage *sima= sa->spacedata.first;
54         float x1, y1, *rectf= NULL;
55         unsigned int *rect32= NULL;
56         int ymin, ymax, xmin, xmax;
57         
58         /* if renrect argument, we only display scanlines */
59         if(renrect) {
60                 /* if ymax==recty, rendering of layer is ready, we should not draw, other things happen... */
61                 if(rr->renlay==NULL || renrect->ymax>=rr->recty)
62                         return;
63                 
64                 /* xmin here is first subrect x coord, xmax defines subrect width */
65                 xmin = renrect->xmin;
66                 xmax = renrect->xmax - xmin;
67                 if (xmax<2) return;
68                 
69                 ymin= renrect->ymin;
70                 ymax= renrect->ymax - ymin;
71                 if(ymax<2)
72                         return;
73                 renrect->ymin= renrect->ymax;
74         }
75         else {
76                 xmin = ymin = 0;
77                 xmax = rr->rectx - 2*rr->crop;
78                 ymax = rr->recty - 2*rr->crop;
79         }
80         
81         /* image window cruft */
82         
83         /* find current float rect for display, first case is after composit... still weak */
84         if(rr->rectf)
85                 rectf= rr->rectf;
86         else {
87                 if(rr->rect32)
88                         rect32= (unsigned int *)rr->rect32;
89                 else {
90                         if(rr->renlay==NULL || rr->renlay->rectf==NULL) return;
91                         rectf= rr->renlay->rectf;
92                 }
93         }
94         if(rectf) {
95                 /* if scanline updates... */
96                 rectf+= 4*(rr->rectx*ymin + xmin);
97                 
98                 /* when rendering more pixels than needed, we crop away cruft */
99                 if(rr->crop)
100                         rectf+= 4*(rr->crop*rr->rectx + rr->crop);
101         }
102         
103         /* tilerect defines drawing offset from (0,0) */
104         /* however, tilerect (xmin, ymin) is first pixel */
105         x1 = sima->centx + (rr->tilerect.xmin + rr->crop + xmin)*sima->zoom;
106         y1 = sima->centy + (rr->tilerect.ymin + rr->crop + ymin)*sima->zoom;
107         
108         /* needed for gla draw */
109         // XXX { rcti rct= ar->winrct; rct.ymax-= HEADER_HEIGHT; glaDefine2DArea(&rct);}
110
111         glPixelZoom(sima->zoom, sima->zoom);
112         
113         if(rect32)
114                 glaDrawPixelsSafe(x1, y1, xmax, ymax, rr->rectx, GL_RGBA, GL_UNSIGNED_BYTE, rect32);
115         else
116                 glaDrawPixelsSafe_to32(x1, y1, xmax, ymax, rr->rectx, rectf);
117         
118         glPixelZoom(1.0, 1.0);
119         
120 }
121
122 /* in render window; display a couple of scanlines of rendered image */
123 /* NOTE: called while render, so no malloc allowed! */
124 static void imagewindow_progress_display_cb(RenderResult *rr, volatile rcti *rect)
125 {
126         if (image_area) {
127                 imagewindow_progress(image_area, rr, rect);
128
129                 /* no screen_swapbuffers, prevent any other window to draw */
130                 // XXX myswapbuffers();
131         }
132 }
133
134 /* unused, init_display_cb is called on each render */
135 static void imagewindow_clear_display_cb(RenderResult *rr)
136 {
137         if (image_area) {
138         }
139 }
140
141 /* returns biggest area that is not uv/image editor. Note that it uses buttons */
142 /* window as the last possible alternative.                                                                        */
143 static ScrArea *biggest_non_image_area(bContext *C)
144 {
145         bScreen *sc= CTX_wm_screen(C);
146         ScrArea *sa, *big= NULL;
147         int size, maxsize= 0, bwmaxsize= 0;
148         short foundwin= 0;
149         
150         for(sa= sc->areabase.first; sa; sa= sa->next) {
151                 if(sa->winx > 10 && sa->winy > 10) {
152                         size= sa->winx*sa->winy;
153                         if(sa->spacetype == SPACE_BUTS) {
154                                 if(foundwin == 0 && size > bwmaxsize) {
155                                         bwmaxsize= size;
156                                         big= sa;        
157                                 }
158                         }
159                         else if(sa->spacetype != SPACE_IMAGE && size > maxsize) {
160                                 maxsize= size;
161                                 big= sa;
162                                 foundwin= 1;
163                         }
164                 }
165         }
166                 
167         return big;
168 }
169
170 static ScrArea *biggest_area(bContext *C)
171 {
172         bScreen *sc= CTX_wm_screen(C);
173         ScrArea *sa, *big= NULL;
174         int size, maxsize= 0;
175         
176         for(sa= sc->areabase.first; sa; sa= sa->next) {
177                 size= sa->winx*sa->winy;
178                 if(size > maxsize) {
179                         maxsize= size;
180                         big= sa;
181                 }
182         }
183         return big;
184 }
185
186
187 /* if R_DISPLAYIMAGE
188       use Image Window showing Render Result
189           else: turn largest non-image area into Image Window (not to frustrate texture or composite usage)
190           else: then we use Image Window anyway...
191    if R_DISPSCREEN
192       make a new temp fullscreen area with Image Window
193 */
194
195 static ScrArea *find_area_showing_r_result(bContext *C)
196 {
197         bScreen *sc= CTX_wm_screen(C);
198         ScrArea *sa;
199         SpaceImage *sima;
200         
201         /* find an imagewindow showing render result */
202         for(sa=sc->areabase.first; sa; sa= sa->next) {
203                 if(sa->spacetype==SPACE_IMAGE) {
204                         sima= sa->spacedata.first;
205                         if(sima->image && sima->image->type==IMA_TYPE_R_RESULT)
206                                 break;
207                 }
208         }
209         return sa;
210 }
211
212 static ScrArea *imagewindow_set_render_display(bContext *C)
213 {
214         ScrArea *sa;
215         SpaceImage *sima;
216         
217         sa= find_area_showing_r_result(C);
218         
219         if(sa==NULL) {
220                 /* find largest open non-image area */
221                 sa= biggest_non_image_area(C);
222                 if(sa) {
223                         // XXX newspace(sa, SPACE_IMAGE);
224                         sima= sa->spacedata.first;
225                         
226                         /* makes ESC go back to prev space */
227                         sima->flag |= SI_PREVSPACE;
228                 }
229                 else {
230                         /* use any area of decent size */
231                         sa= biggest_area(C);
232                         if(sa->spacetype!=SPACE_IMAGE) {
233                                 // XXX newspace(sa, SPACE_IMAGE);
234                                 sima= sa->spacedata.first;
235                                 
236                                 /* makes ESC go back to prev space */
237                                 sima->flag |= SI_PREVSPACE;
238                         }
239                 }
240         }
241         
242         sima= sa->spacedata.first;
243         
244         /* get the correct image, and scale it */
245         sima->image= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
246         
247         if(0) { // XXX G.displaymode==R_DISPLAYSCREEN) {
248                 if(sa->full==0) {
249                         sima->flag |= SI_FULLWINDOW;
250                         /* fullscreen works with lousy curarea */
251                         // XXX curarea= sa;
252                         // XXX area_fullscreen();
253                         // XXX sa= curarea;
254                 }
255         }
256         
257         return sa;
258 }
259
260 static void imagewindow_init_display_cb(RenderResult *rr)
261 {
262         bContext *C= NULL; // XXX
263
264         image_area= imagewindow_set_render_display(C);
265         
266         if(image_area) {
267                 SpaceImage *sima= image_area->spacedata.first;
268                 
269                 // XXX areawinset(image_area->win);
270                 
271                 /* calc location using original size (tiles don't tell) */
272                 sima->centx= (image_area->winx - sima->zoom*(float)rr->rectx)/2.0f;
273                 sima->centy= (image_area->winy - sima->zoom*(float)rr->recty)/2.0f;
274                 
275                 sima->centx-= sima->zoom*sima->xof;
276                 sima->centy-= sima->zoom*sima->yof;
277                 
278                 // XXX drawimagespace(image_area, sima);
279                 // XXX if(image_area->headertype) scrarea_do_headdraw(image_area);
280
281                 /* no screen_swapbuffers, prevent any other window to draw */
282                 // XXX myswapbuffers();
283                 
284                 // XXX allqueue(REDRAWIMAGE, 0);        /* redraw in end */
285         }
286 }
287
288 /* coming from BIF_toggle_render_display() */
289 void imagewindow_toggle_render(bContext *C)
290 {
291         bScreen *sc= CTX_wm_screen(C);
292         ScrArea *sa;
293         
294         /* check if any imagewindow is showing temporal render output */
295         for(sa=sc->areabase.first; sa; sa= sa->next) {
296                 if(sa->spacetype==SPACE_IMAGE) {
297                         SpaceImage *sima= sa->spacedata.first;
298                         
299                         if(sima->image && sima->image->type==IMA_TYPE_R_RESULT)
300                                 if(sima->flag & (SI_PREVSPACE|SI_FULLWINDOW))
301                                         break;
302                 }
303         }
304
305         if(sa) {
306                 // XXX addqueue(sa->win, ESCKEY, 1);    /* also returns from fullscreen */
307         }
308         else {
309                 sa= imagewindow_set_render_display(C);
310                 // XXX scrarea_queue_headredraw(sa);
311                 // XXX scrarea_queue_winredraw(sa);
312         }
313 }
314
315 /* NOTE: called while render, so no malloc allowed! */
316 static void imagewindow_renderinfo_cb(RenderStats *rs)
317 {
318         if(image_area) {
319                 // XXX BIF_make_render_text(rs);
320
321                 // XXX sima_draw_render_info(sima, ar);
322                 
323                 /* no screen_swapbuffers, prevent any other window to draw */
324                 // XXX myswapbuffers();
325         }
326 }
327
328 void imagewindow_render_callbacks(Render *re)
329 {
330         RE_display_init_cb(re, imagewindow_init_display_cb);
331         RE_display_draw_cb(re, imagewindow_progress_display_cb);
332         RE_display_clear_cb(re, imagewindow_clear_display_cb);
333         RE_stats_draw_cb(re, imagewindow_renderinfo_cb);        
334 }
335