Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / render / render_opengl.c
index 1e5fdf7b0d3dcbdda53bcf0ad9402afe63b2259e..21a7ec0d06ce101d23735056e5c031ce257a80b5 100644 (file)
 #include "BLI_utildefines.h"
 #include "BLI_jitter.h"
 #include "BLI_threads.h"
 #include "BLI_utildefines.h"
 #include "BLI_jitter.h"
 #include "BLI_threads.h"
+#include "BLI_task.h"
 
 #include "DNA_scene_types.h"
 #include "DNA_object_types.h"
 
 #include "DNA_scene_types.h"
 #include "DNA_object_types.h"
+#include "DNA_gpencil_types.h"
 
 #include "BKE_camera.h"
 #include "BKE_context.h"
 
 #include "BKE_camera.h"
 #include "BKE_context.h"
 #include "RNA_access.h"
 #include "RNA_define.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
 
-#include "GPU_extensions.h"
 #include "GPU_glew.h"
 #include "GPU_compositing.h"
 #include "GPU_glew.h"
 #include "GPU_compositing.h"
-
+#include "GPU_framebuffer.h"
 
 #include "render_intern.h"
 
 
 #include "render_intern.h"
 
+/* Define this to get timing information. */
+// #undef DEBUG_TIME
+
+#ifdef DEBUG_TIME
+#  include "PIL_time.h"
+#endif
+
+// TODO(sergey): Find better approximation of the scheduled frames.
+// For really highres renders it might fail still.
+#define MAX_SCHEDULED_FRAMES 8
+
 typedef struct OGLRender {
        Main *bmain;
        Render *re;
        Scene *scene;
 typedef struct OGLRender {
        Main *bmain;
        Render *re;
        Scene *scene;
+       SceneLayer *scene_layer;
 
        View3D *v3d;
        RegionView3D *rv3d;
 
        View3D *v3d;
        RegionView3D *rv3d;
@@ -88,8 +101,13 @@ typedef struct OGLRender {
        ScrArea *prevsa;
        ARegion *prevar;
 
        ScrArea *prevsa;
        ARegion *prevar;
 
+       int views_len;  /* multi-view views */
+
        bool is_sequencer;
        SpaceSeq *sseq;
        bool is_sequencer;
        SpaceSeq *sseq;
+       struct {
+               ImBuf **ibufs_arr;
+       } seq_data;
 
 
        Image *ima;
 
 
        Image *ima;
@@ -117,6 +135,19 @@ typedef struct OGLRender {
 
        wmTimer *timer; /* use to check if running modal or not (invoke'd or exec'd)*/
        void **movie_ctx_arr;
 
        wmTimer *timer; /* use to check if running modal or not (invoke'd or exec'd)*/
        void **movie_ctx_arr;
+
+       TaskScheduler *task_scheduler;
+       TaskPool *task_pool;
+       bool pool_ok;
+       bool is_animation;
+       SpinLock reports_lock;
+       unsigned int num_scheduled_frames;
+       ThreadMutex task_mutex;
+       ThreadCondition task_condition;
+
+#ifdef DEBUG_TIME
+       double time_start;
+#endif
 } OGLRender;
 
 /* added because v3d is not always valid */
 } OGLRender;
 
 /* added because v3d is not always valid */
@@ -176,6 +207,9 @@ static void screen_opengl_views_setup(OGLRender *oglrender)
                        if (rv_del->rectz)
                                MEM_freeN(rv_del->rectz);
 
                        if (rv_del->rectz)
                                MEM_freeN(rv_del->rectz);
 
+                       if (rv_del->rect32)
+                               MEM_freeN(rv_del->rect32);
+
                        MEM_freeN(rv_del);
                }
        }
                        MEM_freeN(rv_del);
                }
        }
@@ -188,8 +222,6 @@ static void screen_opengl_views_setup(OGLRender *oglrender)
                while (rv) {
                        srv = BLI_findstring(&rd->views, rv->name, offsetof(SceneRenderView, name));
                        if (BKE_scene_multiview_is_render_view_active(rd, srv)) {
                while (rv) {
                        srv = BLI_findstring(&rd->views, rv->name, offsetof(SceneRenderView, name));
                        if (BKE_scene_multiview_is_render_view_active(rd, srv)) {
-                               if (rv->rectf == NULL)
-                                       rv->rectf = MEM_callocN(sizeof(float) * 4 * oglrender->sizex * oglrender->sizey, "screen_opengl_render_init rect");
                                rv = rv->prev;
                        }
                        else {
                                rv = rv->prev;
                        }
                        else {
@@ -204,6 +236,9 @@ static void screen_opengl_views_setup(OGLRender *oglrender)
                                if (rv_del->rectz)
                                        MEM_freeN(rv_del->rectz);
 
                                if (rv_del->rectz)
                                        MEM_freeN(rv_del->rectz);
 
+                               if (rv_del->rect32)
+                                       MEM_freeN(rv_del->rect32);
+
                                MEM_freeN(rv_del);
                        }
                }
                                MEM_freeN(rv_del);
                        }
                }
@@ -223,12 +258,6 @@ static void screen_opengl_views_setup(OGLRender *oglrender)
                }
        }
 
                }
        }
 
-       for (rv = rr->views.first; rv; rv = rv->next) {
-               if (rv->rectf == NULL) {
-                       rv->rectf = MEM_callocN(sizeof(float) * 4 * oglrender->sizex * oglrender->sizey, "screen_opengl_render_init rect");
-               }
-       }
-
        BLI_lock_thread(LOCK_DRAW_IMAGE);
        if (!(is_multiview && BKE_scene_multiview_is_stereo3d(rd)))
                oglrender->iuser.flag &= ~IMA_SHOW_STEREO;
        BLI_lock_thread(LOCK_DRAW_IMAGE);
        if (!(is_multiview && BKE_scene_multiview_is_stereo3d(rd)))
                oglrender->iuser.flag &= ~IMA_SHOW_STEREO;
@@ -249,11 +278,11 @@ static void screen_opengl_views_setup(OGLRender *oglrender)
 static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr)
 {
        Scene *scene = oglrender->scene;
 static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr)
 {
        Scene *scene = oglrender->scene;
+       SceneLayer *sl = oglrender->scene_layer;
        ARegion *ar = oglrender->ar;
        View3D *v3d = oglrender->v3d;
        RegionView3D *rv3d = oglrender->rv3d;
        Object *camera = NULL;
        ARegion *ar = oglrender->ar;
        View3D *v3d = oglrender->v3d;
        RegionView3D *rv3d = oglrender->rv3d;
        Object *camera = NULL;
-       ImBuf *ibuf;
        int sizex = oglrender->sizex;
        int sizey = oglrender->sizey;
        const short view_context = (v3d != NULL);
        int sizex = oglrender->sizex;
        int sizey = oglrender->sizey;
        const short view_context = (v3d != NULL);
@@ -261,24 +290,14 @@ static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr)
        bool draw_sky = (scene->r.alphamode == R_ADDSKY);
        unsigned char *rect = NULL;
        const char *viewname = RE_GetActiveRenderView(oglrender->re);
        bool draw_sky = (scene->r.alphamode == R_ADDSKY);
        unsigned char *rect = NULL;
        const char *viewname = RE_GetActiveRenderView(oglrender->re);
+       ImBuf *ibuf_result = NULL;
 
        if (oglrender->is_sequencer) {
 
        if (oglrender->is_sequencer) {
-               SeqRenderData context;
                SpaceSeq *sseq = oglrender->sseq;
                SpaceSeq *sseq = oglrender->sseq;
-               int chanshown = sseq ? sseq->chanshown : 0;
                struct bGPdata *gpd = (sseq && (sseq->flag & SEQ_SHOW_GPENCIL)) ? sseq->gpd : NULL;
 
                struct bGPdata *gpd = (sseq && (sseq->flag & SEQ_SHOW_GPENCIL)) ? sseq->gpd : NULL;
 
-               BKE_sequencer_new_render_data(
-                       oglrender->bmain->eval_ctx, oglrender->bmain, scene,
-                       oglrender->sizex, oglrender->sizey, 100.0f,
-                       &context);
-
-               context.view_id = BKE_scene_multiview_view_id_get(&scene->r, viewname);
-               context.gpu_offscreen = oglrender->ofs;
-               context.gpu_fx = oglrender->fx;
-               context.gpu_full_samples = oglrender->ofs_full_samples;
-
-               ibuf = BKE_sequencer_give_ibuf(&context, CFRA, chanshown);
+               /* use pre-calculated ImBuf (avoids deadlock), see: */
+               ImBuf *ibuf = oglrender->seq_data.ibufs_arr[oglrender->view_id];
 
                if (ibuf) {
                        ImBuf *out = IMB_dupImBuf(ibuf);
 
                if (ibuf) {
                        ImBuf *out = IMB_dupImBuf(ibuf);
@@ -298,11 +317,17 @@ static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr)
                        RE_render_result_rect_from_ibuf(rr, &scene->r, out, oglrender->view_id);
                        IMB_freeImBuf(out);
                }
                        RE_render_result_rect_from_ibuf(rr, &scene->r, out, oglrender->view_id);
                        IMB_freeImBuf(out);
                }
+               else if (gpd) {
+                       /* If there are no strips, Grease Pencil still needs a buffer to draw on */
+                       ImBuf *out = IMB_allocImBuf(oglrender->sizex, oglrender->sizey, 32, IB_rect);
+                       RE_render_result_rect_from_ibuf(rr, &scene->r, out, oglrender->view_id);
+                       IMB_freeImBuf(out);
+               }
 
                if (gpd) {
                        int i;
                        unsigned char *gp_rect;
 
                if (gpd) {
                        int i;
                        unsigned char *gp_rect;
-                       unsigned char *rect = (unsigned char *)RE_RenderViewGetById(rr, oglrender->view_id)->rect32;
+                       unsigned char *render_rect = (unsigned char *)RE_RenderViewGetById(rr, oglrender->view_id)->rect32;
 
                        GPU_offscreen_bind(oglrender->ofs, true);
 
 
                        GPU_offscreen_bind(oglrender->ofs, true);
 
@@ -320,7 +345,7 @@ static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr)
                        GPU_offscreen_read_pixels(oglrender->ofs, GL_UNSIGNED_BYTE, gp_rect);
 
                        for (i = 0; i < sizex * sizey * 4; i += 4) {
                        GPU_offscreen_read_pixels(oglrender->ofs, GL_UNSIGNED_BYTE, gp_rect);
 
                        for (i = 0; i < sizex * sizey * 4; i += 4) {
-                               blend_color_mix_byte(&rect[i], &rect[i], &gp_rect[i]);
+                               blend_color_mix_byte(&render_rect[i], &render_rect[i], &gp_rect[i]);
                        }
                        GPU_offscreen_unbind(oglrender->ofs, true);
 
                        }
                        GPU_offscreen_unbind(oglrender->ofs, true);
 
@@ -335,7 +360,7 @@ static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr)
 
                if (view_context) {
                        ibuf_view = ED_view3d_draw_offscreen_imbuf(
 
                if (view_context) {
                        ibuf_view = ED_view3d_draw_offscreen_imbuf(
-                              scene, v3d, ar, sizex, sizey,
+                              scene, sl, v3d, ar, sizex, sizey,
                               IB_rect, draw_bgpic,
                               alpha_mode, oglrender->ofs_samples, oglrender->ofs_full_samples, viewname,
                               oglrender->fx, oglrender->ofs, err_out);
                               IB_rect, draw_bgpic,
                               alpha_mode, oglrender->ofs_samples, oglrender->ofs_full_samples, viewname,
                               oglrender->fx, oglrender->ofs, err_out);
@@ -347,7 +372,7 @@ static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr)
                }
                else {
                        ibuf_view = ED_view3d_draw_offscreen_imbuf_simple(
                }
                else {
                        ibuf_view = ED_view3d_draw_offscreen_imbuf_simple(
-                               scene, scene->camera, oglrender->sizex, oglrender->sizey,
+                               scene, sl, scene->camera, oglrender->sizex, oglrender->sizey,
                                IB_rect, OB_SOLID, false, true, true,
                                alpha_mode, oglrender->ofs_samples, oglrender->ofs_full_samples, viewname,
                                oglrender->fx, oglrender->ofs, err_out);
                                IB_rect, OB_SOLID, false, true, true,
                                alpha_mode, oglrender->ofs_samples, oglrender->ofs_full_samples, viewname,
                                oglrender->fx, oglrender->ofs, err_out);
@@ -355,46 +380,20 @@ static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr)
                }
 
                if (ibuf_view) {
                }
 
                if (ibuf_view) {
-                       /* steal rect reference from ibuf */
+                       ibuf_result = ibuf_view;
                        rect = (unsigned char *)ibuf_view->rect;
                        rect = (unsigned char *)ibuf_view->rect;
-                       ibuf_view->mall &= ~IB_rect;
-
-                       IMB_freeImBuf(ibuf_view);
                }
                else {
                        fprintf(stderr, "%s: failed to get buffer, %s\n", __func__, err_out);
                }
        }
 
                }
                else {
                        fprintf(stderr, "%s: failed to get buffer, %s\n", __func__, err_out);
                }
        }
 
-       /* note on color management:
-        *
-        * OpenGL renders into sRGB colors, but render buffers are expected to be
-        * linear So we convert to linear here, so the conversion back to bytes can make it
-        * sRGB (or other display space) again, and so that e.g. openexr saving also saves the
-        * correct linear float buffer.
-        */
-
-       if (rect) {
-               int profile_to;
-               float *rectf = RE_RenderViewGetById(rr, oglrender->view_id)->rectf;
-
-               if (BKE_scene_check_color_management_enabled(scene))
-                       profile_to = IB_PROFILE_LINEAR_RGB;
-               else
-                       profile_to = IB_PROFILE_SRGB;
-
-               /* sequencer has got trickier conversion happened above
-                * also assume opengl's space matches byte buffer color space */
-               IMB_buffer_float_from_byte(rectf, rect,
-                                          profile_to, IB_PROFILE_SRGB, true,
-                                          oglrender->sizex, oglrender->sizey, oglrender->sizex, oglrender->sizex);
-
-               /* rr->rectf is now filled with image data */
-
-               if ((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW))
-                       BKE_image_stamp_buf(scene, camera, rect, rectf, rr->rectx, rr->recty, 4);
-
-               MEM_freeN(rect);
+       if (ibuf_result != NULL) {
+               if ((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW)) {
+                       BKE_image_stamp_buf(scene, camera, NULL, rect, NULL, rr->rectx, rr->recty, 4);
+               }
+               RE_render_result_rect_from_ibuf(rr, &scene->r, ibuf_result, oglrender->view_id);
+               IMB_freeImBuf(ibuf_result);
        }
 }
 
        }
 }
 
@@ -421,6 +420,105 @@ static void screen_opengl_render_write(OGLRender *oglrender)
        else printf("OpenGL Render failed to write '%s'\n", name);
 }
 
        else printf("OpenGL Render failed to write '%s'\n", name);
 }
 
+static void addAlphaOverFloat(float dest[4], const float source[4])
+{
+       /* d = s + (1-alpha_s)d*/
+       float mul;
+
+       mul = 1.0f - source[3];
+
+       dest[0] = (mul * dest[0]) + source[0];
+       dest[1] = (mul * dest[1]) + source[1];
+       dest[2] = (mul * dest[2]) + source[2];
+       dest[3] = (mul * dest[3]) + source[3];
+
+}
+
+/* add renderlayer and renderpass for each grease pencil layer for using in composition */
+static void add_gpencil_renderpass(OGLRender *oglrender, RenderResult *rr, RenderView *rv)
+{
+       bGPdata *gpd = oglrender->scene->gpd;
+       Scene *scene = oglrender->scene;
+
+       /* sanity checks */
+       if (gpd == NULL) {
+               return;
+       }
+       if (scene == NULL) {
+               return;
+       }
+       if (BLI_listbase_is_empty(&gpd->layers)) {
+               return;
+       }
+       if (oglrender->v3d != NULL && (oglrender->v3d->flag2 & V3D_SHOW_GPENCIL) == 0) {
+               return;
+       }
+
+       /* save old alpha mode */
+       short oldalphamode = scene->r.alphamode;
+       /* set alpha transparent for gp */
+       scene->r.alphamode = R_ALPHAPREMUL;
+       
+       /* saves layer status */
+       short *oldsts = MEM_mallocN(BLI_listbase_count(&gpd->layers) * sizeof(short), "temp_gplayers_flag");
+       int i = 0;
+       for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
+               oldsts[i] = gpl->flag;
+               ++i;
+       }
+       /* loop all layers to create separate render */
+       for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
+               /* dont draw layer if hidden */
+               if (gpl->flag & GP_LAYER_HIDE)
+                       continue;
+               /* hide all layer except current */
+               for (bGPDlayer *gph = gpd->layers.first; gph; gph = gph->next) {
+                       if (gpl != gph) {
+                               gph->flag |= GP_LAYER_HIDE;
+                       }
+               }
+
+               /* render this gp layer */
+               screen_opengl_render_doit(oglrender, rr);
+               
+               /* add RendePass composite */
+               RenderPass *rp = RE_create_gp_pass(rr, gpl->info, rv->name);
+
+               /* copy image data from rectf */
+               // XXX: Needs conversion.
+               unsigned char *src = (unsigned char *)RE_RenderViewGetById(rr, oglrender->view_id)->rect32;
+               if (src != NULL) {
+                       float *dest = rp->rect;
+
+                       int x, y, rectx, recty;
+                       rectx = rr->rectx;
+                       recty = rr->recty;
+                       for (y = 0; y < recty; y++) {
+                               for (x = 0; x < rectx; x++) {
+                                       unsigned char *pixSrc = src + 4 * (rectx * y + x);
+                                       if (pixSrc[3] > 0) {
+                                               float *pixDest = dest + 4 * (rectx * y + x);
+                                               float float_src[4];
+                                               srgb_to_linearrgb_uchar4(float_src, pixSrc);
+                                               addAlphaOverFloat(pixDest, float_src);
+                                       }
+                               }
+                       }
+               }
+               /* back layer status */
+               i = 0;
+               for (bGPDlayer *gph = gpd->layers.first; gph; gph = gph->next) {
+                       gph->flag = oldsts[i];
+                       ++i;
+               }
+       }
+       /* free memory */
+       MEM_freeN(oldsts);
+
+       /* back default alpha mode */
+       scene->r.alphamode = oldalphamode;
+}
+
 static void screen_opengl_render_apply(OGLRender *oglrender)
 {
        RenderResult *rr;
 static void screen_opengl_render_apply(OGLRender *oglrender)
 {
        RenderResult *rr;
@@ -429,10 +527,39 @@ static void screen_opengl_render_apply(OGLRender *oglrender)
        ImBuf *ibuf;
        void *lock;
 
        ImBuf *ibuf;
        void *lock;
 
+       if (oglrender->is_sequencer) {
+               Scene *scene = oglrender->scene;
+
+               SeqRenderData context;
+               SpaceSeq *sseq = oglrender->sseq;
+               int chanshown = sseq ? sseq->chanshown : 0;
+
+               BKE_sequencer_new_render_data(
+                       oglrender->bmain->eval_ctx, oglrender->bmain, scene,
+                       oglrender->sizex, oglrender->sizey, 100.0f,
+                       &context);
+
+               for (view_id = 0; view_id < oglrender->views_len; view_id++) {
+                       context.view_id = view_id;
+                       context.gpu_offscreen = oglrender->ofs;
+                       context.gpu_fx = oglrender->fx;
+                       context.gpu_full_samples = oglrender->ofs_full_samples;
+
+                       oglrender->seq_data.ibufs_arr[view_id] = BKE_sequencer_give_ibuf(&context, CFRA, chanshown);
+               }
+       }
+
        rr = RE_AcquireResultRead(oglrender->re);
        for (rv = rr->views.first, view_id = 0; rv; rv = rv->next, view_id++) {
        rr = RE_AcquireResultRead(oglrender->re);
        for (rv = rr->views.first, view_id = 0; rv; rv = rv->next, view_id++) {
+               BLI_assert(view_id < oglrender->views_len);
                RE_SetActiveRenderView(oglrender->re, rv->name);
                oglrender->view_id = view_id;
                RE_SetActiveRenderView(oglrender->re, rv->name);
                oglrender->view_id = view_id;
+               /* add grease pencil passes. For sequencer, the render does not include renderpasses
+                * TODO: The sequencer render of grease pencil should be rethought */
+               if (!oglrender->is_sequencer) {
+                       add_gpencil_renderpass(oglrender, rr, rv);
+               }
+               /* render composite */
                screen_opengl_render_doit(oglrender, rr);
        }
 
                screen_opengl_render_doit(oglrender, rr);
        }
 
@@ -525,13 +652,19 @@ static bool screen_opengl_render_init(bContext *C, wmOperator *op)
        oglrender->sizey = sizey;
        oglrender->bmain = CTX_data_main(C);
        oglrender->scene = scene;
        oglrender->sizey = sizey;
        oglrender->bmain = CTX_data_main(C);
        oglrender->scene = scene;
+       oglrender->scene_layer = CTX_data_scene_layer(C);
        oglrender->cfrao = scene->r.cfra;
 
        oglrender->write_still = is_write_still && !is_animation;
        oglrender->cfrao = scene->r.cfra;
 
        oglrender->write_still = is_write_still && !is_animation;
+       oglrender->is_animation = is_animation;
+
+       oglrender->views_len = BKE_scene_multiview_num_views_get(&scene->r);
 
        oglrender->is_sequencer = is_sequencer;
        if (is_sequencer) {
                oglrender->sseq = CTX_wm_space_seq(C);
 
        oglrender->is_sequencer = is_sequencer;
        if (is_sequencer) {
                oglrender->sseq = CTX_wm_space_seq(C);
+               ImBuf **ibufs_arr = MEM_callocN(sizeof(*ibufs_arr) * oglrender->views_len, __func__);
+               oglrender->seq_data.ibufs_arr = ibufs_arr;
        }
 
        oglrender->prevsa = prevsa;
        }
 
        oglrender->prevsa = prevsa;
@@ -559,7 +692,7 @@ static bool screen_opengl_render_init(bContext *C, wmOperator *op)
        /* create image and image user */
        oglrender->ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
        BKE_image_signal(oglrender->ima, NULL, IMA_SIGNAL_FREE);
        /* create image and image user */
        oglrender->ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
        BKE_image_signal(oglrender->ima, NULL, IMA_SIGNAL_FREE);
-       BKE_image_backup_render(oglrender->scene, oglrender->ima);
+       BKE_image_backup_render(oglrender->scene, oglrender->ima, true);
 
        oglrender->iuser.scene = scene;
        oglrender->iuser.ok = 1;
 
        oglrender->iuser.scene = scene;
        oglrender->iuser.ok = 1;
@@ -578,6 +711,35 @@ static bool screen_opengl_render_init(bContext *C, wmOperator *op)
        oglrender->mh = NULL;
        oglrender->movie_ctx_arr = NULL;
 
        oglrender->mh = NULL;
        oglrender->movie_ctx_arr = NULL;
 
+       if (is_animation) {
+               TaskScheduler *task_scheduler = BLI_task_scheduler_get();
+               if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
+                       task_scheduler = BLI_task_scheduler_create(1);
+                       oglrender->task_scheduler = task_scheduler;
+                       oglrender->task_pool = BLI_task_pool_create_background(task_scheduler,
+                                                                              oglrender);
+                       BLI_pool_set_num_threads(oglrender->task_pool, 1);
+               }
+               else {
+                       oglrender->task_scheduler = NULL;
+                       oglrender->task_pool = BLI_task_pool_create(task_scheduler,
+                                                                   oglrender);
+               }
+               oglrender->pool_ok = true;
+               BLI_spin_init(&oglrender->reports_lock);
+       }
+       else {
+               oglrender->task_scheduler = NULL;
+               oglrender->task_pool = NULL;
+       }
+       oglrender->num_scheduled_frames = 0;
+       BLI_mutex_init(&oglrender->task_mutex);
+       BLI_condition_init(&oglrender->task_condition);
+
+#ifdef DEBUG_TIME
+       oglrender->time_start = PIL_check_seconds_timer();
+#endif
+
        return true;
 }
 
        return true;
 }
 
@@ -587,6 +749,22 @@ static void screen_opengl_render_end(bContext *C, OGLRender *oglrender)
        Scene *scene = oglrender->scene;
        int i;
 
        Scene *scene = oglrender->scene;
        int i;
 
+       if (oglrender->is_animation) {
+               BLI_task_pool_work_and_wait(oglrender->task_pool);
+               BLI_task_pool_free(oglrender->task_pool);
+               /* Depending on various things we might or might not use global scheduler. */
+               if (oglrender->task_scheduler != NULL) {
+                       BLI_task_scheduler_free(oglrender->task_scheduler);
+               }
+               BLI_spin_end(&oglrender->reports_lock);
+       }
+       BLI_mutex_end(&oglrender->task_mutex);
+       BLI_condition_end(&oglrender->task_condition);
+
+#ifdef DEBUG_TIME
+       printf("Total render time: %f\n", PIL_check_seconds_timer() - oglrender->time_start);
+#endif
+
        if (oglrender->mh) {
                if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
                        for (i = 0; i < oglrender->totvideos; i++) {
        if (oglrender->mh) {
                if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
                        for (i = 0; i < oglrender->totvideos; i++) {
@@ -616,6 +794,10 @@ static void screen_opengl_render_end(bContext *C, OGLRender *oglrender)
 
        GPU_offscreen_free(oglrender->ofs);
 
 
        GPU_offscreen_free(oglrender->ofs);
 
+       if (oglrender->is_sequencer) {
+               MEM_freeN(oglrender->seq_data.ibufs_arr);
+       }
+
        oglrender->scene->customdata_mask_modal = 0;
 
        CTX_wm_area_set(C, oglrender->prevsa);
        oglrender->scene->customdata_mask_modal = 0;
 
        CTX_wm_area_set(C, oglrender->prevsa);
@@ -677,6 +859,124 @@ static bool screen_opengl_render_anim_initialize(bContext *C, wmOperator *op)
        return true;
 }
 
        return true;
 }
 
+typedef struct WriteTaskData {
+       RenderResult *rr;
+       int cfra;
+} WriteTaskData;
+
+static void write_result_func(TaskPool * __restrict pool,
+                              void *task_data_v,
+                              int UNUSED(thread_id))
+{
+       OGLRender *oglrender = (OGLRender *) BLI_task_pool_userdata(pool);
+       WriteTaskData *task_data = (WriteTaskData *) task_data_v;
+       Scene *scene = oglrender->scene;
+       RenderResult *rr = task_data->rr;
+       const bool is_movie = BKE_imtype_is_movie(scene->r.im_format.imtype);
+       const int cfra = task_data->cfra;
+       bool ok;
+       /* Don't attempt to write if we've got an error. */
+       if (!oglrender->pool_ok) {
+               RE_FreeRenderResult(rr);
+               BLI_mutex_lock(&oglrender->task_mutex);
+               oglrender->num_scheduled_frames--;
+               BLI_condition_notify_all(&oglrender->task_condition);
+               BLI_mutex_unlock(&oglrender->task_mutex);
+               return;
+       }
+       /* Construct local thread0safe copy of reports structure which we can
+        * safely pass to the underlying functions.
+        */
+       ReportList reports;
+       BKE_reports_init(&reports, oglrender->reports->flag & ~RPT_PRINT);
+       /* Do actual save logic here, depending on the file format.
+        *
+        * NOTE: We have to construct temporary scene with proper scene->r.cfra.
+        * This is because underlying calls do not use r.cfra but use scene
+        * for that.
+        */
+       Scene tmp_scene = *scene;
+       tmp_scene.r.cfra = cfra;
+       if (is_movie) {
+               ok = RE_WriteRenderViewsMovie(&reports,
+                                             rr,
+                                             &tmp_scene,
+                                             &tmp_scene.r,
+                                             oglrender->mh,
+                                             oglrender->movie_ctx_arr,
+                                             oglrender->totvideos,
+                                             PRVRANGEON != 0);
+       }
+       else {
+               /* TODO(sergey): We can in theory save some CPU ticks here because we
+                * calculate file name again here.
+                */
+               char name[FILE_MAX];
+               BKE_image_path_from_imformat(name,
+                                            scene->r.pic,
+                                            oglrender->bmain->name,
+                                            cfra,
+                                            &scene->r.im_format,
+                                            (scene->r.scemode & R_EXTENSION) != 0,
+                                            true,
+                                            NULL);
+
+               BKE_render_result_stamp_info(&tmp_scene, tmp_scene.camera, rr, false);
+               ok = RE_WriteRenderViewsImage(NULL, rr, &tmp_scene, true, name);
+               if (!ok) {
+                       BKE_reportf(&reports,
+                                   RPT_ERROR,
+                                   "Write error: cannot save %s",
+                                   name);
+               }
+       }
+       if (reports.list.first != NULL) {
+               BLI_spin_lock(&oglrender->reports_lock);
+               for (Report *report = reports.list.first;
+                    report != NULL;
+                    report = report->next)
+               {
+                       BKE_report(oglrender->reports,
+                                  report->type,
+                                  report->message);
+               }
+               BLI_spin_unlock(&oglrender->reports_lock);
+       }
+       if (!ok) {
+               oglrender->pool_ok = false;
+       }
+       RE_FreeRenderResult(rr);
+       BLI_mutex_lock(&oglrender->task_mutex);
+       oglrender->num_scheduled_frames--;
+       BLI_condition_notify_all(&oglrender->task_condition);
+       BLI_mutex_unlock(&oglrender->task_mutex);
+}
+
+static bool schedule_write_result(OGLRender *oglrender, RenderResult *rr)
+{
+       if (!oglrender->pool_ok) {
+               RE_FreeRenderResult(rr);
+               return false;
+       }
+       Scene *scene = oglrender->scene;
+       WriteTaskData *task_data = MEM_mallocN(sizeof(WriteTaskData), "write task data");
+       task_data->rr = rr;
+       task_data->cfra = scene->r.cfra;
+       BLI_mutex_lock(&oglrender->task_mutex);
+       oglrender->num_scheduled_frames++;
+       if (oglrender->num_scheduled_frames > MAX_SCHEDULED_FRAMES) {
+               BLI_condition_wait(&oglrender->task_condition,
+                                  &oglrender->task_mutex);
+       }
+       BLI_mutex_unlock(&oglrender->task_mutex);
+       BLI_task_pool_push(oglrender->task_pool,
+                          write_result_func,
+                          task_data,
+                          true,
+                          TASK_PRIORITY_LOW);
+       return true;
+}
+
 static bool screen_opengl_render_anim_step(bContext *C, wmOperator *op)
 {
        Main *bmain = CTX_data_main(C);
 static bool screen_opengl_render_anim_step(bContext *C, wmOperator *op)
 {
        Main *bmain = CTX_data_main(C);
@@ -709,7 +1009,9 @@ static bool screen_opengl_render_anim_step(bContext *C, wmOperator *op)
                        &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, true, NULL);
 
                if ((scene->r.mode & R_NO_OVERWRITE) && BLI_exists(name)) {
                        &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, true, NULL);
 
                if ((scene->r.mode & R_NO_OVERWRITE) && BLI_exists(name)) {
+                       BLI_spin_lock(&oglrender->reports_lock);
                        BKE_reportf(op->reports, RPT_INFO, "Skipping existing frame \"%s\"", name);
                        BKE_reportf(op->reports, RPT_INFO, "Skipping existing frame \"%s\"", name);
+                       BLI_spin_unlock(&oglrender->reports_lock);
                        ok = true;
                        goto finally;
                }
                        ok = true;
                        goto finally;
                }
@@ -737,34 +1039,10 @@ static bool screen_opengl_render_anim_step(bContext *C, wmOperator *op)
 
        /* save to disk */
        rr = RE_AcquireResultRead(oglrender->re);
 
        /* save to disk */
        rr = RE_AcquireResultRead(oglrender->re);
-
-       if (is_movie) {
-               ok = RE_WriteRenderViewsMovie(oglrender->reports, rr, scene, &scene->r, oglrender->mh,
-                                             oglrender->movie_ctx_arr, oglrender->totvideos, PRVRANGEON != 0);
-               if (ok) {
-                       printf("Append frame %d", scene->r.cfra);
-                       BKE_reportf(op->reports, RPT_INFO, "Appended frame: %d", scene->r.cfra);
-               }
-       }
-       else {
-               BKE_render_result_stamp_info(scene, scene->camera, rr, false);
-               ok = RE_WriteRenderViewsImage(op->reports, rr, scene, true, name);
-               if (ok) {
-                       printf("Saved: %s", name);
-                       BKE_reportf(op->reports, RPT_INFO, "Saved file: %s", name);
-               }
-               else {
-                       printf("Write error: cannot save %s\n", name);
-                       BKE_reportf(op->reports, RPT_ERROR, "Write error: cannot save %s", name);
-               }
-       }
-
+       RenderResult *new_rr = RE_DuplicateRenderResult(rr);
        RE_ReleaseResult(oglrender->re);
 
        RE_ReleaseResult(oglrender->re);
 
-
-       /* movie stats prints have no line break */
-       printf("\n");
-
+       ok = schedule_write_result(oglrender, new_rr);
 
 finally:  /* Step the frame and bail early if needed */
 
 
 finally:  /* Step the frame and bail early if needed */
 
@@ -790,6 +1068,7 @@ static int screen_opengl_render_modal(bContext *C, wmOperator *op, const wmEvent
        switch (event->type) {
                case ESCKEY:
                        /* cancel */
        switch (event->type) {
                case ESCKEY:
                        /* cancel */
+                       oglrender->pool_ok = false;  /* Flag pool for cancel. */
                        screen_opengl_render_end(C, op->customdata);
                        return OPERATOR_FINISHED;
                case TIMER:
                        screen_opengl_render_end(C, op->customdata);
                        return OPERATOR_FINISHED;
                case TIMER: