2010-04-27 15:29:29 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-04-27 15:29:29 -07:00
|
|
|
|
|
|
|
#include "GLContextProvider.h"
|
2014-01-07 12:02:18 -08:00
|
|
|
#include "GLContextCGL.h"
|
2013-12-03 10:44:38 -08:00
|
|
|
#include "TextureImageCGL.h"
|
2010-04-27 15:29:29 -07:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include <OpenGL/gl.h>
|
2011-02-07 12:15:46 -08:00
|
|
|
#include "gfxFailure.h"
|
2015-04-02 17:59:47 -07:00
|
|
|
#include "gfxPrefs.h"
|
2010-09-21 11:39:38 -07:00
|
|
|
#include "prenv.h"
|
2013-03-18 07:25:50 -07:00
|
|
|
#include "GeckoProfiler.h"
|
2013-04-16 19:21:06 -07:00
|
|
|
#include "mozilla/gfx/MacIOSurface.h"
|
2010-04-27 15:29:29 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2014-05-22 03:11:45 -07:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
class CGLLibrary
|
|
|
|
{
|
|
|
|
public:
|
2010-07-18 22:01:14 -07:00
|
|
|
CGLLibrary()
|
2015-02-12 19:00:41 -08:00
|
|
|
: mInitialized(false)
|
|
|
|
, mUseDoubleBufferedWindows(true)
|
|
|
|
, mOGLLibrary(nullptr)
|
|
|
|
{}
|
2010-04-27 15:29:29 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool EnsureInitialized()
|
2010-04-27 15:29:29 -07:00
|
|
|
{
|
|
|
|
if (mInitialized) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
if (!mOGLLibrary) {
|
|
|
|
mOGLLibrary = PR_LoadLibrary("/System/Library/Frameworks/OpenGL.framework/OpenGL");
|
|
|
|
if (!mOGLLibrary) {
|
|
|
|
NS_WARNING("Couldn't load OpenGL Framework.");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
}
|
2010-09-21 11:39:38 -07:00
|
|
|
|
|
|
|
const char* db = PR_GetEnv("MOZ_CGL_DB");
|
2015-02-12 19:00:41 -08:00
|
|
|
if (db) {
|
|
|
|
mUseDoubleBufferedWindows = *db != '0';
|
|
|
|
}
|
2010-09-21 11:39:38 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mInitialized = true;
|
|
|
|
return true;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2015-02-12 19:00:41 -08:00
|
|
|
bool UseDoubleBufferedWindows() const {
|
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
return mUseDoubleBufferedWindows;
|
2015-02-06 11:37:04 -08:00
|
|
|
}
|
2015-02-12 19:00:41 -08:00
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
private:
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mInitialized;
|
2015-02-12 19:00:41 -08:00
|
|
|
bool mUseDoubleBufferedWindows;
|
2010-04-27 15:29:29 -07:00
|
|
|
PRLibrary *mOGLLibrary;
|
2014-03-03 18:47:43 -08:00
|
|
|
};
|
2010-04-27 15:29:29 -07:00
|
|
|
|
|
|
|
CGLLibrary sCGLLibrary;
|
|
|
|
|
2015-02-12 19:00:41 -08:00
|
|
|
GLContextCGL::GLContextCGL(const SurfaceCaps& caps, NSOpenGLContext* context,
|
|
|
|
bool isOffscreen, ContextProfile profile)
|
|
|
|
: GLContext(caps, nullptr, isOffscreen)
|
|
|
|
, mContext(context)
|
2010-04-27 15:29:29 -07:00
|
|
|
{
|
2015-02-12 19:00:41 -08:00
|
|
|
SetProfileVersion(profile, 210);
|
2014-01-07 12:02:18 -08:00
|
|
|
}
|
2010-04-27 15:29:29 -07:00
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
GLContextCGL::~GLContextCGL()
|
|
|
|
{
|
|
|
|
MarkDestroyed();
|
|
|
|
|
|
|
|
if (mContext) {
|
|
|
|
if ([NSOpenGLContext currentContext] == mContext) {
|
|
|
|
// Clear the current context before releasing. If we don't do
|
|
|
|
// this, the next time we call [NSOpenGLContext currentContext],
|
|
|
|
// "invalid context" will be printed to the console.
|
|
|
|
[NSOpenGLContext clearCurrentContext];
|
2013-10-09 07:39:22 -07:00
|
|
|
}
|
2014-01-07 12:02:18 -08:00
|
|
|
[mContext release];
|
2010-07-18 22:01:14 -07:00
|
|
|
}
|
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
}
|
2010-04-27 15:29:29 -07:00
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
bool
|
|
|
|
GLContextCGL::Init()
|
|
|
|
{
|
|
|
|
if (!InitWithPrefix("gl", true))
|
|
|
|
return false;
|
2010-04-27 15:29:29 -07:00
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGLContextObj
|
|
|
|
GLContextCGL::GetCGLContext() const
|
|
|
|
{
|
|
|
|
return static_cast<CGLContextObj>([mContext CGLContextObj]);
|
|
|
|
}
|
2012-05-12 16:23:56 -07:00
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
bool
|
|
|
|
GLContextCGL::MakeCurrentImpl(bool aForce)
|
|
|
|
{
|
|
|
|
if (!aForce && [NSOpenGLContext currentContext] == mContext) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
if (mContext) {
|
|
|
|
[mContext makeCurrentContext];
|
|
|
|
// Use non-blocking swap in "ASAP mode".
|
|
|
|
// ASAP mode means that rendering is iterated as fast as possible.
|
|
|
|
// ASAP mode is entered when layout.frame_rate=0 (requires restart).
|
|
|
|
// If swapInt is 1, then glSwapBuffers will block and wait for a vblank signal.
|
|
|
|
// When we're iterating as fast as possible, however, we want a non-blocking
|
|
|
|
// glSwapBuffers, which will happen when swapInt==0.
|
2014-02-26 18:52:54 -08:00
|
|
|
GLint swapInt = gfxPrefs::LayoutFrameRate() == 0 ? 0 : 1;
|
2014-01-07 12:02:18 -08:00
|
|
|
[mContext setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
|
2012-08-21 20:30:20 -07:00
|
|
|
}
|
2014-01-07 12:02:18 -08:00
|
|
|
return true;
|
|
|
|
}
|
2012-08-21 20:30:20 -07:00
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
bool
|
|
|
|
GLContextCGL::IsCurrent() {
|
|
|
|
return [NSOpenGLContext currentContext] == mContext;
|
|
|
|
}
|
2013-07-09 07:13:33 -07:00
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
GLenum
|
2014-03-03 18:47:43 -08:00
|
|
|
GLContextCGL::GetPreferredARGB32Format() const
|
|
|
|
{
|
|
|
|
return LOCAL_GL_BGRA;
|
|
|
|
}
|
2010-04-27 15:29:29 -07:00
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
bool
|
|
|
|
GLContextCGL::SetupLookupFunction()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-21 11:39:38 -07:00
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
bool
|
2014-03-03 19:13:34 -08:00
|
|
|
GLContextCGL::IsDoubleBuffered() const
|
2014-01-07 12:02:18 -08:00
|
|
|
{
|
2015-02-12 19:00:41 -08:00
|
|
|
return sCGLLibrary.UseDoubleBufferedWindows();
|
2014-01-07 12:02:18 -08:00
|
|
|
}
|
2011-11-18 19:57:29 -08:00
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
bool
|
2014-03-03 18:47:43 -08:00
|
|
|
GLContextCGL::SupportsRobustness() const
|
2014-01-07 12:02:18 -08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-21 11:39:38 -07:00
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
bool
|
|
|
|
GLContextCGL::SwapBuffers()
|
|
|
|
{
|
2014-05-23 14:12:29 -07:00
|
|
|
PROFILER_LABEL("GLContextCGL", "SwapBuffers",
|
|
|
|
js::ProfileEntry::Category::GRAPHICS);
|
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
[mContext flushBuffer];
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-18 22:01:14 -07:00
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
|
2015-02-06 11:37:04 -08:00
|
|
|
already_AddRefed<GLContext>
|
|
|
|
GLContextProviderCGL::CreateWrappingExisting(void*, void*)
|
2015-02-04 16:34:55 -08:00
|
|
|
{
|
2015-02-06 11:37:04 -08:00
|
|
|
return nullptr;
|
2015-02-04 16:34:55 -08:00
|
|
|
}
|
|
|
|
|
2015-02-12 19:00:41 -08:00
|
|
|
static const NSOpenGLPixelFormatAttribute kAttribs_singleBuffered[] = {
|
|
|
|
NSOpenGLPFAAccelerated,
|
|
|
|
NSOpenGLPFAAllowOfflineRenderers,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
static const NSOpenGLPixelFormatAttribute kAttribs_doubleBuffered[] = {
|
|
|
|
NSOpenGLPFAAccelerated,
|
|
|
|
NSOpenGLPFAAllowOfflineRenderers,
|
|
|
|
NSOpenGLPFADoubleBuffer,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
static const NSOpenGLPixelFormatAttribute kAttribs_offscreen[] = {
|
2015-04-02 17:59:47 -07:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
static const NSOpenGLPixelFormatAttribute kAttribs_offscreen_accel[] = {
|
|
|
|
NSOpenGLPFAAccelerated,
|
2015-02-12 19:00:41 -08:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
static const NSOpenGLPixelFormatAttribute kAttribs_offscreen_coreProfile[] = {
|
|
|
|
NSOpenGLPFAAccelerated,
|
|
|
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
static NSOpenGLContext*
|
|
|
|
CreateWithFormat(const NSOpenGLPixelFormatAttribute* attribs)
|
|
|
|
{
|
|
|
|
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc]
|
|
|
|
initWithAttributes:attribs];
|
2015-04-02 17:59:47 -07:00
|
|
|
if (!format) {
|
|
|
|
NS_WARNING("Failed to create NSOpenGLPixelFormat.");
|
2015-02-12 19:00:41 -08:00
|
|
|
return nullptr;
|
2015-04-02 17:59:47 -07:00
|
|
|
}
|
2015-02-12 19:00:41 -08:00
|
|
|
|
|
|
|
NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:format
|
|
|
|
shareContext:nullptr];
|
|
|
|
|
|
|
|
[format release];
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:57:26 -07:00
|
|
|
already_AddRefed<GLContext>
|
|
|
|
GLContextProviderCGL::CreateForWindow(nsIWidget *aWidget)
|
|
|
|
{
|
2015-02-12 19:00:41 -08:00
|
|
|
if (!sCGLLibrary.EnsureInitialized()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2010-07-18 22:01:14 -07:00
|
|
|
|
2015-02-12 19:00:41 -08:00
|
|
|
const NSOpenGLPixelFormatAttribute* attribs;
|
|
|
|
if (sCGLLibrary.UseDoubleBufferedWindows()) {
|
|
|
|
attribs = kAttribs_doubleBuffered;
|
|
|
|
} else {
|
|
|
|
attribs = kAttribs_singleBuffered;
|
|
|
|
}
|
|
|
|
NSOpenGLContext* context = CreateWithFormat(attribs);
|
2010-07-18 22:01:14 -07:00
|
|
|
if (!context) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2010-09-21 09:30:19 -07:00
|
|
|
// make the context transparent
|
2013-03-11 09:29:00 -07:00
|
|
|
GLint opaque = 0;
|
|
|
|
[context setValues:&opaque forParameter:NSOpenGLCPSurfaceOpacity];
|
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
SurfaceCaps caps = SurfaceCaps::ForRGBA();
|
2015-02-12 19:00:41 -08:00
|
|
|
ContextProfile profile = ContextProfile::OpenGLCompatibility;
|
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(caps, context, false,
|
|
|
|
profile);
|
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
if (!glContext->Init()) {
|
2015-02-12 19:00:41 -08:00
|
|
|
glContext = nullptr;
|
|
|
|
[context release];
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-12 15:10:38 -07:00
|
|
|
}
|
2010-04-27 15:29:29 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
return glContext.forget();
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
static already_AddRefed<GLContextCGL>
|
2015-02-12 19:00:41 -08:00
|
|
|
CreateOffscreenFBOContext(bool requireCompatProfile)
|
2010-07-18 22:01:14 -07:00
|
|
|
{
|
|
|
|
if (!sCGLLibrary.EnsureInitialized()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-17 21:04:22 -07:00
|
|
|
}
|
|
|
|
|
2015-02-12 19:00:41 -08:00
|
|
|
ContextProfile profile;
|
|
|
|
NSOpenGLContext* context = nullptr;
|
2015-02-06 11:37:04 -08:00
|
|
|
|
2015-02-12 19:00:41 -08:00
|
|
|
if (!requireCompatProfile) {
|
|
|
|
profile = ContextProfile::OpenGLCore;
|
|
|
|
context = CreateWithFormat(kAttribs_offscreen_coreProfile);
|
|
|
|
}
|
|
|
|
if (!context) {
|
|
|
|
profile = ContextProfile::OpenGLCompatibility;
|
2015-04-02 17:59:47 -07:00
|
|
|
|
|
|
|
if (gfxPrefs::RequireHardwareGL())
|
|
|
|
context = CreateWithFormat(kAttribs_offscreen_accel);
|
|
|
|
else
|
|
|
|
context = CreateWithFormat(kAttribs_offscreen);
|
2015-02-12 19:00:41 -08:00
|
|
|
}
|
2010-07-18 22:01:14 -07:00
|
|
|
if (!context) {
|
2015-04-02 17:59:47 -07:00
|
|
|
NS_WARNING("Failed to create NSOpenGLContext.");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-17 21:04:22 -07:00
|
|
|
}
|
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
SurfaceCaps dummyCaps = SurfaceCaps::Any();
|
2015-02-12 19:00:41 -08:00
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(dummyCaps, context,
|
|
|
|
true, profile);
|
2012-03-12 15:10:38 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
2014-08-27 16:16:22 -07:00
|
|
|
already_AddRefed<GLContext>
|
2015-02-12 19:00:41 -08:00
|
|
|
GLContextProviderCGL::CreateHeadless(bool requireCompatProfile)
|
2014-08-27 16:16:22 -07:00
|
|
|
{
|
2015-02-12 19:00:41 -08:00
|
|
|
nsRefPtr<GLContextCGL> gl;
|
|
|
|
gl = CreateOffscreenFBOContext(requireCompatProfile);
|
|
|
|
if (!gl)
|
2014-08-27 16:16:22 -07:00
|
|
|
return nullptr;
|
|
|
|
|
2015-04-02 17:59:47 -07:00
|
|
|
if (!gl->Init()) {
|
|
|
|
NS_WARNING("Failed during Init.");
|
2014-08-27 16:16:22 -07:00
|
|
|
return nullptr;
|
2015-04-02 17:59:47 -07:00
|
|
|
}
|
2014-08-27 16:16:22 -07:00
|
|
|
|
2015-02-12 19:00:41 -08:00
|
|
|
return gl.forget();
|
2014-08-27 16:16:22 -07:00
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
already_AddRefed<GLContext>
|
2013-02-13 15:26:24 -08:00
|
|
|
GLContextProviderCGL::CreateOffscreen(const gfxIntSize& size,
|
2015-02-12 19:00:41 -08:00
|
|
|
const SurfaceCaps& caps,
|
|
|
|
bool requireCompatProfile)
|
2010-07-18 22:01:14 -07:00
|
|
|
{
|
2015-02-12 19:00:41 -08:00
|
|
|
nsRefPtr<GLContext> glContext = CreateHeadless(requireCompatProfile);
|
2015-04-02 17:59:47 -07:00
|
|
|
if (!glContext->InitOffscreen(size, caps)) {
|
|
|
|
NS_WARNING("Failed during InitOffscreen.");
|
2014-08-27 16:16:22 -07:00
|
|
|
return nullptr;
|
2015-04-02 17:59:47 -07:00
|
|
|
}
|
2010-07-18 22:01:14 -07:00
|
|
|
|
2014-08-27 16:16:22 -07:00
|
|
|
return glContext.forget();
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
static nsRefPtr<GLContext> gGlobalContext;
|
|
|
|
|
2014-08-27 16:16:22 -07:00
|
|
|
GLContext*
|
2014-01-10 10:55:23 -08:00
|
|
|
GLContextProviderCGL::GetGlobalContext()
|
2010-07-18 22:01:14 -07:00
|
|
|
{
|
|
|
|
if (!sCGLLibrary.EnsureInitialized()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-07-18 22:01:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gGlobalContext) {
|
|
|
|
// There are bugs in some older drivers with pbuffers less
|
|
|
|
// than 16x16 in size; also 16x16 is POT so that we can do
|
|
|
|
// a FBO with it on older video cards. A FBO context for
|
|
|
|
// sharing is preferred since it has no associated target.
|
2013-02-13 15:26:24 -08:00
|
|
|
gGlobalContext = CreateOffscreenFBOContext(false);
|
2010-09-10 09:19:09 -07:00
|
|
|
if (!gGlobalContext || !static_cast<GLContextCGL*>(gGlobalContext.get())->Init()) {
|
2010-07-24 17:10:58 -07:00
|
|
|
NS_WARNING("Couldn't init gGlobalContext.");
|
2012-07-30 07:20:58 -07:00
|
|
|
gGlobalContext = nullptr;
|
2014-03-03 18:47:43 -08:00
|
|
|
return nullptr;
|
2010-07-24 17:10:58 -07:00
|
|
|
}
|
2010-07-18 22:01:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return gGlobalContext;
|
|
|
|
}
|
|
|
|
|
2010-07-19 21:05:42 -07:00
|
|
|
void
|
|
|
|
GLContextProviderCGL::Shutdown()
|
|
|
|
{
|
2015-02-12 19:00:41 -08:00
|
|
|
gGlobalContext = nullptr;
|
2010-07-19 21:05:42 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|