2010-05-24 08:28:51 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* 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) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Vladimir Vukicevic <vladimir@pobox.com>
|
|
|
|
* Bas Schouten <bschouten@mozilla.com>
|
|
|
|
*
|
|
|
|
* 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 "CanvasLayerD3D9.h"
|
|
|
|
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "gfxWindowsSurface.h"
|
2010-09-07 20:27:36 -07:00
|
|
|
#include "gfxWindowsPlatform.h"
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
|
|
|
CanvasLayerD3D9::~CanvasLayerD3D9()
|
|
|
|
{
|
2010-09-15 15:15:56 -07:00
|
|
|
if (mD3DManager) {
|
2010-09-14 03:56:56 -07:00
|
|
|
mD3DManager->deviceManager()->mLayersWithResources.RemoveElement(this);
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasLayerD3D9::Initialize(const Data& aData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mSurface == nsnull, "BasicCanvasLayer::Initialize called twice!");
|
|
|
|
|
|
|
|
if (aData.mSurface) {
|
|
|
|
mSurface = aData.mSurface;
|
|
|
|
NS_ASSERTION(aData.mGLContext == nsnull,
|
|
|
|
"CanvasLayer can't have both surface and GLContext");
|
|
|
|
mNeedsYFlip = PR_FALSE;
|
2010-08-23 20:58:27 -07:00
|
|
|
mDataIsPremultiplied = PR_TRUE;
|
2010-05-24 08:28:51 -07:00
|
|
|
} else if (aData.mGLContext) {
|
2010-07-18 22:01:14 -07:00
|
|
|
NS_ASSERTION(aData.mGLContext->IsOffscreen(), "canvas gl context isn't offscreen");
|
2010-05-24 08:28:51 -07:00
|
|
|
mGLContext = aData.mGLContext;
|
2010-07-18 22:01:14 -07:00
|
|
|
mCanvasFramebuffer = mGLContext->GetOffscreenFBO();
|
2010-08-23 20:58:27 -07:00
|
|
|
mDataIsPremultiplied = aData.mGLBufferIsPremultiplied;
|
2010-05-24 08:28:51 -07:00
|
|
|
mNeedsYFlip = PR_TRUE;
|
|
|
|
} else {
|
|
|
|
NS_ERROR("CanvasLayer created without mSurface or mGLContext?");
|
|
|
|
}
|
|
|
|
|
|
|
|
mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height);
|
|
|
|
|
2010-09-14 03:57:03 -07:00
|
|
|
CreateTexture();
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-27 16:59:46 -07:00
|
|
|
CanvasLayerD3D9::UpdateSurface()
|
2010-05-24 08:28:51 -07:00
|
|
|
{
|
2011-03-27 16:59:46 -07:00
|
|
|
if (!mDirty)
|
|
|
|
return;
|
|
|
|
mDirty = PR_FALSE;
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
if (!mTexture) {
|
2010-09-14 03:57:03 -07:00
|
|
|
CreateTexture();
|
2010-05-24 08:28:51 -07:00
|
|
|
NS_WARNING("CanvasLayerD3D9::Updated called but no texture present!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mGLContext) {
|
|
|
|
// WebGL reads entire surface.
|
|
|
|
D3DLOCKED_RECT r;
|
2010-09-14 03:57:25 -07:00
|
|
|
HRESULT hr = mTexture->LockRect(0, &r, NULL, 0);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
NS_WARNING("Failed to lock CanvasLayer texture.");
|
|
|
|
return;
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
PRUint8 *destination;
|
|
|
|
if (r.Pitch != mBounds.width * 4) {
|
|
|
|
destination = new PRUint8[mBounds.width * mBounds.height * 4];
|
|
|
|
} else {
|
|
|
|
destination = (PRUint8*)r.pBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to flush to ensure that any buffered GL operations are
|
|
|
|
// in the framebuffer before we read.
|
|
|
|
mGLContext->fFlush();
|
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
PRUint32 currentFramebuffer = 0;
|
|
|
|
|
|
|
|
mGLContext->fGetIntegerv(LOCAL_GL_FRAMEBUFFER_BINDING, (GLint*)¤tFramebuffer);
|
|
|
|
|
|
|
|
// Make sure that we read pixels from the correct framebuffer, regardless
|
|
|
|
// of what's currently bound.
|
|
|
|
if (currentFramebuffer != mCanvasFramebuffer)
|
|
|
|
mGLContext->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, mCanvasFramebuffer);
|
|
|
|
|
2010-08-06 22:09:18 -07:00
|
|
|
nsRefPtr<gfxImageSurface> tmpSurface =
|
|
|
|
new gfxImageSurface(destination,
|
|
|
|
gfxIntSize(mBounds.width, mBounds.height),
|
|
|
|
mBounds.width * 4,
|
|
|
|
gfxASurface::ImageFormatARGB32);
|
|
|
|
mGLContext->ReadPixelsIntoImageSurface(0, 0,
|
|
|
|
mBounds.width, mBounds.height,
|
|
|
|
tmpSurface);
|
|
|
|
tmpSurface = nsnull;
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
// Put back the previous framebuffer binding.
|
|
|
|
if (currentFramebuffer != mCanvasFramebuffer)
|
|
|
|
mGLContext->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, currentFramebuffer);
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
if (r.Pitch != mBounds.width * 4) {
|
|
|
|
for (int y = 0; y < mBounds.height; y++) {
|
|
|
|
memcpy((PRUint8*)r.pBits + r.Pitch * y,
|
|
|
|
destination + mBounds.width * 4 * y,
|
|
|
|
mBounds.width * 4);
|
|
|
|
}
|
|
|
|
delete [] destination;
|
|
|
|
}
|
|
|
|
mTexture->UnlockRect(0);
|
|
|
|
} else if (mSurface) {
|
|
|
|
RECT r;
|
2011-03-27 16:59:46 -07:00
|
|
|
r.left = mBounds.x;
|
|
|
|
r.top = mBounds.y;
|
|
|
|
r.right = mBounds.XMost();
|
|
|
|
r.bottom = mBounds.YMost();
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
D3DLOCKED_RECT lockedRect;
|
2010-09-14 03:57:25 -07:00
|
|
|
HRESULT hr = mTexture->LockRect(0, &lockedRect, &r, 0);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
NS_WARNING("Failed to lock CanvasLayer texture.");
|
|
|
|
return;
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> sourceSurface;
|
|
|
|
|
|
|
|
if (mSurface->GetType() == gfxASurface::SurfaceTypeWin32) {
|
2010-11-11 12:31:22 -08:00
|
|
|
sourceSurface = mSurface->GetAsImageSurface();
|
2010-05-24 08:28:51 -07:00
|
|
|
} else if (mSurface->GetType() == gfxASurface::SurfaceTypeImage) {
|
2010-08-23 20:57:59 -07:00
|
|
|
sourceSurface = static_cast<gfxImageSurface*>(mSurface.get());
|
2010-05-24 08:28:51 -07:00
|
|
|
if (sourceSurface->Format() != gfxASurface::ImageFormatARGB32 &&
|
|
|
|
sourceSurface->Format() != gfxASurface::ImageFormatRGB24)
|
|
|
|
{
|
|
|
|
mTexture->UnlockRect(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2011-03-27 16:59:46 -07:00
|
|
|
sourceSurface = new gfxImageSurface(gfxIntSize(mBounds.width, mBounds.height),
|
2010-05-24 08:28:51 -07:00
|
|
|
gfxASurface::ImageFormatARGB32);
|
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(sourceSurface);
|
|
|
|
ctx->SetOperator(gfxContext::OPERATOR_SOURCE);
|
2010-08-23 20:57:59 -07:00
|
|
|
ctx->SetSource(mSurface);
|
2010-05-24 08:28:51 -07:00
|
|
|
ctx->Paint();
|
|
|
|
}
|
|
|
|
|
2011-03-27 16:59:46 -07:00
|
|
|
PRUint8 *startBits = sourceSurface->Data();
|
|
|
|
PRUint32 sourceStride = sourceSurface->Stride();
|
|
|
|
|
2011-02-10 12:03:24 -08:00
|
|
|
if (sourceSurface->Format() != gfxASurface::ImageFormatARGB32) {
|
|
|
|
mHasAlpha = false;
|
|
|
|
} else {
|
|
|
|
mHasAlpha = true;
|
|
|
|
}
|
|
|
|
|
2011-03-27 16:59:46 -07:00
|
|
|
for (int y = 0; y < mBounds.height; y++) {
|
2010-05-24 08:28:51 -07:00
|
|
|
memcpy((PRUint8*)lockedRect.pBits + lockedRect.Pitch * y,
|
|
|
|
startBits + sourceStride * y,
|
2011-03-27 16:59:46 -07:00
|
|
|
mBounds.width * 4);
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
2010-05-28 20:27:03 -07:00
|
|
|
mTexture->UnlockRect(0);
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer*
|
|
|
|
CanvasLayerD3D9::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-08 01:06:15 -08:00
|
|
|
CanvasLayerD3D9::RenderLayer()
|
2010-05-24 08:28:51 -07:00
|
|
|
{
|
2011-03-27 16:59:46 -07:00
|
|
|
UpdateSurface();
|
2011-03-27 16:59:46 -07:00
|
|
|
FireDidTransactionCallback();
|
|
|
|
|
2011-03-27 16:59:46 -07:00
|
|
|
if (!mTexture)
|
|
|
|
return;
|
2010-09-14 03:57:03 -07:00
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
/*
|
2010-10-01 15:24:58 -07:00
|
|
|
* We flip the Y axis here, note we can only do this because we are in
|
|
|
|
* CULL_NONE mode!
|
2010-05-24 08:28:51 -07:00
|
|
|
*/
|
2010-10-01 15:24:58 -07:00
|
|
|
|
|
|
|
ShaderConstantRect quad(0, 0, mBounds.width, mBounds.height);
|
2010-05-24 08:28:51 -07:00
|
|
|
if (mNeedsYFlip) {
|
2010-10-01 15:24:58 -07:00
|
|
|
quad.mHeight = (float)-mBounds.height;
|
|
|
|
quad.mY = (float)mBounds.height;
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
2010-10-01 15:24:58 -07:00
|
|
|
device()->SetVertexShaderConstantF(CBvLayerQuad, quad, 1);
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2010-11-08 01:06:15 -08:00
|
|
|
SetShaderTransformAndOpacity();
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2011-02-10 12:03:24 -08:00
|
|
|
if (mHasAlpha) {
|
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER);
|
|
|
|
} else {
|
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBLAYER);
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2010-12-29 11:22:18 -08:00
|
|
|
if (mFilter == gfxPattern::FILTER_NEAREST) {
|
|
|
|
device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
|
|
|
|
device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
|
|
|
|
}
|
2010-08-23 20:58:27 -07:00
|
|
|
if (!mDataIsPremultiplied) {
|
2010-05-24 08:28:51 -07:00
|
|
|
device()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
|
2010-08-23 20:57:22 -07:00
|
|
|
device()->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
device()->SetTexture(0, mTexture);
|
|
|
|
device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
|
2010-08-23 20:58:27 -07:00
|
|
|
if (!mDataIsPremultiplied) {
|
2010-05-24 08:28:51 -07:00
|
|
|
device()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
|
2010-08-23 20:57:22 -07:00
|
|
|
device()->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
2010-12-29 11:22:18 -08:00
|
|
|
if (mFilter == gfxPattern::FILTER_NEAREST) {
|
|
|
|
device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
|
|
|
device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
2010-09-14 03:56:56 -07:00
|
|
|
void
|
|
|
|
CanvasLayerD3D9::CleanResources()
|
|
|
|
{
|
|
|
|
if (mD3DManager->deviceManager()->HasDynamicTextures()) {
|
|
|
|
// In this case we have a texture in POOL_DEFAULT
|
|
|
|
mTexture = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-15 15:15:49 -07:00
|
|
|
void
|
|
|
|
CanvasLayerD3D9::LayerManagerDestroyed()
|
|
|
|
{
|
|
|
|
mD3DManager->deviceManager()->mLayersWithResources.RemoveElement(this);
|
|
|
|
mD3DManager = nsnull;
|
|
|
|
}
|
|
|
|
|
2010-09-14 03:57:03 -07:00
|
|
|
void
|
|
|
|
CanvasLayerD3D9::CreateTexture()
|
|
|
|
{
|
|
|
|
if (mD3DManager->deviceManager()->HasDynamicTextures()) {
|
|
|
|
device()->CreateTexture(mBounds.width, mBounds.height, 1, D3DUSAGE_DYNAMIC,
|
|
|
|
D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT,
|
|
|
|
getter_AddRefs(mTexture), NULL);
|
|
|
|
} else {
|
|
|
|
// D3DPOOL_MANAGED is fine here since we require Dynamic Textures for D3D9Ex
|
|
|
|
// devices.
|
|
|
|
device()->CreateTexture(mBounds.width, mBounds.height, 1, 0,
|
|
|
|
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
|
|
|
|
getter_AddRefs(mTexture), NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
} /* namespace layers */
|
2010-07-18 22:01:14 -07:00
|
|
|
} /* namespace mozilla */
|