glamor: Add glamor_program based 0-width dashed lines

This makes sure the pixelization for dashed lines matches non-dashed
lines, while also speeding them up.

v2: Switch to glamor_make_current

v3: Create dash pattern pixmap without GLAMOR_CREATE_FBO_NO_FBO

v4: Adopt suggestions from Eric's review:

  - Drops power-of-two alignment of our line vertex data, simplifying
    the code.

  - Stops reading from the VBO.  While on keithp's and my machines the
    VBO is mapped cached, on many implementations it will be mapped WC,
    making those reads extremely expensive.

  - Style fixes (line wrapping, spaces around operators).

v5: Adopt suggestions from Markus' review:

  - Use max when computing zero-width dashed line length.

    Don't open code max here.

  - Embed CoordModePrevious into VBO writing for dashed lines

    Instead of pre-computing the coord mode previous results, just
    embed this in the loop which fills the vertex buffer. Saves
    re-writing the request buffer, and shortens the code a bit

v6: Export glamor_destroy_gc for UXA

    UXA needs to call glamor_destroy_gc from its GCFuncs, so export
    it.

Signed-off-by: Keith Packard <keithp@keithp.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
This commit is contained in:
Keith Packard 2014-04-02 14:07:20 -07:00 committed by Eric Anholt
parent dc9fa9080a
commit d18f5801c9
10 changed files with 496 additions and 20 deletions

View File

@ -9,6 +9,7 @@ libglamor_la_SOURCES = \
glamor_context.h \
glamor_copy.c \
glamor_core.c \
glamor_dash.c \
glamor_debug.h \
glamor_fill.c \
glamor_font.c \

View File

@ -37,6 +37,7 @@
DevPrivateKeyRec glamor_screen_private_key;
DevPrivateKeyRec glamor_pixmap_private_key;
DevPrivateKeyRec glamor_gc_private_key;
/**
* glamor_get_drawable_pixmap() returns a backing pixmap for a given drawable.
@ -346,7 +347,15 @@ glamor_init(ScreenPtr screen, unsigned int flags)
LogMessage(X_WARNING,
"glamor%d: Failed to allocate pixmap private\n",
screen->myNum);
goto fail;;
goto fail;
}
if (!dixRegisterPrivateKey(&glamor_gc_private_key, PRIVATE_GC,
sizeof (glamor_gc_private))) {
LogMessage(X_WARNING,
"glamor%d: Failed to allocate gc private\n",
screen->myNum);
goto fail;
}
if (epoxy_is_desktop_gl())

View File

@ -321,6 +321,10 @@ extern _X_EXPORT int glamor_create_gc(GCPtr gc);
extern _X_EXPORT void glamor_validate_gc(GCPtr gc, unsigned long changes,
DrawablePtr drawable);
extern _X_EXPORT void glamor_destroy_gc(GCPtr gc);
#define HAS_GLAMOR_DESTROY_GC 1
extern Bool _X_EXPORT glamor_change_window_attributes(WindowPtr pWin, unsigned long mask);
extern void _X_EXPORT glamor_copy_window(WindowPtr window, DDXPointRec old_origin, RegionPtr src_region);

View File

@ -409,14 +409,35 @@ glamor_validate_gc(GCPtr gc, unsigned long changes, DrawablePtr drawable)
fbValidateGC(gc, changes, drawable);
}
if (changes & GCDashList) {
glamor_gc_private *gc_priv = glamor_get_gc_private(gc);
if (gc_priv->dash) {
glamor_destroy_pixmap(gc_priv->dash);
gc_priv->dash = NULL;
}
}
gc->ops = &glamor_gc_ops;
}
void
glamor_destroy_gc(GCPtr gc)
{
glamor_gc_private *gc_priv = glamor_get_gc_private(gc);
if (gc_priv->dash) {
glamor_destroy_pixmap(gc_priv->dash);
gc_priv->dash = NULL;
}
miDestroyGC(gc);
}
static GCFuncs glamor_gc_funcs = {
glamor_validate_gc,
miChangeGC,
miCopyGC,
miDestroyGC,
glamor_destroy_gc,
miChangeClip,
miDestroyClip,
miCopyClip
@ -429,6 +450,9 @@ static GCFuncs glamor_gc_funcs = {
int
glamor_create_gc(GCPtr gc)
{
glamor_gc_private *gc_priv = glamor_get_gc_private(gc);
gc_priv->dash = NULL;
if (!fbCreateGC(gc))
return FALSE;

370
glamor/glamor_dash.c Normal file
View File

@ -0,0 +1,370 @@
/*
* Copyright © 2014 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include "glamor_priv.h"
#include "glamor_program.h"
#include "glamor_transform.h"
#include "glamor_transfer.h"
#include "glamor_prepare.h"
static const char dash_vs_vars[] =
"attribute vec3 primitive;\n"
"varying float dash_offset;\n";
static const char dash_vs_exec[] =
" dash_offset = primitive.z / dash_length;\n"
GLAMOR_POS(gl_Position, primitive.xy);
static const char dash_fs_vars[] =
"varying float dash_offset;\n";
static const char on_off_fs_exec[] =
" float pattern = texture2D(dash, vec2(dash_offset, 0.5)).w;\n"
" if (pattern == 0.0)\n"
" discard;\n";
/* XXX deal with stippled double dashed lines once we have stippling support */
static const char double_fs_exec[] =
" float pattern = texture2D(dash, vec2(dash_offset, 0.5)).w;\n"
" if (pattern == 0.0)\n"
" gl_FragColor = bg;\n"
" else\n"
" gl_FragColor = fg;\n";
static const glamor_facet glamor_facet_on_off_dash_lines = {
.version = 130,
.name = "poly_lines_on_off_dash",
.vs_vars = dash_vs_vars,
.vs_exec = dash_vs_exec,
.fs_vars = dash_fs_vars,
.fs_exec = on_off_fs_exec,
.locations = glamor_program_location_dash,
};
static const glamor_facet glamor_facet_double_dash_lines = {
.version = 130,
.name = "poly_lines_double_dash",
.vs_vars = dash_vs_vars,
.vs_exec = dash_vs_exec,
.fs_vars = dash_fs_vars,
.fs_exec = double_fs_exec,
.locations = (glamor_program_location_dash|
glamor_program_location_fg|
glamor_program_location_bg),
};
static PixmapPtr
glamor_get_dash_pixmap(GCPtr gc)
{
glamor_gc_private *gc_priv = glamor_get_gc_private(gc);
ScreenPtr screen = gc->pScreen;
PixmapPtr pixmap;
int offset;
int d;
uint32_t pixel;
GCPtr scratch_gc;
if (gc_priv->dash)
return gc_priv->dash;
offset = 0;
for (d = 0; d < gc->numInDashList; d++)
offset += gc->dash[d];
pixmap = glamor_create_pixmap(screen, offset, 1, 8, 0);
if (!pixmap)
goto bail;
scratch_gc = GetScratchGC(8, screen);
if (!scratch_gc)
goto bail_pixmap;
pixel = 0xffffffff;
offset = 0;
for (d = 0; d < gc->numInDashList; d++) {
xRectangle rect;
ChangeGCVal changes;
changes.val = pixel;
(void) ChangeGC(NullClient, scratch_gc,
GCForeground, &changes);
ValidateGC(&pixmap->drawable, scratch_gc);
rect.x = offset;
rect.y = 0;
rect.width = gc->dash[d];
rect.height = 1;
scratch_gc->ops->PolyFillRect (&pixmap->drawable, scratch_gc, 1, &rect);
offset += gc->dash[d];
pixel = ~pixel;
}
FreeScratchGC(scratch_gc);
gc_priv->dash = pixmap;
return pixmap;
bail_pixmap:
glamor_destroy_pixmap(pixmap);
bail:
return NULL;
}
static glamor_program *
glamor_dash_setup(DrawablePtr drawable, GCPtr gc)
{
ScreenPtr screen = drawable->pScreen;
glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
PixmapPtr dash_pixmap;
glamor_pixmap_private *dash_priv;
glamor_program *prog;
if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
goto bail;
if (gc->lineWidth != 0)
goto bail;
dash_pixmap = glamor_get_dash_pixmap(gc);
dash_priv = glamor_get_pixmap_private(pixmap);
if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dash_priv))
goto bail;
glamor_make_current(glamor_priv);
switch (gc->lineStyle) {
case LineOnOffDash:
prog = glamor_use_program_fill(pixmap, gc,
&glamor_priv->on_off_dash_line_progs,
&glamor_facet_on_off_dash_lines);
if (!prog)
goto bail_ctx;
break;
case LineDoubleDash:
if (gc->fillStyle != FillSolid)
goto bail_ctx;
prog = &glamor_priv->double_dash_line_prog;
if (!prog->prog) {
if (!glamor_build_program(screen, prog,
&glamor_facet_double_dash_lines,
NULL))
goto bail_ctx;
}
if (!glamor_use_program(pixmap, gc, prog, NULL))
goto bail_ctx;
glamor_set_color(pixmap, gc->fgPixel, prog->fg_uniform);
glamor_set_color(pixmap, gc->bgPixel, prog->bg_uniform);
break;
default:
goto bail_ctx;
}
/* Set the dash pattern as texture 1 */
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, dash_priv->base.fbo->tex);
glUniform1i(prog->dash_uniform, 1);
glUniform1f(prog->dash_length_uniform, dash_pixmap->drawable.width);
return prog;
bail_ctx:
glDisable(GL_COLOR_LOGIC_OP);
bail:
return NULL;
}
static void
glamor_dash_loop(DrawablePtr drawable, GCPtr gc, glamor_program *prog,
int n, GLenum mode)
{
PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
int box_x, box_y;
int off_x, off_y;
glEnable(GL_SCISSOR_TEST);
glamor_pixmap_loop(pixmap_priv, box_x, box_y) {
int nbox = RegionNumRects(gc->pCompositeClip);
BoxPtr box = RegionRects(gc->pCompositeClip);
glamor_set_destination_drawable(drawable, box_x, box_y, TRUE, TRUE,
prog->matrix_uniform, &off_x, &off_y);
while (nbox--) {
glScissor(box->x1 + off_x,
box->y1 + off_y,
box->x2 - box->x1,
box->y2 - box->y1);
box++;
glDrawArrays(mode, 0, n);
}
}
glDisable(GL_SCISSOR_TEST);
glDisable(GL_COLOR_LOGIC_OP);
glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
}
static int
glamor_line_length(short x1, short y1, short x2, short y2)
{
return max(abs(x2 - x1), abs(y2 - y1));
}
Bool
glamor_poly_lines_dash_gl(DrawablePtr drawable, GCPtr gc,
int mode, int n, DDXPointPtr points)
{
ScreenPtr screen = drawable->pScreen;
glamor_program *prog;
short *v;
char *vbo_offset;
int add_last;
int dash_pos;
int prev_x, prev_y;
int i;
if (n < 2)
return TRUE;
if (!(prog = glamor_dash_setup(drawable, gc)))
return FALSE;
add_last = 0;
if (gc->capStyle != CapNotLast)
add_last = 1;
/* Set up the vertex buffers for the points */
v = glamor_get_vbo_space(drawable->pScreen,
(n + add_last) * 3 * sizeof (short),
&vbo_offset);
glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
glVertexAttribPointer(GLAMOR_VERTEX_POS, 3, GL_SHORT, GL_FALSE,
3 * sizeof (short), vbo_offset);
dash_pos = gc->dashOffset;
prev_x = prev_y = 0;
for (i = 0; i < n; i++) {
int this_x = points[i].x;
int this_y = points[i].y;
if (i) {
if (mode == CoordModePrevious) {
this_x += prev_x;
this_y += prev_y;
}
dash_pos += glamor_line_length(prev_x, prev_y,
this_x, this_y);
}
v[0] = prev_x = this_x;
v[1] = prev_y = this_y;
v[2] = dash_pos;
v += 3;
}
if (add_last) {
v[0] = prev_x + 1;
v[1] = prev_y;
v[2] = dash_pos + 1;
}
glamor_put_vbo_space(screen);
glamor_dash_loop(drawable, gc, prog, n + add_last, GL_LINE_STRIP);
return TRUE;
}
static short *
glamor_add_segment(short *v, short x1, short y1, short x2, short y2,
int dash_start, int dash_end)
{
v[0] = x1;
v[1] = y1;
v[2] = dash_start;
v[3] = x2;
v[4] = y2;
v[5] = dash_end;
return v + 6;
}
Bool
glamor_poly_segment_dash_gl(DrawablePtr drawable, GCPtr gc,
int nseg, xSegment *segs)
{
ScreenPtr screen = drawable->pScreen;
glamor_program *prog;
short *v;
char *vbo_offset;
int dash_start = gc->dashOffset;
int add_last;
int i;
if (!(prog = glamor_dash_setup(drawable, gc)))
return FALSE;
add_last = 0;
if (gc->capStyle != CapNotLast)
add_last = 1;
/* Set up the vertex buffers for the points */
v = glamor_get_vbo_space(drawable->pScreen,
(nseg<<add_last) * 6 * sizeof (short),
&vbo_offset);
glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
glVertexAttribPointer(GLAMOR_VERTEX_POS, 3, GL_SHORT, GL_FALSE,
3 * sizeof (short), vbo_offset);
for (i = 0; i < nseg; i++) {
int dash_end = dash_start + glamor_line_length(segs[i].x1, segs[i].y1,
segs[i].x2, segs[i].y2);
v = glamor_add_segment(v,
segs[i].x1, segs[i].y1,
segs[i].x2, segs[i].y2,
dash_start, dash_end);
if (add_last)
v = glamor_add_segment(v,
segs[i].x2, segs[i].y2,
segs[i].x2 + 1, segs[i].y2,
dash_end, dash_end + 1);
}
glamor_put_vbo_space(screen);
glamor_dash_loop(drawable, gc, prog, nseg << (1 + add_last), GL_LINES);
return TRUE;
}

View File

@ -33,8 +33,8 @@ static const glamor_facet glamor_facet_poly_lines = {
};
static Bool
glamor_poly_lines_gl(DrawablePtr drawable, GCPtr gc,
int mode, int n, DDXPointPtr points)
glamor_poly_lines_solid_gl(DrawablePtr drawable, GCPtr gc,
int mode, int n, DDXPointPtr points)
{
ScreenPtr screen = drawable->pScreen;
glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
@ -51,12 +51,6 @@ glamor_poly_lines_gl(DrawablePtr drawable, GCPtr gc,
if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
goto bail;
if (gc->lineWidth != 0)
goto bail;
if (gc->lineStyle != LineSolid)
goto bail;
add_last = 0;
if (gc->capStyle != CapNotLast)
add_last = 1;
@ -133,6 +127,28 @@ bail:
return FALSE;
}
static Bool
glamor_poly_lines_gl(DrawablePtr drawable, GCPtr gc,
int mode, int n, DDXPointPtr points)
{
if (gc->lineWidth != 0)
return FALSE;
switch (gc->lineStyle) {
case LineSolid:
return glamor_poly_lines_solid_gl(drawable, gc, mode, n, points);
case LineOnOffDash:
return glamor_poly_lines_dash_gl(drawable, gc, mode, n, points);
case LineDoubleDash:
if (gc->fillStyle == FillTiled)
return glamor_poly_lines_solid_gl(drawable, gc, mode, n, points);
else
return glamor_poly_lines_dash_gl(drawable, gc, mode, n, points);
default:
return FALSE;
}
}
static void
glamor_poly_lines_bail(DrawablePtr drawable, GCPtr gc,
int mode, int n, DDXPointPtr points)

View File

@ -247,6 +247,10 @@ typedef struct glamor_screen_private {
/* glamor segment shaders */
glamor_program_fill poly_segment_program;
/* glamor dash line shader */
glamor_program_fill on_off_dash_line_progs;
glamor_program double_dash_line_prog;
/* vertext/elment_index buffer object for render */
GLuint vbo, ebo;
/** Next offset within the VBO that glamor_get_vbo_space() will use. */
@ -559,6 +563,13 @@ typedef enum glamor_pixmap_status {
GLAMOR_UPLOAD_FAILED
} glamor_pixmap_status_t;
/* GC private structure. Currently holds only any computed dash pixmap */
typedef struct {
PixmapPtr dash;
} glamor_gc_private;
extern DevPrivateKeyRec glamor_gc_private_key;
extern DevPrivateKeyRec glamor_screen_private_key;
extern DevPrivateKeyRec glamor_pixmap_private_key;
@ -591,6 +602,12 @@ glamor_get_pixmap_private(PixmapPtr pixmap)
void glamor_set_pixmap_private(PixmapPtr pixmap, glamor_pixmap_private *priv);
static inline glamor_gc_private *
glamor_get_gc_private(GCPtr gc)
{
return dixLookupPrivate(&gc->devPrivates, &glamor_gc_private_key);
}
/**
* Returns TRUE if the given planemask covers all the significant bits in the
* pixel values for pDrawable.
@ -969,7 +986,17 @@ glamor_put_image(DrawablePtr drawable, GCPtr gc, int depth, int x, int y,
void
glamor_get_image(DrawablePtr pDrawable, int x, int y, int w, int h,
unsigned int format, unsigned long planeMask, char *d);
/* glamor_lines.c */
/* glamor_dash.c */
Bool
glamor_poly_lines_dash_gl(DrawablePtr drawable, GCPtr gc,
int mode, int n, DDXPointPtr points);
Bool
glamor_poly_segment_dash_gl(DrawablePtr drawable, GCPtr gc,
int nseg, xSegment *segs);
/* glamor_lines.c */
void
glamor_poly_lines(DrawablePtr drawable, GCPtr gc,
int mode, int n, DDXPointPtr points);

View File

@ -122,6 +122,11 @@ static glamor_location_var location_vars[] = {
.fs_vars = ("uniform uvec4 bitplane;\n"
"uniform vec4 bitmul;\n"),
},
{
.location = glamor_program_location_dash,
.vs_vars = "uniform float dash_length;\n",
.fs_vars = "uniform sampler2D dash;\n",
},
};
#define NUM_LOCATION_VARS (sizeof location_vars / sizeof location_vars[0])
@ -326,6 +331,8 @@ glamor_build_program(ScreenPtr screen,
prog->font_uniform = glamor_get_uniform(prog, glamor_program_location_font, "font");
prog->bitplane_uniform = glamor_get_uniform(prog, glamor_program_location_bitplane, "bitplane");
prog->bitmul_uniform = glamor_get_uniform(prog, glamor_program_location_bitplane, "bitmul");
prog->dash_uniform = glamor_get_uniform(prog, glamor_program_location_dash, "dash");
prog->dash_length_uniform = glamor_get_uniform(prog, glamor_program_location_dash, "dash_length");
if (glGetError() != GL_NO_ERROR)
goto fail;

View File

@ -30,6 +30,7 @@ typedef enum {
glamor_program_location_fill = 4,
glamor_program_location_font = 8,
glamor_program_location_bitplane = 16,
glamor_program_location_dash = 32,
} glamor_program_location;
typedef enum {
@ -64,6 +65,8 @@ struct _glamor_program {
GLint font_uniform;
GLint bitplane_uniform;
GLint bitmul_uniform;
GLint dash_uniform;
GLint dash_length_uniform;
glamor_program_location locations;
glamor_program_flag flags;
glamor_use prim_use;

View File

@ -33,8 +33,8 @@ static const glamor_facet glamor_facet_poly_segment = {
};
static Bool
glamor_poly_segment_gl(DrawablePtr drawable, GCPtr gc,
int nseg, xSegment *segs)
glamor_poly_segment_solid_gl(DrawablePtr drawable, GCPtr gc,
int nseg, xSegment *segs)
{
ScreenPtr screen = drawable->pScreen;
glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
@ -51,12 +51,6 @@ glamor_poly_segment_gl(DrawablePtr drawable, GCPtr gc,
if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
goto bail;
if (gc->lineWidth != 0)
goto bail;
if (gc->lineStyle != LineSolid)
goto bail;
add_last = 0;
if (gc->capStyle != CapNotLast)
add_last = 1;
@ -125,6 +119,28 @@ bail:
return FALSE;
}
static Bool
glamor_poly_segment_gl(DrawablePtr drawable, GCPtr gc,
int nseg, xSegment *segs)
{
if (gc->lineWidth != 0)
return FALSE;
switch (gc->lineStyle) {
case LineSolid:
return glamor_poly_segment_solid_gl(drawable, gc, nseg, segs);
case LineOnOffDash:
return glamor_poly_segment_dash_gl(drawable, gc, nseg, segs);
case LineDoubleDash:
if (gc->fillStyle == FillTiled)
return glamor_poly_segment_solid_gl(drawable, gc, nseg, segs);
else
return glamor_poly_segment_dash_gl(drawable, gc, nseg, segs);
default:
return FALSE;
}
}
static void
glamor_poly_segment_bail(DrawablePtr drawable, GCPtr gc,
int nseg, xSegment *segs)
@ -143,7 +159,6 @@ glamor_poly_segment_bail(DrawablePtr drawable, GCPtr gc,
miPolySegment(drawable, gc, nseg, segs);
}
void
glamor_poly_segment(DrawablePtr drawable, GCPtr gc,
int nseg, xSegment *segs)