Subversion Repositories Kolibri OS

Rev

Rev 2342 | Rev 2351 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. #include <drmP.h>
  3. #include <drm.h>
  4. #include "i915_drm.h"
  5. #include "i915_drv.h"
  6. #include "intel_drv.h"
  7. #include "bitmap.h"
  8.  
  9. void __attribute__((regparm(1))) destroy_bitmap(bitmap_t *bitmap)
  10. {
  11.     printf("destroy bitmap %d\n", bitmap->handle);
  12.     free_handle(&bm_man, bitmap->handle);
  13.     bitmap->handle = 0;
  14.     i915_gem_object_unpin(bitmap->obj);
  15.     drm_gem_object_unreference(&bitmap->obj->base);
  16.     __DestroyObject(bitmap);
  17. };
  18.  
  19. extern struct drm_device *main_device;
  20.  
  21. struct hman bm_man;
  22.  
  23. int init_bitmaps()
  24. {
  25.     int ret;
  26.  
  27.     ret = init_hman(&bm_man, 1024);
  28.  
  29.     return ret;
  30. };
  31.  
  32.  
  33. int create_surface(struct io_call_10 *pbitmap)
  34. {
  35.     struct drm_i915_gem_object *obj;
  36.  
  37.     bitmap_t   *bitmap;
  38.     u32         handle;
  39.     u32         width, max_width;
  40.     u32         height, max_height;
  41.     u32         size,  max_size;
  42.     u32         pitch, max_pitch;
  43.     void       *uaddr;
  44.  
  45.     int   ret;
  46.  
  47.     pbitmap->handle = 0;
  48.     pbitmap->data   = (void*)-1;
  49.  
  50.     width  = pbitmap->width;
  51.     height = pbitmap->height;
  52.  
  53. /*
  54.     if((width==0)||(height==0)||(width>4096)||(height>4096))
  55.         goto err1;
  56.  
  57.     if( ((pbitmap->max_width !=0 ) &&
  58.          (pbitmap->max_width < width)) ||
  59.          (pbitmap->max_width > 4096) )
  60.         goto err1;
  61.  
  62.     if( ((pbitmap->max_height !=0 ) &&
  63.          (pbitmap->max_height < width)) ||
  64.          (pbitmap->max_height > 4096) )
  65.         goto err1;
  66.  
  67.     if( pbitmap->format != 0)
  68.         goto err1;
  69. */
  70.  
  71.     max_width  = (pbitmap->max_width ==0) ? width  : pbitmap->max_width;
  72.     max_height = (pbitmap->max_height==0) ? height : pbitmap->max_height;
  73.  
  74.     handle = alloc_handle(&bm_man);
  75. //    printf("%s %d\n",__FUNCTION__, handle);
  76.  
  77.     if(handle == 0)
  78.         goto err1;
  79.  
  80.     bitmap = CreateObject(GetPid(), sizeof(*bitmap));
  81.     bitmap->handle = handle;
  82.     bitmap->header.destroy = destroy_bitmap;
  83.     bitmap->obj    = NULL;
  84.  
  85. //    printf("bitmap %x\n", bitmap);
  86.     if( bitmap == NULL)
  87.         goto err1;
  88.  
  89.     hman_set_data(&bm_man, handle, bitmap);
  90.  
  91.     pitch = ALIGN(width*4,64);
  92.  
  93.     size =  roundup(pitch*height, PAGE_SIZE);
  94.  
  95. //    printf("pitch %d size %d\n", pitch, size);
  96.  
  97.     obj = i915_gem_alloc_object(main_device, size);
  98.     if (obj == NULL)
  99.         goto err2;
  100.  
  101.     ret = i915_gem_object_pin(obj, 4096, true);
  102.     if (ret)
  103.         goto err3;
  104.  
  105.     max_pitch = ALIGN(max_width*4,64);
  106.     max_size =  roundup(max_pitch*max_height, PAGE_SIZE);
  107.  
  108.     uaddr = UserAlloc(max_size);
  109.     if( uaddr == NULL)
  110.         goto err4;
  111.     else
  112.     {
  113.         u32_t *src, *dst;
  114.         u32 count, max_count;
  115.  
  116. #define page_tabs  0xFDC00000      /* really dirty hack */
  117.  
  118.         src =  (u32_t*)obj->pages;
  119.         dst =  &((u32_t*)page_tabs)[(u32_t)uaddr >> 12];
  120.         count = size/4096;
  121.         max_count = max_size/4096 - count;
  122.  
  123.         while(count--)
  124.         {
  125.             *dst++ = (0xFFFFF000 & *src++) | 0x207 ; // map as shared page
  126.         };
  127. //        while(max_count--)
  128. //            *dst++ = 0;                              // cleanup unused space
  129.     }
  130.  
  131.     bitmap->handle = handle;
  132.     bitmap->uaddr  = uaddr;
  133.     bitmap->pitch  = pitch;
  134.     bitmap->gaddr  = obj->gtt_offset;
  135.  
  136.     bitmap->width  = width;
  137.     bitmap->height = height;
  138.     bitmap->max_width  = max_width;
  139.     bitmap->max_height = max_height;
  140.  
  141.     bitmap->obj    = obj;
  142.     bitmap->header.destroy = destroy_bitmap;
  143.  
  144.     pbitmap->handle = handle;
  145.     pbitmap->data   = uaddr;
  146.     pbitmap->pitch  = pitch;
  147.  
  148.  
  149.     printf("%s handle: %d pitch: %d gpu_addr: %x user_addr: %x\n",
  150.             __FUNCTION__, handle, pitch, obj->gtt_offset, uaddr);
  151.  
  152.     return 0;
  153.  
  154. err4:
  155.     i915_gem_object_unpin(obj);
  156. err3:
  157.     drm_gem_object_unreference(&obj->base);
  158. err2:
  159.     free_handle(&bm_man, handle);
  160.     __DestroyObject(bitmap);
  161. err1:
  162.     return -1;
  163.  
  164. };
  165.  
  166.  
  167.  
  168.  
  169. int init_hman(struct hman *man, u32 count)
  170. {
  171.     u32* data;
  172.  
  173.     data = malloc(count*sizeof(u32*));
  174.     if(data)
  175.     {
  176.         int i;
  177.  
  178.         for(i=0;i < count-1;)
  179.             data[i] = ++i;
  180.         data[i] = 0;
  181.  
  182.         man->table = data;
  183.         man->next  = 0;
  184.         man->avail = count;
  185.         man->count = count;
  186.  
  187.         return 0;
  188.     };
  189.     return -ENOMEM;
  190. };
  191.  
  192. u32  alloc_handle(struct hman *man)
  193. {
  194.     u32 handle = 0;
  195.  
  196.     if(man->avail)
  197.     {
  198.         handle = man->next;
  199.         man->next = man->table[handle];
  200.         man->avail--;
  201.         handle++;
  202.     }
  203.     return handle;
  204. };
  205.  
  206. int free_handle(struct hman *man, u32 handle)
  207. {
  208.     int ret = -1;
  209.  
  210.     handle--;
  211.  
  212.     if(handle < man->count)
  213.     {
  214.         man->table[handle] = man->next;
  215.         man->next = handle;
  216.         man->avail++;
  217.         ret = 0;
  218.     };
  219.  
  220.     return ret;
  221. };
  222.  
  223.  
  224.