at v5.1 150 lines 4.2 kB view raw
1/* 2 * Copyright (C) 2015 Red Hat, Inc. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial 15 * portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26#include <drm/drmP.h> 27#include <drm/drm_fb_helper.h> 28#include "virtgpu_drv.h" 29 30static int virtio_gpu_dirty_update(struct virtio_gpu_framebuffer *fb, 31 bool store, int x, int y, 32 int width, int height) 33{ 34 struct drm_device *dev = fb->base.dev; 35 struct virtio_gpu_device *vgdev = dev->dev_private; 36 bool store_for_later = false; 37 int bpp = fb->base.format->cpp[0]; 38 int x2, y2; 39 unsigned long flags; 40 struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(fb->base.obj[0]); 41 42 if ((width <= 0) || 43 (x + width > fb->base.width) || 44 (y + height > fb->base.height)) { 45 DRM_DEBUG("values out of range %dx%d+%d+%d, fb %dx%d\n", 46 width, height, x, y, 47 fb->base.width, fb->base.height); 48 return -EINVAL; 49 } 50 51 /* 52 * Can be called with pretty much any context (console output 53 * path). If we are in atomic just store the dirty rect info 54 * to send out the update later. 55 * 56 * Can't test inside spin lock. 57 */ 58 if (in_atomic() || store) 59 store_for_later = true; 60 61 x2 = x + width - 1; 62 y2 = y + height - 1; 63 64 spin_lock_irqsave(&fb->dirty_lock, flags); 65 66 if (fb->y1 < y) 67 y = fb->y1; 68 if (fb->y2 > y2) 69 y2 = fb->y2; 70 if (fb->x1 < x) 71 x = fb->x1; 72 if (fb->x2 > x2) 73 x2 = fb->x2; 74 75 if (store_for_later) { 76 fb->x1 = x; 77 fb->x2 = x2; 78 fb->y1 = y; 79 fb->y2 = y2; 80 spin_unlock_irqrestore(&fb->dirty_lock, flags); 81 return 0; 82 } 83 84 fb->x1 = fb->y1 = INT_MAX; 85 fb->x2 = fb->y2 = 0; 86 87 spin_unlock_irqrestore(&fb->dirty_lock, flags); 88 89 { 90 uint32_t offset; 91 uint32_t w = x2 - x + 1; 92 uint32_t h = y2 - y + 1; 93 94 offset = (y * fb->base.pitches[0]) + x * bpp; 95 96 virtio_gpu_cmd_transfer_to_host_2d(vgdev, obj, 97 offset, 98 cpu_to_le32(w), 99 cpu_to_le32(h), 100 cpu_to_le32(x), 101 cpu_to_le32(y), 102 NULL); 103 104 } 105 virtio_gpu_cmd_resource_flush(vgdev, obj->hw_res_handle, 106 x, y, x2 - x + 1, y2 - y + 1); 107 return 0; 108} 109 110int virtio_gpu_surface_dirty(struct virtio_gpu_framebuffer *vgfb, 111 struct drm_clip_rect *clips, 112 unsigned int num_clips) 113{ 114 struct virtio_gpu_device *vgdev = vgfb->base.dev->dev_private; 115 struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(vgfb->base.obj[0]); 116 struct drm_clip_rect norect; 117 struct drm_clip_rect *clips_ptr; 118 int left, right, top, bottom; 119 int i; 120 int inc = 1; 121 122 if (!num_clips) { 123 num_clips = 1; 124 clips = &norect; 125 norect.x1 = norect.y1 = 0; 126 norect.x2 = vgfb->base.width; 127 norect.y2 = vgfb->base.height; 128 } 129 left = clips->x1; 130 right = clips->x2; 131 top = clips->y1; 132 bottom = clips->y2; 133 134 /* skip the first clip rect */ 135 for (i = 1, clips_ptr = clips + inc; 136 i < num_clips; i++, clips_ptr += inc) { 137 left = min_t(int, left, (int)clips_ptr->x1); 138 right = max_t(int, right, (int)clips_ptr->x2); 139 top = min_t(int, top, (int)clips_ptr->y1); 140 bottom = max_t(int, bottom, (int)clips_ptr->y2); 141 } 142 143 if (obj->dumb) 144 return virtio_gpu_dirty_update(vgfb, false, left, top, 145 right - left, bottom - top); 146 147 virtio_gpu_cmd_resource_flush(vgdev, obj->hw_res_handle, 148 left, top, right - left, bottom - top); 149 return 0; 150}