1 /*
2 * Copyright (C) 2016 Rockchip Electronics Co., Ltd.
3 * Authors:
4 * Zhiqin Wei <wzq@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 #ifndef _rk_drm_rga_
20 #define _rk_drm_rga_
21
22 #include <stdint.h>
23 #include <errno.h>
24 #include <sys/cdefs.h>
25
26 #include "rga.h"
27
28 #ifdef ANDROID
29 #define DRMRGA_HARDWARE_MODULE_ID "librga"
30
31 #include <hardware/gralloc.h>
32 #include <hardware/hardware.h>
33 #include <system/graphics.h>
34 #include <cutils/native_handle.h>
35
36 #ifdef ANDROID_12
37 #include <hardware/hardware_rockchip.h>
38 #endif
39
40 #endif
41
42 #define RGA_BLIT_SYNC 0x5017
43 #define RGA_BLIT_ASYNC 0x5018
44
45 #ifndef ANDROID /* LINUX */
46 /* flip source image horizontally (around the vertical axis) */
47 #define HAL_TRANSFORM_FLIP_H 0x01
48 /* flip source image vertically (around the horizontal axis)*/
49 #define HAL_TRANSFORM_FLIP_V 0x02
50 /* rotate source image 90 degrees clockwise */
51 #define HAL_TRANSFORM_ROT_90 0x04
52 /* rotate source image 180 degrees */
53 #define HAL_TRANSFORM_ROT_180 0x03
54 /* rotate source image 270 degrees clockwise */
55 #define HAL_TRANSFORM_ROT_270 0x07
56 #endif
57
58 #define HAL_TRANSFORM_FLIP_H_V 0x08
59
60 /*****************************************************************************/
61
62 /* for compatibility */
63 #define DRM_RGA_MODULE_API_VERSION HWC_MODULE_API_VERSION_0_1
64 #define DRM_RGA_DEVICE_API_VERSION HWC_DEVICE_API_VERSION_0_1
65 #define DRM_RGA_API_VERSION HWC_DEVICE_API_VERSION
66
67 #define DRM_RGA_TRANSFORM_ROT_MASK 0x0000000F
68 #define DRM_RGA_TRANSFORM_ROT_0 0x00000000
69 #define DRM_RGA_TRANSFORM_ROT_90 HAL_TRANSFORM_ROT_90
70 #define DRM_RGA_TRANSFORM_ROT_180 HAL_TRANSFORM_ROT_180
71 #define DRM_RGA_TRANSFORM_ROT_270 HAL_TRANSFORM_ROT_270
72
73 #define DRM_RGA_TRANSFORM_FLIP_MASK 0x00000003
74 #define DRM_RGA_TRANSFORM_FLIP_H HAL_TRANSFORM_FLIP_H
75 #define DRM_RGA_TRANSFORM_FLIP_V HAL_TRANSFORM_FLIP_V
76
77 enum {
78 AWIDTH = 0,
79 AHEIGHT,
80 ASTRIDE,
81 AFORMAT,
82 ASIZE,
83 ATYPE,
84 };
85 /*****************************************************************************/
86
87 #ifndef ANDROID /* LINUX */
88 /* memory type definitions. */
89 enum drm_rockchip_gem_mem_type {
90 /* Physically Continuous memory and used as default. */
91 ROCKCHIP_BO_CONTIG = 1 << 0,
92 /* cachable mapping. */
93 ROCKCHIP_BO_CACHABLE = 1 << 1,
94 /* write-combine mapping. */
95 ROCKCHIP_BO_WC = 1 << 2,
96 ROCKCHIP_BO_SECURE = 1 << 3,
97 ROCKCHIP_BO_MASK = ROCKCHIP_BO_CONTIG | ROCKCHIP_BO_CACHABLE |
98 ROCKCHIP_BO_WC | ROCKCHIP_BO_SECURE
99 };
100
101 typedef struct bo {
102 int fd;
103 void *ptr;
104 size_t size;
105 size_t offset;
106 size_t pitch;
107 unsigned handle;
108 } bo_t;
109 #endif
110
111 /*
112 @value size: user not need care about.For avoid read/write out of memory
113 */
114 typedef struct rga_rect {
115 int xoffset;
116 int yoffset;
117 int width;
118 int height;
119 int wstride;
120 int hstride;
121 int format;
122 int size;
123 } rga_rect_t;
124
125 typedef struct rga_nn {
126 int nn_flag;
127 int scale_r;
128 int scale_g;
129 int scale_b;
130 int offset_r;
131 int offset_g;
132 int offset_b;
133 } rga_nn_t;
134
135 typedef struct rga_dither {
136 int enable;
137 int mode;
138 int lut0_l;
139 int lut0_h;
140 int lut1_l;
141 int lut1_h;
142 } rga_dither_t;
143
144 struct rga_mosaic_info {
145 uint8_t enable;
146 uint8_t mode;
147 };
148
149 struct rga_pre_intr_info {
150 uint8_t enable;
151
152 uint8_t read_intr_en;
153 uint8_t write_intr_en;
154 uint8_t read_hold_en;
155 uint32_t read_threshold;
156 uint32_t write_start;
157 uint32_t write_step;
158 };
159
160 /* MAX(min, (max - channel_value)) */
161 struct rga_osd_invert_factor {
162 uint8_t alpha_max;
163 uint8_t alpha_min;
164 uint8_t yg_max;
165 uint8_t yg_min;
166 uint8_t crb_max;
167 uint8_t crb_min;
168 };
169
170 struct rga_color {
171 union {
172 struct {
173 uint8_t red;
174 uint8_t green;
175 uint8_t blue;
176 uint8_t alpha;
177 };
178 uint32_t value;
179 };
180 };
181
182 struct rga_osd_bpp2 {
183 uint8_t ac_swap; // ac swap flag
184 // 0: CA
185 // 1: AC
186 uint8_t endian_swap; // rgba2bpp endian swap
187 // 0: Big endian
188 // 1: Little endian
189 struct rga_color color0;
190 struct rga_color color1;
191 };
192
193 struct rga_osd_mode_ctrl {
194 uint8_t mode; // OSD cal mode:
195 // 0b'1: statistics mode
196 // 1b'1: auto inversion overlap mode
197 uint8_t direction_mode; // horizontal or vertical
198 // 0: horizontal
199 // 1: vertical
200 uint8_t width_mode; // using @fix_width or LUT width
201 // 0: fix width
202 // 1: LUT width
203 uint16_t block_fix_width; // OSD block fixed width
204 // real width = (fix_width + 1) * 2
205 uint8_t block_num; // OSD block num
206 uint16_t flags_index; // auto invert flags index
207
208 /* invertion config */
209 uint8_t color_mode; // selete color
210 // 0: src1 color
211 // 1: config data color
212 uint8_t invert_flags_mode; // invert flag selete
213 // 0: use RAM flag
214 // 1: usr last result
215 uint8_t default_color_sel; // default color mode
216 // 0: default is bright
217 // 1: default is dark
218 uint8_t invert_enable; // invert channel enable
219 // 1 << 0: aplha enable
220 // 1 << 1: Y/G disable
221 // 1 << 2: C/RB disable
222 uint8_t invert_mode; // invert cal mode
223 // 0: normal(max-data)
224 // 1: swap
225 uint8_t invert_thresh; // if luma > thresh, osd_flag to be 1
226 uint8_t unfix_index; // OSD width config index
227 };
228
229 struct rga_osd_info {
230 uint8_t enable;
231
232 struct rga_osd_mode_ctrl mode_ctrl;
233 struct rga_osd_invert_factor cal_factor;
234 struct rga_osd_bpp2 bpp2_info;
235
236 union {
237 struct {
238 uint32_t last_flags1;
239 uint32_t last_flags0;
240 };
241 uint64_t last_flags;
242 };
243
244 union {
245 struct {
246 uint32_t cur_flags1;
247 uint32_t cur_flags0;
248 };
249 uint64_t cur_flags;
250 };
251 };
252
253 /*
254 @value fd: use fd to share memory, it can be ion shard fd,and dma fd.
255 @value virAddr:userspace address
256 @value phyAddr:use phy address
257 @value hnd: use buffer_handle_t
258 */
259 typedef struct rga_info {
260 int fd;
261 void *virAddr;
262 void *phyAddr;
263 #ifndef ANDROID /* LINUX */
264 unsigned hnd;
265 #else /* Android */
266 buffer_handle_t hnd;
267 #endif
268 int format;
269 rga_rect_t rect;
270 unsigned int blend;
271 int bufferSize;
272 int rotation;
273 int color;
274 int testLog;
275 int mmuFlag;
276 int colorkey_en;
277 int colorkey_mode;
278 int colorkey_max;
279 int colorkey_min;
280 int scale_mode;
281 int color_space_mode;
282 int sync_mode;
283 rga_nn_t nn;
284 rga_dither_t dither;
285 int rop_code;
286 int rd_mode;
287 unsigned short is_10b_compact;
288 unsigned short is_10b_endian;
289
290 int in_fence_fd;
291 int out_fence_fd;
292
293 int core;
294 int priority;
295
296 unsigned short enable;
297
298 int handle;
299
300 struct rga_mosaic_info mosaic_info;
301
302 struct rga_osd_info osd_info;
303
304 struct rga_pre_intr_info pre_intr;
305
306 int mpi_mode;
307
308 union {
309 int ctx_id;
310 int job_handle;
311 };
312
313 char reserve[402];
314 } rga_info_t;
315
316
317 typedef struct drm_rga {
318 rga_rect_t src;
319 rga_rect_t dst;
320 } drm_rga_t;
321
322 /*
323 @fun rga_set_rect:For use to set the rects esayly
324
325 @param rect:The rect user want to set,like setting the src rect:
326 drm_rga_t rects;
327 rga_set_rect(rects.src,0,0,1920,1080,1920,NV12);
328 mean to set the src rect to the value.
329 */
rga_set_rect(rga_rect_t * rect,int x,int y,int w,int h,int sw,int sh,int f)330 static inline int rga_set_rect(rga_rect_t *rect,
331 int x, int y, int w, int h, int sw, int sh, int f) {
332 if (!rect)
333 return -EINVAL;
334
335 rect->xoffset = x;
336 rect->yoffset = y;
337 rect->width = w;
338 rect->height = h;
339 rect->wstride = sw;
340 rect->hstride = sh;
341 rect->format = f;
342
343 return 0;
344 }
345
346 #ifndef ANDROID /* LINUX */
rga_set_rotation(rga_info_t * info,int angle)347 static inline void rga_set_rotation(rga_info_t *info, int angle) {
348 if (angle == 90)
349 info->rotation = HAL_TRANSFORM_ROT_90;
350 else if (angle == 180)
351 info->rotation = HAL_TRANSFORM_ROT_180;
352 else if (angle == 270)
353 info->rotation = HAL_TRANSFORM_ROT_270;
354 }
355 #endif
356 /*****************************************************************************/
357
358 #endif
359