Merge branch 'master' into libkms-master
[platform/upstream/libdrm.git] / libkms / api.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 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "internal.h"
33
34 int kms_create(int fd, struct kms_driver **out)
35 {
36         return vmwgfx_create(fd, out);
37 }
38
39 int kms_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
40 {
41         switch (key) {
42         case KMS_MAX_SCANOUT_WIDTH:
43         case KMS_MAX_SCANOUT_HEIGHT:
44         case KMS_MIN_SCANOUT_WIDTH:
45         case KMS_MIN_SCANOUT_HEIGHT:
46         case KMS_MAX_CURSOR_WIDTH:
47         case KMS_MAX_CURSOR_HEIGHT:
48         case KMS_MIN_CURSOR_WIDTH:
49         case KMS_MIN_CURSOR_HEIGHT:
50                 break;
51         default:
52                 return -EINVAL;
53         }
54         return kms->get_prop(kms, key, out);
55 }
56
57 int kms_destroy(struct kms_driver **kms)
58 {
59         if (!(*kms))
60                 return 0;
61
62         free(*kms);
63         *kms = NULL;
64         return 0;
65 }
66
67 int kms_bo_create(struct kms_driver *kms, const unsigned *attr, struct kms_bo **out)
68 {
69         unsigned width = 0;
70         unsigned height = 0;
71         enum kms_bo_type type = KMS_BO_TYPE_SCANOUT;
72         int i;
73
74         for (i = 0; attr[i];) {
75                 unsigned key = attr[i++];
76                 unsigned value = attr[i++];
77
78                 switch (key) {
79                 case KMS_WIDTH:
80                         width = value;
81                         break;
82                 case KMS_HEIGHT:
83                         height = value;
84                         break;
85                 case KMS_BO_TYPE:
86                         type = value;
87                         break;
88                 default:
89                         return EINVAL;
90                 }
91         }
92
93         if (width == 0 || height == 0)
94                 return -EINVAL;
95
96         return kms->bo_create(kms, width, height, type, attr, out);
97 }
98
99 int kms_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
100 {
101         switch (key) {
102         case KMS_PITCH:
103                 *out = bo->pitch;
104                 break;
105         case KMS_HANDLE:
106                 *out = bo->handle;
107                 break;
108         default:
109                 return -EINVAL;
110         }
111
112         return 0;
113 }
114
115 int kms_bo_map(struct kms_bo *bo, void **out)
116 {
117         return bo->kms->bo_map(bo, out);
118 }
119
120 int kms_bo_unmap(struct kms_bo *bo)
121 {
122         return bo->kms->bo_unmap(bo);
123 }
124
125 int kms_bo_destroy(struct kms_bo **bo)
126 {
127         int ret;
128
129         if (!(*bo))
130                 return 0;
131
132         ret = (*bo)->kms->bo_destroy(*bo);
133         if (ret)
134                 return ret;
135
136         *bo = NULL;
137         return 0;
138 }