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
|
|
|
|
2013-04-24 11:42:40 -07:00
|
|
|
#include "mozilla/layers/PLayerTransaction.h"
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
#include "gfxWindowsSurface.h"
|
2010-09-07 20:27:36 -07:00
|
|
|
#include "gfxWindowsPlatform.h"
|
2013-02-13 15:26:24 -08:00
|
|
|
#include "SurfaceStream.h"
|
|
|
|
#include "SharedSurfaceGL.h"
|
2013-09-06 19:11:41 -07:00
|
|
|
#include "GLContext.h"
|
2011-07-04 06:15:05 -07:00
|
|
|
#include "CanvasLayerD3D9.h"
|
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
using namespace mozilla::gl;
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2013-09-06 19:11:41 -07:00
|
|
|
CanvasLayerD3D9::CanvasLayerD3D9(LayerManagerD3D9 *aManager)
|
|
|
|
: CanvasLayer(aManager, nullptr)
|
|
|
|
, LayerD3D9(aManager)
|
|
|
|
, mDataIsPremultiplied(false)
|
|
|
|
, mNeedsYFlip(false)
|
|
|
|
, mHasAlpha(true)
|
|
|
|
{
|
|
|
|
mImplData = static_cast<LayerD3D9*>(this);
|
|
|
|
aManager->deviceManager()->mLayersWithResources.AppendElement(this);
|
|
|
|
}
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
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)
|
|
|
|
{
|
2014-02-12 07:07:47 -08:00
|
|
|
NS_ASSERTION(mDrawTarget == 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;
|
|
|
|
mNeedsYFlip = false;
|
|
|
|
mDataIsPremultiplied = true;
|
2010-05-24 08:28:51 -07:00
|
|
|
} else if (aData.mGLContext) {
|
|
|
|
mGLContext = aData.mGLContext;
|
2013-02-13 15:26:24 -08:00
|
|
|
NS_ASSERTION(mGLContext->IsOffscreen(), "Canvas GLContext must be offscreen.");
|
|
|
|
mDataIsPremultiplied = aData.mIsGLAlphaPremult;
|
2011-10-17 07:59:28 -07:00
|
|
|
mNeedsYFlip = true;
|
2010-05-24 08:28:51 -07:00
|
|
|
} else {
|
2014-02-12 07:07:47 -08:00
|
|
|
NS_ERROR("CanvasLayer created without 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
|
|
|
}
|
|
|
|
|
2014-02-12 07:07:47 -08:00
|
|
|
RefPtr<SourceSurface> surface;
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
if (mGLContext) {
|
2014-03-10 19:08:49 -07:00
|
|
|
SharedSurface_GL* surf = mGLContext->RequestFrame();
|
2013-02-13 15:26:24 -08:00
|
|
|
if (!surf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SharedSurface_Basic* shareSurf = SharedSurface_Basic::Cast(surf);
|
2014-02-12 07:07:47 -08:00
|
|
|
surface = shareSurf->GetData();
|
2011-11-17 20:00:37 -08:00
|
|
|
} else {
|
2014-02-12 07:07:47 -08:00
|
|
|
surface = mDrawTarget->Snapshot();
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2014-02-12 07:07:47 -08:00
|
|
|
// WebGL reads entire surface.
|
|
|
|
LockTextureRectD3D9 textureLock(mTexture);
|
|
|
|
if (!textureLock.HasLock()) {
|
|
|
|
NS_WARNING("Failed to lock CanvasLayer texture.");
|
|
|
|
return;
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
2014-02-12 07:07:47 -08:00
|
|
|
|
|
|
|
D3DLOCKED_RECT rect = textureLock.GetLockRect();
|
2014-04-07 07:24:14 -07:00
|
|
|
IntSize boundsSize(mBounds.width, mBounds.height);
|
2014-02-12 07:07:47 -08:00
|
|
|
RefPtr<DrawTarget> rectDt = Factory::CreateDrawTargetForData(BackendType::CAIRO,
|
|
|
|
(uint8_t*)rect.pBits,
|
2014-04-07 07:24:14 -07:00
|
|
|
boundsSize,
|
2014-02-12 07:07:47 -08:00
|
|
|
rect.Pitch,
|
|
|
|
SurfaceFormat::B8G8R8A8);
|
|
|
|
|
|
|
|
Rect drawRect(0, 0, surface->GetSize().width, surface->GetSize().height);
|
|
|
|
rectDt->DrawSurface(surface, drawRect, drawRect,
|
|
|
|
DrawSurfaceOptions(), DrawOptions(1.0F, CompositionOp::OP_SOURCE));
|
|
|
|
rectDt->Flush();
|
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
|
|
|
{
|
2013-02-13 15:26:24 -08:00
|
|
|
FirePreTransactionCallback();
|
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
|
|
|
/*
|
2014-03-10 19:08:49 -07:00
|
|
|
* We flip the Y axis here, note we can only do this because we are in
|
2010-10-01 15:24:58 -07:00
|
|
|
* 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
|
|
|
|
2013-10-01 14:01:19 -07:00
|
|
|
if (mFilter == GraphicsFilter::FILTER_NEAREST) {
|
2010-12-29 11:22:18 -08:00
|
|
|
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
|
|
|
}
|
2013-10-01 14:01:19 -07:00
|
|
|
if (mFilter == GraphicsFilter::FILTER_NEAREST) {
|
2010-12-29 11:22:18 -08:00
|
|
|
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,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(mTexture), nullptr);
|
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,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(mTexture), nullptr);
|
2011-11-03 12:10:10 -07:00
|
|
|
}
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
mD3DManager->ReportFailure(NS_LITERAL_CSTRING("CanvasLayerD3D9::CreateTexture() failed"),
|
|
|
|
hr);
|
|
|
|
return;
|
2010-09-14 03:57:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
} /* namespace layers */
|
2010-07-18 22:01:14 -07:00
|
|
|
} /* namespace mozilla */
|