neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

fb_fbdev.c (5907B)


      1 #include "fb.h"
      2 #include "launch.h"
      3 #include "util.h"
      4 
      5 #include <fcntl.h>
      6 #include <linux/fb.h>
      7 #include <stdint.h>
      8 #include <string.h>
      9 #include <sys/ioctl.h>
     10 #include <sys/mman.h>
     11 #include <unistd.h>
     12 
     13 struct fb_channel {
     14 	uint8_t offset;
     15 	uint8_t length;
     16 };
     17 
     18 static struct {
     19 	int fd;
     20 	void *memory;
     21 	size_t memory_length;
     22 	size_t memory_offset;
     23 	uint32_t pitch;
     24 	uint8_t bits_per_pixel;
     25 	struct fb_channel red;
     26 	struct fb_channel green;
     27 	struct fb_channel blue;
     28 } fbdev;
     29 
     30 static struct fb_channel
     31 channel_from_fbdev(struct fb_bitfield field)
     32 {
     33 	return (struct fb_channel) {
     34 	    .offset = field.offset,
     35 	    .length = field.length,
     36 	};
     37 }
     38 
     39 bool
     40 framebuffer_initialize(struct swc_fb *fb)
     41 {
     42 	struct fb_fix_screeninfo fixed;
     43 	struct fb_var_screeninfo variable;
     44 	size_t visible_offset;
     45 
     46 	memset(&fbdev, 0, sizeof(fbdev));
     47 	fbdev.fd = launch_open_device(FBDEV_DEVICE, O_RDWR | O_CLOEXEC);
     48 	if (fbdev.fd < 0) {
     49 		ERROR("Could not open framebuffer device %s\n", FBDEV_DEVICE);
     50 		return false;
     51 	}
     52 	if (ioctl(fbdev.fd, FBIOGET_FSCREENINFO, &fixed) < 0 ||
     53 	    ioctl(fbdev.fd, FBIOGET_VSCREENINFO, &variable) < 0) {
     54 		ERROR("Could not query framebuffer device %s\n", FBDEV_DEVICE);
     55 		goto error;
     56 	}
     57 	if (variable.bits_per_pixel != 16 && variable.bits_per_pixel != 24 &&
     58 	    variable.bits_per_pixel != 32) {
     59 		ERROR("Unsupported fbdev depth: %u bits per pixel\n",
     60 		      variable.bits_per_pixel);
     61 		goto error;
     62 	}
     63 	if (fixed.type != FB_TYPE_PACKED_PIXELS ||
     64 	    (fixed.visual != FB_VISUAL_TRUECOLOR &&
     65 	     fixed.visual != FB_VISUAL_DIRECTCOLOR)) {
     66 		ERROR("Unsupported fbdev framebuffer type or visual\n");
     67 		goto error;
     68 	}
     69 
     70 	if (!variable.xres || !variable.yres || variable.xres > UINT16_MAX ||
     71 	    variable.yres > UINT16_MAX ||
     72 	    variable.red.msb_right || variable.green.msb_right ||
     73 	    variable.blue.msb_right ||
     74 	    variable.red.length > 16 || variable.green.length > 16 ||
     75 	    variable.blue.length > 16 ||
     76 	    variable.red.offset + variable.red.length > variable.bits_per_pixel ||
     77 	    variable.green.offset + variable.green.length > variable.bits_per_pixel ||
     78 	    variable.blue.offset + variable.blue.length > variable.bits_per_pixel) {
     79 		ERROR("Unsupported fbdev geometry or channel layout\n");
     80 		goto error;
     81 	}
     82 	fbdev.memory_length = fixed.smem_len;
     83 	fbdev.memory = mmap(NULL, fbdev.memory_length, PROT_READ | PROT_WRITE,
     84 	                  MAP_SHARED, fbdev.fd, 0);
     85 	if (fbdev.memory == MAP_FAILED) {
     86 		fbdev.memory = NULL;
     87 		ERROR("Could not map framebuffer device %s\n", FBDEV_DEVICE);
     88 		goto error;
     89 	}
     90 
     91 	fb->width = variable.xres;
     92 	fb->height = variable.yres;
     93 	fbdev.pitch = fixed.line_length;
     94 	fbdev.bits_per_pixel = variable.bits_per_pixel;
     95 	fbdev.red = channel_from_fbdev(variable.red);
     96 	fbdev.green = channel_from_fbdev(variable.green);
     97 	fbdev.blue = channel_from_fbdev(variable.blue);
     98 	visible_offset = (size_t)variable.yoffset * fbdev.pitch +
     99 	                 (size_t)variable.xoffset * variable.bits_per_pixel / 8;
    100 	if (fixed.line_length < (size_t)variable.xres * variable.bits_per_pixel / 8 ||
    101 	    visible_offset >= fbdev.memory_length ||
    102 	    (size_t)(variable.yres - 1) * fixed.line_length >
    103 	        fbdev.memory_length - visible_offset ||
    104 	    (size_t)variable.xres * variable.bits_per_pixel / 8 >
    105 	        fbdev.memory_length - visible_offset -
    106 	            (size_t)(variable.yres - 1) * fixed.line_length) {
    107 		ERROR("fbdev visible buffer lies outside its mapping\n");
    108 		goto error;
    109 	}
    110 	fbdev.memory_offset = visible_offset;
    111 	return true;
    112 
    113 error:
    114 	if (fbdev.memory) {
    115 		munmap(fbdev.memory, fbdev.memory_length);
    116 	}
    117 	close(fbdev.fd);
    118 	fbdev.fd = -1;
    119 	return false;
    120 }
    121 
    122 void
    123 framebuffer_finalize(struct swc_fb *fb)
    124 {
    125 	(void)fb;
    126 	munmap(fbdev.memory, fbdev.memory_length);
    127 	close(fbdev.fd);
    128 }
    129 
    130 static uint32_t
    131 channel(uint8_t value, struct fb_channel field)
    132 {
    133 	uint32_t maximum;
    134 
    135 	if (!field.length) {
    136 		return 0;
    137 	}
    138 	maximum = field.length >= 32 ? UINT32_MAX : (1u << field.length) - 1;
    139 	return (((uint32_t)value * maximum + 127) / 255) << field.offset;
    140 }
    141 
    142 static uint32_t
    143 native_pixel(uint32_t pixel)
    144 {
    145 	return channel(pixel >> 16, fbdev.red) |
    146 	       channel(pixel >> 8, fbdev.green) |
    147 	       channel(pixel, fbdev.blue);
    148 }
    149 
    150 static void
    151 store_pixel(uint8_t *destination, uint32_t pixel)
    152 {
    153 	uint32_t native = native_pixel(pixel);
    154 
    155 	if (fbdev.bits_per_pixel == 24) {
    156 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
    157 		destination[0] = native >> 16;
    158 		destination[1] = native >> 8;
    159 		destination[2] = native;
    160 #else
    161 		destination[0] = native;
    162 		destination[1] = native >> 8;
    163 		destination[2] = native >> 16;
    164 #endif
    165 	} else {
    166 		memcpy(destination, &native, fbdev.bits_per_pixel / 8);
    167 	}
    168 }
    169 
    170 /* a pixel of the copy is the screen's own: rows go over whole */
    171 static bool
    172 native_is_xrgb(void)
    173 {
    174 	return fbdev.bits_per_pixel == 32 && fbdev.red.offset == 16 &&
    175 	       fbdev.red.length == 8 && fbdev.green.offset == 8 &&
    176 	       fbdev.green.length == 8 && fbdev.blue.offset == 0 &&
    177 	       fbdev.blue.length == 8;
    178 }
    179 
    180 bool
    181 framebuffer_present(struct swc_fb *fb, const pixman_region32_t *region)
    182 {
    183 	uint32_t bytes = fbdev.bits_per_pixel / 8;
    184 	bool same = native_is_xrgb();
    185 	const pixman_box32_t *boxes;
    186 	int32_t x, y;
    187 	int i, n;
    188 
    189 	boxes = pixman_region32_rectangles((pixman_region32_t *)region, &n);
    190 	for (i = 0; i < n; ++i) {
    191 		const pixman_box32_t *b = &boxes[i];
    192 
    193 		for (y = b->y1; y < b->y2; ++y) {
    194 			uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels +
    195 			                                (size_t)y * fb->pitch);
    196 			uint8_t *destination = (uint8_t *)fbdev.memory +
    197 			                       fbdev.memory_offset +
    198 			                       (size_t)y * fbdev.pitch;
    199 
    200 			if (same) {
    201 				memcpy(destination + (size_t)b->x1 * 4, source + b->x1,
    202 				       (size_t)(b->x2 - b->x1) * 4);
    203 				continue;
    204 			}
    205 			for (x = b->x1; x < b->x2; ++x) {
    206 				store_pixel(destination + (size_t)x * bytes, source[x]);
    207 			}
    208 		}
    209 	}
    210 	return true;
    211 }
    212 
    213 const char *
    214 framebuffer_name(void)
    215 {
    216 	return "fbdev-0";
    217 }