Debian Codegraph @

tools/window/mac/RasterWindowContext_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/RasterWindowContext_mac.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColorFilter.h"
12 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
13 #include "include/gpu/ganesh/gl/mac/GrGLMakeMacInterface.h"
14 #include "include/gpu/ganesh/gl/GrGLInterface.h"
15 #include "tools/ToolUtils.h"
16 #include "tools/window/GLWindowContext.h"
17 #include "tools/window/mac/MacWindowInfo.h"
18
19 #include <OpenGL/gl.h>
20
21 #include <Cocoa/Cocoa.h>
22
23 using skwindow::DisplayParams;
24 using skwindow::MacWindowInfo;
25 using skwindow::internal::GLWindowContext;
26
27 // All of NSOpenGL is deprecated.
28 #pragma clang diagnostic push
29 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
30
31 namespace {
32
33 // TODO: This still uses GL to handle the update rather than using a purely raster backend,
34 // for historical reasons. Writing a pure raster backend would be better in the long run.
35
36 class RasterWindowContext_mac : public GLWindowContext {
37 public:
38 RasterWindowContext_mac(const MacWindowInfo&, std::unique_ptr<const DisplayParams>);
39
40 ~RasterWindowContext_mac() override;
41
42 sk_sp<SkSurface> getBackbufferSurface() override;
43
44 sk_sp<const GrGLInterface> onInitializeContext() override;
45 void onDestroyContext() override {}
46
47 void resize(int w, int h) override;
48
49 private:
50 void onSwapBuffers() override;
51
52 NSView* fMainView;
53 NSOpenGLContext* fGLContext;
54 NSOpenGLPixelFormat* fPixelFormat;
55 sk_sp<SkSurface> fBackbufferSurface;
56 };
57
58 RasterWindowContext_mac::RasterWindowContext_mac(const MacWindowInfo& info,
59 std::unique_ptr<const DisplayParams> params)
60 : GLWindowContext(std::move(params)), fMainView(info.fMainView), fGLContext(nil) {
61 // any config code here (particularly for msaa)?
62
63 this->initializeContext();
64 }
65
66 RasterWindowContext_mac::~RasterWindowContext_mac() {
67 [NSOpenGLContext clearCurrentContext];
68 [fPixelFormat release];
69 fPixelFormat = nil;
70 [fGLContext release];
71 fGLContext = nil;
72 }
73
74 sk_sp<const GrGLInterface> RasterWindowContext_mac::onInitializeContext() {
75 SkASSERT(nil != fMainView);
76
77 if (!fGLContext) {
78 // set up pixel format
79 constexpr int kMaxAttributes = 18;
80 NSOpenGLPixelFormatAttribute attributes[kMaxAttributes];
81 int numAttributes = 0;
82 attributes[numAttributes++] = NSOpenGLPFAAccelerated;
83 attributes[numAttributes++] = NSOpenGLPFAClosestPolicy;
84 attributes[numAttributes++] = NSOpenGLPFADoubleBuffer;
85 attributes[numAttributes++] = NSOpenGLPFAOpenGLProfile;
86 attributes[numAttributes++] = NSOpenGLProfileVersion3_2Core;
87 attributes[numAttributes++] = NSOpenGLPFAColorSize;
88 attributes[numAttributes++] = 24;
89 attributes[numAttributes++] = NSOpenGLPFAAlphaSize;
90 attributes[numAttributes++] = 8;
91 attributes[numAttributes++] = NSOpenGLPFADepthSize;
92 attributes[numAttributes++] = 0;
93 attributes[numAttributes++] = NSOpenGLPFAStencilSize;
94 attributes[numAttributes++] = 8;
95 if (fDisplayParams->msaaSampleCount() > 1) {
96 attributes[numAttributes++] = NSOpenGLPFASampleBuffers;
97 attributes[numAttributes++] = 1;
98 attributes[numAttributes++] = NSOpenGLPFASamples;
99 attributes[numAttributes++] = fDisplayParams->msaaSampleCount();
100 } else {
101 attributes[numAttributes++] = NSOpenGLPFASampleBuffers;
102 attributes[numAttributes++] = 0;
103 }
104 attributes[numAttributes++] = 0;
105 SkASSERT(numAttributes <= kMaxAttributes);
106
107 fPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
108 if (nil == fPixelFormat) {
109 return nullptr;
110 }
111
112 // create context
113 fGLContext = [[NSOpenGLContext alloc] initWithFormat:fPixelFormat shareContext:nil];
114 if (nil == fGLContext) {
115 [fPixelFormat release];
116 fPixelFormat = nil;
117 return nullptr;
118 }
119
120 [fMainView setWantsBestResolutionOpenGLSurface:YES];
121 [fGLContext setView:fMainView];
122
123 GLint swapInterval = fDisplayParams->disableVsync() ? 0 : 1;
124 [fGLContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
125 }
126
127 // make context current
128 [fGLContext makeCurrentContext];
129
130 glClearStencil(0);
131 glClearColor(0, 0, 0, 255);
132 glStencilMask(0xffffffff);
133 glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
134
135 GLint stencilBits;
136 [fPixelFormat getValues:&stencilBits forAttribute:NSOpenGLPFAStencilSize forVirtualScreen:0];
137 fStencilBits = stencilBits;
138 GLint sampleCount;
139 [fPixelFormat getValues:&sampleCount forAttribute:NSOpenGLPFASamples forVirtualScreen:0];
140 fSampleCount = sampleCount;
141 fSampleCount = std::max(fSampleCount, 1);
142
143 CGFloat backingScaleFactor = skwindow::GetBackingScaleFactor(fMainView);
144 fWidth = fMainView.bounds.size.width * backingScaleFactor;
145 fHeight = fMainView.bounds.size.height * backingScaleFactor;
146 glViewport(0, 0, fWidth, fHeight);
147
148 // make the offscreen image
149 SkImageInfo info = SkImageInfo::Make(fWidth,
150 fHeight,
151 fDisplayParams->colorType(),
152 kPremul_SkAlphaType,
153 fDisplayParams->colorSpace());
154 fBackbufferSurface = SkSurfaces::Raster(info);
155 return GrGLInterfaces::MakeMac();
156 }
157
158 sk_sp<SkSurface> RasterWindowContext_mac::getBackbufferSurface() { return fBackbufferSurface; }
159
160 void RasterWindowContext_mac::onSwapBuffers() {
161 if (fBackbufferSurface) {
162 // We made/have an off-screen surface. Get the contents as an SkImage:
163 sk_sp<SkImage> snapshot = fBackbufferSurface->makeImageSnapshot();
164
165 sk_sp<SkSurface> gpuSurface = GLWindowContext::getBackbufferSurface();
166 SkCanvas* gpuCanvas = gpuSurface->getCanvas();
167 gpuCanvas->drawImage(snapshot, 0, 0);
168 skgpu::ganesh::Flush(gpuSurface);
169
170 [fGLContext flushBuffer];
171 }
172 }
173
174 void RasterWindowContext_mac::resize(int w, int h) {
175 [fGLContext update];
176
177 // The super class always recreates the context.
178 GLWindowContext::resize(0, 0);
179 }
180
181 } // anonymous namespace
182
183 #pragma clang diagnostic pop
184
185 namespace skwindow {
186
187 std::unique_ptr<WindowContext> MakeRasterForMac(const MacWindowInfo& info,
188 std::unique_ptr<const DisplayParams> params) {
189 std::unique_ptr<WindowContext> ctx(new RasterWindowContext_mac(info, std::move(params)));
190 if (!ctx->isValid()) {
191 return nullptr;
192 }
193 return ctx;
194 }
195
196 } // namespace skwindow