2010-09-30 22:53:49 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 11:12:37 +00: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 22:53:49 +00:00
|
|
|
|
|
|
|
#include "CanvasLayerD3D10.h"
|
|
|
|
|
2011-08-09 19:38:26 +00:00
|
|
|
#include "../d3d9/Nv3DVUtils.h"
|
2010-09-30 22:53:49 +00:00
|
|
|
#include "gfxWindowsSurface.h"
|
|
|
|
#include "gfxWindowsPlatform.h"
|
2013-02-13 23:26:24 +00:00
|
|
|
#include "SurfaceStream.h"
|
|
|
|
#include "SharedSurfaceANGLE.h"
|
2014-03-11 02:08:49 +00:00
|
|
|
#include "SharedSurfaceGL.h"
|
2013-02-13 23:26:24 +00:00
|
|
|
#include "gfxContext.h"
|
2013-09-04 12:14:52 +00:00
|
|
|
#include "GLContext.h"
|
2014-02-28 17:59:37 +00:00
|
|
|
#include "gfxPrefs.h"
|
2010-09-30 22:53:49 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2014-05-22 10:11:45 +00:00
|
|
|
using namespace mozilla::gl;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2013-09-04 12:14:52 +00:00
|
|
|
CanvasLayerD3D10::CanvasLayerD3D10(LayerManagerD3D10 *aManager)
|
|
|
|
: CanvasLayer(aManager, nullptr)
|
|
|
|
, LayerD3D10(aManager)
|
|
|
|
, mDataIsPremultiplied(false)
|
|
|
|
, mNeedsYFlip(false)
|
|
|
|
, mHasAlpha(true)
|
|
|
|
{
|
|
|
|
mImplData = static_cast<LayerD3D10*>(this);
|
|
|
|
}
|
|
|
|
|
2010-09-30 22:53:49 +00:00
|
|
|
CanvasLayerD3D10::~CanvasLayerD3D10()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasLayerD3D10::Initialize(const Data& aData)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ASSERTION(mSurface == nullptr, "BasicCanvasLayer::Initialize called twice!");
|
2010-09-30 22:53:49 +00:00
|
|
|
|
2014-02-06 14:46:30 +00:00
|
|
|
if (aData.mGLContext) {
|
2010-09-30 22:53:49 +00:00
|
|
|
mGLContext = aData.mGLContext;
|
2013-02-13 23:26:24 +00:00
|
|
|
NS_ASSERTION(mGLContext->IsOffscreen(), "Canvas GLContext must be offscreen.");
|
|
|
|
mDataIsPremultiplied = aData.mIsGLAlphaPremult;
|
2011-10-17 14:59:28 +00:00
|
|
|
mNeedsYFlip = true;
|
2013-02-13 23:26:24 +00:00
|
|
|
|
|
|
|
GLScreenBuffer* screen = mGLContext->Screen();
|
|
|
|
SurfaceStreamType streamType =
|
|
|
|
SurfaceStream::ChooseGLStreamType(SurfaceStream::MainThread,
|
|
|
|
screen->PreserveBuffer());
|
|
|
|
|
|
|
|
SurfaceFactory_GL* factory = nullptr;
|
2014-02-28 17:59:37 +00:00
|
|
|
if (!gfxPrefs::WebGLForceLayersReadback()) {
|
2014-01-07 20:02:18 +00:00
|
|
|
if (mGLContext->IsANGLE()) {
|
|
|
|
factory = SurfaceFactory_ANGLEShareHandle::Create(mGLContext,
|
|
|
|
screen->Caps());
|
|
|
|
}
|
2013-02-13 23:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (factory) {
|
|
|
|
screen->Morph(factory, streamType);
|
|
|
|
}
|
2011-06-24 17:41:18 +00:00
|
|
|
} else if (aData.mDrawTarget) {
|
|
|
|
mDrawTarget = aData.mDrawTarget;
|
2012-08-02 04:01:05 +00:00
|
|
|
mNeedsYFlip = false;
|
|
|
|
mDataIsPremultiplied = true;
|
2014-01-10 19:06:16 +00:00
|
|
|
void *texture = mDrawTarget->GetNativeSurface(NativeSurfaceType::D3D10_TEXTURE);
|
2011-06-24 17:41:18 +00:00
|
|
|
|
2012-08-02 04:01:05 +00:00
|
|
|
if (texture) {
|
|
|
|
mTexture = static_cast<ID3D10Texture2D*>(texture);
|
2011-06-24 17:41:18 +00:00
|
|
|
|
2014-02-06 14:46:30 +00:00
|
|
|
NS_ASSERTION(!aData.mGLContext,
|
|
|
|
"CanvasLayer can't have both DrawTarget and WebGLContext/Surface");
|
2011-06-24 17:41:18 +00:00
|
|
|
|
2012-08-02 04:01:05 +00:00
|
|
|
mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height);
|
2013-07-20 08:48:55 +00:00
|
|
|
device()->CreateShaderResourceView(mTexture, nullptr, getter_AddRefs(mSRView));
|
2012-08-02 04:01:05 +00:00
|
|
|
return;
|
2014-03-11 02:08:49 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 04:01:05 +00:00
|
|
|
// XXX we should store mDrawTarget and use it directly in UpdateSurface,
|
|
|
|
// bypassing Thebes
|
2014-02-12 15:07:46 +00:00
|
|
|
mSurface = mDrawTarget->Snapshot();
|
2010-09-30 22:53:49 +00:00
|
|
|
} else {
|
2011-06-24 17:41:18 +00:00
|
|
|
NS_ERROR("CanvasLayer created without mSurface, mDrawTarget or mGLContext?");
|
2010-09-30 22:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height);
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsD2DTexture = false;
|
2013-02-20 15:01:20 +00:00
|
|
|
|
2013-02-13 23:26:24 +00: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 19:54:25 +00:00
|
|
|
|
2013-07-20 08:48:55 +00:00
|
|
|
HRESULT hr = device()->CreateTexture2D(&desc, nullptr, getter_AddRefs(mTexture));
|
2013-02-13 23:26:24 +00:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
NS_WARNING("Failed to create texture for CanvasLayer!");
|
|
|
|
return;
|
2010-09-30 22:53:49 +00:00
|
|
|
}
|
2011-01-06 22:07:12 +00:00
|
|
|
|
2013-07-20 08:48:55 +00:00
|
|
|
device()->CreateShaderResourceView(mTexture, nullptr, getter_AddRefs(mUploadSRView));
|
2010-09-30 22:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-27 23:59:46 +00:00
|
|
|
CanvasLayerD3D10::UpdateSurface()
|
2010-09-30 22:53:49 +00:00
|
|
|
{
|
2012-09-30 06:20:25 +00:00
|
|
|
if (!IsDirty())
|
2011-03-27 23:59:46 +00:00
|
|
|
return;
|
2012-09-30 06:20:25 +00:00
|
|
|
Painted();
|
2011-03-27 23:59:46 +00:00
|
|
|
|
2011-06-24 17:41:18 +00:00
|
|
|
if (mDrawTarget) {
|
|
|
|
mDrawTarget->Flush();
|
2012-08-02 04:01:05 +00:00
|
|
|
} else if (mIsD2DTexture) {
|
2010-09-30 22:53:49 +00:00
|
|
|
return;
|
2011-01-06 22:07:12 +00:00
|
|
|
}
|
2010-09-30 22:53:49 +00:00
|
|
|
|
2014-04-16 18:05:48 +00:00
|
|
|
if (!mTexture) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-13 23:26:24 +00:00
|
|
|
if (mGLContext) {
|
2014-03-11 02:08:49 +00:00
|
|
|
SharedSurface_GL* surf = mGLContext->RequestFrame();
|
2014-04-16 18:05:48 +00:00
|
|
|
if (!surf) {
|
|
|
|
return;
|
|
|
|
}
|
2013-02-13 23:26:24 +00:00
|
|
|
|
2013-02-13 23:26:24 +00:00
|
|
|
switch (surf->Type()) {
|
|
|
|
case SharedSurfaceType::EGLSurfaceANGLE: {
|
|
|
|
SharedSurface_ANGLEShareHandle* shareSurf = SharedSurface_ANGLEShareHandle::Cast(surf);
|
2014-06-19 00:04:05 +00:00
|
|
|
HANDLE shareHandle = shareSurf->GetShareHandle();
|
|
|
|
|
|
|
|
HRESULT hr = device()->OpenSharedResource(shareHandle,
|
|
|
|
__uuidof(ID3D10Texture2D),
|
|
|
|
getter_AddRefs(mTexture));
|
|
|
|
if (FAILED(hr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
hr = device()->CreateShaderResourceView(mTexture,
|
|
|
|
nullptr,
|
|
|
|
getter_AddRefs(mSRView));
|
|
|
|
if (FAILED(hr))
|
|
|
|
return;
|
2013-02-13 23:26:24 +00:00
|
|
|
|
2013-02-13 23:26:24 +00:00
|
|
|
return;
|
2013-02-21 19:54:25 +00:00
|
|
|
}
|
2013-02-13 23:26:24 +00: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 16:11:58 +00:00
|
|
|
DataSourceSurface* frameData = shareSurf->GetData();
|
2014-01-13 19:06:22 +00:00
|
|
|
// Scope for DrawTarget, so it's destroyed before Unmap.
|
2013-02-13 23:26:24 +00:00
|
|
|
{
|
2014-04-07 14:24:14 +00:00
|
|
|
IntSize boundsSize(mBounds.width, mBounds.height);
|
2014-01-10 19:06:16 +00:00
|
|
|
RefPtr<DrawTarget> mapDt = Factory::CreateDrawTargetForData(BackendType::CAIRO,
|
2013-12-10 16:11:58 +00:00
|
|
|
(uint8_t*)map.pData,
|
2014-04-07 14:24:14 +00:00
|
|
|
boundsSize,
|
2013-12-10 16:11:58 +00:00
|
|
|
map.RowPitch,
|
2014-01-10 19:06:16 +00:00
|
|
|
SurfaceFormat::B8G8R8A8);
|
2013-12-10 16:11:58 +00:00
|
|
|
|
2014-01-13 19:06:22 +00: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 16:11:58 +00:00
|
|
|
mapDt->Flush();
|
2013-02-13 23:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mTexture->Unmap(0);
|
|
|
|
mSRView = mUploadSRView;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2013-06-29 01:38:30 +00:00
|
|
|
MOZ_CRASH("Unhandled SharedSurfaceType.");
|
2010-09-30 22:53:49 +00: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 15:07:46 +00: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 22:53:49 +00:00
|
|
|
|
|
|
|
mTexture->Unmap(0);
|
2013-02-13 23:26:24 +00:00
|
|
|
mSRView = mUploadSRView;
|
2010-09-30 22:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer*
|
|
|
|
CanvasLayerD3D10::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-08 09:06:15 +00:00
|
|
|
CanvasLayerD3D10::RenderLayer()
|
2010-09-30 22:53:49 +00:00
|
|
|
{
|
2013-02-13 23:26:24 +00:00
|
|
|
FirePreTransactionCallback();
|
2011-03-27 23:59:46 +00:00
|
|
|
UpdateSurface();
|
2011-03-27 23:59:46 +00:00
|
|
|
FireDidTransactionCallback();
|
2011-03-27 23:59:46 +00:00
|
|
|
|
2011-03-27 23:59:46 +00:00
|
|
|
if (!mTexture)
|
2010-09-30 22:53:49 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
nsIntRect visibleRect = mVisibleRegion.GetBounds();
|
|
|
|
|
2010-11-08 09:06:15 +00:00
|
|
|
SetEffectTransformAndOpacity();
|
2010-09-30 22:53:49 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t shaderFlags = 0;
|
2012-03-01 17:29:30 +00:00
|
|
|
shaderFlags |= LoadMaskTexture();
|
|
|
|
shaderFlags |= mDataIsPremultiplied
|
|
|
|
? SHADER_PREMUL : SHADER_NON_PREMUL | SHADER_RGBA;
|
|
|
|
shaderFlags |= mHasAlpha ? SHADER_RGBA : SHADER_RGB;
|
2013-10-01 21:01:19 +00:00
|
|
|
shaderFlags |= mFilter == GraphicsFilter::FILTER_NEAREST
|
2012-03-01 17:29:30 +00:00
|
|
|
? SHADER_POINT : SHADER_LINEAR;
|
|
|
|
ID3D10EffectTechnique* technique = SelectShader(shaderFlags);
|
2010-09-30 22:53:49 +00: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 */
|