Bug 811958 - Pull GLContext out of Cocoa stuff - r=bgirard

This commit is contained in:
Jeff Gilbert 2012-11-26 14:23:27 -08:00
parent cbec1edc43
commit 9c9ac2fca3
9 changed files with 277 additions and 182 deletions

View File

@ -32,6 +32,7 @@
#include "nsRegion.h"
#include "nsAutoPtr.h"
#include "nsThreadUtils.h"
#include "GLContextTypes.h"
typedef char realGLboolean;
@ -55,24 +56,6 @@ class GLContext;
typedef uintptr_t SharedTextureHandle;
enum ShaderProgramType {
RGBALayerProgramType,
RGBALayerExternalProgramType,
BGRALayerProgramType,
RGBXLayerProgramType,
BGRXLayerProgramType,
RGBARectLayerProgramType,
RGBAExternalLayerProgramType,
ColorLayerProgramType,
YCbCrLayerProgramType,
ComponentAlphaPass1ProgramType,
ComponentAlphaPass2ProgramType,
Copy2DProgramType,
Copy2DRectProgramType,
NumProgramTypes
};
/**
* A TextureImage encapsulates a surface that can be drawn to by a
* Thebes gfxContext and (hopefully efficiently!) synchronized to a

42
gfx/gl/GLContextTypes.h Normal file
View File

@ -0,0 +1,42 @@
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */
#ifndef GLCONTEXTSTUFF_H_
#define GLCONTEXTSTUFF_H_
/**
* We don't include GLDefs.h here since we don't want to drag in all defines
* in for all our users.
*/
typedef unsigned int GLenum;
typedef unsigned int GLbitfield;
typedef unsigned int GLuint;
typedef int GLint;
typedef int GLsizei;
namespace mozilla {
namespace gl {
enum ShaderProgramType {
RGBALayerProgramType,
RGBALayerExternalProgramType,
BGRALayerProgramType,
RGBXLayerProgramType,
BGRXLayerProgramType,
RGBARectLayerProgramType,
RGBAExternalLayerProgramType,
ColorLayerProgramType,
YCbCrLayerProgramType,
ComponentAlphaPass1ProgramType,
ComponentAlphaPass2ProgramType,
Copy2DProgramType,
Copy2DRectProgramType,
NumProgramTypes
};
} // namespace gl
} // namespace mozilla
#endif /* GLCONTEXTSTUFF_H_ */

View File

@ -18,6 +18,7 @@ FAIL_ON_WARNINGS = 1
EXPORTS = \
GLDefs.h \
GLContext.h \
GLContextTypes.h \
GLContextSymbols.h \
GLContextProvider.h \
GLContextProviderImpl.h \

View File

@ -3,13 +3,14 @@
* 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/. */
#include "LayerManagerOGL.h"
#include "mozilla/layers/PLayers.h"
/* This must occur *after* layers/PLayers.h to avoid typedefs conflicts. */
#include "mozilla/Util.h"
#include "Composer2D.h"
#include "LayerManagerOGL.h"
#include "ThebesLayerOGL.h"
#include "ContainerLayerOGL.h"
#include "ImageLayerOGL.h"
@ -52,6 +53,91 @@ using namespace mozilla::gl;
int ShaderProgramOGL::sCurrentProgramKey = 0;
#endif
bool
LayerManagerOGL::Initialize(bool force)
{
return Initialize(CreateContext(), force);
}
int32_t
LayerManagerOGL::GetMaxTextureSize() const
{
return mGLContext->GetMaxTextureSize();
}
void
LayerManagerOGL::MakeCurrent(bool aForce)
{
if (mDestroyed) {
NS_WARNING("Call on destroyed layer manager");
return;
}
mGLContext->MakeCurrent(aForce);
}
void*
LayerManagerOGL::GetNSOpenGLContext() const
{
return gl()->GetNativeData(GLContext::NativeGLContext);
}
void
LayerManagerOGL::BindQuadVBO() {
mGLContext->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mQuadVBO);
}
void
LayerManagerOGL::QuadVBOVerticesAttrib(GLuint aAttribIndex) {
mGLContext->fVertexAttribPointer(aAttribIndex, 2,
LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0,
(GLvoid*) QuadVBOVertexOffset());
}
void
LayerManagerOGL::QuadVBOTexCoordsAttrib(GLuint aAttribIndex) {
mGLContext->fVertexAttribPointer(aAttribIndex, 2,
LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0,
(GLvoid*) QuadVBOTexCoordOffset());
}
void
LayerManagerOGL::QuadVBOFlippedTexCoordsAttrib(GLuint aAttribIndex) {
mGLContext->fVertexAttribPointer(aAttribIndex, 2,
LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0,
(GLvoid*) QuadVBOFlippedTexCoordOffset());
}
// Super common
void
LayerManagerOGL::BindAndDrawQuad(GLuint aVertAttribIndex,
GLuint aTexCoordAttribIndex,
bool aFlipped)
{
BindQuadVBO();
QuadVBOVerticesAttrib(aVertAttribIndex);
if (aTexCoordAttribIndex != GLuint(-1)) {
if (aFlipped)
QuadVBOFlippedTexCoordsAttrib(aTexCoordAttribIndex);
else
QuadVBOTexCoordsAttrib(aTexCoordAttribIndex);
mGLContext->fEnableVertexAttribArray(aTexCoordAttribIndex);
}
mGLContext->fEnableVertexAttribArray(aVertAttribIndex);
mGLContext->fDrawArrays(LOCAL_GL_TRIANGLE_STRIP, 0, 4);
mGLContext->fDisableVertexAttribArray(aVertAttribIndex);
if (aTexCoordAttribIndex != GLuint(-1)) {
mGLContext->fDisableVertexAttribArray(aTexCoordAttribIndex);
}
}
static const double kFpsWindowMs = 250.0;
static const size_t kNumFrameTimeStamps = 16;
struct FPSCounter {

View File

@ -9,31 +9,23 @@
#include "LayerManagerOGLProgram.h"
#include "mozilla/layers/ShadowLayers.h"
#include "mozilla/TimeStamp.h"
#ifdef XP_WIN
#include <windows.h>
#endif
/**
* We don't include GLDefs.h here since we don't want to drag in all defines
* in for all our users.
*/
typedef unsigned int GLenum;
typedef unsigned int GLbitfield;
typedef unsigned int GLuint;
typedef int GLint;
typedef int GLsizei;
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
#include "gfxContext.h"
#include "gfx3DMatrix.h"
#include "nsIWidget.h"
#include "GLContext.h"
#include "GLContextTypes.h"
namespace mozilla {
namespace gl {
class GLContext;
}
namespace layers {
class Composer2D;
@ -71,9 +63,7 @@ public:
*
* \return True is initialization was succesful, false when it was not.
*/
bool Initialize(bool force = false) {
return Initialize(CreateContext(), force);
}
bool Initialize(bool force = false);
bool Initialize(nsRefPtr<GLContext> aContext, bool force = false);
@ -110,18 +100,14 @@ public:
virtual void SetRoot(Layer* aLayer) { mRoot = aLayer; }
virtual bool CanUseCanvasLayerForSize(const gfxIntSize &aSize)
{
if (!mGLContext)
return false;
int32_t maxSize = mGLContext->GetMaxTextureSize();
return aSize <= gfxIntSize(maxSize, maxSize);
virtual bool CanUseCanvasLayerForSize(const gfxIntSize &aSize) {
if (!mGLContext)
return false;
int32_t maxSize = GetMaxTextureSize();
return aSize <= gfxIntSize(maxSize, maxSize);
}
virtual int32_t GetMaxTextureSize() const
{
return mGLContext->GetMaxTextureSize();
}
virtual int32_t GetMaxTextureSize() const;
virtual already_AddRefed<ThebesLayer> CreateThebesLayer();
@ -151,13 +137,7 @@ public:
/**
* Helper methods.
*/
void MakeCurrent(bool aForce = false) {
if (mDestroyed) {
NS_WARNING("Call on destroyed layer manager");
return;
}
mGLContext->MakeCurrent(aForce);
}
void MakeCurrent(bool aForce = false);
ShaderProgramOGL* GetBasicLayerProgram(bool aOpaque, bool aIsRGB,
MaskType aMask = MaskNone)
@ -203,6 +183,9 @@ public:
GLContext* gl() const { return mGLContext; }
// |NSOpenGLContext*|:
void* GetNSOpenGLContext() const;
DrawThebesLayerCallback GetThebesLayerCallback() const
{ return mThebesLayerCallback; }
@ -254,56 +237,15 @@ public:
GLintptr QuadVBOTexCoordOffset() { return sizeof(float)*4*2; }
GLintptr QuadVBOFlippedTexCoordOffset() { return sizeof(float)*8*2; }
void BindQuadVBO() {
mGLContext->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mQuadVBO);
}
void QuadVBOVerticesAttrib(GLuint aAttribIndex) {
mGLContext->fVertexAttribPointer(aAttribIndex, 2,
LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0,
(GLvoid*) QuadVBOVertexOffset());
}
void QuadVBOTexCoordsAttrib(GLuint aAttribIndex) {
mGLContext->fVertexAttribPointer(aAttribIndex, 2,
LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0,
(GLvoid*) QuadVBOTexCoordOffset());
}
void QuadVBOFlippedTexCoordsAttrib(GLuint aAttribIndex) {
mGLContext->fVertexAttribPointer(aAttribIndex, 2,
LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0,
(GLvoid*) QuadVBOFlippedTexCoordOffset());
}
void BindQuadVBO();
void QuadVBOVerticesAttrib(GLuint aAttribIndex);
void QuadVBOTexCoordsAttrib(GLuint aAttribIndex);
void QuadVBOFlippedTexCoordsAttrib(GLuint aAttribIndex);
// Super common
void BindAndDrawQuad(GLuint aVertAttribIndex,
GLuint aTexCoordAttribIndex,
bool aFlipped = false)
{
BindQuadVBO();
QuadVBOVerticesAttrib(aVertAttribIndex);
if (aTexCoordAttribIndex != GLuint(-1)) {
if (aFlipped)
QuadVBOFlippedTexCoordsAttrib(aTexCoordAttribIndex);
else
QuadVBOTexCoordsAttrib(aTexCoordAttribIndex);
mGLContext->fEnableVertexAttribArray(aTexCoordAttribIndex);
}
mGLContext->fEnableVertexAttribArray(aVertAttribIndex);
mGLContext->fDrawArrays(LOCAL_GL_TRIANGLE_STRIP, 0, 4);
mGLContext->fDisableVertexAttribArray(aVertAttribIndex);
if (aTexCoordAttribIndex != GLuint(-1)) {
mGLContext->fDisableVertexAttribArray(aTexCoordAttribIndex);
}
}
bool aFlipped = false);
void BindAndDrawQuad(ShaderProgramOGL *aProg,
bool aFlipped = false)

View File

@ -3,9 +3,12 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "LayerManagerOGLProgram.h"
#include "LayerManagerOGLShaders.h"
#include "LayerManagerOGL.h"
#include "GLContext.h"
namespace mozilla {
namespace layers {
@ -227,6 +230,28 @@ ProgramProfileOGL::GetProfileFor(gl::ShaderProgramType aType,
const char* const ShaderProgramOGL::VertexCoordAttrib = "aVertexCoord";
const char* const ShaderProgramOGL::TexCoordAttrib = "aTexCoord";
ShaderProgramOGL::ShaderProgramOGL(GLContext* aGL, const ProgramProfileOGL& aProfile)
: mIsProjectionMatrixStale(false)
, mGL(aGL)
, mProgram(0)
, mProfile(aProfile)
, mProgramState(STATE_NEW)
{}
ShaderProgramOGL::~ShaderProgramOGL()
{
if (mProgram <= 0) {
return;
}
nsRefPtr<GLContext> ctx = mGL->GetSharedContext();
if (!ctx) {
ctx = mGL;
}
ctx->MakeCurrent();
ctx->fDeleteProgram(mProgram);
}
bool
ShaderProgramOGL::Initialize()
{
@ -394,5 +419,87 @@ ShaderProgramOGL::LoadMask(Layer* aMaskLayer)
return true;
}
void
ShaderProgramOGL::Activate()
{
if (mProgramState == STATE_NEW) {
if (!Initialize()) {
NS_WARNING("Shader could not be initialised");
return;
}
}
NS_ASSERTION(HasInitialized(), "Attempting to activate a program that's not in use!");
mGL->fUseProgram(mProgram);
#if CHECK_CURRENT_PROGRAM
mGL->SetUserData(&sCurrentProgramKey, this);
#endif
// check and set the projection matrix
if (mIsProjectionMatrixStale) {
SetProjectionMatrix(mProjectionMatrix);
}
}
void
ShaderProgramOGL::SetUniform(GLint aLocation, float aFloatValue)
{
ASSERT_THIS_PROGRAM;
NS_ASSERTION(aLocation >= 0, "Invalid location");
mGL->fUniform1f(aLocation, aFloatValue);
}
void
ShaderProgramOGL::SetUniform(GLint aLocation, const gfxRGBA& aColor)
{
ASSERT_THIS_PROGRAM;
NS_ASSERTION(aLocation >= 0, "Invalid location");
mGL->fUniform4f(aLocation, float(aColor.r), float(aColor.g), float(aColor.b), float(aColor.a));
}
void
ShaderProgramOGL::SetUniform(GLint aLocation, int aLength, float *aFloatValues)
{
ASSERT_THIS_PROGRAM;
NS_ASSERTION(aLocation >= 0, "Invalid location");
if (aLength == 1) {
mGL->fUniform1fv(aLocation, 1, aFloatValues);
} else if (aLength == 2) {
mGL->fUniform2fv(aLocation, 1, aFloatValues);
} else if (aLength == 3) {
mGL->fUniform3fv(aLocation, 1, aFloatValues);
} else if (aLength == 4) {
mGL->fUniform4fv(aLocation, 1, aFloatValues);
} else {
NS_NOTREACHED("Bogus aLength param");
}
}
void
ShaderProgramOGL::SetUniform(GLint aLocation, GLint aIntValue)
{
ASSERT_THIS_PROGRAM;
NS_ASSERTION(aLocation >= 0, "Invalid location");
mGL->fUniform1i(aLocation, aIntValue);
}
void
ShaderProgramOGL::SetMatrixUniform(GLint aLocation, const gfx3DMatrix& aMatrix)
{
SetMatrixUniform(aLocation, &aMatrix._11);
}
void
ShaderProgramOGL::SetMatrixUniform(GLint aLocation, const float *aFloatValues)
{
ASSERT_THIS_PROGRAM;
NS_ASSERTION(aLocation >= 0, "Invalid location");
mGL->fUniformMatrix4fv(aLocation, 1, false, aFloatValues);
}
} /* layers */
} /* mozilla */

View File

@ -10,11 +10,16 @@
#include "prenv.h"
#include "nsAutoPtr.h"
#include "nsString.h"
#include "GLContext.h"
#include "GLContextTypes.h"
#include "gfx3DMatrix.h"
#include "gfxColor.h"
namespace mozilla {
namespace gl {
class GLContext;
}
namespace layers {
class Layer;
@ -140,45 +145,16 @@ class ShaderProgramOGL
public:
typedef mozilla::gl::GLContext GLContext;
ShaderProgramOGL(GLContext* aGL, const ProgramProfileOGL& aProfile) :
mIsProjectionMatrixStale(false), mGL(aGL), mProgram(0),
mProfile(aProfile), mProgramState(STATE_NEW) { }
ShaderProgramOGL(GLContext* aGL, const ProgramProfileOGL& aProfile);
~ShaderProgramOGL() {
if (mProgram <= 0) {
return;
}
nsRefPtr<GLContext> ctx = mGL->GetSharedContext();
if (!ctx) {
ctx = mGL;
}
ctx->MakeCurrent();
ctx->fDeleteProgram(mProgram);
}
~ShaderProgramOGL();
bool HasInitialized() {
NS_ASSERTION(mProgramState != STATE_OK || mProgram > 0, "Inconsistent program state");
return mProgramState == STATE_OK;
}
void Activate() {
if (mProgramState == STATE_NEW) {
if (!Initialize()) {
NS_WARNING("Shader could not be initialised");
return;
}
}
NS_ASSERTION(HasInitialized(), "Attempting to activate a program that's not in use!");
mGL->fUseProgram(mProgram);
#if CHECK_CURRENT_PROGRAM
mGL->SetUserData(&sCurrentProgramKey, this);
#endif
// check and set the projection matrix
if (mIsProjectionMatrixStale) {
SetProjectionMatrix(mProjectionMatrix);
}
}
void Activate();
bool Initialize();
@ -329,54 +305,12 @@ protected:
static int sCurrentProgramKey;
#endif
void SetUniform(GLint aLocation, float aFloatValue) {
ASSERT_THIS_PROGRAM;
NS_ASSERTION(aLocation >= 0, "Invalid location");
mGL->fUniform1f(aLocation, aFloatValue);
}
void SetUniform(GLint aLocation, const gfxRGBA& aColor) {
ASSERT_THIS_PROGRAM;
NS_ASSERTION(aLocation >= 0, "Invalid location");
mGL->fUniform4f(aLocation, float(aColor.r), float(aColor.g), float(aColor.b), float(aColor.a));
}
void SetUniform(GLint aLocation, int aLength, float *aFloatValues) {
ASSERT_THIS_PROGRAM;
NS_ASSERTION(aLocation >= 0, "Invalid location");
if (aLength == 1) {
mGL->fUniform1fv(aLocation, 1, aFloatValues);
} else if (aLength == 2) {
mGL->fUniform2fv(aLocation, 1, aFloatValues);
} else if (aLength == 3) {
mGL->fUniform3fv(aLocation, 1, aFloatValues);
} else if (aLength == 4) {
mGL->fUniform4fv(aLocation, 1, aFloatValues);
} else {
NS_NOTREACHED("Bogus aLength param");
}
}
void SetUniform(GLint aLocation, GLint aIntValue) {
ASSERT_THIS_PROGRAM;
NS_ASSERTION(aLocation >= 0, "Invalid location");
mGL->fUniform1i(aLocation, aIntValue);
}
void SetMatrixUniform(GLint aLocation, const gfx3DMatrix& aMatrix) {
SetMatrixUniform(aLocation, &aMatrix._11);
}
void SetMatrixUniform(GLint aLocation, const float *aFloatValues) {
ASSERT_THIS_PROGRAM;
NS_ASSERTION(aLocation >= 0, "Invalid location");
mGL->fUniformMatrix4fv(aLocation, 1, false, aFloatValues);
}
void SetUniform(GLint aLocation, float aFloatValue);
void SetUniform(GLint aLocation, const gfxRGBA& aColor);
void SetUniform(GLint aLocation, int aLength, float *aFloatValues);
void SetUniform(GLint aLocation, GLint aIntValue);
void SetMatrixUniform(GLint aLocation, const gfx3DMatrix& aMatrix);
void SetMatrixUniform(GLint aLocation, const float *aFloatValues);
};

View File

@ -4,6 +4,8 @@
#include "ReusableTileStoreOGL.h"
#include "GLContext.h"
namespace mozilla {
namespace layers {

View File

@ -51,7 +51,6 @@
#include "nsRegion.h"
#include "Layers.h"
#include "LayerManagerOGL.h"
#include "GLContext.h"
#include "mozilla/layers/CompositorCocoaWidgetHelper.h"
#ifdef ACCESSIBILITY
#include "nsAccessibilityService.h"
@ -1692,8 +1691,7 @@ nsChildView::CreateCompositor()
LayerManagerOGL *manager =
static_cast<LayerManagerOGL*>(compositor::GetLayerManager(mCompositorParent));
NSOpenGLContext *glContext =
(NSOpenGLContext *) manager->gl()->GetNativeData(GLContext::NativeGLContext);
NSOpenGLContext *glContext = (NSOpenGLContext *)manager->GetNSOpenGLContext();
[(ChildView *)mView setGLContext:glContext];
[(ChildView *)mView setUsingOMTCompositor:true];
@ -2457,7 +2455,7 @@ NSEvent* gLastDragMouseDownEvent = nil;
LayerManagerOGL *manager = static_cast<LayerManagerOGL*>(layerManager);
manager->SetClippingRegion(region);
glContext = (NSOpenGLContext *)manager->gl()->GetNativeData(mozilla::gl::GLContext::NativeGLContext);
glContext = (NSOpenGLContext *)manager->GetNSOpenGLContext();
if (!mGLContext) {
[self setGLContext:glContext];