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