Merge branch 'modesetting-gem' of ssh://git.freedesktop.org/git/mesa/drm into modeset...
[profile/ivi/libdrm.git] / libdrm / intel / intel_bufmgr.c
1 /*
2  * Copyright © 2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <drm.h>
38 #include <i915_drm.h>
39 #include "intel_bufmgr.h"
40 #include "intel_bufmgr_priv.h"
41
42 /** @file dri_bufmgr.c
43  *
44  * Convenience functions for buffer management methods.
45  */
46
47 dri_bo *
48 dri_bo_alloc(dri_bufmgr *bufmgr, const char *name, unsigned long size,
49              unsigned int alignment)
50 {
51    return bufmgr->bo_alloc(bufmgr, name, size, alignment);
52 }
53
54 void
55 dri_bo_reference(dri_bo *bo)
56 {
57    bo->bufmgr->bo_reference(bo);
58 }
59
60 void
61 dri_bo_unreference(dri_bo *bo)
62 {
63    if (bo == NULL)
64       return;
65
66    bo->bufmgr->bo_unreference(bo);
67 }
68
69 int
70 dri_bo_map(dri_bo *buf, int write_enable)
71 {
72    return buf->bufmgr->bo_map(buf, write_enable);
73 }
74
75 int
76 dri_bo_unmap(dri_bo *buf)
77 {
78    return buf->bufmgr->bo_unmap(buf);
79 }
80
81 int
82 dri_bo_subdata(dri_bo *bo, unsigned long offset,
83                unsigned long size, const void *data)
84 {
85    int ret;
86    if (bo->bufmgr->bo_subdata)
87       return bo->bufmgr->bo_subdata(bo, offset, size, data);
88    if (size == 0 || data == NULL)
89       return 0;
90
91    ret = dri_bo_map(bo, 1);
92    if (ret)
93        return ret;
94    memcpy((unsigned char *)bo->virtual + offset, data, size);
95    dri_bo_unmap(bo);
96    return 0;
97 }
98
99 int
100 dri_bo_get_subdata(dri_bo *bo, unsigned long offset,
101                    unsigned long size, void *data)
102 {
103    int ret;
104    if (bo->bufmgr->bo_subdata)
105       return bo->bufmgr->bo_get_subdata(bo, offset, size, data);
106
107    if (size == 0 || data == NULL)
108       return 0;
109
110    ret = dri_bo_map(bo, 0);
111    if (ret)
112        return ret;
113    memcpy(data, (unsigned char *)bo->virtual + offset, size);
114    dri_bo_unmap(bo);
115    return 0;
116 }
117
118 void
119 dri_bo_wait_rendering(dri_bo *bo)
120 {
121    bo->bufmgr->bo_wait_rendering(bo);
122 }
123
124 void
125 dri_bufmgr_destroy(dri_bufmgr *bufmgr)
126 {
127    bufmgr->destroy(bufmgr);
128 }
129
130 int
131 dri_bo_exec(dri_bo *bo, int used,
132             drm_clip_rect_t *cliprects, int num_cliprects,
133             int DR4)
134 {
135    return bo->bufmgr->bo_exec(bo, used, cliprects, num_cliprects, DR4);
136 }
137
138 void
139 dri_bufmgr_set_debug(dri_bufmgr *bufmgr, int enable_debug)
140 {
141    bufmgr->debug = enable_debug;
142 }
143
144 int
145 dri_bufmgr_check_aperture_space(dri_bo **bo_array, int count)
146 {
147         return bo_array[0]->bufmgr->check_aperture_space(bo_array, count);
148 }
149
150 int
151 dri_bo_flink(dri_bo *bo, uint32_t *name)
152 {
153     if (bo->bufmgr->bo_flink)
154         return bo->bufmgr->bo_flink(bo, name);
155
156     return -ENODEV;
157 }
158
159 int
160 dri_bo_emit_reloc(dri_bo *reloc_buf,
161                   uint32_t read_domains, uint32_t write_domain,
162                   uint32_t delta, uint32_t offset, dri_bo *target_buf)
163 {
164     return reloc_buf->bufmgr->bo_emit_reloc(reloc_buf,
165                                             read_domains, write_domain,
166                                             delta, offset, target_buf);
167 }
168
169 int
170 dri_bo_pin(dri_bo *bo, uint32_t alignment)
171 {
172     if (bo->bufmgr->bo_pin)
173         return bo->bufmgr->bo_pin(bo, alignment);
174
175     return -ENODEV;
176 }
177
178 int
179 dri_bo_unpin(dri_bo *bo)
180 {
181     if (bo->bufmgr->bo_unpin)
182         return bo->bufmgr->bo_unpin(bo);
183
184     return -ENODEV;
185 }
186
187 int dri_bo_set_tiling(dri_bo *bo, uint32_t *tiling_mode)
188 {
189     if (bo->bufmgr->bo_set_tiling)
190         return bo->bufmgr->bo_set_tiling(bo, tiling_mode);
191
192     *tiling_mode = I915_TILING_NONE;
193     return 0;
194 }