• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "tools/sk_app/MetalWindowContext.h"
9#include "tools/sk_app/mac/WindowContextFactory_mac.h"
10
11#import <Cocoa/Cocoa.h>
12#import <QuartzCore/CAConstraintLayoutManager.h>
13
14using sk_app::DisplayParams;
15using sk_app::window_context_factory::MacWindowInfo;
16using sk_app::MetalWindowContext;
17
18namespace {
19
20class MetalWindowContext_mac : public MetalWindowContext {
21public:
22    MetalWindowContext_mac(const MacWindowInfo&, const DisplayParams&);
23
24    ~MetalWindowContext_mac() override;
25
26    bool onInitializeContext() override;
27    void onDestroyContext() override;
28
29    void resize(int w, int h) override;
30
31private:
32    NSView*              fMainView;
33
34    typedef MetalWindowContext INHERITED;
35};
36
37MetalWindowContext_mac::MetalWindowContext_mac(const MacWindowInfo& info,
38                                               const DisplayParams& params)
39    : INHERITED(params)
40    , fMainView(info.fMainView) {
41
42    // any config code here (particularly for msaa)?
43
44    this->initializeContext();
45}
46
47MetalWindowContext_mac::~MetalWindowContext_mac() {
48    this->destroyContext();
49}
50
51bool MetalWindowContext_mac::onInitializeContext() {
52    SkASSERT(nil != fMainView);
53
54    NSRect frameRect = [fMainView frame];
55    fMetalLayer.drawableSize = frameRect.size;
56    fMetalLayer.frame = frameRect;
57
58    BOOL useVsync = fDisplayParams.fDisableVsync ? NO : YES;
59    fMetalLayer.displaySyncEnabled = useVsync;  // TODO: need solution for 10.12 or lower
60    fMetalLayer.layoutManager = [CAConstraintLayoutManager layoutManager];
61    fMetalLayer.autoresizingMask = kCALayerHeightSizable | kCALayerWidthSizable;
62    fMetalLayer.contentsGravity = kCAGravityTopLeft;
63
64    fMainView.layer = fMetalLayer;
65    fMainView.wantsLayer = YES;
66
67    fWidth = frameRect.size.width;
68    fHeight = frameRect.size.height;
69
70    return true;
71}
72
73void MetalWindowContext_mac::onDestroyContext() {
74    fMainView.layer = nil;
75    fMainView.wantsLayer = NO;
76}
77
78void MetalWindowContext_mac::resize(int w, int h) {
79    fMetalLayer.drawableSize = fMainView.frame.size;
80    fMetalLayer.frame = fMainView.frame;
81    fWidth = w;
82    fHeight = h;
83}
84
85}  // anonymous namespace
86
87namespace sk_app {
88namespace window_context_factory {
89
90std::unique_ptr<WindowContext> MakeMetalForMac(const MacWindowInfo& info,
91                                               const DisplayParams& params) {
92    std::unique_ptr<WindowContext> ctx(new MetalWindowContext_mac(info, params));
93    if (!ctx->isValid()) {
94        return nullptr;
95    }
96    return ctx;
97}
98
99}  // namespace window_context_factory
100}  // namespace sk_app
101