Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a recorder interface, using a custom protocol
A separate client using the lipstick_recorder protocol can request frames, which lipstick will provide when redrawing.
- Loading branch information
Showing
7 changed files
with
362 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<protocol name="lipstick_recorder"> | ||
<copyright> | ||
Copyright (C) 2014 Jolla Ltd. | ||
|
||
Permission to use, copy, modify, distribute, and sell this | ||
software and its documentation for any purpose is hereby granted | ||
without fee, provided that the above copyright notice appear in | ||
all copies and that both that copyright notice and this permission | ||
notice appear in supporting documentation, and that the name of | ||
the copyright holders not be used in advertising or publicity | ||
pertaining to distribution of the software without specific, | ||
written prior permission. The copyright holders make no | ||
representations about the suitability of this software for any | ||
purpose. It is provided "as is" without express or implied | ||
warranty. | ||
|
||
THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS | ||
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||
FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
SPECIAL, 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. | ||
</copyright> | ||
|
||
<interface name="lipstick_recorder_manager" version="1"> | ||
<request name="create_recorder"> | ||
<description summary="create a recorder object"> | ||
Create a recorder object for the specified output. | ||
</description> | ||
<arg name="recorder" type="new_id" interface="lipstick_recorder"/> | ||
<arg name="output" type="object" interface="wl_output"/> | ||
</request> | ||
</interface> | ||
|
||
<interface name="lipstick_recorder" version="1"> | ||
<request name="destroy" type="destructor"> | ||
<description summary="destroy the recorder object"> | ||
Destroy the recorder object, discarding any frame request | ||
that may be pending. | ||
</description> | ||
</request> | ||
<request name="record_frame"> | ||
<description summary="request a frame to be recorded"> | ||
Ask the compositor to record its next frame, putting | ||
the content into the specified buffer data. The frame | ||
event will be sent when the frame is recorded. | ||
Only one frame will be recorded, the client will have | ||
to call this again after the frame event if it wants to | ||
record more frames. | ||
|
||
The buffer must be a shm buffer, trying to use another | ||
type of buffer will result in frame being sent with the | ||
bad_buffer result. | ||
</description> | ||
<arg name="buffer" type="object" interface="wl_buffer"/> | ||
</request> | ||
|
||
<enum name="result"> | ||
<entry name="ok" value="1"/> | ||
<entry name="bad_buffer" value="2"/> | ||
</enum> | ||
|
||
<event name="frame"> | ||
<description summary="notify a frame was recorded, or an error"> | ||
The compositor will send this event after a frame was | ||
recorded, or in case an error happened. The client can | ||
call record_frame again to record the next frame. | ||
|
||
The value of the 'result' argument will be one of the | ||
values of the 'result' enum. | ||
'time' is the time the compositor recorded that frame, | ||
in milliseconds, with an unspecified base. | ||
</description> | ||
<arg name="result" type="int"/> | ||
<arg name="buffer" type="object" interface="wl_buffer"/> | ||
<arg name="time" type="uint"/> | ||
</event> | ||
|
||
<event name="cancelled"> | ||
<description summary="notify a request was cancelled"> | ||
The compositor will send this event if the client calls | ||
request_frame more than one time for the same compositor | ||
frame. The cancel event will be sent carrying the old | ||
buffer, and the frame will be recorded using the newest | ||
buffer. | ||
</description> | ||
<arg name="buffer" type="object" interface="wl_buffer"/> | ||
</event> | ||
</interface> | ||
</protocol> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/*************************************************************************** | ||
** | ||
** Copyright (C) 2014 Jolla Ltd. | ||
** Contact: Giulio Camuffo <giulio.camuffo@jollamobile.com> | ||
** | ||
** This file is part of lipstick. | ||
** | ||
** This library is free software; you can redistribute it and/or | ||
** modify it under the terms of the GNU Lesser General Public | ||
** License version 2.1 as published by the Free Software Foundation | ||
** and appearing in the file LICENSE.LGPL included in the packaging | ||
** of this file. | ||
** | ||
****************************************************************************/ | ||
|
||
#include <sys/time.h> | ||
|
||
#include <QMutexLocker> | ||
|
||
#include "lipstickrecorder.h" | ||
#include "lipstickcompositor.h" | ||
|
||
static uint32_t getTime() | ||
{ | ||
struct timeval tv; | ||
gettimeofday(&tv, NULL); | ||
return tv.tv_sec * 1000 + tv.tv_usec / 1000; | ||
} | ||
|
||
static const QEvent::Type FrameEventType = (QEvent::Type)QEvent::registerEventType(); | ||
|
||
class FrameEvent : public QEvent | ||
{ | ||
public: | ||
FrameEvent(int r, uint32_t t) | ||
: QEvent(FrameEventType) | ||
, result(r) | ||
, time(t) | ||
{ | ||
} | ||
|
||
int result; | ||
uint32_t time; | ||
}; | ||
|
||
LipstickRecorderManager::LipstickRecorderManager() | ||
: QWaylandGlobalInterface() | ||
{ | ||
} | ||
|
||
const wl_interface* LipstickRecorderManager::interface() const | ||
{ | ||
return &lipstick_recorder_manager_interface; | ||
} | ||
|
||
void LipstickRecorderManager::recordFrame(QWindow *window) | ||
{ | ||
QMutexLocker lock(&m_mutex); | ||
if (m_requests.isEmpty()) | ||
return; | ||
|
||
uchar *pixels; | ||
uint32_t time = getTime(); | ||
foreach (LipstickRecorder *recorder, m_requests.values(window)) { | ||
wl_shm_buffer *buffer = recorder->buffer(); | ||
pixels = static_cast<uchar *>(wl_shm_buffer_get_data(buffer)); | ||
int width = wl_shm_buffer_get_width(buffer); | ||
int height = wl_shm_buffer_get_height(buffer); | ||
|
||
if (width != window->width() || height != window->height()) { | ||
qApp->postEvent(recorder, new FrameEvent(QtWaylandServer::lipstick_recorder::result_bad_buffer, time)); | ||
continue; | ||
} | ||
|
||
glPixelStorei(GL_PACK_ALIGNMENT, 1); | ||
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); | ||
qApp->postEvent(recorder, new FrameEvent(QtWaylandServer::lipstick_recorder::result_ok, time)); | ||
|
||
m_requests.remove(window, recorder); | ||
} | ||
} | ||
|
||
void LipstickRecorderManager::requestFrame(QWindow *window, LipstickRecorder *recorder) | ||
{ | ||
QMutexLocker lock(&m_mutex); | ||
m_requests.insert(window, recorder); | ||
} | ||
|
||
void LipstickRecorderManager::remove(QWindow *window, LipstickRecorder *recorder) | ||
{ | ||
QMutexLocker lock(&m_mutex); | ||
m_requests.remove(window, recorder); | ||
} | ||
|
||
void LipstickRecorderManager::bind(wl_client *client, quint32 version, quint32 id) | ||
{ | ||
Q_UNUSED(version) | ||
|
||
add(client, id, version); | ||
} | ||
|
||
void LipstickRecorderManager::lipstick_recorder_manager_create_recorder(Resource *resource, uint32_t id, ::wl_resource *output) | ||
{ | ||
// TODO: we should find out the window associated with this output, but there isn't | ||
// a way to do that in qtcompositor yet. Just ignore it for now and use the one window we have. | ||
Q_UNUSED(output) | ||
|
||
new LipstickRecorder(this, resource->client(), id, LipstickCompositor::instance()); | ||
} | ||
|
||
|
||
LipstickRecorder::LipstickRecorder(LipstickRecorderManager *manager, wl_client *client, quint32 id, QWindow *window) | ||
: QtWaylandServer::lipstick_recorder(client, id, 1) | ||
, m_manager(manager) | ||
, m_bufferResource(Q_NULLPTR) | ||
, m_client(client) | ||
, m_window(window) | ||
{ | ||
} | ||
|
||
LipstickRecorder::~LipstickRecorder() | ||
{ | ||
m_manager->remove(m_window, this); | ||
} | ||
|
||
void LipstickRecorder::sendFrame(int result, int time) | ||
{ | ||
send_frame(result, m_bufferResource, time); | ||
m_bufferResource = Q_NULLPTR; | ||
} | ||
|
||
void LipstickRecorder::lipstick_recorder_destroy_resource(Resource *resource) | ||
{ | ||
Q_UNUSED(resource) | ||
delete this; | ||
} | ||
|
||
void LipstickRecorder::lipstick_recorder_destroy(Resource *resource) | ||
{ | ||
wl_resource_destroy(resource->handle); | ||
} | ||
|
||
void LipstickRecorder::lipstick_recorder_record_frame(Resource *resource, ::wl_resource *buffer) | ||
{ | ||
Q_UNUSED(resource) | ||
if (m_bufferResource) { | ||
send_cancelled(buffer); | ||
} | ||
m_bufferResource = buffer; | ||
m_buffer = wl_shm_buffer_get(buffer); | ||
if (m_buffer) { | ||
m_manager->requestFrame(m_window, this); | ||
} else { | ||
m_bufferResource = Q_NULLPTR; | ||
send_frame(result_bad_buffer, buffer, getTime()); | ||
} | ||
} | ||
|
||
bool LipstickRecorder::event(QEvent *e) | ||
{ | ||
if (e->type() == FrameEventType) { | ||
FrameEvent *fe = static_cast<FrameEvent *>(e); | ||
sendFrame(fe->result, fe->time); | ||
wl_client_flush(client()); | ||
return true; | ||
} | ||
return QObject::event(e); | ||
} |
Oops, something went wrong.