2010-04-28 02:29:29 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04: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-28 02:29:29 +04: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 13:24:31 +04:00
|
|
|
#include "gfxASurface.h"
|
2010-07-01 20:30:38 +04:00
|
|
|
#include "gfxImageSurface.h"
|
2010-11-11 23:31:23 +03:00
|
|
|
#include "gfxQuartzSurface.h"
|
2010-07-01 20:30:38 +04:00
|
|
|
#include "gfxPlatform.h"
|
2011-02-07 23:15:46 +03:00
|
|
|
#include "gfxFailure.h"
|
2010-09-21 22:39:38 +04:00
|
|
|
#include "prenv.h"
|
2011-10-19 23:09:57 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-03-18 18:25:50 +04:00
|
|
|
#include "GeckoProfiler.h"
|
2010-04-28 02:29:29 +04:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2010-04-28 02:29:29 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gUseDoubleBufferedWindows = true;
|
2010-09-21 22:39:38 +04:00
|
|
|
|
2010-04-28 02:29:29 +04:00
|
|
|
class CGLLibrary
|
|
|
|
{
|
|
|
|
public:
|
2010-07-19 09:01:14 +04:00
|
|
|
CGLLibrary()
|
2011-10-17 18:59:28 +04:00
|
|
|
: mInitialized(false),
|
2012-07-30 18:20:58 +04:00
|
|
|
mOGLLibrary(nullptr),
|
|
|
|
mPixelFormat(nullptr)
|
2010-07-19 09:01:14 +04:00
|
|
|
{ }
|
2010-04-28 02:29:29 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool EnsureInitialized()
|
2010-04-28 02:29:29 +04:00
|
|
|
{
|
|
|
|
if (mInitialized) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
if (!mOGLLibrary) {
|
|
|
|
mOGLLibrary = PR_LoadLibrary("/System/Library/Frameworks/OpenGL.framework/OpenGL");
|
|
|
|
if (!mOGLLibrary) {
|
|
|
|
NS_WARNING("Couldn't load OpenGL Framework.");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
}
|
2010-09-21 22:39:38 +04:00
|
|
|
|
|
|
|
const char* db = PR_GetEnv("MOZ_CGL_DB");
|
|
|
|
gUseDoubleBufferedWindows = (!db || *db != '0');
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = true;
|
|
|
|
return true;
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
NSOpenGLPixelFormat *PixelFormat()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mPixelFormat == nullptr) {
|
2010-07-19 09:01:14 +04:00
|
|
|
NSOpenGLPixelFormatAttribute attribs[] = {
|
|
|
|
NSOpenGLPFAAccelerated,
|
2011-09-21 23:20:40 +04:00
|
|
|
NSOpenGLPFAAllowOfflineRenderers,
|
2010-09-21 22:39:38 +04:00
|
|
|
NSOpenGLPFADoubleBuffer,
|
2012-05-10 23:56:42 +04:00
|
|
|
0
|
2010-07-19 09:01:14 +04:00
|
|
|
};
|
|
|
|
|
2010-09-21 22:39:38 +04:00
|
|
|
if (!gUseDoubleBufferedWindows) {
|
2012-05-10 23:56:42 +04:00
|
|
|
attribs[2] = 0;
|
2010-09-21 22:39:38 +04:00
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
mPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
|
|
|
|
}
|
|
|
|
|
|
|
|
return mPixelFormat;
|
|
|
|
}
|
2010-04-28 02:29:29 +04:00
|
|
|
private:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mInitialized;
|
2010-04-28 02:29:29 +04:00
|
|
|
PRLibrary *mOGLLibrary;
|
2010-07-19 09:01:14 +04:00
|
|
|
NSOpenGLPixelFormat *mPixelFormat;
|
2010-04-28 02:29:29 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
CGLLibrary sCGLLibrary;
|
|
|
|
|
|
|
|
class GLContextCGL : public GLContext
|
|
|
|
{
|
2010-07-19 09:01:14 +04:00
|
|
|
friend class GLContextProviderCGL;
|
|
|
|
|
2010-04-28 02:29:29 +04:00
|
|
|
public:
|
2013-02-14 03:26:24 +04:00
|
|
|
GLContextCGL(const SurfaceCaps& caps,
|
|
|
|
GLContext *shareContext,
|
|
|
|
NSOpenGLContext *context,
|
|
|
|
bool isOffscreen = false)
|
|
|
|
: GLContext(caps, shareContext, isOffscreen),
|
|
|
|
mContext(context),
|
2013-02-21 23:54:25 +04:00
|
|
|
mTempTextureName(0)
|
2013-02-14 03:26:24 +04:00
|
|
|
{}
|
2010-04-28 02:29:29 +04:00
|
|
|
|
|
|
|
~GLContextCGL()
|
|
|
|
{
|
2010-09-21 23:41:24 +04:00
|
|
|
MarkDestroyed();
|
2010-07-19 09:01:14 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
if (mContext)
|
|
|
|
[mContext release];
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GLContextType GetContextType() {
|
|
|
|
return ContextTypeCGL;
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Init()
|
2010-04-28 02:29:29 +04:00
|
|
|
{
|
|
|
|
MakeCurrent();
|
2012-03-13 02:10:38 +04:00
|
|
|
if (!InitWithPrefix("gl", true))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
void *GetNativeData(NativeDataType aType)
|
2010-04-28 02:29:29 +04:00
|
|
|
{
|
2010-05-18 08:04:22 +04:00
|
|
|
switch (aType) {
|
|
|
|
case NativeGLContext:
|
|
|
|
return mContext;
|
|
|
|
|
|
|
|
default:
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-05-18 08:04:22 +04:00
|
|
|
}
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
|
2012-05-13 03:23:56 +04:00
|
|
|
bool MakeCurrentImpl(bool aForce = false)
|
2010-04-28 02:29:29 +04:00
|
|
|
{
|
2012-05-13 03:23:56 +04:00
|
|
|
if (!aForce && [NSOpenGLContext currentContext] == mContext) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
if (mContext) {
|
|
|
|
[mContext makeCurrentContext];
|
2012-05-24 08:40:40 +04:00
|
|
|
GLint swapInt = 1;
|
|
|
|
[mContext setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
|
2010-05-18 08:04:22 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 07:30:20 +04:00
|
|
|
virtual bool IsCurrent() {
|
|
|
|
return [NSOpenGLContext currentContext] == mContext;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool SetupLookupFunction()
|
2010-04-28 02:29:29 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsDoubleBuffered()
|
2010-09-21 22:39:38 +04:00
|
|
|
{
|
|
|
|
return gUseDoubleBufferedWindows;
|
|
|
|
}
|
|
|
|
|
2011-11-19 07:57:29 +04:00
|
|
|
bool SupportsRobustness()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool SwapBuffers()
|
2010-09-21 22:39:38 +04:00
|
|
|
{
|
2013-03-16 08:47:02 +04:00
|
|
|
PROFILER_LABEL("GLContext", "SwapBuffers");
|
2010-09-21 22:39:38 +04:00
|
|
|
[mContext flushBuffer];
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-09-21 22:39:38 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ResizeOffscreen(const gfxIntSize& aNewSize);
|
2010-07-19 09:01:14 +04:00
|
|
|
|
2010-07-01 20:30:38 +04:00
|
|
|
virtual already_AddRefed<TextureImage>
|
|
|
|
CreateBasicTextureImage(GLuint aTexture,
|
|
|
|
const nsIntSize& aSize,
|
2010-11-12 23:02:20 +03:00
|
|
|
GLenum aWrapMode,
|
2010-07-01 20:30:38 +04:00
|
|
|
TextureImage::ContentType aContentType,
|
2012-05-10 00:55:31 +04:00
|
|
|
GLContext* aContext,
|
|
|
|
TextureImage::Flags aFlags = TextureImage::NoFlags);
|
2010-07-01 20:30:38 +04:00
|
|
|
|
2010-04-28 02:29:29 +04:00
|
|
|
NSOpenGLContext *mContext;
|
2010-07-19 09:01:14 +04:00
|
|
|
GLuint mTempTextureName;
|
2010-04-28 02:29:29 +04:00
|
|
|
};
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextCGL::ResizeOffscreen(const gfxIntSize& aNewSize)
|
|
|
|
{
|
2013-02-14 03:26:24 +04:00
|
|
|
return ResizeScreenBuffer(aNewSize);
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
2010-07-01 20:30:38 +04:00
|
|
|
class TextureImageCGL : public BasicTextureImage
|
|
|
|
{
|
|
|
|
friend already_AddRefed<TextureImage>
|
|
|
|
GLContextCGL::CreateBasicTextureImage(GLuint,
|
|
|
|
const nsIntSize&,
|
2010-11-12 23:02:20 +03:00
|
|
|
GLenum,
|
2010-07-01 20:30:38 +04:00
|
|
|
TextureImage::ContentType,
|
2012-05-10 00:55:31 +04:00
|
|
|
GLContext*,
|
|
|
|
TextureImage::Flags);
|
2010-12-17 10:49:42 +03:00
|
|
|
public:
|
|
|
|
~TextureImageCGL()
|
|
|
|
{
|
|
|
|
if (mPixelBuffer) {
|
2011-01-03 20:01:19 +03:00
|
|
|
mGLContext->MakeCurrent();
|
2010-12-17 10:49:42 +03:00
|
|
|
mGLContext->fDeleteBuffers(1, &mPixelBuffer);
|
|
|
|
}
|
|
|
|
}
|
2010-07-01 20:30:38 +04:00
|
|
|
|
|
|
|
protected:
|
2010-12-17 10:49:42 +03:00
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
GetSurfaceForUpdate(const gfxIntSize& aSize, ImageFormat aFmt)
|
2010-11-11 23:31:23 +03:00
|
|
|
{
|
2012-04-27 04:24:53 +04:00
|
|
|
gfxIntSize size(aSize.width + 1, aSize.height + 1);
|
2011-01-03 20:01:19 +03:00
|
|
|
mGLContext->MakeCurrent();
|
2011-01-18 00:47:18 +03:00
|
|
|
if (!mGLContext->
|
|
|
|
IsExtensionSupported(GLContext::ARB_pixel_buffer_object))
|
|
|
|
{
|
2010-12-17 10:49:42 +03:00
|
|
|
return gfxPlatform::GetPlatform()->
|
2012-04-27 04:24:53 +04:00
|
|
|
CreateOffscreenSurface(size,
|
2011-01-18 00:47:18 +03:00
|
|
|
gfxASurface::ContentFromFormat(aFmt));
|
2010-11-11 23:31:23 +03:00
|
|
|
}
|
|
|
|
|
2010-12-17 10:49:42 +03:00
|
|
|
if (!mPixelBuffer) {
|
|
|
|
mGLContext->fGenBuffers(1, &mPixelBuffer);
|
2010-12-17 10:29:23 +03:00
|
|
|
}
|
2010-12-17 10:49:42 +03:00
|
|
|
mGLContext->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, mPixelBuffer);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t length = size.width * 4 * size.height;
|
2010-12-17 10:29:23 +03:00
|
|
|
|
2012-04-27 04:24:53 +04:00
|
|
|
if (length > mPixelBufferSize) {
|
|
|
|
mGLContext->fBufferData(LOCAL_GL_PIXEL_UNPACK_BUFFER, length,
|
2010-12-17 10:49:42 +03:00
|
|
|
NULL, LOCAL_GL_STREAM_DRAW);
|
2012-04-27 04:24:53 +04:00
|
|
|
mPixelBufferSize = length;
|
2010-11-11 23:31:23 +03:00
|
|
|
}
|
2010-12-17 10:49:42 +03:00
|
|
|
unsigned char* data =
|
2011-01-18 00:47:18 +03:00
|
|
|
(unsigned char*)mGLContext->
|
|
|
|
fMapBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
LOCAL_GL_WRITE_ONLY);
|
2010-11-11 23:31:23 +03:00
|
|
|
|
2011-01-18 10:22:25 +03:00
|
|
|
mGLContext->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
|
2010-12-17 10:49:42 +03:00
|
|
|
if (!data) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString failure;
|
2011-02-07 23:15:46 +03:00
|
|
|
failure += "Pixel buffer binding failed: ";
|
2012-04-27 04:24:53 +04:00
|
|
|
failure.AppendPrintf("%dx%d\n", size.width, size.height);
|
2011-02-07 23:15:46 +03:00
|
|
|
gfx::LogFailure(failure);
|
|
|
|
|
2011-01-18 00:47:18 +03:00
|
|
|
mGLContext->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
return gfxPlatform::GetPlatform()->
|
2012-04-27 04:24:53 +04:00
|
|
|
CreateOffscreenSurface(size,
|
2011-01-18 00:47:18 +03:00
|
|
|
gfxASurface::ContentFromFormat(aFmt));
|
2010-11-13 02:34:36 +03:00
|
|
|
}
|
2010-11-11 23:31:23 +03:00
|
|
|
|
2010-12-17 10:49:42 +03:00
|
|
|
nsRefPtr<gfxQuartzSurface> surf =
|
2012-04-27 04:24:53 +04:00
|
|
|
new gfxQuartzSurface(data, size, size.width * 4, aFmt);
|
2010-11-11 23:31:23 +03:00
|
|
|
|
2011-01-18 00:47:18 +03:00
|
|
|
mBoundPixelBuffer = true;
|
2010-12-17 10:49:42 +03:00
|
|
|
return surf.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FinishedSurfaceUpdate()
|
2010-11-11 23:31:23 +03:00
|
|
|
{
|
2011-01-18 00:47:18 +03:00
|
|
|
if (mBoundPixelBuffer) {
|
2011-01-03 20:01:19 +03:00
|
|
|
mGLContext->MakeCurrent();
|
2011-01-18 10:22:25 +03:00
|
|
|
mGLContext->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, mPixelBuffer);
|
2010-12-17 10:49:42 +03:00
|
|
|
mGLContext->fUnmapBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-11-11 23:31:23 +03:00
|
|
|
}
|
|
|
|
|
2010-12-17 10:49:42 +03:00
|
|
|
void FinishedSurfaceUpload()
|
2010-07-01 20:30:38 +04:00
|
|
|
{
|
2011-01-18 00:47:18 +03:00
|
|
|
if (mBoundPixelBuffer) {
|
2011-01-03 20:01:19 +03:00
|
|
|
mGLContext->MakeCurrent();
|
2010-12-17 10:49:42 +03:00
|
|
|
mGLContext->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, 0);
|
2011-01-18 00:47:18 +03:00
|
|
|
mBoundPixelBuffer = false;
|
2010-12-17 10:49:42 +03:00
|
|
|
}
|
2010-07-01 20:30:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TextureImageCGL(GLuint aTexture,
|
|
|
|
const nsIntSize& aSize,
|
2010-11-12 23:02:20 +03:00
|
|
|
GLenum aWrapMode,
|
2010-07-01 20:30:38 +04:00
|
|
|
ContentType aContentType,
|
2012-05-10 00:55:31 +04:00
|
|
|
GLContext* aContext,
|
|
|
|
TextureImage::Flags aFlags = TextureImage::NoFlags)
|
|
|
|
: BasicTextureImage(aTexture, aSize, aWrapMode, aContentType, aContext, aFlags)
|
2010-12-17 10:49:42 +03:00
|
|
|
, mPixelBuffer(0)
|
|
|
|
, mPixelBufferSize(0)
|
2011-01-18 00:47:18 +03:00
|
|
|
, mBoundPixelBuffer(false)
|
2010-07-01 20:30:38 +04:00
|
|
|
{}
|
2010-12-17 10:49:42 +03:00
|
|
|
|
|
|
|
GLuint mPixelBuffer;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mPixelBufferSize;
|
2011-01-18 00:47:18 +03:00
|
|
|
bool mBoundPixelBuffer;
|
2010-07-01 20:30:38 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<TextureImage>
|
|
|
|
GLContextCGL::CreateBasicTextureImage(GLuint aTexture,
|
|
|
|
const nsIntSize& aSize,
|
2010-11-12 23:02:20 +03:00
|
|
|
GLenum aWrapMode,
|
2010-07-01 20:30:38 +04:00
|
|
|
TextureImage::ContentType aContentType,
|
2012-05-10 00:55:31 +04:00
|
|
|
GLContext* aContext,
|
|
|
|
TextureImage::Flags aFlags)
|
2010-07-01 20:30:38 +04:00
|
|
|
{
|
2010-11-12 23:02:20 +03:00
|
|
|
nsRefPtr<TextureImageCGL> teximage
|
2012-05-10 00:55:31 +04:00
|
|
|
(new TextureImageCGL(aTexture, aSize, aWrapMode, aContentType, aContext, aFlags));
|
2010-07-01 20:30:38 +04:00
|
|
|
return teximage.forget();
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
static GLContextCGL *
|
|
|
|
GetGlobalContextCGL()
|
|
|
|
{
|
|
|
|
return static_cast<GLContextCGL*>(GLContextProviderCGL::GetGlobalContext());
|
|
|
|
}
|
|
|
|
|
2010-04-28 02:29:29 +04:00
|
|
|
already_AddRefed<GLContext>
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextProviderCGL::CreateForWindow(nsIWidget *aWidget)
|
2010-04-28 02:29:29 +04:00
|
|
|
{
|
|
|
|
if (!sCGLLibrary.EnsureInitialized()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextCGL *shareContext = GetGlobalContextCGL();
|
|
|
|
|
2013-03-11 20:29:00 +04:00
|
|
|
NSOpenGLContext *context = [[NSOpenGLContext alloc]
|
2010-07-19 09:01:14 +04:00
|
|
|
initWithFormat:sCGLLibrary.PixelFormat()
|
|
|
|
shareContext:(shareContext ? shareContext->mContext : NULL)];
|
|
|
|
if (!context) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
|
2010-09-21 20:30:19 +04:00
|
|
|
// make the context transparent
|
2013-03-11 20:29:00 +04:00
|
|
|
GLint opaque = 0;
|
|
|
|
[context setValues:&opaque forParameter:NSOpenGLCPSurfaceOpacity];
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
SurfaceCaps caps = SurfaceCaps::ForRGBA();
|
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(caps,
|
2010-07-19 09:01:14 +04:00
|
|
|
shareContext,
|
|
|
|
context);
|
2010-04-28 02:29:29 +04:00
|
|
|
if (!glContext->Init()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-03-13 02:10:38 +04:00
|
|
|
}
|
2010-04-28 02:29:29 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
return glContext.forget();
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
static already_AddRefed<GLContextCGL>
|
2013-02-14 03:26:24 +04:00
|
|
|
CreateOffscreenFBOContext(bool aShare = true)
|
2010-07-19 09:01:14 +04:00
|
|
|
{
|
|
|
|
if (!sCGLLibrary.EnsureInitialized()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-05-18 08:04:22 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
GLContextCGL *shareContext = aShare ? GetGlobalContextCGL() : nullptr;
|
2010-07-19 09:01:14 +04:00
|
|
|
if (aShare && !shareContext) {
|
|
|
|
// if there is no share context, then we can't use FBOs.
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
NSOpenGLContext *context = [[NSOpenGLContext alloc]
|
|
|
|
initWithFormat:sCGLLibrary.PixelFormat()
|
|
|
|
shareContext:shareContext ? shareContext->mContext : NULL];
|
|
|
|
if (!context) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-05-18 08:04:22 +04:00
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
SurfaceCaps dummyCaps = SurfaceCaps::Any();
|
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(dummyCaps, shareContext, context, true);
|
2012-03-13 02:10:38 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<GLContext>
|
2013-02-14 03:26:24 +04:00
|
|
|
GLContextProviderCGL::CreateOffscreen(const gfxIntSize& size,
|
|
|
|
const SurfaceCaps& caps,
|
2012-03-22 03:13:59 +04:00
|
|
|
const ContextFlags flags)
|
2010-07-19 09:01:14 +04:00
|
|
|
{
|
2013-02-14 03:26:24 +04:00
|
|
|
nsRefPtr<GLContextCGL> glContext = CreateOffscreenFBOContext();
|
2010-07-19 09:01:14 +04:00
|
|
|
if (glContext &&
|
|
|
|
glContext->Init() &&
|
2013-02-14 03:26:24 +04:00
|
|
|
glContext->InitOffscreen(size, caps))
|
2010-07-19 09:01:14 +04:00
|
|
|
{
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything failed
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
static nsRefPtr<GLContext> gGlobalContext;
|
|
|
|
|
|
|
|
GLContext *
|
2012-06-02 20:05:45 +04:00
|
|
|
GLContextProviderCGL::GetGlobalContext(const ContextFlags)
|
2010-07-19 09:01:14 +04:00
|
|
|
{
|
|
|
|
if (!sCGLLibrary.EnsureInitialized()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-07-19 09:01:14 +04: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-14 03:26:24 +04:00
|
|
|
gGlobalContext = CreateOffscreenFBOContext(false);
|
2010-09-10 20:19:09 +04:00
|
|
|
if (!gGlobalContext || !static_cast<GLContextCGL*>(gGlobalContext.get())->Init()) {
|
2010-07-25 04:10:58 +04:00
|
|
|
NS_WARNING("Couldn't init gGlobalContext.");
|
2012-07-30 18:20:58 +04:00
|
|
|
gGlobalContext = nullptr;
|
|
|
|
return nullptr;
|
2010-07-25 04:10:58 +04:00
|
|
|
}
|
2010-08-07 09:09:18 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
gGlobalContext->SetIsGlobalSharedContext(true);
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return gGlobalContext;
|
|
|
|
}
|
|
|
|
|
2013-04-17 06:21:06 +04:00
|
|
|
SharedTextureHandle
|
|
|
|
GLContextProviderCGL::CreateSharedHandle(GLContext::SharedTextureShareType shareType,
|
|
|
|
void* buffer,
|
|
|
|
GLContext::SharedTextureBufferType bufferType)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-20 08:05:42 +04:00
|
|
|
void
|
|
|
|
GLContextProviderCGL::Shutdown()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
gGlobalContext = nullptr;
|
2010-07-20 08:05:42 +04:00
|
|
|
}
|
|
|
|
|
2010-04-28 02:29:29 +04:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|