This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch up/output-remove
in repository enlightenment.

View the commit online.

commit 7aafef4e6da1418fbe146d7df7ab7799404d013f
Author: Cedric BAIL <[email protected]>
AuthorDate: Sat Nov 28 14:09:02 2026 -0600

    e_comp_wl - add wp_viewporter
    
    A viewport crops a surface's buffer to a source rectangle and scales
    the result to a destination size, so the surface is no longer the size
    of its buffer. Browsers use it to hand video to the compositor at its
    own resolution, and it is what wp_fractional_scale_v1 clients draw
    through.
    
    Both halves are double buffered surface state, latched at commit with
    the buffer scale and transform. The source is in surface-local units,
    after the buffer scale and transform, so the surface size is worked
    out in that order: the buffer, turned and divided by its scale, then
    the source size, then the destination. The source reaches the screen
    through evas_object_image_buffer_crop_set(), multiplied back by the
    buffer scale and rounded out to whole pixels; evas crops the turned
    image, which is the space the source is in. The destination needs
    nothing more: the comp object already scales the image to the surface
    size.
    
    out_of_buffer and bad_size are raised at commit, as the protocol says:
    the buffer, the transform and the destination may come in the same
    commit as the source. out_of_buffer is checked first, as a source can
    be both fractional and outside of the buffer.
    
    Damage and the opaque region under a viewport are handled as under a
    transform: the buffer is redrawn whole, and the opaque hint dropped.
    E redraws a surface whole on every commit, so neither shows.
    
    e_comp_object.c is shared with X11, which never sets a crop.
    
    wl-viewporter crops a four-quadrant buffer to one quadrant, scales a
    fractional source to a destination without a new buffer, unsets each
    half, crops a buffer of scale 2 and a turned buffer, destroys the
    viewport, and checks every protocol error. Each check of the size and
    pixels fails with the matching part of this change removed; so do the
    error checks without the commit check, without the transform in it,
    and, as a crash, without forgetting the surface once it is gone.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/bin/e_comp_object.c             |  20 +++
 src/bin/e_comp_object.h             |   1 +
 src/bin/e_comp_wl.c                 | 106 +++++++++++-
 src/bin/e_comp_wl.h                 |  22 ++-
 src/bin/e_comp_wl_extensions.c      | 120 ++++++++++++++
 src/bin/generated/meson.build       |   1 +
 src/tests/wayland/globals.expected  |   1 +
 src/tests/wayland/meson.build       |   2 +
 src/tests/wayland/test_viewporter.c | 310 ++++++++++++++++++++++++++++++++++++
 src/tests/wlcs/e_wlcs.c             |   1 +
 10 files changed, 574 insertions(+), 10 deletions(-)

diff --git a/src/bin/e_comp_object.c b/src/bin/e_comp_object.c
index 1937e5338..e2f931e46 100644
--- a/src/bin/e_comp_object.c
+++ b/src/bin/e_comp_object.c
@@ -77,6 +77,9 @@ typedef struct _E_Comp_Object
    int                  content_w, content_h;
    /* How a Wayland buffer is turned to be shown the right way up. */
    Evas_Image_Orient    content_orient;
+   /* The part of the turned buffer shown, for a wp_viewport source; 0x0 is
+    * all of it. */
+   Eina_Rectangle       content_crop;
 
    Eina_Stringshare   *frame_theme;
    Eina_Stringshare   *frame_name;
@@ -1805,6 +1808,8 @@ _e_comp_intercept_show(void *data, Evas_Object *obj EINA_UNUSED)
         evas_object_image_smooth_scale_set(cw->obj, e_comp_config_get()->smooth_windows);
         evas_object_name_set(cw->obj, "cw->obj");
         evas_object_image_colorspace_set(cw->obj, EVAS_COLORSPACE_ARGB8888);
+        evas_object_image_buffer_crop_set(cw->obj, cw->content_crop.x, cw->content_crop.y,
+                                          cw->content_crop.w, cw->content_crop.h);
         _e_comp_object_alpha_set(cw);
 
         if (cw->frame_object)
@@ -4217,6 +4222,19 @@ e_comp_object_content_orient_set(Evas_Object *obj, Evas_Image_Orient orient)
      _e_comp_object_content_orient_apply(cw, o);
 }
 
+EINTERN void
+e_comp_object_content_crop_set(Evas_Object *obj, int x, int y, int w, int h)
+{
+   Eina_List *l;
+   Evas_Object *o;
+
+   API_ENTRY;
+   EINA_RECTANGLE_SET(&cw->content_crop, x, y, w, h);
+   if (cw->obj) evas_object_image_buffer_crop_set(cw->obj, x, y, w, h);
+   EINA_LIST_FOREACH(cw->obj_mirror, l, o)
+     evas_object_image_buffer_crop_set(o, x, y, w, h);
+}
+
 /* mark an object as dirty and setup damages */
 E_API void
 e_comp_object_dirty(Evas_Object *obj)
@@ -4698,6 +4716,8 @@ e_comp_object_util_mirror_add(Evas_Object *obj)
    o = evas_object_image_filled_add(evas_object_evas_get(obj));
    evas_object_image_colorspace_set(o, EVAS_COLORSPACE_ARGB8888);
    evas_object_image_smooth_scale_set(o, e_comp_config_get()->smooth_windows);
+   evas_object_image_buffer_crop_set(o, cw->content_crop.x, cw->content_crop.y,
+                                     cw->content_crop.w, cw->content_crop.h);
    cw->obj_mirror = eina_list_append(cw->obj_mirror, o);
    evas_object_event_callback_add(o, EVAS_CALLBACK_DEL, _e_comp_object_cb_mirror_del, cw);
    evas_object_event_callback_add(o, EVAS_CALLBACK_SHOW, _e_comp_object_cb_mirror_show, cw);
diff --git a/src/bin/e_comp_object.h b/src/bin/e_comp_object.h
index 152454cd0..ee5dae0b7 100644
--- a/src/bin/e_comp_object.h
+++ b/src/bin/e_comp_object.h
@@ -91,6 +91,7 @@ E_API void e_comp_object_blank(Evas_Object *obj, Eina_Bool set);
 E_API void e_comp_object_dirty(Evas_Object *obj);
 EINTERN void e_comp_object_content_size_set(Evas_Object *obj, int w, int h);
 EINTERN void e_comp_object_content_orient_set(Evas_Object *obj, Evas_Image_Orient orient);
+EINTERN void e_comp_object_content_crop_set(Evas_Object *obj, int x, int y, int w, int h);
 E_API Eina_Bool e_comp_object_render(Evas_Object *obj);
 E_API Eina_Bool e_comp_object_effect_allowed_get(Evas_Object *obj);
 E_API Eina_Bool e_comp_object_effect_set(Evas_Object *obj, const char *effect);
diff --git a/src/bin/e_comp_wl.c b/src/bin/e_comp_wl.c
index 5ecb7ba0c..297d49867 100644
--- a/src/bin/e_comp_wl.c
+++ b/src/bin/e_comp_wl.c
@@ -5,6 +5,8 @@
 #define __STDC_FORMAT_MACROS
 #include <inttypes.h>
 
+#include "viewporter-server-protocol.h"
+
 #define COMPOSITOR_VERSION 4
 
 E_API int E_EVENT_WAYLAND_GLOBAL_ADD = -1;
@@ -1548,9 +1550,41 @@ _e_comp_wl_buffer_transform_orient(int transform)
      }
 }
 
+/* Whether surface coordinates are other than buffer ones
+ * divided by the buffer scale. */
+static Eina_Bool
+_e_comp_wl_buffer_reshaped(const E_Client *ec)
+{
+   return (_e_comp_wl_buffer_transform_get(ec) != WL_OUTPUT_TRANSFORM_NORMAL) ||
+          ec->comp_data->viewport.src_w || ec->comp_data->viewport.dst_w;
+}
+
+/* The part of the buffer a viewport source shows, in pixels of the
+ * turned buffer, which is what evas crops. Fractional edges take the whole
+ * pixels they touch. */
+static void
+_e_comp_wl_viewport_crop_set(E_Client *ec)
+{
+   E_Comp_Wl_Viewport *vp = &ec->comp_data->viewport;
+   int scale = ec->comp_data->buffer_scale;
+   double x1, y1, x2, y2;
+
+   if (!vp->src_w)
+     {
+        e_comp_object_content_crop_set(ec->frame, 0, 0, 0, 0);
+        return;
+     }
+   x1 = floor(wl_fixed_to_double(vp->src_x) * scale);
+   y1 = floor(wl_fixed_to_double(vp->src_y) * scale);
+   x2 = ceil(wl_fixed_to_double(vp->src_x + vp->src_w) * scale);
+   y2 = ceil(wl_fixed_to_double(vp->src_y + vp->src_h) * scale);
+   e_comp_object_content_crop_set(ec->frame, x1, y1, x2 - x1, y2 - y1);
+}
+
 static void
 _e_comp_wl_surface_state_size_update(E_Client *ec, E_Comp_Wl_Surface_State *state)
 {
+   E_Comp_Wl_Viewport *vp = &ec->comp_data->viewport;
    Eina_Rectangle *window;
    int w, h, transform;
 
@@ -1561,9 +1595,17 @@ _e_comp_wl_surface_state_size_update(E_Client *ec, E_Comp_Wl_Surface_State *stat
      state->bw = h, state->bh = w;
    else
      state->bw = w, state->bh = h;
-   state->bw = (state->bw / ec->comp_data->buffer_scale) * e_comp_wl_client_scale_get(ec);
-   state->bh = (state->bh / ec->comp_data->buffer_scale) * e_comp_wl_client_scale_get(ec);
+   state->bw /= ec->comp_data->buffer_scale;
+   state->bh /= ec->comp_data->buffer_scale;
+   /* A source alone is a whole number of pixels: the commit checked it. */
+   if (vp->src_w)
+     state->bw = wl_fixed_to_int(vp->src_w), state->bh = wl_fixed_to_int(vp->src_h);
+   if (vp->dst_w)
+     state->bw = vp->dst_w, state->bh = vp->dst_h;
+   state->bw *= e_comp_wl_client_scale_get(ec);
+   state->bh *= e_comp_wl_client_scale_get(ec);
    e_comp_object_content_size_set(ec->frame, state->bw, state->bh);
+   _e_comp_wl_viewport_crop_set(ec);
    e_comp_object_content_orient_set(ec->frame,
                                     _e_comp_wl_buffer_transform_orient(transform));
    if (e_client_has_xwindow(ec) || e_comp_object_frame_exists(ec->frame)) return;
@@ -1699,6 +1741,49 @@ _e_comp_wl_surface_input_passthrough_update(E_Client *ec, Eina_Bool parent_unmap
      }
 }
 
+/* wp_viewport raises its errors when the surface state is applied: the
+ * buffer and the destination may come in the same commit as the source.
+ * A source both fractional and outside the buffer is out_of_buffer. */
+static Eina_Bool
+_e_comp_wl_viewport_check(E_Client *ec, E_Comp_Wl_Surface_State *state)
+{
+   E_Comp_Wl_Viewport *vp = &state->viewport;
+   int w = 0, h = 0;
+
+   if (!vp->src_w) return EINA_TRUE;
+   if (state->new_attach)
+     {
+        if (!state->buffer) return EINA_TRUE;
+        w = state->buffer->w, h = state->buffer->h;
+     }
+   else if (!e_pixmap_size_get(ec->pixmap, &w, &h))
+     return EINA_TRUE;
+   if (state->buffer_transform & 1)
+     {
+        int tmp = w;
+
+        w = h, h = tmp;
+     }
+   if ((vp->src_x + vp->src_w > wl_fixed_from_double((double)w / state->buffer_scale)) ||
+       (vp->src_y + vp->src_h > wl_fixed_from_double((double)h / state->buffer_scale)))
+     {
+        wl_resource_post_error(ec->comp_data->viewport_resource,
+                               WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
+                               "source rectangle outside of the buffer");
+        return EINA_FALSE;
+     }
+   if ((!vp->dst_w) &&
+       ((wl_fixed_from_int(wl_fixed_to_int(vp->src_w)) != vp->src_w) ||
+        (wl_fixed_from_int(wl_fixed_to_int(vp->src_h)) != vp->src_h)))
+     {
+        wl_resource_post_error(ec->comp_data->viewport_resource,
+                               WP_VIEWPORT_ERROR_BAD_SIZE,
+                               "fractional source size without a destination");
+        return EINA_FALSE;
+     }
+   return EINA_TRUE;
+}
+
 static void
 _e_comp_wl_surface_state_commit(E_Client *ec, E_Comp_Wl_Surface_State *state)
 {
@@ -1720,9 +1805,12 @@ _e_comp_wl_surface_state_commit(E_Client *ec, E_Comp_Wl_Surface_State *state)
         return;
      }
 
+   if (!_e_comp_wl_viewport_check(ec, state)) return;
+
    ec->comp_data->in_commit = 1;
    ec->comp_data->buffer_scale = state->buffer_scale;
    ec->comp_data->buffer_transform = state->buffer_transform;
+   ec->comp_data->viewport = state->viewport;
    if (ec->ignored && ec->comp_data->shell.surface)
      {
         EC_CHANGED(ec);
@@ -1994,12 +2082,11 @@ _e_comp_wl_surface_state_commit(E_Client *ec, E_Comp_Wl_Surface_State *state)
    e_comp_wl_presentation_feedback_commit(ec, &state->presentation_feedbacks);
 
    /* put state damages into surface */
-   if ((!e_comp->nocomp) && (ec->frame) &&
-       (_e_comp_wl_buffer_transform_get(ec) != WL_OUTPUT_TRANSFORM_NORMAL))
+   if ((!e_comp->nocomp) && (ec->frame) && _e_comp_wl_buffer_reshaped(ec))
      {
         int pw = 0, ph = 0;
 
-        /* Damage is in the turned buffer: redraw all of it. */
+        /* Damage is in the turned or cropped buffer: redraw all of it. */
         EINA_LIST_FREE(state->buffer_damages, dmg)
           eina_rectangle_free(dmg);
         EINA_LIST_FREE(state->damages, dmg)
@@ -2027,9 +2114,9 @@ _e_comp_wl_surface_state_commit(E_Client *ec, E_Comp_Wl_Surface_State *state)
    /* put state opaque into surface */
    if (state->opaque)
      {
-        /* Only a hint, and in surface coordinates that a turned buffer
-         * does not share: drop it. */
-        if (_e_comp_wl_buffer_transform_get(ec) != WL_OUTPUT_TRANSFORM_NORMAL)
+        /* Only a hint, and in surface coordinates that a turned or cropped
+         * buffer does not share: drop it. */
+        if (_e_comp_wl_buffer_reshaped(ec))
           eina_tiler_clear(state->opaque);
         if (!eina_tiler_empty(state->opaque))
           {
@@ -2707,6 +2794,7 @@ _e_comp_wl_subsurface_commit_to_cache(E_Client *ec)
    cdata->pending.buffer_damages = NULL;
    sdata->cached.buffer_scale = cdata->pending.buffer_scale;
    sdata->cached.buffer_transform = cdata->pending.buffer_transform;
+   sdata->cached.viewport = cdata->pending.viewport;
 
    if (cdata->pending.new_attach)
      {
@@ -3179,6 +3267,8 @@ _e_comp_wl_client_cb_del(void *data EINA_UNUSED, E_Client *ec)
    e_comp_wl_extension_pointer_unconstrain(ec);
    if (ec->comp_data->shortcuts_inhibitor)
      wl_resource_set_user_data(ec->comp_data->shortcuts_inhibitor, NULL);
+   if (ec->comp_data->viewport_resource)
+     wl_resource_set_user_data(ec->comp_data->viewport_resource, NULL);
    _e_comp_wl_touch_points_drop(ec);
 
    /* remove sub list */
diff --git a/src/bin/e_comp_wl.h b/src/bin/e_comp_wl.h
index 612baa6f2..6d5e01d21 100644
--- a/src/bin/e_comp_wl.h
+++ b/src/bin/e_comp_wl.h
@@ -74,6 +74,15 @@ struct _E_Comp_Wl_Buffer
    Eina_Bool destroyed;
 };
 
+/* A wp_viewport's source rectangle, in surface-local units before the
+ * viewport, and its destination size. A width of 0 means unset: a set
+ * source or destination is never empty. */
+typedef struct _E_Comp_Wl_Viewport
+{
+   wl_fixed_t src_x, src_y, src_w, src_h;
+   int dst_w, dst_h;
+} E_Comp_Wl_Viewport;
+
 struct _E_Comp_Wl_Surface_State
 {
    int sx, sy;
@@ -87,6 +96,7 @@ struct _E_Comp_Wl_Surface_State
    Eina_Tiler *input, *opaque;
    int buffer_scale;
    int buffer_transform;
+   E_Comp_Wl_Viewport viewport;
    Eina_Bool new_attach E_BITFIELD;
    Eina_Bool has_data E_BITFIELD;
 };
@@ -175,6 +185,10 @@ typedef struct E_Comp_Wl_Extension_Data
      {
         struct wl_global *global;
      } wp_presentation;
+   struct
+     {
+        struct wl_global *global;
+     } wp_viewporter;
    struct
      {
         struct wl_global *global;
@@ -377,11 +391,15 @@ struct _E_Comp_Wl_Client_Data
      } shell;
 
    E_Comp_Wl_Surface_State pending;
-   /* The wl_surface.set_buffer_scale and set_buffer_transform in effect,
-    * and the surface size the last commit worked out, in canvas pixels. */
+   /* The wl_surface.set_buffer_scale, set_buffer_transform and wp_viewport
+    * in effect, and the surface size the last commit worked out, in canvas
+    * pixels. */
    int buffer_scale;
    int buffer_transform;
+   E_Comp_Wl_Viewport viewport;
    int surface_w, surface_h;
+   /* The surface's wp_viewport: there is at most one. */
+   struct wl_resource *viewport_resource;
 
    Eina_List *frames;
    /* wp_presentation_feedback of the committed content, which is waiting
diff --git a/src/bin/e_comp_wl_extensions.c b/src/bin/e_comp_wl_extensions.c
index 2cc10e29e..079582b3f 100644
--- a/src/bin/e_comp_wl_extensions.c
+++ b/src/bin/e_comp_wl_extensions.c
@@ -13,6 +13,7 @@
 #include "idle-inhibit-unstable-v1-server-protocol.h"
 #include "presentation-time-server-protocol.h"
 #include "keyboard-shortcuts-inhibit-unstable-v1-server-protocol.h"
+#include "viewporter-server-protocol.h"
 
 /* mutter uses 32, seems reasonable */
 #define HANDLE_LEN 32
@@ -1682,6 +1683,123 @@ static const struct zwp_keyboard_shortcuts_inhibit_manager_v1_interface _e_zwp_k
    _e_zwp_keyboard_shortcuts_inhibit_manager_v1_cb_inhibit_shortcuts,
 };
 
+/* A wp_viewport only fills in its surface's pending state: the commit
+ * applies it and raises the errors that depend on the buffer. Its user
+ * data is the E_Client, NULL once the surface is gone. */
+static void
+_e_wp_viewport_cb_destroy(struct wl_client *client EINA_UNUSED, struct wl_resource *resource)
+{
+   wl_resource_destroy(resource);
+}
+
+static void
+_e_wp_viewport_cb_resource_destroy(struct wl_resource *resource)
+{
+   E_Client *ec = wl_resource_get_user_data(resource);
+
+   if (!ec) return;
+   ec->comp_data->viewport_resource = NULL;
+   memset(&ec->comp_data->pending.viewport, 0, sizeof(E_Comp_Wl_Viewport));
+}
+
+static void
+_e_wp_viewport_cb_set_source(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, wl_fixed_t x, wl_fixed_t y, wl_fixed_t w, wl_fixed_t h)
+{
+   E_Client *ec = wl_resource_get_user_data(resource);
+   E_Comp_Wl_Viewport *vp;
+
+   if (!ec)
+     {
+        wl_resource_post_error(resource, WP_VIEWPORT_ERROR_NO_SURFACE,
+                               "the surface is gone");
+        return;
+     }
+   vp = &ec->comp_data->pending.viewport;
+   if ((x == wl_fixed_from_int(-1)) && (y == wl_fixed_from_int(-1)) &&
+       (w == wl_fixed_from_int(-1)) && (h == wl_fixed_from_int(-1)))
+     {
+        vp->src_x = vp->src_y = vp->src_w = vp->src_h = 0;
+        return;
+     }
+   if ((x < 0) || (y < 0) || (w <= 0) || (h <= 0))
+     {
+        wl_resource_post_error(resource, WP_VIEWPORT_ERROR_BAD_VALUE,
+                               "source rectangle is not positive");
+        return;
+     }
+   vp->src_x = x, vp->src_y = y, vp->src_w = w, vp->src_h = h;
+}
+
+static void
+_e_wp_viewport_cb_set_destination(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, int32_t w, int32_t h)
+{
+   E_Client *ec = wl_resource_get_user_data(resource);
+   E_Comp_Wl_Viewport *vp;
+
+   if (!ec)
+     {
+        wl_resource_post_error(resource, WP_VIEWPORT_ERROR_NO_SURFACE,
+                               "the surface is gone");
+        return;
+     }
+   vp = &ec->comp_data->pending.viewport;
+   if ((w == -1) && (h == -1))
+     {
+        vp->dst_w = vp->dst_h = 0;
+        return;
+     }
+   if ((w <= 0) || (h <= 0))
+     {
+        wl_resource_post_error(resource, WP_VIEWPORT_ERROR_BAD_VALUE,
+                               "destination size is not positive");
+        return;
+     }
+   vp->dst_w = w, vp->dst_h = h;
+}
+
+static const struct wp_viewport_interface _e_wp_viewport_interface =
+{
+   _e_wp_viewport_cb_destroy,
+   _e_wp_viewport_cb_set_source,
+   _e_wp_viewport_cb_set_destination,
+};
+
+static void
+_e_wp_viewporter_cb_destroy(struct wl_client *client EINA_UNUSED, struct wl_resource *resource)
+{
+   wl_resource_destroy(resource);
+}
+
+static void
+_e_wp_viewporter_cb_get_viewport(struct wl_client *client, struct wl_resource *resource, uint32_t id, struct wl_resource *surface)
+{
+   E_Client *ec = wl_resource_get_user_data(surface);
+   struct wl_resource *res;
+
+   if ((ec) && (ec->comp_data->viewport_resource))
+     {
+        wl_resource_post_error(resource, WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
+                               "the surface already has a viewport");
+        return;
+     }
+   res = wl_resource_create(client, &wp_viewport_interface,
+                            wl_resource_get_version(resource), id);
+   if (!res)
+     {
+        wl_resource_post_no_memory(resource);
+        return;
+     }
+   wl_resource_set_implementation(res, &_e_wp_viewport_interface, ec,
+                                  _e_wp_viewport_cb_resource_destroy);
+   if (ec) ec->comp_data->viewport_resource = res;
+}
+
+static const struct wp_viewporter_interface _e_wp_viewporter_interface =
+{
+   _e_wp_viewporter_cb_destroy,
+   _e_wp_viewporter_cb_get_viewport,
+};
+
 #define GLOBAL_BIND_CB(NAME, IFACE, ...) \
 static void \
 _e_comp_wl_##NAME##_cb_bind(struct wl_client *client, void *data EINA_UNUSED, uint32_t version, uint32_t id) \
@@ -1710,6 +1828,7 @@ GLOBAL_BIND_CB(zxdg_output_manager_v1, zxdg_output_manager_v1_interface)
 GLOBAL_BIND_CB(xdg_activation_v1, xdg_activation_v1_interface)
 GLOBAL_BIND_CB(zwp_idle_inhibit_manager_v1, zwp_idle_inhibit_manager_v1_interface)
 GLOBAL_BIND_CB(zwp_keyboard_shortcuts_inhibit_manager_v1, zwp_keyboard_shortcuts_inhibit_manager_v1_interface)
+GLOBAL_BIND_CB(wp_viewporter, wp_viewporter_interface)
 GLOBAL_BIND_CB(wp_presentation, wp_presentation_interface,
      wp_presentation_send_clock_id(res, CLOCK_MONOTONIC);
 )
@@ -1818,6 +1937,7 @@ e_comp_wl_extensions_init(void)
    GLOBAL_CREATE_OR_RETURN(zwp_idle_inhibit_manager_v1, zwp_idle_inhibit_manager_v1_interface, 1);
    GLOBAL_CREATE_OR_RETURN(wp_presentation, wp_presentation_interface, 1);
    GLOBAL_CREATE_OR_RETURN(zwp_keyboard_shortcuts_inhibit_manager_v1, zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1);
+   GLOBAL_CREATE_OR_RETURN(wp_viewporter, wp_viewporter_interface, 1);
    if (!e_comp_wl_text_input_init()) return EINA_FALSE;
    e_comp_wl_privileges_init();
 
diff --git a/src/bin/generated/meson.build b/src/bin/generated/meson.build
index 9adcb4c06..0c0802299 100644
--- a/src/bin/generated/meson.build
+++ b/src/bin/generated/meson.build
@@ -10,6 +10,7 @@ protos = [
   '@0@/unstable/xdg-output/xdg-output-unstable-v1.xml'.format(dir_wayland_protocols),
   '@0@/staging/xdg-activation/xdg-activation-v1.xml'.format(dir_wayland_protocols),
   '@0@/stable/presentation-time/presentation-time.xml'.format(dir_wayland_protocols),
+  '@0@/stable/viewporter/viewporter.xml'.format(dir_wayland_protocols),
   '@0@/unstable/keyboard-shortcuts-inhibit/keyboard-shortcuts-inhibit-unstable-v1.xml'.format(dir_wayland_protocols),
   '@0@/unstable/idle-inhibit/idle-inhibit-unstable-v1.xml'.format(dir_wayland_protocols),
   '@0@/unstable/primary-selection/primary-selection-unstable-v1.xml'.format(dir_wayland_protocols),
diff --git a/src/tests/wayland/globals.expected b/src/tests/wayland/globals.expected
index 3c2b3ff91..ce13a074e 100644
--- a/src/tests/wayland/globals.expected
+++ b/src/tests/wayland/globals.expected
@@ -8,6 +8,7 @@ wl_shell	1
 wl_shm	>=1
 wl_subcompositor	1
 wp_presentation	1
+wp_viewporter	1
 xdg_activation_v1	1
 xdg_toplevel_drag_manager_v1	1
 xdg_wm_base	6
diff --git a/src/tests/wayland/meson.build b/src/tests/wayland/meson.build
index 46ae5b868..b179cfe50 100644
--- a/src/tests/wayland/meson.build
+++ b/src/tests/wayland/meson.build
@@ -32,6 +32,7 @@ foreach p: [
   '@0@/unstable/xdg-output/xdg-output-unstable-v1.xml'.format(dir_wayland_protocols),
   '@0@/staging/xdg-activation/xdg-activation-v1.xml'.format(dir_wayland_protocols),
   '@0@/stable/presentation-time/presentation-time.xml'.format(dir_wayland_protocols),
+  '@0@/stable/viewporter/viewporter.xml'.format(dir_wayland_protocols),
   '@0@/staging/xdg-toplevel-drag/xdg-toplevel-drag-v1.xml'.format(dir_wayland_protocols),
   '@0@/unstable/keyboard-shortcuts-inhibit/keyboard-shortcuts-inhibit-unstable-v1.xml'.format(dir_wayland_protocols),
   '@0@/unstable/idle-inhibit/idle-inhibit-unstable-v1.xml'.format(dir_wayland_protocols),
@@ -91,6 +92,7 @@ wl_protocol_tests = [
   ['buffer-scale', 'test_buffer_scale.c'],
   ['output-scale', 'test_output_scale.c'],
   ['buffer-transform', 'test_buffer_transform.c'],
+  ['viewporter', 'test_viewporter.c'],
   ['activation', 'test_activation.c'],
   ['activation-expiry', 'test_activation.c',
    {'E_TEST_ACTIVATION_TOKEN_LIFETIME': '1'}],
diff --git a/src/tests/wayland/test_viewporter.c b/src/tests/wayland/test_viewporter.c
new file mode 100644
index 000000000..726ceafe3
--- /dev/null
+++ b/src/tests/wayland/test_viewporter.c
@@ -0,0 +1,310 @@
+/* wp_viewporter: a source rectangle crops the buffer and a destination
+ * sizes the surface, each on the commit after it was set. */
+#define _GNU_SOURCE 1
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <time.h>
+
+#include "e_wl_testkit.h"
+#include "viewporter-client-protocol.h"
+
+#define PROG "test-viewporter"
+#define APP_ID "e.test.viewporter"
+
+#define TL 0xffff0000
+#define TR 0xff00ff00
+#define BL 0xff0000ff
+#define BR 0xffffff00
+
+typedef struct
+{
+   Tk *tk;
+   struct wl_test *tester;
+   struct wl_shm *shm;
+   struct wp_viewporter *viewporter;
+   int configured;
+   unsigned int argb;
+   int answered;
+} Ctx;
+
+static void
+_pixel(void *data, struct wl_test *t __attribute__((unused)),
+       int32_t x __attribute__((unused)), int32_t y __attribute__((unused)),
+       uint32_t argb)
+{
+   Ctx *ctx = data;
+
+   ctx->argb = argb;
+   ctx->answered = 1;
+}
+
+/* Only the requests this test sends are answered on its own wl_test. */
+static const struct wl_test_listener _tester_listener =
+{
+   .pixel = _pixel
+};
+
+static void
+_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial)
+{
+   Ctx *ctx = data;
+
+   xdg_surface_ack_configure(xdg_surface, serial);
+   ctx->configured = 1;
+}
+
+static const struct xdg_surface_listener _xdg_surface_listener =
+{
+   _configure
+};
+
+static void
+_connect(Ctx *ctx)
+{
+   ctx->tk = tk_connect(PROG);
+   ctx->shm = tk_bind(ctx->tk, &wl_shm_interface, 1);
+   ctx->viewporter = tk_bind(ctx->tk, &wp_viewporter_interface, 1);
+   if (!ctx->viewporter) tk_fail(ctx->tk, "no wp_viewporter");
+}
+
+/* A buffer of four solid quadrants. */
+static struct wl_buffer *
+_buffer(Ctx *ctx, int w, int h)
+{
+   uint32_t colors[4] = { TL, TR, BL, BR };
+   struct wl_shm_pool *pool;
+   struct wl_buffer *buffer;
+   uint32_t *pixels;
+   int fd, x, y;
+
+   fd = memfd_create(PROG, MFD_CLOEXEC);
+   if ((fd < 0) || (ftruncate(fd, w * h * 4) < 0))
+     tk_fail(ctx->tk, "cannot make a %dx%d buffer", w, h);
+   pixels = mmap(NULL, w * h * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+   if (pixels == MAP_FAILED) tk_fail(ctx->tk, "cannot map a buffer");
+   for (y = 0; y < h; y++)
+     for (x = 0; x < w; x++)
+       pixels[(y * w) + x] = colors[((y >= h / 2) * 2) + (x >= w / 2)];
+   munmap(pixels, w * h * 4);
+   pool = wl_shm_create_pool(ctx->shm, fd, w * h * 4);
+   buffer = wl_shm_pool_create_buffer(pool, 0, w, h, w * 4,
+                                      WL_SHM_FORMAT_ARGB8888);
+   wl_shm_pool_destroy(pool);
+   close(fd);
+   return buffer;
+}
+
+static unsigned int
+_pixel_get(Ctx *ctx, int x, int y)
+{
+   ctx->answered = 0;
+   wl_test_get_pixel(ctx->tester, x, y);
+   tk_sync(ctx->tk);
+   if (!ctx->answered) tk_fail(ctx->tk, "no answer to get_pixel");
+   return ctx->argb;
+}
+
+/* The theme animates a window being shown. */
+static void
+_pixel_wait(Ctx *ctx, int x, int y, unsigned int want, const char *what)
+{
+   struct timespec step = { 0, 50000000 };
+   unsigned int argb = 0;
+   int i;
+
+   for (i = 0; i < TK_WAIT_SECONDS * 20; i++)
+     {
+        argb = _pixel_get(ctx, x, y);
+        if (argb == want) return;
+        nanosleep(&step, NULL);
+     }
+   tk_fail(ctx->tk, "%s: pixel %d,%d is %#x, wanted %#x", what, x, y,
+           argb, want);
+}
+
+/* Checks the window's size, and the colour near each of its corners. */
+static void
+_check(Ctx *ctx, struct wl_surface *surface, int w, int h,
+       const unsigned int corners[4], const char *what)
+{
+   int x, y, sw, sh;
+
+   tk_settle(ctx->tk);
+   tk_surface_geometry(ctx->tk, surface, &x, &y, &sw, &sh);
+   if ((sw != w) || (sh != h))
+     tk_fail(ctx->tk, "%s: the window is %dx%d, wanted %dx%d", what, sw, sh,
+             w, h);
+   _pixel_wait(ctx, x + 10, y + 10, corners[0], what);
+   _pixel_wait(ctx, x + w - 10, y + 10, corners[1], what);
+   _pixel_wait(ctx, x + 10, y + h - 10, corners[2], what);
+   _pixel_wait(ctx, x + w - 10, y + h - 10, corners[3], what);
+}
+
+static void
+_window_test(void)
+{
+   static const unsigned int all_tr[4] = { TR, TR, TR, TR };
+   static const unsigned int all_br[4] = { BR, BR, BR, BR };
+   static const unsigned int all_bl[4] = { BL, BL, BL, BL };
+   static const unsigned int quadrants[4] = { TL, TR, BL, BR };
+   static const unsigned int turned[4] = { BL, TL, BR, TR };
+   struct xdg_surface *xdg_surface;
+   struct xdg_toplevel *toplevel;
+   struct wp_viewport *viewport;
+   struct wl_surface *surface;
+   Ctx ctx = { 0 };
+
+   _connect(&ctx);
+   ctx.tester = tk_bind(ctx.tk, &wl_test_interface, 9);
+   if (!ctx.tester) tk_fail(ctx.tk, "no wl_test version 9");
+   wl_test_add_listener(ctx.tester, &_tester_listener, &ctx);
+
+   surface = tk_surface_new(ctx.tk);
+   viewport = wp_viewporter_get_viewport(ctx.viewporter, surface);
+   xdg_surface = xdg_wm_base_get_xdg_surface(tk_wm_base(ctx.tk), surface);
+   xdg_surface_add_listener(xdg_surface, &_xdg_surface_listener, &ctx);
+   toplevel = xdg_surface_get_toplevel(xdg_surface);
+   xdg_toplevel_set_app_id(toplevel, APP_ID);
+   wl_surface_commit(surface);
+   while (!ctx.configured)
+     wl_display_dispatch(tk_display(ctx.tk));
+
+   /* The cursor is drawn on the same canvas. */
+   tk_pointer_warp(ctx.tk, 1023, 767);
+
+   wp_viewport_set_source(viewport, wl_fixed_from_int(100), 0,
+                          wl_fixed_from_int(100), wl_fixed_from_int(100));
+   wl_surface_attach(surface, _buffer(&ctx, 200, 200), 0, 0);
+   wl_surface_damage_buffer(surface, 0, 0, 200, 200);
+   wl_surface_commit(surface);
+   tk_settle(ctx.tk);
+   tk_expect(ctx.tk, APP_ID);
+   _check(&ctx, surface, 100, 100, all_tr,
+          "the top right quadrant of a 200x200 buffer as source");
+
+   /* A viewport needs no new buffer, and a destination allows a fractional
+    * source. */
+   wp_viewport_set_source(viewport, wl_fixed_from_double(100.5), 0,
+                          wl_fixed_from_double(99.5), wl_fixed_from_int(100));
+   wp_viewport_set_destination(viewport, 300, 150);
+   wl_surface_commit(surface);
+   _check(&ctx, surface, 300, 150, all_tr,
+          "a fractional source scaled to a 300x150 destination");
+
+   wp_viewport_set_source(viewport, wl_fixed_from_int(-1), wl_fixed_from_int(-1),
+                          wl_fixed_from_int(-1), wl_fixed_from_int(-1));
+   wl_surface_commit(surface);
+   _check(&ctx, surface, 300, 150, quadrants,
+          "the whole buffer scaled to the destination");
+
+   wp_viewport_set_destination(viewport, -1, -1);
+   wl_surface_commit(surface);
+   _check(&ctx, surface, 200, 200, quadrants, "the viewport unset");
+
+   /* The source is in surface-local units, after the buffer scale. */
+   wl_surface_set_buffer_scale(surface, 2);
+   wp_viewport_set_source(viewport, wl_fixed_from_int(50), wl_fixed_from_int(50),
+                          wl_fixed_from_int(50), wl_fixed_from_int(50));
+   wl_surface_commit(surface);
+   _check(&ctx, surface, 50, 50, all_br,
+          "the bottom right quarter of a buffer of scale 2 as source");
+
+   /* ... and after the buffer transform: the top left of the surface is the
+    * bottom left of a buffer turned by 90 degrees. */
+   wl_surface_set_buffer_scale(surface, 1);
+   wl_surface_set_buffer_transform(surface, WL_OUTPUT_TRANSFORM_90);
+   wp_viewport_set_source(viewport, 0, 0,
+                          wl_fixed_from_int(100), wl_fixed_from_int(100));
+   wl_surface_commit(surface);
+   _check(&ctx, surface, 100, 100, all_bl,
+          "the top left of a turned buffer as source");
+
+   /* Destroying the viewport unsets it on the next commit. */
+   wp_viewport_destroy(viewport);
+   wl_surface_commit(surface);
+   _check(&ctx, surface, 200, 200, turned, "the viewport destroyed");
+
+   tk_disconnect(ctx.tk);
+}
+
+typedef enum
+{
+   ERROR_OUT_OF_BUFFER,
+   ERROR_OUT_OF_TURNED_BUFFER,
+   ERROR_BAD_SIZE,
+   ERROR_BAD_VALUE,
+   ERROR_VIEWPORT_EXISTS,
+   ERROR_NO_SURFACE,
+} Error_Case;
+
+/* Each error ends the connection, so each one gets its own. */
+static void
+_error_test(Error_Case which, int code, const char *what)
+{
+   struct wp_viewport *viewport;
+   struct wl_surface *surface;
+   Ctx ctx = { 0 };
+   int error;
+
+   _connect(&ctx);
+   surface = tk_surface_new(ctx.tk);
+   viewport = wp_viewporter_get_viewport(ctx.viewporter, surface);
+   wl_surface_attach(surface, _buffer(&ctx, 200, 100), 0, 0);
+   switch (which)
+     {
+      case ERROR_OUT_OF_BUFFER:
+        wp_viewport_set_source(viewport, wl_fixed_from_int(150), 0,
+                               wl_fixed_from_int(100), wl_fixed_from_int(100));
+        wl_surface_commit(surface);
+        break;
+      case ERROR_OUT_OF_TURNED_BUFFER:
+        /* A quarter turn makes the surface 100x200. */
+        wl_surface_set_buffer_transform(surface, WL_OUTPUT_TRANSFORM_90);
+        wp_viewport_set_source(viewport, 0, 0,
+                               wl_fixed_from_int(150), wl_fixed_from_int(100));
+        wl_surface_commit(surface);
+        break;
+      case ERROR_BAD_SIZE:
+        wp_viewport_set_source(viewport, 0, 0, wl_fixed_from_double(50.5),
+                               wl_fixed_from_int(50));
+        wl_surface_commit(surface);
+        break;
+      case ERROR_BAD_VALUE:
+        wp_viewport_set_destination(viewport, 0, 10);
+        break;
+      case ERROR_VIEWPORT_EXISTS:
+        wp_viewporter_get_viewport(ctx.viewporter, surface);
+        break;
+      case ERROR_NO_SURFACE:
+        wl_surface_destroy(surface);
+        wp_viewport_set_destination(viewport, 10, 10);
+        break;
+     }
+   error = tk_protocol_error(ctx.tk, NULL);
+   if (error != code)
+     tk_fail(ctx.tk, "%s: error %d, wanted %d", what, error, code);
+   tk_disconnect(ctx.tk);
+}
+
+int
+main(void)
+{
+   _window_test();
+   _error_test(ERROR_OUT_OF_BUFFER, WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
+               "a source outside of the buffer");
+   _error_test(ERROR_OUT_OF_TURNED_BUFFER, WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
+               "a source outside of a turned buffer");
+   _error_test(ERROR_BAD_SIZE, WP_VIEWPORT_ERROR_BAD_SIZE,
+               "a fractional source without a destination");
+   _error_test(ERROR_BAD_VALUE, WP_VIEWPORT_ERROR_BAD_VALUE,
+               "an empty destination");
+   _error_test(ERROR_VIEWPORT_EXISTS, WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
+               "a second viewport for a surface");
+   _error_test(ERROR_NO_SURFACE, WP_VIEWPORT_ERROR_NO_SURFACE,
+               "a viewport outliving its surface");
+   printf(PROG ": ok\n");
+   return 0;
+}
diff --git a/src/tests/wlcs/e_wlcs.c b/src/tests/wlcs/e_wlcs.c
index a85b1e033..3d3f8da35 100644
--- a/src/tests/wlcs/e_wlcs.c
+++ b/src/tests/wlcs/e_wlcs.c
@@ -715,6 +715,7 @@ static const WlcsExtensionDescriptor _extensions[] =
    { "wl_seat", 5 },
    { "wl_output", 2 },
    { "wl_data_device_manager", 3 },
+   { "wp_viewporter", 1 },
    { "xdg_wm_base", 6 },
    { "zxdg_shell_v6", 1 },
    { "wl_shell", 1 },

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to