xref: /OK3568_Linux_fs/external/linux-rga/samples/copy_demo/src/rga_copy_splice_task_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_task_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 
41 #include "utils.h"
42 
43 #define LOCAL_FILE_PATH "/data"
44 
main()45 int main() {
46     int ret = 0;
47     int left_width, left_height, left_format;
48     int right_width, right_height, right_format;
49     int dst_width, dst_height, dst_format;
50     char *left_buf, *right_buf, *dst_buf;
51     int left_buf_size, right_buf_size, dst_buf_size;
52 
53     rga_buffer_t left_img, right_img, dst_img;
54     im_rect left_rect, right_rect;
55     rga_buffer_handle_t left_handle, right_handle, dst_handle;
56     im_job_handle_t job_handle;
57 
58     memset(&left_img, 0, sizeof(left_img));
59     memset(&right_img, 0, sizeof(right_img));
60     memset(&dst_img, 0, sizeof(dst_img));
61     memset(&left_rect, 0, sizeof(left_rect));
62     memset(&right_rect, 0, sizeof(right_rect));
63 
64     left_width = 1280;
65     left_height = 720;
66     left_format = RK_FORMAT_RGBA_8888;
67 
68     right_width = 1280;
69     right_height = 720;
70     right_format = RK_FORMAT_RGBA_8888;
71 
72     dst_width = 2560;
73     dst_height = 720;
74     dst_format = RK_FORMAT_RGBA_8888;
75 
76     left_buf_size = left_width * left_height * get_bpp_from_format(left_format);
77     right_buf_size = right_width * right_height * get_bpp_from_format(right_format);
78     dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
79 
80     left_buf = (char *)malloc(left_buf_size);
81     right_buf = (char *)malloc(right_buf_size);
82     dst_buf = (char *)malloc(dst_buf_size);
83 
84     /* fill image data */
85     if (0 != read_image_from_file(left_buf, LOCAL_FILE_PATH, left_width, left_height, left_format, 0)) {
86         printf("left image read err\n");
87         memset(left_buf, 0xaa, left_buf_size);
88     }
89     if (0 != read_image_from_file(right_buf, LOCAL_FILE_PATH, right_width, right_height, right_format, 0)) {
90         printf("right image read err\n");
91         memset(left_buf, 0xbb, left_buf_size);
92     }
93     memset(dst_buf, 0x80, dst_buf_size);
94 
95     left_handle = importbuffer_virtualaddr(left_buf, left_buf_size);
96     right_handle = importbuffer_virtualaddr(right_buf, left_buf_size);
97     dst_handle = importbuffer_virtualaddr(dst_buf, dst_buf_size);
98     if (left_handle == 0 || dst_handle == 0) {
99         printf("importbuffer failed!\n");
100         goto release_buffer;
101     }
102 
103     left_img = wrapbuffer_handle(left_handle, left_width, left_height, left_format);
104     right_img = wrapbuffer_handle(right_handle, right_width, right_height, right_format);
105     dst_img = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
106 
107     /*
108      * Splicing the left and right images to output.
109         ---------------------------
110         |            |            |
111         | left_rect  | right_rect |
112         |            |            |
113         ---------------------------
114      */
115 
116     /*
117      * 1). Create a job handle.
118      */
119     job_handle = imbeginJob();
120     if (job_handle <= 0) {
121         printf("job begin failed![%d], %s\n", job_handle, imStrError());
122         goto release_buffer;
123     }
124 
125     /*
126      * 2). Add a task of copying the left image.
127                                     dst_img
128         --------------    ---------------------------
129         |            |    |            |            |
130         |  left_img  | => |  left_rect |            |
131         |            |    |            |            |
132         --------------    ---------------------------
133      */
134     left_rect.x = 0;
135     left_rect.y = 0;
136     left_rect.width = left_width;
137     left_rect.height = left_height;
138 
139     ret = imcheck(left_img, dst_img, {}, left_rect);
140     if (IM_STATUS_NOERROR != ret) {
141         printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
142         goto cancel_job;
143     }
144 
145     ret = improcessTask(job_handle, left_img, dst_img, {}, {}, left_rect, {}, NULL, IM_SYNC);
146     if (ret == IM_STATUS_SUCCESS) {
147         printf("%s job[%d] add left task success!\n", LOG_TAG, job_handle);
148     } else {
149         printf("%s job[%d] add left task failed, %s\n", LOG_TAG, job_handle, imStrError((IM_STATUS)ret));
150         goto cancel_job;
151     }
152 
153     /*
154      * 3). Add a task of copying the right image.
155                                     dst_img
156         --------------    ---------------------------
157         |            |    |            |            |
158         | right_img  | => |            | right_rect |
159         |            |    |            |            |
160         --------------    ---------------------------
161      */
162     right_rect.x = left_width;
163     right_rect.y = 0;
164     right_rect.width = right_width;
165     right_rect.height = right_height;
166 
167     ret = imcheck(right_img, dst_img, {}, right_rect);
168     if (IM_STATUS_NOERROR != ret) {
169         printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
170         goto cancel_job;
171     }
172 
173     ret = improcessTask(job_handle, right_img, dst_img, {}, {}, right_rect, {}, NULL, IM_SYNC);
174     if (ret == IM_STATUS_SUCCESS) {
175         printf("%s job[%d] add right task success!\n", LOG_TAG, job_handle);
176     } else {
177         printf("%s job[%d] add right task failed, %s\n", LOG_TAG, job_handle, imStrError((IM_STATUS)ret));
178         goto cancel_job;
179     }
180 
181     /*
182      * 4). Submit and wait for the job to complete.
183      */
184     ret = imendJob(job_handle);
185     if (ret == IM_STATUS_SUCCESS) {
186         printf("%s job[%d] running success!\n", LOG_TAG, job_handle);
187     } else {
188         printf("%s job[%d] running failed, %s\n", LOG_TAG, job_handle, imStrError((IM_STATUS)ret));
189         goto release_buffer;
190     }
191 
192 	printf("output [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
193     write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
194 
195 cancel_job:
196     imcancelJob(job_handle);
197 
198 release_buffer:
199     if (left_handle)
200         releasebuffer_handle(left_handle);
201     if (dst_handle)
202         releasebuffer_handle(dst_handle);
203 
204     if (left_buf)
205         free(left_buf);
206     if (dst_buf)
207         free(dst_buf);
208 
209     return ret;
210 }
211