michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "SourceSurfaceD2D.h" michael@0: #include "DrawTargetD2D.h" michael@0: #include "Logging.h" michael@0: #include "Tools.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: SourceSurfaceD2D::SourceSurfaceD2D() michael@0: { michael@0: } michael@0: michael@0: SourceSurfaceD2D::~SourceSurfaceD2D() michael@0: { michael@0: if (mBitmap) { michael@0: DrawTargetD2D::mVRAMUsageSS -= GetByteSize(); michael@0: } michael@0: } michael@0: michael@0: IntSize michael@0: SourceSurfaceD2D::GetSize() const michael@0: { michael@0: return mSize; michael@0: } michael@0: michael@0: SurfaceFormat michael@0: SourceSurfaceD2D::GetFormat() const michael@0: { michael@0: return mFormat; michael@0: } michael@0: michael@0: bool michael@0: SourceSurfaceD2D::IsValid() const michael@0: { michael@0: return mDevice == Factory::GetDirect3D10Device(); michael@0: } michael@0: michael@0: TemporaryRef michael@0: SourceSurfaceD2D::GetDataSurface() michael@0: { michael@0: RefPtr result = new DataSourceSurfaceD2D(this); michael@0: if (result->IsValid()) { michael@0: return result; michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: bool michael@0: SourceSurfaceD2D::InitFromData(unsigned char *aData, michael@0: const IntSize &aSize, michael@0: int32_t aStride, michael@0: SurfaceFormat aFormat, michael@0: ID2D1RenderTarget *aRT) michael@0: { michael@0: HRESULT hr; michael@0: michael@0: mFormat = aFormat; michael@0: mSize = aSize; michael@0: michael@0: if ((uint32_t)aSize.width > aRT->GetMaximumBitmapSize() || michael@0: (uint32_t)aSize.height > aRT->GetMaximumBitmapSize()) { michael@0: gfxDebug() << "Bitmap does not fit in texture."; michael@0: return false; michael@0: } michael@0: michael@0: D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(D2DPixelFormat(aFormat)); michael@0: hr = aRT->CreateBitmap(D2DIntSize(aSize), aData, aStride, props, byRef(mBitmap)); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create D2D Bitmap for data. Code: " << hr; michael@0: return false; michael@0: } michael@0: michael@0: DrawTargetD2D::mVRAMUsageSS += GetByteSize(); michael@0: mDevice = Factory::GetDirect3D10Device(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: SourceSurfaceD2D::InitFromTexture(ID3D10Texture2D *aTexture, michael@0: SurfaceFormat aFormat, michael@0: ID2D1RenderTarget *aRT) michael@0: { michael@0: HRESULT hr; michael@0: michael@0: RefPtr surf; michael@0: michael@0: hr = aTexture->QueryInterface((IDXGISurface**)&surf); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to QI texture to surface. Code: " << hr; michael@0: return false; michael@0: } michael@0: michael@0: D3D10_TEXTURE2D_DESC desc; michael@0: aTexture->GetDesc(&desc); michael@0: michael@0: mSize = IntSize(desc.Width, desc.Height); michael@0: mFormat = aFormat; michael@0: michael@0: D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(D2DPixelFormat(aFormat)); michael@0: hr = aRT->CreateSharedBitmap(IID_IDXGISurface, surf, &props, byRef(mBitmap)); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create SharedBitmap. Code: " << hr; michael@0: return false; michael@0: } michael@0: michael@0: aTexture->GetDevice(byRef(mDevice)); michael@0: DrawTargetD2D::mVRAMUsageSS += GetByteSize(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: uint32_t michael@0: SourceSurfaceD2D::GetByteSize() const michael@0: { michael@0: return mSize.width * mSize.height * BytesPerPixel(mFormat); michael@0: } michael@0: michael@0: DataSourceSurfaceD2D::DataSourceSurfaceD2D(SourceSurfaceD2D* aSourceSurface) michael@0: : mTexture(nullptr) michael@0: , mFormat(aSourceSurface->mFormat) michael@0: , mSize(aSourceSurface->mSize) michael@0: , mMapped(false) michael@0: { michael@0: // We allocate ourselves a regular D3D surface (sourceTexture) and paint the michael@0: // D2D bitmap into it via a DXGI render target. Then we need to copy michael@0: // sourceTexture into a staging texture (mTexture), which we will lazily map michael@0: // to get the data. michael@0: michael@0: CD3D10_TEXTURE2D_DESC desc(DXGIFormat(mFormat), mSize.width, mSize.height); michael@0: desc.MipLevels = 1; michael@0: desc.Usage = D3D10_USAGE_DEFAULT; michael@0: desc.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE; michael@0: RefPtr sourceTexture; michael@0: HRESULT hr = aSourceSurface->mDevice->CreateTexture2D(&desc, nullptr, michael@0: byRef(sourceTexture)); michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create texture. Code: " << hr; michael@0: return; michael@0: } michael@0: michael@0: RefPtr dxgiSurface; michael@0: hr = sourceTexture->QueryInterface((IDXGISurface**)byRef(dxgiSurface)); michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create DXGI surface. Code: " << hr; michael@0: return; michael@0: } michael@0: michael@0: D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties( michael@0: D2D1_RENDER_TARGET_TYPE_DEFAULT, michael@0: D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED)); michael@0: michael@0: RefPtr renderTarget; michael@0: hr = DrawTargetD2D::factory()->CreateDxgiSurfaceRenderTarget(dxgiSurface, michael@0: &rtProps, michael@0: byRef(renderTarget)); michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create render target. Code: " << hr; michael@0: return; michael@0: } michael@0: michael@0: renderTarget->BeginDraw(); michael@0: renderTarget->Clear(D2D1::ColorF(0, 0.0f)); michael@0: renderTarget->DrawBitmap(aSourceSurface->mBitmap, michael@0: D2D1::RectF(0, 0, michael@0: Float(mSize.width), michael@0: Float(mSize.height))); michael@0: hr = renderTarget->EndDraw(); michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to draw bitmap. Code: " << hr; michael@0: return; michael@0: } michael@0: michael@0: desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ | D3D10_CPU_ACCESS_WRITE; michael@0: desc.Usage = D3D10_USAGE_STAGING; michael@0: desc.BindFlags = 0; michael@0: hr = aSourceSurface->mDevice->CreateTexture2D(&desc, nullptr, byRef(mTexture)); michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create staging texture. Code: " << hr; michael@0: mTexture = nullptr; michael@0: return; michael@0: } michael@0: michael@0: aSourceSurface->mDevice->CopyResource(mTexture, sourceTexture); michael@0: } michael@0: michael@0: DataSourceSurfaceD2D::~DataSourceSurfaceD2D() michael@0: { michael@0: if (mMapped) { michael@0: mTexture->Unmap(0); michael@0: } michael@0: } michael@0: michael@0: unsigned char* michael@0: DataSourceSurfaceD2D::GetData() michael@0: { michael@0: EnsureMappedTexture(); michael@0: if (!mMapped) { michael@0: return nullptr; michael@0: } michael@0: michael@0: return reinterpret_cast(mData.pData); michael@0: } michael@0: michael@0: int32_t michael@0: DataSourceSurfaceD2D::Stride() michael@0: { michael@0: EnsureMappedTexture(); michael@0: if (!mMapped) { michael@0: return 0; michael@0: } michael@0: michael@0: return mData.RowPitch; michael@0: } michael@0: michael@0: IntSize michael@0: DataSourceSurfaceD2D::GetSize() const michael@0: { michael@0: return mSize; michael@0: } michael@0: michael@0: SurfaceFormat michael@0: DataSourceSurfaceD2D::GetFormat() const michael@0: { michael@0: return mFormat; michael@0: } michael@0: michael@0: bool michael@0: DataSourceSurfaceD2D::Map(MapType aMapType, MappedSurface *aMappedSurface) michael@0: { michael@0: // DataSourceSurfaces used with the new Map API should not be used with GetData!! michael@0: MOZ_ASSERT(!mMapped); michael@0: MOZ_ASSERT(!mIsMapped); michael@0: michael@0: if (!mTexture) { michael@0: return false; michael@0: } michael@0: michael@0: D3D10_MAP mapType; michael@0: michael@0: if (aMapType == MapType::READ) { michael@0: mapType = D3D10_MAP_READ; michael@0: } else if (aMapType == MapType::WRITE) { michael@0: mapType = D3D10_MAP_WRITE; michael@0: } else { michael@0: mapType = D3D10_MAP_READ_WRITE; michael@0: } michael@0: michael@0: D3D10_MAPPED_TEXTURE2D map; michael@0: michael@0: HRESULT hr = mTexture->Map(0, mapType, 0, &map); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Texture map failed with code: " << hr; michael@0: return false; michael@0: } michael@0: michael@0: aMappedSurface->mData = (uint8_t*)map.pData; michael@0: aMappedSurface->mStride = map.RowPitch; michael@0: mIsMapped = true; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: DataSourceSurfaceD2D::Unmap() michael@0: { michael@0: MOZ_ASSERT(mIsMapped); michael@0: michael@0: mIsMapped = false; michael@0: mTexture->Unmap(0); michael@0: } michael@0: michael@0: void michael@0: DataSourceSurfaceD2D::EnsureMappedTexture() michael@0: { michael@0: // Do not use GetData() after having used Map! michael@0: MOZ_ASSERT(!mIsMapped); michael@0: michael@0: if (mMapped || michael@0: !mTexture) { michael@0: return; michael@0: } michael@0: michael@0: HRESULT hr = mTexture->Map(0, D3D10_MAP_READ, 0, &mData); michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to map texture. Code: " << hr; michael@0: mTexture = nullptr; michael@0: } else { michael@0: mMapped = true; michael@0: } michael@0: } michael@0: michael@0: } michael@0: }