1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-21 GuEe-GUI first version
*/
#include "rga2_internal.h"
#include "rga2_reg.h"
struct rga2_format_info
{
rt_uint8_t hw_format;
rt_uint8_t bytes_per_pixel;
rt_uint8_t rb_swap;
};
static rt_err_t rga2_get_format(rt_uint16_t format,
struct rga2_format_info *info)
{
static const struct rga2_format_info formats[] =
{
[RGA2_FORMAT_RGBA8888] = { 0, 4, 0 },
[RGA2_FORMAT_BGRA8888] = { 0, 4, 1 },
[RGA2_FORMAT_RGBX8888] = { 1, 4, 0 },
[RGA2_FORMAT_BGRX8888] = { 1, 4, 1 },
[RGA2_FORMAT_RGB888] = { 2, 3, 0 },
[RGA2_FORMAT_BGR888] = { 2, 3, 1 },
[RGA2_FORMAT_RGB565] = { 4, 2, 0 },
[RGA2_FORMAT_BGR565] = { 4, 2, 1 },
};
if (format >= sizeof(formats) / sizeof(formats[0]))
{
return -RT_EINVAL;
}
*info = formats[format];
return RT_EOK;
}
static rt_uint32_t rga2_scale_factor(rt_uint16_t source,
rt_uint16_t destination)
{
if (source == destination)
{
return 0;
}
if (source > destination)
{
/* Average down-scaling uses a 16.16 destination/source ratio. */
return ((rt_uint32_t)destination << 16) / source;
}
/* Up-scaling uses a 16.16 source/destination ratio in the high half. */
return (((rt_uint32_t)source << 16) / destination) << 16;
}
static rt_uint32_t rga2_destination_address(const struct rga2_req *req,
rt_uint32_t stride, rt_uint8_t bytes_per_pixel)
{
const struct rga2_rect *rect = &req->dst.rect;
rt_uint32_t left_top = (rt_uint32_t)req->dst.address +
rect->y * stride + rect->x * bytes_per_pixel;
rt_uint32_t right_top = left_top +
(rect->width - 1) * bytes_per_pixel;
rt_uint32_t left_bottom = left_top +
(rect->height - 1) * stride;
rt_uint32_t right_bottom = left_bottom +
(rect->width - 1) * bytes_per_pixel;
rt_uint32_t mirror_x = req->rotate >= RGA2_ROTATE_180;
rt_uint32_t mirror_y = mirror_x;
mirror_x ^= !!(req->mirror & RGA2_MIRROR_X);
mirror_y ^= !!(req->mirror & RGA2_MIRROR_Y);
if (!(req->rotate & 1))
{
return mirror_y ? (mirror_x ? right_bottom : left_bottom) :
(mirror_x ? right_top : left_top);
}
return mirror_y ? (mirror_x ? left_bottom : right_bottom) :
(mirror_x ? left_top : right_top);
}
rt_err_t rockchip_rga2_build_command(const struct rga2_req *req,
rt_uint32_t command[RGA2_COMMAND_WORDS])
{
struct rga2_format_info src_format = { 0 };
struct rga2_format_info dst_format;
rt_uint16_t scale_width;
rt_uint16_t scale_height;
rt_uint32_t src_stride = 0;
rt_uint32_t dst_stride;
rt_uint32_t src_info = 0;
rt_memset(command, 0, RGA2_COMMAND_WORDS * sizeof(*command));
if (rga2_get_format(req->dst.format, &dst_format) != RT_EOK)
{
return -RT_EINVAL;
}
dst_stride = RT_ALIGN(req->dst.stride, 4);
command[RGA2_MODE_CTRL / 4] = req->render_mode;
command[RGA2_DST_INFO / 4] = dst_format.hw_format |
((rt_uint32_t)dst_format.rb_swap << 4);
command[RGA2_DST_BASE0 / 4] = rga2_destination_address(req,
dst_stride, dst_format.bytes_per_pixel);
command[RGA2_DST_VIR_INFO / 4] = dst_stride >> 2;
command[RGA2_DST_ACT_INFO / 4] = (req->dst.rect.width - 1) |
((rt_uint32_t)(req->dst.rect.height - 1) << 16);
if (req->render_mode == RGA2_MODE_COLOR_FILL)
{
command[RGA2_SRC_BG_COLOR / 4] = req->color;
return RT_EOK;
}
if (rga2_get_format(req->src.format, &src_format) != RT_EOK)
{
return -RT_EINVAL;
}
src_stride = RT_ALIGN(req->src.stride, 4);
scale_width = (req->rotate & 1) ? req->dst.rect.height :
req->dst.rect.width;
scale_height = (req->rotate & 1) ? req->dst.rect.width :
req->dst.rect.height;
src_info = src_format.hw_format |
((rt_uint32_t)src_format.rb_swap << 4) |
((rt_uint32_t)req->rotate << 10) |
((rt_uint32_t)(req->mirror & 3) << 12);
if (req->src.rect.width > scale_width)
{
src_info |= 1U << 14;
}
else if (req->src.rect.width < scale_width)
{
src_info |= 2U << 14;
}
if (req->src.rect.height > scale_height)
{
src_info |= 1U << 16;
}
else if (req->src.rect.height < scale_height)
{
src_info |= 2U << 16;
}
command[RGA2_SRC_INFO / 4] = src_info;
command[RGA2_SRC_BASE0 / 4] = (rt_uint32_t)req->src.address +
req->src.rect.y * src_stride +
req->src.rect.x * src_format.bytes_per_pixel;
command[RGA2_SRC_VIR_INFO / 4] = src_stride >> 2;
command[RGA2_SRC_ACT_INFO / 4] = (req->src.rect.width - 1) |
((rt_uint32_t)(req->src.rect.height - 1) << 16);
command[RGA2_SRC_X_FACTOR / 4] = rga2_scale_factor(
req->src.rect.width, scale_width);
command[RGA2_SRC_Y_FACTOR / 4] = rga2_scale_factor(
req->src.rect.height, scale_height);
return RT_EOK;
}
|