Subversion Repositories Kolibri OS

Rev

Rev 3068 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. #include <stdio.h>
  3. #include <pixlib2.h>
  4. #include "system.h"
  5.  
  6.  
  7. #define DISPLAY_VERSION     0x0200     /*      2.00     */
  8.  
  9. #define SRV_GETVERSION       0
  10. #define SRV_GET_CAPS         3
  11.  
  12. #define SRV_CREATE_SURFACE  10
  13. #define SRV_DESTROY_SURFACE     11
  14. #define SRV_LOCK_SURFACE    12
  15. #define SRV_UNLOCK_SURFACE      13
  16. #define SRV_RESIZE_SURFACE      14
  17. #define SRV_BLIT_BITMAP         15
  18. #define SRV_BLIT_TEXTURE        16
  19. #define SRV_BLIT_VIDEO          17
  20.  
  21.  
  22. #define BUFFER_SIZE(n) ((n)*sizeof(uint32_t))
  23. #define __ALIGN_MASK(x,mask)  (((x)+(mask))&~(mask))
  24. #define ALIGN(x,a)            __ALIGN_MASK(x,(typeof(x))(a)-1)
  25.  
  26.  
  27.  
  28. static uint32_t service;
  29. static uint32_t blit_caps;
  30. static uint32_t screen_width;
  31. static uint32_t screen_height;
  32.  
  33. typedef struct
  34. {
  35.   unsigned      handle;
  36.   unsigned      io_code;
  37.   void          *input;
  38.   int           inp_size;
  39.   void          *output;
  40.   int           out_size;
  41. }ioctl_t;
  42.  
  43.  
  44.  
  45. typedef struct
  46. {
  47.     uint32_t  idx;
  48.     union
  49.     {
  50.         uint32_t opt[2];
  51.         struct {
  52.             uint32_t max_tex_width;
  53.             uint32_t max_tex_height;
  54.         }cap1;
  55.     };
  56. }hwcaps_t;
  57.  
  58. static inline uint32_t GetScreenSize()
  59. {
  60.      uint32_t retval;
  61.  
  62.      __asm__ __volatile__(
  63.      "int $0x40"
  64.      :"=a"(retval)
  65.      :"a"(61), "b"(1));
  66.      return retval;
  67. }
  68.  
  69. static uint32_t get_service(char *name)
  70. {
  71.   uint32_t retval = 0;
  72.   asm volatile ("int $0x40"
  73.       :"=a"(retval)
  74.       :"a"(68),"b"(16),"c"(name)
  75.       :"memory");
  76.  
  77.   return retval;
  78. };
  79.  
  80. static int call_service(ioctl_t *io)
  81. {
  82.   int retval;
  83.  
  84.   asm volatile("int $0x40"
  85.       :"=a"(retval)
  86.       :"a"(68),"b"(17),"c"(io)
  87.       :"memory","cc");
  88.  
  89.   return retval;
  90. };
  91.  
  92.  
  93. uint32_t init_pixlib(uint32_t caps)
  94. {
  95.     uint32_t  api_version;
  96.     uint32_t  screensize;
  97.     hwcaps_t  hwcaps;
  98.     ioctl_t   io;
  99.  
  100.  //   __asm__ __volatile__("int3");
  101.  
  102.     screensize    = GetScreenSize();
  103.     screen_width  = screensize >> 16;
  104.     screen_height = screensize & 0xFFFF;
  105.  
  106.     service = get_service("DISPLAY");
  107.     if(service == 0)
  108.         goto fail;
  109.  
  110.     io.handle   = service;
  111.     io.io_code  = SRV_GETVERSION;
  112.     io.input    = NULL;
  113.     io.inp_size = 0;
  114.     io.output   = &api_version;
  115.     io.out_size = BUFFER_SIZE(1);
  116.  
  117.     if (call_service(&io)!=0)
  118.         goto fail;
  119.  
  120.     if( (DISPLAY_VERSION > (api_version & 0xFFFF)) ||
  121.         (DISPLAY_VERSION < (api_version >> 16)))
  122.         goto fail;
  123.  
  124. /*
  125.  * Let's see what this service can do
  126. */
  127.     hwcaps.idx  = 0;
  128.  
  129.     io.handle   = service;
  130.     io.io_code  = SRV_GET_CAPS;
  131.     io.input    = &hwcaps;
  132.     io.inp_size = sizeof(hwcaps);
  133.     io.output   = NULL;
  134.     io.out_size = 0;
  135.  
  136.     if (call_service(&io)!=0)
  137.         goto fail;
  138.  
  139.     blit_caps = hwcaps.opt[0];
  140.  
  141.     printf("\nDISPLAY service handle %x\n", service);
  142.  
  143.     if( blit_caps )
  144.         printf("service caps %s%s%s\n",
  145.                (blit_caps & HW_BIT_BLIT) != 0 ?"HW_BIT_BLIT ":"",
  146.                (blit_caps & HW_TEX_BLIT) != 0 ?"HW_TEX_BLIT ":"",
  147.                (blit_caps & HW_VID_BLIT) != 0 ?"HW_VID_BLIT ":"");
  148.  
  149.     blit_caps&= caps;
  150.     return blit_caps;
  151.  
  152. fail:
  153.     service = 0;
  154.     return 0;
  155. };
  156.  
  157.  
  158. int create_bitmap(bitmap_t *bitmap)
  159. {
  160.  //    __asm__ __volatile__("int3");
  161.  
  162.     if( bitmap->flags &&  blit_caps & HW_BIT_BLIT )
  163.     {
  164.         struct __attribute__((packed))  /*     SRV_CREATE_SURFACE    */
  165.         {
  166.             uint32_t  handle;           // ignored
  167.             void      *data;            // ignored
  168.  
  169.             uint32_t  width;
  170.             uint32_t  height;
  171.             uint32_t  pitch;            // ignored
  172.  
  173.             uint32_t  max_width;
  174.             uint32_t  max_height;
  175.             uint32_t  format;           // reserved mbz
  176.         }io_10;
  177.  
  178.         ioctl_t io;
  179.         int     err;
  180.  
  181. //        printf("create bitmap %d x %d\n",
  182. //                bitmap->width, bitmap->height);
  183.  
  184.         io_10.width      = bitmap->width;
  185.         io_10.height     = bitmap->height;
  186.         io_10.max_width  = screen_width;
  187.         io_10.max_height = screen_height;
  188.         io_10.format     = 0;
  189.  
  190.         io.handle   = service;
  191.         io.io_code  = SRV_CREATE_SURFACE;
  192.         io.input    = &io_10;
  193.         io.inp_size = BUFFER_SIZE(8);
  194.         io.output   = NULL;
  195.         io.out_size = 0;
  196.  
  197.         err = call_service(&io);
  198.         if(err==0)
  199.         {
  200.             bitmap->handle = io_10.handle;
  201.             bitmap->pitch  = io_10.pitch;
  202.             bitmap->data   = io_10.data;
  203. //            printf("Create hardware surface %x pitch %d, buffer %x\n",
  204. //                     bitmap->handle, bitmap->pitch, bitmap->data);
  205.             return 0;
  206.         };
  207.     };
  208.  
  209.     uint32_t   size;
  210.     uint32_t   pitch;
  211.     uint8_t   *buffer;
  212.  
  213.     pitch = ALIGN(bitmap->width*4, 16);
  214.     size  = pitch * bitmap->height;
  215.  
  216.     buffer = (uint8_t*)user_alloc(size);
  217.     if( buffer )
  218.     {
  219.         bitmap->handle = 0;
  220.         bitmap->pitch  = pitch;
  221.         bitmap->data   = buffer;
  222.         bitmap->flags  = 0;
  223.         return 0;
  224.     };
  225.  
  226.     printf("Cannot alloc frame buffer\n\r");
  227.  
  228.     return -1;
  229. };
  230.  
  231. int lock_bitmap(bitmap_t *bitmap)
  232. {
  233.  //    __asm__ __volatile__("int3");
  234.     int err = 0;
  235.  
  236.     if( bitmap->flags && blit_caps & HW_BIT_BLIT )
  237.     {
  238.         struct __attribute__((packed))  /*     SRV_LOCK_SURFACE    */
  239.         {
  240.             uint32_t  handle;
  241.             void      *data;
  242.             uint32_t  pitch;
  243.  
  244.         }io_12;
  245.  
  246.         ioctl_t io;
  247.  
  248.         io_12.handle  = bitmap->handle;
  249.         io_12.pitch   = 0;
  250.         io_12.data    = 0;
  251.  
  252.         io.handle   = service;
  253.         io.io_code  = SRV_LOCK_SURFACE;
  254.         io.input    = &io_12;
  255.         io.inp_size = BUFFER_SIZE(3);
  256.         io.output   = NULL;
  257.         io.out_size = 0;
  258.  
  259.         err = call_service(&io);
  260.         if(err==0)
  261.         {
  262.             bitmap->pitch  = io_12.pitch;
  263.             bitmap->data   = io_12.data;
  264. //            printf("Lock hardware surface %x pitch %d, buffer %x\n",
  265. //                     bitmap->handle, bitmap->pitch, bitmap->data);
  266.         };
  267.     };
  268.  
  269.     return err;
  270. };
  271.  
  272. int blit_bitmap(bitmap_t *bitmap, int dst_x, int dst_y,
  273.                 int w, int h)
  274. {
  275.     int err;
  276.  
  277.     if( bitmap->flags && blit_caps & HW_BIT_BLIT )
  278.     {
  279.  
  280. /*
  281.  *   Now you will experience the full power of the dark side...
  282. */
  283.  
  284.         struct __attribute__((packed))
  285.         {
  286.             uint32_t handle;
  287.             int      dst_x;
  288.             int      dst_y;
  289.             int      src_x;
  290.             int      src_y;
  291.             uint32_t w;
  292.             uint32_t h;
  293.         }io_15;
  294.  
  295.         ioctl_t io;
  296.  
  297.         io_15.handle = bitmap->handle;
  298.         io_15.dst_x  = dst_x;
  299.         io_15.dst_y  = dst_y;
  300.         io_15.src_x  = 0;
  301.         io_15.src_y  = 0;
  302.         io_15.w      = w;
  303.         io_15.h      = h;
  304.  
  305.         io.handle    = service;
  306.         io.io_code   = SRV_BLIT_BITMAP;
  307.         io.input     = &io_15;
  308.         io.inp_size  = BUFFER_SIZE(7);
  309.         io.output    = NULL;
  310.         io.out_size  = 0;
  311.  
  312. //        printf("do blit %x pitch %d\n",bitmap->handle,
  313. //                bitmap->pitch);
  314.         err = call_service(&io);
  315.         return err;
  316.     };
  317.  
  318.     struct blit_call bc;
  319.  
  320.     bc.dstx = dst_x;
  321.     bc.dsty = dst_y;
  322.     bc.w    = w;
  323.     bc.h    = h;
  324.     bc.srcx = 0;
  325.     bc.srcy = 0;
  326.     bc.srcw = w;
  327.     bc.srch = h;
  328.     bc.stride = bitmap->pitch;
  329.     bc.bitmap = bitmap->data;
  330.  
  331.     __asm__ __volatile__(
  332.     "int $0x40"
  333.     :"=a"(err)
  334.     :"a"(73),"b"(0x00),"c"(&bc)
  335.     :"memory");
  336.    
  337.     return err;
  338. };
  339.  
  340. int resize_bitmap(bitmap_t *bitmap)
  341. {
  342.  //    __asm__ __volatile__("int3");
  343.  
  344.     if( bitmap->flags && blit_caps & HW_BIT_BLIT )
  345.     {
  346.         struct __attribute__((packed))
  347.         {
  348.             uint32_t  handle;
  349.             char     *data;
  350.             uint32_t  new_w;
  351.             uint32_t  new_h;
  352.             uint32_t  pitch;
  353.         }io_14;
  354.  
  355.         ioctl_t io;
  356.         int     err;
  357.  
  358.         io_14.handle = bitmap->handle;
  359.         io_14.new_w  = bitmap->width;
  360.         io_14.new_h  = bitmap->height;
  361.  
  362.         io.handle    = service;
  363.         io.io_code   = SRV_RESIZE_SURFACE;
  364.         io.input     = &io_14;
  365.         io.inp_size  = BUFFER_SIZE(5);
  366.         io.output    = NULL;
  367.         io.out_size  = 0;
  368.  
  369.         err = call_service(&io);
  370.         if(err==0)
  371.     {
  372.             bitmap->pitch  = io_14.pitch;
  373.             bitmap->data   = io_14.data;
  374.         };
  375.         return err;
  376.     };
  377.  
  378.     uint32_t   size;
  379.     uint32_t   pitch;
  380.     uint8_t   *buffer;
  381.  
  382.     pitch = ALIGN(bitmap->width*4, 16);
  383.     size  = pitch * bitmap->height;
  384.  
  385.     buffer = (uint8_t*)user_realloc(bitmap->data, size);
  386.     if( buffer )
  387.     {
  388.         bitmap->handle = 0;
  389.         bitmap->pitch  = pitch;
  390.         bitmap->data   = buffer;
  391.         return 0;
  392.     };
  393.  
  394.     printf("Cannot realloc frame buffer\n\r");
  395.  
  396.     return -1;
  397. };
  398.  
  399.