1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include "drape_frontend/base_renderer.hpp"
#include "drape_frontend/message_subclasses.hpp"
#include <utility>
#if defined(OMIM_METAL_AVAILABLE)
namespace dp
{
extern void RenderFrameMediator(std::function<void()> && renderFrameFunction);
} // namespace dp
#define RENDER_FRAME_MEDIATOR(renderFunction) dp::RenderFrameMediator([this]{ renderFunction; });
#else
#define RENDER_FRAME_MEDIATOR(renderFunction) renderFunction;
#endif
namespace df
{
// static
std::atomic<uint8_t> BaseRenderer::m_contextCounter(0);
BaseRenderer::BaseRenderer(ThreadsCommutator::ThreadName name, Params const & params)
: m_apiVersion(params.m_apiVersion)
, m_commutator(params.m_commutator)
, m_contextFactory(params.m_oglContextFactory)
, m_texMng(params.m_texMng)
, m_threadName(name)
, m_isEnabled(true)
, m_renderingEnablingCompletionHandler(nullptr)
, m_wasNotified(false)
, m_wasContextReset(false)
, m_onGraphicsContextInitialized(params.m_onGraphicsContextInitialized)
{
m_commutator->RegisterThread(m_threadName, this);
}
void BaseRenderer::StartThread()
{
m_selfThread.Create(CreateRoutine());
}
void BaseRenderer::StopThread()
{
// stop rendering and close queue
m_selfThread.GetRoutine()->Cancel();
CloseQueue();
// wake up render thread if necessary
if (!m_isEnabled)
{
WakeUp();
}
// wait for render thread completion
m_selfThread.Join();
}
void BaseRenderer::IterateRenderLoop()
{
RENDER_FRAME_MEDIATOR(IterateRenderLoopImpl());
}
void BaseRenderer::IterateRenderLoopImpl()
{
RenderFrame();
CheckRenderingEnabled();
}
void BaseRenderer::SetRenderingEnabled(ref_ptr<dp::GraphicsContextFactory> contextFactory)
{
if (m_wasContextReset && contextFactory != nullptr)
m_contextFactory = contextFactory;
SetRenderingEnabled(true);
}
void BaseRenderer::SetRenderingDisabled(bool const destroySurface)
{
if (destroySurface)
m_wasContextReset = true;
SetRenderingEnabled(false);
}
bool BaseRenderer::IsRenderingEnabled() const
{
return m_isEnabled;
}
void BaseRenderer::SetRenderingEnabled(bool const isEnabled)
{
if (isEnabled == m_isEnabled)
return;
// here we have to wait for completion of internal SetRenderingEnabled
std::mutex completionMutex;
std::condition_variable completionCondition;
bool notified = false;
auto handler = [&]()
{
std::lock_guard<std::mutex> lock(completionMutex);
notified = true;
completionCondition.notify_one();
};
{
std::lock_guard<std::mutex> lock(m_completionHandlerMutex);
m_renderingEnablingCompletionHandler = std::move(handler);
}
if (isEnabled)
{
// wake up rendering thread
WakeUp();
}
else
{
// here we set up value only if rendering disabled
m_isEnabled = false;
// if renderer thread is waiting for message let it go
CancelMessageWaiting();
}
std::unique_lock<std::mutex> lock(completionMutex);
completionCondition.wait(lock, [¬ified] { return notified; });
}
bool BaseRenderer::FilterContextDependentMessage(ref_ptr<Message> msg)
{
return msg->IsGraphicsContextDependent();
}
void BaseRenderer::CreateContext()
{
OnContextCreate();
m_contextCounter++;
uint8_t constexpr kContextCount = 2;
if (m_contextCounter == kContextCount && m_onGraphicsContextInitialized)
m_onGraphicsContextInitialized();
}
void BaseRenderer::CheckRenderingEnabled()
{
if (!m_isEnabled)
{
dp::GraphicsContext * context = nullptr;
if (m_wasContextReset)
{
using namespace std::placeholders;
EnableMessageFiltering(std::bind(&BaseRenderer::FilterContextDependentMessage, this, _1));
OnContextDestroy();
CHECK(m_contextCounter > 0, ());
m_contextCounter--;
}
else
{
bool const isDrawContext = m_threadName == ThreadsCommutator::RenderThread;
context = isDrawContext ? m_contextFactory->GetDrawContext() :
m_contextFactory->GetResourcesUploadContext();
context->SetRenderingEnabled(false);
}
OnRenderingDisabled();
// notify initiator-thread about rendering disabling
Notify();
// wait for signal
std::unique_lock<std::mutex> lock(m_renderingEnablingMutex);
m_renderingEnablingCondition.wait(lock, [this] { return m_wasNotified; });
m_wasNotified = false;
bool needCreateContext = false;
if (!m_selfThread.GetRoutine()->IsCancelled())
{
// here rendering is enabled again
m_isEnabled = true;
if (m_wasContextReset)
{
m_wasContextReset = false;
needCreateContext = true;
}
else
{
context->SetRenderingEnabled(true);
}
}
if (needCreateContext)
DisableMessageFiltering();
// notify initiator-thread about rendering enabling
// m_renderingEnablingCompletionHandler will be setup before awakening of this thread
Notify();
OnRenderingEnabled();
if (needCreateContext)
CreateContext();
}
}
void BaseRenderer::Notify()
{
std::function<void()> handler;
{
std::lock_guard<std::mutex> lock(m_completionHandlerMutex);
handler = std::move(m_renderingEnablingCompletionHandler);
m_renderingEnablingCompletionHandler = nullptr;
}
if (handler != nullptr)
handler();
}
void BaseRenderer::WakeUp()
{
std::lock_guard<std::mutex> lock(m_renderingEnablingMutex);
m_wasNotified = true;
m_renderingEnablingCondition.notify_one();
}
bool BaseRenderer::CanReceiveMessages()
{
threads::IRoutine * routine = m_selfThread.GetRoutine();
return routine != nullptr && !routine->IsCancelled();
}
} // namespace df
|