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