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_array_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 green_rect[4] = {};
49 im_rect red_rect[4] = {};
50 rga_buffer_handle_t dst_handle;
51 im_job_handle_t job_handle;
52
53 dst_width = 1920;
54 dst_height = 1080;
55 dst_format = RK_FORMAT_RGBA_8888;
56
57 dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
58
59 /*
60 * Allocate dma_buf within 4G from dma32_heap,
61 * return dma_fd and virtual address.
62 * ColorFill can only be used on buffers within 4G.
63 */
64 ret = dma_buf_alloc(DMA_HEAP_DMA32_UNCACHE_PATCH, dst_buf_size, &dst_dma_fd, (void **)&dst_buf);
65 if (ret < 0) {
66 printf("alloc dma32_heap buffer failed!\n");
67 return -1;
68 }
69
70 memset(dst_buf, 0x33, dst_buf_size);
71
72 /*
73 * Import the allocated dma_fd into RGA by calling
74 * importbuffer_fd, and use the returned buffer_handle
75 * to call RGA to process the image.
76 */
77 dst_handle = importbuffer_fd(dst_dma_fd, dst_buf_size);
78 if (dst_handle == 0) {
79 printf("import dma_fd error!\n");
80 ret = -1;
81 goto free_buf;
82 }
83
84 dst = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
85
86 /*
87 * Fill one rectangle array with a green solid rectangle, and fill another
88 * rectangle array with a red border of thickness 2.
89 dst_image
90 --------------
91 | ------- |
92 | | --|-- |
93 | ----|-- | |
94 | ----- |
95 --------------
96 */
97
98 /* Create a job handle. */
99 job_handle = imbeginJob();
100 if (job_handle <= 0) {
101 printf("job begin failed![%d], %s\n", job_handle, imStrError());
102 goto release_buffer;
103 }
104
105 /* Add a task that fills an array of green rectangle borders */
106 green_rect[0] = {0, 0, 100, 100};
107 green_rect[1] = {50, 50, 150, 150};
108 green_rect[2] = {100, 100, 200, 200};
109 green_rect[3] = {150, 150, 250, 250};
110
111 for (int i = 0; i < 4; i++) {
112 ret = imcheck({}, dst, {}, green_rect[i], IM_COLOR_FILL);
113 if (IM_STATUS_NOERROR != ret) {
114 printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
115 goto cancel_job;
116 }
117 }
118
119 ret = imrectangleTaskArray(job_handle, dst, green_rect, 4, 0xff00ff00, -1);
120 if (ret == IM_STATUS_SUCCESS) {
121 printf("%s job[%d] add fill task array success!\n", LOG_TAG, job_handle);
122 } else {
123 printf("%s job[%d] add fill task array failed, %s\n", LOG_TAG, job_handle, imStrError((IM_STATUS)ret));
124 goto cancel_job;
125 }
126
127 /* Add a task that fills an array of green rectangle borders */
128 red_rect[0] = {200, 200, 300, 300};
129 red_rect[1] = {250, 250, 350, 350};
130 red_rect[2] = {300, 300, 400, 400};
131 red_rect[3] = {350, 350, 450, 450};
132
133 for (int i = 0; i < 4; i++) {
134 ret = imcheck({}, dst, {}, red_rect[i], IM_COLOR_FILL);
135 if (IM_STATUS_NOERROR != ret) {
136 printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
137 goto cancel_job;
138 }
139 }
140
141 ret = imrectangleTaskArray(job_handle, dst, red_rect, 4, 0xff0000ff, 2);
142 if (ret == IM_STATUS_SUCCESS) {
143 printf("%s job[%d] add fill task array success!\n", LOG_TAG, job_handle);
144 } else {
145 printf("%s job[%d] add fill task array failed, %s\n", LOG_TAG, job_handle, imStrError((IM_STATUS)ret));
146 goto cancel_job;
147 }
148
149 /* Submit and wait for the job to complete. */
150 ret = imendJob(job_handle);
151 if (ret == IM_STATUS_SUCCESS) {
152 printf("%s job[%d] running success!\n", LOG_TAG, job_handle);
153 } else {
154 printf("%s job[%d] running failed, %s\n", LOG_TAG, job_handle, imStrError((IM_STATUS)ret));
155 goto release_buffer;
156 }
157
158 printf("output [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
159 write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
160
161 cancel_job:
162 imcancelJob(job_handle);
163
164 release_buffer:
165 if (dst_handle > 0)
166 releasebuffer_handle(dst_handle);
167
168 free_buf:
169 dma_buf_free(dst_buf_size, &dst_dma_fd, dst_buf);
170
171 return 0;
172 }
173