Code style cleanup in the Cocoa and UIKit vulkan files.

This commit is contained in:
Alex Szpakowski 2017-08-31 21:26:13 -03:00
parent cfd7a7fac6
commit b959be2569
4 changed files with 72 additions and 68 deletions

View File

@ -46,12 +46,18 @@
} }
/* Indicate the view wants to draw using a backing layer instead of drawRect. */ /* Indicate the view wants to draw using a backing layer instead of drawRect. */
-(BOOL) wantsUpdateLayer { return YES; } -(BOOL) wantsUpdateLayer
{
return YES;
}
/* When the wantsLayer property is set to YES, this method will be invoked to /* When the wantsLayer property is set to YES, this method will be invoked to
* return a layer instance. * return a layer instance.
*/ */
-(CALayer*) makeBackingLayer { return [self.class.layerClass layer]; } -(CALayer*) makeBackingLayer
{
return [self.class.layerClass layer];
}
- (instancetype)initWithFrame:(NSRect)frame - (instancetype)initWithFrame:(NSRect)frame
useHighDPI:(bool)useHighDPI useHighDPI:(bool)useHighDPI
@ -77,12 +83,11 @@
- (void)updateDrawableSize - (void)updateDrawableSize
{ {
NSRect bounds = [self bounds];
if (_useHighDPI) { if (_useHighDPI) {
NSSize size = [self convertRectToBacking:[self bounds]].size; bounds = [self convertRectToBacking:bounds];
/* Isn't there a better way to convert from NSSize to CGSize? */
CGSize cgsize = *(CGSize*)&size;
((CAMetalLayer *) self.layer).drawableSize = cgsize;
} }
((CAMetalLayer *) self.layer).drawableSize = NSSizeToCGSize(bounds.size);
} }
@end @end
@ -111,11 +116,13 @@ Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
if (metalview) { if (metalview) {
CAMetalLayer *layer = (CAMetalLayer*)metalview.layer; CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
assert(layer != NULL); assert(layer != NULL);
if (w) if (w) {
*w = layer.drawableSize.width; *w = layer.drawableSize.width;
if (h) }
if (h) {
*h = layer.drawableSize.height; *h = layer.drawableSize.height;
} }
}
} }
#endif /* SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_COCOA */ #endif /* SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_COCOA */

View File

@ -1,6 +1,6 @@
/* /*
Simple DirectMedia Layer Simple DirectMedia Layer
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org> Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages

View File

@ -1,6 +1,6 @@
/* /*
Simple DirectMedia Layer Simple DirectMedia Layer
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org> Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -50,17 +50,17 @@ int Cocoa_Vulkan_LoadLibrary(_THIS, const char *path)
SDL_bool hasSurfaceExtension = SDL_FALSE; SDL_bool hasSurfaceExtension = SDL_FALSE;
SDL_bool hasMacOSSurfaceExtension = SDL_FALSE; SDL_bool hasMacOSSurfaceExtension = SDL_FALSE;
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL; PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
if(_this->vulkan_config.loader_handle)
{ if (_this->vulkan_config.loader_handle) {
SDL_SetError("MoltenVK/Vulkan already loaded"); SDL_SetError("MoltenVK/Vulkan already loaded");
return -1; return -1;
} }
/* Load the Vulkan loader library */ /* Load the Vulkan loader library */
if(!path) if (!path) {
path = SDL_getenv("SDL_VULKAN_LIBRARY"); path = SDL_getenv("SDL_VULKAN_LIBRARY");
if(!path) }
{ if (!path) {
/* MoltenVK framework, currently, v0.17.0, has a static library and is /* MoltenVK framework, currently, v0.17.0, has a static library and is
* the recommended way to use the package. There is likely no object to * the recommended way to use the package. There is likely no object to
* load. */ * load. */
@ -69,28 +69,25 @@ int Cocoa_Vulkan_LoadLibrary(_THIS, const char *path)
"vkGetInstanceProcAddr"); "vkGetInstanceProcAddr");
} }
if(vkGetInstanceProcAddr) if (vkGetInstanceProcAddr) {
{
_this->vulkan_config.loader_handle = DEFAULT_HANDLE; _this->vulkan_config.loader_handle = DEFAULT_HANDLE;
} } else {
else if (!path) {
{
if (!path)
{
/* Look for the .dylib packaged with the application instead. */ /* Look for the .dylib packaged with the application instead. */
path = DEFAULT_MOLTENVK; path = DEFAULT_MOLTENVK;
} }
_this->vulkan_config.loader_handle = SDL_LoadObject(path); _this->vulkan_config.loader_handle = SDL_LoadObject(path);
if(!_this->vulkan_config.loader_handle) if (!_this->vulkan_config.loader_handle) {
return -1; return -1;
}
SDL_strlcpy(_this->vulkan_config.loader_path, path, SDL_strlcpy(_this->vulkan_config.loader_path, path,
SDL_arraysize(_this->vulkan_config.loader_path)); SDL_arraysize(_this->vulkan_config.loader_path));
vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction( vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction(
_this->vulkan_config.loader_handle, "vkGetInstanceProcAddr"); _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
} }
if(!vkGetInstanceProcAddr)
{ if (!vkGetInstanceProcAddr) {
SDL_SetError("Failed to find %s in either executable or %s: %s", SDL_SetError("Failed to find %s in either executable or %s: %s",
"vkGetInstanceProcAddr", "vkGetInstanceProcAddr",
DEFAULT_MOLTENVK, DEFAULT_MOLTENVK,
@ -102,30 +99,29 @@ int Cocoa_Vulkan_LoadLibrary(_THIS, const char *path)
_this->vulkan_config.vkEnumerateInstanceExtensionProperties = _this->vulkan_config.vkEnumerateInstanceExtensionProperties =
(void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)( (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)(
VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties"); VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
if(!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) if (!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) {
goto fail; goto fail;
}
extensions = SDL_Vulkan_CreateInstanceExtensionsList( extensions = SDL_Vulkan_CreateInstanceExtensionsList(
(PFN_vkEnumerateInstanceExtensionProperties) (PFN_vkEnumerateInstanceExtensionProperties)
_this->vulkan_config.vkEnumerateInstanceExtensionProperties, _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
&extensionCount); &extensionCount);
if(!extensions) if (!extensions) {
goto fail; goto fail;
for(Uint32 i = 0; i < extensionCount; i++) }
{ for (Uint32 i = 0; i < extensionCount; i++) {
if(SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
hasSurfaceExtension = SDL_TRUE; hasSurfaceExtension = SDL_TRUE;
else if(SDL_strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) } else if (SDL_strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
hasMacOSSurfaceExtension = SDL_TRUE; hasMacOSSurfaceExtension = SDL_TRUE;
} }
}
SDL_free(extensions); SDL_free(extensions);
if(!hasSurfaceExtension) if (!hasSurfaceExtension) {
{
SDL_SetError("Installed MoltenVK/Vulkan doesn't implement the " SDL_SetError("Installed MoltenVK/Vulkan doesn't implement the "
VK_KHR_SURFACE_EXTENSION_NAME " extension"); VK_KHR_SURFACE_EXTENSION_NAME " extension");
goto fail; goto fail;
} } else if (!hasMacOSSurfaceExtension) {
else if(!hasMacOSSurfaceExtension)
{
SDL_SetError("Installed MoltenVK/Vulkan doesn't implement the " SDL_SetError("Installed MoltenVK/Vulkan doesn't implement the "
VK_MVK_MACOS_SURFACE_EXTENSION_NAME "extension"); VK_MVK_MACOS_SURFACE_EXTENSION_NAME "extension");
goto fail; goto fail;
@ -140,10 +136,10 @@ fail:
void Cocoa_Vulkan_UnloadLibrary(_THIS) void Cocoa_Vulkan_UnloadLibrary(_THIS)
{ {
if(_this->vulkan_config.loader_handle) if (_this->vulkan_config.loader_handle) {
{ if (_this->vulkan_config.loader_handle != DEFAULT_HANDLE) {
if (_this->vulkan_config.loader_handle != DEFAULT_HANDLE)
SDL_UnloadObject(_this->vulkan_config.loader_handle); SDL_UnloadObject(_this->vulkan_config.loader_handle);
}
_this->vulkan_config.loader_handle = NULL; _this->vulkan_config.loader_handle = NULL;
} }
} }
@ -156,8 +152,7 @@ SDL_bool Cocoa_Vulkan_GetInstanceExtensions(_THIS,
static const char *const extensionsForCocoa[] = { static const char *const extensionsForCocoa[] = {
VK_KHR_SURFACE_EXTENSION_NAME, VK_MVK_MACOS_SURFACE_EXTENSION_NAME VK_KHR_SURFACE_EXTENSION_NAME, VK_MVK_MACOS_SURFACE_EXTENSION_NAME
}; };
if(!_this->vulkan_config.loader_handle) if (!_this->vulkan_config.loader_handle) {
{
SDL_SetError("Vulkan is not loaded"); SDL_SetError("Vulkan is not loaded");
return SDL_FALSE; return SDL_FALSE;
} }
@ -180,14 +175,12 @@ SDL_bool Cocoa_Vulkan_CreateSurface(_THIS,
VkMacOSSurfaceCreateInfoMVK createInfo = {}; VkMacOSSurfaceCreateInfoMVK createInfo = {};
VkResult result; VkResult result;
if(!_this->vulkan_config.loader_handle) if (!_this->vulkan_config.loader_handle) {
{
SDL_SetError("Vulkan is not loaded"); SDL_SetError("Vulkan is not loaded");
return SDL_FALSE; return SDL_FALSE;
} }
if(!vkCreateMacOSSurfaceMVK) if (!vkCreateMacOSSurfaceMVK) {
{
SDL_SetError(VK_MVK_MACOS_SURFACE_EXTENSION_NAME SDL_SetError(VK_MVK_MACOS_SURFACE_EXTENSION_NAME
" extension is not enabled in the Vulkan instance."); " extension is not enabled in the Vulkan instance.");
return SDL_FALSE; return SDL_FALSE;
@ -198,8 +191,7 @@ SDL_bool Cocoa_Vulkan_CreateSurface(_THIS,
createInfo.pView = Cocoa_Mtl_AddMetalView(window); createInfo.pView = Cocoa_Mtl_AddMetalView(window);
result = vkCreateMacOSSurfaceMVK(instance, &createInfo, result = vkCreateMacOSSurfaceMVK(instance, &createInfo,
NULL, surface); NULL, surface);
if(result != VK_SUCCESS) if (result != VK_SUCCESS) {
{
SDL_SetError("vkCreateMacOSSurfaceMVK failed: %s", SDL_SetError("vkCreateMacOSSurfaceMVK failed: %s",
SDL_Vulkan_GetResultString(result)); SDL_Vulkan_GetResultString(result));
return SDL_FALSE; return SDL_FALSE;

View File

@ -129,18 +129,23 @@ UIKit_Mtl_AddMetalView(SDL_Window* window)
void void
UIKit_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h) UIKit_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
{ {
@autoreleasepool {
SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata; SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
SDL_uikitview *view = (SDL_uikitview*)data.uiwindow.rootViewController.view; SDL_uikitview *view = (SDL_uikitview*)data.uiwindow.rootViewController.view;
SDL_uikitmetalview* metalview = [view viewWithTag:METALVIEW_TAG]; SDL_uikitmetalview* metalview = [view viewWithTag:METALVIEW_TAG];
if (metalview) { if (metalview) {
CAMetalLayer *layer = (CAMetalLayer*)metalview.layer; CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
assert(layer != NULL); assert(layer != NULL);
if (w) if (w) {
*w = layer.drawableSize.width; *w = layer.drawableSize.width;
if (h) }
if (h) {
*h = layer.drawableSize.height; *h = layer.drawableSize.height;
} else }
} else {
SDL_GetWindowSize(window, w, h); SDL_GetWindowSize(window, w, h);
}
}
} }
#endif #endif