summaryrefslogtreecommitdiff
path: root/Source/DirectFB/gfxdrivers/neomagic/neo2200.c
blob: 4ac54ac63d319cc7e36407b5908f92f2a9a0ab30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
   (c) Copyright 2001-2009  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <config.h>

#include <dfb_types.h>

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include <sys/mman.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <directfb.h>

#include <direct/messages.h>

#include <core/coredefs.h>
#include <core/coretypes.h>

#include <core/state.h>
#include <core/screens.h>
#include <core/gfxcard.h>
#include <core/surface.h>

#include <gfx/convert.h>

#include "neomagic.h"

typedef volatile struct {
  u32 bltStat;
  u32 bltCntl;
  u32 xpColor;
  u32 fgColor;
  u32 bgColor;
  u32 pitch;
  u32 clipLT;
  u32 clipRB;
  u32 srcBitOffset;
  u32 srcStart;
  u32 reserved0;
  u32 dstStart;
  u32 xyExt;

  u32 reserved1[19];

  u32 pageCntl;
  u32 pageBase;
  u32 postBase;
  u32 postPtr;
  u32 dataPtr;
} Neo2200;

typedef struct {
     NeoDeviceData neo;

     int dstOrg;
     int dstPitch;
     int dstPixelWidth;

     int srcOrg;
     int srcPitch;
     int srcPixelWidth;

     u32 bltCntl;

     bool src_dst_equal;

     /* state validation */
     int n_bltMode_dst;
     int n_src;
     int n_fgColor;
     int n_xpColor;
} Neo2200DeviceData;

typedef struct {
     NeoDriverData neo;

     Neo2200 *neo2200;
} Neo2200DriverData;


static inline void neo2200_waitidle( Neo2200DriverData *ndrv,
                                     Neo2200DeviceData *ndev )
{
     while (ndrv->neo2200->bltStat & 1)
          ndev->neo.idle_waitcycles++;
}

static inline void neo2200_waitfifo( Neo2200DriverData *ndrv,
                                     Neo2200DeviceData *ndev,
                                     int requested_fifo_space )
{
  ndev->neo.waitfifo_calls++;
  ndev->neo.waitfifo_sum += requested_fifo_space;

  /* FIXME: does not work
  if (neo_fifo_space < requested_fifo_space)
    {
      neo_fifo_waitcycles++;

      while (1)
    {
      neo_fifo_space = (neo2200->bltStat >> 8);
      if (neo_fifo_space >= requested_fifo_space)
        break;
    }
    }
  else
    {
      neo_fifo_cache_hits++;
    }

  neo_fifo_space -= requested_fifo_space;
  */

  neo2200_waitidle( ndrv, ndev );
}



static inline void neo2200_validate_bltMode_dst( Neo2200DriverData     *ndrv,
                                                 Neo2200DeviceData     *ndev,
                                                 CoreSurface           *dst,
                                                 CoreSurfaceBufferLock *lock )
{
  int bltMode = 0;

  if (ndev->n_bltMode_dst)
    return;

  switch (dst->config.format)
    {
    case DSPF_A8:
    case DSPF_LUT8:
    case DSPF_RGB332:
      bltMode |= NEO_MODE1_DEPTH8;
      break;
    case DSPF_ARGB1555:
    case DSPF_RGB16:
    case DSPF_YUY2:
      bltMode |= NEO_MODE1_DEPTH16;
      break;
    default:
      D_BUG( "unexpected pixelformat!" );
      break;
    }

  ndev->dstOrg = lock->offset;
  ndev->dstPitch = lock->pitch;
  ndev->dstPixelWidth = DFB_BYTES_PER_PIXEL(dst->config.format);


  neo2200_waitfifo( ndrv, ndev, 2 );

  ndrv->neo2200->bltStat = bltMode << 16;
  ndrv->neo2200->pitch = (ndev->dstPitch << 16) | (ndev->srcPitch & 0xffff);


  ndev->n_bltMode_dst = 1;
}

static inline void neo2200_validate_src( Neo2200DriverData     *ndrv,
                                         Neo2200DeviceData     *ndev,
                                         CoreSurface           *src,
                                         CoreSurfaceBufferLock *lock )
{
  if (ndev->n_src)
    return;

  ndev->srcOrg = lock->offset;
  ndev->srcPitch = lock->pitch;
  ndev->srcPixelWidth = DFB_BYTES_PER_PIXEL(src->config.format);

  neo2200_waitfifo( ndrv, ndev, 1 );
  ndrv->neo2200->pitch = (ndev->dstPitch << 16) | (ndev->srcPitch & 0xffff);

  ndev->n_src = 1;
}

static inline void neo2200_validate_fgColor( Neo2200DriverData *ndrv,
                                             Neo2200DeviceData *ndev,
                                             CardState         *state )
{
  if (ndev->n_fgColor)
    return;

  neo2200_waitfifo( ndrv, ndev, 1 );

  switch (state->destination->config.format)
    {
    case DSPF_A8:
      ndrv->neo2200->fgColor = state->color.a;
      break;
    case DSPF_LUT8:
      ndrv->neo2200->fgColor = state->color_index;
      break;
    case DSPF_RGB332:
      ndrv->neo2200->fgColor = PIXEL_RGB332( state->color.r,
                                             state->color.g,
                                             state->color.b );
      break;
    case DSPF_ARGB1555:
      ndrv->neo2200->fgColor = PIXEL_ARGB1555( state->color.a,
                                               state->color.r,
                                               state->color.g,
                                               state->color.b );
      break;
    case DSPF_RGB16:
      ndrv->neo2200->fgColor = PIXEL_RGB16( state->color.r,
                                            state->color.g,
                                            state->color.b );
      break;
    default:
      D_BUG( "unexpected pixelformat!" );
      break;
    }

  ndev->n_fgColor = 1;
}

static inline void neo2200_validate_xpColor( Neo2200DriverData *ndrv,
                                             Neo2200DeviceData *ndev,
                                             CardState         *state )
{
  if (ndev->n_xpColor)
    return;

  neo2200_waitfifo( ndrv, ndev, 1 );

  ndrv->neo2200->xpColor = state->src_colorkey;

  ndev->n_xpColor = 1;
}


/* required implementations */

static DFBResult neo2200EngineSync( void *drv, void *dev )
{
     Neo2200DriverData *ndrv = (Neo2200DriverData*) drv;
     Neo2200DeviceData *ndev = (Neo2200DeviceData*) dev;

     neo2200_waitidle( ndrv, ndev );

     return DFB_OK;
}

#define NEO_SUPPORTED_DRAWINGFLAGS \
               (DSDRAW_NOFX)

#define NEO_SUPPORTED_DRAWINGFUNCTIONS \
               (DFXL_FILLRECTANGLE | DFXL_DRAWRECTANGLE)

#define NEO_SUPPORTED_BLITTINGFLAGS \
               (DSBLIT_SRC_COLORKEY)

#define NEO_SUPPORTED_BLITTINGFUNCTIONS \
               (DFXL_BLIT)

static void neo2200CheckState( void *drv, void *dev,
                               CardState *state, DFBAccelerationMask accel )
{
  switch (state->destination->config.format)
    {
    case DSPF_A8:
    case DSPF_LUT8:
    case DSPF_RGB332:
    case DSPF_ARGB1555:
    case DSPF_RGB16:
      break;
    case DSPF_YUY2:
      if (accel == DFXL_BLIT)
        break;
    default:
      return;
    }

  if (DFB_DRAWING_FUNCTION(accel))
    {
      /* if the function is supported and there are no other
         drawing flags than the supported */
      if (!(accel & ~NEO_SUPPORTED_DRAWINGFUNCTIONS) &&
          !(state->drawingflags & ~NEO_SUPPORTED_DRAWINGFLAGS))
        state->accel |= accel;
    }
  else
    {
      /* if the function is supported, there are no other
         blitting flags than the supported, the source and
         destination formats are the same and the source and dest.
         are different due to a blitting bug */
      if (!(accel & ~NEO_SUPPORTED_BLITTINGFUNCTIONS)             &&
          !(state->blittingflags & ~NEO_SUPPORTED_BLITTINGFLAGS)  &&
          state->source->config.format == state->destination->config.format)
        state->accel |= accel;
    }
}

static void neo2200SetState( void *drv, void *dev,
                             GraphicsDeviceFuncs *funcs,
                             CardState *state, DFBAccelerationMask accel )
{
     Neo2200DriverData *ndrv = (Neo2200DriverData*) drv;
     Neo2200DeviceData *ndev = (Neo2200DeviceData*) dev;

     if (state->mod_hw & SMF_DESTINATION)
          ndev->n_fgColor = ndev->n_bltMode_dst = 0;
     else if (state->mod_hw & SMF_COLOR)
          ndev->n_fgColor = 0;

     if (state->mod_hw & SMF_SOURCE)
          ndev->n_src = 0;

     if (state->mod_hw & SMF_SRC_COLORKEY)
          ndev->n_xpColor = 0;

     neo2200_validate_bltMode_dst( ndrv, ndev, state->destination, &state->dst );

     switch (accel) {
          case DFXL_BLIT:
               neo2200_validate_src( ndrv, ndev, state->source, &state->src );

               ndev->src_dst_equal = (state->src.buffer ==
                                      state->dst.buffer);

               if (state->blittingflags & DSBLIT_SRC_COLORKEY) {
                    ndev->bltCntl = NEO_BC0_SRC_TRANS;
                    neo2200_validate_xpColor( ndrv, ndev, state );
               }
               else
                    ndev->bltCntl = 0;

               state->set |= DFXL_BLIT;
               break;

          case DFXL_FILLRECTANGLE:
          case DFXL_DRAWRECTANGLE:
               neo2200_validate_fgColor( ndrv, ndev, state );

               state->set |= DFXL_FILLRECTANGLE | DFXL_DRAWRECTANGLE;
               break;

          default:
               D_BUG( "unexpected drawing/blitting function!" );
               break;
     }

     state->mod_hw = 0;
}

static bool neo2200FillRectangle( void *drv, void *dev, DFBRectangle *rect )
{
     Neo2200DriverData *ndrv    = (Neo2200DriverData*) drv;
     Neo2200DeviceData *ndev    = (Neo2200DeviceData*) dev;
     Neo2200           *neo2200 = ndrv->neo2200;

     neo2200_waitfifo( ndrv, ndev, 3 );

     /* set blt control */
     neo2200->bltCntl = NEO_BC3_FIFO_EN      |
                     NEO_BC0_SRC_IS_FG    |
                     NEO_BC3_SKIP_MAPPING |  0x0c0000;

     neo2200->dstStart = ndev->dstOrg +
           (rect->y * ndev->dstPitch) +
           (rect->x * ndev->dstPixelWidth);

     neo2200->xyExt    = (rect->h << 16) | (rect->w & 0xffff);

     return true;
}

static bool neo2200DrawRectangle( void *drv, void *dev, DFBRectangle *rect )
{
     Neo2200DriverData *ndrv    = (Neo2200DriverData*) drv;
     Neo2200DeviceData *ndev    = (Neo2200DeviceData*) dev;
     Neo2200           *neo2200 = ndrv->neo2200;

     u32 dst = ndev->dstOrg +
              (rect->y * ndev->dstPitch) +
              (rect->x * ndev->dstPixelWidth);

     neo2200_waitfifo( ndrv, ndev, 3 );

     /* set blt control */
     neo2200->bltCntl = NEO_BC3_FIFO_EN      |
                        NEO_BC0_SRC_IS_FG    |
                        NEO_BC3_SKIP_MAPPING | 0x0c0000;

     neo2200->dstStart = dst;
     neo2200->xyExt    = (1 << 16) | (rect->w & 0xffff);


     dst += (rect->h - 1) * ndev->dstPitch;
     neo2200_waitfifo( ndrv, ndev, 2 );
     neo2200->dstStart = dst;
     neo2200->xyExt    = (1 << 16) | (rect->w & 0xffff);


     dst -= (rect->h - 2) * ndev->dstPitch;
     neo2200_waitfifo( ndrv, ndev, 2 );
     neo2200->dstStart = dst;
     neo2200->xyExt    = ((rect->h - 2) << 16) | 1;


     dst += (rect->w - 1) * ndev->dstPixelWidth;
     neo2200_waitfifo( ndrv, ndev, 2 );
     neo2200->dstStart = dst;
     neo2200->xyExt    = ((rect->h - 2) << 16) | 1;

     return true;
}

static bool neo2200Blit( void *drv, void *dev,
                         DFBRectangle *rect, int dx, int dy )
{
     Neo2200DriverData *ndrv    = (Neo2200DriverData*) drv;
     Neo2200DeviceData *ndev    = (Neo2200DeviceData*) dev;
     Neo2200           *neo2200 = ndrv->neo2200;

     u32 src_start, dst_start;
     u32 bltCntl = ndev->bltCntl;

//     fprintf(stderr, "blit: %d, %d (%dx%d) -> %d, %d\n",
//             rect->x, rect->y, rect->w, rect->h, dx, dy);

/*     if (rect->x < dx) {
          //rect->x += rect->w - 1;
          //dx      += rect->w - 1;

          bltCntl |= NEO_BC0_X_DEC;
     }

     if (rect->y < dy) {
          //rect->y += rect->h - 1;
          //dy      += rect->h - 1;

          bltCntl |= NEO_BC0_DST_Y_DEC | NEO_BC0_SRC_Y_DEC;
     }
*/
     /* ARGH, the above code for the blitting direction doesn't work. */
     if (ndev->src_dst_equal && (rect->x < dx || rect->y < dy))
          return false;

     src_start = rect->y * ndev->srcPitch + rect->x * ndev->srcPixelWidth;
     dst_start = dy * ndev->dstPitch + dx * ndev->dstPixelWidth;

     neo2200_waitfifo( ndrv, ndev, 4 );

     /* set blt control */
     neo2200->bltCntl  = bltCntl |
                         NEO_BC3_FIFO_EN      |
                         NEO_BC3_SKIP_MAPPING |  0x0c0000;

     /* set start addresses */
     neo2200->srcStart = ndev->srcOrg + src_start;
     neo2200->dstStart = ndev->dstOrg + dst_start;

     /* set size */
     neo2200->xyExt    = (rect->h << 16) | (rect->w & 0xffff);

     return true;
}



void
neo2200_get_info( CoreGraphicsDevice *device,
                  GraphicsDriverInfo *info )
{
     info->version.major = 0;
     info->version.minor = 3;

     info->driver_data_size = sizeof (Neo2200DriverData);
     info->device_data_size = sizeof (Neo2200DeviceData);
}

DFBResult
neo2200_init_driver( CoreGraphicsDevice  *device,
                     GraphicsDeviceFuncs *funcs,
                     void                *driver_data )
{
     Neo2200DriverData *ndrv = (Neo2200DriverData*) driver_data;

     ndrv->neo2200 = (Neo2200*) ndrv->neo.mmio_base;

     funcs->CheckState = neo2200CheckState;
     funcs->SetState = neo2200SetState;
     funcs->EngineSync = neo2200EngineSync;

     funcs->FillRectangle = neo2200FillRectangle;
     funcs->DrawRectangle = neo2200DrawRectangle;
     //     funcs->DrawLine = neoDrawLine2D;
     funcs->Blit = neo2200Blit;
     //     funcs->StretchBlit = neoStretchBlit;

     /* overlay support */
     dfb_layers_register( dfb_screens_at(DSCID_PRIMARY),
                          driver_data, &neoOverlayFuncs );
     return DFB_OK;
}

DFBResult
neo2200_init_device( CoreGraphicsDevice *device,
                     GraphicsDeviceInfo *device_info,
                     void               *driver_data,
                     void               *device_data )
{
     /* fill device info */
     snprintf( device_info->name,
               DFB_GRAPHICS_DEVICE_INFO_NAME_LENGTH, "2200/2230/2360/2380" );

     snprintf( device_info->vendor,
               DFB_GRAPHICS_DEVICE_INFO_VENDOR_LENGTH, "NeoMagic" );


     device_info->caps.flags    = 0;
     device_info->caps.accel    = NEO_SUPPORTED_DRAWINGFUNCTIONS |
                                  NEO_SUPPORTED_BLITTINGFUNCTIONS;
     device_info->caps.drawing  = NEO_SUPPORTED_DRAWINGFLAGS;
     device_info->caps.blitting = NEO_SUPPORTED_BLITTINGFLAGS;

     device_info->limits.surface_byteoffset_alignment = 32 * 4;
     device_info->limits.surface_pixelpitch_alignment = 32;

     return DFB_OK;
}

void
neo2200_close_device( CoreGraphicsDevice *device,
                      void               *driver_data,
                      void               *device_data )
{
}

void
neo2200_close_driver( CoreGraphicsDevice *device,
                      void               *driver_data )
{
}