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_fill_rectangle_task_demo"
22
23 #include <iostream>
24 #include <fstream>
25 #include <sstream>
26 #include <cstddef>
27 #include <cmath>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/time.h>
31 #include <unistd.h>
32
33 #include "im2d.h"
34 #include "RgaUtils.h"
35
36 #include "utils.h"
37 #include "dma_alloc.h"
38
39 #define LOCAL_FILE_PATH "/data"
40
main(void)41 int main(void) {
42 int ret = 0;
43 int dst_width, dst_height, dst_format;
44 int dst_buf_size;
45 char *dst_buf;
46 int dst_dma_fd;
47 rga_buffer_t dst = {};
48 im_rect dst_rect[2] = {};
49 rga_buffer_handle_t dst_handle;
50 im_job_handle_t job_handle;
51
52 dst_width = 1280;
53 dst_height = 720;
54 dst_format = RK_FORMAT_RGBA_8888;
55
56 dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
57
58 /*
59 * Allocate dma_buf within 4G from dma32_heap,
60 * return dma_fd and virtual address.
61 * ColorFill can only be used on buffers within 4G.
62 */
63 ret = dma_buf_alloc(DMA_HEAP_DMA32_UNCACHE_PATCH, dst_buf_size, &dst_dma_fd, (void **)&dst_buf);
64 if (ret < 0) {
65 printf("alloc dma32_heap buffer failed!\n");
66 return -1;
67 }
68
69 memset(dst_buf, 0x33, dst_buf_size);
70
71 /*
72 * Import the allocated dma_fd into RGA by calling
73 * importbuffer_fd, and use the returned buffer_handle
74 * to call RGA to process the image.
75 */
76 dst_handle = importbuffer_fd(dst_dma_fd, dst_buf_size);
77 if (dst_handle == 0) {
78 printf("import dma_fd error!\n");
79 ret = -1;
80 goto free_buf;
81 }
82
83 dst = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
84
85 /*
86 * Fills multiple rectangular areas on the dst image with the specified color.
87 dst_image
88 --------------
89 | ------- |
90 | | --|-- |
91 | ----|-- | |
92 | ----- |
93 --------------
94 */
95
96 /* Create a job handle. */
97 job_handle = imbeginJob();
98 if (job_handle <= 0) {
99 printf("job begin failed![%d], %s\n", job_handle, imStrError());
100 goto release_buffer;
101 }
102
103 /* Add a task to fill a filled rectangle. */
104 dst_rect[0] = {0, 0, 300, 200};
105
106 ret = imcheck({}, dst, {}, dst_rect[0], IM_COLOR_FILL);
107 if (IM_STATUS_NOERROR != ret) {
108 printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
109 goto cancel_job;
110 }
111
112 ret = imrectangleTask(job_handle, dst, dst_rect[0], 0xff00ff00, -1);
113 if (ret == IM_STATUS_SUCCESS) {
114 printf("%s job[%d] add fill task success!\n", LOG_TAG, job_handle);
115 } else {
116 printf("%s job[%d] add fill task failed, %s\n", LOG_TAG, job_handle, imStrError((IM_STATUS)ret));
117 goto cancel_job;
118 }
119
120 /* Add a task to fill the rectangle border. */
121 dst_rect[0] = {100, 100, 300, 200};
122
123 ret = imcheck({}, dst, {}, dst_rect[0], IM_COLOR_FILL);
124 if (IM_STATUS_NOERROR != ret) {
125 printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
126 goto cancel_job;
127 }
128
129 ret = imrectangleTask(job_handle, dst, dst_rect[0], 0xffff0000, 4);
130 if (ret == IM_STATUS_SUCCESS) {
131 printf("%s job[%d] add fill task success!\n", LOG_TAG, job_handle);
132 } else {
133 printf("%s job[%d] add fill task failed, %s\n", LOG_TAG, job_handle, imStrError((IM_STATUS)ret));
134 goto cancel_job;
135 }
136
137 /* Submit and wait for the job to complete. */
138 ret = imendJob(job_handle);
139 if (ret == IM_STATUS_SUCCESS) {
140 printf("%s job[%d] running success!\n", LOG_TAG, job_handle);
141 } else {
142 printf("%s job[%d] running failed, %s\n", LOG_TAG, job_handle, imStrError((IM_STATUS)ret));
143 goto release_buffer;
144 }
145
146 printf("output [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
147 write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
148
149 cancel_job:
150 imcancelJob(job_handle);
151
152 release_buffer:
153 if (dst_handle > 0)
154 releasebuffer_handle(dst_handle);
155
156 free_buf:
157 dma_buf_free(dst_buf_size, &dst_dma_fd, dst_buf);
158
159 return 0;
160 }
161