[Dri-users] KMS experiments

Mandeep Singh Baines msb at chromium.org
Wed Feb 29 06:17:18 PST 2012


Léo Gillot-Lamure (leo.gillot at navaati.net) wrote:
> Hi.
> 
> I'm trying to play with KMS using libdrm and libkms.
> 
> With the help of people on #xorg-devel (thanks to them) i wrote the code
> in attachment. It is supposed to display a red square of 100x100 pixels
> at the top left corner of the screen. Of course it does not : when i run
> it, there is actually a mode setting operation done since my tty goes
> black but no red square appears.
> 
> Could somebody check this code to see the obvious errors or
> misunderstanding of the whole thing i must have done ?
> 

Hi Léo,

Have you looked at eglkms.c in mesa/demos?

http://cgit.freedesktop.org/mesa/demos/tree/src/egl/opengl/eglkms.c

Here's an app I wrote (based on eglkms.c) to bounce a red square around the
screen:

http://git.chromium.org/gitweb/?p=chromiumos/platform/drm-tests.git;a=blob;f=src/eglkms.c

But I guess neither of these example mmap a bo or uses libkms.

Another piece of code I've found helpful is the wayland drm compositor:

http://cgit.freedesktop.org/wayland/weston/tree/src/compositor-drm.c

Hope that helps.

Regards,
Mandeep

> Regards,
> 
> Léo Gillot-Lamure.

> #include <stdlib.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <stdio.h>
> #include <stdint.h>
> #include <string.h>
> #include <libkms.h>
> #include <xf86drmMode.h>
> 
> struct kms_bo* create_buffer_object(int drm_fd, uint32_t width, uint32_t height);
> void draw_in_memory(uint8_t (*mmaped_bo)[4], uint32_t width, uint32_t height, uint32_t pitch);
> 
> // build with "cc `pkg-config --libs --cflags libdrm libkms` -Wall -std=c99 kms_test.c", run in a tty
> int main()
> {
>   printf("Start\n");
>   
>   int drm_fd = open("/dev/dri/card0", O_RDWR);
>   if (drm_fd < 0) { printf("Can't open drm"); exit(-1); }
>   printf("DRM opened\n");
>   
>   drmModeRes ressources = *drmModeGetResources(drm_fd);
>   printf("Number of CRTCs : %d\n", ressources.count_crtcs);
>   printf("Number of connectors : %d \n", ressources.count_connectors);
>   
>   drmModeCrtc crtc = *drmModeGetCrtc(drm_fd, ressources.crtcs[0]);
>   uint32_t width = (uint32_t)crtc.mode.hdisplay;
>   uint32_t height = (uint32_t)crtc.mode.vdisplay;
>   printf("Current FB id : %u\n", crtc.buffer_id);
>   printf("Is mode valid : %i\n", crtc.mode_valid);
>   printf("Current definition : %ux%u\n", width, height);
>   printf("Mode name : %s\n", crtc.mode.name);
>   
>   struct kms_bo* bo = create_buffer_object(drm_fd, width, height);
>   
>   uint32_t pitch = 0;
>   kms_bo_get_prop(bo, KMS_PITCH, (uint32_t*)&pitch);
>   printf("My BO pitch : %u\n", pitch);
>   
>   uint8_t (*mmaped_bo)[4] = NULL;
>   if (kms_bo_map(bo, (void**)&mmaped_bo) < 0 || !mmaped_bo) { printf("Can't map KMS BO\n"); exit(-1); }
>   printf("KMS BO mapped\n");
>   draw_in_memory(mmaped_bo, width, height, pitch);
>   if (kms_bo_unmap(bo) < 0) { printf("Can't unmap KMS framebuffer\n"); exit(-1); }
>   printf("KMS Framebuffer unmapped\n");
> 
>   uint32_t bo_handle = 0;
>   kms_bo_get_prop(bo, KMS_HANDLE, (uint32_t*)&bo_handle);
>   printf("My BO handle : %u\n", bo_handle);
>   uint32_t fb_id = 0;
>   drmModeAddFB(drm_fd, width, height, 24, 32, pitch, bo_handle, &fb_id);
>   printf("My FB id : %u\n", fb_id);
>   
>   uint32_t connector = ressources.connectors[0];
>   
>   drmModeSetCrtc(drm_fd, crtc.crtc_id, fb_id, 0, 0, &connector, 1, &crtc.mode);
>   
>   printf("We should be done\n");
>   
>   printf("End\n");
>   return 0;
> }
> 
> 
> struct kms_bo* create_buffer_object(int drm_fd, uint32_t width, uint32_t height)
> {
>   struct kms_driver* driver = NULL;
>   if (kms_create(drm_fd, &driver) < 0 || !driver) { printf("Can't create KMS driver\n"); exit(-1); }
>   printf("Got KMS driver\n");
>   
>   struct kms_bo* bo = NULL;
>   const unsigned attributes[5] = {KMS_WIDTH, width, KMS_HEIGHT, height};
>   if (kms_bo_create(driver, attributes, &bo) < 0 || !bo) { printf("Can't create KMS BO\n"); exit(-1); }
>   printf("Got KMS BO\n");
>   return bo;
> }
> 
> void draw_in_memory(uint8_t (*mmaped_bo)[4], uint32_t width, uint32_t height, uint32_t pitch)
> {  
>   const uint8_t color[] = {0, 255, 0, 0};
>   
>   for (uint8_t (*line)[4] = mmaped_bo; line < &mmaped_bo[pitch*100]; line+=pitch)
>   {
>     //printf("Writing line at relative address  %u\n", line-mmaped_bo);
>     for (uint8_t (*pixel_ptr)[4] = line; pixel_ptr < &line[100]; pixel_ptr++)
>     {
>       //printf("\rWriting pixel at relative address %u", pixel_ptr-mmaped_bo);
>       memcpy(pixel_ptr, color, sizeof(*pixel_ptr));
>     }
>     //printf("\n");
>   }
>   
>   printf("Drawing done\n");
> }

> _______________________________________________
> dri-users mailing list
> dri-users at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/dri-users



More information about the dri-users mailing list