Z-Pocket Game Pro(ZPG Pro)

libdrm page-flip


參考資訊:
1. hexiaolong2009

main.c

#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
 
struct fb_obj {
  uint32_t width;
  uint32_t height;
  uint32_t pitch;
  uint32_t handle;
  uint32_t size;
  uint32_t *vaddr;
  uint32_t fb_id;
};
 
static int terminate=0;
struct fb_obj fb[2]={0};
 
static int modeset_create_fb(int fd, struct fb_obj *bo, uint32_t color)
{
  uint32_t i=0;
  struct drm_mode_map_dumb map={0};
  struct drm_mode_create_dumb create={0};
 
  create.width = bo->width;
  create.height = bo->height;
  create.bpp = 32;
  drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
  bo->pitch = create.pitch;
  bo->size = create.size;
  bo->handle = create.handle;
  drmModeAddFB(fd, bo->width, bo->height, 24, 32, bo->pitch, bo->handle, &bo->fb_id);
  map.handle = create.handle;
  drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
  bo->vaddr = mmap(0, create.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, map.offset);
  for(i=0; i<(bo->size/4); i++){
    bo->vaddr[i] = color;
  }
  return 0;
}
 
static void modeset_destroy_fb(int fd, struct fb_obj *bo)
{
  struct drm_mode_destroy_dumb destroy={0};
 
  drmModeRmFB(fd, bo->fb_id);
  munmap(bo->vaddr, bo->size);
  destroy.handle = bo->handle;
  drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy);
}
 
static void modeset_page_flip_handler(int fd, uint32_t frame, uint32_t sec, uint32_t usec, void *data)
{
  static int i=0;
  uint32_t crtc_id=*(uint32_t*)data;
 
  i^= 1;
  drmModePageFlip(fd, crtc_id, fb[i].fb_id, DRM_MODE_PAGE_FLIP_EVENT, data);
  usleep(500000);
}
 
static void sigint_handler(int arg)
{
  terminate = 1;
}
 
int main(int argc, char **argv)
{
  int fd=-1;
  uint32_t conn_id=-1;
  uint32_t crtc_id=-1;
  drmModeRes *res=NULL;
  drmEventContext ev={0};
  drmModeConnector *conn=NULL;
 
  signal(SIGINT, sigint_handler);
  ev.version = DRM_EVENT_CONTEXT_VERSION;
  ev.page_flip_handler = modeset_page_flip_handler;
  fd = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);
  res = drmModeGetResources(fd);
  crtc_id = res->crtcs[0];
  conn_id = res->connectors[0];
  conn = drmModeGetConnector(fd, conn_id);
  fb[0].width = conn->modes[0].hdisplay;
  fb[0].height = conn->modes[0].vdisplay;
  fb[1].width = conn->modes[0].hdisplay;
  fb[1].height = conn->modes[0].vdisplay;
  modeset_create_fb(fd, &fb[0], 0xff0000);
  modeset_create_fb(fd, &fb[1], 0x0000ff);
  drmModeSetCrtc(fd, crtc_id, fb[0].fb_id, 0, 0, &conn_id, 1, &conn->modes[0]);
  drmModePageFlip(fd, crtc_id, fb[0].fb_id, DRM_MODE_PAGE_FLIP_EVENT, &crtc_id);
  while(!terminate){
    drmHandleEvent(fd, &ev);
  }
  modeset_destroy_fb(fd, &fb[1]);
  modeset_destroy_fb(fd, &fb[0]);
  drmModeFreeConnector(conn);
  drmModeFreeResources(res);
  close(fd);
  return 0;
}

編譯

$ sudo apt-get install libdrm-dev -y
$ gcc main.c -o test -I/usr/include/libdrm -ldrm


返回上一頁