2010-05-24 08:28:51 -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-05-24 08:28:51 -07:00
|
|
|
|
2011-07-04 06:15:05 -07:00
|
|
|
|
2012-07-12 05:51:58 -07:00
|
|
|
#include "ipc/AutoOpenSurface.h"
|
2011-07-04 06:15:05 -07:00
|
|
|
#include "mozilla/layers/PLayers.h"
|
|
|
|
#include "mozilla/layers/ShadowLayers.h"
|
|
|
|
#include "ShadowBufferD3D9.h"
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
#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
|
|
|
|
2011-07-04 06:15:05 -07:00
|
|
|
#include "CanvasLayerD3D9.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)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(mSurface == nullptr, "BasicCanvasLayer::Initialize called twice!");
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2011-11-17 20:00:37 -08:00
|
|
|
if (aData.mDrawTarget) {
|
|
|
|
mDrawTarget = aData.mDrawTarget;
|
2012-07-25 19:30:20 -07:00
|
|
|
mSurface = gfxPlatform::GetPlatform()->GetThebesSurfaceForDrawTarget(mDrawTarget);
|
2011-11-17 20:00:37 -08:00
|
|
|
mNeedsYFlip = false;
|
|
|
|
mDataIsPremultiplied = true;
|
|
|
|
} else if (aData.mSurface) {
|
2010-05-24 08:28:51 -07:00
|
|
|
mSurface = aData.mSurface;
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(aData.mGLContext == nullptr,
|
2013-02-21 11:54:25 -08:00
|
|
|
"CanvasLayer can't have both surface and GLContext");
|
2011-10-17 07:59:28 -07:00
|
|
|
mNeedsYFlip = false;
|
|
|
|
mDataIsPremultiplied = true;
|
2010-05-24 08:28:51 -07:00
|
|
|
} else if (aData.mGLContext) {
|
2013-02-21 11:54:25 -08:00
|
|
|
NS_ASSERTION(aData.mGLContext->IsOffscreen(), "canvas gl context isn't offscreen");
|
2010-05-24 08:28:51 -07:00
|
|
|
mGLContext = aData.mGLContext;
|
2013-02-21 11:54:25 -08:00
|
|
|
mDataIsPremultiplied = aData.mGLBufferIsPremultiplied;
|
2011-10-17 07:59:28 -07:00
|
|
|
mNeedsYFlip = true;
|
2010-05-24 08:28:51 -07:00
|
|
|
} else {
|
2011-11-17 20:00:37 -08:00
|
|
|
NS_ERROR("CanvasLayer created without mSurface, mGLContext or mDrawTarget?");
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2012-09-29 23:20:25 -07:00
|
|
|
if (!IsDirty() && mTexture)
|
2011-03-27 16:59:46 -07:00
|
|
|
return;
|
2012-09-29 23:20:25 -07:00
|
|
|
Painted();
|
2011-03-27 16:59:46 -07:00
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
if (!mTexture) {
|
2010-09-14 03:57:03 -07:00
|
|
|
CreateTexture();
|
2012-01-17 09:32:24 -08:00
|
|
|
|
|
|
|
if (!mTexture) {
|
|
|
|
NS_WARNING("CanvasLayerD3D9::Updated called but no texture present and creation failed!");
|
|
|
|
return;
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mGLContext) {
|
|
|
|
// WebGL reads entire surface.
|
2011-07-04 06:15:05 -07:00
|
|
|
LockTextureRectD3D9 textureLock(mTexture);
|
|
|
|
if (!textureLock.HasLock()) {
|
2010-09-14 03:57:25 -07:00
|
|
|
NS_WARNING("Failed to lock CanvasLayer texture.");
|
|
|
|
return;
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2013-02-21 11:54:25 -08:00
|
|
|
D3DLOCKED_RECT r = textureLock.GetLockRect();
|
2011-07-04 06:15:05 -07:00
|
|
|
|
2013-02-21 11:54:25 -08:00
|
|
|
const bool stridesMatch = r.Pitch == mBounds.width * 4;
|
2012-03-25 12:50:26 -07:00
|
|
|
|
2013-02-21 11:54:25 -08:00
|
|
|
uint8_t *destination;
|
|
|
|
if (!stridesMatch) {
|
|
|
|
destination = GetTempBlob(mBounds.width * mBounds.height * 4);
|
|
|
|
} else {
|
|
|
|
DiscardTempBlob();
|
|
|
|
destination = (uint8_t*)r.pBits;
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2013-02-21 11:54:25 -08:00
|
|
|
mGLContext->MakeCurrent();
|
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> tmpSurface =
|
|
|
|
new gfxImageSurface(destination,
|
|
|
|
gfxIntSize(mBounds.width, mBounds.height),
|
|
|
|
mBounds.width * 4,
|
|
|
|
gfxASurface::ImageFormatARGB32);
|
|
|
|
mGLContext->ReadScreenIntoImageSurface(tmpSurface);
|
|
|
|
tmpSurface = nullptr;
|
|
|
|
|
|
|
|
if (!stridesMatch) {
|
|
|
|
for (int y = 0; y < mBounds.height; y++) {
|
|
|
|
memcpy((uint8_t*)r.pBits + r.Pitch * y,
|
|
|
|
destination + mBounds.width * 4 * y,
|
|
|
|
mBounds.width * 4);
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
2011-11-17 20:00:37 -08:00
|
|
|
} else {
|
2010-05-24 08:28:51 -07:00
|
|
|
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
|
|
|
|
2011-07-04 06:15:05 -07:00
|
|
|
LockTextureRectD3D9 textureLock(mTexture);
|
|
|
|
if (!textureLock.HasLock()) {
|
2010-09-14 03:57:25 -07:00
|
|
|
NS_WARNING("Failed to lock CanvasLayer texture.");
|
|
|
|
return;
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2011-07-04 06:15:05 -07:00
|
|
|
D3DLOCKED_RECT lockedRect = textureLock.GetLockRect();
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
nsRefPtr<gfxImageSurface> sourceSurface;
|
|
|
|
|
2012-07-25 19:30:20 -07:00
|
|
|
if (mSurface->GetType() == gfxASurface::SurfaceTypeWin32) {
|
|
|
|
sourceSurface = mSurface->GetAsImageSurface();
|
|
|
|
} else if (mSurface->GetType() == gfxASurface::SurfaceTypeImage) {
|
|
|
|
sourceSurface = static_cast<gfxImageSurface*>(mSurface.get());
|
2010-05-24 08:28:51 -07:00
|
|
|
if (sourceSurface->Format() != gfxASurface::ImageFormatARGB32 &&
|
|
|
|
sourceSurface->Format() != gfxASurface::ImageFormatRGB24)
|
|
|
|
{
|
|
|
|
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);
|
2012-07-25 19:30:20 -07:00
|
|
|
ctx->SetSource(mSurface);
|
2010-05-24 08:28:51 -07:00
|
|
|
ctx->Paint();
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t *startBits = sourceSurface->Data();
|
|
|
|
uint32_t sourceStride = sourceSurface->Stride();
|
2011-03-27 16:59:46 -07:00
|
|
|
|
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++) {
|
2012-08-22 08:56:38 -07:00
|
|
|
memcpy((uint8_t*)lockedRect.pBits + lockedRect.Pitch * y,
|
2010-05-24 08:28:51 -07:00
|
|
|
startBits + sourceStride * y,
|
2011-03-27 16:59:46 -07:00
|
|
|
mBounds.width * 4);
|
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();
|
2012-08-13 03:10:10 -07:00
|
|
|
if (mD3DManager->CompositingDisabled()) {
|
|
|
|
return;
|
|
|
|
}
|
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) {
|
2012-03-06 13:07:48 -08:00
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER, GetMaskLayer());
|
2011-02-10 12:03:24 -08:00
|
|
|
} else {
|
2012-03-06 13:07:48 -08:00
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBLAYER, GetMaskLayer());
|
2011-02-10 12:03:24 -08:00
|
|
|
}
|
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
|
2012-07-30 07:20:58 -07:00
|
|
|
mTexture = nullptr;
|
2010-09-14 03:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-15 15:15:49 -07:00
|
|
|
void
|
|
|
|
CanvasLayerD3D9::LayerManagerDestroyed()
|
|
|
|
{
|
|
|
|
mD3DManager->deviceManager()->mLayersWithResources.RemoveElement(this);
|
2012-07-30 07:20:58 -07:00
|
|
|
mD3DManager = nullptr;
|
2010-09-15 15:15:49 -07:00
|
|
|
}
|
|
|
|
|
2010-09-14 03:57:03 -07:00
|
|
|
void
|
|
|
|
CanvasLayerD3D9::CreateTexture()
|
|
|
|
{
|
2011-11-03 12:10:10 -07:00
|
|
|
HRESULT hr;
|
2010-09-14 03:57:03 -07:00
|
|
|
if (mD3DManager->deviceManager()->HasDynamicTextures()) {
|
2011-11-03 12:10:10 -07:00
|
|
|
hr = device()->CreateTexture(mBounds.width, mBounds.height, 1, D3DUSAGE_DYNAMIC,
|
|
|
|
D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT,
|
|
|
|
getter_AddRefs(mTexture), NULL);
|
2010-09-14 03:57:03 -07:00
|
|
|
} else {
|
|
|
|
// D3DPOOL_MANAGED is fine here since we require Dynamic Textures for D3D9Ex
|
|
|
|
// devices.
|
2011-11-03 12:10:10 -07:00
|
|
|
hr = device()->CreateTexture(mBounds.width, mBounds.height, 1, 0,
|
|
|
|
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
|
|
|
|
getter_AddRefs(mTexture), NULL);
|
|
|
|
}
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
mD3DManager->ReportFailure(NS_LITERAL_CSTRING("CanvasLayerD3D9::CreateTexture() failed"),
|
|
|
|
hr);
|
|
|
|
return;
|
2010-09-14 03:57:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-04 06:15:05 -07:00
|
|
|
ShadowCanvasLayerD3D9::ShadowCanvasLayerD3D9(LayerManagerD3D9* aManager)
|
2012-07-30 07:20:58 -07:00
|
|
|
: ShadowCanvasLayer(aManager, nullptr)
|
2011-07-04 06:15:05 -07:00
|
|
|
, LayerD3D9(aManager)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mNeedsYFlip(false)
|
2011-07-04 06:15:05 -07:00
|
|
|
{
|
|
|
|
mImplData = static_cast<LayerD3D9*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ShadowCanvasLayerD3D9::~ShadowCanvasLayerD3D9()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowCanvasLayerD3D9::Initialize(const Data& aData)
|
|
|
|
{
|
|
|
|
NS_RUNTIMEABORT("Non-shadow layer API unexpectedly used for shadow layer");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-27 15:19:28 -07:00
|
|
|
ShadowCanvasLayerD3D9::Init(bool needYFlip)
|
2011-07-04 06:15:05 -07:00
|
|
|
{
|
|
|
|
if (!mBuffer) {
|
|
|
|
mBuffer = new ShadowBufferD3D9(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
mNeedsYFlip = needYFlip;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-27 15:19:28 -07:00
|
|
|
ShadowCanvasLayerD3D9::Swap(const CanvasSurface& aNewFront,
|
|
|
|
bool needYFlip,
|
|
|
|
CanvasSurface* aNewBack)
|
2011-07-04 06:15:05 -07:00
|
|
|
{
|
2011-09-27 15:19:28 -07:00
|
|
|
NS_ASSERTION(aNewFront.type() == CanvasSurface::TSurfaceDescriptor,
|
|
|
|
"ShadowCanvasLayerD3D9::Swap expected CanvasSurface surface");
|
2011-07-04 06:15:05 -07:00
|
|
|
|
2012-07-12 05:51:58 -07:00
|
|
|
AutoOpenSurface surf(OPEN_READ_ONLY, aNewFront);
|
2011-09-27 15:19:28 -07:00
|
|
|
if (!mBuffer) {
|
|
|
|
Init(needYFlip);
|
2011-07-04 06:15:05 -07:00
|
|
|
}
|
2012-07-12 05:51:58 -07:00
|
|
|
mBuffer->Upload(surf.Get(), GetVisibleRegion().GetBounds());
|
2011-07-04 06:15:05 -07:00
|
|
|
|
|
|
|
*aNewBack = aNewFront;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowCanvasLayerD3D9::DestroyFrontBuffer()
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowCanvasLayerD3D9::Disconnect()
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowCanvasLayerD3D9::Destroy()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mBuffer = nullptr;
|
2011-07-04 06:15:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowCanvasLayerD3D9::CleanResources()
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowCanvasLayerD3D9::LayerManagerDestroyed()
|
|
|
|
{
|
|
|
|
mD3DManager->deviceManager()->mLayersWithResources.RemoveElement(this);
|
2012-07-30 07:20:58 -07:00
|
|
|
mD3DManager = nullptr;
|
2011-07-04 06:15:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Layer*
|
|
|
|
ShadowCanvasLayerD3D9::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowCanvasLayerD3D9::RenderLayer()
|
|
|
|
{
|
2012-08-13 03:10:10 -07:00
|
|
|
if (!mBuffer || mD3DManager->CompositingDisabled()) {
|
2011-07-04 06:15:05 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBuffer->RenderTo(mD3DManager, GetEffectiveVisibleRegion());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
} /* namespace layers */
|
2010-07-18 22:01:14 -07:00
|
|
|
} /* namespace mozilla */
|