DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (9136070a0561)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "D3D9SurfaceImage.h"
#include "gfx2DGlue.h"
#include "gfxWindowsPlatform.h"
#include "mozilla/layers/CompositableClient.h"
#include "mozilla/layers/CompositableForwarder.h"
#include "mozilla/layers/ImageBridgeChild.h"
#include "mozilla/gfx/Types.h"

namespace mozilla {
namespace layers {

DXGID3D9TextureData::DXGID3D9TextureData(gfx::SurfaceFormat aFormat,
                                         IDirect3DTexture9* aTexture,
                                         HANDLE aHandle,
                                         IDirect3DDevice9* aDevice)
    : mDevice(aDevice), mTexture(aTexture), mFormat(aFormat), mHandle(aHandle) {
  MOZ_COUNT_CTOR(DXGID3D9TextureData);
}

DXGID3D9TextureData::~DXGID3D9TextureData() {
  gfxWindowsPlatform::sD3D9SharedTextures -= mDesc.Width * mDesc.Height * 4;
  MOZ_COUNT_DTOR(DXGID3D9TextureData);
}

// static
DXGID3D9TextureData* DXGID3D9TextureData::Create(gfx::IntSize aSize,
                                                 gfx::SurfaceFormat aFormat,
                                                 TextureFlags aFlags,
                                                 IDirect3DDevice9* aDevice) {
  AUTO_PROFILER_LABEL("DXGID3D9TextureData::Create", GRAPHICS);
  MOZ_ASSERT(aFormat == gfx::SurfaceFormat::B8G8R8A8);
  if (aFormat != gfx::SurfaceFormat::B8G8R8A8) {
    return nullptr;
  }

  RefPtr<IDirect3DTexture9> texture;
  HANDLE shareHandle = nullptr;
  HRESULT hr = aDevice->CreateTexture(
      aSize.width, aSize.height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,
      D3DPOOL_DEFAULT, getter_AddRefs(texture), &shareHandle);
  if (FAILED(hr) || !shareHandle) {
    return nullptr;
  }

  D3DSURFACE_DESC surfaceDesc;
  hr = texture->GetLevelDesc(0, &surfaceDesc);
  if (FAILED(hr)) {
    return nullptr;
  }
  DXGID3D9TextureData* data =
      new DXGID3D9TextureData(aFormat, texture, shareHandle, aDevice);
  data->mDesc = surfaceDesc;

  gfxWindowsPlatform::sD3D9SharedTextures += aSize.width * aSize.height * 4;
  return data;
}

void DXGID3D9TextureData::FillInfo(TextureData::Info& aInfo) const {
  aInfo.size = GetSize();
  aInfo.format = mFormat;
  aInfo.supportsMoz2D = false;
  aInfo.canExposeMappedData = false;
  aInfo.hasIntermediateBuffer = false;
  aInfo.hasSynchronization = false;
}

already_AddRefed<IDirect3DSurface9> DXGID3D9TextureData::GetD3D9Surface()
    const {
  RefPtr<IDirect3DSurface9> textureSurface;
  HRESULT hr = mTexture->GetSurfaceLevel(0, getter_AddRefs(textureSurface));
  NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);

  return textureSurface.forget();
}

bool DXGID3D9TextureData::Serialize(SurfaceDescriptor& aOutDescriptor) {
  // In reality, with D3D9 we will only ever deal with RGBA textures.
  bool isYUV = mFormat == SurfaceFormat::NV12 ||
               mFormat == SurfaceFormat::P010 || mFormat == SurfaceFormat::P016;
  aOutDescriptor = SurfaceDescriptorD3D10(
      (WindowsHandle)(mHandle), mFormat, GetSize(),
      isYUV ? gfx::YUVColorSpace::BT601 : gfx::YUVColorSpace::UNKNOWN,
      gfx::ColorRange::LIMITED);
  return true;
}

D3D9SurfaceImage::D3D9SurfaceImage()
    : Image(nullptr, ImageFormat::D3D9_RGB32_TEXTURE),
      mSize(0, 0),
      mShareHandle(0),
      mValid(true) {}

D3D9SurfaceImage::~D3D9SurfaceImage() {}

HRESULT
D3D9SurfaceImage::AllocateAndCopy(D3D9RecycleAllocator* aAllocator,
                                  IDirect3DSurface9* aSurface,
                                  const gfx::IntRect& aRegion) {
  NS_ENSURE_TRUE(aSurface, E_POINTER);
  HRESULT hr;
  RefPtr<IDirect3DSurface9> surface = aSurface;

  RefPtr<IDirect3DDevice9> device;
  hr = surface->GetDevice(getter_AddRefs(device));
  NS_ENSURE_TRUE(SUCCEEDED(hr), E_FAIL);

  RefPtr<IDirect3D9> d3d9;
  hr = device->GetDirect3D(getter_AddRefs(d3d9));
  NS_ENSURE_TRUE(SUCCEEDED(hr), E_FAIL);

  D3DSURFACE_DESC desc;
  surface->GetDesc(&desc);
  // Ensure we can convert the textures format to RGB conversion
  // in StretchRect. Fail if we can't.
  hr = d3d9->CheckDeviceFormatConversion(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
                                         desc.Format, D3DFMT_A8R8G8B8);
  NS_ENSURE_TRUE(SUCCEEDED(hr), hr);

  // DXVA surfaces aren't created sharable, so we need to copy the surface
  // to a sharable texture to that it's accessible to the layer manager's
  // device.
  if (aAllocator) {
    mTextureClient = aAllocator->CreateOrRecycleClient(
        gfx::SurfaceFormat::B8G8R8A8, aRegion.Size());
    if (!mTextureClient) {
      return E_FAIL;
    }

    DXGID3D9TextureData* texData =
        static_cast<DXGID3D9TextureData*>(mTextureClient->GetInternalData());
    mTexture = texData->GetD3D9Texture();
    mShareHandle = texData->GetShareHandle();
    mDesc = texData->GetDesc();
  } else {
    hr = device->CreateTexture(aRegion.Size().width, aRegion.Size().height, 1,
                               D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,
                               D3DPOOL_DEFAULT, getter_AddRefs(mTexture),
                               &mShareHandle);
    if (FAILED(hr) || !mShareHandle) {
      return E_FAIL;
    }

    hr = mTexture->GetLevelDesc(0, &mDesc);
    if (FAILED(hr)) {
      return E_FAIL;
    }
  }

  // Copy the image onto the texture, preforming YUV -> RGB conversion if
  // necessary.
  RefPtr<IDirect3DSurface9> textureSurface = GetD3D9Surface();
  if (!textureSurface) {
    return E_FAIL;
  }

  RECT src = {aRegion.X(), aRegion.Y(), aRegion.XMost(), aRegion.YMost()};
  hr =
      device->StretchRect(surface, &src, textureSurface, nullptr, D3DTEXF_NONE);
  NS_ENSURE_TRUE(SUCCEEDED(hr), hr);

  mSize = aRegion.Size();
  return S_OK;
}

already_AddRefed<IDirect3DSurface9> D3D9SurfaceImage::GetD3D9Surface() const {
  RefPtr<IDirect3DSurface9> textureSurface;
  HRESULT hr = mTexture->GetSurfaceLevel(0, getter_AddRefs(textureSurface));
  NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
  return textureSurface.forget();
}

const D3DSURFACE_DESC& D3D9SurfaceImage::GetDesc() const { return mDesc; }

HANDLE
D3D9SurfaceImage::GetShareHandle() const { return mShareHandle; }

gfx::IntSize D3D9SurfaceImage::GetSize() const { return mSize; }

TextureClient* D3D9SurfaceImage::GetTextureClient(
    KnowsCompositor* aKnowsCompositor) {
  MOZ_ASSERT(mTextureClient);
  MOZ_ASSERT(mTextureClient->GetAllocator() ==
             aKnowsCompositor->GetTextureForwarder());
  return mTextureClient;
}

already_AddRefed<gfx::SourceSurface> D3D9SurfaceImage::GetAsSourceSurface() {
  if (!mTexture) {
    return nullptr;
  }

  HRESULT hr;
  RefPtr<gfx::DataSourceSurface> surface =
      gfx::Factory::CreateDataSourceSurface(mSize,
                                            gfx::SurfaceFormat::B8G8R8X8);
  if (NS_WARN_IF(!surface)) {
    return nullptr;
  }

  // Readback the texture from GPU memory into system memory, so that
  // we can copy it into the Cairo image. This is expensive.
  RefPtr<IDirect3DSurface9> textureSurface = GetD3D9Surface();
  if (!textureSurface) {
    return nullptr;
  }

  RefPtr<IDirect3DDevice9> device;
  hr = textureSurface->GetDevice(getter_AddRefs(device));
  NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);

  RefPtr<IDirect3DSurface9> systemMemorySurface;
  hr = device->CreateOffscreenPlainSurface(
      mSize.width, mSize.height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM,
      getter_AddRefs(systemMemorySurface), 0);
  NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);

  hr = device->GetRenderTargetData(textureSurface, systemMemorySurface);
  NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);

  D3DLOCKED_RECT rect;
  hr = systemMemorySurface->LockRect(&rect, nullptr, 0);
  NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);

  gfx::DataSourceSurface::MappedSurface mappedSurface;
  if (!surface->Map(gfx::DataSourceSurface::WRITE, &mappedSurface)) {
    systemMemorySurface->UnlockRect();
    return nullptr;
  }

  const unsigned char* src = (const unsigned char*)(rect.pBits);
  const unsigned srcPitch = rect.Pitch;
  for (int y = 0; y < mSize.height; y++) {
    memcpy(mappedSurface.mData + mappedSurface.mStride * y,
           (unsigned char*)(src) + srcPitch * y, mSize.width * 4);
  }

  systemMemorySurface->UnlockRect();
  surface->Unmap();

  return surface.forget();
}

already_AddRefed<TextureClient> D3D9RecycleAllocator::Allocate(
    gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector,
    TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) {
  TextureData* data =
      DXGID3D9TextureData::Create(aSize, aFormat, aTextureFlags, mDevice);
  if (!data) {
    return nullptr;
  }

  return MakeAndAddRef<TextureClient>(data, aTextureFlags,
                                      mSurfaceAllocator->GetTextureForwarder());
}

already_AddRefed<TextureClient> D3D9RecycleAllocator::CreateOrRecycleClient(
    gfx::SurfaceFormat aFormat, const gfx::IntSize& aSize) {
  return CreateOrRecycle(aFormat, aSize, BackendSelector::Content,
                         TextureFlags::DEFAULT);
}

}  // namespace layers
}  // namespace mozilla