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"
|
2013-02-13 15:26:24 -08:00
|
|
|
#include "SurfaceStream.h"
|
|
|
|
#include "SharedSurfaceANGLE.h"
|
|
|
|
#include "gfxContext.h"
|
2013-09-04 05:14:52 -07:00
|
|
|
#include "GLContext.h"
|
2010-09-30 15:53:49 -07:00
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
using namespace mozilla::gl;
|
2011-06-24 10:41:18 -07:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2010-09-30 15:53:49 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2013-09-04 05:14:52 -07:00
|
|
|
CanvasLayerD3D10::CanvasLayerD3D10(LayerManagerD3D10 *aManager)
|
|
|
|
: CanvasLayer(aManager, nullptr)
|
|
|
|
, LayerD3D10(aManager)
|
|
|
|
, mDataIsPremultiplied(false)
|
|
|
|
, mNeedsYFlip(false)
|
|
|
|
, mHasAlpha(true)
|
|
|
|
{
|
|
|
|
mImplData = static_cast<LayerD3D10*>(this);
|
|
|
|
mForceReadback = Preferences::GetBool("webgl.force-layers-readback", false);
|
|
|
|
}
|
|
|
|
|
2010-09-30 15:53:49 -07:00
|
|
|
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
|
|
|
|
2014-02-06 06:46:30 -08:00
|
|
|
if (aData.mGLContext) {
|
2010-09-30 15:53:49 -07:00
|
|
|
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;
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
GLScreenBuffer* screen = mGLContext->Screen();
|
|
|
|
SurfaceStreamType streamType =
|
|
|
|
SurfaceStream::ChooseGLStreamType(SurfaceStream::MainThread,
|
|
|
|
screen->PreserveBuffer());
|
|
|
|
|
|
|
|
SurfaceFactory_GL* factory = nullptr;
|
|
|
|
if (!mForceReadback) {
|
2014-01-07 12:02:18 -08:00
|
|
|
if (mGLContext->IsANGLE()) {
|
|
|
|
factory = SurfaceFactory_ANGLEShareHandle::Create(mGLContext,
|
|
|
|
device(),
|
|
|
|
screen->Caps());
|
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (factory) {
|
|
|
|
screen->Morph(factory, streamType);
|
|
|
|
}
|
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;
|
2014-01-10 11:06:16 -08:00
|
|
|
void *texture = mDrawTarget->GetNativeSurface(NativeSurfaceType::D3D10_TEXTURE);
|
2011-06-24 10:41:18 -07:00
|
|
|
|
2012-08-01 21:01:05 -07:00
|
|
|
if (texture) {
|
|
|
|
mTexture = static_cast<ID3D10Texture2D*>(texture);
|
2011-06-24 10:41:18 -07:00
|
|
|
|
2014-02-06 06:46:30 -08:00
|
|
|
NS_ASSERTION(!aData.mGLContext,
|
|
|
|
"CanvasLayer can't have both DrawTarget and WebGLContext/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);
|
2013-07-20 01:48:55 -07:00
|
|
|
device()->CreateShaderResourceView(mTexture, nullptr, getter_AddRefs(mSRView));
|
2012-08-01 21:01:05 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX we should store mDrawTarget and use it directly in UpdateSurface,
|
|
|
|
// bypassing Thebes
|
2014-02-12 07:07:46 -08:00
|
|
|
mSurface = mDrawTarget->Snapshot();
|
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);
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsD2DTexture = false;
|
2013-02-20 07:01:20 -08:00
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
// Create a texture in case we need to readback.
|
|
|
|
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;
|
2013-02-21 11:54:25 -08:00
|
|
|
|
2013-07-20 01:48:55 -07:00
|
|
|
HRESULT hr = device()->CreateTexture2D(&desc, nullptr, getter_AddRefs(mTexture));
|
2013-02-13 15:26:24 -08:00
|
|
|
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
|
|
|
|
2013-07-20 01:48:55 -07:00
|
|
|
device()->CreateShaderResourceView(mTexture, nullptr, getter_AddRefs(mUploadSRView));
|
2010-09-30 15:53:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-27 16:59:46 -07:00
|
|
|
CanvasLayerD3D10::UpdateSurface()
|
2010-09-30 15:53:49 -07:00
|
|
|
{
|
2012-09-29 23:20:25 -07:00
|
|
|
if (!IsDirty())
|
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
|
|
|
|
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
|
|
|
return;
|
2011-01-06 14:07:12 -08:00
|
|
|
}
|
2010-09-30 15:53:49 -07:00
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
if (mGLContext) {
|
|
|
|
SharedSurface* surf = mGLContext->RequestFrame();
|
|
|
|
if (!surf)
|
|
|
|
return;
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
switch (surf->Type()) {
|
|
|
|
case SharedSurfaceType::EGLSurfaceANGLE: {
|
|
|
|
SharedSurface_ANGLEShareHandle* shareSurf = SharedSurface_ANGLEShareHandle::Cast(surf);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
mSRView = shareSurf->GetSRV();
|
|
|
|
return;
|
2013-02-21 11:54:25 -08:00
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
case SharedSurfaceType::Basic: {
|
|
|
|
SharedSurface_Basic* shareSurf = SharedSurface_Basic::Cast(surf);
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2013-12-10 08:11:58 -08:00
|
|
|
DataSourceSurface* frameData = shareSurf->GetData();
|
2014-01-13 11:06:22 -08:00
|
|
|
// Scope for DrawTarget, so it's destroyed before Unmap.
|
2013-02-13 15:26:24 -08:00
|
|
|
{
|
2014-01-10 11:06:16 -08:00
|
|
|
RefPtr<DrawTarget> mapDt = Factory::CreateDrawTargetForData(BackendType::CAIRO,
|
2013-12-10 08:11:58 -08:00
|
|
|
(uint8_t*)map.pData,
|
2014-01-13 11:06:22 -08:00
|
|
|
frameData->GetSize(),
|
2013-12-10 08:11:58 -08:00
|
|
|
map.RowPitch,
|
2014-01-10 11:06:16 -08:00
|
|
|
SurfaceFormat::B8G8R8A8);
|
2013-12-10 08:11:58 -08:00
|
|
|
|
2014-01-13 11:06:22 -08:00
|
|
|
Rect drawRect(0, 0, frameData->GetSize().width, frameData->GetSize().height);
|
|
|
|
mapDt->DrawSurface(frameData, drawRect, drawRect,
|
|
|
|
DrawSurfaceOptions(), DrawOptions(1.0F, CompositionOp::OP_SOURCE));
|
2013-12-10 08:11:58 -08:00
|
|
|
mapDt->Flush();
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mTexture->Unmap(0);
|
|
|
|
mSRView = mUploadSRView;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unhandled SharedSurfaceType.");
|
2010-09-30 15:53:49 -07:00
|
|
|
}
|
|
|
|
} else if (mSurface) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-12 07:07:46 -08:00
|
|
|
RefPtr<DrawTarget> destTarget =
|
|
|
|
Factory::CreateDrawTargetForD3D10Texture(mTexture,
|
|
|
|
SurfaceFormat::R8G8B8A8);
|
|
|
|
Rect r(Point(0, 0), ToRect(mBounds).Size());
|
|
|
|
destTarget->DrawSurface(mSurface, r, r, DrawSurfaceOptions(),
|
|
|
|
DrawOptions(1.0F, CompositionOp::OP_SOURCE));
|
2010-09-30 15:53:49 -07:00
|
|
|
|
|
|
|
mTexture->Unmap(0);
|
2013-02-13 15:26:24 -08:00
|
|
|
mSRView = mUploadSRView;
|
2010-09-30 15:53:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer*
|
|
|
|
CanvasLayerD3D10::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-08 01:06:15 -08:00
|
|
|
CanvasLayerD3D10::RenderLayer()
|
2010-09-30 15:53:49 -07:00
|
|
|
{
|
2013-02-13 15:26:24 -08:00
|
|
|
FirePreTransactionCallback();
|
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-08-22 08:56:38 -07:00
|
|
|
uint8_t shaderFlags = 0;
|
2012-03-01 09:29:30 -08:00
|
|
|
shaderFlags |= LoadMaskTexture();
|
|
|
|
shaderFlags |= mDataIsPremultiplied
|
|
|
|
? SHADER_PREMUL : SHADER_NON_PREMUL | SHADER_RGBA;
|
|
|
|
shaderFlags |= mHasAlpha ? SHADER_RGBA : SHADER_RGB;
|
2013-10-01 14:01:19 -07:00
|
|
|
shaderFlags |= mFilter == GraphicsFilter::FILTER_NEAREST
|
2012-03-01 09:29:30 -08:00
|
|
|
? 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 */
|