Files
palemoon27/gfx/layers/D3D11ShareHandleImage.cpp
T
roytam1 8a2a9d0991 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1196785 - Alphabetically sort redirected about:pages. r=jst (ba0f254ae5)
- Bug 1196785 - Make the common category style work in HTML. r=jaws (09b3f854e4)
- Bug 1219494 - Part 1. TextureD3D11 and gfxDevCrash instead of MOZ_CRASH where appropriate. r=mchang (2ba6492c83)
- Bug 1195531 - Don't discard d3d11 textures when we move the TextureSource to a new compositor. r=jrmuizel,nical (a4b9674552)
- Bug 1176570 - Make sure all shared texture handles are opened correctly before attempting to use them. r=jrmuizel (75856f47a0)
- misspatch Bug 1145513 (f4e6e6c52a)
- Null-check D3D10 devices in a few places. (bug 1225645, r=bas) (2f7372a29d)
- Bug 1200595 - D3D11 TextureData implementation. r=Bas (8935487dae)
- Bug 1216366: Ensure D3D11 uploads the entire surface the first time for component alpha textures. r=nical (789985252d)
- Bug 1194335. Disable partial present on Nvidia hardware. r=bas (3da45b4ce9)
- Bug 1167326, fix up VR distortion constant buffer; r=bas (396b34e5da)
- Bug 1204922 - Part 3. Report failed before HandleError call. r=bas (4f8ca8b0d6)
- Fail gracefully when we can't get a texture sync handle. (bug 1207665 part 4, r=bas,vladan) (d8cbab3307)
- Bug 1144136. Remove unneeded Flush() from D3D11 compositor. r=bas (8f49efa0aa)
- Bug 1163840 - Lazy-init blocker stack in AsyncShutdown to save startup time; r=Yoric (89f728be0f)
- Bug 1213280 - fix OSX font selection under 10.11. r=jfkthame (a5c21cacb0)
- Bug 1167284 - implement localized name lookup for fontconfig font families. r=karlt (098cb7067e)
- Bug 1173260 - support multiple families for generics. r=heycam (19bab2c23d)
- Bug 1163487 - sniff LANGUAGE when determining language for lang group. r=karlt (207283f74d)
- Bug 1224965 p1 - add pref for max substitutions for generics under fontconfig. r=m_kato (b64c53e636)
- Bug 1224965 p2 - tweak the handling of generic substitutions. r=karlt (a5c2688808)
- First version of patch 1469116, this helps fix patch 1209812 without skia. (dd570d1e71)
2023-02-01 10:53:25 +08:00

162 lines
4.7 KiB
C++

/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* 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/. */
#include "WMF.h"
#include "D3D11ShareHandleImage.h"
#include "gfxImageSurface.h"
#include "gfxWindowsPlatform.h"
#include "mozilla/layers/TextureClient.h"
#include "mozilla/layers/TextureD3D11.h"
#include "mozilla/layers/CompositableClient.h"
#include "mozilla/layers/CompositableForwarder.h"
#include "d3d11.h"
namespace mozilla {
namespace layers {
D3D11ShareHandleImage::D3D11ShareHandleImage(const gfx::IntSize& aSize,
const gfx::IntRect& aRect)
: Image(nullptr, ImageFormat::D3D11_SHARE_HANDLE_TEXTURE),
mSize(aSize),
mPictureRect(aRect)
{
}
bool
D3D11ShareHandleImage::AllocateTexture(D3D11RecycleAllocator* aAllocator)
{
mTextureClient = aAllocator->CreateOrRecycleClient(gfx::SurfaceFormat::B8G8R8A8, mSize);
return !!mTextureClient;
}
gfx::IntSize
D3D11ShareHandleImage::GetSize()
{
return mSize;
}
TextureClient*
D3D11ShareHandleImage::GetTextureClient(CompositableClient* aClient)
{
return mTextureClient;
}
already_AddRefed<gfx::SourceSurface>
D3D11ShareHandleImage::GetAsSourceSurface()
{
RefPtr<ID3D11Texture2D> texture = GetTexture();
if (!texture) {
NS_WARNING("Cannot readback from shared texture because no texture is available.");
return nullptr;
}
RefPtr<ID3D11Device> device;
texture->GetDevice(getter_AddRefs(device));
RefPtr<IDXGIKeyedMutex> keyedMutex;
if (FAILED(texture->QueryInterface(static_cast<IDXGIKeyedMutex**>(getter_AddRefs(keyedMutex))))) {
NS_WARNING("Failed to QueryInterface for IDXGIKeyedMutex, strange.");
return nullptr;
}
if (FAILED(keyedMutex->AcquireSync(0, 0))) {
NS_WARNING("Failed to acquire sync for keyedMutex, plugin failed to release?");
return nullptr;
}
D3D11_TEXTURE2D_DESC desc;
texture->GetDesc(&desc);
CD3D11_TEXTURE2D_DESC softDesc(desc.Format, desc.Width, desc.Height);
softDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
softDesc.BindFlags = 0;
softDesc.MiscFlags = 0;
softDesc.MipLevels = 1;
softDesc.Usage = D3D11_USAGE_STAGING;
RefPtr<ID3D11Texture2D> softTexture;
HRESULT hr = device->CreateTexture2D(&softDesc,
NULL,
static_cast<ID3D11Texture2D**>(getter_AddRefs(softTexture)));
if (FAILED(hr)) {
NS_WARNING("Failed to create 2D staging texture.");
keyedMutex->ReleaseSync(0);
return nullptr;
}
RefPtr<ID3D11DeviceContext> context;
device->GetImmediateContext(getter_AddRefs(context));
if (!context) {
keyedMutex->ReleaseSync(0);
return nullptr;
}
context->CopyResource(softTexture, texture);
keyedMutex->ReleaseSync(0);
RefPtr<gfx::DataSourceSurface> surface =
gfx::Factory::CreateDataSourceSurface(mSize, gfx::SurfaceFormat::B8G8R8X8);
if (NS_WARN_IF(!surface)) {
return nullptr;
}
gfx::DataSourceSurface::MappedSurface mappedSurface;
if (!surface->Map(gfx::DataSourceSurface::WRITE, &mappedSurface)) {
return nullptr;
}
D3D11_MAPPED_SUBRESOURCE map;
hr = context->Map(softTexture, 0, D3D11_MAP_READ, 0, &map);
if (!SUCCEEDED(hr)) {
surface->Unmap();
return nullptr;
}
for (int y = 0; y < mSize.height; y++) {
memcpy(mappedSurface.mData + mappedSurface.mStride * y,
(unsigned char*)(map.pData) + map.RowPitch * y,
mSize.width * 4);
}
context->Unmap(softTexture, 0);
surface->Unmap();
return surface.forget();
}
ID3D11Texture2D*
D3D11ShareHandleImage::GetTexture() const {
return static_cast<D3D11TextureData*>(mTextureClient->GetInternalData())->GetD3D11Texture();
}
already_AddRefed<TextureClient>
D3D11RecycleAllocator::Allocate(gfx::SurfaceFormat aFormat,
gfx::IntSize aSize,
BackendSelector aSelector,
TextureFlags aTextureFlags,
TextureAllocationFlags aAllocFlags)
{
return CreateD3D11TextureClientWithDevice(aSize, aFormat,
aTextureFlags, aAllocFlags,
mDevice, mSurfaceAllocator);
}
already_AddRefed<TextureClient>
D3D11RecycleAllocator::CreateOrRecycleClient(gfx::SurfaceFormat aFormat,
const gfx::IntSize& aSize)
{
RefPtr<TextureClient> textureClient =
CreateOrRecycle(aFormat,
aSize,
BackendSelector::Content,
layers::TextureFlags::DEFAULT);
return textureClient.forget();
}
} // namespace layers
} // namespace mozilla