Merge branch 'mesa_7_7_branch'
[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.depth0 = 1;
118 templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
119 templat.width0 = w;
120 templat.height0 = h;
121
122 texture = screen->texture_create(dev->screen,
123 &templat);
124
125 if (!texture)
126 goto err_tex;
127
128 surface = screen->get_tex_surface(screen,
129 texture,
130 0,
131 0,
132 0,
133 PIPE_BUFFER_USAGE_GPU_WRITE);
134
135 if (!surface)
136 goto err_surf;
137
138 scrn->tex = texture;
139 scrn->surface = surface;
140 scrn->front.width = w;
141 scrn->front.height = h;
142 scrn->front.pitch = pitch;
143 dev->api->local_handle_from_texture(dev->api, screen, texture,
144 &scrn->front.pitch, &scrn->front.handle);
145 if (0)
146 goto err_handle;
147
148 return;
149
150 err_handle:
151 pipe_surface_reference(&surface, NULL);
152 err_surf:
153 pipe_texture_reference(&texture, NULL);
154 err_tex:
155 pipe_buffer_reference(&buf, NULL);
156 return;
157 }
158
159 /*
160 * Exported functions
161 */
162
163 void
164 drm_takedown_shown_screen(_EGLDisplay *dpy, struct drm_screen *screen)
165 {
166 struct drm_device *dev = lookup_drm_device(dpy);
167
168 screen->surf = NULL;
169
170 drmModeSetCrtc(
171 dev->drmFD,
172 screen->crtcID,
173 0, // FD
174 0, 0,
175 NULL, 0, // List of output ids
176 NULL);
177
178 drmModeRmFB(dev->drmFD, screen->fbID);
179 drmModeFreeFB(screen->fb);
180 screen->fb = NULL;
181
182 pipe_surface_reference(&screen->surface, NULL);
183 pipe_texture_reference(&screen->tex, NULL);
184
185 screen->shown = 0;
186 }
187
188 /**
189 * Called by libEGL's eglCreateWindowSurface().
190 */
191 _EGLSurface *
192 drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, NativeWindowType window, const EGLint *attrib_list)
193 {
194 return NULL;
195 }
196
197
198 /**
199 * Called by libEGL's eglCreatePixmapSurface().
200 */
201 _EGLSurface *
202 drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, NativePixmapType pixmap, const EGLint *attrib_list)
203 {
204 return NULL;
205 }
206
207
208 /**
209 * Called by libEGL's eglCreatePbufferSurface().
210 */
211 _EGLSurface *
212 drm_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
213 const EGLint *attrib_list)
214 {
215 struct drm_device *dev = lookup_drm_device(dpy);
216 int i;
217 int width = -1;
218 int height = -1;
219 struct drm_surface *surf = NULL;
220 __GLcontextModes *visual;
221
222 for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
223 switch (attrib_list[i]) {
224 case EGL_WIDTH:
225 width = attrib_list[++i];
226 break;
227 case EGL_HEIGHT:
228 height = attrib_list[++i];
229 break;
230 default:
231 _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface");
232 return EGL_NO_SURFACE;
233 }
234 }
235
236 if (width < 1 || height < 1) {
237 _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface");
238 return NULL;
239 }
240
241 surf = (struct drm_surface *) calloc(1, sizeof(struct drm_surface));
242 if (!surf)
243 goto err;
244
245 if (!_eglInitSurface(drv, &surf->base, EGL_PBUFFER_BIT, conf, attrib_list))
246 goto err_surf;
247
248 surf->w = width;
249 surf->h = height;
250
251 visual = drm_visual_from_config(conf);
252 surf->stfb = drm_create_framebuffer(dev->screen, visual,
253 width, height,
254 (void*)surf);
255 drm_visual_modes_destroy(visual);
256
257 return &surf->base;
258
259 err_surf:
260 free(surf);
261 err:
262 return NULL;
263 }
264
265 /**
266 * Called by libEGL's eglCreateScreenSurfaceMESA().
267 */
268 _EGLSurface *
269 drm_create_screen_surface_mesa(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *cfg,
270 const EGLint *attrib_list)
271 {
272 EGLSurface surf = drm_create_pbuffer_surface(drv, dpy, cfg, attrib_list);
273
274 return surf;
275 }
276
277 /**
278 * Called by libEGL's eglShowScreenSurfaceMESA().
279 */
280 EGLBoolean
281 drm_show_screen_surface_mesa(_EGLDriver *drv, _EGLDisplay *dpy,
282 _EGLScreen *screen,
283 _EGLSurface *surface, _EGLMode *mode)
284 {
285 struct drm_device *dev = lookup_drm_device(dpy);
286 struct drm_surface *surf = lookup_drm_surface(surface);
287 struct drm_screen *scrn = lookup_drm_screen(screen);
288 int ret;
289 unsigned int i, k;
290
291 if (scrn->shown)
292 drm_takedown_shown_screen(dpy, scrn);
293
294
295 drm_create_texture(dpy, scrn, mode->Width, mode->Height);
296 if (!scrn->tex)
297 goto err_tex;
298
299 ret = drmModeAddFB(dev->drmFD,
300 scrn->front.width, scrn->front.height,
301 32, 32, scrn->front.pitch,
302 scrn->front.handle,
303 &scrn->fbID);
304
305 if (ret)
306 goto err_bo;
307
308 scrn->fb = drmModeGetFB(dev->drmFD, scrn->fbID);
309 if (!scrn->fb)
310 goto err_bo;
311
312 /* find a fitting crtc */
313 {
314 drmModeConnector *con = scrn->connector;
315
316 scrn->mode = drm_find_mode(con, mode);
317 if (!scrn->mode)
318 goto err_fb;
319
320 for (k = 0; k < con->count_encoders; k++) {
321 drmModeEncoder *enc = drmModeGetEncoder(dev->drmFD, con->encoders[k]);
322 for (i = 0; i < dev->res->count_crtcs; i++) {
323 if (enc->possible_crtcs & (1<<i)) {
324 /* save the ID */
325 scrn->crtcID = dev->res->crtcs[i];
326
327 /* skip the rest */
328 i = dev->res->count_crtcs;
329 k = dev->res->count_encoders;
330 }
331 }
332 drmModeFreeEncoder(enc);
333 }
334 }
335
336 ret = drmModeSetCrtc(dev->drmFD,
337 scrn->crtcID,
338 scrn->fbID,
339 0, 0,
340 &scrn->connectorID, 1,
341 scrn->mode);
342
343 if (ret)
344 goto err_crtc;
345
346
347 if (scrn->dpms)
348 drmModeConnectorSetProperty(dev->drmFD,
349 scrn->connectorID,
350 scrn->dpms->prop_id,
351 DRM_MODE_DPMS_ON);
352
353 surf->screen = scrn;
354
355 scrn->surf = surf;
356 scrn->shown = 1;
357
358 return EGL_TRUE;
359
360 err_crtc:
361 scrn->crtcID = 0;
362
363 err_fb:
364 drmModeRmFB(dev->drmFD, scrn->fbID);
365 drmModeFreeFB(scrn->fb);
366 scrn->fb = NULL;
367
368 err_bo:
369 pipe_surface_reference(&scrn->surface, NULL);
370 pipe_texture_reference(&scrn->tex, NULL);
371
372 err_tex:
373 return EGL_FALSE;
374 }
375
376 /**
377 * Called by libEGL's eglDestroySurface().
378 */
379 EGLBoolean
380 drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface)
381 {
382 struct drm_surface *surf = lookup_drm_surface(surface);
383 if (!_eglIsSurfaceBound(&surf->base)) {
384 if (surf->screen)
385 drm_takedown_shown_screen(dpy, surf->screen);
386 st_unreference_framebuffer(surf->stfb);
387 free(surf);
388 }
389 return EGL_TRUE;
390 }
391
392 /**
393 * Called by libEGL's eglSwapBuffers().
394 */
395 EGLBoolean
396 drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw)
397 {
398 struct drm_device *dev = lookup_drm_device(dpy);
399 struct drm_surface *surf = lookup_drm_surface(draw);
400 struct pipe_surface *back_surf;
401
402 if (!surf)
403 return EGL_FALSE;
404
405 st_get_framebuffer_surface(surf->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
406
407 if (back_surf) {
408 struct drm_context *ctx = lookup_drm_context(draw->Binding);
409
410 st_notify_swapbuffers(surf->stfb);
411
412 if (ctx && surf->screen) {
413 if (ctx->pipe->surface_copy) {
414 ctx->pipe->surface_copy(ctx->pipe,
415 surf->screen->surface,
416 0, 0,
417 back_surf,
418 0, 0,
419 surf->w, surf->h);
420 } else {
421 util_surface_copy(ctx->pipe, FALSE,
422 surf->screen->surface,
423 0, 0,
424 back_surf,
425 0, 0,
426 surf->w, surf->h);
427 }
428 ctx->pipe->flush(ctx->pipe, PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_TEXTURE_CACHE, NULL);
429
430 #ifdef DRM_MODE_FEATURE_DIRTYFB
431 /* TODO query connector property to see if this is needed */
432 drmModeDirtyFB(dev->drmFD, surf->screen->fbID, NULL, 0);
433 #else
434 (void)dev;
435 #endif
436
437 /* TODO more stuff here */
438 }
439 }
440
441 return EGL_TRUE;
442 }