glamor_dash.c revision 35c4bbdf
1/* 2 * Copyright © 2014 Keith Packard 3 * 4 * Permission to use, copy, modify, distribute, and sell this software and its 5 * documentation for any purpose is hereby granted without fee, provided that 6 * the above copyright notice appear in all copies and that both that copyright 7 * notice and this permission notice appear in supporting documentation, and 8 * that the name of the copyright holders not be used in advertising or 9 * publicity pertaining to distribution of the software without specific, 10 * written prior permission. The copyright holders make no representations 11 * about the suitability of this software for any purpose. It is provided "as 12 * is" without express or implied warranty. 13 * 14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 20 * OF THIS SOFTWARE. 21 */ 22 23#include "glamor_priv.h" 24#include "glamor_program.h" 25#include "glamor_transform.h" 26#include "glamor_transfer.h" 27#include "glamor_prepare.h" 28 29static const char dash_vs_vars[] = 30 "attribute vec3 primitive;\n" 31 "varying float dash_offset;\n"; 32 33static const char dash_vs_exec[] = 34 " dash_offset = primitive.z / dash_length;\n" 35 GLAMOR_POS(gl_Position, primitive.xy); 36 37static const char dash_fs_vars[] = 38 "varying float dash_offset;\n"; 39 40static const char on_off_fs_exec[] = 41 " float pattern = texture2D(dash, vec2(dash_offset, 0.5)).w;\n" 42 " if (pattern == 0.0)\n" 43 " discard;\n"; 44 45/* XXX deal with stippled double dashed lines once we have stippling support */ 46static const char double_fs_exec[] = 47 " float pattern = texture2D(dash, vec2(dash_offset, 0.5)).w;\n" 48 " if (pattern == 0.0)\n" 49 " gl_FragColor = bg;\n" 50 " else\n" 51 " gl_FragColor = fg;\n"; 52 53 54static const glamor_facet glamor_facet_on_off_dash_lines = { 55 .version = 130, 56 .name = "poly_lines_on_off_dash", 57 .vs_vars = dash_vs_vars, 58 .vs_exec = dash_vs_exec, 59 .fs_vars = dash_fs_vars, 60 .fs_exec = on_off_fs_exec, 61 .locations = glamor_program_location_dash, 62}; 63 64static const glamor_facet glamor_facet_double_dash_lines = { 65 .version = 130, 66 .name = "poly_lines_double_dash", 67 .vs_vars = dash_vs_vars, 68 .vs_exec = dash_vs_exec, 69 .fs_vars = dash_fs_vars, 70 .fs_exec = double_fs_exec, 71 .locations = (glamor_program_location_dash| 72 glamor_program_location_fg| 73 glamor_program_location_bg), 74}; 75 76static PixmapPtr 77glamor_get_dash_pixmap(GCPtr gc) 78{ 79 glamor_gc_private *gc_priv = glamor_get_gc_private(gc); 80 ScreenPtr screen = gc->pScreen; 81 PixmapPtr pixmap; 82 int offset; 83 int d; 84 uint32_t pixel; 85 GCPtr scratch_gc; 86 87 if (gc_priv->dash) 88 return gc_priv->dash; 89 90 offset = 0; 91 for (d = 0; d < gc->numInDashList; d++) 92 offset += gc->dash[d]; 93 94 pixmap = glamor_create_pixmap(screen, offset, 1, 8, 0); 95 if (!pixmap) 96 goto bail; 97 98 scratch_gc = GetScratchGC(8, screen); 99 if (!scratch_gc) 100 goto bail_pixmap; 101 102 pixel = 0xffffffff; 103 offset = 0; 104 for (d = 0; d < gc->numInDashList; d++) { 105 xRectangle rect; 106 ChangeGCVal changes; 107 108 changes.val = pixel; 109 (void) ChangeGC(NullClient, scratch_gc, 110 GCForeground, &changes); 111 ValidateGC(&pixmap->drawable, scratch_gc); 112 rect.x = offset; 113 rect.y = 0; 114 rect.width = gc->dash[d]; 115 rect.height = 1; 116 scratch_gc->ops->PolyFillRect (&pixmap->drawable, scratch_gc, 1, &rect); 117 offset += gc->dash[d]; 118 pixel = ~pixel; 119 } 120 FreeScratchGC(scratch_gc); 121 122 gc_priv->dash = pixmap; 123 return pixmap; 124 125bail_pixmap: 126 glamor_destroy_pixmap(pixmap); 127bail: 128 return NULL; 129} 130 131static glamor_program * 132glamor_dash_setup(DrawablePtr drawable, GCPtr gc) 133{ 134 ScreenPtr screen = drawable->pScreen; 135 glamor_screen_private *glamor_priv = glamor_get_screen_private(screen); 136 PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable); 137 glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap); 138 PixmapPtr dash_pixmap; 139 glamor_pixmap_private *dash_priv; 140 glamor_program *prog; 141 142 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv)) 143 goto bail; 144 145 if (gc->lineWidth != 0) 146 goto bail; 147 148 dash_pixmap = glamor_get_dash_pixmap(gc); 149 dash_priv = glamor_get_pixmap_private(pixmap); 150 151 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dash_priv)) 152 goto bail; 153 154 glamor_make_current(glamor_priv); 155 156 switch (gc->lineStyle) { 157 case LineOnOffDash: 158 prog = glamor_use_program_fill(pixmap, gc, 159 &glamor_priv->on_off_dash_line_progs, 160 &glamor_facet_on_off_dash_lines); 161 if (!prog) 162 goto bail; 163 break; 164 case LineDoubleDash: 165 if (gc->fillStyle != FillSolid) 166 goto bail; 167 168 prog = &glamor_priv->double_dash_line_prog; 169 170 if (!prog->prog) { 171 if (!glamor_build_program(screen, prog, 172 &glamor_facet_double_dash_lines, 173 NULL, NULL, NULL)) 174 goto bail; 175 } 176 177 if (!glamor_use_program(pixmap, gc, prog, NULL)) 178 goto bail; 179 180 glamor_set_color(pixmap, gc->fgPixel, prog->fg_uniform); 181 glamor_set_color(pixmap, gc->bgPixel, prog->bg_uniform); 182 break; 183 184 default: 185 goto bail; 186 } 187 188 189 /* Set the dash pattern as texture 1 */ 190 191 glamor_bind_texture(glamor_priv, GL_TEXTURE1, dash_priv->fbo, FALSE); 192 glUniform1i(prog->dash_uniform, 1); 193 glUniform1f(prog->dash_length_uniform, dash_pixmap->drawable.width); 194 195 return prog; 196 197bail: 198 return NULL; 199} 200 201static void 202glamor_dash_loop(DrawablePtr drawable, GCPtr gc, glamor_program *prog, 203 int n, GLenum mode) 204{ 205 PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable); 206 glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap); 207 int box_index; 208 int off_x, off_y; 209 210 glEnable(GL_SCISSOR_TEST); 211 212 glamor_pixmap_loop(pixmap_priv, box_index) { 213 int nbox = RegionNumRects(gc->pCompositeClip); 214 BoxPtr box = RegionRects(gc->pCompositeClip); 215 216 glamor_set_destination_drawable(drawable, box_index, TRUE, TRUE, 217 prog->matrix_uniform, &off_x, &off_y); 218 219 while (nbox--) { 220 glScissor(box->x1 + off_x, 221 box->y1 + off_y, 222 box->x2 - box->x1, 223 box->y2 - box->y1); 224 box++; 225 glDrawArrays(mode, 0, n); 226 } 227 } 228 229 glDisable(GL_SCISSOR_TEST); 230 glDisableVertexAttribArray(GLAMOR_VERTEX_POS); 231} 232 233static int 234glamor_line_length(short x1, short y1, short x2, short y2) 235{ 236 return max(abs(x2 - x1), abs(y2 - y1)); 237} 238 239Bool 240glamor_poly_lines_dash_gl(DrawablePtr drawable, GCPtr gc, 241 int mode, int n, DDXPointPtr points) 242{ 243 ScreenPtr screen = drawable->pScreen; 244 glamor_program *prog; 245 short *v; 246 char *vbo_offset; 247 int add_last; 248 int dash_pos; 249 int prev_x, prev_y; 250 int i; 251 252 if (n < 2) 253 return TRUE; 254 255 if (!(prog = glamor_dash_setup(drawable, gc))) 256 return FALSE; 257 258 add_last = 0; 259 if (gc->capStyle != CapNotLast) 260 add_last = 1; 261 262 /* Set up the vertex buffers for the points */ 263 264 v = glamor_get_vbo_space(drawable->pScreen, 265 (n + add_last) * 3 * sizeof (short), 266 &vbo_offset); 267 268 glEnableVertexAttribArray(GLAMOR_VERTEX_POS); 269 glVertexAttribPointer(GLAMOR_VERTEX_POS, 3, GL_SHORT, GL_FALSE, 270 3 * sizeof (short), vbo_offset); 271 272 dash_pos = gc->dashOffset; 273 prev_x = prev_y = 0; 274 for (i = 0; i < n; i++) { 275 int this_x = points[i].x; 276 int this_y = points[i].y; 277 if (i) { 278 if (mode == CoordModePrevious) { 279 this_x += prev_x; 280 this_y += prev_y; 281 } 282 dash_pos += glamor_line_length(prev_x, prev_y, 283 this_x, this_y); 284 } 285 v[0] = prev_x = this_x; 286 v[1] = prev_y = this_y; 287 v[2] = dash_pos; 288 v += 3; 289 } 290 291 if (add_last) { 292 v[0] = prev_x + 1; 293 v[1] = prev_y; 294 v[2] = dash_pos + 1; 295 } 296 297 glamor_put_vbo_space(screen); 298 299 glamor_dash_loop(drawable, gc, prog, n + add_last, GL_LINE_STRIP); 300 301 return TRUE; 302} 303 304static short * 305glamor_add_segment(short *v, short x1, short y1, short x2, short y2, 306 int dash_start, int dash_end) 307{ 308 v[0] = x1; 309 v[1] = y1; 310 v[2] = dash_start; 311 312 v[3] = x2; 313 v[4] = y2; 314 v[5] = dash_end; 315 return v + 6; 316} 317 318Bool 319glamor_poly_segment_dash_gl(DrawablePtr drawable, GCPtr gc, 320 int nseg, xSegment *segs) 321{ 322 ScreenPtr screen = drawable->pScreen; 323 glamor_program *prog; 324 short *v; 325 char *vbo_offset; 326 int dash_start = gc->dashOffset; 327 int add_last; 328 int i; 329 330 if (!(prog = glamor_dash_setup(drawable, gc))) 331 return FALSE; 332 333 add_last = 0; 334 if (gc->capStyle != CapNotLast) 335 add_last = 1; 336 337 /* Set up the vertex buffers for the points */ 338 339 v = glamor_get_vbo_space(drawable->pScreen, 340 (nseg<<add_last) * 6 * sizeof (short), 341 &vbo_offset); 342 343 glEnableVertexAttribArray(GLAMOR_VERTEX_POS); 344 glVertexAttribPointer(GLAMOR_VERTEX_POS, 3, GL_SHORT, GL_FALSE, 345 3 * sizeof (short), vbo_offset); 346 347 for (i = 0; i < nseg; i++) { 348 int dash_end = dash_start + glamor_line_length(segs[i].x1, segs[i].y1, 349 segs[i].x2, segs[i].y2); 350 v = glamor_add_segment(v, 351 segs[i].x1, segs[i].y1, 352 segs[i].x2, segs[i].y2, 353 dash_start, dash_end); 354 if (add_last) 355 v = glamor_add_segment(v, 356 segs[i].x2, segs[i].y2, 357 segs[i].x2 + 1, segs[i].y2, 358 dash_end, dash_end + 1); 359 } 360 361 glamor_put_vbo_space(screen); 362 363 glamor_dash_loop(drawable, gc, prog, nseg << (1 + add_last), GL_LINES); 364 365 return TRUE; 366} 367