Subversion Repositories Kolibri OS

Rev

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