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_async_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 src_width, src_height, src_format;
48 int tmp_width, tmp_height, tmp_format;
49 int dst_width, dst_height, dst_format;
50 char *src_buf, *tmp_buf, *dst_buf;
51 int src_buf_size, tmp_buf_size, dst_buf_size;
52 int acquire_fence_fd, release_fence_fd;
53
54 rga_buffer_t src_img, tmp_img, dst_img;
55 rga_buffer_handle_t src_handle, tmp_handle, dst_handle;
56
57 memset(&src_img, 0, sizeof(src_img));
58 memset(&tmp_img, 0, sizeof(tmp_img));
59 memset(&dst_img, 0, sizeof(dst_img));
60
61 src_width = 1280;
62 src_height = 720;
63 src_format = RK_FORMAT_RGBA_8888;
64
65 tmp_width = 1280;
66 tmp_height = 720;
67 tmp_format = RK_FORMAT_RGBA_8888;
68
69 dst_width = 1280;
70 dst_height = 720;
71 dst_format = RK_FORMAT_RGBA_8888;
72
73 src_buf_size = src_width * src_height * get_bpp_from_format(src_format);
74 tmp_buf_size = tmp_width * tmp_height * get_bpp_from_format(tmp_format);
75 dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
76
77 src_buf = (char *)malloc(src_buf_size);
78 tmp_buf = (char *)malloc(tmp_buf_size);
79 dst_buf = (char *)malloc(dst_buf_size);
80
81 /* fill image data */
82 if (0 != read_image_from_file(src_buf, LOCAL_FILE_PATH, src_width, src_height, src_format, 0)) {
83 printf("src image image read err\n");
84 draw_rgba(src_buf, src_width, src_height);
85 }
86 memset(tmp_buf, 0x40, tmp_buf_size);
87 memset(dst_buf, 0x80, dst_buf_size);
88
89 src_handle = importbuffer_virtualaddr(src_buf, src_buf_size);
90 tmp_handle = importbuffer_virtualaddr(tmp_buf, tmp_buf_size);
91 dst_handle = importbuffer_virtualaddr(dst_buf, dst_buf_size);
92 if (src_handle == 0 || tmp_handle == 0 || dst_handle == 0) {
93 printf("importbuffer failed!\n");
94 goto release_buffer;
95 }
96
97 src_img = wrapbuffer_handle(src_handle, src_width, src_height, src_format);
98 tmp_img = wrapbuffer_handle(tmp_handle, tmp_width, tmp_height, tmp_format);
99 dst_img = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
100
101 /*
102 * The image will be asynchronously copied from src_image to tmp, and
103 * rely on fence to copy src_image to tmp_image and then copy to dst_image.
104 -------------- -------------- --------------
105 | | | | | |
106 | src_image | => | tmp_image | => | dst_image |
107 | | | | | |
108 -------------- -------------- --------------
109 */
110
111 /*
112 * (1). Copy src_image to tmp_image and get release_fence.
113 */
114 ret = imcheck(src_img, tmp_img, {}, {});
115 if (IM_STATUS_NOERROR != ret) {
116 printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
117 goto release_buffer;
118 }
119
120 ret = imcopy(src_img, tmp_img, 1, &release_fence_fd);
121 if (ret == IM_STATUS_SUCCESS) {
122 printf("%s src->tmp running success!\n", LOG_TAG);
123 } else {
124 printf("%s src->tmp running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
125 goto release_buffer;
126 }
127
128 if (release_fence_fd < 0) {
129 printf("get release_fence_fd failed!\n");
130 goto release_buffer;
131 }
132
133 /*
134 * (2). Copy tmp_image to dst_image, use the release_fence obtained in
135 * operation 1 as acquire_fence, and get the release_fence of this task.
136 */
137 acquire_fence_fd = release_fence_fd;
138 release_fence_fd = -1;
139
140 ret = imcheck(tmp_img, dst_img, {}, {});
141 if (IM_STATUS_NOERROR != ret) {
142 printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
143 goto release_buffer;
144 }
145
146 ret = improcess(tmp_img, dst_img, {}, {}, {}, {}, acquire_fence_fd, &release_fence_fd, NULL, IM_ASYNC);
147 if (ret == IM_STATUS_SUCCESS) {
148 printf("%s tmp->dst running success!\n", LOG_TAG);
149 } else {
150 printf("%s tmp->dst running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
151 goto release_buffer;
152 }
153
154 if (release_fence_fd < 0) {
155 printf("get release_fence_fd failed!\n");
156 goto release_buffer;
157 }
158
159 /*
160 * (3). Will wait for the release_fence of the last operation, when it is
161 * signed it indicates that all tasks are completed.
162 */
163 ret = imsync(release_fence_fd);
164 if (ret == IM_STATUS_SUCCESS) {
165 printf("%s waiting success!\n", LOG_TAG);
166 } else {
167 printf("%s waiting failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
168 goto release_buffer;
169 }
170
171 printf("output [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
172 write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
173
174 release_buffer:
175 if (src_handle)
176 releasebuffer_handle(src_handle);
177 if (tmp_handle)
178 releasebuffer_handle(tmp_handle);
179 if (dst_handle)
180 releasebuffer_handle(dst_handle);
181
182 if (src_buf)
183 free(src_buf);
184 if (tmp_buf)
185 free(tmp_buf);
186 if (dst_buf)
187 free(dst_buf);
188
189 return ret;
190 }
191