Skip to content

Commit

Permalink
Fixed the render viewport not updating when the metal view resizes
Browse files Browse the repository at this point in the history
When the phone is in portrait mode and the window is in landscape mode, the view changes orientation after layoutSubviews runs. In this case we need some way of notifying the application that the Metal view has changed.
  • Loading branch information
slouken committed Jul 24, 2024
1 parent 195c26a commit b98e1e9
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/SDL3/SDL_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ typedef enum SDL_EventType
SDL_EVENT_WINDOW_MOVED, /**< Window has been moved to data1, data2 */
SDL_EVENT_WINDOW_RESIZED, /**< Window has been resized to data1xdata2 */
SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED,/**< The pixel size of the window has changed to data1xdata2 */
SDL_EVENT_WINDOW_METAL_VIEW_RESIZED,/**< The pixel size of a Metal view associated with the window has changed */
SDL_EVENT_WINDOW_MINIMIZED, /**< Window has been minimized */
SDL_EVENT_WINDOW_MAXIMIZED, /**< Window has been maximized */
SDL_EVENT_WINDOW_RESTORED, /**< Window has been restored to normal size and position */
Expand Down
1 change: 1 addition & 0 deletions src/events/SDL_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ static void SDL_LogEvent(const SDL_Event *event)
SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MOVED);
SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_RESIZED);
SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED);
SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_METAL_VIEW_RESIZED);
SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_SAFE_AREA_CHANGED);
SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MINIMIZED);
SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MAXIMIZED);
Expand Down
3 changes: 2 additions & 1 deletion src/render/SDL_render.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,8 @@ static int SDLCALL SDL_RendererEventWatch(void *userdata, SDL_Event *event)
}

if (event->type == SDL_EVENT_WINDOW_RESIZED ||
event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) {
event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED ||
event->type == SDL_EVENT_WINDOW_METAL_VIEW_RESIZED) {
UpdateMainViewDimensions(renderer);
UpdateLogicalPresentation(renderer);
} else if (event->type == SDL_EVENT_WINDOW_HIDDEN) {
Expand Down
4 changes: 4 additions & 0 deletions src/test/SDL_test_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,10 @@ static void SDLTest_PrintEvent(const SDL_Event *event)
SDL_Log("SDL EVENT: Window %" SDL_PRIu32 " changed pixel size to %" SDL_PRIs32 "x%" SDL_PRIs32,
event->window.windowID, event->window.data1, event->window.data2);
break;
case SDL_EVENT_WINDOW_METAL_VIEW_RESIZED:
SDL_Log("SDL EVENT: Window %" SDL_PRIu32 " changed metal view size",
event->window.windowID);
break;
case SDL_EVENT_WINDOW_SAFE_AREA_CHANGED: {
SDL_Rect rect;

Expand Down
9 changes: 8 additions & 1 deletion src/video/uikit/SDL_uikitmetalview.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#if defined(SDL_VIDEO_DRIVER_UIKIT) && (defined(SDL_VIDEO_VULKAN) || defined(SDL_VIDEO_METAL))

#include "../SDL_sysvideo.h"
#include "../../events/SDL_windowevents_c.h"

#import "SDL_uikitwindow.h"
#import "SDL_uikitmetalview.h"
Expand Down Expand Up @@ -67,7 +68,13 @@ - (void)updateDrawableSize
CGSize size = self.bounds.size;
size.width *= self.layer.contentsScale;
size.height *= self.layer.contentsScale;
((CAMetalLayer *)self.layer).drawableSize = size;

CAMetalLayer *metallayer = ((CAMetalLayer *)self.layer);
if (metallayer.drawableSize.width != size.width ||
metallayer.drawableSize.height != size.height) {
metallayer.drawableSize = size;
SDL_SendWindowEvent([self getSDLWindow], SDL_EVENT_WINDOW_METAL_VIEW_RESIZED, 0, 0);
}
}

@end
Expand Down
1 change: 1 addition & 0 deletions src/video/uikit/SDL_uikitview.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- (instancetype)initWithFrame:(CGRect)frame;

- (void)setSDLWindow:(SDL_Window *)window;
- (SDL_Window *)getSDLWindow;

#if !defined(SDL_PLATFORM_TVOS) && defined(__IPHONE_13_4)
- (UIPointerRegion *)pointerInteraction:(UIPointerInteraction *)interaction regionForRequest:(UIPointerRegionRequest *)request defaultRegion:(UIPointerRegion *)defaultRegion API_AVAILABLE(ios(13.4));
Expand Down
5 changes: 5 additions & 0 deletions src/video/uikit/SDL_uikitview.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ - (void)setSDLWindow:(SDL_Window *)window
sdlwindow = window;
}

- (SDL_Window *)getSDLWindow
{
return sdlwindow;
}

#if !defined(SDL_PLATFORM_TVOS) && defined(__IPHONE_13_4)
- (UIPointerRegion *)pointerInteraction:(UIPointerInteraction *)interaction regionForRequest:(UIPointerRegionRequest *)request defaultRegion:(UIPointerRegion *)defaultRegion API_AVAILABLE(ios(13.4))
{
Expand Down

0 comments on commit b98e1e9

Please sign in to comment.