2020-04-12 13:18:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 - 2020 Andri Yngvason
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-09-15 20:44:49 +00:00
|
|
|
#include "neatvnc.h"
|
2019-10-07 17:39:54 +00:00
|
|
|
#include "fb.h"
|
2019-09-15 20:44:49 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pixman.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <libdrm/drm_fourcc.h>
|
2020-03-16 20:09:22 +00:00
|
|
|
#include <aml.h>
|
2019-09-15 20:44:49 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#define EXPORT __attribute__((visibility("default")))
|
|
|
|
#define ALIGN_DOWN(a, b) (((a) / (b)) * (b))
|
|
|
|
|
|
|
|
struct damage_check {
|
2020-03-16 20:09:22 +00:00
|
|
|
struct aml_work* work;
|
2020-01-29 21:33:13 +00:00
|
|
|
struct nvnc_fb* fb0;
|
|
|
|
struct nvnc_fb* fb1;
|
2019-09-15 20:44:49 +00:00
|
|
|
int x_hint;
|
|
|
|
int y_hint;
|
|
|
|
int width_hint;
|
|
|
|
int height_hint;
|
|
|
|
nvnc_damage_fn on_done;
|
|
|
|
struct pixman_region16 damage;
|
2019-10-20 22:13:51 +00:00
|
|
|
void* userdata;
|
2019-09-15 20:44:49 +00:00
|
|
|
};
|
|
|
|
|
2019-10-20 22:13:51 +00:00
|
|
|
static bool fbs_are_compatible(const struct nvnc_fb* fb0,
|
|
|
|
const struct nvnc_fb* fb1)
|
2019-09-15 20:44:49 +00:00
|
|
|
{
|
2019-10-20 22:13:51 +00:00
|
|
|
return fb0->fourcc_format == fb1->fourcc_format &&
|
|
|
|
fb0->width == fb1->width && fb0->height == fb1->height;
|
2019-09-15 20:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool are_tiles_equal(const uint32_t* a, const uint32_t* b,
|
2019-10-20 22:13:51 +00:00
|
|
|
int stride, int width, int height)
|
2019-09-15 20:44:49 +00:00
|
|
|
{
|
|
|
|
for (int y = 0; y < height; ++y)
|
|
|
|
if (memcmp(a + y * stride, b + y * stride, width * 4) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TILE_SIDE_LENGTH 32
|
2020-05-17 23:42:55 +00:00
|
|
|
static int check_damage_linear(struct pixman_region16* damage,
|
|
|
|
const struct nvnc_fb* fb0,
|
|
|
|
const struct nvnc_fb* fb1,
|
|
|
|
int x_hint, int y_hint,
|
|
|
|
int width_hint, int height_hint)
|
2019-09-15 20:44:49 +00:00
|
|
|
{
|
2019-10-20 22:13:51 +00:00
|
|
|
uint32_t* b0 = fb0->addr;
|
|
|
|
uint32_t* b1 = fb1->addr;
|
2019-09-15 20:44:49 +00:00
|
|
|
|
|
|
|
int width = fb0->width;
|
|
|
|
int height = fb0->height;
|
|
|
|
|
|
|
|
assert(x_hint + width_hint <= width);
|
|
|
|
assert(y_hint + height_hint <= height);
|
|
|
|
|
|
|
|
int x_start = ALIGN_DOWN(x_hint, TILE_SIDE_LENGTH);
|
|
|
|
int y_start = ALIGN_DOWN(y_hint, TILE_SIDE_LENGTH);
|
2019-10-20 22:13:51 +00:00
|
|
|
|
2019-12-27 18:04:36 +00:00
|
|
|
width_hint += x_hint - x_start;
|
|
|
|
height_hint += y_hint - y_start;
|
|
|
|
|
2019-10-20 22:13:51 +00:00
|
|
|
for (int y = y_start; y < y_start + height_hint; y += TILE_SIDE_LENGTH) {
|
2019-09-15 20:44:49 +00:00
|
|
|
int tile_height = MIN(TILE_SIDE_LENGTH, height - y);
|
|
|
|
|
|
|
|
for (int x = x_start; x < x_start + width_hint;
|
|
|
|
x += TILE_SIDE_LENGTH) {
|
|
|
|
int tile_width = MIN(TILE_SIDE_LENGTH, width - x);
|
|
|
|
|
2019-10-13 10:05:36 +00:00
|
|
|
int offset = x + y * width;
|
2019-09-19 19:21:52 +00:00
|
|
|
|
2019-10-20 22:13:51 +00:00
|
|
|
if (are_tiles_equal(b0 + offset, b1 + offset, width,
|
|
|
|
tile_width, tile_height))
|
2019-09-15 20:44:49 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
pixman_region_union_rect(damage, damage, x, y,
|
2019-10-20 22:13:51 +00:00
|
|
|
tile_width, tile_height);
|
2019-09-15 20:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#undef TILE_SIDE_LENGTH
|
|
|
|
|
2020-05-17 23:42:55 +00:00
|
|
|
static void do_damage_check_linear(void* work)
|
2019-09-15 20:44:49 +00:00
|
|
|
{
|
2020-03-16 20:09:22 +00:00
|
|
|
struct damage_check* check = aml_get_userdata(work);
|
2019-09-15 20:44:49 +00:00
|
|
|
|
|
|
|
check_damage_linear(&check->damage, check->fb0, check->fb1,
|
2019-10-20 22:13:51 +00:00
|
|
|
check->x_hint, check->y_hint, check->width_hint,
|
|
|
|
check->height_hint);
|
2019-09-15 20:44:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:42:55 +00:00
|
|
|
static void on_damage_check_done_linear(void* work)
|
2019-09-15 20:44:49 +00:00
|
|
|
{
|
2020-03-16 20:09:22 +00:00
|
|
|
struct damage_check* check = aml_get_userdata(work);
|
2019-09-15 20:44:49 +00:00
|
|
|
|
|
|
|
check->on_done(&check->damage, check->userdata);
|
|
|
|
|
2020-01-29 21:33:13 +00:00
|
|
|
nvnc_fb_unref(check->fb1);
|
|
|
|
nvnc_fb_unref(check->fb0);
|
|
|
|
|
2019-09-15 20:44:49 +00:00
|
|
|
pixman_region_fini(&check->damage);
|
|
|
|
}
|
|
|
|
|
2020-05-17 23:42:55 +00:00
|
|
|
static int check_damage_linear_threaded(struct nvnc_fb* fb0,
|
|
|
|
struct nvnc_fb* fb1,
|
|
|
|
int x_hint, int y_hint,
|
|
|
|
int width_hint, int height_hint,
|
|
|
|
nvnc_damage_fn on_check_done,
|
|
|
|
void* userdata)
|
2019-09-15 20:44:49 +00:00
|
|
|
{
|
2019-10-20 22:13:51 +00:00
|
|
|
struct damage_check* work = calloc(1, sizeof(*work));
|
2019-09-15 20:44:49 +00:00
|
|
|
if (!work)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
work->on_done = on_check_done;
|
|
|
|
work->userdata = userdata;
|
|
|
|
work->fb0 = fb0;
|
|
|
|
work->fb1 = fb1;
|
|
|
|
work->x_hint = x_hint;
|
|
|
|
work->y_hint = y_hint;
|
|
|
|
work->width_hint = width_hint;
|
|
|
|
work->height_hint = height_hint;
|
|
|
|
pixman_region_init(&work->damage);
|
|
|
|
|
|
|
|
/* TODO: Spread the work into more tasks */
|
2020-03-16 20:09:22 +00:00
|
|
|
struct aml_work* obj =
|
|
|
|
aml_work_new(do_damage_check_linear, on_damage_check_done_linear,
|
|
|
|
work, free);
|
|
|
|
if (!obj) {
|
2019-09-15 20:44:49 +00:00
|
|
|
free(work);
|
2020-03-16 20:09:22 +00:00
|
|
|
return -1;
|
2020-01-29 21:33:13 +00:00
|
|
|
}
|
2019-09-15 20:44:49 +00:00
|
|
|
|
2020-03-16 20:09:22 +00:00
|
|
|
int rc = aml_start(aml_get_default(), obj);
|
|
|
|
aml_unref(obj);
|
|
|
|
if (rc < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
work->work = obj;
|
|
|
|
|
|
|
|
nvnc_fb_ref(fb0);
|
|
|
|
nvnc_fb_ref(fb1);
|
|
|
|
|
|
|
|
return 0;
|
2019-09-15 20:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT
|
2020-01-29 21:33:13 +00:00
|
|
|
int nvnc_check_damage(struct nvnc_fb* fb0, struct nvnc_fb* fb1,
|
2019-10-20 22:13:51 +00:00
|
|
|
int x_hint, int y_hint, int width_hint, int height_hint,
|
|
|
|
nvnc_damage_fn on_check_done, void* userdata)
|
2019-09-15 20:44:49 +00:00
|
|
|
{
|
|
|
|
if (!fbs_are_compatible(fb0, fb1))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (fb0->fourcc_modifier) {
|
|
|
|
case DRM_FORMAT_MOD_LINEAR:
|
|
|
|
return check_damage_linear_threaded(fb0, fb1, x_hint, y_hint,
|
2019-10-20 22:13:51 +00:00
|
|
|
width_hint, height_hint,
|
|
|
|
on_check_done, userdata);
|
2019-09-15 20:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|