Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / screen / screen_draw.c
index e67ae4e321cab2906bd9344ac5c14aa740829cb7..2e4e9127ed6c45bd15e4f082dd5034317ce13e94 100644 (file)
  *  \ingroup edscr
  */
 
-#include "BIF_gl.h"
+#include "ED_screen.h"
 
-#include "WM_api.h"
+#include "GPU_framebuffer.h"
+#include "GPU_immediate.h"
+#include "GPU_matrix.h"
 
-#include "ED_screen.h"
+#include "WM_api.h"
+#include "WM_types.h"
 
 #include "screen_intern.h"
 
 /**
  * Draw horizontal shape visualizing future joining (left as well right direction of future joining).
  */
-static void draw_horizontal_join_shape(ScrArea *sa, char dir)
+static void draw_horizontal_join_shape(ScrArea *sa, char dir, unsigned int pos)
 {
        vec2f points[10];
        short i;
@@ -90,24 +93,31 @@ static void draw_horizontal_join_shape(ScrArea *sa, char dir)
                }
        }
 
-       glBegin(GL_POLYGON);
-       for (i = 0; i < 5; i++)
-               glVertex2f(points[i].x, points[i].y);
-       glEnd();
-       glBegin(GL_POLYGON);
-       for (i = 4; i < 8; i++)
-               glVertex2f(points[i].x, points[i].y);
-       glVertex2f(points[0].x, points[0].y);
-       glEnd();
-
-       glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
-       glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
+       immBegin(GWN_PRIM_TRI_FAN, 5);
+
+       for (i = 0; i < 5; i++) {
+               immVertex2f(pos, points[i].x, points[i].y);
+       }
+
+       immEnd();
+
+       immBegin(GWN_PRIM_TRI_FAN, 5);
+
+       for (i = 4; i < 8; i++) {
+               immVertex2f(pos, points[i].x, points[i].y);
+       }
+
+       immVertex2f(pos, points[0].x, points[0].y);
+       immEnd();
+
+       immRectf(pos, points[2].x, points[2].y, points[8].x, points[8].y);
+       immRectf(pos, points[6].x, points[6].y, points[9].x, points[9].y);
 }
 
 /**
  * Draw vertical shape visualizing future joining (up/down direction).
  */
-static void draw_vertical_join_shape(ScrArea *sa, char dir)
+static void draw_vertical_join_shape(ScrArea *sa, char dir, unsigned int pos)
 {
        vec2f points[10];
        short i;
@@ -144,7 +154,7 @@ static void draw_vertical_join_shape(ScrArea *sa, char dir)
 
        points[6].x = sa->v4->vec.x;
        points[6].y = sa->v4->vec.y + h;
-       
+
        points[7].x = sa->v3->vec.x;
        points[7].y = sa->v3->vec.y;
 
@@ -164,93 +174,117 @@ static void draw_vertical_join_shape(ScrArea *sa, char dir)
                }
        }
 
-       glBegin(GL_POLYGON);
-       for (i = 0; i < 5; i++)
-               glVertex2f(points[i].x, points[i].y);
-       glEnd();
-       glBegin(GL_POLYGON);
-       for (i = 4; i < 8; i++)
-               glVertex2f(points[i].x, points[i].y);
-       glVertex2f(points[0].x, points[0].y);
-       glEnd();
-
-       glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
-       glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
+       immBegin(GWN_PRIM_TRI_FAN, 5);
+
+       for (i = 0; i < 5; i++) {
+               immVertex2f(pos, points[i].x, points[i].y);
+       }
+
+       immEnd();
+
+       immBegin(GWN_PRIM_TRI_FAN, 5);
+
+       for (i = 4; i < 8; i++) {
+               immVertex2f(pos, points[i].x, points[i].y);
+       }
+
+       immVertex2f(pos, points[0].x, points[0].y);
+       immEnd();
+
+       immRectf(pos, points[2].x, points[2].y, points[8].x, points[8].y);
+       immRectf(pos, points[6].x, points[6].y, points[9].x, points[9].y);
 }
 
 /**
  * Draw join shape due to direction of joining.
  */
-static void draw_join_shape(ScrArea *sa, char dir)
+static void draw_join_shape(ScrArea *sa, char dir, unsigned int pos)
 {
        if (dir == 'u' || dir == 'd') {
-               draw_vertical_join_shape(sa, dir);
+               draw_vertical_join_shape(sa, dir, pos);
        }
        else {
-               draw_horizontal_join_shape(sa, dir);
+               draw_horizontal_join_shape(sa, dir, pos);
        }
 }
 
 /**
  * Draw screen area darker with arrow (visualization of future joining).
  */
-static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
+static void scrarea_draw_shape_dark(ScrArea *sa, char dir, unsigned int pos)
 {
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glColor4ub(0, 0, 0, 50);
-       draw_join_shape(sa, dir);
+       immUniformColor4ub(0, 0, 0, 50);
+
+       draw_join_shape(sa, dir, pos);
 }
 
 /**
  * Draw screen area ligher with arrow shape ("eraser" of previous dark shape).
  */
-static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir))
+static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir), unsigned int pos)
 {
        glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
        /* value 181 was hardly computed: 181~105 */
-       glColor4ub(255, 255, 255, 50);
+       immUniformColor4ub(255, 255, 255, 50);
        /* draw_join_shape(sa, dir); */
-       glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
+
+       immRectf(pos, sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
 }
 
-static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2)
+static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, unsigned int pos)
 {
+       int count = 0;
+
+       if (x2 < sizex - 1) count += 2;
+       if (x1 > 0) count += 2;
+       if (y2 < sizey - 1) count += 2;
+       if (y1 > 0) count += 2;
+
+       if (count == 0) {
+               return;
+       }
+
+       immBegin(GWN_PRIM_LINES, count);
+
        /* right border area */
        if (x2 < sizex - 1) {
-               glVertex2s(x2, y1);
-               glVertex2s(x2, y2);
+               immVertex2f(pos, x2, y1);
+               immVertex2f(pos, x2, y2);
        }
 
        /* left border area */
        if (x1 > 0) { /* otherwise it draws the emboss of window over */
-               glVertex2s(x1, y1);
-               glVertex2s(x1, y2);
+               immVertex2f(pos, x1, y1);
+               immVertex2f(pos, x1, y2);
        }
 
        /* top border area */
        if (y2 < sizey - 1) {
-               glVertex2s(x1, y2);
-               glVertex2s(x2, y2);
+               immVertex2f(pos, x1, y2);
+               immVertex2f(pos, x2, y2);
        }
 
        /* bottom border area */
        if (y1 > 0) {
-               glVertex2s(x1, y1);
-               glVertex2s(x2, y1);
+               immVertex2f(pos, x1, y1);
+               immVertex2f(pos, x2, y1);
        }
+
+       immEnd();
 }
 
 /**
  * \brief Screen edges drawing.
  */
-static void drawscredge_area(ScrArea *sa, int sizex, int sizey)
+static void drawscredge_area(ScrArea *sa, int sizex, int sizey, unsigned int pos)
 {
        short x1 = sa->v1->vec.x;
        short y1 = sa->v1->vec.y;
        short x2 = sa->v3->vec.x;
        short y2 = sa->v3->vec.y;
 
-       drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2);
+       drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, pos);
 }
 
 /**
@@ -258,33 +292,38 @@ static void drawscredge_area(ScrArea *sa, int sizex, int sizey)
  */
 void ED_screen_draw_edges(wmWindow *win)
 {
+       bScreen *screen = WM_window_get_active_screen(win);
        const int winsize_x = WM_window_pixels_x(win);
        const int winsize_y = WM_window_pixels_y(win);
 
        ScrArea *sa;
 
-       wmSubWindowSet(win, win->screen->mainwin);
+       wmSubWindowSet(win, screen->mainwin);
+
+       unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
+       immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
 
        /* Note: first loop only draws if U.pixelsize > 1, skip otherwise */
        if (U.pixelsize > 1.0f) {
                /* FIXME: doesn't our glLineWidth already scale by U.pixelsize? */
                glLineWidth((2.0f * U.pixelsize) - 1);
-               glColor3ub(0x50, 0x50, 0x50);
-               glBegin(GL_LINES);
-               for (sa = win->screen->areabase.first; sa; sa = sa->next)
-                       drawscredge_area(sa, winsize_x, winsize_y);
-               glEnd();
+               immUniformColor3ub(0x50, 0x50, 0x50);
+
+               for (sa = screen->areabase.first; sa; sa = sa->next) {
+                       drawscredge_area(sa, winsize_x, winsize_y, pos);
+               }
        }
 
        glLineWidth(1);
-       glColor3ub(0, 0, 0);
-       glBegin(GL_LINES);
-       for (sa = win->screen->areabase.first; sa; sa = sa->next) {
-               drawscredge_area(sa, winsize_x, winsize_y);
+       immUniformColor3ub(0, 0, 0);
+
+       for (sa = screen->areabase.first; sa; sa = sa->next) {
+               drawscredge_area(sa, winsize_x, winsize_y, pos);
        }
-       glEnd();
 
-       win->screen->do_draw = false;
+       immUnbindProgram();
+
+       screen->do_draw = false;
 }
 
 /**
@@ -295,6 +334,9 @@ void ED_screen_draw_edges(wmWindow *win)
  */
 void ED_screen_draw_join_shape(ScrArea *sa1, ScrArea *sa2)
 {
+       unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
+       immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
+
        glLineWidth(1);
 
        /* blended join arrow */
@@ -319,37 +361,154 @@ void ED_screen_draw_join_shape(ScrArea *sa1, ScrArea *sa2)
                                dira = 'd';
                                break;
                }
+
                glEnable(GL_BLEND);
-               scrarea_draw_shape_dark(sa2, dir);
-               scrarea_draw_shape_light(sa1, dira);
+
+               scrarea_draw_shape_dark(sa2, dir, pos);
+               scrarea_draw_shape_light(sa1, dira, pos);
+
                glDisable(GL_BLEND);
        }
+
+       immUnbindProgram();
 }
 
 void ED_screen_draw_split_preview(ScrArea *sa, const int dir, const float fac)
 {
+       unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
+       immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
+
        /* splitpoint */
        glEnable(GL_BLEND);
-       glBegin(GL_LINES);
-       glColor4ub(255, 255, 255, 100);
+       immUniformColor4ub(255, 255, 255, 100);
+
+       immBegin(GWN_PRIM_LINES, 2);
 
        if (dir == 'h') {
                const float y = (1 - fac) * sa->totrct.ymin + fac * sa->totrct.ymax;
-               glVertex2s(sa->totrct.xmin, y);
-               glVertex2s(sa->totrct.xmax, y);
-               glColor4ub(0, 0, 0, 100);
-               glVertex2s(sa->totrct.xmin, y + 1);
-               glVertex2s(sa->totrct.xmax, y + 1);
+
+               immVertex2f(pos, sa->totrct.xmin, y);
+               immVertex2f(pos, sa->totrct.xmax, y);
+
+               immEnd();
+
+               immUniformColor4ub(0, 0, 0, 100);
+
+               immBegin(GWN_PRIM_LINES, 2);
+
+               immVertex2f(pos, sa->totrct.xmin, y + 1);
+               immVertex2f(pos, sa->totrct.xmax, y + 1);
+
+               immEnd();
        }
        else {
+               BLI_assert(dir == 'v');
                const float x = (1 - fac) * sa->totrct.xmin + fac * sa->totrct.xmax;
-               glVertex2s(x, sa->totrct.ymin);
-               glVertex2s(x, sa->totrct.ymax);
-               glColor4ub(0, 0, 0, 100);
-               glVertex2s(x + 1, sa->totrct.ymin);
-               glVertex2s(x + 1, sa->totrct.ymax);
+
+               immVertex2f(pos, x, sa->totrct.ymin);
+               immVertex2f(pos, x, sa->totrct.ymax);
+
+               immEnd();
+
+               immUniformColor4ub(0, 0, 0, 100);
+
+               immBegin(GWN_PRIM_LINES, 2);
+
+               immVertex2f(pos, x + 1, sa->totrct.ymin);
+               immVertex2f(pos, x + 1, sa->totrct.ymax);
+
+               immEnd();
        }
-       glEnd();
+
        glDisable(GL_BLEND);
+
+       immUnbindProgram();
 }
 
+
+/* -------------------------------------------------------------------- */
+/* Screen Thumbnail Preview */
+
+/**
+ * Calculates a scale factor to squash the preview for \a screen into a rectangle of given size and aspect.
+ */
+static void screen_preview_scale_get(
+        const bScreen *screen, float size_x, float size_y,
+        const float asp[2],
+        float r_scale[2])
+{
+       float max_x = 0, max_y = 0;
+
+       for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
+               max_x = MAX2(max_x, sa->totrct.xmax);
+               max_y = MAX2(max_y, sa->totrct.ymax);
+       }
+       r_scale[0] = (size_x * asp[0]) / max_x;
+       r_scale[1] = (size_y * asp[1]) / max_y;
+}
+
+static void screen_preview_draw_areas(const bScreen *screen, const float scale[2], const float col[4],
+                                      const float ofs_between_areas)
+{
+       const float ofs_h = ofs_between_areas * 0.5f;
+       unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
+
+       immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
+       immUniformColor4fv(col);
+
+       for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
+               rctf rect = {
+                       .xmin = sa->totrct.xmin * scale[0] + ofs_h,
+                       .xmax = sa->totrct.xmax * scale[0] - ofs_h,
+                       .ymin = sa->totrct.ymin * scale[1] + ofs_h,
+                       .ymax = sa->totrct.ymax * scale[1] - ofs_h
+               };
+
+               immBegin(GWN_PRIM_TRI_FAN, 4);
+               immVertex2f(pos, rect.xmin, rect.ymin);
+               immVertex2f(pos, rect.xmax, rect.ymin);
+               immVertex2f(pos, rect.xmax, rect.ymax);
+               immVertex2f(pos, rect.xmin, rect.ymax);
+               immEnd();
+       }
+
+       immUnbindProgram();
+}
+
+static void screen_preview_draw(const bScreen *screen, int size_x, int size_y)
+{
+       const float asp[2] = {1.0f, 0.8f}; /* square previews look a bit ugly */
+       /* could use theme color (tui.wcol_menu_item.text), but then we'd need to regenerate all previews when changing */
+       const float col[4] = {1.0f, 1.0f, 1.0f, 1.0f};
+       float scale[2];
+
+       wmOrtho2(0.0f, size_x, 0.0f, size_y);
+       /* center */
+       gpuPushMatrix();
+       gpuTranslate2f(size_x * (1.0f - asp[0]) * 0.5f, size_y * (1.0f - asp[1]) * 0.5f);
+
+       screen_preview_scale_get(screen, size_x, size_y, asp, scale);
+       screen_preview_draw_areas(screen, scale, col, 1.5f);
+
+       gpuPopMatrix();
+}
+
+/**
+ * Render the preview for a screen layout in \a screen.
+ */
+void ED_screen_preview_render(const bScreen *screen, int size_x, int size_y, unsigned int *r_rect)
+{
+       char err_out[256] = "unknown";
+       GPUOffScreen *offscreen = GPU_offscreen_create(size_x, size_y, 0, false, err_out);
+
+       GPU_offscreen_bind(offscreen, true);
+       glClearColor(0.0, 0.0, 0.0, 0.0);
+       glClear(GL_COLOR_BUFFER_BIT);
+
+       screen_preview_draw(screen, size_x, size_y);
+
+       GPU_offscreen_read_pixels(offscreen, GL_UNSIGNED_BYTE, r_rect);
+       GPU_offscreen_unbind(offscreen, true);
+
+       GPU_offscreen_free(offscreen);
+}