| 1 |
|
| 2 |
|
| 3 |
#pragma once |
| 4 |
#include <pixman.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <xcb/xcb.h> |
| 7 |
|
| 8 |
#include <picom/types.h> |
| 9 |
|
| 10 |
#include "log.h" |
| 11 |
#include "utils/misc.h" |
| 12 |
|
| 13 |
typedef struct pixman_region32 pixman_region32_t; |
| 14 |
typedef struct pixman_box32 pixman_box32_t; |
| 15 |
typedef pixman_region32_t region_t; |
| 16 |
typedef pixman_box32_t rect_t; |
| 17 |
|
| 18 |
static inline void region_free(region_t *region) { |
| 19 |
if (region) { |
| 20 |
pixman_region32_fini(region); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
#define scoped_region_t cleanup(region_free) region_t |
| 25 |
|
| 26 |
static inline void dump_region(const region_t *x) { |
| 27 |
if (log_get_level_tls() > LOG_LEVEL_TRACE) { |
| 28 |
return; |
| 29 |
} |
| 30 |
int nrects; |
| 31 |
const rect_t *rects = pixman_region32_rectangles((region_t *)x, &nrects); |
| 32 |
log_trace("nrects: %d", nrects); |
| 33 |
for (int i = 0; i < nrects; i++) { |
| 34 |
log_trace("(%d, %d) - (%d, %d)", rects[i].x1, rects[i].y1, rects[i].x2, |
| 35 |
rects[i].y2); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
static inline void log_region_(enum log_level level, const char *func, const region_t *x) { |
| 40 |
if (level < log_get_level_tls()) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
int nrects; |
| 45 |
const rect_t *rects = pixman_region32_rectangles((region_t *)x, &nrects); |
| 46 |
if (nrects == 0) { |
| 47 |
log_printf(tls_logger, level, func, "\t(empty)"); |
| 48 |
return; |
| 49 |
} |
| 50 |
for (int i = 0; i < min2(nrects, 3); i++) { |
| 51 |
log_printf(tls_logger, level, func, "\t(%d, %d) - (%d, %d)", rects[i].x1, |
| 52 |
rects[i].y1, rects[i].x2, rects[i].y2); |
| 53 |
} |
| 54 |
if (nrects > 3) { |
| 55 |
auto extent = pixman_region32_extents(x); |
| 56 |
log_printf(tls_logger, level, func, "\t..."); |
| 57 |
log_printf(tls_logger, level, func, "\ttotal: (%d, %d) - (%d, %d)", |
| 58 |
extent->x1, extent->y1, extent->x2, extent->y2); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
#define log_region(level, x) log_region_(LOG_LEVEL_##level, __func__, x) |
| 63 |
|
| 64 |
|
| 65 |
static inline rect_t from_x_rect(const xcb_rectangle_t *rect) { |
| 66 |
return (rect_t){ |
| 67 |
.x1 = rect->x, |
| 68 |
.y1 = rect->y, |
| 69 |
.x2 = rect->x + rect->width, |
| 70 |
.y2 = rect->y + rect->height, |
| 71 |
}; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static inline rect_t *from_x_rects(int nrects, const xcb_rectangle_t *rects) { |
| 77 |
rect_t *ret = ccalloc(nrects, rect_t); |
| 78 |
for (int i = 0; i < nrects; i++) { |
| 79 |
ret[i] = from_x_rect(rects + i); |
| 80 |
} |
| 81 |
return ret; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static inline void |
| 88 |
resize_region_inner(const region_t *region, region_t *output, int dx, int dy) { |
| 89 |
if (!region || !output) { |
| 90 |
return; |
| 91 |
} |
| 92 |
if (!dx && !dy) { |
| 93 |
if (region != output) { |
| 94 |
pixman_region32_copy(output, (region_t *)region); |
| 95 |
} |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
int nrects; |
| 100 |
int nnewrects = 0; |
| 101 |
const rect_t *rects = pixman_region32_rectangles((region_t *)region, &nrects); |
| 102 |
rect_t *newrects = calloc((size_t)nrects, sizeof(rect_t)); |
| 103 |
for (int i = 0; i < nrects; i++) { |
| 104 |
int x1 = rects[i].x1 - dx; |
| 105 |
int y1 = rects[i].y1 - dy; |
| 106 |
int x2 = rects[i].x2 + dx; |
| 107 |
int y2 = rects[i].y2 + dy; |
| 108 |
int wid = x2 - x1; |
| 109 |
int hei = y2 - y1; |
| 110 |
if (wid <= 0 || hei <= 0) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
newrects[nnewrects] = (rect_t){.x1 = x1, .x2 = x2, .y1 = y1, .y2 = y2}; |
| 114 |
++nnewrects; |
| 115 |
} |
| 116 |
|
| 117 |
pixman_region32_fini(output); |
| 118 |
pixman_region32_init_rects(output, newrects, nnewrects); |
| 119 |
|
| 120 |
free(newrects); |
| 121 |
} |
| 122 |
|
| 123 |
static inline region_t resize_region(const region_t *region, int dx, int dy) { |
| 124 |
region_t ret; |
| 125 |
pixman_region32_init(&ret); |
| 126 |
resize_region_inner(region, &ret, dx, dy); |
| 127 |
return ret; |
| 128 |
} |
| 129 |
|
| 130 |
static inline void resize_region_in_place(region_t *region, int dx, int dy) { |
| 131 |
return resize_region_inner(region, region, dx, dy); |
| 132 |
} |
| 133 |
|
| 134 |
static inline rect_t region_translate_rect(rect_t rect, ivec2 origin) { |
| 135 |
return (rect_t){ |
| 136 |
.x1 = rect.x1 + origin.x, |
| 137 |
.y1 = rect.y1 + origin.y, |
| 138 |
.x2 = rect.x2 + origin.x, |
| 139 |
.y2 = rect.y2 + origin.y, |
| 140 |
}; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
static inline void region_subtract(region_t *region, ivec2 origin, const region_t *other) { |
| 145 |
pixman_region32_translate(region, -origin.x, -origin.y); |
| 146 |
pixman_region32_subtract(region, region, other); |
| 147 |
pixman_region32_translate(region, origin.x, origin.y); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
static inline void region_union(region_t *region, ivec2 origin, const region_t *other) { |
| 152 |
pixman_region32_translate(region, -origin.x, -origin.y); |
| 153 |
pixman_region32_union(region, region, other); |
| 154 |
pixman_region32_translate(region, origin.x, origin.y); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
static inline void region_intersect(region_t *region, ivec2 origin, const region_t *other) { |
| 159 |
pixman_region32_translate(region, -origin.x, -origin.y); |
| 160 |
pixman_region32_intersect(region, region, other); |
| 161 |
pixman_region32_translate(region, origin.x, origin.y); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
static inline void region_scale(region_t *region, ivec2 origin, vec2 scale) { |
| 167 |
if (vec2_eq(scale, SCALE_IDENTITY)) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
int n; |
| 172 |
region_t tmp = *region; |
| 173 |
auto r = pixman_region32_rectangles(&tmp, &n); |
| 174 |
for (int i = 0; i < n; i++) { |
| 175 |
r[i].x1 = to_i32_saturated(floor((r[i].x1 - origin.x) * scale.x + origin.x)); |
| 176 |
r[i].y1 = to_i32_saturated(floor((r[i].y1 - origin.y) * scale.y + origin.y)); |
| 177 |
r[i].x2 = to_i32_saturated(ceil((r[i].x2 - origin.x) * scale.x + origin.x)); |
| 178 |
r[i].y2 = to_i32_saturated(ceil((r[i].y2 - origin.y) * scale.y + origin.y)); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
pixman_region32_init_rects(region, r, n); |
| 185 |
pixman_region32_fini(&tmp); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
static inline void |
| 193 |
region_symmetric_difference_local(region_t *result, region_t *scratch, |
| 194 |
const region_t *region1, const region_t *region2) { |
| 195 |
pixman_region32_copy(scratch, region1); |
| 196 |
pixman_region32_subtract(scratch, scratch, region2); |
| 197 |
pixman_region32_union(result, result, scratch); |
| 198 |
|
| 199 |
pixman_region32_copy(scratch, region2); |
| 200 |
pixman_region32_subtract(scratch, scratch, region1); |
| 201 |
pixman_region32_union(result, result, scratch); |
| 202 |
} |
| 203 |
|
| 204 |
static inline region_t region_from_box(struct ibox a) { |
| 205 |
region_t ret; |
| 206 |
unsigned width = (unsigned)(min2(INT_MAX - a.origin.x, a.size.width)), |
| 207 |
height = (unsigned)(min2(INT_MAX - a.origin.y, a.size.height)); |
| 208 |
pixman_region32_init_rect(&ret, a.origin.x, a.origin.y, width, height); |
| 209 |
return ret; |
| 210 |
} |