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"
|
2012-01-18 00:33:04 +04:00
|
|
|
#include "sampler.h"
|
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),
|
2010-07-19 09:01:14 +04:00
|
|
|
mOGLLibrary(nsnull),
|
|
|
|
mPixelFormat(nsnull)
|
|
|
|
{ }
|
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()
|
|
|
|
{
|
|
|
|
if (mPixelFormat == nsnull) {
|
|
|
|
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:
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextCGL(const ContextFormat& aFormat,
|
|
|
|
GLContext *aShareContext,
|
|
|
|
NSOpenGLContext *aContext,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aIsOffscreen = false)
|
2010-07-19 09:01:14 +04:00
|
|
|
: GLContext(aFormat, aIsOffscreen, aShareContext),
|
|
|
|
mContext(aContext),
|
|
|
|
mPBuffer(nsnull),
|
|
|
|
mTempTextureName(0)
|
2010-05-18 08:04:22 +04:00
|
|
|
{ }
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextCGL(const ContextFormat& aFormat,
|
|
|
|
GLContext *aShareContext,
|
|
|
|
NSOpenGLContext *aContext,
|
|
|
|
NSOpenGLPixelBuffer *aPixelBuffer)
|
2011-10-17 18:59:28 +04:00
|
|
|
: GLContext(aFormat, true, aShareContext),
|
2010-07-19 09:01:14 +04:00
|
|
|
mContext(aContext),
|
|
|
|
mPBuffer(aPixelBuffer),
|
|
|
|
mTempTextureName(0)
|
2010-05-18 08:04:22 +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];
|
|
|
|
|
|
|
|
if (mPBuffer)
|
2010-07-19 09:01:14 +04:00
|
|
|
[mPBuffer release];
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
InitFramebuffers();
|
|
|
|
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:
|
|
|
|
return nsnull;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-01-18 00:33:04 +04:00
|
|
|
SAMPLE_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 BindTex2DOffscreen(GLContext *aOffscreen);
|
2010-07-19 09:01:14 +04:00
|
|
|
void UnbindTex2DOffscreen(GLContext *aOffscreen);
|
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
|
|
|
NSOpenGLPixelBuffer *mPBuffer;
|
|
|
|
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::BindTex2DOffscreen(GLContext *aOffscreen)
|
|
|
|
{
|
|
|
|
if (aOffscreen->GetContextType() != ContextTypeCGL) {
|
|
|
|
NS_WARNING("non-CGL context");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aOffscreen->IsOffscreen()) {
|
|
|
|
NS_WARNING("non-offscreen context");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GLContextCGL *offs = static_cast<GLContextCGL*>(aOffscreen);
|
|
|
|
|
|
|
|
if (offs->mPBuffer) {
|
|
|
|
fGenTextures(1, &mTempTextureName);
|
|
|
|
fBindTexture(LOCAL_GL_TEXTURE_2D, mTempTextureName);
|
|
|
|
|
|
|
|
[mContext
|
|
|
|
setTextureImageToPixelBuffer:offs->mPBuffer
|
|
|
|
colorBuffer:LOCAL_GL_FRONT];
|
|
|
|
} else if (offs->mOffscreenTexture) {
|
|
|
|
if (offs->GetSharedContext() != GLContextProviderCGL::GetGlobalContext())
|
|
|
|
{
|
|
|
|
NS_WARNING("offscreen FBO context can only be bound with context sharing!");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fBindTexture(LOCAL_GL_TEXTURE_2D, offs->mOffscreenTexture);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("don't know how to bind this!");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLContextCGL::UnbindTex2DOffscreen(GLContext *aOffscreen)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aOffscreen->GetContextType() == ContextTypeCGL, "wrong type");
|
|
|
|
|
|
|
|
GLContextCGL *offs = static_cast<GLContextCGL*>(aOffscreen);
|
|
|
|
if (offs->mPBuffer) {
|
|
|
|
NS_ASSERTION(mTempTextureName, "We didn't have an offscreen texture name?");
|
|
|
|
fDeleteTextures(1, &mTempTextureName);
|
|
|
|
mTempTextureName = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextCGL::ResizeOffscreen(const gfxIntSize& aNewSize)
|
|
|
|
{
|
2011-10-19 23:09:57 +04:00
|
|
|
if (!IsOffscreenSizeAllowed(aNewSize))
|
|
|
|
return false;
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
if (mPBuffer) {
|
|
|
|
NSOpenGLPixelBuffer *pb = [[NSOpenGLPixelBuffer alloc]
|
|
|
|
initWithTextureTarget:LOCAL_GL_TEXTURE_2D
|
|
|
|
textureInternalFormat:(mCreationFormat.alpha ? LOCAL_GL_RGBA : LOCAL_GL_RGB)
|
|
|
|
textureMaxMipMapLevel:0
|
|
|
|
pixelsWide:aNewSize.width
|
|
|
|
pixelsHigh:aNewSize.height];
|
|
|
|
if (!pb) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
2012-04-25 18:10:43 +04:00
|
|
|
if (!ResizeOffscreenFBOs(aNewSize, false)) {
|
2011-12-07 03:36:40 +04:00
|
|
|
[pb release];
|
2011-10-19 23:09:57 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
[mPBuffer release];
|
|
|
|
mPBuffer = pb;
|
|
|
|
|
|
|
|
mOffscreenSize = aNewSize;
|
|
|
|
mOffscreenActualSize = aNewSize;
|
|
|
|
|
|
|
|
[mContext setPixelBuffer:pb cubeMapFace:0 mipMapLevel:0
|
|
|
|
currentVirtualScreen:[mContext currentVirtualScreen]];
|
|
|
|
|
|
|
|
MakeCurrent();
|
|
|
|
ClearSafely();
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
2012-04-25 18:10:43 +04:00
|
|
|
return ResizeOffscreenFBOs(aNewSize, true);
|
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-04-27 04:24:53 +04:00
|
|
|
PRInt32 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) {
|
2011-02-07 23:15:46 +03:00
|
|
|
nsCAutoString failure;
|
|
|
|
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;
|
|
|
|
PRInt32 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()) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextCGL *shareContext = GetGlobalContextCGL();
|
|
|
|
|
|
|
|
NSOpenGLContext *context = [[NSOpenGLContext alloc]
|
|
|
|
initWithFormat:sCGLLibrary.PixelFormat()
|
|
|
|
shareContext:(shareContext ? shareContext->mContext : NULL)];
|
|
|
|
if (!context) {
|
2010-04-28 02:29:29 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
NSView *childView = (NSView *)aWidget->GetNativeData(NS_NATIVE_WIDGET);
|
|
|
|
[context setView:childView];
|
|
|
|
|
2010-09-21 20:30:19 +04:00
|
|
|
// make the context transparent
|
2010-09-29 22:36:20 +04:00
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(ContextFormat(ContextFormat::BasicRGB24),
|
2010-07-19 09:01:14 +04:00
|
|
|
shareContext,
|
|
|
|
context);
|
2010-04-28 02:29:29 +04:00
|
|
|
if (!glContext->Init()) {
|
|
|
|
return nsnull;
|
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>
|
|
|
|
CreateOffscreenPBufferContext(const gfxIntSize& aSize,
|
|
|
|
const ContextFormat& aFormat,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aShare = false)
|
2010-04-28 02:29:29 +04:00
|
|
|
{
|
2010-05-18 08:04:22 +04:00
|
|
|
if (!sCGLLibrary.EnsureInitialized()) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextCGL *shareContext = aShare ? GetGlobalContextCGL() : nsnull;
|
|
|
|
if (aShare && !shareContext) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<NSOpenGLPixelFormatAttribute> attribs;
|
|
|
|
|
|
|
|
#define A_(_x) attribs.AppendElement(NSOpenGLPixelFormatAttribute(_x))
|
|
|
|
A_(NSOpenGLPFAAccelerated);
|
|
|
|
A_(NSOpenGLPFAPixelBuffer);
|
|
|
|
A_(NSOpenGLPFAMinimumPolicy);
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
A_(NSOpenGLPFAColorSize);
|
|
|
|
A_(aFormat.colorBits());
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
A_(NSOpenGLPFAAlphaSize);
|
|
|
|
A_(aFormat.alpha);
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
A_(NSOpenGLPFADepthSize);
|
|
|
|
A_(aFormat.depth);
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
A_(NSOpenGLPFAStencilSize);
|
|
|
|
A_(aFormat.stencil);
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
A_(0);
|
|
|
|
#undef A_
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
NSOpenGLPixelFormat *pbFormat = [[NSOpenGLPixelFormat alloc]
|
|
|
|
initWithAttributes:attribs.Elements()];
|
|
|
|
if (!pbFormat) {
|
2010-05-18 08:04:22 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2011-11-01 04:49:55 +04:00
|
|
|
// If we ask for any of these to be on/off and we get the opposite, we stop
|
|
|
|
// creating a pbuffer and instead create an FBO.
|
|
|
|
GLint alphaBits, depthBits, stencilBits;
|
|
|
|
[pbFormat getValues: &alphaBits forAttribute: NSOpenGLPFAAlphaSize forVirtualScreen: 0];
|
|
|
|
[pbFormat getValues: &depthBits forAttribute: NSOpenGLPFADepthSize forVirtualScreen: 0];
|
|
|
|
[pbFormat getValues: &stencilBits forAttribute: NSOpenGLPFAStencilSize forVirtualScreen: 0];
|
|
|
|
if ((alphaBits && !aFormat.alpha) || (!alphaBits && aFormat.alpha) ||
|
|
|
|
(depthBits && !aFormat.alpha) || (!depthBits && aFormat.depth) ||
|
|
|
|
(stencilBits && !aFormat.stencil) || (!stencilBits && aFormat.stencil))
|
|
|
|
{
|
|
|
|
[pbFormat release];
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
NSOpenGLPixelBuffer *pb = [[NSOpenGLPixelBuffer alloc]
|
|
|
|
initWithTextureTarget:LOCAL_GL_TEXTURE_2D
|
|
|
|
textureInternalFormat:(aFormat.alpha ? LOCAL_GL_RGBA : LOCAL_GL_RGB)
|
|
|
|
textureMaxMipMapLevel:0
|
|
|
|
pixelsWide:aSize.width
|
|
|
|
pixelsHigh:aSize.height];
|
|
|
|
if (!pb) {
|
|
|
|
[pbFormat release];
|
2010-05-18 08:04:22 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
NSOpenGLContext *context = [[NSOpenGLContext alloc]
|
|
|
|
initWithFormat:pbFormat
|
|
|
|
shareContext:shareContext ? shareContext->mContext : NULL];
|
|
|
|
if (!context) {
|
|
|
|
[pbFormat release];
|
|
|
|
[pb release];
|
2010-05-18 08:04:22 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
[context
|
|
|
|
setPixelBuffer:pb
|
|
|
|
cubeMapFace:0
|
|
|
|
mipMapLevel:0
|
|
|
|
currentVirtualScreen:[context currentVirtualScreen]];
|
|
|
|
|
|
|
|
{
|
|
|
|
GLint l;
|
|
|
|
[pbFormat getValues:&l forAttribute:NSOpenGLPFADepthSize forVirtualScreen:[context currentVirtualScreen]];
|
2010-05-18 08:04:22 +04:00
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
[pbFormat release];
|
|
|
|
|
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(aFormat, shareContext, context, pb);
|
2012-03-13 02:10:38 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<GLContextCGL>
|
2011-10-19 23:09:57 +04:00
|
|
|
CreateOffscreenFBOContext(const ContextFormat& aFormat,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aShare = true)
|
2010-07-19 09:01:14 +04:00
|
|
|
{
|
|
|
|
if (!sCGLLibrary.EnsureInitialized()) {
|
2010-05-18 08:04:22 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextCGL *shareContext = aShare ? GetGlobalContextCGL() : nsnull;
|
|
|
|
if (aShare && !shareContext) {
|
|
|
|
// if there is no share context, then we can't use FBOs.
|
|
|
|
return nsnull;
|
|
|
|
}
|
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) {
|
2010-05-18 08:04:22 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(aFormat, 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>
|
|
|
|
GLContextProviderCGL::CreateOffscreen(const gfxIntSize& aSize,
|
2012-03-22 03:13:59 +04:00
|
|
|
const ContextFormat& aFormat,
|
|
|
|
const ContextFlags flags)
|
2010-07-19 09:01:14 +04:00
|
|
|
{
|
2011-10-19 23:09:57 +04:00
|
|
|
ContextFormat actualFormat(aFormat);
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
nsRefPtr<GLContextCGL> glContext;
|
2011-10-19 23:09:57 +04:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(Preferences::GetRootBranch(), nsnull);
|
2011-11-24 04:49:02 +04:00
|
|
|
const bool preferFBOs = Preferences::GetBool("cgl.prefer-fbo", true);
|
2011-10-19 23:09:57 +04:00
|
|
|
if (!preferFBOs)
|
2011-10-19 23:31:33 +04:00
|
|
|
{
|
2011-10-19 23:09:57 +04:00
|
|
|
glContext = CreateOffscreenPBufferContext(aSize, actualFormat);
|
2011-10-19 23:09:57 +04:00
|
|
|
if (glContext &&
|
2011-10-19 23:09:57 +04:00
|
|
|
glContext->Init() &&
|
2012-04-25 18:10:43 +04:00
|
|
|
glContext->ResizeOffscreenFBOs(aSize, false))
|
2011-10-19 23:09:57 +04:00
|
|
|
{
|
|
|
|
glContext->mOffscreenSize = aSize;
|
|
|
|
glContext->mOffscreenActualSize = aSize;
|
|
|
|
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
}
|
2010-07-19 09:01:14 +04:00
|
|
|
|
|
|
|
// try a FBO as second choice
|
2011-10-19 23:09:57 +04:00
|
|
|
glContext = CreateOffscreenFBOContext(actualFormat);
|
2010-07-19 09:01:14 +04:00
|
|
|
if (glContext &&
|
|
|
|
glContext->Init() &&
|
2012-04-25 18:10:43 +04:00
|
|
|
glContext->ResizeOffscreenFBOs(aSize, true))
|
2010-07-19 09:01:14 +04:00
|
|
|
{
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything failed
|
|
|
|
return nsnull;
|
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()) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2011-10-19 23:09:57 +04:00
|
|
|
gGlobalContext = CreateOffscreenFBOContext(ContextFormat(ContextFormat::BasicRGB24),
|
2011-10-17 18:59:28 +04:00
|
|
|
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.");
|
|
|
|
gGlobalContext = nsnull;
|
|
|
|
return nsnull;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2010-07-20 08:05:42 +04:00
|
|
|
void
|
|
|
|
GLContextProviderCGL::Shutdown()
|
|
|
|
{
|
2010-09-03 07:51:15 +04:00
|
|
|
gGlobalContext = nsnull;
|
2010-07-20 08:05:42 +04:00
|
|
|
}
|
|
|
|
|
2010-04-28 02:29:29 +04:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|