Skip to content

Commit

Permalink
perf: 优化初始化时解析效果的性能
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Apr 13, 2024
1 parent 241e7bf commit 00e2312
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
16 changes: 8 additions & 8 deletions src/Magpie.App/AppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,16 +813,16 @@ bool AppSettings::_LoadProfile(
profile.maxFrameRate = 60.0f;
}

JsonHelper::ReadBoolFlag(profileObj, "disableWindowResizing", ScalingFlags::DisableWindowResizing, profile.flags);
JsonHelper::ReadBoolFlag(profileObj, "3DGameMode", ScalingFlags::Is3DGameMode, profile.flags);
JsonHelper::ReadBoolFlag(profileObj, "showFPS", ScalingFlags::ShowFPS, profile.flags);
if (!JsonHelper::ReadBoolFlag(profileObj, "captureTitleBar", ScalingFlags::CaptureTitleBar, profile.flags, true)) {
JsonHelper::ReadBoolFlag(profileObj, "disableWindowResizing", ScalingFlags::DisableWindowResizing, profile.scalingFlags);
JsonHelper::ReadBoolFlag(profileObj, "3DGameMode", ScalingFlags::Is3DGameMode, profile.scalingFlags);
JsonHelper::ReadBoolFlag(profileObj, "showFPS", ScalingFlags::ShowFPS, profile.scalingFlags);
if (!JsonHelper::ReadBoolFlag(profileObj, "captureTitleBar", ScalingFlags::CaptureTitleBar, profile.scalingFlags, true)) {
// v0.10.0-preview1 使用 reserveTitleBar
JsonHelper::ReadBoolFlag(profileObj, "reserveTitleBar", ScalingFlags::CaptureTitleBar, profile.flags);
JsonHelper::ReadBoolFlag(profileObj, "reserveTitleBar", ScalingFlags::CaptureTitleBar, profile.scalingFlags);
}
JsonHelper::ReadBoolFlag(profileObj, "adjustCursorSpeed", ScalingFlags::AdjustCursorSpeed, profile.flags);
JsonHelper::ReadBoolFlag(profileObj, "drawCursor", ScalingFlags::DrawCursor, profile.flags);
JsonHelper::ReadBoolFlag(profileObj, "disableDirectFlip", ScalingFlags::DisableDirectFlip, profile.flags);
JsonHelper::ReadBoolFlag(profileObj, "adjustCursorSpeed", ScalingFlags::AdjustCursorSpeed, profile.scalingFlags);
JsonHelper::ReadBoolFlag(profileObj, "drawCursor", ScalingFlags::DrawCursor, profile.scalingFlags);
JsonHelper::ReadBoolFlag(profileObj, "disableDirectFlip", ScalingFlags::DisableDirectFlip, profile.scalingFlags);

{
uint32_t cursorScaling = (uint32_t)CursorScaling::NoScaling;
Expand Down
40 changes: 19 additions & 21 deletions src/Magpie.App/EffectsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,23 @@ fire_and_forget EffectsService::StartInitialize() {
ListEffects(effectNames);

const uint32_t nEffect = (uint32_t)effectNames.size();
_effectsMap.reserve(nEffect);
_effects.reserve(nEffect);

// 用于同步 _effectsMap 和 _effects 的初始化
wil::srwlock srwLock;

std::vector<EffectDesc> descs(nEffect);
// 并行解析效果
Win32Utils::RunParallel([&](uint32_t id) {
descs[id].name = StrUtils::UTF16ToUTF8(effectNames[id]);
if (EffectCompiler::Compile(descs[id], EffectCompilerFlags::NoCompile)) {
descs[id].name.clear();
}
}, nEffect);
EffectDesc effectDesc;

_effectsMap.reserve(nEffect);

for (uint32_t i = 0; i < nEffect; ++i) {
EffectDesc& effectDesc = descs[i];
if (effectDesc.name.empty()) {
continue;
effectDesc.name = StrUtils::UTF16ToUTF8(effectNames[id]);
if (EffectCompiler::Compile(effectDesc, EffectCompilerFlags::NoCompile)) {
return;
}

// 这里修改 _effects 无需同步,因为用户界面尚未显示
EffectInfo& effect = _effects.emplace_back();
effect.name = std::move(effectNames[i]);
EffectInfo effect;
effect.name = std::move(effectNames[id]);

if (effectDesc.sortName.empty()) {
effect.sortName = effect.name;
Expand All @@ -87,22 +84,23 @@ fire_and_forget EffectsService::StartInitialize() {
);
}
}

effect.params = std::move(effectDesc.params);
if (effectDesc.GetOutputSizeExpr().first.empty()) {
effect.flags |= EffectInfoFlags::CanScale;
}

_effectsMap.emplace(effect.name, (uint32_t)_effects.size() - 1);
}
auto lock = srwLock.lock_exclusive();
_effectsMap.emplace(effect.name, (uint32_t)_effects.size());
_effects.emplace_back(std::move(effect));
}, nEffect);

_initialized.store(true, std::memory_order_release);
_initialized.notify_one();
}

void EffectsService::WaitForInitialize() {
while (!_initialized.load(std::memory_order_acquire)) {
Sleep(0);
}
_initialized.wait(false, std::memory_order_acquire);
}

}
18 changes: 9 additions & 9 deletions src/Magpie.App/Profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ struct Profile {
multiMonitorUsage = other.multiMonitorUsage;
cursorInterpolationMode = other.cursorInterpolationMode;
launchParameters = other.launchParameters;
flags = other.flags;
scalingFlags = other.scalingFlags;
}

DEFINE_FLAG_ACCESSOR(IsWindowResizingDisabled, ::Magpie::Core::ScalingFlags::DisableWindowResizing, flags)
DEFINE_FLAG_ACCESSOR(Is3DGameMode, ::Magpie::Core::ScalingFlags::Is3DGameMode, flags)
DEFINE_FLAG_ACCESSOR(IsShowFPS, ::Magpie::Core::ScalingFlags::ShowFPS, flags)
DEFINE_FLAG_ACCESSOR(IsCaptureTitleBar, ::Magpie::Core::ScalingFlags::CaptureTitleBar, flags)
DEFINE_FLAG_ACCESSOR(IsAdjustCursorSpeed, ::Magpie::Core::ScalingFlags::AdjustCursorSpeed, flags)
DEFINE_FLAG_ACCESSOR(IsDrawCursor, ::Magpie::Core::ScalingFlags::DrawCursor, flags)
DEFINE_FLAG_ACCESSOR(IsDirectFlipDisabled, ::Magpie::Core::ScalingFlags::DisableDirectFlip, flags)
DEFINE_FLAG_ACCESSOR(IsWindowResizingDisabled, ::Magpie::Core::ScalingFlags::DisableWindowResizing, scalingFlags)
DEFINE_FLAG_ACCESSOR(Is3DGameMode, ::Magpie::Core::ScalingFlags::Is3DGameMode, scalingFlags)
DEFINE_FLAG_ACCESSOR(IsShowFPS, ::Magpie::Core::ScalingFlags::ShowFPS, scalingFlags)
DEFINE_FLAG_ACCESSOR(IsCaptureTitleBar, ::Magpie::Core::ScalingFlags::CaptureTitleBar, scalingFlags)
DEFINE_FLAG_ACCESSOR(IsAdjustCursorSpeed, ::Magpie::Core::ScalingFlags::AdjustCursorSpeed, scalingFlags)
DEFINE_FLAG_ACCESSOR(IsDrawCursor, ::Magpie::Core::ScalingFlags::DrawCursor, scalingFlags)
DEFINE_FLAG_ACCESSOR(IsDirectFlipDisabled, ::Magpie::Core::ScalingFlags::DisableDirectFlip, scalingFlags)

std::wstring name;

Expand Down Expand Up @@ -68,7 +68,7 @@ struct Profile {

std::wstring launchParameters;

uint32_t flags = ::Magpie::Core::ScalingFlags::AdjustCursorSpeed | ::Magpie::Core::ScalingFlags::DrawCursor;
uint32_t scalingFlags = ::Magpie::Core::ScalingFlags::AdjustCursorSpeed | ::Magpie::Core::ScalingFlags::DrawCursor;

bool isPackaged = false;
bool isCroppingEnabled = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Magpie.App/ScalingService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ bool ScalingService::_StartScale(HWND hWnd, const Profile& profile) {
}
options.multiMonitorUsage = profile.multiMonitorUsage;
options.cursorInterpolationMode = profile.cursorInterpolationMode;
options.flags = profile.flags;
options.flags = profile.scalingFlags;

if (profile.isCroppingEnabled) {
options.cropping = profile.cropping;
Expand Down

0 comments on commit 00e2312

Please sign in to comment.