2.5
[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 #include <stdio.h>
30
31 #include "DNA_image_types.h"
32 #include "DNA_screen_types.h"
33 #include "DNA_space_types.h"
34
35 #include "BKE_context.h"
36 #include "BKE_global.h"
37 #include "BKE_image.h"
38 #include "BKE_screen.h"
39
40 #include "BIF_gl.h"
41 #include "BIF_glutil.h"
42
43 #include "ED_screen.h"
44
45 #include "RE_pipeline.h"
46
47 #include "WM_api.h"
48
49 #define HEADER_HEIGHT 18
50
51 /* *********************** render callbacks ***************** */
52
53 /* set on initialize render, only one render output to imagewindow can exist, so the global isnt dangerous yet :) */
54 static ScrArea *image_area= NULL;
55
56 /* can get as well the full picture, as the parts while rendering */
57 /* XXX will be obsolete, here for reference now */
58 void imagewindow_progress(SpaceImage *sima, RenderResult *rr, volatile rcti *renrect)
59 {
60         float x1, y1, *rectf= NULL;
61         unsigned int *rect32= NULL;
62         int ymin, ymax, xmin, xmax;
63         
64         /* if renrect argument, we only display scanlines */
65         if(renrect) {
66                 /* if ymax==recty, rendering of layer is ready, we should not draw, other things happen... */
67                 if(rr->renlay==NULL || renrect->ymax>=rr->recty)
68                         return;
69                 
70                 /* xmin here is first subrect x coord, xmax defines subrect width */
71                 xmin = renrect->xmin;
72                 xmax = renrect->xmax - xmin;
73                 if (xmax<2) return;
74                 
75                 ymin= renrect->ymin;
76                 ymax= renrect->ymax - ymin;
77                 if(ymax<2)
78                         return;
79                 renrect->ymin= renrect->ymax;
80         }
81         else {
82                 xmin = ymin = 0;
83                 xmax = rr->rectx - 2*rr->crop;
84                 ymax = rr->recty - 2*rr->crop;
85         }
86         
87         /* image window cruft */
88         
89         /* find current float rect for display, first case is after composit... still weak */
90         if(rr->rectf)
91                 rectf= rr->rectf;
92         else {
93                 if(rr->rect32)
94                         rect32= (unsigned int *)rr->rect32;
95                 else {
96                         if(rr->renlay==NULL || rr->renlay->rectf==NULL) return;
97                         rectf= rr->renlay->rectf;
98                 }
99         }
100         if(rectf) {
101                 /* if scanline updates... */
102                 rectf+= 4*(rr->rectx*ymin + xmin);
103                 
104                 /* when rendering more pixels than needed, we crop away cruft */
105                 if(rr->crop)
106                         rectf+= 4*(rr->crop*rr->rectx + rr->crop);
107         }
108         
109         /* tilerect defines drawing offset from (0,0) */
110         /* however, tilerect (xmin, ymin) is first pixel */
111         x1 = sima->centx + (rr->tilerect.xmin + rr->crop + xmin)*sima->zoom;
112         y1 = sima->centy + (rr->tilerect.ymin + rr->crop + ymin)*sima->zoom;
113         
114         /* needed for gla draw */
115         // XXX { rcti rct= ar->winrct; rct.ymax-= HEADER_HEIGHT; glaDefine2DArea(&rct);}
116
117         glPixelZoom(sima->zoom, sima->zoom);
118         
119         if(rect32)
120                 glaDrawPixelsSafe(x1, y1, xmax, ymax, rr->rectx, GL_RGBA, GL_UNSIGNED_BYTE, rect32);
121         else
122                 glaDrawPixelsSafe_to32(x1, y1, xmax, ymax, rr->rectx, rectf);
123         
124         glPixelZoom(1.0, 1.0);
125         
126 }
127
128
129 /* coming from BIF_toggle_render_display() */
130 void imagewindow_toggle_render(bContext *C)
131 {
132         bScreen *sc= CTX_wm_screen(C);
133         ScrArea *sa;
134         
135         /* check if any imagewindow is showing temporal render output */
136         for(sa=sc->areabase.first; sa; sa= sa->next) {
137                 if(sa->spacetype==SPACE_IMAGE) {
138                         SpaceImage *sima= sa->spacedata.first;
139                         
140                         if(sima->image && sima->image->type==IMA_TYPE_R_RESULT)
141                                 if(sima->flag & (SI_PREVSPACE|SI_FULLWINDOW))
142                                         break;
143                 }
144         }
145
146         if(sa) {
147                 // XXX addqueue(sa->win, ESCKEY, 1);    /* also returns from fullscreen */
148         }
149         else {
150 //              sa= imagewindow_set_render_display(C);
151                 // XXX scrarea_queue_headredraw(sa);
152                 // XXX scrarea_queue_winredraw(sa);
153         }
154 }
155
156 /* NOTE: called while render, so no malloc allowed! */
157 static void imagewindow_renderinfo_cb(void *handle, RenderStats *rs)
158 {
159         if(image_area) {
160                 // XXX BIF_make_render_text(rs);
161
162                 // XXX sima_draw_render_info(sima, ar);
163                 
164                 /* no screen_swapbuffers, prevent any other window to draw */
165                 // XXX myswapbuffers();
166         }
167 }
168
169 void ED_space_image_render_callbacks(bContext *C, Render *re)
170 {
171         
172 //      RE_display_init_cb(re, C, imagewindow_init_display_cb);
173 //      RE_display_draw_cb(re, C, imagewindow_progress_display_cb);
174 //      RE_display_clear_cb(re, C, imagewindow_clear_display_cb);
175         RE_stats_draw_cb(re, C, imagewindow_renderinfo_cb);     
176 }
177