Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / screen / screen_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  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/editors/screen/screen_draw.c
22  *  \ingroup edscr
23  */
24
25 #include "ED_screen.h"
26
27 #include "GPU_batch_presets.h"
28 #include "GPU_extensions.h"
29 #include "GPU_framebuffer.h"
30 #include "GPU_immediate.h"
31 #include "GPU_matrix.h"
32 #include "GPU_state.h"
33
34 #include "BLI_math.h"
35 #include "BLI_rect.h"
36
37 #include "WM_api.h"
38 #include "WM_types.h"
39
40 #include "UI_interface.h"
41 #include "UI_resources.h"
42
43 #include "screen_intern.h"
44
45 /**
46  * Draw horizontal shape visualizing future joining (left as well right direction of future joining).
47  */
48 static void draw_horizontal_join_shape(ScrArea *sa, char dir, unsigned int pos)
49 {
50         const float width = screen_geom_area_width(sa) - 1;
51         const float height = screen_geom_area_height(sa) - 1;
52         vec2f points[10];
53         short i;
54         float w, h;
55
56         if (height < width) {
57                 h = height / 8;
58                 w = height / 4;
59         }
60         else {
61                 h = width / 8;
62                 w = width / 4;
63         }
64
65         points[0].x = sa->v1->vec.x;
66         points[0].y = sa->v1->vec.y + height / 2;
67
68         points[1].x = sa->v1->vec.x;
69         points[1].y = sa->v1->vec.y;
70
71         points[2].x = sa->v4->vec.x - w;
72         points[2].y = sa->v4->vec.y;
73
74         points[3].x = sa->v4->vec.x - w;
75         points[3].y = sa->v4->vec.y + height / 2 - 2 * h;
76
77         points[4].x = sa->v4->vec.x - 2 * w;
78         points[4].y = sa->v4->vec.y + height / 2;
79
80         points[5].x = sa->v4->vec.x - w;
81         points[5].y = sa->v4->vec.y + height / 2 + 2 * h;
82
83         points[6].x = sa->v3->vec.x - w;
84         points[6].y = sa->v3->vec.y;
85
86         points[7].x = sa->v2->vec.x;
87         points[7].y = sa->v2->vec.y;
88
89         points[8].x = sa->v4->vec.x;
90         points[8].y = sa->v4->vec.y + height / 2 - h;
91
92         points[9].x = sa->v4->vec.x;
93         points[9].y = sa->v4->vec.y + height / 2 + h;
94
95         if (dir == 'l') {
96                 /* when direction is left, then we flip direction of arrow */
97                 float cx = sa->v1->vec.x + width;
98                 for (i = 0; i < 10; i++) {
99                         points[i].x -= cx;
100                         points[i].x = -points[i].x;
101                         points[i].x += sa->v1->vec.x;
102                 }
103         }
104
105         immBegin(GPU_PRIM_TRI_FAN, 5);
106
107         for (i = 0; i < 5; i++) {
108                 immVertex2f(pos, points[i].x, points[i].y);
109         }
110
111         immEnd();
112
113         immBegin(GPU_PRIM_TRI_FAN, 5);
114
115         for (i = 4; i < 8; i++) {
116                 immVertex2f(pos, points[i].x, points[i].y);
117         }
118
119         immVertex2f(pos, points[0].x, points[0].y);
120         immEnd();
121
122         immRectf(pos, points[2].x, points[2].y, points[8].x, points[8].y);
123         immRectf(pos, points[6].x, points[6].y, points[9].x, points[9].y);
124 }
125
126 /**
127  * Draw vertical shape visualizing future joining (up/down direction).
128  */
129 static void draw_vertical_join_shape(ScrArea *sa, char dir, unsigned int pos)
130 {
131         const float width = screen_geom_area_width(sa) - 1;
132         const float height = screen_geom_area_height(sa) - 1;
133         vec2f points[10];
134         short i;
135         float w, h;
136
137         if (height < width) {
138                 h = height / 4;
139                 w = height / 8;
140         }
141         else {
142                 h = width / 4;
143                 w = width / 8;
144         }
145
146         points[0].x = sa->v1->vec.x + width / 2;
147         points[0].y = sa->v3->vec.y;
148
149         points[1].x = sa->v2->vec.x;
150         points[1].y = sa->v2->vec.y;
151
152         points[2].x = sa->v1->vec.x;
153         points[2].y = sa->v1->vec.y + h;
154
155         points[3].x = sa->v1->vec.x + width / 2 - 2 * w;
156         points[3].y = sa->v1->vec.y + h;
157
158         points[4].x = sa->v1->vec.x + width / 2;
159         points[4].y = sa->v1->vec.y + 2 * h;
160
161         points[5].x = sa->v1->vec.x + width / 2 + 2 * w;
162         points[5].y = sa->v1->vec.y + h;
163
164         points[6].x = sa->v4->vec.x;
165         points[6].y = sa->v4->vec.y + h;
166
167         points[7].x = sa->v3->vec.x;
168         points[7].y = sa->v3->vec.y;
169
170         points[8].x = sa->v1->vec.x + width / 2 - w;
171         points[8].y = sa->v1->vec.y;
172
173         points[9].x = sa->v1->vec.x + width / 2 + w;
174         points[9].y = sa->v1->vec.y;
175
176         if (dir == 'u') {
177                 /* when direction is up, then we flip direction of arrow */
178                 float cy = sa->v1->vec.y + height;
179                 for (i = 0; i < 10; i++) {
180                         points[i].y -= cy;
181                         points[i].y = -points[i].y;
182                         points[i].y += sa->v1->vec.y;
183                 }
184         }
185
186         immBegin(GPU_PRIM_TRI_FAN, 5);
187
188         for (i = 0; i < 5; i++) {
189                 immVertex2f(pos, points[i].x, points[i].y);
190         }
191
192         immEnd();
193
194         immBegin(GPU_PRIM_TRI_FAN, 5);
195
196         for (i = 4; i < 8; i++) {
197                 immVertex2f(pos, points[i].x, points[i].y);
198         }
199
200         immVertex2f(pos, points[0].x, points[0].y);
201         immEnd();
202
203         immRectf(pos, points[2].x, points[2].y, points[8].x, points[8].y);
204         immRectf(pos, points[6].x, points[6].y, points[9].x, points[9].y);
205 }
206
207 /**
208  * Draw join shape due to direction of joining.
209  */
210 static void draw_join_shape(ScrArea *sa, char dir, unsigned int pos)
211 {
212         if (dir == 'u' || dir == 'd') {
213                 draw_vertical_join_shape(sa, dir, pos);
214         }
215         else {
216                 draw_horizontal_join_shape(sa, dir, pos);
217         }
218 }
219
220 #define CORNER_RESOLUTION 3
221
222 static void do_vert_pair(GPUVertBuf *vbo, uint pos, uint *vidx, int corner, int i)
223 {
224         float inter[2], exter[2];
225         inter[0] = cosf(corner * M_PI_2 + (i * M_PI_2 / (CORNER_RESOLUTION - 1.0f)));
226         inter[1] = sinf(corner * M_PI_2 + (i * M_PI_2 / (CORNER_RESOLUTION - 1.0f)));
227
228         /* Snap point to edge */
229         float div = 1.0f / max_ff(fabsf(inter[0]), fabsf(inter[1]));
230         mul_v2_v2fl(exter, inter, div);
231         exter[0] = roundf(exter[0]);
232         exter[1] = roundf(exter[1]);
233
234         if (i == 0 || i == (CORNER_RESOLUTION - 1)) {
235                 copy_v2_v2(inter, exter);
236         }
237
238         /* Line width is 20% of the entire corner size. */
239         const float line_width = 0.2f; /* Keep in sync with shader */
240         mul_v2_fl(inter, 1.0f - line_width);
241         mul_v2_fl(exter, 1.0f + line_width);
242
243         switch (corner) {
244                 case 0:
245                         add_v2_v2(inter, (float[2]){-1.0f, -1.0f});
246                         add_v2_v2(exter, (float[2]){-1.0f, -1.0f});
247                         break;
248                 case 1:
249                         add_v2_v2(inter, (float[2]){1.0f, -1.0f});
250                         add_v2_v2(exter, (float[2]){1.0f, -1.0f});
251                         break;
252                 case 2:
253                         add_v2_v2(inter, (float[2]){1.0f, 1.0f});
254                         add_v2_v2(exter, (float[2]){1.0f, 1.0f});
255                         break;
256                 case 3:
257                         add_v2_v2(inter, (float[2]){-1.0f, 1.0f});
258                         add_v2_v2(exter, (float[2]){-1.0f, 1.0f});
259                         break;
260         }
261
262         GPU_vertbuf_attr_set(vbo, pos, (*vidx)++, inter);
263         GPU_vertbuf_attr_set(vbo, pos, (*vidx)++, exter);
264 }
265
266 static GPUBatch *batch_screen_edges_get(int *corner_len)
267 {
268         static GPUBatch *screen_edges_batch = NULL;
269
270         if (screen_edges_batch == NULL) {
271                 GPUVertFormat format = {0};
272                 uint pos = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
273
274                 GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);
275                 GPU_vertbuf_data_alloc(vbo, CORNER_RESOLUTION * 2 * 4 + 2);
276
277                 uint vidx = 0;
278                 for (int corner = 0; corner < 4; ++corner) {
279                         for (int c = 0; c < CORNER_RESOLUTION; ++c) {
280                                 do_vert_pair(vbo, pos, &vidx, corner, c);
281                         }
282                 }
283                 /* close the loop */
284                 do_vert_pair(vbo, pos, &vidx, 0, 0);
285
286                 screen_edges_batch = GPU_batch_create_ex(GPU_PRIM_TRI_STRIP, vbo, NULL, GPU_BATCH_OWNS_VBO);
287                 gpu_batch_presets_register(screen_edges_batch);
288         }
289
290         if (corner_len) {
291                 *corner_len = CORNER_RESOLUTION * 2;
292         }
293         return screen_edges_batch;
294 }
295
296 #undef CORNER_RESOLUTION
297
298 /**
299  * Draw screen area darker with arrow (visualization of future joining).
300  */
301 static void scrarea_draw_shape_dark(ScrArea *sa, char dir, unsigned int pos)
302 {
303         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
304         immUniformColor4ub(0, 0, 0, 50);
305
306         draw_join_shape(sa, dir, pos);
307 }
308
309 /**
310  * Draw screen area lighter with arrow shape ("eraser" of previous dark shape).
311  */
312 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir), unsigned int pos)
313 {
314         GPU_blend_set_func(GPU_DST_COLOR, GPU_SRC_ALPHA);
315         /* value 181 was hardly computed: 181~105 */
316         immUniformColor4ub(255, 255, 255, 50);
317         /* draw_join_shape(sa, dir); */
318
319         immRectf(pos, sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
320 }
321
322 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, float edge_thickness)
323 {
324         rctf rect;
325         BLI_rctf_init(&rect, (float)x1, (float)x2, (float)y1, (float)y2);
326
327         /* right border area */
328         if (x2 >= sizex - 1) {
329                 rect.xmax += edge_thickness * 0.5f;
330         }
331
332         /* left border area */
333         if (x1 <= 0) { /* otherwise it draws the emboss of window over */
334                 rect.xmin -= edge_thickness * 0.5f;
335         }
336
337         /* top border area */
338         if (y2 >= sizey - 1) {
339                 rect.ymax += edge_thickness * 0.5f;
340         }
341
342         /* bottom border area */
343         if (y1 <= 0) {
344                 rect.ymin -= edge_thickness * 0.5f;
345         }
346
347         GPUBatch *batch = batch_screen_edges_get(NULL);
348         GPU_batch_uniform_4fv(batch, "rect", (float *)&rect);
349         GPU_batch_draw(batch);
350 }
351
352 /**
353  * \brief Screen edges drawing.
354  */
355 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, float edge_thickness)
356 {
357         short x1 = sa->v1->vec.x;
358         short y1 = sa->v1->vec.y;
359         short x2 = sa->v3->vec.x;
360         short y2 = sa->v3->vec.y;
361
362         drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, edge_thickness);
363 }
364
365 /**
366  * Only for edge lines between areas.
367  */
368 void ED_screen_draw_edges(wmWindow *win)
369 {
370         bScreen *screen = WM_window_get_active_screen(win);
371         screen->do_draw = false;
372
373         if (screen->state == SCREENFULL) {
374                 return;
375         }
376
377         const int winsize_x = WM_window_pixels_x(win);
378         const int winsize_y = WM_window_pixels_y(win);
379         float col[4], corner_scale, edge_thickness;
380         int verts_per_corner = 0;
381
382         ScrArea *sa;
383
384         rcti scissor_rect;
385         BLI_rcti_init_minmax(&scissor_rect);
386         for (sa = screen->areabase.first; sa; sa = sa->next) {
387                 BLI_rcti_do_minmax_v(&scissor_rect, (int[2]){sa->v1->vec.x, sa->v1->vec.y});
388                 BLI_rcti_do_minmax_v(&scissor_rect, (int[2]){sa->v3->vec.x, sa->v3->vec.y});
389         }
390
391         if (GPU_type_matches(GPU_DEVICE_INTEL_UHD, GPU_OS_UNIX, GPU_DRIVER_ANY)) {
392                 /* For some reason, on linux + Intel UHD Graphics 620 the driver
393                  * hangs if we don't flush before this. (See T57455) */
394                 GPU_flush();
395         }
396
397         GPU_scissor(scissor_rect.xmin,
398                     scissor_rect.ymin,
399                     BLI_rcti_size_x(&scissor_rect) + 1,
400                     BLI_rcti_size_y(&scissor_rect) + 1);
401
402         /* It seems that all areas gets smaller when pixelsize is > 1.
403          * So in order to avoid missing pixels we just disable de scissors. */
404         if (U.pixelsize <= 1.0f) {
405                 glEnable(GL_SCISSOR_TEST);
406         }
407
408         UI_GetThemeColor4fv(TH_EDITOR_OUTLINE, col);
409         col[3] = 1.0f;
410         corner_scale = U.pixelsize * 8.0f;
411         edge_thickness = corner_scale * 0.21f;
412
413         GPU_blend(true);
414
415         GPUBatch *batch = batch_screen_edges_get(&verts_per_corner);
416         GPU_batch_program_set_builtin(batch, GPU_SHADER_2D_AREA_EDGES);
417         GPU_batch_uniform_1i(batch, "cornerLen", verts_per_corner);
418         GPU_batch_uniform_1f(batch, "scale", corner_scale);
419         GPU_batch_uniform_4fv(batch, "color", col);
420
421         for (sa = screen->areabase.first; sa; sa = sa->next) {
422                 drawscredge_area(sa, winsize_x, winsize_y, edge_thickness);
423         }
424
425         GPU_blend(false);
426
427         if (U.pixelsize <= 1.0f) {
428                 glDisable(GL_SCISSOR_TEST);
429         }
430 }
431
432 /**
433  * The blended join arrows.
434  *
435  * \param sa1: Area from which the resultant originates.
436  * \param sa2: Target area that will be replaced.
437  */
438 void ED_screen_draw_join_shape(ScrArea *sa1, ScrArea *sa2)
439 {
440         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
441         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
442
443         GPU_line_width(1);
444
445         /* blended join arrow */
446         int dir = area_getorientation(sa1, sa2);
447         int dira = -1;
448         if (dir != -1) {
449                 switch (dir) {
450                         case 0: /* W */
451                                 dir = 'r';
452                                 dira = 'l';
453                                 break;
454                         case 1: /* N */
455                                 dir = 'd';
456                                 dira = 'u';
457                                 break;
458                         case 2: /* E */
459                                 dir = 'l';
460                                 dira = 'r';
461                                 break;
462                         case 3: /* S */
463                                 dir = 'u';
464                                 dira = 'd';
465                                 break;
466                 }
467
468                 GPU_blend(true);
469
470                 scrarea_draw_shape_dark(sa2, dir, pos);
471                 scrarea_draw_shape_light(sa1, dira, pos);
472
473                 GPU_blend(false);
474         }
475
476         immUnbindProgram();
477 }
478
479 void ED_screen_draw_split_preview(ScrArea *sa, const int dir, const float fac)
480 {
481         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
482         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
483
484         /* splitpoint */
485         GPU_blend(true);
486         immUniformColor4ub(255, 255, 255, 100);
487
488         immBegin(GPU_PRIM_LINES, 2);
489
490         if (dir == 'h') {
491                 const float y = (1 - fac) * sa->totrct.ymin + fac * sa->totrct.ymax;
492
493                 immVertex2f(pos, sa->totrct.xmin, y);
494                 immVertex2f(pos, sa->totrct.xmax, y);
495
496                 immEnd();
497
498                 immUniformColor4ub(0, 0, 0, 100);
499
500                 immBegin(GPU_PRIM_LINES, 2);
501
502                 immVertex2f(pos, sa->totrct.xmin, y + 1);
503                 immVertex2f(pos, sa->totrct.xmax, y + 1);
504
505                 immEnd();
506         }
507         else {
508                 BLI_assert(dir == 'v');
509                 const float x = (1 - fac) * sa->totrct.xmin + fac * sa->totrct.xmax;
510
511                 immVertex2f(pos, x, sa->totrct.ymin);
512                 immVertex2f(pos, x, sa->totrct.ymax);
513
514                 immEnd();
515
516                 immUniformColor4ub(0, 0, 0, 100);
517
518                 immBegin(GPU_PRIM_LINES, 2);
519
520                 immVertex2f(pos, x + 1, sa->totrct.ymin);
521                 immVertex2f(pos, x + 1, sa->totrct.ymax);
522
523                 immEnd();
524         }
525
526         GPU_blend(false);
527
528         immUnbindProgram();
529 }
530
531
532 /* -------------------------------------------------------------------- */
533 /* Screen Thumbnail Preview */
534
535 /**
536  * Calculates a scale factor to squash the preview for \a screen into a rectangle of given size and aspect.
537  */
538 static void screen_preview_scale_get(
539         const bScreen *screen, float size_x, float size_y,
540         const float asp[2],
541         float r_scale[2])
542 {
543         float max_x = 0, max_y = 0;
544
545         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
546                 max_x = MAX2(max_x, sa->totrct.xmax);
547                 max_y = MAX2(max_y, sa->totrct.ymax);
548         }
549         r_scale[0] = (size_x * asp[0]) / max_x;
550         r_scale[1] = (size_y * asp[1]) / max_y;
551 }
552
553 static void screen_preview_draw_areas(const bScreen *screen, const float scale[2], const float col[4],
554                                       const float ofs_between_areas)
555 {
556         const float ofs_h = ofs_between_areas * 0.5f;
557         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
558
559         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
560         immUniformColor4fv(col);
561
562         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
563                 rctf rect = {
564                         .xmin = sa->totrct.xmin * scale[0] + ofs_h,
565                         .xmax = sa->totrct.xmax * scale[0] - ofs_h,
566                         .ymin = sa->totrct.ymin * scale[1] + ofs_h,
567                         .ymax = sa->totrct.ymax * scale[1] - ofs_h
568                 };
569
570                 immBegin(GPU_PRIM_TRI_FAN, 4);
571                 immVertex2f(pos, rect.xmin, rect.ymin);
572                 immVertex2f(pos, rect.xmax, rect.ymin);
573                 immVertex2f(pos, rect.xmax, rect.ymax);
574                 immVertex2f(pos, rect.xmin, rect.ymax);
575                 immEnd();
576         }
577
578         immUnbindProgram();
579 }
580
581 static void screen_preview_draw(const bScreen *screen, int size_x, int size_y)
582 {
583         const float asp[2] = {1.0f, 0.8f}; /* square previews look a bit ugly */
584         /* could use theme color (tui.wcol_menu_item.text), but then we'd need to regenerate all previews when changing */
585         const float col[4] = {1.0f, 1.0f, 1.0f, 1.0f};
586         float scale[2];
587
588         wmOrtho2(0.0f, size_x, 0.0f, size_y);
589         /* center */
590         GPU_matrix_push();
591         GPU_matrix_identity_set();
592         GPU_matrix_translate_2f(size_x * (1.0f - asp[0]) * 0.5f, size_y * (1.0f - asp[1]) * 0.5f);
593
594         screen_preview_scale_get(screen, size_x, size_y, asp, scale);
595         screen_preview_draw_areas(screen, scale, col, 1.5f);
596
597         GPU_matrix_pop();
598 }
599
600 /**
601  * Render the preview for a screen layout in \a screen.
602  */
603 void ED_screen_preview_render(const bScreen *screen, int size_x, int size_y, unsigned int *r_rect)
604 {
605         char err_out[256] = "unknown";
606         GPUOffScreen *offscreen = GPU_offscreen_create(size_x, size_y, 0, true, false, err_out);
607
608         GPU_offscreen_bind(offscreen, true);
609         GPU_clear_color(0.0, 0.0, 0.0, 0.0);
610         GPU_clear(GPU_COLOR_BIT | GPU_DEPTH_BIT);
611
612         screen_preview_draw(screen, size_x, size_y);
613
614         GPU_offscreen_read_pixels(offscreen, GL_UNSIGNED_BYTE, r_rect);
615         GPU_offscreen_unbind(offscreen, true);
616
617         GPU_offscreen_free(offscreen);
618 }