Sync DRM code with FreeBSD trunk rev 190433.
[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  */
30
31 /** @file drm_drawable.c
32  * This file implements ioctls to store information along with DRM drawables,
33  * such as the current set of cliprects for vblank-synced buffer swaps.
34  */
35
36 #include "dev/drm/drmP.h"
37
38 struct bsd_drm_drawable_info {
39         struct drm_drawable_info info;
40         int handle;
41         RB_ENTRY(bsd_drm_drawable_info) tree;
42 };
43
44 static int
45 drm_drawable_compare(struct bsd_drm_drawable_info *a,
46     struct bsd_drm_drawable_info *b)
47 {
48         if (a->handle > b->handle)
49                 return 1;
50         if (a->handle < b->handle)
51                 return -1;
52         return 0;
53 }
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(struct drm_device *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(struct drm_device *dev, void *data, struct drm_file *file_priv)
72 {
73         struct drm_draw *draw = data;
74         struct bsd_drm_drawable_info *info;
75
76         info = malloc(sizeof(struct bsd_drm_drawable_info), DRM_MEM_DRAWABLE,
77             M_NOWAIT | M_ZERO);
78         if (info == NULL)
79                 return ENOMEM;
80
81         /*
82          * XXX Only valid for sizeof(int) == sizeof(void *)
83          */
84         info->handle = (int)info;
85
86         DRM_SPINLOCK(&dev->drw_lock);
87         RB_INSERT(drawable_tree, &dev->drw_head, info);
88         draw->handle = info->handle;
89         DRM_SPINUNLOCK(&dev->drw_lock);
90
91         DRM_DEBUG("%d\n", draw->handle);
92
93         return 0;
94 }
95
96 int drm_rmdraw(struct drm_device *dev, void *data, struct drm_file *file_priv)
97 {
98         struct drm_draw *draw = (struct drm_draw *)data;
99         struct drm_drawable_info *info;
100
101         DRM_SPINLOCK(&dev->drw_lock);
102         info = drm_get_drawable_info(dev, draw->handle);
103         if (info != NULL) {
104                 RB_REMOVE(drawable_tree, &dev->drw_head,
105                     (struct bsd_drm_drawable_info *)info);
106                 DRM_SPINUNLOCK(&dev->drw_lock);
107                 free(info->rects, DRM_MEM_DRAWABLE);
108                 free(info, DRM_MEM_DRAWABLE);
109                 return 0;
110         } else {
111                 DRM_SPINUNLOCK(&dev->drw_lock);
112                 return EINVAL;
113         }
114 }
115
116 int drm_update_draw(struct drm_device *dev, void *data,
117                     struct drm_file *file_priv)
118 {
119         struct drm_drawable_info *info;
120         struct drm_update_draw *update = (struct drm_update_draw *)data;
121         int ret;
122
123         info = drm_get_drawable_info(dev, update->handle);
124         if (info == NULL)
125                 return EINVAL;
126
127         switch (update->type) {
128         case DRM_DRAWABLE_CLIPRECTS:
129                 DRM_SPINLOCK(&dev->drw_lock);
130                 if (update->num != info->num_rects) {
131                         free(info->rects, DRM_MEM_DRAWABLE);
132                         info->rects = NULL;
133                         info->num_rects = 0;
134                 }
135                 if (update->num == 0) {
136                         DRM_SPINUNLOCK(&dev->drw_lock);
137                         return 0;
138                 }
139                 if (info->rects == NULL) {
140                         info->rects = malloc(sizeof(*info->rects) *
141                             update->num, DRM_MEM_DRAWABLE, M_NOWAIT);
142                         if (info->rects == NULL) {
143                                 DRM_SPINUNLOCK(&dev->drw_lock);
144                                 return ENOMEM;
145                         }
146                         info->num_rects = update->num;
147                 }
148                 /* For some reason the pointer arg is unsigned long long. */
149                 ret = copyin((void *)(intptr_t)update->data, info->rects,
150                     sizeof(*info->rects) * info->num_rects);
151                 DRM_SPINUNLOCK(&dev->drw_lock);
152                 return ret;
153         default:
154                 return EINVAL;
155         }
156 }
157
158 void drm_drawable_free_all(struct drm_device *dev)
159 {
160         struct bsd_drm_drawable_info *info, *next;
161
162         DRM_SPINLOCK(&dev->drw_lock);
163         for (info = RB_MIN(drawable_tree, &dev->drw_head);
164             info != NULL ; info = next) {
165                 next = RB_NEXT(drawable_tree, &dev->drw_head, info);
166                 RB_REMOVE(drawable_tree, &dev->drw_head,
167                     (struct bsd_drm_drawable_info *)info);
168                 DRM_SPINUNLOCK(&dev->drw_lock);
169                 free(info->info.rects, DRM_MEM_DRAWABLE);
170                 free(info, DRM_MEM_DRAWABLE);
171                 DRM_SPINLOCK(&dev->drw_lock);
172         }
173         DRM_SPINUNLOCK(&dev->drw_lock);
174 }