xref: /OK3568_Linux_fs/external/linux-rga/samples/copy_demo/src/rga_copy_splice_demo.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2022  Rockchip Electronics Co., Ltd.
3  * Authors:
4  *     YuQiaowei <cerf.yu@rock-chips.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #define LOG_NDEBUG 0
20 #undef LOG_TAG
21 #define LOG_TAG "rga_copy_splice_demo"
22 
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/mman.h>
32 #include <math.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <unistd.h>
36 #include <linux/stddef.h>
37 
38 #include "RgaUtils.h"
39 #include "im2d.hpp"
40 #include "utils.h"
41 
42 #define LOCAL_FILE_PATH "/data"
43 
main()44 int main() {
45     int ret = 0;
46     int left_width, left_height, left_format;
47     int right_width, right_height, right_format;
48     int dst_width, dst_height, dst_format;
49     char *left_buf, *right_buf, *dst_buf;
50     int left_buf_size, right_buf_size, dst_buf_size;
51 
52     rga_buffer_t left_img, right_img, dst_img;
53     im_rect left_rect, right_rect;
54     rga_buffer_handle_t left_handle, right_handle, dst_handle;
55 
56     memset(&left_img, 0, sizeof(left_img));
57     memset(&right_img, 0, sizeof(right_img));
58     memset(&dst_img, 0, sizeof(dst_img));
59     memset(&left_rect, 0, sizeof(left_rect));
60     memset(&right_rect, 0, sizeof(right_rect));
61 
62     left_width = 1280;
63     left_height = 720;
64     left_format = RK_FORMAT_RGBA_8888;
65 
66     right_width = 1280;
67     right_height = 720;
68     right_format = RK_FORMAT_RGBA_8888;
69 
70     dst_width = 2560;
71     dst_height = 720;
72     dst_format = RK_FORMAT_RGBA_8888;
73 
74     left_buf_size = left_width * left_height * get_bpp_from_format(left_format);
75     right_buf_size = right_width * right_height * get_bpp_from_format(right_format);
76     dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
77 
78     left_buf = (char *)malloc(left_buf_size);
79     right_buf = (char *)malloc(right_buf_size);
80     dst_buf = (char *)malloc(dst_buf_size);
81 
82     /* fill image data */
83     if (0 != read_image_from_file(left_buf, LOCAL_FILE_PATH, left_width, left_height, left_format, 0)) {
84         printf("left image read err\n");
85         memset(left_buf, 0xaa, left_buf_size);
86     }
87     if (0 != read_image_from_file(right_buf, LOCAL_FILE_PATH, right_width, right_height, right_format, 0)) {
88         printf("right image read err\n");
89         memset(left_buf, 0xbb, left_buf_size);
90     }
91     memset(dst_buf, 0x80, dst_buf_size);
92 
93     left_handle = importbuffer_virtualaddr(left_buf, left_buf_size);
94     right_handle = importbuffer_virtualaddr(right_buf, left_buf_size);
95     dst_handle = importbuffer_virtualaddr(dst_buf, dst_buf_size);
96     if (left_handle == 0 || dst_handle == 0) {
97         printf("importbuffer failed!\n");
98         goto release_buffer;
99     }
100 
101     left_img = wrapbuffer_handle(left_handle, left_width, left_height, left_format);
102     right_img = wrapbuffer_handle(right_handle, right_width, right_height, right_format);
103     dst_img = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
104 
105     /*
106      * Splicing the left and right images to output.
107         ---------------------------
108         |            |            |
109         | left_rect  | right_rect |
110         |            |            |
111         ---------------------------
112      */
113 
114     /*
115      * 1). copy left image.
116                                     dst_img
117         --------------    ---------------------------
118         |            |    |            |            |
119         |  left_img  | => |  left_rect |            |
120         |            |    |            |            |
121         --------------    ---------------------------
122      */
123     left_rect.x = 0;
124     left_rect.y = 0;
125     left_rect.width = left_width;
126     left_rect.height = left_height;
127 
128     ret = imcheck(left_img, dst_img, {}, left_rect);
129     if (IM_STATUS_NOERROR != ret) {
130         printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
131         goto release_buffer;
132     }
133 
134     ret = improcess(left_img, dst_img, {}, {}, left_rect, {}, IM_SYNC);
135     if (ret == IM_STATUS_SUCCESS) {
136         printf("%s left running success!\n", LOG_TAG);
137     } else {
138         printf("%s left running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
139         goto release_buffer;
140     }
141 
142     /*
143      * 2). copy right image.
144                                     dst_img
145         --------------    ---------------------------
146         |            |    |            |            |
147         | right_img  | => |            | right_rect |
148         |            |    |            |            |
149         --------------    ---------------------------
150      */
151     right_rect.x = left_width;
152     right_rect.y = 0;
153     right_rect.width = right_width;
154     right_rect.height = right_height;
155 
156     ret = imcheck(right_img, dst_img, {}, right_rect);
157     if (IM_STATUS_NOERROR != ret) {
158         printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
159         goto release_buffer;
160     }
161 
162     ret = improcess(right_img, dst_img, {}, {}, right_rect, {}, IM_SYNC);
163     if (ret == IM_STATUS_SUCCESS) {
164         printf("%s right running success!\n", LOG_TAG);
165     } else {
166         printf("%s right running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
167         goto release_buffer;
168     }
169 
170 	printf("output [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
171     write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
172 
173 release_buffer:
174     if (left_handle)
175         releasebuffer_handle(left_handle);
176     if (dst_handle)
177         releasebuffer_handle(dst_handle);
178 
179     if (left_buf)
180         free(left_buf);
181     if (dst_buf)
182         free(dst_buf);
183 
184     return ret;
185 }
186