xref: /OK3568_Linux_fs/external/linux-rga/samples/allocator_demo/src/rga_allocator_drm_phy_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_drm_phy_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     long src_phy, dst_phy;
56     rga_buffer_t src = {};
57     rga_buffer_t dst = {};
58     im_rect src_rect = {};
59     im_rect dst_rect = {};
60     DrmObject drm_src;
61     DrmObject drm_dst;
62     rga_buffer_handle_t src_handle, dst_handle;
63 
64     src_width = 1280;
65     src_height = 720;
66     src_format = RK_FORMAT_RGBA_8888;
67 
68     dst_width = 1280;
69     dst_height = 720;
70     dst_format = RK_FORMAT_RGBA_8888;
71 
72     src_alloc_flags |= ROCKCHIP_BO_CONTIG;
73     dst_alloc_flags |= ROCKCHIP_BO_CONTIG;
74 
75     /*
76      * Allocate a physically continuous drm_buf, return dma_fd and
77      * virtual address, and get the physical address.
78      */
79     drm_src.drm_buf = (uint8_t *)drm_buf_alloc(src_width, src_height,
80                                                get_bpp_from_format(src_format) * 8,
81                                                &drm_src.drm_buffer_fd,
82                                                &drm_src.drm_buffer_handle,
83                                                &drm_src.actual_size,
84                                                src_alloc_flags);
85 
86     drm_dst.drm_buf = (uint8_t *)drm_buf_alloc(dst_width, dst_height,
87                                                get_bpp_from_format(dst_format) * 8,
88                                                &drm_dst.drm_buffer_fd,
89                                                &drm_dst.drm_buffer_handle,
90                                                &drm_dst.actual_size,
91                                                dst_alloc_flags);
92     if (drm_src.drm_buf == NULL || drm_dst.drm_buf == NULL) {
93         printf("alloc physically continuous drm buffer failed!\n");
94         return -1;
95     }
96 
97     src_phy = drm_buf_get_phy(drm_src.drm_buffer_handle);
98     dst_phy = drm_buf_get_phy(drm_dst.drm_buffer_handle);
99     if (src_phy == 0 || dst_phy == 0) {
100         printf("get drm buffer phy_addr failed!\n");
101         goto drm_buf_destroy;
102     }
103 
104     ret = read_image_from_file(drm_src.drm_buf, LOCAL_FILE_PATH, src_width, src_height, src_format, 0);
105     if (ret < 0) {
106         printf ("open file %s so memset!\n", "fault");
107         draw_rgba((char *)drm_src.drm_buf, src_width, src_height);
108     }
109     memset(drm_dst.drm_buf, 0x33, drm_dst.actual_size);
110 
111     /*
112      * Import the allocated physical address into RGA by calling
113      * importbuffer_physicaladdr, and use the returned buffer_handle
114      * to call RGA to process the image.
115      */
116     src_handle = importbuffer_physicaladdr(src_phy, drm_src.actual_size);
117     dst_handle = importbuffer_physicaladdr(dst_phy, drm_dst.actual_size);
118     if (src_handle == 0 || dst_handle == 0) {
119         printf("import drm phy_addr error!\n");
120         ret = -1;
121         goto drm_buf_destroy;
122     }
123 
124     src = wrapbuffer_handle(src_handle, src_width, src_height, src_format);
125     dst = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
126 
127     ret = imcheck(src, dst, src_rect, dst_rect);
128     if (IM_STATUS_NOERROR != ret) {
129         printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
130         goto release_buffer;
131     }
132 
133     ts = get_cur_us();
134 
135     ret = imcopy(src, dst);
136     if (ret == IM_STATUS_SUCCESS) {
137         printf("%s running success! cost %ld us\n", LOG_TAG, get_cur_us() - ts);
138     } else {
139         printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
140         goto release_buffer;
141     }
142 
143     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]);
144     write_image_to_file(drm_dst.drm_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
145 
146 release_buffer:
147     if (src_handle > 0)
148         releasebuffer_handle(src_handle);
149     if (dst_handle > 0)
150         releasebuffer_handle(dst_handle);
151 
152 drm_buf_destroy:
153     drm_buf_destroy(drm_src.drm_buffer_fd, drm_src.drm_buffer_handle, drm_src.drm_buf, drm_src.actual_size);
154     drm_buf_destroy(drm_dst.drm_buffer_fd, drm_dst.drm_buffer_handle, drm_dst.drm_buf, drm_dst.actual_size);
155 
156     return 0;
157 }
158