7 #include "sol3/core/msg/buffer.h"
8 #include "sol3/core/msg/text.h"
36 uint32_t component_id,
37 size_t slot_count = 2,
38 size_t slot_size = 1024,
41 : exchange_(exchange),
42 text_input_(exchange, component_id),
43 buffer_input_(exchange, component_id),
44 text_output_(exchange, component_id, slot_count, slot_size),
45 buffer_output_(exchange, component_id, slot_count, slot_size),
48 for (
size_t i = 0; i < slot_count; ++i) {
50 info.mutate_id(buffer_output_.port().encoded());
51 info.mutate_idx(
static_cast<uint32_t
>(i + 1));
52 info.mutate_format(msg::BufferFormat_CPU_GENERIC);
53 info.mutate_width(width_);
54 info.mutate_height(height_);
55 info.mutate_stride_w(width_);
56 info.mutate_stride_h(height_);
57 info.mutate_size(
static_cast<uint64_t
>(width_) * height_);
58 info.mutate_buffer_use(msg::BufferUse_NONE);
62 last_buffer_info_ = buffer_pool_.front()->info();
74 auto text_view = text_input_.readHead();
75 if (text_view.hasValue()) {
76 auto const& payload = text_view.payload();
78 last_text_ = payload.data()->str();
82 auto buffer_view = buffer_input_.readHead();
83 if (buffer_view.hasValue()) {
84 auto const& payload = buffer_view.payload();
86 last_buffer_info_ = *payload.info();
91 text_output_.payload().data =
92 "echo: " + last_text_ +
" step=" + std::to_string(step_count_);
93 result.
text_seq = text_output_.write();
96 auto& buffer = buffer_pool_[step_count_ % buffer_pool_.size()];
97 auto* data =
static_cast<uint8_t*
>(buffer->mutableData());
98 if (last_buffer_info_.id() != 0 && last_buffer_info_.size() != 0) {
100 exchange_.
get(last_buffer_info_.id(), last_buffer_info_.idx());
101 if (incoming && data !=
nullptr) {
102 auto copy_size = std::min(buffer->size(), incoming->size());
103 std::memcpy(data, incoming->data(), copy_size);
105 }
else if (data !=
nullptr) {
106 std::memset(data,
static_cast<int>(step_count_ & 0xFF), buffer->size());
108 buffer_output_.payload().info = buffer->info();
122 uint32_t height_ = 0;
123 uint64_t step_count_ = 0;
124 std::string last_text_ =
"";
125 msg::BufferInfo last_buffer_info_{};
126 std::vector<std::unique_ptr<IBufferMutable>> buffer_pool_;
Definition: demo_component.h:26
MessagePort textOutputPort() const
Definition: demo_component.h:67
DemoStepResult step()
Definition: demo_component.h:70
MessagePort bufferInputPort() const
Definition: demo_component.h:66
MessagePort textInputPort() const
Definition: demo_component.h:65
MessagePort bufferOutputPort() const
Definition: demo_component.h:68
DemoComponent(IBufferExchange &exchange, uint32_t component_id, size_t slot_count=2, size_t slot_size=1024, uint32_t width=8, uint32_t height=8)
Definition: demo_component.h:34
Interface for registering, discovering, and disposing shared buffers.
Definition: shmem_buffer.h:112
virtual void registerBuffer(IBufferMutable const &buffer)=0
virtual std::shared_ptr< IBufferConst > get(uint32_t id, uint32_t idx) const =0
Definition: message_output.h:20
Definition: message_port.h:19
Definition: any_message_input.h:15
std::unique_ptr< IBufferMutable > makeShmemBuffer(BufferInfo const &info)
Creates a mutable buffer, matching the BufferInfo.
Definition: demo_component.h:18
bool buffer_written
Definition: demo_component.h:20
uint64_t text_seq
Definition: demo_component.h:21
bool text_written
Definition: demo_component.h:19
uint64_t buffer_seq
Definition: demo_component.h:22
Compile time message type and id specification.
Definition: message_spec.h:15