DRM update to git snapshot from 2008-01-04.
[dragonfly.git] / sys / dev / drm / drm_drawable.c
1 /*-
2  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4  * All Rights Reserved.
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  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
20  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Rickard E. (Rik) Faith <faith@valinux.com>
27  *    Gareth Hughes <gareth@valinux.com>
28  *
29  * $DragonFly: src/sys/dev/drm/drm_drawable.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
30  */
31
32 /** @file drm_drawable.c
33  * This file implements ioctls to store information along with DRM drawables,
34  * such as the current set of cliprects for vblank-synced buffer swaps.
35  */
36
37 #include "drmP.h"
38
39 struct bsd_drm_drawable_info {
40         struct drm_drawable_info info;
41         int handle;
42         RB_ENTRY(bsd_drm_drawable_info) tree;
43 };
44
45 static int
46 drm_drawable_compare(struct bsd_drm_drawable_info *a,
47     struct bsd_drm_drawable_info *b)
48 {
49         if (a->handle > b->handle)
50                 return 1;
51         if (a->handle < b->handle)
52                 return -1;
53         return 0;
54 }
55 RB_PROTOTYPE_STATIC(drawable_tree, bsd_drm_drawable_info, tree,
56     drm_drawable_compare);
57 RB_GENERATE_STATIC(drawable_tree, bsd_drm_drawable_info, tree,
58     drm_drawable_compare);
59
60 struct drm_drawable_info *
61 drm_get_drawable_info(drm_device_t *dev, int handle)
62 {
63         struct bsd_drm_drawable_info find, *result;
64
65         find.handle = handle;
66         result = RB_FIND(drawable_tree, &dev->drw_head, &find);
67
68         return &result->info;
69 }
70
71 int drm_adddraw(drm_device_t *dev, void *data, struct drm_file *file_priv)
72 {
73         drm_draw_t *draw = data;
74         struct bsd_drm_drawable_info *info;
75
76         info = drm_calloc(1, sizeof(struct bsd_drm_drawable_info),
77             DRM_MEM_DRAWABLE);
78         if (info == NULL)
79                 return ENOMEM;
80
81 #ifdef __FreeBSD__
82         info->handle = alloc_unr(dev->drw_unrhdr);
83 #else
84         /*
85          * XXX Only valid for sizeof(int) == sizeof(void *)
86          */
87         info->handle = (int)info;
88 #endif
89         DRM_SPINLOCK(&dev->drw_lock);
90         RB_INSERT(drawable_tree, &dev->drw_head, info);
91         draw->handle = info->handle;
92         DRM_SPINUNLOCK(&dev->drw_lock);
93
94         DRM_DEBUG("%d\n", draw->handle);
95
96         return 0;
97 }
98
99 int drm_rmdraw(drm_device_t *dev, void *data, struct drm_file *file_priv)
100 {
101         drm_draw_t *draw = (drm_draw_t *)data;
102         struct drm_drawable_info *info;
103
104         DRM_SPINLOCK(&dev->drw_lock);
105         info = drm_get_drawable_info(dev, draw->handle);
106         if (info != NULL) {
107                 RB_REMOVE(drawable_tree, &dev->drw_head,
108                     (struct bsd_drm_drawable_info *)info);
109                 DRM_SPINUNLOCK(&dev->drw_lock);
110 #ifdef __FreeBSD__
111                 free_unr(dev->drw_unrhdr, draw->handle);
112 #endif
113                 drm_free(info, sizeof(struct bsd_drm_drawable_info),
114                     DRM_MEM_DRAWABLE);
115                 return 0;
116         } else {
117                 DRM_SPINUNLOCK(&dev->drw_lock);
118                 return EINVAL;
119         }
120 }
121
122 int drm_update_draw(drm_device_t *dev, void *data, struct drm_file *file_priv)
123 {
124         struct drm_drawable_info *info;
125         struct drm_update_draw *update = (struct drm_update_draw *)data;
126         int ret;
127
128         info = drm_get_drawable_info(dev, update->handle);
129         if (info == NULL)
130                 return EINVAL;
131
132         switch (update->type) {
133         case DRM_DRAWABLE_CLIPRECTS:
134                 DRM_SPINLOCK(&dev->drw_lock);
135                 if (update->num != info->num_rects) {
136                         drm_free(info->rects,
137                             sizeof(*info->rects) * info->num_rects,
138                             DRM_MEM_DRAWABLE);
139                         info->rects = NULL;
140                         info->num_rects = 0;
141                 }
142                 if (update->num == 0) {
143                         DRM_SPINUNLOCK(&dev->drw_lock);
144                         return 0;
145                 }
146                 if (info->rects == NULL) {
147                         info->rects = drm_alloc(sizeof(*info->rects) *
148                             update->num, DRM_MEM_DRAWABLE);
149                         if (info->rects == NULL)
150                                 return ENOMEM;
151                         info->num_rects = update->num;
152                 }
153                 /* For some reason the pointer arg is unsigned long long. */
154                 ret = copyin((void *)(intptr_t)update->data, info->rects,
155                     sizeof(*info->rects) * info->num_rects);
156                 DRM_SPINUNLOCK(&dev->drw_lock);
157                 return ret;
158         default:
159                 return EINVAL;
160         }
161 }