Advertisement
Guest User

(drm test)main_c

a guest
Apr 7th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <fcntl.h>
  5. #include <sys/mman.h>
  6. #include <sys/ioctl.h>
  7. #include <drm/drm.h>
  8. #include <drm/drm_mode.h>
  9.  
  10. static int dri_fd = 0;
  11.  
  12. static int deskenv_finddri(void);
  13.  
  14. int main(int argc, char **argv)
  15. {
  16.     (void)argc;
  17.     (void)argv;
  18.  
  19.     if (deskenv_finddri() == 1)
  20.         return 1;
  21.  
  22.     (void)argc;
  23.     (void)argv;
  24.  
  25.     if (init_drm() == 1)
  26.         return 1;
  27. //------------------------------------------------------------------------------
  28. //Kernel Mode Setting (KMS)
  29. //------------------------------------------------------------------------------
  30.  
  31.     uint64_t res_fb_buf[10]={0},
  32.             res_crtc_buf[10]={0},
  33.             res_conn_buf[10]={0},
  34.             res_enc_buf[10]={0};
  35.  
  36.     struct drm_mode_card_res res={0};
  37.  
  38.     //Become the "master" of the DRI device
  39.     ioctl(dri_fd, DRM_IOCTL_SET_MASTER, 0);
  40.  
  41.     //Get resource counts
  42.     ioctl(dri_fd, DRM_IOCTL_MODE_GETRESOURCES, &res);
  43.     res_fb_buf = (uint64_t)res.fb_id_ptr;
  44.     res_crtc_buf = (uint64_t)res.crtc_id_ptr;
  45.     res_conn_buf = (uint64_t)res.connector_id_ptr;
  46.     res_enc_buf = (uint64_t)res.encoder_id_ptr;
  47.     //Get resource IDs
  48.     ioctl(dri_fd, DRM_IOCTL_MODE_GETRESOURCES, &res);
  49.  
  50.     printf("fb: %d, crtc: %d, conn: %d, enc: %d\n",res.count_fbs,res.count_crtcs,res.count_connectors,res.count_encoders);
  51.  
  52.     void *fb_base[10];
  53.     long fb_w[10];
  54.     long fb_h[10];
  55.  
  56.     //Loop though all available connectors
  57.     int i;
  58.     for (i=0;i<res.count_connectors;i++)
  59.     {
  60.         struct drm_mode_modeinfo conn_mode_buf[20]={0};
  61.         uint64_t    conn_prop_buf[20]={0},
  62.                     conn_propval_buf[20]={0},
  63.                     conn_enc_buf[20]={0};
  64.  
  65.         struct drm_mode_get_connector conn={0};
  66.  
  67.         conn.connector_id=res_conn_buf[i];
  68.  
  69.         ioctl(dri_fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn);  //get connector resource counts
  70.         conn.modes_ptr=(uint64_t)conn_mode_buf;
  71.         conn.props_ptr=(uint64_t)conn_prop_buf;
  72.         conn.prop_values_ptr=(uint64_t)conn_propval_buf;
  73.         conn.encoders_ptr=(uint64_t)conn_enc_buf;
  74.         ioctl(dri_fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn);  //get connector resources
  75.  
  76.         //Check if the connector is OK to use (connected to something)
  77.         if (conn.count_encoders<1 || conn.count_modes<1 || !conn.encoder_id || !conn.connection)
  78.         {
  79.             printf("Not connected\n");
  80.             continue;
  81.         }
  82.  
  83. //------------------------------------------------------------------------------
  84. //Creating a dumb buffer
  85. //------------------------------------------------------------------------------
  86.         struct drm_mode_create_dumb create_dumb={0};
  87.         struct drm_mode_map_dumb map_dumb={0};
  88.         struct drm_mode_fb_cmd cmd_dumb={0};
  89.  
  90.         //If we create the buffer later, we can get the size of the screen first.
  91.         //This must be a valid mode, so it's probably best to do this after we find
  92.         //a valid crtc with modes.
  93.         create_dumb.width = conn_mode_buf[i].hdisplay;
  94.         create_dumb.height = conn_mode_buf[i].vdisplay;
  95.         create_dumb.bpp = 32;
  96.         create_dumb.flags = 0;
  97.         create_dumb.pitch = 0;
  98.         create_dumb.size = 0;
  99.         create_dumb.handle = 0;
  100.         ioctl(dri_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
  101.  
  102.         cmd_dumb.width=create_dumb.width;
  103.         cmd_dumb.height=create_dumb.height;
  104.         cmd_dumb.bpp=create_dumb.bpp;
  105.         cmd_dumb.pitch=create_dumb.pitch;
  106.         cmd_dumb.depth=24;
  107.         cmd_dumb.handle=create_dumb.handle;
  108.         ioctl(dri_fd,DRM_IOCTL_MODE_ADDFB,&cmd_dumb);
  109.  
  110.         map_dumb.handle=create_dumb.handle;
  111.         ioctl(dri_fd,DRM_IOCTL_MODE_MAP_DUMB,&map_dumb);
  112.  
  113.         fb_base[i] = mmap(0, create_dumb.size, PROT_READ | PROT_WRITE, MAP_SHARED, dri_fd, map_dumb.offset);
  114.         fb_w[i]=create_dumb.width;
  115.         fb_h[i]=create_dumb.height;
  116.  
  117. //------------------------------------------------------------------------------
  118. //Kernel Mode Setting (KMS)
  119. //------------------------------------------------------------------------------
  120.  
  121.         printf("%d : mode: %d, prop: %d, enc: %d\n",conn.connection,conn.count_modes,conn.count_props,conn.count_encoders);
  122.         printf("modes: %dx%d FB: %d\n",conn_mode_buf[i].hdisplay,conn_mode_buf[i].vdisplay,fb_base[i]);
  123.  
  124.         struct drm_mode_get_encoder enc={0};
  125.  
  126.         enc.encoder_id=conn.encoder_id;
  127.         ioctl(dri_fd, DRM_IOCTL_MODE_GETENCODER, &enc); //get encoder
  128.  
  129.         struct drm_mode_crtc crtc={0};
  130.  
  131.         crtc.crtc_id=enc.crtc_id;
  132.         ioctl(dri_fd, DRM_IOCTL_MODE_GETCRTC, &crtc);
  133.  
  134.         crtc.fb_id=cmd_dumb.fb_id;
  135.         crtc.set_connectors_ptr=(uint64_t)&res_conn_buf[i];
  136.         crtc.count_connectors=1;
  137.         crtc.mode=conn_mode_buf[i];
  138.         crtc.mode_valid=1;
  139.         ioctl(dri_fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
  140.     }
  141.  
  142.     //Stop being the "master" of the DRI device
  143.     ioctl(dri_fd, DRM_IOCTL_DROP_MASTER, 0);
  144.  
  145.     while (1) {
  146.         int x,y;
  147.         for (i=0;i<100;i++)
  148.         {
  149.             int j;
  150.             for (j=0;j<res.count_connectors;j++)
  151.             {
  152.                 int col=(rand()%0x00ffffff)&0x00ff00ff;
  153.                 for (y=0;y<fb_h[j];y++)
  154.                     for (x=0;x<fb_w[j];x++)
  155.                     {
  156.                         int location=y*(fb_w[j]) + x;
  157.                         *(((uint32_t*)fb_base[j])+location)=col;
  158.                     }
  159.             }
  160.            
  161.         }
  162.     }  
  163.     return 0;
  164. }
  165.  
  166. static int deskenv_finddri(void)
  167. {
  168.     if (dri_fd == 0)
  169.         dri_fd  = open("/dev/dri/card0", O_RDWR | FD_CLOEXEC);
  170.    
  171.     if (dri_fd == 0)
  172.         return 1;
  173.    
  174.     return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement