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"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "OpenGL/OpenGL.h"
|
|
|
|
#include <OpenGL/gl.h>
|
|
|
|
#include <AppKit/NSOpenGL.h>
|
2010-06-23 02:24:31 -07:00
|
|
|
#include "gfxASurface.h"
|
2010-07-01 09:30:38 -07:00
|
|
|
#include "gfxImageSurface.h"
|
2010-11-11 12:31:23 -08:00
|
|
|
#include "gfxQuartzSurface.h"
|
2010-07-01 09:30:38 -07:00
|
|
|
#include "gfxPlatform.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:10:30 -07:00
|
|
|
#include "sampler.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;
|
2010-04-27 15:29:29 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
CGLLibrary sCGLLibrary;
|
|
|
|
|
|
|
|
class GLContextCGL : public GLContext
|
|
|
|
{
|
2010-07-18 22:01:14 -07:00
|
|
|
friend class GLContextProviderCGL;
|
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
public:
|
2013-02-13 15:26:24 -08:00
|
|
|
GLContextCGL(const SurfaceCaps& caps,
|
|
|
|
GLContext *shareContext,
|
|
|
|
NSOpenGLContext *context,
|
|
|
|
bool isOffscreen = false)
|
|
|
|
: GLContext(caps, shareContext, isOffscreen),
|
|
|
|
mContext(context),
|
2013-02-21 11:54:25 -08:00
|
|
|
mTempTextureName(0)
|
2013-02-13 15:26:24 -08:00
|
|
|
{}
|
2010-04-27 15:29:29 -07:00
|
|
|
|
|
|
|
~GLContextCGL()
|
|
|
|
{
|
2010-09-21 12:41:24 -07:00
|
|
|
MarkDestroyed();
|
2010-07-18 22:01:14 -07:00
|
|
|
|
2010-05-17 21:04:22 -07:00
|
|
|
if (mContext)
|
|
|
|
[mContext release];
|
2010-07-18 22:01:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
GLContextType GetContextType() {
|
|
|
|
return ContextTypeCGL;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Init()
|
2010-04-27 15:29:29 -07:00
|
|
|
{
|
|
|
|
MakeCurrent();
|
2012-03-12 15:10:38 -07:00
|
|
|
if (!InitWithPrefix("gl", true))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2010-05-17 21:04:22 -07:00
|
|
|
void *GetNativeData(NativeDataType aType)
|
2010-04-27 15:29:29 -07:00
|
|
|
{
|
2010-05-17 21:04:22 -07:00
|
|
|
switch (aType) {
|
|
|
|
case NativeGLContext:
|
|
|
|
return mContext;
|
|
|
|
|
|
|
|
default:
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-17 21:04:22 -07:00
|
|
|
}
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2012-05-12 16:23:56 -07:00
|
|
|
bool MakeCurrentImpl(bool aForce = false)
|
2010-04-27 15:29:29 -07:00
|
|
|
{
|
2012-05-12 16:23:56 -07:00
|
|
|
if (!aForce && [NSOpenGLContext currentContext] == mContext) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-17 21:04:22 -07:00
|
|
|
if (mContext) {
|
|
|
|
[mContext makeCurrentContext];
|
2012-05-23 21:40:40 -07:00
|
|
|
GLint swapInt = 1;
|
|
|
|
[mContext setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
|
2010-05-17 21:04:22 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2012-08-21 20:30:20 -07:00
|
|
|
virtual bool IsCurrent() {
|
|
|
|
return [NSOpenGLContext currentContext] == mContext;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool SetupLookupFunction()
|
2010-04-27 15:29:29 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsDoubleBuffered()
|
2010-09-21 11:39:38 -07:00
|
|
|
{
|
|
|
|
return gUseDoubleBufferedWindows;
|
|
|
|
}
|
|
|
|
|
2011-11-18 19:57:29 -08:00
|
|
|
bool SupportsRobustness()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool SwapBuffers()
|
2010-09-21 11:39:38 -07:00
|
|
|
{
|
2013-03-18 07:10:35 -07:00
|
|
|
SAMPLE_LABEL("GLContext", "SwapBuffers");
|
2010-09-21 11:39:38 -07:00
|
|
|
[mContext flushBuffer];
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-09-21 11:39:38 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool ResizeOffscreen(const gfxIntSize& aNewSize);
|
2010-07-18 22:01:14 -07:00
|
|
|
|
2010-07-01 09:30:38 -07:00
|
|
|
virtual already_AddRefed<TextureImage>
|
|
|
|
CreateBasicTextureImage(GLuint aTexture,
|
|
|
|
const nsIntSize& aSize,
|
2010-11-12 12:02:20 -08:00
|
|
|
GLenum aWrapMode,
|
2010-07-01 09:30:38 -07:00
|
|
|
TextureImage::ContentType aContentType,
|
2012-05-09 13:55:31 -07:00
|
|
|
GLContext* aContext,
|
|
|
|
TextureImage::Flags aFlags = TextureImage::NoFlags);
|
2010-07-01 09:30:38 -07:00
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
NSOpenGLContext *mContext;
|
2010-07-18 22:01:14 -07:00
|
|
|
GLuint mTempTextureName;
|
2010-04-27 15:29:29 -07:00
|
|
|
};
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-07-18 22:01:14 -07:00
|
|
|
GLContextCGL::ResizeOffscreen(const gfxIntSize& aNewSize)
|
|
|
|
{
|
2013-02-13 15:26:24 -08:00
|
|
|
return ResizeScreenBuffer(aNewSize);
|
2010-07-18 22:01:14 -07:00
|
|
|
}
|
|
|
|
|
2010-07-01 09:30:38 -07:00
|
|
|
class TextureImageCGL : public BasicTextureImage
|
|
|
|
{
|
|
|
|
friend already_AddRefed<TextureImage>
|
|
|
|
GLContextCGL::CreateBasicTextureImage(GLuint,
|
|
|
|
const nsIntSize&,
|
2010-11-12 12:02:20 -08:00
|
|
|
GLenum,
|
2010-07-01 09:30:38 -07:00
|
|
|
TextureImage::ContentType,
|
2012-05-09 13:55:31 -07:00
|
|
|
GLContext*,
|
|
|
|
TextureImage::Flags);
|
2010-12-16 23:49:42 -08:00
|
|
|
public:
|
|
|
|
~TextureImageCGL()
|
|
|
|
{
|
|
|
|
if (mPixelBuffer) {
|
2011-01-03 09:01:19 -08:00
|
|
|
mGLContext->MakeCurrent();
|
2010-12-16 23:49:42 -08:00
|
|
|
mGLContext->fDeleteBuffers(1, &mPixelBuffer);
|
|
|
|
}
|
|
|
|
}
|
2010-07-01 09:30:38 -07:00
|
|
|
|
|
|
|
protected:
|
2010-12-16 23:49:42 -08:00
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
GetSurfaceForUpdate(const gfxIntSize& aSize, ImageFormat aFmt)
|
2010-11-11 12:31:23 -08:00
|
|
|
{
|
2012-04-26 17:24:53 -07:00
|
|
|
gfxIntSize size(aSize.width + 1, aSize.height + 1);
|
2011-01-03 09:01:19 -08:00
|
|
|
mGLContext->MakeCurrent();
|
2011-01-17 13:47:18 -08:00
|
|
|
if (!mGLContext->
|
|
|
|
IsExtensionSupported(GLContext::ARB_pixel_buffer_object))
|
|
|
|
{
|
2010-12-16 23:49:42 -08:00
|
|
|
return gfxPlatform::GetPlatform()->
|
2012-04-26 17:24:53 -07:00
|
|
|
CreateOffscreenSurface(size,
|
2011-01-17 13:47:18 -08:00
|
|
|
gfxASurface::ContentFromFormat(aFmt));
|
2010-11-11 12:31:23 -08:00
|
|
|
}
|
|
|
|
|
2010-12-16 23:49:42 -08:00
|
|
|
if (!mPixelBuffer) {
|
|
|
|
mGLContext->fGenBuffers(1, &mPixelBuffer);
|
2010-12-16 23:29:23 -08:00
|
|
|
}
|
2010-12-16 23:49:42 -08:00
|
|
|
mGLContext->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, mPixelBuffer);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t length = size.width * 4 * size.height;
|
2010-12-16 23:29:23 -08:00
|
|
|
|
2012-04-26 17:24:53 -07:00
|
|
|
if (length > mPixelBufferSize) {
|
|
|
|
mGLContext->fBufferData(LOCAL_GL_PIXEL_UNPACK_BUFFER, length,
|
2010-12-16 23:49:42 -08:00
|
|
|
NULL, LOCAL_GL_STREAM_DRAW);
|
2012-04-26 17:24:53 -07:00
|
|
|
mPixelBufferSize = length;
|
2010-11-11 12:31:23 -08:00
|
|
|
}
|
2010-12-16 23:49:42 -08:00
|
|
|
unsigned char* data =
|
2011-01-17 13:47:18 -08:00
|
|
|
(unsigned char*)mGLContext->
|
|
|
|
fMapBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
LOCAL_GL_WRITE_ONLY);
|
2010-11-11 12:31:23 -08:00
|
|
|
|
2011-01-17 23:22:25 -08:00
|
|
|
mGLContext->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
|
2010-12-16 23:49:42 -08:00
|
|
|
if (!data) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString failure;
|
2011-02-07 12:15:46 -08:00
|
|
|
failure += "Pixel buffer binding failed: ";
|
2012-04-26 17:24:53 -07:00
|
|
|
failure.AppendPrintf("%dx%d\n", size.width, size.height);
|
2011-02-07 12:15:46 -08:00
|
|
|
gfx::LogFailure(failure);
|
|
|
|
|
2011-01-17 13:47:18 -08:00
|
|
|
mGLContext->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
return gfxPlatform::GetPlatform()->
|
2012-04-26 17:24:53 -07:00
|
|
|
CreateOffscreenSurface(size,
|
2011-01-17 13:47:18 -08:00
|
|
|
gfxASurface::ContentFromFormat(aFmt));
|
2010-11-12 15:34:36 -08:00
|
|
|
}
|
2010-11-11 12:31:23 -08:00
|
|
|
|
2010-12-16 23:49:42 -08:00
|
|
|
nsRefPtr<gfxQuartzSurface> surf =
|
2012-04-26 17:24:53 -07:00
|
|
|
new gfxQuartzSurface(data, size, size.width * 4, aFmt);
|
2010-11-11 12:31:23 -08:00
|
|
|
|
2011-01-17 13:47:18 -08:00
|
|
|
mBoundPixelBuffer = true;
|
2010-12-16 23:49:42 -08:00
|
|
|
return surf.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FinishedSurfaceUpdate()
|
2010-11-11 12:31:23 -08:00
|
|
|
{
|
2011-01-17 13:47:18 -08:00
|
|
|
if (mBoundPixelBuffer) {
|
2011-01-03 09:01:19 -08:00
|
|
|
mGLContext->MakeCurrent();
|
2011-01-17 23:22:25 -08:00
|
|
|
mGLContext->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, mPixelBuffer);
|
2010-12-16 23:49:42 -08:00
|
|
|
mGLContext->fUnmapBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-11-11 12:31:23 -08:00
|
|
|
}
|
|
|
|
|
2010-12-16 23:49:42 -08:00
|
|
|
void FinishedSurfaceUpload()
|
2010-07-01 09:30:38 -07:00
|
|
|
{
|
2011-01-17 13:47:18 -08:00
|
|
|
if (mBoundPixelBuffer) {
|
2011-01-03 09:01:19 -08:00
|
|
|
mGLContext->MakeCurrent();
|
2010-12-16 23:49:42 -08:00
|
|
|
mGLContext->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, 0);
|
2011-01-17 13:47:18 -08:00
|
|
|
mBoundPixelBuffer = false;
|
2010-12-16 23:49:42 -08:00
|
|
|
}
|
2010-07-01 09:30:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TextureImageCGL(GLuint aTexture,
|
|
|
|
const nsIntSize& aSize,
|
2010-11-12 12:02:20 -08:00
|
|
|
GLenum aWrapMode,
|
2010-07-01 09:30:38 -07:00
|
|
|
ContentType aContentType,
|
2012-05-09 13:55:31 -07:00
|
|
|
GLContext* aContext,
|
|
|
|
TextureImage::Flags aFlags = TextureImage::NoFlags)
|
|
|
|
: BasicTextureImage(aTexture, aSize, aWrapMode, aContentType, aContext, aFlags)
|
2010-12-16 23:49:42 -08:00
|
|
|
, mPixelBuffer(0)
|
|
|
|
, mPixelBufferSize(0)
|
2011-01-17 13:47:18 -08:00
|
|
|
, mBoundPixelBuffer(false)
|
2010-07-01 09:30:38 -07:00
|
|
|
{}
|
2010-12-16 23:49:42 -08:00
|
|
|
|
|
|
|
GLuint mPixelBuffer;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mPixelBufferSize;
|
2011-01-17 13:47:18 -08:00
|
|
|
bool mBoundPixelBuffer;
|
2010-07-01 09:30:38 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<TextureImage>
|
|
|
|
GLContextCGL::CreateBasicTextureImage(GLuint aTexture,
|
|
|
|
const nsIntSize& aSize,
|
2010-11-12 12:02:20 -08:00
|
|
|
GLenum aWrapMode,
|
2010-07-01 09:30:38 -07:00
|
|
|
TextureImage::ContentType aContentType,
|
2012-05-09 13:55:31 -07:00
|
|
|
GLContext* aContext,
|
|
|
|
TextureImage::Flags aFlags)
|
2010-07-01 09:30:38 -07:00
|
|
|
{
|
2010-11-12 12:02:20 -08:00
|
|
|
nsRefPtr<TextureImageCGL> teximage
|
2012-05-09 13:55:31 -07:00
|
|
|
(new TextureImageCGL(aTexture, aSize, aWrapMode, aContentType, aContext, aFlags));
|
2010-07-01 09:30:38 -07:00
|
|
|
return teximage.forget();
|
|
|
|
}
|
|
|
|
|
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>
|
2010-07-18 22:01:14 -07:00
|
|
|
GLContextProviderCGL::CreateForWindow(nsIWidget *aWidget)
|
2010-04-27 15:29:29 -07:00
|
|
|
{
|
|
|
|
if (!sCGLLibrary.EnsureInitialized()) {
|
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
|
|
|
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()
|
|
|
|
shareContext:shareContext ? shareContext->mContext : NULL];
|
|
|
|
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,
|
|
|
|
const SurfaceCaps& caps,
|
2012-03-21 16:13:59 -07:00
|
|
|
const ContextFlags flags)
|
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() &&
|
2013-02-13 15:26:24 -08:00
|
|
|
glContext->InitOffscreen(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 *
|
2012-06-02 09:05:45 -07:00
|
|
|
GLContextProviderCGL::GetGlobalContext(const ContextFlags)
|
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;
|
|
|
|
return nullptr;
|
2010-07-24 17:10:58 -07:00
|
|
|
}
|
2010-08-06 22:09:18 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
gGlobalContext->SetIsGlobalSharedContext(true);
|
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 */
|