summarylogtreecommitdiffstats
path: root/hidpi.patch
blob: 4f07e0213897fc7d607beae921d5a34af6b7c4ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
diff --git a/hw/xwayland/xwayland-output.c b/hw/xwayland/xwayland-output.c
index f75591500..10c165ea6 100644
--- a/hw/xwayland/xwayland-output.c
+++ b/hw/xwayland/xwayland-output.c
@@ -192,6 +192,13 @@ update_screen_size(struct xwl_screen *xwl_screen, int width, int height)
     if (xwl_screen_get_height(xwl_screen) != height)
         xwl_screen->height = height;
 
+    /* In rootless mode, if global_surface_scale is set, we must scale accordingly here */
+    /* This is because xscale is never set if we are not running rootful */
+    if (xwl_screen->rootless && xwl_screen->global_surface_scale > 1) {
+        width *= xwl_screen->global_surface_scale;
+        height *= xwl_screen->global_surface_scale;
+    }
+
     if (xwl_screen->root_clip_mode == ROOT_CLIP_FULL)
         SetRootClip(xwl_screen->screen, ROOT_CLIP_NONE);
 
@@ -615,14 +622,15 @@ maybe_update_fullscreen_state(struct xwl_output *xwl_output)
     }
 }
 
-static void
-apply_output_change(struct xwl_output *xwl_output)
+void
+xwl_output_apply_changes(struct xwl_output *xwl_output)
 {
     struct xwl_screen *xwl_screen = xwl_output->xwl_screen;
     struct xwl_output *it;
     int mode_width, mode_height, count;
     int width = 0, height = 0, has_this_output = 0;
     RRModePtr *randr_modes;
+    int32_t scale = xwl_screen->global_surface_scale;
 
     /* Clear out the "done" received flags */
     xwl_output->wl_output_done = FALSE;
@@ -641,10 +649,10 @@ apply_output_change(struct xwl_output *xwl_output)
     }
     if (xwl_output->randr_output) {
         /* Build a fresh modes array using the current refresh rate */
-        randr_modes = output_get_rr_modes(xwl_output, mode_width, mode_height, &count);
+        randr_modes = output_get_rr_modes(xwl_output, mode_width * scale, mode_height * scale, &count);
         RROutputSetModes(xwl_output->randr_output, randr_modes, count, 1);
         RRCrtcNotify(xwl_output->randr_crtc, randr_modes[0],
-                     xwl_output->x, xwl_output->y,
+                     xwl_output->x * scale, xwl_output->y * scale,
                      xwl_output->rotation, NULL, 1, &xwl_output->randr_output);
         /* RROutputSetModes takes ownership of the passed in modes, so we only
          * have to free the pointer array.
@@ -738,7 +746,7 @@ output_handle_done(void *data, struct wl_output *wl_output)
      */
     if (xwl_output->xdg_output_done || !xwl_output->xdg_output ||
         zxdg_output_v1_get_version(xwl_output->xdg_output) >= 3)
-        apply_output_change(xwl_output);
+        xwl_output_apply_changes(xwl_output);
 }
 
 static void
@@ -806,7 +814,7 @@ xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output)
 
     if (xwl_output->wl_output_done &&
         zxdg_output_v1_get_version(xdg_output) < 3)
-        apply_output_change(xwl_output);
+        xwl_output_apply_changes(xwl_output);
 }
 
 static void
@@ -942,6 +950,8 @@ xwl_output_create(struct xwl_screen *xwl_screen, uint32_t id,
     RROutputSetConnection(xwl_output->randr_output,
                           connected ? RR_Connected : RR_Disconnected);
 
+    xwl_output->scale = 1;
+
     /* We want the output to be in the list as soon as created so we can
      * use it when binding to the xdg-output protocol...
      */
diff --git a/hw/xwayland/xwayland-output.h b/hw/xwayland/xwayland-output.h
index 0d36f459a..123d01380 100644
--- a/hw/xwayland/xwayland-output.h
+++ b/hw/xwayland/xwayland-output.h
@@ -121,4 +121,6 @@ void xwl_output_set_window_randr_emu_props(struct xwl_screen *xwl_screen,
 
 void xwl_screen_init_xdg_output(struct xwl_screen *xwl_screen);
 
+void xwl_output_apply_changes(struct xwl_output *xwl_output);
+
 #endif /* XWAYLAND_OUTPUT_H */
diff --git a/hw/xwayland/xwayland-present.c b/hw/xwayland/xwayland-present.c
index 60427cca0..0e12215e3 100644
--- a/hw/xwayland/xwayland-present.c
+++ b/hw/xwayland/xwayland-present.c
@@ -911,6 +911,7 @@ xwl_present_flip(present_vblank_ptr vblank, RegionPtr damage)
 
     /* We can flip directly to the main surface (full screen window without clips) */
     wl_surface_attach(xwl_window->surface, buffer, 0, 0);
+    wl_surface_set_buffer_scale(xwl_window->surface, xwl_window->xwl_screen->global_surface_scale);
 
     if (xorg_list_is_empty(&xwl_present_window->frame_callback_list)) {
         xorg_list_add(&xwl_present_window->frame_callback_list,
diff --git a/hw/xwayland/xwayland-screen.c b/hw/xwayland/xwayland-screen.c
index 4b575667a..ff536883a 100644
--- a/hw/xwayland/xwayland-screen.c
+++ b/hw/xwayland/xwayland-screen.c
@@ -44,6 +44,8 @@
 #include <inputstr.h>
 #include <xacestr.h>
 #include <xserver_poll.h>
+#include <xace.h>
+#include <xacestr.h>
 
 #include "xwayland-cursor.h"
 #include "xwayland-screen.h"
@@ -53,6 +55,7 @@
 #include "xwayland-pixmap.h"
 #include "xwayland-present.h"
 #include "xwayland-shm.h"
+#include "xwayland-window-buffers.h"
 #ifdef XWL_HAS_EI
 #include "xwayland-xtest.h"
 #endif
@@ -121,6 +124,12 @@ xwl_screen_has_resolution_change_emulation(struct xwl_screen *xwl_screen)
     return xwl_screen->rootless && xwl_screen_has_viewport_support(xwl_screen);
 }
 
+int
+xwl_scale_to(struct xwl_screen *xwl_screen, int value)
+{
+    return value / (double)xwl_screen->global_surface_scale + 0.5;
+}
+
 /* Return the output @ 0x0, falling back to the first output in the list */
 struct xwl_output *
 xwl_screen_get_first_output(struct xwl_screen *xwl_screen)
@@ -159,6 +168,38 @@ xwl_screen_get_height(struct xwl_screen *xwl_screen)
     return round(xwl_screen->height);
 }
 
+static void
+xwl_screen_set_global_scale_from_property(struct xwl_screen *screen,
+                                          PropertyPtr prop)
+{
+    CARD32 *propdata;
+
+    if (prop->type != XA_CARDINAL || prop->format != 32 || prop->size != 1) {
+        // TODO: handle warnings more cleanly.
+        LogMessageVerb(X_WARNING, 0, "Bad value for property %s.\n",
+                       NameForAtom(prop->propertyName));
+        return;
+    }
+
+    propdata = prop->data;
+    xwl_screen_set_global_scale(screen, propdata[0]);
+}
+
+static void
+xwl_screen_update_property(struct xwl_screen *screen,
+                           PropertyStateRec *propstate)
+{
+    switch (propstate->state) {
+    case PropertyNewValue:
+        xwl_screen_set_global_scale_from_property(screen, propstate->prop);
+        break;
+    case PropertyDelete:
+        // This seems to be causing a double free for some reason, remove it for now
+        // xwl_screen_set_global_scale(screen, 1);
+        break;
+    }
+}
+
 static void
 xwl_property_callback(CallbackListPtr *pcbl, void *closure,
                       void *calldata)
@@ -166,19 +207,24 @@ xwl_property_callback(CallbackListPtr *pcbl, void *closure,
     ScreenPtr screen = closure;
     PropertyStateRec *rec = calldata;
     struct xwl_screen *xwl_screen;
-    struct xwl_window *xwl_window;
 
     if (rec->win->drawable.pScreen != screen)
         return;
 
-    xwl_window = xwl_window_get(rec->win);
-    if (!xwl_window)
-        return;
-
     xwl_screen = xwl_screen_get(screen);
 
-    if (rec->prop->propertyName == xwl_screen->allow_commits_prop)
+    if (rec->prop->propertyName == xwl_screen->allow_commits_prop) {
+        struct xwl_window *xwl_window;
+
+        xwl_window = xwl_window_get(rec->win);
+        if (!xwl_window)
+            return;
+
         xwl_window_update_property(xwl_window, rec);
+    }
+    else if (rec->prop->propertyName == xwl_screen->global_output_scale_prop) {
+        xwl_screen_update_property(xwl_screen, rec);
+    }
 }
 
 #define readOnlyPropertyAccessMask (DixReadAccess |\
@@ -718,8 +764,14 @@ void xwl_surface_damage(struct xwl_screen *xwl_screen,
 {
     if (wl_surface_get_version(surface) >= WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION)
         wl_surface_damage_buffer(surface, x, y, width, height);
-    else
+    else {
+        x = xwl_scale_to(xwl_screen, x);
+        y = xwl_scale_to(xwl_screen, y);
+        width = xwl_scale_to(xwl_screen, width);
+        height = xwl_scale_to(xwl_screen, height);
+
         wl_surface_damage(surface, x, y, width, height);
+    }
 }
 
 void
@@ -851,10 +903,34 @@ xwl_screen_update_global_surface_scale(struct xwl_screen *xwl_screen)
     return (xwl_screen->global_surface_scale != old_scale);
 }
 
+void
+xwl_screen_set_global_scale(struct xwl_screen *xwl_screen, int32_t scale)
+{
+    struct xwl_output *it;
+    struct xwl_window *xwl_window;
+
+    xwl_screen->global_surface_scale = scale;
+
+    /* change randr resolutions and positions */
+    xorg_list_for_each_entry(it, &xwl_screen->output_list, link) {
+        xwl_output_apply_changes(it);
+    }
+
+    if (!xwl_screen->rootless && xwl_screen->screen->root) {
+        /* Clear all the buffers, so that they'll be remade with the new sizes
+         * (this doesn't occur automatically because as far as Xorg is
+         *  concerned, the window's size is the same) */
+        xorg_list_for_each_entry(xwl_window, &xwl_screen->window_list, link_window) {
+            xwl_window_buffers_dispose(xwl_window, FALSE);
+        }
+    }
+}
+
 Bool
 xwl_screen_init(ScreenPtr pScreen, int argc, char **argv)
 {
     static const char allow_commits[] = "_XWAYLAND_ALLOW_COMMITS";
+    static const char global_output_scale[] = "_XWAYLAND_GLOBAL_OUTPUT_SCALE";
     struct xwl_screen *xwl_screen;
     Pixel red_mask, blue_mask, green_mask;
     int ret, bpc, green_bpc, i;
@@ -891,6 +967,7 @@ xwl_screen_init(ScreenPtr pScreen, int argc, char **argv)
 #ifdef XWL_HAS_GLAMOR
     xwl_screen->glamor = XWL_GLAMOR_DEFAULT;
 #endif
+    xwl_screen->global_surface_scale = 1;
 
     for (i = 1; i < argc; i++) {
         if (strcmp(argv[i], "-rootless") == 0) {
@@ -1163,6 +1240,12 @@ xwl_screen_init(ScreenPtr pScreen, int argc, char **argv)
     if (xwl_screen->allow_commits_prop == BAD_RESOURCE)
         return FALSE;
 
+    xwl_screen->global_output_scale_prop = MakeAtom(global_output_scale,
+                                                    strlen(global_output_scale),
+                                                    TRUE);
+    if (xwl_screen->global_output_scale_prop == BAD_RESOURCE)
+        return FALSE;
+
     AddCallback(&PropertyStateCallback, xwl_property_callback, pScreen);
     AddCallback(&RootWindowFinalizeCallback, xwl_root_window_finalized_callback, pScreen);
     XaceRegisterCallback(XACE_PROPERTY_ACCESS, xwl_access_property_callback, pScreen);
diff --git a/hw/xwayland/xwayland-screen.h b/hw/xwayland/xwayland-screen.h
index 0800fb392..2ed4b3aca 100644
--- a/hw/xwayland/xwayland-screen.h
+++ b/hw/xwayland/xwayland-screen.h
@@ -139,6 +139,7 @@ struct xwl_screen {
     struct glamor_context *glamor_ctx;
 
     Atom allow_commits_prop;
+    Atom global_output_scale_prop;
 
     /* The preferred GLVND vendor. If NULL, "mesa" is assumed. */
     const char *glvnd_vendor;
@@ -181,5 +182,7 @@ int xwl_screen_get_next_output_serial(struct xwl_screen * xwl_screen);
 void xwl_screen_lost_focus(struct xwl_screen *xwl_screen);
 Bool xwl_screen_update_global_surface_scale(struct xwl_screen *xwl_screen);
 Bool xwl_screen_should_use_fractional_scale(struct xwl_screen *xwl_screen);
+int xwl_scale_to(struct xwl_screen *xwl_screen, int value);
+void xwl_screen_set_global_scale( struct xwl_screen *xwl_screen, int32_t scale);
 
 #endif /* XWAYLAND_SCREEN_H */
diff --git a/hw/xwayland/xwayland-window.c b/hw/xwayland/xwayland-window.c
index 05f45e16f..56e4338be 100644
--- a/hw/xwayland/xwayland-window.c
+++ b/hw/xwayland/xwayland-window.c
@@ -1317,7 +1317,8 @@ xwl_create_root_surface(struct xwl_window *xwl_window)
     }
 
     wl_region_add(region, 0, 0,
-                  window->drawable.width, window->drawable.height);
+                      xwl_scale_to(xwl_screen, window->drawable.width),
+                      xwl_scale_to(xwl_screen, window->drawable.height));
     wl_surface_set_opaque_region(xwl_window->surface, region);
     wl_region_destroy(region);
 
@@ -1955,6 +1956,7 @@ xwl_window_attach_buffer(struct xwl_window *xwl_window)
     }
 
     wl_surface_attach(xwl_window->surface, buffer, 0, 0);
+    wl_surface_set_buffer_scale(xwl_window->surface, xwl_screen->global_surface_scale);
 
     /* Arbitrary limit to try to avoid flooding the Wayland
      * connection. If we flood it too much anyway, this could