5 #include "egl_tracker.h"
9 #include "pipe/p_inlines.h"
10 #include "pipe/p_screen.h"
11 #include "pipe/p_context.h"
13 #include "state_tracker/drm_api.h"
15 #include "util/u_rect.h"
21 static drmModeModeInfoPtr
22 drm_find_mode(drmModeConnectorPtr connector
, _EGLMode
*mode
)
25 drmModeModeInfoPtr m
= NULL
;
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
)
31 m
= &connector
->modes
[0]; /* if we can't find one, return first */
37 static struct st_framebuffer
*
38 drm_create_framebuffer(struct pipe_screen
*screen
,
39 const __GLcontextModes
*visual
,
44 enum pipe_format color_format
, depth_stencil_format
;
45 boolean d_depth_bits_last
;
46 boolean ds_depth_bits_last
;
49 screen
->is_format_supported(screen
, PIPE_FORMAT_X8Z24_UNORM
,
51 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
53 screen
->is_format_supported(screen
, PIPE_FORMAT_S8Z24_UNORM
,
55 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
57 if (visual
->redBits
== 8) {
58 if (visual
->alphaBits
== 8)
59 color_format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
61 color_format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
63 color_format
= PIPE_FORMAT_R5G6B5_UNORM
;
66 switch(visual
->depthBits
) {
69 depth_stencil_format
= PIPE_FORMAT_NONE
;
72 depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
75 if (visual
->stencilBits
== 0) {
76 depth_stencil_format
= (d_depth_bits_last
) ?
77 PIPE_FORMAT_X8Z24_UNORM
:
78 PIPE_FORMAT_Z24X8_UNORM
;
80 depth_stencil_format
= (ds_depth_bits_last
) ?
81 PIPE_FORMAT_S8Z24_UNORM
:
82 PIPE_FORMAT_Z24S8_UNORM
;
86 depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
90 return st_create_framebuffer(visual
,
100 drm_create_texture(_EGLDisplay
*dpy
,
101 struct drm_screen
*scrn
,
102 unsigned w
, unsigned h
)
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
;
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;
118 templat
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
122 texture
= screen
->texture_create(dev
->screen
,
128 surface
= screen
->get_tex_surface(screen
,
133 PIPE_BUFFER_USAGE_GPU_WRITE
);
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
);
151 pipe_surface_reference(&surface
, NULL
);
153 pipe_texture_reference(&texture
, NULL
);
155 pipe_buffer_reference(&buf
, NULL
);
164 drm_takedown_shown_screen(_EGLDisplay
*dpy
, struct drm_screen
*screen
)
166 struct drm_device
*dev
= lookup_drm_device(dpy
);
175 NULL
, 0, // List of output ids
178 drmModeRmFB(dev
->drmFD
, screen
->fbID
);
179 drmModeFreeFB(screen
->fb
);
182 pipe_surface_reference(&screen
->surface
, NULL
);
183 pipe_texture_reference(&screen
->tex
, NULL
);
189 * Called by libEGL's eglCreateWindowSurface().
192 drm_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
, NativeWindowType window
, const EGLint
*attrib_list
)
199 * Called by libEGL's eglCreatePixmapSurface().
202 drm_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
, NativePixmapType pixmap
, const EGLint
*attrib_list
)
209 * Called by libEGL's eglCreatePbufferSurface().
212 drm_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
213 const EGLint
*attrib_list
)
215 struct drm_device
*dev
= lookup_drm_device(dpy
);
219 struct drm_surface
*surf
= NULL
;
220 __GLcontextModes
*visual
;
222 for (i
= 0; attrib_list
&& attrib_list
[i
] != EGL_NONE
; i
++) {
223 switch (attrib_list
[i
]) {
225 width
= attrib_list
[++i
];
228 height
= attrib_list
[++i
];
231 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreatePbufferSurface");
232 return EGL_NO_SURFACE
;
236 if (width
< 1 || height
< 1) {
237 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreatePbufferSurface");
241 surf
= (struct drm_surface
*) calloc(1, sizeof(struct drm_surface
));
245 if (!_eglInitSurface(drv
, &surf
->base
, EGL_PBUFFER_BIT
, conf
, attrib_list
))
251 visual
= drm_visual_from_config(conf
);
252 surf
->stfb
= drm_create_framebuffer(dev
->screen
, visual
,
255 drm_visual_modes_destroy(visual
);
266 * Called by libEGL's eglCreateScreenSurfaceMESA().
269 drm_create_screen_surface_mesa(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*cfg
,
270 const EGLint
*attrib_list
)
272 EGLSurface surf
= drm_create_pbuffer_surface(drv
, dpy
, cfg
, attrib_list
);
278 * Called by libEGL's eglShowScreenSurfaceMESA().
281 drm_show_screen_surface_mesa(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
283 _EGLSurface
*surface
, _EGLMode
*mode
)
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
);
292 drm_takedown_shown_screen(dpy
, scrn
);
295 drm_create_texture(dpy
, scrn
, mode
->Width
, mode
->Height
);
299 ret
= drmModeAddFB(dev
->drmFD
,
300 scrn
->front
.width
, scrn
->front
.height
,
301 32, 32, scrn
->front
.pitch
,
308 scrn
->fb
= drmModeGetFB(dev
->drmFD
, scrn
->fbID
);
312 /* find a fitting crtc */
314 drmModeConnector
*con
= scrn
->connector
;
316 scrn
->mode
= drm_find_mode(con
, mode
);
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
)) {
325 scrn
->crtcID
= dev
->res
->crtcs
[i
];
328 i
= dev
->res
->count_crtcs
;
329 k
= dev
->res
->count_encoders
;
332 drmModeFreeEncoder(enc
);
336 ret
= drmModeSetCrtc(dev
->drmFD
,
340 &scrn
->connectorID
, 1,
348 drmModeConnectorSetProperty(dev
->drmFD
,
364 drmModeRmFB(dev
->drmFD
, scrn
->fbID
);
365 drmModeFreeFB(scrn
->fb
);
369 pipe_surface_reference(&scrn
->surface
, NULL
);
370 pipe_texture_reference(&scrn
->tex
, NULL
);
377 * Called by libEGL's eglDestroySurface().
380 drm_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surface
)
382 struct drm_surface
*surf
= lookup_drm_surface(surface
);
383 if (!_eglIsSurfaceBound(&surf
->base
)) {
385 drm_takedown_shown_screen(dpy
, surf
->screen
);
386 st_unreference_framebuffer(surf
->stfb
);
393 * Called by libEGL's eglSwapBuffers().
396 drm_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*draw
)
398 struct drm_device
*dev
= lookup_drm_device(dpy
);
399 struct drm_surface
*surf
= lookup_drm_surface(draw
);
400 struct pipe_surface
*back_surf
;
405 st_get_framebuffer_surface(surf
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
408 struct drm_context
*ctx
= lookup_drm_context(draw
->Binding
);
410 st_notify_swapbuffers(surf
->stfb
);
412 if (ctx
&& surf
->screen
) {
413 if (ctx
->pipe
->surface_copy
) {
414 ctx
->pipe
->surface_copy(ctx
->pipe
,
415 surf
->screen
->surface
,
421 util_surface_copy(ctx
->pipe
, FALSE
,
422 surf
->screen
->surface
,
428 ctx
->pipe
->flush(ctx
->pipe
, PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_TEXTURE_CACHE
, NULL
);
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);
437 /* TODO more stuff here */