Merge branch 'master' into blender2.8
[blender.git] / source / blender / windowmanager / intern / wm_draw.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/windowmanager/intern/wm_draw.c
28  *  \ingroup wm
29  *
30  * Handle OpenGL buffers for windowing, also paint cursor.
31  */
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "DNA_listBase.h"
37 #include "DNA_object_types.h"
38 #include "DNA_camera_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_windowmanager_types.h"
41 #include "DNA_userdef_types.h"
42 #include "DNA_view3d_types.h"
43
44 #include "MEM_guardedalloc.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_utildefines.h"
48
49 #include "BIF_gl.h"
50
51 #include "BKE_context.h"
52 #include "BKE_image.h"
53
54 #include "GHOST_C-api.h"
55
56 #include "ED_node.h"
57 #include "ED_view3d.h"
58 #include "ED_screen.h"
59
60 #include "GPU_draw.h"
61 #include "GPU_extensions.h"
62 #include "GPU_immediate.h"
63 #include "GPU_viewport.h"
64
65 #include "RE_engine.h"
66
67 #include "WM_api.h"
68 #include "WM_types.h"
69 #include "wm.h"
70 #include "wm_draw.h"
71 #include "wm_window.h"
72 #include "wm_event_system.h"
73
74 #ifdef WITH_OPENSUBDIV
75 #  include "BKE_subsurf.h"
76 #endif
77
78 /* swap */
79 #define WIN_NONE_OK     0
80 #define WIN_BACK_OK     1
81 #define WIN_FRONT_OK    2
82 #define WIN_BOTH_OK     3
83
84 /* ******************* drawing, overlays *************** */
85
86
87 static void wm_paintcursor_draw(bContext *C, ARegion *ar)
88 {
89         wmWindowManager *wm = CTX_wm_manager(C);
90         
91         if (wm->paintcursors.first) {
92                 wmWindow *win = CTX_wm_window(C);
93                 bScreen *screen = WM_window_get_active_screen(win);
94                 wmPaintCursor *pc;
95
96                 if (ar->swinid && screen->subwinactive == ar->swinid) {
97                         for (pc = wm->paintcursors.first; pc; pc = pc->next) {
98                                 if (pc->poll == NULL || pc->poll(C)) {
99                                         ARegion *ar_other = CTX_wm_region(C);
100                                         if (ELEM(win->grabcursor, GHOST_kGrabWrap, GHOST_kGrabHide)) {
101                                                 int x = 0, y = 0;
102                                                 wm_get_cursor_position(win, &x, &y);
103                                                 pc->draw(C,
104                                                          x - ar_other->winrct.xmin,
105                                                          y - ar_other->winrct.ymin,
106                                                          pc->customdata);
107                                         }
108                                         else {
109                                                 pc->draw(C,
110                                                          win->eventstate->x - ar_other->winrct.xmin,
111                                                          win->eventstate->y - ar_other->winrct.ymin,
112                                                          pc->customdata);
113                                         }
114                                 }
115                         }
116                 }
117         }
118 }
119
120 /* ********************* drawing, swap ****************** */
121
122 static void wm_area_mark_invalid_backbuf(ScrArea *sa)
123 {
124         if (sa->spacetype == SPACE_VIEW3D)
125                 ((View3D *)sa->spacedata.first)->flag |= V3D_INVALID_BACKBUF;
126 }
127
128 static bool wm_area_test_invalid_backbuf(ScrArea *sa)
129 {
130         if (sa->spacetype == SPACE_VIEW3D)
131                 return (((View3D *)sa->spacedata.first)->flag & V3D_INVALID_BACKBUF) != 0;
132         else
133                 return true;
134 }
135
136 static void wm_region_test_render_do_draw(const Scene *scene, ScrArea *sa, ARegion *ar)
137 {
138         /* tag region for redraw from render engine preview running inside of it */
139         if (sa->spacetype == SPACE_VIEW3D) {
140                 RegionView3D *rv3d = ar->regiondata;
141                 RenderEngine *engine = (rv3d) ? rv3d->render_engine : NULL;
142                 GPUViewport *viewport = (rv3d) ? rv3d->viewport : NULL;
143
144                 if (engine && (engine->flag & RE_ENGINE_DO_DRAW)) {
145                         View3D *v3d = sa->spacedata.first;
146                         rcti border_rect;
147
148                         /* do partial redraw when possible */
149                         if (ED_view3d_calc_render_border(scene, v3d, ar, &border_rect))
150                                 ED_region_tag_redraw_partial(ar, &border_rect);
151                         else
152                                 ED_region_tag_redraw(ar);
153
154                         engine->flag &= ~RE_ENGINE_DO_DRAW;
155                 }
156                 else if (viewport && GPU_viewport_do_update(viewport)) {
157                         ED_region_tag_redraw(ar);
158                 }
159         }
160 }
161
162 /********************** draw all **************************/
163 /* - reference method, draw all each time                 */
164
165 typedef struct WindowDrawCB {
166         struct WindowDrawCB *next, *prev;
167
168         void(*draw)(const struct wmWindow *, void *);
169         void *customdata;
170
171 } WindowDrawCB;
172
173 void *WM_draw_cb_activate(
174         wmWindow *win,
175         void(*draw)(const struct wmWindow *, void *),
176         void *customdata)
177 {
178         WindowDrawCB *wdc = MEM_callocN(sizeof(*wdc), "WindowDrawCB");
179
180         BLI_addtail(&win->drawcalls, wdc);
181         wdc->draw = draw;
182         wdc->customdata = customdata;
183
184         return wdc;
185 }
186
187 void WM_draw_cb_exit(wmWindow *win, void *handle)
188 {
189         for (WindowDrawCB *wdc = win->drawcalls.first; wdc; wdc = wdc->next) {
190                 if (wdc == (WindowDrawCB *)handle) {
191                         BLI_remlink(&win->drawcalls, wdc);
192                         MEM_freeN(wdc);
193                         return;
194                 }
195         }
196 }
197
198 static void wm_draw_callbacks(wmWindow *win)
199 {
200         for (WindowDrawCB *wdc = win->drawcalls.first; wdc; wdc = wdc->next) {
201                 wdc->draw(win, wdc->customdata);
202         }
203 }
204
205 static void wm_method_draw_full(bContext *C, wmWindow *win)
206 {
207         bScreen *screen = WM_window_get_active_screen(win);
208         ScrArea *sa;
209         ARegion *ar;
210
211         /* draw area regions */
212         for (sa = screen->areabase.first; sa; sa = sa->next) {
213                 CTX_wm_area_set(C, sa);
214
215                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
216                         if (ar->swinid) {
217                                 CTX_wm_region_set(C, ar);
218                                 ED_region_do_draw(C, ar);
219                                 ar->do_draw = false;
220                                 wm_paintcursor_draw(C, ar);
221                                 CTX_wm_region_set(C, NULL);
222                         }
223                 }
224                 
225                 wm_area_mark_invalid_backbuf(sa);
226                 CTX_wm_area_set(C, NULL);
227         }
228
229         ED_screen_draw_edges(win);
230         screen->do_draw = false;
231         wm_draw_callbacks(win);
232
233         /* draw overlapping regions */
234         for (ar = screen->regionbase.first; ar; ar = ar->next) {
235                 if (ar->swinid) {
236                         CTX_wm_menu_set(C, ar);
237                         ED_region_do_draw(C, ar);
238                         ar->do_draw = false;
239                         CTX_wm_menu_set(C, NULL);
240                 }
241         }
242
243         if (screen->do_draw_gesture)
244                 wm_gesture_draw(win);
245 }
246
247 /****************** draw overlap all **********************/
248 /* - redraw marked areas, and anything that overlaps it   */
249 /* - it also handles swap exchange optionally, assuming   */
250 /*   that on swap no clearing happens and we get back the */
251 /*   same buffer as we swapped to the front               */
252
253 /* mark area-regions to redraw if overlapped with rect */
254 static void wm_flush_regions_down(bScreen *screen, rcti *dirty)
255 {
256         ScrArea *sa;
257         ARegion *ar;
258
259         for (sa = screen->areabase.first; sa; sa = sa->next) {
260                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
261                         if (BLI_rcti_isect(dirty, &ar->winrct, NULL)) {
262                                 ar->do_draw = RGN_DRAW;
263                                 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
264                                 ar->swap = WIN_NONE_OK;
265                         }
266                 }
267         }
268 }
269
270 /* mark menu-regions to redraw if overlapped with rect */
271 static void wm_flush_regions_up(bScreen *screen, rcti *dirty)
272 {
273         ARegion *ar;
274         
275         for (ar = screen->regionbase.first; ar; ar = ar->next) {
276                 if (BLI_rcti_isect(dirty, &ar->winrct, NULL)) {
277                         ar->do_draw = RGN_DRAW;
278                         memset(&ar->drawrct, 0, sizeof(ar->drawrct));
279                         ar->swap = WIN_NONE_OK;
280                 }
281         }
282 }
283
284 static void wm_method_draw_overlap_all(bContext *C, wmWindow *win, int exchange)
285 {
286         wmWindowManager *wm = CTX_wm_manager(C);
287         bScreen *screen = WM_window_get_active_screen(win);
288         ScrArea *sa;
289         ARegion *ar;
290         static rcti rect = {0, 0, 0, 0};
291
292         /* after backbuffer selection draw, we need to redraw */
293         for (sa = screen->areabase.first; sa; sa = sa->next)
294                 for (ar = sa->regionbase.first; ar; ar = ar->next)
295                         if (ar->swinid && !wm_area_test_invalid_backbuf(sa))
296                                 ED_region_tag_redraw(ar);
297
298         /* flush overlapping regions */
299         if (screen->regionbase.first) {
300                 /* flush redraws of area regions up to overlapping regions */
301                 for (sa = screen->areabase.first; sa; sa = sa->next)
302                         for (ar = sa->regionbase.first; ar; ar = ar->next)
303                                 if (ar->swinid && ar->do_draw)
304                                         wm_flush_regions_up(screen, &ar->winrct);
305                 
306                 /* flush between overlapping regions */
307                 for (ar = screen->regionbase.last; ar; ar = ar->prev)
308                         if (ar->swinid && ar->do_draw)
309                                 wm_flush_regions_up(screen, &ar->winrct);
310                 
311                 /* flush redraws of overlapping regions down to area regions */
312                 for (ar = screen->regionbase.last; ar; ar = ar->prev)
313                         if (ar->swinid && ar->do_draw)
314                                 wm_flush_regions_down(screen, &ar->winrct);
315         }
316
317         /* flush drag item */
318         if (rect.xmin != rect.xmax) {
319                 wm_flush_regions_down(screen, &rect);
320                 rect.xmin = rect.xmax = 0;
321         }
322         if (wm->drags.first) {
323                 /* doesnt draw, fills rect with boundbox */
324                 wm_drags_draw(C, win, &rect);
325         }
326         
327         /* draw marked area regions */
328         for (sa = screen->areabase.first; sa; sa = sa->next) {
329                 CTX_wm_area_set(C, sa);
330
331                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
332                         if (ar->swinid) {
333                                 if (ar->do_draw) {
334                                         CTX_wm_region_set(C, ar);
335                                         ED_region_do_draw(C, ar);
336                                         ar->do_draw = false;
337                                         wm_paintcursor_draw(C, ar);
338                                         CTX_wm_region_set(C, NULL);
339
340                                         if (exchange)
341                                                 ar->swap = WIN_FRONT_OK;
342                                 }
343                                 else if (exchange) {
344                                         if (ar->swap == WIN_FRONT_OK) {
345                                                 CTX_wm_region_set(C, ar);
346                                                 ED_region_do_draw(C, ar);
347                                                 ar->do_draw = false;
348                                                 wm_paintcursor_draw(C, ar);
349                                                 CTX_wm_region_set(C, NULL);
350
351                                                 ar->swap = WIN_BOTH_OK;
352                                         }
353                                         else if (ar->swap == WIN_BACK_OK)
354                                                 ar->swap = WIN_FRONT_OK;
355                                         else if (ar->swap == WIN_BOTH_OK)
356                                                 ar->swap = WIN_BOTH_OK;
357                                 }
358                         }
359                 }
360
361                 wm_area_mark_invalid_backbuf(sa);
362                 CTX_wm_area_set(C, NULL);
363         }
364
365         /* after area regions so we can do area 'overlay' drawing */
366         if (screen->do_draw) {
367                 ED_screen_draw_edges(win);
368                 screen->do_draw = false;
369                 wm_draw_callbacks(win);
370
371                 if (exchange)
372                         screen->swap = WIN_FRONT_OK;
373         }
374         else if (exchange) {
375                 if (screen->swap == WIN_FRONT_OK) {
376                         ED_screen_draw_edges(win);
377                         screen->do_draw = false;
378                         screen->swap = WIN_BOTH_OK;
379                         wm_draw_callbacks(win);
380                 }
381                 else if (screen->swap == WIN_BACK_OK)
382                         screen->swap = WIN_FRONT_OK;
383                 else if (screen->swap == WIN_BOTH_OK)
384                         screen->swap = WIN_BOTH_OK;
385         }
386
387         /* draw marked overlapping regions */
388         for (ar = screen->regionbase.first; ar; ar = ar->next) {
389                 if (ar->swinid && ar->do_draw) {
390                         CTX_wm_menu_set(C, ar);
391                         ED_region_do_draw(C, ar);
392                         ar->do_draw = false;
393                         CTX_wm_menu_set(C, NULL);
394                 }
395         }
396
397         if (screen->do_draw_gesture)
398                 wm_gesture_draw(win);
399         
400         /* needs pixel coords in screen */
401         if (wm->drags.first) {
402                 wm_drags_draw(C, win, NULL);
403         }
404 }
405
406 /****************** draw triple buffer ********************/
407 /* - area regions are written into a texture, without any */
408 /*   of the overlapping menus, brushes, gestures. these   */
409 /*   are redrawn each time.                               */
410
411 static void wm_draw_triple_free(wmDrawTriple *triple)
412 {
413         if (triple) {
414                 glDeleteTextures(1, &triple->bind);
415                 MEM_freeN(triple);
416         }
417 }
418
419 static void wm_draw_triple_fail(bContext *C, wmWindow *win)
420 {
421         wm_draw_window_clear(win);
422
423         win->drawfail = true;
424         wm_method_draw_overlap_all(C, win, 0);
425 }
426
427 static bool wm_triple_gen_textures(wmWindow *win, wmDrawTriple *triple)
428 {
429         /* compute texture sizes */
430         triple->x = WM_window_pixels_x(win);
431         triple->y = WM_window_pixels_y(win);
432
433 #if USE_TEXTURE_RECTANGLE
434         /* GL_TEXTURE_RECTANGLE is part of GL 3.1 so we can use it soon without runtime checks */
435         triple->target = GL_TEXTURE_RECTANGLE;
436 #else
437         triple->target = GL_TEXTURE_2D;
438 #endif
439
440         /* generate texture names */
441         glGenTextures(1, &triple->bind);
442
443         /* proxy texture is only guaranteed to test for the cases that
444          * there is only one texture in use, which may not be the case */
445         const GLint maxsize = GPU_max_texture_size();
446
447         if (triple->x > maxsize || triple->y > maxsize) {
448                 printf("WM: failed to allocate texture for triple buffer drawing "
449                        "(texture too large for graphics card).\n");
450                 return false;
451         }
452
453         /* setup actual texture */
454         glBindTexture(triple->target, triple->bind);
455
456         /* no mipmaps */
457 #if USE_TEXTURE_RECTANGLE
458         /* already has no mipmaps */
459 #else
460         glTexParameteri(triple->target, GL_TEXTURE_MAX_LEVEL, 0);
461         /* GL_TEXTURE_BASE_LEVEL = 0 by default */
462 #endif
463
464         glTexParameteri(triple->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
465         glTexParameteri(triple->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
466
467         glTexImage2D(triple->target, 0, GL_RGB8, triple->x, triple->y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
468
469         glBindTexture(triple->target, 0);
470
471         return true;
472 }
473
474 void wm_triple_draw_textures(wmWindow *win, wmDrawTriple *triple, float alpha)
475 {
476         const int sizex = WM_window_pixels_x(win);
477         const int sizey = WM_window_pixels_y(win);
478
479         /* wmOrtho for the screen has this same offset */
480         float ratiox = sizex;
481         float ratioy = sizey;
482         float halfx = GLA_PIXEL_OFS;
483         float halfy = GLA_PIXEL_OFS;
484
485 #if USE_TEXTURE_RECTANGLE
486         /* texture rectangle has unnormalized coordinates */
487 #else
488         ratiox /= triple->x;
489         ratioy /= triple->y;
490         halfx /= triple->x;
491         halfy /= triple->y;
492 #endif
493
494         Gwn_VertFormat *format = immVertexFormat();
495         unsigned int texcoord = GWN_vertformat_attr_add(format, "texCoord", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
496         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
497
498         const int activeTex = 7; /* arbitrary */
499         glActiveTexture(GL_TEXTURE0 + activeTex);
500         glBindTexture(triple->target, triple->bind);
501
502 #if USE_TEXTURE_RECTANGLE
503         immBindBuiltinProgram(GPU_SHADER_3D_IMAGE_RECT_MODULATE_ALPHA);
504 #else
505         immBindBuiltinProgram(GPU_SHADER_3D_IMAGE_MODULATE_ALPHA);
506         /* TODO: make pure 2D version
507          * and a 2D_IMAGE (replace, not modulate) version for when alpha = 1.0
508          */
509 #endif
510         immUniform1f("alpha", alpha);
511         immUniform1i("image", activeTex);
512
513         immBegin(GWN_PRIM_TRI_FAN, 4);
514
515         immAttrib2f(texcoord, halfx, halfy);
516         immVertex2f(pos, 0.0f, 0.0f);
517
518         immAttrib2f(texcoord, ratiox + halfx, halfy);
519         immVertex2f(pos, sizex, 0.0f);
520
521         immAttrib2f(texcoord, ratiox + halfx, ratioy + halfy);
522         immVertex2f(pos, sizex, sizey);
523
524         immAttrib2f(texcoord, halfx, ratioy + halfy);
525         immVertex2f(pos, 0.0f, sizey);
526
527         immEnd();
528         immUnbindProgram();
529
530         glBindTexture(triple->target, 0);
531         if (activeTex != 0)
532                 glActiveTexture(GL_TEXTURE0);
533 }
534
535 static void wm_triple_copy_textures(wmWindow *win, wmDrawTriple *triple)
536 {
537         const int sizex = WM_window_pixels_x(win);
538         const int sizey = WM_window_pixels_y(win);
539
540         glBindTexture(triple->target, triple->bind);
541         /* what is GL_READ_BUFFER right now? */
542         glCopyTexSubImage2D(triple->target, 0, 0, 0, 0, 0, sizex, sizey);
543         glBindTexture(triple->target, 0);
544 }
545
546 static void wm_draw_region_blend(wmWindow *win, ARegion *ar, wmDrawTriple *triple)
547 {
548         float fac = ED_region_blend_factor(ar);
549         
550         /* region blend always is 1, except when blend timer is running */
551         if (fac < 1.0f) {
552                 bScreen *screen = WM_window_get_active_screen(win);
553
554                 wmSubWindowScissorSet(win, screen->mainwin, &ar->winrct, true);
555
556                 glEnable(GL_BLEND);
557                 wm_triple_draw_textures(win, triple, 1.0f - fac);
558                 glDisable(GL_BLEND);
559         }
560 }
561
562 static void wm_method_draw_triple(bContext *C, wmWindow *win)
563 {
564         wmWindowManager *wm = CTX_wm_manager(C);
565         wmDrawData *dd, *dd_next, *drawdata = win->drawdata.first;
566         bScreen *screen = WM_window_get_active_screen(win);
567         ScrArea *sa;
568         ARegion *ar;
569         bool copytex = false;
570
571         if (drawdata && drawdata->triple) {
572 #if 0 /* why do we need to clear before overwriting? */
573                 glClearColor(1, 1, 0, 0);
574                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
575 #endif
576
577                 wmSubWindowSet(win, screen->mainwin);
578
579                 wm_triple_draw_textures(win, drawdata->triple, 1.0f);
580         }
581         else {
582                 /* we run it when we start OR when we turn stereo on */
583                 if (drawdata == NULL) {
584                         drawdata = MEM_callocN(sizeof(wmDrawData), "wmDrawData");
585                         BLI_addhead(&win->drawdata, drawdata);
586                 }
587
588                 drawdata->triple = MEM_callocN(sizeof(wmDrawTriple), "wmDrawTriple");
589
590                 if (!wm_triple_gen_textures(win, drawdata->triple)) {
591                         wm_draw_triple_fail(C, win);
592                         return;
593                 }
594         }
595
596         /* it means stereo was just turned off */
597         /* note: we are removing all drawdatas that are not the first */
598         for (dd = drawdata->next; dd; dd = dd_next) {
599                 dd_next = dd->next;
600
601                 BLI_remlink(&win->drawdata, dd);
602                 wm_draw_triple_free(dd->triple);
603                 MEM_freeN(dd);
604         }
605
606         wmDrawTriple *triple = drawdata->triple;
607
608         /* draw marked area regions */
609         for (sa = screen->areabase.first; sa; sa = sa->next) {
610                 CTX_wm_area_set(C, sa);
611
612                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
613                         if (ar->swinid && ar->do_draw) {
614                                 if (ar->overlap == false) {
615                                         CTX_wm_region_set(C, ar);
616                                         ED_region_do_draw(C, ar);
617                                         ar->do_draw = false;
618                                         CTX_wm_region_set(C, NULL);
619                                         copytex = true;
620                                 }
621                         }
622                 }
623
624                 wm_area_mark_invalid_backbuf(sa);
625                 CTX_wm_area_set(C, NULL);
626         }
627
628         if (copytex) {
629                 wmSubWindowSet(win, screen->mainwin);
630
631                 wm_triple_copy_textures(win, triple);
632         }
633
634         if (wm->paintcursors.first) {
635                 for (sa = screen->areabase.first; sa; sa = sa->next) {
636                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
637                                 if (ar->swinid && ar->swinid == screen->subwinactive) {
638                                         CTX_wm_area_set(C, sa);
639                                         CTX_wm_region_set(C, ar);
640
641                                         /* make region ready for draw, scissor, pixelspace */
642                                         ED_region_set(C, ar);
643                                         wm_paintcursor_draw(C, ar);
644
645                                         CTX_wm_region_set(C, NULL);
646                                         CTX_wm_area_set(C, NULL);
647                                 }
648                         }
649                 }
650
651                 wmSubWindowSet(win, screen->mainwin);
652         }
653
654         /* draw overlapping area regions (always like popups) */
655         for (sa = screen->areabase.first; sa; sa = sa->next) {
656                 CTX_wm_area_set(C, sa);
657
658                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
659                         if (ar->swinid && ar->overlap) {
660                                 CTX_wm_region_set(C, ar);
661                                 ED_region_do_draw(C, ar);
662                                 ar->do_draw = false;
663                                 CTX_wm_region_set(C, NULL);
664
665                                 wm_draw_region_blend(win, ar, triple);
666                         }
667                 }
668
669                 CTX_wm_area_set(C, NULL);
670         }
671
672         /* after area regions so we can do area 'overlay' drawing */
673         ED_screen_draw_edges(win);
674         WM_window_get_active_screen(win)->do_draw = false;
675         wm_draw_callbacks(win);
676
677         /* draw floating regions (menus) */
678         for (ar = screen->regionbase.first; ar; ar = ar->next) {
679                 if (ar->swinid) {
680                         CTX_wm_menu_set(C, ar);
681                         ED_region_do_draw(C, ar);
682                         ar->do_draw = false;
683                         CTX_wm_menu_set(C, NULL);
684                 }
685         }
686
687         /* always draw, not only when screen tagged */
688         if (win->gesture.first)
689                 wm_gesture_draw(win);
690
691         /* needs pixel coords in screen */
692         if (wm->drags.first) {
693                 wm_drags_draw(C, win, NULL);
694         }
695 }
696
697 static void wm_method_draw_triple_multiview(bContext *C, wmWindow *win, eStereoViews sview)
698 {
699         wmWindowManager *wm = CTX_wm_manager(C);
700         wmDrawData *drawdata;
701         wmDrawTriple *triple_data, *triple_all;
702         bScreen *screen = WM_window_get_active_screen(win);
703         ScrArea *sa;
704         ARegion *ar;
705         int copytex = false;
706         int id;
707
708         /* we store the triple_data in sequence to triple_all */
709         for (id = 0; id < 2; id++) {
710                 drawdata = BLI_findlink(&win->drawdata, (sview * 2) + id);
711
712                 if (drawdata && drawdata->triple) {
713                         if (id == 0) {
714 #if 0 /* why do we need to clear before overwriting? */
715                                 glClearColor(0, 0, 0, 0);
716                                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
717 #endif
718
719                                 wmSubWindowSet(win, screen->mainwin);
720
721                                 wm_triple_draw_textures(win, drawdata->triple, 1.0f);
722                         }
723                 }
724                 else {
725                         /* we run it when we start OR when we turn stereo on */
726                         if (drawdata == NULL) {
727                                 drawdata = MEM_callocN(sizeof(wmDrawData), "wmDrawData");
728                                 BLI_addtail(&win->drawdata, drawdata);
729                         }
730
731                         drawdata->triple = MEM_callocN(sizeof(wmDrawTriple), "wmDrawTriple");
732
733                         if (!wm_triple_gen_textures(win, drawdata->triple)) {
734                                 wm_draw_triple_fail(C, win);
735                                 return;
736                         }
737                 }
738         }
739
740         triple_data = ((wmDrawData *) BLI_findlink(&win->drawdata, sview * 2))->triple;
741         triple_all  = ((wmDrawData *) BLI_findlink(&win->drawdata, (sview * 2) + 1))->triple;
742
743         /* draw marked area regions */
744         for (sa = screen->areabase.first; sa; sa = sa->next) {
745                 CTX_wm_area_set(C, sa);
746
747                 switch (sa->spacetype) {
748                         case SPACE_IMAGE:
749                         {
750                                 SpaceImage *sima = sa->spacedata.first;
751                                 sima->iuser.multiview_eye = sview;
752                                 break;
753                         }
754                         case SPACE_VIEW3D:
755                         {
756                                 View3D *v3d = sa->spacedata.first;
757                                 if (v3d->camera && v3d->camera->type == OB_CAMERA) {
758                                         Camera *cam = v3d->camera->data;
759                                         CameraBGImage *bgpic = cam->bg_images.first;
760                                         v3d->multiview_eye = sview;
761                                         if (bgpic) bgpic->iuser.multiview_eye = sview;
762                                 }
763                                 break;
764                         }
765                         case SPACE_NODE:
766                         {
767                                 SpaceNode *snode = sa->spacedata.first;
768                                 if ((snode->flag & SNODE_BACKDRAW) && ED_node_is_compositor(snode)) {
769                                         Image *ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
770                                         ima->eye = sview;
771                                 }
772                                 break;
773                         }
774                         case SPACE_SEQ:
775                         {
776                                 SpaceSeq *sseq = sa->spacedata.first;
777                                 sseq->multiview_eye = sview;
778                                 break;
779                         }
780                 }
781
782                 /* draw marked area regions */
783                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
784                         if (ar->swinid && ar->do_draw) {
785
786                                 if (ar->overlap == false) {
787                                         CTX_wm_region_set(C, ar);
788                                         ED_region_do_draw(C, ar);
789
790                                         if (sview == STEREO_RIGHT_ID)
791                                                 ar->do_draw = false;
792
793                                         CTX_wm_region_set(C, NULL);
794                                         copytex = true;
795                                 }
796                         }
797                 }
798
799                 wm_area_mark_invalid_backbuf(sa);
800                 CTX_wm_area_set(C, NULL);
801         }
802
803         if (copytex) {
804                 wmSubWindowSet(win, screen->mainwin);
805
806                 wm_triple_copy_textures(win, triple_data);
807         }
808
809         if (wm->paintcursors.first) {
810                 for (sa = screen->areabase.first; sa; sa = sa->next) {
811                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
812                                 if (ar->swinid && ar->swinid == screen->subwinactive) {
813                                         CTX_wm_area_set(C, sa);
814                                         CTX_wm_region_set(C, ar);
815
816                                         /* make region ready for draw, scissor, pixelspace */
817                                         ED_region_set(C, ar);
818                                         wm_paintcursor_draw(C, ar);
819
820                                         CTX_wm_region_set(C, NULL);
821                                         CTX_wm_area_set(C, NULL);
822                                 }
823                         }
824                 }
825
826                 wmSubWindowSet(win, screen->mainwin);
827         }
828
829         /* draw overlapping area regions (always like popups) */
830         for (sa = screen->areabase.first; sa; sa = sa->next) {
831                 CTX_wm_area_set(C, sa);
832
833                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
834                         if (ar->swinid && ar->overlap) {
835                                 CTX_wm_region_set(C, ar);
836                                 ED_region_do_draw(C, ar);
837                                 if (sview == STEREO_RIGHT_ID)
838                                         ar->do_draw = false;
839                                 CTX_wm_region_set(C, NULL);
840
841                                 wm_draw_region_blend(win, ar, triple_data);
842                         }
843                 }
844
845                 CTX_wm_area_set(C, NULL);
846         }
847
848         /* after area regions so we can do area 'overlay' drawing */
849         ED_screen_draw_edges(win);
850         if (sview == STEREO_RIGHT_ID)
851                 screen->do_draw = false;
852
853         wm_draw_callbacks(win);
854
855         /* draw floating regions (menus) */
856         for (ar = screen->regionbase.first; ar; ar = ar->next) {
857                 if (ar->swinid) {
858                         CTX_wm_menu_set(C, ar);
859                         ED_region_do_draw(C, ar);
860                         if (sview == STEREO_RIGHT_ID)
861                                 ar->do_draw = false;
862                         CTX_wm_menu_set(C, NULL);
863                 }
864         }
865
866         /* always draw, not only when screen tagged */
867         if (win->gesture.first)
868                 wm_gesture_draw(win);
869
870         /* needs pixel coords in screen */
871         if (wm->drags.first) {
872                 wm_drags_draw(C, win, NULL);
873         }
874
875         /* copy the ui + overlays */
876         wmSubWindowSet(win, screen->mainwin);
877         wm_triple_copy_textures(win, triple_all);
878 }
879
880 /****************** main update call **********************/
881
882 /* quick test to prevent changing window drawable */
883 static bool wm_draw_update_test_window(wmWindow *win)
884 {
885         const Scene *scene = WM_window_get_active_scene(win);
886         const bScreen *screen = WM_window_get_active_screen(win);
887         ScrArea *sa;
888         ARegion *ar;
889         bool do_draw = false;
890
891         for (ar = screen->regionbase.first; ar; ar = ar->next) {
892                 if (ar->do_draw_overlay) {
893                         wm_tag_redraw_overlay(win, ar);
894                         ar->do_draw_overlay = false;
895                 }
896                 if (ar->swinid && ar->do_draw)
897                         do_draw = true;
898         }
899
900         for (sa = screen->areabase.first; sa; sa = sa->next) {
901                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
902                         wm_region_test_render_do_draw(scene, sa, ar);
903
904                         if (ar->swinid && ar->do_draw)
905                                 do_draw = true;
906                 }
907         }
908
909         if (do_draw)
910                 return true;
911         
912         if (screen->do_refresh)
913                 return true;
914         if (screen->do_draw)
915                 return true;
916         if (screen->do_draw_gesture)
917                 return true;
918         if (screen->do_draw_paintcursor)
919                 return true;
920         if (screen->do_draw_drag)
921                 return true;
922         
923         return false;
924 }
925
926 static int wm_automatic_draw_method(wmWindow *win)
927 {
928         /* We assume all supported GPUs now support triple buffer well. */
929         if (win->drawmethod == USER_DRAW_AUTOMATIC) {
930                 return USER_DRAW_TRIPLE;
931         }
932         else {
933                 return win->drawmethod;
934         }
935 }
936
937 bool WM_is_draw_triple(wmWindow *win)
938 {
939         /* function can get called before this variable is set in drawing code below */
940         if (win->drawmethod != U.wmdrawmethod)
941                 win->drawmethod = U.wmdrawmethod;
942         return (USER_DRAW_TRIPLE == wm_automatic_draw_method(win));
943 }
944
945 void wm_tag_redraw_overlay(wmWindow *win, ARegion *ar)
946 {
947         /* for draw triple gestures, paint cursors don't need region redraw */
948         if (ar && win) {
949                 bScreen *screen = WM_window_get_active_screen(win);
950
951                 if (wm_automatic_draw_method(win) != USER_DRAW_TRIPLE)
952                         ED_region_tag_redraw(ar);
953                 screen->do_draw_paintcursor = true;
954         }
955 }
956
957 void WM_paint_cursor_tag_redraw(wmWindow *win, ARegion *ar)
958 {
959         bScreen *screen = WM_window_get_active_screen(win);
960         screen->do_draw_paintcursor = true;
961         wm_tag_redraw_overlay(win, ar);
962 }
963
964 void wm_draw_update(bContext *C)
965 {
966         wmWindowManager *wm = CTX_wm_manager(C);
967         wmWindow *win;
968
969 #ifdef WITH_OPENSUBDIV
970         BKE_subsurf_free_unused_buffers();
971 #endif
972
973         GPU_free_unused_buffers();
974         
975         for (win = wm->windows.first; win; win = win->next) {
976 #ifdef WIN32
977                 GHOST_TWindowState state = GHOST_GetWindowState(win->ghostwin);
978
979                 if (state == GHOST_kWindowStateMinimized) {
980                         /* do not update minimized windows, gives issues on Intel (see T33223)
981                          * and AMD (see T50856). it seems logical to skip update for invisible
982                          * window anyway.
983                          */
984                         continue;
985                 }
986 #endif
987                 if (win->drawmethod != U.wmdrawmethod) {
988                         wm_draw_window_clear(win);
989                         win->drawmethod = U.wmdrawmethod;
990                 }
991
992                 if (wm_draw_update_test_window(win)) {
993                         bScreen *screen = WM_window_get_active_screen(win);
994
995                         CTX_wm_window_set(C, win);
996                         
997                         /* sets context window+screen */
998                         wm_window_make_drawable(wm, win);
999
1000                         /* notifiers for screen redraw */
1001                         if (screen->do_refresh)
1002                                 ED_screen_refresh(wm, win);
1003
1004                         int drawmethod = wm_automatic_draw_method(win);
1005
1006                         if (win->drawfail)
1007                                 wm_method_draw_overlap_all(C, win, 0);
1008                         else if (drawmethod == USER_DRAW_FULL)
1009                                 wm_method_draw_full(C, win);
1010                         else if (drawmethod == USER_DRAW_OVERLAP)
1011                                 wm_method_draw_overlap_all(C, win, 0);
1012                         else if (drawmethod == USER_DRAW_OVERLAP_FLIP)
1013                                 wm_method_draw_overlap_all(C, win, 1);
1014                         else { /* USER_DRAW_TRIPLE */
1015                                 if ((WM_stereo3d_enabled(win, false)) == false) {
1016                                         wm_method_draw_triple(C, win);
1017                                 }
1018                                 else {
1019                                         wm_method_draw_triple_multiview(C, win, STEREO_LEFT_ID);
1020                                         wm_method_draw_triple_multiview(C, win, STEREO_RIGHT_ID);
1021                                         wm_method_draw_stereo3d(C, win);
1022                                 }
1023                         }
1024
1025                         screen->do_draw_gesture = false;
1026                         screen->do_draw_paintcursor = false;
1027                         screen->do_draw_drag = false;
1028                 
1029                         wm_window_swap_buffers(win);
1030
1031                         CTX_wm_window_set(C, NULL);
1032                 }
1033         }
1034 }
1035
1036 void wm_draw_data_free(wmWindow *win)
1037 {
1038         wmDrawData *dd;
1039
1040         for (dd = win->drawdata.first; dd; dd = dd->next) {
1041                 wm_draw_triple_free(dd->triple);
1042         }
1043         BLI_freelistN(&win->drawdata);
1044 }
1045
1046 void wm_draw_window_clear(wmWindow *win)
1047 {
1048         bScreen *screen = WM_window_get_active_screen(win);
1049         ScrArea *sa;
1050         ARegion *ar;
1051
1052         wm_draw_data_free(win);
1053
1054         /* clear screen swap flags */
1055         if (screen) {
1056                 for (sa = screen->areabase.first; sa; sa = sa->next)
1057                         for (ar = sa->regionbase.first; ar; ar = ar->next)
1058                                 ar->swap = WIN_NONE_OK;
1059                 
1060                 screen->swap = WIN_NONE_OK;
1061         }
1062 }
1063
1064 void wm_draw_region_clear(wmWindow *win, ARegion *ar)
1065 {
1066         bScreen *screen = WM_window_get_active_screen(win);
1067         int drawmethod = wm_automatic_draw_method(win);
1068
1069         if (ELEM(drawmethod, USER_DRAW_OVERLAP, USER_DRAW_OVERLAP_FLIP))
1070                 wm_flush_regions_down(screen, &ar->winrct);
1071
1072         screen->do_draw = true;
1073 }
1074
1075 void WM_redraw_windows(bContext *C)
1076 {
1077         wmWindow *win_prev = CTX_wm_window(C);
1078         ScrArea *area_prev = CTX_wm_area(C);
1079         ARegion *ar_prev = CTX_wm_region(C);
1080
1081         wm_draw_update(C);
1082
1083         CTX_wm_window_set(C, win_prev);
1084         CTX_wm_area_set(C, area_prev);
1085         CTX_wm_region_set(C, ar_prev);
1086 }
1087