Skip to content

Commit

Permalink
Merge branch 'master' into wlserver_fix_data_races
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkautarch committed Jul 15, 2024
2 parents e7a35b6 + 3c5a232 commit 54f8202
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 22 deletions.
173 changes: 173 additions & 0 deletions src/Utils/Algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#pragma once

#include <iterator>
#include <span>

namespace gamescope::Algorithm
{
template <typename TObj>
constexpr TObj *Begin( std::span<TObj> span )
{
return span.data();
}

template <typename TObj>
constexpr TObj *End( std::span<TObj> span )
{
return Begin( span ) + span.size();
}

template <typename TIter, typename TObj>
constexpr TIter FindSimple( TIter pFirst, TIter pEnd, const TObj &obj )
{
while ( pFirst != pEnd && *pFirst != obj )
++pFirst;

return pFirst;
}

template <typename TIter, typename TObj>
constexpr TIter FindByFour( TIter pFirst, TIter pEnd, const TObj &obj )
{
typename std::iterator_traits< TIter >::difference_type ulTripCount = ( pEnd - pFirst ) >> 2;

while ( ulTripCount-- > 0 )
{
if ( pFirst[0] == obj )
return &pFirst[0];

if ( pFirst[1] == obj )
return &pFirst[1];

if ( pFirst[2] == obj )
return &pFirst[2];

if ( pFirst[3] == obj )
return &pFirst[3];

pFirst += 4;
}

switch ( pEnd - pFirst )
{
case 3:
{
if ( pFirst[0] == obj )
return &pFirst[0];

if ( pFirst[1] == obj )
return &pFirst[1];

if ( pFirst[2] == obj )
return &pFirst[2];

return pEnd;
}
case 2:
{
if ( pFirst[0] == obj )
return &pFirst[0];

if ( pFirst[1] == obj )
return &pFirst[1];

return pEnd;
}
case 1:
{
if ( pFirst[0] == obj )
return &pFirst[0];

return pEnd;
}
case 0:
{
return pEnd;
}
default:
{
__builtin_unreachable();
}
}
}

template <typename TIter, typename TObj>
constexpr TIter Find( TIter pFirst, TIter pEnd, const TObj &obj )
{
return FindSimple( pFirst, pEnd, obj );
}

template <typename TIter, typename TObj>
constexpr TIter Find( std::span<TObj> span, const TObj &obj )
{
return Find( Begin( span ), End( span ), obj );
}

template <typename TIter, typename TObj>
constexpr bool ContainsShortcut( TIter pFirst, TIter pEnd, const TObj &obj )
{
return Find( pFirst, pEnd, obj ) != pEnd;
}

template <typename TIter, typename TObj>
constexpr bool ContainsNoShortcut( TIter pFirst, TIter pEnd, const TObj &obj )
{
bool bFound = false;

typename std::iterator_traits< TIter >::difference_type ulTripCount = ( pEnd - pFirst ) >> 2;

while ( ulTripCount-- > 0 )
{
bFound |= pFirst[0] == obj ||
pFirst[1] == obj ||
pFirst[2] == obj ||
pFirst[3] == obj;

pFirst += 4;
}

switch ( pEnd - pFirst )
{
case 3:
{
bFound |= pFirst[0] == obj ||
pFirst[1] == obj ||
pFirst[2] == obj;
break;
}
case 2:
{
bFound |= pFirst[0] == obj ||
pFirst[1] == obj;
break;
}
case 1:
{
bFound |= pFirst[0] == obj;
break;
}
case 0:
{
break;
}
default:
{
__builtin_unreachable();
}
}

return bFound;
}

template <typename TIter, typename TObj>
constexpr bool Contains( TIter pFirst, TIter pEnd, const TObj &obj )
{
return ContainsNoShortcut( pFirst, pEnd, obj );
}

template <typename TSpanObj, typename TObj>
constexpr bool Contains( std::span<TSpanObj> span, const TObj &obj )
{
return Contains( Begin( span ), End( span ), obj );
}
}
3 changes: 2 additions & 1 deletion src/Utils/Process.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Process.h"
#include "../Utils/Algorithm.h"
#include "../convar.h"
#include "../log.hpp"
#include "../Utils/Defer.h"
Expand Down Expand Up @@ -268,7 +269,7 @@ namespace gamescope::Process

int nFd = *onFd;

bool bExcluded = std::find( nExcludedFds.begin(), nExcludedFds.end(), nFd ) != nExcludedFds.end();
bool bExcluded = Algorithm::Contains( nExcludedFds, nFd );
if ( bExcluded )
continue;

Expand Down
137 changes: 137 additions & 0 deletions src/color_bench.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <array>
#include <benchmark/benchmark.h>

#include <algorithm>
#include "Utils/Algorithm.h"

#include "color_helpers.h"

const uint32_t nLutSize1d = 4096;
Expand Down Expand Up @@ -74,4 +78,137 @@ static void BenchmarkCalcColorTransforms(benchmark::State &state)
}
BENCHMARK(BenchmarkCalcColorTransforms);

static constexpr uint32_t k_uFindTestValueCountLarge = 524288;
static constexpr uint32_t k_uFindTestValueCountMedium = 16;
static constexpr uint32_t k_uFindTestValueCountSmall = 5;

template <uint32_t uSize>
static __attribute__((noinline)) std::array<int, uSize> GetFindTestValues()
{
static std::array<int, uSize> s_Values = []()
{
std::array<int, uSize> values;
for ( uint32_t i = 0; i < uSize; i++ )
values[i] = rand() % 255;

return values;
}();

return s_Values;
}

// Large

static void Benchmark_Find_Large_Gamescope(benchmark::State &state)
{
std::array<int, k_uFindTestValueCountLarge> values = GetFindTestValues<k_uFindTestValueCountLarge>();

for (auto _ : state)
{
auto iter = gamescope::Algorithm::Find( values.begin(), values.end(), 765678478 );
benchmark::DoNotOptimize( iter );
}
}
BENCHMARK(Benchmark_Find_Large_Gamescope);

static void Benchmark_Find_Large_Std(benchmark::State &state)
{
std::array<int, k_uFindTestValueCountLarge> values = GetFindTestValues<k_uFindTestValueCountLarge>();

for (auto _ : state)
{
auto iter = std::find( values.begin(), values.end(), 765678478 );
benchmark::DoNotOptimize( iter );
}
}
BENCHMARK(Benchmark_Find_Large_Std);

static void Benchmark_Contains_Large_Gamescope(benchmark::State &state)
{
std::array<int, k_uFindTestValueCountLarge> values = GetFindTestValues<k_uFindTestValueCountLarge>();

for (auto _ : state)
{
bool bContains = gamescope::Algorithm::ContainsNoShortcut( values.begin(), values.end(), 765678478 );
benchmark::DoNotOptimize( bContains );
}
}
BENCHMARK(Benchmark_Contains_Large_Gamescope);

//

static void Benchmark_Find_Medium_Gamescope(benchmark::State &state)
{
std::array<int, k_uFindTestValueCountMedium> values = GetFindTestValues<k_uFindTestValueCountMedium>();

for (auto _ : state)
{
auto iter = gamescope::Algorithm::Find( values.begin(), values.end(), 765678478 );
benchmark::DoNotOptimize( iter );
}
}
BENCHMARK(Benchmark_Find_Medium_Gamescope);

static void Benchmark_Find_Medium_Std(benchmark::State &state)
{
std::array<int, k_uFindTestValueCountMedium> values = GetFindTestValues<k_uFindTestValueCountMedium>();

for (auto _ : state)
{
auto iter = std::find( values.begin(), values.end(), 765678478 );
benchmark::DoNotOptimize( iter );
}
}
BENCHMARK(Benchmark_Find_Medium_Std);

static void Benchmark_Contains_Medium_Gamescope(benchmark::State &state)
{
std::array<int, k_uFindTestValueCountMedium> values = GetFindTestValues<k_uFindTestValueCountMedium>();

for (auto _ : state)
{
bool bContains = gamescope::Algorithm::ContainsNoShortcut( values.begin(), values.end(), 765678478 );
benchmark::DoNotOptimize( bContains );
}
}
BENCHMARK(Benchmark_Contains_Medium_Gamescope);

//

static void Benchmark_Find_Small_Gamescope(benchmark::State &state)
{
std::array<int, k_uFindTestValueCountSmall> values = GetFindTestValues<k_uFindTestValueCountSmall>();

for (auto _ : state)
{
auto iter = gamescope::Algorithm::Find( values.begin(), values.end(), 765678478 );
benchmark::DoNotOptimize( iter );
}
}
BENCHMARK(Benchmark_Find_Small_Gamescope);

static void Benchmark_Find_Small_Std(benchmark::State &state)
{
std::array<int, k_uFindTestValueCountSmall> values = GetFindTestValues<k_uFindTestValueCountSmall>();

for (auto _ : state)
{
auto iter = std::find( values.begin(), values.end(), 765678478 );
benchmark::DoNotOptimize( iter );
}
}
BENCHMARK(Benchmark_Find_Small_Std);

static void Benchmark_Contains_Small_Gamescope(benchmark::State &state)
{
std::array<int, k_uFindTestValueCountSmall> values = GetFindTestValues<k_uFindTestValueCountSmall>();

for (auto _ : state)
{
bool bContains = gamescope::Algorithm::ContainsNoShortcut( values.begin(), values.end(), 765678478 );
benchmark::DoNotOptimize( bContains );
}
}
BENCHMARK(Benchmark_Contains_Small_Gamescope);

BENCHMARK_MAIN();
13 changes: 6 additions & 7 deletions src/steamcompmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,6 @@ constexpr const T& clamp( const T& x, const T& min, const T& max )
}

extern bool g_bForceRelativeMouse;
bool bSteamCompMgrGrab = false;

CommitDoneList_t g_steamcompmgr_xdg_done_commits;

Expand Down Expand Up @@ -7133,8 +7132,6 @@ steamcompmgr_main(int argc, char **argv)
// Reset getopt() state
optind = 1;

bSteamCompMgrGrab = GetBackend()->GetNestedHints() && g_bForceRelativeMouse;

int o;
int opt_index = -1;
bool bForceWindowsFullscreen = false;
Expand Down Expand Up @@ -7591,13 +7588,15 @@ steamcompmgr_main(int argc, char **argv)

if ( GetBackend()->GetNestedHints() && !g_bForceRelativeMouse )
{
bool bImageEmpty =
const bool bImageEmpty =
( global_focus.cursor && global_focus.cursor->imageEmpty() ) &&
( !window_is_steam( global_focus.inputFocusWindow ) );

if ( GetBackend()->GetNestedHints() )
GetBackend()->GetNestedHints()->SetRelativeMouseMode( bImageEmpty );
bSteamCompMgrGrab = GetBackend()->GetNestedHints() && bImageEmpty;
const bool bHasPointerConstraint = wlserver.HasMouseConstraint(); // atomic, no lock needed

const bool bRelativeMouseMode = bImageEmpty && bHasPointerConstraint;

GetBackend()->GetNestedHints()->SetRelativeMouseMode( bRelativeMouseMode );
}

static int nIgnoredOverlayRepaints = 0;
Expand Down
Loading

0 comments on commit 54f8202

Please sign in to comment.