Bug 1200595 - D3D9 TextureData implementation. r=Bas
authorNicolas Silva <nsilva@mozilla.com>
Mon, 19 Oct 2015 14:46:17 +0200
changeset 308847 4b2a5d9c0ccd4aa98a18c6d245019feabf357ecd
parent 308846 981a6bc9b1aea682f912099fb57257640e8f9359
child 308848 d296b66867926f465c23e2c8e14bccede063cc5d
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersBas
bugs1200595
milestone45.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1200595 - D3D9 TextureData implementation. r=Bas
gfx/layers/D3D9SurfaceImage.cpp
gfx/layers/D3D9SurfaceImage.h
gfx/layers/client/TextureClient.cpp
gfx/layers/d3d9/TextureD3D9.cpp
gfx/layers/d3d9/TextureD3D9.h
--- a/gfx/layers/D3D9SurfaceImage.cpp
+++ b/gfx/layers/D3D9SurfaceImage.cpp
@@ -51,24 +51,25 @@ D3D9SurfaceImage::AllocateAndCopy(D3D9Re
                                          D3DDEVTYPE_HAL,
                                          desc.Format,
                                          D3DFMT_X8R8G8B8);
   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.
-  RefPtr<SharedTextureClientD3D9> textureClient =
+  RefPtr<TextureClient> textureClient =
     aAllocator->CreateOrRecycleClient(gfx::SurfaceFormat::B8G8R8X8, aRegion.Size());
   if (!textureClient) {
     return E_FAIL;
   }
 
   // Copy the image onto the texture, preforming YUV -> RGB conversion if necessary.
-  RefPtr<IDirect3DSurface9> textureSurface = textureClient->GetD3D9Surface();
+  RefPtr<IDirect3DSurface9> textureSurface = static_cast<DXGID3D9TextureData*>(
+    textureClient->GetInternalData())->GetD3D9Surface();
   if (!textureSurface) {
     return E_FAIL;
   }
 
   RECT src = { aRegion.x, aRegion.y, aRegion.x+aRegion.width, aRegion.y+aRegion.height };
   hr = device->StretchRect(surface, &src, textureSurface, nullptr, D3DTEXF_NONE);
   NS_ENSURE_TRUE(SUCCEEDED(hr), hr);
 
@@ -116,17 +117,17 @@ D3D9SurfaceImage::EnsureSynchronized()
     break;
   }
   mQuery = nullptr;
 }
 
 const D3DSURFACE_DESC&
 D3D9SurfaceImage::GetDesc() const
 {
-  return mTextureClient->GetDesc();
+  return static_cast<DXGID3D9TextureData*>(mTextureClient->GetInternalData())->GetDesc();
 }
 
 gfx::IntSize
 D3D9SurfaceImage::GetSize()
 {
   return mSize;
 }
 
@@ -137,35 +138,38 @@ D3D9SurfaceImage::GetTextureClient(Compo
   MOZ_ASSERT(mTextureClient->GetAllocator() == aClient->GetForwarder());
   EnsureSynchronized();
   return mTextureClient;
 }
 
 already_AddRefed<gfx::SourceSurface>
 D3D9SurfaceImage::GetAsSourceSurface()
 {
-  NS_ENSURE_TRUE(mTextureClient, nullptr);
+  if (!mTextureClient) {
+    return nullptr;
+  }
 
   HRESULT hr;
   RefPtr<gfx::DataSourceSurface> surface = gfx::Factory::CreateDataSourceSurface(mSize, gfx::SurfaceFormat::B8G8R8X8);
   if (NS_WARN_IF(!surface)) {
     return nullptr;
   }
 
   // Ensure that the texture is ready to be used.
   EnsureSynchronized();
 
+  DXGID3D9TextureData* texData = static_cast<DXGID3D9TextureData*>(mTextureClient->GetInternalData());
   // 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 = mTextureClient->GetD3D9Surface();
+  RefPtr<IDirect3DSurface9> textureSurface = texData->GetD3D9Surface();
   if (!textureSurface) {
     return nullptr;
   }
 
-  RefPtr<IDirect3DDevice9> device = mTextureClient->GetD3D9Device();
+  RefPtr<IDirect3DDevice9> device = texData->GetD3D9Device();
   if (!device) {
     return nullptr;
   }
 
   RefPtr<IDirect3DSurface9> systemMemorySurface;
   hr = device->CreateOffscreenPlainSurface(mSize.width,
                                            mSize.height,
                                            D3DFMT_X8R8G8B8,
@@ -203,34 +207,26 @@ D3D9SurfaceImage::GetAsSourceSurface()
 
 already_AddRefed<TextureClient>
 D3D9RecycleAllocator::Allocate(gfx::SurfaceFormat aFormat,
                                gfx::IntSize aSize,
                                BackendSelector aSelector,
                                TextureFlags aTextureFlags,
                                TextureAllocationFlags aAllocFlags)
 {
-  return SharedTextureClientD3D9::Create(mSurfaceAllocator,
-                                         aFormat,
-                                         aTextureFlags,
-                                         mDevice,
-                                         aSize);
+  TextureData* data = DXGID3D9TextureData::Create(aSize, aFormat, aTextureFlags, mDevice);
+  if (!data) {
+    return nullptr;
+  }
+
+  return MakeAndAddRef<ClientTexture>(data, aTextureFlags, mSurfaceAllocator);
 }
 
-already_AddRefed<SharedTextureClientD3D9>
+already_AddRefed<TextureClient>
 D3D9RecycleAllocator::CreateOrRecycleClient(gfx::SurfaceFormat aFormat,
                                             const gfx::IntSize& aSize)
 {
-  RefPtr<TextureClient> textureClient =
-    CreateOrRecycle(aFormat,
-                    aSize,
-                    BackendSelector::Content,
-                    layers::TextureFlags::DEFAULT);
-  if (!textureClient) {
-    return nullptr;
-  }
-
-  RefPtr<SharedTextureClientD3D9> textureD3D9 = static_cast<SharedTextureClientD3D9*>(textureClient.get());
-  return textureD3D9.forget();
+  return CreateOrRecycle(aFormat, aSize, BackendSelector::Content,
+                         TextureFlags::DEFAULT);
 }
 
 } // namespace layers
 } // namespace mozilla
--- a/gfx/layers/D3D9SurfaceImage.h
+++ b/gfx/layers/D3D9SurfaceImage.h
@@ -10,28 +10,28 @@
 #include "ImageContainer.h"
 #include "nsAutoPtr.h"
 #include "d3d9.h"
 #include "mozilla/layers/TextureClientRecycleAllocator.h"
 
 namespace mozilla {
 namespace layers {
 
-class SharedTextureClientD3D9;
+class TextureClient;
 
 class D3D9RecycleAllocator : public TextureClientRecycleAllocator
 {
 public:
   explicit D3D9RecycleAllocator(CompositableForwarder* aAllocator,
                                 IDirect3DDevice9* aDevice)
     : TextureClientRecycleAllocator(aAllocator)
     , mDevice(aDevice)
   {}
 
-  already_AddRefed<SharedTextureClientD3D9>
+  already_AddRefed<TextureClient>
   CreateOrRecycleClient(gfx::SurfaceFormat aFormat,
                         const gfx::IntSize& aSize);
 
 protected:
   virtual already_AddRefed<TextureClient>
   Allocate(gfx::SurfaceFormat aFormat,
            gfx::IntSize aSize,
            BackendSelector aSelector,
@@ -68,17 +68,17 @@ public:
 private:
 
   // Blocks the calling thread until the copy operation started in SetData()
   // is complete, whereupon the texture is safe to use.
   void EnsureSynchronized();
 
   gfx::IntSize mSize;
   RefPtr<IDirect3DQuery9> mQuery;
-  RefPtr<SharedTextureClientD3D9> mTextureClient;
+  RefPtr<TextureClient> mTextureClient;
   bool mValid;
   bool mIsFirstFrame;
 };
 
 } // namepace layers
 } // namespace mozilla
 
 #endif // GFX_D3DSURFACEIMAGE_H
--- a/gfx/layers/client/TextureClient.cpp
+++ b/gfx/layers/client/TextureClient.cpp
@@ -577,17 +577,20 @@ TextureClient::CreateForDrawing(Composit
   }
   if (parentBackend == LayersBackend::LAYERS_D3D9 &&
       moz2DBackend == gfx::BackendType::CAIRO &&
       aAllocator->IsSameProcess() &&
       aSize.width <= maxTextureSize &&
       aSize.height <= maxTextureSize &&
       NS_IsMainThread()) {
     if (gfxWindowsPlatform::GetPlatform()->GetD3D9Device()) {
-      texture = new TextureClientD3D9(aAllocator, aFormat, aTextureFlags);
+      TextureData* data = D3D9TextureData::Create(aSize, aFormat, aAllocFlags);
+      if (data) {
+        return MakeAndAddRef<ClientTexture>(data, aTextureFlags, aAllocator);
+      }
     }
   }
 
   if (!texture && aFormat == SurfaceFormat::B8G8R8X8 &&
       aAllocator->IsSameProcess() &&
       moz2DBackend == gfx::BackendType::CAIRO &&
       NS_IsMainThread()) {
     if (aAllocator->IsSameProcess()) {
--- a/gfx/layers/d3d9/TextureD3D9.cpp
+++ b/gfx/layers/d3d9/TextureD3D9.cpp
@@ -437,331 +437,267 @@ DataTextureSourceD3D9::GetTileRect(uint3
 }
 
 IntRect
 DataTextureSourceD3D9::GetTileRect()
 {
   return GetTileRect(mCurrentTile);
 }
 
-TextureClientD3D9::TextureClientD3D9(ISurfaceAllocator* aAllocator,
-                                     gfx::SurfaceFormat aFormat,
-                                     TextureFlags aFlags)
-  : TextureClient(aAllocator, aFlags)
-  , mFormat(aFormat)
-  , mIsLocked(false)
-  , mNeedsClear(false)
-  , mNeedsClearWhite(false)
-  , mLockRect(false)
+
+D3D9TextureData::D3D9TextureData(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
+                                 IDirect3DTexture9* aTexture)
+: mTexture(aTexture)
+, mSize(aSize)
+, mFormat(aFormat)
+, mNeedsClear(false)
+, mNeedsClearWhite(false)
 {
-  MOZ_COUNT_CTOR(TextureClientD3D9);
+  MOZ_COUNT_CTOR(D3D9TextureData);
+}
+
+D3D9TextureData::~D3D9TextureData()
+{
+  MOZ_COUNT_DTOR(D3D9TextureData);
 }
 
-TextureClientD3D9::~TextureClientD3D9()
+D3D9TextureData*
+D3D9TextureData::Create(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
+                        TextureAllocationFlags aAllocFlags)
 {
-  MOZ_COUNT_DTOR(TextureClientD3D9);
+  _D3DFORMAT format = SurfaceFormatToD3D9Format(aFormat);
+  DeviceManagerD3D9* deviceManager = gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager();
+  RefPtr<IDirect3DTexture9> d3d9Texture = deviceManager ? deviceManager->CreateTexture(aSize, format,
+                                                                                       D3DPOOL_SYSTEMMEM,
+                                                                                       nullptr)
+                                                        : nullptr;
+  if (!d3d9Texture) {
+    NS_WARNING("Could not create a d3d9 texture");
+    return nullptr;
+  }
+  D3D9TextureData* data = new D3D9TextureData(aSize, aFormat, d3d9Texture);
+
+  data->mNeedsClear = aAllocFlags & ALLOC_CLEAR_BUFFER;
+  data->mNeedsClearWhite = aAllocFlags & ALLOC_CLEAR_BUFFER_WHITE;
+
+  return data;
 }
 
-already_AddRefed<TextureClient>
-TextureClientD3D9::CreateSimilar(TextureFlags aFlags, TextureAllocationFlags aAllocFlags) const
+TextureData*
+D3D9TextureData::CreateSimilar(ISurfaceAllocator*, TextureFlags aFlags, TextureAllocationFlags aAllocFlags) const
 {
-  RefPtr<TextureClient> tex = new TextureClientD3D9(mAllocator, mFormat,
-                                                    mFlags | aFlags);
+  return D3D9TextureData::Create(mSize, mFormat, aAllocFlags);
+}
 
-  if (!tex->AllocateForSurface(mSize, aAllocFlags)) {
-    return nullptr;
+void
+D3D9TextureData::FinalizeOnIPDLThread(TextureClient* aWrapper)
+{
+  if (mTexture) {
+    aWrapper->KeepUntilFullDeallocation(MakeUnique<TKeepAlive<IDirect3DTexture9>>(mTexture));
   }
-
-  return tex.forget();
 }
 
 bool
-TextureClientD3D9::Lock(OpenMode aMode)
+D3D9TextureData::Lock(OpenMode aMode, FenceHandle*)
 {
-  MOZ_ASSERT(!mIsLocked);
-  if (!IsValid() || !IsAllocated()) {
-    return false;
-  }
-
   if (!gfxWindowsPlatform::GetPlatform()->GetD3D9Device()) {
     // If the device has failed then we should not lock the surface,
     // even if we could.
     mD3D9Surface = nullptr;
     return false;
   }
 
   if (!mD3D9Surface) {
     HRESULT hr = mTexture->GetSurfaceLevel(0, getter_AddRefs(mD3D9Surface));
     if (FAILED(hr)) {
       NS_WARNING("Failed to get texture surface level.");
       return false;
     }
   }
-
-  mIsLocked = true;
-
   return true;
 }
-
 void
-TextureClientD3D9::Unlock()
+D3D9TextureData::Unlock()
 {
-  MOZ_ASSERT(mIsLocked, "Unlocked called while the texture is not locked!");
-  if (!mIsLocked) {
-    return;
-  }
-
-  if (mDrawTarget) {
-    mDrawTarget->Flush();
-    mDrawTarget = nullptr;
-  }
-
   if (mLockRect) {
     mD3D9Surface->UnlockRect();
     mLockRect = false;
   }
-
-  mIsLocked = false;
 }
 
 bool
-TextureClientD3D9::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor)
+D3D9TextureData::Serialize(SurfaceDescriptor& aOutDescriptor)
 {
-  MOZ_ASSERT(IsValid());
-  if (!IsAllocated()) {
-    return false;
-  }
-
   mTexture->AddRef(); // Release in TextureHostD3D9::TextureHostD3D9
   aOutDescriptor = SurfaceDescriptorD3D9(reinterpret_cast<uintptr_t>(mTexture.get()));
   return true;
 }
 
-gfx::DrawTarget*
-TextureClientD3D9::BorrowDrawTarget()
+already_AddRefed<gfx::DrawTarget>
+D3D9TextureData::BorrowDrawTarget()
 {
-  MOZ_ASSERT(mIsLocked && mD3D9Surface);
-  if (!mIsLocked || !mD3D9Surface) {
-    NS_WARNING("Calling BorrowDrawTarget on an Unlocked TextureClient");
-    gfxCriticalNote << "BorrowDrawTarget on an Unlocked TextureClient";
+  MOZ_ASSERT(mD3D9Surface);
+  if (!mD3D9Surface) {
     return nullptr;
   }
 
-  if (mDrawTarget) {
-    return mDrawTarget;
-  }
-
+  RefPtr<DrawTarget> dt;
   if (ContentForFormat(mFormat) == gfxContentType::COLOR) {
     RefPtr<gfxASurface> surface = new gfxWindowsSurface(mD3D9Surface);
     if (!surface || surface->CairoStatus()) {
-      NS_WARNING("Could not create surface for d3d9 surface");
-      gfxCriticalNote << "Failed creation on D3D9";
+      NS_WARNING("Could not create gfxASurface for d3d9 surface");
       return nullptr;
     }
-    mDrawTarget =
-      gfxPlatform::GetPlatform()->CreateDrawTargetForSurface(surface, mSize);
-    if (!mDrawTarget) {
-      gfxCriticalNote << "Bad draw target creation for surface D3D9 " << mSize;
+    dt = gfxPlatform::GetPlatform()->CreateDrawTargetForSurface(surface, mSize);
+
+    if (!dt) {
+      return nullptr;
     }
   } else {
     // gfxWindowsSurface don't support transparency so we can't use the d3d9
     // windows surface optimization.
     // Instead we have to use a gfxImageSurface and fallback for font drawing.
     D3DLOCKED_RECT rect;
     HRESULT hr = mD3D9Surface->LockRect(&rect, nullptr, 0);
     if (FAILED(hr) || !rect.pBits) {
       gfxCriticalError() << "Failed to lock rect borrowing the target in D3D9 " << hexa(hr);
       return nullptr;
     }
-    mDrawTarget =
-     gfxPlatform::GetPlatform()->CreateDrawTargetForData((uint8_t*)rect.pBits, mSize,
-                                                         rect.Pitch, mFormat);
-    if (!mDrawTarget) {
-      gfxCriticalNote << "Bad draw target creation for data D3D9 " << mSize << ", " << (int)mFormat;
+    dt = gfxPlatform::GetPlatform()->CreateDrawTargetForData((uint8_t*)rect.pBits, mSize,
+                                                             rect.Pitch, mFormat);
+    if (!dt) {
+      return nullptr;
     }
-    mLockRect = true;
+
+     mLockRect = true;
   }
 
   if (mNeedsClear) {
-    mDrawTarget->ClearRect(Rect(0, 0, GetSize().width, GetSize().height));
+    dt->ClearRect(Rect(0, 0, GetSize().width, GetSize().height));
     mNeedsClear = false;
   }
   if (mNeedsClearWhite) {
-    mDrawTarget->FillRect(Rect(0, 0, GetSize().width, GetSize().height), ColorPattern(Color(1.0, 1.0, 1.0, 1.0)));
+    dt->FillRect(Rect(0, 0, GetSize().width, GetSize().height), ColorPattern(Color(1.0, 1.0, 1.0, 1.0)));
     mNeedsClearWhite = false;
   }
 
-  return mDrawTarget;
+  return dt.forget();
 }
 
-void
-TextureClientD3D9::UpdateFromSurface(gfx::SourceSurface* aSurface)
+bool
+D3D9TextureData::UpdateFromSurface(gfx::SourceSurface* aSurface)
 {
-  MOZ_ASSERT(mIsLocked && mD3D9Surface);
+  MOZ_ASSERT(mD3D9Surface);
 
   // gfxWindowsSurface don't support transparency so we can't use the d3d9
   // windows surface optimization.
   // Instead we have to use a gfxImageSurface and fallback for font drawing.
   D3DLOCKED_RECT rect;
   HRESULT hr = mD3D9Surface->LockRect(&rect, nullptr, 0);
   if (FAILED(hr) || !rect.pBits) {
     gfxCriticalError() << "Failed to lock rect borrowing the target in D3D9 " << hexa(hr);
-    return;
+    return false;
   }
 
   RefPtr<DataSourceSurface> srcSurf = aSurface->GetDataSurface();
 
   if (!srcSurf) {
     gfxCriticalError() << "Failed to GetDataSurface in UpdateFromSurface.";
-    return;
-  }
-
-  if (mSize != srcSurf->GetSize() || mFormat != srcSurf->GetFormat()) {
-    gfxCriticalError() << "Attempt to update texture client from a surface with a different size or format! This: " << mSize << " " << mFormat << " Other: " << srcSurf->GetSize() << " " << srcSurf->GetFormat();
-    return;
+    mD3D9Surface->UnlockRect();
+    return false;
   }
 
   DataSourceSurface::MappedSurface sourceMap;
   if (!srcSurf->Map(DataSourceSurface::READ, &sourceMap)) {
     gfxCriticalError() << "Failed to map source surface for UpdateFromSurface.";
-    return;
+    return false;
   }
 
   for (int y = 0; y < srcSurf->GetSize().height; y++) {
     memcpy((uint8_t*)rect.pBits + rect.Pitch * y,
            sourceMap.mData + sourceMap.mStride * y,
            srcSurf->GetSize().width * BytesPerPixel(srcSurf->GetFormat()));
   }
 
   srcSurf->Unmap();
   mD3D9Surface->UnlockRect();
-}
 
-bool
-TextureClientD3D9::AllocateForSurface(gfx::IntSize aSize, TextureAllocationFlags aFlags)
-{
-  MOZ_ASSERT(!IsAllocated());
-  mSize = aSize;
-  _D3DFORMAT format = SurfaceFormatToD3D9Format(mFormat);
-
-  DeviceManagerD3D9* deviceManager = gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager();
-  if (!deviceManager ||
-      !(mTexture = deviceManager->CreateTexture(mSize, format, D3DPOOL_SYSTEMMEM, nullptr))) {
-    NS_WARNING("Could not create d3d9 texture");
-    return false;
-  }
-
-  mNeedsClear = aFlags & ALLOC_CLEAR_BUFFER;
-  mNeedsClearWhite = aFlags & ALLOC_CLEAR_BUFFER_WHITE;
-
-  MOZ_ASSERT(mTexture);
   return true;
 }
 
-SharedTextureClientD3D9::SharedTextureClientD3D9(ISurfaceAllocator* aAllocator,
-                                                 gfx::SurfaceFormat aFormat,
-                                                 TextureFlags aFlags)
-  : TextureClient(aAllocator, aFlags)
-  , mFormat(aFormat)
-  , mHandle(0)
-  , mIsLocked(false)
+DXGID3D9TextureData::DXGID3D9TextureData(gfx::SurfaceFormat aFormat,
+                                         IDirect3DTexture9* aTexture, HANDLE aHandle,
+                                         IDirect3DDevice9* aDevice)
+: mFormat(aFormat)
+, mTexture(aTexture)
+, mHandle(aHandle)
+, mDevice(aDevice)
 {
-  MOZ_COUNT_CTOR(SharedTextureClientD3D9);
+  MOZ_COUNT_CTOR(DXGID3D9TextureData);
 }
 
-SharedTextureClientD3D9::~SharedTextureClientD3D9()
+DXGID3D9TextureData::~DXGID3D9TextureData()
 {
-  MOZ_COUNT_DTOR(SharedTextureClientD3D9);
-  if (mTexture) {
-    gfxWindowsPlatform::sD3D9SharedTextureUsed -= mDesc.Width * mDesc.Height * 4;
-  }
-}
-
-void
-SharedTextureClientD3D9::FinalizeOnIPDLThread()
-{
-  if (mTexture && mActor) {
-    KeepUntilFullDeallocation(MakeUnique<TKeepAlive<IDirect3DTexture9>>(mTexture));
-  }
+  gfxWindowsPlatform::sD3D9SharedTextureUsed -= mDesc.Width * mDesc.Height * 4;
+  MOZ_COUNT_DTOR(DXGID3D9TextureData);
 }
 
 // static
-already_AddRefed<SharedTextureClientD3D9>
-SharedTextureClientD3D9::Create(ISurfaceAllocator* aAllocator,
-                                gfx::SurfaceFormat aFormat,
-                                TextureFlags aFlags,
-                                IDirect3DDevice9* aDevice,
-                                const gfx::IntSize& aSize)
+DXGID3D9TextureData*
+DXGID3D9TextureData::Create(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
+                            TextureFlags aFlags,
+                            IDirect3DDevice9* aDevice)
 {
   MOZ_ASSERT(aFormat == gfx::SurfaceFormat::B8G8R8X8);
+  if (aFormat != gfx::SurfaceFormat::B8G8R8X8) {
+    return nullptr;
+  }
 
   RefPtr<IDirect3DTexture9> texture;
   HANDLE shareHandle = nullptr;
-  HRESULT hr = aDevice->CreateTexture(aSize.width,
-                                      aSize.height,
+  HRESULT hr = aDevice->CreateTexture(aSize.width, aSize.height,
                                       1,
                                       D3DUSAGE_RENDERTARGET,
                                       D3DFMT_X8R8G8B8,
                                       D3DPOOL_DEFAULT,
                                       getter_AddRefs(texture),
                                       &shareHandle);
-  NS_ENSURE_TRUE(SUCCEEDED(hr) && shareHandle, nullptr);
+  if (FAILED(hr) || !shareHandle) {
+    return nullptr;
+  }
 
-  RefPtr<SharedTextureClientD3D9> client =
-    new SharedTextureClientD3D9(aAllocator,
-                                aFormat,
-                                aFlags);
-
-  client->mDevice = aDevice;
-  client->mTexture = texture;
-  client->mHandle = shareHandle;
-  texture->GetLevelDesc(0, &client->mDesc);
+  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::sD3D9SharedTextureUsed += aSize.width * aSize.height * 4;
-  return client.forget();
+  return data;
 }
 
 already_AddRefed<IDirect3DSurface9>
-SharedTextureClientD3D9::GetD3D9Surface() const
+DXGID3D9TextureData::GetD3D9Surface() const
 {
   RefPtr<IDirect3DSurface9> textureSurface;
   HRESULT hr = mTexture->GetSurfaceLevel(0, getter_AddRefs(textureSurface));
   NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
 
   return textureSurface.forget();
 }
 
 bool
-SharedTextureClientD3D9::Lock(OpenMode)
+DXGID3D9TextureData::Serialize(SurfaceDescriptor& aOutDescriptor)
 {
-  MOZ_ASSERT(!mIsLocked);
-  if (!IsValid()) {
-    return false;
-  }
-  mIsLocked = true;
+  aOutDescriptor = SurfaceDescriptorD3D10((WindowsHandle)(mHandle), mFormat, GetSize());
   return true;
 }
 
-void
-SharedTextureClientD3D9::Unlock()
-{
-  MOZ_ASSERT(mIsLocked, "Unlock called while the texture is not locked!");
-}
-
-bool
-SharedTextureClientD3D9::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor)
-{
-  MOZ_ASSERT(IsValid());
-  if (!IsAllocated()) {
-    return false;
-  }
-
-  aOutDescriptor = SurfaceDescriptorD3D10((WindowsHandle)(mHandle), mFormat, GetSize());
-  return true;
-}
 
 TextureHostD3D9::TextureHostD3D9(TextureFlags aFlags,
                                  const SurfaceDescriptorD3D9& aDescriptor)
   : TextureHost(aFlags)
   , mFormat(SurfaceFormat::UNKNOWN)
   , mIsLocked(false)
 {
   mTexture = reinterpret_cast<IDirect3DTexture9*>(aDescriptor.texture());
--- a/gfx/layers/d3d9/TextureD3D9.h
+++ b/gfx/layers/d3d9/TextureD3D9.h
@@ -5,16 +5,17 @@
 
 #ifndef MOZILLA_GFX_TEXTURED3D9_H
 #define MOZILLA_GFX_TEXTURED3D9_H
 
 #include "mozilla/layers/Compositor.h"
 #include "mozilla/layers/TextureClient.h"
 #include "mozilla/layers/TextureHost.h"
 #include "mozilla/GfxMessageUtils.h"
+#include "mozilla/gfx/2D.h"
 #include "gfxWindowsPlatform.h"
 #include "d3d9.h"
 #include <vector>
 #include "DeviceManagerD3D9.h"
 
 namespace mozilla {
 namespace gfxs {
 class DrawTarget;
@@ -169,135 +170,111 @@ protected:
   bool mIsTiled;
   bool mIterating;
 };
 
 /**
  * Needs a D3D9 context on the client side.
  * The corresponding TextureHost is TextureHostD3D9.
  */
-class TextureClientD3D9 : public TextureClient
+class D3D9TextureData : public TextureData
 {
 public:
-  TextureClientD3D9(ISurfaceAllocator* aAllocator, gfx::SurfaceFormat aFormat,
-                         TextureFlags aFlags);
+  ~D3D9TextureData();
 
-  virtual ~TextureClientD3D9();
+  virtual bool SupportsMoz2D() const override { return true; }
 
-  // TextureClient
+  virtual bool HasInternalBuffer() const override { return true; }
 
-  virtual bool IsAllocated() const override { return !!mTexture; }
+  virtual bool Serialize(SurfaceDescriptor& aOutDescrptor) override;
 
-  virtual bool Lock(OpenMode aOpenMode) override;
+  virtual bool Lock(OpenMode aMode, FenceHandle*) override;
 
   virtual void Unlock() override;
 
-  virtual bool IsLocked() const override { return mIsLocked; }
+  virtual already_AddRefed<gfx::DrawTarget> BorrowDrawTarget() override;
 
-  virtual bool ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor) override;
-
-  virtual gfx::IntSize GetSize() const { return mSize; }
+  virtual gfx::SurfaceFormat GetFormat() const override { return mFormat; }
 
-  virtual gfx::SurfaceFormat GetFormat() const { return mFormat; }
+  virtual gfx::IntSize GetSize() const override { return mSize; }
 
-  virtual bool CanExposeDrawTarget() const override { return true; }
-
-  virtual gfx::DrawTarget* BorrowDrawTarget() override;
+  virtual bool UpdateFromSurface(gfx::SourceSurface* aSurface) override;
 
-  virtual void UpdateFromSurface(gfx::SourceSurface* aSurface) override;
+  virtual TextureData*
+  CreateSimilar(ISurfaceAllocator* aAllocator,
+                TextureFlags aFlags,
+                TextureAllocationFlags aAllocFlags) const override;
 
-  virtual bool AllocateForSurface(gfx::IntSize aSize,
-                                  TextureAllocationFlags aFlags = ALLOC_DEFAULT) override;
+  static D3D9TextureData*
+  Create(gfx::IntSize aSize, gfx::SurfaceFormat aFormat, TextureAllocationFlags aFlags);
 
-  virtual bool HasInternalBuffer() const override { return true; }
+  virtual void Deallocate(ISurfaceAllocator* aAllocator) {}
 
-  virtual already_AddRefed<TextureClient>
-  CreateSimilar(TextureFlags aFlags = TextureFlags::DEFAULT,
-                TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT) const override;
+protected:
+  virtual void FinalizeOnIPDLThread(TextureClient* aWrapper) override;
 
-private:
+  D3D9TextureData(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
+                  IDirect3DTexture9* aTexture);
+
   RefPtr<IDirect3DTexture9> mTexture;
   RefPtr<IDirect3DSurface9> mD3D9Surface;
-  RefPtr<gfx::DrawTarget> mDrawTarget;
   gfx::IntSize mSize;
   gfx::SurfaceFormat mFormat;
-  bool mIsLocked;
   bool mNeedsClear;
   bool mNeedsClearWhite;
   bool mLockRect;
 };
 
 /**
  * Wraps a D3D9 texture, shared with the compositor though DXGI.
  * At the moment it is only used with D3D11 compositing, and the corresponding
  * TextureHost is DXGITextureHostD3D11.
  */
-class SharedTextureClientD3D9 : public TextureClient
+class DXGID3D9TextureData : public TextureData
 {
 public:
-  SharedTextureClientD3D9(ISurfaceAllocator* aAllocator,
-                          gfx::SurfaceFormat aFormat,
-                          TextureFlags aFlags);
-
-  virtual ~SharedTextureClientD3D9();
+  static DXGID3D9TextureData*
+  Create(gfx::IntSize aSize, gfx::SurfaceFormat aFormat, TextureFlags aFlags, IDirect3DDevice9* aDevice);
 
-  // Creates a TextureClient and init width.
-  static already_AddRefed<SharedTextureClientD3D9>
-  Create(ISurfaceAllocator* aAllocator,
-         gfx::SurfaceFormat aFormat,
-         TextureFlags aFlags,
-         IDirect3DDevice9* aDevice,
-         const gfx::IntSize& aSize);
+  ~DXGID3D9TextureData();
 
-  // TextureClient
-
-  virtual bool IsAllocated() const override { return !!mTexture; }
+  virtual gfx::IntSize GetSize() const { return gfx::IntSize(mDesc.Width, mDesc.Height); }
 
   virtual gfx::SurfaceFormat GetFormat() const override { return mFormat; }
 
-  virtual bool Lock(OpenMode aOpenMode) override;
-
-  virtual void Unlock() override;
+  virtual bool Lock(OpenMode, FenceHandle*) override { return true; }
 
-  virtual bool IsLocked() const override { return mIsLocked; }
-
-  virtual bool ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor) override;
+  virtual void Unlock() override {}
 
-  virtual gfx::IntSize GetSize() const
-  {
-    return gfx::IntSize(mDesc.Width, mDesc.Height);
-  }
+  virtual bool Serialize(SurfaceDescriptor& aOutDescriptor) override;
 
-  virtual bool HasInternalBuffer() const override { return true; }
+  virtual bool HasInternalBuffer() const override { return false; }
 
-  // This TextureClient should not be used in a context where we use CreateSimilar
-  // (ex. component alpha) because the underlying texture data is always created by
-  // an external producer.
-  virtual already_AddRefed<TextureClient>
-  CreateSimilar(TextureFlags, TextureAllocationFlags) const override { return nullptr; }
+  virtual void Deallocate(ISurfaceAllocator* aAllocator) {}
 
   IDirect3DDevice9* GetD3D9Device() { return mDevice; }
   IDirect3DTexture9* GetD3D9Texture() { return mTexture; }
   HANDLE GetShareHandle() const { return mHandle; }
   already_AddRefed<IDirect3DSurface9> GetD3D9Surface() const;
 
   const D3DSURFACE_DESC& GetDesc() const
   {
     return mDesc;
   }
 
-private:
-  virtual void FinalizeOnIPDLThread() override;
+protected:
+  DXGID3D9TextureData(gfx::SurfaceFormat aFormat,
+                      IDirect3DTexture9* aTexture, HANDLE aHandle,
+                      IDirect3DDevice9* aDevice);
 
   RefPtr<IDirect3DDevice9> mDevice;
   RefPtr<IDirect3DTexture9> mTexture;
   gfx::SurfaceFormat mFormat;
   HANDLE mHandle;
   D3DSURFACE_DESC mDesc;
-  bool mIsLocked;
 };
 
 class TextureHostD3D9 : public TextureHost
 {
 public:
   TextureHostD3D9(TextureFlags aFlags,
                   const SurfaceDescriptorD3D9& aDescriptor);