mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
The video decoding thread efficiently waits for a video to be created before doing anything, rather than sleeping for 2ms in a loop. Reduces the CPU usage of the video worker thread to ~0% when no videos are loaded.
This commit is contained in:
@@ -71,6 +71,7 @@ void Worker::addStream(VideoStream *stream)
|
||||
{
|
||||
love::thread::Lock l(mutex);
|
||||
streams.push_back(stream);
|
||||
cond->broadcast();
|
||||
}
|
||||
|
||||
void Worker::stop()
|
||||
@@ -78,6 +79,7 @@ void Worker::stop()
|
||||
{
|
||||
love::thread::Lock l(mutex);
|
||||
stopping = true;
|
||||
cond->broadcast();
|
||||
}
|
||||
|
||||
owner->wait();
|
||||
@@ -85,34 +87,39 @@ void Worker::stop()
|
||||
|
||||
void Worker::threadFunction()
|
||||
{
|
||||
double lastFrame = love::timer::Timer::getTimeSinceEpoch();
|
||||
double lastFrame = love::timer::Timer::getTime();
|
||||
|
||||
while (true)
|
||||
{
|
||||
double curFrame = love::timer::Timer::getTimeSinceEpoch();
|
||||
love::sleep(2);
|
||||
|
||||
love::thread::Lock l(mutex);
|
||||
|
||||
while (!stopping && streams.empty())
|
||||
{
|
||||
cond->wait(mutex);
|
||||
lastFrame = love::timer::Timer::getTime();
|
||||
}
|
||||
|
||||
if (stopping)
|
||||
return;
|
||||
|
||||
double curFrame = love::timer::Timer::getTime();
|
||||
double dt = curFrame-lastFrame;
|
||||
lastFrame = curFrame;
|
||||
|
||||
for (auto it = streams.begin(); it != streams.end(); ++it)
|
||||
{
|
||||
love::thread::Lock l(mutex);
|
||||
|
||||
if (stopping)
|
||||
return;
|
||||
|
||||
for (auto it = streams.begin(); it != streams.end(); ++it)
|
||||
VideoStream *stream = *it;
|
||||
if (stream->getReferenceCount() == 1)
|
||||
{
|
||||
VideoStream *stream = *it;
|
||||
if (stream->getReferenceCount() == 1)
|
||||
{
|
||||
// We're the only ones left
|
||||
streams.erase(it);
|
||||
break;
|
||||
}
|
||||
|
||||
stream->threadedFillBackBuffer(dt);
|
||||
// We're the only ones left
|
||||
streams.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
love::sleep(2);
|
||||
stream->threadedFillBackBuffer(dt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,10 +68,13 @@ public:
|
||||
void stop();
|
||||
|
||||
private:
|
||||
std::vector<StrongRef<VideoStream>> streams;
|
||||
love::thread::MutexRef mutex;
|
||||
|
||||
volatile bool stopping;
|
||||
std::vector<StrongRef<VideoStream>> streams;
|
||||
|
||||
love::thread::MutexRef mutex;
|
||||
love::thread::ConditionalRef cond;
|
||||
|
||||
bool stopping;
|
||||
}; // Worker
|
||||
|
||||
} // theora
|
||||
|
||||
Reference in New Issue
Block a user