2019-10-08 22:05:54 +00:00
|
|
|
/*
|
2020-01-14 20:42:48 +00:00
|
|
|
* Copyright (c) 2019 - 2020 Andri Yngvason
|
2019-10-08 22:05:54 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
2019-10-10 20:48:01 +00:00
|
|
|
#include <sys/mman.h>
|
2019-12-27 14:41:54 +00:00
|
|
|
#include <wayland-client-protocol.h>
|
2019-10-08 22:05:54 +00:00
|
|
|
#include <wayland-client.h>
|
2019-10-10 22:32:54 +00:00
|
|
|
#include <libdrm/drm_fourcc.h>
|
2020-03-16 22:01:43 +00:00
|
|
|
#include <aml.h>
|
2022-04-14 17:08:21 +00:00
|
|
|
#include <neatvnc.h>
|
2019-10-08 22:05:54 +00:00
|
|
|
|
2020-06-20 21:07:58 +00:00
|
|
|
#include "wlr-screencopy-unstable-v1.h"
|
|
|
|
#include "buffer.h"
|
2019-10-08 22:05:54 +00:00
|
|
|
#include "shm.h"
|
|
|
|
#include "screencopy.h"
|
2020-01-14 20:40:57 +00:00
|
|
|
#include "smooth.h"
|
2019-12-29 11:47:31 +00:00
|
|
|
#include "time-util.h"
|
2020-04-02 00:34:00 +00:00
|
|
|
#include "usdt.h"
|
2020-07-16 18:10:12 +00:00
|
|
|
#include "pixels.h"
|
2020-07-26 14:52:00 +00:00
|
|
|
#include "config.h"
|
2019-12-29 11:47:31 +00:00
|
|
|
|
2020-01-14 20:40:57 +00:00
|
|
|
#define DELAY_SMOOTHER_TIME_CONSTANT 0.5 // s
|
2019-10-08 22:05:54 +00:00
|
|
|
|
2020-07-19 14:37:48 +00:00
|
|
|
static void screencopy__stop(struct screencopy* self)
|
2019-10-08 22:05:54 +00:00
|
|
|
{
|
2020-03-16 22:01:43 +00:00
|
|
|
aml_stop(aml_get_default(), self->timer);
|
2020-01-14 20:12:58 +00:00
|
|
|
|
2020-06-26 21:44:58 +00:00
|
|
|
self->status = SCREENCOPY_STOPPED;
|
2020-04-04 15:01:38 +00:00
|
|
|
|
2019-10-08 22:05:54 +00:00
|
|
|
if (self->frame) {
|
|
|
|
zwlr_screencopy_frame_v1_destroy(self->frame);
|
|
|
|
self->frame = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 14:37:48 +00:00
|
|
|
void screencopy_stop(struct screencopy* self)
|
|
|
|
{
|
|
|
|
if (self->front)
|
|
|
|
wv_buffer_pool_release(self->pool, self->front);
|
|
|
|
self->front = NULL;
|
|
|
|
|
|
|
|
return screencopy__stop(self);
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:04:57 +00:00
|
|
|
static void screencopy_linux_dmabuf(void* data,
|
2019-10-08 22:05:54 +00:00
|
|
|
struct zwlr_screencopy_frame_v1* frame,
|
2020-06-23 23:04:57 +00:00
|
|
|
uint32_t format, uint32_t width, uint32_t height)
|
2019-10-08 22:05:54 +00:00
|
|
|
{
|
2020-07-17 16:27:46 +00:00
|
|
|
#ifdef ENABLE_SCREENCOPY_DMABUF
|
2019-10-08 22:05:54 +00:00
|
|
|
struct screencopy* self = data;
|
|
|
|
|
2020-07-26 14:52:00 +00:00
|
|
|
if (!(wv_buffer_get_available_types() & WV_BUFFER_DMABUF))
|
|
|
|
return;
|
|
|
|
|
2020-06-23 23:04:57 +00:00
|
|
|
self->have_linux_dmabuf = true;
|
|
|
|
self->dmabuf_width = width;
|
|
|
|
self->dmabuf_height = height;
|
|
|
|
self->fourcc = format;
|
2020-07-17 16:27:46 +00:00
|
|
|
#endif
|
2020-06-23 23:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void screencopy_buffer_done(void* data,
|
|
|
|
struct zwlr_screencopy_frame_v1* frame)
|
|
|
|
{
|
|
|
|
struct screencopy* self = data;
|
|
|
|
uint32_t width, height, stride, fourcc;
|
2020-07-16 18:10:12 +00:00
|
|
|
enum wv_buffer_type type = WV_BUFFER_UNSPEC;
|
2020-06-23 23:04:57 +00:00
|
|
|
|
2020-07-26 14:52:00 +00:00
|
|
|
#ifdef ENABLE_SCREENCOPY_DMABUF
|
2022-07-09 17:34:05 +00:00
|
|
|
if (self->have_linux_dmabuf && self->enable_linux_dmabuf) {
|
2020-06-23 23:04:57 +00:00
|
|
|
width = self->dmabuf_width;
|
|
|
|
height = self->dmabuf_height;
|
|
|
|
stride = 0;
|
|
|
|
fourcc = self->fourcc;
|
2020-07-16 18:10:12 +00:00
|
|
|
type = WV_BUFFER_DMABUF;
|
2020-07-26 14:52:00 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2020-06-23 23:04:57 +00:00
|
|
|
width = self->wl_shm_width;
|
|
|
|
height = self->wl_shm_height;
|
|
|
|
stride = self->wl_shm_stride;
|
2020-07-16 18:10:12 +00:00
|
|
|
fourcc = fourcc_from_wl_shm(self->wl_shm_format);
|
|
|
|
type = WV_BUFFER_SHM;
|
2020-06-23 23:04:57 +00:00
|
|
|
}
|
2020-06-20 21:07:58 +00:00
|
|
|
|
2020-07-16 18:10:12 +00:00
|
|
|
wv_buffer_pool_resize(self->pool, type, width, height, stride, fourcc);
|
|
|
|
|
2020-06-20 21:07:58 +00:00
|
|
|
struct wv_buffer* buffer = wv_buffer_pool_acquire(self->pool);
|
|
|
|
if (!buffer) {
|
2020-07-19 14:37:48 +00:00
|
|
|
screencopy__stop(self);
|
2020-06-26 21:44:58 +00:00
|
|
|
self->status = SCREENCOPY_FATAL;
|
|
|
|
self->on_done(self);
|
2020-06-23 23:04:57 +00:00
|
|
|
return;
|
2019-10-08 22:05:54 +00:00
|
|
|
}
|
2019-10-09 21:59:13 +00:00
|
|
|
|
2020-06-20 21:07:58 +00:00
|
|
|
assert(!self->front);
|
|
|
|
self->front = buffer;
|
|
|
|
|
2020-04-04 15:01:38 +00:00
|
|
|
if (self->is_immediate_copy)
|
2020-06-20 21:07:58 +00:00
|
|
|
zwlr_screencopy_frame_v1_copy(self->frame, buffer->wl_buffer);
|
2020-04-04 15:01:38 +00:00
|
|
|
else
|
|
|
|
zwlr_screencopy_frame_v1_copy_with_damage(self->frame,
|
2020-06-20 21:07:58 +00:00
|
|
|
buffer->wl_buffer);
|
2019-10-08 22:05:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 23:04:57 +00:00
|
|
|
static void screencopy_buffer(void* data,
|
|
|
|
struct zwlr_screencopy_frame_v1* frame,
|
|
|
|
enum wl_shm_format format, uint32_t width,
|
|
|
|
uint32_t height, uint32_t stride)
|
|
|
|
{
|
|
|
|
struct screencopy* self = data;
|
|
|
|
|
|
|
|
self->wl_shm_format = format;
|
|
|
|
self->wl_shm_width = width;
|
|
|
|
self->wl_shm_height = height;
|
|
|
|
self->wl_shm_stride = stride;
|
|
|
|
|
2021-12-15 23:23:21 +00:00
|
|
|
int version = zwlr_screencopy_manager_v1_get_version(self->manager);
|
|
|
|
if (version < 3) {
|
2020-06-23 23:04:57 +00:00
|
|
|
self->have_linux_dmabuf = false;
|
|
|
|
screencopy_buffer_done(data, frame);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 22:05:54 +00:00
|
|
|
static void screencopy_flags(void* data,
|
|
|
|
struct zwlr_screencopy_frame_v1* frame,
|
|
|
|
uint32_t flags)
|
|
|
|
{
|
|
|
|
(void)frame;
|
|
|
|
|
2020-06-28 14:26:53 +00:00
|
|
|
struct screencopy* self = data;
|
|
|
|
|
|
|
|
self->front->y_inverted =
|
|
|
|
!!(flags & ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT);
|
2019-10-08 22:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void screencopy_ready(void* data,
|
|
|
|
struct zwlr_screencopy_frame_v1* frame,
|
|
|
|
uint32_t sec_hi, uint32_t sec_lo, uint32_t nsec)
|
|
|
|
{
|
|
|
|
struct screencopy* self = data;
|
2019-10-10 22:32:54 +00:00
|
|
|
|
2022-06-26 13:54:59 +00:00
|
|
|
uint64_t sec = (uint64_t)sec_hi << 32 | (uint64_t)sec_lo;
|
|
|
|
uint64_t pts = sec * UINT64_C(1000000) + (uint64_t)nsec / UINT64_C(1000);
|
|
|
|
|
|
|
|
DTRACE_PROBE2(wayvnc, screencopy_ready, self, pts);
|
2020-04-02 00:34:00 +00:00
|
|
|
|
2020-07-19 14:37:48 +00:00
|
|
|
screencopy__stop(self);
|
2019-10-10 22:32:54 +00:00
|
|
|
|
2020-01-14 20:12:58 +00:00
|
|
|
self->last_time = gettime_us();
|
|
|
|
|
2020-01-14 20:40:57 +00:00
|
|
|
double delay = (self->last_time - self->start_time) * 1.0e-6;
|
|
|
|
self->delay = smooth(&self->delay_smoother, delay);
|
|
|
|
|
2020-06-26 18:05:54 +00:00
|
|
|
if (self->is_immediate_copy)
|
|
|
|
wv_buffer_damage_whole(self->front);
|
2020-04-04 15:01:38 +00:00
|
|
|
|
2020-06-20 21:07:58 +00:00
|
|
|
if (self->back)
|
|
|
|
wv_buffer_pool_release(self->pool, self->back);
|
|
|
|
self->back = self->front;
|
|
|
|
self->front = NULL;
|
|
|
|
|
2022-04-14 17:08:21 +00:00
|
|
|
nvnc_fb_set_pts(self->back->nvnc_fb, pts);
|
|
|
|
|
2020-06-26 21:44:58 +00:00
|
|
|
self->status = SCREENCOPY_DONE;
|
|
|
|
self->on_done(self);
|
2019-10-08 22:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void screencopy_failed(void* data,
|
|
|
|
struct zwlr_screencopy_frame_v1* frame)
|
|
|
|
{
|
|
|
|
struct screencopy* self = data;
|
2019-10-10 22:32:54 +00:00
|
|
|
|
2020-04-02 00:34:00 +00:00
|
|
|
DTRACE_PROBE1(wayvnc, screencopy_failed, self);
|
|
|
|
|
2020-07-19 14:37:48 +00:00
|
|
|
screencopy__stop(self);
|
2020-07-17 16:22:17 +00:00
|
|
|
|
2020-12-19 00:48:39 +00:00
|
|
|
if (self->front)
|
|
|
|
wv_buffer_pool_release(self->pool, self->front);
|
2020-07-17 16:22:17 +00:00
|
|
|
self->front = NULL;
|
|
|
|
|
2020-06-26 21:44:58 +00:00
|
|
|
self->status = SCREENCOPY_FAILED;
|
|
|
|
self->on_done(self);
|
2019-10-08 22:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void screencopy_damage(void* data,
|
|
|
|
struct zwlr_screencopy_frame_v1* frame,
|
|
|
|
uint32_t x, uint32_t y,
|
|
|
|
uint32_t width, uint32_t height)
|
|
|
|
{
|
|
|
|
struct screencopy* self = data;
|
|
|
|
|
2020-04-02 00:34:00 +00:00
|
|
|
DTRACE_PROBE1(wayvnc, screencopy_damage, self);
|
|
|
|
|
2020-06-26 18:05:54 +00:00
|
|
|
wv_buffer_damage_rect(self->front, x, y, width, height);
|
2019-10-08 22:05:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 21:44:58 +00:00
|
|
|
static int screencopy__start_capture(struct screencopy* self)
|
2019-10-08 22:05:54 +00:00
|
|
|
{
|
2020-04-02 00:34:00 +00:00
|
|
|
DTRACE_PROBE1(wayvnc, screencopy_start, self);
|
|
|
|
|
2019-10-08 22:05:54 +00:00
|
|
|
static const struct zwlr_screencopy_frame_v1_listener frame_listener = {
|
|
|
|
.buffer = screencopy_buffer,
|
2020-06-23 23:04:57 +00:00
|
|
|
.linux_dmabuf = screencopy_linux_dmabuf,
|
|
|
|
.buffer_done = screencopy_buffer_done,
|
2019-10-08 22:05:54 +00:00
|
|
|
.flags = screencopy_flags,
|
|
|
|
.ready = screencopy_ready,
|
|
|
|
.failed = screencopy_failed,
|
|
|
|
.damage = screencopy_damage,
|
|
|
|
};
|
|
|
|
|
2020-01-14 20:40:57 +00:00
|
|
|
self->start_time = gettime_us();
|
|
|
|
|
2020-06-26 21:44:58 +00:00
|
|
|
self->frame = zwlr_screencopy_manager_v1_capture_output(self->manager,
|
|
|
|
self->overlay_cursor, self->wl_output);
|
2019-10-08 22:05:54 +00:00
|
|
|
if (!self->frame)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
zwlr_screencopy_frame_v1_add_listener(self->frame, &frame_listener,
|
|
|
|
self);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-10 22:32:54 +00:00
|
|
|
|
2020-03-16 22:01:43 +00:00
|
|
|
static void screencopy__poll(void* obj)
|
2019-12-29 11:47:31 +00:00
|
|
|
{
|
2020-03-16 22:01:43 +00:00
|
|
|
struct screencopy* self = aml_get_userdata(obj);
|
2019-12-29 11:47:31 +00:00
|
|
|
|
2020-06-26 21:44:58 +00:00
|
|
|
screencopy__start_capture(self);
|
2019-12-29 11:47:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 21:44:58 +00:00
|
|
|
static int screencopy__start(struct screencopy* self, bool is_immediate_copy)
|
2019-12-29 11:47:31 +00:00
|
|
|
{
|
2020-06-26 21:44:58 +00:00
|
|
|
if (self->status == SCREENCOPY_IN_PROGRESS)
|
2020-01-14 20:12:58 +00:00
|
|
|
return -1;
|
|
|
|
|
2020-06-26 21:44:58 +00:00
|
|
|
self->is_immediate_copy = is_immediate_copy;
|
2020-04-04 15:01:38 +00:00
|
|
|
|
2019-12-29 11:47:31 +00:00
|
|
|
uint64_t now = gettime_us();
|
|
|
|
double dt = (now - self->last_time) * 1.0e-6;
|
2022-10-29 11:56:54 +00:00
|
|
|
int32_t time_left = (1.0 / self->rate_limit - dt - self->delay) * 1.0e6;
|
2019-12-29 11:47:31 +00:00
|
|
|
|
2020-06-26 21:44:58 +00:00
|
|
|
self->status = SCREENCOPY_IN_PROGRESS;
|
2019-12-29 11:47:31 +00:00
|
|
|
|
2020-03-16 22:01:43 +00:00
|
|
|
if (time_left > 0) {
|
|
|
|
aml_set_duration(self->timer, time_left);
|
|
|
|
return aml_start(aml_get_default(), self->timer);
|
|
|
|
}
|
|
|
|
|
2020-06-26 21:44:58 +00:00
|
|
|
return screencopy__start_capture(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
int screencopy_start(struct screencopy* self)
|
|
|
|
{
|
|
|
|
return screencopy__start(self, false);
|
2019-12-29 11:47:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 21:44:58 +00:00
|
|
|
int screencopy_start_immediate(struct screencopy* self)
|
2020-03-28 11:44:40 +00:00
|
|
|
{
|
2020-06-26 21:44:58 +00:00
|
|
|
return screencopy__start(self, true);
|
2020-03-28 11:44:40 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 22:32:54 +00:00
|
|
|
void screencopy_init(struct screencopy* self)
|
|
|
|
{
|
2020-06-23 17:20:43 +00:00
|
|
|
self->pool = wv_buffer_pool_create(0, 0, 0, 0, 0);
|
2020-06-20 21:07:58 +00:00
|
|
|
assert(self->pool);
|
|
|
|
|
2020-03-16 22:01:43 +00:00
|
|
|
self->timer = aml_timer_new(0, screencopy__poll, self, NULL);
|
|
|
|
assert(self->timer);
|
2020-01-14 20:40:57 +00:00
|
|
|
|
|
|
|
self->delay_smoother.time_constant = DELAY_SMOOTHER_TIME_CONSTANT;
|
2019-10-10 22:32:54 +00:00
|
|
|
}
|
2019-12-27 14:41:54 +00:00
|
|
|
|
|
|
|
void screencopy_destroy(struct screencopy* self)
|
|
|
|
{
|
2020-03-16 22:01:43 +00:00
|
|
|
aml_stop(aml_get_default(), self->timer);
|
|
|
|
aml_unref(self->timer);
|
2020-01-04 13:48:09 +00:00
|
|
|
|
2020-06-20 21:07:58 +00:00
|
|
|
if (self->back)
|
|
|
|
wv_buffer_pool_release(self->pool, self->back);
|
|
|
|
if (self->front)
|
|
|
|
wv_buffer_pool_release(self->pool, self->front);
|
|
|
|
|
|
|
|
wv_buffer_pool_destroy(self->pool);
|
2019-12-27 14:41:54 +00:00
|
|
|
}
|