xref: /linux/drivers/video/fbdev/core/bitblit.c (revision 06fccf840640d8f6b0e08a1756f8d2f0435b4cbe)
1 /*
2  *  linux/drivers/video/console/bitblit.c -- BitBlitting Operation
3  *
4  *  Originally from the 'accel_*' routines in drivers/video/console/fbcon.c
5  *
6  *      Copyright (C) 2004 Antonino Daplas <adaplas @pol.net>
7  *
8  *  This file is subject to the terms and conditions of the GNU General Public
9  *  License.  See the file COPYING in the main directory of this archive for
10  *  more details.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/fb.h>
17 #include <linux/vt_kern.h>
18 #include <linux/console.h>
19 #include <asm/types.h>
20 #include "fbcon.h"
21 #include "fbcon_rotate.h"
22 
23 /*
24  * Accelerated handlers.
25  */
26 static void update_attr(u8 *dst, const u8 *src, int attribute, struct vc_data *vc)
27 {
28 	int i, offset = (vc->vc_font.height < 10) ? 1 : 2;
29 	int width = DIV_ROUND_UP(vc->vc_font.width, 8);
30 	unsigned int cellsize = vc->vc_font.height * width;
31 	u8 c;
32 
33 	offset = cellsize - (offset * width);
34 	for (i = 0; i < cellsize; i++) {
35 		c = src[i];
36 		if (attribute & FBCON_ATTRIBUTE_UNDERLINE && i >= offset)
37 			c = 0xff;
38 		if (attribute & FBCON_ATTRIBUTE_BOLD)
39 			c |= c >> 1;
40 		if (attribute & FBCON_ATTRIBUTE_REVERSE)
41 			c = ~c;
42 		dst[i] = c;
43 	}
44 }
45 
46 static void bit_bmove(struct vc_data *vc, struct fb_info *info, int sy,
47 		      int sx, int dy, int dx, int height, int width)
48 {
49 	struct fb_copyarea area;
50 
51 	area.sx = sx * vc->vc_font.width;
52 	area.sy = sy * vc->vc_font.height;
53 	area.dx = dx * vc->vc_font.width;
54 	area.dy = dy * vc->vc_font.height;
55 	area.height = height * vc->vc_font.height;
56 	area.width = width * vc->vc_font.width;
57 
58 	info->fbops->fb_copyarea(info, &area);
59 }
60 
61 static void bit_clear(struct vc_data *vc, struct fb_info *info, int sy,
62 		      int sx, int height, int width, int fg, int bg)
63 {
64 	struct fb_fillrect region;
65 
66 	region.color = bg;
67 	region.dx = sx * vc->vc_font.width;
68 	region.dy = sy * vc->vc_font.height;
69 	region.width = width * vc->vc_font.width;
70 	region.height = height * vc->vc_font.height;
71 	region.rop = ROP_COPY;
72 
73 	info->fbops->fb_fillrect(info, &region);
74 }
75 
76 static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,
77 				     const u16 *s, u32 attr, u32 cnt,
78 				     u32 d_pitch, u32 s_pitch, u32 cellsize,
79 				     struct fb_image *image, u8 *buf, u8 *dst)
80 {
81 	u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
82 	unsigned int charcnt = vc->vc_font.charcount;
83 	u32 idx = vc->vc_font.width >> 3;
84 	const u8 *src;
85 
86 	while (cnt--) {
87 		u16 ch = scr_readw(s++) & charmask;
88 
89 		if (ch >= charcnt)
90 			ch = 0;
91 		src = vc->vc_font.data + (unsigned int)ch * cellsize;
92 
93 		if (attr) {
94 			update_attr(buf, src, attr, vc);
95 			src = buf;
96 		}
97 
98 		if (likely(idx == 1))
99 			__fb_pad_aligned_buffer(dst, d_pitch, src, idx,
100 						image->height);
101 		else
102 			fb_pad_aligned_buffer(dst, d_pitch, src, idx,
103 					      image->height);
104 
105 		dst += s_pitch;
106 	}
107 
108 	info->fbops->fb_imageblit(info, image);
109 }
110 
111 static inline void bit_putcs_unaligned(struct vc_data *vc,
112 				       struct fb_info *info, const u16 *s,
113 				       u32 attr, u32 cnt, u32 d_pitch,
114 				       u32 s_pitch, u32 cellsize,
115 				       struct fb_image *image, u8 *buf,
116 				       u8 *dst)
117 {
118 	u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
119 	unsigned int charcnt = vc->vc_font.charcount;
120 	u32 shift_low = 0, mod = vc->vc_font.width % 8;
121 	u32 shift_high = 8;
122 	u32 idx = vc->vc_font.width >> 3;
123 	const u8 *src;
124 
125 	while (cnt--) {
126 		u16 ch = scr_readw(s++) & charmask;
127 
128 		if (ch >= charcnt)
129 			ch = 0;
130 		src = vc->vc_font.data + (unsigned int)ch * cellsize;
131 
132 		if (attr) {
133 			update_attr(buf, src, attr, vc);
134 			src = buf;
135 		}
136 
137 		fb_pad_unaligned_buffer(dst, d_pitch, src, idx,
138 					image->height, shift_high,
139 					shift_low, mod);
140 		shift_low += mod;
141 		dst += (shift_low >= 8) ? s_pitch : s_pitch - 1;
142 		shift_low &= 7;
143 		shift_high = 8 - shift_low;
144 	}
145 
146 	info->fbops->fb_imageblit(info, image);
147 
148 }
149 
150 static void bit_putcs(struct vc_data *vc, struct fb_info *info,
151 		      const unsigned short *s, int count, int yy, int xx,
152 		      int fg, int bg)
153 {
154 	struct fbcon_par *par = info->fbcon_par;
155 	struct fb_image image;
156 	u32 vyres = GETVYRES(par->p, info);
157 	u32 width = DIV_ROUND_UP(vc->vc_font.width, 8);
158 	u32 cellsize = width * vc->vc_font.height;
159 	u32 maxcnt = info->pixmap.size/cellsize;
160 	u32 scan_align = info->pixmap.scan_align - 1;
161 	u32 buf_align = info->pixmap.buf_align - 1;
162 	u32 mod = vc->vc_font.width % 8, cnt, pitch, size;
163 	u32 attribute = get_attribute(info, scr_readw(s));
164 	u8 *dst, *buf = NULL;
165 
166 	image.fg_color = fg;
167 	image.bg_color = bg;
168 	image.dx = xx * vc->vc_font.width;
169 	image.dy = yy * vc->vc_font.height;
170 	image.height = vc->vc_font.height;
171 	image.depth = 1;
172 
173 	if (image.dy >= vyres)
174 		return;
175 
176 	image.height = min(image.height, vyres - image.dy);
177 
178 	if (attribute) {
179 		buf = kmalloc(cellsize, GFP_ATOMIC);
180 		if (!buf)
181 			return;
182 	}
183 
184 	while (count) {
185 		if (count > maxcnt)
186 			cnt = maxcnt;
187 		else
188 			cnt = count;
189 
190 		image.width = vc->vc_font.width * cnt;
191 
192 		if (image.dx >= info->var.xres)
193 			break;
194 
195 		if (image.dx + image.width > info->var.xres) {
196 			image.width = info->var.xres - image.dx;
197 			cnt = image.width / vc->vc_font.width;
198 			if (cnt == 0)
199 				break;
200 			image.width = cnt * vc->vc_font.width;
201 		}
202 
203 		pitch = DIV_ROUND_UP(image.width, 8) + scan_align;
204 		pitch &= ~scan_align;
205 		size = pitch * image.height + buf_align;
206 		size &= ~buf_align;
207 		dst = fb_get_buffer_offset(info, &info->pixmap, size);
208 		image.data = dst;
209 
210 		if (!mod)
211 			bit_putcs_aligned(vc, info, s, attribute, cnt, pitch,
212 					  width, cellsize, &image, buf, dst);
213 		else
214 			bit_putcs_unaligned(vc, info, s, attribute, cnt,
215 					    pitch, width, cellsize, &image,
216 					    buf, dst);
217 
218 		image.dx += cnt * vc->vc_font.width;
219 		count -= cnt;
220 		s += cnt;
221 	}
222 
223 	/* buf is always NULL except when in monochrome mode, so in this case
224 	   it's a gain to check buf against NULL even though kfree() handles
225 	   NULL pointers just fine */
226 	if (unlikely(buf))
227 		kfree(buf);
228 
229 }
230 
231 static void bit_clear_margins(struct vc_data *vc, struct fb_info *info,
232 			      int color, int bottom_only)
233 {
234 	unsigned int cw = vc->vc_font.width;
235 	unsigned int ch = vc->vc_font.height;
236 	unsigned int rw = info->var.xres - (vc->vc_cols*cw);
237 	unsigned int bh = info->var.yres - (vc->vc_rows*ch);
238 	unsigned int rs = info->var.xres - rw;
239 	unsigned int bs = info->var.yres - bh;
240 	struct fb_fillrect region;
241 
242 	region.color = color;
243 	region.rop = ROP_COPY;
244 
245 	if ((int) rw > 0 && !bottom_only) {
246 		region.dx = info->var.xoffset + rs;
247 		region.dy = 0;
248 		region.width = rw;
249 		region.height = info->var.yres_virtual;
250 		info->fbops->fb_fillrect(info, &region);
251 	}
252 
253 	if ((int) bh > 0) {
254 		region.dx = info->var.xoffset;
255 		region.dy = info->var.yoffset + bs;
256 		region.width = rs;
257 		region.height = bh;
258 		info->fbops->fb_fillrect(info, &region);
259 	}
260 }
261 
262 static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable,
263 		       int fg, int bg)
264 {
265 	struct fb_cursor cursor;
266 	struct fbcon_par *par = info->fbcon_par;
267 	unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
268 	int w = DIV_ROUND_UP(vc->vc_font.width, 8), c;
269 	int y = real_y(par->p, vc->state.y);
270 	int attribute, use_sw = vc->vc_cursor_type & CUR_SW;
271 	int err = 1;
272 	const u8 *src;
273 
274 	cursor.set = 0;
275 
276 	if (!vc->vc_font.data)
277 		return;
278 
279 	c = scr_readw((u16 *) vc->vc_pos);
280 	attribute = get_attribute(info, c);
281 	c &= charmask;
282 
283 	/* Clamp to font size, same as bit_putcs_aligned() */
284 	if (c >= vc->vc_font.charcount)
285 		c = 0;
286 	src = vc->vc_font.data + (c * (w * vc->vc_font.height));
287 
288 	if (par->cursor_state.image.data != (const char *)src ||
289 	    par->cursor_reset) {
290 		par->cursor_state.image.data = src;
291 		cursor.set |= FB_CUR_SETIMAGE;
292 	}
293 
294 	if (attribute) {
295 		u8 *dst;
296 
297 		dst = kmalloc_array(w, vc->vc_font.height, GFP_ATOMIC);
298 		if (!dst)
299 			return;
300 		kfree(par->cursor_data);
301 		par->cursor_data = dst;
302 		update_attr(dst, src, attribute, vc);
303 		src = dst;
304 	}
305 
306 	if (par->cursor_state.image.fg_color != fg ||
307 	    par->cursor_state.image.bg_color != bg ||
308 	    par->cursor_reset) {
309 		par->cursor_state.image.fg_color = fg;
310 		par->cursor_state.image.bg_color = bg;
311 		cursor.set |= FB_CUR_SETCMAP;
312 	}
313 
314 	if ((par->cursor_state.image.dx != (vc->vc_font.width * vc->state.x)) ||
315 	    (par->cursor_state.image.dy != (vc->vc_font.height * y)) ||
316 	    par->cursor_reset) {
317 		par->cursor_state.image.dx = vc->vc_font.width * vc->state.x;
318 		par->cursor_state.image.dy = vc->vc_font.height * y;
319 		cursor.set |= FB_CUR_SETPOS;
320 	}
321 
322 	if (par->cursor_state.image.height != vc->vc_font.height ||
323 	    par->cursor_state.image.width != vc->vc_font.width ||
324 	    par->cursor_reset) {
325 		par->cursor_state.image.height = vc->vc_font.height;
326 		par->cursor_state.image.width = vc->vc_font.width;
327 		cursor.set |= FB_CUR_SETSIZE;
328 	}
329 
330 	if (par->cursor_state.hot.x || par->cursor_state.hot.y ||
331 	    par->cursor_reset) {
332 		par->cursor_state.hot.x = cursor.hot.y = 0;
333 		cursor.set |= FB_CUR_SETHOT;
334 	}
335 
336 	if (cursor.set & FB_CUR_SETSIZE ||
337 	    vc->vc_cursor_type != par->p->cursor_shape ||
338 	    par->cursor_state.mask == NULL ||
339 	    par->cursor_reset) {
340 		unsigned char *mask = fbcon_cursor_glyph(&vc->vc_font, vc->vc_cursor_type);
341 
342 		if (!mask)
343 			return;
344 
345 		kfree(par->cursor_state.mask);
346 		par->cursor_state.mask = (const char *)mask;
347 
348 		par->p->cursor_shape = vc->vc_cursor_type;
349 		cursor.set |= FB_CUR_SETSHAPE;
350 	}
351 
352 	par->cursor_state.enable = enable && !use_sw;
353 
354 	cursor.image.data = src;
355 	cursor.image.fg_color = par->cursor_state.image.fg_color;
356 	cursor.image.bg_color = par->cursor_state.image.bg_color;
357 	cursor.image.dx = par->cursor_state.image.dx;
358 	cursor.image.dy = par->cursor_state.image.dy;
359 	cursor.image.height = par->cursor_state.image.height;
360 	cursor.image.width = par->cursor_state.image.width;
361 	cursor.hot.x = par->cursor_state.hot.x;
362 	cursor.hot.y = par->cursor_state.hot.y;
363 	cursor.mask = par->cursor_state.mask;
364 	cursor.enable = par->cursor_state.enable;
365 	cursor.image.depth = 1;
366 	cursor.rop = ROP_XOR;
367 
368 	if (info->fbops->fb_cursor)
369 		err = info->fbops->fb_cursor(info, &cursor);
370 
371 	if (err)
372 		soft_cursor(info, &cursor);
373 
374 	par->cursor_reset = 0;
375 }
376 
377 static int bit_update_start(struct fb_info *info)
378 {
379 	struct fbcon_par *par = info->fbcon_par;
380 	int err;
381 
382 	err = fb_pan_display(info, &par->var);
383 	par->var.xoffset = info->var.xoffset;
384 	par->var.yoffset = info->var.yoffset;
385 	par->var.vmode = info->var.vmode;
386 	return err;
387 }
388 
389 static const struct fbcon_bitops bit_fbcon_bitops = {
390 	.bmove = bit_bmove,
391 	.clear = bit_clear,
392 	.putcs = bit_putcs,
393 	.clear_margins = bit_clear_margins,
394 	.cursor = bit_cursor,
395 	.update_start = bit_update_start,
396 };
397 
398 void fbcon_set_bitops_ur(struct fbcon_par *par)
399 {
400 	par->bitops = &bit_fbcon_bitops;
401 }
402