2019-01-28 19:46:04 +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 "include/gpu/gl/GrGLInterface.h"
|
|
|
|
#include "tools/sk_app/GLWindowContext.h"
|
|
|
|
#include "tools/sk_app/mac/WindowContextFactory_mac.h"
|
2019-01-28 19:46:04 +00:00
|
|
|
|
|
|
|
#include <OpenGL/gl.h>
|
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
using sk_app::DisplayParams;
|
|
|
|
using sk_app::window_context_factory::MacWindowInfo;
|
|
|
|
using sk_app::GLWindowContext;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class GLWindowContext_mac : public GLWindowContext {
|
|
|
|
public:
|
|
|
|
GLWindowContext_mac(const MacWindowInfo&, const DisplayParams&);
|
|
|
|
|
|
|
|
~GLWindowContext_mac() override;
|
|
|
|
|
|
|
|
void onSwapBuffers() override;
|
|
|
|
|
|
|
|
sk_sp<const GrGLInterface> onInitializeContext() override;
|
2019-07-11 20:23:49 +00:00
|
|
|
void onDestroyContext() override;
|
2019-05-16 14:31:56 +00:00
|
|
|
|
|
|
|
void resize(int w, int h) override;
|
2019-01-28 19:46:04 +00:00
|
|
|
|
|
|
|
private:
|
2020-05-06 19:25:54 +00:00
|
|
|
void teardownContext();
|
|
|
|
|
2019-01-28 19:46:04 +00:00
|
|
|
NSView* fMainView;
|
|
|
|
NSOpenGLContext* fGLContext;
|
|
|
|
NSOpenGLPixelFormat* fPixelFormat;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GLWindowContext;
|
2019-01-28 19:46:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GLWindowContext_mac::GLWindowContext_mac(const MacWindowInfo& info, const DisplayParams& params)
|
|
|
|
: INHERITED(params)
|
2019-05-16 14:31:56 +00:00
|
|
|
, fMainView(info.fMainView)
|
|
|
|
, fGLContext(nil) {
|
2019-01-28 19:46:04 +00:00
|
|
|
|
|
|
|
// any config code here (particularly for msaa)?
|
|
|
|
|
|
|
|
this->initializeContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
GLWindowContext_mac::~GLWindowContext_mac() {
|
2020-05-06 19:25:54 +00:00
|
|
|
teardownContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLWindowContext_mac::teardownContext() {
|
2020-02-07 17:54:32 +00:00
|
|
|
[NSOpenGLContext clearCurrentContext];
|
2019-05-16 14:31:56 +00:00
|
|
|
[fPixelFormat release];
|
|
|
|
fPixelFormat = nil;
|
|
|
|
[fGLContext release];
|
|
|
|
fGLContext = nil;
|
2019-01-28 19:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<const GrGLInterface> GLWindowContext_mac::onInitializeContext() {
|
|
|
|
SkASSERT(nil != fMainView);
|
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
if (!fGLContext) {
|
|
|
|
// set up pixel format
|
2020-05-06 00:32:05 +00:00
|
|
|
constexpr int kMaxAttributes = 19;
|
2019-05-16 14:31:56 +00:00
|
|
|
NSOpenGLPixelFormatAttribute attributes[kMaxAttributes];
|
|
|
|
int numAttributes = 0;
|
|
|
|
attributes[numAttributes++] = NSOpenGLPFAAccelerated;
|
|
|
|
attributes[numAttributes++] = NSOpenGLPFAClosestPolicy;
|
|
|
|
attributes[numAttributes++] = NSOpenGLPFADoubleBuffer;
|
|
|
|
attributes[numAttributes++] = NSOpenGLPFAOpenGLProfile;
|
|
|
|
attributes[numAttributes++] = NSOpenGLProfileVersion3_2Core;
|
|
|
|
attributes[numAttributes++] = NSOpenGLPFAColorSize;
|
|
|
|
attributes[numAttributes++] = 24;
|
|
|
|
attributes[numAttributes++] = NSOpenGLPFAAlphaSize;
|
|
|
|
attributes[numAttributes++] = 8;
|
|
|
|
attributes[numAttributes++] = NSOpenGLPFADepthSize;
|
2019-01-28 19:46:04 +00:00
|
|
|
attributes[numAttributes++] = 0;
|
2019-05-16 14:31:56 +00:00
|
|
|
attributes[numAttributes++] = NSOpenGLPFAStencilSize;
|
|
|
|
attributes[numAttributes++] = 8;
|
|
|
|
if (fDisplayParams.fMSAASampleCount > 1) {
|
2020-05-06 00:32:05 +00:00
|
|
|
attributes[numAttributes++] = NSOpenGLPFAMultisample;
|
2019-05-16 14:31:56 +00:00
|
|
|
attributes[numAttributes++] = NSOpenGLPFASampleBuffers;
|
|
|
|
attributes[numAttributes++] = 1;
|
|
|
|
attributes[numAttributes++] = NSOpenGLPFASamples;
|
|
|
|
attributes[numAttributes++] = fDisplayParams.fMSAASampleCount;
|
|
|
|
} else {
|
|
|
|
attributes[numAttributes++] = NSOpenGLPFASampleBuffers;
|
|
|
|
attributes[numAttributes++] = 0;
|
|
|
|
}
|
|
|
|
attributes[numAttributes++] = 0;
|
|
|
|
SkASSERT(numAttributes <= kMaxAttributes);
|
|
|
|
|
|
|
|
fPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
|
|
|
|
if (nil == fPixelFormat) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create context
|
|
|
|
fGLContext = [[NSOpenGLContext alloc] initWithFormat:fPixelFormat shareContext:nil];
|
|
|
|
if (nil == fGLContext) {
|
|
|
|
[fPixelFormat release];
|
|
|
|
fPixelFormat = nil;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-01-28 19:30:59 +00:00
|
|
|
[fMainView setWantsBestResolutionOpenGLSurface:YES];
|
2019-05-16 14:31:56 +00:00
|
|
|
[fGLContext setView:fMainView];
|
2019-01-28 19:46:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 14:49:41 +00:00
|
|
|
GLint swapInterval = fDisplayParams.fDisableVsync ? 0 : 1;
|
|
|
|
[fGLContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
|
|
|
|
|
2019-01-28 19:46:04 +00:00
|
|
|
// make context current
|
|
|
|
[fGLContext makeCurrentContext];
|
|
|
|
|
|
|
|
glClearStencil(0);
|
|
|
|
glClearColor(0, 0, 0, 255);
|
|
|
|
glStencilMask(0xffffffff);
|
|
|
|
glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
GLint stencilBits;
|
|
|
|
[fPixelFormat getValues:&stencilBits forAttribute:NSOpenGLPFAStencilSize forVirtualScreen:0];
|
|
|
|
fStencilBits = stencilBits;
|
|
|
|
GLint sampleCount;
|
|
|
|
[fPixelFormat getValues:&sampleCount forAttribute:NSOpenGLPFASamples forVirtualScreen:0];
|
|
|
|
fSampleCount = sampleCount;
|
2020-02-07 15:36:46 +00:00
|
|
|
fSampleCount = std::max(fSampleCount, 1);
|
2019-01-28 19:46:04 +00:00
|
|
|
|
2021-01-28 19:30:59 +00:00
|
|
|
CGFloat backingScaleFactor = sk_app::GetBackingScaleFactor(fMainView);
|
|
|
|
fWidth = fMainView.bounds.size.width * backingScaleFactor;
|
|
|
|
fHeight = fMainView.bounds.size.height * backingScaleFactor;
|
2019-01-28 19:46:04 +00:00
|
|
|
glViewport(0, 0, fWidth, fHeight);
|
|
|
|
|
|
|
|
return GrGLMakeNativeInterface();
|
|
|
|
}
|
|
|
|
|
2019-07-11 20:23:49 +00:00
|
|
|
void GLWindowContext_mac::onDestroyContext() {
|
|
|
|
// We only need to tear down the GLContext if we've changed the sample count.
|
|
|
|
if (fGLContext && fSampleCount != fDisplayParams.fMSAASampleCount) {
|
2020-05-06 19:25:54 +00:00
|
|
|
teardownContext();
|
2019-07-11 20:23:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 19:46:04 +00:00
|
|
|
void GLWindowContext_mac::onSwapBuffers() {
|
|
|
|
[fGLContext flushBuffer];
|
|
|
|
}
|
|
|
|
|
2019-05-16 14:31:56 +00:00
|
|
|
void GLWindowContext_mac::resize(int w, int h) {
|
|
|
|
[fGLContext update];
|
2021-01-28 19:30:59 +00:00
|
|
|
|
|
|
|
// The super class always recreates the context.
|
|
|
|
INHERITED::resize(0, 0);
|
2019-05-16 14:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-28 19:46:04 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace sk_app {
|
|
|
|
namespace window_context_factory {
|
|
|
|
|
2019-08-01 20:21:49 +00:00
|
|
|
std::unique_ptr<WindowContext> MakeGLForMac(const MacWindowInfo& info,
|
|
|
|
const DisplayParams& params) {
|
|
|
|
std::unique_ptr<WindowContext> ctx(new GLWindowContext_mac(info, params));
|
2019-01-28 19:46:04 +00:00
|
|
|
if (!ctx->isValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace window_context_factory
|
|
|
|
} // namespace sk_app
|