Subversion Repositories Kolibri OS

Rev

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