Add headless EGL/OSMesa OpenGL UI context rendering for web viewer

PiperOrigin-RevId: 957060514
Change-Id: I8a0df22255c5a938d18e172c638a8c83dfe64b1b
This commit is contained in:
Matija Kecman
2026-07-31 04:35:28 -07:00
committed by Copybara-Service
parent 47e1b1bd1f
commit f1f52a92b8
5 changed files with 460 additions and 30 deletions
+2
View File
@@ -44,6 +44,8 @@ target_sources(${MUJOCO_PLATFORM_TARGET_NAME}
sim/step_control.h
ux/enum_utils.h
ux/file_dialog.h
ux/fonts.cc
ux/fonts.h
ux/gui.cc
ux/gui.h
ux/gui_spec.cc
+2 -30
View File
@@ -32,6 +32,7 @@
#include <imgui.h>
#include <mujoco/mujoco.h>
#include "experimental/platform/hal/graphics_mode.h"
#include "experimental/platform/ux/fonts.h"
#include "user/user_resource.h"
// Because X11/Xlib.h defines Status.
@@ -63,36 +64,7 @@ static void InitImGui(SDL_Window* window, float content_scale,
style.FontScaleDpi = content_scale;
if (load_fonts) {
mjResource* font = nullptr;
int size = 0;
void* data = nullptr;
ImFontConfig main_cfg;
main_cfg.FontDataOwnedByAtlas = false;
font =
mju_openResource("", "font:AtkinsonHyperlegibleNext[wght].ttf",
nullptr, nullptr, 0);
size = mju_readResource(font, const_cast<const void**>(&data));
io.Fonts->AddFontFromMemoryTTF(data, size, 16.f, &main_cfg);
ImFontConfig icon_cfg;
icon_cfg.FontDataOwnedByAtlas = false;
icon_cfg.MergeMode = true;
font = mju_openResource("", "font:fontawesome-webfont.ttf", nullptr,
nullptr, 0);
size = mju_readResource(font, const_cast<const void**>(&data));
constexpr ImWchar icon_ranges[] = {0xf000, 0xf3ff, 0x000};
io.Fonts->AddFontFromMemoryTTF(data, size, 13.f, &icon_cfg, icon_ranges);
ImFontConfig mono_cfg;
mono_cfg.FontDataOwnedByAtlas = false;
font = mju_openResource("", "font:AtkinsonHyperlegibleMono-Regular.ttf", nullptr,
nullptr, 0);
size = mju_readResource(font, const_cast<const void**>(&data));
io.Fonts->AddFontFromMemoryTTF(data, size, 14.f, &mono_cfg);
// Note: we purposefully do not "close" the font resources as ImGui may
// need them again to resize fonts.
AddStudioFonts(LoadFontResource);
}
}
+106
View File
@@ -0,0 +1,106 @@
// Copyright 2026 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "experimental/platform/ux/fonts.h"
#include <cstddef>
#include <cstring>
#include <filesystem> // NOLINT(build/c++17)
#include <fstream>
#include <ios>
#include <string>
#include <string_view>
#include <vector>
#include <imgui.h>
#include <mujoco/mujoco.h>
namespace mujoco::platform {
namespace {
// Copies `data` into ImGui-owned memory and adds it to the current context's
// atlas; the atlas frees the copy with the context.
ImFont* AddFontCopy(const std::vector<std::byte>& data, float size_pixels,
const ImFontConfig* config = nullptr,
const ImWchar* glyph_ranges = nullptr) {
void* copy = ImGui::MemAlloc(data.size());
memcpy(copy, data.data(), data.size());
return ImGui::GetIO().Fonts->AddFontFromMemoryTTF(
copy, static_cast<int>(data.size()), size_pixels, config, glyph_ranges);
}
} // namespace
std::vector<std::byte> LoadFontAsset(const std::string& assets_dir,
std::string_view filename) {
if (assets_dir.empty()) return {};
const std::string file_path =
(std::filesystem::path(assets_dir) / filename).string();
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
return {};
}
const std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::byte> buffer(file_size);
if (!file.read(reinterpret_cast<char*>(buffer.data()), file_size)) {
return {};
}
return buffer;
}
std::vector<std::byte> LoadFontResource(std::string_view filename) {
const std::string name = std::string("font:") + std::string(filename);
mjResource* resource =
mju_openResource("", name.c_str(), nullptr, nullptr, 0);
if (resource == nullptr) {
return {};
}
const void* data = nullptr;
const int size = mju_readResource(resource, &data);
std::vector<std::byte> buffer;
if (size > 0) {
const std::byte* bytes = static_cast<const std::byte*>(data);
buffer.assign(bytes, bytes + size);
}
// Resource can be closed because the font atlas owns a copy of the data.
mju_closeResource(resource);
return buffer;
}
void AddStudioFonts(const FontLoaderFn& load) {
const std::vector<std::byte> main_data = load(kMainFontFile);
if (!main_data.empty()) {
AddFontCopy(main_data, 16.0f);
}
// The icon font merges into the font added above; without a base font the
// merge has nothing to attach to, so skip it.
const std::vector<std::byte> icon_data = load(kIconFontFile);
if (!icon_data.empty() && !main_data.empty()) {
ImFontConfig icon_config;
icon_config.MergeMode = true;
static constexpr ImWchar kIconRanges[] = {0xf000, 0xf3ff, 0};
AddFontCopy(icon_data, 13.0f, &icon_config, kIconRanges);
}
const std::vector<std::byte> mono_data = load(kMonoFontFile);
if (!mono_data.empty()) {
AddFontCopy(mono_data, 14.0f);
}
}
} // namespace mujoco::platform
+48
View File
@@ -0,0 +1,48 @@
// Copyright 2026 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef MUJOCO_SRC_EXPERIMENTAL_PLATFORM_UX_FONTS_H_
#define MUJOCO_SRC_EXPERIMENTAL_PLATFORM_UX_FONTS_H_
#include <cstddef>
#include <functional>
#include <string>
#include <string_view>
#include <vector>
namespace mujoco::platform {
// The Studio UI font files (every viewer loads this same files).
inline constexpr char kMainFontFile[] = "AtkinsonHyperlegibleNext[wght].ttf";
inline constexpr char kIconFontFile[] = "fontawesome-webfont.ttf";
inline constexpr char kMonoFontFile[] = "AtkinsonHyperlegibleMono-Regular.ttf";
// Maps a Studio font filename to its TTF bytes; empty means unavailable.
using FontLoaderFn = std::function<std::vector<std::byte>(std::string_view)>;
// A FontLoaderFn that reads font `filename` from an `assets_dir` folder.
std::vector<std::byte> LoadFontAsset(const std::string& assets_dir,
std::string_view filename);
// A FontLoaderFn that reads font `font:<filename>` using a resource provider.
std::vector<std::byte> LoadFontResource(std::string_view filename);
// Adds the Studio fonts to the current ImGui context's atlas, fetching font
// data via `load`. The ImGui font atlas will own a copy of the data so it can
// rebuild at new DPI scales without buffers used by `load` staying alive.
void AddStudioFonts(const FontLoaderFn& load);
} // namespace mujoco::platform
#endif // MUJOCO_SRC_EXPERIMENTAL_PLATFORM_UX_FONTS_H_