1 /* 2 * Copyright 2011 Red Hat, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * on the rights to use, copy, modify, merge, publish, distribute, sub 8 * license, and/or sell copies of the Software, and to permit persons to whom 9 * the Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 23 #include "qxl_drv.h" 24 #include "qxl_object.h" 25 26 static int alloc_clips(struct qxl_device *qdev, 27 struct qxl_release *release, 28 unsigned num_clips, 29 struct qxl_bo **clips_bo) 30 { 31 int size = sizeof(struct qxl_clip_rects) + sizeof(struct qxl_rect) * num_clips; 32 33 return qxl_alloc_bo_reserved(qdev, release, size, clips_bo); 34 } 35 36 /* returns a pointer to the already allocated qxl_rect array inside 37 * the qxl_clip_rects. This is *not* the same as the memory allocated 38 * on the device, it is offset to qxl_clip_rects.chunk.data */ 39 static struct qxl_rect *drawable_set_clipping(struct qxl_device *qdev, 40 unsigned num_clips, 41 struct qxl_bo *clips_bo) 42 { 43 struct qxl_clip_rects *dev_clips; 44 int ret; 45 46 ret = qxl_bo_kmap(clips_bo, (void **)&dev_clips); 47 if (ret) { 48 return NULL; 49 } 50 dev_clips->num_rects = num_clips; 51 dev_clips->chunk.next_chunk = 0; 52 dev_clips->chunk.prev_chunk = 0; 53 dev_clips->chunk.data_size = sizeof(struct qxl_rect) * num_clips; 54 return (struct qxl_rect *)dev_clips->chunk.data; 55 } 56 57 static int 58 alloc_drawable(struct qxl_device *qdev, struct qxl_release **release) 59 { 60 int ret; 61 ret = qxl_alloc_release_reserved(qdev, sizeof(struct qxl_drawable), 62 QXL_RELEASE_DRAWABLE, release, 63 NULL); 64 return ret; 65 } 66 67 static void 68 free_drawable(struct qxl_device *qdev, struct qxl_release *release) 69 { 70 qxl_release_free(qdev, release); 71 } 72 73 /* release needs to be reserved at this point */ 74 static int 75 make_drawable(struct qxl_device *qdev, int surface, uint8_t type, 76 const struct qxl_rect *rect, 77 struct qxl_release *release) 78 { 79 struct qxl_drawable *drawable; 80 int i; 81 82 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release); 83 if (!drawable) 84 return -ENOMEM; 85 86 drawable->type = type; 87 88 drawable->surface_id = surface; /* Only primary for now */ 89 drawable->effect = QXL_EFFECT_OPAQUE; 90 drawable->self_bitmap = 0; 91 drawable->self_bitmap_area.top = 0; 92 drawable->self_bitmap_area.left = 0; 93 drawable->self_bitmap_area.bottom = 0; 94 drawable->self_bitmap_area.right = 0; 95 /* FIXME: add clipping */ 96 drawable->clip.type = SPICE_CLIP_TYPE_NONE; 97 98 /* 99 * surfaces_dest[i] should apparently be filled out with the 100 * surfaces that we depend on, and surface_rects should be 101 * filled with the rectangles of those surfaces that we 102 * are going to use. 103 */ 104 for (i = 0; i < 3; ++i) 105 drawable->surfaces_dest[i] = -1; 106 107 if (rect) 108 drawable->bbox = *rect; 109 110 drawable->mm_time = qdev->rom->mm_clock; 111 qxl_release_unmap(qdev, release, &drawable->release_info); 112 return 0; 113 } 114 115 static int alloc_palette_object(struct qxl_device *qdev, 116 struct qxl_release *release, 117 struct qxl_bo **palette_bo) 118 { 119 return qxl_alloc_bo_reserved(qdev, release, 120 sizeof(struct qxl_palette) + sizeof(uint32_t) * 2, 121 palette_bo); 122 } 123 124 static int qxl_palette_create_1bit(struct qxl_bo *palette_bo, 125 struct qxl_release *release, 126 const struct qxl_fb_image *qxl_fb_image) 127 { 128 const struct fb_image *fb_image = &qxl_fb_image->fb_image; 129 uint32_t visual = qxl_fb_image->visual; 130 const uint32_t *pseudo_palette = qxl_fb_image->pseudo_palette; 131 struct qxl_palette *pal; 132 int ret; 133 uint32_t fgcolor, bgcolor; 134 static uint64_t unique; /* we make no attempt to actually set this 135 * correctly globaly, since that would require 136 * tracking all of our palettes. */ 137 ret = qxl_bo_kmap(palette_bo, (void **)&pal); 138 if (ret) 139 return ret; 140 pal->num_ents = 2; 141 pal->unique = unique++; 142 if (visual == FB_VISUAL_TRUECOLOR || visual == FB_VISUAL_DIRECTCOLOR) { 143 /* NB: this is the only used branch currently. */ 144 fgcolor = pseudo_palette[fb_image->fg_color]; 145 bgcolor = pseudo_palette[fb_image->bg_color]; 146 } else { 147 fgcolor = fb_image->fg_color; 148 bgcolor = fb_image->bg_color; 149 } 150 pal->ents[0] = bgcolor; 151 pal->ents[1] = fgcolor; 152 qxl_bo_kunmap(palette_bo); 153 return 0; 154 } 155 156 void qxl_draw_opaque_fb(const struct qxl_fb_image *qxl_fb_image, 157 int stride /* filled in if 0 */) 158 { 159 struct qxl_device *qdev = qxl_fb_image->qdev; 160 struct qxl_drawable *drawable; 161 struct qxl_rect rect; 162 const struct fb_image *fb_image = &qxl_fb_image->fb_image; 163 int x = fb_image->dx; 164 int y = fb_image->dy; 165 int width = fb_image->width; 166 int height = fb_image->height; 167 const char *src = fb_image->data; 168 int depth = fb_image->depth; 169 struct qxl_release *release; 170 struct qxl_image *image; 171 int ret; 172 struct qxl_drm_image *dimage; 173 struct qxl_bo *palette_bo = NULL; 174 if (stride == 0) 175 stride = depth * width / 8; 176 177 ret = alloc_drawable(qdev, &release); 178 if (ret) 179 return; 180 181 ret = qxl_image_alloc_objects(qdev, release, 182 &dimage, 183 height, stride); 184 if (ret) 185 goto out_free_drawable; 186 187 if (depth == 1) { 188 ret = alloc_palette_object(qdev, release, &palette_bo); 189 if (ret) 190 goto out_free_image; 191 } 192 193 /* do a reservation run over all the objects we just allocated */ 194 ret = qxl_release_reserve_list(release, true); 195 if (ret) 196 goto out_free_palette; 197 198 rect.left = x; 199 rect.right = x + width; 200 rect.top = y; 201 rect.bottom = y + height; 202 203 ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &rect, release); 204 if (ret) { 205 qxl_release_backoff_reserve_list(release); 206 goto out_free_palette; 207 } 208 209 ret = qxl_image_init(qdev, release, dimage, 210 (const uint8_t *)src, 0, 0, 211 width, height, depth, stride); 212 if (ret) { 213 qxl_release_backoff_reserve_list(release); 214 qxl_release_free(qdev, release); 215 return; 216 } 217 218 if (depth == 1) { 219 void *ptr; 220 ret = qxl_palette_create_1bit(palette_bo, release, qxl_fb_image); 221 222 ptr = qxl_bo_kmap_atomic_page(qdev, dimage->bo, 0); 223 image = ptr; 224 image->u.bitmap.palette = 225 qxl_bo_physical_address(qdev, palette_bo, 0); 226 qxl_bo_kunmap_atomic_page(qdev, dimage->bo, ptr); 227 } 228 229 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release); 230 231 drawable->u.copy.src_area.top = 0; 232 drawable->u.copy.src_area.bottom = height; 233 drawable->u.copy.src_area.left = 0; 234 drawable->u.copy.src_area.right = width; 235 236 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT; 237 drawable->u.copy.scale_mode = 0; 238 drawable->u.copy.mask.flags = 0; 239 drawable->u.copy.mask.pos.x = 0; 240 drawable->u.copy.mask.pos.y = 0; 241 drawable->u.copy.mask.bitmap = 0; 242 243 drawable->u.copy.src_bitmap = 244 qxl_bo_physical_address(qdev, dimage->bo, 0); 245 qxl_release_unmap(qdev, release, &drawable->release_info); 246 247 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false); 248 qxl_release_fence_buffer_objects(release); 249 250 out_free_palette: 251 if (palette_bo) 252 qxl_bo_unref(&palette_bo); 253 out_free_image: 254 qxl_image_free_objects(qdev, dimage); 255 out_free_drawable: 256 if (ret) 257 free_drawable(qdev, release); 258 } 259 260 /* push a draw command using the given clipping rectangles as 261 * the sources from the shadow framebuffer. 262 * 263 * Right now implementing with a single draw and a clip list. Clip 264 * lists are known to be a problem performance wise, this can be solved 265 * by treating them differently in the server. 266 */ 267 void qxl_draw_dirty_fb(struct qxl_device *qdev, 268 struct qxl_framebuffer *qxl_fb, 269 struct qxl_bo *bo, 270 unsigned flags, unsigned color, 271 struct drm_clip_rect *clips, 272 unsigned num_clips, int inc) 273 { 274 /* 275 * TODO: if flags & DRM_MODE_FB_DIRTY_ANNOTATE_FILL then we should 276 * send a fill command instead, much cheaper. 277 * 278 * See include/drm/drm_mode.h 279 */ 280 struct drm_clip_rect *clips_ptr; 281 int i; 282 int left, right, top, bottom; 283 int width, height; 284 struct qxl_drawable *drawable; 285 struct qxl_rect drawable_rect; 286 struct qxl_rect *rects; 287 int stride = qxl_fb->base.pitches[0]; 288 /* depth is not actually interesting, we don't mask with it */ 289 int depth = qxl_fb->base.bits_per_pixel; 290 uint8_t *surface_base; 291 struct qxl_release *release; 292 struct qxl_bo *clips_bo; 293 struct qxl_drm_image *dimage; 294 int ret; 295 296 ret = alloc_drawable(qdev, &release); 297 if (ret) 298 return; 299 300 left = clips->x1; 301 right = clips->x2; 302 top = clips->y1; 303 bottom = clips->y2; 304 305 /* skip the first clip rect */ 306 for (i = 1, clips_ptr = clips + inc; 307 i < num_clips; i++, clips_ptr += inc) { 308 left = min_t(int, left, (int)clips_ptr->x1); 309 right = max_t(int, right, (int)clips_ptr->x2); 310 top = min_t(int, top, (int)clips_ptr->y1); 311 bottom = max_t(int, bottom, (int)clips_ptr->y2); 312 } 313 314 width = right - left; 315 height = bottom - top; 316 317 ret = alloc_clips(qdev, release, num_clips, &clips_bo); 318 if (ret) 319 goto out_free_drawable; 320 321 ret = qxl_image_alloc_objects(qdev, release, 322 &dimage, 323 height, stride); 324 if (ret) 325 goto out_free_clips; 326 327 /* do a reservation run over all the objects we just allocated */ 328 ret = qxl_release_reserve_list(release, true); 329 if (ret) 330 goto out_free_image; 331 332 drawable_rect.left = left; 333 drawable_rect.right = right; 334 drawable_rect.top = top; 335 drawable_rect.bottom = bottom; 336 337 ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &drawable_rect, 338 release); 339 if (ret) 340 goto out_release_backoff; 341 342 ret = qxl_bo_kmap(bo, (void **)&surface_base); 343 if (ret) 344 goto out_release_backoff; 345 346 347 ret = qxl_image_init(qdev, release, dimage, surface_base, 348 left, top, width, height, depth, stride); 349 qxl_bo_kunmap(bo); 350 if (ret) 351 goto out_release_backoff; 352 353 rects = drawable_set_clipping(qdev, num_clips, clips_bo); 354 if (!rects) 355 goto out_release_backoff; 356 357 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release); 358 359 drawable->clip.type = SPICE_CLIP_TYPE_RECTS; 360 drawable->clip.data = qxl_bo_physical_address(qdev, 361 clips_bo, 0); 362 363 drawable->u.copy.src_area.top = 0; 364 drawable->u.copy.src_area.bottom = height; 365 drawable->u.copy.src_area.left = 0; 366 drawable->u.copy.src_area.right = width; 367 368 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT; 369 drawable->u.copy.scale_mode = 0; 370 drawable->u.copy.mask.flags = 0; 371 drawable->u.copy.mask.pos.x = 0; 372 drawable->u.copy.mask.pos.y = 0; 373 drawable->u.copy.mask.bitmap = 0; 374 375 drawable->u.copy.src_bitmap = qxl_bo_physical_address(qdev, dimage->bo, 0); 376 qxl_release_unmap(qdev, release, &drawable->release_info); 377 378 clips_ptr = clips; 379 for (i = 0; i < num_clips; i++, clips_ptr += inc) { 380 rects[i].left = clips_ptr->x1; 381 rects[i].right = clips_ptr->x2; 382 rects[i].top = clips_ptr->y1; 383 rects[i].bottom = clips_ptr->y2; 384 } 385 qxl_bo_kunmap(clips_bo); 386 387 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false); 388 qxl_release_fence_buffer_objects(release); 389 390 out_release_backoff: 391 if (ret) 392 qxl_release_backoff_reserve_list(release); 393 out_free_image: 394 qxl_image_free_objects(qdev, dimage); 395 out_free_clips: 396 qxl_bo_unref(&clips_bo); 397 out_free_drawable: 398 /* only free drawable on error */ 399 if (ret) 400 free_drawable(qdev, release); 401 402 } 403 404 void qxl_draw_copyarea(struct qxl_device *qdev, 405 u32 width, u32 height, 406 u32 sx, u32 sy, 407 u32 dx, u32 dy) 408 { 409 struct qxl_drawable *drawable; 410 struct qxl_rect rect; 411 struct qxl_release *release; 412 int ret; 413 414 ret = alloc_drawable(qdev, &release); 415 if (ret) 416 return; 417 418 /* do a reservation run over all the objects we just allocated */ 419 ret = qxl_release_reserve_list(release, true); 420 if (ret) 421 goto out_free_release; 422 423 rect.left = dx; 424 rect.top = dy; 425 rect.right = dx + width; 426 rect.bottom = dy + height; 427 ret = make_drawable(qdev, 0, QXL_COPY_BITS, &rect, release); 428 if (ret) { 429 qxl_release_backoff_reserve_list(release); 430 goto out_free_release; 431 } 432 433 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release); 434 drawable->u.copy_bits.src_pos.x = sx; 435 drawable->u.copy_bits.src_pos.y = sy; 436 qxl_release_unmap(qdev, release, &drawable->release_info); 437 438 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false); 439 qxl_release_fence_buffer_objects(release); 440 441 out_free_release: 442 if (ret) 443 free_drawable(qdev, release); 444 } 445 446 void qxl_draw_fill(struct qxl_draw_fill *qxl_draw_fill_rec) 447 { 448 struct qxl_device *qdev = qxl_draw_fill_rec->qdev; 449 struct qxl_rect rect = qxl_draw_fill_rec->rect; 450 uint32_t color = qxl_draw_fill_rec->color; 451 uint16_t rop = qxl_draw_fill_rec->rop; 452 struct qxl_drawable *drawable; 453 struct qxl_release *release; 454 int ret; 455 456 ret = alloc_drawable(qdev, &release); 457 if (ret) 458 return; 459 460 /* do a reservation run over all the objects we just allocated */ 461 ret = qxl_release_reserve_list(release, true); 462 if (ret) 463 goto out_free_release; 464 465 ret = make_drawable(qdev, 0, QXL_DRAW_FILL, &rect, release); 466 if (ret) { 467 qxl_release_backoff_reserve_list(release); 468 goto out_free_release; 469 } 470 471 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release); 472 drawable->u.fill.brush.type = SPICE_BRUSH_TYPE_SOLID; 473 drawable->u.fill.brush.u.color = color; 474 drawable->u.fill.rop_descriptor = rop; 475 drawable->u.fill.mask.flags = 0; 476 drawable->u.fill.mask.pos.x = 0; 477 drawable->u.fill.mask.pos.y = 0; 478 drawable->u.fill.mask.bitmap = 0; 479 480 qxl_release_unmap(qdev, release, &drawable->release_info); 481 482 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false); 483 qxl_release_fence_buffer_objects(release); 484 485 out_free_release: 486 if (ret) 487 free_drawable(qdev, release); 488 } 489