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_dma_cache_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 int64_t ts;
44 int src_width, src_height, src_format;
45 int dst_width, dst_height, dst_format;
46 int src_buf_size, dst_buf_size;
47 char *src_buf, *dst_buf;
48 int src_dma_fd, dst_dma_fd;
49 rga_buffer_t src = {};
50 rga_buffer_t dst = {};
51 im_rect src_rect = {};
52 im_rect dst_rect = {};
53 rga_buffer_handle_t src_handle, dst_handle;
54
55 src_width = 1280;
56 src_height = 720;
57 src_format = RK_FORMAT_RGBA_8888;
58
59 dst_width = 1280;
60 dst_height = 720;
61 dst_format = RK_FORMAT_RGBA_8888;
62
63 src_buf_size = src_width * src_height * get_bpp_from_format(src_format);
64 dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
65
66 /* Allocate cacheable dma_buf, return dma_fd and virtual address. */
67 ret = dma_buf_alloc(DMA_HEAP_PATH, src_buf_size, &src_dma_fd, (void **)&src_buf);
68 if (ret < 0) {
69 printf("alloc src dma_heap buffer failed!\n");
70 return -1;
71 }
72
73 ret = dma_buf_alloc(DMA_HEAP_PATH, dst_buf_size, &dst_dma_fd, (void **)&dst_buf);
74 if (ret < 0) {
75 printf("alloc dst dma_heap buffer failed!\n");
76 dma_buf_free(src_buf_size, &src_dma_fd, src_buf);
77 return -1;
78 }
79
80 ret = read_image_from_file(src_buf, LOCAL_FILE_PATH, src_width, src_height, src_format, 0);
81 if (ret < 0) {
82 printf ("open file %s so memset!\n", "fault");
83 draw_rgba((char *)src_buf, src_width, src_height);
84 }
85 memset(dst_buf, 0x33, dst_buf_size);
86
87 /* clear CPU cache */
88 dma_sync_cpu_to_device(src_dma_fd);
89 dma_sync_cpu_to_device(dst_dma_fd);
90
91 /*
92 * Import the allocated dma_fd into RGA by calling
93 * importbuffer_fd, and use the returned buffer_handle
94 * to call RGA to process the image.
95 */
96 src_handle = importbuffer_fd(src_dma_fd, src_buf_size);
97 dst_handle = importbuffer_fd(dst_dma_fd, dst_buf_size);
98 if (src_handle == 0 || dst_handle == 0) {
99 printf("import dma_fd error!\n");
100 ret = -1;
101 goto free_buf;
102 }
103
104 src = wrapbuffer_handle(src_handle, src_width, src_height, src_format);
105 dst = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
106
107 ret = imcheck(src, dst, src_rect, dst_rect);
108 if (IM_STATUS_NOERROR != ret) {
109 printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
110 goto release_buffer;
111 }
112
113 ts = get_cur_us();
114
115 ret = imcopy(src, dst);
116 if (ret == IM_STATUS_SUCCESS) {
117 printf("%s running success! cost %ld us\n", LOG_TAG, get_cur_us() - ts);
118 } else {
119 printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
120 goto release_buffer;
121 }
122
123 printf("Before flushing the cache [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
124 write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
125
126 /* invalid CPU cache */
127 dma_sync_device_to_cpu(src_dma_fd);
128 dma_sync_device_to_cpu(dst_dma_fd);
129
130 printf("After flushing the cache [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
131 write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 1);
132
133 release_buffer:
134 if (src_handle > 0)
135 releasebuffer_handle(src_handle);
136 if (dst_handle > 0)
137 releasebuffer_handle(dst_handle);
138
139 free_buf:
140 dma_buf_free(src_buf_size, &src_dma_fd, src_buf);
141 dma_buf_free(dst_buf_size, &dst_dma_fd, dst_buf);
142
143 return 0;
144 }
145