shm.c
This file
Path
shm.c
1,087 lines · 29.7 KiB · C
Last changed by
shm: clang 23 complains max_pool_size is unused on x86 (which it is)Daniel Eklöfbefore adoption251550d
History of this fileTake it
The walk stopped after 200 commits: lines older than 6a4c416 point at it.
changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1–94why, one step back1#include "shm.h"23#include <stdio.h>4#include <string.h>5#include <errno.h>6#include <unistd.h>7#include <limits.h>8#include <time.h>910#include <sys/types.h>11#include <sys/mman.h>12#include <fcntl.h>1314#include <pixman.h>1516#include <fcft/stride.h>17#include <tllist.h>1819#define LOG_MODULE "shm"20#define LOG_ENABLE_DBG 021#include "log.h"22#include "debug.h"23#include "macros.h"24#include "xmalloc.h"2526#if !defined(MAP_UNINITIALIZED)27 #define MAP_UNINITIALIZED 028#endif2930#if !defined(MFD_NOEXEC_SEAL)31 #define MFD_NOEXEC_SEAL 032#endif3334#define TIME_SCROLL 03536#define FORCED_DOUBLE_BUFFERING 03738/*39 * Maximum memfd size allowed.40 *41 * On 64-bit, we could in theory use up to 2GB (wk_shm_create_pool()42 * is limited to int32_t), since we never mmap() the entire region.43 *44 * The compositor is different matter - it needs to mmap() the entire45 * range, and *keep* the mapping for as long as is has buffers46 * referencing it (thus - always). And if we open multiple terminals,47 * then the required address space multiples...48 *49 * That said, 128TB (the total amount of available user address space50 * on 64-bit) is *a lot*; we can fit 67108864 2GB memfds into51 * that. But, let's be conservative for now.52 *53 * On 32-bit the available address space is too small and SHM54 * scrolling is disabled.55 *56 * Note: this is the _default_ size. It can be overridden by calling57 * shm_set_max_pool_size();58 */59static off_t max_pool_size = 512 * 1024 * 1024;6061static bool can_punch_hole = false;62static bool can_punch_hole_initialized = false;6364struct buffer_pool {65 int fd; /* memfd */66 struct wl_shm_pool *wl_pool;6768 void *real_mmapped; /* Address returned from mmap */69 size_t mmap_size; /* Size of mmap (>= size) */7071 size_t ref_count;72};7374struct buffer_chain;75struct buffer_private {76 struct buffer public;77 struct buffer_chain *chain;7879 size_t ref_count;80 bool busy; /* Owned by compositor */8182 struct buffer_pool *pool;83 off_t offset; /* Offset into memfd where data begins */84 size_t size;85 bool with_alpha;8687 bool scrollable;88};8990struct buffer_chain {91 tll(struct buffer_private *) bufs;92 struct wl_shm *shm;93 size_t pix_instances;94 bool scrollable;Implement gamma-correct blendingDaniel Eklöfbefore adoption2d19826
lines 95–98why, one step back95 pixman_format_code_t pix_fmt_with_alpha;96 pixman_format_code_t pix_fmt_without_alpha;97 enum wl_shm_format wl_shm_fmt_with_alpha;98 enum wl_shm_format wl_shm_fmt_without_alpha;changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 99–289why, one step back99};100101static tll(struct buffer_private *) deferred;102103#undef MEASURE_SHM_ALLOCS104#if defined(MEASURE_SHM_ALLOCS)105static size_t max_alloced = 0;106#endif107108void109shm_set_max_pool_size(off_t _max_pool_size)110{111 max_pool_size = _max_pool_size;112}113114static void115buffer_destroy_dont_close(struct buffer *buf)116{117 if (buf->pix != NULL) {118 for (size_t i = 0; i < buf->pix_instances; i++) {119 if (buf->pix[i] != NULL)120 pixman_image_unref(buf->pix[i]);121 }122 }123 free(buf->pix);124125#if defined(FUZZEL_ENABLE_CAIRO)126 if (buf->cairo != NULL) {127 for (size_t i = 0; i < buf->pix_instances; i++) {128 if (buf->cairo[i] != NULL) {129 cairo_surface_destroy(cairo_get_target(buf->cairo[i]));130 cairo_destroy(buf->cairo[i]);131 }132 }133134 free(buf->cairo);135 }136#endif137138 if (buf->wl_buf != NULL)139 wl_buffer_destroy(buf->wl_buf);140141 buf->pix = NULL;142 buf->wl_buf = NULL;143 buf->data = NULL;144}145146static void147pool_unref(struct buffer_pool *pool)148{149 if (pool == NULL)150 return;151152 assert(pool->ref_count > 0);153 pool->ref_count--;154155 if (pool->ref_count > 0)156 return;157158 if (pool->real_mmapped != MAP_FAILED)159 munmap(pool->real_mmapped, pool->mmap_size);160 if (pool->wl_pool != NULL)161 wl_shm_pool_destroy(pool->wl_pool);162 if (pool->fd >= 0)163 close(pool->fd);164165 pool->real_mmapped = MAP_FAILED;166 pool->wl_pool = NULL;167 pool->fd = -1;168 free(pool);169}170171static void172buffer_destroy(struct buffer_private *buf)173{174 buffer_destroy_dont_close(&buf->public);175 pool_unref(buf->pool);176 buf->pool = NULL;177178 for (size_t i = 0; i < buf->public.pix_instances; i++)179 pixman_region32_fini(&buf->public.dirty[i]);180 free(buf->public.dirty);181 free(buf);182}183184static bool185buffer_unref_no_remove_from_chain(struct buffer_private *buf)186{187 assert(buf->ref_count > 0);188 buf->ref_count--;189190 if (buf->ref_count > 0)191 return false;192193 if (buf->busy)194 tll_push_back(deferred, buf);195 else196 buffer_destroy(buf);197 return true;198}199200void201shm_fini(void)202{203 LOG_DBG("deferred buffers: %zu", tll_length(deferred));204205 tll_foreach(deferred, it) {206 buffer_destroy(it->item);207 tll_remove(deferred, it);208 }209210#if defined(MEASURE_SHM_ALLOCS) && MEASURE_SHM_ALLOCS211 LOG_INFO("max total allocations was: %zu MB", max_alloced / 1024 / 1024);212#endif213}214215static void216buffer_release(void *data, struct wl_buffer *wl_buffer)217{218 struct buffer_private *buffer = data;219220 assert(buffer->public.wl_buf == wl_buffer);221 assert(buffer->busy);222 buffer->busy = false;223224 if (buffer->ref_count == 0) {225 bool found = false;226 tll_foreach(deferred, it) {227 if (it->item == buffer) {228 found = true;229 tll_remove(deferred, it);230 break;231 }232 }233234 buffer_destroy(buffer);235236 assert(found);237 if (!found)238 LOG_WARN("deferred delete: buffer not on the 'deferred' list");239 }240}241242static const struct wl_buffer_listener buffer_listener = {243 .release = &buffer_release,244};245246#if __SIZEOF_POINTER__ == 8247static size_t248page_size(void)249{250 static size_t size = 0;251 if (size == 0) {252 long n = sysconf(_SC_PAGE_SIZE);253 if (n <= 0) {254 LOG_ERRNO("failed to get page size");255 size = 4096;256 } else {257 size = (size_t)n;258 }259 }260 assert(size > 0);261 return size;262}263#endif264265static bool266instantiate_offset(struct buffer_private *buf, off_t new_offset)267{268 assert(buf->public.data == NULL);269 assert(buf->public.pix == NULL);270 assert(buf->public.wl_buf == NULL);271 assert(buf->pool != NULL);272273 const struct buffer_pool *pool = buf->pool;274275 void *mmapped = MAP_FAILED;276 struct wl_buffer *wl_buf = NULL;277 pixman_image_t **pix = xcalloc(buf->public.pix_instances, sizeof(pix[0]));278279#if defined(FUZZEL_ENABLE_CAIRO)280 cairo_t **cairos = xcalloc(buf->public.pix_instances, sizeof(cairos[0]));281 cairo_surface_t **cairo_surfs =282 xcalloc(buf->public.pix_instances, sizeof(cairo_surfs[0]));283#endif284285 mmapped = (uint8_t *)pool->real_mmapped + new_offset;286287 wl_buf = wl_shm_pool_create_buffer(288 pool->wl_pool, new_offset,289 buf->public.width, buf->public.height, buf->public.stride,Implement gamma-correct blendingDaniel Eklöfbefore adoption2d19826
lines 290–292why, one step back290 buf->with_alpha291 ? buf->chain->wl_shm_fmt_with_alpha292 : buf->chain->wl_shm_fmt_without_alpha);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 293–301why, one step back293294 if (wl_buf == NULL) {295 LOG_ERR("failed to create SHM buffer");296 goto err;297 }298299 /* One pixman image for each worker thread (do we really need multiple?) */300 for (size_t i = 0; i < buf->public.pix_instances; i++) {301 pix[i] = pixman_image_create_bits_no_clear(Implement gamma-correct blendingDaniel Eklöfbefore adoption2d19826
lines 302–304why, one step back302 buf->with_alpha303 ? buf->chain->pix_fmt_with_alpha304 : buf->chain->pix_fmt_without_alpha,changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 305–402why, one step back305 buf->public.width, buf->public.height,306 (uint32_t *)mmapped, buf->public.stride);307308 if (pix[i] == NULL) {309 LOG_ERR("failed to create pixman image");310 goto err;311 }312313#if defined(FUZZEL_ENABLE_CAIRO)314 cairo_surfs[i] = cairo_image_surface_create_for_data(315 mmapped, buf->with_alpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,316 buf->public.width, buf->public.height, buf->public.stride);317318 if (cairo_surface_status(cairo_surfs[i]) != CAIRO_STATUS_SUCCESS) {319 LOG_ERR(320 "failed to create cairo surface: %s",321 cairo_status_to_string(cairo_surface_status(cairo_surfs[i])));322 goto err;323 }324325 cairos[i] = cairo_create(cairo_surfs[i]);326 if (cairo_status(cairos[i]) != CAIRO_STATUS_SUCCESS) {327 LOG_ERR("failed to create cairo context: %s",328 cairo_status_to_string(cairo_status(cairos[i])));329 goto err;330 }331#endif332 }333334 buf->public.data = mmapped;335 buf->public.wl_buf = wl_buf;336 buf->public.pix = pix;337 buf->offset = new_offset;338339#if defined(FUZZEL_ENABLE_CAIRO)340 buf->public.cairo = cairos;341 free(cairo_surfs);342#endif343344 wl_buffer_add_listener(wl_buf, &buffer_listener, buf);345 return true;346347err:348 if (pix != NULL) {349 for (size_t i = 0; i < buf->public.pix_instances; i++) {350 if (pix[i] != NULL)351 pixman_image_unref(pix[i]);352 }353 }354 free(pix);355356#if defined(FUZZEL_ENABLE_CAIRO)357 if (cairos != NULL) {358 for (size_t i = 0; i < buf->public.pix_instances; i++) {359 if (cairos[i] != NULL)360 cairo_destroy(cairos[i]);361 }362 free(cairos);363 }364 if (cairo_surfs != NULL) {365 for (size_t i = 0; i < buf->public.pix_instances; i++) {366 if (cairo_surfs[i] != NULL)367 cairo_surface_destroy(cairo_surfs[i]);368 }369 free(cairo_surfs);370 }371#endif372373 if (wl_buf != NULL)374 wl_buffer_destroy(wl_buf);375376 abort();377 return false;378}379380static void381get_new_buffers(struct buffer_chain *chain, size_t count,382 int widths[static count], int heights[static count],383 struct buffer *bufs[static count], bool with_alpha,384 bool immediate_purge)385{386 assert(count == 1 || !chain->scrollable);387 /*388 * No existing buffer available. Create a new one by:389 *390 * 1. open a memory backed "file" with memfd_create()391 * 2. mmap() the memory file, to be used by the pixman image392 * 3. create a wayland shm buffer for the same memory file393 *394 * The pixman image and the wayland buffer are now sharing memory.395 */396397 int stride[count];398 int sizes[count];399400 size_t total_size = 0;401 for (size_t i = 0; i < count; i++) {402 stride[i] = stride_for_format_and_width(Implement gamma-correct blendingDaniel Eklöfbefore adoption2d19826
lines 403–406why, one step back403 with_alpha404 ? chain->pix_fmt_with_alpha405 : chain->pix_fmt_without_alpha,406 widths[i]);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 407–426why, one step back407 sizes[i] = stride[i] * heights[i];408 total_size += sizes[i];409 }410 if (total_size == 0)411 return;412413 int pool_fd = -1;414415 void *real_mmapped = MAP_FAILED;416 struct wl_shm_pool *wl_pool = NULL;417 struct buffer_pool *pool = NULL;418419 /* Backing memory for SHM */420#if defined(MEMFD_CREATE)421 /*422 * Older kernels reject MFD_NOEXEC_SEAL with EINVAL. Try first423 * *with* it, and if that fails, try again *without* it.424 */425 errno = 0;426 pool_fd = memfd_create(shm: foot -> fuzzelDaniel Eklöfbefore adoption1031211
line 427why, one step back427 "fuzzel-wayland-shm-buffer-pool",changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 428–431why, one step back428 MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_NOEXEC_SEAL);429430 if (pool_fd < 0 && errno == EINVAL && MFD_NOEXEC_SEAL != 0) {431 pool_fd = memfd_create(shm: foot -> fuzzelDaniel Eklöfbefore adoption1031211
line 432why, one step back432 "fuzzel-wayland-shm-buffer-pool", MFD_CLOEXEC | MFD_ALLOW_SEALING);changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 433–438why, one step back433 }434435#elif defined(__FreeBSD__)436 // memfd_create on FreeBSD 13 is SHM_ANON without sealing support437 pool_fd = shm_open(SHM_ANON, O_RDWR | O_CLOEXEC, 0600);438#elseshm: foot -> fuzzelDaniel Eklöfbefore adoption1031211
line 439why, one step back439 char name[] = "/tmp/fuzzel-wayland-shm-buffer-pool-XXXXXX";changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 440–457why, one step back440 pool_fd = mkostemp(name, O_CLOEXEC);441 unlink(name);442#endif443 if (pool_fd == -1) {444 LOG_ERRNO("failed to create SHM backing memory file");445 goto err;446 }447448#if __SIZEOF_POINTER__ == 8449 off_t offset = chain->scrollable && max_pool_size > 0450 ? (max_pool_size / 4) & ~(page_size() - 1)451 : 0;452 off_t memfd_size = chain->scrollable && max_pool_size > 0453 ? max_pool_size454 : total_size;455#else456 off_t offset = 0;457 off_t memfd_size = total_size;shm: clang 23 complains max_pool_size is unused on x86 (which it is)Daniel Eklöfbefore adoption251550d
line 458why, one step back458 (void)max_pool_size;changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 459–691why, one step back459#endif460461 assert(chain->scrollable || (offset == 0 && memfd_size == total_size));462463 LOG_DBG("memfd-size: %lu, initial offset: %lu", memfd_size, offset);464465 if (ftruncate(pool_fd, memfd_size) == -1) {466 LOG_ERRNO("failed to set size of SHM backing memory file");467 goto err;468 }469470 if (!can_punch_hole_initialized) {471 can_punch_hole_initialized = true;472#if __SIZEOF_POINTER__ == 8 && defined(FALLOC_FL_PUNCH_HOLE)473 can_punch_hole = fallocate(474 pool_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1) == 0;475476 if (!can_punch_hole) {477 LOG_WARN(478 "fallocate(FALLOC_FL_PUNCH_HOLE) not "479 "supported (%s): expect lower performance", strerror(errno));480 }481#else482 /* This is mostly to make sure we skip the warning issued483 * above */484 can_punch_hole = false;485#endif486 }487488 if (chain->scrollable && !can_punch_hole) {489 offset = 0;490 memfd_size = total_size;491 chain->scrollable = false;492493 if (ftruncate(pool_fd, memfd_size) < 0) {494 LOG_ERRNO("failed to set size of SHM backing memory file");495 goto err;496 }497 }498499 real_mmapped = mmap(500 NULL, memfd_size, PROT_READ | PROT_WRITE,501 MAP_SHARED | MAP_UNINITIALIZED, pool_fd, 0);502503 if (real_mmapped == MAP_FAILED) {504 LOG_ERRNO("failed to mmap SHM backing memory file");505 goto err;506 }507508#if defined(MEMFD_CREATE)509 /* Seal file - we no longer allow any kind of resizing */510 /* TODO: wayland mmaps(PROT_WRITE), for some unknown reason, hence we cannot use F_SEAL_FUTURE_WRITE */511 if (fcntl(pool_fd, F_ADD_SEALS,512 F_SEAL_GROW | F_SEAL_SHRINK | /*F_SEAL_FUTURE_WRITE |*/ F_SEAL_SEAL) < 0)513 {514 LOG_ERRNO("failed to seal SHM backing memory file");515 /* This is not a fatal error */516 }517#endif518519 wl_pool = wl_shm_create_pool(chain->shm, pool_fd, memfd_size);520 if (wl_pool == NULL) {521 LOG_ERR("failed to create SHM pool");522 goto err;523 }524525 pool = xmalloc(sizeof(*pool));526 *pool = (struct buffer_pool){527 .fd = pool_fd,528 .wl_pool = wl_pool,529 .real_mmapped = real_mmapped,530 .mmap_size = memfd_size,531 .ref_count = 0,532 };533534 for (size_t i = 0; i < count; i++) {535 if (sizes[i] == 0) {536 bufs[i] = NULL;537 continue;538 }539540 /* Push to list of available buffers, but marked as 'busy' */541 struct buffer_private *buf = xmalloc(sizeof(*buf));542 *buf = (struct buffer_private){543 .public = {544 .width = widths[i],545 .height = heights[i],546 .stride = stride[i],547 .pix_instances = chain->pix_instances,548 .age = 1234, /* Force a full repaint */549 },550 .chain = chain,551 .ref_count = immediate_purge ? 0 : 1,552 .busy = true,553 .with_alpha = with_alpha,554 .pool = pool,555 .offset = 0,556 .size = sizes[i],557 .scrollable = chain->scrollable,558 };559560 if (!instantiate_offset(buf, offset)) {561 free(buf);562 goto err;563 }564565 if (immediate_purge)566 tll_push_front(deferred, buf);567 else568 tll_push_front(chain->bufs, buf);569570 buf->public.dirty = xmalloc(571 chain->pix_instances * sizeof(buf->public.dirty[0]));572573 for (size_t j = 0; j < chain->pix_instances; j++)574 pixman_region32_init(&buf->public.dirty[j]);575576 pool->ref_count++;577 offset += buf->size;578 bufs[i] = &buf->public;579 }580581#if defined(MEASURE_SHM_ALLOCS) && MEASURE_SHM_ALLOCS582 {583 size_t currently_alloced = 0;584 tll_foreach(buffers, it)585 currently_alloced += it->item.size;586 if (currently_alloced > max_alloced)587 max_alloced = currently_alloced;588 }589#endif590591 if (!(bufs[0] && shm_can_scroll(bufs[0]))) {592 /* We only need to keep the pool FD open if we're going to SHM593 * scroll it */594 close(pool_fd);595 pool->fd = -1;596 }597598 return;599600err:601 pool_unref(pool);602 if (wl_pool != NULL)603 wl_shm_pool_destroy(wl_pool);604 if (real_mmapped != MAP_FAILED)605 munmap(real_mmapped, memfd_size);606 if (pool_fd != -1)607 close(pool_fd);608609 /* We don't handle this */610 abort();611}612613void614shm_did_not_use_buf(struct buffer *_buf)615{616 struct buffer_private *buf = (struct buffer_private *)_buf;617 buf->busy = false;618}619620void621shm_get_many(struct buffer_chain *chain, size_t count,622 int widths[static count], int heights[static count],623 struct buffer *bufs[static count], bool with_alpha)624{625 get_new_buffers(chain, count, widths, heights, bufs, with_alpha, true);626}627628struct buffer *629shm_get_buffer(struct buffer_chain *chain, int width, int height, bool with_alpha)630{631 LOG_DBG(632 "chain=%p: looking for a reusable %dx%d buffer "633 "among %zu potential buffers",634 (void *)chain, width, height, tll_length(chain->bufs));635636 struct buffer_private *cached = NULL;637 tll_foreach(chain->bufs, it) {638 struct buffer_private *buf = it->item;639640 if (buf->public.width != width || buf->public.height != height ||641 with_alpha != buf->with_alpha)642 {643 LOG_DBG("purging mismatching buffer %p", (void *)buf);644 if (buffer_unref_no_remove_from_chain(buf))645 tll_remove(chain->bufs, it);646 continue;647 }648649 if (buf->busy)650 buf->public.age++;651 else652#if FORCED_DOUBLE_BUFFERING653 if (buf->age == 0)654 buf->age++;655 else656#endif657 {658 if (cached == NULL)659 cached = buf;660 else {661 /* We have multiple buffers eligible for662 * reuse. Pick the "youngest" one, and mark the663 * other one for purging */664 if (buf->public.age < cached->public.age) {665 shm_unref(&cached->public);666 cached = buf;667 } else {668 /*669 * TODO: I think we _can_ use shm_unref()670 * here...671 *672 * shm_unref() may remove 'it', but that673 * should be safe; "our" tll_foreach() already674 * holds the next pointer.675 */676 if (buffer_unref_no_remove_from_chain(buf))677 tll_remove(chain->bufs, it);678 }679 }680 }681 }682683 if (cached != NULL) {684 LOG_DBG("reusing buffer %p from cache", (void *)cached);685 cached->busy = true;686 for (size_t i = 0; i < cached->public.pix_instances; i++)687 pixman_region32_clear(&cached->public.dirty[i]);688 assert(cached->public.pix_instances == chain->pix_instances);689 return &cached->public;690 }691shm: silence compiler warningDaniel Eklöfbefore adoptiondfcc118
line 692why, one step back692 struct buffer *ret = NULL;changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 693–1033why, one step back693 get_new_buffers(chain, 1, &width, &height, &ret, with_alpha, false);694 return ret;695}696697bool698shm_can_scroll(const struct buffer *_buf)699{700#if __SIZEOF_POINTER__ == 8701 const struct buffer_private *buf = (const struct buffer_private *)_buf;702 return can_punch_hole && max_pool_size > 0 && buf->scrollable;703#else704 /* Not enough virtual address space in 32-bit */705 return false;706#endif707}708709#if __SIZEOF_POINTER__ == 8 && defined(FALLOC_FL_PUNCH_HOLE)710static bool711wrap_buffer(struct buffer_private *buf, off_t new_offset)712{713 struct buffer_pool *pool = buf->pool;714 assert(pool->ref_count == 1);715716 /* We don't allow overlapping offsets */717 off_t UNUSED diff = new_offset < buf->offset718 ? buf->offset - new_offset719 : new_offset - buf->offset;720 assert(diff > buf->size);721722 memcpy((uint8_t *)pool->real_mmapped + new_offset,723 buf->public.data,724 buf->size);725726 off_t trim_ofs, trim_len;727 if (new_offset > buf->offset) {728 /* Trim everything *before* the new offset */729 trim_ofs = 0;730 trim_len = new_offset;731 } else {732 /* Trim everything *after* the new buffer location */733 trim_ofs = new_offset + buf->size;734 trim_len = pool->mmap_size - trim_ofs;735 }736737 if (fallocate(738 pool->fd,739 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,740 trim_ofs, trim_len) < 0)741 {742 LOG_ERRNO("failed to trim SHM backing memory file");743 return false;744 }745746 /* Re-instantiate pixman+wl_buffer+raw pointersw */747 buffer_destroy_dont_close(&buf->public);748 return instantiate_offset(buf, new_offset);749}750751static bool752shm_scroll_forward(struct buffer_private *buf, int rows,753 int top_margin, int top_keep_rows,754 int bottom_margin, int bottom_keep_rows)755{756 struct buffer_pool *pool = buf->pool;757758 assert(can_punch_hole);759 assert(buf->busy);760 assert(buf->public.pix != NULL);761 assert(buf->public.wl_buf != NULL);762 assert(pool != NULL);763 assert(pool->ref_count == 1);764 assert(pool->fd >= 0);765766 LOG_DBG("scrolling %d rows (%d bytes)", rows, rows * buf->public.stride);767768 const off_t diff = rows * buf->public.stride;769 assert(rows > 0);770 assert(diff < buf->size);771772 if (buf->offset + diff + buf->size > max_pool_size) {773 LOG_DBG("memfd offset wrap around");774 if (!wrap_buffer(buf, 0))775 goto err;776 }777778 off_t new_offset = buf->offset + diff;779 assert(new_offset > buf->offset);780 assert(new_offset + buf->size <= max_pool_size);781782#if TIME_SCROLL783 struct timespec tot;784 struct timespec time1;785 clock_gettime(CLOCK_MONOTONIC, &time1);786787 struct timespec time2 = time1;788#endif789790 if (top_keep_rows > 0) {791 /* Copy current 'top' region to its new location */792 const int stride = buf->public.stride;793 uint8_t *base = buf->public.data;794795 memmove(796 base + (top_margin + rows) * stride,797 base + (top_margin + 0) * stride,798 top_keep_rows * stride);799800#if TIME_SCROLL801 clock_gettime(CLOCK_MONOTONIC, &time2);802 timespec_sub(&time2, &time1, &tot);803 LOG_INFO("memmove (top region): %lds %ldns",804 (long)tot.tv_sec, tot.tv_nsec);805#endif806 }807808 /* Destroy old objects (they point to the old offset) */809 buffer_destroy_dont_close(&buf->public);810811 /* Free unused memory - everything up until the new offset */812 const off_t trim_ofs = 0;813 const off_t trim_len = new_offset;814815 if (fallocate(816 pool->fd,817 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,818 trim_ofs, trim_len) < 0)819 {820 LOG_ERRNO("failed to trim SHM backing memory file");821 goto err;822 }823824#if TIME_SCROLL825 struct timespec time3;826 clock_gettime(CLOCK_MONOTONIC, &time3);827 timespec_sub(&time3, &time2, &tot);828 LOG_INFO("PUNCH HOLE: %lds %ldns", (long)tot.tv_sec, tot.tv_nsec);829#endif830831 /* Re-instantiate pixman+wl_buffer+raw pointersw */832 bool ret = instantiate_offset(buf, new_offset);833834#if TIME_SCROLL835 struct timespec time4;836 clock_gettime(CLOCK_MONOTONIC, &time4);837 timespec_sub(&time4, &time3, &tot);838 LOG_INFO("instantiate offset: %lds %ldns", (long)tot.tv_sec, tot.tv_nsec);839#endif840841 if (ret && bottom_keep_rows > 0) {842 /* Copy 'bottom' region to its new location */843 const size_t size = buf->size;844 const int stride = buf->public.stride;845 uint8_t *base = buf->public.data;846847 memmove(848 base + size - (bottom_margin + bottom_keep_rows) * stride,849 base + size - (bottom_margin + rows + bottom_keep_rows) * stride,850 bottom_keep_rows * stride);851852#if TIME_SCROLL853 struct timespec time5;854 clock_gettime(CLOCK_MONOTONIC, &time5);855856 timespec_sub(&time5, &time4, &tot);857 LOG_INFO("memmove (bottom region): %lds %ldns",858 (long)tot.tv_sec, tot.tv_nsec);859#endif860 }861862 return ret;863864err:865 abort();866 return false;867}868869static bool870shm_scroll_reverse(struct buffer_private *buf, int rows,871 int top_margin, int top_keep_rows,872 int bottom_margin, int bottom_keep_rows)873{874 assert(rows > 0);875876 struct buffer_pool *pool = buf->pool;877 assert(pool->ref_count == 1);878879 const off_t diff = rows * buf->public.stride;880 if (diff > buf->offset) {881 LOG_DBG("memfd offset reverse wrap-around");882 if (!wrap_buffer(buf, (max_pool_size - buf->size) & ~(page_size() - 1)))883 goto err;884 }885886 off_t new_offset = buf->offset - diff;887 assert(new_offset < buf->offset);888 assert(new_offset <= max_pool_size);889890#if TIME_SCROLL891 struct timespec time0;892 clock_gettime(CLOCK_MONOTONIC, &time0);893894 struct timespec tot;895 struct timespec time1 = time0;896#endif897898 if (bottom_keep_rows > 0) {899 /* Copy 'bottom' region to its new location */900 const size_t size = buf->size;901 const int stride = buf->public.stride;902 uint8_t *base = buf->public.data;903904 memmove(905 base + size - (bottom_margin + rows + bottom_keep_rows) * stride,906 base + size - (bottom_margin + bottom_keep_rows) * stride,907 bottom_keep_rows * stride);908909#if TIME_SCROLL910 clock_gettime(CLOCK_MONOTONIC, &time1);911 timespec_sub(&time1, &time0, &tot);912 LOG_INFO("memmove (bottom region): %lds %ldns",913 (long)tot.tv_sec, tot.tv_nsec);914#endif915 }916917 /* Destroy old objects (they point to the old offset) */918 buffer_destroy_dont_close(&buf->public);919920 /* Free unused memory - everything after the relocated buffer */921 const off_t trim_ofs = new_offset + buf->size;922 const off_t trim_len = pool->mmap_size - trim_ofs;923924 if (fallocate(925 pool->fd,926 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,927 trim_ofs, trim_len) < 0)928 {929 LOG_ERRNO("failed to trim SHM backing memory");930 goto err;931 }932#if TIME_SCROLL933 struct timespec time2;934 clock_gettime(CLOCK_MONOTONIC, &time2);935 timespec_sub(&time2, &time1, &tot);936 LOG_INFO("fallocate: %lds %ldns", (long)tot.tv_sec, tot.tv_nsec);937#endif938939 /* Re-instantiate pixman+wl_buffer+raw pointers */940 bool ret = instantiate_offset(buf, new_offset);941942#if TIME_SCROLL943 struct timespec time3;944 clock_gettime(CLOCK_MONOTONIC, &time3);945 timespec_sub(&time3, &time2, &tot);946 LOG_INFO("instantiate offset: %lds %ldns", (long)tot.tv_sec, tot.tv_nsec);947#endif948949 if (ret && top_keep_rows > 0) {950 /* Copy current 'top' region to its new location */951 const int stride = buf->public.stride;952 uint8_t *base = buf->public.data;953954 memmove(955 base + (top_margin + 0) * stride,956 base + (top_margin + rows) * stride,957 top_keep_rows * stride);958959#if TIME_SCROLL960 struct timespec time4;961 clock_gettime(CLOCK_MONOTONIC, &time4);962 timespec_sub(&time4, &time3, &tot);963 LOG_INFO("memmove (top region): %lds %ldns",964 (long)tot.tv_sec, tot.tv_nsec);965#endif966 }967968 return ret;969970err:971 abort();972 return false;973}974#endif /* FALLOC_FL_PUNCH_HOLE */975976bool977shm_scroll(struct buffer *_buf, int rows,978 int top_margin, int top_keep_rows,979 int bottom_margin, int bottom_keep_rows)980{981#if __SIZEOF_POINTER__ == 8 && defined(FALLOC_FL_PUNCH_HOLE)982 if (!shm_can_scroll(_buf))983 return false;984985 struct buffer_private *buf = (struct buffer_private *)_buf;986987 assert(rows != 0);988 return rows > 0989 ? shm_scroll_forward(buf, rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows)990 : shm_scroll_reverse(buf, -rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows);991#else992 return false;993#endif994}995996void997shm_purge(struct buffer_chain *chain)998{999 LOG_DBG("chain: %p: purging all buffers", (void *)chain);10001001 /* Purge old buffers associated with this cookie */1002 tll_foreach(chain->bufs, it) {1003 if (buffer_unref_no_remove_from_chain(it->item))1004 tll_remove(chain->bufs, it);1005 }1006}10071008void1009shm_addref(struct buffer *_buf)1010{1011 struct buffer_private *buf = (struct buffer_private *)_buf;1012 buf->ref_count++;1013}10141015void1016shm_unref(struct buffer *_buf)1017{1018 if (_buf == NULL)1019 return;10201021 struct buffer_private *buf = (struct buffer_private *)_buf;1022 struct buffer_chain *chain = buf->chain;10231024 tll_foreach(chain->bufs, it) {1025 if (it->item != buf)1026 continue;10271028 if (buffer_unref_no_remove_from_chain(buf))1029 tll_remove(chain->bufs, it);1030 break;1031 }1032}1033Implement gamma-correct blendingDaniel Eklöfbefore adoption2d19826
lines 1034–1053why, one step back1034static enum wl_shm_format1035pix_fmt_to_shm_fmt(pixman_format_code_t pix_fmt)1036{1037 switch (pix_fmt) {1038 default: assert(false); break;10391040 case PIXMAN_a8r8g8b8: return WL_SHM_FORMAT_ARGB8888;1041 case PIXMAN_x8r8g8b8: return WL_SHM_FORMAT_XRGB8888;1042 case PIXMAN_a8b8g8r8: return WL_SHM_FORMAT_ABGR8888;1043 case PIXMAN_x8b8g8r8: return WL_SHM_FORMAT_XBGR8888;1044 case PIXMAN_a2r10g10b10: return WL_SHM_FORMAT_ARGB2101010;1045 case PIXMAN_x2r10g10b10: return WL_SHM_FORMAT_XRGB2101010;1046 case PIXMAN_a2b10g10r10: return WL_SHM_FORMAT_ABGR2101010;1047 case PIXMAN_x2b10g10r10: return WL_SHM_FORMAT_XBGR2101010;1048 case PIXMAN_a16b16g16r16: return WL_SHM_FORMAT_ABGR16161616;1049 }10501051 return WL_SHM_FORMAT_ARGB8888;1052}1053changelog: add missing refDaniel Eklöfbefore adoption6a4c416
line 1054why, one step back1054struct buffer_chain *Implement gamma-correct blendingDaniel Eklöfbefore adoption2d19826
lines 1055–1057why, one step back1055shm_chain_new(struct wl_shm *shm, bool scrollable, size_t pix_instances,1056 pixman_format_code_t pix_fmt_with_alpha,1057 pixman_format_code_t pix_fmt_without_alpha)changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1058–1064why, one step back1058{1059 struct buffer_chain *chain = xmalloc(sizeof(*chain));1060 *chain = (struct buffer_chain){1061 .bufs = tll_init(),1062 .shm = shm,1063 .pix_instances = pix_instances,1064 .scrollable = scrollable,Implement gamma-correct blendingDaniel Eklöfbefore adoption2d19826
lines 1065–1068why, one step back1065 .pix_fmt_with_alpha = pix_fmt_with_alpha,1066 .pix_fmt_without_alpha = pix_fmt_without_alpha,1067 .wl_shm_fmt_with_alpha = pix_fmt_to_shm_fmt(pix_fmt_with_alpha),1068 .wl_shm_fmt_without_alpha = pix_fmt_to_shm_fmt(pix_fmt_without_alpha),changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1069–1087why, one step back1069 };1070 return chain;1071}10721073void1074shm_chain_free(struct buffer_chain *chain)1075{1076 if (chain == NULL)1077 return;10781079 shm_purge(chain);10801081 if (tll_length(chain->bufs) > 0) {1082 BUG("chain=%p: there are buffers remaining; "1083 "is there a missing call to shm_unref()?", (void *)chain);1084 }10851086 free(chain);1087}