qxl_surface.c 22.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
/*
 * Copyright 2010 Red Hat, Inc.
 *
 * 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
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, 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 NON-INFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS 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.
 */

Dave Airlie's avatar
Dave Airlie committed
23 24 25 26
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

27
#include "qxl.h"
28
#include "qxl_surface.h"/* send anything pending to the other side */
29

30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
enum ROPDescriptor
{
    ROPD_INVERS_SRC = (1 << 0),
    ROPD_INVERS_BRUSH = (1 << 1),
    ROPD_INVERS_DEST = (1 << 2),
    ROPD_OP_PUT = (1 << 3),
    ROPD_OP_OR = (1 << 4),
    ROPD_OP_AND = (1 << 5),
    ROPD_OP_XOR = (1 << 6),
    ROPD_OP_BLACKNESS = (1 << 7),
    ROPD_OP_WHITENESS = (1 << 8),
    ROPD_OP_INVERS = (1 << 9),
    ROPD_INVERS_RES = (1 <<10),
};

46
static struct qxl_bo *
47
make_drawable (qxl_screen_t *qxl, qxl_surface_t *surf, uint8_t type,
48
	       const struct QXLRect *rect
49 50
	       /* , pRegion clip */)
{
51
    struct QXLDrawable *drawable;
52
    struct qxl_bo *draw_bo;
53
    int i;
54 55 56 57
   
    draw_bo = qxl->bo_funcs->cmd_alloc (qxl, sizeof *drawable, "drawable command");
    assert(draw_bo);
    drawable = qxl->bo_funcs->bo_map(draw_bo);
58
    assert(drawable);
59
    
60
    drawable->release_info.id = pointer_to_u64 (draw_bo);
61 62 63
    
    drawable->type = type;
    
64
    qxl->bo_funcs->bo_output_surf_reloc(qxl, offsetof(struct QXLDrawable, surface_id), draw_bo, surf);
65

66 67 68 69 70 71 72
    drawable->effect = QXL_EFFECT_OPAQUE;
    drawable->self_bitmap = 0;
    drawable->self_bitmap_area.top = 0;
    drawable->self_bitmap_area.left = 0;
    drawable->self_bitmap_area.bottom = 0;
    drawable->self_bitmap_area.right = 0;
    /* FIXME: add clipping */
73
    drawable->clip.type = SPICE_CLIP_TYPE_NONE;
74 75 76 77 78 79 80 81 82 83 84 85 86
    
    /*
     * surfaces_dest[i] should apparently be filled out with the
     * surfaces that we depend on, and surface_rects should be
     * filled with the rectangles of those surfaces that we
     * are going to use.
     */
    for (i = 0; i < 3; ++i)
	drawable->surfaces_dest[i] = -1;
    
    if (rect)
	drawable->bbox = *rect;
    
87
    /* No longer needed since spice-server commit c541d7e29 */
Dave Airlie's avatar
Dave Airlie committed
88 89
    if (!qxl->kms_enabled)
        drawable->mm_time = qxl->rom->mm_clock;
90 91
    else
        drawable->mm_time = 0;
92 93 94

    qxl->bo_funcs->bo_unmap(draw_bo);
    return draw_bo;
95 96 97
}

static void
98
push_drawable (qxl_screen_t *qxl, struct qxl_bo *drawable_bo)
99
{
100
    qxl->bo_funcs->write_command (qxl, QXL_CMD_DRAW, drawable_bo);
101 102 103
}

static void
104
submit_fill (qxl_screen_t *qxl, qxl_surface_t *surf,
105
	     const struct QXLRect *rect, uint32_t color)
106
{
107
    struct qxl_bo *drawable_bo;
108
    struct QXLDrawable *drawable;
109
    
110
    drawable_bo = make_drawable (qxl, surf, QXL_DRAW_FILL, rect);
111
    
112
    drawable = qxl->bo_funcs->bo_map(drawable_bo);
113
    drawable->u.fill.brush.type = SPICE_BRUSH_TYPE_SOLID;
114 115 116 117 118 119 120
    drawable->u.fill.brush.u.color = color;
    drawable->u.fill.rop_descriptor = ROPD_OP_PUT;
    drawable->u.fill.mask.flags = 0;
    drawable->u.fill.mask.pos.x = 0;
    drawable->u.fill.mask.pos.y = 0;
    drawable->u.fill.mask.bitmap = 0;
    
121 122 123
    qxl->bo_funcs->bo_unmap(drawable_bo);

    push_drawable (qxl, drawable_bo);
124 125
}

126 127 128 129 130 131 132
void
qxl_surface_flush (qxl_surface_t *surface)
{
    ;
}

/* access */
133 134 135 136 137 138 139 140 141 142
static void
download_box_no_update (qxl_surface_t *surface, int x1, int y1, int x2, int y2)
{
    pixman_image_composite (PIXMAN_OP_SRC,
                            surface->dev_image,
                            NULL,
                            surface->host_image,
                            x1, y1, 0, 0, x1, y1, x2 - x1, y2 - y1);
}

143 144
void
qxl_download_box (qxl_surface_t *surface, int x1, int y1, int x2, int y2)
145
{
146 147 148 149 150
    assert (x2 >= x1 && y2 >= y1);

    if (x1 == x2 || y1 == y2)
        return;

151
    surface->qxl->bo_funcs->update_area(surface, x1, y1, x2, y2);
152

153
    download_box_no_update(surface, x1, y1, x2, y2);
154 155 156 157 158 159 160 161 162 163 164
}

Bool
qxl_surface_prepare_access (qxl_surface_t  *surface,
			    PixmapPtr       pixmap,
			    RegionPtr       region,
			    uxa_access_t    access)
{
    int n_boxes;
    BoxPtr boxes;
    ScreenPtr pScreen = pixmap->drawable.pScreen;
165
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
166 167
    RegionRec new;

168 169 170
    if (!pScrn->vtSema)
        return FALSE;

171 172 173
    REGION_INIT (NULL, &new, (BoxPtr)NULL, 0);
    REGION_SUBTRACT (NULL, &new, region, &surface->access_region);

174 175 176
    if (access == UXA_ACCESS_RW)
	surface->access_type = UXA_ACCESS_RW;
    
177 178 179 180 181 182 183 184 185
    region = &new;
    
    n_boxes = REGION_NUM_RECTS (region);
    boxes = REGION_RECTS (region);

    if (n_boxes < 25)
    {
	while (n_boxes--)
	{
186
	    qxl_download_box (surface, boxes->x1, boxes->y1, boxes->x2, boxes->y2);
187 188 189 190 191 192
	    
	    boxes++;
	}
    }
    else
    {
193
	qxl_download_box (
194 195
	    surface,
	    new.extents.x1, new.extents.y1, new.extents.x2, new.extents.y2);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    }
    
    REGION_UNION (pScreen,
		  &(surface->access_region),
		  &(surface->access_region),
		      region);
    
    REGION_UNINIT (NULL, &new);
    
    pScreen->ModifyPixmapHeader(
	pixmap,
	pixmap->drawable.width,
	pixmap->drawable.height,
	-1, -1, -1,
	pixman_image_get_data (surface->host_image));

    pixmap->devKind = pixman_image_get_stride (surface->host_image);
    
    return TRUE;
}

static void
218
translate_rect (struct QXLRect *rect)
219 220 221 222 223 224 225
{
    rect->right -= rect->left;
    rect->bottom -= rect->top;
    rect->left = rect->top = 0;
}

static void
226
real_upload_box (qxl_surface_t *surface, int x1, int y1, int x2, int y2)
227
{
228 229
    struct QXLRect rect;
    struct QXLDrawable *drawable;
230
    struct qxl_bo *image_bo, *drawable_bo;
231
    qxl_screen_t *qxl = surface->qxl;
232 233 234 235 236 237 238 239
    uint32_t *data;
    int stride;
    
    rect.left = x1;
    rect.right = x2;
    rect.top = y1;
    rect.bottom = y2;
    
240 241
    drawable_bo = make_drawable (qxl, surface, QXL_DRAW_COPY, &rect);
    drawable = qxl->bo_funcs->bo_map(drawable_bo);
242 243 244 245 246 247 248 249 250
    drawable->u.copy.src_area = rect;
    translate_rect (&drawable->u.copy.src_area);
    drawable->u.copy.rop_descriptor = ROPD_OP_PUT;
    drawable->u.copy.scale_mode = 0;
    drawable->u.copy.mask.flags = 0;
    drawable->u.copy.mask.pos.x = 0;
    drawable->u.copy.mask.pos.y = 0;
    drawable->u.copy.mask.bitmap = 0;

251 252
    qxl->bo_funcs->bo_unmap(drawable_bo);

253 254 255
    data = pixman_image_get_data (surface->host_image);
    stride = pixman_image_get_stride (surface->host_image);
    
256
    image_bo = qxl_image_create (
Søren Sandmann's avatar
Søren Sandmann committed
257
	qxl, (const uint8_t *)data, x1, y1, x2 - x1, y2 - y1, stride, 
258
	surface->bpp == 24 ? 4 : surface->bpp / 8, TRUE);
259 260 261 262 263
    qxl->bo_funcs->bo_output_bo_reloc(qxl, offsetof(QXLDrawable, u.copy.src_bitmap),
				   drawable_bo, image_bo);
    push_drawable (qxl, drawable_bo);

    qxl->bo_funcs->bo_decref(qxl, image_bo);
264 265
}

266 267 268
#define TILE_WIDTH 512
#define TILE_HEIGHT 512

Dave Airlie's avatar
Dave Airlie committed
269 270
void
qxl_upload_box (qxl_surface_t *surface, int x1, int y1, int x2, int y2)
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
{
    int tile_x1, tile_y1;

    for (tile_y1 = y1; tile_y1 < y2; tile_y1 += TILE_HEIGHT)
    {
	for (tile_x1 = x1; tile_x1 < x2; tile_x1 += TILE_WIDTH)
	{
	    int tile_x2 = tile_x1 + TILE_WIDTH;
	    int tile_y2 = tile_y1 + TILE_HEIGHT;

	    if (tile_x2 > x2)
		tile_x2 = x2;
	    if (tile_y2 > y2)
		tile_y2 = y2;

	    real_upload_box (surface, tile_x1, tile_y1, tile_x2, tile_y2);
	}
    }
}

291 292 293 294
static void
upload_one_primary_region(qxl_screen_t *qxl, PixmapPtr pixmap, BoxPtr b)
{
    struct QXLRect rect;
295
    struct qxl_bo *drawable_bo, *image_bo;
296 297 298 299 300
    struct QXLDrawable *drawable;
    FbBits *data;
    int stride;
    int bpp;

301 302 303
    if (b->x1 >= qxl->virtual_x || b->y1 >= qxl->virtual_y)
        return;

304
    rect.left = b->x1;
305
    rect.right = min(b->x2, qxl->virtual_x);
306
    rect.top = b->y1;
307
    rect.bottom = min(b->y2, qxl->virtual_y);
308

309
    drawable_bo = make_drawable (qxl, qxl->primary, QXL_DRAW_COPY, &rect);
310
    drawable = qxl->bo_funcs->bo_map(drawable_bo);
311 312 313 314 315 316 317 318
    drawable->u.copy.src_area = rect;
    translate_rect (&drawable->u.copy.src_area);
    drawable->u.copy.rop_descriptor = ROPD_OP_PUT;
    drawable->u.copy.scale_mode = 0;
    drawable->u.copy.mask.flags = 0;
    drawable->u.copy.mask.pos.x = 0;
    drawable->u.copy.mask.pos.y = 0;
    drawable->u.copy.mask.bitmap = 0;
319
    qxl->bo_funcs->bo_unmap(drawable_bo);
320 321

    fbGetPixmapBitsData(pixmap, data, stride, bpp);
322
    image_bo = qxl_image_create (
323
	qxl, (const uint8_t *)data, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, stride * sizeof(*data),
324
	bpp == 24 ? 4 : bpp / 8, TRUE);
325 326
    qxl->bo_funcs->bo_output_bo_reloc(qxl, offsetof(QXLDrawable, u.copy.src_bitmap),
				   drawable_bo, image_bo);
327

328 329
    push_drawable (qxl, drawable_bo);
    qxl->bo_funcs->bo_decref(qxl, image_bo);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
}

void
qxl_surface_upload_primary_regions(qxl_screen_t *qxl, PixmapPtr pixmap, RegionRec *r)
{
    int n_boxes;
    BoxPtr boxes;

    n_boxes = RegionNumRects(r);
    boxes = RegionRects(r);

    while (n_boxes--)
    {
        upload_one_primary_region(qxl, pixmap, boxes);
        boxes++;
    }
}

348 349 350 351 352 353 354 355 356 357 358 359
void
qxl_surface_finish_access (qxl_surface_t *surface, PixmapPtr pixmap)
{
    ScreenPtr pScreen = pixmap->drawable.pScreen;
    int w = pixmap->drawable.width;
    int h = pixmap->drawable.height;
    int n_boxes;
    BoxPtr boxes;

    n_boxes = REGION_NUM_RECTS (&surface->access_region);
    boxes = REGION_RECTS (&surface->access_region);

360
    if (surface->access_type == UXA_ACCESS_RW)
361
    {
362
	if (n_boxes < 25)
363
	{
364 365
	    while (n_boxes--)
	    {
Dave Airlie's avatar
Dave Airlie committed
366
		qxl_upload_box (surface, boxes->x1, boxes->y1, boxes->x2, boxes->y2);
367 368 369 370 371 372
		
		boxes++;
	    }
	}
	else
	{
Dave Airlie's avatar
Dave Airlie committed
373
	    qxl_upload_box (surface,
374 375 376 377
			surface->access_region.extents.x1,
			surface->access_region.extents.y1,
			surface->access_region.extents.x2,
			surface->access_region.extents.y2);
378 379 380 381
	}
    }

    REGION_EMPTY (pScreen, &surface->access_region);
382
    surface->access_type = UXA_ACCESS_RO;
383 384 385 386 387
    
    pScreen->ModifyPixmapHeader(pixmap, w, h, -1, -1, 0, NULL);
}


388
#ifdef DEBUG_REGIONS
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
static void
print_region (const char *header, RegionPtr pRegion)
{
    int nbox = REGION_NUM_RECTS (pRegion);
    BoxPtr pbox = REGION_RECTS (pRegion);
    
    ErrorF ("%s", header);

    if (nbox == 0)
	ErrorF (" (empty)\n");
    else
	ErrorF ("\n");
    
    while (nbox--)
    {
	ErrorF ("   %d %d %d %d (size: %d %d)\n",
		pbox->x1, pbox->y1, pbox->x2, pbox->y2,
		pbox->x2 - pbox->x1, pbox->y2 - pbox->y1);
	
	pbox++;
    }
}
411
#endif // DEBUG_REGIONS
412

413 414 415 416 417
/* solid */
Bool
qxl_surface_prepare_solid (qxl_surface_t *destination,
			   Pixel	  fg)
{
418 419 420 421 422
    if (!REGION_NIL (&(destination->access_region)))
    {
	ErrorF (" solid not in vmem\n");
    }

423
#ifdef DEBUG_REGIONS
424 425 426
    print_region ("prepare solid", &(destination->access_region));
#endif
    
427
    destination->u.solid_pixel = fg; //  ^ (rand() >> 16);
428

429 430 431 432 433 434 435 436 437 438
    return TRUE;
}

void
qxl_surface_solid (qxl_surface_t *destination,
		   int	          x1,
		   int	          y1,
		   int	          x2,
		   int	          y2)
{
439
    qxl_screen_t *qxl = destination->qxl;
440
    struct QXLRect qrect;
441
    uint32_t p;
442 443 444 445 446

    qrect.top = y1;
    qrect.bottom = y2;
    qrect.left = x1;
    qrect.right = x2;
447

448
    p = destination->u.solid_pixel;
449
    
450
    submit_fill (qxl, destination, &qrect, p);
451 452 453 454 455 456 457
}

/* copy */
Bool
qxl_surface_prepare_copy (qxl_surface_t *dest,
			  qxl_surface_t *source)
{
458 459 460 461 462 463
    if (!REGION_NIL (&(dest->access_region))	||
	!REGION_NIL (&(source->access_region)))
    {
	return FALSE;
    }

464
    dest->u.copy_src = source;
465

466 467 468
    return TRUE;
}

Dave Airlie's avatar
Dave Airlie committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

static struct qxl_bo *
image_from_surface_internal(qxl_screen_t *qxl,
			    qxl_surface_t *surface)
{
    struct qxl_bo *image_bo = qxl->bo_funcs->bo_alloc (qxl, sizeof(struct QXLImage), "image struct for surface");
    struct QXLImage *image = qxl->bo_funcs->bo_map(image_bo);

    image->descriptor.id = 0;
    image->descriptor.type = SPICE_IMAGE_TYPE_SURFACE;
    image->descriptor.width = 0;
    image->descriptor.height = 0;
    qxl->bo_funcs->bo_unmap(image_bo);
    return image_bo;
}

485
static struct qxl_bo *image_from_surface(qxl_screen_t *qxl, qxl_surface_t *dest)
Dave Airlie's avatar
Dave Airlie committed
486 487 488 489 490 491 492 493 494 495
{
    if (!dest->image_bo)
      dest->image_bo = image_from_surface_internal(qxl, dest);

    qxl->bo_funcs->bo_incref(qxl, dest->image_bo);
    qxl->bo_funcs->bo_output_surf_reloc(qxl, offsetof(struct QXLImage, surface_image.surface_id), dest->image_bo, dest);

    return dest->image_bo;
}

496 497 498 499 500 501
void
qxl_surface_copy (qxl_surface_t *dest,
		  int  src_x1, int src_y1,
		  int  dest_x1, int dest_y1,
		  int width, int height)
{
502
    qxl_screen_t *qxl = dest->qxl;
503
    struct qxl_bo *drawable_bo;
504 505
    struct QXLDrawable *drawable;
    struct QXLRect qrect;
506

507
#ifdef DEBUG_REGIONS
508 509 510
    print_region (" copy src", &(dest->u.copy_src->access_region));
    print_region (" copy dest", &(dest->access_region));
#endif
511

512 513 514 515 516
    qrect.top = dest_y1;
    qrect.bottom = dest_y1 + height;
    qrect.left = dest_x1;
    qrect.right = dest_x1 + width;
    
517 518
    if (dest->id == dest->u.copy_src->id)
    {
519
	drawable_bo = make_drawable (qxl, dest, QXL_COPY_BITS, &qrect);
520

521
	drawable = qxl->bo_funcs->bo_map(drawable_bo);
522 523
	drawable->u.copy_bits.src_pos.x = src_x1;
	drawable->u.copy_bits.src_pos.y = src_y1;
524 525 526 527
	qxl->bo_funcs->bo_unmap(drawable_bo);

	push_drawable (qxl, drawable_bo);

528 529 530
    }
    else
    {
531
	struct qxl_bo *image_bo;
532 533

	dest->u.copy_src->ref_count++;
Dave Airlie's avatar
Dave Airlie committed
534 535

	image_bo = image_from_surface(qxl, dest->u.copy_src);
536

537
	drawable_bo = make_drawable (qxl, dest, QXL_DRAW_COPY, &qrect);
538

539 540 541
	drawable = qxl->bo_funcs->bo_map(drawable_bo);
	qxl->bo_funcs->bo_output_bo_reloc(qxl, offsetof(QXLDrawable, u.copy.src_bitmap),
				       drawable_bo, image_bo);
542 543 544 545 546 547 548 549 550 551
	drawable->u.copy.src_area.left = src_x1;
	drawable->u.copy.src_area.top = src_y1;
	drawable->u.copy.src_area.right = src_x1 + width;
	drawable->u.copy.src_area.bottom = src_y1 + height;
	drawable->u.copy.rop_descriptor = ROPD_OP_PUT;
	drawable->u.copy.scale_mode = 0;
	drawable->u.copy.mask.flags = 0;
	drawable->u.copy.mask.pos.x = 0;
	drawable->u.copy.mask.pos.y = 0;
	drawable->u.copy.mask.bitmap = 0;
552

553
	qxl->bo_funcs->bo_output_surf_reloc(qxl, offsetof(struct QXLDrawable, surfaces_dest[0]), drawable_bo, dest->u.copy_src);
554
	drawable->surfaces_rects[0] = drawable->u.copy.src_area;
555
 	
556 557
	assert (src_x1 >= 0);
	assert (src_y1 >= 0);
Søren Sandmann's avatar
Søren Sandmann committed
558 559 560 561 562 563 564

	if (width > pixman_image_get_width (dest->u.copy_src->host_image))
	{
	    ErrorF ("dest w: %d   src w: %d\n",
		    width, pixman_image_get_width (dest->u.copy_src->host_image));
	}
	
565 566
	assert (width <= pixman_image_get_width (dest->u.copy_src->host_image));
	assert (height <= pixman_image_get_height (dest->u.copy_src->host_image));
567

568 569 570 571
	qxl->bo_funcs->bo_unmap(drawable_bo);
	push_drawable (qxl, drawable_bo);
	qxl->bo_funcs->bo_decref(qxl, image_bo);
    }
572
}
573

Søren Sandmann's avatar
Søren Sandmann committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
/* composite */
Bool
qxl_surface_prepare_composite (int op,
			       PicturePtr	src_picture,
			       PicturePtr	mask_picture,
			       PicturePtr	dest_picture,
			       qxl_surface_t *	src,
			       qxl_surface_t *	mask,
			       qxl_surface_t *	dest)
{
    dest->u.composite.op = op;
    dest->u.composite.src_picture = src_picture;
    dest->u.composite.mask_picture = mask_picture;
    dest->u.composite.dest_picture = dest_picture;
    dest->u.composite.src = src;
    dest->u.composite.mask = mask;
    dest->u.composite.dest = dest;
    
    return TRUE;
}

595
static struct qxl_bo *
Søren Sandmann's avatar
Søren Sandmann committed
596 597 598 599 600 601 602 603 604
image_from_picture (qxl_screen_t *qxl,
		    PicturePtr picture,
		    qxl_surface_t *surface,
		    int *force_opaque)
{
    if (picture->format == PICT_x8r8g8b8)
	*force_opaque = TRUE;
    else
	*force_opaque = FALSE;
Dave Airlie's avatar
Dave Airlie committed
605 606

    return image_from_surface(qxl, surface);
Søren Sandmann's avatar
Søren Sandmann committed
607 608
}

609
static struct qxl_bo *
Søren Sandmann's avatar
Søren Sandmann committed
610 611 612 613
get_transform (qxl_screen_t *qxl, PictTransform *transform)
{
    if (transform)
    {
614 615
	struct qxl_bo *qxform_bo = qxl->bo_funcs->bo_alloc (qxl, sizeof (QXLTransform), "transform");
	QXLTransform *qxform = qxl->bo_funcs->bo_map(qxform_bo);
Søren Sandmann's avatar
Søren Sandmann committed
616 617 618 619 620 621 622 623

	qxform->t00 = transform->matrix[0][0];
	qxform->t01 = transform->matrix[0][1];
	qxform->t02 = transform->matrix[0][2];
	qxform->t10 = transform->matrix[1][0];
	qxform->t11 = transform->matrix[1][1];
	qxform->t12 = transform->matrix[1][2];

624 625
	qxl->bo_funcs->bo_unmap(qxform_bo);
	return qxform_bo;
Søren Sandmann's avatar
Søren Sandmann committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
    }
    else
    {
	return NULL;
    }
}

static QXLRect
full_rect (qxl_surface_t *surface)
{
    QXLRect r;
    int w = pixman_image_get_width (surface->host_image);
    int h = pixman_image_get_height (surface->host_image);
	    
    r.left = r.top = 0;
    r.right = w;
    r.bottom = h;

    return r;
}

void
qxl_surface_composite (qxl_surface_t *dest,
		       int src_x, int src_y,
		       int mask_x, int mask_y,
		       int dest_x, int dest_y,
		       int width, int height)
{
654
    qxl_screen_t *qxl = dest->qxl;
Søren Sandmann's avatar
Søren Sandmann committed
655 656 657 658 659 660
    PicturePtr src = dest->u.composite.src_picture;
    qxl_surface_t *qsrc = dest->u.composite.src;
    PicturePtr mask = dest->u.composite.mask_picture;
    qxl_surface_t *qmask = dest->u.composite.mask;
    int op = dest->u.composite.op;
    struct QXLDrawable *drawable;
661
    struct qxl_bo *drawable_bo;
Søren Sandmann's avatar
Søren Sandmann committed
662 663
    QXLComposite *composite;
    QXLRect rect;
664
    struct qxl_bo *trans_bo, *img_bo;
Søren Sandmann's avatar
Søren Sandmann committed
665 666
    int n_deps = 0;
    int force_opaque;
667 668
    struct qxl_bo *derefs[4];
    int n_derefs = 0, i;
Søren Sandmann's avatar
Søren Sandmann committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
#if 0
    ErrorF ("QXL Composite: src:       %x (%d %d) id: %d; \n"
	    "               mask:      id: %d\n"
	    "               dest:      %x %d %d %d %d (id: %d)\n",
	    dest->u.composite.src_picture->format,
	    dest->u.composite.src_picture->pDrawable->width,
	    dest->u.composite.src_picture->pDrawable->height,
	    dest->u.composite.src->id,
	    dest->u.composite.mask? dest->u.composite.mask->id : -1,
	    dest->u.composite.dest_picture->format,
	    dest_x, dest_y, width, height,
	    dest->id
	);
#endif

    rect.left = dest_x;
    rect.right = dest_x + width;
    rect.top = dest_y;
    rect.bottom = dest_y + height;
    
689 690 691
    drawable_bo = make_drawable (qxl, dest, QXL_DRAW_COMPOSITE, &rect);

    drawable = qxl->bo_funcs->bo_map(drawable_bo);
Søren Sandmann's avatar
Søren Sandmann committed
692 693 694 695 696 697 698 699 700 701

    composite = &drawable->u.composite;

    composite->flags = 0;

    if (dest->u.composite.dest_picture->format == PICT_x8r8g8b8)
	composite->flags |= SPICE_COMPOSITE_DEST_OPAQUE;
    
    composite->flags |= (op & 0xff);

702
    img_bo = image_from_picture (qxl, src, qsrc, &force_opaque);
Søren Sandmann's avatar
Søren Sandmann committed
703 704
    if (force_opaque)
	composite->flags |= SPICE_COMPOSITE_SOURCE_OPAQUE;
705 706 707 708
    qxl->bo_funcs->bo_output_bo_reloc(qxl, offsetof(QXLDrawable, u.composite.src),
				   drawable_bo, img_bo);
    derefs[n_derefs++] = img_bo;

Søren Sandmann's avatar
Søren Sandmann committed
709 710
    composite->flags |= (src->filter << 8);
    composite->flags |= (src->repeat << 14);
711 712 713 714 715 716 717 718 719
    trans_bo = get_transform (qxl, src->transform);
    if (trans_bo) {
	qxl->bo_funcs->bo_output_bo_reloc(qxl, offsetof(QXLDrawable, u.composite.src_transform),
				       drawable_bo, trans_bo);
	derefs[n_derefs++] = trans_bo;
    } else
	composite->src_transform = 0;

    qxl->bo_funcs->bo_output_surf_reloc(qxl, offsetof(struct QXLDrawable, surfaces_dest[n_deps]), drawable_bo, qsrc);
Søren Sandmann's avatar
Søren Sandmann committed
720 721 722 723 724 725
    drawable->surfaces_rects[n_deps] = full_rect (qsrc);

    n_deps++;
    
    if (mask)
    {
726
	img_bo = image_from_picture (qxl, mask, qmask, &force_opaque);
Søren Sandmann's avatar
Søren Sandmann committed
727 728
	if (force_opaque)
	    composite->flags |= SPICE_COMPOSITE_MASK_OPAQUE;
729 730 731 732

	qxl->bo_funcs->bo_output_bo_reloc(qxl, offsetof(QXLDrawable, u.composite.mask),
				       drawable_bo, img_bo);
	derefs[n_derefs++] = img_bo;
Søren Sandmann's avatar
Søren Sandmann committed
733 734 735 736
	composite->flags |= (mask->filter << 11);
	composite->flags |= (mask->repeat << 16);
	composite->flags |= (mask->componentAlpha << 18);

737
	qxl->bo_funcs->bo_output_surf_reloc(qxl, offsetof(struct QXLDrawable, surfaces_dest[n_deps]), drawable_bo, qmask);
Søren Sandmann's avatar
Søren Sandmann committed
738 739 740
	drawable->surfaces_rects[n_deps] = full_rect (qmask);
	n_deps++;
	
741 742 743 744 745 746 747 748
	trans_bo = get_transform (qxl, src->transform);
	if (trans_bo) {
	    qxl->bo_funcs->bo_output_bo_reloc(qxl, offsetof(QXLDrawable, u.composite.mask_transform),
					   drawable_bo, trans_bo);
	    derefs[n_derefs++] = trans_bo;
	}
	else
	  composite->mask_transform = 0;
Søren Sandmann's avatar
Søren Sandmann committed
749 750 751 752 753 754 755
    }
    else
    {
	composite->mask = 0x00000000;
	composite->mask_transform = 0x00000000;
    }

756
    qxl->bo_funcs->bo_output_surf_reloc(qxl, offsetof(struct QXLDrawable, surfaces_dest[n_deps]), drawable_bo, dest);
Søren Sandmann's avatar
Søren Sandmann committed
757 758 759 760 761 762 763 764 765
    drawable->surfaces_rects[n_deps] = full_rect (dest);
    
    composite->src_origin.x = src_x;
    composite->src_origin.y = src_y;
    composite->mask_origin.x = mask_x;
    composite->mask_origin.y = mask_y;

    drawable->effect = QXL_EFFECT_BLEND;
    
766 767 768 769 770
    qxl->bo_funcs->bo_unmap(drawable_bo);
    push_drawable (qxl, drawable_bo);

    for (i = 0; i < n_derefs; i++)
      qxl->bo_funcs->bo_decref(qxl, derefs[i]);
Søren Sandmann's avatar
Søren Sandmann committed
771 772
}

773 774 775 776 777
Bool
qxl_surface_put_image (qxl_surface_t *dest,
		       int x, int y, int width, int height,
		       const char *src, int src_pitch)
{
778
    struct qxl_bo *drawable_bo;
779
    struct QXLDrawable *drawable;
780
    qxl_screen_t *qxl = dest->qxl;
781
    struct QXLRect rect;
782 783
    struct qxl_bo *image_bo;

784 785 786 787 788
    rect.left = x;
    rect.right = x + width;
    rect.top = y;
    rect.bottom = y + height;

789
    drawable_bo = make_drawable (qxl, dest, QXL_DRAW_COPY, &rect);
790

791
    drawable = qxl->bo_funcs->bo_map(drawable_bo);
792 793 794 795 796 797 798 799 800 801 802 803
    drawable->u.copy.src_area.top = 0;
    drawable->u.copy.src_area.bottom = height;
    drawable->u.copy.src_area.left = 0;
    drawable->u.copy.src_area.right = width;

    drawable->u.copy.rop_descriptor = ROPD_OP_PUT;
    drawable->u.copy.scale_mode = 0;
    drawable->u.copy.mask.flags = 0;
    drawable->u.copy.mask.pos.x = 0;
    drawable->u.copy.mask.pos.y = 0;
    drawable->u.copy.mask.bitmap = 0;

804
    image_bo = qxl_image_create (
805
	qxl, (const uint8_t *)src, 0, 0, width, height, src_pitch,
806
	dest->bpp == 24 ? 4 : dest->bpp / 8, FALSE);
807 808
    qxl->bo_funcs->bo_output_bo_reloc(qxl, offsetof(QXLDrawable, u.copy.src_bitmap),
				   drawable_bo, image_bo);
809

810 811 812 813
    qxl->bo_funcs->bo_unmap(drawable_bo);
    
    push_drawable (qxl, drawable_bo);
    qxl->bo_funcs->bo_decref(qxl, image_bo);    
814 815
    return TRUE;
}
Dave Airlie's avatar
Dave Airlie committed
816 817

void
818
qxl_get_formats (int bpp, SpiceSurfaceFmt *format, pixman_format_code_t *pformat)
Dave Airlie's avatar
Dave Airlie committed
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
{
    switch (bpp)
    {
    case 8:
	*format = SPICE_SURFACE_FMT_8_A;
	*pformat = PIXMAN_a8;
	break;

    case 16:
	*format = SPICE_SURFACE_FMT_16_565;
	*pformat = PIXMAN_r5g6b5;
	break;

    case 24:
	*format = SPICE_SURFACE_FMT_32_xRGB;
	*pformat = PIXMAN_a8r8g8b8;
	break;
	
    case 32:
	*format = SPICE_SURFACE_FMT_32_ARGB;
	*pformat = PIXMAN_a8r8g8b8;
	break;

    default:
843 844
	*format = -1;
	*pformat = -1;
Dave Airlie's avatar
Dave Airlie committed
845 846 847
	break;
    }
}