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_graphicbuffer_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.hpp"
34 #include "RgaUtils.h"
35 
36 #include "utils.h"
37 
38 /*
39  *   In order to be compatible with different android versions,
40  * some gralloc usage is defined here.
41  *   The correct usage should be to refer to the corresponding header file:
42  *   Android 12 and above: #include "hardware/gralloc_rockchip.h"
43  *   Android 11 and below: #include "hardware/gralloc.h"
44  */
45 #define GRALLOC_USAGE_PRIVATE_11                (1ULL << 56)
46 #define RK_GRALLOC_USAGE_WITHIN_4G              GRALLOC_USAGE_PRIVATE_11
47 #define RK_GRALLOC_USAGE_RGA_ACCESS             RK_GRALLOC_USAGE_WITHIN_4G
48 
49 #define LOCAL_FILE_PATH "/data"
50 
51 using namespace android;
52 
main(void)53 int main(void) {
54     int ret = 0;
55     int64_t ts;
56     int src_width, src_height, src_format;
57     int dst_width, dst_height, dst_format;
58     int src_buf_size, dst_buf_size;
59     char *src_buf, *dst_buf;
60     rga_buffer_t src = {};
61     rga_buffer_t dst = {};
62     im_rect src_rect = {};
63     im_rect dst_rect = {};
64     rga_buffer_handle_t src_handle, dst_handle;
65     uint64_t src_gb_flags = 0, dst_gb_flags = 0;
66 
67     src_width = 1280;
68     src_height = 720;
69     src_format = HAL_PIXEL_FORMAT_RGBA_8888;
70 
71     dst_width = 1280;
72     dst_height = 720;
73     dst_format = HAL_PIXEL_FORMAT_RGBA_8888;
74 
75     src_buf_size = src_width * src_height * get_bpp_from_format(src_format);
76     dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
77 
78     /* allocate GraphicBuffer */
79     src_gb_flags |= RK_GRALLOC_USAGE_WITHIN_4G;
80     dst_gb_flags |= RK_GRALLOC_USAGE_WITHIN_4G;
81 
82     sp<GraphicBuffer> src_gb(new GraphicBuffer(src_width, src_height, src_format, 0, src_gb_flags));
83     if (src_gb->initCheck()) {
84         printf("src GraphicBuffer check error : %s\n",strerror(errno));
85         return -1;
86     }
87     sp<GraphicBuffer> dst_gb(new GraphicBuffer(dst_width, dst_height, dst_format, 0, dst_gb_flags));
88     if (dst_gb->initCheck()) {
89         printf("dst GraphicBuffer check error : %s\n",strerror(errno));
90         return -1;
91     }
92 
93     ret = src_gb->lock(0, (void **)&src_buf);
94     if (ret) {
95         printf("lock buffer error : %s\n",strerror(errno));
96         return -1;
97     }
98 
99     ret = read_image_from_file(src_buf, LOCAL_FILE_PATH, src_width, src_height, src_format, 0);
100     if (ret < 0) {
101         printf ("open file %s so memset!\n", "fault");
102         draw_rgba((char *)src_buf, src_width, src_height);
103     }
104 
105     ret = src_gb->unlock();
106 	if (ret) {
107         printf("unlock buffer error : %s\n",strerror(errno));
108         return -1;
109     }
110 
111     ret = dst_gb->lock(0, (void **)&dst_buf);
112     if (ret) {
113         printf("lock buffer error : %s\n",strerror(errno));
114         return -1;
115     }
116 
117     memset(dst_buf, 0x33, dst_buf_size);
118 
119     ret = dst_gb->unlock();
120 	if (ret) {
121         printf("unlock buffer error : %s\n",strerror(errno));
122         return -1;
123     }
124 
125     /*
126      * Import the allocated GraphicBuffer into RGA by calling
127      * importbuffer_GraphicBuffer, and use the returned buffer_handle
128      * to call RGA to process the image.
129      */
130     src_handle = importbuffer_GraphicBuffer(src_gb);
131     dst_handle = importbuffer_GraphicBuffer(dst_gb);
132     if (src_handle == 0 || dst_handle == 0) {
133         printf("import GraphicBuffer error!\n");
134         ret = -1;
135         goto free_buf;
136     }
137 
138     src = wrapbuffer_handle(src_handle, src_width, src_height, src_format);
139     dst = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
140 
141     ret = imcheck(src, dst, src_rect, dst_rect);
142     if (IM_STATUS_NOERROR != ret) {
143         printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
144         goto release_buffer;
145     }
146 
147     ts = get_cur_us();
148 
149     ret = imcopy(src, dst);
150     if (ret == IM_STATUS_SUCCESS) {
151         printf("%s running success! cost %ld us\n", LOG_TAG, get_cur_us() - ts);
152     } else {
153         printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
154         goto release_buffer;
155     }
156 
157     ret = dst_gb->lock(0, (void **)&dst_buf);
158     if (ret) {
159         printf("lock buffer error : %s\n",strerror(errno));
160         return -1;
161     }
162 
163     printf("output [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
164     write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
165 
166     ret = dst_gb->unlock();
167 	if (ret) {
168         printf("unlock buffer error : %s\n",strerror(errno));
169         return -1;
170     }
171 
172 release_buffer:
173     if (src_handle > 0)
174         releasebuffer_handle(src_handle);
175     if (dst_handle > 0)
176         releasebuffer_handle(dst_handle);
177 
178 free_buf:
179     src_gb = NULL;
180     dst_gb = NULL;
181 
182     return 0;
183 }
184