skia2/tools/sk_app/mac/VulkanWindowContext_mac.mm
Mike Klein c0bd9f9fe5 rewrite includes to not need so much -Ifoo
Current strategy: everything from the top

Things to look at first are the manual changes:

   - added tools/rewrite_includes.py
   - removed -Idirectives from BUILD.gn
   - various compile.sh simplifications
   - tweak tools/embed_resources.py
   - update gn/find_headers.py to write paths from the top
   - update gn/gn_to_bp.py SkUserConfig.h layout
     so that #include "include/config/SkUserConfig.h" always
     gets the header we want.

No-Presubmit: true
Change-Id: I73a4b181654e0e38d229bc456c0d0854bae3363e
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/209706
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Hal Canary <halcanary@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
2019-04-24 16:27:11 +00:00

155 lines
5.1 KiB
Plaintext

/*
* Copyright 2019 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/gpu/vk/GrVkVulkan.h"
#include "src/gpu/vk/GrVkInterface.h"
#include "src/gpu/vk/GrVkUtil.h"
#include "tools/gpu/vk/VkTestUtils.h"
#include "include/third_party/vulkan/vulkan/vulkan_macos.h"
#include "tools/sk_app/VulkanWindowContext.h"
#include "tools/sk_app/mac/WindowContextFactory_mac.h"
#import <MetalKit/MetalKit.h>
namespace sk_app {
class VulkanWindowContext_mac : public VulkanWindowContext {
public:
VulkanWindowContext_mac(MTKView* mtkView,
const DisplayParams& params,
CreateVkSurfaceFn createVkSurface,
CanPresentFn canPresent,
PFN_vkGetInstanceProcAddr instProc,
PFN_vkGetDeviceProcAddr devProc);
~VulkanWindowContext_mac() override;
void resize(int w, int h) override;
private:
MTKView* fMTKView;
typedef VulkanWindowContext INHERITED;
};
VulkanWindowContext_mac::VulkanWindowContext_mac(MTKView* mtkView,
const DisplayParams& params,
CreateVkSurfaceFn createVkSurface,
CanPresentFn canPresent,
PFN_vkGetInstanceProcAddr instProc,
PFN_vkGetDeviceProcAddr devProc)
: INHERITED(params, createVkSurface, canPresent, instProc, devProc)
, fMTKView(mtkView) {
// any config code here (particularly for msaa)?
}
VulkanWindowContext_mac::~VulkanWindowContext_mac() {
[fMTKView removeFromSuperview];
[fMTKView release];
fMTKView = nil;
}
void VulkanWindowContext_mac::resize(int w, int h) {
CGSize newSize;
newSize.width = w;
newSize.height = h;
fMTKView.drawableSize = newSize;
this->INHERITED::resize(w, h);
}
namespace window_context_factory {
WindowContext* NewVulkanForMac(const MacWindowInfo& info, const DisplayParams& displayParams) {
PFN_vkGetInstanceProcAddr instProc;
PFN_vkGetDeviceProcAddr devProc;
if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) {
return nullptr;
}
// Create mtkview
NSRect rect = info.fMainView.frame;
MTKView* mtkView = [[MTKView alloc] initWithFrame:rect device:nil];
if (nil == mtkView) {
return nullptr;
}
mtkView.autoResizeDrawable = NO;
mtkView.colorPixelFormat = MTLPixelFormatBGRA8Unorm;
mtkView.drawableSize = rect.size;
// if (fDisplayParams.fMSAASampleCount > 1) {
// if (![fDevice supportsTextureSampleCount:fDisplayParams.fMSAASampleCount]) {
// return nullptr;
// }
// }
// mtkView.sampleCount = fDisplayParams.fMSAASampleCount;
// attach Metal view to main view
[mtkView setTranslatesAutoresizingMaskIntoConstraints:NO];
[info.fMainView addSubview:mtkView];
NSDictionary *views = NSDictionaryOfVariableBindings(mtkView);
[info.fMainView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mtkView]|"
options:0
metrics:nil
views:views]];
[info.fMainView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mtkView]|"
options:0
metrics:nil
views:views]];
auto createVkSurface = [mtkView, instProc](VkInstance instance) -> VkSurfaceKHR {
static PFN_vkCreateMacOSSurfaceMVK createMacOSSurfaceMVK = nullptr;
if (!createMacOSSurfaceMVK) {
createMacOSSurfaceMVK =
(PFN_vkCreateMacOSSurfaceMVK) instProc(instance, "vkCreateMacOSSurfaceMVK");
}
VkSurfaceKHR surface;
VkMacOSSurfaceCreateInfoMVK surfaceCreateInfo;
memset(&surfaceCreateInfo, 0, sizeof(VkMacOSSurfaceCreateInfoMVK));
surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
surfaceCreateInfo.pNext = nullptr;
surfaceCreateInfo.flags = 0;
surfaceCreateInfo.pView = (__bridge void*)mtkView;
VkResult res = createMacOSSurfaceMVK(instance, &surfaceCreateInfo, nullptr, &surface);
if (VK_SUCCESS != res) {
return VK_NULL_HANDLE;
}
return surface;
};
auto canPresent = [](VkInstance instance, VkPhysicalDevice physDev, uint32_t queueFamilyIndex) {
return true;
};
WindowContext* context = new VulkanWindowContext_mac(mtkView, displayParams, createVkSurface,
canPresent, instProc, devProc);
if (!context->isValid()) {
delete context;
[mtkView removeFromSuperview];
[mtkView release];
return nullptr;
}
return context;
}
} // namespace VulkanWindowContextFactory
} // namespace sk_app