2019-02-08 20:36:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/sk_app/MetalWindowContext.h"
|
|
|
|
#include "tools/sk_app/mac/WindowContextFactory_mac.h"
|
2019-02-08 20:36:14 +00:00
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
void resize(int w, int h) override;
|
|
|
|
|
2019-02-08 20:36:14 +00:00
|
|
|
private:
|
|
|
|
NSView* fMainView;
|
|
|
|
|
|
|
|
typedef MetalWindowContext INHERITED;
|
|
|
|
};
|
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
MetalWindowContext_mac::MetalWindowContext_mac(const MacWindowInfo& info,
|
|
|
|
const DisplayParams& params)
|
2019-02-08 20:36:14 +00:00
|
|
|
: 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);
|
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
NSRect frameRect = [fMainView frame];
|
|
|
|
fMetalLayer.drawableSize = frameRect.size;
|
|
|
|
fMetalLayer.frame = frameRect;
|
2019-02-08 20:36:14 +00:00
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
fMainView.layer = fMetalLayer;
|
|
|
|
fMainView.wantsLayer = YES;
|
2019-02-08 20:36:14 +00:00
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
fWidth = frameRect.size.width;
|
|
|
|
fHeight = frameRect.size.height;
|
2019-02-08 20:36:14 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MetalWindowContext_mac::onDestroyContext() {
|
2019-05-16 14:31:56 +00:00
|
|
|
fMainView.layer = nil;
|
|
|
|
fMainView.wantsLayer = NO;
|
2019-02-08 20:36:14 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
void MetalWindowContext_mac::resize(int w, int h) {
|
|
|
|
fMetalLayer.drawableSize = fMainView.frame.size;
|
|
|
|
fMetalLayer.frame = fMainView.frame;
|
|
|
|
fWidth = w;
|
|
|
|
fHeight = h;
|
|
|
|
}
|
2019-02-08 20:36:14 +00:00
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
} // anonymous namespace
|
2019-02-08 20:36:14 +00:00
|
|
|
|
|
|
|
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
|