1 /* $NetBSD: qxl_draw.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $ */ 2 3 /* 4 * Copyright 2011 Red Hat, Inc. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * on the rights to use, copy, modify, merge, publish, distribute, sub 10 * license, and/or sell copies of the Software, and to permit persons to whom 11 * the Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25 #include <sys/cdefs.h> 26 __KERNEL_RCSID(0, "$NetBSD: qxl_draw.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $"); 27 28 #include <drm/drm_fourcc.h> 29 30 #include "qxl_drv.h" 31 #include "qxl_object.h" 32 33 static int alloc_clips(struct qxl_device *qdev, 34 struct qxl_release *release, 35 unsigned int num_clips, 36 struct qxl_bo **clips_bo) 37 { 38 int size = sizeof(struct qxl_clip_rects) + sizeof(struct qxl_rect) * num_clips; 39 40 return qxl_alloc_bo_reserved(qdev, release, size, clips_bo); 41 } 42 43 /* returns a pointer to the already allocated qxl_rect array inside 44 * the qxl_clip_rects. This is *not* the same as the memory allocated 45 * on the device, it is offset to qxl_clip_rects.chunk.data */ 46 static struct qxl_rect *drawable_set_clipping(struct qxl_device *qdev, 47 unsigned int num_clips, 48 struct qxl_bo *clips_bo) 49 { 50 struct qxl_clip_rects *dev_clips; 51 int ret; 52 53 ret = qxl_bo_kmap(clips_bo, (void **)&dev_clips); 54 if (ret) { 55 return NULL; 56 } 57 dev_clips->num_rects = num_clips; 58 dev_clips->chunk.next_chunk = 0; 59 dev_clips->chunk.prev_chunk = 0; 60 dev_clips->chunk.data_size = sizeof(struct qxl_rect) * num_clips; 61 return (struct qxl_rect *)dev_clips->chunk.data; 62 } 63 64 static int 65 alloc_drawable(struct qxl_device *qdev, struct qxl_release **release) 66 { 67 return qxl_alloc_release_reserved(qdev, sizeof(struct qxl_drawable), 68 QXL_RELEASE_DRAWABLE, release, NULL); 69 } 70 71 static void 72 free_drawable(struct qxl_device *qdev, struct qxl_release *release) 73 { 74 qxl_release_free(qdev, release); 75 } 76 77 /* release needs to be reserved at this point */ 78 static int 79 make_drawable(struct qxl_device *qdev, int surface, uint8_t type, 80 const struct qxl_rect *rect, 81 struct qxl_release *release) 82 { 83 struct qxl_drawable *drawable; 84 int i; 85 86 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release); 87 if (!drawable) 88 return -ENOMEM; 89 90 drawable->type = type; 91 92 drawable->surface_id = surface; /* Only primary for now */ 93 drawable->effect = QXL_EFFECT_OPAQUE; 94 drawable->self_bitmap = 0; 95 drawable->self_bitmap_area.top = 0; 96 drawable->self_bitmap_area.left = 0; 97 drawable->self_bitmap_area.bottom = 0; 98 drawable->self_bitmap_area.right = 0; 99 /* FIXME: add clipping */ 100 drawable->clip.type = SPICE_CLIP_TYPE_NONE; 101 102 /* 103 * surfaces_dest[i] should apparently be filled out with the 104 * surfaces that we depend on, and surface_rects should be 105 * filled with the rectangles of those surfaces that we 106 * are going to use. 107 */ 108 for (i = 0; i < 3; ++i) 109 drawable->surfaces_dest[i] = -1; 110 111 if (rect) 112 drawable->bbox = *rect; 113 114 drawable->mm_time = qdev->rom->mm_clock; 115 qxl_release_unmap(qdev, release, &drawable->release_info); 116 return 0; 117 } 118 119 /* push a draw command using the given clipping rectangles as 120 * the sources from the shadow framebuffer. 121 * 122 * Right now implementing with a single draw and a clip list. Clip 123 * lists are known to be a problem performance wise, this can be solved 124 * by treating them differently in the server. 125 */ 126 void qxl_draw_dirty_fb(struct qxl_device *qdev, 127 struct drm_framebuffer *fb, 128 struct qxl_bo *bo, 129 unsigned int flags, unsigned int color, 130 struct drm_clip_rect *clips, 131 unsigned int num_clips, int inc, 132 uint32_t dumb_shadow_offset) 133 { 134 /* 135 * TODO: if flags & DRM_MODE_FB_DIRTY_ANNOTATE_FILL then we should 136 * send a fill command instead, much cheaper. 137 * 138 * See include/drm/drm_mode.h 139 */ 140 struct drm_clip_rect *clips_ptr; 141 int i; 142 int left, right, top, bottom; 143 int width, height; 144 struct qxl_drawable *drawable; 145 struct qxl_rect drawable_rect; 146 struct qxl_rect *rects; 147 int stride = fb->pitches[0]; 148 /* depth is not actually interesting, we don't mask with it */ 149 int depth = fb->format->cpp[0] * 8; 150 uint8_t *surface_base; 151 struct qxl_release *release; 152 struct qxl_bo *clips_bo; 153 struct qxl_drm_image *dimage; 154 int ret; 155 156 ret = alloc_drawable(qdev, &release); 157 if (ret) 158 return; 159 160 clips->x1 += dumb_shadow_offset; 161 clips->x2 += dumb_shadow_offset; 162 163 left = clips->x1; 164 right = clips->x2; 165 top = clips->y1; 166 bottom = clips->y2; 167 168 /* skip the first clip rect */ 169 for (i = 1, clips_ptr = clips + inc; 170 i < num_clips; i++, clips_ptr += inc) { 171 left = min_t(int, left, (int)clips_ptr->x1); 172 right = max_t(int, right, (int)clips_ptr->x2); 173 top = min_t(int, top, (int)clips_ptr->y1); 174 bottom = max_t(int, bottom, (int)clips_ptr->y2); 175 } 176 177 width = right - left; 178 height = bottom - top; 179 180 ret = alloc_clips(qdev, release, num_clips, &clips_bo); 181 if (ret) 182 goto out_free_drawable; 183 184 ret = qxl_image_alloc_objects(qdev, release, 185 &dimage, 186 height, stride); 187 if (ret) 188 goto out_free_clips; 189 190 /* do a reservation run over all the objects we just allocated */ 191 ret = qxl_release_reserve_list(release, true); 192 if (ret) 193 goto out_free_image; 194 195 drawable_rect.left = left; 196 drawable_rect.right = right; 197 drawable_rect.top = top; 198 drawable_rect.bottom = bottom; 199 200 ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &drawable_rect, 201 release); 202 if (ret) 203 goto out_release_backoff; 204 205 ret = qxl_bo_kmap(bo, (void **)&surface_base); 206 if (ret) 207 goto out_release_backoff; 208 209 ret = qxl_image_init(qdev, release, dimage, surface_base, 210 left - dumb_shadow_offset, 211 top, width, height, depth, stride); 212 qxl_bo_kunmap(bo); 213 if (ret) 214 goto out_release_backoff; 215 216 rects = drawable_set_clipping(qdev, num_clips, clips_bo); 217 if (!rects) 218 goto out_release_backoff; 219 220 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release); 221 222 drawable->clip.type = SPICE_CLIP_TYPE_RECTS; 223 drawable->clip.data = qxl_bo_physical_address(qdev, 224 clips_bo, 0); 225 226 drawable->u.copy.src_area.top = 0; 227 drawable->u.copy.src_area.bottom = height; 228 drawable->u.copy.src_area.left = 0; 229 drawable->u.copy.src_area.right = width; 230 231 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT; 232 drawable->u.copy.scale_mode = 0; 233 drawable->u.copy.mask.flags = 0; 234 drawable->u.copy.mask.pos.x = 0; 235 drawable->u.copy.mask.pos.y = 0; 236 drawable->u.copy.mask.bitmap = 0; 237 238 drawable->u.copy.src_bitmap = qxl_bo_physical_address(qdev, dimage->bo, 0); 239 qxl_release_unmap(qdev, release, &drawable->release_info); 240 241 clips_ptr = clips; 242 for (i = 0; i < num_clips; i++, clips_ptr += inc) { 243 rects[i].left = clips_ptr->x1; 244 rects[i].right = clips_ptr->x2; 245 rects[i].top = clips_ptr->y1; 246 rects[i].bottom = clips_ptr->y2; 247 } 248 qxl_bo_kunmap(clips_bo); 249 250 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false); 251 qxl_release_fence_buffer_objects(release); 252 253 out_release_backoff: 254 if (ret) 255 qxl_release_backoff_reserve_list(release); 256 out_free_image: 257 qxl_image_free_objects(qdev, dimage); 258 out_free_clips: 259 qxl_bo_unref(&clips_bo); 260 out_free_drawable: 261 /* only free drawable on error */ 262 if (ret) 263 free_drawable(qdev, release); 264 265 } 266