summaryrefslogtreecommitdiff
path: root/Source/DirectFB/gfxdrivers/mach64/mach64_overlay.c
blob: 11cff286765009ceeb83d4a711818d51ba82a1a9 (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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/*
   (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 <stdio.h>

#include <direct/messages.h>

#include <core/coredefs.h>
#include <core/layers.h>
#include <core/surface.h>
#include <core/system.h>

#include <gfx/convert.h>

#include <misc/util.h>

#include "regs.h"
#include "mmio.h"
#include "mach64.h"

typedef struct {
     CoreLayerRegionConfig config;
     bool                  visible;

     /* overlay registers */
     struct {
          u32 overlay_Y_X_START;
          u32 overlay_Y_X_END;
          u32 overlay_GRAPHICS_KEY_CLR;
          u32 overlay_GRAPHICS_KEY_MSK;
          u32 overlay_VIDEO_KEY_CLR;
          u32 overlay_VIDEO_KEY_MSK;
          u32 overlay_KEY_CNTL;
          u32 overlay_SCALE_INC;
          u32 overlay_SCALE_CNTL;
          u32 scaler_HEIGHT_WIDTH;
          u32 scaler_BUF_PITCH;
          u32 scaler_BUF0_OFFSET;
          u32 scaler_BUF1_OFFSET;
          u32 scaler_BUF0_OFFSET_U;
          u32 scaler_BUF0_OFFSET_V;
          u32 scaler_BUF1_OFFSET_U;
          u32 scaler_BUF1_OFFSET_V;
          u32 video_FORMAT;
          u32 capture_CONFIG;
     } regs;
} Mach64OverlayLayerData;

static void ov_reset( Mach64DriverData *mdrv );
static void ov_set_regs( Mach64DriverData *mdrv, Mach64OverlayLayerData *mov );
static void ov_calc_regs( Mach64DriverData *mdrv, Mach64OverlayLayerData *mov,
                          CoreLayerRegionConfig *config, CoreSurface *surface,
                          CoreSurfaceBufferLock  *lock );
static void ov_set_buffer( Mach64DriverData *mdrv, Mach64OverlayLayerData *mov );
static void ov_calc_buffer( Mach64DriverData *mdrv, Mach64OverlayLayerData *mov,
                            CoreLayerRegionConfig *config, CoreSurface *surface,
                            CoreSurfaceBufferLock  *lock );
static void ov_set_colorkey( Mach64DriverData *mdrv, Mach64OverlayLayerData *mov );
static void ov_calc_colorkey( Mach64DriverData *mdrv, Mach64OverlayLayerData *mov,
                              CoreLayerRegionConfig *config );
static void ov_set_opacity( Mach64DriverData *mdrv, Mach64OverlayLayerData *mov );
static void ov_calc_opacity( Mach64DriverData *mdrv, Mach64OverlayLayerData *mov,
                             CoreLayerRegionConfig *config );
static void ov_set_field( Mach64DriverData *mdrv, Mach64OverlayLayerData *mov );

#define OV_SUPPORTED_OPTIONS   (DLOP_SRC_COLORKEY | DLOP_DST_COLORKEY | DLOP_DEINTERLACING)

/**********************/

static int
ovLayerDataSize( void )
{
     return sizeof(Mach64OverlayLayerData);
}

static DFBResult
ovInitLayer( CoreLayer                  *layer,
             void                       *driver_data,
             void                       *layer_data,
             DFBDisplayLayerDescription *description,
             DFBDisplayLayerConfig      *config,
             DFBColorAdjustment         *adjustment )
{
     Mach64DriverData *mdrv = (Mach64DriverData*) driver_data;
     Mach64DeviceData *mdev = mdrv->device_data;

     /* set capabilities and type */
     description->caps = DLCAPS_SCREEN_LOCATION | DLCAPS_SURFACE |
                         DLCAPS_DST_COLORKEY | DLCAPS_DEINTERLACING;

     if (mdev->chip >= CHIP_264VT3)
          description->caps |= DLCAPS_SRC_COLORKEY;

     description->type = DLTF_VIDEO | DLTF_STILL_PICTURE;

     /* set name */
     snprintf( description->name,
               DFB_DISPLAY_LAYER_DESC_NAME_LENGTH, "Mach64 Overlay" );

     /* fill out the default configuration */
     config->flags       = DLCONF_WIDTH | DLCONF_HEIGHT |
                           DLCONF_PIXELFORMAT | DLCONF_BUFFERMODE |
                           DLCONF_OPTIONS;
     config->width       = (mdev->chip >= CHIP_264VT3) ? 640 : 320;
     config->height      = (mdev->chip >= CHIP_264VT3) ? 480 : 240;
     config->pixelformat = DSPF_YUY2;
     config->buffermode  = DLBM_FRONTONLY;
     config->options     = DLOP_NONE;

     adjustment->flags   = DCAF_NONE;

     if (mdev->chip >= CHIP_3D_RAGE_PRO) {
          description->caps      |= DLCAPS_BRIGHTNESS | DLCAPS_SATURATION;

          /* fill out default color adjustment,
             only fields set in flags will be accepted from applications */
          adjustment->flags      |= DCAF_BRIGHTNESS | DCAF_SATURATION;
          adjustment->brightness  = 0x8000;
          adjustment->saturation  = 0x8000;
     }

     return DFB_OK;
}

static DFBResult
ovTestRegion( CoreLayer                  *layer,
              void                       *driver_data,
              void                       *layer_data,
              CoreLayerRegionConfig      *config,
              CoreLayerRegionConfigFlags *failed )
{
     Mach64DriverData *mdrv = (Mach64DriverData*) driver_data;
     Mach64DeviceData *mdev = mdrv->device_data;
     CoreLayerRegionConfigFlags fail = 0;
     int max_width, max_height = 1024;

     switch (mdev->chip) {
          case CHIP_264VT: /* 264VT2 verified */
          case CHIP_3D_RAGE: /* not verified */
               max_width = 384;
               break;
          case CHIP_264VT3: /* not verified */
          case CHIP_3D_RAGE_II: /* not verified */
          case CHIP_3D_RAGE_IIPLUS:
          case CHIP_264VT4: /* not verified */
          case CHIP_3D_RAGE_IIC:
          case CHIP_3D_RAGE_XLXC:
          case CHIP_3D_RAGE_MOBILITY:
               max_width = 720;
               break;
          case CHIP_3D_RAGE_PRO: /* not verified */
          case CHIP_3D_RAGE_LT_PRO:
               max_width = 768;
               break;
          default:
               D_BUG( "unknown chip" );
               return DFB_UNSUPPORTED;
     }

     if (config->options & DLOP_DEINTERLACING)
          max_height = 2048;

     /* check for unsupported options */
     if (config->options & ~OV_SUPPORTED_OPTIONS)
          fail |= CLRCF_OPTIONS;

     /*
      * Video keying doesn't work the same way on 264VT2 as it does
      * on later chips. If enabled the overlay goes completely black
      * so clearly it does something but not what we want.
      */
     if (mdev->chip < CHIP_264VT3 && config->options & DLOP_SRC_COLORKEY)
          fail |= CLRCF_OPTIONS;

     /* check pixel format */
     switch (config->format) {
          case DSPF_RGB555:
          case DSPF_ARGB1555:
          case DSPF_RGB16:
          case DSPF_RGB32:
          case DSPF_YUY2:
          case DSPF_UYVY:
               break;
          case DSPF_I420:
          case DSPF_YV12:
               if (mdev->chip >= CHIP_3D_RAGE_PRO)
                    break;
          default:
               fail |= CLRCF_FORMAT;
     }

     switch (config->format) {
          case DSPF_I420:
          case DSPF_YV12:
               if (config->height & 1)
                    fail |= CLRCF_HEIGHT;
          case DSPF_YUY2:
          case DSPF_UYVY:
               if (config->width & 1)
                    fail |= CLRCF_WIDTH;
          default:
               break;
     }

     /* check width */
     if (config->width > max_width || config->width < 1)
          fail |= CLRCF_WIDTH;

     /* check height */
     if (config->height > max_height || config->height < 1)
          fail |= CLRCF_HEIGHT;

     /* write back failing fields */
     if (failed)
          *failed = fail;

     /* return failure if any field failed */
     if (fail)
          return DFB_UNSUPPORTED;

     return DFB_OK;
}

static DFBResult
ovSetRegion( CoreLayer                  *layer,
             void                       *driver_data,
             void                       *layer_data,
             void                       *region_data,
             CoreLayerRegionConfig      *config,
             CoreLayerRegionConfigFlags  updated,
             CoreSurface                *surface,
             CorePalette                *palette,
             CoreSurfaceBufferLock      *lock )
{
     Mach64DriverData       *mdrv = (Mach64DriverData*) driver_data;
     Mach64OverlayLayerData *mov  = (Mach64OverlayLayerData*) layer_data;

     /* remember configuration */
     mov->config = *config;

     if (updated == CLRCF_ALL)
          ov_reset( mdrv );

     if (updated & (CLRCF_WIDTH | CLRCF_HEIGHT | CLRCF_FORMAT | CLRCF_SOURCE | CLRCF_DEST | CLRCF_OPTIONS)) {
          ov_calc_buffer( mdrv, mov, config, surface, lock );
          ov_calc_regs( mdrv, mov, config, surface, lock );
          ov_set_buffer( mdrv, mov );
          ov_set_regs( mdrv, mov );
     }

     if (updated & (CLRCF_OPTIONS | CLRCF_SRCKEY | CLRCF_DSTKEY)) {
          ov_calc_colorkey( mdrv, mov, config );
          ov_set_colorkey( mdrv, mov );
     }

     if (updated & CLRCF_OPTIONS)
          ov_set_field( mdrv, mov );

     if (updated & (CLRCF_DEST | CLRCF_OPACITY)) {
          ov_calc_opacity( mdrv, mov, config );
          ov_set_opacity( mdrv, mov );
     }

     return DFB_OK;
}

static DFBResult
ovRemoveRegion( CoreLayer *layer,
                void      *driver_data,
                void      *layer_data,
                void      *region_data )
{
     Mach64DriverData *mdrv = (Mach64DriverData*) driver_data;
     Mach64DeviceData *mdev = mdrv->device_data;
     volatile u8      *mmio = mdrv->mmio_base;

     mach64_waitfifo( mdrv, mdev, 2 );

     /*
      * On 264VT2 the keyer sometimes remains active
      * even after the overlay has been disabled.
      */
     mach64_out32( mmio, OVERLAY_KEY_CNTL,
                   VIDEO_KEY_FN_FALSE | GRAPHICS_KEY_FN_FALSE | OVERLAY_CMP_MIX_OR );

     mach64_out32( mmio, OVERLAY_SCALE_CNTL, 0 );

     return DFB_OK;
}

static DFBResult
ovFlipRegion( CoreLayer             *layer,
              void                  *driver_data,
              void                  *layer_data,
              void                  *region_data,
              CoreSurface           *surface,
              DFBSurfaceFlipFlags    flags,
              CoreSurfaceBufferLock *lock )
{
     Mach64DriverData       *mdrv = (Mach64DriverData*) driver_data;
     Mach64OverlayLayerData *mov  = (Mach64OverlayLayerData*) layer_data;

     ov_calc_buffer( mdrv, mov, &mov->config, surface, lock );
     ov_set_buffer( mdrv, mov );

     dfb_surface_flip( surface, false );

     return DFB_OK;
}

static DFBResult
ovSetColorAdjustment( CoreLayer          *layer,
                      void               *driver_data,
                      void               *layer_data,
                      DFBColorAdjustment *adj )
{
     Mach64DriverData *mdrv = (Mach64DriverData*) driver_data;
     Mach64DeviceData *mdev = mdrv->device_data;
     volatile u8      *mmio = mdrv->mmio_base;

     if (mdev->chip < CHIP_3D_RAGE_PRO)
          return DFB_UNSUPPORTED;

     mach64_waitfifo( mdrv, mdev, 1 );

     mach64_out32( mmio, SCALER_COLOUR_CNTL,
                   (((adj->brightness >> 9) - 64) & 0x0000007F) |
                   ((adj->saturation >> 3)  & 0x00001F00) |
                   ((adj->saturation << 5)  & 0x001F0000) );

     return DFB_OK;
}

static DFBResult
ovSetInputField( CoreLayer          *layer,
                 void               *driver_data,
                 void               *layer_data,
                 void               *region_data,
                 int                 field )
{
     Mach64DriverData       *mdrv = (Mach64DriverData*) driver_data;
     Mach64OverlayLayerData *mov  = (Mach64OverlayLayerData*) layer_data;

     mov->regs.capture_CONFIG = OVL_BUF_MODE_SINGLE | (field ? OVL_BUF_NEXT_BUF1 : OVL_BUF_NEXT_BUF0);

     ov_set_field( mdrv, mov );

     return DFB_OK;
}

DisplayLayerFuncs mach64OverlayFuncs = {
     .LayerDataSize      = ovLayerDataSize,
     .InitLayer          = ovInitLayer,

     .TestRegion         = ovTestRegion,
     .SetRegion          = ovSetRegion,
     .RemoveRegion       = ovRemoveRegion,
     .FlipRegion         = ovFlipRegion,
     .SetColorAdjustment = ovSetColorAdjustment,
     .SetInputField      = ovSetInputField,
};

/* internal */

static void ov_reset( Mach64DriverData *mdrv )
{
     Mach64DeviceData *mdev = mdrv->device_data;
     volatile u8      *mmio = mdrv->mmio_base;

     if (mdev->chip >= CHIP_3D_RAGE_PRO) {
          mach64_waitfifo( mdrv, mdev, 6 );

          mach64_out32( mmio, SCALER_H_COEFF0, 0x00002000 );
          mach64_out32( mmio, SCALER_H_COEFF1, 0x0D06200D );
          mach64_out32( mmio, SCALER_H_COEFF2, 0x0D0A1C0D );
          mach64_out32( mmio, SCALER_H_COEFF3, 0x0C0E1A0C );
          mach64_out32( mmio, SCALER_H_COEFF4, 0x0C14140C );
          mach64_out32( mmio, SCALER_COLOUR_CNTL, 0x00101000 );
     }

     if (mdev->chip >= CHIP_264VT3) {
          mach64_waitfifo( mdrv, mdev, 2 );

          mach64_out32( mmio, OVERLAY_EXCLUSIVE_HORZ, 0 );
          mach64_out32( mmio, OVERLAY_EXCLUSIVE_VERT, 0 );
     }

     mach64_waitfifo( mdrv, mdev, 2 );

     mach64_out32( mmio, OVERLAY_SCALE_CNTL, 0 );
     mach64_out32( mmio, SCALER_TEST, 0 );
}

static void ov_set_regs( Mach64DriverData       *mdrv,
                         Mach64OverlayLayerData *mov )
{
     Mach64DeviceData *mdev = mdrv->device_data;
     volatile u8      *mmio = mdrv->mmio_base;

     mach64_waitfifo( mdrv, mdev, (mdev->chip >= CHIP_264VT3) ? 6 : 7 );

     mach64_out32( mmio, VIDEO_FORMAT,          mov->regs.video_FORMAT );
     mach64_out32( mmio, OVERLAY_Y_X_START,     mov->regs.overlay_Y_X_START );
     mach64_out32( mmio, OVERLAY_Y_X_END,       mov->regs.overlay_Y_X_END );
     mach64_out32( mmio, OVERLAY_SCALE_INC,     mov->regs.overlay_SCALE_INC );
     mach64_out32( mmio, SCALER_HEIGHT_WIDTH,   mov->regs.scaler_HEIGHT_WIDTH );

     if (mdev->chip >= CHIP_264VT3) {
          mach64_out32( mmio, SCALER_BUF_PITCH, mov->regs.scaler_BUF_PITCH );
     } else {
          mach64_out32( mmio, BUF0_PITCH,       mov->regs.scaler_BUF_PITCH );
          mach64_out32( mmio, BUF1_PITCH,       mov->regs.scaler_BUF_PITCH );
     }
}

static void ov_set_buffer( Mach64DriverData       *mdrv,
                           Mach64OverlayLayerData *mov )
{
     Mach64DeviceData *mdev = mdrv->device_data;
     volatile u8      *mmio = mdrv->mmio_base;

     mach64_waitfifo( mdrv, mdev, (mdev->chip >= CHIP_3D_RAGE_PRO) ? 6 : 2 );

     if (mdev->chip >= CHIP_264VT3) {
          mach64_out32( mmio, SCALER_BUF0_OFFSET,   mov->regs.scaler_BUF0_OFFSET );
          mach64_out32( mmio, SCALER_BUF1_OFFSET,   mov->regs.scaler_BUF1_OFFSET );
     } else {
          mach64_out32( mmio, BUF0_OFFSET,          mov->regs.scaler_BUF0_OFFSET );
          mach64_out32( mmio, BUF1_OFFSET,          mov->regs.scaler_BUF1_OFFSET );
     }

     if (mdev->chip >= CHIP_3D_RAGE_PRO) {
          mach64_out32( mmio, SCALER_BUF0_OFFSET_U, mov->regs.scaler_BUF0_OFFSET_U );
          mach64_out32( mmio, SCALER_BUF0_OFFSET_V, mov->regs.scaler_BUF0_OFFSET_V );
          mach64_out32( mmio, SCALER_BUF1_OFFSET_U, mov->regs.scaler_BUF1_OFFSET_U );
          mach64_out32( mmio, SCALER_BUF1_OFFSET_V, mov->regs.scaler_BUF1_OFFSET_V );
     }
}

static void ov_set_colorkey( Mach64DriverData       *mdrv,
                             Mach64OverlayLayerData *mov )
{
     Mach64DeviceData *mdev = mdrv->device_data;
     volatile u8      *mmio = mdrv->mmio_base;

     mach64_waitfifo( mdrv, mdev, 5 );

     mach64_out32( mmio, OVERLAY_GRAPHICS_KEY_CLR, mov->regs.overlay_GRAPHICS_KEY_CLR );
     mach64_out32( mmio, OVERLAY_GRAPHICS_KEY_MSK, mov->regs.overlay_GRAPHICS_KEY_MSK );
     mach64_out32( mmio, OVERLAY_VIDEO_KEY_CLR,    mov->regs.overlay_VIDEO_KEY_CLR );
     mach64_out32( mmio, OVERLAY_VIDEO_KEY_MSK,    mov->regs.overlay_VIDEO_KEY_MSK );
     mach64_out32( mmio, OVERLAY_KEY_CNTL,         mov->regs.overlay_KEY_CNTL );
}

static void ov_set_opacity( Mach64DriverData       *mdrv,
                            Mach64OverlayLayerData *mov )
{
     Mach64DeviceData *mdev = mdrv->device_data;
     volatile u8      *mmio = mdrv->mmio_base;

     mach64_waitfifo( mdrv, mdev, 1 );

     mach64_out32( mmio, OVERLAY_SCALE_CNTL, mov->regs.overlay_SCALE_CNTL );
}

static void ov_set_field( Mach64DriverData       *mdrv,
                          Mach64OverlayLayerData *mov )
{
     Mach64DeviceData *mdev = mdrv->device_data;
     volatile u8      *mmio = mdrv->mmio_base;

     mach64_waitfifo( mdrv, mdev, 1 );

     mach64_out32( mmio, CAPTURE_CONFIG, mov->regs.capture_CONFIG );
}

static void ov_calc_regs( Mach64DriverData       *mdrv,
                          Mach64OverlayLayerData *mov,
                          CoreLayerRegionConfig  *config,
                          CoreSurface            *surface,
                          CoreSurfaceBufferLock  *lock )
{
     Mach64DeviceData *mdev   = mdrv->device_data;
     volatile u8      *mmio   = mdrv->mmio_base;
     VideoMode        *mode   = dfb_system_current_mode();
     int               yres   = mode->yres;
     unsigned int      pitch  = lock->pitch / DFB_BYTES_PER_PIXEL( surface->config.format );
     DFBRectangle      source = config->source;
     DFBRectangle      dest   = config->dest;

     DFBRegion dst;
     int h_inc, v_inc;
     u32 lcd_gen_ctrl, vert_stretching;
     u8 ecp_div;

     if (mode->doubled) {
          dest.y *= 2;
          dest.h *= 2;
          yres   *= 2;
     }

     if (config->options & DLOP_DEINTERLACING) {
          source.y /= 2;
          source.h /= 2;
          pitch    *= 2;
     } else
          mov->regs.capture_CONFIG = OVL_BUF_MODE_SINGLE | OVL_BUF_NEXT_BUF0;

     dst.x1 = dest.x;
     dst.y1 = dest.y;
     dst.x2 = dest.x + dest.w - 1;
     dst.y2 = dest.y + dest.h - 1;

     mov->visible = dfb_region_intersect( &dst, 0, 0, mode->xres - 1, yres - 1 );

     if (mode->laced) {
          dest.y /= 2;
          dest.h /= 2;
     }

     ecp_div = (mach64_in_pll( mmio, PLL_VCLK_CNTL ) & ECP_DIV) >> 4;
     h_inc = (source.w << (12 + ecp_div)) / dest.w;

     lcd_gen_ctrl = mach64_in_lcd( mdev, mmio, LCD_GEN_CTRL );
     vert_stretching = mach64_in_lcd( mdev, mmio, VERT_STRETCHING );
     if ((lcd_gen_ctrl & LCD_ON) && (vert_stretching & VERT_STRETCH_EN))
          v_inc = (source.h << 2) * (vert_stretching & VERT_STRETCH_RATIO0) / dest.h;
     else
          v_inc = (source.h << 12) / dest.h;

     switch (surface->config.format) {
          case DSPF_RGB555:
          case DSPF_ARGB1555:
               mov->regs.video_FORMAT = SCALER_IN_RGB15;
               break;
          case DSPF_RGB16:
               mov->regs.video_FORMAT = SCALER_IN_RGB16;
               break;
          case DSPF_RGB32:
               mov->regs.video_FORMAT = SCALER_IN_RGB32;
               break;
          case DSPF_UYVY:
               mov->regs.video_FORMAT = SCALER_IN_YVYU422;
               break;
          case DSPF_YUY2:
               mov->regs.video_FORMAT = SCALER_IN_VYUY422;
               break;
          case DSPF_I420:
          case DSPF_YV12:
               mov->regs.video_FORMAT = SCALER_IN_YUV12;
               break;
          default:
               D_BUG( "unexpected pixelformat" );
     }

     mov->regs.scaler_HEIGHT_WIDTH = (source.w << 16) | source.h;
     mov->regs.scaler_BUF_PITCH    = pitch;

     mov->regs.overlay_Y_X_START   = (dst.x1 << 16) | dst.y1 | OVERLAY_LOCK_START;
     mov->regs.overlay_Y_X_END     = (dst.x2 << 16) | dst.y2;
     mov->regs.overlay_SCALE_INC   = (h_inc << 16) | v_inc;
}

static void ov_calc_buffer( Mach64DriverData       *mdrv,
                            Mach64OverlayLayerData *mov,
                            CoreLayerRegionConfig  *config,
                            CoreSurface            *surface,
                            CoreSurfaceBufferLock  *lock )
{
     unsigned int   pitch  = lock->pitch;
     DFBRectangle   source = config->source;

     u32 offset, offset_u, offset_v;
     int cropleft, croptop;

     if (config->options & DLOP_DEINTERLACING) {
          source.y /= 2;
          source.h /= 2;
          pitch    *= 2;
     }

     /* Source cropping */
     cropleft = source.x;
     croptop  = source.y;

     /* Add destination cropping */
     if (config->dest.x < 0)
          cropleft += -config->dest.x * source.w / config->dest.w;
     if (config->dest.y < 0)
          croptop  += -config->dest.y * source.h / config->dest.h;

     switch (surface->config.format) {
          case DSPF_I420:
               cropleft &= ~15;
               croptop  &= ~1;

               offset_u  = lock->offset + surface->config.size.h * lock->pitch;
               offset_v  = offset_u + surface->config.size.h/2 * lock->pitch/2;
               offset_u += croptop/2 * pitch/2 + cropleft/2;
               offset_v += croptop/2 * pitch/2 + cropleft/2;
               break;

          case DSPF_YV12:
               cropleft &= ~15;
               croptop  &= ~1;

               offset_v  = lock->offset + surface->config.size.h * lock->pitch;
               offset_u  = offset_v + surface->config.size.h/2 * lock->pitch/2;
               offset_v += croptop/2 * pitch/2 + cropleft/2;
               offset_u += croptop/2 * pitch/2 + cropleft/2;
               break;

          default:
               offset_u = 0;
               offset_v = 0;
               break;
     }

     offset  = lock->offset;
     offset += croptop * pitch + cropleft * DFB_BYTES_PER_PIXEL( surface->config.format );

     mov->regs.scaler_BUF0_OFFSET   = offset;
     mov->regs.scaler_BUF0_OFFSET_U = offset_u;
     mov->regs.scaler_BUF0_OFFSET_V = offset_v;

     mov->regs.scaler_BUF1_OFFSET   = offset   + lock->pitch;
     mov->regs.scaler_BUF1_OFFSET_U = offset_u + lock->pitch/2;
     mov->regs.scaler_BUF1_OFFSET_V = offset_v + lock->pitch/2;
}

static u32 ovColorKey[] = {
     VIDEO_KEY_FN_TRUE      | GRAPHICS_KEY_FN_TRUE  | OVERLAY_CMP_MIX_OR, /* 0 */
     VIDEO_KEY_FN_NOT_EQUAL | GRAPHICS_KEY_FN_FALSE | OVERLAY_CMP_MIX_OR, /* DLOP_SRC_COLORKEY */
     VIDEO_KEY_FN_FALSE     | GRAPHICS_KEY_FN_EQUAL | OVERLAY_CMP_MIX_OR, /* DLOP_DST_COLORKEY */
     VIDEO_KEY_FN_NOT_EQUAL | GRAPHICS_KEY_FN_EQUAL | OVERLAY_CMP_MIX_AND /* DLOP_SRC_COLORKEY |
                                                                             DLOP_DST_COLORKEY */
};

static void ov_calc_colorkey( Mach64DriverData       *mdrv,
                              Mach64OverlayLayerData *mov,
                              CoreLayerRegionConfig  *config )
{
     DFBSurfacePixelFormat primary_format = dfb_primary_layer_pixelformat();

     /* Video key is always RGB24 */
     mov->regs.overlay_VIDEO_KEY_CLR = PIXEL_RGB32( config->src_key.r,
                                                    config->src_key.g,
                                                    config->src_key.b );

     /* The same mask is used for all three components */
     mov->regs.overlay_VIDEO_KEY_MSK = 0xFF;

     switch (primary_format) {
          case DSPF_RGB332:
               mov->regs.overlay_GRAPHICS_KEY_CLR = PIXEL_RGB332( config->dst_key.r,
                                                                  config->dst_key.g,
                                                                  config->dst_key.b );
               break;
          case DSPF_RGB555:
               mov->regs.overlay_GRAPHICS_KEY_CLR = PIXEL_RGB555( config->dst_key.r,
                                                                  config->dst_key.g,
                                                                  config->dst_key.b );
               break;
          case DSPF_ARGB1555:
               mov->regs.overlay_GRAPHICS_KEY_CLR = PIXEL_RGB555( config->dst_key.r,
                                                                  config->dst_key.g,
                                                                  config->dst_key.b );
               break;
          case DSPF_RGB16:
               mov->regs.overlay_GRAPHICS_KEY_CLR = PIXEL_RGB16( config->dst_key.r,
                                                                 config->dst_key.g,
                                                                 config->dst_key.b );
               break;
          case DSPF_ARGB:
          case DSPF_RGB32:
               mov->regs.overlay_GRAPHICS_KEY_CLR = PIXEL_RGB32( config->dst_key.r,
                                                                 config->dst_key.g,
                                                                 config->dst_key.b );
               break;
          default:
               D_BUG( "unexpected pixelformat" );
     }

     mov->regs.overlay_GRAPHICS_KEY_MSK = (1 << DFB_COLOR_BITS_PER_PIXEL( primary_format )) - 1;

     mov->regs.overlay_KEY_CNTL = ovColorKey[(config->options >> 3) & 3];
}

static void ov_calc_opacity( Mach64DriverData       *mdrv,
                             Mach64OverlayLayerData *mov,
                             CoreLayerRegionConfig  *config )
{
     mov->regs.overlay_SCALE_CNTL = SCALE_PIX_EXPAND | SCALE_Y2R_TEMP;

     if (config->opacity && mov->visible)
          mov->regs.overlay_SCALE_CNTL |= OVERLAY_EN | SCALE_EN;
}