OSDN Git Service

android: set LOCAL_MODULE_TAGS to optional
[android-x86/external-libdrm.git] / libkms / dumb.c
1 /**************************************************************************
2  *
3  * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
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
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27
28
29 #define HAVE_STDINT_H
30 #define _FILE_OFFSET_BITS 64
31
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "internal.h"
37
38 #include <sys/mman.h>
39 #include <sys/ioctl.h>
40 #include "xf86drm.h"
41
42 #include "i915_drm.h"
43
44 struct dumb_bo
45 {
46         struct kms_bo base;
47         unsigned map_count;
48 };
49
50 static int
51 dumb_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
52 {
53         switch (key) {
54         case KMS_BO_TYPE:
55                 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
56                 break;
57         default:
58                 return -EINVAL;
59         }
60         return 0;
61 }
62
63 static int
64 dumb_destroy(struct kms_driver *kms)
65 {
66         free(kms);
67         return 0;
68 }
69
70 static int
71 dumb_bo_create(struct kms_driver *kms,
72                  const unsigned width, const unsigned height,
73                  const enum kms_bo_type type, const unsigned *attr,
74                  struct kms_bo **out)
75 {
76         struct drm_mode_create_dumb arg;
77         struct dumb_bo *bo;
78         int i, ret;
79
80         for (i = 0; attr[i]; i += 2) {
81                 switch (attr[i]) {
82                 case KMS_WIDTH:
83                 case KMS_HEIGHT:
84                         break;
85                 case KMS_BO_TYPE:
86                         break;
87                 default:
88                         return -EINVAL;
89                 }
90         }
91
92         bo = calloc(1, sizeof(*bo));
93         if (!bo)
94                 return -ENOMEM;
95
96         memset(&arg, 0, sizeof(arg));
97
98         arg.bpp = 16;
99         arg.width = width;
100         arg.height = height;
101
102         ret = drmIoctl(kms->fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
103         if (ret)
104                 goto err_free;
105
106         bo->base.kms = kms;
107         bo->base.handle = arg.handle;
108         bo->base.size = arg.size;
109         bo->base.pitch = arg.pitch;
110
111         *out = &bo->base;
112
113         return 0;
114
115 err_free:
116         free(bo);
117         return ret;
118 }
119
120 static int
121 dumb_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
122 {
123         switch (key) {
124         default:
125                 return -EINVAL;
126         }
127 }
128
129 static int
130 dumb_bo_map(struct kms_bo *_bo, void **out)
131 {
132         struct dumb_bo *bo = (struct dumb_bo *)_bo;
133         struct drm_mode_map_dumb arg;
134         void *map = NULL;
135         int ret;
136
137         if (bo->base.ptr) {
138                 bo->map_count++;
139                 *out = bo->base.ptr;
140                 return 0;
141         }
142
143         memset(&arg, 0, sizeof(arg));
144         arg.handle = bo->base.handle;
145
146         ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
147         if (ret)
148                 return ret;
149
150         map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
151         if (map == MAP_FAILED)
152                 return -errno;
153
154         bo->base.ptr = map;
155         bo->map_count++;
156         *out = bo->base.ptr;
157
158         return 0;
159 }
160
161 static int
162 dumb_bo_unmap(struct kms_bo *_bo)
163 {
164         struct dumb_bo *bo = (struct dumb_bo *)_bo;
165         bo->map_count--;
166         return 0;
167 }
168
169 static int
170 dumb_bo_destroy(struct kms_bo *_bo)
171 {
172         struct dumb_bo *bo = (struct dumb_bo *)_bo;
173         struct drm_mode_destroy_dumb arg;
174         int ret;
175
176         if (bo->base.ptr) {
177                 /* XXX Sanity check map_count */
178                 munmap(bo->base.ptr, bo->base.size);
179                 bo->base.ptr = NULL;
180         }
181
182         memset(&arg, 0, sizeof(arg));
183         arg.handle = bo->base.handle;
184
185         ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
186         if (ret)
187                 return -errno;
188
189         free(bo);
190         return 0;
191 }
192
193 int
194 dumb_create(int fd, struct kms_driver **out)
195 {
196         struct kms_driver *kms;
197         int ret;
198         uint64_t cap = 0;
199
200         ret = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &cap);
201         if (ret || cap == 0)
202                 return -EINVAL;
203
204         kms = calloc(1, sizeof(*kms));
205         if (!kms)
206                 return -ENOMEM;
207
208         kms->fd = fd;
209
210         kms->bo_create = dumb_bo_create;
211         kms->bo_map = dumb_bo_map;
212         kms->bo_unmap = dumb_bo_unmap;
213         kms->bo_get_prop = dumb_bo_get_prop;
214         kms->bo_destroy = dumb_bo_destroy;
215         kms->get_prop = dumb_get_prop;
216         kms->destroy = dumb_destroy;
217         *out = kms;
218
219         return 0;
220 }