xref: /openbmc/linux/drivers/video/fbdev/core/bitblit.c (revision 2cf1c348)
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_clear(struct vc_data *vc, struct fb_info *info, int sy,
47 		      int sx, int height, int width)
48 {
49 	int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
50 	struct fb_fillrect region;
51 
52 	region.color = attr_bgcol_ec(bgshift, vc, info);
53 	region.dx = sx * vc->vc_font.width;
54 	region.dy = sy * vc->vc_font.height;
55 	region.width = width * vc->vc_font.width;
56 	region.height = height * vc->vc_font.height;
57 	region.rop = ROP_COPY;
58 
59 	info->fbops->fb_fillrect(info, &region);
60 }
61 
62 static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,
63 				     const u16 *s, u32 attr, u32 cnt,
64 				     u32 d_pitch, u32 s_pitch, u32 cellsize,
65 				     struct fb_image *image, u8 *buf, u8 *dst)
66 {
67 	u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
68 	u32 idx = vc->vc_font.width >> 3;
69 	u8 *src;
70 
71 	while (cnt--) {
72 		src = vc->vc_font.data + (scr_readw(s++)&
73 					  charmask)*cellsize;
74 
75 		if (attr) {
76 			update_attr(buf, src, attr, vc);
77 			src = buf;
78 		}
79 
80 		if (likely(idx == 1))
81 			__fb_pad_aligned_buffer(dst, d_pitch, src, idx,
82 						image->height);
83 		else
84 			fb_pad_aligned_buffer(dst, d_pitch, src, idx,
85 					      image->height);
86 
87 		dst += s_pitch;
88 	}
89 
90 	info->fbops->fb_imageblit(info, image);
91 }
92 
93 static inline void bit_putcs_unaligned(struct vc_data *vc,
94 				       struct fb_info *info, const u16 *s,
95 				       u32 attr, u32 cnt, u32 d_pitch,
96 				       u32 s_pitch, u32 cellsize,
97 				       struct fb_image *image, u8 *buf,
98 				       u8 *dst)
99 {
100 	u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
101 	u32 shift_low = 0, mod = vc->vc_font.width % 8;
102 	u32 shift_high = 8;
103 	u32 idx = vc->vc_font.width >> 3;
104 	u8 *src;
105 
106 	while (cnt--) {
107 		src = vc->vc_font.data + (scr_readw(s++)&
108 					  charmask)*cellsize;
109 
110 		if (attr) {
111 			update_attr(buf, src, attr, vc);
112 			src = buf;
113 		}
114 
115 		fb_pad_unaligned_buffer(dst, d_pitch, src, idx,
116 					image->height, shift_high,
117 					shift_low, mod);
118 		shift_low += mod;
119 		dst += (shift_low >= 8) ? s_pitch : s_pitch - 1;
120 		shift_low &= 7;
121 		shift_high = 8 - shift_low;
122 	}
123 
124 	info->fbops->fb_imageblit(info, image);
125 
126 }
127 
128 static void bit_putcs(struct vc_data *vc, struct fb_info *info,
129 		      const unsigned short *s, int count, int yy, int xx,
130 		      int fg, int bg)
131 {
132 	struct fb_image image;
133 	u32 width = DIV_ROUND_UP(vc->vc_font.width, 8);
134 	u32 cellsize = width * vc->vc_font.height;
135 	u32 maxcnt = info->pixmap.size/cellsize;
136 	u32 scan_align = info->pixmap.scan_align - 1;
137 	u32 buf_align = info->pixmap.buf_align - 1;
138 	u32 mod = vc->vc_font.width % 8, cnt, pitch, size;
139 	u32 attribute = get_attribute(info, scr_readw(s));
140 	u8 *dst, *buf = NULL;
141 
142 	image.fg_color = fg;
143 	image.bg_color = bg;
144 	image.dx = xx * vc->vc_font.width;
145 	image.dy = yy * vc->vc_font.height;
146 	image.height = vc->vc_font.height;
147 	image.depth = 1;
148 
149 	if (attribute) {
150 		buf = kmalloc(cellsize, GFP_ATOMIC);
151 		if (!buf)
152 			return;
153 	}
154 
155 	while (count) {
156 		if (count > maxcnt)
157 			cnt = maxcnt;
158 		else
159 			cnt = count;
160 
161 		image.width = vc->vc_font.width * cnt;
162 		pitch = DIV_ROUND_UP(image.width, 8) + scan_align;
163 		pitch &= ~scan_align;
164 		size = pitch * image.height + buf_align;
165 		size &= ~buf_align;
166 		dst = fb_get_buffer_offset(info, &info->pixmap, size);
167 		image.data = dst;
168 
169 		if (!mod)
170 			bit_putcs_aligned(vc, info, s, attribute, cnt, pitch,
171 					  width, cellsize, &image, buf, dst);
172 		else
173 			bit_putcs_unaligned(vc, info, s, attribute, cnt,
174 					    pitch, width, cellsize, &image,
175 					    buf, dst);
176 
177 		image.dx += cnt * vc->vc_font.width;
178 		count -= cnt;
179 		s += cnt;
180 	}
181 
182 	/* buf is always NULL except when in monochrome mode, so in this case
183 	   it's a gain to check buf against NULL even though kfree() handles
184 	   NULL pointers just fine */
185 	if (unlikely(buf))
186 		kfree(buf);
187 
188 }
189 
190 static void bit_clear_margins(struct vc_data *vc, struct fb_info *info,
191 			      int color, int bottom_only)
192 {
193 	unsigned int cw = vc->vc_font.width;
194 	unsigned int ch = vc->vc_font.height;
195 	unsigned int rw = info->var.xres - (vc->vc_cols*cw);
196 	unsigned int bh = info->var.yres - (vc->vc_rows*ch);
197 	unsigned int rs = info->var.xres - rw;
198 	unsigned int bs = info->var.yres - bh;
199 	struct fb_fillrect region;
200 
201 	region.color = color;
202 	region.rop = ROP_COPY;
203 
204 	if ((int) rw > 0 && !bottom_only) {
205 		region.dx = info->var.xoffset + rs;
206 		region.dy = 0;
207 		region.width = rw;
208 		region.height = info->var.yres_virtual;
209 		info->fbops->fb_fillrect(info, &region);
210 	}
211 
212 	if ((int) bh > 0) {
213 		region.dx = info->var.xoffset;
214 		region.dy = info->var.yoffset + bs;
215 		region.width = rs;
216 		region.height = bh;
217 		info->fbops->fb_fillrect(info, &region);
218 	}
219 }
220 
221 static void bit_cursor(struct vc_data *vc, struct fb_info *info, int mode,
222 		       int fg, int bg)
223 {
224 	struct fb_cursor cursor;
225 	struct fbcon_ops *ops = info->fbcon_par;
226 	unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
227 	int w = DIV_ROUND_UP(vc->vc_font.width, 8), c;
228 	int y = real_y(ops->p, vc->state.y);
229 	int attribute, use_sw = vc->vc_cursor_type & CUR_SW;
230 	int err = 1;
231 	char *src;
232 
233 	cursor.set = 0;
234 
235  	c = scr_readw((u16 *) vc->vc_pos);
236 	attribute = get_attribute(info, c);
237 	src = vc->vc_font.data + ((c & charmask) * (w * vc->vc_font.height));
238 
239 	if (ops->cursor_state.image.data != src ||
240 	    ops->cursor_reset) {
241 	    ops->cursor_state.image.data = src;
242 	    cursor.set |= FB_CUR_SETIMAGE;
243 	}
244 
245 	if (attribute) {
246 		u8 *dst;
247 
248 		dst = kmalloc_array(w, vc->vc_font.height, GFP_ATOMIC);
249 		if (!dst)
250 			return;
251 		kfree(ops->cursor_data);
252 		ops->cursor_data = dst;
253 		update_attr(dst, src, attribute, vc);
254 		src = dst;
255 	}
256 
257 	if (ops->cursor_state.image.fg_color != fg ||
258 	    ops->cursor_state.image.bg_color != bg ||
259 	    ops->cursor_reset) {
260 		ops->cursor_state.image.fg_color = fg;
261 		ops->cursor_state.image.bg_color = bg;
262 		cursor.set |= FB_CUR_SETCMAP;
263 	}
264 
265 	if ((ops->cursor_state.image.dx != (vc->vc_font.width * vc->state.x)) ||
266 	    (ops->cursor_state.image.dy != (vc->vc_font.height * y)) ||
267 	    ops->cursor_reset) {
268 		ops->cursor_state.image.dx = vc->vc_font.width * vc->state.x;
269 		ops->cursor_state.image.dy = vc->vc_font.height * y;
270 		cursor.set |= FB_CUR_SETPOS;
271 	}
272 
273 	if (ops->cursor_state.image.height != vc->vc_font.height ||
274 	    ops->cursor_state.image.width != vc->vc_font.width ||
275 	    ops->cursor_reset) {
276 		ops->cursor_state.image.height = vc->vc_font.height;
277 		ops->cursor_state.image.width = vc->vc_font.width;
278 		cursor.set |= FB_CUR_SETSIZE;
279 	}
280 
281 	if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
282 	    ops->cursor_reset) {
283 		ops->cursor_state.hot.x = cursor.hot.y = 0;
284 		cursor.set |= FB_CUR_SETHOT;
285 	}
286 
287 	if (cursor.set & FB_CUR_SETSIZE ||
288 	    vc->vc_cursor_type != ops->p->cursor_shape ||
289 	    ops->cursor_state.mask == NULL ||
290 	    ops->cursor_reset) {
291 		char *mask = kmalloc_array(w, vc->vc_font.height, GFP_ATOMIC);
292 		int cur_height, size, i = 0;
293 		u8 msk = 0xff;
294 
295 		if (!mask)
296 			return;
297 
298 		kfree(ops->cursor_state.mask);
299 		ops->cursor_state.mask = mask;
300 
301 		ops->p->cursor_shape = vc->vc_cursor_type;
302 		cursor.set |= FB_CUR_SETSHAPE;
303 
304 		switch (CUR_SIZE(ops->p->cursor_shape)) {
305 		case CUR_NONE:
306 			cur_height = 0;
307 			break;
308 		case CUR_UNDERLINE:
309 			cur_height = (vc->vc_font.height < 10) ? 1 : 2;
310 			break;
311 		case CUR_LOWER_THIRD:
312 			cur_height = vc->vc_font.height/3;
313 			break;
314 		case CUR_LOWER_HALF:
315 			cur_height = vc->vc_font.height >> 1;
316 			break;
317 		case CUR_TWO_THIRDS:
318 			cur_height = (vc->vc_font.height << 1)/3;
319 			break;
320 		case CUR_BLOCK:
321 		default:
322 			cur_height = vc->vc_font.height;
323 			break;
324 		}
325 		size = (vc->vc_font.height - cur_height) * w;
326 		while (size--)
327 			mask[i++] = ~msk;
328 		size = cur_height * w;
329 		while (size--)
330 			mask[i++] = msk;
331 	}
332 
333 	switch (mode) {
334 	case CM_ERASE:
335 		ops->cursor_state.enable = 0;
336 		break;
337 	case CM_DRAW:
338 	case CM_MOVE:
339 	default:
340 		ops->cursor_state.enable = (use_sw) ? 0 : 1;
341 		break;
342 	}
343 
344 	cursor.image.data = src;
345 	cursor.image.fg_color = ops->cursor_state.image.fg_color;
346 	cursor.image.bg_color = ops->cursor_state.image.bg_color;
347 	cursor.image.dx = ops->cursor_state.image.dx;
348 	cursor.image.dy = ops->cursor_state.image.dy;
349 	cursor.image.height = ops->cursor_state.image.height;
350 	cursor.image.width = ops->cursor_state.image.width;
351 	cursor.hot.x = ops->cursor_state.hot.x;
352 	cursor.hot.y = ops->cursor_state.hot.y;
353 	cursor.mask = ops->cursor_state.mask;
354 	cursor.enable = ops->cursor_state.enable;
355 	cursor.image.depth = 1;
356 	cursor.rop = ROP_XOR;
357 
358 	if (info->fbops->fb_cursor)
359 		err = info->fbops->fb_cursor(info, &cursor);
360 
361 	if (err)
362 		soft_cursor(info, &cursor);
363 
364 	ops->cursor_reset = 0;
365 }
366 
367 static int bit_update_start(struct fb_info *info)
368 {
369 	struct fbcon_ops *ops = info->fbcon_par;
370 	int err;
371 
372 	err = fb_pan_display(info, &ops->var);
373 	ops->var.xoffset = info->var.xoffset;
374 	ops->var.yoffset = info->var.yoffset;
375 	ops->var.vmode = info->var.vmode;
376 	return err;
377 }
378 
379 void fbcon_set_bitops(struct fbcon_ops *ops)
380 {
381 	ops->clear = bit_clear;
382 	ops->putcs = bit_putcs;
383 	ops->clear_margins = bit_clear_margins;
384 	ops->cursor = bit_cursor;
385 	ops->update_start = bit_update_start;
386 	ops->rotate_font = NULL;
387 
388 	if (ops->rotate)
389 		fbcon_set_rotate(ops);
390 }
391