2010-05-24 23:35:35 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
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-03-29 21:48:52 -07:00
|
|
|
|
|
|
|
#include "ImageLayerOGL.h"
|
2013-08-11 16:17:23 -07:00
|
|
|
#include <stdint.h> // for uint32_t
|
|
|
|
#include "mozilla-config.h" // for GL_PROVIDER_GLX
|
2013-09-06 19:11:41 -07:00
|
|
|
#include "GLContext.h" // for GLContext, etc
|
2013-08-11 16:17:23 -07:00
|
|
|
#include "ImageContainer.h" // for CairoImage, etc
|
|
|
|
#include "ImageTypes.h" // for ImageFormat::CAIRO_SURFACE, etc
|
|
|
|
#include "SharedTextureImage.h" // for SharedTextureImage::Data, etc
|
|
|
|
#include "gfx3DMatrix.h" // for gfx3DMatrix
|
|
|
|
#include "gfxASurface.h" // for gfxASurface, etc
|
|
|
|
#include "gfxImageSurface.h" // for gfxImageSurface
|
|
|
|
#include "gfxUtils.h" // for NextPowerOfTwo
|
|
|
|
#include "mozilla/gfx/BaseSize.h" // for BaseSize
|
|
|
|
#include "mozilla/gfx/Types.h" // for SurfaceFormat
|
|
|
|
#include "mozilla/layers/LayersTypes.h"
|
|
|
|
#include "nsAutoRef.h" // for nsCountedRef, nsAutoRefBase
|
|
|
|
#include "nsCOMPtr.h" // for nsCOMPtr, already_AddRefed
|
|
|
|
#include "nsDebug.h" // for NS_ASSERTION, NS_ERROR
|
|
|
|
#include "nsIRunnable.h" // for nsIRunnable
|
|
|
|
#include "nsPoint.h" // for nsIntPoint
|
|
|
|
#include "nsRect.h" // for nsIntRect
|
|
|
|
#include "nsSize.h" // for nsIntSize
|
|
|
|
#include "nsThreadUtils.h" // for nsRunnable
|
|
|
|
#include "nscore.h" // for NS_IMETHOD
|
|
|
|
#include "LayerManagerOGL.h" // for LayerOGL::GLContext, etc
|
2012-11-26 00:08:22 -08:00
|
|
|
#if defined(GL_PROVIDER_GLX)
|
2011-04-07 16:58:08 -07:00
|
|
|
# include "GLXLibrary.h"
|
2012-09-04 16:10:22 -07:00
|
|
|
# include "gfxXlibSurface.h"
|
2011-04-07 16:58:08 -07:00
|
|
|
#endif
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2012-02-18 18:23:06 -08:00
|
|
|
using namespace mozilla::gfx;
|
2010-05-16 16:08:01 -07:00
|
|
|
using namespace mozilla::gl;
|
|
|
|
|
2010-03-29 21:48:52 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2013-08-11 16:17:23 -07:00
|
|
|
class Layer;
|
|
|
|
|
2010-05-16 16:08:01 -07:00
|
|
|
/**
|
|
|
|
* This is an event used to unref a GLContext on the main thread and
|
|
|
|
* optionally delete a texture associated with that context.
|
|
|
|
*/
|
|
|
|
class TextureDeleter : public nsRunnable {
|
|
|
|
public:
|
|
|
|
TextureDeleter(already_AddRefed<GLContext> aContext,
|
|
|
|
GLuint aTexture)
|
|
|
|
: mContext(aContext), mTexture(aTexture)
|
|
|
|
{
|
2010-08-06 22:09:18 -07:00
|
|
|
NS_ASSERTION(aTexture, "TextureDeleter instantiated with nothing to do");
|
2010-05-16 16:08:01 -07:00
|
|
|
}
|
2010-08-06 22:09:18 -07:00
|
|
|
|
2010-05-16 16:08:01 -07:00
|
|
|
NS_IMETHOD Run() {
|
2010-08-06 22:09:18 -07:00
|
|
|
mContext->MakeCurrent();
|
|
|
|
mContext->fDeleteTextures(1, &mTexture);
|
|
|
|
|
2010-05-16 16:08:01 -07:00
|
|
|
// Ensure context is released on the main thread
|
2012-07-30 07:20:58 -07:00
|
|
|
mContext = nullptr;
|
2010-05-16 16:08:01 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<GLContext> mContext;
|
|
|
|
GLuint mTexture;
|
|
|
|
};
|
|
|
|
|
2013-09-06 19:11:41 -07:00
|
|
|
GLTexture::GLTexture()
|
|
|
|
: mTexture(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GLTexture::~GLTexture()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2010-05-16 16:08:01 -07:00
|
|
|
void
|
|
|
|
GLTexture::Allocate(GLContext *aContext)
|
|
|
|
{
|
2012-04-10 13:20:02 -07:00
|
|
|
NS_ASSERTION(aContext->IsGlobalSharedContext() || aContext->IsOwningThreadCurrent(),
|
|
|
|
"Can only allocate texture on context's owning thread or with cx sharing");
|
2010-05-16 16:08:01 -07:00
|
|
|
|
|
|
|
Release();
|
|
|
|
|
|
|
|
mContext = aContext;
|
2010-08-06 22:09:18 -07:00
|
|
|
|
|
|
|
mContext->MakeCurrent();
|
|
|
|
mContext->fGenTextures(1, &mTexture);
|
2010-05-16 16:08:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLTexture::TakeFrom(GLTexture *aOther)
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
|
|
|
|
mContext = aOther->mContext.forget();
|
|
|
|
mTexture = aOther->mTexture;
|
|
|
|
aOther->mTexture = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLTexture::Release()
|
|
|
|
{
|
|
|
|
if (!mContext) {
|
|
|
|
NS_ASSERTION(!mTexture, "Can't delete texture without a context");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:13:25 -07:00
|
|
|
if (mContext->IsDestroyed() && !mContext->IsGlobalSharedContext()) {
|
|
|
|
mContext = mContext->GetSharedContext();
|
|
|
|
if (!mContext) {
|
|
|
|
NS_ASSERTION(!mTexture,
|
|
|
|
"Context has been destroyed and couldn't find a shared context!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-06 22:09:18 -07:00
|
|
|
if (mTexture) {
|
2012-04-10 13:20:02 -07:00
|
|
|
if (mContext->IsOwningThreadCurrent() || mContext->IsGlobalSharedContext()) {
|
2010-08-06 22:09:18 -07:00
|
|
|
mContext->MakeCurrent();
|
|
|
|
mContext->fDeleteTextures(1, &mTexture);
|
|
|
|
} else {
|
2013-04-22 04:15:59 -07:00
|
|
|
already_AddRefed<GLContext> context = mContext.forget();
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new TextureDeleter(context, mTexture);
|
|
|
|
context.get()->DispatchToOwningThread(runnable);
|
2010-05-16 16:08:01 -07:00
|
|
|
}
|
2010-08-06 22:09:18 -07:00
|
|
|
|
|
|
|
mTexture = 0;
|
2010-05-16 16:08:01 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mContext = nullptr;
|
2010-05-16 16:08:01 -07:00
|
|
|
}
|
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
TextureRecycleBin::TextureRecycleBin()
|
|
|
|
: mLock("mozilla.layers.TextureRecycleBin.mLock")
|
2010-05-16 16:08:41 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-01-31 18:18:30 -08:00
|
|
|
TextureRecycleBin::RecycleTexture(GLTexture *aTexture, TextureType aType,
|
2010-05-16 16:09:28 -07:00
|
|
|
const gfxIntSize& aSize)
|
|
|
|
{
|
2010-10-12 14:01:59 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2010-05-16 16:09:28 -07:00
|
|
|
if (!aTexture->IsAllocated())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!mRecycledTextures[aType].IsEmpty() && aSize != mRecycledTextureSizes[aType]) {
|
|
|
|
mRecycledTextures[aType].Clear();
|
|
|
|
}
|
|
|
|
mRecycledTextureSizes[aType] = aSize;
|
|
|
|
mRecycledTextures[aType].AppendElement()->TakeFrom(aTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-01-31 18:18:30 -08:00
|
|
|
TextureRecycleBin::GetTexture(TextureType aType, const gfxIntSize& aSize,
|
2010-08-06 22:09:18 -07:00
|
|
|
GLContext *aContext, GLTexture *aOutTexture)
|
2010-05-16 16:09:28 -07:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (mRecycledTextures[aType].IsEmpty() || mRecycledTextureSizes[aType] != aSize) {
|
|
|
|
aOutTexture->Allocate(aContext);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t last = mRecycledTextures[aType].Length() - 1;
|
2010-05-16 16:09:28 -07:00
|
|
|
aOutTexture->TakeFrom(&mRecycledTextures[aType].ElementAt(last));
|
|
|
|
mRecycledTextures[aType].RemoveElementAt(last);
|
|
|
|
}
|
|
|
|
|
2013-05-29 14:59:24 -07:00
|
|
|
struct ImageOGLBackendData : public ImageBackendData
|
2010-04-08 00:29:55 -07:00
|
|
|
{
|
2012-01-31 18:18:30 -08:00
|
|
|
GLTexture mTexture;
|
|
|
|
};
|
2010-08-06 22:09:18 -07:00
|
|
|
|
2013-04-16 19:21:06 -07:00
|
|
|
void
|
|
|
|
AllocateTextureSharedTexture(SharedTextureImage *aTexImage, mozilla::gl::GLContext* aGL, GLenum aTarget)
|
|
|
|
{
|
|
|
|
nsAutoPtr<ImageOGLBackendData> backendData(
|
|
|
|
new ImageOGLBackendData);
|
|
|
|
|
|
|
|
backendData->mTexture.Allocate(aGL);
|
|
|
|
|
|
|
|
aGL->fBindTexture(aTarget, backendData->mTexture.GetTextureID());
|
|
|
|
aGL->fTexParameteri(aTarget, LOCAL_GL_TEXTURE_MIN_FILTER, LOCAL_GL_LINEAR);
|
|
|
|
aGL->fTexParameteri(aTarget, LOCAL_GL_TEXTURE_MAG_FILTER, LOCAL_GL_LINEAR);
|
|
|
|
aGL->fTexParameteri(aTarget, LOCAL_GL_TEXTURE_WRAP_S, LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
aGL->fTexParameteri(aTarget, LOCAL_GL_TEXTURE_WRAP_T, LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
|
|
|
|
aTexImage->SetBackendData(LAYERS_OPENGL, backendData.forget());
|
|
|
|
}
|
|
|
|
|
2010-03-29 21:48:52 -07:00
|
|
|
Layer*
|
|
|
|
ImageLayerOGL::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-05-24 23:35:35 -07:00
|
|
|
ImageLayerOGL::RenderLayer(int,
|
2010-11-08 01:06:15 -08:00
|
|
|
const nsIntPoint& aOffset)
|
2010-03-29 21:48:52 -07:00
|
|
|
{
|
2012-03-05 10:09:05 -08:00
|
|
|
nsRefPtr<ImageContainer> container = GetContainer();
|
|
|
|
|
2012-08-13 03:10:10 -07:00
|
|
|
if (!container || mOGLManager->CompositingDisabled())
|
2010-03-29 21:48:52 -07:00
|
|
|
return;
|
|
|
|
|
2010-05-24 23:35:35 -07:00
|
|
|
mOGLManager->MakeCurrent();
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2012-03-05 10:09:05 -08:00
|
|
|
AutoLockImage autoLock(container);
|
|
|
|
|
|
|
|
Image *image = autoLock.GetImage();
|
2011-01-19 00:27:54 -08:00
|
|
|
if (!image) {
|
|
|
|
return;
|
|
|
|
}
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2012-08-19 19:39:10 -07:00
|
|
|
NS_ASSERTION(image->GetFormat() != REMOTE_IMAGE_BITMAP,
|
2012-03-05 10:09:05 -08:00
|
|
|
"Remote images aren't handled yet in OGL layers!");
|
|
|
|
|
2012-08-19 19:39:10 -07:00
|
|
|
if (image->GetFormat() == PLANAR_YCBCR) {
|
2012-01-31 18:18:30 -08:00
|
|
|
PlanarYCbCrImage *yuvImage =
|
2012-03-05 10:09:05 -08:00
|
|
|
static_cast<PlanarYCbCrImage*>(image);
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2012-08-21 03:18:20 -07:00
|
|
|
if (!yuvImage->IsValid()) {
|
2010-03-29 21:48:52 -07:00
|
|
|
return;
|
2010-08-06 22:09:18 -07:00
|
|
|
}
|
2012-01-31 18:18:30 -08:00
|
|
|
|
|
|
|
PlanarYCbCrOGLBackendData *data =
|
2012-07-18 09:31:40 -07:00
|
|
|
static_cast<PlanarYCbCrOGLBackendData*>(yuvImage->GetBackendData(LAYERS_OPENGL));
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2012-03-21 22:48:41 -07:00
|
|
|
if (data && data->mTextures->GetGLContext() != gl()) {
|
|
|
|
// If these textures were allocated by another layer manager,
|
|
|
|
// clear them out and re-allocate below.
|
2012-07-30 07:20:58 -07:00
|
|
|
data = nullptr;
|
|
|
|
yuvImage->SetBackendData(LAYERS_OPENGL, nullptr);
|
2012-03-21 22:48:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
AllocateTexturesYCbCr(yuvImage);
|
2012-07-18 09:31:40 -07:00
|
|
|
data = static_cast<PlanarYCbCrOGLBackendData*>(yuvImage->GetBackendData(LAYERS_OPENGL));
|
2012-03-21 22:48:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!data || data->mTextures->GetGLContext() != gl()) {
|
2012-01-31 18:18:30 -08:00
|
|
|
// XXX - Can this ever happen? If so I need to fix this!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl()->MakeCurrent();
|
2012-04-30 11:51:05 -07:00
|
|
|
gl()->fActiveTexture(LOCAL_GL_TEXTURE2);
|
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, data->mTextures[2].GetTextureID());
|
2011-10-05 11:20:33 -07:00
|
|
|
gl()->ApplyFilterToBoundTexture(mFilter);
|
2010-04-26 19:09:47 -07:00
|
|
|
gl()->fActiveTexture(LOCAL_GL_TEXTURE1);
|
2012-01-31 18:18:30 -08:00
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, data->mTextures[1].GetTextureID());
|
2011-10-05 11:20:33 -07:00
|
|
|
gl()->ApplyFilterToBoundTexture(mFilter);
|
2012-04-30 11:51:05 -07:00
|
|
|
gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
|
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, data->mTextures[0].GetTextureID());
|
2011-10-05 11:20:33 -07:00
|
|
|
gl()->ApplyFilterToBoundTexture(mFilter);
|
2013-07-04 10:25:50 -07:00
|
|
|
|
2012-03-18 13:08:53 -07:00
|
|
|
ShaderProgramOGL *program = mOGLManager->GetProgram(YCbCrLayerProgramType,
|
|
|
|
GetMaskLayer());
|
2010-05-24 23:35:35 -07:00
|
|
|
|
|
|
|
program->Activate();
|
|
|
|
program->SetLayerQuadRect(nsIntRect(0, 0,
|
2012-08-21 03:18:20 -07:00
|
|
|
yuvImage->GetSize().width,
|
|
|
|
yuvImage->GetSize().height));
|
2010-11-08 01:06:15 -08:00
|
|
|
program->SetLayerTransform(GetEffectiveTransform());
|
2013-07-18 20:05:06 -07:00
|
|
|
program->SetTextureTransform(gfx3DMatrix());
|
2010-11-08 01:06:15 -08:00
|
|
|
program->SetLayerOpacity(GetEffectiveOpacity());
|
2010-05-24 23:35:35 -07:00
|
|
|
program->SetRenderOffset(aOffset);
|
|
|
|
program->SetYCbCrTextureUnits(0, 1, 2);
|
2012-03-18 13:08:53 -07:00
|
|
|
program->LoadMask(GetMaskLayer());
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2011-07-04 19:52:00 -07:00
|
|
|
mOGLManager->BindAndDrawQuadWithTextureRect(program,
|
2012-08-21 03:18:20 -07:00
|
|
|
yuvImage->GetData()->GetPictureRect(),
|
|
|
|
nsIntSize(yuvImage->GetData()->mYSize.width,
|
|
|
|
yuvImage->GetData()->mYSize.height));
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2010-05-24 23:35:35 -07:00
|
|
|
// We shouldn't need to do this, but do it anyway just in case
|
|
|
|
// someone else forgets.
|
|
|
|
gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
|
2012-08-19 19:39:10 -07:00
|
|
|
} else if (image->GetFormat() == CAIRO_SURFACE) {
|
2012-01-31 18:18:30 -08:00
|
|
|
CairoImage *cairoImage =
|
2012-03-05 10:09:05 -08:00
|
|
|
static_cast<CairoImage*>(image);
|
2012-01-31 18:18:30 -08:00
|
|
|
|
|
|
|
if (!cairoImage->mSurface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-25 19:43:31 -07:00
|
|
|
NS_ASSERTION(cairoImage->mSurface->GetContentType() != gfxASurface::CONTENT_ALPHA,
|
|
|
|
"Image layer has alpha image");
|
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
CairoOGLBackendData *data =
|
2012-07-18 09:31:40 -07:00
|
|
|
static_cast<CairoOGLBackendData*>(cairoImage->GetBackendData(LAYERS_OPENGL));
|
2012-01-31 18:18:30 -08:00
|
|
|
|
2012-03-21 22:48:41 -07:00
|
|
|
if (data && data->mTexture.GetGLContext() != gl()) {
|
|
|
|
// If this texture was allocated by another layer manager, clear
|
|
|
|
// it out and re-allocate below.
|
2012-07-30 07:20:58 -07:00
|
|
|
data = nullptr;
|
|
|
|
cairoImage->SetBackendData(LAYERS_OPENGL, nullptr);
|
2012-03-21 22:48:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
AllocateTexturesCairo(cairoImage);
|
2012-07-18 09:31:40 -07:00
|
|
|
data = static_cast<CairoOGLBackendData*>(cairoImage->GetBackendData(LAYERS_OPENGL));
|
2012-03-21 22:48:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!data || data->mTexture.GetGLContext() != gl()) {
|
2012-01-31 18:18:30 -08:00
|
|
|
// XXX - Can this ever happen? If so I need to fix this!
|
|
|
|
return;
|
|
|
|
}
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2011-05-19 03:53:02 -07:00
|
|
|
gl()->MakeCurrent();
|
|
|
|
|
2010-05-24 23:35:35 -07:00
|
|
|
gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
|
2012-01-31 18:18:30 -08:00
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, data->mTexture.GetTextureID());
|
2010-11-04 23:40:19 -07:00
|
|
|
|
2013-07-04 10:25:50 -07:00
|
|
|
ShaderProgramOGL *program = mOGLManager->GetProgram(data->mLayerProgram, GetMaskLayer());
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2011-10-05 11:20:33 -07:00
|
|
|
gl()->ApplyFilterToBoundTexture(mFilter);
|
2010-09-21 09:32:17 -07:00
|
|
|
|
2010-03-29 21:48:52 -07:00
|
|
|
program->Activate();
|
2012-07-16 19:12:58 -07:00
|
|
|
program->SetLayerQuadRect(nsIntRect(0, 0,
|
|
|
|
cairoImage->GetSize().width,
|
|
|
|
cairoImage->GetSize().height));
|
2010-11-08 01:06:15 -08:00
|
|
|
program->SetLayerTransform(GetEffectiveTransform());
|
2013-07-18 20:05:06 -07:00
|
|
|
program->SetTextureTransform(gfx3DMatrix());
|
2010-11-08 01:06:15 -08:00
|
|
|
program->SetLayerOpacity(GetEffectiveOpacity());
|
2010-05-24 23:35:35 -07:00
|
|
|
program->SetRenderOffset(aOffset);
|
|
|
|
program->SetTextureUnit(0);
|
2012-03-18 13:08:53 -07:00
|
|
|
program->LoadMask(GetMaskLayer());
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2012-09-01 08:35:56 -07:00
|
|
|
mOGLManager->BindAndDrawQuad(program);
|
2013-04-16 19:21:06 -07:00
|
|
|
} else if (image->GetFormat() == SHARED_TEXTURE) {
|
|
|
|
SharedTextureImage* texImage =
|
|
|
|
static_cast<SharedTextureImage*>(image);
|
|
|
|
const SharedTextureImage::Data* data = texImage->GetData();
|
|
|
|
GLContext::SharedHandleDetails handleDetails;
|
|
|
|
if (!gl()->GetSharedHandleDetails(data->mShareType, data->mHandle, handleDetails)) {
|
|
|
|
NS_ERROR("Failed to get shared handle details");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-04 10:25:50 -07:00
|
|
|
ShaderProgramType programType =
|
|
|
|
ShaderProgramFromTargetAndFormat(handleDetails.mTarget,
|
|
|
|
handleDetails.mTextureFormat);
|
|
|
|
ShaderProgramOGL* program = mOGLManager->GetProgram(programType, GetMaskLayer());
|
2013-04-16 19:21:06 -07:00
|
|
|
|
|
|
|
program->Activate();
|
2013-07-04 10:25:50 -07:00
|
|
|
if (programType == RGBARectLayerProgramType) {
|
2013-04-16 19:21:06 -07:00
|
|
|
// 2DRect case, get the multiplier right for a sampler2DRect
|
|
|
|
program->SetTexCoordMultiplier(data->mSize.width, data->mSize.height);
|
|
|
|
}
|
|
|
|
program->SetLayerTransform(GetEffectiveTransform());
|
2013-07-18 20:05:06 -07:00
|
|
|
program->SetTextureTransform(gfx3DMatrix());
|
2013-04-16 19:21:06 -07:00
|
|
|
program->SetLayerOpacity(GetEffectiveOpacity());
|
|
|
|
program->SetRenderOffset(aOffset);
|
|
|
|
program->SetTextureUnit(0);
|
|
|
|
program->SetTextureTransform(handleDetails.mTextureTransform);
|
|
|
|
program->LoadMask(GetMaskLayer());
|
|
|
|
|
|
|
|
if (!texImage->GetBackendData(LAYERS_OPENGL)) {
|
|
|
|
AllocateTextureSharedTexture(texImage, gl(), handleDetails.mTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageOGLBackendData *backendData =
|
|
|
|
static_cast<ImageOGLBackendData*>(texImage->GetBackendData(LAYERS_OPENGL));
|
|
|
|
gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
|
|
|
|
gl()->fBindTexture(handleDetails.mTarget, backendData->mTexture.GetTextureID());
|
|
|
|
|
|
|
|
if (!gl()->AttachSharedHandle(data->mShareType, data->mHandle)) {
|
|
|
|
NS_ERROR("Failed to bind shared texture handle");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl()->ApplyFilterToBoundTexture(handleDetails.mTarget, mFilter);
|
|
|
|
program->SetLayerQuadRect(nsIntRect(nsIntPoint(0, 0), data->mSize));
|
|
|
|
mOGLManager->BindAndDrawQuad(program, data->mInverted);
|
|
|
|
gl()->fBindTexture(handleDetails.mTarget, 0);
|
|
|
|
gl()->DetachSharedHandle(data->mShareType, data->mHandle);
|
2010-03-29 21:48:52 -07:00
|
|
|
}
|
2011-03-23 15:28:57 -07:00
|
|
|
GetContainer()->NotifyPaintedImage(image);
|
2010-03-29 21:48:52 -07:00
|
|
|
}
|
|
|
|
|
2010-08-06 22:09:18 -07:00
|
|
|
static void
|
2012-03-22 01:02:29 -07:00
|
|
|
SetClamping(GLContext* aGL, GLuint aTexture)
|
2010-08-06 22:09:18 -07:00
|
|
|
{
|
|
|
|
aGL->fBindTexture(LOCAL_GL_TEXTURE_2D, aTexture);
|
|
|
|
aGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_S, LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
aGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_T, LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
}
|
|
|
|
|
2011-04-20 21:38:39 -07:00
|
|
|
static void
|
|
|
|
UploadYUVToTexture(GLContext* gl, const PlanarYCbCrImage::Data& aData,
|
|
|
|
GLTexture* aYTexture,
|
|
|
|
GLTexture* aUTexture,
|
|
|
|
GLTexture* aVTexture)
|
2010-08-06 22:09:18 -07:00
|
|
|
{
|
2011-07-04 19:52:00 -07:00
|
|
|
nsIntRect size(0, 0, aData.mYSize.width, aData.mYSize.height);
|
|
|
|
GLuint texture = aYTexture->GetTextureID();
|
|
|
|
nsRefPtr<gfxASurface> surf = new gfxImageSurface(aData.mYChannel,
|
|
|
|
aData.mYSize,
|
|
|
|
aData.mYStride,
|
|
|
|
gfxASurface::ImageFormatA8);
|
2012-01-29 16:41:48 -08:00
|
|
|
gl->UploadSurfaceToTexture(surf, size, texture, true);
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2011-07-04 19:52:00 -07:00
|
|
|
size = nsIntRect(0, 0, aData.mCbCrSize.width, aData.mCbCrSize.height);
|
|
|
|
texture = aUTexture->GetTextureID();
|
|
|
|
surf = new gfxImageSurface(aData.mCbChannel,
|
|
|
|
aData.mCbCrSize,
|
|
|
|
aData.mCbCrStride,
|
|
|
|
gfxASurface::ImageFormatA8);
|
2012-01-29 16:41:48 -08:00
|
|
|
gl->UploadSurfaceToTexture(surf, size, texture, true);
|
2011-07-04 19:52:00 -07:00
|
|
|
|
|
|
|
texture = aVTexture->GetTextureID();
|
|
|
|
surf = new gfxImageSurface(aData.mCrChannel,
|
|
|
|
aData.mCbCrSize,
|
|
|
|
aData.mCbCrStride,
|
|
|
|
gfxASurface::ImageFormatA8);
|
2012-01-29 16:41:48 -08:00
|
|
|
gl->UploadSurfaceToTexture(surf, size, texture, true);
|
2010-03-29 21:48:52 -07:00
|
|
|
}
|
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
ImageLayerOGL::ImageLayerOGL(LayerManagerOGL *aManager)
|
2013-07-20 01:48:55 -07:00
|
|
|
: ImageLayer(aManager, nullptr)
|
2012-01-31 18:18:30 -08:00
|
|
|
, LayerOGL(aManager)
|
|
|
|
, mTextureRecycleBin(new TextureRecycleBin())
|
|
|
|
{
|
|
|
|
mImplData = static_cast<LayerOGL*>(this);
|
|
|
|
}
|
|
|
|
|
2011-04-20 21:38:39 -07:00
|
|
|
void
|
2012-01-31 18:18:30 -08:00
|
|
|
ImageLayerOGL::AllocateTexturesYCbCr(PlanarYCbCrImage *aImage)
|
2011-04-20 21:38:39 -07:00
|
|
|
{
|
2012-08-21 03:18:20 -07:00
|
|
|
if (!aImage->IsValid())
|
2011-04-20 21:38:39 -07:00
|
|
|
return;
|
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
nsAutoPtr<PlanarYCbCrOGLBackendData> backendData(
|
|
|
|
new PlanarYCbCrOGLBackendData);
|
2011-04-20 21:38:39 -07:00
|
|
|
|
2012-08-21 03:18:20 -07:00
|
|
|
const PlanarYCbCrImage::Data *data = aImage->GetData();
|
2010-05-16 16:08:01 -07:00
|
|
|
|
2012-03-21 22:48:41 -07:00
|
|
|
gl()->MakeCurrent();
|
2012-07-18 09:31:40 -07:00
|
|
|
|
2012-08-21 03:18:20 -07:00
|
|
|
mTextureRecycleBin->GetTexture(TextureRecycleBin::TEXTURE_Y, data->mYSize, gl(), &backendData->mTextures[0]);
|
2012-03-22 01:02:29 -07:00
|
|
|
SetClamping(gl(), backendData->mTextures[0].GetTextureID());
|
2012-01-31 18:18:30 -08:00
|
|
|
|
2012-08-21 03:18:20 -07:00
|
|
|
mTextureRecycleBin->GetTexture(TextureRecycleBin::TEXTURE_C, data->mCbCrSize, gl(), &backendData->mTextures[1]);
|
2012-03-22 01:02:29 -07:00
|
|
|
SetClamping(gl(), backendData->mTextures[1].GetTextureID());
|
2012-01-31 18:18:30 -08:00
|
|
|
|
2012-08-21 03:18:20 -07:00
|
|
|
mTextureRecycleBin->GetTexture(TextureRecycleBin::TEXTURE_C, data->mCbCrSize, gl(), &backendData->mTextures[2]);
|
2012-03-22 01:02:29 -07:00
|
|
|
SetClamping(gl(), backendData->mTextures[2].GetTextureID());
|
2012-01-31 18:18:30 -08:00
|
|
|
|
2012-08-21 03:18:20 -07:00
|
|
|
UploadYUVToTexture(gl(), *data,
|
2012-01-31 18:18:30 -08:00
|
|
|
&backendData->mTextures[0],
|
|
|
|
&backendData->mTextures[1],
|
|
|
|
&backendData->mTextures[2]);
|
|
|
|
|
2012-08-21 03:18:20 -07:00
|
|
|
backendData->mYSize = data->mYSize;
|
|
|
|
backendData->mCbCrSize = data->mCbCrSize;
|
2012-01-31 18:18:30 -08:00
|
|
|
backendData->mTextureRecycleBin = mTextureRecycleBin;
|
|
|
|
|
2012-07-18 09:31:40 -07:00
|
|
|
aImage->SetBackendData(LAYERS_OPENGL, backendData.forget());
|
2010-03-29 21:48:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-01-31 18:18:30 -08:00
|
|
|
ImageLayerOGL::AllocateTexturesCairo(CairoImage *aImage)
|
2010-03-29 21:48:52 -07:00
|
|
|
{
|
2012-01-31 18:18:30 -08:00
|
|
|
nsAutoPtr<CairoOGLBackendData> backendData(
|
|
|
|
new CairoOGLBackendData);
|
|
|
|
|
|
|
|
GLTexture &texture = backendData->mTexture;
|
2011-04-07 16:58:08 -07:00
|
|
|
|
2012-03-21 22:48:41 -07:00
|
|
|
texture.Allocate(gl());
|
2012-01-31 18:18:30 -08:00
|
|
|
|
|
|
|
if (!texture.IsAllocated()) {
|
2010-05-16 16:08:01 -07:00
|
|
|
return;
|
2012-01-31 18:18:30 -08:00
|
|
|
}
|
2010-05-16 16:08:01 -07:00
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
mozilla::gl::GLContext *gl = texture.GetGLContext();
|
2010-05-16 16:08:01 -07:00
|
|
|
gl->MakeCurrent();
|
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
GLuint tex = texture.GetTextureID();
|
2011-01-13 05:20:46 -08:00
|
|
|
gl->fActiveTexture(LOCAL_GL_TEXTURE0);
|
2010-06-23 07:03:31 -07:00
|
|
|
|
2012-03-22 01:02:59 -07:00
|
|
|
SetClamping(gl, tex);
|
|
|
|
|
2013-04-29 20:16:04 -07:00
|
|
|
#if defined(GL_PROVIDER_GLX)
|
2012-09-04 16:10:22 -07:00
|
|
|
if (aImage->mSurface->GetType() == gfxASurface::SurfaceTypeXlib) {
|
|
|
|
gfxXlibSurface *xsurf =
|
|
|
|
static_cast<gfxXlibSurface*>(aImage->mSurface.get());
|
|
|
|
GLXPixmap pixmap = xsurf->GetGLXPixmap();
|
|
|
|
if (pixmap) {
|
2013-07-04 10:25:50 -07:00
|
|
|
backendData->mLayerProgram = ShaderProgramFromContentType(aImage->mSurface->GetContentType());
|
2012-09-17 06:17:02 -07:00
|
|
|
|
2012-09-04 16:10:22 -07:00
|
|
|
aImage->SetBackendData(LAYERS_OPENGL, backendData.forget());
|
|
|
|
|
|
|
|
sDefGLXLib.BindTexImage(pixmap);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2011-04-07 16:58:08 -07:00
|
|
|
}
|
|
|
|
#endif
|
2013-07-04 10:25:50 -07:00
|
|
|
gfx::SurfaceFormat format =
|
2012-01-31 18:18:30 -08:00
|
|
|
gl->UploadSurfaceToTexture(aImage->mSurface,
|
|
|
|
nsIntRect(0,0, aImage->mSize.width, aImage->mSize.height),
|
2012-01-29 16:41:48 -08:00
|
|
|
tex, true);
|
2013-07-04 10:25:50 -07:00
|
|
|
backendData->mLayerProgram = ShaderProgramFromSurfaceFormat(format);
|
2012-01-31 18:18:30 -08:00
|
|
|
|
2012-07-18 09:31:40 -07:00
|
|
|
aImage->SetBackendData(LAYERS_OPENGL, backendData.forget());
|
2010-03-29 21:48:52 -07:00
|
|
|
}
|
|
|
|
|
2012-03-18 13:08:49 -07:00
|
|
|
/*
|
|
|
|
* Returns a size that is larger than and closest to aSize where both
|
|
|
|
* width and height are powers of two.
|
|
|
|
* If the OpenGL setup is capable of using non-POT textures, then it
|
|
|
|
* will just return aSize.
|
|
|
|
*/
|
Bug 825928: Land layers refactoring. r=jrmuizel,bas,nical,mattwoodrow,roc,nrc,benwa,bjacob,jgilbert,kchen CLOSED TREE
Please contact Bas Schouten <bschouten@mozilla.com>, Nicolas Silva <nsilva@mozilla.com> or Nicholas Cameron <ncameron@mozilla.com> with general questions. Below is a rough list of authors to contact with specific questions.
Authors:
gfx/layers/Compositor.* gfx/layers/Effects.h - Compositor Interface - bas,nrc,nical
gfx/layers/d3d* - D3D9/D3D10 - bas
gfx/layers/ThebesLayer* - ThebesLayers - nrc,bas
gfx/layers/composite/* - CompositeLayers - nrc,nical
gfx/layers/client/* - Client - nrc,nical,bas
gfx/layers/*Image* - nical
gfx/layers/ipc ipc - IPC - nical
gfx/layers/opengl - CompositorOGL - nrc,nical
gfx/2d - bas,nrc
gfx/gl - GLContext - bjacob
dom/* layout/* - DOM - mattwoodrow
2013-04-10 02:20:52 -07:00
|
|
|
static gfxIntSize
|
|
|
|
CalculatePOTSize(const gfxIntSize& aSize, GLContext* gl)
|
2012-03-18 13:08:49 -07:00
|
|
|
{
|
|
|
|
if (gl->CanUploadNonPowerOfTwo())
|
|
|
|
return aSize;
|
|
|
|
|
|
|
|
return gfxIntSize(NextPowerOfTwo(aSize.width), NextPowerOfTwo(aSize.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ImageLayerOGL::LoadAsTexture(GLuint aTextureUnit, gfxIntSize* aSize)
|
|
|
|
{
|
|
|
|
// this method shares a lot of code with RenderLayer, but it doesn't seem
|
|
|
|
// to be possible to factor it out into a helper method
|
|
|
|
|
|
|
|
if (!GetContainer()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoLockImage autoLock(GetContainer());
|
|
|
|
|
|
|
|
Image *image = autoLock.GetImage();
|
|
|
|
if (!image) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-19 19:39:10 -07:00
|
|
|
if (image->GetFormat() != CAIRO_SURFACE) {
|
2012-03-18 13:08:49 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CairoImage* cairoImage = static_cast<CairoImage*>(image);
|
|
|
|
|
|
|
|
if (!cairoImage->mSurface) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CairoOGLBackendData *data = static_cast<CairoOGLBackendData*>(
|
2012-07-18 09:31:40 -07:00
|
|
|
cairoImage->GetBackendData(LAYERS_OPENGL));
|
2012-03-18 13:08:49 -07:00
|
|
|
|
|
|
|
if (!data) {
|
2012-06-25 19:43:31 -07:00
|
|
|
NS_ASSERTION(cairoImage->mSurface->GetContentType() == gfxASurface::CONTENT_ALPHA,
|
|
|
|
"OpenGL mask layers must be backed by alpha surfaces");
|
|
|
|
|
2012-03-18 13:08:49 -07:00
|
|
|
// allocate a new texture and save the details in the backend data
|
|
|
|
data = new CairoOGLBackendData;
|
|
|
|
data->mTextureSize = CalculatePOTSize(cairoImage->mSize, gl());
|
|
|
|
|
|
|
|
GLTexture &texture = data->mTexture;
|
|
|
|
texture.Allocate(mOGLManager->gl());
|
|
|
|
|
|
|
|
if (!texture.IsAllocated()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::gl::GLContext *texGL = texture.GetGLContext();
|
|
|
|
texGL->MakeCurrent();
|
|
|
|
|
|
|
|
GLuint texID = texture.GetTextureID();
|
|
|
|
|
2013-07-04 10:25:50 -07:00
|
|
|
gfx::SurfaceFormat format =
|
2012-03-18 13:08:49 -07:00
|
|
|
texGL->UploadSurfaceToTexture(cairoImage->mSurface,
|
|
|
|
nsIntRect(0,0,
|
|
|
|
data->mTextureSize.width,
|
|
|
|
data->mTextureSize.height),
|
|
|
|
texID, true, nsIntPoint(0,0), false,
|
|
|
|
aTextureUnit);
|
2013-07-04 10:25:50 -07:00
|
|
|
data->mLayerProgram = ShaderProgramFromSurfaceFormat(format);
|
2012-03-18 13:08:49 -07:00
|
|
|
|
2012-07-18 09:31:40 -07:00
|
|
|
cairoImage->SetBackendData(LAYERS_OPENGL, data);
|
2012-03-18 13:08:49 -07:00
|
|
|
|
|
|
|
gl()->MakeCurrent();
|
|
|
|
gl()->fActiveTexture(aTextureUnit);
|
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, texID);
|
|
|
|
gl()->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MIN_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
|
|
|
gl()->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MAG_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
|
|
|
gl()->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_S,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
gl()->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_T,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
} else {
|
|
|
|
gl()->fActiveTexture(aTextureUnit);
|
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, data->mTexture.GetTextureID());
|
|
|
|
}
|
|
|
|
|
|
|
|
*aSize = data->mTextureSize;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-29 21:48:52 -07:00
|
|
|
} /* layers */
|
|
|
|
} /* mozilla */
|