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_drm_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 "drm_alloc.h"
37 #include "utils.h"
38
39 #define LOCAL_FILE_PATH "/data"
40
41 class DrmObject {
42 public:
43 int drm_buffer_fd;
44 int drm_buffer_handle;
45 size_t actual_size;
46 uint8_t * drm_buf;
47 };
48
main(void)49 int main(void) {
50 int ret = 0;
51 int64_t ts;
52 int src_width, src_height, src_format;
53 int dst_width, dst_height, dst_format;
54 int src_alloc_flags = 0, dst_alloc_flags = 0;
55 rga_buffer_t src = {};
56 rga_buffer_t dst = {};
57 im_rect src_rect = {};
58 im_rect dst_rect = {};
59 DrmObject drm_src;
60 DrmObject drm_dst;
61 rga_buffer_handle_t src_handle, dst_handle;
62
63 src_width = 1280;
64 src_height = 720;
65 src_format = RK_FORMAT_RGBA_8888;
66
67 dst_width = 1280;
68 dst_height = 720;
69 dst_format = RK_FORMAT_RGBA_8888;
70
71 /* Allocate drm_buf, return dma_fd and virtual address. */
72 drm_src.drm_buf = (uint8_t *)drm_buf_alloc(src_width, src_height,
73 get_bpp_from_format(src_format) * 8,
74 &drm_src.drm_buffer_fd,
75 &drm_src.drm_buffer_handle,
76 &drm_src.actual_size,
77 src_alloc_flags);
78
79 drm_dst.drm_buf = (uint8_t *)drm_buf_alloc(dst_width, dst_height,
80 get_bpp_from_format(dst_format) * 8,
81 &drm_dst.drm_buffer_fd,
82 &drm_dst.drm_buffer_handle,
83 &drm_dst.actual_size,
84 dst_alloc_flags);
85 if (drm_src.drm_buf == NULL || drm_dst.drm_buf == NULL) {
86 printf("alloc drm buffer failed!\n");
87 return -1;
88 }
89
90 ret = read_image_from_file(drm_src.drm_buf, LOCAL_FILE_PATH, src_width, src_height, src_format, 0);
91 if (ret < 0) {
92 printf ("open file %s so memset!\n", "fault");
93 draw_rgba((char *)drm_src.drm_buf, src_width, src_height);
94 }
95 memset(drm_dst.drm_buf, 0x33, drm_dst.actual_size);
96
97 /*
98 * Import the allocated dma_fd into RGA by calling
99 * importbuffer_fd, and use the returned buffer_handle
100 * to call RGA to process the image.
101 */
102 src_handle = importbuffer_fd(drm_src.drm_buffer_fd, drm_src.actual_size);
103 dst_handle = importbuffer_fd(drm_dst.drm_buffer_fd, drm_dst.actual_size);
104 if (src_handle == 0 || dst_handle == 0) {
105 printf("import drm fd error!\n");
106 ret = -1;
107 goto drm_buf_destroy;
108 }
109
110 src = wrapbuffer_handle(src_handle, src_width, src_height, src_format);
111 dst = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
112
113 ret = imcheck(src, dst, src_rect, dst_rect);
114 if (IM_STATUS_NOERROR != ret) {
115 printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
116 goto release_buffer;
117 }
118
119 ts = get_cur_us();
120
121 ret = imcopy(src, dst);
122 if (ret == IM_STATUS_SUCCESS) {
123 printf("%s running success! cost %ld us\n", LOG_TAG, get_cur_us() - ts);
124 } else {
125 printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
126 goto release_buffer;
127 }
128
129 printf("output [0x%x, 0x%x, 0x%x, 0x%x]\n", drm_dst.drm_buf[0], drm_dst.drm_buf[1], drm_dst.drm_buf[2], drm_dst.drm_buf[3]);
130 write_image_to_file(drm_dst.drm_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
131
132 release_buffer:
133 if (src_handle > 0)
134 releasebuffer_handle(src_handle);
135 if (dst_handle > 0)
136 releasebuffer_handle(dst_handle);
137
138 drm_buf_destroy:
139 drm_buf_destroy(drm_src.drm_buffer_fd, drm_src.drm_buffer_handle, drm_src.drm_buf, drm_src.actual_size);
140 drm_buf_destroy(drm_dst.drm_buffer_fd, drm_dst.drm_buffer_handle, drm_dst.drm_buf, drm_dst.actual_size);
141
142 return 0;
143 }
144