summaryrefslogtreecommitdiff
path: root/Source/DirectFB/gfxdrivers/unichrome/uc_overlay.c
blob: 4402cb352d2fef48e3f70c086963fcb6beab7217 (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
/*
   Copyright (c) 2003 Andreas Robinson, All rights reserved.

   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.
*/

#include <config.h>

#include <fbdev/fbdev.h>

#include "unichrome.h"
#include "uc_overlay.h"
#include "uc_ioctl.h"
#include "vidregs.h"
#include "mmio.h"

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

#include <direct/messages.h>

#include <core/system.h>

#include <misc/conf.h>

// Forward declaration
static DFBResult
uc_ovl_remove(CoreLayer *layer,
              void      *driver_data,
              void      *layer_data,
              void      *region_data);


static int uc_ovl_datasize( void )
{
    return sizeof(UcOverlayData);
}


static DFBResult
uc_ovl_init_layer( CoreLayer                   *layer,
                   void                        *driver_data,
                   void                        *layer_data,
                   DFBDisplayLayerDescription  *description,
                   DFBDisplayLayerConfig       *config,
                   DFBColorAdjustment          *adjustment )
{
    UcDriverData* ucdrv = (UcDriverData*) driver_data;
    UcOverlayData* ucovl = (UcOverlayData*) layer_data;

    // Save a pointer to the layer data for access by the primary
    // This is needed to properly support levels and the primary alpha channel

    ucdrv->ovl = ucovl;

    // Set layer type, capabilities and name

    description->caps = UC_OVL_CAPS;
    description->type = DLTF_GRAPHICS | DLTF_VIDEO | DLTF_STILL_PICTURE;
    snprintf(description->name,
        DFB_DISPLAY_LAYER_DESC_NAME_LENGTH, "VIA Unichrome Video");

    adjustment->flags = DCAF_NONE;

    // Fill out the default configuration

    config->flags  = DLCONF_WIDTH | DLCONF_HEIGHT |
                     DLCONF_PIXELFORMAT | DLCONF_BUFFERMODE | DLCONF_OPTIONS;

    ucovl->v1.win.w = 720;
    ucovl->v1.win.h = 576;
    ucovl->v1.win.x = 0;
    ucovl->v1.win.y = 0;

    config->width  = 720;
    config->height = 576;

    config->pixelformat = DSPF_YV12;
    config->buffermode  = DLBM_FRONTONLY;
    config->options     = DLOP_NONE;

    // Reset overlay

    ucovl->extfifo_on = false;
    ucovl->hwrev = ucdrv->hwrev;
    ucovl->scrwidth = ucovl->v1.win.w;

    ucovl->v1.isenabled = false;
    ucovl->v1.cfg = *config;
    ucovl->v1.ox = 0;
    ucovl->v1.oy = 0;
    ucovl->v1.dst_key.index = 0;
    ucovl->v1.dst_key.r = 0;
    ucovl->v1.dst_key.g = 0;
    ucovl->v1.dst_key.b = 0;
    ucovl->v1.dstkey_enabled = false;
    ucovl->v1.opacity = 0xff;
    ucovl->v1.level = 1;

//    adjustment->flags = DCAF_BRIGHTNESS | DCAF_CONTRAST |
//        DCAF_HUE | DCAF_SATURATION;
    adjustment->brightness = 0x8000;
    adjustment->contrast = 0x8000;
    adjustment->saturation = 0x8000;
    adjustment->hue = 0x8000;
    ucovl->v1.adj = *adjustment;

    uc_ovl_remove(layer, driver_data, layer_data, NULL);

    return DFB_OK;
}


static DFBResult
uc_ovl_set_region( CoreLayer                  *layer,
                   void                       *driver_data,
                   void                       *layer_data,
                   void                       *region_data,
                   CoreLayerRegionConfig      *config,
                   CoreLayerRegionConfigFlags  updated,
                   CoreSurface                *surface,
                   CorePalette                *palette,
                   CoreSurfaceBufferLock      *lock )
{
    UcDriverData*  ucdrv = (UcDriverData*) driver_data;
    UcOverlayData* ucovl = (UcOverlayData*) layer_data;
    DFBRectangle   win;
    
    /* remember configuration */
    ucovl->config = *config;

    /* get new destination rectangle */
    win = config->dest;

    // Bounds checking
    if ((win.x < -8192) || (win.x > 8192) ||
        (win.y < -8192) || (win.y > 8192) ||
        (win.w < 32) || (win.w > 4096) ||
        (win.h < 32) || (win.h > 4096))
    {
        D_DEBUG("Layer size or position is out of bounds.");
        return DFB_INVAREA;
    }

    ucovl->v1.isenabled = true;
    ucovl->v1.win = win;
    ucovl->v1.dst_key = config->dst_key;
    ucovl->v1.dstkey_enabled = config->options & DLOP_DST_COLORKEY;
    
    if (config->options & DLOP_OPACITY)
        ucovl->v1.opacity = config->opacity;
    else
        ucovl->v1.opacity = 0xff;

    // printf("uc_overlay: color-keying is %s\n",
    //     ucovl->v1.dstkey_enabled ? "enabled" : "disabled");

    ucovl->deinterlace = config->options & DLOP_DEINTERLACING;
    ucovl->surface     = surface;
    ucovl->lock        = lock;

    if (ucdrv->canfliponvsync) {
        FBDev *dfb_fbdev = dfb_system_data();
        int field_option = VIAFB_WAIT_FLIP; // wait for any pending flip
        ioctl(dfb_fbdev->fd, FBIO_WAITFORVSYNC, &field_option);
    }

    return uc_ovl_update(ucdrv, ucovl, UC_OVL_CHANGE, surface, lock);
}


static DFBResult
uc_ovl_remove(CoreLayer *layer,
              void      *driver_data,
              void      *layer_data,
              void      *region_data)
{
    UcDriverData*  ucdrv = (UcDriverData*) driver_data;
    UcOverlayData* ucovl = (UcOverlayData*) layer_data;
    volatile u8*   vio = ucdrv->hwregs;

    ucovl->v1.isenabled = false;

    uc_ovl_vcmd_wait(vio);

    VIDEO_OUT(vio, V_FIFO_CONTROL, UC_MAP_V1_FIFO_CONTROL(16,12,8));
    //  VIDEO_OUT(vio, ALPHA_V3_FIFO_CONTROL, 0x0407181f);

    if (ucovl->hwrev >= 0x10) {
        VIDEO_OUT(vio, V1_ColorSpaceReg_1, ColorSpaceValue_1_3123C0);
        VIDEO_OUT(vio, V1_ColorSpaceReg_2, ColorSpaceValue_2_3123C0);
    }
    else {
        VIDEO_OUT(vio, V1_ColorSpaceReg_1, ColorSpaceValue_1);
        VIDEO_OUT(vio, V1_ColorSpaceReg_2, ColorSpaceValue_2);
    }

    VIDEO_OUT(vio, HQV_CONTROL, VIDEO_IN(vio, HQV_CONTROL) & ~HQV_ENABLE);
    VIDEO_OUT(vio, V1_CONTROL, VIDEO_IN(vio, V1_CONTROL) & ~V1_ENABLE);
    //  VIDEO_OUT(vio, V3_CONTROL, VIDEO_IN(vio, V3_CONTROL) & ~V3_ENABLE);

    VIDEO_OUT(vio, V_COMPOSE_MODE,
        (VIDEO_IN(vio, V_COMPOSE_MODE) & ~ENABLE_COLOR_KEYING) | V1_COMMAND_FIRE);

    ucovl->surface = NULL;

    return DFB_OK;
}


static DFBResult
uc_ovl_test_region(CoreLayer                  *layer,
                   void                       *driver_data,
                   void                       *layer_data,
                   CoreLayerRegionConfig      *config,
                   CoreLayerRegionConfigFlags *failed)
{
    CoreLayerRegionConfigFlags fail = 0;

    // Check layer options

    if (config->options & ~UC_OVL_OPTIONS)
        fail |= CLRCF_OPTIONS;

    // Check pixelformats

    switch (config->format) {
          case DSPF_YUY2:
              break;
          case DSPF_UYVY:
              fail |= CLRCF_FORMAT;   // Nope...  doesn't work.
              break;
          case DSPF_I420:
          case DSPF_YV12:
          case DSPF_ARGB1555:
          case DSPF_RGB16:
          case DSPF_RGB32:
          case DSPF_ARGB:
              break;
          default:
              fail |= CLRCF_FORMAT;
    }

    // Check width and height

    if (config->width > 4096 || config->width < 32)
        fail |= CLRCF_WIDTH;

    if (config->height > 4096 || config->height < 32)
        fail |= CLRCF_HEIGHT;

    if (failed) *failed = fail;
    if (fail) return DFB_UNSUPPORTED;

    return DFB_OK;
}


static DFBResult
uc_ovl_flip_region( CoreLayer             *layer,
                    void                  *driver_data,
                    void                  *layer_data,
                    void                  *region_data,
                    CoreSurface           *surface,
                    DFBSurfaceFlipFlags    flags,
                    CoreSurfaceBufferLock *lock )
{
    //printf("Entering %s ... \n", __PRETTY_FUNCTION__);

    UcDriverData*  ucdrv = (UcDriverData*) driver_data;
    UcOverlayData* ucovl = (UcOverlayData*) layer_data;
    DFBResult    ret;
    FBDev *dfb_fbdev = dfb_system_data();

    dfb_surface_flip(surface, false);

    ucovl->field = 0;
    ucovl->lock = lock;

    if (ucdrv->canfliponvsync)
    {
        if (ucovl->config.options & DLOP_FIELD_PARITY)
        {
            struct fb_flip flip;
            int field_option;

            field_option = VIAFB_WAIT_FLIP; // ensure last pending flip complete
            ioctl(dfb_fbdev->fd, FBIO_WAITFORVSYNC, &field_option);

            flip.device = VIAFB_FLIP_V1;
            flip.field = ucovl->config.parity;
            flip.count = 0; // until we implement this

            uc_ovl_map_buffer(surface->config.format,
                lock->offset,
                ucovl->v1.ox, ucovl->v1.oy, surface->config.size.w, surface->config.size.h,
                lock->pitch, 0,
                &flip.offset[0], &flip.offset[1], &flip.offset[2]);

            ioctl(dfb_fbdev->fd, FBIO_FLIPONVSYNC, &flip);
        }
        else
        {
            ret = uc_ovl_update(ucdrv, ucovl, UC_OVL_FLIP, surface, lock);
            if (ret)
                return ret;
        }
    }
    else
    {
        if (ucovl->config.options & DLOP_FIELD_PARITY)
        {
            int field_option;
        
            if (ucovl->config.parity == 0)  // top field first?
                field_option = VIAFB_WAIT_BOTTOMFIELD;
            else
                field_option = VIAFB_WAIT_TOPFIELD;
            ioctl(dfb_fbdev->fd, FBIO_WAITFORVSYNC, &field_option);
            // that actually waits for VBLANK so we need a further delay
            // to be sure the field has started and that the flip will
            // take effect on the next field
            usleep(2500);
        }
    
        ret = uc_ovl_update(ucdrv, ucovl, UC_OVL_FLIP, surface, lock);
        if (ret)
            return ret;
    }

    if (flags & DSFLIP_WAIT)
        dfb_layer_wait_vsync(layer);

    return DFB_OK;
}

static DFBResult
uc_ovl_get_level(CoreLayer    *layer,
                 void         *driver_data,
                 void         *layer_data,
                 int          *level)
{
    UcOverlayData* ucovl = (UcOverlayData*) layer_data;
    *level = ucovl->v1.level;
    return DFB_OK;
}

static DFBResult
uc_ovl_set_level(CoreLayer    *layer,
                 void         *driver_data,
                 void         *layer_data,
                 int          level)
{
    UcOverlayData* ucovl = (UcOverlayData*) layer_data;
    UcDriverData*  ucdrv = (UcDriverData*) driver_data;

    if (level == 0) return DFB_INVARG;
    
    if (level < 0) {
        // Enable underlay mode.
        VIDEO_OUT(ucdrv->hwregs, V_ALPHA_CONTROL,
            uc_ovl_map_alpha(ucovl->opacity_primary));
    }
    else {
        // Enable overlay mode (default)
        VIDEO_OUT(ucdrv->hwregs, V_ALPHA_CONTROL,
            uc_ovl_map_alpha(ucovl->v1.opacity));
    }
    VIDEO_OUT(ucdrv->hwregs, V_COMPOSE_MODE, V1_COMMAND_FIRE |
        (ucovl->v1.dstkey_enabled ? ENABLE_COLOR_KEYING : 0));

    ucovl->v1.level = level;
    return DFB_OK;
}

static DFBResult
uc_ovl_set_input_field( CoreLayer *layer,
                        void      *driver_data,
                        void      *layer_data,
                        void      *region_data,
                        int        field )
{
     UcOverlayData* ucovl = (UcOverlayData*) layer_data;
     UcDriverData*  ucdrv = (UcDriverData*) driver_data;

     ucovl->field = field;

     return uc_ovl_update(ucdrv, ucovl, UC_OVL_FIELD, ucovl->surface, ucovl->lock);
}

DisplayLayerFuncs ucOverlayFuncs = {
     .LayerDataSize      = uc_ovl_datasize,
     .InitLayer          = uc_ovl_init_layer,
     .SetRegion          = uc_ovl_set_region,
     .RemoveRegion       = uc_ovl_remove,
     .TestRegion         = uc_ovl_test_region,
     .FlipRegion         = uc_ovl_flip_region,
     .GetLevel           = uc_ovl_get_level,
     .SetLevel           = uc_ovl_set_level,
     .SetInputField      = uc_ovl_set_input_field,
//   .SetColorAdjustment = uc_ovl_set_adjustment,
};