xref: /linux/drivers/video/fbdev/core/bitblit.c (revision 09b1704f5b02c18dd02b21343530463fcfc92c54)
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 
22 /*
23  * Accelerated handlers.
24  */
25 static void update_attr(u8 *dst, u8 *src, int attribute,
26 			       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 	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 	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 fb_image image;
155 	u32 width = DIV_ROUND_UP(vc->vc_font.width, 8);
156 	u32 cellsize = width * vc->vc_font.height;
157 	u32 maxcnt = info->pixmap.size/cellsize;
158 	u32 scan_align = info->pixmap.scan_align - 1;
159 	u32 buf_align = info->pixmap.buf_align - 1;
160 	u32 mod = vc->vc_font.width % 8, cnt, pitch, size;
161 	u32 attribute = get_attribute(info, scr_readw(s));
162 	u8 *dst, *buf = NULL;
163 
164 	image.fg_color = fg;
165 	image.bg_color = bg;
166 	image.dx = xx * vc->vc_font.width;
167 	image.dy = yy * vc->vc_font.height;
168 	image.height = vc->vc_font.height;
169 	image.depth = 1;
170 
171 	if (image.dy >= info->var.yres)
172 		return;
173 
174 	image.height = min(image.height, info->var.yres - image.dy);
175 
176 	if (attribute) {
177 		buf = kmalloc(cellsize, GFP_ATOMIC);
178 		if (!buf)
179 			return;
180 	}
181 
182 	while (count) {
183 		if (count > maxcnt)
184 			cnt = maxcnt;
185 		else
186 			cnt = count;
187 
188 		image.width = vc->vc_font.width * cnt;
189 
190 		if (image.dx >= info->var.xres)
191 			break;
192 
193 		if (image.dx + image.width > info->var.xres) {
194 			image.width = info->var.xres - image.dx;
195 			cnt = image.width / vc->vc_font.width;
196 			if (cnt == 0)
197 				break;
198 			image.width = cnt * vc->vc_font.width;
199 		}
200 
201 		pitch = DIV_ROUND_UP(image.width, 8) + scan_align;
202 		pitch &= ~scan_align;
203 		size = pitch * image.height + buf_align;
204 		size &= ~buf_align;
205 		dst = fb_get_buffer_offset(info, &info->pixmap, size);
206 		image.data = dst;
207 
208 		if (!mod)
209 			bit_putcs_aligned(vc, info, s, attribute, cnt, pitch,
210 					  width, cellsize, &image, buf, dst);
211 		else
212 			bit_putcs_unaligned(vc, info, s, attribute, cnt,
213 					    pitch, width, cellsize, &image,
214 					    buf, dst);
215 
216 		image.dx += cnt * vc->vc_font.width;
217 		count -= cnt;
218 		s += cnt;
219 	}
220 
221 	/* buf is always NULL except when in monochrome mode, so in this case
222 	   it's a gain to check buf against NULL even though kfree() handles
223 	   NULL pointers just fine */
224 	if (unlikely(buf))
225 		kfree(buf);
226 
227 }
228 
229 static void bit_clear_margins(struct vc_data *vc, struct fb_info *info,
230 			      int color, int bottom_only)
231 {
232 	unsigned int cw = vc->vc_font.width;
233 	unsigned int ch = vc->vc_font.height;
234 	unsigned int rw = info->var.xres - (vc->vc_cols*cw);
235 	unsigned int bh = info->var.yres - (vc->vc_rows*ch);
236 	unsigned int rs = info->var.xres - rw;
237 	unsigned int bs = info->var.yres - bh;
238 	struct fb_fillrect region;
239 
240 	region.color = color;
241 	region.rop = ROP_COPY;
242 
243 	if ((int) rw > 0 && !bottom_only) {
244 		region.dx = info->var.xoffset + rs;
245 		region.dy = 0;
246 		region.width = rw;
247 		region.height = info->var.yres_virtual;
248 		info->fbops->fb_fillrect(info, &region);
249 	}
250 
251 	if ((int) bh > 0) {
252 		region.dx = info->var.xoffset;
253 		region.dy = info->var.yoffset + bs;
254 		region.width = rs;
255 		region.height = bh;
256 		info->fbops->fb_fillrect(info, &region);
257 	}
258 }
259 
260 static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable,
261 		       int fg, int bg)
262 {
263 	struct fb_cursor cursor;
264 	struct fbcon_ops *ops = info->fbcon_par;
265 	unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
266 	int w = DIV_ROUND_UP(vc->vc_font.width, 8), c;
267 	int y = real_y(ops->p, vc->state.y);
268 	int attribute, use_sw = vc->vc_cursor_type & CUR_SW;
269 	int err = 1;
270 	char *src;
271 
272 	cursor.set = 0;
273 
274 	if (!vc->vc_font.data)
275 		return;
276 
277  	c = scr_readw((u16 *) vc->vc_pos);
278 	attribute = get_attribute(info, c);
279 	src = vc->vc_font.data + ((c & charmask) * (w * vc->vc_font.height));
280 
281 	if (ops->cursor_state.image.data != src ||
282 	    ops->cursor_reset) {
283 	    ops->cursor_state.image.data = src;
284 	    cursor.set |= FB_CUR_SETIMAGE;
285 	}
286 
287 	if (attribute) {
288 		u8 *dst;
289 
290 		dst = kmalloc_array(w, vc->vc_font.height, GFP_ATOMIC);
291 		if (!dst)
292 			return;
293 		kfree(ops->cursor_data);
294 		ops->cursor_data = dst;
295 		update_attr(dst, src, attribute, vc);
296 		src = dst;
297 	}
298 
299 	if (ops->cursor_state.image.fg_color != fg ||
300 	    ops->cursor_state.image.bg_color != bg ||
301 	    ops->cursor_reset) {
302 		ops->cursor_state.image.fg_color = fg;
303 		ops->cursor_state.image.bg_color = bg;
304 		cursor.set |= FB_CUR_SETCMAP;
305 	}
306 
307 	if ((ops->cursor_state.image.dx != (vc->vc_font.width * vc->state.x)) ||
308 	    (ops->cursor_state.image.dy != (vc->vc_font.height * y)) ||
309 	    ops->cursor_reset) {
310 		ops->cursor_state.image.dx = vc->vc_font.width * vc->state.x;
311 		ops->cursor_state.image.dy = vc->vc_font.height * y;
312 		cursor.set |= FB_CUR_SETPOS;
313 	}
314 
315 	if (ops->cursor_state.image.height != vc->vc_font.height ||
316 	    ops->cursor_state.image.width != vc->vc_font.width ||
317 	    ops->cursor_reset) {
318 		ops->cursor_state.image.height = vc->vc_font.height;
319 		ops->cursor_state.image.width = vc->vc_font.width;
320 		cursor.set |= FB_CUR_SETSIZE;
321 	}
322 
323 	if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
324 	    ops->cursor_reset) {
325 		ops->cursor_state.hot.x = cursor.hot.y = 0;
326 		cursor.set |= FB_CUR_SETHOT;
327 	}
328 
329 	if (cursor.set & FB_CUR_SETSIZE ||
330 	    vc->vc_cursor_type != ops->p->cursor_shape ||
331 	    ops->cursor_state.mask == NULL ||
332 	    ops->cursor_reset) {
333 		char *mask = kmalloc_array(w, vc->vc_font.height, GFP_ATOMIC);
334 		int cur_height, size, i = 0;
335 		u8 msk = 0xff;
336 
337 		if (!mask)
338 			return;
339 
340 		kfree(ops->cursor_state.mask);
341 		ops->cursor_state.mask = mask;
342 
343 		ops->p->cursor_shape = vc->vc_cursor_type;
344 		cursor.set |= FB_CUR_SETSHAPE;
345 
346 		switch (CUR_SIZE(ops->p->cursor_shape)) {
347 		case CUR_NONE:
348 			cur_height = 0;
349 			break;
350 		case CUR_UNDERLINE:
351 			cur_height = (vc->vc_font.height < 10) ? 1 : 2;
352 			break;
353 		case CUR_LOWER_THIRD:
354 			cur_height = vc->vc_font.height/3;
355 			break;
356 		case CUR_LOWER_HALF:
357 			cur_height = vc->vc_font.height >> 1;
358 			break;
359 		case CUR_TWO_THIRDS:
360 			cur_height = (vc->vc_font.height << 1)/3;
361 			break;
362 		case CUR_BLOCK:
363 		default:
364 			cur_height = vc->vc_font.height;
365 			break;
366 		}
367 		size = (vc->vc_font.height - cur_height) * w;
368 		while (size--)
369 			mask[i++] = ~msk;
370 		size = cur_height * w;
371 		while (size--)
372 			mask[i++] = msk;
373 	}
374 
375 	ops->cursor_state.enable = enable && !use_sw;
376 
377 	cursor.image.data = src;
378 	cursor.image.fg_color = ops->cursor_state.image.fg_color;
379 	cursor.image.bg_color = ops->cursor_state.image.bg_color;
380 	cursor.image.dx = ops->cursor_state.image.dx;
381 	cursor.image.dy = ops->cursor_state.image.dy;
382 	cursor.image.height = ops->cursor_state.image.height;
383 	cursor.image.width = ops->cursor_state.image.width;
384 	cursor.hot.x = ops->cursor_state.hot.x;
385 	cursor.hot.y = ops->cursor_state.hot.y;
386 	cursor.mask = ops->cursor_state.mask;
387 	cursor.enable = ops->cursor_state.enable;
388 	cursor.image.depth = 1;
389 	cursor.rop = ROP_XOR;
390 
391 	if (info->fbops->fb_cursor)
392 		err = info->fbops->fb_cursor(info, &cursor);
393 
394 	if (err)
395 		soft_cursor(info, &cursor);
396 
397 	ops->cursor_reset = 0;
398 }
399 
400 static int bit_update_start(struct fb_info *info)
401 {
402 	struct fbcon_ops *ops = info->fbcon_par;
403 	int err;
404 
405 	err = fb_pan_display(info, &ops->var);
406 	ops->var.xoffset = info->var.xoffset;
407 	ops->var.yoffset = info->var.yoffset;
408 	ops->var.vmode = info->var.vmode;
409 	return err;
410 }
411 
412 void fbcon_set_bitops(struct fbcon_ops *ops)
413 {
414 	ops->bmove = bit_bmove;
415 	ops->clear = bit_clear;
416 	ops->putcs = bit_putcs;
417 	ops->clear_margins = bit_clear_margins;
418 	ops->cursor = bit_cursor;
419 	ops->update_start = bit_update_start;
420 	ops->rotate_font = NULL;
421 
422 	if (ops->rotate)
423 		fbcon_set_rotate(ops);
424 }
425