Nuke the nv0x-nv2x gallium pipe drivers.
[mesa.git] / src / gallium / winsys / drm / nouveau / drm / nouveau_drm_api.c
1 #include "pipe/p_context.h"
2 #include "pipe/p_state.h"
3 #include "util/u_format.h"
4 #include "util/u_memory.h"
5
6 #include "nouveau_drm_api.h"
7
8 #include "nouveau_drmif.h"
9 #include "nouveau_channel.h"
10 #include "nouveau_bo.h"
11
12 #include "nouveau/nouveau_winsys.h"
13 #include "nouveau/nouveau_screen.h"
14
15 static struct pipe_surface *
16 dri_surface_from_handle(struct drm_api *api, struct pipe_screen *pscreen,
17 unsigned handle, enum pipe_format format,
18 unsigned width, unsigned height, unsigned pitch)
19 {
20 struct pipe_surface *ps = NULL;
21 struct pipe_texture *pt = NULL;
22 struct pipe_texture tmpl;
23
24 memset(&tmpl, 0, sizeof(tmpl));
25 tmpl.tex_usage = PIPE_TEXTURE_USAGE_PRIMARY;
26 tmpl.target = PIPE_TEXTURE_2D;
27 tmpl.last_level = 0;
28 tmpl.depth0 = 1;
29 tmpl.format = format;
30 tmpl.width0 = width;
31 tmpl.height0 = height;
32
33 pt = api->texture_from_shared_handle(api, pscreen, &tmpl,
34 "front buffer", pitch, handle);
35 if (!pt)
36 return NULL;
37
38 ps = pscreen->get_tex_surface(pscreen, pt, 0, 0, 0,
39 PIPE_BUFFER_USAGE_GPU_READ |
40 PIPE_BUFFER_USAGE_GPU_WRITE);
41
42 /* we don't need the texture from this point on */
43 pipe_texture_reference(&pt, NULL);
44 return ps;
45 }
46
47 static struct pipe_surface *
48 nouveau_dri1_front_surface(struct pipe_context *pipe)
49 {
50 return nouveau_winsys_screen(pipe->screen)->front;
51 }
52
53 static struct dri1_api nouveau_dri1_api = {
54 nouveau_dri1_front_surface,
55 };
56
57 static void
58 nouveau_drm_destroy_winsys(struct pipe_winsys *s)
59 {
60 struct nouveau_winsys *nv_winsys = nouveau_winsys(s);
61 struct nouveau_screen *nv_screen= nouveau_screen(nv_winsys->pscreen);
62 nouveau_device_close(&nv_screen->device);
63 FREE(nv_winsys->pctx);
64 FREE(nv_winsys);
65 }
66
67 static struct pipe_screen *
68 nouveau_drm_create_screen(struct drm_api *api, int fd,
69 struct drm_create_screen_arg *arg)
70 {
71 struct dri1_create_screen_arg *dri1 = (void *)arg;
72 struct nouveau_winsys *nvws;
73 struct pipe_winsys *ws;
74 struct nouveau_device *dev = NULL;
75 struct pipe_screen *(*init)(struct pipe_winsys *,
76 struct nouveau_device *);
77 int ret;
78
79 ret = nouveau_device_open_existing(&dev, 0, fd, 0);
80 if (ret)
81 return NULL;
82
83 switch (dev->chipset & 0xf0) {
84 case 0x30:
85 init = nv30_screen_create;
86 break;
87 case 0x40:
88 case 0x60:
89 init = nv40_screen_create;
90 break;
91 case 0x50:
92 case 0x80:
93 case 0x90:
94 case 0xa0:
95 init = nv50_screen_create;
96 break;
97 default:
98 debug_printf("%s: unknown chipset nv%02x\n", __func__,
99 dev->chipset);
100 return NULL;
101 }
102
103 nvws = CALLOC_STRUCT(nouveau_winsys);
104 if (!nvws) {
105 nouveau_device_close(&dev);
106 return NULL;
107 }
108 ws = &nvws->base;
109 ws->destroy = nouveau_drm_destroy_winsys;
110
111 nvws->pscreen = init(ws, dev);
112 if (!nvws->pscreen) {
113 ws->destroy(ws);
114 return NULL;
115 }
116
117 if (arg && arg->mode == DRM_CREATE_DRI1) {
118 struct nouveau_dri *nvdri = dri1->ddx_info;
119 enum pipe_format format;
120
121 if (nvdri->bpp == 16)
122 format = PIPE_FORMAT_R5G6B5_UNORM;
123 else
124 format = PIPE_FORMAT_A8R8G8B8_UNORM;
125
126 nvws->front = dri_surface_from_handle(api, nvws->pscreen,
127 nvdri->front_offset,
128 format, nvdri->width,
129 nvdri->height,
130 nvdri->front_pitch *
131 (nvdri->bpp / 8));
132 if (!nvws->front) {
133 debug_printf("%s: error referencing front buffer\n",
134 __func__);
135 ws->destroy(ws);
136 return NULL;
137 }
138
139 dri1->api = &nouveau_dri1_api;
140 }
141
142 return nvws->pscreen;
143 }
144
145 static struct pipe_context *
146 nouveau_drm_create_context(struct drm_api *api, struct pipe_screen *pscreen)
147 {
148 struct nouveau_winsys *nvws = nouveau_winsys_screen(pscreen);
149 struct pipe_context *(*init)(struct pipe_screen *, unsigned);
150 unsigned chipset = nouveau_screen(pscreen)->device->chipset;
151 int i;
152
153 switch (chipset & 0xf0) {
154 case 0x30:
155 init = nv30_create;
156 break;
157 case 0x40:
158 case 0x60:
159 init = nv40_create;
160 break;
161 case 0x50:
162 case 0x80:
163 case 0x90:
164 case 0xa0:
165 init = nv50_create;
166 break;
167 default:
168 debug_printf("%s: unknown chipset nv%02x\n", __func__, chipset);
169 return NULL;
170 }
171
172 /* Find a free slot for a pipe context, allocate a new one if needed */
173 for (i = 0; i < nvws->nr_pctx; i++) {
174 if (nvws->pctx[i] == NULL)
175 break;
176 }
177
178 if (i == nvws->nr_pctx) {
179 nvws->nr_pctx++;
180 nvws->pctx = realloc(nvws->pctx,
181 sizeof(*nvws->pctx) * nvws->nr_pctx);
182 }
183
184 nvws->pctx[i] = init(pscreen, i);
185 return nvws->pctx[i];
186 }
187
188 static struct pipe_texture *
189 nouveau_drm_pt_from_name(struct drm_api *api, struct pipe_screen *pscreen,
190 struct pipe_texture *templ, const char *name,
191 unsigned stride, unsigned handle)
192 {
193 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
194 struct pipe_texture *pt;
195 struct pipe_buffer *pb;
196 int ret;
197
198 pb = CALLOC(1, sizeof(struct pipe_buffer) + sizeof(struct nouveau_bo*));
199 if (!pb)
200 return NULL;
201
202 ret = nouveau_bo_handle_ref(dev, handle, (struct nouveau_bo**)(pb+1));
203 if (ret) {
204 debug_printf("%s: ref name 0x%08x failed with %d\n",
205 __func__, handle, ret);
206 FREE(pb);
207 return NULL;
208 }
209
210 pipe_reference_init(&pb->reference, 1);
211 pb->screen = pscreen;
212 pb->alignment = 0;
213 pb->usage = PIPE_BUFFER_USAGE_GPU_READ_WRITE |
214 PIPE_BUFFER_USAGE_CPU_READ_WRITE;
215 pb->size = nouveau_bo(pb)->size;
216 pt = pscreen->texture_blanket(pscreen, templ, &stride, pb);
217 pipe_buffer_reference(&pb, NULL);
218 return pt;
219 }
220
221 static boolean
222 nouveau_drm_name_from_pt(struct drm_api *api, struct pipe_screen *pscreen,
223 struct pipe_texture *pt, unsigned *stride,
224 unsigned *handle)
225 {
226 struct nouveau_miptree *mt = nouveau_miptree(pt);
227
228 if (!mt || !mt->bo)
229 return false;
230
231 return nouveau_bo_handle_get(mt->bo, handle) == 0;
232 }
233
234 static boolean
235 nouveau_drm_handle_from_pt(struct drm_api *api, struct pipe_screen *pscreen,
236 struct pipe_texture *pt, unsigned *stride,
237 unsigned *handle)
238 {
239 struct nouveau_miptree *mt = nouveau_miptree(pt);
240
241 if (!mt || !mt->bo)
242 return false;
243
244 *handle = mt->bo->handle;
245 *stride = util_format_get_stride(mt->base.format, mt->base.width0);
246 return true;
247 }
248
249 struct drm_api drm_api_hooks = {
250 .name = "nouveau",
251 .driver_name = "nouveau",
252 .create_screen = nouveau_drm_create_screen,
253 .create_context = nouveau_drm_create_context,
254 .texture_from_shared_handle = nouveau_drm_pt_from_name,
255 .shared_handle_from_texture = nouveau_drm_name_from_pt,
256 .local_handle_from_texture = nouveau_drm_handle_from_pt,
257 };
258
259 struct drm_api *
260 drm_api_create() {
261 return &drm_api_hooks;
262 }
263