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: #include "D3D9SurfaceImage.h" michael@0: #include "gfx2DGlue.h" michael@0: #include "mozilla/layers/TextureD3D9.h" michael@0: #include "mozilla/gfx/Types.h" michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: michael@0: D3D9SurfaceImage::D3D9SurfaceImage() michael@0: : Image(nullptr, ImageFormat::D3D9_RGB32_TEXTURE) michael@0: , mSize(0, 0) michael@0: {} michael@0: michael@0: D3D9SurfaceImage::~D3D9SurfaceImage() {} michael@0: michael@0: HRESULT michael@0: D3D9SurfaceImage::SetData(const Data& aData) michael@0: { michael@0: NS_ENSURE_TRUE(aData.mSurface, E_POINTER); michael@0: HRESULT hr; michael@0: RefPtr surface = aData.mSurface; michael@0: michael@0: RefPtr device; michael@0: hr = surface->GetDevice(byRef(device)); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), E_FAIL); michael@0: michael@0: RefPtr d3d9; michael@0: hr = device->GetDirect3D(byRef(d3d9)); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), E_FAIL); michael@0: michael@0: D3DSURFACE_DESC desc; michael@0: surface->GetDesc(&desc); michael@0: // Ensure we can convert the textures format to RGB conversion michael@0: // in StretchRect. Fail if we can't. michael@0: hr = d3d9->CheckDeviceFormatConversion(D3DADAPTER_DEFAULT, michael@0: D3DDEVTYPE_HAL, michael@0: desc.Format, michael@0: D3DFMT_X8R8G8B8); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), hr); michael@0: michael@0: // DXVA surfaces aren't created sharable, so we need to copy the surface michael@0: // to a sharable texture to that it's accessible to the layer manager's michael@0: // device. michael@0: const nsIntRect& region = aData.mRegion; michael@0: RefPtr texture; michael@0: HANDLE shareHandle = nullptr; michael@0: hr = device->CreateTexture(region.width, michael@0: region.height, michael@0: 1, michael@0: D3DUSAGE_RENDERTARGET, michael@0: D3DFMT_X8R8G8B8, michael@0: D3DPOOL_DEFAULT, michael@0: byRef(texture), michael@0: &shareHandle); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr) && shareHandle, hr); michael@0: michael@0: // Copy the image onto the texture, preforming YUV -> RGB conversion if necessary. michael@0: RefPtr textureSurface; michael@0: hr = texture->GetSurfaceLevel(0, byRef(textureSurface)); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), hr); michael@0: michael@0: // Stash the surface description for later use. michael@0: textureSurface->GetDesc(&mDesc); michael@0: michael@0: RECT src = { region.x, region.y, region.x+region.width, region.y+region.height }; michael@0: hr = device->StretchRect(surface, &src, textureSurface, nullptr, D3DTEXF_NONE); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), hr); michael@0: michael@0: // Flush the draw command now, so that by the time we come to draw this michael@0: // image, we're less likely to need to wait for the draw operation to michael@0: // complete. michael@0: RefPtr query; michael@0: hr = device->CreateQuery(D3DQUERYTYPE_EVENT, byRef(query)); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), hr); michael@0: hr = query->Issue(D3DISSUE_END); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), hr); michael@0: michael@0: mTexture = texture; michael@0: mShareHandle = shareHandle; michael@0: mSize = gfx::IntSize(region.width, region.height); michael@0: mQuery = query; michael@0: michael@0: return S_OK; michael@0: } michael@0: michael@0: void michael@0: D3D9SurfaceImage::EnsureSynchronized() michael@0: { michael@0: if (!mQuery) { michael@0: // Not setup, or already synchronized. michael@0: return; michael@0: } michael@0: int iterations = 0; michael@0: while (iterations < 10 && S_FALSE == mQuery->GetData(nullptr, 0, D3DGETDATA_FLUSH)) { michael@0: Sleep(1); michael@0: iterations++; michael@0: } michael@0: mQuery = nullptr; michael@0: } michael@0: michael@0: HANDLE michael@0: D3D9SurfaceImage::GetShareHandle() michael@0: { michael@0: // Ensure the image has completed its synchronization, michael@0: // and safe to used by the caller on another device. michael@0: EnsureSynchronized(); michael@0: return mShareHandle; michael@0: } michael@0: michael@0: const D3DSURFACE_DESC& michael@0: D3D9SurfaceImage::GetDesc() const michael@0: { michael@0: return mDesc; michael@0: } michael@0: michael@0: gfx::IntSize michael@0: D3D9SurfaceImage::GetSize() michael@0: { michael@0: return mSize; michael@0: } michael@0: michael@0: TextureClient* michael@0: D3D9SurfaceImage::GetTextureClient(CompositableClient* aClient) michael@0: { michael@0: EnsureSynchronized(); michael@0: if (!mTextureClient) { michael@0: RefPtr textureClient = michael@0: new SharedTextureClientD3D9(gfx::SurfaceFormat::B8G8R8X8, TEXTURE_FLAGS_DEFAULT); michael@0: textureClient->InitWith(mTexture, mShareHandle, mDesc); michael@0: mTextureClient = textureClient; michael@0: } michael@0: return mTextureClient; michael@0: } michael@0: michael@0: TemporaryRef michael@0: D3D9SurfaceImage::GetAsSourceSurface() michael@0: { michael@0: NS_ENSURE_TRUE(mTexture, nullptr); michael@0: michael@0: HRESULT hr; michael@0: RefPtr surface = gfx::Factory::CreateDataSourceSurface(mSize, gfx::SurfaceFormat::B8G8R8X8); michael@0: michael@0: if (!surface) { michael@0: NS_WARNING("Failed to created SourceSurface for D3D9SurfaceImage."); michael@0: return nullptr; michael@0: } michael@0: michael@0: // Ensure that the texture is ready to be used. michael@0: EnsureSynchronized(); michael@0: michael@0: // Readback the texture from GPU memory into system memory, so that michael@0: // we can copy it into the Cairo image. This is expensive. michael@0: RefPtr textureSurface; michael@0: hr = mTexture->GetSurfaceLevel(0, byRef(textureSurface)); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr); michael@0: michael@0: RefPtr device; michael@0: hr = mTexture->GetDevice(byRef(device)); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr); michael@0: michael@0: RefPtr systemMemorySurface; michael@0: hr = device->CreateOffscreenPlainSurface(mDesc.Width, michael@0: mDesc.Height, michael@0: D3DFMT_X8R8G8B8, michael@0: D3DPOOL_SYSTEMMEM, michael@0: byRef(systemMemorySurface), michael@0: 0); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr); michael@0: michael@0: hr = device->GetRenderTargetData(textureSurface, systemMemorySurface); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr); michael@0: michael@0: D3DLOCKED_RECT rect; michael@0: hr = systemMemorySurface->LockRect(&rect, nullptr, 0); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr); michael@0: michael@0: gfx::DataSourceSurface::MappedSurface mappedSurface; michael@0: if (!surface->Map(gfx::DataSourceSurface::WRITE, &mappedSurface)) { michael@0: systemMemorySurface->UnlockRect(); michael@0: return nullptr; michael@0: } michael@0: michael@0: const unsigned char* src = (const unsigned char*)(rect.pBits); michael@0: const unsigned srcPitch = rect.Pitch; michael@0: for (int y = 0; y < mSize.height; y++) { michael@0: memcpy(mappedSurface.mData + mappedSurface.mStride * y, michael@0: (unsigned char*)(src) + srcPitch * y, michael@0: mSize.width * 4); michael@0: } michael@0: michael@0: systemMemorySurface->UnlockRect(); michael@0: surface->Unmap(); michael@0: michael@0: return surface; michael@0: } michael@0: michael@0: } /* layers */ michael@0: } /* mozilla */