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"
19 static drmModeModeInfoPtr
20 drm_find_mode(drmModeConnectorPtr connector
, _EGLMode
*mode
)
23 drmModeModeInfoPtr m
= NULL
;
25 for (i
= 0; i
< connector
->count_modes
; i
++) {
26 m
= &connector
->modes
[i
];
27 if (m
->hdisplay
== mode
->Width
&& m
->vdisplay
== mode
->Height
&& m
->vrefresh
== mode
->RefreshRate
)
29 m
= &connector
->modes
[0]; /* if we can't find one, return first */
35 static struct st_framebuffer
*
36 drm_create_framebuffer(const __GLcontextModes
*visual
,
41 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
43 if (visual
->redBits
== 5)
44 colorFormat
= PIPE_FORMAT_R5G6B5_UNORM
;
46 colorFormat
= PIPE_FORMAT_A8R8G8B8_UNORM
;
48 if (visual
->depthBits
== 16)
49 depthFormat
= PIPE_FORMAT_Z16_UNORM
;
50 else if (visual
->depthBits
== 24)
51 depthFormat
= PIPE_FORMAT_S8Z24_UNORM
;
53 depthFormat
= PIPE_FORMAT_NONE
;
55 if (visual
->stencilBits
== 8)
56 stencilFormat
= PIPE_FORMAT_S8Z24_UNORM
;
58 stencilFormat
= PIPE_FORMAT_NONE
;
60 return st_create_framebuffer(visual
,
70 drm_create_texture(_EGLDisplay
*dpy
,
71 struct drm_screen
*scrn
,
72 unsigned w
, unsigned h
)
74 struct drm_device
*dev
= lookup_drm_device(dpy
);
75 struct pipe_screen
*screen
= dev
->screen
;
76 struct pipe_surface
*surface
;
77 struct pipe_texture
*texture
;
78 struct pipe_texture templat
;
79 struct pipe_buffer
*buf
= NULL
;
82 memset(&templat
, 0, sizeof(templat
));
83 templat
.tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
84 templat
.tex_usage
|= PIPE_TEXTURE_USAGE_PRIMARY
;
85 templat
.target
= PIPE_TEXTURE_2D
;
86 templat
.last_level
= 0;
88 templat
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
90 templat
.height
[0] = h
;
91 pf_get_block(templat
.format
, &templat
.block
);
93 texture
= screen
->texture_create(dev
->screen
,
99 surface
= screen
->get_tex_surface(screen
,
104 PIPE_BUFFER_USAGE_GPU_WRITE
);
110 scrn
->surface
= surface
;
111 scrn
->front
.width
= w
;
112 scrn
->front
.height
= h
;
113 scrn
->front
.pitch
= pitch
;
114 dev
->api
->local_handle_from_texture(dev
->api
, screen
, texture
,
115 &scrn
->front
.pitch
, &scrn
->front
.handle
);
122 pipe_surface_reference(&surface
, NULL
);
124 pipe_texture_reference(&texture
, NULL
);
126 pipe_buffer_reference(&buf
, NULL
);
135 drm_takedown_shown_screen(_EGLDisplay
*dpy
, struct drm_screen
*screen
)
137 struct drm_device
*dev
= lookup_drm_device(dpy
);
146 NULL
, 0, // List of output ids
149 drmModeRmFB(dev
->drmFD
, screen
->fbID
);
150 drmModeFreeFB(screen
->fb
);
153 pipe_surface_reference(&screen
->surface
, NULL
);
154 pipe_texture_reference(&screen
->tex
, NULL
);
160 drm_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
, NativeWindowType window
, const EGLint
*attrib_list
)
167 drm_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
, NativePixmapType pixmap
, const EGLint
*attrib_list
)
174 drm_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
175 const EGLint
*attrib_list
)
180 struct drm_surface
*surf
= NULL
;
181 __GLcontextModes
*visual
;
183 for (i
= 0; attrib_list
&& attrib_list
[i
] != EGL_NONE
; i
++) {
184 switch (attrib_list
[i
]) {
186 width
= attrib_list
[++i
];
189 height
= attrib_list
[++i
];
192 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreatePbufferSurface");
193 return EGL_NO_SURFACE
;
197 if (width
< 1 || height
< 1) {
198 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreatePbufferSurface");
202 surf
= (struct drm_surface
*) calloc(1, sizeof(struct drm_surface
));
206 if (!_eglInitSurface(drv
, &surf
->base
, EGL_PBUFFER_BIT
, conf
, attrib_list
))
212 visual
= drm_visual_from_config(conf
);
213 surf
->stfb
= drm_create_framebuffer(visual
,
217 drm_visual_modes_destroy(visual
);
228 drm_create_screen_surface_mesa(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*cfg
,
229 const EGLint
*attrib_list
)
231 EGLSurface surf
= drm_create_pbuffer_surface(drv
, dpy
, cfg
, attrib_list
);
237 drm_show_screen_surface_mesa(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
239 _EGLSurface
*surface
, _EGLMode
*mode
)
241 struct drm_device
*dev
= lookup_drm_device(dpy
);
242 struct drm_surface
*surf
= lookup_drm_surface(surface
);
243 struct drm_screen
*scrn
= lookup_drm_screen(screen
);
248 drm_takedown_shown_screen(dpy
, scrn
);
251 drm_create_texture(dpy
, scrn
, mode
->Width
, mode
->Height
);
255 ret
= drmModeAddFB(dev
->drmFD
,
256 scrn
->front
.width
, scrn
->front
.height
,
257 32, 32, scrn
->front
.pitch
,
264 scrn
->fb
= drmModeGetFB(dev
->drmFD
, scrn
->fbID
);
268 /* find a fitting crtc */
270 drmModeConnector
*con
= scrn
->connector
;
272 scrn
->mode
= drm_find_mode(con
, mode
);
276 for (k
= 0; k
< con
->count_encoders
; k
++) {
277 drmModeEncoder
*enc
= drmModeGetEncoder(dev
->drmFD
, con
->encoders
[k
]);
278 for (i
= 0; i
< dev
->res
->count_crtcs
; i
++) {
279 if (enc
->possible_crtcs
& (1<<i
)) {
281 scrn
->crtcID
= dev
->res
->crtcs
[i
];
284 i
= dev
->res
->count_crtcs
;
285 k
= dev
->res
->count_encoders
;
288 drmModeFreeEncoder(enc
);
292 ret
= drmModeSetCrtc(dev
->drmFD
,
296 &scrn
->connectorID
, 1,
304 drmModeConnectorSetProperty(dev
->drmFD
,
320 drmModeRmFB(dev
->drmFD
, scrn
->fbID
);
321 drmModeFreeFB(scrn
->fb
);
325 pipe_surface_reference(&scrn
->surface
, NULL
);
326 pipe_texture_reference(&scrn
->tex
, NULL
);
333 drm_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surface
)
335 struct drm_surface
*surf
= lookup_drm_surface(surface
);
336 if (!_eglIsSurfaceBound(&surf
->base
)) {
338 drm_takedown_shown_screen(dpy
, surf
->screen
);
339 st_unreference_framebuffer(surf
->stfb
);
346 drm_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*draw
)
348 struct drm_device
*dev
= lookup_drm_device(dpy
);
349 struct drm_surface
*surf
= lookup_drm_surface(draw
);
350 struct pipe_surface
*back_surf
;
356 if (!_eglSwapBuffers(drv
, dpy
, draw
))
359 st_get_framebuffer_surface(surf
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
363 st_notify_swapbuffers(surf
->stfb
);
366 surf
->user
->pipe
->surface_copy(surf
->user
->pipe
,
367 surf
->screen
->surface
,
372 surf
->user
->pipe
->flush(surf
->user
->pipe
, PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_TEXTURE_CACHE
, NULL
);
374 #ifdef DRM_MODE_FEATURE_DIRTYFB
375 /* TODO query connector property to see if this is needed */
376 drmModeDirtyFB(dev
->drmFD
, surf
->screen
->fbID
, NULL
, 0);
381 /* TODO more stuff here */