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.
|
|
|
|
*/
|
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>
|
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;
|
|
|
|
void makeCurrent() const override;
|
|
|
|
void swapBuffers() const 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();
|
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
CGLContextObj fContext;
|
|
|
|
};
|
2011-10-24 21:17:53 +00:00
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
MacGLContext::MacGLContext()
|
2011-10-19 20:43:20 +00:00
|
|
|
: fContext(NULL) {
|
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
|
|
|
|
2013-02-14 15:10:59 +00:00
|
|
|
if (NULL == pixFormat) {
|
|
|
|
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
|
|
|
|
2013-02-14 15:10:59 +00:00
|
|
|
CGLCreateContext(pixFormat, NULL, &fContext);
|
|
|
|
CGLReleasePixelFormat(pixFormat);
|
2013-02-15 07:16:57 +00:00
|
|
|
|
2011-10-19 20:43:20 +00:00
|
|
|
if (NULL == 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
|
|
|
|
2014-10-16 06:03:54 +00:00
|
|
|
fGL.reset(GrGLCreateNativeInterface());
|
|
|
|
if (NULL == fGL.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;
|
|
|
|
}
|
|
|
|
if (!fGL->validate()) {
|
|
|
|
SkDebugf("Context could not validate GL interface.\n");
|
|
|
|
this->destroyGLContext();
|
|
|
|
return;
|
2011-10-19 20:43:20 +00:00
|
|
|
}
|
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() {
|
|
|
|
this->destroyGLContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacGLContext::destroyGLContext() {
|
|
|
|
fGL.reset(NULL);
|
|
|
|
if (fContext) {
|
|
|
|
CGLReleaseContext(fContext);
|
|
|
|
fContext = NULL;
|
|
|
|
}
|
2011-10-19 20:43:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
void MacGLContext::makeCurrent() 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
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
void MacGLContext::swapBuffers() const {
|
2013-10-09 18:25:38 +00:00
|
|
|
CGLFlushDrawable(fContext);
|
|
|
|
}
|
2014-10-09 12:24:15 +00:00
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-10-16 06:03:54 +00:00
|
|
|
SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI) {
|
|
|
|
if (kGLES_GrGLStandard == forcedGpuAPI) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
MacGLContext* ctx = SkNEW(MacGLContext);
|
|
|
|
if (!ctx->isValid()) {
|
|
|
|
SkDELETE(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ctx;
|
2014-10-09 12:24:15 +00:00
|
|
|
}
|