2021-05-14 02:51:45 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
|
|
|
* Copyright (C) 2017-2021 The Looking Glass Authors
|
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include "wayland.h"
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <wayland-client.h>
|
|
|
|
|
|
|
|
#include "common/debug.h"
|
|
|
|
#include "common/time.h"
|
|
|
|
|
|
|
|
struct FrameData
|
|
|
|
{
|
|
|
|
struct timespec sent;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void presentationClockId(void * data,
|
|
|
|
struct wp_presentation * presentation, uint32_t clkId)
|
|
|
|
{
|
|
|
|
wlWm.clkId = clkId;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wp_presentation_listener presentationListener = {
|
|
|
|
.clock_id = presentationClockId,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void presentationFeedbackSyncOutput(void * data,
|
|
|
|
struct wp_presentation_feedback * feedback, struct wl_output * output)
|
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
static void presentationFeedbackPresented(void * opaque,
|
|
|
|
struct wp_presentation_feedback * feedback, uint32_t tvSecHi, uint32_t tvSecLo,
|
|
|
|
uint32_t tvNsec, uint32_t refresh, uint32_t seqHi, uint32_t seqLo, uint32_t flags)
|
|
|
|
{
|
|
|
|
struct FrameData * data = opaque;
|
|
|
|
struct timespec present = {
|
|
|
|
.tv_sec = (uint64_t) tvSecHi << 32 | tvSecLo,
|
|
|
|
.tv_nsec = tvNsec,
|
|
|
|
};
|
|
|
|
struct timespec delta;
|
|
|
|
|
|
|
|
tsDiff(&delta, &present, &data->sent);
|
2021-07-18 02:57:15 +00:00
|
|
|
ringbuffer_push(wlWm.photonTimings, &(float){ delta.tv_sec + delta.tv_nsec * 1e-6f });
|
2021-05-14 02:51:45 +00:00
|
|
|
free(data);
|
2021-07-22 02:12:01 +00:00
|
|
|
wp_presentation_feedback_destroy(feedback);
|
2021-05-14 02:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void presentationFeedbackDiscarded(void * data,
|
|
|
|
struct wp_presentation_feedback * feedback)
|
|
|
|
{
|
|
|
|
free(data);
|
2021-07-22 02:12:01 +00:00
|
|
|
wp_presentation_feedback_destroy(feedback);
|
2021-05-14 02:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wp_presentation_feedback_listener presentationFeedbackListener = {
|
|
|
|
.sync_output = presentationFeedbackSyncOutput,
|
|
|
|
.presented = presentationFeedbackPresented,
|
|
|
|
.discarded = presentationFeedbackDiscarded,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool waylandPresentationInit(void)
|
|
|
|
{
|
|
|
|
if (wlWm.presentation)
|
2021-07-20 04:20:55 +00:00
|
|
|
{
|
|
|
|
wlWm.photonTimings = ringbuffer_new(256, sizeof(float));
|
2021-07-29 01:56:50 +00:00
|
|
|
wlWm.photonGraph = app_registerGraph("PHOTON", wlWm.photonTimings, 0.0f, 30.0f);
|
2021-05-14 02:51:45 +00:00
|
|
|
wp_presentation_add_listener(wlWm.presentation, &presentationListener, NULL);
|
2021-07-20 04:20:55 +00:00
|
|
|
}
|
2021-05-14 02:51:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void waylandPresentationFree(void)
|
|
|
|
{
|
2021-07-20 04:20:55 +00:00
|
|
|
if (!wlWm.presentation)
|
|
|
|
return;
|
|
|
|
|
2021-05-14 02:51:45 +00:00
|
|
|
wp_presentation_destroy(wlWm.presentation);
|
2021-07-18 02:57:15 +00:00
|
|
|
app_unregisterGraph(wlWm.photonGraph);
|
|
|
|
ringbuffer_free(&wlWm.photonTimings);
|
2021-05-14 02:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void waylandPresentationFrame(void)
|
|
|
|
{
|
2021-07-20 04:20:55 +00:00
|
|
|
if (!wlWm.presentation)
|
|
|
|
return;
|
|
|
|
|
2021-05-14 02:51:45 +00:00
|
|
|
struct FrameData * data = malloc(sizeof(struct FrameData));
|
|
|
|
if (clock_gettime(wlWm.clkId, &data->sent))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("clock_gettime failed: %s\n", strerror(errno));
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wp_presentation_feedback * feedback = wp_presentation_feedback(wlWm.presentation, wlWm.surface);
|
|
|
|
wp_presentation_feedback_add_listener(feedback, &presentationFeedbackListener, data);
|
|
|
|
}
|