2010-05-25 06:35:35 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2010-03-30 04:48:52 +00:00
|
|
|
* 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 Original Code is Mozilla Corporation code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Bas Schouten <bschouten@mozilla.org>
|
2010-05-25 06:35:35 +00:00
|
|
|
* Vladimir Vukicevic <vladimir@pobox.com>
|
2010-03-30 04:48:52 +00:00
|
|
|
*
|
|
|
|
* 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 "ThebesLayerOGL.h"
|
|
|
|
#include "ContainerLayerOGL.h"
|
|
|
|
#include "gfxContext.h"
|
2010-04-26 22:24:03 +00:00
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "gfxWindowsSurface.h"
|
|
|
|
#endif
|
2010-06-23 14:01:29 +00:00
|
|
|
#include "GLContextProvider.h"
|
2010-03-30 04:48:52 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2010-06-23 14:01:29 +00:00
|
|
|
using namespace mozilla::gl;
|
|
|
|
|
2010-03-30 04:48:52 +00:00
|
|
|
// Returns true if it's OK to save the contents of aLayer in an
|
|
|
|
// opaque surface (a surface without an alpha channel).
|
|
|
|
// If we can use a surface without an alpha channel, we should, because
|
|
|
|
// it will often make painting of antialiased text faster and higher
|
|
|
|
// quality.
|
|
|
|
static PRBool
|
|
|
|
UseOpaqueSurface(Layer* aLayer)
|
|
|
|
{
|
|
|
|
// If the visible content in the layer is opaque, there is no need
|
|
|
|
// for an alpha channel.
|
|
|
|
if (aLayer->IsOpaqueContent())
|
|
|
|
return PR_TRUE;
|
|
|
|
// Also, if this layer is the bottommost layer in a container which
|
|
|
|
// doesn't need an alpha channel, we can use an opaque surface for this
|
|
|
|
// layer too. Any transparent areas must be covered by something else
|
|
|
|
// in the container.
|
|
|
|
ContainerLayerOGL* parent =
|
|
|
|
static_cast<ContainerLayerOGL*>(aLayer->GetParent());
|
|
|
|
return parent && parent->GetFirstChild() == aLayer &&
|
|
|
|
UseOpaqueSurface(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-27 02:09:47 +00:00
|
|
|
ThebesLayerOGL::ThebesLayerOGL(LayerManagerOGL *aManager)
|
2010-03-30 04:48:52 +00:00
|
|
|
: ThebesLayer(aManager, NULL)
|
2010-04-27 02:09:47 +00:00
|
|
|
, LayerOGL(aManager)
|
2010-03-30 04:48:52 +00:00
|
|
|
, mTexture(0)
|
2010-06-23 14:01:29 +00:00
|
|
|
, mOffscreenFormat(gfxASurface::ImageFormatUnknown)
|
2010-06-25 11:18:56 +00:00
|
|
|
, mOffscreenSize(-1,-1)
|
2010-03-30 04:48:52 +00:00
|
|
|
{
|
|
|
|
mImplData = static_cast<LayerOGL*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThebesLayerOGL::~ThebesLayerOGL()
|
|
|
|
{
|
2010-05-25 06:35:35 +00:00
|
|
|
mOGLManager->MakeCurrent();
|
2010-06-23 14:01:29 +00:00
|
|
|
if (mOffscreenSurfaceAsGLContext)
|
|
|
|
mOffscreenSurfaceAsGLContext->ReleaseTexImage();
|
2010-03-30 04:48:52 +00:00
|
|
|
if (mTexture) {
|
2010-04-27 02:09:47 +00:00
|
|
|
gl()->fDeleteTextures(1, &mTexture);
|
2010-03-30 04:48:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 14:01:29 +00:00
|
|
|
PRBool
|
|
|
|
ThebesLayerOGL::EnsureSurface()
|
2010-03-30 04:48:52 +00:00
|
|
|
{
|
2010-06-23 14:01:29 +00:00
|
|
|
gfxASurface::gfxImageFormat imageFormat = gfxASurface::ImageFormatARGB32;
|
|
|
|
if (UseOpaqueSurface(this))
|
|
|
|
imageFormat = gfxASurface::ImageFormatRGB24;
|
|
|
|
|
|
|
|
if (mInvalidatedRect.IsEmpty())
|
|
|
|
return mOffScreenSurface ? PR_TRUE : PR_FALSE;
|
|
|
|
|
|
|
|
if ((mOffscreenSize == gfxIntSize(mInvalidatedRect.width, mInvalidatedRect.height)
|
|
|
|
&& imageFormat == mOffscreenFormat)
|
|
|
|
|| mInvalidatedRect.IsEmpty())
|
|
|
|
return mOffScreenSurface ? PR_TRUE : PR_FALSE;
|
|
|
|
|
|
|
|
mOffScreenSurface =
|
|
|
|
gfxPlatform::GetPlatform()->
|
|
|
|
CreateOffscreenSurface(gfxIntSize(mInvalidatedRect.width, mInvalidatedRect.height),
|
|
|
|
imageFormat);
|
|
|
|
if (!mOffScreenSurface)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
if (mOffScreenSurface) {
|
|
|
|
mOffscreenSize.width = mInvalidatedRect.width;
|
|
|
|
mOffscreenSize.height = mInvalidatedRect.height;
|
|
|
|
mOffscreenFormat = imageFormat;
|
|
|
|
}
|
2010-05-25 06:35:35 +00:00
|
|
|
|
2010-03-30 04:48:52 +00:00
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
mOGLManager->MakeCurrent();
|
2010-03-30 04:48:52 +00:00
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
if (!mTexture)
|
2010-04-27 02:09:47 +00:00
|
|
|
gl()->fGenTextures(1, &mTexture);
|
2010-03-30 04:48:52 +00:00
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
|
2010-04-27 02:09:47 +00:00
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, mTexture);
|
|
|
|
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-06-23 14:01:29 +00:00
|
|
|
// Try bind our offscreen surface directly to texture
|
|
|
|
mOffscreenSurfaceAsGLContext = sGLContextProvider.CreateForNativePixmapSurface(mOffScreenSurface);
|
|
|
|
// Bind GL surface to the texture, and return
|
|
|
|
if (mOffscreenSurfaceAsGLContext)
|
|
|
|
return mOffscreenSurfaceAsGLContext->BindTexImage();
|
|
|
|
|
|
|
|
// Otherwise allocate new texture
|
2010-04-27 02:09:47 +00:00
|
|
|
gl()->fTexImage2D(LOCAL_GL_TEXTURE_2D,
|
2010-05-25 06:35:35 +00:00
|
|
|
0,
|
|
|
|
LOCAL_GL_RGBA,
|
2010-05-31 23:29:37 +00:00
|
|
|
mInvalidatedRect.width,
|
|
|
|
mInvalidatedRect.height,
|
2010-05-25 06:35:35 +00:00
|
|
|
0,
|
|
|
|
LOCAL_GL_RGBA,
|
|
|
|
LOCAL_GL_UNSIGNED_BYTE,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
DEBUG_GL_ERROR_CHECK(gl());
|
2010-06-23 14:01:29 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThebesLayerOGL::SetVisibleRegion(const nsIntRegion &aRegion)
|
|
|
|
{
|
|
|
|
if (aRegion.IsEqual(mVisibleRegion))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ThebesLayer::SetVisibleRegion(aRegion);
|
|
|
|
|
|
|
|
mInvalidatedRect = mVisibleRegion.GetBounds();
|
2010-03-30 04:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThebesLayerOGL::InvalidateRegion(const nsIntRegion &aRegion)
|
|
|
|
{
|
|
|
|
nsIntRegion invalidatedRegion;
|
|
|
|
invalidatedRegion.Or(aRegion, mInvalidatedRect);
|
2010-05-31 23:29:37 +00:00
|
|
|
invalidatedRegion.And(invalidatedRegion, mVisibleRegion);
|
2010-03-30 04:48:52 +00:00
|
|
|
mInvalidatedRect = invalidatedRegion.GetBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerOGL::LayerType
|
|
|
|
ThebesLayerOGL::GetType()
|
|
|
|
{
|
|
|
|
return TYPE_THEBES;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-05-21 03:20:48 +00:00
|
|
|
ThebesLayerOGL::RenderLayer(int aPreviousFrameBuffer,
|
2010-05-25 06:35:35 +00:00
|
|
|
const nsIntPoint& aOffset)
|
2010-03-30 04:48:52 +00:00
|
|
|
{
|
2010-06-23 14:01:29 +00:00
|
|
|
if (!EnsureSurface())
|
|
|
|
return;
|
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
if (!mTexture)
|
2010-03-30 04:48:52 +00:00
|
|
|
return;
|
2010-05-25 06:35:35 +00:00
|
|
|
|
|
|
|
mOGLManager->MakeCurrent();
|
|
|
|
gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
|
|
|
|
|
|
|
|
bool needsTextureBind = true;
|
2010-05-31 23:29:37 +00:00
|
|
|
nsIntRect visibleRect = mVisibleRegion.GetBounds();
|
2010-03-30 04:48:52 +00:00
|
|
|
|
2010-06-23 14:01:29 +00:00
|
|
|
nsRefPtr<gfxASurface> surface = mOffScreenSurface;
|
|
|
|
gfxASurface::gfxImageFormat imageFormat = mOffscreenFormat;
|
2010-05-21 03:20:48 +00:00
|
|
|
|
2010-06-23 14:01:29 +00:00
|
|
|
if (!mInvalidatedRect.IsEmpty()) {
|
2010-05-21 03:20:48 +00:00
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(surface);
|
|
|
|
ctx->Translate(gfxPoint(-mInvalidatedRect.x, -mInvalidatedRect.y));
|
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
/* Call the thebes layer callback */
|
|
|
|
mOGLManager->CallThebesLayerDrawCallback(this, ctx, mInvalidatedRect);
|
2010-06-23 14:01:29 +00:00
|
|
|
}
|
2010-05-21 03:20:48 +00:00
|
|
|
|
2010-06-23 14:01:29 +00:00
|
|
|
// If draw callback happend and we don't have native surface
|
|
|
|
if (!mInvalidatedRect.IsEmpty() && !mOffscreenSurfaceAsGLContext) {
|
2010-05-25 06:35:35 +00:00
|
|
|
/* Then take its results and put it in an image surface,
|
|
|
|
* in preparation for a texture upload */
|
2010-05-21 03:20:48 +00:00
|
|
|
nsRefPtr<gfxImageSurface> imageSurface;
|
|
|
|
|
|
|
|
switch (surface->GetType()) {
|
|
|
|
case gfxASurface::SurfaceTypeImage:
|
|
|
|
imageSurface = static_cast<gfxImageSurface*>(surface.get());
|
|
|
|
break;
|
|
|
|
#ifdef XP_WIN
|
|
|
|
case gfxASurface::SurfaceTypeWin32:
|
|
|
|
imageSurface =
|
|
|
|
static_cast<gfxWindowsSurface*>(surface.get())->
|
|
|
|
GetImageSurface();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/**
|
|
|
|
* XXX - This is very undesirable. Implement this for other platforms in
|
|
|
|
* a more efficient way as well!
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
imageSurface = new gfxImageSurface(gfxIntSize(mInvalidatedRect.width,
|
|
|
|
mInvalidatedRect.height),
|
|
|
|
imageFormat);
|
|
|
|
nsRefPtr<gfxContext> tmpContext = new gfxContext(imageSurface);
|
|
|
|
tmpContext->SetSource(surface);
|
|
|
|
tmpContext->SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
tmpContext->Paint();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-06-23 14:01:29 +00:00
|
|
|
// Upload image to texture (slow)
|
2010-05-21 03:20:48 +00:00
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, mTexture);
|
|
|
|
gl()->fTexSubImage2D(LOCAL_GL_TEXTURE_2D,
|
|
|
|
0,
|
2010-05-31 23:29:37 +00:00
|
|
|
mInvalidatedRect.x - visibleRect.x,
|
|
|
|
mInvalidatedRect.y - visibleRect.y,
|
2010-05-21 03:20:48 +00:00
|
|
|
mInvalidatedRect.width,
|
|
|
|
mInvalidatedRect.height,
|
2010-05-25 06:35:35 +00:00
|
|
|
LOCAL_GL_RGBA,
|
2010-05-21 03:20:48 +00:00
|
|
|
LOCAL_GL_UNSIGNED_BYTE,
|
|
|
|
imageSurface->Data());
|
2010-05-25 06:35:35 +00:00
|
|
|
|
|
|
|
needsTextureBind = false;
|
2010-05-21 03:20:48 +00:00
|
|
|
}
|
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
if (needsTextureBind)
|
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, mTexture);
|
|
|
|
|
|
|
|
// Note BGR: Cairo's image surfaces are always in what
|
|
|
|
// OpenGL and our shaders consider BGR format.
|
|
|
|
ColorTextureLayerProgram *program =
|
|
|
|
UseOpaqueSurface(this)
|
|
|
|
? mOGLManager->GetBGRXLayerProgram()
|
|
|
|
: mOGLManager->GetBGRALayerProgram();
|
2010-03-30 04:48:52 +00:00
|
|
|
|
|
|
|
program->Activate();
|
2010-05-31 23:29:37 +00:00
|
|
|
program->SetLayerQuadRect(visibleRect);
|
2010-03-30 04:48:52 +00:00
|
|
|
program->SetLayerOpacity(GetOpacity());
|
2010-05-25 06:35:35 +00:00
|
|
|
program->SetLayerTransform(mTransform);
|
|
|
|
program->SetRenderOffset(aOffset);
|
|
|
|
program->SetTextureUnit(0);
|
2010-03-30 04:48:52 +00:00
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
mOGLManager->BindAndDrawQuad(program);
|
2010-03-30 04:48:52 +00:00
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
DEBUG_GL_ERROR_CHECK(gl());
|
2010-03-30 04:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsIntRect&
|
|
|
|
ThebesLayerOGL::GetInvalidatedRect()
|
|
|
|
{
|
|
|
|
return mInvalidatedRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer*
|
|
|
|
ThebesLayerOGL::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
ThebesLayerOGL::IsEmpty()
|
|
|
|
{
|
|
|
|
return !mTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* layers */
|
|
|
|
} /* mozilla */
|