1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #ifndef FB_H
6 #define FB_H
7 /**
8  *  @addtogroup aos_fbdev fbdev
9  *  This is the brief description of the component.
10  *
11  *  This is the detailed description, and it's optional.
12  *  @{
13  */
14 
15 #include <fb_define.h>
16 #include <ulog/ulog.h>
17 #include <aos/kernel.h>
18 
19 #ifdef CONFIG_BACKLIGHT_MODE_ENABLE
20 #include "backlight.h"
21 #endif
22 
23 #include "aos/vfs.h"
24 
25 /* Here is Macro and struct definition*/
26 #define FB_MAX 3
27 #define FB_STR "FB"
28 
29 #define FB_MAX_NUM  16
30 #define FB_NAME_LEN 16
31 #define fb_node_path       "/dev/fb"
32 
33 #define FBDEV_STEP_DEBUG printf("[%s][%d]excute to here\n", __func__, __LINE__);
34 
35 #define FBINFO_STATE_RUNNING    0
36 #define FBINFO_STATE_SUSPENDED    1
37 
38 #define PAGE_SIZE 4096
39 
40 #define fb_memcpy_fromfb memcpy
41 #define fb_memcpy_tofb memcpy
42 
43 #ifndef unlikely
44 #if defined(__CC_ARM)
45 #define unlikely(x) __builtin_expect(!!(x), 0)
46 #elif defined(__ICCARM__)
47 #define unlikely(x) (x)
48 #elif defined(__GNUC__)
49 #define unlikely(x) __builtin_expect(!!(x), 0)
50 #else
51 #define unlikely(x) (x)
52 #endif
53 #endif
54 
55 /* FBINFO_* = fb_info.flags bit flags */
56 #define FBINFO_DEFAULT        0
57 #define FBINFO_HWACCEL_DISABLED    0x0002
58 
59 #define FBINFO_HWACCEL_NONE        0x0000
60 #define FBINFO_HWACCEL_COPYAREA        0x0100 /* required */
61 #define FBINFO_HWACCEL_FILLRECT        0x0200 /* required */
62 #define FBINFO_HWACCEL_IMAGEBLIT    0x0400 /* required */
63 #define FBINFO_HWACCEL_ROTATE        0x0800 /* optional */
64 #define FBINFO_HWACCEL_XPAN        0x1000 /* optional */
65 #define FBINFO_HWACCEL_YPAN        0x2000 /* optional */
66 #define FBINFO_HWACCEL_YWRAP        0x4000 /* optional */
67 
68 struct _fb_ops_t;
69 #ifdef CONFIG_BACKLIGHT_MODE_ENABLE
70 struct _bl_device_t;
71 #endif
72 struct _fb_info_t;
73 
74 typedef struct _fb_videomode_t {
75     const char *name;    /* optional */
76     uint32_t refresh;        /* optional */
77     uint32_t xres;
78     uint32_t yres;
79     uint32_t pixclock;
80     uint32_t left_margin;
81     uint32_t right_margin;
82     uint32_t upper_margin;
83     uint32_t lower_margin;
84     uint32_t hsync_len;
85     uint32_t vsync_len;
86     uint32_t sync;
87     uint32_t vmode;
88     uint32_t flag;
89 } fb_videomode_t;
90 
91 struct fb_pixmap {
92     uint8_t  *addr;        /* pointer to memory            */
93     uint32_t size;        /* size of buffer in bytes        */
94     uint32_t offset;        /* current offset to buffer        */
95     uint32_t buf_align;        /* byte alignment of each bitmap    */
96     uint32_t scan_align;        /* alignment per scanline        */
97     uint32_t access_align;    /* alignment per read/write (bits)    */
98     uint32_t flags;        /* see FB_PIXMAP_*            */
99     uint32_t blit_x;             /* supported bit block dimensions (1-32)*/
100     uint32_t blit_y;             /* Format: blit_x = 1 << (width - 1)    */
101                             /*         blit_y = 1 << (height - 1)   */
102                             /* if 0, will be set to 0xffffffff (all)*/
103     /* access methods */
104     void (*writeio)(struct _fb_info_t *info, void *dst, void *src, unsigned int size);
105     void (*readio) (struct _fb_info_t *info, void *dst, void *src, unsigned int size);
106 };
107 
108 typedef struct _fb_info_t {
109     char path[FB_NAME_LEN];
110     int ref;
111     int node;
112     int flags;
113 
114     aos_mutex_t lock;            /* Lock for open/release/ioctl funcs */
115     aos_mutex_t mm_lock;        /* Lock for fb_mmap and smem_* fields */
116     fb_var_screeninfo_t var;    /* Current var */
117     fb_fix_screeninfo_t fix;    /* Current fix */
118     struct fb_pixmap pixmap;    /* Image hardware mapper */
119     struct fb_pixmap sprite;    /* Cursor hardware mapper */
120     // struct fb_cmap cmap;        /* Current cmap */
121     uint8_t *prefb;                /* Pre-frame buffer from user space*/
122     void *par;                    /* From here on everything is device dependent */
123 
124 #ifdef CONFIG_BACKLIGHT_MODE_ENABLE
125     struct _bl_device_t *bl_dev;        /*backlight device*/
126 
127     uint8_t bl_curve[FB_BACKLIGHT_LEVELS];
128 #endif
129 
130     uint32_t state;            /* Hardware state i.e suspend */
131     const struct _fb_ops_t *fbops;
132 
133     union {
134         uint8_t *screen_base;    /* Virtual address */
135         uint8_t *screen_buffer;
136     };
137     unsigned long screen_size;    /* Amount of ioremapped VRAM or 0 */
138 } fb_info_t;
139 
140 typedef struct _fb_ops_t {
141     /* open/release and usage marking */
142     int (*fb_open)(fb_info_t *info, int user);
143     int (*fb_release)(fb_info_t *info, int user);
144 
145     /* For framebuffers with strange non linear layouts or that do not
146      * work with normal memory mapped access
147      */
148     ssize_t (*fb_read)(fb_info_t *info, char *buf,
149                size_t count);
150     ssize_t (*fb_write)(fb_info_t *info, const char *buf,
151                 size_t count);
152 
153     /* blank display */
154     int (*fb_blank)(int blank, fb_info_t *info);
155 
156     /* pan display */
157     int (*fb_pan_display)(fb_var_screeninfo_t *var, fb_info_t *info);
158 
159     /* set the video mode according to info->var */
160     int (*fb_set_par)(struct fb_info *info);
161 
162     /* Rotates the display */
163     void (*fb_rotate)(fb_info_t *info, int angle);
164 
165     /* wait for blit idle, optional */
166     int (*fb_sync)(fb_info_t *info);
167 
168     /* perform fb specific ioctl (optional) */
169     int (*fb_ioctl)(fb_info_t *info, unsigned int cmd,
170             unsigned long arg);
171 
172     /* perform fb specific mmap */
173     void *(*fb_mmap)(fb_info_t *info, size_t size);
174 
175 #ifdef CONFIG_HW_ACCELERATION
176     /* Draws a rectangle */
177     void (*fb_fillrect) (fb_info_t *info, const fb_fillrect_t *rect);
178     /* Copy data from area to another */
179     void (*fb_copyarea) (fb_info_t *info, const fb_copyarea_t *region);
180     /* Draws a image to the display */
181     void (*fb_imageblit) (fb_info_t *info, const fb_image_t *image);
182 #endif
183 
184     /* teardown any resources to do with this framebuffer */
185     void (*fb_destroy)(fb_info_t *info);
186 } fb_ops_t;
187 
188 /* Here is API and callback definition*/
189 
lock_fb_info(fb_info_t * info)190 static inline void lock_fb_info(fb_info_t *info)
191 {
192     if (aos_mutex_lock(&info->lock, AOS_WAIT_FOREVER) != 0) {
193         LOGE(FB_STR, "Failed to lock mutex (%s).", __func__);
194         return;
195     }
196 }
197 
unlock_fb_info(fb_info_t * info)198 static inline void unlock_fb_info(fb_info_t *info)
199 {
200     aos_mutex_unlock(&info->lock);
201 }
202 
file_inode(const file_t * file)203 static inline inode_t *file_inode(const file_t *file)
204 {
205     if (!file)
206         return NULL;
207 
208     return file->node;
209 }
210 
211 int fb_init(void);
212 fb_info_t *framebuffer_alloc(size_t size);
213 int register_framebuffer(fb_info_t *info);
214 void unregister_framebuffer(fb_info_t *info);
215 void framebuffer_release(fb_info_t *info);
216 /** @} */
217 #endif
218 
219