xref: /OK3568_Linux_fs/external/linux-rga/samples/copy_demo/src/rga_copy_tile8x8_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_copy_tile8x8_demo"
22 
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/mman.h>
32 #include <math.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <unistd.h>
36 #include <linux/stddef.h>
37 
38 #include "RgaUtils.h"
39 #include "im2d.hpp"
40 #include "utils.h"
41 
42 #define LOCAL_FILE_PATH "/data"
43 
main()44 int main() {
45     int ret = 0;
46     int src_width, src_height, src_format;
47     int dst_width, dst_height, dst_format;
48     char *src_buf, *dst_buf, *tile8_buf;
49     int src_buf_size, dst_buf_size;
50 
51     rga_buffer_t src_img, dst_img, tile8_img;
52     rga_buffer_handle_t src_handle, dst_handle, tile8_handle;
53 
54     memset(&src_img, 0, sizeof(src_img));
55     memset(&dst_img, 0, sizeof(dst_img));
56 
57     src_width = 1280;
58     src_height = 720;
59     src_format = RK_FORMAT_YCbCr_420_SP;
60 
61     dst_width = 1280;
62     dst_height = 720;
63     dst_format = RK_FORMAT_YCbCr_420_SP;
64 
65     src_buf_size = src_width * src_height * get_bpp_from_format(src_format);
66     dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
67 
68     src_buf = (char *)malloc(src_buf_size);
69     dst_buf = (char *)malloc(dst_buf_size);
70     tile8_buf = (char *)malloc(dst_buf_size);
71 
72     /* fill image data */
73     if (0 != read_image_from_file(src_buf, LOCAL_FILE_PATH, src_width, src_height, src_format, 0)) {
74         printf("src image read err\n");
75         draw_YUV420(src_buf, src_width, src_height);
76     }
77     memset(dst_buf, 0x80, dst_buf_size);
78     memset(tile8_buf, 0x80, dst_buf_size);
79 
80     src_handle = importbuffer_virtualaddr(src_buf, src_buf_size);
81     dst_handle = importbuffer_virtualaddr(dst_buf, dst_buf_size);
82     tile8_handle = importbuffer_virtualaddr(tile8_buf, dst_buf_size);
83     if (src_handle == 0 || dst_handle == 0) {
84         printf("importbuffer failed!\n");
85         goto release_buffer;
86     }
87 
88 
89     src_img = wrapbuffer_handle(src_handle, src_width, src_height, src_format);
90     dst_img = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
91     tile8_img = wrapbuffer_handle(tile8_handle, dst_width, dst_height, dst_format);
92 
93     /*
94      * Copy the src(raster) image to the tile8(tile8x8) image.
95         --------------        --------------
96         |            |        |            |
97         |  src_image |   =>   | tile8_image|
98         |            |        |            |
99         --------------        --------------
100      */
101 
102     src_img.rd_mode = IM_RASTER_MODE;
103     tile8_img.rd_mode = IM_TILE_MODE;
104 
105     ret = imcopy(src_img, tile8_img);
106     if (ret == IM_STATUS_SUCCESS) {
107         printf("%s raster -> tile8x8 running success!\n", LOG_TAG);
108     } else {
109         printf("%s raster -> tile8x8 running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
110         goto release_buffer;
111     }
112 
113 	printf("tile8x8 [0x%x, 0x%x, 0x%x, 0x%x]\n", tile8_buf[0], tile8_buf[1], tile8_buf[2], tile8_buf[3]);
114     write_image_to_file(tile8_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
115 
116     /*
117      * Copy the tile8(tile8x8) image to the dst(raster) image.
118         --------------        --------------
119         |            |        |            |
120         | tile8_image|   =>   |  dst_image |
121         |            |        |            |
122         --------------        --------------
123      */
124 
125     tile8_img.rd_mode = IM_TILE_MODE;
126     dst_img.rd_mode = IM_RASTER_MODE;
127 
128     ret = imcopy(tile8_img, dst_img);
129     if (ret == IM_STATUS_SUCCESS) {
130         printf("%s tile8x8 -> raster running success!\n", LOG_TAG);
131     } else {
132         printf("%s tile8x8 -> raster running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
133         goto release_buffer;
134     }
135 
136 	printf("raster [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
137     write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 1);
138 
139 release_buffer:
140     if (src_handle)
141         releasebuffer_handle(src_handle);
142     if (dst_handle)
143         releasebuffer_handle(dst_handle);
144     if (tile8_handle)
145         releasebuffer_handle(tile8_handle);
146 
147     if (src_buf)
148         free(src_buf);
149     if (dst_buf)
150         free(dst_buf);
151     if (tile8_buf)
152         free(tile8_buf);
153 
154     return ret;
155 }
156