xref: /OK3568_Linux_fs/external/linux-rga/samples/allocator_demo/src/rga_allocator_malloc_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_allocator_malloc_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 
38 #define LOCAL_FILE_PATH "/data"
39 
main(void)40 int main(void) {
41     int ret = 0;
42     int64_t ts;
43     int src_width, src_height, src_format;
44     int dst_width, dst_height, dst_format;
45     int src_buf_size, dst_buf_size;
46     char *src_buf, *dst_buf;
47     rga_buffer_t src = {};
48     rga_buffer_t dst = {};
49     im_rect src_rect = {};
50     im_rect dst_rect = {};
51     rga_buffer_handle_t src_handle, dst_handle;
52 
53     src_width = 1280;
54     src_height = 720;
55     src_format = RK_FORMAT_RGBA_8888;
56 
57     dst_width = 1280;
58     dst_height = 720;
59     dst_format = RK_FORMAT_RGBA_8888;
60 
61     src_buf_size = src_width * src_height * get_bpp_from_format(src_format);
62     dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
63     src_buf = (char *)malloc(src_buf_size);
64     dst_buf = (char *)malloc(dst_buf_size);
65     if (src_buf == NULL || dst_buf == NULL) {
66         printf("malloc buffer failed!\n");
67         return -1;
68     }
69 
70     ret = read_image_from_file(src_buf, LOCAL_FILE_PATH, src_width, src_height, src_format, 0);
71     if (ret < 0) {
72         printf ("open file %s so memset!\n", "fault");
73         draw_rgba((char *)src_buf, src_width, src_height);
74     }
75     memset(dst_buf, 0x33, dst_buf_size);
76 
77     /*
78      * Import the allocated virtual address into RGA by calling
79      * importbuffer_virtualaddr, and use the returned buffer_handle
80      * to call RGA to process the image.
81      */
82     src_handle = importbuffer_virtualaddr(src_buf, src_buf_size);
83     dst_handle = importbuffer_virtualaddr(dst_buf, dst_buf_size);
84     if (src_handle == 0 || dst_handle == 0) {
85         printf("import malloc virt_addr error!\n");
86         ret = -1;
87         goto free_buf;
88     }
89 
90     src = wrapbuffer_handle(src_handle, src_width, src_height, src_format);
91     dst = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
92 
93     ret = imcheck(src, dst, src_rect, dst_rect);
94     if (IM_STATUS_NOERROR != ret) {
95         printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
96         goto release_buffer;
97     }
98 
99     ts = get_cur_us();
100 
101     ret = imcopy(src, dst);
102     if (ret == IM_STATUS_SUCCESS) {
103         printf("%s running success! cost %ld us\n", LOG_TAG, get_cur_us() - ts);
104     } else {
105         printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
106         goto release_buffer;
107     }
108 
109     printf("output [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
110     write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
111 
112 release_buffer:
113     if (src_handle > 0)
114         releasebuffer_handle(src_handle);
115     if (dst_handle > 0)
116         releasebuffer_handle(dst_handle);
117 
118 free_buf:
119     if (src_buf)
120         free(src_buf);
121     if (dst_buf)
122         free(dst_buf);
123 
124     return 0;
125 }
126