st/egl: Probe hardware for depth stencil format
[mesa.git] / src / gallium / state_trackers / egl / egl_surface.c
1
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "egl_tracker.h"
6
7 #include "egllog.h"
8
9 #include "pipe/p_inlines.h"
10 #include "pipe/p_screen.h"
11 #include "pipe/p_context.h"
12
13 #include "state_tracker/drm_api.h"
14
15 #include "util/u_rect.h"
16
17 /*
18 * Util functions
19 */
20
21 static drmModeModeInfoPtr
22 drm_find_mode(drmModeConnectorPtr connector, _EGLMode *mode)
23 {
24 int i;
25 drmModeModeInfoPtr m = NULL;
26
27 for (i = 0; i < connector->count_modes; i++) {
28 m = &connector->modes[i];
29 if (m->hdisplay == mode->Width && m->vdisplay == mode->Height && m->vrefresh == mode->RefreshRate)
30 break;
31 m = &connector->modes[0]; /* if we can't find one, return first */
32 }
33
34 return m;
35 }
36
37 static struct st_framebuffer *
38 drm_create_framebuffer(struct pipe_screen *screen,
39 const __GLcontextModes *visual,
40 unsigned width,
41 unsigned height,
42 void *priv)
43 {
44 enum pipe_format color_format, depth_stencil_format;
45 boolean d_depth_bits_last;
46 boolean ds_depth_bits_last;
47
48 d_depth_bits_last =
49 screen->is_format_supported(screen, PIPE_FORMAT_X8Z24_UNORM,
50 PIPE_TEXTURE_2D,
51 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
52 ds_depth_bits_last =
53 screen->is_format_supported(screen, PIPE_FORMAT_S8Z24_UNORM,
54 PIPE_TEXTURE_2D,
55 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
56
57 if (visual->redBits == 8) {
58 if (visual->alphaBits == 8)
59 color_format = PIPE_FORMAT_A8R8G8B8_UNORM;
60 else
61 color_format = PIPE_FORMAT_X8R8G8B8_UNORM;
62 } else {
63 color_format = PIPE_FORMAT_R5G6B5_UNORM;
64 }
65
66 switch(visual->depthBits) {
67 default:
68 case 0:
69 depth_stencil_format = PIPE_FORMAT_NONE;
70 break;
71 case 16:
72 depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
73 break;
74 case 24:
75 if (visual->stencilBits == 0) {
76 depth_stencil_format = (d_depth_bits_last) ?
77 PIPE_FORMAT_X8Z24_UNORM:
78 PIPE_FORMAT_Z24X8_UNORM;
79 } else {
80 depth_stencil_format = (ds_depth_bits_last) ?
81 PIPE_FORMAT_S8Z24_UNORM:
82 PIPE_FORMAT_Z24S8_UNORM;
83 }
84 break;
85 case 32:
86 depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
87 break;
88 }
89
90 return st_create_framebuffer(visual,
91 color_format,
92 depth_stencil_format,
93 depth_stencil_format,
94 width,
95 height,
96 priv);
97 }
98
99 static void
100 drm_create_texture(_EGLDisplay *dpy,
101 struct drm_screen *scrn,
102 unsigned w, unsigned h)
103 {
104 struct drm_device *dev = lookup_drm_device(dpy);
105 struct pipe_screen *screen = dev->screen;
106 struct pipe_surface *surface;
107 struct pipe_texture *texture;
108 struct pipe_texture templat;
109 struct pipe_buffer *buf = NULL;
110 unsigned pitch = 0;
111
112 memset(&templat, 0, sizeof(templat));
113 templat.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
114 templat.tex_usage |= PIPE_TEXTURE_USAGE_PRIMARY;
115 templat.target = PIPE_TEXTURE_2D;
116 templat.last_level = 0;
117 templat.depth[0] = 1;
118 templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
119 templat.width[0] = w;
120 templat.height[0] = h;
121 pf_get_block(templat.format, &templat.block);
122
123 texture = screen->texture_create(dev->screen,
124 &templat);
125
126 if (!texture)
127 goto err_tex;
128
129 surface = screen->get_tex_surface(screen,
130 texture,
131 0,
132 0,
133 0,
134 PIPE_BUFFER_USAGE_GPU_WRITE);
135
136 if (!surface)
137 goto err_surf;
138
139 scrn->tex = texture;
140 scrn->surface = surface;
141 scrn->front.width = w;
142 scrn->front.height = h;
143 scrn->front.pitch = pitch;
144 dev->api->local_handle_from_texture(dev->api, screen, texture,
145 &scrn->front.pitch, &scrn->front.handle);
146 if (0)
147 goto err_handle;
148
149 return;
150
151 err_handle:
152 pipe_surface_reference(&surface, NULL);
153 err_surf:
154 pipe_texture_reference(&texture, NULL);
155 err_tex:
156 pipe_buffer_reference(&buf, NULL);
157 return;
158 }
159
160 /*
161 * Exported functions
162 */
163
164 void
165 drm_takedown_shown_screen(_EGLDisplay *dpy, struct drm_screen *screen)
166 {
167 struct drm_device *dev = lookup_drm_device(dpy);
168
169 screen->surf = NULL;
170
171 drmModeSetCrtc(
172 dev->drmFD,
173 screen->crtcID,
174 0, // FD
175 0, 0,
176 NULL, 0, // List of output ids
177 NULL);
178
179 drmModeRmFB(dev->drmFD, screen->fbID);
180 drmModeFreeFB(screen->fb);
181 screen->fb = NULL;
182
183 pipe_surface_reference(&screen->surface, NULL);
184 pipe_texture_reference(&screen->tex, NULL);
185
186 screen->shown = 0;
187 }
188
189 _EGLSurface *
190 drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, NativeWindowType window, const EGLint *attrib_list)
191 {
192 return NULL;
193 }
194
195
196 _EGLSurface *
197 drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, NativePixmapType pixmap, const EGLint *attrib_list)
198 {
199 return NULL;
200 }
201
202
203 _EGLSurface *
204 drm_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
205 const EGLint *attrib_list)
206 {
207 struct drm_device *dev = lookup_drm_device(dpy);
208 int i;
209 int width = -1;
210 int height = -1;
211 struct drm_surface *surf = NULL;
212 __GLcontextModes *visual;
213
214 for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
215 switch (attrib_list[i]) {
216 case EGL_WIDTH:
217 width = attrib_list[++i];
218 break;
219 case EGL_HEIGHT:
220 height = attrib_list[++i];
221 break;
222 default:
223 _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface");
224 return EGL_NO_SURFACE;
225 }
226 }
227
228 if (width < 1 || height < 1) {
229 _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface");
230 return NULL;
231 }
232
233 surf = (struct drm_surface *) calloc(1, sizeof(struct drm_surface));
234 if (!surf)
235 goto err;
236
237 if (!_eglInitSurface(drv, &surf->base, EGL_PBUFFER_BIT, conf, attrib_list))
238 goto err_surf;
239
240 surf->w = width;
241 surf->h = height;
242
243 visual = drm_visual_from_config(conf);
244 surf->stfb = drm_create_framebuffer(dev->screen, visual,
245 width, height,
246 (void*)surf);
247 drm_visual_modes_destroy(visual);
248
249 return &surf->base;
250
251 err_surf:
252 free(surf);
253 err:
254 return NULL;
255 }
256
257 _EGLSurface *
258 drm_create_screen_surface_mesa(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *cfg,
259 const EGLint *attrib_list)
260 {
261 EGLSurface surf = drm_create_pbuffer_surface(drv, dpy, cfg, attrib_list);
262
263 return surf;
264 }
265
266 EGLBoolean
267 drm_show_screen_surface_mesa(_EGLDriver *drv, _EGLDisplay *dpy,
268 _EGLScreen *screen,
269 _EGLSurface *surface, _EGLMode *mode)
270 {
271 struct drm_device *dev = lookup_drm_device(dpy);
272 struct drm_surface *surf = lookup_drm_surface(surface);
273 struct drm_screen *scrn = lookup_drm_screen(screen);
274 int ret;
275 unsigned int i, k;
276
277 if (scrn->shown)
278 drm_takedown_shown_screen(dpy, scrn);
279
280
281 drm_create_texture(dpy, scrn, mode->Width, mode->Height);
282 if (!scrn->tex)
283 goto err_tex;
284
285 ret = drmModeAddFB(dev->drmFD,
286 scrn->front.width, scrn->front.height,
287 32, 32, scrn->front.pitch,
288 scrn->front.handle,
289 &scrn->fbID);
290
291 if (ret)
292 goto err_bo;
293
294 scrn->fb = drmModeGetFB(dev->drmFD, scrn->fbID);
295 if (!scrn->fb)
296 goto err_bo;
297
298 /* find a fitting crtc */
299 {
300 drmModeConnector *con = scrn->connector;
301
302 scrn->mode = drm_find_mode(con, mode);
303 if (!scrn->mode)
304 goto err_fb;
305
306 for (k = 0; k < con->count_encoders; k++) {
307 drmModeEncoder *enc = drmModeGetEncoder(dev->drmFD, con->encoders[k]);
308 for (i = 0; i < dev->res->count_crtcs; i++) {
309 if (enc->possible_crtcs & (1<<i)) {
310 /* save the ID */
311 scrn->crtcID = dev->res->crtcs[i];
312
313 /* skip the rest */
314 i = dev->res->count_crtcs;
315 k = dev->res->count_encoders;
316 }
317 }
318 drmModeFreeEncoder(enc);
319 }
320 }
321
322 ret = drmModeSetCrtc(dev->drmFD,
323 scrn->crtcID,
324 scrn->fbID,
325 0, 0,
326 &scrn->connectorID, 1,
327 scrn->mode);
328
329 if (ret)
330 goto err_crtc;
331
332
333 if (scrn->dpms)
334 drmModeConnectorSetProperty(dev->drmFD,
335 scrn->connectorID,
336 scrn->dpms->prop_id,
337 DRM_MODE_DPMS_ON);
338
339 surf->screen = scrn;
340
341 scrn->surf = surf;
342 scrn->shown = 1;
343
344 return EGL_TRUE;
345
346 err_crtc:
347 scrn->crtcID = 0;
348
349 err_fb:
350 drmModeRmFB(dev->drmFD, scrn->fbID);
351 drmModeFreeFB(scrn->fb);
352 scrn->fb = NULL;
353
354 err_bo:
355 pipe_surface_reference(&scrn->surface, NULL);
356 pipe_texture_reference(&scrn->tex, NULL);
357
358 err_tex:
359 return EGL_FALSE;
360 }
361
362 EGLBoolean
363 drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface)
364 {
365 struct drm_surface *surf = lookup_drm_surface(surface);
366 if (!_eglIsSurfaceBound(&surf->base)) {
367 if (surf->screen)
368 drm_takedown_shown_screen(dpy, surf->screen);
369 st_unreference_framebuffer(surf->stfb);
370 free(surf);
371 }
372 return EGL_TRUE;
373 }
374
375 EGLBoolean
376 drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw)
377 {
378 struct drm_device *dev = lookup_drm_device(dpy);
379 struct drm_surface *surf = lookup_drm_surface(draw);
380 struct pipe_surface *back_surf;
381
382 if (!surf)
383 return EGL_FALSE;
384
385 st_get_framebuffer_surface(surf->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
386
387 if (back_surf) {
388 struct drm_context *ctx = lookup_drm_context(draw->Binding);
389
390 st_notify_swapbuffers(surf->stfb);
391
392 if (ctx && surf->screen) {
393 if (ctx->pipe->surface_copy) {
394 ctx->pipe->surface_copy(ctx->pipe,
395 surf->screen->surface,
396 0, 0,
397 back_surf,
398 0, 0,
399 surf->w, surf->h);
400 } else {
401 util_surface_copy(ctx->pipe, FALSE,
402 surf->screen->surface,
403 0, 0,
404 back_surf,
405 0, 0,
406 surf->w, surf->h);
407 }
408 ctx->pipe->flush(ctx->pipe, PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_TEXTURE_CACHE, NULL);
409
410 #ifdef DRM_MODE_FEATURE_DIRTYFB
411 /* TODO query connector property to see if this is needed */
412 drmModeDirtyFB(dev->drmFD, surf->screen->fbID, NULL, 0);
413 #else
414 (void)dev;
415 #endif
416
417 /* TODO more stuff here */
418 }
419 }
420
421 return EGL_TRUE;
422 }