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>
|
2014-02-26 18:52:54 -08:00
|
|
|
#include "gfxPrefs.h"
|
2011-02-07 12:15:46 -08:00
|
|
|
#include "gfxFailure.h"
|
2010-09-21 11:39:38 -07:00
|
|
|
#include "prenv.h"
|
2011-10-19 12:09:57 -07:00
|
|
|
#include "mozilla/Preferences.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
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool gUseDoubleBufferedWindows = true;
|
2010-09-21 11:39:38 -07:00
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
class CGLLibrary
|
|
|
|
{
|
|
|
|
public:
|
2010-07-18 22:01:14 -07:00
|
|
|
CGLLibrary()
|
2011-10-17 07:59:28 -07:00
|
|
|
: mInitialized(false),
|
2012-07-30 07:20:58 -07:00
|
|
|
mOGLLibrary(nullptr),
|
|
|
|
mPixelFormat(nullptr)
|
2010-07-18 22:01:14 -07:00
|
|
|
{ }
|
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");
|
|
|
|
gUseDoubleBufferedWindows = (!db || *db != '0');
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mInitialized = true;
|
|
|
|
return true;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
NSOpenGLPixelFormat *PixelFormat()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
if (mPixelFormat == nullptr) {
|
2010-07-18 22:01:14 -07:00
|
|
|
NSOpenGLPixelFormatAttribute attribs[] = {
|
|
|
|
NSOpenGLPFAAccelerated,
|
2011-09-21 12:20:40 -07:00
|
|
|
NSOpenGLPFAAllowOfflineRenderers,
|
2010-09-21 11:39:38 -07:00
|
|
|
NSOpenGLPFADoubleBuffer,
|
2012-05-10 12:56:42 -07:00
|
|
|
0
|
2010-07-18 22:01:14 -07:00
|
|
|
};
|
|
|
|
|
2010-09-21 11:39:38 -07:00
|
|
|
if (!gUseDoubleBufferedWindows) {
|
2012-05-10 12:56:42 -07:00
|
|
|
attribs[2] = 0;
|
2010-09-21 11:39:38 -07:00
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
mPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
|
|
|
|
}
|
|
|
|
|
|
|
|
return mPixelFormat;
|
|
|
|
}
|
2010-04-27 15:29:29 -07:00
|
|
|
private:
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mInitialized;
|
2010-04-27 15:29:29 -07:00
|
|
|
PRLibrary *mOGLLibrary;
|
2010-07-18 22:01:14 -07:00
|
|
|
NSOpenGLPixelFormat *mPixelFormat;
|
2014-03-03 18:47:43 -08:00
|
|
|
};
|
2010-04-27 15:29:29 -07:00
|
|
|
|
|
|
|
CGLLibrary sCGLLibrary;
|
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
GLContextCGL::GLContextCGL(
|
|
|
|
const SurfaceCaps& caps,
|
|
|
|
GLContext *shareContext,
|
|
|
|
NSOpenGLContext *context,
|
|
|
|
bool isOffscreen)
|
|
|
|
: GLContext(caps, shareContext, isOffscreen),
|
|
|
|
mContext(context)
|
2010-04-27 15:29:29 -07:00
|
|
|
{
|
2014-01-07 12:02:18 -08:00
|
|
|
SetProfileVersion(ContextProfile::OpenGLCompatibility, 210);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
return gUseDoubleBufferedWindows;
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
PROFILER_LABEL("GLContext", "SwapBuffers");
|
|
|
|
[mContext flushBuffer];
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-18 22:01:14 -07:00
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
static GLContextCGL *
|
|
|
|
GetGlobalContextCGL()
|
|
|
|
{
|
|
|
|
return static_cast<GLContextCGL*>(GLContextProviderCGL::GetGlobalContext());
|
|
|
|
}
|
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
already_AddRefed<GLContext>
|
2014-04-15 07:57:26 -07:00
|
|
|
GLContextProviderCGL::CreateWrappingExisting(void*, void*)
|
2010-04-27 15:29:29 -07:00
|
|
|
{
|
2014-04-15 07:57:26 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
2010-04-27 15:29:29 -07:00
|
|
|
|
2014-04-15 07:57:26 -07:00
|
|
|
already_AddRefed<GLContext>
|
|
|
|
GLContextProviderCGL::CreateForWindow(nsIWidget *aWidget)
|
|
|
|
{
|
2010-07-18 22:01:14 -07:00
|
|
|
GLContextCGL *shareContext = GetGlobalContextCGL();
|
|
|
|
|
2013-03-11 09:29:00 -07:00
|
|
|
NSOpenGLContext *context = [[NSOpenGLContext alloc]
|
2010-07-18 22:01:14 -07:00
|
|
|
initWithFormat:sCGLLibrary.PixelFormat()
|
|
|
|
shareContext:(shareContext ? shareContext->mContext : NULL)];
|
|
|
|
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();
|
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(caps,
|
2010-07-18 22:01:14 -07:00
|
|
|
shareContext,
|
|
|
|
context);
|
2010-04-27 15:29:29 -07:00
|
|
|
if (!glContext->Init()) {
|
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>
|
2013-02-13 15:26:24 -08:00
|
|
|
CreateOffscreenFBOContext(bool aShare = true)
|
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
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
GLContextCGL *shareContext = aShare ? GetGlobalContextCGL() : nullptr;
|
2010-07-18 22:01:14 -07:00
|
|
|
if (aShare && !shareContext) {
|
|
|
|
// if there is no share context, then we can't use FBOs.
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-07-18 22:01:14 -07:00
|
|
|
}
|
2010-05-17 21:04:22 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
NSOpenGLContext *context = [[NSOpenGLContext alloc]
|
|
|
|
initWithFormat:sCGLLibrary.PixelFormat()
|
2014-01-07 12:02:18 -08:00
|
|
|
shareContext:shareContext ? shareContext->GetNSOpenGLContext() : NULL];
|
2010-07-18 22:01:14 -07:00
|
|
|
if (!context) {
|
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();
|
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(dummyCaps, shareContext, context, true);
|
2012-03-12 15:10:38 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<GLContext>
|
2013-02-13 15:26:24 -08:00
|
|
|
GLContextProviderCGL::CreateOffscreen(const gfxIntSize& size,
|
2014-01-10 10:55:23 -08:00
|
|
|
const SurfaceCaps& caps)
|
2010-07-18 22:01:14 -07:00
|
|
|
{
|
2013-02-13 15:26:24 -08:00
|
|
|
nsRefPtr<GLContextCGL> glContext = CreateOffscreenFBOContext();
|
2010-07-18 22:01:14 -07:00
|
|
|
if (glContext &&
|
|
|
|
glContext->Init() &&
|
2014-01-01 11:47:19 -08:00
|
|
|
glContext->InitOffscreen(ToIntSize(size), caps))
|
2010-07-18 22:01:14 -07:00
|
|
|
{
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything failed
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
static nsRefPtr<GLContext> gGlobalContext;
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
gGlobalContext = nullptr;
|
2010-07-19 21:05:42 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|