sunxi_disp.c 31.8 KB
Newer Older
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
/*
 * Copyright © 2013 Siarhei Siamashka <siarhei.siamashka@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <inttypes.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

#include "sunxi_disp.h"
#include "sunxi_disp_ioctl.h"
35
#include "g2d_driver.h"
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
static int sunxi_gfx_layer_change_work_mode(sunxi_disp_t *ctx, int new_mode)
{
    __disp_layer_info_t layer_info;
    uint32_t tmp[4];

    if (ctx->layer_id < 0)
        return -1;

    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->gfx_layer_id;
    tmp[2] = (uintptr_t)&layer_info;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_GET_PARA, tmp) < 0)
        return -1;

    layer_info.mode = new_mode;

    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->gfx_layer_id;
    tmp[2] = (uintptr_t)&layer_info;
    return ioctl(ctx->fd_disp, DISP_CMD_LAYER_SET_PARA, tmp);
}


62
sunxi_disp_t *sunxi_disp_init(const char *device, void *xserver_fbmem)
63
64
65
66
67
68
69
70
71
{
    sunxi_disp_t *ctx = calloc(sizeof(sunxi_disp_t), 1);
    struct fb_var_screeninfo fb_var;
    struct fb_fix_screeninfo fb_fix;

    int tmp, version;
    int gfx_layer_size;
    int ovl_layer_size;

72
73
74
75
    /* use /dev/fb0 by default */
    if (!device)
        device = "/dev/fb0";

76
77
78
79
80
81
82
83
84
85
86
87
    if (strcmp(device, "/dev/fb0") == 0) {
        ctx->fb_id = 0;
    }
    else if (strcmp(device, "/dev/fb1") == 0) {
        ctx->fb_id = 1;
    }
    else
    {
        free(ctx);
        return NULL;
    }

88
89
90
    /* store the already existing mapping done by xserver */
    ctx->xserver_fbmem = xserver_fbmem;

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
    ctx->fd_disp = open("/dev/disp", O_RDWR);

    /* maybe it's even not a sunxi hardware */
    if (ctx->fd_disp < 0) {
        free(ctx);
        return NULL;
    }

    /* version check */
    tmp = SUNXI_DISP_VERSION;
    version = ioctl(ctx->fd_disp, DISP_CMD_VERSION, &tmp);
    if (version < 0) {
        close(ctx->fd_disp);
        free(ctx);
        return NULL;
    }

    ctx->fd_fb = open(device, O_RDWR);
    if (ctx->fd_fb < 0) {
        close(ctx->fd_disp);
        free(ctx);
        return NULL;
    }

    if (ioctl(ctx->fd_fb, FBIOGET_VSCREENINFO, &fb_var) < 0 ||
        ioctl(ctx->fd_fb, FBIOGET_FSCREENINFO, &fb_fix) < 0)
    {
        close(ctx->fd_fb);
        close(ctx->fd_disp);
        free(ctx);
        return NULL;
    }

    ctx->xres = fb_var.xres;
    ctx->yres = fb_var.yres;
    ctx->bits_per_pixel = fb_var.bits_per_pixel;
    ctx->framebuffer_paddr = fb_fix.smem_start;
    ctx->framebuffer_size = fb_fix.smem_len;
129
130
    ctx->framebuffer_height = ctx->framebuffer_size /
                              (ctx->xres * ctx->bits_per_pixel / 8);
131
132
133
134
135
136
137
138
139
    ctx->gfx_layer_size = ctx->xres * ctx->yres * fb_var.bits_per_pixel / 8;

    if (ctx->framebuffer_size < ctx->gfx_layer_size) {
        close(ctx->fd_fb);
        close(ctx->fd_disp);
        free(ctx);
        return NULL;
    }

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    if (ctx->xserver_fbmem) {
        /* use already existing mapping */
        ctx->framebuffer_addr = ctx->xserver_fbmem;
    }
    else {
        /* mmap framebuffer memory */
        ctx->framebuffer_addr = (uint8_t *)mmap(0, ctx->framebuffer_size,
                                                PROT_READ | PROT_WRITE,
                                                MAP_SHARED, ctx->fd_fb, 0);
        if (ctx->framebuffer_addr == MAP_FAILED) {
            close(ctx->fd_fb);
            close(ctx->fd_disp);
            free(ctx);
            return NULL;
        }
155
156
157
158
159
160
    }

    ctx->cursor_enabled = 0;
    ctx->cursor_x = -1;
    ctx->cursor_y = -1;

161
162
163
164
165
166
167
168
169
170
171
    /* Get the id of the screen layer */
    if (ioctl(ctx->fd_fb,
              ctx->fb_id == 0 ? FBIOGET_LAYER_HDL_0 : FBIOGET_LAYER_HDL_1,
              &ctx->gfx_layer_id))
    {
        close(ctx->fd_fb);
        close(ctx->fd_disp);
        free(ctx);
        return NULL;
    }

172
173
174
    /* Try to enable scaler for the screen layer */
    sunxi_gfx_layer_change_work_mode(ctx, DISP_LAYER_WORK_MODE_SCALER);

175
176
177
178
179
180
181
182
    if (sunxi_layer_reserve(ctx) < 0)
    {
        close(ctx->fd_fb);
        close(ctx->fd_disp);
        free(ctx);
        return NULL;
    }

183
184
    ctx->fd_g2d = open("/dev/g2d", O_RDWR);

185
186
187
    ctx->blt2d.self = ctx;
    ctx->blt2d.overlapped_blt = sunxi_g2d_blt;

188
189
190
191
192
193
    return ctx;
}

int sunxi_disp_close(sunxi_disp_t *ctx)
{
    if (ctx->fd_disp >= 0) {
194
195
196
        if (ctx->fd_g2d >= 0) {
            close(ctx->fd_g2d);
        }
197
198
199
200
201
202
        /* release layer */
        sunxi_layer_release(ctx);
        /* disable cursor */
        if (ctx->cursor_enabled)
            sunxi_hw_cursor_hide(ctx);
        /* close descriptors */
203
204
        if (!ctx->xserver_fbmem)
            munmap(ctx->framebuffer_addr, ctx->framebuffer_size);
205
206
207
        close(ctx->fd_fb);
        close(ctx->fd_disp);
        ctx->fd_disp = -1;
208
        free(ctx);
209
210
211
212
213
214
215
216
217
    }
    return 0;
}

/*****************************************************************************
 * Support for hardware cursor, which has 64x64 size, 2 bits per pixel,      *
 * four 32-bit ARGB entries in the palette.                                  *
 *****************************************************************************/

218
int sunxi_hw_cursor_load_64x64x2bpp(sunxi_disp_t *ctx, uint8_t pixeldata[1024])
219
220
221
222
223
224
225
226
227
228
{
    uint32_t tmp[4];
    __disp_hwc_pattern_t hwc;
    hwc.addr = (uintptr_t)&pixeldata[0];
    hwc.pat_mode = DISP_HWC_MOD_H64_V64_2BPP;
    tmp[0] = ctx->fb_id;
    tmp[1] = (uintptr_t)&hwc;
    return ioctl(ctx->fd_disp, DISP_CMD_HWC_SET_FB, &tmp);
}

229
230
231
232
233
234
235
236
237
238
239
240
int sunxi_hw_cursor_load_32x32x8bpp(sunxi_disp_t *ctx, uint8_t pixeldata[1024])
{
    uint32_t tmp[4];
    __disp_hwc_pattern_t hwc;
    hwc.addr = (uintptr_t)&pixeldata[0];
    hwc.pat_mode = DISP_HWC_MOD_H32_V32_8BPP;
    tmp[0] = ctx->fb_id;
    tmp[1] = (uintptr_t)&hwc;
    return ioctl(ctx->fd_disp, DISP_CMD_HWC_SET_FB, &tmp);
}

int sunxi_hw_cursor_load_palette(sunxi_disp_t *ctx, uint32_t *palette, int n)
241
242
243
{
    uint32_t tmp[4];
    tmp[0] = ctx->fb_id;
244
    tmp[1] = (uintptr_t)palette;
245
    tmp[2] = 0;
246
    tmp[3] = n * sizeof(uint32_t);
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
    return ioctl(ctx->fd_disp, DISP_CMD_HWC_SET_PALETTE_TABLE, &tmp);
}

int sunxi_hw_cursor_set_position(sunxi_disp_t *ctx, int x, int y)
{
    int result;
    uint32_t tmp[4];
    __disp_pos_t pos = { x, y };
    tmp[0] = ctx->fb_id;
    tmp[1] = (uintptr_t)&pos;
    if (pos.x < 0)
        pos.x = 0;
    if (pos.y < 0)
        pos.y = 0;
    result = ioctl(ctx->fd_disp, DISP_CMD_HWC_SET_POS, &tmp);
    if (result >= 0) {
        ctx->cursor_x = pos.x;
        ctx->cursor_y = pos.y;
    }
    return result;
}

int sunxi_hw_cursor_show(sunxi_disp_t *ctx)
{
    int result;
    uint32_t tmp[4];
    tmp[0] = ctx->fb_id;
    result = ioctl(ctx->fd_disp, DISP_CMD_HWC_OPEN, &tmp);
    if (result >= 0)
        ctx->cursor_enabled = 1;
    return result;
}

int sunxi_hw_cursor_hide(sunxi_disp_t *ctx)
{
    int result;
    uint32_t tmp[4];
    tmp[0] = ctx->fb_id;
    result = ioctl(ctx->fd_disp, DISP_CMD_HWC_CLOSE, &tmp);
    if (result >= 0)
        ctx->cursor_enabled = 0;
    return result;
}

/*****************************************************************************
 * Support for scaled layers                                                 *
 *****************************************************************************/

295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
static int sunxi_layer_change_work_mode(sunxi_disp_t *ctx, int new_mode)
{
    __disp_layer_info_t layer_info;
    uint32_t tmp[4];

    if (ctx->layer_id < 0)
        return -1;

    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    tmp[2] = (uintptr_t)&layer_info;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_GET_PARA, tmp) < 0)
        return -1;

    layer_info.mode = new_mode;

    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    tmp[2] = (uintptr_t)&layer_info;
    return ioctl(ctx->fd_disp, DISP_CMD_LAYER_SET_PARA, tmp);
}

317
318
319
320
321
322
323
324
int sunxi_layer_reserve(sunxi_disp_t *ctx)
{
    __disp_layer_info_t layer_info;
    uint32_t tmp[4];

    /* try to allocate a layer */

    tmp[0] = ctx->fb_id;
325
    tmp[1] = DISP_LAYER_WORK_MODE_SCALER;
326
327
328
329
    ctx->layer_id = ioctl(ctx->fd_disp, DISP_CMD_LAYER_REQUEST, &tmp);
    if (ctx->layer_id < 0)
        return -1;

330
    /* Initially set the layer configuration to something reasonable */
331
332
333
334
335

    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    tmp[2] = (uintptr_t)&layer_info;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_GET_PARA, tmp) < 0)
336
        return -1;
337

338
339
340
341
    /* the screen and overlay layers need to be in different pipes */
    layer_info.pipe      = 1;
    layer_info.alpha_en  = 1;
    layer_info.alpha_val = 255;
342

343
344
345
346
347
348
349
    layer_info.fb.addr[0] = ctx->framebuffer_paddr;
    layer_info.fb.size.width = 1;
    layer_info.fb.size.height = 1;
    layer_info.fb.format = DISP_FORMAT_ARGB8888;
    layer_info.fb.seq = DISP_SEQ_ARGB;
    layer_info.fb.mode = DISP_MOD_INTERLEAVED;

350
351
352
    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    tmp[2] = (uintptr_t)&layer_info;
353
354
355
356
357
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_SET_PARA, tmp) < 0)
        return -1;

    /* Now probe the scaler mode to see if there is a free scaler available */
    if (sunxi_layer_change_work_mode(ctx, DISP_LAYER_WORK_MODE_SCALER) == 0)
358
359
        ctx->layer_has_scaler = 1;

360
    /* Revert back to normal mode */
361
    sunxi_layer_change_work_mode(ctx, DISP_LAYER_WORK_MODE_SCALER);
362
363
364
    ctx->layer_scaler_is_enabled = 0;
    ctx->layer_format = DISP_FORMAT_ARGB8888;

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    return ctx->layer_id;
}


int sunxi_layer_release(sunxi_disp_t *ctx)
{
    int result;
    uint32_t tmp[4];

    if (ctx->layer_id < 0)
        return -1;

    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    ioctl(ctx->fd_disp, DISP_CMD_LAYER_RELEASE, &tmp);

    ctx->layer_id = -1;
    ctx->layer_has_scaler = 0;
    return 0;
}

386
387
388
389
390
391
int sunxi_layer_set_rgb_input_buffer(sunxi_disp_t *ctx,
                                     int           bpp,
                                     uint32_t      offset_in_framebuffer,
                                     int           width,
                                     int           height,
                                     int           stride)
392
393
394
395
396
397
398
399
400
{
    __disp_fb_t fb;
    __disp_rect_t rect = { 0, 0, width, height };
    uint32_t tmp[4];
    memset(&fb, 0, sizeof(fb));

    if (ctx->layer_id < 0)
        return -1;

401
    if (ctx->layer_scaler_is_enabled) {
402
        if (sunxi_layer_change_work_mode(ctx, DISP_LAYER_WORK_MODE_SCALER) == 0)
403
404
405
406
407
            ctx->layer_scaler_is_enabled = 0;
        else
            return -1;
    }

408
409
    fb.addr[0] = ctx->framebuffer_paddr + offset_in_framebuffer;
    fb.size.height = height;
410
411
412
413
414
415
416
417
418
419
420
421
422
    if (bpp == 32) {
        fb.format = DISP_FORMAT_ARGB8888;
        fb.seq = DISP_SEQ_ARGB;
        fb.mode = DISP_MOD_INTERLEAVED;
        fb.size.width = stride;
    } else if (bpp == 16) {
        fb.format = DISP_FORMAT_RGB565;
        fb.seq = DISP_SEQ_P10;
        fb.mode = DISP_MOD_INTERLEAVED;
        fb.size.width = stride * 2;
    } else {
        return -1;
    }
423
424
425
426
427
428
429

    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    tmp[2] = (uintptr_t)&fb;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_SET_FB, &tmp) < 0)
        return -1;

430
431
432
433
    ctx->layer_buf_x = rect.x;
    ctx->layer_buf_y = rect.y;
    ctx->layer_buf_w = rect.width;
    ctx->layer_buf_h = rect.height;
434
435
    ctx->layer_format = fb.format;

436
437
438
439
440
441
    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    tmp[2] = (uintptr_t)&rect;
    return ioctl(ctx->fd_disp, DISP_CMD_LAYER_SET_SRC_WINDOW, &tmp);
}

442
443
444
445
446
447
int sunxi_layer_set_yuv420_input_buffer(sunxi_disp_t *ctx,
                                        uint32_t      y_offset_in_framebuffer,
                                        uint32_t      u_offset_in_framebuffer,
                                        uint32_t      v_offset_in_framebuffer,
                                        int           width,
                                        int           height,
448
449
450
                                        int           stride,
                                        int           x_pixel_offset,
                                        int           y_pixel_offset)
451
452
{
    __disp_fb_t fb;
453
    __disp_rect_t rect = { x_pixel_offset, y_pixel_offset, width, height };
454
455
456
457
458
459
    uint32_t tmp[4];
    memset(&fb, 0, sizeof(fb));

    if (ctx->layer_id < 0)
        return -1;

460
461
462
463
464
465
466
    if (!ctx->layer_scaler_is_enabled) {
        if (sunxi_layer_change_work_mode(ctx, DISP_LAYER_WORK_MODE_SCALER) == 0)
            ctx->layer_scaler_is_enabled = 1;
        else
            return -1;
    }

467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
    fb.addr[0] = ctx->framebuffer_paddr + y_offset_in_framebuffer;
    fb.addr[1] = ctx->framebuffer_paddr + u_offset_in_framebuffer;
    fb.addr[2] = ctx->framebuffer_paddr + v_offset_in_framebuffer;
    fb.size.width = stride;
    fb.size.height = height;
    fb.format = DISP_FORMAT_YUV420;
    fb.seq = DISP_SEQ_P3210;
    fb.mode = DISP_MOD_NON_MB_PLANAR;

    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    tmp[2] = (uintptr_t)&fb;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_SET_FB, &tmp) < 0)
        return -1;

482
483
484
485
    ctx->layer_buf_x = rect.x;
    ctx->layer_buf_y = rect.y;
    ctx->layer_buf_w = rect.width;
    ctx->layer_buf_h = rect.height;
486
487
    ctx->layer_format = fb.format;

488
489
490
491
492
493
    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    tmp[2] = (uintptr_t)&rect;
    return ioctl(ctx->fd_disp, DISP_CMD_LAYER_SET_SRC_WINDOW, &tmp);
}

494
495
int sunxi_layer_set_output_window(sunxi_disp_t *ctx, int x, int y, int w, int h)
{
496
497
498
499
500
    __disp_rect_t buf_rect = {
        ctx->layer_buf_x, ctx->layer_buf_y,
        ctx->layer_buf_w, ctx->layer_buf_h
    };
    __disp_rect_t win_rect = { x, y, w, h };
501
    uint32_t tmp[4];
502
    int err;
503

504
    if (ctx->layer_id < 0 || w <= 0 || h <= 0)
505
506
        return -1;

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
    /*
     * Handle negative window Y coordinates (workaround a bug).
     * The Allwinner A10/A13 display controller hardware is expected to
     * support negative coordinates of the top left corners of the layers.
     * But there is some bug either in the kernel driver or in the hardware,
     * which messes up the picture on screen when the Y coordinate is negative
     * for YUV layer. Negative X coordinates are not affected. RGB formats
     * are not affected too.
     *
     * We fix this by just recalculating which part of the buffer in memory
     * corresponds to Y=0 on screen and adjust the input buffer settings.
     */
    if (ctx->layer_format == DISP_FORMAT_YUV420 &&
                                  (y < 0 || ctx->layer_win_y < 0)) {
        if (win_rect.y < 0) {
            int y_shift = -(double)y * buf_rect.height / win_rect.height;
            buf_rect.y      += y_shift;
            buf_rect.height -= y_shift;
            win_rect.height += win_rect.y;
            win_rect.y       = 0;
        }

        if (buf_rect.height <= 0 || win_rect.height <= 0) {
            /* No part of the window is visible. Just construct a fake rectangle
             * outside the screen as a window placement (but with a non-negative Y
             * coordinate). Do this to avoid passing bogus negative heights to
             * the kernel driver (who knows how it would react?) */
            win_rect.x = -1;
            win_rect.y = 0;
            win_rect.width = 1;
            win_rect.height = 1;
            tmp[0] = ctx->fb_id;
            tmp[1] = ctx->layer_id;
            tmp[2] = (uintptr_t)&win_rect;
            return ioctl(ctx->fd_disp, DISP_CMD_LAYER_SET_SCN_WINDOW, &tmp);
        }

        tmp[0] = ctx->fb_id;
        tmp[1] = ctx->layer_id;
        tmp[2] = (uintptr_t)&buf_rect;
        if ((err = ioctl(ctx->fd_disp, DISP_CMD_LAYER_SET_SRC_WINDOW, &tmp)))
            return err;
    }
    /* Save the new non-adjusted window position */
    ctx->layer_win_x = x;
    ctx->layer_win_y = y;

554
555
    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
556
    tmp[2] = (uintptr_t)&win_rect;
557
558
559
560
561
562
563
564
565
566
    return ioctl(ctx->fd_disp, DISP_CMD_LAYER_SET_SCN_WINDOW, &tmp);
}

int sunxi_layer_show(sunxi_disp_t *ctx)
{
    uint32_t tmp[4];

    if (ctx->layer_id < 0)
        return -1;

567
568
569
570
571
572
    /* YUV formats need to use a scaler */
    if (ctx->layer_format == DISP_FORMAT_YUV420 && !ctx->layer_scaler_is_enabled) {
        if (sunxi_layer_change_work_mode(ctx, DISP_LAYER_WORK_MODE_SCALER) == 0)
            ctx->layer_scaler_is_enabled = 1;
    }

573
574
575
576
577
578
579
580
581
582
583
584
585
    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    return ioctl(ctx->fd_disp, DISP_CMD_LAYER_OPEN, &tmp);
}

int sunxi_layer_hide(sunxi_disp_t *ctx)
{
    int result;
    uint32_t tmp[4];

    if (ctx->layer_id < 0)
        return -1;

586
587
    /* If the layer is hidden, there is no need to keep the scaler occupied */
    if (ctx->layer_scaler_is_enabled) {
588
        if (sunxi_layer_change_work_mode(ctx, DISP_LAYER_WORK_MODE_SCALER) == 0)
589
590
591
            ctx->layer_scaler_is_enabled = 0;
    }

592
593
594
595
596
    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    return ioctl(ctx->fd_disp, DISP_CMD_LAYER_CLOSE, &tmp);
}

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
int sunxi_layer_set_colorkey(sunxi_disp_t *ctx, uint32_t color)
{
    uint32_t tmp[4];
    __disp_colorkey_t colorkey;
    __disp_color_t disp_color;

    disp_color.alpha = (color >> 24) & 0xFF;
    disp_color.red   = (color >> 16) & 0xFF;
    disp_color.green = (color >> 8)  & 0xFF;
    disp_color.blue  = (color >> 0)  & 0xFF;

    colorkey.ck_min = disp_color;
    colorkey.ck_max = disp_color;
    colorkey.red_match_rule   = 2;
    colorkey.green_match_rule = 2;
    colorkey.blue_match_rule  = 2;

    tmp[0] = ctx->fb_id;
    tmp[1] = (uintptr_t)&colorkey;
    if (ioctl(ctx->fd_disp, DISP_CMD_SET_COLORKEY, &tmp))
        return -1;

    /* Set the overlay layer below the screen layer */
    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->gfx_layer_id;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_BOTTOM, &tmp) < 0)
        return -1;

    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_BOTTOM, &tmp) < 0)
        return -1;

    /* Enable color key for the overlay layer */
    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_CK_ON, &tmp) < 0)
        return -1;

    /* Disable color key and enable global alpha for the screen layer */
    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->gfx_layer_id;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_CK_OFF, &tmp) < 0)
        return -1;

    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->gfx_layer_id;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_ALPHA_ON, &tmp) < 0)
        return -1;

    return 0;
}

int sunxi_layer_disable_colorkey(sunxi_disp_t *ctx)
{
    uint32_t tmp[4];

    /* Disable color key for the overlay layer */
    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_CK_OFF, &tmp) < 0)
        return -1;

    /* Set the overlay layer above the screen layer */
    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->layer_id;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_BOTTOM, &tmp) < 0)
        return -1;

    tmp[0] = ctx->fb_id;
    tmp[1] = ctx->gfx_layer_id;
    if (ioctl(ctx->fd_disp, DISP_CMD_LAYER_BOTTOM, &tmp) < 0)
        return -1;

    return 0;
}

674
675
676
677
678
679
/*****************************************************************************/

int sunxi_wait_for_vsync(sunxi_disp_t *ctx)
{
    return ioctl(ctx->fd_fb, FBIO_WAITFORVSYNC, 0);
}
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751

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

int sunxi_g2d_fill_a8r8g8b8(sunxi_disp_t *disp,
                            int           x,
                            int           y,
                            int           w,
                            int           h,
                            uint32_t      color)
{
    g2d_fillrect tmp;

    if (disp->fd_g2d < 0)
        return -1;

    if (w <= 0 || h <= 0)
        return 0;

    tmp.flag                = G2D_FIL_NONE;
    tmp.dst_image.addr[0]   = disp->framebuffer_paddr;
    tmp.dst_image.w         = disp->xres;
    tmp.dst_image.h         = disp->framebuffer_height;
    tmp.dst_image.format    = G2D_FMT_ARGB_AYUV8888;
    tmp.dst_image.pixel_seq = G2D_SEQ_NORMAL;
    tmp.dst_rect.x          = x;
    tmp.dst_rect.y          = y;
    tmp.dst_rect.w          = w;
    tmp.dst_rect.h          = h;
    tmp.color               = color;
    tmp.alpha               = 0;

    return ioctl(disp->fd_g2d, G2D_CMD_FILLRECT, &tmp);
}

int sunxi_g2d_blit_a8r8g8b8(sunxi_disp_t *disp,
                            int           dst_x,
                            int           dst_y,
                            int           src_x,
                            int           src_y,
                            int           w,
                            int           h)
{
    g2d_blt tmp;

    if (disp->fd_g2d < 0)
        return -1;

    if (w <= 0 || h <= 0)
        return 0;

    tmp.flag                = G2D_BLT_NONE;
    tmp.src_image.addr[0]   = disp->framebuffer_paddr;
    tmp.src_image.w         = disp->xres;
    tmp.src_image.h         = disp->framebuffer_height;
    tmp.src_image.format    = G2D_FMT_ARGB_AYUV8888;
    tmp.src_image.pixel_seq = G2D_SEQ_NORMAL;
    tmp.src_rect.x          = src_x;
    tmp.src_rect.y          = src_y;
    tmp.src_rect.w          = w;
    tmp.src_rect.h          = h;
    tmp.dst_image.addr[0]   = disp->framebuffer_paddr;
    tmp.dst_image.w         = disp->xres;
    tmp.dst_image.h         = disp->framebuffer_height;
    tmp.dst_image.format    = G2D_FMT_ARGB_AYUV8888;
    tmp.dst_image.pixel_seq = G2D_SEQ_NORMAL;
    tmp.dst_x               = dst_x;
    tmp.dst_y               = dst_y;
    tmp.color               = 0;
    tmp.alpha               = 0;

    return ioctl(disp->fd_g2d, G2D_CMD_BITBLT, &tmp);
}
752

753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/*
 * The following function implements a 16bpp blit using 32bpp mode by
 * splitting the area into an aligned middle part (which is blit using
 * 32bpp mode) and left and right edges if required.
 *
 * It assumes the parameters have already been validated by the caller.
 * This includes the condition (src_x & 1) == (dst_x & 1), which is
 * necessary to be able to use 32bpp mode.
 */

int sunxi_g2d_blit_r5g6b5_in_three(sunxi_disp_t *disp, uint8_t *src_bits,
    uint8_t *dst_bits, int src_stride, int dst_stride, int src_x, int src_y,
    int dst_x, int dst_y, int w, int h)
{
    g2d_blt tmp;
    /* Set up the invariant blit parameters. */
    tmp.flag                = G2D_BLT_NONE;
    tmp.src_image.h         = src_y + h;
    tmp.src_rect.y          = src_y;
    tmp.src_rect.h          = h;
    tmp.dst_image.h         = dst_y + h;
    tmp.dst_y               = dst_y;
    tmp.color               = 0;
    tmp.alpha               = 0;

    if (src_x & 1) {
        tmp.src_image.addr[0]   = disp->framebuffer_paddr +
                                  (src_bits - disp->framebuffer_addr);
        tmp.src_image.format    = G2D_FMT_RGB565;
        tmp.src_image.pixel_seq = G2D_SEQ_P10;
        tmp.src_image.w         = src_stride * 2;
        tmp.src_rect.x          = src_x;
        tmp.src_rect.w          = 1;
        tmp.dst_image.addr[0]   = disp->framebuffer_paddr +
                                  (dst_bits - disp->framebuffer_addr);
        tmp.dst_image.format    = G2D_FMT_RGB565;
        tmp.dst_image.pixel_seq = G2D_SEQ_P10;
        tmp.dst_image.w         = dst_stride * 2;
        tmp.dst_x               = dst_x;
        if (ioctl(disp->fd_g2d, G2D_CMD_BITBLT, &tmp))
            return 0;
        src_x++;
        dst_x++;
        w--;
    }
    if (w >= 2) {
        int w2;
        tmp.src_image.addr[0]   = disp->framebuffer_paddr +
                                  (src_bits - disp->framebuffer_addr);
        tmp.src_image.format    = G2D_FMT_ARGB_AYUV8888;
        tmp.src_image.pixel_seq = G2D_SEQ_NORMAL;
        tmp.src_image.w         = src_stride;
        tmp.src_rect.x          = src_x >> 1;
        tmp.src_rect.w          = w >> 1;
        tmp.dst_image.addr[0]   = disp->framebuffer_paddr +
                                  (dst_bits - disp->framebuffer_addr);
        tmp.dst_image.format    = G2D_FMT_ARGB_AYUV8888;
        tmp.dst_image.pixel_seq = G2D_SEQ_NORMAL;
        tmp.dst_image.w         = dst_stride;
        tmp.dst_x               = dst_x >> 1;
        if (ioctl(disp->fd_g2d, G2D_CMD_BITBLT, &tmp))
            return 0;
        w2 = (w >> 1) * 2;
        src_x += w2;
        dst_x += w2;
        w &= 1;
    }
    if (w) {
        tmp.src_image.addr[0]   = disp->framebuffer_paddr +
                                  (src_bits - disp->framebuffer_addr);
        tmp.src_image.format    = G2D_FMT_RGB565;
        tmp.src_image.pixel_seq = G2D_SEQ_P10;
        tmp.src_image.w         = src_stride * 2;
        tmp.src_rect.x          = src_x;
        tmp.src_rect.w          = 1;
        tmp.dst_image.addr[0]   = disp->framebuffer_paddr +
                                  (dst_bits - disp->framebuffer_addr);

        tmp.dst_image.format    = G2D_FMT_RGB565;
        tmp.dst_image.pixel_seq = G2D_SEQ_P10;
        tmp.dst_image.w         = dst_stride * 2;
        tmp.dst_x               = dst_x;
        if (ioctl(disp->fd_g2d, G2D_CMD_BITBLT, &tmp))
            return 0;
    }
    return 1;
}

841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
static inline int sunxi_g2d_try_fallback_blt(void               *self,
                                             uint32_t           *src_bits,
                                             uint32_t           *dst_bits,
                                             int                 src_stride,
                                             int                 dst_stride,
                                             int                 src_bpp,
                                             int                 dst_bpp,
                                             int                 src_x,
                                             int                 src_y,
                                             int                 dst_x,
                                             int                 dst_y,
                                             int                 w,
                                             int                 h)
{
    sunxi_disp_t *disp = (sunxi_disp_t *)self;
    if (disp->fallback_blt2d)
        return disp->fallback_blt2d->overlapped_blt(disp->fallback_blt2d->self,
                                                    src_bits, dst_bits,
                                                    src_stride, dst_stride,
                                                    src_bpp, dst_bpp,
                                                    src_x, src_y,
                                                    dst_x, dst_y, w, h);
    return 0;

}

#define FALLBACK_BLT() sunxi_g2d_try_fallback_blt(self, src_bits,        \
                                                  dst_bits, src_stride,  \
                                                  dst_stride, src_bpp,   \
                                                  dst_bpp, src_x, src_y, \
                                                  dst_x, dst_y, w, h);

873
874
875
876
877
878
879
880
/*
 * G2D counterpart for pixman_blt (function arguments are the same with
 * only sunxi_disp_t extra argument added). Supports 16bpp (r5g6b5) and
 * 32bpp (a8r8g8b8) formats and also conversion between them.
 *
 * Can do G2D accelerated blits only if both source and destination
 * buffers are inside framebuffer. Returns FALSE (0) otherwise.
 */
881
int sunxi_g2d_blt(void               *self,
882
883
884
885
886
887
888
889
890
891
892
893
894
                  uint32_t           *src_bits,
                  uint32_t           *dst_bits,
                  int                 src_stride,
                  int                 dst_stride,
                  int                 src_bpp,
                  int                 dst_bpp,
                  int                 src_x,
                  int                 src_y,
                  int                 dst_x,
                  int                 dst_y,
                  int                 w,
                  int                 h)
{
895
    sunxi_disp_t *disp = (sunxi_disp_t *)self;
896
    int blt_size_threshold;
897
    g2d_blt tmp;
898
899
900
901
902

    /* Zero size blit, nothing to do */
    if (w <= 0 || h <= 0)
        return 1;

903
904
905
906
907
908
909
910
911
912
913
914
    /*
     * Very minimal validation here. We just assume that if the begginging
     * of both source and destination images belongs to the framebuffer,
     * then these images are entirely residing inside the framebuffer
     * without crossing its borders. Any other checks are supposed
     * to be done by the caller.
     */
    if ((uint8_t *)src_bits < disp->framebuffer_addr ||
        (uint8_t *)src_bits >= disp->framebuffer_addr + disp->framebuffer_size ||
        (uint8_t *)dst_bits < disp->framebuffer_addr ||
        (uint8_t *)dst_bits >= disp->framebuffer_addr + disp->framebuffer_size)
    {
915
        return FALLBACK_BLT();
916
917
    }

918
919
920
921
922
923
924
925
926
927
    /*
     * If the area is smaller than G2D_BLT_SIZE_THRESHOLD, prefer to avoid the
     * overhead of G2D and do a CPU blit instead. There is a special threshold
     * for 16bpp to 16bpp copy.
     */
    if (src_bpp == 16 && dst_bpp == 16)
        blt_size_threshold = G2D_BLT_SIZE_THRESHOLD_16BPP;
    else
        blt_size_threshold = G2D_BLT_SIZE_THRESHOLD;
    if (w * h < blt_size_threshold)
928
        return FALLBACK_BLT();
929

930
931
    /* Unsupported overlapping type */
    if (src_bits == dst_bits && src_y == dst_y && src_x + 1 < dst_x)
932
        return FALLBACK_BLT();
933

934
    if (disp->fd_g2d < 0)
935
        return FALLBACK_BLT();
936

937
938
939
940
941
942
943
944
945
    /* Do a 16-bit using 32-bit mode if possible. */
    if (src_bpp == 16 && dst_bpp == 16 && (src_x & 1) == (dst_x & 1))
        /* Check whether the overlapping type is supported, the condition */
        /* is slightly different compared to the regular blit. */
        if (!(src_bits == dst_bits && src_y == dst_y && src_x < dst_x))
            return sunxi_g2d_blit_r5g6b5_in_three(disp, (uint8_t *)src_bits,
                (uint8_t *)dst_bits, src_stride, dst_stride, src_x, src_y,
                dst_x, dst_y, w, h);

946
    if ((src_bpp != 16 && src_bpp != 32) || (dst_bpp != 16 && dst_bpp != 32))
947
        return FALLBACK_BLT();
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987

    tmp.flag                    = G2D_BLT_NONE;
    tmp.src_image.addr[0]       = disp->framebuffer_paddr +
                                  ((uint8_t *)src_bits - disp->framebuffer_addr);
    tmp.src_rect.x              = src_x;
    tmp.src_rect.y              = src_y;
    tmp.src_rect.w              = w;
    tmp.src_rect.h              = h;
    tmp.src_image.h             = src_y + h;
    if (src_bpp == 32) {
        tmp.src_image.w         = src_stride;
        tmp.src_image.format    = G2D_FMT_ARGB_AYUV8888;
        tmp.src_image.pixel_seq = G2D_SEQ_NORMAL;
    }
    else if (src_bpp == 16) {
        tmp.src_image.w         = src_stride * 2;
        tmp.src_image.format    = G2D_FMT_RGB565;
        tmp.src_image.pixel_seq = G2D_SEQ_P10;
    }

    tmp.dst_image.addr[0]       = disp->framebuffer_paddr +
                                  ((uint8_t *)dst_bits - disp->framebuffer_addr);
    tmp.dst_x                   = dst_x;
    tmp.dst_y                   = dst_y;
    tmp.color                   = 0;
    tmp.alpha                   = 0;
    tmp.dst_image.h             = dst_y + h;
    if (dst_bpp == 32) {
        tmp.dst_image.w         = dst_stride;
        tmp.dst_image.format    = G2D_FMT_ARGB_AYUV8888;
        tmp.dst_image.pixel_seq = G2D_SEQ_NORMAL;
    }
    else if (dst_bpp == 16) {
        tmp.dst_image.w         = dst_stride * 2;
        tmp.dst_image.format    = G2D_FMT_RGB565;
        tmp.dst_image.pixel_seq = G2D_SEQ_P10;
    }

    return ioctl(disp->fd_g2d, G2D_CMD_BITBLT, &tmp) == 0;
}