[client] util: add function for merging overlapping rectangles

This will be used to merge overlapping rectangles in order to avoid copying
the same rectangles multiple times.
This commit is contained in:
Quantum 2021-07-13 02:45:15 -04:00 committed by Geoffrey McRae
parent 92706caddc
commit 12cb3e512f
2 changed files with 47 additions and 0 deletions

View File

@ -25,6 +25,9 @@
#include <stdbool.h>
#include "common/types.h"
#define min(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
#define max(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
// reads the specified file into a new buffer
// the callee must free the buffer
bool util_fileGetContents(const char * filename, char ** buffer, size_t * length);
@ -34,6 +37,7 @@ bool util_guestCurToLocal(struct DoublePoint *local);
void util_localCurToGuest(struct DoublePoint *guest);
void util_rotatePoint(struct DoublePoint *point);
bool util_hasGLExt(const char * exts, const char * ext);
int util_mergeOverlappingRects(FrameDamageRect * out, const FrameDamageRect * rects, int count);
static inline double util_clamp(double x, double min, double max)
{

View File

@ -215,3 +215,46 @@ bool util_hasGLExt(const char * exts, const char * ext)
{
return str_containsValue(exts, ' ', ext);
}
static bool rectIntersects(const FrameDamageRect * r1, const FrameDamageRect * r2)
{
return r1->x < r2->x + r2->width &&
r1->x + r1->width > r2->x &&
r1->y < r2->y + r2->height &&
r2->y + r1->height > r2->y;
}
int util_mergeOverlappingRects(FrameDamageRect * out, const FrameDamageRect * rects, int count)
{
bool removed[count];
bool changed;
memset(removed, 0, sizeof(removed));
memcpy(out, rects, count * sizeof(FrameDamageRect));
do
{
changed = false;
for (int i = 0; i < count; ++i)
if (!removed[i])
for (int j = i + 1; j < count; ++j)
if (!removed[j] && rectIntersects(out + i, out + j))
{
uint32_t x2 = max(out[i].x + out[i].width, out[j].x + out[j].width);
uint32_t y2 = max(out[i].y + out[i].height, out[j].y + out[j].height);
out[i].x = min(out[i].x, out[j].x);
out[i].y = min(out[i].y, out[j].y);
out[i].width = x2 - out[i].x;
out[i].height = y2 - out[i].y;
removed[j] = true;
changed = true;
}
}
while (changed);
int o = 0;
for (int i = 0; i < count; ++i)
if (!removed[i])
out[o++] = out[i];
return o;
}