1 #include "pipe/p_context.h"
2 #include "pipe/p_state.h"
3 #include "util/u_memory.h"
5 #include "nouveau_drm_api.h"
7 #include "nouveau_drmif.h"
8 #include "nouveau_channel.h"
9 #include "nouveau_bo.h"
11 #include "nouveau/nouveau_winsys.h"
12 #include "nouveau/nouveau_screen.h"
14 static struct pipe_surface
*
15 dri_surface_from_handle(struct pipe_screen
*screen
,
17 enum pipe_format format
,
22 struct pipe_surface
*surface
= NULL
;
23 struct pipe_texture
*texture
= NULL
;
24 struct pipe_texture templat
;
25 struct pipe_buffer
*buf
= NULL
;
27 buf
= drm_api_hooks
.buffer_from_handle(screen
, "front buffer", handle
);
31 memset(&templat
, 0, sizeof(templat
));
32 templat
.tex_usage
= PIPE_TEXTURE_USAGE_PRIMARY
|
33 NOUVEAU_TEXTURE_USAGE_LINEAR
;
34 templat
.target
= PIPE_TEXTURE_2D
;
35 templat
.last_level
= 0;
37 templat
.format
= format
;
38 templat
.width
[0] = width
;
39 templat
.height
[0] = height
;
40 pf_get_block(templat
.format
, &templat
.block
);
42 texture
= screen
->texture_blanket(screen
,
47 /* we don't need the buffer from this point on */
48 pipe_buffer_reference(&buf
, NULL
);
53 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
54 PIPE_BUFFER_USAGE_GPU_READ
|
55 PIPE_BUFFER_USAGE_GPU_WRITE
);
57 /* we don't need the texture from this point on */
58 pipe_texture_reference(&texture
, NULL
);
62 static struct pipe_surface
*
63 nouveau_dri1_front_surface(struct pipe_context
*pipe
)
65 return nouveau_winsys_screen(pipe
->screen
)->front
;
68 static struct dri1_api nouveau_dri1_api
= {
69 nouveau_dri1_front_surface
,
72 static struct pipe_screen
*
73 nouveau_drm_create_screen(int fd
, struct drm_create_screen_arg
*arg
)
75 struct dri1_create_screen_arg
*dri1
= (void *)arg
;
76 struct nouveau_winsys
*nvws
;
77 struct pipe_winsys
*ws
;
78 struct nouveau_device
*dev
= NULL
;
79 struct pipe_screen
*(*init
)(struct pipe_winsys
*,
80 struct nouveau_device
*);
83 ret
= nouveau_device_open_existing(&dev
, 0, fd
, 0);
87 switch (dev
->chipset
& 0xf0) {
89 init
= nv04_screen_create
;
92 init
= nv10_screen_create
;
95 init
= nv20_screen_create
;
98 init
= nv30_screen_create
;
102 init
= nv40_screen_create
;
107 init
= nv50_screen_create
;
110 debug_printf("%s: unknown chipset nv%02x\n", __func__
,
115 nvws
= CALLOC_STRUCT(nouveau_winsys
);
117 nouveau_device_close(&dev
);
122 nvws
->pscreen
= init(ws
, dev
);
123 if (!nvws
->pscreen
) {
128 if (arg
->mode
== DRM_CREATE_DRI1
) {
129 struct nouveau_dri
*nvdri
= dri1
->ddx_info
;
130 enum pipe_format format
;
132 if (nvdri
->bpp
== 16)
133 format
= PIPE_FORMAT_R5G6B5_UNORM
;
135 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
137 nvws
->front
= dri_surface_from_handle(nvws
->pscreen
,
145 debug_printf("%s: error referencing front buffer\n",
151 dri1
->api
= &nouveau_dri1_api
;
154 return nvws
->pscreen
;
157 static struct pipe_context
*
158 nouveau_drm_create_context(struct pipe_screen
*pscreen
)
160 struct nouveau_winsys
*nvws
= nouveau_winsys_screen(pscreen
);
161 struct pipe_context
*(*init
)(struct pipe_screen
*, unsigned);
162 unsigned chipset
= nouveau_screen(pscreen
)->device
->chipset
;
165 switch (chipset
& 0xf0) {
188 debug_printf("%s: unknown chipset nv%02x\n", __func__
, chipset
);
192 /* Find a free slot for a pipe context, allocate a new one if needed */
193 for (i
= 0; i
< nvws
->nr_pctx
; i
++) {
194 if (nvws
->pctx
[i
] == NULL
)
198 if (i
== nvws
->nr_pctx
) {
200 nvws
->pctx
= realloc(nvws
->pctx
,
201 sizeof(*nvws
->pctx
) * nvws
->nr_pctx
);
204 nvws
->pctx
[i
] = init(pscreen
, i
);
205 return nvws
->pctx
[i
];
209 nouveau_drm_pb_from_pt(struct pipe_texture
*pt
, struct pipe_buffer
**ppb
,
215 static struct pipe_buffer
*
216 nouveau_drm_pb_from_handle(struct pipe_screen
*pscreen
, const char *name
,
219 struct nouveau_device
*dev
= nouveau_screen(pscreen
)->device
;
220 struct pipe_buffer
*pb
;
223 pb
= CALLOC(1, sizeof(struct pipe_buffer
) + sizeof(struct nouveau_bo
*));
227 ret
= nouveau_bo_handle_ref(dev
, handle
, (struct nouveau_bo
**)(pb
+1));
229 debug_printf("%s: ref name 0x%08x failed with %d\n",
230 __func__
, handle
, ret
);
235 pipe_reference_init(&pb
->reference
, 1);
236 pb
->screen
= pscreen
;
238 pb
->usage
= PIPE_BUFFER_USAGE_GPU_READ_WRITE
|
239 PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
240 pb
->size
= nouveau_bo(pb
)->size
;
245 nouveau_drm_handle_from_pb(struct pipe_screen
*pscreen
, struct pipe_buffer
*pb
,
248 struct nouveau_bo
*bo
= nouveau_bo(pb
);
253 *handle
= bo
->handle
;
258 nouveau_drm_name_from_pb(struct pipe_screen
*pscreen
, struct pipe_buffer
*pb
,
261 struct nouveau_bo
*bo
= nouveau_bo(pb
);
266 return nouveau_bo_handle_get(bo
, handle
) == 0;
269 struct drm_api drm_api_hooks
= {
270 .create_screen
= nouveau_drm_create_screen
,
271 .create_context
= nouveau_drm_create_context
,
272 .buffer_from_texture
= nouveau_drm_pb_from_pt
,
273 .buffer_from_handle
= nouveau_drm_pb_from_handle
,
274 .handle_from_buffer
= nouveau_drm_handle_from_pb
,
275 .global_handle_from_buffer
= nouveau_drm_name_from_pb
,