Subversion Repositories Kolibri OS

Rev

Rev 4367 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1.  
  2. // -kr -i4 -ts4 -bls -bl -bli0
  3.  
  4. #include <stdio.h>
  5. #include <malloc.h>
  6. #include <stdbool.h>
  7. #include <pixlib2.h>
  8. #include <kos32sys.h>
  9.  
  10.  
  11. #define DISPLAY_VERSION         0x0200  /*      2.00     */
  12.  
  13. #define SRV_GETVERSION              0
  14. #define SRV_GET_CAPS                3
  15.  
  16.  
  17. #define BUFFER_SIZE(n) ((n)*sizeof(uint32_t))
  18. #define __ALIGN_MASK(x,mask)  (((x)+(mask))&~(mask))
  19. #define ALIGN(x,a)            __ALIGN_MASK(x,(typeof(x))(a)-1)
  20.  
  21. #define to_surface(x) (surface_t*)((x)->handle)
  22.  
  23. typedef struct
  24. {
  25.         uint32_t width;
  26.         uint32_t height;
  27.         void *data;
  28.         uint32_t pitch;
  29.         uint32_t bo;
  30.         uint32_t bo_size;
  31.         uint32_t flags;
  32. } surface_t;
  33.  
  34.  
  35. int uxa_init(uint32_t service);
  36. void uxa_fini();
  37.  
  38. int sna_create_bitmap(bitmap_t * bitmap);
  39. int sna_destroy_bitmap(bitmap_t * bitmap);
  40. int sna_lock_bitmap(bitmap_t * bitmap);
  41. int sna_resize_bitmap(bitmap_t *bitmap);
  42. //int sna_blit_copy(bitmap_t * src_bitmap, int dst_x, int dst_y,
  43. //                                int w, int h, int src_x, int src_y);
  44. int sna_blit_tex(bitmap_t * src_bitmap, bool scale, int dst_x, int dst_y,
  45.                                  int w, int h, int src_x, int src_y);
  46.  
  47.  
  48. static uint32_t service;
  49. static uint32_t hw_caps;
  50.  
  51.  
  52. uint32_t init_pixlib(uint32_t caps)
  53. {
  54.         uint32_t api_version;
  55.         ioctl_t io;
  56.  
  57.         if (service != 0)
  58.                 return caps & hw_caps;
  59.  
  60. #ifndef BUILD_EBOX
  61.         service = get_service("DISPLAY");
  62.         if (service == 0)
  63.                 goto fail;
  64.  
  65.         io.handle = service;
  66.         io.io_code = SRV_GETVERSION;
  67.         io.input = NULL;
  68.         io.inp_size = 0;
  69.         io.output = &api_version;
  70.         io.out_size = BUFFER_SIZE(1);
  71.  
  72.         if (call_service(&io) != 0)
  73.                 goto fail;
  74.  
  75.         if ((DISPLAY_VERSION > (api_version & 0xFFFF)) ||
  76.                 (DISPLAY_VERSION < (api_version >> 16)))
  77.                 goto fail;
  78.  
  79.         hw_caps = uxa_init(service);
  80.  
  81.         if (hw_caps)
  82.                 printf("2D caps %s%s%s\n",
  83.                            (hw_caps & HW_BIT_BLIT) != 0 ? "HW_BIT_BLIT " : "",
  84.                            (hw_caps & HW_TEX_BLIT) != 0 ? "HW_TEX_BLIT " : "",
  85.                            (hw_caps & HW_VID_BLIT) != 0 ? "HW_VID_BLIT " : "");
  86.  
  87.         return caps & hw_caps;
  88. #endif
  89.  
  90.   fail:
  91.         service = 0;
  92.         return 0;
  93. };
  94.  
  95. void done_pixlib()
  96. {
  97. //      if (hw_caps != 0)
  98. //              uxa_fini();
  99. };
  100.  
  101.  
  102. int create_bitmap(bitmap_t * bitmap)
  103. {
  104.         uint32_t size, bo_size;
  105.         uint32_t pitch, max_pitch;
  106.         void *buffer;
  107.         surface_t *sf;
  108.  
  109.         bitmap->handle = -1;
  110.         bitmap->data = (void *) -1;
  111.         bitmap->pitch = -1;
  112.  
  113. //      if (bitmap->flags &= hw_caps)
  114. //              return sna_create_bitmap(bitmap);
  115.  
  116.         pitch = ALIGN(bitmap->width * 4, 16);
  117.         max_pitch = ALIGN(bitmap->max_width * 4, 16);
  118.  
  119.         size = ALIGN(pitch * bitmap->height, 4096);
  120.         bo_size = ALIGN(max_pitch * bitmap->max_height, 4096);
  121.  
  122.         if (bo_size < size)
  123.                 bo_size = size;
  124.  
  125.         sf = malloc(sizeof(*sf));
  126.         if (sf == NULL)
  127.                 return -1;
  128.  
  129.         buffer = user_alloc(bo_size);
  130.  
  131.         if (buffer == NULL)
  132.         {
  133.                 free(sf);
  134.                 return -1;
  135.         };
  136.  
  137.         sf->width = bitmap->width;
  138.         sf->height = bitmap->height;
  139.         sf->data = buffer;
  140.         sf->pitch = pitch;
  141.         sf->bo = 0;
  142.         sf->bo_size = bo_size;
  143.         sf->flags = bitmap->flags;
  144.  
  145.         bitmap->handle = (uint32_t) sf;
  146.  
  147. //    printf("create bitmap %p handle %p data %p  w %d h%d\n",
  148. //            bitmap, bitmap->handle, bitmap->data, bitmap->width, bitmap->height);
  149.  
  150.         return 0;
  151. };
  152.  
  153. int destroy_bitmap(bitmap_t * bitmap)
  154. {
  155.         surface_t *sf = to_surface(bitmap);
  156.  
  157. //      if (sf->flags & hw_caps)
  158. //              return sna_destroy_bitmap(bitmap);
  159.  
  160.         user_free(sf->data);
  161.         free(sf);
  162.  
  163.         bitmap->handle = -1;
  164.         bitmap->data = (void *) -1;
  165.         bitmap->pitch = -1;
  166.  
  167.         return 0;
  168. };
  169.  
  170. int lock_bitmap(bitmap_t * bitmap)
  171. {
  172.         surface_t *sf = to_surface(bitmap);
  173.  
  174.         if (bitmap->data != (void *) -1)
  175.                 return 0;
  176.  
  177. //      if (sf->flags & hw_caps)
  178. //              return sna_lock_bitmap(bitmap);
  179.  
  180.         bitmap->data = sf->data;
  181.         bitmap->pitch = sf->pitch;
  182.  
  183.         return 0;
  184. };
  185.  
  186. int blit_bitmap(bitmap_t * bitmap, int dst_x, int dst_y,
  187.                 int w, int h, int src_x, int src_y)
  188. {
  189.         struct blit_call bc;
  190.         int ret;
  191.  
  192.         surface_t *sf = to_surface(bitmap);
  193.  
  194. //      if (sf->flags & hw_caps & HW_BIT_BLIT)
  195. //              return sna_blit_tex(bitmap, false, dst_x, dst_y, w, h, src_x, src_y);
  196.  
  197.         bc.dstx     = dst_x;
  198.         bc.dsty     = dst_y;
  199.         bc.w        = w;
  200.         bc.h        = h;
  201.         bc.srcx     = 0;
  202.         bc.srcy     = 0;
  203.         bc.srcw     = w;
  204.         bc.srch     = h;
  205.         bc.stride   = sf->pitch;
  206.         bc.bitmap   = sf->data;
  207.  
  208.         __asm__ __volatile__(
  209.     "int $0x40":"=a"(ret):"a"(73), "b"(0x00),
  210.         "c"(&bc):"memory");
  211.  
  212.         bitmap->data = (void *) -1;
  213.         bitmap->pitch = -1;
  214.  
  215.         return ret;
  216. };
  217.  
  218. int fplay_blit_bitmap(bitmap_t * bitmap, int dst_x, int dst_y, int w, int h)
  219. {
  220.         struct blit_call bc;
  221.         int ret;
  222.  
  223.         surface_t *sf = to_surface(bitmap);
  224.  
  225. //      if (sf->flags & hw_caps & HW_TEX_BLIT)
  226. //              return sna_blit_tex(bitmap, true, dst_x, dst_y, w, h, 0, 0);
  227.  
  228.         bc.dstx = dst_x;
  229.         bc.dsty = dst_y;
  230.         bc.w = w;
  231.         bc.h = h;
  232.         bc.srcx = 0;
  233.         bc.srcy = 0;
  234.         bc.srcw = w;
  235.         bc.srch = h;
  236.         bc.stride = sf->pitch;
  237.         bc.bitmap = sf->data;
  238.  
  239.         __asm__ __volatile__(
  240.     "int $0x40":"=a"(ret):"a"(73), "b"(0x00),
  241.         "c"(&bc):"memory");
  242.  
  243.         bitmap->data = (void *) -1;
  244.         bitmap->pitch = -1;
  245.  
  246.         return ret;
  247. };
  248.  
  249. int resize_bitmap(bitmap_t * bitmap)
  250. {
  251.         uint32_t size;
  252.         uint32_t pitch;
  253.  
  254. //    printf("%s\n", __FUNCTION__);
  255.  
  256.         surface_t *sf = to_surface(bitmap);
  257.  
  258. //      if (sf->flags & hw_caps)
  259. //      {
  260. //              return sna_resize_bitmap(bitmap);
  261. //      };
  262.  
  263.         pitch = ALIGN(bitmap->width * 4, 16);
  264.         size = ALIGN(pitch * bitmap->height, 4096);
  265.  
  266.         bitmap->pitch = -1;
  267.         bitmap->data = (void *) -1;
  268.  
  269.         if (size > sf->bo_size)
  270.         {
  271.                 sf->data = user_realloc(sf->data, size);        /* grow buffer */
  272.                 if (sf->data == NULL)
  273.                         return -1;
  274.  
  275.                 sf->bo_size = size;
  276.         } else if (size < sf->bo_size)
  277.                 user_unmap(sf->data, size, sf->bo_size - size); /* unmap unused pages */
  278.  
  279.         sf->width  = bitmap->width;
  280.         sf->height = bitmap->height;
  281.         sf->pitch  = pitch;
  282.  
  283.         return 0;
  284. };
  285.  
  286. int sna_create_mask()
  287. {
  288.     return 0;
  289. };
  290.