skia2/tools/sk_app/mac/MetalWindowContext_mac.mm
Jim Van Verth e94af49a3f Fix a couple of Mac windowing issues.
* Set correct backing size for Metal and Vulkan
* Don't capture mouse events when resizing

Bug: skia:8737
Change-Id: I7798ca95aebe0c11b75b23a55a94514550317b32
Reviewed-on: https://skia-review.googlesource.com/c/196423
Auto-Submit: Jim Van Verth <jvanverth@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2019-02-28 19:24:52 +00:00

122 lines
3.2 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 "../MetalWindowContext.h"
#include "WindowContextFactory_mac.h"
#import <MetalKit/MetalKit.h>
#include <Cocoa/Cocoa.h>
using sk_app::DisplayParams;
using sk_app::window_context_factory::MacWindowInfo;
using sk_app::MetalWindowContext;
namespace {
class MetalWindowContext_mac : public MetalWindowContext {
public:
MetalWindowContext_mac(const MacWindowInfo&, const DisplayParams&);
~MetalWindowContext_mac() override;
bool onInitializeContext() override;
void onDestroyContext() override;
private:
NSView* fMainView;
typedef MetalWindowContext INHERITED;
};
MetalWindowContext_mac::MetalWindowContext_mac(const MacWindowInfo& info, const DisplayParams& params)
: INHERITED(params)
, fMainView(info.fMainView) {
// any config code here (particularly for msaa)?
this->initializeContext();
}
MetalWindowContext_mac::~MetalWindowContext_mac() {
this->destroyContext();
}
bool MetalWindowContext_mac::onInitializeContext() {
SkASSERT(nil != fMainView);
// create mtkview
NSRect rect = fMainView.bounds;
fMTKView = [[MTKView alloc] initWithFrame:rect device:fDevice];
if (nil == fMTKView) {
return false;
}
fMTKView.autoResizeDrawable = NO;
fMTKView.colorPixelFormat = MTLPixelFormatBGRA8Unorm;
fMTKView.drawableSize = rect.size;
if (fDisplayParams.fMSAASampleCount > 1) {
if (![fDevice supportsTextureSampleCount:fDisplayParams.fMSAASampleCount]) {
return false;
}
}
fMTKView.sampleCount = fDisplayParams.fMSAASampleCount;
// attach Metal view to main view
[fMTKView setTranslatesAutoresizingMaskIntoConstraints:NO];
[fMainView addSubview:fMTKView];
NSDictionary *views = NSDictionaryOfVariableBindings(fMTKView);
[fMainView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[fMTKView]|"
options:0
metrics:nil
views:views]];
[fMainView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[fMTKView]|"
options:0
metrics:nil
views:views]];
fSampleCount = [fMTKView sampleCount];
fStencilBits = 8;
CGSize backingSize = [fMTKView drawableSize];
fWidth = backingSize.width;
fHeight = backingSize.height;
return true;
}
void MetalWindowContext_mac::onDestroyContext() {
[fMTKView removeFromSuperview];
[fMTKView release];
fMTKView = nil;
}
} // anonymous namespace
namespace sk_app {
namespace window_context_factory {
WindowContext* NewMetalForMac(const MacWindowInfo& info, const DisplayParams& params) {
WindowContext* ctx = new MetalWindowContext_mac(info, params);
if (!ctx->isValid()) {
delete ctx;
return nullptr;
}
return ctx;
}
} // namespace window_context_factory
} // namespace sk_app