hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
/*
 * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
 * Authors:
 *  PutinLee <putin.lee@rock-chips.com>
 *  Cerf Yu <cerf.yu@rock-chips.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#ifndef _im2d_h_
#define _im2d_h_
 
#ifdef __cplusplus
extern "C" {
#endif
 
#ifndef IM_API
#define IM_API /* define API export as needed */
#endif
 
typedef enum {
    /* Rotation */
    IM_HAL_TRANSFORM_ROT_90     = 1 << 0,
    IM_HAL_TRANSFORM_ROT_180    = 1 << 1,
    IM_HAL_TRANSFORM_ROT_270    = 1 << 2,
    IM_HAL_TRANSFORM_FLIP_H     = 1 << 3,
    IM_HAL_TRANSFORM_FLIP_V     = 1 << 4,
    IM_HAL_TRANSFORM_FLIP_H_V   = 1 << 5,
    IM_HAL_TRANSFORM_MASK       = 0x3f,
 
    /*
     * Blend
     * Additional blend usage, can be used with both source and target configs.
     * If none of the below is set, the default "SRC over DST" is applied.
     */
    IM_ALPHA_BLEND_SRC_OVER     = 1 << 6,     /* Default, Porter-Duff "SRC over DST" */
    IM_ALPHA_BLEND_SRC          = 1 << 7,     /* Porter-Duff "SRC" */
    IM_ALPHA_BLEND_DST          = 1 << 8,     /* Porter-Duff "DST" */
    IM_ALPHA_BLEND_SRC_IN       = 1 << 9,     /* Porter-Duff "SRC in DST" */
    IM_ALPHA_BLEND_DST_IN       = 1 << 10,    /* Porter-Duff "DST in SRC" */
    IM_ALPHA_BLEND_SRC_OUT      = 1 << 11,    /* Porter-Duff "SRC out DST" */
    IM_ALPHA_BLEND_DST_OUT      = 1 << 12,    /* Porter-Duff "DST out SRC" */
    IM_ALPHA_BLEND_DST_OVER     = 1 << 13,    /* Porter-Duff "DST over SRC" */
    IM_ALPHA_BLEND_SRC_ATOP     = 1 << 14,    /* Porter-Duff "SRC ATOP" */
    IM_ALPHA_BLEND_DST_ATOP     = 1 << 15,    /* Porter-Duff "DST ATOP" */
    IM_ALPHA_BLEND_XOR          = 1 << 16,    /* Xor */
    IM_ALPHA_BLEND_MASK         = 0x1ffc0,
 
    IM_ALPHA_COLORKEY_NORMAL    = 1 << 17,
    IM_ALPHA_COLORKEY_INVERTED  = 1 << 18,
    IM_ALPHA_COLORKEY_MASK      = 0x60000,
 
    IM_SYNC                     = 1 << 19,
    IM_ASYNC                    = 1 << 26,
    IM_CROP                     = 1 << 20,    /* Unused */
    IM_COLOR_FILL               = 1 << 21,
    IM_COLOR_PALETTE            = 1 << 22,
    IM_NN_QUANTIZE              = 1 << 23,
    IM_ROP                      = 1 << 24,
    IM_ALPHA_BLEND_PRE_MUL      = 1 << 25,
 
} IM_USAGE;
 
typedef enum {
    IM_ROP_AND                  = 0x88,
    IM_ROP_OR                   = 0xee,
    IM_ROP_NOT_DST              = 0x55,
    IM_ROP_NOT_SRC              = 0x33,
    IM_ROP_XOR                  = 0xf6,
    IM_ROP_NOT_XOR              = 0xf9,
} IM_ROP_CODE;
 
typedef enum {
    IM_RGA_SUPPORT_FORMAT_ERROR_INDEX = 0,
    IM_RGA_SUPPORT_FORMAT_RGB_INDEX,
    IM_RGA_SUPPORT_FORMAT_RGB_OTHER_INDEX,
    IM_RGA_SUPPORT_FORMAT_BPP_INDEX,
    IM_RGA_SUPPORT_FORMAT_YUV_8_INDEX,
    IM_RGA_SUPPORT_FORMAT_YUV_10_INDEX,
    IM_RGA_SUPPORT_FORMAT_YUYV_420_INDEX,
    IM_RGA_SUPPORT_FORMAT_YUYV_422_INDEX,
    IM_RGA_SUPPORT_FORMAT_YUV_400_INDEX,
    IM_RGA_SUPPORT_FORMAT_Y4_INDEX,
    IM_RGA_SUPPORT_FORMAT_MASK_INDEX,
} IM_RGA_SUPPORT_FORMAT_INDEX;
 
typedef enum {
    IM_RGA_SUPPORT_FORMAT_ERROR     = 1 << IM_RGA_SUPPORT_FORMAT_ERROR_INDEX,
    IM_RGA_SUPPORT_FORMAT_RGB       = 1 << IM_RGA_SUPPORT_FORMAT_RGB_INDEX,
    IM_RGA_SUPPORT_FORMAT_RGB_OTHER = 1 << IM_RGA_SUPPORT_FORMAT_RGB_OTHER_INDEX,
    IM_RGA_SUPPORT_FORMAT_BPP       = 1 << IM_RGA_SUPPORT_FORMAT_BPP_INDEX,
    IM_RGA_SUPPORT_FORMAT_YUV_8     = 1 << IM_RGA_SUPPORT_FORMAT_YUV_8_INDEX,
    IM_RGA_SUPPORT_FORMAT_YUV_10    = 1 << IM_RGA_SUPPORT_FORMAT_YUV_10_INDEX,
    IM_RGA_SUPPORT_FORMAT_YUYV_420  = 1 << IM_RGA_SUPPORT_FORMAT_YUYV_420_INDEX,
    IM_RGA_SUPPORT_FORMAT_YUYV_422  = 1 << IM_RGA_SUPPORT_FORMAT_YUYV_422_INDEX,
    IM_RGA_SUPPORT_FORMAT_YUV_400   = 1 << IM_RGA_SUPPORT_FORMAT_YUV_400_INDEX,
    IM_RGA_SUPPORT_FORMAT_Y4        = 1 << IM_RGA_SUPPORT_FORMAT_Y4_INDEX,
    IM_RGA_SUPPORT_FORMAT_MASK      = ~((~(unsigned int)0x0 << IM_RGA_SUPPORT_FORMAT_MASK_INDEX) | 1),
} IM_RGA_SUPPORT_FORMAT;
 
typedef enum {
    IM_RGA_SUPPORT_FEATURE_ERROR_INDEX = 0,
    IM_RGA_SUPPORT_FEATURE_COLOR_FILL_INDEX,
    IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE_INDEX,
    IM_RGA_SUPPORT_FEATURE_ROP_INDEX,
    IM_RGA_SUPPORT_FEATURE_QUANTIZE_INDEX,
    IM_RGA_SUPPORT_FEATURE_SRC1_R2Y_CSC_INDEX,
    IM_RGA_SUPPORT_FEATURE_DST_FULL_CSC_INDEX,
    IM_RGA_SUPPORT_FEATURE_MASK_INDEX,
} IM_RGA_SUPPORT_FEATURE_INDEX;
 
typedef enum {
    IM_RGA_SUPPORT_FEATURE_ERROR          = 1 << IM_RGA_SUPPORT_FEATURE_ERROR_INDEX,
    IM_RGA_SUPPORT_FEATURE_COLOR_FILL     = 1 << IM_RGA_SUPPORT_FEATURE_COLOR_FILL_INDEX,
    IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE  = 1 << IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE_INDEX,
    IM_RGA_SUPPORT_FEATURE_ROP            = 1 << IM_RGA_SUPPORT_FEATURE_ROP_INDEX,
    IM_RGA_SUPPORT_FEATURE_QUANTIZE       = 1 << IM_RGA_SUPPORT_FEATURE_QUANTIZE_INDEX,
    IM_RGA_SUPPORT_FEATURE_SRC1_R2Y_CSC   = 1 << IM_RGA_SUPPORT_FEATURE_SRC1_R2Y_CSC_INDEX,
    IM_RGA_SUPPORT_FEATURE_DST_FULL_CSC   = 1 << IM_RGA_SUPPORT_FEATURE_DST_FULL_CSC_INDEX,
    IM_RGA_SUPPORT_FEATURE_MASK           = ~((~(unsigned int)0x0 << IM_RGA_SUPPORT_FEATURE_MASK_INDEX) | 1),
} IM_RGA_SUPPORT_FEATURE;
 
/* Status codes, returned by any blit function */
typedef enum {
    IM_STATUS_NOERROR         =  2,
    IM_STATUS_SUCCESS         =  1,
    IM_STATUS_NOT_SUPPORTED   = -1,
    IM_STATUS_OUT_OF_MEMORY   = -2,
    IM_STATUS_INVALID_PARAM   = -3,
    IM_STATUS_ILLEGAL_PARAM   = -4,
    IM_STATUS_FAILED          =  0,
} IM_STATUS;
 
/* Status codes, returned by any blit function */
typedef enum {
    IM_YUV_TO_RGB_BT601_LIMIT     = 1 << 0,
    IM_YUV_TO_RGB_BT601_FULL      = 2 << 0,
    IM_YUV_TO_RGB_BT709_LIMIT     = 3 << 0,
    IM_YUV_TO_RGB_MASK            = 3 << 0,
    IM_RGB_TO_YUV_BT601_FULL      = 1 << 2,
    IM_RGB_TO_YUV_BT601_LIMIT     = 2 << 2,
    IM_RGB_TO_YUV_BT709_LIMIT     = 3 << 2,
    IM_RGB_TO_YUV_MASK            = 3 << 2,
    IM_RGB_TO_Y4                  = 1 << 4,
    IM_RGB_TO_Y4_DITHER           = 2 << 4,
    IM_RGB_TO_Y1_DITHER           = 3 << 4,
    IM_Y4_MASK                    = 3 << 4,
    IM_RGB_FULL                   = 1 << 8,
    IM_RGB_CLIP                   = 2 << 8,
    IM_YUV_BT601_LIMIT_RANGE      = 3 << 8,
    IM_YUV_BT601_FULL_RANGE       = 4 << 8,
    IM_YUV_BT709_LIMIT_RANGE      = 5 << 8,
    IM_YUV_BT709_FULL_RANGE       = 6 << 8,
    IM_FULL_CSC_MASK              = 0xf << 8,
    IM_COLOR_SPACE_DEFAULT        = 0,
} IM_COLOR_SPACE_MODE;
 
typedef enum {
    IM_UP_SCALE,
    IM_DOWN_SCALE,
} IM_SCALE;
 
typedef enum {
    INTER_NEAREST,
    INTER_LINEAR,
    INTER_CUBIC,
} IM_SCALE_MODE;
 
/* Get RGA basic information index */
typedef enum {
    RGA_VENDOR = 0,
    RGA_VERSION,
    RGA_MAX_INPUT,
    RGA_MAX_OUTPUT,
    RGA_SCALE_LIMIT,
    RGA_INPUT_FORMAT,
    RGA_OUTPUT_FORMAT,
    RGA_FEATURE,
    RGA_EXPECTED,
    RGA_ALL,
} IM_INFORMATION;
 
/*rga version index*/
typedef enum {
    RGA_V_ERR                  = 0x0,
    RGA_1                      = 0x1,
    RGA_1_PLUS                 = 0x2,
    RGA_2                      = 0x3,
    RGA_2_LITE0                = 0x4,
    RGA_2_LITE1                = 0x5,
    RGA_2_ENHANCE              = 0x6,
} RGA_VERSION_NUM;
 
//struct AHardwareBuffer AHardwareBuffer;
 
typedef struct {
    RGA_VERSION_NUM version;
    unsigned int input_resolution;
    unsigned int output_resolution;
    unsigned int scale_limit;
    unsigned int performance;
    unsigned int input_format;
    unsigned int output_format;
    unsigned int feature;
    char reserved[28];
} rga_info_table_entry;
 
/* Rectangle definition */
typedef struct {
    int x;        /* upper-left x */
    int y;        /* upper-left y */
    int width;    /* width */
    int height;   /* height */
} im_rect;
 
typedef struct {
    int max;                    /* The Maximum value of the color key */
    int min;                    /* The minimum value of the color key */
} im_colorkey_range;
 
 
typedef struct im_nn {
    int scale_r;                /* scaling factor on R channal */
    int scale_g;                /* scaling factor on G channal */
    int scale_b;                /* scaling factor on B channal */
    int offset_r;               /* offset on R channal */
    int offset_g;               /* offset on G channal */
    int offset_b;               /* offset on B channal */
} im_nn_t;
 
/* im_info definition */
typedef struct {
    void* vir_addr;                     /* virtual address */
    void* phy_addr;                     /* physical address */
    int fd;                             /* shared fd */
    int width;                          /* width */
    int height;                         /* height */
    int wstride;                        /* wstride */
    int hstride;                        /* hstride */
    int format;                         /* format */
    int color_space_mode;               /* color_space_mode */
    int color;                          /* color, used by color fill */
    int global_alpha;                   /* global_alpha */
    im_colorkey_range colorkey_range;   /* range value of color key */
    im_nn_t nn;
    int rop_code;
} rga_buffer_t;
 
/*
 * @return error message string
 */
#define imStrError(...) \
    ({ \
        const char* im2d_api_err; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_err = imStrError_t(IM_STATUS_INVALID_PARAM); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_err = imStrError_t((IM_STATUS)im2d_api_args[0]); \
        } else { \
            im2d_api_err = ("Fatal error, imStrError() too many parameters\n"); \
            printf("Fatal error, imStrError() too many parameters\n"); \
        } \
        im2d_api_err; \
    })
IM_API const char* imStrError_t(IM_STATUS status);
 
/*
 * @return rga_buffer_t
 */
#define wrapbuffer_virtualaddr(vir_addr, width, height, format, ...) \
    ({ \
        rga_buffer_t im2d_api_buffer; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_buffer = wrapbuffer_virtualaddr_t(vir_addr, width, height, width, height, format); \
        } else if (im2d_api_argc == 2){ \
            im2d_api_buffer = wrapbuffer_virtualaddr_t(vir_addr, width, height, im2d_api_args[0], im2d_api_args[1], format); \
        } else { \
            printf("invalid parameter\n"); \
        } \
        im2d_api_buffer; \
    })
 
#define wrapbuffer_physicaladdr(phy_addr, width, height, format, ...) \
    ({ \
        rga_buffer_t im2d_api_buffer; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_buffer = wrapbuffer_physicaladdr_t(phy_addr, width, height, width, height, format); \
        } else if (im2d_api_argc == 2){ \
            im2d_api_buffer = wrapbuffer_physicaladdr_t(phy_addr, width, height, im2d_api_args[0], im2d_api_args[1], format); \
        } else { \
            printf("invalid parameter\n"); \
        } \
        im2d_api_buffer; \
    })
 
#define wrapbuffer_fd(fd, width, height, format, ...) \
    ({ \
        rga_buffer_t im2d_api_buffer; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_buffer = wrapbuffer_fd_t(fd, width, height, width, height, format); \
        } else if (im2d_api_argc == 2){ \
            im2d_api_buffer = wrapbuffer_fd_t(fd, width, height, im2d_api_args[0], im2d_api_args[1], format); \
        } else { \
            printf("invalid parameter\n"); \
        } \
        im2d_api_buffer; \
    })
 
IM_API rga_buffer_t wrapbuffer_virtualaddr_t(void* vir_addr, int width, int height, int wstride, int hstride, int format);
IM_API rga_buffer_t wrapbuffer_physicaladdr_t(void* phy_addr, int width, int height, int wstride, int hstride, int format);
IM_API rga_buffer_t wrapbuffer_fd_t(int fd, int width, int height, int wstride, int hstride, int format);
 
/*
 * Get RGA basic information, supported resolution, supported format, etc.
 *
 * @param name
 *      RGA_VENDOR
 *      RGA_VERSION
 *      RGA_MAX_INPUT
 *      RGA_MAX_OUTPUT
 *      RGA_INPUT_FORMAT
 *      RGA_OUTPUT_FORMAT
 *      RGA_EXPECTED
 *      RGA_ALL
 *
 * @returns a usage describing properties of RGA.
 */
//IM_API int rga_get_info(rga_info_table_entry *);
IM_API IM_STATUS rga_get_info(rga_info_table_entry *return_table);
 
/*
 * Query RGA basic information, supported resolution, supported format, etc.
 *
 * @param name
 *      RGA_VENDOR
 *      RGA_VERSION
 *      RGA_MAX_INPUT
 *      RGA_MAX_OUTPUT
 *      RGA_INPUT_FORMAT
 *      RGA_OUTPUT_FORMAT
 *      RGA_EXPECTED
 *      RGA_ALL
 *
 * @returns a string describing properties of RGA.
 */
IM_API const char* querystring(int name);
 
/*
 * check RGA basic information, supported resolution, supported format, etc.
 *
 * @param src
 * @param dst
 * @param src_rect
 * @param dst_rect
 * @param mode_usage
 *
 * @returns no error or else negative error code.
 */
#define imcheck(src, dst, src_rect, dst_rect, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_NOERROR; \
        rga_buffer_t im2d_api_pat; \
        im_rect im2d_api_pat_rect; \
        memset(&im2d_api_pat, 0, sizeof(rga_buffer_t)); \
        memset(&im2d_api_pat_rect, 0, sizeof(im_rect)); \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            rga_check_perpare((rga_buffer_t *)(&src), (rga_buffer_t *)(&dst), (rga_buffer_t *)(&im2d_api_pat), \
                              (im_rect *)(&src_rect), (im_rect *)(&dst_rect), (im_rect *)(&im2d_api_pat_rect), 0); \
            im2d_api_ret = imcheck_t(src, dst, im2d_api_pat, src_rect, dst_rect, im2d_api_pat_rect, 0); \
        } else if (im2d_api_argc == 1){ \
            rga_check_perpare((rga_buffer_t *)(&src), (rga_buffer_t *)(&dst), (rga_buffer_t *)(&im2d_api_pat), \
                              (im_rect *)(&src_rect), (im_rect *)(&dst_rect), (im_rect *)(&im2d_api_pat_rect), im2d_api_args[0]); \
            im2d_api_ret = imcheck_t(src, dst, im2d_api_pat, src_rect, dst_rect, im2d_api_pat_rect, im2d_api_args[0]); \
        } else { \
            im2d_api_ret = IM_STATUS_FAILED; \
            printf("check failed\n"); \
        } \
        im2d_api_ret; \
    })
#define imcheck_composite(src, dst, pat, src_rect, dst_rect, pat_rect, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_NOERROR; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            rga_check_perpare((rga_buffer_t *)(&src), (rga_buffer_t *)(&dst), (rga_buffer_t *)(&pat), \
                              (im_rect *)(&src_rect), (im_rect *)(&dst_rect), (im_rect *)(&pat_rect), 0); \
            im2d_api_ret = imcheck_t(src, dst, pat, src_rect, dst_rect, pat_rect, 0); \
        } else if (im2d_api_argc == 1){ \
            rga_check_perpare((rga_buffer_t *)(&src), (rga_buffer_t *)(&dst), (rga_buffer_t *)(&pat), \
                              (im_rect *)(&src_rect), (im_rect *)(&dst_rect), (im_rect *)(&pat_rect), im2d_api_args[0]); \
            im2d_api_ret = imcheck_t(src, dst, pat, src_rect, dst_rect, pat_rect, im2d_api_args[0]); \
        } else { \
            im2d_api_ret = IM_STATUS_FAILED; \
            printf("check failed\n"); \
        } \
        im2d_api_ret; \
    })
IM_API void rga_check_perpare(rga_buffer_t *src, rga_buffer_t *dst, rga_buffer_t *pat,
                              im_rect *src_rect, im_rect *dst_rect, im_rect *pat_rect, int mode_usage);
IM_API IM_STATUS imcheck_t(const rga_buffer_t src, const rga_buffer_t dst, const rga_buffer_t pat,
                           const im_rect src_rect, const im_rect dst_rect, const im_rect pat_rect, const int mode_usage);
 
/*
 * Resize
 *
 * @param src
 * @param dst
 * @param fx
 * @param fy
 * @param interpolation
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imresize(src, dst, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        double im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(double); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imresize_t(src, dst, 0, 0, INTER_LINEAR, 1); \
        } else if (im2d_api_argc == 2){ \
            im2d_api_ret = imresize_t(src, dst, im2d_api_args[0], im2d_api_args[1], INTER_LINEAR, 1); \
        } else if (im2d_api_argc == 3){ \
            im2d_api_ret = imresize_t(src, dst, im2d_api_args[0], im2d_api_args[1], (int)im2d_api_args[2], 1); \
        } else if (im2d_api_argc == 4){ \
            im2d_api_ret = imresize_t(src, dst, im2d_api_args[0], im2d_api_args[1], (int)im2d_api_args[2], (int)im2d_api_args[3]); \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
 
#define impyramid(src, dst, direction) \
        imresize_t(src, \
                   dst, \
                   direction == IM_UP_SCALE ? 0.5 : 2, \
                   direction == IM_UP_SCALE ? 0.5 : 2, \
                   INTER_LINEAR, 1)
 
IM_API IM_STATUS imresize_t(const rga_buffer_t src, rga_buffer_t dst, double fx, double fy, int interpolation, int sync);
 
/*
 * Crop
 *
 * @param src
 * @param dst
 * @param rect
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imcrop(src, dst, rect, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imcrop_t(src, dst, rect, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imcrop_t(src, dst, rect, im2d_api_args[0]);; \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
 
IM_API IM_STATUS imcrop_t(const rga_buffer_t src, rga_buffer_t dst, im_rect rect, int sync);
 
/*
 * rotation
 *
 * @param src
 * @param dst
 * @param rotation
 *      IM_HAL_TRANSFORM_ROT_90
 *      IM_HAL_TRANSFORM_ROT_180
 *      IM_HAL_TRANSFORM_ROT_270
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imrotate(src, dst, rotation, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imrotate_t(src, dst, rotation, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imrotate_t(src, dst, rotation, im2d_api_args[0]);; \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
 
IM_API IM_STATUS imrotate_t(const rga_buffer_t src, rga_buffer_t dst, int rotation, int sync);
 
/*
 * flip
 *
 * @param src
 * @param dst
 * @param mode
 *      IM_HAL_TRANSFORM_FLIP_H
 *      IM_HAL_TRANSFORM_FLIP_V
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imflip(src, dst, mode, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imflip_t(src, dst, mode, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imflip_t(src, dst, mode, im2d_api_args[0]);; \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
 
IM_API IM_STATUS imflip_t (const rga_buffer_t src, rga_buffer_t dst, int mode, int sync);
 
/*
 * fill/reset/draw
 *
 * @param src
 * @param dst
 * @param rect
 * @param color
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imfill(buf, rect, color, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imfill_t(buf, rect, color, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imfill_t(buf, rect, color, im2d_api_args[0]);; \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
 
#define imreset(buf, rect, color, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imfill_t(buf, rect, color, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imfill_t(buf, rect, color, im2d_api_args[0]);; \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
 
#define imdraw(buf, rect, color, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imfill_t(buf, rect, color, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imfill_t(buf, rect, color, im2d_api_args[0]);; \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
IM_API IM_STATUS imfill_t(rga_buffer_t dst, im_rect rect, int color, int sync);
 
/*
 * palette
 *
 * @param src
 * @param dst
 * @param lut
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define impalette(src, dst, lut,  ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = impalette_t(src, dst, lut, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = impalette_t(src, dst, lut, im2d_api_args[0]);; \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
IM_API IM_STATUS impalette_t(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t lut, int sync);
 
/*
 * translate
 *
 * @param src
 * @param dst
 * @param x
 * @param y
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imtranslate(src, dst, x, y, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imtranslate_t(src, dst, x, y, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imtranslate_t(src, dst, x, y, im2d_api_args[0]);; \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
IM_API IM_STATUS imtranslate_t(const rga_buffer_t src, rga_buffer_t dst, int x, int y, int sync);
 
/*
 * copy
 *
 * @param src
 * @param dst
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imcopy(src, dst, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imcopy_t(src, dst, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imcopy_t(src, dst, im2d_api_args[0]);; \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
 
IM_API IM_STATUS imcopy_t(const rga_buffer_t src, rga_buffer_t dst, int sync);
 
/*
 * blend (SRC + DST -> DST or SRCA + SRCB -> DST)
 *
 * @param srcA
 * @param srcB can be NULL.
 * @param dst
 * @param mode
 *      IM_ALPHA_BLEND_MODE
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imblend(srcA, dst, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        rga_buffer_t srcB; \
        memset(&srcB, 0x00, sizeof(rga_buffer_t)); \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imblend_t(srcA, srcB, dst, IM_ALPHA_BLEND_SRC_OVER, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imblend_t(srcA, srcB, dst, im2d_api_args[0], 1); \
        } else if (im2d_api_argc == 2){ \
            im2d_api_ret = imblend_t(srcA, srcB, dst, im2d_api_args[0], im2d_api_args[1]); \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
#define imcomposite(srcA, srcB, dst, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imblend_t(srcA, srcB, dst, IM_ALPHA_BLEND_SRC_OVER, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imblend_t(srcA, srcB, dst, im2d_api_args[0], 1); \
        } else if (im2d_api_argc == 2){ \
            im2d_api_ret = imblend_t(srcA, srcB, dst, im2d_api_args[0], im2d_api_args[1]); \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
IM_API IM_STATUS imblend_t(const rga_buffer_t srcA, const rga_buffer_t srcB, rga_buffer_t dst, int mode, int sync);
 
/*
 * color key
 *
 * @param src
 * @param dst
 * @param colorkey_range
 *      max color
 *      min color
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imcolorkey(src, dst, range, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imcolorkey_t(src, dst, range, IM_ALPHA_COLORKEY_NORMAL, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imcolorkey_t(src, dst, range, im2d_api_args[0], 1); \
        } else if (im2d_api_argc == 2){ \
            im2d_api_ret = imcolorkey_t(src, dst, range, im2d_api_args[0], im2d_api_args[1]); \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
IM_API IM_STATUS imcolorkey_t(const rga_buffer_t src, rga_buffer_t dst, im_colorkey_range range, int mode, int sync);
 
/*
 * format convert
 *
 * @param src
 * @param dst
 * @param sfmt
 * @param dfmt
 * @param mode
 *      color space mode: IM_COLOR_SPACE_MODE
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imcvtcolor(src, dst, sfmt, dfmt, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imcvtcolor_t(src, dst, sfmt, dfmt, IM_COLOR_SPACE_DEFAULT, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imcvtcolor_t(src, dst, sfmt, dfmt, im2d_api_args[0], 1); \
        } else if (im2d_api_argc == 2){ \
            im2d_api_ret = imcvtcolor_t(src, dst, sfmt, dfmt, im2d_api_args[0], im2d_api_args[1]); \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
 
IM_API IM_STATUS imcvtcolor_t(rga_buffer_t src, rga_buffer_t dst, int sfmt, int dfmt, int mode, int sync);
 
/*
 * nn quantize
 *
 * @param src
 * @param dst
 * @param nninfo
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imquantize(src, dst, nn_info, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imquantize_t(src, dst, nn_info, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imquantize_t(src, dst, nn_info, im2d_api_args[0]);; \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
 
IM_API IM_STATUS imquantize_t(const rga_buffer_t src, rga_buffer_t dst, im_nn_t nn_info, int sync);
 
/*
 * ROP
 *
 * @param src
 * @param dst
 * @param rop_code
 * @param sync
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
#define imrop(src, dst, rop_code, ...) \
    ({ \
        IM_STATUS im2d_api_ret = IM_STATUS_SUCCESS; \
        int im2d_api_args[] = {__VA_ARGS__}; \
        int im2d_api_argc = sizeof(im2d_api_args)/sizeof(int); \
        if (im2d_api_argc == 0) { \
            im2d_api_ret = imrop_t(src, dst, rop_code, 1); \
        } else if (im2d_api_argc == 1){ \
            im2d_api_ret = imrop_t(src, dst, rop_code, im2d_api_args[0]);; \
        } else { \
            im2d_api_ret = IM_STATUS_INVALID_PARAM; \
            printf("invalid parameter\n"); \
        } \
        im2d_api_ret; \
    })
IM_API IM_STATUS imrop_t(const rga_buffer_t src, rga_buffer_t dst, int rop_code, int sync);
 
/*
 * process
 *
 * @param src
 * @param dst
 * @param usage
 * @param ...
 *      wait until operation complete
 *
 * @returns success or else negative error code.
 */
IM_API IM_STATUS improcess(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat, im_rect srect, im_rect drect, im_rect prect, int usage);
 
/*
 * block until all execution is complete
 *
 * @returns success or else negative error code.
 */
IM_API IM_STATUS imsync(void);
 
#ifdef __cplusplus
}
#endif
#endif /* _im2d_h_ */