Attempted fix for [#21491] rendering from the api does not work
[blender.git] / source / blender / editors / render / render_internal.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) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #include <math.h>
28 #include <string.h>
29 #include <stddef.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_blenlib.h"
34 #include "BLI_math.h"
35 #include "BLI_threads.h"
36
37 #include "DNA_scene_types.h"
38
39 #include "BKE_blender.h"
40 #include "BKE_colortools.h"
41 #include "BKE_context.h"
42 #include "BKE_global.h"
43 #include "BKE_image.h"
44 #include "BKE_library.h"
45 #include "BKE_main.h"
46 #include "BKE_multires.h"
47 #include "BKE_report.h"
48 #include "BKE_scene.h"
49 #include "BKE_screen.h"
50 #include "BKE_utildefines.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "ED_screen.h"
56 #include "ED_object.h"
57
58 #include "RE_pipeline.h"
59 #include "IMB_imbuf.h"
60 #include "IMB_imbuf_types.h"
61
62 #include "RNA_access.h"
63 #include "RNA_define.h"
64
65 #include "wm_window.h"
66
67 #include "render_intern.h"
68
69 static ScrArea *biggest_area(bContext *C);
70 static ScrArea *biggest_non_image_area(bContext *C);
71 static ScrArea *find_area_showing_r_result(bContext *C);
72 static ScrArea *find_area_image_empty(bContext *C);
73
74 /* called inside thread! */
75 void image_buffer_rect_update(Scene *scene, RenderResult *rr, ImBuf *ibuf, volatile rcti *renrect)
76 {
77         float x1, y1, *rectf= NULL;
78         int ymin, ymax, xmin, xmax;
79         int rymin, rxmin;
80         char *rectc;
81
82         /* if renrect argument, we only refresh scanlines */
83         if(renrect) {
84                 /* if ymax==recty, rendering of layer is ready, we should not draw, other things happen... */
85                 if(rr->renlay==NULL || renrect->ymax>=rr->recty)
86                         return;
87
88                 /* xmin here is first subrect x coord, xmax defines subrect width */
89                 xmin = renrect->xmin + rr->crop;
90                 xmax = renrect->xmax - xmin - rr->crop;
91                 if (xmax<2) return;
92
93                 ymin= renrect->ymin + rr->crop;
94                 ymax= renrect->ymax - ymin - rr->crop;
95                 if(ymax<2)
96                         return;
97                 renrect->ymin= renrect->ymax;
98
99         }
100         else {
101                 xmin = ymin = rr->crop;
102                 xmax = rr->rectx - 2*rr->crop;
103                 ymax = rr->recty - 2*rr->crop;
104         }
105
106         /* xmin ymin is in tile coords. transform to ibuf */
107         rxmin= rr->tilerect.xmin + xmin;
108         if(rxmin >= ibuf->x) return;
109         rymin= rr->tilerect.ymin + ymin;
110         if(rymin >= ibuf->y) return;
111
112         if(rxmin + xmax > ibuf->x)
113                 xmax= ibuf->x - rxmin;
114         if(rymin + ymax > ibuf->y)
115                 ymax= ibuf->y - rymin;
116
117         if(xmax < 1 || ymax < 1) return;
118
119         /* find current float rect for display, first case is after composit... still weak */
120         if(rr->rectf)
121                 rectf= rr->rectf;
122         else {
123                 if(rr->rect32)
124                         return;
125                 else {
126                         if(rr->renlay==NULL || rr->renlay->rectf==NULL) return;
127                         rectf= rr->renlay->rectf;
128                 }
129         }
130         if(rectf==NULL) return;
131
132         if(ibuf->rect==NULL)
133                 imb_addrectImBuf(ibuf);
134
135         rectf+= 4*(rr->rectx*ymin + xmin);
136         rectc= (char *)(ibuf->rect + ibuf->x*rymin + rxmin);
137
138         /* XXX make nice consistent functions for this */
139         if (scene && (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)) {
140                 for(y1= 0; y1<ymax; y1++) {
141                         float *rf= rectf;
142                         float srgb[3];
143                         char *rc= rectc;
144
145                         /* XXX temp. because crop offset */
146                         if( rectc >= (char *)(ibuf->rect)) {
147                                 for(x1= 0; x1<xmax; x1++, rf += 4, rc+=4) {
148                                         srgb[0]= linearrgb_to_srgb(rf[0]);
149                                         srgb[1]= linearrgb_to_srgb(rf[1]);
150                                         srgb[2]= linearrgb_to_srgb(rf[2]);
151
152                                         rc[0]= FTOCHAR(srgb[0]);
153                                         rc[1]= FTOCHAR(srgb[1]);
154                                         rc[2]= FTOCHAR(srgb[2]);
155                                         rc[3]= FTOCHAR(rf[3]);
156                                 }
157                         }
158                         rectf += 4*rr->rectx;
159                         rectc += 4*ibuf->x;
160                 }
161         } else {
162                 for(y1= 0; y1<ymax; y1++) {
163                         float *rf= rectf;
164                         char *rc= rectc;
165
166                         /* XXX temp. because crop offset */
167                         if( rectc >= (char *)(ibuf->rect)) {
168                                 for(x1= 0; x1<xmax; x1++, rf += 4, rc+=4) {
169                                         rc[0]= FTOCHAR(rf[0]);
170                                         rc[1]= FTOCHAR(rf[1]);
171                                         rc[2]= FTOCHAR(rf[2]);
172                                         rc[3]= FTOCHAR(rf[3]);
173                                 }
174                         }
175                         rectf += 4*rr->rectx;
176                         rectc += 4*ibuf->x;
177                 }
178         }
179 }
180
181 /* new window uses x,y to set position */
182 void screen_set_image_output(bContext *C, int mx, int my)
183 {
184         wmWindow *win= CTX_wm_window(C);
185         Scene *scene= CTX_data_scene(C);
186         ScrArea *sa= NULL;
187         SpaceImage *sima;
188         int area_was_image=0;
189
190         if(scene->r.displaymode==R_OUTPUT_WINDOW) {
191                 rcti rect;
192                 int sizex, sizey;
193
194                 sizex= 10 + (scene->r.xsch*scene->r.size)/100;
195                 sizey= 40 + (scene->r.ysch*scene->r.size)/100;
196
197                 /* arbitrary... miniature image window views don't make much sense */
198                 if(sizex < 320) sizex= 320;
199                 if(sizey < 256) sizey= 256;
200
201                 /* XXX some magic to calculate postition */
202                 rect.xmin= mx + win->posx - sizex/2;
203                 rect.ymin= my + win->posy - sizey/2;
204                 rect.xmax= rect.xmin + sizex;
205                 rect.ymax= rect.ymin + sizey;
206
207                 /* changes context! */
208                 WM_window_open_temp(C, &rect, WM_WINDOW_RENDER);
209
210                 sa= CTX_wm_area(C);
211         }
212         else if(scene->r.displaymode==R_OUTPUT_SCREEN) {
213                 if (CTX_wm_area(C)->spacetype == SPACE_IMAGE)
214                         area_was_image = 1;
215
216                 /* this function returns with changed context */
217                 ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_IMAGE);
218                 sa= CTX_wm_area(C);
219         }
220
221         if(!sa) {
222                 sa= find_area_showing_r_result(C);
223                 if(sa==NULL)
224                         sa= find_area_image_empty(C);
225
226                 if(sa==NULL) {
227                         /* find largest open non-image area */
228                         sa= biggest_non_image_area(C);
229                         if(sa) {
230                                 ED_area_newspace(C, sa, SPACE_IMAGE);
231                                 sima= sa->spacedata.first;
232
233                                 /* makes ESC go back to prev space */
234                                 sima->flag |= SI_PREVSPACE;
235                         }
236                         else {
237                                 /* use any area of decent size */
238                                 sa= biggest_area(C);
239                                 if(sa->spacetype!=SPACE_IMAGE) {
240                                         // XXX newspace(sa, SPACE_IMAGE);
241                                         sima= sa->spacedata.first;
242
243                                         /* makes ESC go back to prev space */
244                                         sima->flag |= SI_PREVSPACE;
245                                 }
246                         }
247                 }
248         }
249         sima= sa->spacedata.first;
250
251         /* get the correct image, and scale it */
252         sima->image= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
253
254
255         /* if we're rendering to full screen, set appropriate hints on image editor
256          * so it can restore properly on pressing esc */
257         if(sa->full) {
258                 sima->flag |= SI_FULLWINDOW;
259
260                 /* Tell the image editor to revert to previous space in space list on close
261                  * _only_ if it wasn't already an image editor when the render was invoked */
262                 if (area_was_image == 0)
263                         sima->flag |= SI_PREVSPACE;
264                 else {
265                         /* Leave it alone so the image editor will just go back from
266                          * full screen to the original tiled setup */
267                         ;
268                 }
269
270         }
271
272 }
273
274
275 /* ****************************** render invoking ***************** */
276
277 /* set callbacks, exported to sequence render too.
278  Only call in foreground (UI) renders. */
279
280 /* returns biggest area that is not uv/image editor. Note that it uses buttons */
281 /* window as the last possible alternative.                                                                        */
282 static ScrArea *biggest_non_image_area(bContext *C)
283 {
284         bScreen *sc= CTX_wm_screen(C);
285         ScrArea *sa, *big= NULL;
286         int size, maxsize= 0, bwmaxsize= 0;
287         short foundwin= 0;
288
289         for(sa= sc->areabase.first; sa; sa= sa->next) {
290                 if(sa->winx > 30 && sa->winy > 30) {
291                         size= sa->winx*sa->winy;
292                         if(sa->spacetype == SPACE_BUTS) {
293                                 if(foundwin == 0 && size > bwmaxsize) {
294                                         bwmaxsize= size;
295                                         big= sa;
296                                 }
297                         }
298                         else if(sa->spacetype != SPACE_IMAGE && size > maxsize) {
299                                 maxsize= size;
300                                 big= sa;
301                                 foundwin= 1;
302                         }
303                 }
304         }
305
306         return big;
307 }
308
309 static ScrArea *biggest_area(bContext *C)
310 {
311         bScreen *sc= CTX_wm_screen(C);
312         ScrArea *sa, *big= NULL;
313         int size, maxsize= 0;
314
315         for(sa= sc->areabase.first; sa; sa= sa->next) {
316                 size= sa->winx*sa->winy;
317                 if(size > maxsize) {
318                         maxsize= size;
319                         big= sa;
320                 }
321         }
322         return big;
323 }
324
325
326 static ScrArea *find_area_showing_r_result(bContext *C)
327 {
328         wmWindowManager *wm= CTX_wm_manager(C);
329         wmWindow *win;
330         ScrArea *sa = NULL;
331         SpaceImage *sima;
332
333         /* find an imagewindow showing render result */
334         for(win=wm->windows.first; win; win=win->next) {
335                 for(sa=win->screen->areabase.first; sa; sa= sa->next) {
336                         if(sa->spacetype==SPACE_IMAGE) {
337                                 sima= sa->spacedata.first;
338                                 if(sima->image && sima->image->type==IMA_TYPE_R_RESULT)
339                                         break;
340                         }
341                 }
342         }
343
344         return sa;
345 }
346
347 static ScrArea *find_area_image_empty(bContext *C)
348 {
349         bScreen *sc= CTX_wm_screen(C);
350         ScrArea *sa;
351         SpaceImage *sima;
352
353         /* find an imagewindow showing render result */
354         for(sa=sc->areabase.first; sa; sa= sa->next) {
355                 if(sa->spacetype==SPACE_IMAGE) {
356                         sima= sa->spacedata.first;
357                         if(!sima->image)
358                                 break;
359                 }
360         }
361         return sa;
362 }
363
364 #if 0 // XXX not used
365 static ScrArea *find_empty_image_area(bContext *C)
366 {
367         bScreen *sc= CTX_wm_screen(C);
368         ScrArea *sa;
369         SpaceImage *sima;
370
371         /* find an imagewindow showing render result */
372         for(sa=sc->areabase.first; sa; sa= sa->next) {
373                 if(sa->spacetype==SPACE_IMAGE) {
374                         sima= sa->spacedata.first;
375                         if(!sima->image)
376                                 break;
377                 }
378         }
379         return sa;
380 }
381 #endif // XXX not used
382
383 static void render_error_reports(void *reports, char *str)
384 {
385         BKE_report(reports, RPT_ERROR, str);
386 }
387
388 static void result_nothing(void *unused, RenderResult *rr) {}
389 static void result_rcti_nothing(void *unused, RenderResult *rr, volatile struct rcti *rect) {}
390 static void stats_nothing(void *unused, RenderStats *rs) {}
391
392 /* executes blocking render */
393 static int screen_render_exec(bContext *C, wmOperator *op)
394 {
395         Scene *scene= CTX_data_scene(C);
396         Render *re= RE_GetRender(scene->id.name);
397         Image *ima;
398         View3D *v3d= CTX_wm_view3d(C);
399         int lay= (v3d)? v3d->lay|scene->lay: scene->lay;
400
401         if(re==NULL) {
402                 re= RE_NewRender(scene->id.name);
403         }
404         
405         G.afbreek= 0;
406         RE_test_break_cb(re, NULL, (int (*)(void *)) blender_test_break);
407         RE_error_cb(re, op->reports, render_error_reports);
408
409         /* clear other callbacks that may have previosuly been used by interactive render */
410         RE_display_init_cb(re, NULL, result_nothing);
411         RE_display_clear_cb(re, NULL, result_nothing);
412         RE_display_draw_cb(re, NULL, result_rcti_nothing);
413         RE_stats_draw_cb(re, NULL, stats_nothing);
414
415         ima= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
416         BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
417         BKE_image_backup_render(scene, ima);
418
419         if(RNA_boolean_get(op->ptr, "animation"))
420                 RE_BlenderAnim(re, scene, lay, scene->r.sfra, scene->r.efra, scene->r.frame_step, op->reports);
421         else
422                 RE_BlenderFrame(re, scene, NULL, lay, scene->r.cfra);
423
424         // no redraw needed, we leave state as we entered it
425         ED_update_for_newframe(C, 1);
426
427         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, scene);
428
429         return OPERATOR_FINISHED;
430 }
431
432 typedef struct RenderJob {
433         Scene *scene;
434         Render *re;
435         wmWindow *win;
436         SceneRenderLayer *srl;
437         int lay;
438         int anim;
439         Image *image;
440         ImageUser iuser;
441         short *stop;
442         short *do_update;
443         ReportList *reports;
444 } RenderJob;
445
446 static void render_freejob(void *rjv)
447 {
448         RenderJob *rj= rjv;
449
450         MEM_freeN(rj);
451 }
452
453 /* str is IMA_MAX_RENDER_TEXT in size */
454 static void make_renderinfo_string(RenderStats *rs, Scene *scene, char *str)
455 {
456         char info_time_str[32]; // used to be extern to header_info.c
457         uintptr_t mem_in_use, mmap_in_use;
458         float megs_used_memory, mmap_used_memory;
459         char *spos= str;
460
461         mem_in_use= MEM_get_memory_in_use();
462         mmap_in_use= MEM_get_mapped_memory_in_use();
463
464         megs_used_memory= (mem_in_use-mmap_in_use)/(1024.0*1024.0);
465         mmap_used_memory= (mmap_in_use)/(1024.0*1024.0);
466
467         if(scene->lay & 0xFF000000)
468                 spos+= sprintf(spos, "Localview | ");
469         else if(scene->r.scemode & R_SINGLE_LAYER)
470                 spos+= sprintf(spos, "Single Layer | ");
471
472         if(rs->statstr) {
473                 spos+= sprintf(spos, "%s ", rs->statstr);
474         }
475         else {
476                 spos+= sprintf(spos, "Fra:%d  Ve:%d Fa:%d ", (scene->r.cfra), rs->totvert, rs->totface);
477                 if(rs->tothalo) spos+= sprintf(spos, "Ha:%d ", rs->tothalo);
478                 if(rs->totstrand) spos+= sprintf(spos, "St:%d ", rs->totstrand);
479                 spos+= sprintf(spos, "La:%d Mem:%.2fM (%.2fM) ", rs->totlamp, megs_used_memory, mmap_used_memory);
480
481                 if(rs->curfield)
482                         spos+= sprintf(spos, "Field %d ", rs->curfield);
483                 if(rs->curblur)
484                         spos+= sprintf(spos, "Blur %d ", rs->curblur);
485         }
486
487         BLI_timestr(rs->lastframetime, info_time_str);
488         spos+= sprintf(spos, "Time:%s ", info_time_str);
489
490         if(rs->infostr && rs->infostr[0])
491                 spos+= sprintf(spos, "| %s ", rs->infostr);
492
493         /* very weak... but 512 characters is quite safe */
494         if(spos >= str+IMA_MAX_RENDER_TEXT)
495                 if (G.f & G_DEBUG)
496                         printf("WARNING! renderwin text beyond limit \n");
497
498 }
499
500 static void image_renderinfo_cb(void *rjv, RenderStats *rs)
501 {
502         RenderJob *rj= rjv;
503         RenderResult *rr;
504
505         rr= RE_AcquireResultRead(rj->re);
506
507         /* malloc OK here, stats_draw is not in tile threads */
508         if(rr->text==NULL)
509                 rr->text= MEM_callocN(IMA_MAX_RENDER_TEXT, "rendertext");
510
511         make_renderinfo_string(rs, rj->scene, rr->text);
512         RE_ReleaseResult(rj->re);
513
514         /* make jobs timer to send notifier */
515         *(rj->do_update)= 1;
516
517 }
518
519 static void image_rect_update(void *rjv, RenderResult *rr, volatile rcti *renrect)
520 {
521         RenderJob *rj= rjv;
522         Image *ima= rj->image;
523         ImBuf *ibuf;
524         void *lock;
525
526         /* only update if we are displaying the slot being rendered */
527         if(ima->render_slot != ima->last_render_slot)
528                 return;
529
530         ibuf= BKE_image_acquire_ibuf(ima, &rj->iuser, &lock);
531         if(ibuf) {
532                 image_buffer_rect_update(rj->scene, rr, ibuf, renrect);
533
534                 /* make jobs timer to send notifier */
535                 *(rj->do_update)= 1;
536         }
537         BKE_image_release_ibuf(ima, lock);
538 }
539
540 static void render_startjob(void *rjv, short *stop, short *do_update)
541 {
542         RenderJob *rj= rjv;
543 //      Main *mainp= BKE_undo_get_main(&rj->scene);
544
545         rj->stop= stop;
546         rj->do_update= do_update;
547
548         if(rj->anim)
549                 RE_BlenderAnim(rj->re, rj->scene, rj->lay, rj->scene->r.sfra, rj->scene->r.efra, rj->scene->r.frame_step, rj->reports);
550         else
551                 RE_BlenderFrame(rj->re, rj->scene, rj->srl, rj->lay, rj->scene->r.cfra);
552
553 //      if(mainp)
554 //              free_main(mainp);
555 }
556
557 /* called by render, check job 'stop' value or the global */
558 static int render_breakjob(void *rjv)
559 {
560         RenderJob *rj= rjv;
561
562         if(G.afbreek)
563                 return 1;
564         if(rj->stop && *(rj->stop))
565                 return 1;
566         return 0;
567 }
568
569 /* catch esc */
570 static int screen_render_modal(bContext *C, wmOperator *op, wmEvent *event)
571 {
572         /* no running blender, remove handler and pass through */
573         if(0==WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C)))
574                 return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
575
576         /* running render */
577         switch (event->type) {
578                 case ESCKEY:
579                         return OPERATOR_RUNNING_MODAL;
580                         break;
581         }
582         return OPERATOR_PASS_THROUGH;
583 }
584
585 /* using context, starts job */
586 static int screen_render_invoke(bContext *C, wmOperator *op, wmEvent *event)
587 {
588         /* new render clears all callbacks */
589         Scene *scene= CTX_data_scene(C);
590         SceneRenderLayer *srl=NULL;
591         bScreen *screen= CTX_wm_screen(C);
592         View3D *v3d= CTX_wm_view3d(C);
593         Render *re;
594         wmJob *steve;
595         RenderJob *rj;
596         Image *ima;
597
598         /* only one render job at a time */
599         if(WM_jobs_test(CTX_wm_manager(C), scene))
600                 return OPERATOR_CANCELLED;
601
602         /* stop all running jobs, currently previews frustrate Render */
603         WM_jobs_stop_all(CTX_wm_manager(C));
604
605         /* cancel animation playback */
606         if (screen->animtimer)
607                 ED_screen_animation_play(C, 0, 0);
608         
609         /* handle UI stuff */
610         WM_cursor_wait(1);
611
612         /* flush multires changes (for sculpt) */
613         multires_force_render_update(CTX_data_active_object(C));
614
615         /* get editmode results */
616         ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);     /* 0 = does not exit editmode */
617
618         // store spare
619         // get view3d layer, local layer, make this nice api call to render
620         // store spare
621
622         /* ensure at least 1 area shows result */
623         screen_set_image_output(C, event->x, event->y);
624
625         /* single layer re-render */
626         if(RNA_property_is_set(op->ptr, "layer")) {
627                 SceneRenderLayer *rl;
628                 Scene *scn;
629                 char scene_name[19], rl_name[RE_MAXNAME];
630
631                 RNA_string_get(op->ptr, "layer", rl_name);
632                 RNA_string_get(op->ptr, "scene", scene_name);
633
634                 scn = (Scene *)BLI_findstring(&CTX_data_main(C)->scene, scene_name, offsetof(ID, name) + 2);
635                 rl = (SceneRenderLayer *)BLI_findstring(&scene->r.layers, rl_name, offsetof(SceneRenderLayer, name));
636
637                 if (scn && rl) {
638                         scene = scn;
639                         srl = rl;
640                 }
641         }
642
643         /* job custom data */
644         rj= MEM_callocN(sizeof(RenderJob), "render job");
645         rj->scene= scene;
646         rj->win= CTX_wm_window(C);
647         rj->srl = srl;
648         rj->lay = (v3d)? v3d->lay|scene->lay: scene->lay;
649         rj->anim= RNA_boolean_get(op->ptr, "animation");
650         rj->iuser.scene= scene;
651         rj->iuser.ok= 1;
652         rj->reports= op->reports;
653
654         /* setup job */
655         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, WM_JOB_EXCL_RENDER|WM_JOB_PRIORITY);
656         WM_jobs_customdata(steve, rj, render_freejob);
657         WM_jobs_timer(steve, 0.2, NC_SCENE|ND_RENDER_RESULT, 0);
658         WM_jobs_callbacks(steve, render_startjob, NULL, NULL);
659
660         /* get a render result image, and make sure it is empty */
661         ima= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
662         BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
663         BKE_image_backup_render(rj->scene, ima);
664         rj->image= ima;
665
666         /* setup new render */
667         re= RE_NewRender(scene->id.name);
668         RE_test_break_cb(re, rj, render_breakjob);
669         RE_display_draw_cb(re, rj, image_rect_update);
670         RE_stats_draw_cb(re, rj, image_renderinfo_cb);
671
672         rj->re= re;
673         G.afbreek= 0;
674
675         RE_error_cb(re, op->reports, render_error_reports);
676
677         WM_jobs_start(CTX_wm_manager(C), steve);
678
679         WM_cursor_wait(0);
680         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, scene);
681
682         /* add modal handler for ESC */
683         WM_event_add_modal_handler(C, op);
684
685         return OPERATOR_RUNNING_MODAL;
686 }
687
688
689 /* contextual render, using current scene, view3d? */
690 void RENDER_OT_render(wmOperatorType *ot)
691 {
692         /* identifiers */
693         ot->name= "Render";
694         ot->description= "Render active scene";
695         ot->idname= "RENDER_OT_render";
696
697         /* api callbacks */
698         ot->invoke= screen_render_invoke;
699         ot->modal= screen_render_modal;
700         ot->exec= screen_render_exec;
701
702         ot->poll= ED_operator_screenactive;
703
704         RNA_def_boolean(ot->srna, "animation", 0, "Animation", "");
705         RNA_def_string(ot->srna, "layer", "", RE_MAXNAME, "Render Layer", "Single render layer to re-render");
706         RNA_def_string(ot->srna, "scene", "", 19, "Scene", "Re-render single layer in this scene");
707 }
708
709 /* ****************************** opengl render *************************** */
710
711
712 /* *********************** cancel render viewer *************** */
713
714 static int render_view_cancel_exec(bContext *C, wmOperator *unused)
715 {
716         wmWindow *win= CTX_wm_window(C);
717         ScrArea *sa= CTX_wm_area(C);
718         SpaceImage *sima= sa->spacedata.first;
719
720         /* test if we have a temp screen in front */
721         if(CTX_wm_window(C)->screen->full==SCREENTEMP) {
722                 wm_window_lower(CTX_wm_window(C));
723                 return OPERATOR_FINISHED;
724         }
725         /* determine if render already shows */
726         else if(sima->flag & SI_PREVSPACE) {
727                 sima->flag &= ~SI_PREVSPACE;
728
729                 if(sima->flag & SI_FULLWINDOW) {
730                         sima->flag &= ~SI_FULLWINDOW;
731                         ED_screen_full_prevspace(C, sa);
732                 }
733                 else
734                         ED_area_prevspace(C, sa);
735
736                 return OPERATOR_FINISHED;
737         }
738         else if(sima->flag & SI_FULLWINDOW) {
739                 sima->flag &= ~SI_FULLWINDOW;
740                 ED_screen_full_toggle(C, win, sa);
741                 return OPERATOR_FINISHED;
742         }
743
744         return OPERATOR_PASS_THROUGH;
745 }
746
747 void RENDER_OT_view_cancel(struct wmOperatorType *ot)
748 {
749         /* identifiers */
750         ot->name= "Cancel Render View";
751         ot->description= "Cancel show render view";
752         ot->idname= "RENDER_OT_view_cancel";
753
754         /* api callbacks */
755         ot->exec= render_view_cancel_exec;
756         ot->poll= ED_operator_image_active;
757 }
758
759 /* *********************** show render viewer *************** */
760
761 static int render_view_show_invoke(bContext *C, wmOperator *unused, wmEvent *event)
762 {
763         ScrArea *sa= find_area_showing_r_result(C);
764
765         /* test if we have a temp screen in front */
766         if(CTX_wm_window(C)->screen->full==SCREENTEMP) {
767                 wm_window_lower(CTX_wm_window(C));
768         }
769         /* determine if render already shows */
770         else if(sa) {
771                 SpaceImage *sima= sa->spacedata.first;
772
773                 if(sima->flag & SI_PREVSPACE) {
774                         sima->flag &= ~SI_PREVSPACE;
775
776                         if(sima->flag & SI_FULLWINDOW) {
777                                 sima->flag &= ~SI_FULLWINDOW;
778                                 ED_screen_full_prevspace(C, sa);
779                         }
780                         else if(sima->next) {
781                                 ED_area_newspace(C, sa, sima->next->spacetype);
782                                 ED_area_tag_redraw(sa);
783                         }
784                 }
785         }
786         else {
787                 screen_set_image_output(C, event->x, event->y);
788         }
789
790         return OPERATOR_FINISHED;
791 }
792
793 void RENDER_OT_view_show(struct wmOperatorType *ot)
794 {
795         /* identifiers */
796         ot->name= "Show/Hide Render View";
797         ot->description= "Toggle show render view";
798         ot->idname= "RENDER_OT_view_show";
799
800         /* api callbacks */
801         ot->invoke= render_view_show_invoke;
802         ot->poll= ED_operator_screenactive;
803 }