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 "ImageLayerD3D10.h" michael@0: #include "gfxD2DSurface.h" michael@0: #include "gfxWindowsSurface.h" michael@0: #include "yuv_convert.h" michael@0: #include "../d3d9/Nv3DVUtils.h" michael@0: #include "D3D9SurfaceImage.h" michael@0: #include "mozilla/gfx/Point.h" michael@0: #include "gfx2DGlue.h" michael@0: michael@0: #include "gfxWindowsPlatform.h" michael@0: michael@0: using namespace mozilla::gfx; michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: static already_AddRefed michael@0: DataToTexture(ID3D10Device *aDevice, michael@0: unsigned char *data, michael@0: int stride, michael@0: const IntSize &aSize) michael@0: { michael@0: D3D10_SUBRESOURCE_DATA srdata; michael@0: michael@0: CD3D10_TEXTURE2D_DESC desc(DXGI_FORMAT_B8G8R8A8_UNORM, michael@0: aSize.width, michael@0: aSize.height, michael@0: 1, 1); michael@0: desc.Usage = D3D10_USAGE_IMMUTABLE; michael@0: michael@0: srdata.pSysMem = data; michael@0: srdata.SysMemPitch = stride; michael@0: michael@0: nsRefPtr texture; michael@0: HRESULT hr = aDevice->CreateTexture2D(&desc, &srdata, getter_AddRefs(texture)); michael@0: michael@0: if (FAILED(hr)) { michael@0: LayerManagerD3D10::ReportFailure(NS_LITERAL_CSTRING("Failed to create texture for data"), michael@0: hr); michael@0: } michael@0: michael@0: return texture.forget(); michael@0: } michael@0: michael@0: static already_AddRefed michael@0: SurfaceToTexture(ID3D10Device *aDevice, michael@0: SourceSurface *aSurface, michael@0: const IntSize &aSize) michael@0: { michael@0: if (!aSurface) { michael@0: return nullptr; michael@0: } michael@0: michael@0: void *nativeSurf = michael@0: aSurface->GetNativeSurface(NativeSurfaceType::D3D10_TEXTURE); michael@0: if (nativeSurf) { michael@0: nsRefPtr texture = michael@0: static_cast(nativeSurf); michael@0: ID3D10Device *dev; michael@0: texture->GetDevice(&dev); michael@0: if (dev == aDevice) { michael@0: return texture.forget(); michael@0: } michael@0: } michael@0: RefPtr dataSurface = aSurface->GetDataSurface(); michael@0: if (!dataSurface) { michael@0: return nullptr; michael@0: } michael@0: DataSourceSurface::MappedSurface map; michael@0: if (!dataSurface->Map(DataSourceSurface::MapType::READ, &map)) { michael@0: return nullptr; michael@0: } michael@0: nsRefPtr texture = michael@0: DataToTexture(aDevice, map.mData, map.mStride, aSize); michael@0: dataSurface->Unmap(); michael@0: return texture.forget(); michael@0: } michael@0: michael@0: Layer* michael@0: ImageLayerD3D10::GetLayer() michael@0: { michael@0: return this; michael@0: } michael@0: michael@0: /** michael@0: * Returns a shader resource view for a Cairo or remote image. michael@0: * Returns nullptr if unsuccessful. michael@0: * If successful, aHasAlpha will be true iff the resulting texture michael@0: * has an alpha component. michael@0: */ michael@0: ID3D10ShaderResourceView* michael@0: ImageLayerD3D10::GetImageSRView(Image* aImage, bool& aHasAlpha, IDXGIKeyedMutex **aMutex) michael@0: { michael@0: NS_ASSERTION(aImage, "Null image."); michael@0: michael@0: if (aImage->GetFormat() == ImageFormat::REMOTE_IMAGE_BITMAP) { michael@0: RemoteBitmapImage *remoteImage = michael@0: static_cast(aImage); michael@0: michael@0: if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)) { michael@0: nsAutoPtr dat(new TextureD3D10BackendData()); michael@0: dat->mTexture = DataToTexture(device(), remoteImage->mData, remoteImage->mStride, remoteImage->mSize); michael@0: michael@0: if (dat->mTexture) { michael@0: device()->CreateShaderResourceView(dat->mTexture, nullptr, getter_AddRefs(dat->mSRView)); michael@0: aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10, dat.forget()); michael@0: } michael@0: } michael@0: michael@0: aHasAlpha = remoteImage->mFormat == RemoteImageData::BGRA32; michael@0: } else if (aImage->GetFormat() == ImageFormat::REMOTE_IMAGE_DXGI_TEXTURE) { michael@0: RemoteDXGITextureImage *remoteImage = michael@0: static_cast(aImage); michael@0: michael@0: remoteImage->GetD3D10TextureBackendData(device()); michael@0: michael@0: aHasAlpha = remoteImage->mFormat == RemoteImageData::BGRA32; michael@0: } else if (aImage->GetFormat() == ImageFormat::CAIRO_SURFACE) { michael@0: CairoImage *cairoImage = michael@0: static_cast(aImage); michael@0: michael@0: RefPtr surf = cairoImage->GetAsSourceSurface(); michael@0: if (!surf) { michael@0: return nullptr; michael@0: } michael@0: michael@0: if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)) { michael@0: nsAutoPtr dat(new TextureD3D10BackendData()); michael@0: dat->mTexture = SurfaceToTexture(device(), surf, cairoImage->GetSize()); michael@0: michael@0: if (dat->mTexture) { michael@0: device()->CreateShaderResourceView(dat->mTexture, nullptr, getter_AddRefs(dat->mSRView)); michael@0: aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10, dat.forget()); michael@0: } michael@0: } michael@0: michael@0: aHasAlpha = surf->GetFormat() == SurfaceFormat::B8G8R8A8; michael@0: } else if (aImage->GetFormat() == ImageFormat::D3D9_RGB32_TEXTURE) { michael@0: if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)) { michael@0: // Use resource sharing to open the D3D9 texture as a D3D10 texture, michael@0: HRESULT hr; michael@0: D3D9SurfaceImage* d3dImage = reinterpret_cast(aImage); michael@0: nsRefPtr texture; michael@0: hr = device()->OpenSharedResource(d3dImage->GetShareHandle(), michael@0: IID_ID3D10Texture2D, michael@0: (void**)getter_AddRefs(texture)); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr); michael@0: michael@0: nsAutoPtr dat(new TextureD3D10BackendData()); michael@0: dat->mTexture = texture; michael@0: michael@0: hr = device()->CreateShaderResourceView(dat->mTexture, nullptr, getter_AddRefs(dat->mSRView)); michael@0: NS_ENSURE_TRUE(SUCCEEDED(hr) && dat->mSRView, nullptr); michael@0: michael@0: aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10, dat.forget()); michael@0: } michael@0: aHasAlpha = false; michael@0: } else { michael@0: NS_WARNING("Incorrect image type."); michael@0: return nullptr; michael@0: } michael@0: michael@0: TextureD3D10BackendData *data = michael@0: static_cast(aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)); michael@0: michael@0: if (!data) { michael@0: return nullptr; michael@0: } michael@0: michael@0: if (aMutex && michael@0: SUCCEEDED(data->mTexture->QueryInterface(IID_IDXGIKeyedMutex, (void**)aMutex))) { michael@0: if (FAILED((*aMutex)->AcquireSync(0, 0))) { michael@0: NS_WARNING("Failed to acquire sync on keyed mutex, plugin forgot to release?"); michael@0: return nullptr; michael@0: } michael@0: } michael@0: michael@0: nsRefPtr dev; michael@0: data->mTexture->GetDevice(getter_AddRefs(dev)); michael@0: if (dev != device()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: return data->mSRView; michael@0: } michael@0: michael@0: void michael@0: ImageLayerD3D10::RenderLayer() michael@0: { michael@0: ImageContainer *container = GetContainer(); michael@0: if (!container) { michael@0: return; michael@0: } michael@0: michael@0: AutoLockImage autoLock(container); michael@0: michael@0: Image *image = autoLock.GetImage(); michael@0: if (!image) { michael@0: return; michael@0: } michael@0: michael@0: IntSize size = image->GetSize(); michael@0: michael@0: SetEffectTransformAndOpacity(); michael@0: michael@0: ID3D10EffectTechnique *technique; michael@0: nsRefPtr keyedMutex; michael@0: michael@0: if (image->GetFormat() == ImageFormat::CAIRO_SURFACE || michael@0: image->GetFormat() == ImageFormat::REMOTE_IMAGE_BITMAP || michael@0: image->GetFormat() == ImageFormat::REMOTE_IMAGE_DXGI_TEXTURE || michael@0: image->GetFormat() == ImageFormat::D3D9_RGB32_TEXTURE) { michael@0: NS_ASSERTION(image->GetFormat() != ImageFormat::CAIRO_SURFACE || michael@0: !static_cast(image)->mSourceSurface || michael@0: static_cast(image)->mSourceSurface->GetFormat() != SurfaceFormat::A8, michael@0: "Image layer has alpha image"); michael@0: bool hasAlpha = false; michael@0: michael@0: nsRefPtr srView = GetImageSRView(image, hasAlpha, getter_AddRefs(keyedMutex)); michael@0: if (!srView) { michael@0: return; michael@0: } michael@0: michael@0: uint8_t shaderFlags = SHADER_PREMUL; michael@0: shaderFlags |= LoadMaskTexture(); michael@0: shaderFlags |= hasAlpha michael@0: ? SHADER_RGBA : SHADER_RGB; michael@0: shaderFlags |= mFilter == GraphicsFilter::FILTER_NEAREST michael@0: ? SHADER_POINT : SHADER_LINEAR; michael@0: technique = SelectShader(shaderFlags); michael@0: michael@0: michael@0: effect()->GetVariableByName("tRGB")->AsShaderResource()->SetResource(srView); michael@0: michael@0: effect()->GetVariableByName("vLayerQuad")->AsVector()->SetFloatVector( michael@0: ShaderConstantRectD3D10( michael@0: (float)0, michael@0: (float)0, michael@0: (float)size.width, michael@0: (float)size.height) michael@0: ); michael@0: } else if (image->GetFormat() == ImageFormat::PLANAR_YCBCR) { michael@0: PlanarYCbCrImage *yuvImage = michael@0: static_cast(image); michael@0: michael@0: if (!yuvImage->IsValid()) { michael@0: return; michael@0: } michael@0: michael@0: if (!yuvImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)) { michael@0: AllocateTexturesYCbCr(yuvImage); michael@0: } michael@0: michael@0: PlanarYCbCrD3D10BackendData *data = michael@0: static_cast(yuvImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)); michael@0: michael@0: if (!data) { michael@0: return; michael@0: } michael@0: michael@0: nsRefPtr dev; michael@0: data->mYTexture->GetDevice(getter_AddRefs(dev)); michael@0: if (dev != device()) { michael@0: return; michael@0: } michael@0: michael@0: // TODO: At some point we should try to deal with mFilter here, you don't michael@0: // really want to use point filtering in the case of NEAREST, since that michael@0: // would also use point filtering for Chroma upsampling. Where most likely michael@0: // the user would only want point filtering for final RGB image upsampling. michael@0: michael@0: technique = SelectShader(SHADER_YCBCR | LoadMaskTexture()); michael@0: michael@0: effect()->GetVariableByName("tY")->AsShaderResource()->SetResource(data->mYView); michael@0: effect()->GetVariableByName("tCb")->AsShaderResource()->SetResource(data->mCbView); michael@0: effect()->GetVariableByName("tCr")->AsShaderResource()->SetResource(data->mCrView); michael@0: michael@0: /* michael@0: * Send 3d control data and metadata to NV3DVUtils michael@0: */ michael@0: if (GetNv3DVUtils()) { michael@0: Nv_Stereo_Mode mode; michael@0: switch (yuvImage->GetData()->mStereoMode) { michael@0: case StereoMode::LEFT_RIGHT: michael@0: mode = NV_STEREO_MODE_LEFT_RIGHT; michael@0: break; michael@0: case StereoMode::RIGHT_LEFT: michael@0: mode = NV_STEREO_MODE_RIGHT_LEFT; michael@0: break; michael@0: case StereoMode::BOTTOM_TOP: michael@0: mode = NV_STEREO_MODE_BOTTOM_TOP; michael@0: break; michael@0: case StereoMode::TOP_BOTTOM: michael@0: mode = NV_STEREO_MODE_TOP_BOTTOM; michael@0: break; michael@0: case StereoMode::MONO: michael@0: mode = NV_STEREO_MODE_MONO; michael@0: break; michael@0: } michael@0: michael@0: // Send control data even in mono case so driver knows to leave stereo mode. michael@0: GetNv3DVUtils()->SendNv3DVControl(mode, true, FIREFOX_3DV_APP_HANDLE); michael@0: michael@0: if (yuvImage->GetData()->mStereoMode != StereoMode::MONO) { michael@0: // Dst resource is optional michael@0: GetNv3DVUtils()->SendNv3DVMetaData((unsigned int)yuvImage->GetData()->mYSize.width, michael@0: (unsigned int)yuvImage->GetData()->mYSize.height, (HANDLE)(data->mYTexture), (HANDLE)(nullptr)); michael@0: } michael@0: } michael@0: michael@0: effect()->GetVariableByName("vLayerQuad")->AsVector()->SetFloatVector( michael@0: ShaderConstantRectD3D10( michael@0: (float)0, michael@0: (float)0, michael@0: (float)size.width, michael@0: (float)size.height) michael@0: ); michael@0: michael@0: effect()->GetVariableByName("vTextureCoords")->AsVector()->SetFloatVector( michael@0: ShaderConstantRectD3D10( michael@0: (float)yuvImage->GetData()->mPicX / yuvImage->GetData()->mYSize.width, michael@0: (float)yuvImage->GetData()->mPicY / yuvImage->GetData()->mYSize.height, michael@0: (float)yuvImage->GetData()->mPicSize.width / yuvImage->GetData()->mYSize.width, michael@0: (float)yuvImage->GetData()->mPicSize.height / yuvImage->GetData()->mYSize.height) michael@0: ); michael@0: } michael@0: michael@0: bool resetTexCoords = image->GetFormat() == ImageFormat::PLANAR_YCBCR; michael@0: image = nullptr; michael@0: autoLock.Unlock(); michael@0: michael@0: technique->GetPassByIndex(0)->Apply(0); michael@0: device()->Draw(4, 0); michael@0: michael@0: if (keyedMutex) { michael@0: keyedMutex->ReleaseSync(0); michael@0: } michael@0: michael@0: if (resetTexCoords) { michael@0: effect()->GetVariableByName("vTextureCoords")->AsVector()-> michael@0: SetFloatVector(ShaderConstantRectD3D10(0, 0, 1.0f, 1.0f)); michael@0: } michael@0: michael@0: GetContainer()->NotifyPaintedImage(image); michael@0: } michael@0: michael@0: void ImageLayerD3D10::AllocateTexturesYCbCr(PlanarYCbCrImage *aImage) michael@0: { michael@0: nsAutoPtr backendData( michael@0: new PlanarYCbCrD3D10BackendData); michael@0: michael@0: const PlanarYCbCrData *data = aImage->GetData(); michael@0: michael@0: D3D10_SUBRESOURCE_DATA dataY; michael@0: D3D10_SUBRESOURCE_DATA dataCb; michael@0: D3D10_SUBRESOURCE_DATA dataCr; michael@0: CD3D10_TEXTURE2D_DESC descY(DXGI_FORMAT_A8_UNORM, michael@0: data->mYSize.width, michael@0: data->mYSize.height, 1, 1); michael@0: CD3D10_TEXTURE2D_DESC descCbCr(DXGI_FORMAT_A8_UNORM, michael@0: data->mCbCrSize.width, michael@0: data->mCbCrSize.height, 1, 1); michael@0: michael@0: descY.Usage = descCbCr.Usage = D3D10_USAGE_IMMUTABLE; michael@0: michael@0: dataY.pSysMem = data->mYChannel; michael@0: dataY.SysMemPitch = data->mYStride; michael@0: dataCb.pSysMem = data->mCbChannel; michael@0: dataCb.SysMemPitch = data->mCbCrStride; michael@0: dataCr.pSysMem = data->mCrChannel; michael@0: dataCr.SysMemPitch = data->mCbCrStride; michael@0: michael@0: HRESULT hr = device()->CreateTexture2D(&descY, &dataY, getter_AddRefs(backendData->mYTexture)); michael@0: if (!FAILED(hr)) { michael@0: hr = device()->CreateTexture2D(&descCbCr, &dataCb, getter_AddRefs(backendData->mCbTexture)); michael@0: } michael@0: if (!FAILED(hr)) { michael@0: hr = device()->CreateTexture2D(&descCbCr, &dataCr, getter_AddRefs(backendData->mCrTexture)); michael@0: } michael@0: if (FAILED(hr)) { michael@0: LayerManagerD3D10::ReportFailure(NS_LITERAL_CSTRING("PlanarYCbCrImageD3D10::AllocateTextures(): Failed to create texture"), michael@0: hr); michael@0: return; michael@0: } michael@0: device()->CreateShaderResourceView(backendData->mYTexture, nullptr, getter_AddRefs(backendData->mYView)); michael@0: device()->CreateShaderResourceView(backendData->mCbTexture, nullptr, getter_AddRefs(backendData->mCbView)); michael@0: device()->CreateShaderResourceView(backendData->mCrTexture, nullptr, getter_AddRefs(backendData->mCrView)); michael@0: michael@0: aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10, backendData.forget()); michael@0: } michael@0: michael@0: already_AddRefed michael@0: ImageLayerD3D10::GetAsTexture(gfx::IntSize* aSize) michael@0: { michael@0: if (!GetContainer()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: AutoLockImage autoLock(GetContainer()); michael@0: michael@0: Image *image = autoLock.GetImage(); michael@0: if (!image) { michael@0: return nullptr; michael@0: } michael@0: michael@0: if (image->GetFormat() != ImageFormat::CAIRO_SURFACE) { michael@0: return nullptr; michael@0: } michael@0: michael@0: *aSize = image->GetSize(); michael@0: bool dontCare; michael@0: nsRefPtr result = GetImageSRView(image, dontCare); michael@0: return result.forget(); michael@0: } michael@0: michael@0: TemporaryRef michael@0: RemoteDXGITextureImage::GetAsSourceSurface() michael@0: { michael@0: nsRefPtr device = michael@0: gfxWindowsPlatform::GetPlatform()->GetD3D10Device(); michael@0: if (!device) { michael@0: NS_WARNING("Cannot readback from shared texture because no D3D10 device is available."); michael@0: return nullptr; michael@0: } michael@0: michael@0: TextureD3D10BackendData* data = GetD3D10TextureBackendData(device); michael@0: michael@0: if (!data) { michael@0: return nullptr; michael@0: } michael@0: michael@0: nsRefPtr keyedMutex; michael@0: michael@0: if (FAILED(data->mTexture->QueryInterface(IID_IDXGIKeyedMutex, getter_AddRefs(keyedMutex)))) { michael@0: NS_WARNING("Failed to QueryInterface for IDXGIKeyedMutex, strange."); michael@0: return nullptr; michael@0: } michael@0: michael@0: if (FAILED(keyedMutex->AcquireSync(0, 0))) { michael@0: NS_WARNING("Failed to acquire sync for keyedMutex, plugin failed to release?"); michael@0: return nullptr; michael@0: } michael@0: michael@0: D3D10_TEXTURE2D_DESC desc; michael@0: michael@0: data->mTexture->GetDesc(&desc); michael@0: michael@0: desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ; michael@0: desc.BindFlags = 0; michael@0: desc.MiscFlags = 0; michael@0: desc.Usage = D3D10_USAGE_STAGING; michael@0: michael@0: nsRefPtr softTexture; michael@0: HRESULT hr = device->CreateTexture2D(&desc, nullptr, getter_AddRefs(softTexture)); michael@0: michael@0: if (FAILED(hr)) { michael@0: NS_WARNING("Failed to create 2D staging texture."); michael@0: return nullptr; michael@0: } michael@0: michael@0: device->CopyResource(softTexture, data->mTexture); michael@0: keyedMutex->ReleaseSync(0); michael@0: michael@0: RefPtr surface michael@0: = gfx::Factory::CreateDataSourceSurface(mSize, michael@0: mFormat == RemoteImageData::BGRX32 michael@0: ? gfx::SurfaceFormat::B8G8R8X8 michael@0: : gfx::SurfaceFormat::B8G8R8A8); michael@0: michael@0: if (!surface) { michael@0: NS_WARNING("Failed to create SourceSurface for DXGI texture."); michael@0: return nullptr; michael@0: } michael@0: michael@0: gfx::DataSourceSurface::MappedSurface mappedSurface; michael@0: if (!surface->Map(gfx::DataSourceSurface::WRITE, &mappedSurface)) { michael@0: NS_WARNING("Failed to map source surface"); michael@0: return nullptr; michael@0: } michael@0: michael@0: D3D10_MAPPED_TEXTURE2D mapped; michael@0: softTexture->Map(0, D3D10_MAP_READ, 0, &mapped); michael@0: michael@0: for (int y = 0; y < mSize.height; y++) { michael@0: memcpy(mappedSurface.mData + mappedSurface.mStride * y, michael@0: (unsigned char*)(mapped.pData) + mapped.RowPitch * y, michael@0: mSize.width * 4); michael@0: } michael@0: michael@0: softTexture->Unmap(0); michael@0: surface->Unmap(); michael@0: michael@0: return surface; michael@0: } michael@0: michael@0: TextureD3D10BackendData* michael@0: RemoteDXGITextureImage::GetD3D10TextureBackendData(ID3D10Device *aDevice) michael@0: { michael@0: if (GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)) { michael@0: TextureD3D10BackendData *data = michael@0: static_cast(GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)); michael@0: michael@0: nsRefPtr device; michael@0: data->mTexture->GetDevice(getter_AddRefs(device)); michael@0: michael@0: if (device == aDevice) { michael@0: return data; michael@0: } michael@0: } michael@0: nsRefPtr texture; michael@0: aDevice->OpenSharedResource(mHandle, __uuidof(ID3D10Texture2D), getter_AddRefs(texture)); michael@0: michael@0: if (!texture) { michael@0: NS_WARNING("Failed to get texture for shared texture handle."); michael@0: return nullptr; michael@0: } michael@0: michael@0: nsAutoPtr data(new TextureD3D10BackendData()); michael@0: michael@0: data->mTexture = texture; michael@0: michael@0: aDevice->CreateShaderResourceView(texture, nullptr, getter_AddRefs(data->mSRView)); michael@0: michael@0: SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10, data); michael@0: michael@0: return data.forget(); michael@0: } michael@0: michael@0: } /* layers */ michael@0: } /* mozilla */