/
exynos_gfx.c
1516 lines (1210 loc) · 37.3 KB
/
exynos_gfx.c
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
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
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
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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* RetroArch - A frontend for libretro.
* Copyright (C) 2013-2015 - Tobias Jakobi
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <drm_fourcc.h>
#include <libdrm/exynos_drmif.h>
#include <exynos/exynos_fimg2d.h>
#include <retro_inline.h>
#include <string/stdstring.h>
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#ifdef HAVE_MENU
#include "../../menu/menu_driver.h"
#endif
#include "../common/drm_common.h"
#include "../font_driver.h"
#include "../../configuration.h"
#include "../../retroarch.h"
/* TODO: Honor these properties: vsync, menu rotation, menu alpha, aspect ratio change */
/* Set to '1' to enable debug logging code. */
#define EXYNOS_GFX_DEBUG_LOG 0
/* Set to '1' to enable debug perf code. */
#define EXYNOS_GFX_DEBUG_PERF 0
extern void *memcpy_neon(void *dst, const void *src, size_t n);
/* We use two GEM buffers (main and aux) to handle 'data' from the frontend. */
enum exynos_buffer_type
{
EXYNOS_BUFFER_MAIN = 0,
EXYNOS_BUFFER_AUX,
EXYNOS_BUFFER_COUNT
};
/* We have to handle three types of 'data' from the frontend, each abstracted by a *
* G2D image object. The image objects are then backed by some storage buffer. *
* (1) the core framebuffer (backed by main buffer) *
* (2) the menu buffer (backed by aux buffer) *
* (3) the font rendering buffer (backed by aux buffer) */
enum exynos_image_type
{
EXYNOS_IMAGE_FRAME = 0,
EXYNOS_IMAGE_FRONT,
EXYNOS_IMAGE_MENU,
EXYNOS_IMAGE_COUNT
};
static const struct exynos_config_default
{
unsigned width, height;
enum exynos_buffer_type buf_type;
unsigned g2d_color_mode;
unsigned bpp; /* bytes per pixel */
} defaults[EXYNOS_IMAGE_COUNT] = {
{1024, 640, EXYNOS_BUFFER_MAIN, G2D_COLOR_FMT_RGB565 | G2D_ORDER_AXRGB, 2}, /* frame */
{720, 368, EXYNOS_BUFFER_AUX, G2D_COLOR_FMT_ARGB4444 | G2D_ORDER_AXRGB, 2}, /* font */
{400, 240, EXYNOS_BUFFER_AUX, G2D_COLOR_FMT_ARGB4444 | G2D_ORDER_RGBAX, 2} /* menu */
};
struct exynos_data;
#if (EXYNOS_GFX_DEBUG_PERF == 1)
struct exynos_perf
{
unsigned memcpy_calls;
unsigned g2d_calls;
unsigned long long memcpy_time;
unsigned long long g2d_time;
struct timespec tspec;
};
#endif
struct exynos_page
{
struct exynos_bo *bo;
uint32_t buf_id;
struct exynos_data *base;
bool used; /* Set if page is currently used. */
bool clear; /* Set if page has to be cleared. */
};
struct exynos_data
{
char drmname[32];
struct exynos_device *device;
/* G2D is used for scaling to framebuffer dimensions. */
struct g2d_context *g2d;
struct g2d_image *dst;
struct g2d_image *src[EXYNOS_IMAGE_COUNT];
struct exynos_bo *buf[EXYNOS_BUFFER_COUNT];
struct exynos_page *pages;
unsigned num_pages;
/* currently displayed page */
struct exynos_page *cur_page;
unsigned pageflip_pending;
/* framebuffer dimensions */
unsigned width, height;
/* framebuffer aspect ratio */
float aspect;
/* parameters for blitting core fb to screen */
unsigned blit_params[6];
/* bytes per pixel */
unsigned bpp;
/* framebuffer parameters */
unsigned pitch, size;
bool sync;
#if (EXYNOS_GFX_DEBUG_PERF == 1)
struct exynos_perf perf;
#endif
};
static INLINE unsigned align_common(unsigned i, unsigned j)
{
return (i + j - 1) & ~(j - 1);
}
/* Find the index of a compatible DRM device. */
static int exynos_get_device_index(void)
{
drmVersionPtr ver;
char buf[32] = {0};
int index = 0;
bool found = false;
while (!found)
{
int fd;
snprintf(buf, sizeof(buf), "/dev/dri/card%d", index);
fd = open(buf, O_RDWR);
if (fd < 0) break;
ver = drmGetVersion(fd);
if (string_is_equal(ver->name, "exynos"))
found = true;
else
++index;
drmFreeVersion(ver);
close(fd);
}
if (!found)
return -1;
return index;
}
/* The main pageflip handler, which the DRM executes
* when it flips to the page.
*
* Decreases the pending pageflip count and
* updates the current page.
*/
static void exynos_page_flip_handler(int fd, unsigned frame, unsigned sec,
unsigned usec, void *data)
{
struct exynos_page *page = data;
if (page->base->cur_page)
page->base->cur_page->used = false;
page->base->pageflip_pending--;
page->base->cur_page = page;
}
static struct exynos_page *exynos_get_free_page(
struct exynos_page *p, unsigned cnt)
{
unsigned i;
for (i = 0; i < cnt; ++i)
{
if (!p[i].used)
return &p[i];
}
return NULL;
}
/* Count the number of used pages. */
static unsigned exynos_pages_used(struct exynos_page *p, unsigned cnt)
{
unsigned i;
unsigned count = 0;
for (i = 0; i < cnt; ++i)
{
if (p[i].used)
++count;
}
return count;
}
static void exynos_clean_up_pages(struct exynos_page *p, unsigned cnt)
{
unsigned i;
for (i = 0; i < cnt; ++i)
{
if (p[i].bo)
{
if (p[i].buf_id != 0)
drmModeRmFB(p[i].buf_id, p[i].bo->handle);
exynos_bo_destroy(p[i].bo);
}
}
}
#if (EXYNOS_GFX_DEBUG_LOG == 1)
static const char *exynos_buffer_name(enum exynos_buffer_type type)
{
switch (type)
{
case EXYNOS_BUFFER_MAIN:
return "main";
case EXYNOS_BUFFER_AUX:
return "aux";
default:
break;
}
return NULL;
}
#endif
/* Create a GEM buffer with userspace mapping.
* Buffer is cleared after creation. */
static struct exynos_bo *exynos_create_mapped_buffer(
struct exynos_device *dev, unsigned size)
{
const unsigned flags = 0;
struct exynos_bo *buf = exynos_bo_create(dev, size, flags);
if (!buf)
{
RARCH_ERR("[video_exynos]: failed to create temp buffer object\n");
return NULL;
}
if (!exynos_bo_map(buf))
{
RARCH_ERR("[video_exynos]: failed to map temp buffer object\n");
exynos_bo_destroy(buf);
return NULL;
}
memset(buf->vaddr, 0, size);
return buf;
}
static int exynos_realloc_buffer(struct exynos_data *pdata,
enum exynos_buffer_type type, unsigned size)
{
struct exynos_bo *buf = pdata->buf[type];
if (!buf)
return -1;
if (size > buf->size)
{
unsigned i;
#if (EXYNOS_GFX_DEBUG_LOG == 1)
RARCH_LOG("[video_exynos]: reallocating %s buffer (%u -> %u bytes)\n",
exynos_buffer_name(type), buf->size, size);
#endif
exynos_bo_destroy(buf);
buf = exynos_create_mapped_buffer(pdata->device, size);
if (!buf)
{
RARCH_ERR("[video_exynos]: reallocation failed\n");
return -1;
}
pdata->buf[type] = buf;
/* Map new GEM buffer to the G2D images backed by it. */
for (i = 0; i < EXYNOS_IMAGE_COUNT; ++i)
{
if (defaults[i].buf_type == type)
pdata->src[i]->bo[0] = buf->handle;
}
}
return 0;
}
/* Clear a buffer associated to a G2D image by doing a (fast) solid fill. */
static int exynos_clear_buffer(struct g2d_context *g2d, struct g2d_image *img)
{
int ret = g2d_solid_fill(g2d, img, 0, 0, img->width, img->height);
if (ret == 0)
ret = g2d_exec(g2d);
if (ret != 0)
RARCH_ERR("[video_exynos]: failed to clear buffer using G2D\n");
return ret;
}
/* Put a font glyph at a position in the buffer that is backing the G2D font image object. */
static void exynos_put_glyph_rgba4444(struct exynos_data *pdata,
const uint8_t *__restrict__ src,
uint16_t color, unsigned g_width, unsigned g_height,
unsigned g_pitch, unsigned dst_x, unsigned dst_y)
{
unsigned x, y;
const enum exynos_image_type buf_type = defaults[EXYNOS_IMAGE_FONT].buf_type;
const unsigned buf_width = pdata->src[EXYNOS_IMAGE_FONT]->width;
uint16_t *__restrict__ dst = (uint16_t*)pdata->buf[buf_type]->vaddr +
dst_y * buf_width + dst_x;
for (y = 0; y < g_height; ++y, src += g_pitch, dst += buf_width)
{
for (x = 0; x < g_width; ++x)
{
const uint16_t blend = src[x];
dst[x] = color | ((blend << 8) & 0xf000);
}
}
}
#if (EXYNOS_GFX_DEBUG_PERF == 1)
static void exynos_perf_init(struct exynos_perf *p)
{
p->memcpy_calls = 0;
p->g2d_calls = 0;
p->memcpy_time = 0;
p->g2d_time = 0;
memset(&p->tspec, 0, sizeof(struct timespec));
}
static void exynos_perf_finish(struct exynos_perf *p)
{
RARCH_LOG("[video_exynos]: debug: total memcpy calls: %u\n", p->memcpy_calls);
RARCH_LOG("[video_exynos]: debug: total g2d calls: %u\n", p->g2d_calls);
RARCH_LOG("[video_exynos]: debug: total memcpy time: %f seconds\n",
(double)p->memcpy_time / 1000000.0);
RARCH_LOG("[video_exynos]: debug: total g2d time: %f seconds\n",
(double)p->g2d_time / 1000000.0);
RARCH_LOG("[video_exynos]: debug: average time per memcpy call: %f microseconds\n",
(double)p->memcpy_time / (double)p->memcpy_calls);
RARCH_LOG("[video_exynos]: debug: average time per g2d call: %f microseconds\n",
(double)p->g2d_time / (double)p->g2d_calls);
}
static void exynos_perf_memcpy(struct exynos_perf *p, bool start)
{
if (start)
clock_gettime(CLOCK_MONOTONIC, &p->tspec);
else
{
struct timespec new;
clock_gettime(CLOCK_MONOTONIC, &new);
p->memcpy_time += (new.tv_sec - p->tspec.tv_sec) * 1000000;
p->memcpy_time += (new.tv_nsec - p->tspec.tv_nsec) / 1000;
++p->memcpy_calls;
}
}
static void exynos_perf_g2d(struct exynos_perf *p, bool start)
{
if (start)
clock_gettime(CLOCK_MONOTONIC, &p->tspec);
else
{
struct timespec new;
clock_gettime(CLOCK_MONOTONIC, &new);
p->g2d_time += (new.tv_sec - p->tspec.tv_sec) * 1000000;
p->g2d_time += (new.tv_nsec - p->tspec.tv_nsec) / 1000;
++p->g2d_calls;
}
}
#endif
static int exynos_g2d_init(struct exynos_data *pdata)
{
unsigned i;
struct g2d_image *dst = NULL;
struct g2d_context *g2d = g2d_init(g_drm_fd);
if (!g2d)
return -1;
dst = calloc(1, sizeof(struct g2d_image));
if (!dst)
goto fail;
dst->buf_type = G2D_IMGBUF_GEM;
dst->color_mode = (pdata->bpp == 2) ? G2D_COLOR_FMT_RGB565 | G2D_ORDER_AXRGB :
G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
dst->width = pdata->width;
dst->height = pdata->height;
dst->stride = pdata->pitch;
dst->color = 0xff000000; /* Clear color for solid fill operation. */
for (i = 0; i < EXYNOS_IMAGE_COUNT; ++i)
{
const enum exynos_buffer_type buf_type = defaults[i].buf_type;
const unsigned buf_size = defaults[i].width * defaults[i].height * defaults[i].bpp;
struct g2d_image *src = (struct g2d_image*)calloc(1, sizeof(struct g2d_image));
if (!src)
break;
src->width = defaults[i].width;
src->height = defaults[i].height;
src->stride = defaults[i].width * defaults[i].bpp;
src->color_mode = defaults[i].g2d_color_mode;
/* Associate GEM buffer storage with G2D image. */
src->buf_type = G2D_IMGBUF_GEM;
src->bo[0] = pdata->buf[buf_type]->handle;
src->repeat_mode = G2D_REPEAT_MODE_PAD; /* Pad creates no border artifacts. */
/* Make sure that the storage buffer is large enough. If the code is working *
* properly, then this is just a NOP. Still put it here as an insurance. */
exynos_realloc_buffer(pdata, buf_type, buf_size);
pdata->src[i] = src;
}
if (i != EXYNOS_IMAGE_COUNT)
{
while (i-- > 0)
{
free(pdata->src[i]);
pdata->src[i] = NULL;
}
goto fail_src;
}
pdata->dst = dst;
pdata->g2d = g2d;
return 0;
fail_src:
free(dst);
fail:
g2d_fini(g2d);
return -1;
}
static void exynos_g2d_free(struct exynos_data *pdata)
{
unsigned i;
free(pdata->dst);
for (i = 0; i < EXYNOS_IMAGE_COUNT; ++i)
{
free(pdata->src[i]);
pdata->src[i] = NULL;
}
g2d_fini(pdata->g2d);
}
static int exynos_open(struct exynos_data *pdata)
{
unsigned i;
int fd = -1;
char buf[32] = {0};
int devidx = exynos_get_device_index();
if (pdata)
g_drm_fd = -1;
if (devidx != -1)
snprintf(buf, sizeof(buf), "/dev/dri/card%d", devidx);
else
{
RARCH_ERR("[video_exynos]: no compatible DRM device found\n");
return -1;
}
fd = open(buf, O_RDWR);
if (fd < 0)
{
RARCH_ERR("[video_exynos]: can't open DRM device\n");
return -1;
}
if (!drm_get_resources(fd))
goto fail;
if (!drm_get_decoder(fd))
goto fail;
if (!drm_get_encoder(fd))
goto fail;
/* Setup the flip handler. */
g_drm_fds.fd = fd;
g_drm_fds.events = POLLIN;
g_drm_evctx.version = DRM_EVENT_CONTEXT_VERSION;
g_drm_evctx.page_flip_handler = exynos_page_flip_handler;
strncpy(pdata->drmname, buf, sizeof(buf));
g_drm_fd = fd;
RARCH_LOG("[video_exynos]: using DRM device \"%s\" with connector id %u.\n",
pdata->drmname, g_drm_connector->connector_id);
return 0;
fail:
drm_free();
close(g_drm_fd);
return -1;
}
/* Counterpart to exynos_open. */
static void exynos_close(struct exynos_data *pdata)
{
memset(pdata->drmname, 0, sizeof(char) * 32);
drm_free();
close(g_drm_fd);
g_drm_fd = -1;
}
static int exynos_init(struct exynos_data *pdata, unsigned bpp)
{
unsigned i;
settings_t *settings = config_get_ptr();
unsigned video_fullscreen_x = settings->uints.video_fullscreen_x;
unsigned video_fullscreen_y = settings->uints.video_fullscreen_y;
if ( video_fullscreen_x != 0 &&
video_fullscreen_y != 0)
{
for (i = 0; i < g_drm_connector->count_modes; i++)
{
if (g_drm_connector->modes[i].hdisplay == video_fullscreen_x &&
g_drm_connector->modes[i].vdisplay == video_fullscreen_y)
{
g_drm_mode = &g_drm_connector->modes[i];
break;
}
}
if (!g_drm_mode)
{
RARCH_ERR(
"[video_exynos]: requested resolution (%ux%u) not available\n",
video_fullscreen_x,
video_fullscreen_y);
goto fail;
}
}
else
{
/* Select first mode, which is the native one. */
g_drm_mode = &g_drm_connector->modes[0];
}
if (g_drm_mode->hdisplay == 0 || g_drm_mode->vdisplay == 0)
{
RARCH_ERR("[video_exynos]: failed to select sane resolution\n");
goto fail;
}
drm_setup(g_drm_fd);
pdata->width = g_drm_mode->hdisplay;
pdata->height = g_drm_mode->vdisplay;
pdata->aspect = (float)g_drm_mode->hdisplay / (float)g_drm_mode->vdisplay;
/* Always use triple buffering to reduce chance of tearing. */
pdata->num_pages = 3;
pdata->bpp = bpp;
pdata->pitch = bpp * pdata->width;
pdata->size = pdata->pitch * pdata->height;
RARCH_LOG("[video_exynos]: selected %ux%u resolution with %u bpp\n",
pdata->width, pdata->height, pdata->bpp);
return 0;
fail:
drm_restore_crtc();
g_drm_mode = NULL;
return -1;
}
/* Counterpart to exynos_init. */
static void exynos_deinit(struct exynos_data *pdata)
{
drm_restore_crtc();
g_drm_mode = NULL;
pdata->width = 0;
pdata->height = 0;
pdata->num_pages = 0;
pdata->bpp = 0;
pdata->pitch = 0;
pdata->size = 0;
}
static int exynos_alloc(struct exynos_data *pdata)
{
struct exynos_bo *bo;
struct exynos_page *pages;
unsigned i;
uint32_t pixel_format;
const unsigned flags = 0;
uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
struct exynos_device *device = exynos_device_create(g_drm_fd);
if (!device)
{
RARCH_ERR("[video_exynos]: failed to create device from fd\n");
return -1;
}
pages = (struct exynos_page*)calloc(pdata->num_pages,
sizeof(struct exynos_page));
if (!pages)
{
RARCH_ERR("[video_exynos]: failed to allocate pages\n");
goto fail_alloc;
}
for (i = 0; i < EXYNOS_BUFFER_COUNT; ++i)
{
const unsigned buffer_size = defaults[i].width * defaults[i].height * defaults[i].bpp;
bo = exynos_create_mapped_buffer(device, buffer_size);
if (!bo)
break;
pdata->buf[i] = bo;
}
if (i != EXYNOS_BUFFER_COUNT)
{
while (i-- > 0)
{
exynos_bo_destroy(pdata->buf[i]);
pdata->buf[i] = NULL;
}
goto fail;
}
for (i = 0; i < pdata->num_pages; ++i)
{
bo = exynos_bo_create(device, pdata->size, flags);
if (!bo)
{
RARCH_ERR("[video_exynos]: failed to create buffer object\n");
goto fail;
}
/* Don't map the BO, since we don't access it through userspace. */
pages[i].bo = bo;
pages[i].base = pdata;
pages[i].used = false;
pages[i].clear = true;
}
pixel_format = (pdata->bpp == 2) ? DRM_FORMAT_RGB565 : DRM_FORMAT_XRGB8888;
pitches[0] = pdata->pitch;
offsets[0] = 0;
for (i = 0; i < pdata->num_pages; ++i)
{
handles[0] = pages[i].bo->handle;
if (drmModeAddFB2(g_drm_fd, pdata->width, pdata->height,
pixel_format, handles, pitches, offsets,
&pages[i].buf_id, flags))
{
RARCH_ERR("[video_exynos]: failed to add bo %u to fb\n", i);
goto fail;
}
}
/* Setup CRTC: display the last allocated page. */
if (drmModeSetCrtc(g_drm_fd, g_crtc_id,
pages[pdata->num_pages - 1].buf_id,
0, 0, &g_drm_connector_id, 1, g_drm_mode))
{
RARCH_ERR("[video_exynos]: initial CRTC setup failed.\n");
goto fail;
}
pdata->pages = pages;
pdata->device = device;
return 0;
fail:
exynos_clean_up_pages(pages, pdata->num_pages);
fail_alloc:
exynos_device_destroy(device);
return -1;
}
/* Counterpart to exynos_alloc. */
static void exynos_free(struct exynos_data *pdata)
{
unsigned i;
/* Disable the CRTC. */
if (drmModeSetCrtc(g_drm_fd, g_crtc_id, 0,
0, 0, NULL, 0, NULL))
RARCH_WARN("[video_exynos]: failed to disable the CRTC.\n");
exynos_clean_up_pages(pdata->pages, pdata->num_pages);
free(pdata->pages);
pdata->pages = NULL;
for (i = 0; i < EXYNOS_BUFFER_COUNT; ++i)
{
exynos_bo_destroy(pdata->buf[i]);
pdata->buf[i] = NULL;
}
exynos_device_destroy(pdata->device);
pdata->device = NULL;
}
#if (EXYNOS_GFX_DEBUG_LOG == 1)
static void exynos_alloc_status(struct exynos_data *pdata)
{
unsigned i;
struct exynos_page *pages = pdata->pages;
RARCH_LOG("[video_exynos]: Allocated %u pages with %u bytes each (pitch = %u bytes)\n",
pdata->num_pages, pdata->size, pdata->pitch);
for (i = 0; i < pdata->num_pages; ++i)
{
RARCH_LOG("[video_exynos]: page %u: BO at %p, buffer id = %u\n",
i, pages[i].bo, pages[i].buf_id);
}
}
#endif
/* Find a free page, clear it if necessary, and return the page. If *
* no free page is available when called, wait for a page flip. */
static struct exynos_page *exynos_free_page(struct exynos_data *pdata)
{
struct exynos_page *page = NULL;
struct g2d_image *dst = pdata->dst;
/* Wait until a free page is available. */
while (!page)
{
page = exynos_get_free_page(pdata->pages, pdata->num_pages);
if (!page)
drm_wait_flip(-1);
}
dst->bo[0] = page->bo->handle;
if (page->clear)
{
if (exynos_clear_buffer(pdata->g2d, dst) == 0)
page->clear = false;
}
page->used = true;
return page;
}
static void exynos_setup_scale(struct exynos_data *pdata,
unsigned width, unsigned height, unsigned src_bpp)
{
unsigned i;
unsigned w, h;
struct g2d_image *src = pdata->src[EXYNOS_IMAGE_FRAME];
const float aspect = (float)width / (float)height;
src->width = width;
src->height = height;
src->color_mode = (src_bpp == 2) ?
G2D_COLOR_FMT_RGB565 | G2D_ORDER_AXRGB:
G2D_COLOR_FMT_XRGB8888 | G2D_ORDER_AXRGB;
if (fabsf(pdata->aspect - aspect) < 0.0001f)
{
w = pdata->width;
h = pdata->height;
}
else
{
if (pdata->aspect > aspect)
{
w = (float)pdata->width * aspect / pdata->aspect;
h = pdata->height;
}
else
{
w = pdata->width;
h = (float)pdata->height * pdata->aspect / aspect;
}
}
pdata->blit_params[0] = (pdata->width - w) / 2;
pdata->blit_params[1] = (pdata->height - h) / 2;
pdata->blit_params[2] = w;
pdata->blit_params[3] = h;
pdata->blit_params[4] = width;
pdata->blit_params[5] = height;
for (i = 0; i < pdata->num_pages; ++i)
pdata->pages[i].clear = true;
}
static void exynos_set_fake_blit(struct exynos_data *pdata)
{
unsigned i;
pdata->blit_params[0] = 0;
pdata->blit_params[1] = 0;
pdata->blit_params[2] = pdata->width;
pdata->blit_params[3] = pdata->height;
for (i = 0; i < pdata->num_pages; ++i)
pdata->pages[i].clear = true;
}
static int exynos_blit_frame(
struct exynos_data *pdata,
const void *frame,
unsigned src_pitch)
{
const enum exynos_buffer_type
buf_type = defaults[EXYNOS_IMAGE_FRAME].buf_type;
const unsigned size = src_pitch * pdata->blit_params[5];
struct g2d_image *src = pdata->src[EXYNOS_IMAGE_FRAME];
if (exynos_realloc_buffer(pdata, buf_type, size) != 0)
return -1;
#if (EXYNOS_GFX_DEBUG_PERF == 1)
exynos_perf_memcpy(&pdata->perf, true);
#endif
/* HACK: Without IOMMU the G2D only works properly between GEM buffers. */
memcpy_neon(pdata->buf[buf_type]->vaddr, frame, size);
src->stride = src_pitch;
#if (EXYNOS_GFX_DEBUG_PERF == 1)
exynos_perf_memcpy(&pdata->perf, false);
#endif
#if (EXYNOS_GFX_DEBUG_PERF == 1)
exynos_perf_g2d(&pdata->perf, true);
#endif
if (g2d_copy_with_scale(pdata->g2d, src, pdata->dst, 0, 0,
pdata->blit_params[4], pdata->blit_params[5],
pdata->blit_params[0], pdata->blit_params[1],
pdata->blit_params[2], pdata->blit_params[3], 0) ||
g2d_exec(pdata->g2d))
{
RARCH_ERR("[video_exynos]: failed to blit frame.\n");
return -1;
}
#if (EXYNOS_GFX_DEBUG_PERF == 1)
exynos_perf_g2d(&pdata->perf, false);
#endif
return 0;
}
static int exynos_blend_menu(struct exynos_data *pdata,
unsigned rotation)
{
struct g2d_image *src = pdata->src[EXYNOS_IMAGE_MENU];
#if (EXYNOS_GFX_DEBUG_PERF == 1)
exynos_perf_g2d(&pdata->perf, true);
#endif
if (g2d_scale_and_blend(pdata->g2d, src, pdata->dst, 0, 0,
src->width, src->height, pdata->blit_params[0],
pdata->blit_params[1], pdata->blit_params[2],
pdata->blit_params[3], G2D_OP_INTERPOLATE) ||
g2d_exec(pdata->g2d))
{
RARCH_ERR("[video_exynos]: failed to blend menu.\n");
return -1;
}
#if (EXYNOS_GFX_DEBUG_PERF == 1)
exynos_perf_g2d(&pdata->perf, false);
#endif
return 0;
}
static int exynos_blend_font(struct exynos_data *pdata)
{
struct g2d_image *src = pdata->src[EXYNOS_IMAGE_FONT];
#if (EXYNOS_GFX_DEBUG_PERF == 1)
exynos_perf_g2d(&pdata->perf, true);
#endif
if (g2d_scale_and_blend(pdata->g2d, src, pdata->dst, 0, 0, src->width,
src->height, 0, 0, pdata->width, pdata->height,
G2D_OP_INTERPOLATE) ||
g2d_exec(pdata->g2d))
{
RARCH_ERR("[video_exynos]: failed to blend font\n");
return -1;
}
#if (EXYNOS_GFX_DEBUG_PERF == 1)
exynos_perf_g2d(&pdata->perf, false);
#endif
return 0;
}
static int exynos_flip(struct exynos_data *pdata, struct exynos_page *page)
{