Skip to content

Commit

Permalink
local have bitset rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Dec 12, 2024
1 parent f89aeae commit 84ade4d
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ target_sources(tomato PUBLIC
./tox_avatar_loader.cpp
./message_image_loader.hpp
./message_image_loader.cpp
./bitset_image_loader.hpp
./bitset_image_loader.cpp

./tox_avatar_manager.hpp
./tox_avatar_manager.cpp
Expand Down
85 changes: 85 additions & 0 deletions src/bitset_image_loader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "./bitset_image_loader.hpp"

#include <solanaceae/object_store/object_store.hpp>
#include <solanaceae/object_store/meta_components_file.hpp>

#include "./os_comps.hpp"

#include <entt/entity/entity.hpp>

#include <SDL3/SDL.h>

#include <iostream>

// fwd
namespace Message {
uint64_t getTimeMS(void);
}

BitsetImageLoader::BitsetImageLoader(void) {
}

std::optional<TextureEntry> BitsetImageLoader::load(TextureUploaderI& tu, ObjectHandle o) {
if (!static_cast<bool>(o)) {
std::cerr << "BIL error: trying to load invalid object\n";
return std::nullopt;
}

if (!o.all_of<ObjComp::F::LocalHaveBitset>()) {
// after completion, this is called until the texture times out
//std::cout << "BIL: no local have bitset\n";
return std::nullopt;
}

// TODO: const
auto& have = o.get<ObjComp::F::LocalHaveBitset>().have;

auto* surf = SDL_CreateSurfaceFrom(
have.size_bits(), 1,
SDL_PIXELFORMAT_INDEX1MSB, // LSB ?
have.data(), have.size_bytes()
);
if (surf == nullptr) {
std::cerr << "BIL error: bitset to 1bit surface creationg failed o:" << entt::to_integral(o.entity()) << "\n";
return std::nullopt;
}

SDL_Color colors[] {
{0, 0, 0, 0},
{255, 255, 255, 255},
};

SDL_Palette* palette = SDL_CreatePalette(2);
SDL_SetPaletteColors(palette, colors, 0, 2);
SDL_SetSurfacePalette(surf, palette);
auto* conv_surf = SDL_ConvertSurface(surf, SDL_PIXELFORMAT_RGBA32);

SDL_DestroySurface(surf);
SDL_DestroyPalette(palette);

if (conv_surf == nullptr) {
std::cerr << "BIL error: surface conversion failed o:" << entt::to_integral(o.entity()) << " : " << SDL_GetError() << "\n";
return std::nullopt;
}

SDL_LockSurface(conv_surf);

TextureEntry new_entry;
new_entry.timestamp_last_rendered = Message::getTimeMS();
new_entry.current_texture = 0;
new_entry.width = have.size_bits();
new_entry.height = 1;

const auto n_t = tu.upload(static_cast<uint8_t*>(conv_surf->pixels), conv_surf->w, conv_surf->h, TextureUploaderI::RGBA);
assert(n_t != 0);
new_entry.textures.push_back(n_t);
new_entry.frame_duration.push_back(0);

std::cout << "BIL: genereated bitset image o:" << entt::to_integral(o.entity()) << "\n";

SDL_UnlockSurface(conv_surf);
SDL_DestroySurface(conv_surf);

return new_entry;
}

14 changes: 14 additions & 0 deletions src/bitset_image_loader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <solanaceae/object_store/fwd.hpp>

#include "./texture_cache.hpp"

#include <optional>

class BitsetImageLoader {
public:
BitsetImageLoader(void);
std::optional<TextureEntry> load(TextureUploaderI& tu, ObjectHandle o);
};

97 changes: 77 additions & 20 deletions src/chat_gui4.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "./chat_gui4.hpp"

#include <solanaceae/object_store/object_store.hpp>

#include <solanaceae/message3/components.hpp>
#include <solanaceae/tox_messages/msg_components.hpp>
#include <solanaceae/tox_messages/obj_components.hpp>
Expand All @@ -18,13 +16,15 @@

#include <imgui/imgui.h>
#include <imgui/misc/cpp/imgui_stdlib.h>
#include <imgui/imgui_internal.h>

#include <SDL3/SDL.h>

#include "./chat_gui/contact_list.hpp"

#include "./media_meta_info_loader.hpp"
#include "./sdl_clipboard_utils.hpp"
#include "os_comps.hpp"

#include <cctype>
#include <ctime>
Expand Down Expand Up @@ -246,7 +246,19 @@ ChatGui4::ChatGui4(
ContactTextureCache& contact_tc,
MessageTextureCache& msg_tc,
Theme& theme
) : _conf(conf), _os(os), _rmm(rmm), _cr(cr), _contact_tc(contact_tc), _msg_tc(msg_tc), _theme(theme), _sip(tu) {
) :
_conf(conf),
_os(os),
_os_sr(_os.newSubRef(this)),
_rmm(rmm),
_cr(cr),
_contact_tc(contact_tc),
_msg_tc(msg_tc),
_b_tc(_bil, tu),
_theme(theme),
_sip(tu)
{
_os_sr.subscribe(ObjectStore_Event::object_update);
}

ChatGui4::~ChatGui4(void) {
Expand All @@ -263,6 +275,8 @@ ChatGui4::~ChatGui4(void) {
float ChatGui4::render(float time_delta) {
_fss.render();
_sip.render(time_delta);
_b_tc.update();
_b_tc.workLoadQueue();

const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->WorkPos);
Expand Down Expand Up @@ -1162,6 +1176,8 @@ void ChatGui4::renderMessageBodyFile(Message3Registry& reg, const Message3 e) {
// hacky
const auto* fts = o.try_get<ObjComp::Ephemeral::File::TransferStats>();
if (fts != nullptr && o.any_of<ObjComp::F::SingleInfo, ObjComp::F::CollectionInfo>()) {
const bool upload = o.all_of<ObjComp::F::TagLocalHaveAll>() && fts->total_down <= 0;

const int64_t total_size =
o.all_of<ObjComp::F::SingleInfo>() ?
o.get<ObjComp::F::SingleInfo>().file_size :
Expand All @@ -1170,7 +1186,7 @@ void ChatGui4::renderMessageBodyFile(Message3Registry& reg, const Message3 e) {

int64_t transfer_total {0u};
float transfer_rate {0.f};
if (o.all_of<ObjComp::F::TagLocalHaveAll>() && fts->total_down <= 0) {
if (upload) {
// if have all AND no dl -> show upload progress
ImGui::TextUnformatted(" up");
transfer_total = fts->total_up;
Expand All @@ -1183,7 +1199,12 @@ void ChatGui4::renderMessageBodyFile(Message3Registry& reg, const Message3 e) {
}
ImGui::SameLine();

float fraction = float(transfer_total) / total_size;
float fraction{0.f};
if (total_size > 0) {
fraction = float(transfer_total) / total_size;
} else if (o.all_of<ObjComp::F::TagLocalHaveAll>()) {
fraction = 1.f;
}

char overlay_buf[128];
if (transfer_rate > 0.000001f) {
Expand Down Expand Up @@ -1211,11 +1232,48 @@ void ChatGui4::renderMessageBodyFile(Message3Registry& reg, const Message3 e) {
std::snprintf(overlay_buf, sizeof(overlay_buf), "%.1f%%", fraction * 100 + 0.01f);
}

ImGui::ProgressBar(
fraction,
{-FLT_MIN, TEXT_BASE_HEIGHT},
overlay_buf
);
if (!upload && !o.all_of<ObjComp::F::TagLocalHaveAll>() && o.all_of<ObjComp::F::LocalHaveBitset>()) {
ImGui::BeginGroup();

// TODO: hights are all off

ImGui::ProgressBar(
fraction,
{-FLT_MIN, TEXT_BASE_HEIGHT*0.66f},
overlay_buf
);

auto const cursor_start_vec = ImGui::GetCursorScreenPos();
const ImVec2 bar_size{ImGui::GetContentRegionAvail().x, TEXT_BASE_HEIGHT*0.15f};
// TODO: replace with own version, so we dont have to internal
ImGui::RenderFrame(
cursor_start_vec,
{
cursor_start_vec.x + bar_size.x,
cursor_start_vec.y + bar_size.y
},
ImGui::GetColorU32(ImGuiCol_FrameBg),
false
);

auto [id, img_width, img_height] = _b_tc.get(o);
ImGui::Image(
id,
bar_size,
{0.f, 0.f}, // default
{1.f, 1.f}, // default
ImGui::GetStyleColorVec4(ImGuiCol_PlotHistogram)
);

ImGui::EndGroup();
//} else if (upload && o.all_of<ObjComp::F::RemoteHaveBitset>()) {
} else {
ImGui::ProgressBar(
fraction,
{-FLT_MIN, TEXT_BASE_HEIGHT},
overlay_buf
);
}
} else {
// infinite scrolling progressbar fallback
ImGui::TextUnformatted(" ??");
Expand All @@ -1227,16 +1285,6 @@ void ChatGui4::renderMessageBodyFile(Message3Registry& reg, const Message3 e) {
);
}

if (!o.all_of<ObjComp::F::TagLocalHaveAll>() && o.all_of<ObjComp::F::LocalHaveBitset>()) {
// texture based on have bitset
// TODO: missing have chunks/chunksize to get the correct size

//const auto& bitest = o.get<ObjComp::F::LocalHaveBitset>().have;
// generate 1bit sdlsurface zerocopy using bitset.data() and bitset.size_bytes()
// optionally scale down filtered (would copy)
// update texture? in cache?
}

if (o.all_of<ObjComp::F::FrameDims>()) {
const auto& frame_dims = o.get<ObjComp::F::FrameDims>();

Expand Down Expand Up @@ -1721,3 +1769,12 @@ void ChatGui4::sendFileList(const std::vector<std::string_view>& list) {
}
}

bool ChatGui4::onEvent(const ObjectStore::Events::ObjectUpdate& e) {
if (!e.e.all_of<ObjComp::F::LocalHaveBitset>()) {
return false;
}

_b_tc.stale(e.e);
return false;
}

12 changes: 10 additions & 2 deletions src/chat_gui4.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <solanaceae/object_store/fwd.hpp>
#include <solanaceae/object_store/object_store.hpp>
#include <solanaceae/message3/registry_message_model.hpp>
#include <solanaceae/util/config_model.hpp>

Expand All @@ -10,6 +10,7 @@
#include "./texture_cache.hpp"
#include "./tox_avatar_loader.hpp"
#include "./message_image_loader.hpp"
#include "./bitset_image_loader.hpp"
#include "./chat_gui/file_selector.hpp"
#include "./chat_gui/send_image_popup.hpp"

Expand All @@ -23,15 +24,19 @@

using ContactTextureCache = TextureCache<void*, Contact3, ToxAvatarLoader>;
using MessageTextureCache = TextureCache<void*, Message3Handle, MessageImageLoader>;
using BitsetTextureCache = TextureCache<void*, ObjectHandle, BitsetImageLoader>;

class ChatGui4 {
class ChatGui4 : public ObjectStoreEventI {
ConfigModelI& _conf;
ObjectStore2& _os;
ObjectStoreEventProviderI::SubscriptionReference _os_sr;
RegistryMessageModelI& _rmm;
Contact3Registry& _cr;

ContactTextureCache& _contact_tc;
MessageTextureCache& _msg_tc;
BitsetImageLoader _bil;
BitsetTextureCache _b_tc;

Theme& _theme;

Expand Down Expand Up @@ -86,6 +91,9 @@ class ChatGui4 {
//bool renderSubContactListContact(const Contact3 c, const bool selected) const;

void pasteFile(const char* mime_type);

protected:
bool onEvent(const ObjectStore::Events::ObjectUpdate&) override;
};


2 changes: 1 addition & 1 deletion src/message_image_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class MessageImageLoader {

public:
MessageImageLoader(void);
std::optional<TextureEntry> load(TextureUploaderI& tu, Message3Handle c);
std::optional<TextureEntry> load(TextureUploaderI& tu, Message3Handle m);
};

Loading

0 comments on commit 84ade4d

Please sign in to comment.