diff --git a/protocol/scaler.xml b/protocol/scaler.xml index dfe44b87..5094616d 100644 --- a/protocol/scaler.xml +++ b/protocol/scaler.xml @@ -2,7 +2,7 @@ - Copyright © 2013 Collabora, Ltd. + Copyright © 2013-2014 Collabora, Ltd. Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -26,7 +26,7 @@ THIS SOFTWARE. - + The global interface exposing surface cropping and scaling capabilities is used to instantiate an interface extension for a @@ -64,7 +64,7 @@ - + An additional interface to a wl_surface object, which allows the client to specify the cropping and scaling of the surface @@ -76,20 +76,29 @@ dst_height). This state is double-buffered, and is applied on the next wl_surface.commit. - Before the first set request, the wl_surface still behaves as if - there was no crop and scale state. That is, no scaling is applied, - and the surface size is as defined in wl_surface.attach. + 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. - The crop and scale state causes the surface size to become - dst_width, dst_height. 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. + 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. + + 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) + 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 @@ -127,9 +136,9 @@ - Set the crop and scale state of the associated wl_surface. See - wl_viewport for the description, and relation to the wl_buffer - size. + 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 @@ -152,5 +161,45 @@ + + + Set the source rectangle of the associated wl_surface. See + wl_viewport for the description, and relation to the wl_buffer + size. + + If width and/or height are negative, the source rectangle is unset + instead. + + The crop and scale state is double-buffered state, and will be + applied on the next wl_surface.commit. + + + + + + + + + + + Set the destination size of the associated wl_surface. See + wl_viewport for the description, and relation to the wl_buffer + size. + + If width and/or height are negative or zero, the destination size + is unset instead. + + 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. + + + + + diff --git a/src/compositor.c b/src/compositor.c index 82edf356..84680054 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -641,22 +641,29 @@ scaler_surface_to_buffer(struct weston_surface *surface, float sx, float sy, float *bx, float *by) { struct weston_buffer_viewport *vp = &surface->buffer_viewport; + double src_width, src_height; + double src_x, src_y; - if (vp->buffer.src_width != wl_fixed_from_int(-1) && - vp->surface.width != -1) { - double a, b; - - a = sx / vp->surface.width; - b = a * wl_fixed_to_double(vp->buffer.src_width); - *bx = b + wl_fixed_to_double(vp->buffer.src_x); + if (vp->buffer.src_width == wl_fixed_from_int(-1)) { + if (vp->surface.width == -1) { + *bx = sx; + *by = sy; + return; + } - a = sy / vp->surface.height; - b = a * wl_fixed_to_double(vp->buffer.src_height); - *by = b + wl_fixed_to_double(vp->buffer.src_y); + src_x = 0.0; + src_y = 0.0; + src_width = surface->width_from_buffer; + src_height = surface->height_from_buffer; } else { - *bx = sx; - *by = sy; + src_x = wl_fixed_to_double(vp->buffer.src_x); + src_y = wl_fixed_to_double(vp->buffer.src_y); + src_width = wl_fixed_to_double(vp->buffer.src_width); + src_height = wl_fixed_to_double(vp->buffer.src_height); } + + *bx = sx * src_width / surface->width + src_x; + *by = sy * src_height / surface->height + src_y; } WL_EXPORT void @@ -1195,6 +1202,12 @@ weston_surface_set_size(struct weston_surface *surface, surface_set_size(surface, width, height); } +static int +fixed_round_up_to_int(wl_fixed_t f) +{ + return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f); +} + static void weston_surface_set_size_from_buffer(struct weston_surface *surface) { @@ -1203,13 +1216,8 @@ weston_surface_set_size_from_buffer(struct weston_surface *surface) if (!surface->buffer_ref.buffer) { surface_set_size(surface, 0, 0); - return; - } - - if (vp->buffer.src_width != wl_fixed_from_int(-1) && - vp->surface.width != -1) { - surface_set_size(surface, vp->surface.width, - vp->surface.height); + surface->width_from_buffer = 0; + surface->height_from_buffer = 0; return; } @@ -1218,17 +1226,31 @@ weston_surface_set_size_from_buffer(struct weston_surface *surface) case WL_OUTPUT_TRANSFORM_270: case WL_OUTPUT_TRANSFORM_FLIPPED_90: case WL_OUTPUT_TRANSFORM_FLIPPED_270: - width = surface->buffer_ref.buffer->height; - height = surface->buffer_ref.buffer->width; + width = surface->buffer_ref.buffer->height / vp->buffer.scale; + height = surface->buffer_ref.buffer->width / vp->buffer.scale; break; default: - width = surface->buffer_ref.buffer->width; - height = surface->buffer_ref.buffer->height; + width = surface->buffer_ref.buffer->width / vp->buffer.scale; + height = surface->buffer_ref.buffer->height / vp->buffer.scale; break; } - width = width / vp->buffer.scale; - height = height / vp->buffer.scale; + surface->width_from_buffer = width; + surface->height_from_buffer = height; + + if (vp->surface.width != -1) { + surface_set_size(surface, + vp->surface.width, vp->surface.height); + return; + } + + if (vp->buffer.src_width != wl_fixed_from_int(-1)) { + surface_set_size(surface, + fixed_round_up_to_int(vp->buffer.src_width), + fixed_round_up_to_int(vp->buffer.src_height)); + return; + } + surface_set_size(surface, width, height); } @@ -3415,9 +3437,55 @@ viewport_set(struct wl_client *client, surface->pending.buffer_viewport.surface.height = dst_height; } +static void +viewport_set_source(struct wl_client *client, + struct wl_resource *resource, + wl_fixed_t src_x, + wl_fixed_t src_y, + wl_fixed_t src_width, + wl_fixed_t src_height) +{ + struct weston_surface *surface = + wl_resource_get_user_data(resource); + + assert(surface->viewport_resource != NULL); + + if (wl_fixed_to_double(src_width) < 0 || + wl_fixed_to_double(src_height) < 0) { + surface->pending.buffer_viewport.buffer.src_width = + wl_fixed_from_int(-1); + } else { + surface->pending.buffer_viewport.buffer.src_x = src_x; + surface->pending.buffer_viewport.buffer.src_y = src_y; + surface->pending.buffer_viewport.buffer.src_width = src_width; + surface->pending.buffer_viewport.buffer.src_height = src_height; + } +} + +static void +viewport_set_destination(struct wl_client *client, + struct wl_resource *resource, + int32_t dst_width, + int32_t dst_height) +{ + struct weston_surface *surface = + wl_resource_get_user_data(resource); + + assert(surface->viewport_resource != NULL); + + if (dst_width <= 0 || dst_height <= 0) { + surface->pending.buffer_viewport.surface.width = -1; + } else { + surface->pending.buffer_viewport.surface.width = dst_width; + surface->pending.buffer_viewport.surface.height = dst_height; + } +} + static const struct wl_viewport_interface viewport_interface = { viewport_destroy, - viewport_set + viewport_set, + viewport_set_source, + viewport_set_destination }; static void @@ -3433,7 +3501,9 @@ scaler_get_viewport(struct wl_client *client, uint32_t id, struct wl_resource *surface_resource) { - struct weston_surface *surface = wl_resource_get_user_data(surface_resource); + int version = wl_resource_get_version(scaler); + struct weston_surface *surface = + wl_resource_get_user_data(surface_resource); struct wl_resource *resource; if (surface->viewport_resource) { @@ -3444,7 +3514,7 @@ scaler_get_viewport(struct wl_client *client, } resource = wl_resource_create(client, &wl_viewport_interface, - 1, id); + version, id); if (resource == NULL) { wl_client_post_no_memory(client); return; @@ -3468,7 +3538,7 @@ bind_scaler(struct wl_client *client, struct wl_resource *resource; resource = wl_resource_create(client, &wl_scaler_interface, - 1, id); + MIN(version, 2), id); if (resource == NULL) { wl_client_post_no_memory(client); return; @@ -3568,7 +3638,7 @@ weston_compositor_init(struct weston_compositor *ec, ec, bind_subcompositor)) return -1; - if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 1, + if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2, ec, bind_scaler)) return -1; diff --git a/src/compositor.h b/src/compositor.h index d2afacdd..a1dd98a2 100644 --- a/src/compositor.h +++ b/src/compositor.h @@ -864,6 +864,8 @@ struct weston_surface { struct weston_buffer_reference buffer_ref; struct weston_buffer_viewport buffer_viewport; + int32_t width_from_buffer; /* before applying viewport */ + int32_t height_from_buffer; int keep_buffer; /* bool for backends to prevent early release */ /* wl_viewport resource for this surface */ diff --git a/src/pixman-renderer.c b/src/pixman-renderer.c index 6a1181df..b9993434 100644 --- a/src/pixman-renderer.c +++ b/src/pixman-renderer.c @@ -138,6 +138,35 @@ region_global_to_output(struct weston_output *output, pixman_region32_t *region) #define D2F(v) pixman_double_to_fixed((double)v) +static void +transform_apply_viewport(pixman_transform_t *transform, + struct weston_surface *surface) +{ + struct weston_buffer_viewport *vp = &surface->buffer_viewport; + double src_width, src_height; + double src_x, src_y; + + if (vp->buffer.src_width == wl_fixed_from_int(-1)) { + if (vp->surface.width == -1) + return; + + src_x = 0.0; + src_y = 0.0; + src_width = surface->width_from_buffer; + src_height = surface->height_from_buffer; + } else { + src_x = wl_fixed_to_double(vp->buffer.src_x); + src_y = wl_fixed_to_double(vp->buffer.src_y); + src_width = wl_fixed_to_double(vp->buffer.src_width); + src_height = wl_fixed_to_double(vp->buffer.src_height); + } + + pixman_transform_scale(transform, NULL, + D2F(src_width / surface->width), + D2F(src_height / surface->height)); + pixman_transform_translate(transform, NULL, D2F(src_x), D2F(src_y)); +} + static void repaint_region(struct weston_view *ev, struct weston_output *output, pixman_region32_t *region, pixman_region32_t *surf_region, @@ -258,25 +287,7 @@ repaint_region(struct weston_view *ev, struct weston_output *output, pixman_double_to_fixed ((double)-ev->geometry.y)); } - if (vp->buffer.src_width != wl_fixed_from_int(-1) && - vp->surface.width != -1) { - double viewport_x, viewport_y, viewport_width, viewport_height; - double ratio_x, ratio_y; - - viewport_x = wl_fixed_to_double(vp->buffer.src_x); - viewport_y = wl_fixed_to_double(vp->buffer.src_y); - viewport_width = wl_fixed_to_double(vp->buffer.src_width); - viewport_height = wl_fixed_to_double(vp->buffer.src_height); - - ratio_x = viewport_width / vp->surface.width; - ratio_y = viewport_height / vp->surface.height; - - pixman_transform_scale(&transform, NULL, - pixman_double_to_fixed(ratio_x), - pixman_double_to_fixed(ratio_y)); - pixman_transform_translate(&transform, NULL, pixman_double_to_fixed(viewport_x), - pixman_double_to_fixed(viewport_y)); - } + transform_apply_viewport(&transform, ev->surface); fw = pixman_int_to_fixed(ev->surface->width); fh = pixman_int_to_fixed(ev->surface->height);