2.5
[blender-staging.git] / source / blender / windowmanager / intern / wm_draw.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <GL/glew.h>
32
33 #include "DNA_listBase.h"
34 #include "DNA_screen_types.h"
35 #include "DNA_windowmanager_types.h"
36 #include "DNA_userdef_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41
42 #include "BKE_context.h"
43 #include "BKE_global.h"
44
45 #include "ED_screen.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49 #include "wm.h"
50 #include "wm_draw.h"
51 #include "wm_window.h"
52 #include "wm_event_system.h"
53
54 /* swap */
55 #define WIN_NONE_OK             0
56 #define WIN_BACK_OK     1
57 #define WIN_FRONT_OK    2
58 #define WIN_BOTH_OK             3
59
60 /* ********************* drawing, swap ****************** */
61
62 static void wm_paintcursor_draw(bContext *C, ARegion *ar)
63 {
64         wmWindowManager *wm= CTX_wm_manager(C);
65         
66         if(wm->paintcursors.first) {
67                 wmWindow *win= CTX_wm_window(C);
68                 bScreen *screen= win->screen;
69                 wmPaintCursor *pc;
70
71                 if(screen->subwinactive == ar->swinid) {
72                         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
73                                 if(pc->poll(C)) {
74                                         ARegion *ar= CTX_wm_region(C);
75                                         pc->draw(C, win->eventstate->x - ar->winrct.xmin, win->eventstate->y - ar->winrct.ymin, pc->customdata);
76                                 }
77                         }
78                 }
79         }
80 }
81
82 /********************** draw all **************************/
83 /* - reference method, draw all each time                 */
84
85 static void wm_method_draw_full(bContext *C, wmWindow *win)
86 {
87         bScreen *screen= win->screen;
88         ScrArea *sa;
89         ARegion *ar;
90
91         /* draw area regions */
92         for(sa= screen->areabase.first; sa; sa= sa->next) {
93                 CTX_wm_area_set(C, sa);
94
95                 for(ar=sa->regionbase.first; ar; ar= ar->next) {
96                         if(ar->swinid) {
97                                 CTX_wm_region_set(C, ar);
98                                 ED_region_do_draw(C, ar);
99                                 wm_paintcursor_draw(C, ar);
100                                 ED_area_overdraw_flush(C, sa, ar);
101                                 CTX_wm_region_set(C, NULL);
102                         }
103                 }
104                 
105                 CTX_wm_area_set(C, NULL);
106         }
107
108         ED_screen_draw(win);
109         ED_area_overdraw(C);
110
111         /* draw overlapping regions */
112         for(ar=screen->regionbase.first; ar; ar= ar->next) {
113                 if(ar->swinid) {
114                         CTX_wm_menu_set(C, ar);
115                         ED_region_do_draw(C, ar);
116                         CTX_wm_menu_set(C, NULL);
117                 }
118         }
119
120         if(screen->do_draw_gesture)
121                 wm_gesture_draw(win);
122 }
123
124 /****************** draw overlap all **********************/
125 /* - redraw marked areas, and anything that overlaps it   */
126 /* - it also handles swap exchange optionally, assuming   */
127 /*   that on swap no clearing happens and we get back the */
128 /*   same buffer as we swapped to the front               */
129
130 /* mark area-regions to redraw if overlapped with rect */
131 static void wm_flush_regions_down(bScreen *screen, rcti *dirty)
132 {
133         ScrArea *sa;
134         ARegion *ar;
135
136         for(sa= screen->areabase.first; sa; sa= sa->next) {
137                 for(ar= sa->regionbase.first; ar; ar= ar->next) {
138                         if(BLI_isect_rcti(dirty, &ar->winrct, NULL)) {
139                                 ar->do_draw= 1;
140                                 memset(&ar->drawrct, 0, sizeof(ar->drawrct));
141                                 ar->swap= WIN_NONE_OK;
142                         }
143                 }
144         }
145 }
146
147 /* mark menu-regions to redraw if overlapped with rect */
148 static void wm_flush_regions_up(bScreen *screen, rcti *dirty)
149 {
150         ARegion *ar;
151         
152         for(ar= screen->regionbase.first; ar; ar= ar->next) {
153                 if(BLI_isect_rcti(dirty, &ar->winrct, NULL)) {
154                         ar->do_draw= 1;
155                         memset(&ar->drawrct, 0, sizeof(ar->drawrct));
156                         ar->swap= WIN_NONE_OK;
157                 }
158         }
159 }
160
161 static void wm_method_draw_overlap_all(bContext *C, wmWindow *win)
162 {
163         bScreen *screen= win->screen;
164         ScrArea *sa;
165         ARegion *ar;
166         int exchange= (G.f & G_SWAP_EXCHANGE);
167
168         /* flush overlapping regions */
169         if(screen->regionbase.first) {
170                 /* flush redraws of area regions up to overlapping regions */
171                 for(sa= screen->areabase.first; sa; sa= sa->next)
172                         for(ar= sa->regionbase.first; ar; ar= ar->next)
173                                 if(ar->swinid && ar->do_draw)
174                                         wm_flush_regions_up(screen, &ar->winrct);
175                 
176                 /* flush between overlapping regions */
177                 for(ar= screen->regionbase.last; ar; ar= ar->prev)
178                         if(ar->swinid && ar->do_draw)
179                                 wm_flush_regions_up(screen, &ar->winrct);
180                 
181                 /* flush redraws of overlapping regions down to area regions */
182                 for(ar= screen->regionbase.last; ar; ar= ar->prev)
183                         if(ar->swinid && ar->do_draw)
184                                 wm_flush_regions_down(screen, &ar->winrct);
185         }
186
187         /* draw marked area regions */
188         for(sa= screen->areabase.first; sa; sa= sa->next) {
189                 CTX_wm_area_set(C, sa);
190
191                 for(ar=sa->regionbase.first; ar; ar= ar->next) {
192                         if(ar->swinid) {
193                                 if(ar->do_draw) {
194                                         CTX_wm_region_set(C, ar);
195                                         ED_region_do_draw(C, ar);
196                                         wm_paintcursor_draw(C, ar);
197                                         ED_area_overdraw_flush(C, sa, ar);
198                                         CTX_wm_region_set(C, NULL);
199
200                                         if(exchange)
201                                                 ar->swap= WIN_FRONT_OK;
202                                 }
203                                 else if(exchange) {
204                                         if(ar->swap == WIN_FRONT_OK) {
205                                                 CTX_wm_region_set(C, ar);
206                                                 ED_region_do_draw(C, ar);
207                                                 wm_paintcursor_draw(C, ar);
208                                                 ED_area_overdraw_flush(C, sa, ar);
209                                                 CTX_wm_region_set(C, NULL);
210
211                                                 ar->swap= WIN_BOTH_OK;
212                                                 printf("draws swap exchange %d\n", ar->swinid);
213                                         }
214                                         else if(ar->swap == WIN_BACK_OK)
215                                                 ar->swap= WIN_FRONT_OK;
216                                         else if(ar->swap == WIN_BOTH_OK)
217                                                 ar->swap= WIN_BOTH_OK;
218                                 }
219                         }
220                 }
221                 
222                 CTX_wm_area_set(C, NULL);
223         }
224
225         /* after area regions so we can do area 'overlay' drawing */
226         if(screen->do_draw) {
227                 ED_screen_draw(win);
228
229                 if(exchange)
230                         screen->swap= WIN_FRONT_OK;
231         }
232         else if(exchange) {
233                 if(screen->swap==WIN_FRONT_OK) {
234                         ED_screen_draw(win);
235                         screen->swap= WIN_BOTH_OK;
236                 }
237                 else if(screen->swap==WIN_BACK_OK)
238                         screen->swap= WIN_FRONT_OK;
239                 else if(screen->swap==WIN_BOTH_OK)
240                         screen->swap= WIN_BOTH_OK;
241         }
242
243         ED_area_overdraw(C);
244
245         /* draw marked overlapping regions */
246         for(ar=screen->regionbase.first; ar; ar= ar->next) {
247                 if(ar->swinid && ar->do_draw) {
248                         CTX_wm_menu_set(C, ar);
249                         ED_region_do_draw(C, ar);
250                         CTX_wm_menu_set(C, NULL);
251                 }
252         }
253
254         if(screen->do_draw_gesture)
255                 wm_gesture_draw(win);
256 }
257
258 #if 0
259 /******************** draw damage ************************/
260 /* - not implemented                                      */
261
262 static void wm_method_draw_damage(bContext *C, wmWindow *win)
263 {
264         wm_method_draw_all(C, win);
265 }
266 #endif
267
268 /****************** draw triple buffer ********************/
269 /* - area regions are written into a texture, without any */
270 /*   of the overlapping menus, brushes, gestures. these   */
271 /*   are redrawn each time.                               */
272 /*                                                        */
273 /* - if non-power of two textures are supported, that is  */
274 /*   used. if not, multiple smaller ones are used, with   */
275 /*   worst case wasted space being 23.4% for 3x3 textures */
276
277 #define MAX_N_TEX 3
278
279 typedef struct wmDrawTriple {
280         GLuint bind[MAX_N_TEX*MAX_N_TEX];
281         int x[MAX_N_TEX], y[MAX_N_TEX];
282         int nx, ny;
283         GLenum target;
284 } wmDrawTriple;
285
286 static int is_pow2(int n)
287 {
288         return ((n)&(n-1))==0;
289 }
290
291 static int smaller_pow2(int n)
292 {
293     while (!is_pow2(n))
294                 n= n&(n-1);
295
296         return n;
297 }
298
299 static int larger_pow2(int n)
300 {
301         if (is_pow2(n))
302                 return n;
303
304         while(!is_pow2(n))
305                 n= n&(n-1);
306
307         return n*2;
308 }
309
310 static void split_width(int x, int n, int *splitx, int *nx)
311 {
312         int a, newnx, waste;
313
314         /* if already power of two just use it */
315         if(is_pow2(x)) {
316                 splitx[0]= x;
317                 (*nx)++;
318                 return;
319         }
320
321         if(n == 1) {
322                 /* last part, we have to go larger */
323                 splitx[0]= larger_pow2(x);
324                 (*nx)++;
325         }
326         else {
327                 /* two or more parts to go, use smaller part */
328                 splitx[0]= smaller_pow2(x);
329                 newnx= ++(*nx);
330                 split_width(x-splitx[0], n-1, splitx+1, &newnx);
331
332                 for(waste=0, a=0; a<n; a++)
333                         waste += splitx[a];
334
335                 /* if we waste more space or use the same amount,
336                  * revert deeper splits and just use larger */
337                 if(waste >= larger_pow2(x)) {
338                         splitx[0]= larger_pow2(x);
339                         memset(splitx+1, 0, sizeof(int)*(n-1));
340                 }
341                 else
342                         *nx= newnx;
343         }
344 }
345
346 static void wm_draw_triple_free(wmWindow *win)
347 {
348         if(win->drawdata) {
349                 wmDrawTriple *triple= win->drawdata;
350
351                 glDeleteTextures(triple->nx*triple->ny, triple->bind);
352                 MEM_freeN(triple);
353
354                 win->drawdata= NULL;
355         }
356 }
357
358 static void wm_draw_triple_fail(bContext *C, wmWindow *win)
359 {
360         wm_draw_window_clear(win);
361
362         win->drawfail= 1;
363         wm_method_draw_overlap_all(C, win);
364 }
365
366 static int wm_triple_gen_textures(wmWindow *win, wmDrawTriple *triple)
367 {
368         GLint maxsize;
369         int x, y;
370
371         /* compute texture sizes */
372         if(GLEW_ARB_texture_rectangle || GLEW_NV_texture_rectangle || GLEW_EXT_texture_rectangle) {
373                 triple->target= GL_TEXTURE_RECTANGLE_ARB;
374                 triple->nx= 1;
375                 triple->ny= 1;
376                 triple->x[0]= win->sizex;
377                 triple->y[0]= win->sizey;
378         }
379         else if(GLEW_ARB_texture_non_power_of_two) {
380                 triple->target= GL_TEXTURE_2D;
381                 triple->nx= 1;
382                 triple->ny= 1;
383                 triple->x[0]= win->sizex;
384                 triple->y[0]= win->sizey;
385         }
386         else {
387                 triple->target= GL_TEXTURE_2D;
388                 triple->nx= 0;
389                 triple->ny= 0;
390                 split_width(win->sizex, MAX_N_TEX, triple->x, &triple->nx);
391                 split_width(win->sizey, MAX_N_TEX, triple->y, &triple->ny);
392         }
393
394         /* generate texture names */
395         glGenTextures(triple->nx*triple->ny, triple->bind);
396
397         if(!triple->bind[0]) {
398                 /* not the typical failure case but we handle it anyway */
399                 printf("WM: failed to allocate texture for triple buffer drawing (glGenTextures).\n");
400                 return 0;
401         }
402
403         for(y=0; y<triple->ny; y++) {
404                 for(x=0; x<triple->nx; x++) {
405                         /* proxy texture is only guaranteed to test for the cases that
406                          * there is only one texture in use, which may not be the case */
407                         glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxsize);
408
409                         if(triple->x[x] > maxsize || triple->y[y] > maxsize) {
410                                 glBindTexture(triple->target, 0);
411                                 printf("WM: failed to allocate texture for triple buffer drawing (texture too large for graphics card).\n");
412                                 return 0;
413                         }
414
415                         /* setup actual texture */
416                         glBindTexture(triple->target, triple->bind[x + y*triple->nx]);
417                         glTexImage2D(triple->target, 0, GL_RGB8, triple->x[x], triple->y[y], 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
418                         glTexParameteri(triple->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
419                         glTexParameteri(triple->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
420                         // glColor still used with this enabled?
421                         // glTexEnvi(triple->target, GL_TEXTURE_ENV_MODE, GL_REPLACE);
422                         glBindTexture(triple->target, 0);
423
424                         /* not sure if this works everywhere .. */
425                         if(glGetError() == GL_OUT_OF_MEMORY) {
426                                 printf("WM: failed to allocate texture for triple buffer drawing (out of memory).\n");
427                                 return 0;
428                         }
429                 }
430         }
431
432         return 1;
433 }
434
435 static void wm_triple_draw_textures(wmWindow *win, wmDrawTriple *triple)
436 {
437         float halfx, halfy, ratiox, ratioy;
438         int x, y, sizex, sizey, offx, offy;
439
440         glEnable(triple->target);
441
442         for(y=0, offy=0; y<triple->ny; offy+=triple->y[y], y++) {
443                 for(x=0, offx=0; x<triple->nx; offx+=triple->x[x], x++) {
444                         sizex= (x == triple->nx-1)? win->sizex-offx: triple->x[x];
445                         sizey= (y == triple->ny-1)? win->sizey-offy: triple->y[y];
446
447                         /* wmOrtho for the screen has this same offset */
448                         ratiox= sizex;
449                         ratioy= sizey;
450                         halfx= 0.375f;
451                         halfy= 0.375f;
452
453                         /* texture rectangle has unnormalized coordinates */
454                         if(triple->target == GL_TEXTURE_2D) {
455                                 ratiox /= triple->x[x];
456                                 ratioy /= triple->y[y];
457                                 halfx /= triple->x[x];
458                                 halfy /= triple->y[y];
459                         }
460
461                         glBindTexture(triple->target, triple->bind[x + y*triple->nx]);
462
463                         glColor3f(1.0f, 1.0f, 1.0f);
464                         glBegin(GL_QUADS);
465                                 glTexCoord2f(halfx, halfy);
466                                 glVertex2f(offx, offy);
467
468                                 glTexCoord2f(ratiox+halfx, halfy);
469                                 glVertex2f(offx+sizex, offy);
470
471                                 glTexCoord2f(ratiox+halfx, ratioy+halfy);
472                                 glVertex2f(offx+sizex, offy+sizey);
473
474                                 glTexCoord2f(halfx, ratioy+halfy);
475                                 glVertex2f(offx, offy+sizey);
476                         glEnd();
477                 }
478         }
479
480         glBindTexture(triple->target, 0);
481         glDisable(triple->target);
482 }
483
484 static void wm_triple_copy_textures(wmWindow *win, wmDrawTriple *triple)
485 {
486         int x, y, sizex, sizey, offx, offy;
487
488         for(y=0, offy=0; y<triple->ny; offy+=triple->y[y], y++) {
489                 for(x=0, offx=0; x<triple->nx; offx+=triple->x[x], x++) {
490                         sizex= (x == triple->nx-1)? win->sizex-offx: triple->x[x];
491                         sizey= (y == triple->ny-1)? win->sizey-offy: triple->y[y];
492
493                         glBindTexture(triple->target, triple->bind[x + y*triple->nx]);
494                         glCopyTexSubImage2D(triple->target, 0, 0, 0, offx, offy, sizex, sizey);
495                 }
496         }
497
498         glBindTexture(triple->target, 0);
499 }
500
501 static void wm_method_draw_triple(bContext *C, wmWindow *win)
502 {
503         wmWindowManager *wm= CTX_wm_manager(C);
504         wmDrawTriple *triple;
505         bScreen *screen= win->screen;
506         ScrArea *sa;
507         ARegion *ar;
508         int copytex= 0;
509
510         if(win->drawdata) {
511                 glClearColor(0, 0, 0, 0);
512                 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
513
514                 wmSubWindowSet(win, screen->mainwin);
515
516                 wm_triple_draw_textures(win, win->drawdata);
517
518                 triple= win->drawdata;
519         }
520         else {
521                 win->drawdata= MEM_callocN(sizeof(wmDrawTriple), "wmDrawTriple");
522
523                 if(!wm_triple_gen_textures(win, win->drawdata)) {
524                         wm_draw_triple_fail(C, win);
525                         return;
526                 }
527         }
528
529         triple= win->drawdata;
530
531         /* draw marked area regions */
532         for(sa= screen->areabase.first; sa; sa= sa->next) {
533                 CTX_wm_area_set(C, sa);
534
535                 for(ar=sa->regionbase.first; ar; ar= ar->next) {
536                         if(ar->swinid && ar->do_draw) {
537                                 CTX_wm_region_set(C, ar);
538                                 ED_region_do_draw(C, ar);
539                                 ED_area_overdraw_flush(C, sa, ar);
540                                 CTX_wm_region_set(C, NULL);
541                                 copytex= 1;
542                         }
543                 }
544                 
545                 CTX_wm_area_set(C, NULL);
546         }
547
548         if(copytex) {
549                 wmSubWindowSet(win, screen->mainwin);
550                 ED_area_overdraw(C);
551
552                 wm_triple_copy_textures(win, triple);
553         }
554
555         /* after area regions so we can do area 'overlay' drawing */
556         ED_screen_draw(win);
557
558         /* draw overlapping regions */
559         for(ar=screen->regionbase.first; ar; ar= ar->next) {
560                 if(ar->swinid) {
561                         CTX_wm_menu_set(C, ar);
562                         ED_region_do_draw(C, ar);
563                         CTX_wm_menu_set(C, NULL);
564                 }
565         }
566
567         if(win->screen->do_draw_gesture)
568                 wm_gesture_draw(win);
569
570         if(wm->paintcursors.first) {
571                 for(sa= screen->areabase.first; sa; sa= sa->next) {
572                         for(ar=sa->regionbase.first; ar; ar= ar->next) {
573                                 if(ar->swinid == screen->subwinactive) {
574                                         CTX_wm_area_set(C, sa);
575                                         CTX_wm_region_set(C, ar);
576
577                                         /* make region ready for draw, scissor, pixelspace */
578                                         ED_region_set(C, ar);
579                                         wm_paintcursor_draw(C, ar);
580
581                                         CTX_wm_region_set(C, NULL);
582                                         CTX_wm_area_set(C, NULL);
583                                 }
584                         }
585                 }
586
587                 wmSubWindowSet(win, screen->mainwin);
588         }
589 }
590
591 /****************** main update call **********************/
592
593 /* quick test to prevent changing window drawable */
594 static int wm_draw_update_test_window(wmWindow *win)
595 {
596         ScrArea *sa;
597         ARegion *ar;
598         
599         if(win->screen->do_refresh)
600                 return 1;
601         if(win->screen->do_draw)
602                 return 1;
603         if(win->screen->do_draw_gesture)
604                 return 1;
605         if(win->screen->do_draw_paintcursor)
606                 return 1;
607         
608         for(ar= win->screen->regionbase.first; ar; ar= ar->next)
609                 if(ar->swinid && ar->do_draw)
610                         return 1;
611                 
612         for(sa= win->screen->areabase.first; sa; sa= sa->next)
613                 for(ar=sa->regionbase.first; ar; ar= ar->next)
614                         if(ar->swinid && ar->do_draw)
615                                 return 1;
616
617         return 0;
618 }
619
620 void wm_draw_update(bContext *C)
621 {
622         wmWindowManager *wm= CTX_wm_manager(C);
623         wmWindow *win;
624         
625         for(win= wm->windows.first; win; win= win->next) {
626                 if(win->drawmethod != U.wmdrawmethod) {
627                         wm_draw_window_clear(win);
628                         win->drawmethod= U.wmdrawmethod;
629                 }
630
631                 if(wm_draw_update_test_window(win)) {
632                         CTX_wm_window_set(C, win);
633                         
634                         /* sets context window+screen */
635                         wm_window_make_drawable(C, win);
636
637                         /* notifiers for screen redraw */
638                         if(win->screen->do_refresh)
639                                 ED_screen_refresh(wm, win);
640
641                         if(win->drawfail)
642                                 wm_method_draw_overlap_all(C, win);
643                         else if(win->drawmethod == USER_DRAW_FULL)
644                                 wm_method_draw_full(C, win);
645                         else if(win->drawmethod == USER_DRAW_OVERLAP)
646                                 wm_method_draw_overlap_all(C, win);
647                         /*else if(win->drawmethod == USER_DRAW_DAMAGE)
648                                 wm_method_draw_damage(C, win);*/
649                         else // if(win->drawmethod == USER_DRAW_TRIPLE)
650                                 wm_method_draw_triple(C, win);
651
652                         win->screen->do_draw_gesture= 0;
653                         win->screen->do_draw_paintcursor= 0;
654                 
655                         wm_window_swap_buffers(win);
656
657                         CTX_wm_window_set(C, NULL);
658                 }
659         }
660 }
661
662 void wm_draw_window_clear(wmWindow *win)
663 {
664         bScreen *screen= win->screen;
665         ScrArea *sa;
666         ARegion *ar;
667
668         if(win->drawmethod == USER_DRAW_TRIPLE)
669                 wm_draw_triple_free(win);
670
671         /* clear screen swap flags */
672         if(screen) {
673                 for(sa= screen->areabase.first; sa; sa= sa->next)
674                         for(ar=sa->regionbase.first; ar; ar= ar->next)
675                                 ar->swap= WIN_NONE_OK;
676                 
677                 screen->swap= WIN_NONE_OK;
678         }
679 }
680
681 void wm_draw_region_clear(wmWindow *win, ARegion *ar)
682 {
683         if(win->drawmethod == USER_DRAW_OVERLAP)
684                 wm_flush_regions_down(win->screen, &ar->winrct);
685
686         win->screen->do_draw= 1;
687 }
688