summaryrefslogtreecommitdiff
path: root/components/rtgui/common/framebuffer_driver.c
blob: 78dea306ad38a816e700d58711afd15ab0e96b2a (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <rtgui/rtgui_system.h>
#include <rtgui/driver.h>

#define GET_PIXEL(dst, x, y, type)	\
	(type *)((rt_uint8_t*)((dst)->framebuffer) + (y) * (dst)->pitch + (x) * ((dst)->bits_per_pixel/8))

static void _rgb565_set_pixel(rtgui_color_t *c, int x, int y)
{
	*GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t) = rtgui_color_to_565(*c);
}

static void _rgb565_get_pixel(rtgui_color_t *c, int x, int y)
{
	rt_uint16_t pixel;

	pixel = *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t);

	/* get pixel from color */
	*c = rtgui_color_from_565(pixel);
}

static void _rgb565_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
{
	rt_ubase_t index;
	rt_uint16_t pixel;
	rt_uint16_t *pixel_ptr;

	/* get pixel from color */
	pixel = rtgui_color_to_565(*c);

	/* get pixel pointer in framebuffer */
	pixel_ptr = GET_PIXEL(rtgui_graphic_get_device(), x1, y, rt_uint16_t);
	
	for (index = x1; index < x2; index ++)
	{
		*pixel_ptr = pixel;
		pixel_ptr ++;
	}
}

static void _rgb565_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
{
	rt_uint8_t *dst;
	rt_uint16_t pixel;
	rt_ubase_t index;

	pixel = rtgui_color_to_565(*c);
	dst = GET_PIXEL(rtgui_graphic_get_device(), x, y1, rt_uint8_t);
	for (index = y1; index < y2; index ++)
	{
		*(rt_uint16_t*)dst = pixel;
		dst += rtgui_graphic_get_device()->pitch;
	}
}

static void _rgb565p_set_pixel(rtgui_color_t *c, int x, int y)
{
	*GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t) = rtgui_color_to_565p(*c);
}

static void _rgb565p_get_pixel(rtgui_color_t *c, int x, int y)
{
	rt_uint16_t pixel;

	pixel = *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t);

	/* get pixel from color */
	*c = rtgui_color_from_565p(pixel);
}

static void _rgb565p_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
{
	rt_ubase_t index;
	rt_uint16_t pixel;
	rt_uint16_t *pixel_ptr;

	/* get pixel from color */
	pixel = rtgui_color_to_565p(*c);

	/* get pixel pointer in framebuffer */
	pixel_ptr = GET_PIXEL(rtgui_graphic_get_device(), x1, y, rt_uint16_t);
	
	for (index = x1; index < x2; index ++)
	{
		*pixel_ptr = pixel;
		pixel_ptr ++;
	}
}

static void _rgb565p_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
{
	rt_uint8_t *dst;
	rt_uint16_t pixel;
	rt_ubase_t index;

	pixel = rtgui_color_to_565p(*c);
	dst = GET_PIXEL(rtgui_graphic_get_device(), x, y1, rt_uint8_t);
	for (index = y1; index < y2; index ++)
	{
		*(rt_uint16_t*)dst = pixel;
		dst += rtgui_graphic_get_device()->pitch;
	}
}

/* draw raw hline */
static void framebuffer_draw_raw_hline(rt_uint8_t *pixels, int x1, int x2, int y)
{
	rt_uint8_t *dst;

	dst = GET_PIXEL(rtgui_graphic_get_device(), x1, y, rt_uint8_t);
	rt_memcpy(dst, pixels, (x2 - x1) * (rtgui_graphic_get_device()->bits_per_pixel/8));
}

const struct rtgui_graphic_driver_ops _framebuffer_rgb565_ops = 
{
	_rgb565_set_pixel,
	_rgb565_get_pixel,
	_rgb565_draw_hline,
	_rgb565_draw_vline,
	framebuffer_draw_raw_hline,
};

const struct rtgui_graphic_driver_ops _framebuffer_rgb565p_ops = 
{
	_rgb565p_set_pixel,
	_rgb565p_get_pixel,
	_rgb565p_draw_hline,
	_rgb565p_draw_vline,
	framebuffer_draw_raw_hline,
};

#define FRAMEBUFFER	(rtgui_graphic_get_device()->framebuffer)
#define MONO_PIXEL(framebuffer, x, y) \
	((rt_uint8_t**)(framebuffer))[y/8][x]

static void _mono_set_pixel(rtgui_color_t *c, int x, int y)
{
	if (*c == white)
		MONO_PIXEL(FRAMEBUFFER, x, y) &= ~(1 << (y%8));
	else
		MONO_PIXEL(FRAMEBUFFER, x, y) |= (1 << (y%8));
}

static void _mono_get_pixel(rtgui_color_t *c, int x, int y)
{
	if (MONO_PIXEL(FRAMEBUFFER, x, y) & (1 << (y%8)))
		*c = black;
	else
		*c = white;
}

static void _mono_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
{
	rt_ubase_t index;
	
	if (*c == white)
		for (index = x1; index < x2; index ++)
		{
			MONO_PIXEL(FRAMEBUFFER, index, y) &= ~(1 << (y%8));
		}
	else
		for (index = x1; index < x2; index ++)
		{
			MONO_PIXEL(FRAMEBUFFER, index, y) |= (1 << (y%8));
		}
}

static void _mono_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
{
	rt_ubase_t index;
	
	if (*c == white)
		for (index = y1; index < y2; index ++)
		{
			MONO_PIXEL(FRAMEBUFFER, x, index) &= ~(1 << (index%8));
		}
	else
		for (index = y1; index < y2; index ++)
		{
			MONO_PIXEL(FRAMEBUFFER, x, index) |=  (1 << (index%8));
		}
}

/* draw raw hline */
static void _mono_draw_raw_hline(rt_uint8_t *pixels, int x1, int x2, int y)
{
	rt_ubase_t index;

	for (index = x1; index < x2; index ++)
	{
		if (pixels[index/8] && (1 << (index % 8)))
			MONO_PIXEL(FRAMEBUFFER, index, y) |=  (1 << (y%8));
		else
			MONO_PIXEL(FRAMEBUFFER, index, y) &= ~(1 << (y%8));
	}
}

const struct rtgui_graphic_driver_ops _framebuffer_mono_ops = 
{
	_mono_set_pixel,
	_mono_get_pixel,
	_mono_draw_hline,
	_mono_draw_vline,
	_mono_draw_raw_hline,
};

const struct rtgui_graphic_driver_ops *rtgui_framebuffer_get_ops(int pixel_format)
{
	switch (pixel_format)
	{
	case RTGRAPHIC_PIXEL_FORMAT_MONO:
		return &_framebuffer_mono_ops;
	case RTGRAPHIC_PIXEL_FORMAT_GRAY4:
		break;
	case RTGRAPHIC_PIXEL_FORMAT_GRAY16:
		break;
	case RTGRAPHIC_PIXEL_FORMAT_RGB565:
		return &_framebuffer_rgb565_ops;
	case RTGRAPHIC_PIXEL_FORMAT_RGB565P:
		return &_framebuffer_rgb565p_ops;
	}

	return RT_NULL;
}