michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #include "mozilla/layers/PLayerTransaction.h" michael@0: michael@0: #include "gfxWindowsSurface.h" michael@0: #include "gfxWindowsPlatform.h" michael@0: #include "SurfaceStream.h" michael@0: #include "SharedSurfaceGL.h" michael@0: #include "GLContext.h" michael@0: #include "CanvasLayerD3D9.h" michael@0: michael@0: using namespace mozilla::gfx; michael@0: using namespace mozilla::gl; michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: CanvasLayerD3D9::CanvasLayerD3D9(LayerManagerD3D9 *aManager) michael@0: : CanvasLayer(aManager, nullptr) michael@0: , LayerD3D9(aManager) michael@0: , mDataIsPremultiplied(false) michael@0: , mNeedsYFlip(false) michael@0: , mHasAlpha(true) michael@0: { michael@0: mImplData = static_cast(this); michael@0: aManager->deviceManager()->mLayersWithResources.AppendElement(this); michael@0: } michael@0: michael@0: CanvasLayerD3D9::~CanvasLayerD3D9() michael@0: { michael@0: if (mD3DManager) { michael@0: mD3DManager->deviceManager()->mLayersWithResources.RemoveElement(this); michael@0: } michael@0: } michael@0: michael@0: void michael@0: CanvasLayerD3D9::Initialize(const Data& aData) michael@0: { michael@0: NS_ASSERTION(mDrawTarget == nullptr, "BasicCanvasLayer::Initialize called twice!"); michael@0: michael@0: if (aData.mDrawTarget) { michael@0: mDrawTarget = aData.mDrawTarget; michael@0: mNeedsYFlip = false; michael@0: mDataIsPremultiplied = true; michael@0: } else if (aData.mGLContext) { michael@0: mGLContext = aData.mGLContext; michael@0: NS_ASSERTION(mGLContext->IsOffscreen(), "Canvas GLContext must be offscreen."); michael@0: mDataIsPremultiplied = aData.mIsGLAlphaPremult; michael@0: mNeedsYFlip = true; michael@0: } else { michael@0: NS_ERROR("CanvasLayer created without mGLContext or mDrawTarget?"); michael@0: } michael@0: michael@0: mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height); michael@0: michael@0: CreateTexture(); michael@0: } michael@0: michael@0: void michael@0: CanvasLayerD3D9::UpdateSurface() michael@0: { michael@0: if (!IsDirty() && mTexture) michael@0: return; michael@0: Painted(); michael@0: michael@0: if (!mTexture) { michael@0: CreateTexture(); michael@0: michael@0: if (!mTexture) { michael@0: NS_WARNING("CanvasLayerD3D9::Updated called but no texture present and creation failed!"); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: RefPtr surface; michael@0: michael@0: if (mGLContext) { michael@0: SharedSurface_GL* surf = mGLContext->RequestFrame(); michael@0: if (!surf) michael@0: return; michael@0: michael@0: SharedSurface_Basic* shareSurf = SharedSurface_Basic::Cast(surf); michael@0: surface = shareSurf->GetData(); michael@0: } else { michael@0: surface = mDrawTarget->Snapshot(); michael@0: } michael@0: michael@0: // WebGL reads entire surface. michael@0: LockTextureRectD3D9 textureLock(mTexture); michael@0: if (!textureLock.HasLock()) { michael@0: NS_WARNING("Failed to lock CanvasLayer texture."); michael@0: return; michael@0: } michael@0: michael@0: D3DLOCKED_RECT rect = textureLock.GetLockRect(); michael@0: IntSize boundsSize(mBounds.width, mBounds.height); michael@0: RefPtr rectDt = Factory::CreateDrawTargetForData(BackendType::CAIRO, michael@0: (uint8_t*)rect.pBits, michael@0: boundsSize, michael@0: rect.Pitch, michael@0: SurfaceFormat::B8G8R8A8); michael@0: michael@0: Rect drawRect(0, 0, surface->GetSize().width, surface->GetSize().height); michael@0: rectDt->DrawSurface(surface, drawRect, drawRect, michael@0: DrawSurfaceOptions(), DrawOptions(1.0F, CompositionOp::OP_SOURCE)); michael@0: rectDt->Flush(); michael@0: } michael@0: michael@0: Layer* michael@0: CanvasLayerD3D9::GetLayer() michael@0: { michael@0: return this; michael@0: } michael@0: michael@0: void michael@0: CanvasLayerD3D9::RenderLayer() michael@0: { michael@0: FirePreTransactionCallback(); michael@0: UpdateSurface(); michael@0: if (mD3DManager->CompositingDisabled()) { michael@0: return; michael@0: } michael@0: FireDidTransactionCallback(); michael@0: michael@0: if (!mTexture) michael@0: return; michael@0: michael@0: /* michael@0: * We flip the Y axis here, note we can only do this because we are in michael@0: * CULL_NONE mode! michael@0: */ michael@0: michael@0: ShaderConstantRect quad(0, 0, mBounds.width, mBounds.height); michael@0: if (mNeedsYFlip) { michael@0: quad.mHeight = (float)-mBounds.height; michael@0: quad.mY = (float)mBounds.height; michael@0: } michael@0: michael@0: device()->SetVertexShaderConstantF(CBvLayerQuad, quad, 1); michael@0: michael@0: SetShaderTransformAndOpacity(); michael@0: michael@0: if (mHasAlpha) { michael@0: mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER, GetMaskLayer()); michael@0: } else { michael@0: mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBLAYER, GetMaskLayer()); michael@0: } michael@0: michael@0: if (mFilter == GraphicsFilter::FILTER_NEAREST) { michael@0: device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); michael@0: device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); michael@0: } michael@0: if (!mDataIsPremultiplied) { michael@0: device()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); michael@0: device()->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); michael@0: } michael@0: device()->SetTexture(0, mTexture); michael@0: device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); michael@0: if (!mDataIsPremultiplied) { michael@0: device()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); michael@0: device()->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); michael@0: } michael@0: if (mFilter == GraphicsFilter::FILTER_NEAREST) { michael@0: device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); michael@0: device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); michael@0: } michael@0: } michael@0: michael@0: void michael@0: CanvasLayerD3D9::CleanResources() michael@0: { michael@0: if (mD3DManager->deviceManager()->HasDynamicTextures()) { michael@0: // In this case we have a texture in POOL_DEFAULT michael@0: mTexture = nullptr; michael@0: } michael@0: } michael@0: michael@0: void michael@0: CanvasLayerD3D9::LayerManagerDestroyed() michael@0: { michael@0: mD3DManager->deviceManager()->mLayersWithResources.RemoveElement(this); michael@0: mD3DManager = nullptr; michael@0: } michael@0: michael@0: void michael@0: CanvasLayerD3D9::CreateTexture() michael@0: { michael@0: HRESULT hr; michael@0: if (mD3DManager->deviceManager()->HasDynamicTextures()) { michael@0: hr = device()->CreateTexture(mBounds.width, mBounds.height, 1, D3DUSAGE_DYNAMIC, michael@0: D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, michael@0: getter_AddRefs(mTexture), nullptr); michael@0: } else { michael@0: // D3DPOOL_MANAGED is fine here since we require Dynamic Textures for D3D9Ex michael@0: // devices. michael@0: hr = device()->CreateTexture(mBounds.width, mBounds.height, 1, 0, michael@0: D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, michael@0: getter_AddRefs(mTexture), nullptr); michael@0: } michael@0: if (FAILED(hr)) { michael@0: mD3DManager->ReportFailure(NS_LITERAL_CSTRING("CanvasLayerD3D9::CreateTexture() failed"), michael@0: hr); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: } /* namespace layers */ michael@0: } /* namespace mozilla */