gfx/layers/d3d10/ImageLayerD3D10.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/layers/d3d10/ImageLayerD3D10.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,535 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "ImageLayerD3D10.h"
    1.10 +#include "gfxD2DSurface.h"
    1.11 +#include "gfxWindowsSurface.h"
    1.12 +#include "yuv_convert.h"
    1.13 +#include "../d3d9/Nv3DVUtils.h"
    1.14 +#include "D3D9SurfaceImage.h"
    1.15 +#include "mozilla/gfx/Point.h"
    1.16 +#include "gfx2DGlue.h"
    1.17 +
    1.18 +#include "gfxWindowsPlatform.h"
    1.19 +
    1.20 +using namespace mozilla::gfx;
    1.21 +
    1.22 +namespace mozilla {
    1.23 +namespace layers {
    1.24 +
    1.25 +static already_AddRefed<ID3D10Texture2D>
    1.26 +DataToTexture(ID3D10Device *aDevice,
    1.27 +              unsigned char *data,
    1.28 +              int stride,
    1.29 +              const IntSize &aSize)
    1.30 +{
    1.31 +  D3D10_SUBRESOURCE_DATA srdata;
    1.32 +
    1.33 +  CD3D10_TEXTURE2D_DESC desc(DXGI_FORMAT_B8G8R8A8_UNORM,
    1.34 +                             aSize.width,
    1.35 +                             aSize.height,
    1.36 +                             1, 1);
    1.37 +  desc.Usage = D3D10_USAGE_IMMUTABLE;
    1.38 +
    1.39 +  srdata.pSysMem = data;
    1.40 +  srdata.SysMemPitch = stride;
    1.41 +
    1.42 +  nsRefPtr<ID3D10Texture2D> texture;
    1.43 +  HRESULT hr = aDevice->CreateTexture2D(&desc, &srdata, getter_AddRefs(texture));
    1.44 +
    1.45 +  if (FAILED(hr)) {
    1.46 +    LayerManagerD3D10::ReportFailure(NS_LITERAL_CSTRING("Failed to create texture for data"),
    1.47 +                                     hr);
    1.48 +  }
    1.49 +
    1.50 +  return texture.forget();
    1.51 +}
    1.52 +
    1.53 +static already_AddRefed<ID3D10Texture2D>
    1.54 +SurfaceToTexture(ID3D10Device *aDevice,
    1.55 +                 SourceSurface *aSurface,
    1.56 +                 const IntSize &aSize)
    1.57 +{
    1.58 +  if (!aSurface) {
    1.59 +    return nullptr;
    1.60 +  }
    1.61 +
    1.62 +  void *nativeSurf =
    1.63 +    aSurface->GetNativeSurface(NativeSurfaceType::D3D10_TEXTURE);
    1.64 +  if (nativeSurf) {
    1.65 +    nsRefPtr<ID3D10Texture2D> texture =
    1.66 +      static_cast<ID3D10Texture2D*>(nativeSurf);
    1.67 +    ID3D10Device *dev;
    1.68 +    texture->GetDevice(&dev);
    1.69 +    if (dev == aDevice) {
    1.70 +      return texture.forget();
    1.71 +    }
    1.72 +  }
    1.73 +  RefPtr<DataSourceSurface> dataSurface = aSurface->GetDataSurface();
    1.74 +  if (!dataSurface) {
    1.75 +    return nullptr;
    1.76 +  }
    1.77 +  DataSourceSurface::MappedSurface map;
    1.78 +  if (!dataSurface->Map(DataSourceSurface::MapType::READ, &map)) {
    1.79 +    return nullptr;
    1.80 +  }
    1.81 +  nsRefPtr<ID3D10Texture2D> texture =
    1.82 +    DataToTexture(aDevice, map.mData, map.mStride, aSize);
    1.83 +  dataSurface->Unmap();
    1.84 +  return texture.forget();
    1.85 +}
    1.86 +
    1.87 +Layer*
    1.88 +ImageLayerD3D10::GetLayer()
    1.89 +{
    1.90 +  return this;
    1.91 +}
    1.92 +
    1.93 +/**
    1.94 + * Returns a shader resource view for a Cairo or remote image.
    1.95 + * Returns nullptr if unsuccessful.
    1.96 + * If successful, aHasAlpha will be true iff the resulting texture
    1.97 + * has an alpha component.
    1.98 + */
    1.99 +ID3D10ShaderResourceView*
   1.100 +ImageLayerD3D10::GetImageSRView(Image* aImage, bool& aHasAlpha, IDXGIKeyedMutex **aMutex)
   1.101 +{
   1.102 +  NS_ASSERTION(aImage, "Null image.");
   1.103 +
   1.104 +  if (aImage->GetFormat() == ImageFormat::REMOTE_IMAGE_BITMAP) {
   1.105 +    RemoteBitmapImage *remoteImage =
   1.106 +      static_cast<RemoteBitmapImage*>(aImage);
   1.107 +
   1.108 +    if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)) {
   1.109 +      nsAutoPtr<TextureD3D10BackendData> dat(new TextureD3D10BackendData());
   1.110 +      dat->mTexture = DataToTexture(device(), remoteImage->mData, remoteImage->mStride, remoteImage->mSize);
   1.111 +
   1.112 +      if (dat->mTexture) {
   1.113 +        device()->CreateShaderResourceView(dat->mTexture, nullptr, getter_AddRefs(dat->mSRView));
   1.114 +        aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10, dat.forget());
   1.115 +      }
   1.116 +    }
   1.117 +
   1.118 +    aHasAlpha = remoteImage->mFormat == RemoteImageData::BGRA32;
   1.119 +  } else if (aImage->GetFormat() == ImageFormat::REMOTE_IMAGE_DXGI_TEXTURE) {
   1.120 +    RemoteDXGITextureImage *remoteImage =
   1.121 +      static_cast<RemoteDXGITextureImage*>(aImage);
   1.122 +
   1.123 +    remoteImage->GetD3D10TextureBackendData(device());
   1.124 +
   1.125 +    aHasAlpha = remoteImage->mFormat == RemoteImageData::BGRA32;
   1.126 +  } else if (aImage->GetFormat() == ImageFormat::CAIRO_SURFACE) {
   1.127 +    CairoImage *cairoImage =
   1.128 +      static_cast<CairoImage*>(aImage);
   1.129 +
   1.130 +    RefPtr<SourceSurface> surf = cairoImage->GetAsSourceSurface();
   1.131 +    if (!surf) {
   1.132 +      return nullptr;
   1.133 +    }
   1.134 +
   1.135 +    if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)) {
   1.136 +      nsAutoPtr<TextureD3D10BackendData> dat(new TextureD3D10BackendData());
   1.137 +      dat->mTexture = SurfaceToTexture(device(), surf, cairoImage->GetSize());
   1.138 +
   1.139 +      if (dat->mTexture) {
   1.140 +        device()->CreateShaderResourceView(dat->mTexture, nullptr, getter_AddRefs(dat->mSRView));
   1.141 +        aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10, dat.forget());
   1.142 +      }
   1.143 +    }
   1.144 +
   1.145 +    aHasAlpha = surf->GetFormat() == SurfaceFormat::B8G8R8A8;
   1.146 +  } else if (aImage->GetFormat() == ImageFormat::D3D9_RGB32_TEXTURE) {
   1.147 +    if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)) {
   1.148 +      // Use resource sharing to open the D3D9 texture as a D3D10 texture,
   1.149 +      HRESULT hr;
   1.150 +      D3D9SurfaceImage* d3dImage = reinterpret_cast<D3D9SurfaceImage*>(aImage);
   1.151 +      nsRefPtr<ID3D10Texture2D> texture;
   1.152 +      hr = device()->OpenSharedResource(d3dImage->GetShareHandle(),
   1.153 +                                        IID_ID3D10Texture2D,
   1.154 +                                        (void**)getter_AddRefs(texture));
   1.155 +      NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
   1.156 +
   1.157 +      nsAutoPtr<TextureD3D10BackendData> dat(new TextureD3D10BackendData());
   1.158 +      dat->mTexture = texture;
   1.159 +
   1.160 +      hr = device()->CreateShaderResourceView(dat->mTexture, nullptr, getter_AddRefs(dat->mSRView));
   1.161 +      NS_ENSURE_TRUE(SUCCEEDED(hr) && dat->mSRView, nullptr);
   1.162 +
   1.163 +      aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10, dat.forget());
   1.164 +    }
   1.165 +    aHasAlpha = false;
   1.166 +  } else {
   1.167 +    NS_WARNING("Incorrect image type.");
   1.168 +    return nullptr;
   1.169 +  }
   1.170 +
   1.171 +  TextureD3D10BackendData *data =
   1.172 +    static_cast<TextureD3D10BackendData*>(aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10));
   1.173 +
   1.174 +  if (!data) {
   1.175 +    return nullptr;
   1.176 +  }
   1.177 +
   1.178 +  if (aMutex &&
   1.179 +      SUCCEEDED(data->mTexture->QueryInterface(IID_IDXGIKeyedMutex, (void**)aMutex))) {
   1.180 +    if (FAILED((*aMutex)->AcquireSync(0, 0))) {
   1.181 +      NS_WARNING("Failed to acquire sync on keyed mutex, plugin forgot to release?");
   1.182 +      return nullptr;
   1.183 +    }
   1.184 +  }
   1.185 +
   1.186 +  nsRefPtr<ID3D10Device> dev;
   1.187 +  data->mTexture->GetDevice(getter_AddRefs(dev));
   1.188 +  if (dev != device()) {
   1.189 +    return nullptr;
   1.190 +  }
   1.191 +
   1.192 +  return data->mSRView;
   1.193 +}
   1.194 +
   1.195 +void
   1.196 +ImageLayerD3D10::RenderLayer()
   1.197 +{
   1.198 +  ImageContainer *container = GetContainer();
   1.199 +  if (!container) {
   1.200 +    return;
   1.201 +  }
   1.202 +
   1.203 +  AutoLockImage autoLock(container);
   1.204 +
   1.205 +  Image *image = autoLock.GetImage();
   1.206 +  if (!image) {
   1.207 +    return;
   1.208 +  }
   1.209 +
   1.210 +  IntSize size = image->GetSize();
   1.211 +
   1.212 +  SetEffectTransformAndOpacity();
   1.213 +
   1.214 +  ID3D10EffectTechnique *technique;
   1.215 +  nsRefPtr<IDXGIKeyedMutex> keyedMutex;
   1.216 +
   1.217 +  if (image->GetFormat() == ImageFormat::CAIRO_SURFACE ||
   1.218 +      image->GetFormat() == ImageFormat::REMOTE_IMAGE_BITMAP ||
   1.219 +      image->GetFormat() == ImageFormat::REMOTE_IMAGE_DXGI_TEXTURE ||
   1.220 +      image->GetFormat() == ImageFormat::D3D9_RGB32_TEXTURE) {
   1.221 +    NS_ASSERTION(image->GetFormat() != ImageFormat::CAIRO_SURFACE ||
   1.222 +                 !static_cast<CairoImage*>(image)->mSourceSurface ||
   1.223 +                 static_cast<CairoImage*>(image)->mSourceSurface->GetFormat() != SurfaceFormat::A8,
   1.224 +                 "Image layer has alpha image");
   1.225 +    bool hasAlpha = false;
   1.226 +
   1.227 +    nsRefPtr<ID3D10ShaderResourceView> srView = GetImageSRView(image, hasAlpha, getter_AddRefs(keyedMutex));
   1.228 +    if (!srView) {
   1.229 +      return;
   1.230 +    }
   1.231 +
   1.232 +    uint8_t shaderFlags = SHADER_PREMUL;
   1.233 +    shaderFlags |= LoadMaskTexture();
   1.234 +    shaderFlags |= hasAlpha
   1.235 +                  ? SHADER_RGBA : SHADER_RGB;
   1.236 +    shaderFlags |= mFilter == GraphicsFilter::FILTER_NEAREST
   1.237 +                  ? SHADER_POINT : SHADER_LINEAR;
   1.238 +    technique = SelectShader(shaderFlags);
   1.239 +
   1.240 +
   1.241 +    effect()->GetVariableByName("tRGB")->AsShaderResource()->SetResource(srView);
   1.242 +
   1.243 +    effect()->GetVariableByName("vLayerQuad")->AsVector()->SetFloatVector(
   1.244 +      ShaderConstantRectD3D10(
   1.245 +        (float)0,
   1.246 +        (float)0,
   1.247 +        (float)size.width,
   1.248 +        (float)size.height)
   1.249 +      );
   1.250 +  } else if (image->GetFormat() == ImageFormat::PLANAR_YCBCR) {
   1.251 +    PlanarYCbCrImage *yuvImage =
   1.252 +      static_cast<PlanarYCbCrImage*>(image);
   1.253 +
   1.254 +    if (!yuvImage->IsValid()) {
   1.255 +      return;
   1.256 +    }
   1.257 +
   1.258 +    if (!yuvImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)) {
   1.259 +      AllocateTexturesYCbCr(yuvImage);
   1.260 +    }
   1.261 +
   1.262 +    PlanarYCbCrD3D10BackendData *data =
   1.263 +      static_cast<PlanarYCbCrD3D10BackendData*>(yuvImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10));
   1.264 +
   1.265 +    if (!data) {
   1.266 +      return;
   1.267 +    }
   1.268 +
   1.269 +    nsRefPtr<ID3D10Device> dev;
   1.270 +    data->mYTexture->GetDevice(getter_AddRefs(dev));
   1.271 +    if (dev != device()) {
   1.272 +      return;
   1.273 +    }
   1.274 +
   1.275 +    // TODO: At some point we should try to deal with mFilter here, you don't
   1.276 +    // really want to use point filtering in the case of NEAREST, since that
   1.277 +    // would also use point filtering for Chroma upsampling. Where most likely
   1.278 +    // the user would only want point filtering for final RGB image upsampling.
   1.279 +
   1.280 +    technique = SelectShader(SHADER_YCBCR | LoadMaskTexture());
   1.281 +
   1.282 +    effect()->GetVariableByName("tY")->AsShaderResource()->SetResource(data->mYView);
   1.283 +    effect()->GetVariableByName("tCb")->AsShaderResource()->SetResource(data->mCbView);
   1.284 +    effect()->GetVariableByName("tCr")->AsShaderResource()->SetResource(data->mCrView);
   1.285 +
   1.286 +    /*
   1.287 +     * Send 3d control data and metadata to NV3DVUtils
   1.288 +     */
   1.289 +    if (GetNv3DVUtils()) {
   1.290 +      Nv_Stereo_Mode mode;
   1.291 +      switch (yuvImage->GetData()->mStereoMode) {
   1.292 +      case StereoMode::LEFT_RIGHT:
   1.293 +        mode = NV_STEREO_MODE_LEFT_RIGHT;
   1.294 +        break;
   1.295 +      case StereoMode::RIGHT_LEFT:
   1.296 +        mode = NV_STEREO_MODE_RIGHT_LEFT;
   1.297 +        break;
   1.298 +      case StereoMode::BOTTOM_TOP:
   1.299 +        mode = NV_STEREO_MODE_BOTTOM_TOP;
   1.300 +        break;
   1.301 +      case StereoMode::TOP_BOTTOM:
   1.302 +        mode = NV_STEREO_MODE_TOP_BOTTOM;
   1.303 +        break;
   1.304 +      case StereoMode::MONO:
   1.305 +        mode = NV_STEREO_MODE_MONO;
   1.306 +        break;
   1.307 +      }
   1.308 +
   1.309 +      // Send control data even in mono case so driver knows to leave stereo mode.
   1.310 +      GetNv3DVUtils()->SendNv3DVControl(mode, true, FIREFOX_3DV_APP_HANDLE);
   1.311 +
   1.312 +      if (yuvImage->GetData()->mStereoMode != StereoMode::MONO) {
   1.313 +        // Dst resource is optional
   1.314 +        GetNv3DVUtils()->SendNv3DVMetaData((unsigned int)yuvImage->GetData()->mYSize.width,
   1.315 +                                           (unsigned int)yuvImage->GetData()->mYSize.height, (HANDLE)(data->mYTexture), (HANDLE)(nullptr));
   1.316 +      }
   1.317 +    }
   1.318 +
   1.319 +    effect()->GetVariableByName("vLayerQuad")->AsVector()->SetFloatVector(
   1.320 +      ShaderConstantRectD3D10(
   1.321 +        (float)0,
   1.322 +        (float)0,
   1.323 +        (float)size.width,
   1.324 +        (float)size.height)
   1.325 +      );
   1.326 +
   1.327 +    effect()->GetVariableByName("vTextureCoords")->AsVector()->SetFloatVector(
   1.328 +      ShaderConstantRectD3D10(
   1.329 +        (float)yuvImage->GetData()->mPicX / yuvImage->GetData()->mYSize.width,
   1.330 +        (float)yuvImage->GetData()->mPicY / yuvImage->GetData()->mYSize.height,
   1.331 +        (float)yuvImage->GetData()->mPicSize.width / yuvImage->GetData()->mYSize.width,
   1.332 +        (float)yuvImage->GetData()->mPicSize.height / yuvImage->GetData()->mYSize.height)
   1.333 +       );
   1.334 +  }
   1.335 +
   1.336 +  bool resetTexCoords = image->GetFormat() == ImageFormat::PLANAR_YCBCR;
   1.337 +  image = nullptr;
   1.338 +  autoLock.Unlock();
   1.339 +
   1.340 +  technique->GetPassByIndex(0)->Apply(0);
   1.341 +  device()->Draw(4, 0);
   1.342 +
   1.343 +  if (keyedMutex) {
   1.344 +    keyedMutex->ReleaseSync(0);
   1.345 +  }
   1.346 +
   1.347 +  if (resetTexCoords) {
   1.348 +    effect()->GetVariableByName("vTextureCoords")->AsVector()->
   1.349 +      SetFloatVector(ShaderConstantRectD3D10(0, 0, 1.0f, 1.0f));
   1.350 +  }
   1.351 +
   1.352 +  GetContainer()->NotifyPaintedImage(image);
   1.353 +}
   1.354 +
   1.355 +void ImageLayerD3D10::AllocateTexturesYCbCr(PlanarYCbCrImage *aImage)
   1.356 +{
   1.357 +  nsAutoPtr<PlanarYCbCrD3D10BackendData> backendData(
   1.358 +    new PlanarYCbCrD3D10BackendData);
   1.359 +
   1.360 +  const PlanarYCbCrData *data = aImage->GetData();
   1.361 +
   1.362 +  D3D10_SUBRESOURCE_DATA dataY;
   1.363 +  D3D10_SUBRESOURCE_DATA dataCb;
   1.364 +  D3D10_SUBRESOURCE_DATA dataCr;
   1.365 +  CD3D10_TEXTURE2D_DESC descY(DXGI_FORMAT_A8_UNORM,
   1.366 +                              data->mYSize.width,
   1.367 +                              data->mYSize.height, 1, 1);
   1.368 +  CD3D10_TEXTURE2D_DESC descCbCr(DXGI_FORMAT_A8_UNORM,
   1.369 +                                 data->mCbCrSize.width,
   1.370 +                                 data->mCbCrSize.height, 1, 1);
   1.371 +
   1.372 +  descY.Usage = descCbCr.Usage = D3D10_USAGE_IMMUTABLE;
   1.373 +
   1.374 +  dataY.pSysMem = data->mYChannel;
   1.375 +  dataY.SysMemPitch = data->mYStride;
   1.376 +  dataCb.pSysMem = data->mCbChannel;
   1.377 +  dataCb.SysMemPitch = data->mCbCrStride;
   1.378 +  dataCr.pSysMem = data->mCrChannel;
   1.379 +  dataCr.SysMemPitch = data->mCbCrStride;
   1.380 +
   1.381 +  HRESULT hr = device()->CreateTexture2D(&descY, &dataY, getter_AddRefs(backendData->mYTexture));
   1.382 +  if (!FAILED(hr)) {
   1.383 +      hr = device()->CreateTexture2D(&descCbCr, &dataCb, getter_AddRefs(backendData->mCbTexture));
   1.384 +  }
   1.385 +  if (!FAILED(hr)) {
   1.386 +      hr = device()->CreateTexture2D(&descCbCr, &dataCr, getter_AddRefs(backendData->mCrTexture));
   1.387 +  }
   1.388 +  if (FAILED(hr)) {
   1.389 +    LayerManagerD3D10::ReportFailure(NS_LITERAL_CSTRING("PlanarYCbCrImageD3D10::AllocateTextures(): Failed to create texture"),
   1.390 +                                     hr);
   1.391 +    return;
   1.392 +  }
   1.393 +  device()->CreateShaderResourceView(backendData->mYTexture, nullptr, getter_AddRefs(backendData->mYView));
   1.394 +  device()->CreateShaderResourceView(backendData->mCbTexture, nullptr, getter_AddRefs(backendData->mCbView));
   1.395 +  device()->CreateShaderResourceView(backendData->mCrTexture, nullptr, getter_AddRefs(backendData->mCrView));
   1.396 +
   1.397 +  aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10, backendData.forget());
   1.398 +}
   1.399 +
   1.400 +already_AddRefed<ID3D10ShaderResourceView>
   1.401 +ImageLayerD3D10::GetAsTexture(gfx::IntSize* aSize)
   1.402 +{
   1.403 +  if (!GetContainer()) {
   1.404 +    return nullptr;
   1.405 +  }
   1.406 +
   1.407 +  AutoLockImage autoLock(GetContainer());
   1.408 +
   1.409 +  Image *image = autoLock.GetImage();
   1.410 +  if (!image) {
   1.411 +    return nullptr;
   1.412 +  }
   1.413 +
   1.414 +  if (image->GetFormat() != ImageFormat::CAIRO_SURFACE) {
   1.415 +    return nullptr;
   1.416 +  }
   1.417 +
   1.418 +  *aSize = image->GetSize();
   1.419 +  bool dontCare;
   1.420 +  nsRefPtr<ID3D10ShaderResourceView> result = GetImageSRView(image, dontCare);
   1.421 +  return result.forget();
   1.422 +}
   1.423 +
   1.424 +TemporaryRef<gfx::SourceSurface>
   1.425 +RemoteDXGITextureImage::GetAsSourceSurface()
   1.426 +{
   1.427 +  nsRefPtr<ID3D10Device1> device =
   1.428 +    gfxWindowsPlatform::GetPlatform()->GetD3D10Device();
   1.429 +  if (!device) {
   1.430 +    NS_WARNING("Cannot readback from shared texture because no D3D10 device is available.");
   1.431 +    return nullptr;
   1.432 +  }
   1.433 +
   1.434 +  TextureD3D10BackendData* data = GetD3D10TextureBackendData(device);
   1.435 +
   1.436 +  if (!data) {
   1.437 +    return nullptr;
   1.438 +  }
   1.439 +
   1.440 +  nsRefPtr<IDXGIKeyedMutex> keyedMutex;
   1.441 +
   1.442 +  if (FAILED(data->mTexture->QueryInterface(IID_IDXGIKeyedMutex, getter_AddRefs(keyedMutex)))) {
   1.443 +    NS_WARNING("Failed to QueryInterface for IDXGIKeyedMutex, strange.");
   1.444 +    return nullptr;
   1.445 +  }
   1.446 +
   1.447 +  if (FAILED(keyedMutex->AcquireSync(0, 0))) {
   1.448 +    NS_WARNING("Failed to acquire sync for keyedMutex, plugin failed to release?");
   1.449 +    return nullptr;
   1.450 +  }
   1.451 +
   1.452 +  D3D10_TEXTURE2D_DESC desc;
   1.453 +
   1.454 +  data->mTexture->GetDesc(&desc);
   1.455 +
   1.456 +  desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ;
   1.457 +  desc.BindFlags = 0;
   1.458 +  desc.MiscFlags = 0;
   1.459 +  desc.Usage = D3D10_USAGE_STAGING;
   1.460 +
   1.461 +  nsRefPtr<ID3D10Texture2D> softTexture;
   1.462 +  HRESULT hr = device->CreateTexture2D(&desc, nullptr, getter_AddRefs(softTexture));
   1.463 +
   1.464 +  if (FAILED(hr)) {
   1.465 +    NS_WARNING("Failed to create 2D staging texture.");
   1.466 +    return nullptr;
   1.467 +  }
   1.468 +
   1.469 +  device->CopyResource(softTexture, data->mTexture);
   1.470 +  keyedMutex->ReleaseSync(0);
   1.471 +
   1.472 +  RefPtr<gfx::DataSourceSurface> surface
   1.473 +    = gfx::Factory::CreateDataSourceSurface(mSize,
   1.474 +                                            mFormat == RemoteImageData::BGRX32
   1.475 +                                              ? gfx::SurfaceFormat::B8G8R8X8
   1.476 +                                              : gfx::SurfaceFormat::B8G8R8A8);
   1.477 +
   1.478 +  if (!surface) {
   1.479 +    NS_WARNING("Failed to create SourceSurface for DXGI texture.");
   1.480 +    return nullptr;
   1.481 +  }
   1.482 +
   1.483 +  gfx::DataSourceSurface::MappedSurface mappedSurface;
   1.484 +  if (!surface->Map(gfx::DataSourceSurface::WRITE, &mappedSurface)) {
   1.485 +    NS_WARNING("Failed to map source surface");
   1.486 +    return nullptr;
   1.487 +  }
   1.488 +
   1.489 +  D3D10_MAPPED_TEXTURE2D mapped;
   1.490 +  softTexture->Map(0, D3D10_MAP_READ, 0, &mapped);
   1.491 +
   1.492 +  for (int y = 0; y < mSize.height; y++) {
   1.493 +    memcpy(mappedSurface.mData + mappedSurface.mStride * y,
   1.494 +           (unsigned char*)(mapped.pData) + mapped.RowPitch * y,
   1.495 +           mSize.width * 4);
   1.496 +  }
   1.497 +
   1.498 +  softTexture->Unmap(0);
   1.499 +  surface->Unmap();
   1.500 +
   1.501 +  return surface;
   1.502 +}
   1.503 +
   1.504 +TextureD3D10BackendData*
   1.505 +RemoteDXGITextureImage::GetD3D10TextureBackendData(ID3D10Device *aDevice)
   1.506 +{
   1.507 +  if (GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10)) {
   1.508 +    TextureD3D10BackendData *data =
   1.509 +      static_cast<TextureD3D10BackendData*>(GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10));
   1.510 +
   1.511 +    nsRefPtr<ID3D10Device> device;
   1.512 +    data->mTexture->GetDevice(getter_AddRefs(device));
   1.513 +
   1.514 +    if (device == aDevice) {
   1.515 +      return data;
   1.516 +    }
   1.517 +  }
   1.518 +  nsRefPtr<ID3D10Texture2D> texture;
   1.519 +  aDevice->OpenSharedResource(mHandle, __uuidof(ID3D10Texture2D), getter_AddRefs(texture));
   1.520 +
   1.521 +  if (!texture) {
   1.522 +    NS_WARNING("Failed to get texture for shared texture handle.");
   1.523 +    return nullptr;
   1.524 +  }
   1.525 +
   1.526 +  nsAutoPtr<TextureD3D10BackendData> data(new TextureD3D10BackendData());
   1.527 +
   1.528 +  data->mTexture = texture;
   1.529 +
   1.530 +  aDevice->CreateShaderResourceView(texture, nullptr, getter_AddRefs(data->mSRView));
   1.531 +
   1.532 +  SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D10, data);
   1.533 +
   1.534 +  return data.forget();
   1.535 +}
   1.536 +
   1.537 +} /* layers */
   1.538 +} /* mozilla */

mercurial