fb.c (5366B)
1 #include "fb.h" 2 #include "internal.h" 3 #include "output.h" 4 #include "pointer.h" 5 #include "screen.h" 6 #include "seat.h" 7 #include "util.h" 8 9 #include <stdlib.h> 10 #include <string.h> 11 #include <wayland-server.h> 12 #include <wld/pixman.h> 13 #include <wld/wld.h> 14 15 struct swc_fb swc_fb; 16 17 /* for the cursor */ 18 static uint32_t 19 blend(uint32_t background, uint32_t foreground) 20 { 21 uint32_t alpha = foreground >> 24; 22 uint32_t inverse = 255 - alpha; 23 uint32_t red = ((foreground >> 16) & 0xff) + 24 (((background >> 16) & 0xff) * inverse + 127) / 255; 25 uint32_t green = ((foreground >> 8) & 0xff) + 26 (((background >> 8) & 0xff) * inverse + 127) / 255; 27 uint32_t blue = (foreground & 0xff) + 28 ((background & 0xff) * inverse + 127) / 255; 29 return (MIN(red, 255) << 16) | (MIN(green, 255) << 8) | MIN(blue, 255); 30 } 31 32 bool 33 fb_initialize(void) 34 { 35 if (!framebuffer_initialize(&swc_fb)) { 36 return false; 37 } 38 swc_fb.pitch = (size_t)swc_fb.width * sizeof(*swc_fb.pixels); 39 swc_fb.pixels = calloc(swc_fb.height, swc_fb.pitch); 40 if (!swc_fb.pixels) { 41 goto error0; 42 } 43 swc_fb.backend.context = wld_pixman_create_context(); 44 if (!swc_fb.backend.context) { 45 goto error1; 46 } 47 swc_fb.backend.renderer = wld_create_renderer(swc_fb.backend.context); 48 if (!swc_fb.backend.renderer) { 49 goto error2; 50 } 51 swc_fb.backend.cursor_width = 64; 52 swc_fb.backend.cursor_height = 64; 53 swc.backend = &swc_fb.backend; 54 return true; 55 56 error2: 57 wld_destroy_context(swc_fb.backend.context); 58 error1: 59 free(swc_fb.pixels); 60 error0: 61 framebuffer_finalize(&swc_fb); 62 return false; 63 } 64 65 void 66 fb_finalize(void) 67 { 68 wld_destroy_renderer(swc_fb.backend.renderer); 69 wld_destroy_context(swc_fb.backend.context); 70 free(swc_fb.pixels); 71 framebuffer_finalize(&swc_fb); 72 } 73 74 bool 75 fb_create_screens(struct wl_list *screens) 76 { 77 struct output *output; 78 struct screen *screen; 79 80 output = output_new_fb(swc_fb.width, swc_fb.height, framebuffer_name()); 81 if (!output) { 82 return false; 83 } 84 screen = screen_new(0, output, NULL); 85 if (!screen) { 86 output_destroy(output); 87 return false; 88 } 89 screen->id = 0; 90 output->screen = screen; 91 wl_list_insert(screens, &screen->link); 92 return true; 93 } 94 95 /* the frame's rectangle r into the copy, the cursor blended over it */ 96 static void 97 copy_box(struct wld_buffer *buffer, const pixman_box32_t *r, 98 struct wld_buffer *cursor, const pixman_box32_t *c) 99 { 100 int32_t y, x, x1, x2; 101 102 for (y = r->y1; y < r->y2; ++y) { 103 uint32_t *source = (uint32_t *)((uint8_t *)buffer->map + 104 (size_t)y * buffer->pitch); 105 uint32_t *destination = (uint32_t *)((uint8_t *)swc_fb.pixels + 106 (size_t)y * swc_fb.pitch); 107 108 memcpy(destination + r->x1, source + r->x1, 109 (size_t)(r->x2 - r->x1) * sizeof *destination); 110 if (!cursor || y < c->y1 || y >= c->y2) { 111 continue; 112 } 113 x1 = MAX(r->x1, c->x1); 114 x2 = MIN(r->x2, c->x2); 115 for (x = x1; x < x2; ++x) { 116 uint32_t foreground = *(uint32_t *)((uint8_t *)cursor->map + 117 (size_t)(y - c->y1) * cursor->pitch + (size_t)(x - c->x1) * 4); 118 destination[x] = blend(destination[x], foreground); 119 } 120 } 121 } 122 123 bool 124 fb_present(struct wld_buffer *buffer, int32_t origin_x, int32_t origin_y, 125 const pixman_region32_t *damage) 126 { 127 /* where the cursor was drawn last, put back from the frame this time */ 128 static pixman_box32_t drawn; 129 static bool whole_drawn; 130 struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL; 131 struct wld_buffer *cursor = NULL; 132 pixman_box32_t c = { 0, 0, 0, 0 }, *boxes; 133 pixman_region32_t region; 134 uint32_t width, height; 135 int i, n; 136 bool ok; 137 138 if (!buffer || !wld_map(buffer)) { 139 return false; 140 } 141 if (pointer && pointer->cursor.view.buffer && wld_map(pointer->cursor.buffer)) { 142 cursor = pointer->cursor.buffer; 143 c.x1 = pointer->cursor.view.geometry.x - origin_x; 144 c.y1 = pointer->cursor.view.geometry.y - origin_y; 145 c.x2 = c.x1 + (int32_t)cursor->width; 146 c.y2 = c.y1 + (int32_t)cursor->height; 147 } 148 149 width = buffer->width < swc_fb.width ? buffer->width : swc_fb.width; 150 height = buffer->height < swc_fb.height ? buffer->height : swc_fb.height; 151 pixman_region32_init(®ion); 152 /* the first frame, or one that does not fill the screen, is copied whole */ 153 if (width != swc_fb.width || height != swc_fb.height || !whole_drawn || !damage) { 154 if (width != swc_fb.width || height != swc_fb.height) { 155 memset(swc_fb.pixels, 0, swc_fb.height * swc_fb.pitch); 156 } 157 pixman_region32_init_rect(®ion, 0, 0, swc_fb.width, swc_fb.height); 158 whole_drawn = width == swc_fb.width && height == swc_fb.height; 159 } else { 160 pixman_region32_copy(®ion, damage); 161 pixman_region32_union_rect(®ion, ®ion, drawn.x1, drawn.y1, 162 drawn.x2 - drawn.x1, drawn.y2 - drawn.y1); 163 pixman_region32_union_rect(®ion, ®ion, c.x1, c.y1, 164 c.x2 - c.x1, c.y2 - c.y1); 165 } 166 pixman_region32_intersect_rect(®ion, ®ion, 0, 0, width, height); 167 168 boxes = pixman_region32_rectangles(®ion, &n); 169 for (i = 0; i < n; ++i) { 170 copy_box(buffer, &boxes[i], cursor, &c); 171 } 172 drawn = c; 173 if (cursor) { 174 wld_unmap(cursor); 175 } 176 wld_unmap(buffer); 177 if (width != swc_fb.width || height != swc_fb.height) { 178 pixman_region32_union_rect(®ion, ®ion, 0, 0, swc_fb.width, swc_fb.height); 179 } 180 ok = framebuffer_present(&swc_fb, ®ion); 181 pixman_region32_fini(®ion); 182 return ok; 183 }