glamor_putimage.c 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Copyright © 2009 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *
 */


/** @file glamor_putaimge.c
 *
 * XPutImage implementation
 */
#include "glamor_priv.h"

35 36 37 38 39 40 41 42 43 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
void
glamor_init_putimage_shaders(ScreenPtr screen)
{
    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
    const char *xybitmap_vs =
	"uniform float x_bias;\n"
	"uniform float x_scale;\n"
	"uniform float y_bias;\n"
	"uniform float y_scale;\n"
	"varying vec2 bitmap_coords;\n"
	"void main()\n"
	"{\n"
	"	gl_Position = vec4((gl_Vertex.x + x_bias) * x_scale,\n"
	"			   (gl_Vertex.y + y_bias) * y_scale,\n"
	"			   0,\n"
	"			   1);\n"
	"	bitmap_coords = gl_MultiTexCoord0.xy;\n"
	"}\n";
    const char *xybitmap_fs =
	"uniform vec4 fg, bg;\n"
	"varying vec2 bitmap_coords;\n"
	"uniform sampler2D bitmap_sampler;\n"
	"void main()\n"
	"{\n"
	"	float bitmap_value = texture2D(bitmap_sampler,\n"
	"				       bitmap_coords).x;\n"
	"	gl_FragColor = mix(bg, fg, bitmap_value);\n"
	"}\n";
    GLint fs_prog, vs_prog, prog;
    GLint sampler_uniform_location;

    if (!GLEW_ARB_fragment_shader)
	return;

    prog = glCreateProgramObjectARB();
    vs_prog = glamor_compile_glsl_prog(GL_VERTEX_SHADER_ARB, xybitmap_vs);
    fs_prog = glamor_compile_glsl_prog(GL_FRAGMENT_SHADER_ARB, xybitmap_fs);
    glAttachObjectARB(prog, vs_prog);
    glAttachObjectARB(prog, fs_prog);
    glamor_link_glsl_prog(prog);

    glUseProgramObjectARB(prog);
    sampler_uniform_location = glGetUniformLocationARB(prog, "bitmap_sampler");
    glUniform1iARB(sampler_uniform_location, 0);

    glamor_priv->put_image_xybitmap_fg_uniform_location =
	glGetUniformLocationARB(prog, "fg");
    glamor_priv->put_image_xybitmap_bg_uniform_location =
	glGetUniformLocationARB(prog, "bg");
    glamor_get_transform_uniform_locations(prog,
					   &glamor_priv->put_image_xybitmap_transform);
    glamor_priv->put_image_xybitmap_prog = prog;
    glUseProgramObjectARB(0);
}


/* Do an XYBitmap putimage.  The bits are byte-aligned rows of bitmap
 * data (where each row starts at a bit index of left_pad), and the
 * destination gets filled with the gc's fg color where the bitmap is set
 * and the bg color where the bitmap is unset.
 *
 * Implement this by passing the bitmap right through to GL, and sampling
 * it to choose between fg and bg in the fragment shader.  The driver may
 * be exploding the bitmap up to be an 8-bit alpha texture, in which
 * case we might be better off just doing the fg/bg choosing in the CPU
 * and just draw the resulting texture to the destination.
 */
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#if 0

static int
y_flip(PixmapPtr pixmap, int y)
{
    ScreenPtr screen = pixmap->drawable.pScreen;
    PixmapPtr screen_pixmap = screen->GetScreenPixmap(screen);

    if (pixmap == screen_pixmap)
	return (pixmap->drawable.height - 1) - y;
    else
	return y;
}


117 118 119 120 121 122 123 124 125 126 127 128 129 130
static void
glamor_put_image_xybitmap(DrawablePtr drawable, GCPtr gc,
			  int x, int y, int w, int h, int left_pad,
			  int image_format, char *bits)
{
    ScreenPtr screen = drawable->pScreen;
    PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
    float fg[4], bg[4];
    GLuint tex;
    unsigned int stride = PixmapBytePad(1, w + left_pad);
    RegionPtr clip;
    BoxPtr box;
    int nbox;
131
    float dest_coords[8];
132 133 134 135 136 137
    const float bitmap_coords[8] = {
	0.0, 0.0,
	1.0, 0.0,
	1.0, 1.0,
	0.0, 1.0,
    };
138 139 140 141
    GLfloat xscale, yscale;
    glamor_pixmap_private *pixmap_priv;
   
    pixmap_priv = glamor_get_pixmap_private(pixmap);
142

143
    pixmap_priv_get_scale(pixmap_priv, &xscale, &yscale);
144

145 146 147 148 149
    glamor_set_normalize_vcoords(xscale, yscale,
				 x, y,
				 x + w, y + h,
				 glamor_priv->yInverted,
				 dest_coords);
150

151
    glamor_fallback("glamor_put_image_xybitmap: disabled\n");
152 153
    goto fail;

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    if (glamor_priv->put_image_xybitmap_prog == 0) {
	ErrorF("no program for xybitmap putimage\n");
	goto fail;
    }

    glamor_set_alu(gc->alu);
    if (!glamor_set_planemask(pixmap, gc->planemask))
	goto fail;

    glUseProgramObjectARB(glamor_priv->put_image_xybitmap_prog);

    glamor_get_color_4f_from_pixel(pixmap, gc->fgPixel, fg);
    glUniform4fvARB(glamor_priv->put_image_xybitmap_fg_uniform_location,
		    1, fg);
    glamor_get_color_4f_from_pixel(pixmap, gc->bgPixel, bg);
    glUniform4fvARB(glamor_priv->put_image_xybitmap_bg_uniform_location,
		    1, bg);

    glGenTextures(1, &tex);
173
    glActiveTexture(GL_TEXTURE0);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glPixelStorei(GL_UNPACK_ROW_LENGTH, stride * 8);
    glPixelStorei(GL_UNPACK_SKIP_PIXELS, left_pad);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA,
		 w, h, 0,
		 GL_COLOR_INDEX, GL_BITMAP, bits);
    glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
    glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
    glEnable(GL_TEXTURE_2D);

    /* Now that we've set up our bitmap texture and the shader, shove
     * the destination rectangle through the cliprects and run the
     * shader on the resulting fragments.
     */
    glVertexPointer(2, GL_FLOAT, 0, dest_coords);
    glEnableClientState(GL_VERTEX_ARRAY);
    glClientActiveTexture(GL_TEXTURE0);
    glTexCoordPointer(2, GL_FLOAT, 0, bitmap_coords);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glEnable(GL_SCISSOR_TEST);
    clip = fbGetCompositeClip(gc);
    for (nbox = REGION_NUM_RECTS(clip),
	 box = REGION_RECTS(clip);
	 nbox--;
	 box++)
    {
	int x1 = x;
	int y1 = y;
	int x2 = x + w;
	int y2 = y + h;

	if (x1 < box->x1)
	    x1 = box->x1;
	if (y1 < box->y1)
	    y1 = box->y1;
	if (x2 > box->x2)
	    x2 = box->x2;
	if (y2 > box->y2)
	    y2 = box->y2;
	if (x1 >= x2 || y1 >= y2)
	    continue;

	glScissor(box->x1,
		  y_flip(pixmap, box->y1),
		  box->x2 - box->x1,
		  box->y2 - box->y1);
	glDrawArrays(GL_QUADS, 0, 4);
    }

    glDisable(GL_SCISSOR_TEST);
    glamor_set_alu(GXcopy);
    glamor_set_planemask(pixmap, ~0);
    glDeleteTextures(1, &tex);
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    return;
    glamor_set_alu(GXcopy);
    glamor_set_planemask(pixmap, ~0);
238
    glamor_fallback(": to %p (%c)\n",
239
		    drawable, glamor_get_drawable_location(drawable));
240
fail:
241 242 243 244
    if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW)) {
	fbPutImage(drawable, gc, 1, x, y, w, h, left_pad, XYBitmap, bits);
	glamor_finish_access(drawable);
    }
245
}
246 247
#endif

248

249 250 251 252
void
glamor_put_image(DrawablePtr drawable, GCPtr gc, int depth, int x, int y,
		 int w, int h, int left_pad, int image_format, char *bits)
{
253 254
    glamor_screen_private *glamor_priv =
	glamor_get_screen_private(drawable->pScreen);
255
    PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
256
    glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
257 258 259 260 261
    GLenum type, format;
    RegionPtr clip;
    BoxPtr pbox;
    int nbox;
    int src_stride = PixmapBytePad(w, drawable->depth);
262
    int x_off, y_off;
263 264
    float vertices[8], texcoords[8];
    GLfloat xscale, yscale, txscale, tyscale;
265
    GLuint tex;
266
    int ax = 0;
267 268
    if (image_format == XYBitmap) {
	assert(depth == 1);
269
        goto fail;
270 271 272
	return;
    }

273 274 275
    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv)) {
	glamor_fallback("has no fbo.\n");
        goto fail;
276 277
    }

278
    if (image_format != ZPixmap) {
279
	glamor_fallback("non-ZPixmap\n");
280 281 282
	goto fail;
    }

283
    if (!glamor_set_planemask(pixmap, gc->planemask)) {
284 285
	goto fail;
    }
286
    glamor_set_alu(gc->alu);
287

288 289 290 291 292 293 294 295 296 297 298
    if (glamor_get_tex_format_type_from_pixmap(pixmap,
                                               &format, 
                                               &type, 
                                               &ax
                                               )) {
      glamor_fallback("unknown depth. %d \n", 
                     drawable->depth);
      goto fail;
    }

    /* XXX consider to reuse a function to do the following work. */
299 300
    glVertexPointer(2, GL_FLOAT, sizeof(float) * 2, vertices);
    glEnableClientState(GL_VERTEX_ARRAY);
301

302 303 304 305
    glClientActiveTexture(GL_TEXTURE0);
    glTexCoordPointer(2, GL_FLOAT, sizeof(float) * 2, texcoords);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

306
    glamor_set_destination_pixmap_priv_nc(pixmap_priv);
307

308
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
309 310 311
    glPixelStorei(GL_UNPACK_ROW_LENGTH, src_stride * 8 /
		  pixmap->drawable.bitsPerPixel);

312
    
313 314 315 316 317 318 319 320 321 322 323
    glGenTextures(1, &tex);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
		 w, h, 0,
		 format, type, bits);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glEnable(GL_TEXTURE_2D);

    assert(GLEW_ARB_fragment_shader);
324
    glUseProgramObjectARB(glamor_priv->finish_access_prog[ax]);
325 326 327 328 329

    x += drawable->x;
    y += drawable->y;

    glamor_get_drawable_deltas(drawable, pixmap, &x_off, &y_off);
330
    clip = fbGetCompositeClip(gc);
331 332 333 334 335

    txscale = 1.0/w;
    tyscale = 1.0/h;
    pixmap_priv_get_scale(pixmap_priv, &xscale, &yscale);

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    for (nbox = REGION_NUM_RECTS(clip),
	 pbox = REGION_RECTS(clip);
	 nbox--;
	 pbox++)
    {
	int x1 = x;
	int y1 = y;
	int x2 = x + w;
	int y2 = y + h;

	if (x1 < pbox->x1)
	    x1 = pbox->x1;
	if (y1 < pbox->y1)
	    y1 = pbox->y1;
	if (x2 > pbox->x2)
	    x2 = pbox->x2;
	if (y2 > pbox->y2)
	    y2 = pbox->y2;
	if (x1 >= x2 || y1 >= y2)
	    continue;

357 358 359 360 361 362 363 364 365 366 367 368
	glamor_set_normalize_tcoords( txscale, tyscale, 
				      x1 - x, y1 - y,
				      x2 - x, y2 - y,
				      1,
				      texcoords);

	glamor_set_normalize_vcoords( xscale, yscale,
				      x1 + x_off, y1 + y_off,
				      x2 + x_off, y2 + y_off,
				      glamor_priv->yInverted,
				      vertices);

369
	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
370
    }
371 372 373 374 375 376

    glDisable(GL_TEXTURE_2D);
    glUseProgramObjectARB(0);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDeleteTextures(1, &tex);
377
    glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
378
    glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
379
    glamor_set_alu(GXcopy);
380
    glamor_set_planemask(pixmap, ~0);
381 382 383
    return;

fail:
384
    glamor_set_planemask(pixmap, ~0);
385
    glamor_fallback("to %p (%c)\n",
386 387 388 389 390 391
		    drawable, glamor_get_drawable_location(drawable));
    if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW)) {
	fbPutImage(drawable, gc, depth, x, y, w, h, left_pad, image_format,
		   bits);
	glamor_finish_access(drawable);
    }
392
}