damage.patch (14200B)
1 diff --git a/libswc/compositor.c b/libswc/compositor.c 2 index 7056ed6..987d9a7 100644 3 --- a/libswc/compositor.c 4 +++ b/libswc/compositor.c 5 @@ -118,6 +118,8 @@ static struct pointer_handler pointer_handler = { 6 static struct { 7 struct wl_list views; 8 pixman_region32_t damage, opaque; 9 + /* what the frame being presented changed, in its screen's coordinates */ 10 + pixman_region32_t frame_damage; 11 struct wl_listener swc_listener; 12 13 /* A mask of screens that have been repainted but are waiting on a page 14 @@ -805,6 +807,23 @@ schedule_updates(uint32_t screens) 15 compositor.scheduled_updates |= screens; 16 } 17 18 +const pixman_region32_t * 19 +compositor_frame_damage(void) 20 +{ 21 + return &compositor.frame_damage; 22 +} 23 + 24 +void 25 +compositor_damage_rect(int32_t x, int32_t y, uint32_t width, uint32_t height) 26 +{ 27 + if (!compositor.initialized || !width || !height) { 28 + return; 29 + } 30 + pixman_region32_union_rect(&compositor.damage, &compositor.damage, x, y, 31 + width, height); 32 + schedule_updates(-1); 33 +} 34 + 35 void 36 compositor_damage_all(void) 37 { 38 @@ -1998,6 +2017,10 @@ update_screen(struct screen *screen) 39 pixman_region32_intersect_rect(&damage, &compositor.damage, geom->x, 40 geom->y, geom->width, geom->height); 41 pixman_region32_translate(&damage, -geom->x, -geom->y); 42 + if (compositor.zoom != 1.0f) { 43 + pixman_region32_union_rect(&damage, &damage, 0, 0, geom->width, geom->height); 44 + } 45 + pixman_region32_copy(&compositor.frame_damage, &damage); 46 total_damage = wld_surface_damage(target->surface, &damage); 47 48 /* Don't repaint the screen if it is waiting for a page flip. */ 49 @@ -2226,6 +2249,7 @@ compositor_initialize(void) 50 } 51 pixman_region32_init(&compositor.damage); 52 pixman_region32_init(&compositor.opaque); 53 + pixman_region32_init(&compositor.frame_damage); 54 wl_list_init(&compositor.views); 55 wl_signal_init(&swc_compositor.signal.new_surface); 56 compositor.swc_listener.notify = &handle_swc_event; 57 @@ -2261,6 +2285,7 @@ compositor_finalize(void) 58 } 59 decor_finalize(); 60 pixman_region32_fini(&compositor.damage); 61 + pixman_region32_fini(&compositor.frame_damage); 62 pixman_region32_fini(&compositor.opaque); 63 wl_global_destroy(compositor.global); 64 } 65 diff --git a/libswc/compositor.h b/libswc/compositor.h 66 index 2442a8c..89d92f6 100644 67 --- a/libswc/compositor.h 68 +++ b/libswc/compositor.h 69 @@ -63,6 +63,13 @@ void 70 compositor_finalize(void); 71 void 72 compositor_damage_all(void); 73 +/* adds a rectangle of the global space to what the next frame repaints */ 74 +void 75 +compositor_damage_rect(int32_t x, int32_t y, uint32_t width, uint32_t height); 76 +/* what the frame being presented changed, in its screen's coordinates: 77 + * a backend that copies the frame copies only this */ 78 +const pixman_region32_t * 79 +compositor_frame_damage(void); 80 81 struct compositor_view { 82 struct view base; 83 diff --git a/libswc/fb.c b/libswc/fb.c 84 index 0755d94..6166b83 100644 85 --- a/libswc/fb.c 86 +++ b/libswc/fb.c 87 @@ -14,7 +14,7 @@ 88 89 struct swc_fb swc_fb; 90 91 -/* for the cursor */ 92 +/* for the cursor */ 93 static uint32_t 94 blend(uint32_t background, uint32_t foreground) 95 { 96 @@ -92,51 +92,92 @@ fb_create_screens(struct wl_list *screens) 97 return true; 98 } 99 100 +/* the frame's rectangle r into the copy, the cursor blended over it */ 101 +static void 102 +copy_box(struct wld_buffer *buffer, const pixman_box32_t *r, 103 + struct wld_buffer *cursor, const pixman_box32_t *c) 104 +{ 105 + int32_t y, x, x1, x2; 106 + 107 + for (y = r->y1; y < r->y2; ++y) { 108 + uint32_t *source = (uint32_t *)((uint8_t *)buffer->map + 109 + (size_t)y * buffer->pitch); 110 + uint32_t *destination = (uint32_t *)((uint8_t *)swc_fb.pixels + 111 + (size_t)y * swc_fb.pitch); 112 + 113 + memcpy(destination + r->x1, source + r->x1, 114 + (size_t)(r->x2 - r->x1) * sizeof *destination); 115 + if (!cursor || y < c->y1 || y >= c->y2) { 116 + continue; 117 + } 118 + x1 = MAX(r->x1, c->x1); 119 + x2 = MIN(r->x2, c->x2); 120 + for (x = x1; x < x2; ++x) { 121 + uint32_t foreground = *(uint32_t *)((uint8_t *)cursor->map + 122 + (size_t)(y - c->y1) * cursor->pitch + (size_t)(x - c->x1) * 4); 123 + destination[x] = blend(destination[x], foreground); 124 + } 125 + } 126 +} 127 + 128 bool 129 -fb_present(struct wld_buffer *buffer, int32_t origin_x, int32_t origin_y) 130 +fb_present(struct wld_buffer *buffer, int32_t origin_x, int32_t origin_y, 131 + const pixman_region32_t *damage) 132 { 133 + /* where the cursor was drawn last, put back from the frame this time */ 134 + static pixman_box32_t drawn; 135 + static bool whole_drawn; 136 struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL; 137 struct wld_buffer *cursor = NULL; 138 - int32_t cursor_x = 0, cursor_y = 0; 139 - uint32_t width, height, x, y; 140 - bool cursor_mapped = false; 141 + pixman_box32_t c = { 0, 0, 0, 0 }, *boxes; 142 + pixman_region32_t region; 143 + uint32_t width, height; 144 + int i, n; 145 + bool ok; 146 147 if (!buffer || !wld_map(buffer)) { 148 return false; 149 } 150 - if (pointer && pointer->cursor.view.buffer) { 151 + if (pointer && pointer->cursor.view.buffer && wld_map(pointer->cursor.buffer)) { 152 cursor = pointer->cursor.buffer; 153 - cursor_x = pointer->cursor.view.geometry.x - origin_x; 154 - cursor_y = pointer->cursor.view.geometry.y - origin_y; 155 - cursor_mapped = wld_map(cursor); 156 + c.x1 = pointer->cursor.view.geometry.x - origin_x; 157 + c.y1 = pointer->cursor.view.geometry.y - origin_y; 158 + c.x2 = c.x1 + (int32_t)cursor->width; 159 + c.y2 = c.y1 + (int32_t)cursor->height; 160 } 161 162 width = buffer->width < swc_fb.width ? buffer->width : swc_fb.width; 163 height = buffer->height < swc_fb.height ? buffer->height : swc_fb.height; 164 - if (width != swc_fb.width || height != swc_fb.height) { 165 - memset(swc_fb.pixels, 0, swc_fb.height * swc_fb.pitch); 166 - } 167 - for (y = 0; y < height; ++y) { 168 - uint32_t *source = (uint32_t *)((uint8_t *)buffer->map + 169 - (size_t)y * buffer->pitch); 170 - uint32_t *destination = (uint32_t *)((uint8_t *)swc_fb.pixels + 171 - (size_t)y * swc_fb.pitch); 172 - for (x = 0; x < width; ++x) { 173 - uint32_t pixel = source[x]; 174 - int32_t cx = (int32_t)x - cursor_x; 175 - int32_t cy = (int32_t)y - cursor_y; 176 - if (cursor_mapped && cx >= 0 && cy >= 0 && 177 - (uint32_t)cx < cursor->width && (uint32_t)cy < cursor->height) { 178 - uint32_t foreground = *(uint32_t *)((uint8_t *)cursor->map + 179 - (size_t)cy * cursor->pitch + (size_t)cx * 4); 180 - pixel = blend(pixel, foreground); 181 - } 182 - destination[x] = pixel; 183 + pixman_region32_init(®ion); 184 + /* the first frame, or one that does not fill the screen, is copied whole */ 185 + if (width != swc_fb.width || height != swc_fb.height || !whole_drawn || !damage) { 186 + if (width != swc_fb.width || height != swc_fb.height) { 187 + memset(swc_fb.pixels, 0, swc_fb.height * swc_fb.pitch); 188 } 189 + pixman_region32_init_rect(®ion, 0, 0, swc_fb.width, swc_fb.height); 190 + whole_drawn = width == swc_fb.width && height == swc_fb.height; 191 + } else { 192 + pixman_region32_copy(®ion, damage); 193 + pixman_region32_union_rect(®ion, ®ion, drawn.x1, drawn.y1, 194 + drawn.x2 - drawn.x1, drawn.y2 - drawn.y1); 195 + pixman_region32_union_rect(®ion, ®ion, c.x1, c.y1, 196 + c.x2 - c.x1, c.y2 - c.y1); 197 + } 198 + pixman_region32_intersect_rect(®ion, ®ion, 0, 0, width, height); 199 + 200 + boxes = pixman_region32_rectangles(®ion, &n); 201 + for (i = 0; i < n; ++i) { 202 + copy_box(buffer, &boxes[i], cursor, &c); 203 } 204 - if (cursor_mapped) { 205 + drawn = c; 206 + if (cursor) { 207 wld_unmap(cursor); 208 } 209 wld_unmap(buffer); 210 - return framebuffer_present(&swc_fb); 211 + if (width != swc_fb.width || height != swc_fb.height) { 212 + pixman_region32_union_rect(®ion, ®ion, 0, 0, swc_fb.width, swc_fb.height); 213 + } 214 + ok = framebuffer_present(&swc_fb, ®ion); 215 + pixman_region32_fini(®ion); 216 + return ok; 217 } 218 diff --git a/libswc/fb.h b/libswc/fb.h 219 index 8d3273d..5e0cb14 100644 220 --- a/libswc/fb.h 221 +++ b/libswc/fb.h 222 @@ -3,6 +3,7 @@ 223 224 #include "backend.h" 225 226 +#include <pixman.h> 227 #include <stdbool.h> 228 #include <stddef.h> 229 #include <stdint.h> 230 @@ -23,11 +24,14 @@ extern struct swc_fb swc_fb; 231 bool fb_initialize(void); 232 void fb_finalize(void); 233 bool fb_create_screens(struct wl_list *screens); 234 -bool fb_present(struct wld_buffer *buffer, int32_t x, int32_t y); 235 +/* damage is what the frame changed, NULL when unknown */ 236 +bool fb_present(struct wld_buffer *buffer, int32_t x, int32_t y, 237 + const pixman_region32_t *damage); 238 239 bool framebuffer_initialize(struct swc_fb *fb); 240 void framebuffer_finalize(struct swc_fb *fb); 241 -bool framebuffer_present(struct swc_fb *fb); 242 +/* the copy's pixels within region onto the screen */ 243 +bool framebuffer_present(struct swc_fb *fb, const pixman_region32_t *region); 244 const char *framebuffer_name(void); 245 246 #endif 247 diff --git a/libswc/fb_fbdev.c b/libswc/fb_fbdev.c 248 index abd25a0..c3fa65f 100644 249 --- a/libswc/fb_fbdev.c 250 +++ b/libswc/fb_fbdev.c 251 @@ -167,18 +167,44 @@ store_pixel(uint8_t *destination, uint32_t pixel) 252 } 253 } 254 255 +/* a pixel of the copy is the screen's own: rows go over whole */ 256 +static bool 257 +native_is_xrgb(void) 258 +{ 259 + return fbdev.bits_per_pixel == 32 && fbdev.red.offset == 16 && 260 + fbdev.red.length == 8 && fbdev.green.offset == 8 && 261 + fbdev.green.length == 8 && fbdev.blue.offset == 0 && 262 + fbdev.blue.length == 8; 263 +} 264 + 265 bool 266 -framebuffer_present(struct swc_fb *fb) 267 +framebuffer_present(struct swc_fb *fb, const pixman_region32_t *region) 268 { 269 uint32_t bytes = fbdev.bits_per_pixel / 8; 270 - uint32_t x, y; 271 - 272 - for (y = 0; y < fb->height; ++y) { 273 - uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + y * fb->pitch); 274 - uint8_t *destination = (uint8_t *)fbdev.memory + fbdev.memory_offset + 275 - (size_t)y * fbdev.pitch; 276 - for (x = 0; x < fb->width; ++x) { 277 - store_pixel(destination + (size_t)x * bytes, source[x]); 278 + bool same = native_is_xrgb(); 279 + const pixman_box32_t *boxes; 280 + int32_t x, y; 281 + int i, n; 282 + 283 + boxes = pixman_region32_rectangles((pixman_region32_t *)region, &n); 284 + for (i = 0; i < n; ++i) { 285 + const pixman_box32_t *b = &boxes[i]; 286 + 287 + for (y = b->y1; y < b->y2; ++y) { 288 + uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + 289 + (size_t)y * fb->pitch); 290 + uint8_t *destination = (uint8_t *)fbdev.memory + 291 + fbdev.memory_offset + 292 + (size_t)y * fbdev.pitch; 293 + 294 + if (same) { 295 + memcpy(destination + (size_t)b->x1 * 4, source + b->x1, 296 + (size_t)(b->x2 - b->x1) * 4); 297 + continue; 298 + } 299 + for (x = b->x1; x < b->x2; ++x) { 300 + store_pixel(destination + (size_t)x * bytes, source[x]); 301 + } 302 } 303 } 304 return true; 305 diff --git a/libswc/fb_wsdisplay.c b/libswc/fb_wsdisplay.c 306 index 03501d8..e891925 100644 307 --- a/libswc/fb_wsdisplay.c 308 +++ b/libswc/fb_wsdisplay.c 309 @@ -331,20 +331,44 @@ store_pixel(uint8_t *destination, uint32_t pixel) 310 } 311 } 312 313 +/* a pixel of the copy is the screen's own: rows go over whole */ 314 +static bool 315 +native_is_xrgb(void) 316 +{ 317 + return wsdisplay.bits_per_pixel == 32 && wsdisplay.red.offset == 16 && 318 + wsdisplay.red.length == 8 && wsdisplay.green.offset == 8 && 319 + wsdisplay.green.length == 8 && wsdisplay.blue.offset == 0 && 320 + wsdisplay.blue.length == 8; 321 +} 322 + 323 bool 324 -framebuffer_present(struct swc_fb *fb) 325 +framebuffer_present(struct swc_fb *fb, const pixman_region32_t *region) 326 { 327 uint32_t bytes = (wsdisplay.bits_per_pixel + 7) / 8; 328 - uint32_t x, y; 329 - 330 - for (y = 0; y < fb->height; ++y) { 331 - uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + 332 - (size_t)y * fb->pitch); 333 - uint8_t *destination = (uint8_t *)wsdisplay.memory + 334 - wsdisplay.memory_offset + 335 - (size_t)y * wsdisplay.pitch; 336 - for (x = 0; x < fb->width; ++x) { 337 - store_pixel(destination + (size_t)x * bytes, source[x]); 338 + bool same = native_is_xrgb(); 339 + const pixman_box32_t *boxes; 340 + int32_t x, y; 341 + int i, n; 342 + 343 + boxes = pixman_region32_rectangles((pixman_region32_t *)region, &n); 344 + for (i = 0; i < n; ++i) { 345 + const pixman_box32_t *b = &boxes[i]; 346 + 347 + for (y = b->y1; y < b->y2; ++y) { 348 + uint32_t *source = (uint32_t *)((uint8_t *)fb->pixels + 349 + (size_t)y * fb->pitch); 350 + uint8_t *destination = (uint8_t *)wsdisplay.memory + 351 + wsdisplay.memory_offset + 352 + (size_t)y * wsdisplay.pitch; 353 + 354 + if (same) { 355 + memcpy(destination + (size_t)b->x1 * 4, source + b->x1, 356 + (size_t)(b->x2 - b->x1) * 4); 357 + continue; 358 + } 359 + for (x = b->x1; x < b->x2; ++x) { 360 + store_pixel(destination + (size_t)x * bytes, source[x]); 361 + } 362 } 363 } 364 return true; 365 diff --git a/libswc/pointer.c b/libswc/pointer.c 366 index e785cbd..1c279fd 100644 367 --- a/libswc/pointer.c 368 +++ b/libswc/pointer.c 369 @@ -253,6 +253,7 @@ move(struct view *view, int32_t x, int32_t y) 370 #ifdef ENABLE_DRM 371 struct screen *screen; 372 #endif 373 + struct swc_rectangle old = view->geometry; 374 375 if (view_set_position(view, x, y)) { 376 view_update_screens(view); 377 @@ -273,7 +274,11 @@ move(struct view *view, int32_t x, int32_t y) 378 return true; 379 } 380 #endif 381 - compositor_damage_all(); 382 + /* a backend that draws the cursor into the frame repaints only where it 383 + * was and where it is */ 384 + compositor_damage_rect(old.x, old.y, old.width, old.height); 385 + compositor_damage_rect(view->geometry.x, view->geometry.y, 386 + view->geometry.width, view->geometry.height); 387 388 return true; 389 } 390 diff --git a/libswc/primary_plane.c b/libswc/primary_plane.c 391 index 011cb39..cd8616c 100644 392 --- a/libswc/primary_plane.c 393 +++ b/libswc/primary_plane.c 394 @@ -22,8 +22,8 @@ 395 */ 396 397 #include "primary_plane.h" 398 -#include "event.h" 399 #include "compositor.h" 400 +#include "event.h" 401 #ifdef ENABLE_DRM 402 #include "drm.h" 403 #endif 404 @@ -95,7 +95,8 @@ attach_drm(struct primary_plane *plane, struct wld_buffer *buffer) 405 static int 406 attach_fb(struct primary_plane *plane, struct wld_buffer *buffer) 407 { 408 - if (!fb_present(buffer, plane->view.geometry.x, plane->view.geometry.y)) { 409 + if (!fb_present(buffer, plane->view.geometry.x, plane->view.geometry.y, 410 + compositor_frame_damage())) { 411 return -1; 412 } 413 wl_event_loop_add_idle(swc.event_loop, &send_frame, plane);