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 drm_api
*api
, struct pipe_screen
*pscreen
,
16 unsigned handle
, enum pipe_format format
,
17 unsigned width
, unsigned height
, unsigned pitch
)
19 struct pipe_surface
*ps
= NULL
;
20 struct pipe_texture
*pt
= NULL
;
21 struct pipe_texture tmpl
;
23 memset(&tmpl
, 0, sizeof(tmpl
));
24 tmpl
.tex_usage
= PIPE_TEXTURE_USAGE_PRIMARY
|
25 NOUVEAU_TEXTURE_USAGE_LINEAR
;
26 tmpl
.target
= PIPE_TEXTURE_2D
;
30 tmpl
.width
[0] = width
;
31 tmpl
.height
[0] = height
;
32 pf_get_block(tmpl
.format
, &tmpl
.block
);
34 pt
= api
->texture_from_shared_handle(api
, pscreen
, &tmpl
,
35 "front buffer", pitch
, handle
);
39 ps
= pscreen
->get_tex_surface(pscreen
, pt
, 0, 0, 0,
40 PIPE_BUFFER_USAGE_GPU_READ
|
41 PIPE_BUFFER_USAGE_GPU_WRITE
);
43 /* we don't need the texture from this point on */
44 pipe_texture_reference(&pt
, NULL
);
48 static struct pipe_surface
*
49 nouveau_dri1_front_surface(struct pipe_context
*pipe
)
51 return nouveau_winsys_screen(pipe
->screen
)->front
;
54 static struct dri1_api nouveau_dri1_api
= {
55 nouveau_dri1_front_surface
,
58 static struct pipe_screen
*
59 nouveau_drm_create_screen(struct drm_api
*api
, int fd
,
60 struct drm_create_screen_arg
*arg
)
62 struct dri1_create_screen_arg
*dri1
= (void *)arg
;
63 struct nouveau_winsys
*nvws
;
64 struct pipe_winsys
*ws
;
65 struct nouveau_device
*dev
= NULL
;
66 struct pipe_screen
*(*init
)(struct pipe_winsys
*,
67 struct nouveau_device
*);
70 ret
= nouveau_device_open_existing(&dev
, 0, fd
, 0);
74 switch (dev
->chipset
& 0xf0) {
76 init
= nv04_screen_create
;
79 init
= nv10_screen_create
;
82 init
= nv20_screen_create
;
85 init
= nv30_screen_create
;
89 init
= nv40_screen_create
;
94 init
= nv50_screen_create
;
97 debug_printf("%s: unknown chipset nv%02x\n", __func__
,
102 nvws
= CALLOC_STRUCT(nouveau_winsys
);
104 nouveau_device_close(&dev
);
109 nvws
->pscreen
= init(ws
, dev
);
110 if (!nvws
->pscreen
) {
115 if (arg
&& arg
->mode
== DRM_CREATE_DRI1
) {
116 struct nouveau_dri
*nvdri
= dri1
->ddx_info
;
117 enum pipe_format format
;
119 if (nvdri
->bpp
== 16)
120 format
= PIPE_FORMAT_R5G6B5_UNORM
;
122 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
124 nvws
->front
= dri_surface_from_handle(api
, nvws
->pscreen
,
126 format
, nvdri
->width
,
131 debug_printf("%s: error referencing front buffer\n",
137 dri1
->api
= &nouveau_dri1_api
;
140 return nvws
->pscreen
;
143 static struct pipe_context
*
144 nouveau_drm_create_context(struct drm_api
*api
, struct pipe_screen
*pscreen
)
146 struct nouveau_winsys
*nvws
= nouveau_winsys_screen(pscreen
);
147 struct pipe_context
*(*init
)(struct pipe_screen
*, unsigned);
148 unsigned chipset
= nouveau_screen(pscreen
)->device
->chipset
;
151 switch (chipset
& 0xf0) {
174 debug_printf("%s: unknown chipset nv%02x\n", __func__
, chipset
);
178 /* Find a free slot for a pipe context, allocate a new one if needed */
179 for (i
= 0; i
< nvws
->nr_pctx
; i
++) {
180 if (nvws
->pctx
[i
] == NULL
)
184 if (i
== nvws
->nr_pctx
) {
186 nvws
->pctx
= realloc(nvws
->pctx
,
187 sizeof(*nvws
->pctx
) * nvws
->nr_pctx
);
190 nvws
->pctx
[i
] = init(pscreen
, i
);
191 return nvws
->pctx
[i
];
194 static struct pipe_texture
*
195 nouveau_drm_pt_from_name(struct drm_api
*api
, struct pipe_screen
*pscreen
,
196 struct pipe_texture
*templ
, const char *name
,
197 unsigned stride
, unsigned handle
)
199 struct nouveau_device
*dev
= nouveau_screen(pscreen
)->device
;
200 struct pipe_texture
*pt
;
201 struct pipe_buffer
*pb
;
204 pb
= CALLOC(1, sizeof(struct pipe_buffer
) + sizeof(struct nouveau_bo
*));
208 ret
= nouveau_bo_handle_ref(dev
, handle
, (struct nouveau_bo
**)(pb
+1));
210 debug_printf("%s: ref name 0x%08x failed with %d\n",
211 __func__
, handle
, ret
);
216 pipe_reference_init(&pb
->reference
, 1);
217 pb
->screen
= pscreen
;
219 pb
->usage
= PIPE_BUFFER_USAGE_GPU_READ_WRITE
|
220 PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
221 pb
->size
= nouveau_bo(pb
)->size
;
222 pt
= pscreen
->texture_blanket(pscreen
, templ
, &stride
, pb
);
223 pipe_buffer_reference(&pb
, NULL
);
228 nouveau_drm_name_from_pt(struct drm_api
*api
, struct pipe_screen
*pscreen
,
229 struct pipe_texture
*pt
, unsigned *stride
,
232 struct nouveau_miptree
*mt
= nouveau_miptree(pt
);
237 return nouveau_bo_handle_get(mt
->bo
, handle
) == 0;
241 nouveau_drm_handle_from_pt(struct drm_api
*api
, struct pipe_screen
*pscreen
,
242 struct pipe_texture
*pt
, unsigned *stride
,
245 struct nouveau_miptree
*mt
= nouveau_miptree(pt
);
250 *handle
= mt
->bo
->handle
;
251 *stride
= mt
->base
.nblocksx
[0] * mt
->base
.block
.size
;
255 struct drm_api drm_api_hooks
= {
256 .create_screen
= nouveau_drm_create_screen
,
257 .create_context
= nouveau_drm_create_context
,
258 .texture_from_shared_handle
= nouveau_drm_pt_from_name
,
259 .shared_handle_from_texture
= nouveau_drm_name_from_pt
,
260 .local_handle_from_texture
= nouveau_drm_handle_from_pt
,
265 return &drm_api_hooks
;