2010-09-30 15:53:49 -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-09-30 15:53:49 -07:00
|
|
|
|
|
|
|
#include "CanvasLayerD3D10.h"
|
|
|
|
|
2011-08-09 12:38:26 -07:00
|
|
|
#include "../d3d9/Nv3DVUtils.h"
|
2010-09-30 15:53:49 -07:00
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "gfxWindowsSurface.h"
|
|
|
|
#include "gfxWindowsPlatform.h"
|
|
|
|
|
2011-06-24 10:41:18 -07:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2010-09-30 15:53:49 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
|
|
|
CanvasLayerD3D10::~CanvasLayerD3D10()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasLayerD3D10::Initialize(const Data& aData)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(mSurface == nullptr, "BasicCanvasLayer::Initialize called twice!");
|
2010-09-30 15:53:49 -07:00
|
|
|
|
|
|
|
if (aData.mSurface) {
|
|
|
|
mSurface = aData.mSurface;
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(aData.mGLContext == nullptr && !aData.mDrawTarget,
|
2011-06-24 10:41:18 -07:00
|
|
|
"CanvasLayer can't have both surface and GLContext/DrawTarget");
|
2011-10-17 07:59:28 -07:00
|
|
|
mNeedsYFlip = false;
|
|
|
|
mDataIsPremultiplied = true;
|
2010-09-30 15:53:49 -07:00
|
|
|
} else if (aData.mGLContext) {
|
|
|
|
NS_ASSERTION(aData.mGLContext->IsOffscreen(), "canvas gl context isn't offscreen");
|
|
|
|
mGLContext = aData.mGLContext;
|
|
|
|
mDataIsPremultiplied = aData.mGLBufferIsPremultiplied;
|
2011-10-17 07:59:28 -07:00
|
|
|
mNeedsYFlip = true;
|
2011-06-24 10:41:18 -07:00
|
|
|
} else if (aData.mDrawTarget) {
|
|
|
|
mDrawTarget = aData.mDrawTarget;
|
2012-08-01 21:01:05 -07:00
|
|
|
mNeedsYFlip = false;
|
|
|
|
mDataIsPremultiplied = true;
|
2011-06-24 10:41:18 -07:00
|
|
|
void *texture = mDrawTarget->GetNativeSurface(NATIVE_SURFACE_D3D10_TEXTURE);
|
|
|
|
|
2012-08-01 21:01:05 -07:00
|
|
|
if (texture) {
|
|
|
|
mTexture = static_cast<ID3D10Texture2D*>(texture);
|
2011-06-24 10:41:18 -07:00
|
|
|
|
2012-08-01 21:01:05 -07:00
|
|
|
NS_ASSERTION(aData.mGLContext == nullptr && aData.mSurface == nullptr,
|
|
|
|
"CanvasLayer can't have both surface and GLContext/Surface");
|
2011-06-24 10:41:18 -07:00
|
|
|
|
2012-08-01 21:01:05 -07:00
|
|
|
mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height);
|
|
|
|
device()->CreateShaderResourceView(mTexture, NULL, getter_AddRefs(mSRView));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX we should store mDrawTarget and use it directly in UpdateSurface,
|
|
|
|
// bypassing Thebes
|
|
|
|
mSurface = gfxPlatform::GetPlatform()->GetThebesSurfaceForDrawTarget(mDrawTarget);
|
2010-09-30 15:53:49 -07:00
|
|
|
} else {
|
2011-06-24 10:41:18 -07:00
|
|
|
NS_ERROR("CanvasLayer created without mSurface, mDrawTarget or mGLContext?");
|
2010-09-30 15:53:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height);
|
|
|
|
|
|
|
|
if (mSurface && mSurface->GetType() == gfxASurface::SurfaceTypeD2D) {
|
|
|
|
void *data = mSurface->GetData(&gKeyD3D10Texture);
|
|
|
|
if (data) {
|
|
|
|
mTexture = static_cast<ID3D10Texture2D*>(data);
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsD2DTexture = true;
|
2010-09-30 15:53:49 -07:00
|
|
|
device()->CreateShaderResourceView(mTexture, NULL, getter_AddRefs(mSRView));
|
2011-02-11 14:46:28 -08:00
|
|
|
mHasAlpha =
|
|
|
|
mSurface->GetContentType() == gfxASurface::CONTENT_COLOR_ALPHA;
|
2010-09-30 15:53:49 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsD2DTexture = false;
|
|
|
|
mUsingSharedTexture = false;
|
2010-09-30 15:53:49 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
HANDLE shareHandle = mGLContext ? mGLContext->GetD3DShareHandle() : nullptr;
|
2011-01-06 14:07:12 -08:00
|
|
|
if (shareHandle) {
|
|
|
|
HRESULT hr = device()->OpenSharedResource(shareHandle, __uuidof(ID3D10Texture2D), getter_AddRefs(mTexture));
|
2011-05-24 08:05:56 -07:00
|
|
|
if (SUCCEEDED(hr))
|
2011-10-17 07:59:28 -07:00
|
|
|
mUsingSharedTexture = true;
|
2011-01-06 14:07:12 -08:00
|
|
|
}
|
2010-09-30 15:53:49 -07:00
|
|
|
|
2011-07-28 13:37:58 -07:00
|
|
|
if (mUsingSharedTexture) {
|
2012-07-25 09:13:45 -07:00
|
|
|
mNeedsYFlip = true;
|
2011-07-28 13:37:58 -07:00
|
|
|
} else {
|
2011-01-06 14:07:12 -08:00
|
|
|
CD3D10_TEXTURE2D_DESC desc(DXGI_FORMAT_B8G8R8A8_UNORM, mBounds.width, mBounds.height, 1, 1);
|
|
|
|
desc.Usage = D3D10_USAGE_DYNAMIC;
|
|
|
|
desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
|
|
|
|
|
|
|
|
HRESULT hr = device()->CreateTexture2D(&desc, NULL, getter_AddRefs(mTexture));
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
NS_WARNING("Failed to create texture for CanvasLayer!");
|
|
|
|
return;
|
|
|
|
}
|
2010-09-30 15:53:49 -07:00
|
|
|
}
|
2011-01-06 14:07:12 -08:00
|
|
|
|
2010-09-30 15:53:49 -07:00
|
|
|
device()->CreateShaderResourceView(mTexture, NULL, getter_AddRefs(mSRView));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-27 16:59:46 -07:00
|
|
|
CanvasLayerD3D10::UpdateSurface()
|
2010-09-30 15:53:49 -07:00
|
|
|
{
|
2011-03-27 16:59:46 -07:00
|
|
|
if (!mDirty)
|
|
|
|
return;
|
2011-10-17 07:59:28 -07:00
|
|
|
mDirty = false;
|
2011-03-27 16:59:46 -07:00
|
|
|
|
2011-06-24 10:41:18 -07:00
|
|
|
if (mDrawTarget) {
|
|
|
|
mDrawTarget->Flush();
|
2012-08-01 21:01:05 -07:00
|
|
|
} else if (mIsD2DTexture) {
|
2010-09-30 15:53:49 -07:00
|
|
|
mSurface->Flush();
|
|
|
|
return;
|
2012-08-01 21:01:05 -07:00
|
|
|
} else if (mUsingSharedTexture) {
|
2011-01-06 14:07:12 -08:00
|
|
|
// need to sync on the d3d9 device
|
|
|
|
if (mGLContext) {
|
|
|
|
mGLContext->MakeCurrent();
|
2011-12-16 14:24:46 -08:00
|
|
|
mGLContext->GuaranteeResolve();
|
2011-01-06 14:07:12 -08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2010-09-30 15:53:49 -07:00
|
|
|
if (mGLContext) {
|
|
|
|
// WebGL reads entire surface.
|
|
|
|
D3D10_MAPPED_TEXTURE2D map;
|
|
|
|
|
|
|
|
HRESULT hr = mTexture->Map(0, D3D10_MAP_WRITE_DISCARD, 0, &map);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
NS_WARNING("Failed to map CanvasLayer texture.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-25 12:50:26 -07:00
|
|
|
const bool stridesMatch = map.RowPitch == mBounds.width * 4;
|
|
|
|
|
2010-09-30 15:53:49 -07:00
|
|
|
PRUint8 *destination;
|
2012-03-25 12:50:26 -07:00
|
|
|
if (!stridesMatch) {
|
|
|
|
destination = GetTempBlob(mBounds.width * mBounds.height * 4);
|
2010-09-30 15:53:49 -07:00
|
|
|
} else {
|
2012-03-25 12:50:26 -07:00
|
|
|
DiscardTempBlob();
|
2010-09-30 15:53:49 -07:00
|
|
|
destination = (PRUint8*)map.pData;
|
|
|
|
}
|
|
|
|
|
2011-10-07 12:16:48 -07:00
|
|
|
mGLContext->MakeCurrent();
|
|
|
|
|
2010-09-30 15:53:49 -07:00
|
|
|
nsRefPtr<gfxImageSurface> tmpSurface =
|
|
|
|
new gfxImageSurface(destination,
|
|
|
|
gfxIntSize(mBounds.width, mBounds.height),
|
|
|
|
mBounds.width * 4,
|
|
|
|
gfxASurface::ImageFormatARGB32);
|
2012-08-20 17:13:32 -07:00
|
|
|
mGLContext->ReadScreenIntoImageSurface(tmpSurface);
|
2012-07-30 07:20:58 -07:00
|
|
|
tmpSurface = nullptr;
|
2010-09-30 15:53:49 -07:00
|
|
|
|
2012-03-25 12:50:26 -07:00
|
|
|
if (!stridesMatch) {
|
2010-09-30 15:53:49 -07:00
|
|
|
for (int y = 0; y < mBounds.height; y++) {
|
|
|
|
memcpy((PRUint8*)map.pData + map.RowPitch * y,
|
|
|
|
destination + mBounds.width * 4 * y,
|
|
|
|
mBounds.width * 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mTexture->Unmap(0);
|
|
|
|
} else if (mSurface) {
|
|
|
|
RECT r;
|
2011-03-27 16:59:46 -07:00
|
|
|
r.left = 0;
|
|
|
|
r.top = 0;
|
|
|
|
r.right = mBounds.width;
|
|
|
|
r.bottom = mBounds.height;
|
2010-09-30 15:53:49 -07:00
|
|
|
|
|
|
|
D3D10_MAPPED_TEXTURE2D map;
|
|
|
|
HRESULT hr = mTexture->Map(0, D3D10_MAP_WRITE_DISCARD, 0, &map);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
NS_WARNING("Failed to lock CanvasLayer texture.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> dstSurface;
|
|
|
|
|
|
|
|
dstSurface = new gfxImageSurface((unsigned char*)map.pData,
|
2011-03-27 16:59:46 -07:00
|
|
|
gfxIntSize(mBounds.width, mBounds.height),
|
2010-09-30 15:53:49 -07:00
|
|
|
map.RowPitch,
|
|
|
|
gfxASurface::ImageFormatARGB32);
|
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(dstSurface);
|
|
|
|
ctx->SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
ctx->SetSource(mSurface);
|
|
|
|
ctx->Paint();
|
|
|
|
|
|
|
|
mTexture->Unmap(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer*
|
|
|
|
CanvasLayerD3D10::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-08 01:06:15 -08:00
|
|
|
CanvasLayerD3D10::RenderLayer()
|
2010-09-30 15:53:49 -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
|
|
|
|
2011-03-27 16:59:46 -07:00
|
|
|
if (!mTexture)
|
2010-09-30 15:53:49 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
nsIntRect visibleRect = mVisibleRegion.GetBounds();
|
|
|
|
|
2010-11-08 01:06:15 -08:00
|
|
|
SetEffectTransformAndOpacity();
|
2010-09-30 15:53:49 -07:00
|
|
|
|
2012-03-01 09:29:30 -08:00
|
|
|
PRUint8 shaderFlags = 0;
|
|
|
|
shaderFlags |= LoadMaskTexture();
|
|
|
|
shaderFlags |= mDataIsPremultiplied
|
|
|
|
? SHADER_PREMUL : SHADER_NON_PREMUL | SHADER_RGBA;
|
|
|
|
shaderFlags |= mHasAlpha ? SHADER_RGBA : SHADER_RGB;
|
|
|
|
shaderFlags |= mFilter == gfxPattern::FILTER_NEAREST
|
|
|
|
? SHADER_POINT : SHADER_LINEAR;
|
|
|
|
ID3D10EffectTechnique* technique = SelectShader(shaderFlags);
|
2010-09-30 15:53:49 -07:00
|
|
|
|
|
|
|
if (mSRView) {
|
|
|
|
effect()->GetVariableByName("tRGB")->AsShaderResource()->SetResource(mSRView);
|
|
|
|
}
|
|
|
|
|
|
|
|
effect()->GetVariableByName("vLayerQuad")->AsVector()->SetFloatVector(
|
|
|
|
ShaderConstantRectD3D10(
|
|
|
|
(float)mBounds.x,
|
|
|
|
(float)mBounds.y,
|
|
|
|
(float)mBounds.width,
|
|
|
|
(float)mBounds.height)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (mNeedsYFlip) {
|
|
|
|
effect()->GetVariableByName("vTextureCoords")->AsVector()->SetFloatVector(
|
|
|
|
ShaderConstantRectD3D10(
|
|
|
|
0,
|
|
|
|
1.0f,
|
|
|
|
1.0f,
|
|
|
|
-1.0f)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
technique->GetPassByIndex(0)->Apply(0);
|
|
|
|
device()->Draw(4, 0);
|
|
|
|
|
|
|
|
if (mNeedsYFlip) {
|
|
|
|
effect()->GetVariableByName("vTextureCoords")->AsVector()->
|
|
|
|
SetFloatVector(ShaderConstantRectD3D10(0, 0, 1.0f, 1.0f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace layers */
|
|
|
|
} /* namespace mozilla */
|