Skip to content

Commit b6562f1

Browse files
authored
Merge pull request wolfpld#715 from YaLTeR/fix-wayland-scale
Fix and update Wayland scale handling
2 parents a2dd51a + fcdc967 commit b6562f1

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-13
lines changed

profiler/src/Backend.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class RunQueue;
1111
class Backend
1212
{
1313
public:
14-
Backend( const char* title, const std::function<void()>& redraw, RunQueue* mainThreadTasks );
14+
Backend( const char* title, const std::function<void()>& redraw, const std::function<void(float)>& scaleChanged, RunQueue* mainThreadTasks );
1515
~Backend();
1616

1717
void Show();

profiler/src/BackendGlfw.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static void glfw_window_iconify_callback( GLFWwindow*, int iconified )
6060
}
6161

6262

63-
Backend::Backend( const char* title, const std::function<void()>& redraw, RunQueue* mainThreadTasks )
63+
Backend::Backend( const char* title, const std::function<void()>& redraw, const std::function<void(float)>& scaleChanged, RunQueue* mainThreadTasks )
6464
{
6565
glfwSetErrorCallback( glfw_error_callback );
6666
if( !glfwInit() ) exit( 1 );

profiler/src/BackendWayland.cpp

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,12 @@ constexpr ImGuiKey s_keyTable[] = {
160160
};
161161

162162
static std::function<void()> s_redraw;
163+
static std::function<void(float)> s_scaleChanged;
163164
static RunQueue* s_mainThreadTasks;
164165

165166
static struct wl_display* s_dpy;
166167
static struct wl_compositor* s_comp;
168+
static uint32_t s_comp_version;
167169
static struct wl_surface* s_surf;
168170
static struct wl_egl_window* s_eglWin;
169171
static struct wl_shm* s_shm;
@@ -194,6 +196,7 @@ struct Output
194196
{
195197
int32_t scale;
196198
wl_output* obj;
199+
bool entered;
197200
};
198201
static std::unordered_map<uint32_t, std::unique_ptr<Output>> s_output;
199202
static int s_maxScale = 1;
@@ -207,6 +210,19 @@ static uint64_t s_time;
207210
static wl_fixed_t s_wheelAxisX, s_wheelAxisY;
208211
static bool s_wheel;
209212

213+
static void RecomputeScale()
214+
{
215+
// On wl_compositor >= 6 the scale is sent explicitly via wl_surface.preferred_buffer_scale.
216+
if ( s_comp_version >= 6 ) return;
217+
218+
int max = 1;
219+
for( auto& out : s_output )
220+
{
221+
if( out.second->entered && out.second->scale > max ) max = out.second->scale;
222+
}
223+
s_maxScale = max;
224+
}
225+
210226
static void PointerEnter( void*, struct wl_pointer* pointer, uint32_t serial, struct wl_surface* surf, wl_fixed_t sx, wl_fixed_t sy )
211227
{
212228
wl_pointer_set_cursor( pointer, serial, s_cursorSurf, s_cursorX, s_cursorY );
@@ -471,12 +487,7 @@ static void OutputMode( void*, struct wl_output* output, uint32_t flags, int32_t
471487

472488
static void OutputDone( void*, struct wl_output* output )
473489
{
474-
int max = 1;
475-
for( auto& out : s_output )
476-
{
477-
if( out.second->scale > max ) max = out.second->scale;
478-
}
479-
s_maxScale = max;
490+
RecomputeScale();
480491
}
481492

482493
static void OutputScale( void* data, struct wl_output* output, int32_t scale )
@@ -506,7 +517,8 @@ static void RegistryGlobal( void*, struct wl_registry* reg, uint32_t name, const
506517
{
507518
if( strcmp( interface, wl_compositor_interface.name ) == 0 )
508519
{
509-
s_comp = (wl_compositor*)wl_registry_bind( reg, name, &wl_compositor_interface, 4 );
520+
s_comp_version = version;
521+
s_comp = (wl_compositor*)wl_registry_bind( reg, name, &wl_compositor_interface, version >= 6 ? 6 : 4 );
510522
}
511523
else if( strcmp( interface, wl_shm_interface.name ) == 0 )
512524
{
@@ -529,7 +541,7 @@ static void RegistryGlobal( void*, struct wl_registry* reg, uint32_t name, const
529541
else if( strcmp( interface, wl_output_interface.name ) == 0 )
530542
{
531543
auto output = (wl_output*)wl_registry_bind( reg, name, &wl_output_interface, 2 );
532-
auto ptr = std::make_unique<Output>( Output { 1, output } );
544+
auto ptr = std::make_unique<Output>( Output { 1, output, false } );
533545
wl_output_add_listener( output, &outputListener, ptr.get() );
534546
s_output.emplace( name, std::move( ptr ) );
535547
}
@@ -545,6 +557,7 @@ static void RegistryGlobalRemove( void*, struct wl_registry* reg, uint32_t name
545557
if( it == s_output.end() ) return;
546558
wl_output_destroy( it->second->obj );
547559
s_output.erase( it );
560+
RecomputeScale();
548561
}
549562

550563
constexpr struct wl_registry_listener registryListener = {
@@ -603,6 +616,43 @@ constexpr struct xdg_toplevel_listener toplevelListener = {
603616
.close = XdgToplevelClose
604617
};
605618

619+
static void SurfaceEnter( void*, struct wl_surface* surface, struct wl_output* output )
620+
{
621+
for ( auto& out : s_output )
622+
{
623+
if ( out.second->obj == output )
624+
{
625+
out.second->entered = true;
626+
RecomputeScale();
627+
break;
628+
}
629+
}
630+
}
631+
632+
static void SurfaceLeave( void*, struct wl_surface* surface, struct wl_output* output )
633+
{
634+
for ( auto& out : s_output )
635+
{
636+
if ( out.second->obj == output )
637+
{
638+
out.second->entered = false;
639+
RecomputeScale();
640+
break;
641+
}
642+
}
643+
}
644+
645+
static void SurfacePreferredBufferScale( void*, struct wl_surface* surface, int32_t scale )
646+
{
647+
s_maxScale = scale;
648+
}
649+
650+
constexpr struct wl_surface_listener surfaceListener = {
651+
.enter = SurfaceEnter,
652+
.leave = SurfaceLeave,
653+
.preferred_buffer_scale = SurfacePreferredBufferScale,
654+
};
655+
606656
static void SetupCursor()
607657
{
608658
auto env_xcursor_theme = getenv( "XCURSOR_THEME" );
@@ -624,9 +674,10 @@ static void SetupCursor()
624674
s_cursorY = cursor->images[0]->hotspot_y / s_maxScale;
625675
}
626676

627-
Backend::Backend( const char* title, const std::function<void()>& redraw, RunQueue* mainThreadTasks )
677+
Backend::Backend( const char* title, const std::function<void()>& redraw, const std::function<void(float)>& scaleChanged, RunQueue* mainThreadTasks )
628678
{
629679
s_redraw = redraw;
680+
s_scaleChanged = scaleChanged;
630681
s_mainThreadTasks = mainThreadTasks;
631682
s_w = m_winPos.w;
632683
s_h = m_winPos.h;
@@ -645,6 +696,7 @@ Backend::Backend( const char* title, const std::function<void()>& redraw, RunQue
645696
if( !s_seat ) { fprintf( stderr, "No wayland seat!\n" ); exit( 1 ); }
646697

647698
s_surf = wl_compositor_create_surface( s_comp );
699+
wl_surface_add_listener( s_surf, &surfaceListener, nullptr );
648700
s_eglWin = wl_egl_window_create( s_surf, m_winPos.w, m_winPos.h );
649701
s_xdgSurf = xdg_wm_base_get_xdg_surface( s_wm, s_surf );
650702
xdg_surface_add_listener( s_xdgSurf, &xdgSurfaceListener, nullptr );
@@ -781,6 +833,7 @@ void Backend::NewFrame( int& w, int& h )
781833
{
782834
if( s_prevScale != s_maxScale )
783835
{
836+
s_scaleChanged( s_maxScale );
784837
SetupCursor();
785838
wl_surface_set_buffer_scale( s_surf, s_maxScale );
786839
s_prevScale = s_maxScale;

profiler/src/main.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static ConnectionHistory* connHist;
9595
static std::atomic<ViewShutdown> viewShutdown { ViewShutdown::False };
9696
static double animTime = 0;
9797
static float dpiScale = 1.f;
98+
static bool dpiScaleOverriddenFromEnv = false;
9899
static Filters* filt;
99100
static RunQueue mainThreadTasks;
100101
static uint32_t updateVersion = 0;
@@ -197,6 +198,15 @@ static bool SaveConfig()
197198
return true;
198199
}
199200

201+
static void ScaleChanged( float scale )
202+
{
203+
if ( dpiScaleOverriddenFromEnv ) return;
204+
if ( dpiScale == scale ) return;
205+
206+
dpiScale = scale;
207+
SetupDPIScale( dpiScale, s_fixedWidth, s_bigFont, s_smallFont );
208+
}
209+
200210
int main( int argc, char** argv )
201211
{
202212
sprintf( title, "Tracy Profiler %i.%i.%i", tracy::Version::Major, tracy::Version::Minor, tracy::Version::Patch );
@@ -297,7 +307,7 @@ int main( int argc, char** argv )
297307
LoadConfig();
298308

299309
ImGuiTracyContext imguiContext;
300-
Backend backend( title, DrawContents, &mainThreadTasks );
310+
Backend backend( title, DrawContents, ScaleChanged, &mainThreadTasks );
301311
tracy::InitTexture();
302312
iconTex = tracy::MakeTexture();
303313
zigzagTex = tracy::MakeTexture( true );
@@ -310,7 +320,11 @@ int main( int argc, char** argv )
310320
if( envDpiScale )
311321
{
312322
const auto cnv = atof( envDpiScale );
313-
if( cnv != 0 ) dpiScale = cnv;
323+
if( cnv != 0 )
324+
{
325+
dpiScale = cnv;
326+
dpiScaleOverriddenFromEnv = true;
327+
}
314328
}
315329

316330
SetupDPIScale( dpiScale, s_fixedWidth, s_bigFont, s_smallFont );

0 commit comments

Comments
 (0)