summaryrefslogtreecommitdiff
path: root/gfx/2d/SourceSurfaceD2D.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gfx/2d/SourceSurfaceD2D.cpp')
-rw-r--r--gfx/2d/SourceSurfaceD2D.cpp106
1 files changed, 86 insertions, 20 deletions
diff --git a/gfx/2d/SourceSurfaceD2D.cpp b/gfx/2d/SourceSurfaceD2D.cpp
index 005a6901e..538f0d2bd 100644
--- a/gfx/2d/SourceSurfaceD2D.cpp
+++ b/gfx/2d/SourceSurfaceD2D.cpp
@@ -45,7 +45,7 @@ SourceSurfaceD2D::GetDataSurface()
{
RefPtr<DataSourceSurfaceD2D> result = new DataSourceSurfaceD2D(this);
if (result->IsValid()) {
- return result;
+ return result.forget();
}
return nullptr;
}
@@ -68,12 +68,18 @@ SourceSurfaceD2D::InitFromData(unsigned char *aData,
return false;
}
- D2D1_BITMAP_PROPERTIES props =
- D2D1::BitmapProperties(D2D1::PixelFormat(DXGIFormat(aFormat), AlphaMode(aFormat)));
- hr = aRT->CreateBitmap(D2DIntSize(aSize), aData, aStride, props, byRef(mBitmap));
+ D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(D2DPixelFormat(aFormat));
+ hr = aRT->CreateBitmap(D2DIntSize(aSize), props, byRef(mBitmap));
if (FAILED(hr)) {
- gfxWarning() << "Failed to create D2D Bitmap for data. Code: " << hr;
+ gfxWarning() << "Failed to create D2D Bitmap for data. Code: " << hexa(hr);
+ return false;
+ }
+
+ hr = mBitmap->CopyFromMemory(nullptr, aData, aStride);
+
+ if (FAILED(hr)) {
+ gfxWarning() << "Failed to copy data to D2D bitmap. Code: " << hexa(hr);
return false;
}
@@ -95,7 +101,7 @@ SourceSurfaceD2D::InitFromTexture(ID3D10Texture2D *aTexture,
hr = aTexture->QueryInterface((IDXGISurface**)&surf);
if (FAILED(hr)) {
- gfxWarning() << "Failed to QI texture to surface. Code: " << hr;
+ gfxWarning() << "Failed to QI texture to surface. Code: " << hexa(hr);
return false;
}
@@ -105,12 +111,11 @@ SourceSurfaceD2D::InitFromTexture(ID3D10Texture2D *aTexture,
mSize = IntSize(desc.Width, desc.Height);
mFormat = aFormat;
- D2D1_BITMAP_PROPERTIES props =
- D2D1::BitmapProperties(D2D1::PixelFormat(DXGIFormat(aFormat), AlphaMode(aFormat)));
+ D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(D2DPixelFormat(aFormat));
hr = aRT->CreateSharedBitmap(IID_IDXGISurface, surf, &props, byRef(mBitmap));
if (FAILED(hr)) {
- gfxWarning() << "Failed to create SharedBitmap. Code: " << hr;
+ gfxWarning() << "Failed to create SharedBitmap. Code: " << hexa(hr);
return false;
}
@@ -145,14 +150,14 @@ DataSourceSurfaceD2D::DataSourceSurfaceD2D(SourceSurfaceD2D* aSourceSurface)
HRESULT hr = aSourceSurface->mDevice->CreateTexture2D(&desc, nullptr,
byRef(sourceTexture));
if (FAILED(hr)) {
- gfxWarning() << "Failed to create texture. Code: " << hr;
+ gfxWarning() << "Failed to create texture. Code: " << hexa(hr);
return;
}
RefPtr<IDXGISurface> dxgiSurface;
hr = sourceTexture->QueryInterface((IDXGISurface**)byRef(dxgiSurface));
if (FAILED(hr)) {
- gfxWarning() << "Failed to create DXGI surface. Code: " << hr;
+ gfxWarning() << "Failed to create DXGI surface. Code: " << hexa(hr);
return;
}
@@ -165,23 +170,35 @@ DataSourceSurfaceD2D::DataSourceSurfaceD2D(SourceSurfaceD2D* aSourceSurface)
&rtProps,
byRef(renderTarget));
if (FAILED(hr)) {
- gfxWarning() << "Failed to create render target. Code: " << hr;
+ gfxWarning() << "Failed to create render target. Code: " << hexa(hr);
return;
}
renderTarget->BeginDraw();
- renderTarget->DrawBitmap(aSourceSurface->mBitmap,
- D2D1::RectF(0, 0,
- Float(mSize.width),
- Float(mSize.height)));
- renderTarget->EndDraw();
+ renderTarget->Clear(D2D1::ColorF(0, 0.0f));
+ if (aSourceSurface->GetFormat() != SurfaceFormat::A8) {
+ renderTarget->DrawBitmap(aSourceSurface->mBitmap,
+ D2D1::RectF(0, 0,
+ Float(mSize.width),
+ Float(mSize.height)));
+ } else {
+ RefPtr<ID2D1SolidColorBrush> brush;
+ renderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), byRef(brush));
+ renderTarget->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
+ renderTarget->FillOpacityMask(aSourceSurface->mBitmap, brush, D2D1_OPACITY_MASK_CONTENT_GRAPHICS);
+ }
+ hr = renderTarget->EndDraw();
+ if (FAILED(hr)) {
+ gfxWarning() << "Failed to draw bitmap. Code: " << hexa(hr);
+ return;
+ }
- desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ;
+ desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ | D3D10_CPU_ACCESS_WRITE;
desc.Usage = D3D10_USAGE_STAGING;
desc.BindFlags = 0;
hr = aSourceSurface->mDevice->CreateTexture2D(&desc, nullptr, byRef(mTexture));
if (FAILED(hr)) {
- gfxWarning() << "Failed to create staging texture. Code: " << hr;
+ gfxWarning() << "Failed to create staging texture. Code: " << hexa(hr);
mTexture = nullptr;
return;
}
@@ -230,9 +247,58 @@ DataSourceSurfaceD2D::GetFormat() const
return mFormat;
}
+bool
+DataSourceSurfaceD2D::Map(MapType aMapType, MappedSurface *aMappedSurface)
+{
+ // DataSourceSurfaces used with the new Map API should not be used with GetData!!
+ MOZ_ASSERT(!mMapped);
+ MOZ_ASSERT(!mIsMapped);
+
+ if (!mTexture) {
+ return false;
+ }
+
+ D3D10_MAP mapType;
+
+ if (aMapType == MapType::READ) {
+ mapType = D3D10_MAP_READ;
+ } else if (aMapType == MapType::WRITE) {
+ mapType = D3D10_MAP_WRITE;
+ } else {
+ mapType = D3D10_MAP_READ_WRITE;
+ }
+
+ D3D10_MAPPED_TEXTURE2D map;
+
+ HRESULT hr = mTexture->Map(0, mapType, 0, &map);
+
+ if (FAILED(hr)) {
+ gfxWarning() << "Texture map failed with code: " << hexa(hr);
+ return false;
+ }
+
+ aMappedSurface->mData = (uint8_t*)map.pData;
+ aMappedSurface->mStride = map.RowPitch;
+ mIsMapped = !!aMappedSurface->mData;
+
+ return mIsMapped;
+}
+
+void
+DataSourceSurfaceD2D::Unmap()
+{
+ MOZ_ASSERT(mIsMapped);
+
+ mIsMapped = false;
+ mTexture->Unmap(0);
+}
+
void
DataSourceSurfaceD2D::EnsureMappedTexture()
{
+ // Do not use GetData() after having used Map!
+ MOZ_ASSERT(!mIsMapped);
+
if (mMapped ||
!mTexture) {
return;
@@ -240,7 +306,7 @@ DataSourceSurfaceD2D::EnsureMappedTexture()
HRESULT hr = mTexture->Map(0, D3D10_MAP_READ, 0, &mData);
if (FAILED(hr)) {
- gfxWarning() << "Failed to map texture. Code: " << hr;
+ gfxWarning() << "Failed to map texture. Code: " << hexa(hr);
mTexture = nullptr;
} else {
mMapped = true;