2 * Copyright (c) 2013 The FreeBSD Foundation
5 * This software was developed by Aleksandr Rybalko under sponsorship from the
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/queue.h>
40 #include <dev/vt/vt.h>
41 #include <dev/vt/hw/fb/vt_fb.h>
42 #include <dev/vt/colors/vt_termcolors.h>
44 static struct vt_driver vt_fb_driver = {
46 .vd_init = vt_fb_init,
47 .vd_blank = vt_fb_blank,
48 .vd_bitblt_text = vt_fb_bitblt_text,
49 .vd_bitblt_bmp = vt_fb_bitblt_bitmap,
50 .vd_drawrect = vt_fb_drawrect,
51 .vd_setpixel = vt_fb_setpixel,
52 .vd_postswitch = vt_fb_postswitch,
53 .vd_priority = VD_PRIORITY_GENERIC+10,
54 .vd_fb_ioctl = vt_fb_ioctl,
55 .vd_fb_mmap = vt_fb_mmap,
56 .vd_suspend = vt_fb_suspend,
57 .vd_resume = vt_fb_resume,
60 VT_DRIVER_DECLARE(vt_fb, vt_fb_driver);
63 vt_fb_mem_wr1(struct fb_info *sc, uint32_t o, uint8_t v)
66 KASSERT((o < sc->fb_size), ("Offset %#08x out of fb size", o));
67 *(uint8_t *)(sc->fb_vbase + o) = v;
71 vt_fb_mem_wr2(struct fb_info *sc, uint32_t o, uint16_t v)
74 KASSERT((o < sc->fb_size), ("Offset %#08x out of fb size", o));
75 *(uint16_t *)(sc->fb_vbase + o) = v;
79 vt_fb_mem_wr4(struct fb_info *sc, uint32_t o, uint32_t v)
82 KASSERT((o < sc->fb_size), ("Offset %#08x out of fb size", o));
83 *(uint32_t *)(sc->fb_vbase + o) = v;
87 vt_fb_ioctl(struct vt_device *vd, u_long cmd, caddr_t data, struct thread *td)
96 bcopy(info, (struct fbtype *)data, sizeof(struct fbtype));
99 case FBIO_GETWINORG: /* get frame buffer window origin */
103 case FBIO_GETDISPSTART: /* get display start address */
104 ((video_display_start_t *)data)->x = 0;
105 ((video_display_start_t *)data)->y = 0;
108 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
109 *(u_int *)data = info->fb_stride;
112 case FBIO_BLANK: /* blank display */
113 if (vd->vd_driver->vd_blank == NULL)
115 vd->vd_driver->vd_blank(vd, TC_BLACK);
127 vt_fb_mmap(struct vt_device *vd, vm_ooffset_t offset, vm_paddr_t *paddr,
128 int prot, vm_memattr_t *memattr)
130 struct fb_info *info;
134 if (info->fb_flags & FB_FLAG_NOMMAP)
137 if (offset >= 0 && offset < info->fb_size) {
138 *paddr = info->fb_pbase + offset;
139 #ifdef VM_MEMATTR_WRITE_COMBINING
140 *memattr = VM_MEMATTR_WRITE_COMBINING;
149 vt_fb_setpixel(struct vt_device *vd, int x, int y, term_color_t color)
151 struct fb_info *info;
156 c = info->fb_cmap[color];
157 o = info->fb_stride * y + x * FBTYPE_GET_BYTESPP(info);
159 if (info->fb_flags & FB_FLAG_NOWRITE)
162 KASSERT((info->fb_vbase != 0), ("Unmapped framebuffer"));
164 switch (FBTYPE_GET_BYTESPP(info)) {
166 vt_fb_mem_wr1(info, o, c);
169 vt_fb_mem_wr2(info, o, c);
172 vt_fb_mem_wr1(info, o, (c >> 16) & 0xff);
173 vt_fb_mem_wr1(info, o + 1, (c >> 8) & 0xff);
174 vt_fb_mem_wr1(info, o + 2, c & 0xff);
177 vt_fb_mem_wr4(info, o, c);
187 vt_fb_drawrect(struct vt_device *vd, int x1, int y1, int x2, int y2, int fill,
192 for (y = y1; y <= y2; y++) {
193 if (fill || (y == y1) || (y == y2)) {
194 for (x = x1; x <= x2; x++)
195 vt_fb_setpixel(vd, x, y, color);
197 vt_fb_setpixel(vd, x1, y, color);
198 vt_fb_setpixel(vd, x2, y, color);
204 vt_fb_blank(struct vt_device *vd, term_color_t color)
206 struct fb_info *info;
211 c = info->fb_cmap[color];
213 if (info->fb_flags & FB_FLAG_NOWRITE)
216 KASSERT((info->fb_vbase != 0), ("Unmapped framebuffer"));
218 switch (FBTYPE_GET_BYTESPP(info)) {
220 for (h = 0; h < info->fb_height; h++)
221 for (o = 0; o < info->fb_stride; o++)
222 vt_fb_mem_wr1(info, h*info->fb_stride + o, c);
225 for (h = 0; h < info->fb_height; h++)
226 for (o = 0; o < info->fb_stride; o += 2)
227 vt_fb_mem_wr2(info, h*info->fb_stride + o, c);
230 for (h = 0; h < info->fb_height; h++)
231 for (o = 0; o < info->fb_stride; o += 3) {
232 vt_fb_mem_wr1(info, h*info->fb_stride + o,
234 vt_fb_mem_wr1(info, h*info->fb_stride + o + 1,
236 vt_fb_mem_wr1(info, h*info->fb_stride + o + 2,
241 for (h = 0; h < info->fb_height; h++)
242 for (o = 0; o < info->fb_stride; o += 4)
243 vt_fb_mem_wr4(info, h*info->fb_stride + o, c);
252 vt_fb_bitblt_bitmap(struct vt_device *vd, const struct vt_window *vw,
253 const uint8_t *pattern, const uint8_t *mask,
254 unsigned int width, unsigned int height,
255 unsigned int x, unsigned int y, term_color_t fg, term_color_t bg)
257 struct fb_info *info;
258 uint32_t fgc, bgc, cc, o;
265 bpp = FBTYPE_GET_BYTESPP(info);
266 fgc = info->fb_cmap[fg];
267 bgc = info->fb_cmap[bg];
269 bpl = (width + 7) >> 3; /* Bytes per source line. */
271 if (info->fb_flags & FB_FLAG_NOWRITE)
274 KASSERT((info->fb_vbase != 0), ("Unmapped framebuffer"));
276 line = (info->fb_stride * y) + (x * bpp);
278 l < height && y + l < vw->vw_draw_area.tr_end.tp_row;
282 c < width && x + c < vw->vw_draw_area.tr_end.tp_col;
293 /* Skip pixel write, if mask has no bit set. */
297 o = line + (c * bpp);
298 cc = b & 0x80 ? fgc : bgc;
302 vt_fb_mem_wr1(info, o, cc);
305 vt_fb_mem_wr2(info, o, cc);
308 /* Packed mode, so unaligned. Byte access. */
309 vt_fb_mem_wr1(info, o, (cc >> 16) & 0xff);
310 vt_fb_mem_wr1(info, o + 1, (cc >> 8) & 0xff);
311 vt_fb_mem_wr1(info, o + 2, cc & 0xff);
314 vt_fb_mem_wr4(info, o, cc);
321 line += info->fb_stride;
327 vt_fb_bitblt_text(struct vt_device *vd, const struct vt_window *vw,
328 const term_rect_t *area)
330 unsigned int col, row, x, y;
334 const uint8_t *pattern;
338 for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) {
339 for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col;
341 x = col * vf->vf_width +
342 vw->vw_draw_area.tr_begin.tp_col;
343 y = row * vf->vf_height +
344 vw->vw_draw_area.tr_begin.tp_row;
346 c = VTBUF_GET_FIELD(&vw->vw_buf, row, col);
347 pattern = vtfont_lookup(vf, c);
348 vt_determine_colors(c,
349 VTBUF_ISCURSOR(&vw->vw_buf, row, col), &fg, &bg);
351 vt_fb_bitblt_bitmap(vd, vw,
352 pattern, NULL, vf->vf_width, vf->vf_height,
357 #ifndef SC_NO_CUTPASTE
361 term_rect_t drawn_area;
363 drawn_area.tr_begin.tp_col = area->tr_begin.tp_col * vf->vf_width;
364 drawn_area.tr_begin.tp_row = area->tr_begin.tp_row * vf->vf_height;
365 drawn_area.tr_end.tp_col = area->tr_end.tp_col * vf->vf_width;
366 drawn_area.tr_end.tp_row = area->tr_end.tp_row * vf->vf_height;
368 if (vt_is_cursor_in_area(vd, &drawn_area)) {
369 vt_fb_bitblt_bitmap(vd, vw,
370 vd->vd_mcursor->map, vd->vd_mcursor->mask,
371 vd->vd_mcursor->width, vd->vd_mcursor->height,
372 vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col,
373 vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row,
374 vd->vd_mcursor_fg, vd->vd_mcursor_bg);
380 vt_fb_postswitch(struct vt_device *vd)
382 struct fb_info *info;
386 if (info->enter != NULL)
387 info->enter(info->fb_priv);
391 vt_fb_init_cmap(uint32_t *cmap, int depth)
396 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
397 0x7, 5, 0x7, 2, 0x3, 0));
399 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
400 0x1f, 10, 0x1f, 5, 0x1f, 0));
402 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
403 0x1f, 11, 0x3f, 5, 0x1f, 0));
405 case 32: /* Ignore alpha. */
406 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
407 0xff, 16, 0xff, 8, 0xff, 0));
414 vt_fb_init(struct vt_device *vd)
416 struct fb_info *info;
420 vd->vd_height = info->fb_height;
421 vd->vd_width = info->fb_width;
423 if (info->fb_size == 0)
426 if (info->fb_pbase == 0)
427 info->fb_flags |= FB_FLAG_NOMMAP;
429 if (info->fb_cmsize <= 0) {
430 err = vt_fb_init_cmap(info->fb_cmap, FBTYPE_GET_BPP(info));
433 info->fb_cmsize = 16;
436 /* Clear the screen. */
437 vd->vd_driver->vd_blank(vd, TC_BLACK);
439 /* Wakeup screen. KMS need this. */
440 vt_fb_postswitch(vd);
442 return (CN_INTERNAL);
446 vt_fb_attach(struct fb_info *info)
449 vt_allocate(&vt_fb_driver, info);
455 vt_fb_suspend(struct vt_device *vd)
462 vt_fb_resume(struct vt_device *vd)