Skip to content

Commit

Permalink
Do not use xaml rendering callback in non-xaml apps (#10800)
Browse files Browse the repository at this point in the history
* Do not use xaml rendering callback in non-xaml apps

* Change files

* format
  • Loading branch information
acoates-ms committed Oct 27, 2022
1 parent 4920197 commit 5d62f99
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Do not use xaml rendering callback in non-xaml apps",
"packageName": "react-native-windows",
"email": "30809111+acoates-ms@users.noreply.github.com",
"dependentChangeType": "patch"
}
19 changes: 19 additions & 0 deletions vnext/Microsoft.ReactNative/Modules/TimingModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <InstanceManager.h>
#include <UI.Xaml.Media.h>
#include <Utils/ValueUtils.h>
#include <XamlUtils.h>

#include <unknwnbase.h>

Expand Down Expand Up @@ -72,6 +73,7 @@ bool TimerQueue::IsEmpty() {

void Timing::Initialize(winrt::Microsoft::ReactNative::ReactContext const &reactContext) noexcept {
m_context = reactContext;
m_usePostForRendering = !xaml::TryGetCurrentApplication();
}

void Timing::OnTick() {
Expand All @@ -94,6 +96,8 @@ void Timing::OnTick() {

if (m_timerQueue.IsEmpty()) {
StopTicks();
} else if (m_usePostForRendering && m_usingRendering && emittedAnimationFrame) {
PostRenderFrame(); // Repost
} else if (!m_usingRendering || !emittedAnimationFrame) {
// If we're using a rendering callback, check if any animation frame
// requests were emitted in this tick. If not, start the dispatcher timer.
Expand Down Expand Up @@ -122,10 +126,25 @@ winrt::dispatching::DispatcherQueueTimer Timing::EnsureDispatcherTimer() {
return m_dispatcherQueueTimer;
}

void Timing::PostRenderFrame() noexcept {
assert(m_usePostForRendering);
m_usingRendering = true;
m_context.UIDispatcher().Post([wkThis = std::weak_ptr(this->shared_from_this())]() {
if (auto pThis = wkThis.lock()) {
pThis->m_usingRendering = false;
pThis->OnTick();
}
});
}

void Timing::StartRendering() {
if (m_dispatcherQueueTimer)
m_dispatcherQueueTimer.Stop();

if (m_usePostForRendering) {
PostRenderFrame();
return;
}
m_rendering.revoke();
m_usingRendering = true;
m_rendering = xaml::Media::CompositionTarget::Rendering(
Expand Down
2 changes: 2 additions & 0 deletions vnext/Microsoft.ReactNative/Modules/TimingModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct Timing : public std::enable_shared_from_this<Timing> {
void OnTick();
winrt::dispatching::DispatcherQueueTimer EnsureDispatcherTimer();
void StartRendering();
void PostRenderFrame() noexcept;
void StartDispatcherTimer();
void StopTicks();

Expand All @@ -81,6 +82,7 @@ struct Timing : public std::enable_shared_from_this<Timing> {
xaml::Media::CompositionTarget::Rendering_revoker m_rendering;
winrt::dispatching::DispatcherQueueTimer m_dispatcherQueueTimer{nullptr};
bool m_usingRendering{false};
bool m_usePostForRendering{false};
};

} // namespace Microsoft::ReactNative

0 comments on commit 5d62f99

Please sign in to comment.