2010-05-27 20:04:26 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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-18 04:04:22 +00:00
|
|
|
|
2012-07-12 12:51:58 +00:00
|
|
|
#include "ipc/AutoOpenSurface.h"
|
2011-07-04 13:15:05 +00:00
|
|
|
#include "mozilla/layers/PLayers.h"
|
|
|
|
#include "mozilla/layers/ShadowLayers.h"
|
|
|
|
|
2011-04-03 02:14:00 +00:00
|
|
|
#include "gfxSharedImageSurface.h"
|
2010-10-13 22:55:45 +00:00
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
#include "CanvasLayerOGL.h"
|
|
|
|
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "gfxContext.h"
|
2010-06-23 14:02:32 +00:00
|
|
|
#include "GLContextProvider.h"
|
2011-11-02 19:55:03 +00:00
|
|
|
#include "gfxPlatform.h"
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2012-08-19 19:33:25 +00:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include "mozilla/gfx/MacIOSurface.h"
|
|
|
|
#endif
|
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "gfxWindowsSurface.h"
|
|
|
|
#include "WGLLibrary.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include <OpenGL/OpenGL.h>
|
|
|
|
#endif
|
|
|
|
|
2011-04-20 21:45:57 +00:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
#include "gfxXlibSurface.h"
|
|
|
|
#endif
|
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::layers;
|
|
|
|
using namespace mozilla::gl;
|
|
|
|
|
2012-06-01 01:30:08 +00:00
|
|
|
static void
|
|
|
|
MakeTextureIfNeeded(GLContext* gl, GLuint& aTexture)
|
|
|
|
{
|
|
|
|
if (aTexture != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gl->fGenTextures(1, &aTexture);
|
|
|
|
|
|
|
|
gl->fActiveTexture(LOCAL_GL_TEXTURE0);
|
|
|
|
gl->fBindTexture(LOCAL_GL_TEXTURE_2D, aTexture);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-08-07 05:09:18 +00:00
|
|
|
void
|
|
|
|
CanvasLayerOGL::Destroy()
|
2010-05-18 04:04:22 +00:00
|
|
|
{
|
2010-08-07 05:09:18 +00:00
|
|
|
if (!mDestroyed) {
|
2012-01-16 05:41:55 +00:00
|
|
|
CleanupResources();
|
2011-10-17 14:59:28 +00:00
|
|
|
mDestroyed = true;
|
2010-06-18 09:21:42 +00:00
|
|
|
}
|
2010-05-18 04:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasLayerOGL::Initialize(const Data& aData)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ASSERTION(mCanvasSurface == nullptr, "BasicCanvasLayer::Initialize called twice!");
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
if (aData.mGLContext != nullptr &&
|
|
|
|
aData.mSurface != nullptr)
|
2010-07-19 05:01:14 +00:00
|
|
|
{
|
|
|
|
NS_WARNING("CanvasLayerOGL can't have both surface and GLContext");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-08 21:41:10 +00:00
|
|
|
mOGLManager->MakeCurrent();
|
|
|
|
|
2011-11-02 19:55:03 +00:00
|
|
|
if (aData.mDrawTarget) {
|
|
|
|
mDrawTarget = aData.mDrawTarget;
|
2012-07-26 02:30:20 +00:00
|
|
|
mCanvasSurface = gfxPlatform::GetPlatform()->GetThebesSurfaceForDrawTarget(mDrawTarget);
|
2011-11-02 19:55:03 +00:00
|
|
|
mNeedsYFlip = false;
|
|
|
|
} else if (aData.mSurface) {
|
2010-05-25 06:35:35 +00:00
|
|
|
mCanvasSurface = aData.mSurface;
|
2011-10-17 14:59:28 +00:00
|
|
|
mNeedsYFlip = false;
|
2012-09-05 01:01:57 +00:00
|
|
|
#if defined(MOZ_X11) && !defined(MOZ_PLATFORM_MAEMO)
|
2011-08-05 01:13:25 +00:00
|
|
|
if (aData.mSurface->GetType() == gfxASurface::SurfaceTypeXlib) {
|
|
|
|
gfxXlibSurface *xsurf = static_cast<gfxXlibSurface*>(aData.mSurface);
|
|
|
|
mPixmap = xsurf->GetGLXPixmap();
|
|
|
|
if (mPixmap) {
|
|
|
|
if (aData.mSurface->GetContentType() == gfxASurface::CONTENT_COLOR_ALPHA) {
|
|
|
|
mLayerProgram = gl::RGBALayerProgramType;
|
|
|
|
} else {
|
|
|
|
mLayerProgram = gl::RGBXLayerProgramType;
|
|
|
|
}
|
2012-06-01 01:30:08 +00:00
|
|
|
MakeTextureIfNeeded(gl(), mTexture);
|
2011-03-08 21:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-18 04:04:22 +00:00
|
|
|
} else if (aData.mGLContext) {
|
2010-07-19 05:01:14 +00:00
|
|
|
if (!aData.mGLContext->IsOffscreen()) {
|
|
|
|
NS_WARNING("CanvasLayerOGL with a non-offscreen GL context given");
|
2010-05-18 04:04:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
mCanvasGLContext = aData.mGLContext;
|
2010-05-18 04:04:22 +00:00
|
|
|
mGLBufferIsPremultiplied = aData.mGLBufferIsPremultiplied;
|
2010-07-19 05:01:14 +00:00
|
|
|
|
2012-03-26 16:56:10 +00:00
|
|
|
mNeedsYFlip = mCanvasGLContext->GetOffscreenTexture() != 0;
|
2010-05-18 04:04:22 +00:00
|
|
|
} else {
|
|
|
|
NS_WARNING("CanvasLayerOGL::Initialize called without surface or GL context!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height);
|
2012-07-30 18:36:12 +00:00
|
|
|
|
2010-12-17 07:19:28 +00:00
|
|
|
// Check the maximum texture size supported by GL. glTexImage2D supports
|
|
|
|
// images of up to 2 + GL_MAX_TEXTURE_SIZE
|
|
|
|
GLint texSize = gl()->GetMaxTextureSize();
|
|
|
|
if (mBounds.width > (2 + texSize) || mBounds.height > (2 + texSize)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mDelayedUpdates = true;
|
2012-06-01 01:30:08 +00:00
|
|
|
MakeTextureIfNeeded(gl(), mTexture);
|
2010-12-17 07:19:28 +00:00
|
|
|
// This should only ever occur with 2d canvas, WebGL can't already have a texture
|
|
|
|
// of this size can it?
|
2012-07-30 18:36:12 +00:00
|
|
|
NS_ABORT_IF_FALSE(mCanvasSurface || mDrawTarget,
|
2010-12-17 07:19:28 +00:00
|
|
|
"Invalid texture size when WebGL surface already exists at that size?");
|
|
|
|
}
|
2010-05-18 04:04:22 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 15:17:43 +00:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
static GLuint
|
|
|
|
MakeIOSurfaceTexture(void* aCGIOSurfaceContext, mozilla::gl::GLContext* aGL)
|
|
|
|
{
|
|
|
|
GLuint ioSurfaceTexture;
|
|
|
|
|
|
|
|
aGL->fGenTextures(1, &ioSurfaceTexture);
|
|
|
|
|
|
|
|
aGL->fActiveTexture(LOCAL_GL_TEXTURE0);
|
|
|
|
aGL->fBindTexture(LOCAL_GL_TEXTURE_RECTANGLE_ARB, ioSurfaceTexture);
|
|
|
|
|
|
|
|
aGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, LOCAL_GL_TEXTURE_MIN_FILTER, LOCAL_GL_LINEAR);
|
|
|
|
aGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, LOCAL_GL_TEXTURE_MAG_FILTER, LOCAL_GL_LINEAR);
|
|
|
|
aGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, LOCAL_GL_TEXTURE_WRAP_S, LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
aGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, LOCAL_GL_TEXTURE_WRAP_T, LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
|
|
|
|
RefPtr<MacIOSurface> ioSurface = MacIOSurface::IOSurfaceContextGetSurface((CGContextRef)aCGIOSurfaceContext);
|
|
|
|
void *nativeCtx = aGL->GetNativeData(GLContext::NativeGLContext);
|
|
|
|
|
|
|
|
ioSurface->CGLTexImageIOSurface2D(nativeCtx,
|
|
|
|
LOCAL_GL_RGBA, LOCAL_GL_BGRA,
|
|
|
|
LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV, 0);
|
|
|
|
|
|
|
|
aGL->fBindTexture(LOCAL_GL_TEXTURE_RECTANGLE_ARB, 0);
|
|
|
|
|
|
|
|
return ioSurfaceTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
static GLuint
|
|
|
|
MakeIOSurfaceTexture(void* aCGIOSurfaceContext, mozilla::gl::GLContext* aGL)
|
|
|
|
{
|
|
|
|
NS_RUNTIMEABORT("Not implemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-19 19:21:56 +00:00
|
|
|
/**
|
|
|
|
* Following UpdateSurface(), mTexture on context this->gl() should contain the data we want,
|
|
|
|
* unless mDelayedUpdates is true because of a too-large surface.
|
|
|
|
*/
|
2010-05-18 04:04:22 +00:00
|
|
|
void
|
2011-03-27 23:59:46 +00:00
|
|
|
CanvasLayerOGL::UpdateSurface()
|
2010-05-18 04:04:22 +00:00
|
|
|
{
|
2012-09-30 06:20:25 +00:00
|
|
|
if (!IsDirty())
|
2011-03-27 23:59:46 +00:00
|
|
|
return;
|
2012-09-30 06:20:25 +00:00
|
|
|
Painted();
|
2011-03-27 23:59:46 +00:00
|
|
|
|
2010-12-17 07:19:28 +00:00
|
|
|
if (mDestroyed || mDelayedUpdates) {
|
2010-08-07 05:09:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-05 01:01:57 +00:00
|
|
|
#if defined(MOZ_X11) && !defined(MOZ_PLATFORM_MAEMO)
|
2011-03-08 21:41:10 +00:00
|
|
|
if (mPixmap) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-31 23:44:33 +00:00
|
|
|
if (mCanvasGLContext) {
|
|
|
|
mCanvasGLContext->MakeCurrent();
|
|
|
|
}
|
|
|
|
|
2010-08-07 05:09:18 +00:00
|
|
|
if (mCanvasGLContext &&
|
2012-08-22 03:29:06 +00:00
|
|
|
!mForceReadback &&
|
2010-08-07 05:09:18 +00:00
|
|
|
mCanvasGLContext->GetContextType() == gl()->GetContextType())
|
|
|
|
{
|
2012-03-25 19:50:26 +00:00
|
|
|
DiscardTempSurface();
|
|
|
|
|
2011-12-19 19:21:56 +00:00
|
|
|
// Can texture share, just make sure it's resolved first
|
|
|
|
mCanvasGLContext->GuaranteeResolve();
|
|
|
|
|
2010-07-19 05:01:14 +00:00
|
|
|
if (gl()->BindOffscreenNeedsTexture(mCanvasGLContext) &&
|
|
|
|
mTexture == 0)
|
|
|
|
{
|
2011-12-19 19:21:56 +00:00
|
|
|
mOGLManager->MakeCurrent();
|
2012-06-01 01:30:08 +00:00
|
|
|
MakeTextureIfNeeded(gl(), mTexture);
|
2010-06-23 14:02:32 +00:00
|
|
|
}
|
2012-08-31 23:44:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-03-25 19:50:26 +00:00
|
|
|
|
2012-08-31 23:44:33 +00:00
|
|
|
nsRefPtr<gfxASurface> updatedAreaSurface;
|
|
|
|
if (mCanvasGLContext) {
|
|
|
|
gfxIntSize size(mBounds.width, mBounds.height);
|
|
|
|
nsRefPtr<gfxImageSurface> updatedAreaImageSurface =
|
2012-03-25 19:50:26 +00:00
|
|
|
GetTempSurface(size, gfxASurface::ImageFormatARGB32);
|
|
|
|
|
2012-08-31 23:44:33 +00:00
|
|
|
updatedAreaImageSurface->Flush();
|
|
|
|
mCanvasGLContext->ReadScreenIntoImageSurface(updatedAreaImageSurface);
|
|
|
|
updatedAreaImageSurface->MarkDirty();
|
2012-03-25 19:50:26 +00:00
|
|
|
|
2012-08-31 23:44:33 +00:00
|
|
|
updatedAreaSurface = updatedAreaImageSurface;
|
|
|
|
} else if (mCanvasSurface) {
|
|
|
|
updatedAreaSurface = mCanvasSurface;
|
|
|
|
} else {
|
|
|
|
MOZ_NOT_REACHED("Unhandled canvas layer type.");
|
|
|
|
return;
|
2010-05-18 04:04:22 +00:00
|
|
|
}
|
2012-08-31 23:44:33 +00:00
|
|
|
|
|
|
|
mOGLManager->MakeCurrent();
|
|
|
|
mLayerProgram = gl()->UploadSurfaceToTexture(updatedAreaSurface,
|
|
|
|
mBounds,
|
|
|
|
mTexture,
|
|
|
|
false,
|
|
|
|
nsIntPoint(0, 0));
|
2010-05-18 04:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-05-21 03:20:48 +00:00
|
|
|
CanvasLayerOGL::RenderLayer(int aPreviousDestination,
|
2010-11-08 09:06:15 +00:00
|
|
|
const nsIntPoint& aOffset)
|
2010-05-18 04:04:22 +00:00
|
|
|
{
|
2011-03-27 23:59:46 +00:00
|
|
|
UpdateSurface();
|
2012-08-13 10:10:10 +00:00
|
|
|
if (mOGLManager->CompositingDisabled()) {
|
|
|
|
return;
|
|
|
|
}
|
2011-03-27 23:59:46 +00:00
|
|
|
FireDidTransactionCallback();
|
2011-03-27 23:59:46 +00:00
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
mOGLManager->MakeCurrent();
|
2010-05-18 04:04:22 +00:00
|
|
|
|
|
|
|
// XXX We're going to need a different program depending on if
|
|
|
|
// mGLBufferIsPremultiplied is TRUE or not. The RGBLayerProgram
|
|
|
|
// assumes that it's true.
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2010-07-19 05:01:14 +00:00
|
|
|
if (mTexture) {
|
2012-07-31 15:17:43 +00:00
|
|
|
gl()->fBindTexture(mTextureTarget, mTexture);
|
2010-07-19 05:01:14 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
ShaderProgramOGL *program = nullptr;
|
2010-12-17 07:28:30 +00:00
|
|
|
|
2010-08-07 05:09:18 +00:00
|
|
|
bool useGLContext = mCanvasGLContext &&
|
2012-08-22 03:29:06 +00:00
|
|
|
!mForceReadback &&
|
|
|
|
mCanvasGLContext->GetContextType() == gl()->GetContextType();
|
2010-08-07 05:09:18 +00:00
|
|
|
|
2010-12-17 07:19:28 +00:00
|
|
|
nsIntRect drawRect = mBounds;
|
|
|
|
|
2010-08-07 05:09:18 +00:00
|
|
|
if (useGLContext) {
|
2010-07-19 05:01:14 +00:00
|
|
|
gl()->BindTex2DOffscreen(mCanvasGLContext);
|
2012-03-18 20:08:53 +00:00
|
|
|
program = mOGLManager->GetBasicLayerProgram(CanUseOpaqueSurface(),
|
|
|
|
true,
|
|
|
|
GetMaskLayer() ? Mask2d : MaskNone);
|
2010-12-17 07:19:28 +00:00
|
|
|
} else if (mDelayedUpdates) {
|
2012-01-16 21:55:43 +00:00
|
|
|
NS_ABORT_IF_FALSE(mCanvasSurface || mDrawTarget, "WebGL canvases should always be using full texture upload");
|
2012-07-25 15:37:04 +00:00
|
|
|
|
2010-12-17 07:19:28 +00:00
|
|
|
drawRect.IntersectRect(drawRect, GetEffectiveVisibleRegion().GetBounds());
|
|
|
|
|
2010-12-17 07:28:30 +00:00
|
|
|
mLayerProgram =
|
2012-07-26 02:30:20 +00:00
|
|
|
gl()->UploadSurfaceToTexture(mCanvasSurface,
|
2011-02-14 23:23:50 +00:00
|
|
|
nsIntRect(0, 0, drawRect.width, drawRect.height),
|
2010-12-17 07:28:30 +00:00
|
|
|
mTexture,
|
2011-02-14 23:23:50 +00:00
|
|
|
true,
|
|
|
|
drawRect.TopLeft());
|
2010-12-17 07:28:30 +00:00
|
|
|
}
|
2011-12-19 19:21:56 +00:00
|
|
|
|
|
|
|
if (!program) {
|
2012-03-18 20:08:53 +00:00
|
|
|
program = mOGLManager->GetProgram(mLayerProgram, GetMaskLayer());
|
2010-05-18 04:04:22 +00:00
|
|
|
}
|
|
|
|
|
2012-09-05 01:01:57 +00:00
|
|
|
#if defined(MOZ_X11) && !defined(MOZ_PLATFORM_MAEMO)
|
2011-03-08 21:41:10 +00:00
|
|
|
if (mPixmap && !mDelayedUpdates) {
|
2012-09-07 00:16:30 +00:00
|
|
|
sDefGLXLib.BindTexImage(mPixmap);
|
2011-03-08 21:41:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-17 02:31:15 +00:00
|
|
|
gl()->ApplyFilterToBoundTexture(mFilter);
|
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
program->Activate();
|
2012-07-31 15:17:43 +00:00
|
|
|
if (mLayerProgram == gl::RGBARectLayerProgramType) {
|
|
|
|
// This is used by IOSurface that use 0,0...w,h coordinate rather then 0,0..1,1.
|
|
|
|
program->SetTexCoordMultiplier(mDrawTarget->GetSize().width, mDrawTarget->GetSize().height);
|
|
|
|
}
|
2010-12-17 07:19:28 +00:00
|
|
|
program->SetLayerQuadRect(drawRect);
|
2010-11-08 09:06:15 +00:00
|
|
|
program->SetLayerTransform(GetEffectiveTransform());
|
|
|
|
program->SetLayerOpacity(GetEffectiveOpacity());
|
2010-05-25 06:35:35 +00:00
|
|
|
program->SetRenderOffset(aOffset);
|
|
|
|
program->SetTextureUnit(0);
|
2012-03-18 20:08:53 +00:00
|
|
|
program->LoadMask(GetMaskLayer());
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2012-02-19 02:23:06 +00:00
|
|
|
if (gl()->CanUploadNonPowerOfTwo()) {
|
|
|
|
mOGLManager->BindAndDrawQuad(program, mNeedsYFlip ? true : false);
|
|
|
|
} else {
|
|
|
|
mOGLManager->BindAndDrawQuadWithTextureRect(program, drawRect, drawRect.Size());
|
|
|
|
}
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2012-09-05 01:01:57 +00:00
|
|
|
#if defined(MOZ_X11) && !defined(MOZ_PLATFORM_MAEMO)
|
2011-03-08 21:41:10 +00:00
|
|
|
if (mPixmap && !mDelayedUpdates) {
|
2012-09-07 00:16:30 +00:00
|
|
|
sDefGLXLib.ReleaseTexImage(mPixmap);
|
2011-03-08 21:41:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-07 05:09:18 +00:00
|
|
|
if (useGLContext) {
|
2010-07-19 05:01:14 +00:00
|
|
|
gl()->UnbindTex2DOffscreen(mCanvasGLContext);
|
2010-05-18 04:04:22 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-13 22:55:45 +00:00
|
|
|
|
2012-01-16 05:41:55 +00:00
|
|
|
void
|
|
|
|
CanvasLayerOGL::CleanupResources()
|
|
|
|
{
|
|
|
|
if (mTexture) {
|
2012-03-22 05:48:41 +00:00
|
|
|
gl()->MakeCurrent();
|
|
|
|
gl()->fDeleteTextures(1, &mTexture);
|
2012-01-16 05:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 01:30:08 +00:00
|
|
|
static bool
|
|
|
|
IsValidSharedTexDescriptor(const SurfaceDescriptor& aDescriptor)
|
|
|
|
{
|
|
|
|
return aDescriptor.type() == SurfaceDescriptor::TSharedTextureDescriptor;
|
|
|
|
}
|
2010-10-13 22:55:45 +00:00
|
|
|
|
|
|
|
ShadowCanvasLayerOGL::ShadowCanvasLayerOGL(LayerManagerOGL* aManager)
|
2012-07-30 14:20:58 +00:00
|
|
|
: ShadowCanvasLayer(aManager, nullptr)
|
2010-10-13 22:55:45 +00:00
|
|
|
, LayerOGL(aManager)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mNeedsYFlip(false)
|
2012-06-01 01:30:08 +00:00
|
|
|
, mTexture(0)
|
2010-10-13 22:55:45 +00:00
|
|
|
{
|
|
|
|
mImplData = static_cast<LayerOGL*>(this);
|
|
|
|
}
|
2012-07-31 17:28:20 +00:00
|
|
|
|
2010-10-13 22:55:45 +00:00
|
|
|
ShadowCanvasLayerOGL::~ShadowCanvasLayerOGL()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowCanvasLayerOGL::Initialize(const Data& aData)
|
|
|
|
{
|
2011-04-20 21:45:57 +00:00
|
|
|
NS_RUNTIMEABORT("Incompatibe surface type");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-27 22:19:28 +00:00
|
|
|
ShadowCanvasLayerOGL::Init(const CanvasSurface& aNewFront, bool needYFlip)
|
2011-04-20 21:45:57 +00:00
|
|
|
{
|
2012-07-12 12:51:58 +00:00
|
|
|
AutoOpenSurface autoSurf(OPEN_READ_ONLY, aNewFront);
|
2011-04-20 21:45:57 +00:00
|
|
|
|
2012-05-09 20:55:31 +00:00
|
|
|
mNeedsYFlip = needYFlip;
|
|
|
|
|
2012-07-12 12:51:58 +00:00
|
|
|
mTexImage = gl()->CreateTextureImage(autoSurf.Size(),
|
|
|
|
autoSurf.ContentType(),
|
2012-05-09 20:55:31 +00:00
|
|
|
LOCAL_GL_CLAMP_TO_EDGE,
|
|
|
|
mNeedsYFlip ? TextureImage::NeedsYFlip : TextureImage::NoFlags);
|
2010-10-13 22:55:45 +00:00
|
|
|
}
|
|
|
|
|
2011-04-20 21:45:57 +00:00
|
|
|
void
|
2011-09-27 22:19:28 +00:00
|
|
|
ShadowCanvasLayerOGL::Swap(const CanvasSurface& aNewFront,
|
|
|
|
bool needYFlip,
|
|
|
|
CanvasSurface* aNewBack)
|
2010-10-13 22:55:45 +00:00
|
|
|
{
|
2012-06-01 01:30:08 +00:00
|
|
|
if (mDestroyed) {
|
|
|
|
*aNewBack = aNewFront;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-09 14:32:48 +00:00
|
|
|
if (nsRefPtr<TextureImage> texImage =
|
|
|
|
ShadowLayerManager::OpenDescriptorForDirectTexturing(
|
|
|
|
gl(), aNewFront.get_SurfaceDescriptor(), LOCAL_GL_CLAMP_TO_EDGE)) {
|
|
|
|
|
|
|
|
if (mTexImage &&
|
|
|
|
(mTexImage->GetSize() != texImage->GetSize() ||
|
|
|
|
mTexImage->GetContentType() != texImage->GetContentType())) {
|
|
|
|
mTexImage = nullptr;
|
|
|
|
DestroyFrontBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
mTexImage = texImage;
|
|
|
|
*aNewBack = IsSurfaceDescriptorValid(mFrontBufferDescriptor) ?
|
|
|
|
CanvasSurface(mFrontBufferDescriptor) : CanvasSurface(null_t());
|
|
|
|
mFrontBufferDescriptor = aNewFront;
|
|
|
|
mNeedsYFlip = needYFlip;
|
|
|
|
} else if (IsValidSharedTexDescriptor(aNewFront)) {
|
2012-06-01 01:30:08 +00:00
|
|
|
MakeTextureIfNeeded(gl(), mTexture);
|
|
|
|
if (!IsValidSharedTexDescriptor(mFrontBufferDescriptor)) {
|
2012-07-20 19:20:51 +00:00
|
|
|
mFrontBufferDescriptor = SharedTextureDescriptor(TextureImage::ThreadShared, 0, nsIntSize(0, 0), false);
|
2012-06-01 01:30:08 +00:00
|
|
|
}
|
|
|
|
*aNewBack = mFrontBufferDescriptor;
|
|
|
|
mFrontBufferDescriptor = aNewFront;
|
|
|
|
mNeedsYFlip = needYFlip;
|
|
|
|
} else {
|
2012-07-12 12:51:58 +00:00
|
|
|
AutoOpenSurface autoSurf(OPEN_READ_ONLY, aNewFront);
|
|
|
|
gfxIntSize sz = autoSurf.Size();
|
2011-10-21 01:29:09 +00:00
|
|
|
if (!mTexImage || mTexImage->GetSize() != sz ||
|
2012-07-12 12:51:58 +00:00
|
|
|
mTexImage->GetContentType() != autoSurf.ContentType()) {
|
2011-09-27 22:19:28 +00:00
|
|
|
Init(aNewFront, needYFlip);
|
|
|
|
}
|
2010-10-13 22:55:45 +00:00
|
|
|
nsIntRegion updateRegion(nsIntRect(0, 0, sz.width, sz.height));
|
2012-07-12 12:51:58 +00:00
|
|
|
mTexImage->DirectUpdate(autoSurf.Get(), updateRegion);
|
2012-06-01 01:30:08 +00:00
|
|
|
*aNewBack = aNewFront;
|
2010-10-13 22:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowCanvasLayerOGL::DestroyFrontBuffer()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
mTexImage = nullptr;
|
2012-06-01 01:30:08 +00:00
|
|
|
if (mTexture) {
|
|
|
|
gl()->MakeCurrent();
|
|
|
|
gl()->fDeleteTextures(1, &mTexture);
|
|
|
|
}
|
|
|
|
if (IsValidSharedTexDescriptor(mFrontBufferDescriptor)) {
|
|
|
|
SharedTextureDescriptor texDescriptor = mFrontBufferDescriptor.get_SharedTextureDescriptor();
|
|
|
|
gl()->ReleaseSharedHandle(texDescriptor.shareType(), texDescriptor.handle());
|
|
|
|
mFrontBufferDescriptor = SurfaceDescriptor();
|
2012-09-18 00:56:04 +00:00
|
|
|
} else if (IsSurfaceDescriptorValid(mFrontBufferDescriptor)) {
|
|
|
|
mAllocator->DestroySharedSurface(&mFrontBufferDescriptor);
|
2012-06-01 01:30:08 +00:00
|
|
|
}
|
2010-10-13 22:55:45 +00:00
|
|
|
}
|
|
|
|
|
2011-01-06 04:54:47 +00:00
|
|
|
void
|
|
|
|
ShadowCanvasLayerOGL::Disconnect()
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
2010-10-13 22:55:45 +00:00
|
|
|
void
|
|
|
|
ShadowCanvasLayerOGL::Destroy()
|
|
|
|
{
|
|
|
|
if (!mDestroyed) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mDestroyed = true;
|
2012-06-01 01:30:08 +00:00
|
|
|
DestroyFrontBuffer();
|
2010-10-13 22:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer*
|
|
|
|
ShadowCanvasLayerOGL::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowCanvasLayerOGL::RenderLayer(int aPreviousFrameBuffer,
|
2010-11-08 09:06:15 +00:00
|
|
|
const nsIntPoint& aOffset)
|
2010-10-13 22:55:45 +00:00
|
|
|
{
|
2012-06-01 01:30:08 +00:00
|
|
|
if (!mTexImage && !IsValidSharedTexDescriptor(mFrontBufferDescriptor)) {
|
2012-07-12 12:51:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-13 10:10:10 +00:00
|
|
|
if (mOGLManager->CompositingDisabled()) {
|
|
|
|
return;
|
|
|
|
}
|
2010-10-13 22:55:45 +00:00
|
|
|
mOGLManager->MakeCurrent();
|
|
|
|
|
2011-10-14 20:16:19 +00:00
|
|
|
gfx3DMatrix effectiveTransform = GetEffectiveTransform();
|
2012-07-15 15:11:05 +00:00
|
|
|
gfxPattern::GraphicsFilter filter = mFilter;
|
2011-10-14 20:16:19 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
// Bug 691354
|
|
|
|
// Using the LINEAR filter we get unexplained artifacts.
|
|
|
|
// Use NEAREST when no scaling is required.
|
|
|
|
gfxMatrix matrix;
|
|
|
|
bool is2D = GetEffectiveTransform().Is2D(&matrix);
|
|
|
|
if (is2D && !matrix.HasNonTranslationOrFlip()) {
|
2012-07-15 15:11:05 +00:00
|
|
|
filter = gfxPattern::FILTER_NEAREST;
|
2011-10-14 20:16:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-01 01:30:08 +00:00
|
|
|
ShaderProgramOGL *program;
|
|
|
|
if (IsValidSharedTexDescriptor(mFrontBufferDescriptor)) {
|
|
|
|
program = mOGLManager->GetBasicLayerProgram(CanUseOpaqueSurface(),
|
|
|
|
true,
|
|
|
|
GetMaskLayer() ? Mask2d : MaskNone);
|
|
|
|
} else {
|
|
|
|
program = mOGLManager->GetProgram(mTexImage->GetShaderProgramType(),
|
|
|
|
GetMaskLayer());
|
|
|
|
}
|
|
|
|
|
2010-10-13 22:55:45 +00:00
|
|
|
program->Activate();
|
2011-10-14 20:16:19 +00:00
|
|
|
program->SetLayerTransform(effectiveTransform);
|
2010-11-08 09:06:15 +00:00
|
|
|
program->SetLayerOpacity(GetEffectiveOpacity());
|
2010-10-13 22:55:45 +00:00
|
|
|
program->SetRenderOffset(aOffset);
|
|
|
|
program->SetTextureUnit(0);
|
2012-03-18 23:02:38 +00:00
|
|
|
program->LoadMask(GetMaskLayer());
|
2010-10-13 22:55:45 +00:00
|
|
|
|
2012-06-01 01:30:08 +00:00
|
|
|
if (IsValidSharedTexDescriptor(mFrontBufferDescriptor)) {
|
|
|
|
// Shared texture handle rendering path, single texture rendering
|
|
|
|
SharedTextureDescriptor texDescriptor = mFrontBufferDescriptor.get_SharedTextureDescriptor();
|
|
|
|
gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
|
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, mTexture);
|
|
|
|
if (!gl()->AttachSharedHandle(texDescriptor.shareType(), texDescriptor.handle())) {
|
|
|
|
NS_ERROR("Failed to attach shared texture handle");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gl()->ApplyFilterToBoundTexture(filter);
|
|
|
|
program->SetLayerQuadRect(nsIntRect(nsIntPoint(0, 0), texDescriptor.size()));
|
|
|
|
mOGLManager->BindAndDrawQuad(program, mNeedsYFlip);
|
|
|
|
gl()->DetachSharedHandle(texDescriptor.shareType(), texDescriptor.handle());
|
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, 0);
|
2012-02-19 02:23:06 +00:00
|
|
|
} else {
|
2012-06-01 01:30:08 +00:00
|
|
|
// Tiled texture image rendering path
|
|
|
|
mTexImage->SetFilter(filter);
|
|
|
|
mTexImage->BeginTileIteration();
|
|
|
|
if (gl()->CanUploadNonPowerOfTwo()) {
|
|
|
|
do {
|
|
|
|
TextureImage::ScopedBindTextureAndApplyFilter texBind(mTexImage, LOCAL_GL_TEXTURE0);
|
|
|
|
program->SetLayerQuadRect(mTexImage->GetTileRect());
|
|
|
|
mOGLManager->BindAndDrawQuad(program, mNeedsYFlip); // FIXME flip order of tiles?
|
|
|
|
} while (mTexImage->NextTile());
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
TextureImage::ScopedBindTextureAndApplyFilter texBind(mTexImage, LOCAL_GL_TEXTURE0);
|
|
|
|
program->SetLayerQuadRect(mTexImage->GetTileRect());
|
|
|
|
// We can't use BindAndDrawQuad because that always uploads the whole texture from 0.0f -> 1.0f
|
|
|
|
// in x and y. We use BindAndDrawQuadWithTextureRect to actually draw a subrect of the texture
|
|
|
|
// We need to reset the origin to 0,0 from the tile rect because the tile originates at 0,0 in the
|
|
|
|
// actual texture, even though its origin in the composed (tiled) texture is not 0,0
|
|
|
|
// FIXME: we need to handle mNeedsYFlip, Bug #728625
|
|
|
|
mOGLManager->BindAndDrawQuadWithTextureRect(program,
|
|
|
|
nsIntRect(0, 0, mTexImage->GetTileRect().width,
|
|
|
|
mTexImage->GetTileRect().height),
|
|
|
|
mTexImage->GetTileRect().Size(),
|
|
|
|
mTexImage->GetWrapMode(),
|
|
|
|
mNeedsYFlip);
|
|
|
|
} while (mTexImage->NextTile());
|
|
|
|
}
|
2012-02-19 02:23:06 +00:00
|
|
|
}
|
2010-10-13 22:55:45 +00:00
|
|
|
}
|
2012-01-16 05:41:55 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ShadowCanvasLayerOGL::CleanupResources()
|
|
|
|
{
|
|
|
|
DestroyFrontBuffer();
|
|
|
|
}
|