This repository was archived by the owner on Jan 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsommelier-transform.cc
490 lines (403 loc) · 16.3 KB
/
sommelier-transform.cc
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <math.h>
#include <type_traits>
#include "sommelier-tracing.h" // NOLINT(build/include_directory)
#include "sommelier-transform.h" // NOLINT(build/include_directory)
namespace {
template <
typename T,
typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void stable_scale_host_to_guest(T* value, double scale) {
*value = static_cast<T>(ceil(static_cast<double>(*value) * scale));
}
template <
typename T,
typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void stable_scale_size_host_to_guest(T* value, double scale) {
*value = static_cast<T>(floor(static_cast<double>(*value) * scale));
}
template <
typename T,
typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void stable_scale_guest_to_host(T* value, double scale) {
*value = static_cast<T>(floor(static_cast<double>(*value) / scale));
}
template <
typename T,
typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void stable_scale_size_guest_to_host(T* value, double scale) {
*value = static_cast<T>(ceil(static_cast<double>(*value) / scale));
}
} // namespace
static void sl_transform_get_scale_factors(
struct sl_context* ctx,
const struct sl_host_surface* surface,
double* scalex,
double* scaley) {
if (ctx->use_direct_scale && surface && surface->has_own_scale) {
*scalex = surface->xdg_scale_x;
*scaley = surface->xdg_scale_y;
} else if (surface && surface->output) {
*scalex = surface->output.get()->xdg_scale_x;
*scaley = surface->output.get()->xdg_scale_y;
} else {
*scalex = ctx->xdg_scale_x;
*scaley = ctx->xdg_scale_y;
}
}
static double sl_transform_direct_axis_scale(struct sl_context* ctx,
struct sl_host_surface* surface,
uint32_t axis) {
double scalex, scaley;
sl_transform_get_scale_factors(ctx, surface, &scalex, &scaley);
return (axis == 0) ? scaley : scalex;
}
static void sl_transform_direct_to_host_damage(
struct sl_context* ctx,
const struct sl_host_surface* surface,
int64_t* x,
int64_t* y,
double scale_x,
double scale_y) {
if (ctx->stable_scaling) {
stable_scale_guest_to_host(x, scale_x);
stable_scale_guest_to_host(y, scale_y);
} else {
double x_whole = trunc(static_cast<double>(*x) / scale_x);
double y_whole = trunc(static_cast<double>(*y) / scale_y);
*x = static_cast<int64_t>(x_whole);
*y = static_cast<int64_t>(y_whole);
}
}
static void sl_transform_direct_to_guest_fixed(struct sl_context* ctx,
struct sl_host_surface* surface,
wl_fixed_t* coord,
uint32_t axis) {
double scale = sl_transform_direct_axis_scale(ctx, surface, axis);
double result = wl_fixed_to_double(*coord);
result *= scale;
*coord = wl_fixed_from_double(result);
}
static void sl_transform_direct_to_guest_fixed(struct sl_context* ctx,
struct sl_host_surface* surface,
wl_fixed_t* x,
wl_fixed_t* y) {
double scale_x, scale_y;
double result_x = wl_fixed_to_double(*x);
double result_y = wl_fixed_to_double(*y);
sl_transform_get_scale_factors(ctx, surface, &scale_x, &scale_y);
result_x *= scale_x;
result_y *= scale_y;
*x = wl_fixed_from_double(result_x);
*y = wl_fixed_from_double(result_y);
}
static void sl_transform_direct_to_host_fixed(struct sl_context* ctx,
struct sl_host_surface* surface,
wl_fixed_t* coord,
uint32_t axis) {
double scale = sl_transform_direct_axis_scale(ctx, surface, axis);
double result = wl_fixed_to_double(*coord);
result /= scale;
*coord = wl_fixed_from_double(result);
}
static void sl_transform_direct_to_host_fixed(struct sl_context* ctx,
struct sl_host_surface* surface,
wl_fixed_t* x,
wl_fixed_t* y) {
double scale_x, scale_y;
sl_transform_get_scale_factors(ctx, surface, &scale_x, &scale_y);
double result_x = wl_fixed_to_double(*x);
double result_y = wl_fixed_to_double(*y);
result_x /= scale_x;
result_y /= scale_y;
*x = wl_fixed_from_double(result_x);
*y = wl_fixed_from_double(result_y);
}
static void sl_transform_direct_to_guest(struct sl_context* ctx,
struct sl_host_surface* surface,
int32_t* x,
int32_t* y) {
double scale_x, scale_y;
sl_transform_get_scale_factors(ctx, surface, &scale_x, &scale_y);
if (ctx->stable_scaling) {
stable_scale_host_to_guest(x, scale_x);
stable_scale_host_to_guest(y, scale_y);
} else {
double input_x = static_cast<double>(*x) * scale_x;
double input_y = static_cast<double>(*y) * scale_y;
double x_whole = (surface && surface->scale_round_on_x) ? lround(input_x)
: trunc(input_x);
double y_whole = (surface && surface->scale_round_on_y) ? lround(input_y)
: trunc(input_y);
*x = static_cast<int32_t>(x_whole);
*y = static_cast<int32_t>(y_whole);
}
}
static void sl_transform_direct_to_host(struct sl_context* ctx,
struct sl_host_surface* surface,
int32_t* x,
int32_t* y) {
double scale_x, scale_y;
sl_transform_get_scale_factors(ctx, surface, &scale_x, &scale_y);
if (ctx->stable_scaling) {
stable_scale_guest_to_host(x, scale_x);
stable_scale_guest_to_host(y, scale_y);
} else {
double x_whole = trunc(static_cast<double>(*x) / scale_x);
double y_whole = trunc(static_cast<double>(*y) / scale_y);
*x = static_cast<int32_t>(x_whole);
*y = static_cast<int32_t>(y_whole);
}
}
bool sl_transform_viewport_scale(struct sl_context* ctx,
struct sl_host_surface* surface,
double contents_scale,
int32_t* width,
int32_t* height) {
double scale = ctx->scale * contents_scale;
// TODO(mrisaacb): It may be beneficial to skip the set_destination call
// when the virtual and logical space match.
bool do_viewport = true;
if (surface && surface->window && surface->window->viewport_override) {
*width = surface->window->viewport_width;
*height = surface->window->viewport_height;
} else if (ctx->use_direct_scale) {
sl_transform_direct_to_host(ctx, surface, width, height);
// For very small windows (in pixels), the resulting logical dimensions
// could be 0, which will cause issues with the viewporter interface.
//
// In these cases, fix it up here by forcing the logical output
// to be at least 1 pixel
if (*width <= 0)
*width = 1;
if (*height <= 0)
*height = 1;
} else {
if (ctx->stable_scaling) {
stable_scale_size_guest_to_host(width, scale);
stable_scale_size_guest_to_host(height, scale);
} else {
*width = ceil(*width / scale);
*height = ceil(*height / scale);
}
}
return do_viewport;
}
void sl_transform_damage_coord(struct sl_context* ctx,
const struct sl_host_surface* surface,
double buffer_scalex,
double buffer_scaley,
int64_t* x1,
int64_t* y1,
int64_t* x2,
int64_t* y2) {
if (ctx->use_direct_scale) {
double scalex, scaley;
sl_transform_get_scale_factors(ctx, surface, &scalex, &scaley);
scalex *= buffer_scalex;
scaley *= buffer_scaley;
sl_transform_direct_to_host_damage(ctx, surface, x1, y1, scalex, scaley);
sl_transform_direct_to_host_damage(ctx, surface, x2, y2, scalex, scaley);
} else {
double sx = buffer_scalex * ctx->scale;
double sy = buffer_scaley * ctx->scale;
// Enclosing rect after scaling and outset by one pixel to account for
// potential filtering.
*x1 = MAX(MIN_SIZE, (*x1) - 1) / sx;
*y1 = MAX(MIN_SIZE, (*y1) - 1) / sy;
*x2 = ceil(MIN((*x2) + 1, MAX_SIZE) / sx);
*y2 = ceil(MIN((*y2) + 1, MAX_SIZE) / sy);
}
}
void sl_transform_host_to_guest(struct sl_context* ctx,
struct sl_host_surface* surface,
int32_t* x,
int32_t* y) {
if (ctx->use_direct_scale) {
sl_transform_direct_to_guest(ctx, surface, x, y);
} else {
if (ctx->stable_scaling) {
stable_scale_host_to_guest(x, ctx->scale);
stable_scale_host_to_guest(y, ctx->scale);
} else {
(*x) *= ctx->scale;
(*y) *= ctx->scale;
}
}
}
void sl_transform_host_to_guest_fixed(struct sl_context* ctx,
struct sl_host_surface* surface,
wl_fixed_t* x,
wl_fixed_t* y) {
if (ctx->use_direct_scale) {
sl_transform_direct_to_guest_fixed(ctx, surface, x, y);
} else {
double dx = wl_fixed_to_double(*x);
double dy = wl_fixed_to_double(*y);
dx *= ctx->scale;
dy *= ctx->scale;
*x = wl_fixed_from_double(dx);
*y = wl_fixed_from_double(dy);
}
}
void sl_transform_pointer(struct sl_context* ctx,
struct sl_host_surface* surface,
wl_fixed_t* x,
wl_fixed_t* y) {
sl_transform_host_to_guest_fixed(ctx, surface, x, y);
if (surface && surface->window && surface->window->viewport_override) {
double dx = wl_fixed_to_double(*x);
double dy = wl_fixed_to_double(*y);
dx *= surface->window->viewport_pointer_scale;
dy *= surface->window->viewport_pointer_scale;
*x = wl_fixed_from_double(dx);
*y = wl_fixed_from_double(dy);
}
}
void sl_transform_host_to_guest_fixed(struct sl_context* ctx,
struct sl_host_surface* surface,
wl_fixed_t* coord,
uint32_t axis) {
if (ctx->use_direct_scale) {
sl_transform_direct_to_guest_fixed(ctx, surface, coord, axis);
} else {
double dx = wl_fixed_to_double(*coord);
dx *= ctx->scale;
*coord = wl_fixed_from_double(dx);
}
}
void sl_transform_guest_to_host(struct sl_context* ctx,
struct sl_host_surface* surface,
int32_t* x,
int32_t* y) {
if (ctx->use_direct_scale) {
sl_transform_direct_to_host(ctx, surface, x, y);
} else {
if (ctx->stable_scaling) {
stable_scale_guest_to_host(x, ctx->scale);
stable_scale_guest_to_host(y, ctx->scale);
} else {
(*x) /= ctx->scale;
(*y) /= ctx->scale;
}
}
}
void sl_transform_guest_to_host_fixed(struct sl_context* ctx,
struct sl_host_surface* surface,
wl_fixed_t* x,
wl_fixed_t* y) {
if (ctx->use_direct_scale) {
sl_transform_direct_to_host_fixed(ctx, surface, x, y);
} else {
double dx = wl_fixed_to_double(*x);
double dy = wl_fixed_to_double(*y);
dx /= ctx->scale;
dy /= ctx->scale;
*x = wl_fixed_from_double(dx);
*y = wl_fixed_from_double(dy);
}
}
void sl_transform_guest_to_host_fixed(struct sl_context* ctx,
struct sl_host_surface* surface,
wl_fixed_t* coord,
uint32_t axis) {
if (ctx->use_direct_scale) {
sl_transform_direct_to_host_fixed(ctx, surface, coord, axis);
} else {
double dx = wl_fixed_to_double(*coord);
dx /= ctx->scale;
*coord = wl_fixed_from_double(dx);
}
}
struct sl_host_output* sl_transform_guest_position_to_host_position(
sl_context* ctx, sl_host_surface* surface, int32_t* x, int32_t* y) {
sl_host_output* output = sl_infer_output_for_guest_position(ctx, *x, *y);
assert(output);
// Translate from global to output-relative guest coordinates
(*x) -= output->virt_x;
// Convert to host logical scale
sl_transform_guest_to_host(ctx, surface, x, y);
// Translate to global host coordinates
(*x) += output->x;
(*y) += output->y;
return output;
}
struct sl_host_output* sl_transform_host_position_to_guest_position(
sl_context* ctx, sl_host_surface* surface, int32_t* x, int32_t* y) {
sl_host_output* output = sl_infer_output_for_host_position(ctx, *x, *y);
assert(output);
// Translate from global to output-local host coordinates
(*x) -= output->x;
(*y) -= output->y;
// Convert to guest virtual scale
sl_transform_host_to_guest(ctx, surface, x, y);
// Translate to global guest coordinates
(*x) += output->virt_x;
return output;
}
void sl_transform_try_window_scale(struct sl_context* ctx,
struct sl_host_surface* surface,
int32_t width_in_pixels,
int32_t height_in_pixels) {
int32_t reverse_width = width_in_pixels;
int32_t reverse_height = height_in_pixels;
int32_t logical_width;
int32_t logical_height;
// This function should only have an effect in direct scale mode
if (!ctx->use_direct_scale)
return;
// Reset scale so that calls to sl_transform_get_scale_factors will not
// use the current scale.
sl_transform_reset_surface_scale(ctx, surface);
// Transform the window dimensions using the global/per-output scaling factors
sl_transform_guest_to_host(ctx, surface, &reverse_width, &reverse_height);
// Save the logical dimensions for later use
logical_width = reverse_width;
logical_height = reverse_height;
// Transform the logical dimensions back to the virtual pixel dimensions
sl_transform_host_to_guest(ctx, surface, &reverse_width, &reverse_height);
// If the computed logical width or height is zero, force the
// use of the global scaling factors
if ((reverse_width != width_in_pixels ||
reverse_height != height_in_pixels) &&
(logical_width > 0 && logical_height > 0)) {
// There is no match, let's override the scaling setting on our surface
surface->has_own_scale = 1;
surface->xdg_scale_x = static_cast<double>(width_in_pixels) /
static_cast<double>(logical_width);
surface->xdg_scale_y = static_cast<double>(height_in_pixels) /
static_cast<double>(logical_height);
surface->cached_logical_height = logical_height;
surface->cached_logical_width = logical_width;
// Try once more to do a full cycle (pixel -> logical -> pixel),
// if we aren't equal, we need to force a round up on the translation
// to the guest.
reverse_width = width_in_pixels;
reverse_height = height_in_pixels;
sl_transform_guest_to_host(ctx, surface, &reverse_width, &reverse_height);
sl_transform_host_to_guest(ctx, surface, &reverse_width, &reverse_height);
if (reverse_width != width_in_pixels)
surface->scale_round_on_x = true;
if (reverse_height != height_in_pixels)
surface->scale_round_on_y = true;
}
}
void sl_transform_reset_surface_scale(struct sl_context* ctx,
struct sl_host_surface* surface) {
surface->has_own_scale = 0;
surface->scale_round_on_x = surface->scale_round_on_y = false;
surface->xdg_scale_x = surface->xdg_scale_y = 0;
}
void sl_transform_output_dimensions(struct sl_context* ctx,
int32_t* width,
int32_t* height) {
if (ctx->stable_scaling) {
stable_scale_size_host_to_guest(width, ctx->scale);
stable_scale_size_host_to_guest(height, ctx->scale);
} else {
*width = (*width) * ctx->scale;
*height = (*height) * ctx->scale;
}
}