2011-10-19 20:43:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2015-11-02 18:20:27 +00:00
|
|
|
#include "SkTypes.h"
|
|
|
|
#if defined(SK_BUILD_FOR_MAC)
|
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
#include "gl/SkGLContext.h"
|
2013-03-07 19:09:11 +00:00
|
|
|
#include "AvailabilityMacros.h"
|
2011-10-19 20:43:20 +00:00
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
#include <OpenGL/OpenGL.h>
|
2015-06-23 20:23:44 +00:00
|
|
|
#include <dlfcn.h>
|
2014-10-08 11:14:24 +00:00
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
namespace {
|
|
|
|
class MacGLContext : public SkGLContext {
|
|
|
|
public:
|
|
|
|
MacGLContext();
|
2015-03-26 01:17:31 +00:00
|
|
|
~MacGLContext() override;
|
2011-10-24 21:17:53 +00:00
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
private:
|
2014-10-16 06:03:54 +00:00
|
|
|
void destroyGLContext();
|
|
|
|
|
2015-06-23 20:23:44 +00:00
|
|
|
void onPlatformMakeCurrent() const override;
|
|
|
|
void onPlatformSwapBuffers() const override;
|
|
|
|
GrGLFuncPtr onPlatformGetProcAddress(const char*) const override;
|
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
CGLContextObj fContext;
|
2015-06-23 20:23:44 +00:00
|
|
|
void* fGLLibrary;
|
2014-10-09 12:24:15 +00:00
|
|
|
};
|
2011-10-24 21:17:53 +00:00
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
MacGLContext::MacGLContext()
|
2015-08-27 14:41:13 +00:00
|
|
|
: fContext(nullptr)
|
2015-06-23 20:23:44 +00:00
|
|
|
, fGLLibrary(RTLD_DEFAULT) {
|
2013-02-14 15:10:59 +00:00
|
|
|
CGLPixelFormatAttribute attributes[] = {
|
2013-03-07 19:09:11 +00:00
|
|
|
#if MAC_OS_X_VERSION_10_7
|
2013-02-26 21:46:32 +00:00
|
|
|
kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
|
2013-02-14 15:10:59 +00:00
|
|
|
#endif
|
2013-10-09 18:25:38 +00:00
|
|
|
kCGLPFADoubleBuffer,
|
2013-02-14 15:10:59 +00:00
|
|
|
(CGLPixelFormatAttribute)0
|
2011-10-19 20:43:20 +00:00
|
|
|
};
|
2013-02-14 15:10:59 +00:00
|
|
|
CGLPixelFormatObj pixFormat;
|
|
|
|
GLint npix;
|
2013-02-15 07:16:57 +00:00
|
|
|
|
2013-02-14 15:10:59 +00:00
|
|
|
CGLChoosePixelFormat(attributes, &pixFormat, &npix);
|
2013-02-15 07:16:57 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == pixFormat) {
|
2013-02-14 15:10:59 +00:00
|
|
|
SkDebugf("CGLChoosePixelFormat failed.");
|
2014-10-16 06:03:54 +00:00
|
|
|
return;
|
2011-10-19 20:43:20 +00:00
|
|
|
}
|
2013-02-15 07:16:57 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
CGLCreateContext(pixFormat, nullptr, &fContext);
|
2013-02-14 15:10:59 +00:00
|
|
|
CGLReleasePixelFormat(pixFormat);
|
2013-02-15 07:16:57 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fContext) {
|
2013-02-14 15:10:59 +00:00
|
|
|
SkDebugf("CGLCreateContext failed.");
|
2014-10-16 06:03:54 +00:00
|
|
|
return;
|
2011-10-19 20:43:20 +00:00
|
|
|
}
|
2013-02-15 07:16:57 +00:00
|
|
|
|
2013-02-14 15:10:59 +00:00
|
|
|
CGLSetCurrentContext(fContext);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2015-06-23 20:23:44 +00:00
|
|
|
SkAutoTUnref<const GrGLInterface> gl(GrGLCreateNativeInterface());
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == gl.get()) {
|
2011-10-19 20:43:20 +00:00
|
|
|
SkDebugf("Context could not create GL interface.\n");
|
|
|
|
this->destroyGLContext();
|
2014-10-16 06:03:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-06-23 20:23:44 +00:00
|
|
|
if (!gl->validate()) {
|
2014-10-16 06:03:54 +00:00
|
|
|
SkDebugf("Context could not validate GL interface.\n");
|
|
|
|
this->destroyGLContext();
|
|
|
|
return;
|
2011-10-19 20:43:20 +00:00
|
|
|
}
|
2015-06-23 20:23:44 +00:00
|
|
|
|
|
|
|
fGLLibrary = dlopen(
|
|
|
|
"/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib",
|
|
|
|
RTLD_LAZY);
|
|
|
|
|
|
|
|
this->init(gl.detach());
|
2014-10-16 06:03:54 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2014-10-16 06:03:54 +00:00
|
|
|
MacGLContext::~MacGLContext() {
|
2015-06-23 20:23:44 +00:00
|
|
|
this->teardown();
|
2014-10-16 06:03:54 +00:00
|
|
|
this->destroyGLContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacGLContext::destroyGLContext() {
|
|
|
|
if (fContext) {
|
|
|
|
CGLReleaseContext(fContext);
|
2015-08-27 14:41:13 +00:00
|
|
|
fContext = nullptr;
|
2014-10-16 06:03:54 +00:00
|
|
|
}
|
2015-06-23 20:23:44 +00:00
|
|
|
if (RTLD_DEFAULT != fGLLibrary) {
|
|
|
|
dlclose(fGLLibrary);
|
|
|
|
}
|
2011-10-19 20:43:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 20:23:44 +00:00
|
|
|
void MacGLContext::onPlatformMakeCurrent() const {
|
2013-02-14 15:10:59 +00:00
|
|
|
CGLSetCurrentContext(fContext);
|
2011-10-19 20:43:20 +00:00
|
|
|
}
|
2013-10-09 18:25:38 +00:00
|
|
|
|
2015-06-23 20:23:44 +00:00
|
|
|
void MacGLContext::onPlatformSwapBuffers() const {
|
2013-10-09 18:25:38 +00:00
|
|
|
CGLFlushDrawable(fContext);
|
|
|
|
}
|
2014-10-09 12:24:15 +00:00
|
|
|
|
2015-06-23 20:23:44 +00:00
|
|
|
GrGLFuncPtr MacGLContext::onPlatformGetProcAddress(const char* procName) const {
|
|
|
|
return reinterpret_cast<GrGLFuncPtr>(dlsym(fGLLibrary, procName));
|
|
|
|
}
|
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-01-20 16:07:01 +00:00
|
|
|
SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI, SkGLContext* shareContext) {
|
|
|
|
SkASSERT(!shareContext);
|
|
|
|
if (shareContext) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-16 06:03:54 +00:00
|
|
|
if (kGLES_GrGLStandard == forcedGpuAPI) {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2014-10-16 06:03:54 +00:00
|
|
|
}
|
2015-08-26 20:07:48 +00:00
|
|
|
MacGLContext* ctx = new MacGLContext;
|
2014-10-16 06:03:54 +00:00
|
|
|
if (!ctx->isValid()) {
|
2015-08-26 20:07:48 +00:00
|
|
|
delete ctx;
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2014-10-16 06:03:54 +00:00
|
|
|
}
|
|
|
|
return ctx;
|
2014-10-09 12:24:15 +00:00
|
|
|
}
|
2015-11-02 18:20:27 +00:00
|
|
|
|
|
|
|
#endif//defined(SK_BUILD_FOR_MAC)
|