2010-05-24 23:35:35 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2010-03-29 21:48:52 -07: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-24 23:35:35 -07:00
|
|
|
* Vladimir Vukicevic <vladimir@pobox.com>
|
2010-03-29 21:48:52 -07: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 15:24:03 -07:00
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "gfxWindowsSurface.h"
|
|
|
|
#endif
|
2010-06-23 07:01:29 -07:00
|
|
|
#include "GLContextProvider.h"
|
2010-03-29 21:48:52 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2010-06-23 07:01:29 -07:00
|
|
|
using namespace mozilla::gl;
|
|
|
|
|
2010-03-29 21:48:52 -07: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-26 19:09:47 -07:00
|
|
|
ThebesLayerOGL::ThebesLayerOGL(LayerManagerOGL *aManager)
|
2010-07-01 09:30:38 -07:00
|
|
|
: ThebesLayer(aManager, nsnull)
|
2010-04-26 19:09:47 -07:00
|
|
|
, LayerOGL(aManager)
|
2010-07-01 09:30:38 -07:00
|
|
|
, mTexImage(nsnull)
|
2010-03-29 21:48:52 -07:00
|
|
|
{
|
|
|
|
mImplData = static_cast<LayerOGL*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThebesLayerOGL::~ThebesLayerOGL()
|
|
|
|
{
|
2010-07-01 09:30:38 -07:00
|
|
|
mTexImage = nsnull;
|
|
|
|
DEBUG_GL_ERROR_CHECK(gl());
|
2010-03-29 21:48:52 -07:00
|
|
|
}
|
|
|
|
|
2010-06-23 07:01:29 -07:00
|
|
|
PRBool
|
|
|
|
ThebesLayerOGL::EnsureSurface()
|
2010-03-29 21:48:52 -07:00
|
|
|
{
|
2010-07-01 09:30:38 -07:00
|
|
|
nsIntSize visibleSize = mVisibleRegion.GetBounds().Size();
|
|
|
|
TextureImage::ContentType contentType =
|
|
|
|
UseOpaqueSurface(this) ? gfxASurface::CONTENT_COLOR :
|
|
|
|
gfxASurface::CONTENT_COLOR_ALPHA;
|
|
|
|
if (!mTexImage ||
|
|
|
|
mTexImage->GetSize() != visibleSize ||
|
|
|
|
mTexImage->GetContentType() != contentType)
|
|
|
|
{
|
|
|
|
mValidRegion.SetEmpty();
|
|
|
|
mTexImage = nsnull;
|
|
|
|
DEBUG_GL_ERROR_CHECK(gl());
|
2010-06-23 07:01:29 -07:00
|
|
|
}
|
2010-05-24 23:35:35 -07:00
|
|
|
|
2010-07-01 09:30:38 -07:00
|
|
|
if (!mTexImage && !mVisibleRegion.IsEmpty())
|
|
|
|
{
|
|
|
|
mTexImage = gl()->CreateTextureImage(visibleSize,
|
|
|
|
contentType,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
DEBUG_GL_ERROR_CHECK(gl());
|
|
|
|
}
|
|
|
|
return !!mTexImage;
|
2010-06-23 07:01:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThebesLayerOGL::SetVisibleRegion(const nsIntRegion &aRegion)
|
|
|
|
{
|
|
|
|
if (aRegion.IsEqual(mVisibleRegion))
|
|
|
|
return;
|
|
|
|
ThebesLayer::SetVisibleRegion(aRegion);
|
2010-07-01 09:30:38 -07:00
|
|
|
// FIXME/bug 573829: keep some of these pixels, if we can!
|
|
|
|
mValidRegion.SetEmpty();
|
2010-03-29 21:48:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThebesLayerOGL::InvalidateRegion(const nsIntRegion &aRegion)
|
|
|
|
{
|
2010-07-01 09:30:38 -07:00
|
|
|
mValidRegion.Sub(mValidRegion, aRegion);
|
2010-03-29 21:48:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-05-20 20:20:48 -07:00
|
|
|
ThebesLayerOGL::RenderLayer(int aPreviousFrameBuffer,
|
2010-05-24 23:35:35 -07:00
|
|
|
const nsIntPoint& aOffset)
|
2010-03-29 21:48:52 -07:00
|
|
|
{
|
2010-06-23 07:01:29 -07:00
|
|
|
if (!EnsureSurface())
|
|
|
|
return;
|
|
|
|
|
2010-05-24 23:35:35 -07:00
|
|
|
mOGLManager->MakeCurrent();
|
|
|
|
gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
|
|
|
|
|
2010-07-01 09:30:38 -07:00
|
|
|
nsIntRegion rgnToPaint = mVisibleRegion;
|
|
|
|
rgnToPaint.Sub(rgnToPaint, mValidRegion);
|
|
|
|
PRBool textureBound = PR_FALSE;
|
2010-07-19 14:54:17 -07:00
|
|
|
if (!rgnToPaint.IsEmpty()) {
|
2010-07-01 09:30:38 -07:00
|
|
|
nsIntRect visibleRect = mVisibleRegion.GetBounds();
|
2010-07-19 14:54:17 -07:00
|
|
|
|
|
|
|
// Offset rgnToPaint by our visible region's origin, before
|
|
|
|
// passing to BeginUpdate. The TextureImage has no concept of an
|
|
|
|
// origin, only a size, so it always represents a 0,0 origin area.
|
|
|
|
// The layer however has a position, represented by its visible
|
|
|
|
// region. So we have to move things around so that we can
|
|
|
|
// interact with the TextureImage.
|
|
|
|
rgnToPaint.MoveBy(-visibleRect.TopLeft());
|
|
|
|
|
|
|
|
// BeginUpdate is allowed to modify the given region,
|
|
|
|
// if it wants more to be repainted than we request.
|
|
|
|
nsRefPtr<gfxContext> ctx = mTexImage->BeginUpdate(rgnToPaint);
|
|
|
|
if (!ctx) {
|
2010-07-01 09:30:38 -07:00
|
|
|
NS_WARNING("unable to get context for update");
|
|
|
|
return;
|
|
|
|
}
|
2010-07-19 14:54:17 -07:00
|
|
|
|
|
|
|
// Move rgnToPaint back into position so that the thebes callback
|
|
|
|
// gets the right coordintes.
|
|
|
|
rgnToPaint.MoveBy(visibleRect.TopLeft());
|
|
|
|
|
|
|
|
// Translate the context so that we're matching the layer's
|
|
|
|
// origin, not the 0,0-based TextureImage
|
2010-07-01 09:30:38 -07:00
|
|
|
ctx->Translate(-gfxPoint(visibleRect.x, visibleRect.y));
|
|
|
|
|
|
|
|
TextureImage::ContentType contentType = mTexImage->GetContentType();
|
|
|
|
//ClipToRegion(ctx, rgnToDraw);
|
2010-07-19 14:54:17 -07:00
|
|
|
if (gfxASurface::CONTENT_COLOR_ALPHA == contentType) {
|
2010-07-01 09:30:38 -07:00
|
|
|
ctx->SetOperator(gfxContext::OPERATOR_CLEAR);
|
|
|
|
ctx->Paint();
|
|
|
|
ctx->SetOperator(gfxContext::OPERATOR_OVER);
|
2010-05-20 20:20:48 -07:00
|
|
|
}
|
|
|
|
|
2010-07-01 09:30:38 -07:00
|
|
|
mOGLManager->CallThebesLayerDrawCallback(this, ctx, rgnToPaint);
|
2010-05-24 23:35:35 -07:00
|
|
|
|
2010-07-01 09:30:38 -07:00
|
|
|
textureBound = mTexImage->EndUpdate();
|
|
|
|
mValidRegion.Or(mValidRegion, rgnToPaint);
|
2010-05-20 20:20:48 -07:00
|
|
|
}
|
|
|
|
|
2010-07-01 09:30:38 -07:00
|
|
|
if (!textureBound)
|
|
|
|
gl()->fBindTexture(LOCAL_GL_TEXTURE_2D, mTexImage->Texture());
|
2010-05-24 23:35:35 -07:00
|
|
|
|
|
|
|
// 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-29 21:48:52 -07:00
|
|
|
|
|
|
|
program->Activate();
|
2010-07-01 09:30:38 -07:00
|
|
|
program->SetLayerQuadRect(mVisibleRegion.GetBounds());
|
2010-03-29 21:48:52 -07:00
|
|
|
program->SetLayerOpacity(GetOpacity());
|
2010-05-24 23:35:35 -07:00
|
|
|
program->SetLayerTransform(mTransform);
|
|
|
|
program->SetRenderOffset(aOffset);
|
|
|
|
program->SetTextureUnit(0);
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2010-05-24 23:35:35 -07:00
|
|
|
mOGLManager->BindAndDrawQuad(program);
|
2010-03-29 21:48:52 -07:00
|
|
|
|
2010-05-24 23:35:35 -07:00
|
|
|
DEBUG_GL_ERROR_CHECK(gl());
|
2010-03-29 21:48:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Layer*
|
|
|
|
ThebesLayerOGL::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
ThebesLayerOGL::IsEmpty()
|
|
|
|
{
|
2010-07-01 09:30:38 -07:00
|
|
|
return !mTexImage;
|
2010-03-29 21:48:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* layers */
|
|
|
|
} /* mozilla */
|