core: refactor renderable size ownership (#4512)

This commit is contained in:
Bart Louwers
2026-09-01 17:35:29 +02:00
committed by GitHub
parent be3f03b86e
commit e5d0fce32a
35 changed files with 131 additions and 122 deletions

View File

@@ -46,21 +46,21 @@ gl::ProcAddress AndroidGLRendererBackend::getExtensionFunctionPointer(const char
void AndroidGLRendererBackend::updateViewPort() {
assert(gfx::BackendScope::exists());
setViewport(0, 0, size);
setViewport(0, 0, getSize());
}
void AndroidGLRendererBackend::resizeFramebuffer(int width, int height) {
size = {static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
setRenderableSize({static_cast<uint32_t>(width), static_cast<uint32_t>(height)});
}
PremultipliedImage AndroidGLRendererBackend::readFramebuffer() {
assert(gfx::BackendScope::exists());
return gl::RendererBackend::readFramebuffer(size);
return gl::RendererBackend::readFramebuffer(getSize());
}
void AndroidGLRendererBackend::updateAssumedState() {
assumeFramebufferBinding(0);
assumeViewport(0, 0, size);
assumeViewport(0, 0, getSize());
}
void AndroidGLRendererBackend::markContextLost() {

View File

@@ -160,8 +160,6 @@ public:
WGPUTexture currentTexture = nullptr;
WGPUTextureView currentTextureView = nullptr;
bool needsPresent = false;
Size framebufferSize;
};
AndroidWebGPURendererBackend::AndroidWebGPURendererBackend(ANativeWindow* window_)
@@ -303,9 +301,8 @@ AndroidWebGPURendererBackend::AndroidWebGPURendererBackend(ANativeWindow* window
const int w = ANativeWindow_getWidth(window);
const int h = ANativeWindow_getHeight(window);
if (w > 0 && h > 0) {
impl->framebufferSize = {static_cast<uint32_t>(w), static_cast<uint32_t>(h)};
size = impl->framebufferSize;
configureSurface(impl->framebufferSize.width, impl->framebufferSize.height);
setRenderableSize({static_cast<uint32_t>(w), static_cast<uint32_t>(h)});
configureSurface(static_cast<uint32_t>(w), static_cast<uint32_t>(h));
}
}
@@ -508,8 +505,7 @@ void AndroidWebGPURendererBackend::createDepthStencilTexture(uint32_t width, uin
void AndroidWebGPURendererBackend::resizeFramebuffer(int width, int height) {
if (width <= 0 || height <= 0) return;
impl->framebufferSize = {static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
size = impl->framebufferSize;
setRenderableSize({static_cast<uint32_t>(width), static_cast<uint32_t>(height)});
if (impl->currentTextureView) {
wgpuTextureViewRelease(impl->currentTextureView);
@@ -520,16 +516,16 @@ void AndroidWebGPURendererBackend::resizeFramebuffer(int width, int height) {
impl->currentTexture = nullptr;
}
configureSurface(impl->framebufferSize.width, impl->framebufferSize.height);
configureSurface(static_cast<uint32_t>(width), static_cast<uint32_t>(height));
}
PremultipliedImage AndroidWebGPURendererBackend::readFramebuffer() {
if (!impl->currentTexture || !impl->device || !impl->queue || impl->framebufferSize.width == 0 ||
impl->framebufferSize.height == 0) {
return PremultipliedImage(impl->framebufferSize.isEmpty() ? Size(2, 2) : impl->framebufferSize);
const auto framebufferSize = getSize();
if (!impl->currentTexture || !impl->device || !impl->queue || framebufferSize.isEmpty()) {
return PremultipliedImage(framebufferSize.isEmpty() ? Size(2, 2) : framebufferSize);
}
const auto& fbSize = impl->framebufferSize;
const auto& fbSize = framebufferSize;
constexpr uint32_t bytesPerPixel = 4;
constexpr uint32_t bytesPerRowAlignment = 256u;
const uint32_t rowStride = fbSize.width * bytesPerPixel;
@@ -683,7 +679,8 @@ void* AndroidWebGPURendererBackend::getCurrentTextureView() {
auto status = static_cast<WGPUSurfaceGetCurrentTextureStatus>(surfaceTexture.status);
if (status == WGPUSurfaceGetCurrentTextureStatus_Outdated || status == WGPUSurfaceGetCurrentTextureStatus_Lost) {
wgpuTextureRelease(surfaceTexture.texture);
configureSurface(impl->framebufferSize.width, impl->framebufferSize.height);
const auto framebufferSize = getSize();
configureSurface(framebufferSize.width, framebufferSize.height);
return nullptr;
}
@@ -698,10 +695,9 @@ void* AndroidWebGPURendererBackend::getCurrentTextureView() {
// Actual texture size may differ from configured size (Vulkan currentExtent clamping)
const uint32_t texW = wgpuTextureGetWidth(surfaceTexture.texture);
const uint32_t texH = wgpuTextureGetHeight(surfaceTexture.texture);
if (texW != size.width || texH != size.height) {
if (const auto framebufferSize = getSize(); texW != framebufferSize.width || texH != framebufferSize.height) {
if (texW > 0 && texH > 0) {
size = {texW, texH};
impl->framebufferSize = size;
setRenderableSize({texW, texH});
createDepthStencilTexture(texW, texH);
}
}
@@ -759,7 +755,7 @@ void* AndroidWebGPURendererBackend::getDepthStencilView() {
}
mln::Size AndroidWebGPURendererBackend::getFramebufferSize() const {
return impl->framebufferSize;
return getSize();
}
} // namespace android

View File

@@ -9,7 +9,7 @@ HeadlessBackend::HeadlessBackend(Size size_)
: mln::gfx::Renderable(size_, nullptr) {}
void HeadlessBackend::setSize(Size size_) {
size = size_;
setRenderableSize(size_);
resource.reset();
}

View File

@@ -97,7 +97,8 @@ gfx::Renderable& HeadlessBackend::getDefaultRenderable() {
MLN_TRACE_FUNC();
if (!resource) {
resource = std::make_unique<HeadlessRenderableResource>(*this, static_cast<gl::Context&>(getContext()), size);
resource = std::make_unique<HeadlessRenderableResource>(
*this, static_cast<gl::Context&>(getContext()), getSize());
}
return *this;
}
@@ -115,7 +116,7 @@ void HeadlessBackend::updateAssumedState() {
PremultipliedImage HeadlessBackend::readStillImage() {
MLN_TRACE_FUNC();
return static_cast<gl::Context&>(getContext()).readFramebuffer<PremultipliedImage>(size);
return static_cast<gl::Context&>(getContext()).readFramebuffer<PremultipliedImage>(getSize());
}
RendererBackend* HeadlessBackend::getRendererBackend() {

View File

@@ -83,7 +83,8 @@ gfx::Renderable& HeadlessBackend::getDefaultRenderable() {
void HeadlessBackend::ensureResource() {
if (!resource) {
resource = std::make_unique<HeadlessRenderableResource>(*this, static_cast<mtl::Context&>(getContext()), size);
resource = std::make_unique<HeadlessRenderableResource>(
*this, static_cast<mtl::Context&>(getContext()), getSize());
}
}

View File

@@ -23,6 +23,6 @@ public:
}
virtual mln::gfx::RendererBackend& getRendererBackend() = 0;
virtual mln::Size getSize() const = 0;
virtual void setSize(mln::Size) = 0;
virtual mln::Size getFramebufferSize() const = 0;
virtual void setFramebufferSize(mln::Size) = 0;
};

View File

@@ -15,7 +15,7 @@ public:
MLN_TRACE_FUNC();
backend.setFramebufferBinding(0);
backend.setViewport(0, 0, backend.getSize());
backend.setViewport(0, 0, backend.getFramebufferSize());
}
void swap() override {
@@ -72,15 +72,15 @@ void GLFWGLBackend::updateAssumedState() {
MLN_TRACE_FUNC();
assumeFramebufferBinding(0);
setViewport(0, 0, size);
setViewport(0, 0, getSize());
}
mln::Size GLFWGLBackend::getSize() const {
return size;
mln::Size GLFWGLBackend::getFramebufferSize() const {
return getSize();
}
void GLFWGLBackend::setSize(const mln::Size newSize) {
size = newSize;
void GLFWGLBackend::setFramebufferSize(const mln::Size newSize) {
setRenderableSize(newSize);
}
void GLFWGLBackend::swap() {

View File

@@ -17,8 +17,8 @@ public:
// GLFWRendererBackend implementation
public:
mln::gfx::RendererBackend& getRendererBackend() override { return *this; }
mln::Size getSize() const override;
void setSize(mln::Size) override;
mln::Size getFramebufferSize() const override;
void setFramebufferSize(mln::Size) override;
// mln::gfx::RendererBackend implementation
public:

View File

@@ -14,8 +14,8 @@ public:
GLFWMetalBackend(GLFWwindow *window_, const bool capFrameRate);
~GLFWMetalBackend() = default;
mln::gfx::RendererBackend &getRendererBackend() override;
void setSize(mln::Size) override;
mln::Size getSize() const override;
void setFramebufferSize(mln::Size) override;
mln::Size getFramebufferSize() const override;
private:
NSWindow *window;

View File

@@ -11,6 +11,6 @@ GLFWMetalBackend::GLFWMetalBackend(GLFWwindow* window_, [[maybe_unused]] const b
mln::gfx::RendererBackend& GLFWMetalBackend::getRendererBackend() { return rendererBackend; }
void GLFWMetalBackend::setSize(mln::Size size) { rendererBackend.setSize(size); }
void GLFWMetalBackend::setFramebufferSize(mln::Size size) { rendererBackend.setSize(size); }
mln::Size GLFWMetalBackend::getSize() const { return rendererBackend.getSize(); }
mln::Size GLFWMetalBackend::getFramebufferSize() const { return rendererBackend.getSize(); }

View File

@@ -312,10 +312,10 @@ GLFWView::GLFWView(bool fullscreen_,
#if defined(__APPLE__) && !defined(MLN_RENDER_BACKEND_VULKAN)
int fbW, fbH;
glfwGetFramebufferSize(window, &fbW, &fbH);
backend->setSize({static_cast<uint32_t>(fbW), static_cast<uint32_t>(fbH)});
backend->setFramebufferSize({static_cast<uint32_t>(fbW), static_cast<uint32_t>(fbH)});
#endif
pixelRatio = static_cast<float>(backend->getSize().width) / width;
pixelRatio = static_cast<float>(backend->getFramebufferSize().width) / width;
glfwMakeContextCurrent(nullptr);
@@ -1026,7 +1026,7 @@ void GLFWView::onWindowResize(GLFWwindow *window, int width, int height) {
#ifdef __APPLE__
int fbW, fbH;
glfwGetFramebufferSize(window, &fbW, &fbH);
view->backend->setSize({static_cast<uint32_t>(fbW), static_cast<uint32_t>(fbH)});
view->backend->setFramebufferSize({static_cast<uint32_t>(fbW), static_cast<uint32_t>(fbH)});
#endif
}
@@ -1034,7 +1034,7 @@ void GLFWView::onFramebufferResize(GLFWwindow *window, int width, int height) {
MLN_TRACE_FUNC();
auto *view = reinterpret_cast<GLFWView *>(glfwGetWindowUserPointer(window));
view->backend->setSize({static_cast<uint32_t>(width), static_cast<uint32_t>(height)});
view->backend->setFramebufferSize({static_cast<uint32_t>(width), static_cast<uint32_t>(height)});
// This is only triggered when the framebuffer is resized, but not the
// window. It can happen when you move the window between screens with a

View File

@@ -156,12 +156,12 @@ GLFWVulkanBackend::GLFWVulkanBackend(GLFWwindow* window_, const bool capFrameRat
GLFWVulkanBackend::~GLFWVulkanBackend() {}
mln::Size GLFWVulkanBackend::getSize() const {
return size;
mln::Size GLFWVulkanBackend::getFramebufferSize() const {
return getSize();
}
void GLFWVulkanBackend::setSize(const mln::Size newSize) {
size = newSize;
void GLFWVulkanBackend::setFramebufferSize(const mln::Size newSize) {
setRenderableSize(newSize);
auto& contextImpl = static_cast<mln::vulkan::Context&>(*context);
contextImpl.requestSurfaceUpdate();

View File

@@ -23,8 +23,8 @@ public:
// GLFWRendererBackend implementation
public:
mln::gfx::RendererBackend& getRendererBackend() override { return *this; }
mln::Size getSize() const override;
void setSize(mln::Size) override;
mln::Size getFramebufferSize() const override;
void setFramebufferSize(mln::Size) override;
// mln::gfx::RendererBackend implementation
public:

View File

@@ -34,8 +34,8 @@ public:
// GLFWBackend implementation
public:
mln::gfx::RendererBackend& getRendererBackend() override;
mln::Size getSize() const override;
void setSize(mln::Size) override;
mln::Size getFramebufferSize() const override;
void setFramebufferSize(mln::Size) override;
// mln::gfx::RendererBackend implementation
public:
@@ -50,7 +50,6 @@ public:
// Override virtual methods from RendererBackend
void* getCurrentTextureView() override;
void* getDepthStencilView() override;
mln::Size getFramebufferSize() const override;
private:
class SpinLock {

View File

@@ -896,13 +896,13 @@ void GLFWWebGPUBackend::deactivate() {
// WebGPU doesn't need explicit context deactivation like OpenGL
}
mln::Size GLFWWebGPUBackend::getSize() const {
return size;
mln::Size GLFWWebGPUBackend::getFramebufferSize() const {
return getSize();
}
void GLFWWebGPUBackend::setSize(mln::Size newSize) {
void GLFWWebGPUBackend::setFramebufferSize(mln::Size newSize) {
// Update swap chain size if needed
if (size != newSize) {
if (getSize() != newSize) {
#ifdef __APPLE__
int width = 0;
int height = 0;
@@ -918,7 +918,7 @@ void GLFWWebGPUBackend::setSize(mln::Size newSize) {
}
}
#endif
size = newSize;
setRenderableSize(newSize);
surfaceNeedsReconfigure = true;
}
}
@@ -1066,10 +1066,6 @@ void* GLFWWebGPUBackend::getCurrentTextureView() {
#endif
}
mln::Size GLFWWebGPUBackend::getFramebufferSize() const {
return getSize();
}
void* GLFWWebGPUBackend::getDepthStencilView() {
std::lock_guard<SpinLock> guard(textureStateLock);
if (!depthStencilView) {
@@ -1126,7 +1122,8 @@ void GLFWWebGPUBackend::reconfigureSurface() {
return;
}
size = {static_cast<uint32_t>(std::max(width, 0)), static_cast<uint32_t>(std::max(height, 0))};
setRenderableSize(
{static_cast<uint32_t>(std::max(width, 0)), static_cast<uint32_t>(std::max(height, 0))});
// Configure surface
wgpu::SurfaceConfiguration config = {};

View File

@@ -15,5 +15,4 @@ public:
void activate() override;
void deactivate() override;
void setSize(mln::Size size_);
mln::Size getSize() const;
};

View File

@@ -19,15 +19,12 @@ public:
swapchain->setDevice(backend.getDevice().get());
}
void setBackendSize(mln::Size size_) {
size = size_;
void setDrawableSize(const mln::Size& drawableSize) {
swapchain->setDrawableSize(
{static_cast<CGFloat>(size.width), static_cast<CGFloat>(size.height)});
{static_cast<CGFloat>(drawableSize.width), static_cast<CGFloat>(drawableSize.height)});
buffersInvalid = true;
}
mln::Size getSize() const { return size; }
void bind() override {
surface = NS::TransferPtr(swapchain->nextDrawable());
auto texSize = mln::Size{static_cast<uint32_t>(swapchain->drawableSize().width),
@@ -107,7 +104,6 @@ private:
CAMetalLayerPtr swapchain;
gfx::Texture2DPtr depthTexture;
gfx::Texture2DPtr stencilTexture;
mln::Size size;
bool buffersInvalid = true;
};
@@ -129,10 +125,6 @@ void MetalBackend::activate() {}
void MetalBackend::deactivate() {}
void MetalBackend::setSize(mln::Size size_) {
size = size_;
getResource<mln::MetalRenderableResource>().setBackendSize(size_);
}
mln::Size MetalBackend::getSize() const {
return getResource<mln::MetalRenderableResource>().getSize();
setRenderableSize(size_);
getResource<mln::MetalRenderableResource>().setDrawableSize(size_);
}

View File

@@ -235,7 +235,7 @@ void MLNMapViewMetalImpl::layoutChanged() {
resource.mtlView.drawableSize = resource.lastAppliedDrawableSize = target;
}
size = {static_cast<uint32_t>(target.width), static_cast<uint32_t>(target.height)};
setRenderableSize({static_cast<uint32_t>(target.width), static_cast<uint32_t>(target.height)});
}
MLNBackendResource* MLNMapViewMetalImpl::getObject() {

View File

@@ -241,8 +241,8 @@ UIImage* MLNMapViewOpenGLImpl::snapshot() {
void MLNMapViewOpenGLImpl::layoutChanged() {
const auto scaleFactor = contentScaleFactor();
size = {static_cast<uint32_t>(mapView.bounds.size.width * scaleFactor),
static_cast<uint32_t>(mapView.bounds.size.height * scaleFactor)};
setRenderableSize({static_cast<uint32_t>(mapView.bounds.size.width * scaleFactor),
static_cast<uint32_t>(mapView.bounds.size.height * scaleFactor)});
}
EAGLContext* MLNMapViewOpenGLImpl::getEAGLContext() {

View File

@@ -94,8 +94,6 @@ public:
WGPUTextureView currentTextureView = nullptr;
bool needsPresent = false;
mln::Size framebufferSize;
MLNWebGPUView* webGPUView = nil;
bool presentsWithTransaction = false;
NSUInteger activationCount = 0;
@@ -316,8 +314,7 @@ void MLNMapViewWebGPUImpl::createView() {
uint32_t w = static_cast<uint32_t>(drawableSize.width);
uint32_t h = static_cast<uint32_t>(drawableSize.height);
if (w > 0 && h > 0) {
impl->framebufferSize = {w, h};
size = impl->framebufferSize;
setRenderableSize({w, h});
configureSurface(w, h);
}
}
@@ -497,7 +494,8 @@ void* MLNMapViewWebGPUImpl::getCurrentTextureView() {
if (status == WGPUSurfaceGetCurrentTextureStatus_Outdated ||
status == WGPUSurfaceGetCurrentTextureStatus_Lost) {
wgpuTextureRelease(surfaceTexture.texture);
configureSurface(impl->framebufferSize.width, impl->framebufferSize.height);
const auto framebufferSize = getSize();
configureSurface(framebufferSize.width, framebufferSize.height);
return nullptr;
}
@@ -557,7 +555,7 @@ void MLNMapViewWebGPUImpl::presentSurface() {
void* MLNMapViewWebGPUImpl::getDepthStencilView() { return impl->depthStencilView; }
mln::Size MLNMapViewWebGPUImpl::getFramebufferSize() const { return impl->framebufferSize; }
mln::Size MLNMapViewWebGPUImpl::getFramebufferSize() const { return getSize(); }
UIView* MLNMapViewWebGPUImpl::getView() { return impl->webGPUView; }
@@ -624,8 +622,7 @@ void MLNMapViewWebGPUImpl::layoutChanged() {
uint32_t w = static_cast<uint32_t>(drawableSize.width);
uint32_t h = static_cast<uint32_t>(drawableSize.height);
impl->framebufferSize = {w, h};
size = impl->framebufferSize;
setRenderableSize({w, h});
if (impl->currentTextureView) {
wgpuTextureViewRelease(impl->currentTextureView);
@@ -642,12 +639,12 @@ void MLNMapViewWebGPUImpl::layoutChanged() {
}
UIImage* MLNMapViewWebGPUImpl::snapshot() {
if (!impl->currentTexture || !impl->device || !impl->queue || impl->framebufferSize.width == 0 ||
impl->framebufferSize.height == 0) {
const auto framebufferSize = getSize();
if (!impl->currentTexture || !impl->device || !impl->queue || framebufferSize.isEmpty()) {
return nil;
}
const auto& fbSize = impl->framebufferSize;
const auto& fbSize = framebufferSize;
constexpr uint32_t bytesPerPixel = 4;
constexpr uint32_t bytesPerRowAlignment = 256u;
const uint32_t rowStride = fbSize.width * bytesPerPixel;

View File

@@ -139,7 +139,8 @@ MLNMapViewMetalImpl::MLNMapViewMetalImpl(MLNMapView* nativeView_)
}
void MLNMapViewMetalImpl::drawableSizeChanged(CGSize drawableSize) {
size = {static_cast<uint32_t>(drawableSize.width), static_cast<uint32_t>(drawableSize.height)};
setRenderableSize(
{static_cast<uint32_t>(drawableSize.width), static_cast<uint32_t>(drawableSize.height)});
}
MLNMapViewMetalImpl::~MLNMapViewMetalImpl() = default;