From 4286051b422e46f9685339d92f9801f0ffb74a44 Mon Sep 17 00:00:00 2001 From: Tian Liao Date: Tue, 9 Apr 2024 11:39:41 +0800 Subject: [PATCH] resolve comments --- src/GraphControl/Control/Grapher.cpp | 10 +++---- src/GraphControl/DirectX/RenderMain.cpp | 35 ++++++++----------------- src/GraphControl/DirectX/RenderMain.h | 2 +- 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/GraphControl/Control/Grapher.cpp b/src/GraphControl/Control/Grapher.cpp index 0dfff37c..81191a75 100644 --- a/src/GraphControl/Control/Grapher.cpp +++ b/src/GraphControl/Control/Grapher.cpp @@ -55,7 +55,7 @@ namespace // Translate the pointer position to the [-1, 1] bounds. __inline std::pair PointerPositionToGraphPosition(double posX, double posY, double width, double height) { - return std::make_pair((2 * posX / width - 1), (1 - 2 * posY / height)); + return { (2 * posX / width - 1), (1 - 2 * posY / height) }; } } @@ -381,7 +381,7 @@ namespace GraphControl if (initResult.has_value()) { - auto graphedEquations = *initResult; + auto& graphedEquations = *initResult; for (size_t i = 0; i < validEqs.size(); ++i) { @@ -573,8 +573,8 @@ namespace GraphControl } eq->GraphedEquation->GetGraphEquationOptions()->SetLineStyle(static_cast<::Graphing::Renderer::LineStyle>(eq->EquationStyle)); - eq->GraphedEquation->GetGraphEquationOptions()->SetLineWidth(LineWidth); - eq->GraphedEquation->GetGraphEquationOptions()->SetSelectedEquationLineWidth(LineWidth + ((LineWidth <= 2) ? 1 : 2)); + eq->GraphedEquation->GetGraphEquationOptions()->SetLineWidth(static_cast(LineWidth)); + eq->GraphedEquation->GetGraphEquationOptions()->SetSelectedEquationLineWidth(static_cast(LineWidth + ((LineWidth <= 2) ? 1 : 2))); } } options.SetGraphColors(graphColors); @@ -1070,7 +1070,7 @@ void Grapher::OnLineWidthPropertyChanged(double oldValue, double newValue) UpdateGraphOptions(m_graph->GetOptions(), GetGraphableEquations()); if (m_renderMain) { - m_renderMain->SetPointRadius(LineWidth + 1); + m_renderMain->SetPointRadius(static_cast(LineWidth + 1)); m_renderMain->RunRenderPass(); TraceLogger::GetInstance()->LogLineWidthChanged(); diff --git a/src/GraphControl/DirectX/RenderMain.cpp b/src/GraphControl/DirectX/RenderMain.cpp index fbaf9aad..fa5ddb77 100644 --- a/src/GraphControl/DirectX/RenderMain.cpp +++ b/src/GraphControl/DirectX/RenderMain.cpp @@ -46,7 +46,6 @@ namespace GraphControl::DX RenderMain::~RenderMain() { - m_renderPassCts.cancel(); UnregisterEventHandlers(); } @@ -206,44 +205,32 @@ namespace GraphControl::DX concurrency::task RenderMain::RunRenderPassAsync(bool allowCancel) { - if (allowCancel) - { - m_renderPassCts.cancel(); - } - m_renderPassCts = concurrency::cancellation_token_source{}; - bool result = false; + auto currentVer = ++m_renderPassVer; + Platform::WeakReference that{ this }; co_await m_coreWindow->Dispatcher->RunAsync( CoreDispatcherPriority::High, ref new DispatchedHandler( - [this, &result, cancel = m_renderPassCts.get_token()] + [&] { - if (cancel.is_canceled()) + auto self = that.Resolve(); + if (self == nullptr || (allowCancel && m_renderPassVer != currentVer)) + { return; - result = RunRenderPassInternal(); + } + result = self->RunRenderPassInternal(); })); co_return result; } bool RenderMain::RunRenderPassInternal() { - // We are accessing Direct3D resources directly without Direct2D's knowledge, so we - // must manually acquire and apply the Direct2D factory lock. - winrt::com_ptr d2dmultithread; - m_deviceResources.GetD2DFactory()->QueryInterface(IID_PPV_ARGS(d2dmultithread.put())); - d2dmultithread->Enter(); - - bool succesful = Render(); - - if (succesful) + if (Render()) { m_deviceResources.Present(); + return true; } - - // It is absolutely critical that the factory lock be released upon - // exiting this function, or else any consequent Direct2D calls will be blocked. - d2dmultithread->Leave(); - return succesful; + return false; } // Renders the current frame according to the current application state. diff --git a/src/GraphControl/DirectX/RenderMain.h b/src/GraphControl/DirectX/RenderMain.h index 4c5a724f..6e4050aa 100644 --- a/src/GraphControl/DirectX/RenderMain.h +++ b/src/GraphControl/DirectX/RenderMain.h @@ -184,7 +184,7 @@ namespace GraphControl::DX // Are we currently showing the tracing value bool m_Tracing; - concurrency::cancellation_token_source m_renderPassCts; + unsigned m_renderPassVer = 0; HRESULT m_HResult; };