glamor_rects.c revision 1b5d61b8
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 27static const glamor_facet glamor_facet_polyfillrect_130 = { 28 .name = "poly_fill_rect", 29 .version = 130, 30 .vs_vars = "attribute vec4 primitive;\n", 31 .vs_exec = (" vec2 pos = primitive.zw * vec2(gl_VertexID&1, (gl_VertexID&2)>>1);\n" 32 GLAMOR_POS(gl_Position, (primitive.xy + pos))), 33}; 34 35static const glamor_facet glamor_facet_polyfillrect_120 = { 36 .name = "poly_fill_rect", 37 .vs_vars = "attribute vec2 primitive;\n", 38 .vs_exec = (" vec2 pos = vec2(0,0);\n" 39 GLAMOR_POS(gl_Position, primitive.xy)), 40}; 41 42static Bool 43glamor_poly_fill_rect_gl(DrawablePtr drawable, 44 GCPtr gc, int nrect, xRectangle *prect) 45{ 46 ScreenPtr screen = drawable->pScreen; 47 glamor_screen_private *glamor_priv = glamor_get_screen_private(screen); 48 PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable); 49 glamor_pixmap_private *pixmap_priv; 50 glamor_program *prog; 51 int off_x, off_y; 52 GLshort *v; 53 char *vbo_offset; 54 int box_index; 55 Bool ret = FALSE; 56 BoxRec bounds = glamor_no_rendering_bounds(); 57 58 pixmap_priv = glamor_get_pixmap_private(pixmap); 59 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv)) 60 goto bail; 61 62 glamor_make_current(glamor_priv); 63 64 if (nrect < 100) { 65 bounds = glamor_start_rendering_bounds(); 66 for (int i = 0; i < nrect; i++) 67 glamor_bounds_union_rect(&bounds, &prect[i]); 68 } 69 70 if (glamor_priv->glsl_version >= 130) { 71 prog = glamor_use_program_fill(pixmap, gc, 72 &glamor_priv->poly_fill_rect_program, 73 &glamor_facet_polyfillrect_130); 74 75 if (!prog) 76 goto bail; 77 78 /* Set up the vertex buffers for the points */ 79 80 v = glamor_get_vbo_space(drawable->pScreen, nrect * sizeof (xRectangle), &vbo_offset); 81 82 glEnableVertexAttribArray(GLAMOR_VERTEX_POS); 83 glVertexAttribDivisor(GLAMOR_VERTEX_POS, 1); 84 glVertexAttribPointer(GLAMOR_VERTEX_POS, 4, GL_SHORT, GL_FALSE, 85 4 * sizeof (short), vbo_offset); 86 87 memcpy(v, prect, nrect * sizeof (xRectangle)); 88 89 glamor_put_vbo_space(screen); 90 } else { 91 int n; 92 93 prog = glamor_use_program_fill(pixmap, gc, 94 &glamor_priv->poly_fill_rect_program, 95 &glamor_facet_polyfillrect_120); 96 97 if (!prog) 98 goto bail; 99 100 /* Set up the vertex buffers for the points */ 101 102 v = glamor_get_vbo_space(drawable->pScreen, nrect * 8 * sizeof (short), &vbo_offset); 103 104 glEnableVertexAttribArray(GLAMOR_VERTEX_POS); 105 glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE, 106 2 * sizeof (short), vbo_offset); 107 108 for (n = 0; n < nrect; n++) { 109 v[0] = prect->x; v[1] = prect->y; 110 v[2] = prect->x; v[3] = prect->y + prect->height; 111 v[4] = prect->x + prect->width; v[5] = prect->y + prect->height; 112 v[6] = prect->x + prect->width; v[7] = prect->y; 113 prect++; 114 v += 8; 115 } 116 117 glamor_put_vbo_space(screen); 118 } 119 120 glEnable(GL_SCISSOR_TEST); 121 122 glamor_pixmap_loop(pixmap_priv, box_index) { 123 int nbox = RegionNumRects(gc->pCompositeClip); 124 BoxPtr box = RegionRects(gc->pCompositeClip); 125 126 if (!glamor_set_destination_drawable(drawable, box_index, TRUE, FALSE, 127 prog->matrix_uniform, &off_x, &off_y)) 128 goto bail; 129 130 while (nbox--) { 131 BoxRec scissor = { 132 .x1 = max(box->x1, bounds.x1 + drawable->x), 133 .y1 = max(box->y1, bounds.y1 + drawable->y), 134 .x2 = min(box->x2, bounds.x2 + drawable->x), 135 .y2 = min(box->y2, bounds.y2 + drawable->y), 136 }; 137 138 box++; 139 140 if (scissor.x1 >= scissor.x2 || scissor.y1 >= scissor.y2) 141 continue; 142 143 glScissor(scissor.x1 + off_x, 144 scissor.y1 + off_y, 145 scissor.x2 - scissor.x1, 146 scissor.y2 - scissor.y1); 147 if (glamor_priv->glsl_version >= 130) 148 glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, nrect); 149 else { 150 glamor_glDrawArrays_GL_QUADS(glamor_priv, nrect); 151 } 152 } 153 } 154 155 ret = TRUE; 156 157bail: 158 glDisable(GL_SCISSOR_TEST); 159 if (glamor_priv->glsl_version >= 130) 160 glVertexAttribDivisor(GLAMOR_VERTEX_POS, 0); 161 glDisableVertexAttribArray(GLAMOR_VERTEX_POS); 162 163 return ret; 164} 165 166static void 167glamor_poly_fill_rect_bail(DrawablePtr drawable, 168 GCPtr gc, int nrect, xRectangle *prect) 169{ 170 glamor_fallback("to %p (%c)\n", drawable, 171 glamor_get_drawable_location(drawable)); 172 if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) && 173 glamor_prepare_access_gc(gc)) { 174 fbPolyFillRect(drawable, gc, nrect, prect); 175 } 176 glamor_finish_access_gc(gc); 177 glamor_finish_access(drawable); 178} 179 180void 181glamor_poly_fill_rect(DrawablePtr drawable, 182 GCPtr gc, int nrect, xRectangle *prect) 183{ 184 if (glamor_poly_fill_rect_gl(drawable, gc, nrect, prect)) 185 return; 186 glamor_poly_fill_rect_bail(drawable, gc, nrect, prect); 187} 188