xref: /OK3568_Linux_fs/external/linux-rga/samples/alpha_demo/src/rga_alpha_osd_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_alpha_osd_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 "RgaUtils.h"
34 #include "im2d.hpp"
35 #include "dma_alloc.h"
36 #include "utils.h"
37 
38 #define LOCAL_FILE_PATH "/data"
39 
main()40 int main() {
41     int ret = 0;
42     int block_witdh, block_height;
43     int fg_width, fg_height, fg_format;
44     int bg_width, bg_height, bg_format;
45     char *fg_buf, *bg_buf;
46     int fg_dma_fd, bg_dma_fd;
47     int fg_buf_size, bg_buf_size;
48     int block_size, block_count;
49 
50     int usage = 0;
51     rga_buffer_t fg_img, bg_img;
52     im_rect bg_rect;
53     rga_buffer_handle_t fg_handle, bg_handle;
54     im_osd_t osd_config;
55 
56     memset(&fg_img, 0, sizeof(fg_img));
57     memset(&bg_img, 0, sizeof(bg_img));
58     memset(&bg_rect, 0, sizeof(bg_rect));
59     memset(&osd_config, 0, sizeof(osd_config));
60 
61     block_witdh = 64;
62     block_height = 96;
63     block_count = 6;
64 
65     fg_width = block_witdh;
66     fg_height = block_height * block_count;
67     fg_format = RK_FORMAT_RGBA_8888;
68 
69     bg_width = 1280;
70     bg_height = 720;
71     bg_format = RK_FORMAT_RGBA_8888;
72 
73     block_size = block_witdh * block_height * get_bpp_from_format(fg_format);
74     fg_buf_size = fg_width * fg_height * get_bpp_from_format(fg_format);
75     bg_buf_size = bg_width * bg_height * get_bpp_from_format(bg_format);
76 
77     /* Allocate dma_buf from CMA, return dma_fd and virtual address */
78     ret = dma_buf_alloc(DMA_HEAP_UNCACHE_PATH, fg_buf_size, &fg_dma_fd, (void **)&fg_buf);
79     if (ret < 0) {
80         printf("alloc fg CMA buffer failed!\n");
81         return -1;
82     }
83 
84     ret = dma_buf_alloc(DMA_HEAP_UNCACHE_PATH, bg_buf_size, &bg_dma_fd, (void **)&bg_buf);
85     if (ret < 0) {
86         printf("alloc bg CMA buffer failed!\n");
87         dma_buf_free(fg_buf_size, &fg_dma_fd, fg_buf);
88         return -1;
89     }
90 
91     /* fill image data */
92     for (int i = 0; (i < block_count) && ((block_height * i) < fg_height); i++) {
93         if (0 != read_image_from_file(fg_buf + i * block_size, LOCAL_FILE_PATH, block_witdh, block_height, fg_format, 0)) {
94             printf("block image read err\n");
95             memset(fg_buf, 0xaa, fg_buf_size);
96         }
97     }
98     output_buf_data_to_file(fg_buf, fg_format, fg_width, fg_height, 0);
99 
100     if (0 != read_image_from_file(bg_buf, LOCAL_FILE_PATH, bg_width, bg_height, bg_format, 0)) {
101         printf("background image read err\n");
102         memset(bg_buf, 0x66, bg_buf_size);
103     }
104 
105     fg_handle = importbuffer_fd(fg_dma_fd, fg_buf_size);
106     bg_handle = importbuffer_fd(bg_dma_fd, bg_buf_size);
107     if (fg_handle == 0 || bg_handle == 0) {
108         printf("importbuffer failed!\n");
109         goto release_buffer;
110     }
111 
112     fg_img = wrapbuffer_handle(fg_handle, fg_width, fg_height, fg_format);
113     bg_img = wrapbuffer_handle(bg_handle, bg_width, bg_height, bg_format);
114 
115     /*
116      * Overlay multiple blocks on the background image and guide the color of
117      * the blocks according to the external inversion flag.
118         ----     ---------------------    ---------------------
119         |  |     |                   |    | ----              |
120         ----     |                   |    | |  |              |
121         |  |     |                   |    | ----              |
122         ----  +  |                   | => | |  |              |
123         |  |     |                   |    | ----              |
124         ----     |                   |    | |  |              |
125                  |                   |    | ----              |
126                  ---------------------    ---------------------
127 
128      */
129 
130     bg_rect.x = 100;
131     bg_rect.y = 100;
132     bg_rect.width = fg_width;
133     bg_rect.height = fg_height;
134 
135     osd_config.osd_mode = IM_OSD_MODE_STATISTICS | IM_OSD_MODE_AUTO_INVERT;
136 
137     osd_config.block_parm.width_mode = IM_OSD_BLOCK_MODE_NORMAL;
138     osd_config.block_parm.width = block_height;
139     osd_config.block_parm.block_count = block_count;
140     osd_config.block_parm.background_config = IM_OSD_BACKGROUND_DEFAULT_BRIGHT;
141     osd_config.block_parm.direction = IM_OSD_MODE_VERTICAL;
142     osd_config.block_parm.color_mode = IM_OSD_COLOR_PIXEL;
143 
144     osd_config.invert_config.invert_channel = IM_OSD_INVERT_CHANNEL_COLOR;
145     osd_config.invert_config.flags_mode = IM_OSD_FLAGS_EXTERNAL;
146     osd_config.invert_config.invert_flags = 0x000000000000002a;
147     osd_config.invert_config.flags_index = 1;
148     osd_config.invert_config.threash = 40;
149     osd_config.invert_config.invert_mode = IM_OSD_INVERT_USE_SWAP;
150 
151     ret = imcheck(fg_img, bg_img, {}, bg_rect);
152     if (IM_STATUS_NOERROR != ret) {
153         printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
154         return -1;
155     }
156 
157     ret = imosd(fg_img, bg_img, bg_rect, &osd_config);
158     printf("%s .... %s\n", LOG_TAG, imStrError(ret));
159     if (ret != IM_STATUS_SUCCESS)
160         goto release_buffer;
161 
162     write_image_to_file(bg_buf, LOCAL_FILE_PATH, bg_width, bg_height, bg_format, 0);
163 
164 release_buffer:
165     if (fg_handle)
166         releasebuffer_handle(fg_handle);
167     if (bg_handle)
168         releasebuffer_handle(bg_handle);
169 
170     dma_buf_free(fg_buf_size, &fg_dma_fd, fg_buf);
171     dma_buf_free(bg_buf_size, &bg_dma_fd, bg_buf);
172 
173     return ret;
174 }
175