glamor_fill.c 7.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright © 2008 Intel Corporation
 * Copyright © 1998 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 Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD 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.
22 23 24 25
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *    Zhigang Gong <zhigang.gong@linux.intel.com>
26 27 28 29 30 31 32 33
 */

#include "glamor_priv.h"

/** @file glamor_fillspans.c
 *
 * GC fill implementation, based loosely on fb_fill.c
 */
Zhigang Gong's avatar
Zhigang Gong committed
34
Bool
35
glamor_fill(DrawablePtr drawable,
36
	    GCPtr gc, int x, int y, int width, int height, Bool fallback)
37
{
Zhigang Gong's avatar
Zhigang Gong committed
38 39
	PixmapPtr dst_pixmap = glamor_get_drawable_pixmap(drawable);
	int off_x, off_y;
40 41 42 43
	PixmapPtr sub_pixmap = NULL;
	glamor_access_t sub_pixmap_access;
	DrawablePtr saved_drawable = NULL;
	int saved_x, saved_y;
Zhigang Gong's avatar
Zhigang Gong committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

	glamor_get_drawable_deltas(drawable, dst_pixmap, &off_x, &off_y);

	switch (gc->fillStyle) {
	case FillSolid:
		if (!glamor_solid(dst_pixmap,
				  x + off_x,
				  y + off_y,
				  width, height, gc->alu, gc->planemask,
				  gc->fgPixel))
			goto fail;
		break;
	case FillStippled:
	case FillOpaqueStippled:
		if (!glamor_stipple(dst_pixmap,
				    gc->stipple,
				    x + off_x,
				    y + off_y,
				    width,
				    height,
				    gc->alu,
				    gc->planemask,
				    gc->fgPixel,
				    gc->bgPixel, gc->patOrg.x,
				    gc->patOrg.y))
			goto fail;
		break;
	case FillTiled:
		if (!glamor_tile(dst_pixmap,
				 gc->tile.pixmap,
				 x + off_x,
				 y + off_y,
				 width,
				 height,
				 gc->alu,
				 gc->planemask,
80 81
				 x - drawable->x - gc->patOrg.x,
				 y - drawable->y - gc->patOrg.y))
Zhigang Gong's avatar
Zhigang Gong committed
82 83 84 85
			goto fail;
		break;
	}
	return TRUE;
86

Zhigang Gong's avatar
Zhigang Gong committed
87
      fail:
88 89 90 91 92
	if (!fallback) {
		if (glamor_ddx_fallback_check_pixmap(&dst_pixmap->drawable)
		   && glamor_ddx_fallback_check_gc(gc))
		return FALSE;
	}
93
	/* Is it possible to set the access as WO? */
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	sub_pixmap_access = GLAMOR_ACCESS_RW;

	sub_pixmap = glamor_get_sub_pixmap(dst_pixmap, x + off_x,
					   y + off_y, width, height,
					   sub_pixmap_access);

	if (sub_pixmap != NULL) {
		if (gc->fillStyle != FillSolid) {
			gc->patOrg.x += (drawable->x - x);
			gc->patOrg.y += (drawable->y - y);
		}
		saved_drawable = drawable;
		drawable = &sub_pixmap->drawable;
		saved_x = x;
		saved_y = y;
		x = 0;
		y = 0;
	}
Zhigang Gong's avatar
Zhigang Gong committed
113 114 115 116 117
	if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW)) {
		if (glamor_prepare_access_gc(gc)) {
			fbFill(drawable, gc, x, y, width, height);
			glamor_finish_access_gc(gc);
		}
118
		glamor_finish_access(drawable, GLAMOR_ACCESS_RW);
119
	}
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

	if (sub_pixmap != NULL) {
		struct pixman_box16 box;
		int dx, dy;
		if (gc->fillStyle != FillSolid) {
			gc->patOrg.x -= (saved_drawable->x - saved_x);
			gc->patOrg.y -= (saved_drawable->y - saved_y);
		}

		x = saved_x;
		y = saved_y;

		glamor_put_sub_pixmap(sub_pixmap, dst_pixmap,
				      x + off_x, y + off_y,
				      width, height, sub_pixmap_access);
	}

Zhigang Gong's avatar
Zhigang Gong committed
137
	return TRUE;
138
}
139 140 141 142

void
glamor_init_solid_shader(ScreenPtr screen)
{
143 144
	glamor_screen_private *glamor_priv;
	glamor_gl_dispatch *dispatch;
Zhigang Gong's avatar
Zhigang Gong committed
145 146 147 148 149 150 151 152 153
	const char *solid_vs =
	    "attribute vec4 v_position;"
	    "void main()\n" "{\n" "       gl_Position = v_position;\n"
	    "}\n";
	const char *solid_fs =
	    GLAMOR_DEFAULT_PRECISION "uniform vec4 color;\n"
	    "void main()\n" "{\n" "	gl_FragColor = color;\n" "}\n";
	GLint fs_prog, vs_prog;

154
	glamor_priv = glamor_get_screen_private(screen);
Chris Wilson's avatar
Chris Wilson committed
155
	dispatch =  glamor_get_dispatch(glamor_priv);
Zhigang Gong's avatar
Zhigang Gong committed
156
	glamor_priv->solid_prog = dispatch->glCreateProgram();
157 158 159
	vs_prog = glamor_compile_glsl_prog(dispatch, GL_VERTEX_SHADER, solid_vs);
	fs_prog = glamor_compile_glsl_prog(dispatch, GL_FRAGMENT_SHADER,
					   solid_fs);
Zhigang Gong's avatar
Zhigang Gong committed
160 161 162 163 164 165 166 167 168 169
	dispatch->glAttachShader(glamor_priv->solid_prog, vs_prog);
	dispatch->glAttachShader(glamor_priv->solid_prog, fs_prog);

	dispatch->glBindAttribLocation(glamor_priv->solid_prog,
				       GLAMOR_VERTEX_POS, "v_position");
	glamor_link_glsl_prog(dispatch, glamor_priv->solid_prog);

	glamor_priv->solid_color_uniform_location =
	    dispatch->glGetUniformLocation(glamor_priv->solid_prog,
					   "color");
Chris Wilson's avatar
Chris Wilson committed
170
	glamor_put_dispatch(glamor_priv);
171 172
}

173 174 175 176 177 178 179
void
glamor_fini_solid_shader(ScreenPtr screen)
{
	glamor_screen_private *glamor_priv;
	glamor_gl_dispatch *dispatch;

	glamor_priv = glamor_get_screen_private(screen);
Chris Wilson's avatar
Chris Wilson committed
180
	dispatch = glamor_get_dispatch(glamor_priv);
181
	dispatch->glDeleteProgram(glamor_priv->solid_prog);
Chris Wilson's avatar
Chris Wilson committed
182
	glamor_put_dispatch(glamor_priv);
183 184
}

185
Bool
186
glamor_solid(PixmapPtr pixmap, int x, int y, int width, int height,
Zhigang Gong's avatar
Zhigang Gong committed
187 188
	     unsigned char alu, unsigned long planemask,
	     unsigned long fg_pixel)
189
{
Zhigang Gong's avatar
Zhigang Gong committed
190 191 192 193 194
	ScreenPtr screen = pixmap->drawable.pScreen;
	glamor_screen_private *glamor_priv =
	    glamor_get_screen_private(screen);
	glamor_pixmap_private *pixmap_priv =
	    glamor_get_pixmap_private(pixmap);
Chris Wilson's avatar
Chris Wilson committed
195
	glamor_gl_dispatch *dispatch;
Zhigang Gong's avatar
Zhigang Gong committed
196 197 198 199 200 201 202
	int x1 = x;
	int x2 = x + width;
	int y1 = y;
	int y2 = y + height;
	GLfloat color[4];
	float vertices[8];
	GLfloat xscale, yscale;
Chris Wilson's avatar
Chris Wilson committed
203

204
	if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv)) {
Zhigang Gong's avatar
Zhigang Gong committed
205
		glamor_fallback("dest %p has no fbo.\n", pixmap);
Chris Wilson's avatar
Chris Wilson committed
206
		return FALSE;
Zhigang Gong's avatar
Zhigang Gong committed
207
	}
Chris Wilson's avatar
Chris Wilson committed
208

Zhigang Gong's avatar
Zhigang Gong committed
209 210 211
	if (!glamor_set_planemask(pixmap, planemask)) {
		glamor_fallback
		    ("Failedto set planemask  in glamor_solid.\n");
Chris Wilson's avatar
Chris Wilson committed
212
		return FALSE;
Zhigang Gong's avatar
Zhigang Gong committed
213 214 215 216 217 218 219
	}

	glamor_get_rgba_from_pixel(fg_pixel,
				   &color[0],
				   &color[1],
				   &color[2],
				   &color[3], format_for_pixmap(pixmap));
220

Zhigang Gong's avatar
Zhigang Gong committed
221 222
	glamor_set_destination_pixmap_priv_nc(pixmap_priv);

Chris Wilson's avatar
Chris Wilson committed
223
	dispatch = glamor_get_dispatch(glamor_priv);
224
	if (!glamor_set_alu(dispatch, alu)) {
225 226 227 228 229 230 231
		if (alu == GXclear)
			color[0] = color[1] = color[2] = color[3] = 0.0;
		else {
			glamor_fallback("unsupported alu %x\n", alu);
			glamor_put_dispatch(glamor_priv);
			return FALSE;
		}
232
	}
Zhigang Gong's avatar
Zhigang Gong committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	dispatch->glUseProgram(glamor_priv->solid_prog);

	dispatch->glUniform4fv(glamor_priv->solid_color_uniform_location,
			       1, color);

	dispatch->glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT,
					GL_FALSE, 2 * sizeof(float),
					vertices);
	dispatch->glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
	pixmap_priv_get_scale(pixmap_priv, &xscale, &yscale);

	glamor_set_normalize_vcoords(xscale, yscale, x1, y1, x2, y2,
				     glamor_priv->yInverted, vertices);
	dispatch->glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
	dispatch->glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
	dispatch->glUseProgram(0);
249
	glamor_set_alu(dispatch, GXcopy);
Chris Wilson's avatar
Chris Wilson committed
250
	glamor_put_dispatch(glamor_priv);
Zhigang Gong's avatar
Zhigang Gong committed
251
	return TRUE;
252
}