tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

D3D11YCbCrImage.cpp (3725B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "D3D11YCbCrImage.h"
      8 
      9 #include "YCbCrUtils.h"
     10 #include "gfx2DGlue.h"
     11 #include "mozilla/gfx/DeviceManagerDx.h"
     12 #include "mozilla/gfx/gfxVars.h"
     13 #include "mozilla/layers/CompositableClient.h"
     14 #include "mozilla/layers/CompositableForwarder.h"
     15 #include "mozilla/layers/TextureD3D11.h"
     16 
     17 using namespace mozilla::gfx;
     18 
     19 namespace mozilla {
     20 namespace layers {
     21 
     22 DXGIYCbCrTextureAllocationHelper::DXGIYCbCrTextureAllocationHelper(
     23    const PlanarYCbCrData& aData, TextureFlags aTextureFlags,
     24    ID3D11Device* aDevice)
     25    : ITextureClientAllocationHelper(
     26          gfx::SurfaceFormat::YUV420, aData.mPictureRect.Size(),
     27          BackendSelector::Content, aTextureFlags, ALLOC_DEFAULT),
     28      mData(aData),
     29      mDevice(aDevice) {}
     30 
     31 bool DXGIYCbCrTextureAllocationHelper::IsCompatible(
     32    TextureClient* aTextureClient) {
     33  MOZ_ASSERT(aTextureClient->GetFormat() == gfx::SurfaceFormat::YUV420);
     34 
     35  DXGIYCbCrTextureData* dxgiData =
     36      aTextureClient->GetInternalData()->AsDXGIYCbCrTextureData();
     37  if (!dxgiData || dxgiData->mSize != mData.mPictureRect.Size() ||
     38      dxgiData->mSizeY != mData.YDataSize() ||
     39      dxgiData->mSizeCbCr != mData.CbCrDataSize() ||
     40      dxgiData->mColorDepth != mData.mColorDepth ||
     41      dxgiData->mYUVColorSpace != mData.mYUVColorSpace) {
     42    return false;
     43  }
     44 
     45  ID3D11Texture2D* textureY = dxgiData->GetD3D11Texture(0);
     46 
     47  RefPtr<ID3D11Device> device;
     48  textureY->GetDevice(getter_AddRefs(device));
     49  if (!device || device != gfx::DeviceManagerDx::Get()->GetImageDevice()) {
     50    return false;
     51  }
     52 
     53  return true;
     54 }
     55 
     56 already_AddRefed<TextureClient> DXGIYCbCrTextureAllocationHelper::Allocate(
     57    KnowsCompositor* aAllocator) {
     58  auto ySize = mData.YDataSize();
     59  auto cbcrSize = mData.CbCrDataSize();
     60  CD3D11_TEXTURE2D_DESC newDesc(mData.mColorDepth == gfx::ColorDepth::COLOR_8
     61                                    ? DXGI_FORMAT_R8_UNORM
     62                                    : DXGI_FORMAT_R16_UNORM,
     63                                ySize.width, ySize.height, 1, 1);
     64  // Use FenceD3D11 for synchronization.
     65  newDesc.MiscFlags =
     66      D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED;
     67 
     68  RefPtr<ID3D11Texture2D> textureY;
     69  HRESULT hr =
     70      mDevice->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(textureY));
     71  NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
     72 
     73  newDesc.Width = cbcrSize.width;
     74  newDesc.Height = cbcrSize.height;
     75 
     76  RefPtr<ID3D11Texture2D> textureCb;
     77  hr = mDevice->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(textureCb));
     78  NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
     79 
     80  RefPtr<ID3D11Texture2D> textureCr;
     81  hr = mDevice->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(textureCr));
     82  NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
     83 
     84  TextureForwarder* forwarder =
     85      aAllocator ? aAllocator->GetTextureForwarder() : nullptr;
     86 
     87  return TextureClient::CreateWithData(
     88      DXGIYCbCrTextureData::Create(
     89          textureY, textureCb, textureCr, mData.mPictureRect.Size(), ySize,
     90          cbcrSize, mData.mColorDepth, mData.mYUVColorSpace, mData.mColorRange),
     91      mTextureFlags, forwarder);
     92 }
     93 
     94 already_AddRefed<TextureClient> D3D11YCbCrRecycleAllocator::Allocate(
     95    SurfaceFormat aFormat, IntSize aSize, BackendSelector aSelector,
     96    TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) {
     97  MOZ_ASSERT_UNREACHABLE("unexpected to be called");
     98  return nullptr;
     99 }
    100 
    101 }  // namespace layers
    102 }  // namespace mozilla