Debian Codegraph @

tools/window/mac/GaneshGLWindowContext_mac.mm

raw
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/window/mac/GaneshGLWindowContext_mac.h"
9
10 #include "include/gpu/ganesh/GrDirectContext.h"
11 #include "include/gpu/ganesh/gl/GrGLInterface.h"
12 #include "include/gpu/ganesh/gl/mac/GrGLMakeMacInterface.h"
13 #include "src/gpu/ganesh/gl/GrGLUtil.h"
14 #include "tools/window/GLWindowContext.h"
15 #include "tools/window/mac/MacWindowGLUtils.h"
16 #include "tools/window/mac/MacWindowInfo.h"
17
18 #include <Cocoa/Cocoa.h>
19 #include <OpenGL/gl.h>
20
21 using skwindow::DisplayParams;
22 using skwindow::MacWindowInfo;
23 using skwindow::internal::GLWindowContext;
24
25 // All of NSOpenGL is deprecated.
26 #pragma clang diagnostic push
27 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
28
29 namespace {
30
31 class GLWindowContext_mac : public GLWindowContext {
32 public:
33 GLWindowContext_mac(const MacWindowInfo&, std::unique_ptr<const DisplayParams>);
34
35 ~GLWindowContext_mac() override;
36
37 sk_sp<const GrGLInterface> onInitializeContext() override;
38 void onDestroyContext() override;
39
40 void resize(int w, int h) override;
41
42 private:
43 void teardownContext();
44 void onSwapBuffers() override;
45
46 NSView* fMainView;
47 NSOpenGLContext* fGLContext;
48 NSOpenGLPixelFormat* fPixelFormat;
49 };
50
51 GLWindowContext_mac::GLWindowContext_mac(const MacWindowInfo& info,
52 std::unique_ptr<const DisplayParams> params)
53 : GLWindowContext(std::move(params)), fMainView(info.fMainView), fGLContext(nil) {
54 // any config code here (particularly for msaa)?
55
56 this->initializeContext();
57 }
58
59 GLWindowContext_mac::~GLWindowContext_mac() { teardownContext(); }
60
61 void GLWindowContext_mac::teardownContext() {
62 [NSOpenGLContext clearCurrentContext];
63 [fPixelFormat release];
64 fPixelFormat = nil;
65 [fGLContext release];
66 fGLContext = nil;
67 }
68
69 sk_sp<const GrGLInterface> GLWindowContext_mac::onInitializeContext() {
70 SkASSERT(nil != fMainView);
71
72 if (!fGLContext) {
73 fPixelFormat = skwindow::GetGLPixelFormat(fDisplayParams->msaaSampleCount());
74 if (nil == fPixelFormat) {
75 return nullptr;
76 }
77
78 // create context
79 fGLContext = [[NSOpenGLContext alloc] initWithFormat:fPixelFormat shareContext:nil];
80 if (nil == fGLContext) {
81 [fPixelFormat release];
82 fPixelFormat = nil;
83 return nullptr;
84 }
85
86 [fMainView setWantsBestResolutionOpenGLSurface:YES];
87 [fGLContext setView:fMainView];
88 }
89
90 GLint swapInterval = fDisplayParams->disableVsync() ? 0 : 1;
91 [fGLContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
92
93 // make context current
94 [fGLContext makeCurrentContext];
95
96 sk_sp<const GrGLInterface> backendContext = GrGLInterfaces::MakeMac();
97 const GrGLInterface* gl = backendContext.get();
98
99 GR_GL_CALL(gl, ClearStencil(0));
100 GR_GL_CALL(gl, ClearColor(0, 0, 0, 255));
101 GR_GL_CALL(gl, StencilMask(0xffffffff));
102 GR_GL_CALL(gl, Clear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT));
103
104 GLint stencilBits;
105 [fPixelFormat getValues:&stencilBits forAttribute:NSOpenGLPFAStencilSize forVirtualScreen:0];
106 fStencilBits = stencilBits;
107 GLint sampleCount;
108 [fPixelFormat getValues:&sampleCount forAttribute:NSOpenGLPFASamples forVirtualScreen:0];
109 fSampleCount = sampleCount;
110 fSampleCount = std::max(fSampleCount, 1);
111
112 CGFloat backingScaleFactor = skwindow::GetBackingScaleFactor(fMainView);
113 fWidth = fMainView.bounds.size.width * backingScaleFactor;
114 fHeight = fMainView.bounds.size.height * backingScaleFactor;
115 GR_GL_CALL(gl, Viewport(0, 0, fWidth, fHeight));
116
117 return backendContext;
118 }
119
120 void GLWindowContext_mac::onDestroyContext() {
121 // We only need to tear down the GLContext if we've changed the sample count.
122 if (fGLContext && fSampleCount != fDisplayParams->msaaSampleCount()) {
123 teardownContext();
124 }
125 }
126
127 void GLWindowContext_mac::onSwapBuffers() {
128 GrDirectContext* dContext = fSurface->recordingContext()->asDirectContext();
129 dContext->flush(fSurface.get(), SkSurfaces::BackendSurfaceAccess::kPresent, {});
130 [fGLContext flushBuffer];
131 }
132
133 void GLWindowContext_mac::resize(int w, int h) {
134 [fGLContext update];
135
136 // The super class always recreates the context.
137 GLWindowContext::resize(0, 0);
138 }
139
140 } // anonymous namespace
141
142 #pragma clang diagnostic pop
143
144 namespace skwindow {
145
146 std::unique_ptr<WindowContext> MakeGaneshGLForMac(const MacWindowInfo& info,
147 std::unique_ptr<const DisplayParams> params) {
148 std::unique_ptr<WindowContext> ctx(new GLWindowContext_mac(info, std::move(params)));
149 if (!ctx->isValid()) {
150 return nullptr;
151 }
152 return ctx;
153 }
154
155 } // namespace skwindow