1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/layers/d3d9/CompositorD3D9.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,765 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "CompositorD3D9.h" 1.10 +#include "LayerManagerD3D9Shaders.h" 1.11 +#include "gfxWindowsPlatform.h" 1.12 +#include "nsIWidget.h" 1.13 +#include "mozilla/layers/ImageHost.h" 1.14 +#include "mozilla/layers/ContentHost.h" 1.15 +#include "mozilla/layers/Effects.h" 1.16 +#include "nsWindowsHelpers.h" 1.17 +#include "Nv3DVUtils.h" 1.18 +#include "gfxFailure.h" 1.19 +#include "mozilla/layers/PCompositorParent.h" 1.20 +#include "mozilla/layers/LayerManagerComposite.h" 1.21 +#include "gfxPrefs.h" 1.22 + 1.23 +using namespace mozilla::gfx; 1.24 + 1.25 +namespace mozilla { 1.26 +namespace layers { 1.27 + 1.28 +CompositorD3D9::CompositorD3D9(PCompositorParent* aParent, nsIWidget *aWidget) 1.29 + : Compositor(aParent) 1.30 + , mWidget(aWidget) 1.31 + , mDeviceResetCount(0) 1.32 +{ 1.33 + Compositor::SetBackend(LayersBackend::LAYERS_D3D9); 1.34 +} 1.35 + 1.36 +CompositorD3D9::~CompositorD3D9() 1.37 +{ 1.38 + mSwapChain = nullptr; 1.39 + mDeviceManager = nullptr; 1.40 +} 1.41 + 1.42 +bool 1.43 +CompositorD3D9::Initialize() 1.44 +{ 1.45 + if (!gfxPlatform::CanUseDirect3D9()) { 1.46 + NS_WARNING("Direct3D 9-accelerated layers are not supported on this system."); 1.47 + return false; 1.48 + } 1.49 + 1.50 + mDeviceManager = gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager(); 1.51 + if (!mDeviceManager) { 1.52 + return false; 1.53 + } 1.54 + 1.55 + mSwapChain = mDeviceManager-> 1.56 + CreateSwapChain((HWND)mWidget->GetNativeData(NS_NATIVE_WINDOW)); 1.57 + 1.58 + if (!mSwapChain) { 1.59 + return false; 1.60 + } 1.61 + 1.62 + return true; 1.63 +} 1.64 + 1.65 +TextureFactoryIdentifier 1.66 +CompositorD3D9::GetTextureFactoryIdentifier() 1.67 +{ 1.68 + TextureFactoryIdentifier ident; 1.69 + ident.mMaxTextureSize = GetMaxTextureSize(); 1.70 + ident.mParentBackend = LayersBackend::LAYERS_D3D9; 1.71 + ident.mParentProcessId = XRE_GetProcessType(); 1.72 + return ident; 1.73 +} 1.74 + 1.75 +bool 1.76 +CompositorD3D9::CanUseCanvasLayerForSize(const IntSize &aSize) 1.77 +{ 1.78 + int32_t maxTextureSize = GetMaxTextureSize(); 1.79 + 1.80 + if (aSize.width > maxTextureSize || aSize.height > maxTextureSize) { 1.81 + return false; 1.82 + } 1.83 + 1.84 + return true; 1.85 +} 1.86 + 1.87 +int32_t 1.88 +CompositorD3D9::GetMaxTextureSize() const 1.89 +{ 1.90 + return mDeviceManager ? mDeviceManager->GetMaxTextureSize() : INT32_MAX; 1.91 +} 1.92 + 1.93 +TemporaryRef<DataTextureSource> 1.94 +CompositorD3D9::CreateDataTextureSource(TextureFlags aFlags) 1.95 +{ 1.96 + return new DataTextureSourceD3D9(SurfaceFormat::UNKNOWN, this, aFlags); 1.97 +} 1.98 + 1.99 +TemporaryRef<CompositingRenderTarget> 1.100 +CompositorD3D9::CreateRenderTarget(const gfx::IntRect &aRect, 1.101 + SurfaceInitMode aInit) 1.102 +{ 1.103 + if (!mDeviceManager) { 1.104 + return nullptr; 1.105 + } 1.106 + 1.107 + RefPtr<IDirect3DTexture9> texture; 1.108 + HRESULT hr = device()->CreateTexture(aRect.width, aRect.height, 1, 1.109 + D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, 1.110 + D3DPOOL_DEFAULT, byRef(texture), 1.111 + nullptr); 1.112 + if (FAILED(hr)) { 1.113 + ReportFailure(NS_LITERAL_CSTRING("CompositorD3D9::CreateRenderTarget: Failed to create texture"), 1.114 + hr); 1.115 + return nullptr; 1.116 + } 1.117 + 1.118 + RefPtr<CompositingRenderTargetD3D9> rt = 1.119 + new CompositingRenderTargetD3D9(texture, aInit, aRect); 1.120 + 1.121 + return rt; 1.122 +} 1.123 + 1.124 +TemporaryRef<CompositingRenderTarget> 1.125 +CompositorD3D9::CreateRenderTargetFromSource(const gfx::IntRect &aRect, 1.126 + const CompositingRenderTarget *aSource, 1.127 + const gfx::IntPoint &aSourcePoint) 1.128 +{ 1.129 + if (!mDeviceManager) { 1.130 + return nullptr; 1.131 + } 1.132 + 1.133 + RefPtr<IDirect3DTexture9> texture; 1.134 + HRESULT hr = device()->CreateTexture(aRect.width, aRect.height, 1, 1.135 + D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, 1.136 + D3DPOOL_DEFAULT, byRef(texture), 1.137 + nullptr); 1.138 + if (FAILED(hr)) { 1.139 + ReportFailure(NS_LITERAL_CSTRING("CompositorD3D9::CreateRenderTargetFromSource: Failed to create texture"), 1.140 + hr); 1.141 + return nullptr; 1.142 + } 1.143 + 1.144 + if (aSource) { 1.145 + nsRefPtr<IDirect3DSurface9> sourceSurface = 1.146 + static_cast<const CompositingRenderTargetD3D9*>(aSource)->GetD3D9Surface(); 1.147 + 1.148 + nsRefPtr<IDirect3DSurface9> destSurface; 1.149 + hr = texture->GetSurfaceLevel(0, getter_AddRefs(destSurface)); 1.150 + if (FAILED(hr)) { 1.151 + NS_WARNING("Failed to get texture surface level for dest."); 1.152 + } 1.153 + 1.154 + if (sourceSurface && destSurface) { 1.155 + RECT sourceRect; 1.156 + sourceRect.left = aSourcePoint.x; 1.157 + sourceRect.right = aSourcePoint.x + aRect.width; 1.158 + sourceRect.top = aSourcePoint.y; 1.159 + sourceRect.bottom = aSourcePoint.y + aRect.height; 1.160 + RECT destRect; 1.161 + destRect.left = 0; 1.162 + destRect.right = aRect.width; 1.163 + destRect.top = 0; 1.164 + destRect.bottom = aRect.height; 1.165 + 1.166 + // copy the source to the dest 1.167 + hr = device()->StretchRect(sourceSurface, 1.168 + &sourceRect, 1.169 + destSurface, 1.170 + &destRect, 1.171 + D3DTEXF_NONE); 1.172 + if (FAILED(hr)) { 1.173 + ReportFailure(NS_LITERAL_CSTRING("CompositorD3D9::CreateRenderTargetFromSource: Failed to update texture"), 1.174 + hr); 1.175 + } 1.176 + } 1.177 + } 1.178 + 1.179 + RefPtr<CompositingRenderTargetD3D9> rt = 1.180 + new CompositingRenderTargetD3D9(texture, 1.181 + INIT_MODE_NONE, 1.182 + aRect); 1.183 + 1.184 + return rt; 1.185 +} 1.186 + 1.187 +void 1.188 +CompositorD3D9::SetRenderTarget(CompositingRenderTarget *aRenderTarget) 1.189 +{ 1.190 + MOZ_ASSERT(aRenderTarget && mDeviceManager); 1.191 + RefPtr<CompositingRenderTargetD3D9> oldRT = mCurrentRT; 1.192 + mCurrentRT = static_cast<CompositingRenderTargetD3D9*>(aRenderTarget); 1.193 + mCurrentRT->BindRenderTarget(device()); 1.194 + PrepareViewport(mCurrentRT->GetSize(), Matrix()); 1.195 +} 1.196 + 1.197 +static DeviceManagerD3D9::ShaderMode 1.198 +ShaderModeForEffectType(EffectTypes aEffectType, gfx::SurfaceFormat aFormat) 1.199 +{ 1.200 + switch (aEffectType) { 1.201 + case EFFECT_SOLID_COLOR: 1.202 + return DeviceManagerD3D9::SOLIDCOLORLAYER; 1.203 + case EFFECT_RENDER_TARGET: 1.204 + return DeviceManagerD3D9::RGBALAYER; 1.205 + case EFFECT_RGB: 1.206 + if (aFormat == SurfaceFormat::B8G8R8A8 || aFormat == SurfaceFormat::R8G8B8A8) 1.207 + return DeviceManagerD3D9::RGBALAYER; 1.208 + return DeviceManagerD3D9::RGBLAYER; 1.209 + case EFFECT_YCBCR: 1.210 + return DeviceManagerD3D9::YCBCRLAYER; 1.211 + } 1.212 + 1.213 + MOZ_CRASH("Bad effect type"); 1.214 +} 1.215 + 1.216 +void 1.217 +CompositorD3D9::ClearRect(const gfx::Rect& aRect) 1.218 +{ 1.219 + D3DRECT rect; 1.220 + rect.x1 = aRect.X(); 1.221 + rect.y1 = aRect.Y(); 1.222 + rect.x2 = aRect.XMost(); 1.223 + rect.y2 = aRect.YMost(); 1.224 + 1.225 + device()->Clear(1, &rect, D3DCLEAR_TARGET, 1.226 + 0x00000000, 0, 0); 1.227 +} 1.228 + 1.229 +void 1.230 +CompositorD3D9::DrawQuad(const gfx::Rect &aRect, 1.231 + const gfx::Rect &aClipRect, 1.232 + const EffectChain &aEffectChain, 1.233 + gfx::Float aOpacity, 1.234 + const gfx::Matrix4x4 &aTransform) 1.235 +{ 1.236 + if (!mDeviceManager) { 1.237 + return; 1.238 + } 1.239 + 1.240 + IDirect3DDevice9* d3d9Device = device(); 1.241 + MOZ_ASSERT(d3d9Device, "We should be able to get a device now"); 1.242 + 1.243 + MOZ_ASSERT(mCurrentRT, "No render target"); 1.244 + d3d9Device->SetVertexShaderConstantF(CBmLayerTransform, &aTransform._11, 4); 1.245 + 1.246 + IntPoint origin = mCurrentRT->GetOrigin(); 1.247 + float renderTargetOffset[] = { origin.x, origin.y, 0, 0 }; 1.248 + d3d9Device->SetVertexShaderConstantF(CBvRenderTargetOffset, 1.249 + renderTargetOffset, 1.250 + 1); 1.251 + d3d9Device->SetVertexShaderConstantF(CBvLayerQuad, 1.252 + ShaderConstantRect(aRect.x, 1.253 + aRect.y, 1.254 + aRect.width, 1.255 + aRect.height), 1.256 + 1); 1.257 + bool target = false; 1.258 + 1.259 + if (aEffectChain.mPrimaryEffect->mType != EFFECT_SOLID_COLOR) { 1.260 + float opacity[4]; 1.261 + /* 1.262 + * We always upload a 4 component float, but the shader will use only the 1.263 + * first component since it's declared as a 'float'. 1.264 + */ 1.265 + opacity[0] = aOpacity; 1.266 + d3d9Device->SetPixelShaderConstantF(CBfLayerOpacity, opacity, 1); 1.267 + } 1.268 + 1.269 + bool isPremultiplied = true; 1.270 + 1.271 + MaskType maskType = MaskNone; 1.272 + 1.273 + if (aEffectChain.mSecondaryEffects[EFFECT_MASK]) { 1.274 + if (aTransform.Is2D()) { 1.275 + maskType = Mask2d; 1.276 + } else { 1.277 + maskType = Mask3d; 1.278 + } 1.279 + } 1.280 + 1.281 + RECT scissor; 1.282 + scissor.left = aClipRect.x; 1.283 + scissor.right = aClipRect.XMost(); 1.284 + scissor.top = aClipRect.y; 1.285 + scissor.bottom = aClipRect.YMost(); 1.286 + d3d9Device->SetScissorRect(&scissor); 1.287 + 1.288 + uint32_t maskTexture = 0; 1.289 + switch (aEffectChain.mPrimaryEffect->mType) { 1.290 + case EFFECT_SOLID_COLOR: 1.291 + { 1.292 + // output color is premultiplied, so we need to adjust all channels. 1.293 + Color layerColor = 1.294 + static_cast<EffectSolidColor*>(aEffectChain.mPrimaryEffect.get())->mColor; 1.295 + float color[4]; 1.296 + color[0] = layerColor.r * layerColor.a * aOpacity; 1.297 + color[1] = layerColor.g * layerColor.a * aOpacity; 1.298 + color[2] = layerColor.b * layerColor.a * aOpacity; 1.299 + color[3] = layerColor.a * aOpacity; 1.300 + 1.301 + d3d9Device->SetPixelShaderConstantF(CBvColor, color, 1); 1.302 + 1.303 + maskTexture = mDeviceManager 1.304 + ->SetShaderMode(DeviceManagerD3D9::SOLIDCOLORLAYER, maskType); 1.305 + } 1.306 + break; 1.307 + case EFFECT_RENDER_TARGET: 1.308 + case EFFECT_RGB: 1.309 + { 1.310 + TexturedEffect* texturedEffect = 1.311 + static_cast<TexturedEffect*>(aEffectChain.mPrimaryEffect.get()); 1.312 + 1.313 + Rect textureCoords = texturedEffect->mTextureCoords; 1.314 + d3d9Device->SetVertexShaderConstantF(CBvTextureCoords, 1.315 + ShaderConstantRect( 1.316 + textureCoords.x, 1.317 + textureCoords.y, 1.318 + textureCoords.width, 1.319 + textureCoords.height), 1.320 + 1); 1.321 + 1.322 + SetSamplerForFilter(texturedEffect->mFilter); 1.323 + 1.324 + TextureSourceD3D9* source = texturedEffect->mTexture->AsSourceD3D9(); 1.325 + d3d9Device->SetTexture(0, source->GetD3D9Texture()); 1.326 + 1.327 + maskTexture = mDeviceManager 1.328 + ->SetShaderMode(ShaderModeForEffectType(aEffectChain.mPrimaryEffect->mType, 1.329 + texturedEffect->mTexture->GetFormat()), 1.330 + maskType); 1.331 + 1.332 + isPremultiplied = texturedEffect->mPremultiplied; 1.333 + } 1.334 + break; 1.335 + case EFFECT_YCBCR: 1.336 + { 1.337 + EffectYCbCr* ycbcrEffect = 1.338 + static_cast<EffectYCbCr*>(aEffectChain.mPrimaryEffect.get()); 1.339 + 1.340 + SetSamplerForFilter(Filter::LINEAR); 1.341 + 1.342 + Rect textureCoords = ycbcrEffect->mTextureCoords; 1.343 + d3d9Device->SetVertexShaderConstantF(CBvTextureCoords, 1.344 + ShaderConstantRect( 1.345 + textureCoords.x, 1.346 + textureCoords.y, 1.347 + textureCoords.width, 1.348 + textureCoords.height), 1.349 + 1); 1.350 + 1.351 + const int Y = 0, Cb = 1, Cr = 2; 1.352 + TextureSource* source = ycbcrEffect->mTexture; 1.353 + 1.354 + if (!source) { 1.355 + NS_WARNING("No texture to composite"); 1.356 + return; 1.357 + } 1.358 + 1.359 + if (!source->GetSubSource(Y) || !source->GetSubSource(Cb) || !source->GetSubSource(Cr)) { 1.360 + // This can happen if we failed to upload the textures, most likely 1.361 + // because of unsupported dimensions (we don't tile YCbCr textures). 1.362 + return; 1.363 + } 1.364 + 1.365 + TextureSourceD3D9* sourceY = source->GetSubSource(Y)->AsSourceD3D9(); 1.366 + TextureSourceD3D9* sourceCb = source->GetSubSource(Cb)->AsSourceD3D9(); 1.367 + TextureSourceD3D9* sourceCr = source->GetSubSource(Cr)->AsSourceD3D9(); 1.368 + 1.369 + 1.370 + MOZ_ASSERT(sourceY->GetD3D9Texture()); 1.371 + MOZ_ASSERT(sourceCb->GetD3D9Texture()); 1.372 + MOZ_ASSERT(sourceCr->GetD3D9Texture()); 1.373 + 1.374 + /* 1.375 + * Send 3d control data and metadata 1.376 + */ 1.377 + if (mDeviceManager->GetNv3DVUtils()) { 1.378 + Nv_Stereo_Mode mode; 1.379 + switch (source->AsSourceD3D9()->GetStereoMode()) { 1.380 + case StereoMode::LEFT_RIGHT: 1.381 + mode = NV_STEREO_MODE_LEFT_RIGHT; 1.382 + break; 1.383 + case StereoMode::RIGHT_LEFT: 1.384 + mode = NV_STEREO_MODE_RIGHT_LEFT; 1.385 + break; 1.386 + case StereoMode::BOTTOM_TOP: 1.387 + mode = NV_STEREO_MODE_BOTTOM_TOP; 1.388 + break; 1.389 + case StereoMode::TOP_BOTTOM: 1.390 + mode = NV_STEREO_MODE_TOP_BOTTOM; 1.391 + break; 1.392 + case StereoMode::MONO: 1.393 + mode = NV_STEREO_MODE_MONO; 1.394 + break; 1.395 + } 1.396 + 1.397 + // Send control data even in mono case so driver knows to leave stereo mode. 1.398 + mDeviceManager->GetNv3DVUtils()->SendNv3DVControl(mode, true, FIREFOX_3DV_APP_HANDLE); 1.399 + 1.400 + if (source->AsSourceD3D9()->GetStereoMode() != StereoMode::MONO) { 1.401 + mDeviceManager->GetNv3DVUtils()->SendNv3DVControl(mode, true, FIREFOX_3DV_APP_HANDLE); 1.402 + 1.403 + nsRefPtr<IDirect3DSurface9> renderTarget; 1.404 + d3d9Device->GetRenderTarget(0, getter_AddRefs(renderTarget)); 1.405 + mDeviceManager->GetNv3DVUtils()->SendNv3DVMetaData((unsigned int)aRect.width, 1.406 + (unsigned int)aRect.height, 1.407 + (HANDLE)(sourceY->GetD3D9Texture()), 1.408 + (HANDLE)(renderTarget)); 1.409 + } 1.410 + } 1.411 + 1.412 + // Linear scaling is default here, adhering to mFilter is difficult since 1.413 + // presumably even with point filtering we'll still want chroma upsampling 1.414 + // to be linear. In the current approach we can't. 1.415 + device()->SetTexture(Y, sourceY->GetD3D9Texture()); 1.416 + device()->SetTexture(Cb, sourceCb->GetD3D9Texture()); 1.417 + device()->SetTexture(Cr, sourceCr->GetD3D9Texture()); 1.418 + maskTexture = mDeviceManager->SetShaderMode(DeviceManagerD3D9::YCBCRLAYER, maskType); 1.419 + } 1.420 + break; 1.421 + case EFFECT_COMPONENT_ALPHA: 1.422 + { 1.423 + MOZ_ASSERT(gfxPrefs::ComponentAlphaEnabled()); 1.424 + EffectComponentAlpha* effectComponentAlpha = 1.425 + static_cast<EffectComponentAlpha*>(aEffectChain.mPrimaryEffect.get()); 1.426 + TextureSourceD3D9* sourceOnWhite = effectComponentAlpha->mOnWhite->AsSourceD3D9(); 1.427 + TextureSourceD3D9* sourceOnBlack = effectComponentAlpha->mOnBlack->AsSourceD3D9(); 1.428 + 1.429 + Rect textureCoords = effectComponentAlpha->mTextureCoords; 1.430 + d3d9Device->SetVertexShaderConstantF(CBvTextureCoords, 1.431 + ShaderConstantRect( 1.432 + textureCoords.x, 1.433 + textureCoords.y, 1.434 + textureCoords.width, 1.435 + textureCoords.height), 1.436 + 1); 1.437 + 1.438 + SetSamplerForFilter(effectComponentAlpha->mFilter); 1.439 + 1.440 + maskTexture = mDeviceManager->SetShaderMode(DeviceManagerD3D9::COMPONENTLAYERPASS1, maskType); 1.441 + SetMask(aEffectChain, maskTexture); 1.442 + d3d9Device->SetTexture(0, sourceOnBlack->GetD3D9Texture()); 1.443 + d3d9Device->SetTexture(1, sourceOnWhite->GetD3D9Texture()); 1.444 + d3d9Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); 1.445 + d3d9Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR); 1.446 + d3d9Device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); 1.447 + 1.448 + maskTexture = mDeviceManager->SetShaderMode(DeviceManagerD3D9::COMPONENTLAYERPASS2, maskType); 1.449 + SetMask(aEffectChain, maskTexture); 1.450 + d3d9Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); 1.451 + d3d9Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); 1.452 + d3d9Device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); 1.453 + 1.454 + // Restore defaults 1.455 + d3d9Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); 1.456 + d3d9Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); 1.457 + d3d9Device->SetTexture(1, nullptr); 1.458 + } 1.459 + return; 1.460 + default: 1.461 + NS_WARNING("Unknown shader type"); 1.462 + return; 1.463 + } 1.464 + 1.465 + SetMask(aEffectChain, maskTexture); 1.466 + 1.467 + if (!isPremultiplied) { 1.468 + d3d9Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); 1.469 + } 1.470 + 1.471 + HRESULT hr = d3d9Device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); 1.472 + 1.473 + if (!isPremultiplied) { 1.474 + d3d9Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); 1.475 + } 1.476 +} 1.477 + 1.478 +void 1.479 +CompositorD3D9::SetMask(const EffectChain &aEffectChain, uint32_t aMaskTexture) 1.480 +{ 1.481 + EffectMask *maskEffect = 1.482 + static_cast<EffectMask*>(aEffectChain.mSecondaryEffects[EFFECT_MASK].get()); 1.483 + if (!maskEffect) { 1.484 + return; 1.485 + } 1.486 + 1.487 + TextureSourceD3D9 *source = maskEffect->mMaskTexture->AsSourceD3D9(); 1.488 + 1.489 + MOZ_ASSERT(aMaskTexture >= 0); 1.490 + device()->SetTexture(aMaskTexture, source->GetD3D9Texture()); 1.491 + 1.492 + const gfx::Matrix4x4& maskTransform = maskEffect->mMaskTransform; 1.493 + NS_ASSERTION(maskTransform.Is2D(), "How did we end up with a 3D transform here?!"); 1.494 + Rect bounds = Rect(Point(), Size(maskEffect->mSize)); 1.495 + bounds = maskTransform.As2D().TransformBounds(bounds); 1.496 + 1.497 + device()->SetVertexShaderConstantF(DeviceManagerD3D9::sMaskQuadRegister, 1.498 + ShaderConstantRect(bounds.x, 1.499 + bounds.y, 1.500 + bounds.width, 1.501 + bounds.height), 1.502 + 1); 1.503 +} 1.504 + 1.505 +/** 1.506 + * In the next few methods we call |mParent->SendInvalidateAll()| - that has 1.507 + * a few uses - if our device or swap chain is not ready, it causes us to try 1.508 + * to render again, that means we keep trying to get a good device and swap 1.509 + * chain and don't block the main thread (which we would if we kept trying in 1.510 + * a busy loop because this is likely to happen in a sync transaction). 1.511 + * If we had to recreate our device, then we have new textures and we 1.512 + * need to reupload everything (not just what is currently invalid) from the 1.513 + * client side. That means we need to invalidate everything on the client. 1.514 + * If we just reset and didn't need to recreate, then we don't need to reupload 1.515 + * our textures, but we do need to redraw the whole window, which means we still 1.516 + * need to invalidate everything. 1.517 + * Currently we probably do this complete invalidation too much. But it is better 1.518 + * to do that than to miss an invalidation which would result in a black layer 1.519 + * (or multiple layers) until the user moves the mouse. The unnecessary invalidtion 1.520 + * only happens when the device is reset, so that should be pretty rare and when 1.521 + * other things are happening so the user does not expect super performance. 1.522 + */ 1.523 + 1.524 +bool 1.525 +CompositorD3D9::EnsureSwapChain() 1.526 +{ 1.527 + MOZ_ASSERT(mDeviceManager, "Don't call EnsureSwapChain without a device manager"); 1.528 + 1.529 + if (!mSwapChain) { 1.530 + mSwapChain = mDeviceManager-> 1.531 + CreateSwapChain((HWND)mWidget->GetNativeData(NS_NATIVE_WINDOW)); 1.532 + // We could not create a swap chain, return false 1.533 + if (!mSwapChain) { 1.534 + // Check the state of the device too 1.535 + DeviceManagerState state = mDeviceManager->VerifyReadyForRendering(); 1.536 + if (state == DeviceMustRecreate) { 1.537 + mDeviceManager = nullptr; 1.538 + } 1.539 + mParent->SendInvalidateAll(); 1.540 + return false; 1.541 + } 1.542 + } 1.543 + 1.544 + // We have a swap chain, lets initialise it 1.545 + DeviceManagerState state = mSwapChain->PrepareForRendering(); 1.546 + if (state == DeviceOK) { 1.547 + return true; 1.548 + } 1.549 + // Swap chain could not be initialised, handle the failure 1.550 + if (state == DeviceMustRecreate) { 1.551 + mDeviceManager = nullptr; 1.552 + mSwapChain = nullptr; 1.553 + } 1.554 + mParent->SendInvalidateAll(); 1.555 + return false; 1.556 +} 1.557 + 1.558 +void 1.559 +CompositorD3D9::CheckResetCount() 1.560 +{ 1.561 + if (mDeviceResetCount != mDeviceManager->GetDeviceResetCount()) { 1.562 + mParent->SendInvalidateAll(); 1.563 + } 1.564 + mDeviceResetCount = mDeviceManager->GetDeviceResetCount(); 1.565 +} 1.566 + 1.567 +bool 1.568 +CompositorD3D9::Ready() 1.569 +{ 1.570 + if (mDeviceManager) { 1.571 + if (EnsureSwapChain()) { 1.572 + // We don't need to call VerifyReadyForRendering because that is 1.573 + // called by mSwapChain->PrepareForRendering() via EnsureSwapChain(). 1.574 + 1.575 + CheckResetCount(); 1.576 + return true; 1.577 + } 1.578 + return false; 1.579 + } 1.580 + 1.581 + NS_ASSERTION(!mCurrentRT && !mDefaultRT, 1.582 + "Shouldn't have any render targets around, they must be released before our device"); 1.583 + mSwapChain = nullptr; 1.584 + 1.585 + mDeviceManager = gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager(); 1.586 + if (!mDeviceManager) { 1.587 + mParent->SendInvalidateAll(); 1.588 + return false; 1.589 + } 1.590 + if (EnsureSwapChain()) { 1.591 + CheckResetCount(); 1.592 + return true; 1.593 + } 1.594 + return false; 1.595 +} 1.596 + 1.597 +static void 1.598 +CancelCompositing(Rect* aRenderBoundsOut) 1.599 +{ 1.600 + if (aRenderBoundsOut) { 1.601 + *aRenderBoundsOut = Rect(0, 0, 0, 0); 1.602 + } 1.603 +} 1.604 + 1.605 +void 1.606 +CompositorD3D9::BeginFrame(const nsIntRegion& aInvalidRegion, 1.607 + const Rect *aClipRectIn, 1.608 + const gfx::Matrix& aTransform, 1.609 + const Rect& aRenderBounds, 1.610 + Rect *aClipRectOut, 1.611 + Rect *aRenderBoundsOut) 1.612 +{ 1.613 + MOZ_ASSERT(mDeviceManager && mSwapChain); 1.614 + 1.615 + mDeviceManager->SetupRenderState(); 1.616 + 1.617 + EnsureSize(); 1.618 + 1.619 + device()->Clear(0, nullptr, D3DCLEAR_TARGET, 0x00000000, 0, 0); 1.620 + device()->BeginScene(); 1.621 + 1.622 + if (aClipRectOut) { 1.623 + *aClipRectOut = Rect(0, 0, mSize.width, mSize.height); 1.624 + } 1.625 + if (aRenderBoundsOut) { 1.626 + *aRenderBoundsOut = Rect(0, 0, mSize.width, mSize.height); 1.627 + } 1.628 + 1.629 + RECT r; 1.630 + if (aClipRectIn) { 1.631 + r.left = (LONG)aClipRectIn->x; 1.632 + r.top = (LONG)aClipRectIn->y; 1.633 + r.right = (LONG)(aClipRectIn->x + aClipRectIn->width); 1.634 + r.bottom = (LONG)(aClipRectIn->y + aClipRectIn->height); 1.635 + } else { 1.636 + r.left = r.top = 0; 1.637 + r.right = mSize.width; 1.638 + r.bottom = mSize.height; 1.639 + } 1.640 + device()->SetScissorRect(&r); 1.641 + 1.642 + nsRefPtr<IDirect3DSurface9> backBuffer = mSwapChain->GetBackBuffer(); 1.643 + mDefaultRT = new CompositingRenderTargetD3D9(backBuffer, 1.644 + INIT_MODE_CLEAR, 1.645 + IntRect(0, 0, mSize.width, mSize.height)); 1.646 + SetRenderTarget(mDefaultRT); 1.647 +} 1.648 + 1.649 +void 1.650 +CompositorD3D9::EndFrame() 1.651 +{ 1.652 + if (mDeviceManager) { 1.653 + device()->EndScene(); 1.654 + 1.655 + nsIntSize oldSize = mSize; 1.656 + EnsureSize(); 1.657 + if (oldSize == mSize) { 1.658 + if (mTarget) { 1.659 + PaintToTarget(); 1.660 + } else { 1.661 + mSwapChain->Present(); 1.662 + } 1.663 + } 1.664 + } 1.665 + 1.666 + mCurrentRT = nullptr; 1.667 + mDefaultRT = nullptr; 1.668 +} 1.669 + 1.670 +void 1.671 +CompositorD3D9::PrepareViewport(const gfx::IntSize& aSize, 1.672 + const Matrix &aWorldTransform) 1.673 +{ 1.674 + Matrix4x4 viewMatrix; 1.675 + /* 1.676 + * Matrix to transform to viewport space ( <-1.0, 1.0> topleft, 1.677 + * <1.0, -1.0> bottomright) 1.678 + */ 1.679 + viewMatrix._11 = 2.0f / aSize.width; 1.680 + viewMatrix._22 = -2.0f / aSize.height; 1.681 + viewMatrix._41 = -1.0f; 1.682 + viewMatrix._42 = 1.0f; 1.683 + 1.684 + viewMatrix = Matrix4x4::From2D(aWorldTransform) * viewMatrix; 1.685 + 1.686 + HRESULT hr = device()->SetVertexShaderConstantF(CBmProjection, &viewMatrix._11, 4); 1.687 + 1.688 + if (FAILED(hr)) { 1.689 + NS_WARNING("Failed to set projection matrix"); 1.690 + } 1.691 +} 1.692 + 1.693 +void 1.694 +CompositorD3D9::EnsureSize() 1.695 +{ 1.696 + nsIntRect rect; 1.697 + mWidget->GetClientBounds(rect); 1.698 + 1.699 + mSize = rect.Size(); 1.700 +} 1.701 + 1.702 +void 1.703 +CompositorD3D9::SetSamplerForFilter(Filter aFilter) 1.704 +{ 1.705 + switch (aFilter) { 1.706 + case Filter::LINEAR: 1.707 + device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); 1.708 + device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); 1.709 + return; 1.710 + case Filter::POINT: 1.711 + device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); 1.712 + device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); 1.713 + return; 1.714 + default: 1.715 + device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); 1.716 + device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); 1.717 + } 1.718 +} 1.719 + 1.720 +void 1.721 +CompositorD3D9::PaintToTarget() 1.722 +{ 1.723 + if (!mDeviceManager) { 1.724 + return; 1.725 + } 1.726 + 1.727 + nsRefPtr<IDirect3DSurface9> backBuff; 1.728 + nsRefPtr<IDirect3DSurface9> destSurf; 1.729 + device()->GetRenderTarget(0, getter_AddRefs(backBuff)); 1.730 + 1.731 + D3DSURFACE_DESC desc; 1.732 + backBuff->GetDesc(&desc); 1.733 + 1.734 + device()->CreateOffscreenPlainSurface(desc.Width, desc.Height, 1.735 + D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, 1.736 + getter_AddRefs(destSurf), nullptr); 1.737 + 1.738 + device()->GetRenderTargetData(backBuff, destSurf); 1.739 + 1.740 + D3DLOCKED_RECT rect; 1.741 + destSurf->LockRect(&rect, nullptr, D3DLOCK_READONLY); 1.742 + RefPtr<DataSourceSurface> sourceSurface = 1.743 + Factory::CreateWrappingDataSourceSurface((uint8_t*)rect.pBits, 1.744 + rect.Pitch, 1.745 + IntSize(desc.Width, desc.Height), 1.746 + SurfaceFormat::B8G8R8A8); 1.747 + mTarget->CopySurface(sourceSurface, 1.748 + IntRect(0, 0, desc.Width, desc.Height), 1.749 + IntPoint()); 1.750 + mTarget->Flush(); 1.751 + destSurf->UnlockRect(); 1.752 +} 1.753 + 1.754 +void 1.755 +CompositorD3D9::ReportFailure(const nsACString &aMsg, HRESULT aCode) 1.756 +{ 1.757 + // We could choose to abort here when hr == E_OUTOFMEMORY. 1.758 + nsCString msg; 1.759 + msg.Append(aMsg); 1.760 + msg.AppendLiteral(" Error code: "); 1.761 + msg.AppendInt(uint32_t(aCode)); 1.762 + NS_WARNING(msg.BeginReading()); 1.763 + 1.764 + gfx::LogFailure(msg); 1.765 +} 1.766 + 1.767 +} 1.768 +}