Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. //ld -T ld.x -s --shared --image-base 0 --file-alignment 32 -o test.exe test.obj core.lib
  3.  
  4. #include "common.h"
  5. #include "ati2d.h"
  6. #include "accel_2d.h"
  7.  
  8. RHD_t rhd;
  9.  
  10. static clip_t  clip;
  11.  
  12. void STDCALL (*SelectHwCursor)(cursor_t*)__asm__("SelectHwCursor");
  13. void STDCALL (*SetHwCursor)(cursor_t*,int x,int y)__asm__("SetHwCursor");
  14. void STDCALL (*HwCursorRestore)(int x, int y)__asm("HwCursorRestore");
  15. cursor_t* IMPORT (*HwCursorCreate)(void)__asm("HwCursorCreate"); //params eax, ebx, ecx
  16.  
  17. void (__stdcall *old_select)(cursor_t*);
  18. void (__stdcall *old_set)(cursor_t*,int x,int y);
  19. void (__stdcall *old_restore)(int x, int y);
  20. cursor_t* (*old_create)(void);
  21. cursor_t* __create_cursor(void);
  22.  
  23.  
  24. u32 __stdcall drvEntry(int action)
  25. {
  26.   RHDPtr rhdPtr;
  27.   u32 retval;
  28.  
  29.   int i;
  30.  
  31.   if(action != 1)
  32.     return 0;
  33.  
  34.   if(!dbg_open("/hd0/2/ati2d.log"))
  35.   {
  36.      printf("Can't open /rd/1/drivers/ati2d.log\nExit\n");
  37.      return 0;
  38.   }
  39.  
  40.   if((rhdPtr=FindPciDevice())==NULL)
  41.   {
  42.     dbgprintf("Device not found\n");
  43.     return 0;
  44.   };
  45.  
  46.   for(i=0;i<6;i++)
  47.   {
  48.     if(rhd.memBase[i])
  49.       dbgprintf("Memory base_%d 0x%x size 0x%x\n",
  50.                 i,rhd.memBase[i],(1<<rhd.memsize[i]));
  51.   };
  52.   for(i=0;i<6;i++)
  53.   {
  54.     if(rhd.ioBase[i])
  55.       dbgprintf("Io base_%d 0x%x size 0x%x\n",
  56.                 i,rhd.ioBase[i],(1<<rhd.memsize[i]));
  57.   };
  58.   if(!RHDPreInit())
  59.     return 0;
  60.  
  61. //  old_select = SelectHwCursor;
  62. //  old_set = SetHwCursor;
  63. //  old_restore = HwCursorRestore ;
  64. //  old_create = HwCursorCreate;
  65.  
  66.   R5xx2DInit();
  67.  
  68.   //init_r500();
  69.  
  70.  // SelectHwCursor  = r500_SelectCursor;
  71.  // SetHwCursor     = r500_SetCursor;
  72.  // HwCursorCreate  = __create_cursor;
  73.  // HwCursorRestore = r500_CursorRestore;
  74.  
  75.   retval = RegService("HDRAW", srv_2d);
  76.   dbgprintf("reg service %s as: %x\n", "HDRAW", retval);
  77.  
  78. //  retval = RegService("HWCURSOR", srv_cursor);
  79.   return retval;
  80. };
  81.  
  82.  
  83. #define ERR_PARAM  -1
  84.  
  85. #pragma pack (push,1)
  86.  
  87. #pragma pack (pop)
  88.  
  89. #define API_VERSION     0x01000100
  90.  
  91. #define SRV_GETVERSION  0
  92.  
  93. int _stdcall srv_cursor(ioctl_t *io)
  94. {
  95.   u32 *inp;
  96.   u32 *outp;
  97.  
  98.   inp = io->input;
  99.   outp = io->output;
  100.  
  101.   switch(io->io_code)
  102.   {
  103.     case SRV_GETVERSION:
  104.       if(io->out_size==4)
  105.       {
  106.         *(u32*)io->output = API_VERSION;
  107.         return 0;
  108.       }
  109.       break;
  110.  
  111.     default:
  112.       return ERR_PARAM;
  113.   };
  114.   return ERR_PARAM;
  115. }
  116.  
  117.  
  118. int _stdcall srv_2d(ioctl_t *io)
  119. {
  120.   u32 *inp;
  121.   u32 *outp;
  122.  
  123.   inp = io->input;
  124.   outp = io->output;
  125.  
  126.   switch(io->io_code)
  127.   {
  128.     case SRV_GETVERSION:
  129.       if(io->out_size==4)
  130.       {
  131.         *outp = API_VERSION;
  132.         return 0;
  133.       }
  134.       break;
  135.  
  136.       case DRAW_RECT:
  137.         if(io->inp_size==5)
  138.           return DrawRect((draw_t*)inp);
  139.         break;
  140.  
  141.       case LINE_2P:
  142.         if(io->inp_size==5)
  143.           return Line2P((line2p_t*)inp);
  144.         break;
  145.  
  146.       case FILL_RECT:
  147.         if(io->inp_size==8)
  148.           return FillRect((fill_t*)inp);
  149.         break;
  150.  
  151.     default:
  152.       return ERR_PARAM;
  153.   };
  154.   return ERR_PARAM;
  155. }
  156.  
  157.  
  158. #include "init.c"
  159. #include "pci.c"
  160. #include "ati_mem.c"
  161. #include "cursor.inc"
  162.  
  163. #include "r500.inc"
  164. #include "accel_2d.inc"
  165.  
  166. #define CLIP_TOP        1
  167. #define CLIP_BOTTOM     2
  168. #define CLIP_RIGHT      4
  169. #define CLIP_LEFT       8
  170.  
  171.  
  172. char _L1OutCode( int x, int y )
  173. /*=================================
  174.  
  175.     Verify that a point is inside or outside the active viewport.   */
  176. {
  177.     char            flag;
  178.  
  179.     flag = 0;
  180.     if( x < clip.xmin ) {
  181.         flag |= CLIP_LEFT;
  182.     } else if( x > clip.xmax ) {
  183.         flag |= CLIP_RIGHT;
  184.     }
  185.     if( y < clip.ymin ) {
  186.         flag |= CLIP_TOP;
  187.     } else if( y > clip.ymax ) {
  188.         flag |= CLIP_BOTTOM;
  189.     }
  190.     return( flag );
  191. }
  192.  
  193.  
  194. static void line_inter( int * x1, int* y1, int x2, int y2, int x )
  195. /*===========================================================================
  196.  
  197.     Find the intersection of a line with a boundary of the viewport.
  198.     (x1, y1) is outside and ( x2, y2 ) is inside the viewport.
  199.     NOTE : the signs of denom and ( x - *x1 ) cancel out during division
  200.            so make both of them positive before rounding.   */
  201. {
  202.     int            numer;
  203.     int            denom;
  204.  
  205.     denom = abs( x2 - *x1 );
  206.     numer = 2L * (long)( y2 - *y1 ) * abs( x - *x1 );
  207.     if( numer > 0 ) {
  208.         numer += denom;                     /* round to closest pixel   */
  209.     } else {
  210.         numer -= denom;
  211.     }
  212.     *y1 += numer / ( denom << 1 );
  213.     *x1 = x;
  214. }
  215.  
  216.  
  217. int LineClip( int *x1, int *y1, int *x2, int *y2 )
  218. /*=============================================================
  219.  
  220.     Clips the line with end points (x1,y1) and (x2,y2) to the active
  221.     viewport using the Cohen-Sutherland clipping algorithm. Return the
  222.     clipped coordinates and a decision drawing flag.    */
  223. {
  224.     char            flag1;
  225.     char            flag2;
  226.  
  227.     flag1 = _L1OutCode( *x1, *y1 );
  228.     flag2 = _L1OutCode( *x2, *y2 );
  229.     for( ;; ) {
  230.         if( flag1 & flag2 ) break;                  /* trivially outside    */
  231.         if( flag1 == flag2 ) break;                 /* completely inside    */
  232.         if( flag1 == 0 ) {                          /* first point inside   */
  233.             if( flag2 & CLIP_TOP ) {
  234.                 line_inter( y2, x2, *y1, *x1, clip.ymin );
  235.             } else if( flag2 & CLIP_BOTTOM ) {
  236.                 line_inter( y2, x2, *y1, *x1, clip.ymax );
  237.             } else if( flag2 & CLIP_RIGHT ) {
  238.                 line_inter( x2, y2, *x1, *y1, clip.xmax );
  239.             } else if( flag2 & CLIP_LEFT ) {
  240.                 line_inter( x2, y2, *x1, *y1, clip.xmin );
  241.             }
  242.             flag2 = _L1OutCode( *x2, *y2 );
  243.         } else {                                    /* second point inside  */
  244.             if( flag1 & CLIP_TOP ) {
  245.                 line_inter( y1, x1, *y2, *x2, clip.ymin );
  246.             } else if( flag1 & CLIP_BOTTOM ) {
  247.                 line_inter( y1, x1, *y2, *x2, clip.ymax );
  248.             } else if( flag1 & CLIP_RIGHT ) {
  249.                 line_inter( x1, y1, *x2, *y2, clip.xmax );
  250.             } else if( flag1 & CLIP_LEFT ) {
  251.                 line_inter( x1, y1, *x2, *y2, clip.xmin );
  252.             }
  253.             flag1 = _L1OutCode( *x1, *y1 );
  254.         }
  255.     }
  256.     return( flag1 & flag2 );
  257. }
  258.  
  259.  
  260. static void block_inter( int *x, int *y, int flag )
  261. /*======================================================
  262.  
  263.     Find the intersection of a block with a boundary of the viewport.   */
  264. {
  265.     if( flag & CLIP_TOP ) {
  266.         *y = clip.ymin;
  267.     } else if( flag & CLIP_BOTTOM ) {
  268.         *y = clip.ymax;
  269.     } else if( flag & CLIP_RIGHT ) {
  270.         *x = clip.xmax;
  271.     } else if( flag & CLIP_LEFT ) {
  272.         *x = clip.xmin;
  273.     }
  274. }
  275.  
  276.  
  277. int BlockClip( int *x1, int *y1, int *x2, int* y2 )
  278. /*==============================================================
  279.  
  280.     Clip a block with opposite corners (x1,y1) and (x2,y2) to the
  281.     active viewport based on the Cohen-Sutherland algorithm for line
  282.     clipping. Return the clipped coordinates and a decision drawing
  283.     flag ( 0 draw : 1 don't draw ). */
  284. {
  285.     char            flag1;
  286.     char            flag2;
  287.  
  288.     flag1 = _L1OutCode( *x1, *y1 );
  289.     flag2 = _L1OutCode( *x2, *y2 );
  290.     for( ;; ) {
  291.         if( flag1 & flag2 ) break;                  /* trivially outside    */
  292.         if( flag1 == flag2 ) break;                 /* completely inside    */
  293.         if( flag1 == 0 ) {
  294.             block_inter( x2, y2, flag2 );
  295.             flag2 = _L1OutCode( *x2, *y2 );
  296.         } else {
  297.             block_inter( x1, y1, flag1 );
  298.             flag1 = _L1OutCode( *x1, *y1 );
  299.         }
  300.     }
  301.     return( flag1 & flag2 );
  302. }
  303.  
  304. #if 0
  305. typedef struct
  306. {
  307.    int left;
  308.    int top;
  309.    int right;
  310.    int bottom;
  311. }rect_t;
  312.  
  313. typedef struct _window
  314. {
  315.    struct _window *fd;
  316.    struct _window *bk;
  317.  
  318.    rect_t pos;
  319.    int width;
  320.    int height;
  321.  
  322.    int level;
  323. }win_t, *WINPTR;
  324.  
  325.  
  326. WINPTR top    = NULL;
  327. WINPTR bottom = NULL;
  328.  
  329. WINPTR alloc_window()
  330. {
  331.     WINPTR pwin = malloc(sizeof(win_t));
  332.  
  333.     return pwin;
  334. };
  335.  
  336.  
  337. WINPTR create_win(int l, int t, int w, int h, int level)
  338. {
  339.    WINPTR pwin = alloc_window();
  340.  
  341.    pwin->pos.left   = l;
  342.    pwin->pos.top    = t;
  343.    pwin->pos.right  = l+w-1;
  344.    pwin->pos.bottom = t+h-1;
  345.    pwin->width      = w;
  346.    pwin->height     = h;
  347.  
  348.    pwin->level      = level;
  349.  
  350.    return pwin;
  351. };
  352.  
  353. void insert_window(WINPTR pwin)
  354. {
  355.    WINPTR p = top;
  356.  
  357.    if(p)
  358.    {
  359.      if(pwin->level <= p->level)
  360.      {
  361.         pwin->fd = p;
  362.         pwin->bk = p->bk;
  363.         pwin->bk->fd = pwin;
  364.         p->bk = pwin;
  365.      }
  366.      else
  367.        p = p->fd;
  368.    }
  369.    else
  370.      top = pwin;
  371. }
  372.  
  373. #endif
  374.