Compare commits

...

3 Commits

Author SHA1 Message Date
b0ead07f61 fix issue 15? 2020-04-24 22:52:13 +02:00
8581d6850b Stabilize auto-resize
The window dimensions are integers, so resizing to fit the content may
not be exact.

When computing the optimal size, it could cause to reduce alternatively
the width and height by few pixels, making the "optimal size" unstable.

To avoid this problem, check if the optimal size is already correct
either by keeping the width or the height.
2020-04-24 22:52:02 +02:00
92cb3a6661 Improve resizing workaround
Call the same method as when the event is received on the event loop, so
that the behavior is the same in both cases.
2020-04-24 21:36:25 +02:00
2 changed files with 24 additions and 3 deletions

View File

@ -109,9 +109,10 @@ static int
event_watcher(void *data, SDL_Event *event) {
(void) data;
if (event->type == SDL_WINDOWEVENT
&& event->window.event == SDL_WINDOWEVENT_RESIZED) {
// called from another thread, not very safe, but it's a workaround!
screen_render(&screen);
&& event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
// In practice, it seems to always be called from the same thread in
// that specific case. Anyway, it's just a workaround.
screen_handle_window_event(&screen, &event->window);
}
return 0;
}

View File

@ -113,6 +113,13 @@ get_optimal_size(struct size current_size, struct size content_size) {
h = MIN(current_size.height, display_size.height);
}
if (h == w * content_size.height / content_size.width
|| w == h * content_size.width / content_size.height) {
// The size is already optimal, if we ignore rounding errors due to
// integer window dimensions
return (struct size) {w, h};
}
bool keep_width = content_size.width * h > content_size.height * w;
if (keep_width) {
// remove black borders on top and bottom
@ -512,11 +519,24 @@ screen_resize_to_pixel_perfect(struct screen *screen) {
content_size.height);
}
static void
screen_reset_logical_size(struct screen *screen) {
// Re-apply the current logical size.
if (SDL_RenderSetLogicalSize(screen->renderer, screen->content_size.width,
screen->content_size.height)) {
LOGE("Could not reset renderer logical size: %s", SDL_GetError());
}
}
void
screen_handle_window_event(struct screen *screen,
const SDL_WindowEvent *event) {
switch (event->event) {
case SDL_WINDOWEVENT_EXPOSED:
// Re-apply the current logical size, in case the window has been
// moved to a screen with a different HiDPI scaling
// <https://github.com/Genymobile/scrcpy/issues/15>
screen_reset_logical_size(screen);
screen_render(screen);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED: