Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | 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.         service = get_service("DISPLAY");
  61.         if (service == 0)
  62.                 goto fail;
  63.  
  64.         io.handle = service;
  65.         io.io_code = SRV_GETVERSION;
  66.         io.input = NULL;
  67.         io.inp_size = 0;
  68.         io.output = &api_version;
  69.         io.out_size = BUFFER_SIZE(1);
  70.  
  71.         if (call_service(&io) != 0)
  72.                 goto fail;
  73.  
  74.         if ((DISPLAY_VERSION > (api_version & 0xFFFF)) ||
  75.                 (DISPLAY_VERSION < (api_version >> 16)))
  76.                 goto fail;
  77.  
  78.         hw_caps = uxa_init(service);
  79.  
  80.         if (hw_caps)
  81.                 printf("2D caps %s%s%s\n",
  82.                            (hw_caps & HW_BIT_BLIT) != 0 ? "HW_BIT_BLIT " : "",
  83.                            (hw_caps & HW_TEX_BLIT) != 0 ? "HW_TEX_BLIT " : "",
  84.                            (hw_caps & HW_VID_BLIT) != 0 ? "HW_VID_BLIT " : "");
  85.  
  86.         return caps & hw_caps;
  87.  
  88.   fail:
  89.         service = 0;
  90.         return 0;
  91. };
  92.  
  93. void done_pixlib()
  94. {
  95. //      if (hw_caps != 0)
  96. //              uxa_fini();
  97. };
  98.  
  99.  
  100. int create_bitmap(bitmap_t * bitmap)
  101. {
  102.         uint32_t size, bo_size;
  103.         uint32_t pitch, max_pitch;
  104.         void *buffer;
  105.         surface_t *sf;
  106.  
  107.         bitmap->handle = -1;
  108.         bitmap->data = (void *) -1;
  109.         bitmap->pitch = -1;
  110.  
  111. //      if (bitmap->flags &= hw_caps)
  112. //              return sna_create_bitmap(bitmap);
  113.  
  114.         pitch = ALIGN(bitmap->width * 4, 16);
  115.         max_pitch = ALIGN(bitmap->max_width * 4, 16);
  116.  
  117.         size = ALIGN(pitch * bitmap->height, 4096);
  118.         bo_size = ALIGN(max_pitch * bitmap->max_height, 4096);
  119.  
  120.         if (bo_size < size)
  121.                 bo_size = size;
  122.  
  123.         sf = malloc(sizeof(*sf));
  124.         if (sf == NULL)
  125.                 return -1;
  126.  
  127.         buffer = user_alloc(bo_size);
  128.  
  129.         if (buffer == NULL)
  130.         {
  131.                 free(sf);
  132.                 return -1;
  133.         };
  134.  
  135.         sf->width = bitmap->width;
  136.         sf->height = bitmap->height;
  137.         sf->data = buffer;
  138.         sf->pitch = pitch;
  139.         sf->bo = 0;
  140.         sf->bo_size = bo_size;
  141.         sf->flags = bitmap->flags;
  142.  
  143.         bitmap->handle = (uint32_t) sf;
  144.  
  145. //    printf("create bitmap %p handle %p data %p  w %d h%d\n",
  146. //            bitmap, bitmap->handle, bitmap->data, bitmap->width, bitmap->height);
  147.  
  148.         return 0;
  149. };
  150.  
  151. int destroy_bitmap(bitmap_t * bitmap)
  152. {
  153.         surface_t *sf = to_surface(bitmap);
  154.  
  155. //      if (sf->flags & hw_caps)
  156. //              return sna_destroy_bitmap(bitmap);
  157.  
  158.         user_free(sf->data);
  159.         free(sf);
  160.  
  161.         bitmap->handle = -1;
  162.         bitmap->data = (void *) -1;
  163.         bitmap->pitch = -1;
  164.  
  165.         return 0;
  166. };
  167.  
  168. int lock_bitmap(bitmap_t * bitmap)
  169. {
  170.         surface_t *sf = to_surface(bitmap);
  171.  
  172.         if (bitmap->data != (void *) -1)
  173.                 return 0;
  174.  
  175. //      if (sf->flags & hw_caps)
  176. //              return sna_lock_bitmap(bitmap);
  177.  
  178.         bitmap->data = sf->data;
  179.         bitmap->pitch = sf->pitch;
  180.  
  181.         return 0;
  182. };
  183.  
  184. int blit_bitmap(bitmap_t * bitmap, int dst_x, int dst_y,
  185.                 int w, int h, int src_x, int src_y)
  186. {
  187.         struct blit_call bc;
  188.         int ret;
  189.  
  190.         surface_t *sf = to_surface(bitmap);
  191.  
  192. //      if (sf->flags & hw_caps & HW_BIT_BLIT)
  193. //              return sna_blit_tex(bitmap, false, dst_x, dst_y, w, h, src_x, src_y);
  194.  
  195.         bc.dstx     = dst_x;
  196.         bc.dsty     = dst_y;
  197.         bc.w        = w;
  198.         bc.h        = h;
  199.         bc.srcx     = 0;
  200.         bc.srcy     = 0;
  201.         bc.srcw     = w;
  202.         bc.srch     = h;
  203.         bc.stride   = sf->pitch;
  204.         bc.bitmap   = sf->data;
  205.  
  206.         __asm__ __volatile__(
  207.     "int $0x40":"=a"(ret):"a"(73), "b"(0x00),
  208.         "c"(&bc):"memory");
  209.  
  210.         bitmap->data = (void *) -1;
  211.         bitmap->pitch = -1;
  212.  
  213.         return ret;
  214. };
  215.  
  216. int fplay_blit_bitmap(bitmap_t * bitmap, int dst_x, int dst_y, int w, int h)
  217. {
  218.         struct blit_call bc;
  219.         int ret;
  220.  
  221.         surface_t *sf = to_surface(bitmap);
  222.  
  223. //      if (sf->flags & hw_caps & HW_TEX_BLIT)
  224. //              return sna_blit_tex(bitmap, true, dst_x, dst_y, w, h, 0, 0);
  225.  
  226.         bc.dstx = dst_x;
  227.         bc.dsty = dst_y;
  228.         bc.w = w;
  229.         bc.h = h;
  230.         bc.srcx = 0;
  231.         bc.srcy = 0;
  232.         bc.srcw = w;
  233.         bc.srch = h;
  234.         bc.stride = sf->pitch;
  235.         bc.bitmap = sf->data;
  236.  
  237.         __asm__ __volatile__(
  238.     "int $0x40":"=a"(ret):"a"(73), "b"(0x00),
  239.         "c"(&bc):"memory");
  240.  
  241.         bitmap->data = (void *) -1;
  242.         bitmap->pitch = -1;
  243.  
  244.         return ret;
  245. };
  246.  
  247. int resize_bitmap(bitmap_t * bitmap)
  248. {
  249.         uint32_t size;
  250.         uint32_t pitch;
  251.  
  252. //    printf("%s\n", __FUNCTION__);
  253.  
  254.         surface_t *sf = to_surface(bitmap);
  255.  
  256. //      if (sf->flags & hw_caps)
  257. //      {
  258. //              return sna_resize_bitmap(bitmap);
  259. //      };
  260.  
  261.         pitch = ALIGN(bitmap->width * 4, 16);
  262.         size = ALIGN(pitch * bitmap->height, 4096);
  263.  
  264.         bitmap->pitch = -1;
  265.         bitmap->data = (void *) -1;
  266.  
  267.         if (size > sf->bo_size)
  268.         {
  269.                 sf->data = user_realloc(sf->data, size);        /* grow buffer */
  270.                 if (sf->data == NULL)
  271.                         return -1;
  272.  
  273.                 sf->bo_size = size;
  274.         } else if (size < sf->bo_size)
  275.                 user_unmap(sf->data, size, sf->bo_size - size); /* unmap unused pages */
  276.  
  277.         sf->width  = bitmap->width;
  278.         sf->height = bitmap->height;
  279.         sf->pitch  = pitch;
  280.  
  281.         return 0;
  282. };
  283.