2 * Copyright (C) 2015 Red Hat, Inc.
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:
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.
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.
27 #include <drm/drm_fb_helper.h>
28 #include "virtgpu_drv.h"
30 static int virtio_gpu_dirty_update(struct virtio_gpu_framebuffer *fb,
31 bool store, int x, int y,
32 int width, int height)
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];
40 struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(fb->base.obj[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",
47 fb->base.width, fb->base.height);
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.
56 * Can't test inside spin lock.
58 if (in_atomic() || store)
59 store_for_later = true;
64 spin_lock_irqsave(&fb->dirty_lock, flags);
75 if (store_for_later) {
80 spin_unlock_irqrestore(&fb->dirty_lock, flags);
84 fb->x1 = fb->y1 = INT_MAX;
87 spin_unlock_irqrestore(&fb->dirty_lock, flags);
91 uint32_t w = x2 - x + 1;
92 uint32_t h = y2 - y + 1;
94 offset = (y * fb->base.pitches[0]) + x * bpp;
96 virtio_gpu_cmd_transfer_to_host_2d(vgdev, obj,
105 virtio_gpu_cmd_resource_flush(vgdev, obj->hw_res_handle,
106 x, y, x2 - x + 1, y2 - y + 1);
110 int virtio_gpu_surface_dirty(struct virtio_gpu_framebuffer *vgfb,
111 struct drm_clip_rect *clips,
112 unsigned int num_clips)
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;
125 norect.x1 = norect.y1 = 0;
126 norect.x2 = vgfb->base.width;
127 norect.y2 = vgfb->base.height;
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);
144 return virtio_gpu_dirty_update(vgfb, false, left, top,
145 right - left, bottom - top);
147 virtio_gpu_cmd_resource_flush(vgdev, obj->hw_res_handle,
148 left, top, right - left, bottom - top);