2010-04-27 15:29:29 -07: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 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"
|
2012-01-17 12:33:04 -08:00
|
|
|
#include "sampler.h"
|
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),
|
2010-07-18 22:01:14 -07:00
|
|
|
mOGLLibrary(nsnull),
|
|
|
|
mPixelFormat(nsnull)
|
|
|
|
{ }
|
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()
|
|
|
|
{
|
|
|
|
if (mPixelFormat == nsnull) {
|
|
|
|
NSOpenGLPixelFormatAttribute attribs[] = {
|
|
|
|
NSOpenGLPFAAccelerated,
|
2011-09-21 12:20:40 -07:00
|
|
|
NSOpenGLPFAAllowOfflineRenderers,
|
2010-09-21 11:39:38 -07:00
|
|
|
NSOpenGLPFADoubleBuffer,
|
2010-07-18 22:01:14 -07:00
|
|
|
(NSOpenGLPixelFormatAttribute)nil
|
|
|
|
};
|
|
|
|
|
2010-09-21 11:39:38 -07:00
|
|
|
if (!gUseDoubleBufferedWindows) {
|
2011-09-21 12:20:40 -07:00
|
|
|
attribs[2] = (NSOpenGLPixelFormatAttribute)nil;
|
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:
|
2010-07-18 22:01:14 -07:00
|
|
|
GLContextCGL(const ContextFormat& aFormat,
|
|
|
|
GLContext *aShareContext,
|
|
|
|
NSOpenGLContext *aContext,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsOffscreen = false)
|
2010-07-18 22:01:14 -07:00
|
|
|
: GLContext(aFormat, aIsOffscreen, aShareContext),
|
|
|
|
mContext(aContext),
|
|
|
|
mPBuffer(nsnull),
|
|
|
|
mTempTextureName(0)
|
2010-05-17 21:04:22 -07:00
|
|
|
{ }
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
GLContextCGL(const ContextFormat& aFormat,
|
|
|
|
GLContext *aShareContext,
|
|
|
|
NSOpenGLContext *aContext,
|
|
|
|
NSOpenGLPixelBuffer *aPixelBuffer)
|
2011-10-17 07:59:28 -07:00
|
|
|
: GLContext(aFormat, true, aShareContext),
|
2010-07-18 22:01:14 -07:00
|
|
|
mContext(aContext),
|
|
|
|
mPBuffer(aPixelBuffer),
|
|
|
|
mTempTextureName(0)
|
2010-05-17 21:04:22 -07: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];
|
|
|
|
|
|
|
|
if (mPBuffer)
|
2010-07-18 22:01:14 -07:00
|
|
|
[mPBuffer release];
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
InitFramebuffers();
|
|
|
|
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:
|
|
|
|
return nsnull;
|
|
|
|
}
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2012-05-08 06:47:34 -07:00
|
|
|
bool MakeCurrentImpl()
|
2010-04-27 15:29:29 -07:00
|
|
|
{
|
2010-05-17 21:04:22 -07:00
|
|
|
if (mContext) {
|
|
|
|
[mContext makeCurrentContext];
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-01-17 12:33:04 -08: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 BindTex2DOffscreen(GLContext *aOffscreen);
|
2010-07-18 22:01:14 -07:00
|
|
|
void UnbindTex2DOffscreen(GLContext *aOffscreen);
|
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
|
|
|
NSOpenGLPixelBuffer *mPBuffer;
|
|
|
|
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::BindTex2DOffscreen(GLContext *aOffscreen)
|
|
|
|
{
|
|
|
|
if (aOffscreen->GetContextType() != ContextTypeCGL) {
|
|
|
|
NS_WARNING("non-CGL context");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-07-18 22:01:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aOffscreen->IsOffscreen()) {
|
|
|
|
NS_WARNING("non-offscreen context");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-07-18 22:01:14 -07: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 07:59:28 -07:00
|
|
|
return false;
|
2010-07-18 22:01:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fBindTexture(LOCAL_GL_TEXTURE_2D, offs->mOffscreenTexture);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("don't know how to bind this!");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-07-18 22:01:14 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-07-18 22:01:14 -07: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-28 23:19:26 -07:00
|
|
|
bool
|
2010-07-18 22:01:14 -07:00
|
|
|
GLContextCGL::ResizeOffscreen(const gfxIntSize& aNewSize)
|
|
|
|
{
|
2011-10-19 12:09:57 -07:00
|
|
|
if (!IsOffscreenSizeAllowed(aNewSize))
|
|
|
|
return false;
|
|
|
|
|
2010-07-18 22:01:14 -07: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 07:59:28 -07:00
|
|
|
return false;
|
2010-07-18 22:01:14 -07:00
|
|
|
}
|
|
|
|
|
2012-04-25 07:10:43 -07:00
|
|
|
if (!ResizeOffscreenFBOs(aNewSize, false)) {
|
2011-12-06 15:36:40 -08:00
|
|
|
[pb release];
|
2011-10-19 12:09:57 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
[mPBuffer release];
|
|
|
|
mPBuffer = pb;
|
|
|
|
|
|
|
|
mOffscreenSize = aNewSize;
|
|
|
|
mOffscreenActualSize = aNewSize;
|
|
|
|
|
|
|
|
[mContext setPixelBuffer:pb cubeMapFace:0 mipMapLevel:0
|
|
|
|
currentVirtualScreen:[mContext currentVirtualScreen]];
|
|
|
|
|
|
|
|
MakeCurrent();
|
|
|
|
ClearSafely();
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-07-18 22:01:14 -07:00
|
|
|
}
|
|
|
|
|
2012-04-25 07:10:43 -07:00
|
|
|
return ResizeOffscreenFBOs(aNewSize, true);
|
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-04-26 17:24:53 -07:00
|
|
|
PRInt32 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) {
|
2011-02-07 12:15:46 -08:00
|
|
|
nsCAutoString failure;
|
|
|
|
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;
|
|
|
|
PRInt32 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()) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
GLContextCGL *shareContext = GetGlobalContextCGL();
|
|
|
|
|
|
|
|
NSOpenGLContext *context = [[NSOpenGLContext alloc]
|
|
|
|
initWithFormat:sCGLLibrary.PixelFormat()
|
|
|
|
shareContext:(shareContext ? shareContext->mContext : NULL)];
|
|
|
|
if (!context) {
|
2010-04-27 15:29:29 -07:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
NSView *childView = (NSView *)aWidget->GetNativeData(NS_NATIVE_WIDGET);
|
|
|
|
[context setView:childView];
|
|
|
|
|
2010-09-21 09:30:19 -07:00
|
|
|
// make the context transparent
|
2010-09-29 11:36:20 -07:00
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(ContextFormat(ContextFormat::BasicRGB24),
|
2010-07-18 22:01:14 -07:00
|
|
|
shareContext,
|
|
|
|
context);
|
2010-04-27 15:29:29 -07:00
|
|
|
if (!glContext->Init()) {
|
|
|
|
return nsnull;
|
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>
|
|
|
|
CreateOffscreenPBufferContext(const gfxIntSize& aSize,
|
|
|
|
const ContextFormat& aFormat,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aShare = false)
|
2010-04-27 15:29:29 -07:00
|
|
|
{
|
2010-05-17 21:04:22 -07:00
|
|
|
if (!sCGLLibrary.EnsureInitialized()) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07: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-17 21:04:22 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
A_(NSOpenGLPFAColorSize);
|
|
|
|
A_(aFormat.colorBits());
|
2010-05-17 21:04:22 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
A_(NSOpenGLPFAAlphaSize);
|
|
|
|
A_(aFormat.alpha);
|
2010-05-17 21:04:22 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
A_(NSOpenGLPFADepthSize);
|
|
|
|
A_(aFormat.depth);
|
2010-05-17 21:04:22 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
A_(NSOpenGLPFAStencilSize);
|
|
|
|
A_(aFormat.stencil);
|
2010-05-17 21:04:22 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
A_(0);
|
|
|
|
#undef A_
|
2010-05-17 21:04:22 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
NSOpenGLPixelFormat *pbFormat = [[NSOpenGLPixelFormat alloc]
|
|
|
|
initWithAttributes:attribs.Elements()];
|
|
|
|
if (!pbFormat) {
|
2010-05-17 21:04:22 -07:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:49:55 -07: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-18 22:01:14 -07: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-17 21:04:22 -07:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
NSOpenGLContext *context = [[NSOpenGLContext alloc]
|
|
|
|
initWithFormat:pbFormat
|
|
|
|
shareContext:shareContext ? shareContext->mContext : NULL];
|
|
|
|
if (!context) {
|
|
|
|
[pbFormat release];
|
|
|
|
[pb release];
|
2010-05-17 21:04:22 -07:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
[context
|
|
|
|
setPixelBuffer:pb
|
|
|
|
cubeMapFace:0
|
|
|
|
mipMapLevel:0
|
|
|
|
currentVirtualScreen:[context currentVirtualScreen]];
|
|
|
|
|
|
|
|
{
|
|
|
|
GLint l;
|
|
|
|
[pbFormat getValues:&l forAttribute:NSOpenGLPFADepthSize forVirtualScreen:[context currentVirtualScreen]];
|
2010-05-17 21:04:22 -07:00
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
[pbFormat release];
|
|
|
|
|
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(aFormat, shareContext, context, pb);
|
2012-03-12 15:10:38 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<GLContextCGL>
|
2011-10-19 12:09:57 -07:00
|
|
|
CreateOffscreenFBOContext(const ContextFormat& aFormat,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aShare = true)
|
2010-07-18 22:01:14 -07:00
|
|
|
{
|
|
|
|
if (!sCGLLibrary.EnsureInitialized()) {
|
2010-05-17 21:04:22 -07:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07: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-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) {
|
2010-05-17 21:04:22 -07:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
nsRefPtr<GLContextCGL> glContext = new GLContextCGL(aFormat, 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>
|
|
|
|
GLContextProviderCGL::CreateOffscreen(const gfxIntSize& aSize,
|
2012-03-21 16:13:59 -07:00
|
|
|
const ContextFormat& aFormat,
|
|
|
|
const ContextFlags flags)
|
2010-07-18 22:01:14 -07:00
|
|
|
{
|
2011-10-19 12:09:57 -07:00
|
|
|
ContextFormat actualFormat(aFormat);
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
nsRefPtr<GLContextCGL> glContext;
|
2011-10-19 12:09:57 -07:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(Preferences::GetRootBranch(), nsnull);
|
2011-11-23 16:49:02 -08:00
|
|
|
const bool preferFBOs = Preferences::GetBool("cgl.prefer-fbo", true);
|
2011-10-19 12:09:57 -07:00
|
|
|
if (!preferFBOs)
|
2011-10-19 12:31:33 -07:00
|
|
|
{
|
2011-10-19 12:09:57 -07:00
|
|
|
glContext = CreateOffscreenPBufferContext(aSize, actualFormat);
|
2011-10-19 12:09:57 -07:00
|
|
|
if (glContext &&
|
2011-10-19 12:09:57 -07:00
|
|
|
glContext->Init() &&
|
2012-04-25 07:10:43 -07:00
|
|
|
glContext->ResizeOffscreenFBOs(aSize, false))
|
2011-10-19 12:09:57 -07:00
|
|
|
{
|
|
|
|
glContext->mOffscreenSize = aSize;
|
|
|
|
glContext->mOffscreenActualSize = aSize;
|
|
|
|
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
}
|
2010-07-18 22:01:14 -07:00
|
|
|
|
|
|
|
// try a FBO as second choice
|
2011-10-19 12:09:57 -07:00
|
|
|
glContext = CreateOffscreenFBOContext(actualFormat);
|
2010-07-18 22:01:14 -07:00
|
|
|
if (glContext &&
|
|
|
|
glContext->Init() &&
|
2012-04-25 07:10:43 -07:00
|
|
|
glContext->ResizeOffscreenFBOs(aSize, true))
|
2010-07-18 22:01:14 -07:00
|
|
|
{
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything failed
|
|
|
|
return nsnull;
|
2010-04-27 15:29:29 -07:00
|
|
|
}
|
|
|
|
|
2010-06-23 02:24:31 -07:00
|
|
|
already_AddRefed<GLContext>
|
2010-07-18 22:01:14 -07:00
|
|
|
GLContextProviderCGL::CreateForNativePixmapSurface(gfxASurface *aSurface)
|
2010-06-23 02:24:31 -07:00
|
|
|
{
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07: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.
|
2011-10-19 12:09:57 -07:00
|
|
|
gGlobalContext = CreateOffscreenFBOContext(ContextFormat(ContextFormat::BasicRGB24),
|
2011-10-17 07:59:28 -07:00
|
|
|
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.");
|
|
|
|
gGlobalContext = nsnull;
|
|
|
|
return nsnull;
|
|
|
|
}
|
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()
|
|
|
|
{
|
2010-09-02 20:51:15 -07:00
|
|
|
gGlobalContext = nsnull;
|
2010-07-19 21:05:42 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 15:29:29 -07:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|