2010-05-19 13:46:08 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07: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-05-19 13:46:08 -07:00
|
|
|
|
|
|
|
#include "GLContextProvider.h"
|
|
|
|
#include "GLContext.h"
|
2012-03-16 15:24:12 -07:00
|
|
|
#include "GLLibraryLoader.h"
|
2010-05-19 13:46:08 -07:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2010-06-14 11:44:12 -07:00
|
|
|
#include "nsIConsoleService.h"
|
2011-06-11 19:30:16 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-05-19 13:46:08 -07:00
|
|
|
#include "gfxASurface.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
|
2011-03-02 12:50:36 -08:00
|
|
|
#include "gfxCrashReporterUtils.h"
|
|
|
|
|
2010-05-19 13:46:08 -07:00
|
|
|
// from GL/osmesa.h. We don't include that file so as to avoid having a build-time dependency on OSMesa.
|
|
|
|
#define OSMESA_RGBA GL_RGBA
|
|
|
|
#define OSMESA_BGRA 0x1
|
|
|
|
#define OSMESA_ARGB 0x2
|
|
|
|
#define OSMESA_RGB GL_RGB
|
|
|
|
#define OSMESA_BGR 0x4
|
|
|
|
#define OSMESA_RGB_565 0x5
|
|
|
|
#define OSMESA_Y_UP 0x11
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2010-06-14 11:44:12 -07:00
|
|
|
static void LogMessage(const char *msg)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIConsoleService> console(do_GetService(NS_CONSOLESERVICE_CONTRACTID));
|
|
|
|
if (console) {
|
|
|
|
console->LogStringMessage(NS_ConvertUTF8toUTF16(nsDependentCString(msg)).get());
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-19 13:46:08 -07:00
|
|
|
typedef void* PrivateOSMesaContext;
|
|
|
|
|
|
|
|
class OSMesaLibrary
|
|
|
|
{
|
|
|
|
public:
|
2012-07-30 07:20:58 -07:00
|
|
|
OSMesaLibrary() : mInitialized(false), mOSMesaLibrary(nullptr) {}
|
2010-05-19 13:46:08 -07:00
|
|
|
|
|
|
|
typedef PrivateOSMesaContext (GLAPIENTRY * PFNOSMESACREATECONTEXTEXT) (GLenum, GLint, GLint, GLint, PrivateOSMesaContext);
|
|
|
|
typedef void (GLAPIENTRY * PFNOSMESADESTROYCONTEXT) (PrivateOSMesaContext);
|
|
|
|
typedef bool (GLAPIENTRY * PFNOSMESAMAKECURRENT) (PrivateOSMesaContext, void *, GLenum, GLsizei, GLsizei);
|
|
|
|
typedef PrivateOSMesaContext (GLAPIENTRY * PFNOSMESAGETCURRENTCONTEXT) (void);
|
|
|
|
typedef void (GLAPIENTRY * PFNOSMESAPIXELSTORE) (GLint, GLint);
|
|
|
|
typedef PRFuncPtr (GLAPIENTRY * PFNOSMESAGETPROCADDRESS) (const char*);
|
|
|
|
|
|
|
|
PFNOSMESACREATECONTEXTEXT fCreateContextExt;
|
|
|
|
PFNOSMESADESTROYCONTEXT fDestroyContext;
|
|
|
|
PFNOSMESAMAKECURRENT fMakeCurrent;
|
|
|
|
PFNOSMESAGETCURRENTCONTEXT fGetCurrentContext;
|
|
|
|
PFNOSMESAPIXELSTORE fPixelStore;
|
|
|
|
PFNOSMESAGETPROCADDRESS fGetProcAddress;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool EnsureInitialized();
|
2010-05-19 13:46:08 -07:00
|
|
|
|
|
|
|
private:
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mInitialized;
|
2010-05-19 13:46:08 -07:00
|
|
|
PRLibrary *mOSMesaLibrary;
|
|
|
|
};
|
|
|
|
|
|
|
|
OSMesaLibrary sOSMesaLibrary;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-05-19 13:46:08 -07:00
|
|
|
OSMesaLibrary::EnsureInitialized()
|
|
|
|
{
|
|
|
|
if (mInitialized)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-05-19 13:46:08 -07:00
|
|
|
|
2011-06-11 19:30:16 -07:00
|
|
|
nsAdoptingCString osmesalib = Preferences::GetCString("webgl.osmesalib");
|
|
|
|
if (osmesalib.IsEmpty()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mOSMesaLibrary = PR_LoadLibrary(osmesalib.get());
|
|
|
|
|
|
|
|
if (!mOSMesaLibrary) {
|
2010-06-14 11:44:12 -07:00
|
|
|
LogMessage("Couldn't open OSMesa lib for software rendering -- webgl.osmesalib path is incorrect, or not a valid shared library");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
2012-03-16 15:24:12 -07:00
|
|
|
GLLibraryLoader::SymLoadStruct symbols[] = {
|
2010-05-19 13:46:08 -07:00
|
|
|
{ (PRFuncPtr*) &fCreateContextExt, { "OSMesaCreateContextExt", NULL } },
|
|
|
|
{ (PRFuncPtr*) &fMakeCurrent, { "OSMesaMakeCurrent", NULL } },
|
|
|
|
{ (PRFuncPtr*) &fPixelStore, { "OSMesaPixelStore", NULL } },
|
|
|
|
{ (PRFuncPtr*) &fDestroyContext, { "OSMesaDestroyContext", NULL } },
|
|
|
|
{ (PRFuncPtr*) &fGetCurrentContext, { "OSMesaGetCurrentContext", NULL } },
|
|
|
|
{ (PRFuncPtr*) &fMakeCurrent, { "OSMesaMakeCurrent", NULL } },
|
|
|
|
{ (PRFuncPtr*) &fGetProcAddress, { "OSMesaGetProcAddress", NULL } },
|
|
|
|
{ NULL, { NULL } }
|
|
|
|
};
|
|
|
|
|
2012-03-16 15:24:12 -07:00
|
|
|
if (!GLLibraryLoader::LoadSymbols(mOSMesaLibrary, &symbols[0])) {
|
2010-06-14 11:44:12 -07:00
|
|
|
LogMessage("Couldn't find required entry points in OSMesa libary");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mInitialized = true;
|
|
|
|
return true;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class GLContextOSMesa : public GLContext
|
|
|
|
{
|
|
|
|
public:
|
2010-07-18 22:01:14 -07:00
|
|
|
GLContextOSMesa(const ContextFormat& aFormat)
|
2012-07-30 07:20:58 -07:00
|
|
|
: GLContext(aFormat, true, nullptr),
|
|
|
|
mThebesSurface(nullptr),
|
|
|
|
mContext(nullptr)
|
2010-05-19 13:46:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~GLContextOSMesa()
|
|
|
|
{
|
2010-09-21 12:41:24 -07:00
|
|
|
MarkDestroyed();
|
|
|
|
|
2010-05-19 13:46:08 -07:00
|
|
|
if (mContext)
|
|
|
|
sOSMesaLibrary.fDestroyContext(mContext);
|
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
GLContextType GetContextType() {
|
|
|
|
return ContextTypeOSMesa;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Init(const gfxIntSize &aSize)
|
2010-05-19 13:46:08 -07:00
|
|
|
{
|
|
|
|
int osmesa_format = -1;
|
|
|
|
int gfxasurface_imageformat = -1;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool format_accepted = false;
|
2010-05-19 13:46:08 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
if (mCreationFormat.red > 0 &&
|
|
|
|
mCreationFormat.green > 0 &&
|
|
|
|
mCreationFormat.blue > 0 &&
|
|
|
|
mCreationFormat.red <= 8 &&
|
|
|
|
mCreationFormat.green <= 8 &&
|
|
|
|
mCreationFormat.blue <= 8)
|
|
|
|
{
|
|
|
|
if (mCreationFormat.alpha == 0) {
|
2010-05-19 13:46:08 -07:00
|
|
|
// we can't use OSMESA_BGR because it is packed 24 bits per pixel.
|
|
|
|
// So we use OSMESA_BGRA and have to use ImageFormatRGB24
|
|
|
|
// to make sure that the dummy alpha channel is ignored.
|
|
|
|
osmesa_format = OSMESA_BGRA;
|
|
|
|
gfxasurface_imageformat = gfxASurface::ImageFormatRGB24;
|
2011-10-17 07:59:28 -07:00
|
|
|
format_accepted = true;
|
2010-07-18 22:01:14 -07:00
|
|
|
} else if (mCreationFormat.alpha <= 8) {
|
|
|
|
osmesa_format = OSMESA_BGRA;
|
|
|
|
gfxasurface_imageformat = gfxASurface::ImageFormatARGB32;
|
2011-10-17 07:59:28 -07:00
|
|
|
format_accepted = true;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!format_accepted) {
|
|
|
|
NS_WARNING("Pixel format not supported with OSMesa.");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mThebesSurface = new gfxImageSurface(aSize, gfxASurface::gfxImageFormat(gfxasurface_imageformat));
|
|
|
|
if (mThebesSurface->CairoStatus() != 0) {
|
|
|
|
NS_WARNING("image surface failed");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
mContext = sOSMesaLibrary.fCreateContextExt(osmesa_format, mCreationFormat.depth, mCreationFormat.stencil, 0, NULL);
|
2010-05-19 13:46:08 -07:00
|
|
|
if (!mContext) {
|
|
|
|
NS_WARNING("OSMesaCreateContextExt failed!");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
if (!MakeCurrent()) return false;
|
|
|
|
if (!SetupLookupFunction()) return false;
|
2010-05-23 14:36:49 -07:00
|
|
|
|
|
|
|
// OSMesa's different from the other GL providers, it renders to an image surface, not to a pbuffer
|
|
|
|
sOSMesaLibrary.fPixelStore(OSMESA_Y_UP, 0);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return InitWithPrefix("gl", true);
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
2012-05-12 16:23:56 -07:00
|
|
|
bool MakeCurrentImpl(bool aForce = false)
|
2010-05-19 13:46:08 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool succeeded
|
2010-07-18 22:01:14 -07:00
|
|
|
= sOSMesaLibrary.fMakeCurrent(mContext, mThebesSurface->Data(),
|
|
|
|
LOCAL_GL_UNSIGNED_BYTE,
|
|
|
|
mThebesSurface->Width(),
|
|
|
|
mThebesSurface->Height());
|
2010-05-19 13:46:08 -07:00
|
|
|
NS_ASSERTION(succeeded, "Failed to make OSMesa context current!");
|
|
|
|
|
|
|
|
return succeeded;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool SetupLookupFunction()
|
2010-05-19 13:46:08 -07:00
|
|
|
{
|
|
|
|
mLookupFunc = (PlatformLookupFunction)sOSMesaLibrary.fGetProcAddress;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void *GetNativeData(NativeDataType aType)
|
|
|
|
{
|
|
|
|
switch (aType) {
|
|
|
|
case NativeImageSurface:
|
|
|
|
return mThebesSurface.get();
|
|
|
|
default:
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 19:57:29 -08:00
|
|
|
bool SupportsRobustness()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-19 13:46:08 -07:00
|
|
|
private:
|
|
|
|
nsRefPtr<gfxImageSurface> mThebesSurface;
|
|
|
|
PrivateOSMesaContext mContext;
|
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<GLContext>
|
|
|
|
GLContextProviderOSMesa::CreateForWindow(nsIWidget *aWidget)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-06-23 02:24:31 -07:00
|
|
|
already_AddRefed<GLContext>
|
2010-07-18 22:01:14 -07:00
|
|
|
GLContextProviderOSMesa::CreateOffscreen(const gfxIntSize& aSize,
|
2012-03-21 16:13:59 -07:00
|
|
|
const ContextFormat& aFormat,
|
|
|
|
const ContextFlags)
|
2010-05-19 13:46:08 -07:00
|
|
|
{
|
|
|
|
if (!sOSMesaLibrary.EnsureInitialized()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-10-19 12:09:57 -07:00
|
|
|
ContextFormat actualFormat(aFormat);
|
|
|
|
actualFormat.samples = 0;
|
|
|
|
|
|
|
|
nsRefPtr<GLContextOSMesa> glContext = new GLContextOSMesa(actualFormat);
|
2010-05-19 13:46:08 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
if (!glContext->Init(aSize))
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
GLContext *
|
2012-06-02 09:05:45 -07:00
|
|
|
GLContextProviderOSMesa::GetGlobalContext(const ContextFlags)
|
2010-07-18 22:01:14 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-19 13:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-07-19 21:05:42 -07:00
|
|
|
void
|
|
|
|
GLContextProviderOSMesa::Shutdown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-05-19 13:46:08 -07:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|