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