Skip to content
Permalink
Browse files

wayland/shm: use wl_viewport

Lets the compositor scale the video. This relies on the compositors scaling
mechanism and efficiency. But should by far more efficient than ffmpeg scaling
which burns CPU.

In my tests mpv went from 66 % cpu load to 9 %. But the scaling wasn't as nice
as ffmpegs.

Falls back to software scaling if the compositor dosen't advertise the
wl_scaler interface.
  • Loading branch information...
giselher committed Mar 6, 2014
1 parent 840985a commit 77cc885b44a9e95e5c3c9ae4961b9958ff5cf643
Showing with 280 additions and 10 deletions.
  1. +50 −10 video/out/vo_wayland.c
  2. +210 −0 video/out/wayland/protocol/scaler.xml
  3. +8 −0 video/out/wayland_common.c
  4. +3 −0 video/out/wayland_common.h
  5. +9 −0 wscript_build.py
@@ -105,6 +105,8 @@ struct priv {
struct vo *vo;
struct vo_wayland_state *wl;

struct wl_viewport *viewport; // optional scaler in case swscale is not available

struct wl_list format_list;
const format_t *video_format; // pointer to element in supported_format list

@@ -137,6 +139,7 @@ struct priv {
// options
int enable_alpha;
int use_rgb565;
int use_viewport;
};

static bool is_alpha_format(const format_t *fmt)
@@ -297,22 +300,42 @@ static bool resize(struct priv *p)

mp_sws_set_from_cmdline(p->sws, p->vo->opts->sws_opts);
p->sws->src = p->in_format;
p->sws->dst = (struct mp_image_params) {
.imgfmt = p->video_format->mp_format,
.w = p->dst_w,
.h = p->dst_h,
.d_w = p->dst_w,
.d_h = p->dst_h,
};
if (p->use_viewport) {
p->sws->dst = (struct mp_image_params) {
.imgfmt = p->video_format->mp_format,
.w = p->width,
.h = p->height,
.d_w = p->width,
.d_h = p->height,
};
}
else {
p->sws->dst = (struct mp_image_params) {
.imgfmt = p->video_format->mp_format,
.w = p->dst_w,
.h = p->dst_h,
.d_w = p->dst_w,
.d_h = p->dst_h,
};
}

mp_image_params_guess_csp(&p->sws->dst);

if (mp_sws_reinit(p->sws) < 0)
return false;

if (!buffer_pool_resize(&p->video_bufpool, p->dst_w, p->dst_h)) {
MP_ERR(wl, "failed to resize video buffers\n");
return false;
if (p->use_viewport) {
wl_viewport_set(p->viewport,
wl_fixed_from_int(0), wl_fixed_from_int(0),
wl_fixed_from_int(p->width), wl_fixed_from_int(p->height),
p->dst_w, p->dst_h);
}
else {
// use swscale
if (!buffer_pool_resize(&p->video_bufpool, p->dst_w, p->dst_h)) {
MP_ERR(wl, "failed to resize video buffers\n");
return false;
}
}

wl->window.width = p->dst_w;
@@ -622,6 +645,9 @@ static void uninit(struct vo *vo)
wl_surface_destroy(p->osd_surfaces[i]);
}

if (p->viewport)
wl_viewport_destroy(p->viewport);

vo_wayland_uninit(vo);
}

@@ -663,6 +689,16 @@ static int preinit(struct vo *vo)
}
wl_region_destroy(input);

if (p->use_viewport && wl->display.scaler == NULL) {
MP_WARN(wl, "No wl_scaler interface available\n"
"Falling back to software scaling\n");
p->use_viewport = 0;
}

if (p->use_viewport)
p->viewport = wl_scaler_get_viewport(wl->display.scaler,
wl->window.video_surface);

return 0;
}

@@ -716,7 +752,11 @@ const struct vo_driver video_out_wayland = {
.options = (const struct m_option[]) {
OPT_FLAG("alpha", enable_alpha, 0),
OPT_FLAG("rgb565", use_rgb565, 0),
OPT_FLAG("viewport", use_viewport, 0),
{0}
},
.priv_defaults = &(const struct priv) {
.use_viewport = 1
},
};

@@ -0,0 +1,210 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="scaler">

<copyright>
Copyright © 2013-2014 Collabora, 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="wl_scaler" version="2">
<description summary="surface cropping and scaling">
The global interface exposing surface cropping and scaling
capabilities is used to instantiate an interface extension for a
wl_surface object. This extended interface will then allow
cropping and scaling the surface contents, effectively
disconnecting the direct relationship between the buffer and the
surface size.
</description>

<request name="destroy" type="destructor">
<description summary="unbind from the cropping and scaling interface">
Informs the server that the client will not be using this
protocol object anymore. This does not affect any other objects,
wl_viewport objects included.
</description>
</request>

<enum name="error">
<entry name="viewport_exists" value="0"
summary="the surface already has a viewport object associated"/>
</enum>

<request name="get_viewport">
<description summary="extend surface interface for crop and scale">
Instantiate an interface extension for the given wl_surface to
crop and scale its content. If the given wl_surface already has
a wl_viewport object associated, the viewport_exists
protocol error is raised.
</description>

<arg name="id" type="new_id" interface="wl_viewport"
summary="the new viewport interface id"/>
<arg name="surface" type="object" interface="wl_surface"
summary="the surface"/>
</request>
</interface>

<interface name="wl_viewport" version="2">
<description summary="crop and scale interface to a wl_surface">
An additional interface to a wl_surface object, which allows the
client to specify the cropping and scaling of the surface
contents.

This interface allows to define the source rectangle (src_x,
src_y, src_width, src_height) from where to take the wl_buffer
contents, and scale that to destination size (dst_width,
dst_height). This state is double-buffered, and is applied on the
next wl_surface.commit.

The two parts of crop and scale state are independent: the source
rectangle, and the destination size. Initially both are unset, that
is, no scaling is applied. The whole of the current wl_buffer is
used as the source, and the surface size is as defined in
wl_surface.attach.

If the destination size is set, it causes the surface size to become
dst_width, dst_height. The source (rectangle) is scaled to exactly
this size. This overrides whatever the attached wl_buffer size is,
unless the wl_buffer is NULL. If the wl_buffer is NULL, the surface
has no content and therefore no size. Otherwise, the size is always
at least 1x1 in surface coordinates.

If the source rectangle is set, it defines what area of the
wl_buffer is taken as the source. If the source rectangle is set and
the destination size is not set, the surface size becomes the source
rectangle size rounded up to the nearest integer. If the source size
is already exactly integers, this results in cropping without scaling.

The coordinate transformations from buffer pixel coordinates up to
the surface-local coordinates happen in the following order:
1. buffer_transform (wl_surface.set_buffer_transform)
2. buffer_scale (wl_surface.set_buffer_scale)
3. crop and scale (wl_viewport.set*)
This means, that the source rectangle coordinates of crop and scale
are given in the coordinates after the buffer transform and scale,
i.e. in the coordinates that would be the surface-local coordinates
if the crop and scale was not applied.

If the source rectangle is partially or completely outside of the
wl_buffer, then the surface contents are undefined (not void), and
the surface size is still dst_width, dst_height.

The x, y arguments of wl_surface.attach are applied as normal to
the surface. They indicate how many pixels to remove from the
surface size from the left and the top. In other words, they are
still in the surface-local coordinate system, just like dst_width
and dst_height are.

If the wl_surface associated with the wl_viewport is destroyed,
the wl_viewport object becomes inert.

If the wl_viewport object is destroyed, the crop and scale
state is removed from the wl_surface. The change will be applied
on the next wl_surface.commit.
</description>

<request name="destroy" type="destructor">
<description summary="remove scaling and cropping from the surface">
The associated wl_surface's crop and scale state is removed.
The change is applied on the next wl_surface.commit.
</description>
</request>

<enum name="error">
<entry name="bad_value" value="0"
summary="negative or zero values in width or height"/>
</enum>

<request name="set">
<description summary="set the crop and scale state">
Set both source rectangle and destination size of the associated
wl_surface. See wl_viewport for the description, and relation to
the wl_buffer size.

The bad_value protocol error is raised if src_width or
src_height is negative, or if dst_width or dst_height is not
positive.

The crop and scale state is double-buffered state, and will be
applied on the next wl_surface.commit.

Arguments dst_x and dst_y do not exist here, use the x and y
arguments to wl_surface.attach. The x, y, dst_width, and dst_height
define the surface-local coordinate system irrespective of the
attached wl_buffer size.
</description>

<arg name="src_x" type="fixed" summary="source rectangle x"/>
<arg name="src_y" type="fixed" summary="source rectangle y"/>
<arg name="src_width" type="fixed" summary="source rectangle width"/>
<arg name="src_height" type="fixed" summary="source rectangle height"/>
<arg name="dst_width" type="int" summary="surface width"/>
<arg name="dst_height" type="int" summary="surface height"/>
</request>

<request name="set_source" since="2">
<description summary="set the source rectangle for cropping">
Set the source rectangle of the associated wl_surface. See
wl_viewport for the description, and relation to the wl_buffer
size.

If width is -1.0 and height is -1.0, the destination size is unset
instead. Any other pair of values for width and height that
contains zero or negative values raises the bad_value protocol
error.

The crop and scale state is double-buffered state, and will be
applied on the next wl_surface.commit.
</description>

<arg name="x" type="fixed" summary="source rectangle x"/>
<arg name="y" type="fixed" summary="source rectangle y"/>
<arg name="width" type="fixed" summary="source rectangle width"/>
<arg name="height" type="fixed" summary="source rectangle height"/>
</request>

<request name="set_destination" since="2">
<description summary="set the surface size for scaling">
Set the destination size of the associated wl_surface. See
wl_viewport for the description, and relation to the wl_buffer
size.

If width is -1 and height is -1, the destination size is unset
instead. Any other pair of values for width and height that
contains zero or negative values raises the bad_value protocol
error.

The crop and scale state is double-buffered state, and will be
applied on the next wl_surface.commit.

Arguments x and y do not exist here, use the x and y arguments to
wl_surface.attach. The x, y, width, and height define the
surface-local coordinate system irrespective of the attached
wl_buffer size.
</description>

<arg name="width" type="int" summary="surface width"/>
<arg name="height" type="int" summary="surface height"/>
</request>
</interface>
</protocol>
@@ -636,6 +636,11 @@ static void registry_handle_global (void *data,
xdg_shell_use_unstable_version(wl->display.shell,
XDG_SHELL_VERSION_CURRENT);
}

else if (strcmp(interface, "wl_scaler") == 0) {

wl->display.scaler = wl_registry_bind(reg, id, &wl_scaler_interface, 1);
}
}

static void registry_handle_global_remove (void *data,
@@ -782,6 +787,9 @@ static void destroy_display (struct vo_wayland_state *wl)
}
}

if (wl->display.scaler)
wl_scaler_destroy(wl->display.scaler);

if (wl->display.shm)
wl_shm_destroy(wl->display.shm);

@@ -35,6 +35,7 @@

// Generated from xdg-shell.xml
#include "video/out/wayland/xdg-shell-protocol.h"
#include "video/out/wayland/scaler-protocol.h"

struct vo;

@@ -83,6 +84,8 @@ struct vo_wayland_state {

int display_fd;

struct wl_scaler *scaler;

struct wl_shm *shm;

struct wl_subcompositor *subcomp;
@@ -86,10 +86,18 @@ def build(ctx):
source = "video/out/wayland/protocol/xdg-shell.xml",
target = "video/out/wayland/xdg-shell-protocol.c")

ctx.wayland_protocol_code(
source = "video/out/wayland/protocol/scaler.xml",
target = "video/out/wayland/scaler-protocol.c")

ctx.wayland_protocol_header(
source = "video/out/wayland/protocol/xdg-shell.xml",
target = "video/out/wayland/xdg-shell-protocol.h")

ctx.wayland_protocol_header(
source = "video/out/wayland/protocol/scaler.xml",
target = "video/out/wayland/scaler-protocol.h")

getch2_c = {
'win32': 'osdep/terminal-win.c',
}.get(ctx.env.DEST_OS, "osdep/terminal-unix.c")
@@ -381,6 +389,7 @@ def build(ctx):
( "video/out/wayland/buffer.c", "wayland" ),
( "video/out/wayland/memfile.c", "wayland" ),
( "video/out/wayland/xdg-shell-protocol.c", "wayland" ),
( "video/out/wayland/scaler-protocol.c", "wayland" ),
( "video/out/win_state.c"),
( "video/out/x11_common.c", "x11" ),

0 comments on commit 77cc885

Please sign in to comment.
You can’t perform that action at this time.