Accumulated uncommitted work: GHOST Web backend, WebGL/GPU web layer, web workflow engine and protocol, tooling, docs and task records. Large generated evidence, user-testing archives, build snapshots and imported test assets remain untracked. Co-Authored-By: Claude Code <noreply@anthropic.com>
218 lines
7.5 KiB
C++
218 lines
7.5 KiB
C++
/* SPDX-FileCopyrightText: 2026 Blender Authors
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
#include "GHOST_WindowWeb.hh"
|
|
#include "GHOST_ContextWeb.hh"
|
|
#include "GHOST_Event.hh"
|
|
#include "GHOST_SystemWeb.hh"
|
|
#include <emscripten.h>
|
|
#include <algorithm>
|
|
|
|
namespace {
|
|
/* Never reuse a revision when Main/context restoration creates a new window. */
|
|
uint32_t cursor_revision = 0;
|
|
}
|
|
|
|
GHOST_WindowWeb::GHOST_WindowWeb(const char *selector,
|
|
uint32_t width,
|
|
uint32_t height,
|
|
float pixel_ratio)
|
|
: GHOST_WindowNULL("Blender", 0, 0, width, height, GHOST_kWindowStateNormal, nullptr,
|
|
GHOST_kDrawingContextTypeNone, GHOST_CONTEXT_PARAMS_NONE),
|
|
selector_(selector), width_(width), height_(height)
|
|
{
|
|
cursor_revision_ = ++cursor_revision;
|
|
/* Register before setClientSize: worker selectors cannot query a DOM. */
|
|
EM_ASM({ GL.offscreenCanvases[UTF8ToString($0).slice(1)] = Module['canvas']; }, selector_.c_str());
|
|
setPixelRatio(pixel_ratio);
|
|
if (setClientSize(width, height)) setDrawingContextType(GHOST_kDrawingContextTypeWebGL);
|
|
}
|
|
|
|
GHOST_Context *GHOST_WindowWeb::newDrawingContext(GHOST_TDrawingContextType type)
|
|
{
|
|
if (type != GHOST_kDrawingContextTypeWebGL) return nullptr;
|
|
auto *context = new GHOST_ContextWeb(want_context_params_, selector_.c_str());
|
|
if (context->initializeDrawingContext()) return context;
|
|
delete context;
|
|
return nullptr;
|
|
}
|
|
|
|
bool GHOST_WindowWeb::getValid() const
|
|
{
|
|
return drawing_context_type_ == GHOST_kDrawingContextTypeWebGL;
|
|
}
|
|
|
|
void GHOST_WindowWeb::setTitle(const char *title)
|
|
{
|
|
title_ = title ? title : "";
|
|
}
|
|
|
|
std::string GHOST_WindowWeb::getTitle() const
|
|
{
|
|
return title_;
|
|
}
|
|
|
|
GHOST_TSuccess GHOST_WindowWeb::invalidate()
|
|
{
|
|
auto *system = dynamic_cast<GHOST_SystemWeb *>(GHOST_ISystem::getSystem());
|
|
if (!system) return GHOST_kFailure;
|
|
return system->pushBrowserEvent(std::make_unique<GHOST_Event>(
|
|
system->getMilliSeconds(), GHOST_kEventWindowUpdate, this));
|
|
}
|
|
|
|
GHOST_TSuccess GHOST_WindowWeb::setState(GHOST_TWindowState state)
|
|
{
|
|
if (state != GHOST_kWindowStateFullScreen && state != GHOST_kWindowStateNormal) {
|
|
return GHOST_kFailure;
|
|
}
|
|
#ifdef __EMSCRIPTEN__
|
|
/* The worker cannot touch the DOM. Ask the host and let the next GPU frame
|
|
* carry the browser observation back through web_engine_gpu_ui_fullscreen_ack. */
|
|
EM_ASM({
|
|
if (typeof postMessage === 'function') {
|
|
postMessage({type: 'blender-native-fullscreen-request', fullscreen: !!$0});
|
|
}
|
|
}, state == GHOST_kWindowStateFullScreen);
|
|
return GHOST_kSuccess;
|
|
#else
|
|
return GHOST_kFailure;
|
|
#endif
|
|
}
|
|
|
|
void GHOST_WindowWeb::getClientBounds(GHOST_Rect &bounds) const
|
|
{
|
|
bounds.set(0, 0, width_, height_);
|
|
}
|
|
|
|
GHOST_TSuccess GHOST_WindowWeb::setClientSize(uint32_t width, uint32_t height)
|
|
{
|
|
/* Reassigning unchanged canvas dimensions discards the preserved native frame. */
|
|
int canvas_width = 0, canvas_height = 0;
|
|
const bool unchanged = emscripten_get_canvas_element_size(selector_.c_str(), &canvas_width, &canvas_height) == EMSCRIPTEN_RESULT_SUCCESS && canvas_width == width && canvas_height == height;
|
|
if (!width || !height || width > 16384 || height > 16384 ||
|
|
(!unchanged && emscripten_set_canvas_element_size(selector_.c_str(), width, height) != EMSCRIPTEN_RESULT_SUCCESS))
|
|
{
|
|
return GHOST_kFailure;
|
|
}
|
|
width_ = width;
|
|
height_ = height;
|
|
return GHOST_kSuccess;
|
|
}
|
|
|
|
GHOST_TSuccess GHOST_WindowWeb::setClientWidth(uint32_t width)
|
|
{
|
|
return setClientSize(width, height_);
|
|
}
|
|
GHOST_TSuccess GHOST_WindowWeb::setClientHeight(uint32_t height)
|
|
{
|
|
return setClientSize(width_, height);
|
|
}
|
|
GHOST_TSuccess GHOST_WindowWeb::activateDrawingContext()
|
|
{
|
|
return GHOST_Window::activateDrawingContext();
|
|
}
|
|
GHOST_TSuccess GHOST_WindowWeb::swapBufferRelease()
|
|
{
|
|
return GHOST_Window::swapBufferRelease();
|
|
}
|
|
void GHOST_WindowWeb::setPixelRatio(float ratio)
|
|
{
|
|
/* The browser ABI already sends backing pixels for bounds and pointer positions. */
|
|
native_pixel_size_ = 1.0f;
|
|
pixel_ratio_ = ratio;
|
|
}
|
|
uint16_t GHOST_WindowWeb::getDPIHint()
|
|
{
|
|
return uint16_t(96.0f * pixel_ratio_);
|
|
}
|
|
|
|
GHOST_TSuccess GHOST_WindowWeb::setWindowCursorGrab(GHOST_TGrabCursorMode mode)
|
|
{
|
|
auto *system = dynamic_cast<GHOST_SystemWeb *>(GHOST_ISystem::getSystem());
|
|
if (!system) return GHOST_kFailure;
|
|
if (mode == GHOST_kGrabWrap || mode == GHOST_kGrabHide) {
|
|
if (!system->getCursorPosition(cursor_grab_init_pos_[0], cursor_grab_init_pos_[1])) {
|
|
return GHOST_kFailure;
|
|
}
|
|
setCursorGrabAccum(0, 0);
|
|
}
|
|
else if (mode == GHOST_kGrabDisable && getCursorGrabModeIsWarp()) {
|
|
int32_t x = cursor_grab_init_pos_[0], y = cursor_grab_init_pos_[1];
|
|
if (cursor_grab_ != GHOST_kGrabHide) system->getCursorPosition(x, y);
|
|
system->setCursorPosition(x, y);
|
|
setCursorGrabAccum(0, 0);
|
|
}
|
|
/* The serialized frame receipt carries this native mode to the DOM owner.
|
|
* That owner alone can request/release Pointer Lock on the transferred canvas. */
|
|
return GHOST_kSuccess;
|
|
}
|
|
|
|
bool GHOST_WindowWeb::getCursorGrabUseSoftwareDisplay()
|
|
{
|
|
return cursor_visible_ && (cursor_grab_ == GHOST_kGrabNormal || cursor_grab_ == GHOST_kGrabWrap);
|
|
}
|
|
|
|
GHOST_TSuccess GHOST_WindowWeb::hasCursorShape(GHOST_TStandardCursor /*shape*/)
|
|
{
|
|
/* Use WM's original SVG assets, including tool-specific cursors for which
|
|
* browsers have no standard shape. Do not inherit NULL's false success. */
|
|
return GHOST_kFailure;
|
|
}
|
|
|
|
GHOST_TSuccess GHOST_WindowWeb::setWindowCursorShape(GHOST_TStandardCursor shape)
|
|
{
|
|
if (shape != GHOST_kStandardCursorDefault) return GHOST_kFailure;
|
|
cursor_pixels_.clear();
|
|
cursor_revision_ = ++cursor_revision;
|
|
return GHOST_kSuccess;
|
|
}
|
|
|
|
GHOST_TSuccess GHOST_WindowWeb::setWindowCursorVisibility(bool visible)
|
|
{
|
|
if (visible != cursor_visible_) cursor_revision_ = ++cursor_revision;
|
|
cursor_visible_ = visible;
|
|
return GHOST_kSuccess;
|
|
}
|
|
|
|
GHOST_TSuccess GHOST_WindowWeb::setWindowCustomCursorShape(const uint8_t *bitmap,
|
|
const uint8_t *mask,
|
|
const int size[2],
|
|
const int hot_spot[2],
|
|
bool /*can_invert_color*/)
|
|
{
|
|
if (!bitmap || size[0] < 1 || size[1] < 1 || size[0] > 255 || size[1] > 255 ||
|
|
hot_spot[0] < 0 || hot_spot[1] < 0 || hot_spot[0] >= size[0] || hot_spot[1] >= size[1])
|
|
{
|
|
return GHOST_kFailure;
|
|
}
|
|
std::vector<uint8_t> pixels(size_t(size[0]) * size[1] * 4);
|
|
if (!mask) {
|
|
std::copy_n(bitmap, pixels.size(), pixels.data());
|
|
}
|
|
else {
|
|
/* Original WM numeric/progress cursors still use XBM (LSB first). */
|
|
const int stride = (size[0] + 7) / 8;
|
|
for (int y = 0; y < size[1]; y++) {
|
|
for (int x = 0; x < size[0]; x++) {
|
|
const int offset = y * stride + x / 8, bit = 1 << (x % 8);
|
|
uint8_t *pixel = pixels.data() + (y * size[0] + x) * 4;
|
|
pixel[0] = pixel[1] = pixel[2] = (bitmap[offset] & bit) ? 0 : 255;
|
|
pixel[3] = (mask[offset] & bit) ? 255 : 0;
|
|
}
|
|
}
|
|
}
|
|
cursor_pixels_ = std::move(pixels);
|
|
std::copy_n(size, 2, cursor_size_);
|
|
std::copy_n(hot_spot, 2, cursor_hotspot_);
|
|
cursor_revision_ = ++cursor_revision;
|
|
return GHOST_kSuccess;
|
|
}
|
|
|
|
GHOST_TSuccess GHOST_WindowWeb::getCursorBitmap(GHOST_CursorBitmapRef *bitmap)
|
|
{
|
|
if (!bitmap || cursor_pixels_.empty()) return GHOST_kFailure;
|
|
bitmap->data = cursor_pixels_.data();
|
|
std::copy_n(cursor_size_, 2, bitmap->data_size);
|
|
std::copy_n(cursor_hotspot_, 2, bitmap->hot_spot);
|
|
return GHOST_kSuccess;
|
|
}
|