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 " vec2 pos = vec2(0,0);\n" 36 GLAMOR_POS(gl_Position, primitive.xy); 37 38static const char dash_fs_vars[] = 39 "varying float dash_offset;\n"; 40 41static const char on_off_fs_exec[] = 42 " float pattern = texture2D(dash, vec2(dash_offset, 0.5)).w;\n" 43 " if (pattern == 0.0)\n" 44 " discard;\n"; 45 46/* XXX deal with stippled double dashed lines once we have stippling support */ 47static const char double_fs_exec[] = 48 " float pattern = texture2D(dash, vec2(dash_offset, 0.5)).w;\n" 49 " if (pattern == 0.0)\n" 50 " gl_FragColor = bg;\n" 51 " else\n" 52 " gl_FragColor = fg;\n"; 53 54 55static const glamor_facet glamor_facet_on_off_dash_lines = { 56 .version = 130, 57 .name = "poly_lines_on_off_dash", 58 .vs_vars = dash_vs_vars, 59 .vs_exec = dash_vs_exec, 60 .fs_vars = dash_fs_vars, 61 .fs_exec = on_off_fs_exec, 62 .locations = glamor_program_location_dash, 63}; 64 65static const glamor_facet glamor_facet_double_dash_lines = { 66 .version = 130, 67 .name = "poly_lines_double_dash", 68 .vs_vars = dash_vs_vars, 69 .vs_exec = dash_vs_exec, 70 .fs_vars = dash_fs_vars, 71 .fs_exec = double_fs_exec, 72 .locations = (glamor_program_location_dash| 73 glamor_program_location_fg| 74 glamor_program_location_bg), 75}; 76 77static PixmapPtr 78glamor_get_dash_pixmap(GCPtr gc) 79{ 80 glamor_gc_private *gc_priv = glamor_get_gc_private(gc); 81 ScreenPtr screen = gc->pScreen; 82 PixmapPtr pixmap; 83 int offset; 84 int d; 85 uint32_t pixel; 86 GCPtr scratch_gc; 87 88 if (gc_priv->dash) 89 return gc_priv->dash; 90 91 offset = 0; 92 for (d = 0; d < gc->numInDashList; d++) 93 offset += gc->dash[d]; 94 95 pixmap = glamor_create_pixmap(screen, offset, 1, 8, 0); 96 if (!pixmap) 97 goto bail; 98 99 scratch_gc = GetScratchGC(8, screen); 100 if (!scratch_gc) 101 goto bail_pixmap; 102 103 pixel = 0xffffffff; 104 offset = 0; 105 for (d = 0; d < gc->numInDashList; d++) { 106 xRectangle rect; 107 ChangeGCVal changes; 108 109 changes.val = pixel; 110 (void) ChangeGC(NullClient, scratch_gc, 111 GCForeground, &changes); 112 ValidateGC(&pixmap->drawable, scratch_gc); 113 rect.x = offset; 114 rect.y = 0; 115 rect.width = gc->dash[d]; 116 rect.height = 1; 117 scratch_gc->ops->PolyFillRect (&pixmap->drawable, scratch_gc, 1, &rect); 118 offset += gc->dash[d]; 119 pixel = ~pixel; 120 } 121 FreeScratchGC(scratch_gc); 122 123 gc_priv->dash = pixmap; 124 return pixmap; 125 126bail_pixmap: 127 glamor_destroy_pixmap(pixmap); 128bail: 129 return NULL; 130} 131 132static glamor_program * 133glamor_dash_setup(DrawablePtr drawable, GCPtr gc) 134{ 135 ScreenPtr screen = drawable->pScreen; 136 glamor_screen_private *glamor_priv = glamor_get_screen_private(screen); 137 PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable); 138 glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap); 139 PixmapPtr dash_pixmap; 140 glamor_pixmap_private *dash_priv; 141 glamor_program *prog; 142 143 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv)) 144 goto bail; 145 146 if (gc->lineWidth != 0) 147 goto bail; 148 149 dash_pixmap = glamor_get_dash_pixmap(gc); 150 dash_priv = glamor_get_pixmap_private(dash_pixmap); 151 152 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dash_priv)) 153 goto bail; 154 155 glamor_make_current(glamor_priv); 156 157 switch (gc->lineStyle) { 158 case LineOnOffDash: 159 prog = glamor_use_program_fill(pixmap, gc, 160 &glamor_priv->on_off_dash_line_progs, 161 &glamor_facet_on_off_dash_lines); 162 if (!prog) 163 goto bail; 164 break; 165 case LineDoubleDash: 166 if (gc->fillStyle != FillSolid) 167 goto bail; 168 169 prog = &glamor_priv->double_dash_line_prog; 170 171 if (!prog->prog) { 172 if (!glamor_build_program(screen, prog, 173 &glamor_facet_double_dash_lines, 174 NULL, NULL, NULL)) 175 goto bail; 176 } 177 178 if (!glamor_use_program(pixmap, gc, prog, NULL)) 179 goto bail; 180 181 glamor_set_color(pixmap, gc->fgPixel, prog->fg_uniform); 182 glamor_set_color(pixmap, gc->bgPixel, prog->bg_uniform); 183 break; 184 185 default: 186 goto bail; 187 } 188 189 190 /* Set the dash pattern as texture 1 */ 191 192 glamor_bind_texture(glamor_priv, GL_TEXTURE1, dash_priv->fbo, FALSE); 193 glUniform1i(prog->dash_uniform, 1); 194 glUniform1f(prog->dash_length_uniform, dash_pixmap->drawable.width); 195 196 return prog; 197 198bail: 199 return NULL; 200} 201 202static void 203glamor_dash_loop(DrawablePtr drawable, GCPtr gc, glamor_program *prog, 204 int n, GLenum mode) 205{ 206 PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable); 207 glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap); 208 int box_index; 209 int off_x, off_y; 210 211 glEnable(GL_SCISSOR_TEST); 212 213 glamor_pixmap_loop(pixmap_priv, box_index) { 214 int nbox = RegionNumRects(gc->pCompositeClip); 215 BoxPtr box = RegionRects(gc->pCompositeClip); 216 217 glamor_set_destination_drawable(drawable, box_index, TRUE, TRUE, 218 prog->matrix_uniform, &off_x, &off_y); 219 220 while (nbox--) { 221 glScissor(box->x1 + off_x, 222 box->y1 + off_y, 223 box->x2 - box->x1, 224 box->y2 - box->y1); 225 box++; 226 glDrawArrays(mode, 0, n); 227 } 228 } 229 230 glDisable(GL_SCISSOR_TEST); 231 glDisableVertexAttribArray(GLAMOR_VERTEX_POS); 232} 233 234static int 235glamor_line_length(short x1, short y1, short x2, short y2) 236{ 237 return max(abs(x2 - x1), abs(y2 - y1)); 238} 239 240Bool 241glamor_poly_lines_dash_gl(DrawablePtr drawable, GCPtr gc, 242 int mode, int n, DDXPointPtr points) 243{ 244 ScreenPtr screen = drawable->pScreen; 245 glamor_program *prog; 246 short *v; 247 char *vbo_offset; 248 int add_last; 249 int dash_pos; 250 int prev_x, prev_y; 251 int i; 252 253 if (n < 2) 254 return TRUE; 255 256 if (!(prog = glamor_dash_setup(drawable, gc))) 257 return FALSE; 258 259 add_last = 0; 260 if (gc->capStyle != CapNotLast) 261 add_last = 1; 262 263 /* Set up the vertex buffers for the points */ 264 265 v = glamor_get_vbo_space(drawable->pScreen, 266 (n + add_last) * 3 * sizeof (short), 267 &vbo_offset); 268 269 glEnableVertexAttribArray(GLAMOR_VERTEX_POS); 270 glVertexAttribPointer(GLAMOR_VERTEX_POS, 3, GL_SHORT, GL_FALSE, 271 3 * sizeof (short), vbo_offset); 272 273 dash_pos = gc->dashOffset; 274 prev_x = prev_y = 0; 275 for (i = 0; i < n; i++) { 276 int this_x = points[i].x; 277 int this_y = points[i].y; 278 if (i) { 279 if (mode == CoordModePrevious) { 280 this_x += prev_x; 281 this_y += prev_y; 282 } 283 dash_pos += glamor_line_length(prev_x, prev_y, 284 this_x, this_y); 285 } 286 v[0] = prev_x = this_x; 287 v[1] = prev_y = this_y; 288 v[2] = dash_pos; 289 v += 3; 290 } 291 292 if (add_last) { 293 v[0] = prev_x + 1; 294 v[1] = prev_y; 295 v[2] = dash_pos + 1; 296 } 297 298 glamor_put_vbo_space(screen); 299 300 glamor_dash_loop(drawable, gc, prog, n + add_last, GL_LINE_STRIP); 301 302 return TRUE; 303} 304 305static short * 306glamor_add_segment(short *v, short x1, short y1, short x2, short y2, 307 int dash_start, int dash_end) 308{ 309 v[0] = x1; 310 v[1] = y1; 311 v[2] = dash_start; 312 313 v[3] = x2; 314 v[4] = y2; 315 v[5] = dash_end; 316 return v + 6; 317} 318 319Bool 320glamor_poly_segment_dash_gl(DrawablePtr drawable, GCPtr gc, 321 int nseg, xSegment *segs) 322{ 323 ScreenPtr screen = drawable->pScreen; 324 glamor_program *prog; 325 short *v; 326 char *vbo_offset; 327 int dash_start = gc->dashOffset; 328 int add_last; 329 int i; 330 331 if (!(prog = glamor_dash_setup(drawable, gc))) 332 return FALSE; 333 334 add_last = 0; 335 if (gc->capStyle != CapNotLast) 336 add_last = 1; 337 338 /* Set up the vertex buffers for the points */ 339 340 v = glamor_get_vbo_space(drawable->pScreen, 341 (nseg<<add_last) * 6 * sizeof (short), 342 &vbo_offset); 343 344 glEnableVertexAttribArray(GLAMOR_VERTEX_POS); 345 glVertexAttribPointer(GLAMOR_VERTEX_POS, 3, GL_SHORT, GL_FALSE, 346 3 * sizeof (short), vbo_offset); 347 348 for (i = 0; i < nseg; i++) { 349 int dash_end = dash_start + glamor_line_length(segs[i].x1, segs[i].y1, 350 segs[i].x2, segs[i].y2); 351 v = glamor_add_segment(v, 352 segs[i].x1, segs[i].y1, 353 segs[i].x2, segs[i].y2, 354 dash_start, dash_end); 355 if (add_last) 356 v = glamor_add_segment(v, 357 segs[i].x2, segs[i].y2, 358 segs[i].x2 + 1, segs[i].y2, 359 dash_end, dash_end + 1); 360 } 361 362 glamor_put_vbo_space(screen); 363 364 glamor_dash_loop(drawable, gc, prog, nseg << (1 + add_last), GL_LINES); 365 366 return TRUE; 367} 368