OSDN Git Service

link libdrm_intel properly
[android-x86/external-libdrm.git] / bsd-core / 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 "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_GENERATE_STATIC(drawable_tree, bsd_drm_drawable_info, tree,
56     drm_drawable_compare);
57
58 struct drm_drawable_info *
59 drm_get_drawable_info(struct drm_device *dev, int handle)
60 {
61         struct bsd_drm_drawable_info find, *result;
62
63         find.handle = handle;
64         result = RB_FIND(drawable_tree, &dev->drw_head, &find);
65
66         return &result->info;
67 }
68
69 int drm_adddraw(struct drm_device *dev, void *data, struct drm_file *file_priv)
70 {
71         struct drm_draw *draw = data;
72         struct bsd_drm_drawable_info *info;
73
74         info = drm_calloc(1, sizeof(struct bsd_drm_drawable_info),
75             DRM_MEM_DRAWABLE);
76         if (info == NULL)
77                 return ENOMEM;
78
79         info->handle = alloc_unr(dev->drw_unrhdr);
80         DRM_SPINLOCK(&dev->drw_lock);
81         RB_INSERT(drawable_tree, &dev->drw_head, info);
82         draw->handle = info->handle;
83         DRM_SPINUNLOCK(&dev->drw_lock);
84
85         DRM_DEBUG("%d\n", draw->handle);
86
87         return 0;
88 }
89
90 int drm_rmdraw(struct drm_device *dev, void *data, struct drm_file *file_priv)
91 {
92         struct drm_draw *draw = (struct drm_draw *)data;
93         struct drm_drawable_info *info;
94
95         DRM_SPINLOCK(&dev->drw_lock);
96         info = drm_get_drawable_info(dev, draw->handle);
97         if (info != NULL) {
98                 RB_REMOVE(drawable_tree, &dev->drw_head,
99                     (struct bsd_drm_drawable_info *)info);
100                 DRM_SPINUNLOCK(&dev->drw_lock);
101                 free_unr(dev->drw_unrhdr, draw->handle);
102                 drm_free(info, sizeof(struct bsd_drm_drawable_info),
103                     DRM_MEM_DRAWABLE);
104                 return 0;
105         } else {
106                 DRM_SPINUNLOCK(&dev->drw_lock);
107                 return EINVAL;
108         }
109 }
110
111 int drm_update_draw(struct drm_device *dev, void *data,
112                     struct drm_file *file_priv)
113 {
114         struct drm_drawable_info *info;
115         struct drm_update_draw *update = (struct drm_update_draw *)data;
116         int ret;
117
118         info = drm_get_drawable_info(dev, update->handle);
119         if (info == NULL)
120                 return EINVAL;
121
122         switch (update->type) {
123         case DRM_DRAWABLE_CLIPRECTS:
124                 DRM_SPINLOCK(&dev->drw_lock);
125                 if (update->num != info->num_rects) {
126                         drm_free(info->rects,
127                             sizeof(*info->rects) * info->num_rects,
128                             DRM_MEM_DRAWABLE);
129                         info->rects = NULL;
130                         info->num_rects = 0;
131                 }
132                 if (update->num == 0) {
133                         DRM_SPINUNLOCK(&dev->drw_lock);
134                         return 0;
135                 }
136                 if (info->rects == NULL) {
137                         info->rects = drm_alloc(sizeof(*info->rects) *
138                             update->num, DRM_MEM_DRAWABLE);
139                         if (info->rects == NULL) {
140                                 DRM_SPINUNLOCK(&dev->drw_lock);
141                                 return ENOMEM;
142                         }
143                         info->num_rects = update->num;
144                 }
145                 /* For some reason the pointer arg is unsigned long long. */
146                 ret = copyin((void *)(intptr_t)update->data, info->rects,
147                     sizeof(*info->rects) * info->num_rects);
148                 DRM_SPINUNLOCK(&dev->drw_lock);
149                 return ret;
150         default:
151                 return EINVAL;
152         }
153 }
154
155 void drm_drawable_free_all(struct drm_device *dev)
156 {
157         struct bsd_drm_drawable_info *info, *next;
158
159         DRM_SPINLOCK(&dev->drw_lock);
160         for (info = RB_MIN(drawable_tree, &dev->drw_head);
161             info != NULL ; info = next) {
162                 next = RB_NEXT(drawable_tree, &dev->drw_head, info);
163                 RB_REMOVE(drawable_tree, &dev->drw_head,
164                     (struct bsd_drm_drawable_info *)info);
165                 DRM_SPINUNLOCK(&dev->drw_lock);
166                 free_unr(dev->drw_unrhdr, info->handle);
167                 drm_free(info, sizeof(struct bsd_drm_drawable_info),
168                     DRM_MEM_DRAWABLE);
169                 DRM_SPINLOCK(&dev->drw_lock);
170         }
171         DRM_SPINUNLOCK(&dev->drw_lock);
172 }