INput
This commit is contained in:
111
apps/RenderCadenceCompositor/video/SyntheticInputProducer.cpp
Normal file
111
apps/RenderCadenceCompositor/video/SyntheticInputProducer.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include "SyntheticInputProducer.h"
|
||||
|
||||
#include "VideoIOFormat.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
namespace RenderCadenceCompositor
|
||||
{
|
||||
SyntheticInputProducer::SyntheticInputProducer(InputFrameMailbox& mailbox, SyntheticInputProducerConfig config) :
|
||||
mMailbox(mailbox),
|
||||
mConfig(config)
|
||||
{
|
||||
}
|
||||
|
||||
SyntheticInputProducer::~SyntheticInputProducer()
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
|
||||
bool SyntheticInputProducer::Start()
|
||||
{
|
||||
if (mThread.joinable())
|
||||
return true;
|
||||
if (mConfig.width == 0 || mConfig.height == 0)
|
||||
return false;
|
||||
|
||||
mStopping.store(false, std::memory_order_release);
|
||||
mThread = std::thread([this]() { ThreadMain(); });
|
||||
return true;
|
||||
}
|
||||
|
||||
void SyntheticInputProducer::Stop()
|
||||
{
|
||||
mStopping.store(true, std::memory_order_release);
|
||||
if (mThread.joinable())
|
||||
mThread.join();
|
||||
}
|
||||
|
||||
SyntheticInputProducerMetrics SyntheticInputProducer::Metrics() const
|
||||
{
|
||||
SyntheticInputProducerMetrics metrics;
|
||||
metrics.generatedFrames = mGeneratedFrames.load(std::memory_order_relaxed);
|
||||
metrics.submitMisses = mSubmitMisses.load(std::memory_order_relaxed);
|
||||
return metrics;
|
||||
}
|
||||
|
||||
void SyntheticInputProducer::ThreadMain()
|
||||
{
|
||||
const unsigned rowBytes = VideoIORowBytes(VideoIOPixelFormat::Bgra8, mConfig.width);
|
||||
std::vector<unsigned char> buffer(static_cast<std::size_t>(rowBytes) * static_cast<std::size_t>(mConfig.height));
|
||||
const auto frameDuration = std::chrono::duration_cast<std::chrono::steady_clock::duration>(
|
||||
std::chrono::duration<double, std::milli>(mConfig.frameDurationMilliseconds));
|
||||
|
||||
uint64_t frameIndex = 0;
|
||||
auto nextFrameTime = std::chrono::steady_clock::now();
|
||||
while (!mStopping.load(std::memory_order_acquire))
|
||||
{
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (now < nextFrameTime)
|
||||
{
|
||||
std::this_thread::sleep_for((std::min)(std::chrono::milliseconds(1), std::chrono::duration_cast<std::chrono::milliseconds>(nextFrameTime - now)));
|
||||
continue;
|
||||
}
|
||||
|
||||
GenerateFrame(frameIndex, buffer);
|
||||
if (!mMailbox.SubmitFrame(buffer.data(), rowBytes, frameIndex))
|
||||
mSubmitMisses.fetch_add(1, std::memory_order_relaxed);
|
||||
mGeneratedFrames.fetch_add(1, std::memory_order_relaxed);
|
||||
++frameIndex;
|
||||
nextFrameTime += frameDuration;
|
||||
if (std::chrono::steady_clock::now() - nextFrameTime > frameDuration * 4)
|
||||
nextFrameTime = std::chrono::steady_clock::now() + frameDuration;
|
||||
}
|
||||
}
|
||||
|
||||
void SyntheticInputProducer::GenerateFrame(uint64_t frameIndex, std::vector<unsigned char>& buffer) const
|
||||
{
|
||||
const float t = static_cast<float>(frameIndex) / 60.0f;
|
||||
const unsigned boxWidth = (std::max)(1u, mConfig.width / 5u);
|
||||
const unsigned boxHeight = (std::max)(1u, mConfig.height / 6u);
|
||||
const unsigned maxX = mConfig.width > boxWidth ? mConfig.width - boxWidth : 0u;
|
||||
const unsigned maxY = mConfig.height > boxHeight ? mConfig.height - boxHeight : 0u;
|
||||
const unsigned boxX = static_cast<unsigned>((0.5f + 0.5f * std::sin(t * 1.4f)) * static_cast<float>(maxX));
|
||||
const unsigned boxY = static_cast<unsigned>((0.5f + 0.5f * std::sin(t * 0.9f + 1.2f)) * static_cast<float>(maxY));
|
||||
const unsigned rowBytes = VideoIORowBytes(VideoIOPixelFormat::Bgra8, mConfig.width);
|
||||
|
||||
for (unsigned y = 0; y < mConfig.height; ++y)
|
||||
{
|
||||
for (unsigned x = 0; x < mConfig.width; ++x)
|
||||
{
|
||||
const std::size_t offset = static_cast<std::size_t>(y) * rowBytes + static_cast<std::size_t>(x) * 4;
|
||||
const unsigned checker = ((x / 80u) + (y / 80u) + static_cast<unsigned>(frameIndex / 15u)) & 1u;
|
||||
unsigned char red = checker ? 42 : 16;
|
||||
unsigned char green = checker ? 70 : 28;
|
||||
unsigned char blue = checker ? 110 : 55;
|
||||
if (x >= boxX && x < boxX + boxWidth && y >= boxY && y < boxY + boxHeight)
|
||||
{
|
||||
red = 245;
|
||||
green = static_cast<unsigned char>(160 + 60 * (0.5f + 0.5f * std::sin(t * 2.1f)));
|
||||
blue = 35;
|
||||
}
|
||||
|
||||
buffer[offset + 0] = blue;
|
||||
buffer[offset + 1] = green;
|
||||
buffer[offset + 2] = red;
|
||||
buffer[offset + 3] = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
49
apps/RenderCadenceCompositor/video/SyntheticInputProducer.h
Normal file
49
apps/RenderCadenceCompositor/video/SyntheticInputProducer.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include "../frames/InputFrameMailbox.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace RenderCadenceCompositor
|
||||
{
|
||||
struct SyntheticInputProducerConfig
|
||||
{
|
||||
unsigned width = 1920;
|
||||
unsigned height = 1080;
|
||||
double frameDurationMilliseconds = 1000.0 / 59.94;
|
||||
};
|
||||
|
||||
struct SyntheticInputProducerMetrics
|
||||
{
|
||||
uint64_t generatedFrames = 0;
|
||||
uint64_t submitMisses = 0;
|
||||
};
|
||||
|
||||
class SyntheticInputProducer
|
||||
{
|
||||
public:
|
||||
SyntheticInputProducer(InputFrameMailbox& mailbox, SyntheticInputProducerConfig config);
|
||||
SyntheticInputProducer(const SyntheticInputProducer&) = delete;
|
||||
SyntheticInputProducer& operator=(const SyntheticInputProducer&) = delete;
|
||||
~SyntheticInputProducer();
|
||||
|
||||
bool Start();
|
||||
void Stop();
|
||||
SyntheticInputProducerMetrics Metrics() const;
|
||||
|
||||
private:
|
||||
void ThreadMain();
|
||||
void GenerateFrame(uint64_t frameIndex, std::vector<unsigned char>& buffer) const;
|
||||
|
||||
InputFrameMailbox& mMailbox;
|
||||
SyntheticInputProducerConfig mConfig;
|
||||
std::thread mThread;
|
||||
std::atomic<bool> mStopping{ false };
|
||||
std::atomic<uint64_t> mGeneratedFrames{ 0 };
|
||||
std::atomic<uint64_t> mSubmitMisses{ 0 };
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user