xref: /OK3568_Linux_fs/external/linux-rga/samples/fill_demo/src/rga_fill_rectangle_array_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_fill_rectangle_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 dst_rect[4] = {};
49     rga_buffer_handle_t dst_handle;
50 
51     dst_width = 1280;
52     dst_height = 720;
53     dst_format = RK_FORMAT_RGBA_8888;
54 
55     dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
56 
57     /*
58      * Allocate dma_buf within 4G from dma32_heap,
59      * return dma_fd and virtual address.
60      * ColorFill can only be used on buffers within 4G.
61      */
62     ret = dma_buf_alloc(DMA_HEAP_DMA32_UNCACHE_PATCH, dst_buf_size, &dst_dma_fd, (void **)&dst_buf);
63     if (ret < 0) {
64         printf("alloc dma32_heap buffer failed!\n");
65         return -1;
66     }
67 
68     memset(dst_buf, 0x33, dst_buf_size);
69 
70     /*
71      * Import the allocated dma_fd into RGA by calling
72      * importbuffer_fd, and use the returned buffer_handle
73      * to call RGA to process the image.
74      */
75     dst_handle = importbuffer_fd(dst_dma_fd, dst_buf_size);
76     if (dst_handle == 0) {
77         printf("import dma_fd error!\n");
78         ret = -1;
79         goto free_buf;
80     }
81 
82     dst = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
83 
84     /*
85      * Fills an array of rectangular areas on the dst image with the specified color.
86            dst_image
87         --------------
88         | -------    |
89         | |   --|--  |
90         | ----|-- |  |
91         |     -----  |
92         --------------
93      */
94 
95     dst_rect[0] = {0, 0, 100, 100};
96     dst_rect[1] = {50, 50, 150, 150};
97     dst_rect[2] = {100, 100, 200, 200};
98     dst_rect[3] = {150, 150, 250, 250};
99 
100     for (int i = 0; i < 4; i++) {
101         ret = imcheck({}, dst, {}, dst_rect[i], IM_COLOR_FILL);
102         if (IM_STATUS_NOERROR != ret) {
103             printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
104             goto release_buffer;
105         }
106     }
107 
108     ret = imrectangleArray(dst, dst_rect, 4, 0xff00ff00, 2);
109     if (ret == IM_STATUS_SUCCESS) {
110         printf("%s running success!\n", LOG_TAG);
111     } else {
112         printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
113         goto release_buffer;
114     }
115 
116     printf("output [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
117     write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
118 
119 release_buffer:
120     if (dst_handle > 0)
121         releasebuffer_handle(dst_handle);
122 
123 free_buf:
124     dma_buf_free(dst_buf_size, &dst_dma_fd, dst_buf);
125 
126     return 0;
127 }
128