2010-04-28 02:29:29 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Bas Schouten <bschouten@mozilla.com>
|
|
|
|
* Matt Woodrow <mwoodrow@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#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"
|
2010-09-21 22:39:38 +04:00
|
|
|
#include "prenv.h"
|
2010-04-28 02:29:29 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2010-09-21 22:39:38 +04:00
|
|
|
static PRBool gUseDoubleBufferedWindows = PR_TRUE;
|
|
|
|
|
2010-04-28 02:29:29 +04:00
|
|
|
class CGLLibrary
|
|
|
|
{
|
|
|
|
public:
|
2010-07-19 09:01:14 +04:00
|
|
|
CGLLibrary()
|
|
|
|
: mInitialized(PR_FALSE),
|
|
|
|
mOGLLibrary(nsnull),
|
|
|
|
mPixelFormat(nsnull)
|
|
|
|
{ }
|
2010-04-28 02:29:29 +04:00
|
|
|
|
|
|
|
PRBool EnsureInitialized()
|
|
|
|
{
|
|
|
|
if (mInitialized) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
if (!mOGLLibrary) {
|
|
|
|
mOGLLibrary = PR_LoadLibrary("/System/Library/Frameworks/OpenGL.framework/OpenGL");
|
|
|
|
if (!mOGLLibrary) {
|
|
|
|
NS_WARNING("Couldn't load OpenGL Framework.");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2010-09-21 22:39:38 +04:00
|
|
|
|
|
|
|
const char* db = PR_GetEnv("MOZ_CGL_DB");
|
|
|
|
gUseDoubleBufferedWindows = (!db || *db != '0');
|
|
|
|
|
2010-04-28 02:29:29 +04:00
|
|
|
mInitialized = PR_TRUE;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
NSOpenGLPixelFormat *PixelFormat()
|
|
|
|
{
|
|
|
|
if (mPixelFormat == nsnull) {
|
|
|
|
NSOpenGLPixelFormatAttribute attribs[] = {
|
|
|
|
NSOpenGLPFAAccelerated,
|
2010-09-21 22:39:38 +04:00
|
|
|
NSOpenGLPFADoubleBuffer,
|
2010-07-19 09:01:14 +04:00
|
|
|
(NSOpenGLPixelFormatAttribute)nil
|
|
|
|
};
|
|
|
|
|
2010-09-21 22:39:38 +04:00
|
|
|
if (!gUseDoubleBufferedWindows) {
|
|
|
|
attribs[1] = (NSOpenGLPixelFormatAttribute)nil;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
mPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
|
|
|
|
}
|
|
|
|
|
|
|
|
return mPixelFormat;
|
|
|
|
}
|
2010-04-28 02:29:29 +04:00
|
|
|
private:
|
|
|
|
PRBool mInitialized;
|
|
|
|
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,
|
|
|
|
PRBool aIsOffscreen = PR_FALSE)
|
|
|
|
: 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)
|
|
|
|
: GLContext(aFormat, PR_TRUE, aShareContext),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
PRBool Init()
|
|
|
|
{
|
|
|
|
MakeCurrent();
|
|
|
|
return InitWithPrefix("gl", PR_TRUE);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-10-13 01:01:59 +04:00
|
|
|
PRBool MakeCurrentImpl(PRBool aForce = PR_FALSE)
|
2010-04-28 02:29:29 +04:00
|
|
|
{
|
2010-05-18 08:04:22 +04:00
|
|
|
if (mContext) {
|
|
|
|
[mContext makeCurrentContext];
|
|
|
|
}
|
2010-04-28 02:29:29 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool SetupLookupFunction()
|
|
|
|
{
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-09-21 22:39:38 +04:00
|
|
|
PRBool IsDoubleBuffered()
|
|
|
|
{
|
|
|
|
return gUseDoubleBufferedWindows;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool SwapBuffers()
|
|
|
|
{
|
|
|
|
[mContext flushBuffer];
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
PRBool BindTex2DOffscreen(GLContext *aOffscreen);
|
|
|
|
void UnbindTex2DOffscreen(GLContext *aOffscreen);
|
|
|
|
PRBool ResizeOffscreen(const gfxIntSize& aNewSize);
|
|
|
|
|
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,
|
|
|
|
GLContext* aContext);
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
PRBool
|
|
|
|
GLContextCGL::BindTex2DOffscreen(GLContext *aOffscreen)
|
|
|
|
{
|
|
|
|
if (aOffscreen->GetContextType() != ContextTypeCGL) {
|
|
|
|
NS_WARNING("non-CGL context");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aOffscreen->IsOffscreen()) {
|
|
|
|
NS_WARNING("non-offscreen context");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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!");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fBindTexture(LOCAL_GL_TEXTURE_2D, offs->mOffscreenTexture);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("don't know how to bind this!");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
GLContextCGL::ResizeOffscreen(const gfxIntSize& aNewSize)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
[mPBuffer release];
|
|
|
|
mPBuffer = pb;
|
|
|
|
|
|
|
|
mOffscreenSize = aNewSize;
|
|
|
|
mOffscreenActualSize = aNewSize;
|
|
|
|
|
|
|
|
[mContext setPixelBuffer:pb cubeMapFace:0 mipMapLevel:0
|
|
|
|
currentVirtualScreen:[mContext currentVirtualScreen]];
|
|
|
|
|
|
|
|
MakeCurrent();
|
|
|
|
ClearSafely();
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResizeOffscreenFBO(aNewSize);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
GLContext*);
|
|
|
|
|
|
|
|
protected:
|
2010-11-11 23:31:23 +03:00
|
|
|
virtual gfxContext*
|
|
|
|
BeginUpdate(nsIntRegion& aRegion)
|
|
|
|
{
|
|
|
|
ImageFormat format;
|
|
|
|
if (GetContentType() == gfxASurface::CONTENT_COLOR)
|
|
|
|
format = gfxASurface::ImageFormatRGB24;
|
|
|
|
else
|
|
|
|
format = gfxASurface::ImageFormatARGB32;
|
|
|
|
|
2010-12-17 10:29:23 +03:00
|
|
|
if (!mTextureInited || !mUpdateSurface ||
|
|
|
|
mUpdateSurface->GetContentType() != GetContentType())
|
2010-11-11 23:31:23 +03:00
|
|
|
{
|
|
|
|
mUpdateSurface = nsnull;
|
2010-11-12 00:04:36 +03:00
|
|
|
mUpdateOffset = nsIntPoint(0, 0);
|
2010-11-11 23:31:23 +03:00
|
|
|
// We need to (re)create our backing store. Let the base class to that.
|
|
|
|
return BasicTextureImage::BeginUpdate(aRegion);
|
|
|
|
}
|
|
|
|
|
2010-12-17 10:29:23 +03:00
|
|
|
nsRefPtr<gfxImageSurface> imageSurface = mUpdateSurface->GetAsImageSurface();
|
|
|
|
if (!imageSurface ||
|
|
|
|
nsIntSize(imageSurface->Width(), imageSurface->Height()) < mSize) {
|
|
|
|
mUpdateSurface = nsnull;
|
|
|
|
mUpdateOffset = nsIntPoint(0, 0);
|
|
|
|
// We need to (re)create our backing store. Let the base class to that.
|
|
|
|
return BasicTextureImage::BeginUpdate(aRegion);
|
|
|
|
}
|
|
|
|
|
2010-11-11 23:31:23 +03:00
|
|
|
// the basic impl can only upload updates to rectangles
|
|
|
|
mUpdateRect = aRegion.GetBounds();
|
|
|
|
aRegion = nsIntRegion(mUpdateRect);
|
|
|
|
|
|
|
|
if (!nsIntRect(nsIntPoint(0, 0), mSize).Contains(mUpdateRect)) {
|
|
|
|
NS_ERROR("update outside of image");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mUpdateContext = new gfxContext(mUpdateSurface);
|
|
|
|
mUpdateContext->Clip(gfxRect(mUpdateRect.x, mUpdateRect.y,
|
|
|
|
mUpdateRect.width, mUpdateRect.height));
|
2010-11-13 02:34:36 +03:00
|
|
|
if (GetContentType() != gfxASurface::CONTENT_COLOR)
|
|
|
|
{
|
|
|
|
mUpdateContext->SetOperator(gfxContext::OPERATOR_CLEAR);
|
|
|
|
mUpdateContext->Paint();
|
|
|
|
mUpdateContext->SetOperator(gfxContext::OPERATOR_OVER);
|
|
|
|
}
|
2010-11-11 23:31:23 +03:00
|
|
|
mUpdateOffset = mUpdateRect.TopLeft();
|
|
|
|
|
|
|
|
return mUpdateContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual PRBool
|
|
|
|
EndUpdate()
|
|
|
|
{
|
|
|
|
if (!mUpdateSurface)
|
|
|
|
mUpdateSurface = mUpdateContext->OriginalSurface();
|
|
|
|
|
|
|
|
return BasicTextureImage::EndUpdate();
|
|
|
|
}
|
|
|
|
|
2010-07-01 20:30:38 +04:00
|
|
|
virtual already_AddRefed<gfxASurface>
|
|
|
|
CreateUpdateSurface(const gfxIntSize& aSize, ImageFormat aFmt)
|
|
|
|
{
|
|
|
|
mUpdateFormat = aFmt;
|
2010-09-17 01:34:53 +04:00
|
|
|
return gfxPlatform::GetPlatform()->CreateOffscreenSurface(aSize, gfxASurface::ContentFromFormat(aFmt));
|
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,
|
|
|
|
GLContext* aContext)
|
2010-11-12 23:02:20 +03:00
|
|
|
: BasicTextureImage(aTexture, aSize, aWrapMode, aContentType, aContext)
|
2010-07-01 20:30:38 +04:00
|
|
|
{}
|
|
|
|
|
|
|
|
ImageFormat mUpdateFormat;
|
2010-11-11 23:31:23 +03:00
|
|
|
nsRefPtr<gfxASurface> mUpdateSurface;
|
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,
|
|
|
|
GLContext* aContext)
|
|
|
|
{
|
2010-11-12 23:02:20 +03:00
|
|
|
nsRefPtr<TextureImageCGL> teximage
|
|
|
|
(new TextureImageCGL(aTexture, aSize, aWrapMode, aContentType, aContext));
|
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;
|
2010-07-19 09:01:14 +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,
|
|
|
|
PRBool aShare = PR_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
|
|
|
printf_stderr("colorbits: %d alpha: %d depth: %d stencil: %d\n", aFormat.colorBits(), aFormat.alpha, aFormat.depth, aFormat.stencil);
|
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;
|
|
|
|
}
|
|
|
|
|
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]];
|
|
|
|
printf_stderr("*** depth: %d (req: %d)\n", l, aFormat.depth);
|
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);
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<GLContextCGL>
|
|
|
|
CreateOffscreenFBOContext(const gfxIntSize& aSize,
|
|
|
|
const ContextFormat& aFormat,
|
|
|
|
PRBool aShare = PR_TRUE)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(aFormat, shareContext, context, PR_TRUE);
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<GLContext>
|
|
|
|
GLContextProviderCGL::CreateOffscreen(const gfxIntSize& aSize,
|
|
|
|
const ContextFormat& aFormat)
|
|
|
|
{
|
|
|
|
nsRefPtr<GLContextCGL> glContext;
|
|
|
|
|
|
|
|
glContext = CreateOffscreenPBufferContext(aSize, aFormat);
|
|
|
|
if (glContext &&
|
|
|
|
glContext->Init())
|
|
|
|
{
|
|
|
|
glContext->mOffscreenSize = aSize;
|
|
|
|
glContext->mOffscreenActualSize = aSize;
|
|
|
|
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// try a FBO as second choice
|
|
|
|
glContext = CreateOffscreenFBOContext(aSize, aFormat);
|
|
|
|
if (glContext &&
|
|
|
|
glContext->Init() &&
|
|
|
|
glContext->ResizeOffscreenFBO(aSize))
|
|
|
|
{
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything failed
|
|
|
|
return nsnull;
|
2010-04-28 02:29:29 +04:00
|
|
|
}
|
|
|
|
|
2010-06-23 13:24:31 +04:00
|
|
|
already_AddRefed<GLContext>
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextProviderCGL::CreateForNativePixmapSurface(gfxASurface *aSurface)
|
2010-06-23 13:24:31 +04:00
|
|
|
{
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
static nsRefPtr<GLContext> gGlobalContext;
|
|
|
|
|
|
|
|
GLContext *
|
|
|
|
GLContextProviderCGL::GetGlobalContext()
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
gGlobalContext = CreateOffscreenFBOContext(gfxIntSize(16, 16),
|
|
|
|
ContextFormat(ContextFormat::BasicRGB24),
|
|
|
|
PR_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
|
|
|
|
|
|
|
gGlobalContext->SetIsGlobalSharedContext(PR_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 */
|