Repository: godotengine/godot (language: cpp)

Issue:
TextureRect with ViewportTexture from SubViewport in SubViewportContainer crashes when switching back to scene tab.
### Tested versions

- Reproducible in v4.4-beta1.mono.official
- Not reproducible in v4.3.stable.mono.official

### System information

Godot v4.4.beta1.mono - Ubuntu 24.04.1 LTS 24.04 on X11 - X11 display driver, Multi-window, 2 monitors - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4070 SUPER (nvidia; 550.120) - Intel(R) Core(TM) i7-14700KF (28 threads)

### Issue description

When a scene contains a `TextureRect`  inside of `CanvasLayer`, which has a `ViewportTexture` pointing to a `SubViewport` in a `SubViewportContainer`, the editor will crash when switching back to that scene tab.

This crash does not occur when `TextureRect` is not in a `CanvasLayer` or when `SubViewport` is not in a `SubViewportContainer`.

A sample backtrace can be found below:

```
================================================================
handle_crash: Program crashed with signal 11
Engine version: Godot Engine v4.4.beta1.mono.official (d33da79d3f8fe84be2521d25b9ba8e440cf25a88)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] /usr/lib/dotnet8/shared/Microsoft.NETCore.App/8.0.12/libcoreclr.so(+0x6068bc) [0x7ec4948068bc] (??:0)
[2] /usr/lib/dotnet8/shared/Microsoft.NETCore.App/8.0.12/libcoreclr.so(+0x605ef5) [0x7ec494805ef5] (??:0)
[3] /lib/x86_64-linux-gnu/libc.so.6(+0x45320) [0x7ec4cbe45320] (??:0)
[4] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x2945d4a] (??:0)
[5] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x2943eb9] (??:0)
[6] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x294c182] (??:0)
[7] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x2a563dc] (??:0)
[8] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x2ac99e3] (??:0)
[9] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x21180c1] (??:0)
[10] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x477e0c8] (??:0)
[11] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x28a284f] (??:0)
[12] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x21180b7] (??:0)
[13] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x477e0c8] (??:0)
[14] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x29336f6] (??:0)
[15] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x2933975] (??:0)
[16] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x2933975] (??:0)
[17] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x2934792] (??:0)
[18] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x2934a10] (??:0)
[19] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x1950204] (??:0)
[20] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x484291a] (??:0)
[21] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x1cd37a6] (??:0)
[22] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x484291a] (??:0)
[23] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x2cd0e0c] (??:0)
[24] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x2cd1bbe] (??:0)
[25] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x294e07f] (??:0)
[26] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x29c4915] (??:0)
[27] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x29c5ab6] (??:0)
[28] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x29dc19c] (??:0)
[29] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x4f5510b] (??:0)
[30] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x4cf5ed] (??:0)
[31] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x44a0843] (??:0)
[32] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x44a2c2f] (??:0)
[33] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x4d8a83] (??:0)
[34] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x41e313] (??:0)
[35] /lib/x86_64-linux-gnu/libc.so.6(+0x2a1ca) [0x7ec4cbe2a1ca] (??:0)
[36] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x8b) [0x7ec4cbe2a28b] (??:0)
[37] /opt/Godot_v4.4-beta1_mono_linux_x86_64/Godot_v4.4-beta1_mono_linux.x86_64() [0x443faa] (??:0)
-- END OF BACKTRACE --
================================================================
```

### Steps to reproduce

- Create a scene with the setup below, where the `ViewportTexture` in the `TextureRect` is pointing at `SubViewport`.
- Open a new tab in the editor
- Switch back to the scene containing the `TextureRect`

![Image](https://github.com/user-attachments/assets/aa3adc29-9eda-498d-8762-c8be95f0e221)

### Minimal reproduction project (MRP)

[crashrepro.zip](https://github.com/user-attachments/files/18454471/crashrepro.zip)


Discussion / hints:
Can confirm on [4ce466d7f], with the following backtrace:
```
================================================================
CrashHandlerException: Program crashed
Engine version: Godot Engine v4.4.beta.custom_build (4ce466d7fa79e351d4295d5bb47e3266089c3a59)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[0] SubViewport::get_screen_transform_internal (godot\scene\main\viewport.cpp:5252)
[1] Viewport::get_screen_transform (godot\scene\main\viewport.cpp:3939)
[2] ViewportTexture::get_size (godot\scene\main\viewport.cpp:145)
[3] TextureRect::get_minimum_size (godot\scene\gui\texture_rect.cpp:111)
[4] Control::_update_minimum_size_cache (godot\scene\gui\control.cpp:1676)
[5] Control::get_combined_minimum_size (godot\scene\gui\control.cpp:1686)
[6] Control::_size_changed (godot\scene\gui\control.cpp:1704)
[7] Control::_notification (godot\scene\gui\control.cpp:3386)
[8] Control::_notificationv (godot\scene\gui\control.h:47)
[9] TextureRect::_notificationv (godot\scene\gui\texture_rect.h:37)
[10] Object::notification (godot\core\object\object.cpp:911)
[11] CanvasItem::_notification (godot\scene\main\canvas_item.cpp:327)
[12] CanvasItem::_notificationv (godot\scene\main\canvas_item.h:44)
[13] Control::_notificationv (godot\scene\gui\control.h:47)
[14] TextureRect::_notificationv (godot\scene\gui\texture_rect.h:37)
[15] Object::notification (godot\core\object\object.cpp:911)
[16] Node::_propagate_enter_tree (godot\scene\main\node.cpp:310)
[17] Node::_propagate_enter_tree (godot\scene\main\node.cpp:327)
[18] Node::_propagate_enter_tree (godot\scene\main\node.cpp:327)
[19] Node::_set_tree (godot\scene\main\node.cpp:3287)
[20] Node::_add_child_nocheck (godot\scene\main\node.cpp:1636)
[21] Node::add_child (godot\scene\main\node.cpp:1668)
[22] EditorNode::_set_current_scene_nocheck (godot\editor\editor_node.cpp:3908)
[23] EditorNode::_set_current_scene (godot\editor\editor_node.cpp:3873)
[24] call_with_variant_args_helper<EditorNode,int,0> (godot\core\variant\binder_common.h:315)
[25] call_with_variant_args<EditorNode,int> (godot\core\variant\binder_common.h:429)
[26] CallableCustomMethodPointer<EditorNode,void,int>::call (godot\core\object\callable_method_pointer.h:105)
[27] Callable::callp (godot\core\variant\callable.cpp:57)
[28] Object::emit_signalp (godot\core\object\object.cpp:1237)
[29] Node::emit_signalp (godot\scene\main\node.cpp:4022)
[30] Object::emit_signal<int> (godot\core\object\object.h:933)
[31] EditorSceneTabs::_scene_tab_changed (godot\editor\gui\editor_scene_tabs.cpp:76)
[32] call_with_variant_args_helper<EditorSceneTabs,int,0> (godot\core\variant\binder_common.h:315)
[33] call_with_variant_args<EditorSceneTabs,int> (godot\core\variant\binder_common.h:429)
[34] CallableCustomMethodPointer<EditorSceneTabs,void,int>::call (godot\core\object\callable_method_pointer.h:105)
[35] Callable::callp (godot\core\variant\callable.cpp:57)
[36] Object::emit_signalp (godot\core\object\object.cpp:1237)
[37] Node::emit_signalp (godot\scene\main\node.cpp:4022)
[38] Object::emit_signal<int> (godot\core\object\object.h:933)
[39] TabBar::set_current_tab (godot\scene\gui\tab_bar.cpp:702)
[40] TabBar::gui_input (godot\scene\gui\tab_bar.cpp:283)
[41] Control::_call_gui_input (godot\scene\gui\control.cpp:1825)
[42] Viewport::_gui_call_input (godot\scene\main\viewport.cpp:1615)
[43] Viewport::_gui_input_event (godot\scene\main\viewport.cpp:1851)
[44] Viewport::push_input (godot\scene\main\viewport.cpp:3273)
[45] Window::_window_input (godot\scene\main\window.cpp:1694)
[46] call_with_variant_args_helper<Window,Ref<InputEvent> const &,0> (godot\core\variant\binder_common.h:315)
[47] call_with_variant_args<Window,Ref<InputEvent> const &> (godot\core\variant\binder_common.h:429)
[48] CallableCustomMethodPointer<Window,void,Ref<InputEvent> const &>::call (godot\core\object\callable_method_pointer.h:105)
[49] Callable::callp (godot\core\variant\callable.cpp:57)
[50] Callable::call<Ref<InputEvent> > (godot\core\variant\variant.h:961)
[51] DisplayServerWindows::_dispatch_input_event (godot\platform\windows\display_server_windows.cpp:4189)
[52] DisplayServerWindows::_dispatch_input_events (godot\platform\windows\display_server_windows.cpp:4159)
[53] Input::_parse_input_event_impl (godot\core\input\input.cpp:922)
[54] Input::flush_buffered_events (godot\core\input\input.cpp:1203)
[55] DisplayServerWindows::process_events (godot\platform\windows\display_server_windows.cpp:3570)
[56] OS_Windows::run (godot\platform\windows\os_windows.cpp:2061)
[57] widechar_main (godot\platform\windows\godot_windows.cpp:180)
[58] _main (godot\platform\windows\godot_windows.cpp:206)
[59] main (godot\platform\windows\godot_windows.cpp:220)
[60] WinMain (godot\platform\windows\godot_windows.cpp:234)
[61] __scrt_common_main_seh (D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288)
[62] <couldn't map PC to fn name>
-- END OF BACKTRACE --
================================================================
```

Will bisect
Bisected to [0bc2c269cbd6dcec44d1d780d21bea9cbcd2e436] which is:
* https://github.com/godotengine/godot/pull/97745

CC @scgm0 
> Bisected to [[0bc2c26](https://github.com/godotengine/godot/commit/0bc2c269cbd6dcec44d1d780d21bea9cbcd2e436)] which is:
> 
>     * [Improve blurred content of embedded windows #97745](https://github.com/godotengine/godot/pull/97745)
> 
> 
> CC [@scgm0](https://github.com/scgm0)

I'll check it out, please give me some time as my computer doesn't compile godot very fast. (I'm only replying now also because I'm just compiling godot)


Fix patch:
diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp
index e9dfbd6189c1..76b2502bade3 100644
--- a/scene/main/viewport.cpp
+++ b/scene/main/viewport.cpp
@@ -126,7 +126,10 @@ int ViewportTexture::get_width() const {
 		_err_print_viewport_not_set();
 		return 0;
 	}
-	return get_size().width;
+	if (vp->is_sub_viewport()) {
+		return vp->size.width;
+	}
+	return vp->size.width * vp->get_stretch_transform().get_scale().width;
 }
 
 int ViewportTexture::get_height() const {
@@ -134,7 +137,10 @@ int ViewportTexture::get_height() const {
 		_err_print_viewport_not_set();
 		return 0;
 	}
-	return get_size().height;
+	if (vp->is_sub_viewport()) {
+		return vp->size.height;
+	}
+	return vp->size.height * vp->get_stretch_transform().get_scale().height;
 }
 
 Size2 ViewportTexture::get_size() const {
@@ -142,8 +148,11 @@ Size2 ViewportTexture::get_size() const {
 		_err_print_viewport_not_set();
 		return Size2();
 	}
-	float scale = MIN(vp->get_screen_transform().get_scale().width, vp->get_screen_transform().get_scale().height);
-	return Size2(vp->size.width * scale, vp->size.height * scale).ceil();
+	if (vp->is_sub_viewport()) {
+		return vp->size;
+	}
+	Size2 scale = vp->get_stretch_transform().get_scale();
+	return Size2(vp->size.width * scale.width, vp->size.height * scale.height).ceil();
 }
 
 RID ViewportTexture::get_rid() const {


Test patch: