2011-06-24 17:41:16 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 11:12:37 +00:00
|
|
|
* 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/. */
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
#include "SourceSurfaceD2D.h"
|
2012-06-20 21:41:16 +00:00
|
|
|
#include "DrawTargetD2D.h"
|
2011-06-24 17:41:16 +00:00
|
|
|
#include "Logging.h"
|
2012-06-20 21:41:16 +00:00
|
|
|
#include "Tools.h"
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
SourceSurfaceD2D::SourceSurfaceD2D()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceSurfaceD2D::~SourceSurfaceD2D()
|
|
|
|
{
|
2012-06-20 21:41:16 +00:00
|
|
|
if (mBitmap) {
|
|
|
|
DrawTargetD2D::mVRAMUsageSS -= GetByteSize();
|
|
|
|
}
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IntSize
|
|
|
|
SourceSurfaceD2D::GetSize() const
|
|
|
|
{
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceFormat
|
|
|
|
SourceSurfaceD2D::GetFormat() const
|
|
|
|
{
|
|
|
|
return mFormat;
|
|
|
|
}
|
|
|
|
|
2012-07-28 00:33:08 +00:00
|
|
|
bool
|
|
|
|
SourceSurfaceD2D::IsValid() const
|
|
|
|
{
|
|
|
|
return mDevice == Factory::GetDirect3D10Device();
|
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
TemporaryRef<DataSourceSurface>
|
|
|
|
SourceSurfaceD2D::GetDataSurface()
|
|
|
|
{
|
2012-10-30 23:30:25 +00:00
|
|
|
RefPtr<DataSourceSurfaceD2D> result = new DataSourceSurfaceD2D(this);
|
|
|
|
if (result->IsValid()) {
|
2014-06-13 16:09:23 +00:00
|
|
|
return result.forget();
|
2012-10-30 23:30:25 +00:00
|
|
|
}
|
2012-08-14 18:06:12 +00:00
|
|
|
return nullptr;
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SourceSurfaceD2D::InitFromData(unsigned char *aData,
|
|
|
|
const IntSize &aSize,
|
|
|
|
int32_t aStride,
|
|
|
|
SurfaceFormat aFormat,
|
|
|
|
ID2D1RenderTarget *aRT)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
mFormat = aFormat;
|
|
|
|
mSize = aSize;
|
|
|
|
|
|
|
|
if ((uint32_t)aSize.width > aRT->GetMaximumBitmapSize() ||
|
|
|
|
(uint32_t)aSize.height > aRT->GetMaximumBitmapSize()) {
|
2012-05-21 15:27:30 +00:00
|
|
|
gfxDebug() << "Bitmap does not fit in texture.";
|
|
|
|
return false;
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 16:20:50 +00:00
|
|
|
D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(D2DPixelFormat(aFormat));
|
2014-06-12 19:05:37 +00:00
|
|
|
hr = aRT->CreateBitmap(D2DIntSize(aSize), props, byRef(mBitmap));
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2014-09-23 15:35:39 +00:00
|
|
|
gfxWarning() << "Failed to create D2D Bitmap for data. Code: " << hexa(hr);
|
2011-06-24 17:41:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-12 19:05:37 +00:00
|
|
|
hr = mBitmap->CopyFromMemory(nullptr, aData, aStride);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2014-09-23 15:35:39 +00:00
|
|
|
gfxWarning() << "Failed to copy data to D2D bitmap. Code: " << hexa(hr);
|
2014-06-12 19:05:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-20 21:41:16 +00:00
|
|
|
DrawTargetD2D::mVRAMUsageSS += GetByteSize();
|
2012-07-28 00:33:08 +00:00
|
|
|
mDevice = Factory::GetDirect3D10Device();
|
2012-06-20 21:41:16 +00:00
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SourceSurfaceD2D::InitFromTexture(ID3D10Texture2D *aTexture,
|
|
|
|
SurfaceFormat aFormat,
|
|
|
|
ID2D1RenderTarget *aRT)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
RefPtr<IDXGISurface> surf;
|
|
|
|
|
|
|
|
hr = aTexture->QueryInterface((IDXGISurface**)&surf);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2014-09-23 15:35:39 +00:00
|
|
|
gfxWarning() << "Failed to QI texture to surface. Code: " << hexa(hr);
|
2011-06-24 17:41:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D10_TEXTURE2D_DESC desc;
|
|
|
|
aTexture->GetDesc(&desc);
|
|
|
|
|
|
|
|
mSize = IntSize(desc.Width, desc.Height);
|
|
|
|
mFormat = aFormat;
|
|
|
|
|
2013-11-20 16:20:50 +00:00
|
|
|
D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(D2DPixelFormat(aFormat));
|
2011-06-24 17:41:16 +00:00
|
|
|
hr = aRT->CreateSharedBitmap(IID_IDXGISurface, surf, &props, byRef(mBitmap));
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2014-09-23 15:35:39 +00:00
|
|
|
gfxWarning() << "Failed to create SharedBitmap. Code: " << hexa(hr);
|
2011-06-24 17:41:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-28 00:33:08 +00:00
|
|
|
aTexture->GetDevice(byRef(mDevice));
|
2012-06-20 21:41:16 +00:00
|
|
|
DrawTargetD2D::mVRAMUsageSS += GetByteSize();
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-20 21:41:16 +00:00
|
|
|
uint32_t
|
|
|
|
SourceSurfaceD2D::GetByteSize() const
|
|
|
|
{
|
|
|
|
return mSize.width * mSize.height * BytesPerPixel(mFormat);
|
|
|
|
}
|
|
|
|
|
2012-10-30 23:30:25 +00:00
|
|
|
DataSourceSurfaceD2D::DataSourceSurfaceD2D(SourceSurfaceD2D* aSourceSurface)
|
|
|
|
: mTexture(nullptr)
|
|
|
|
, mFormat(aSourceSurface->mFormat)
|
|
|
|
, mSize(aSourceSurface->mSize)
|
|
|
|
, mMapped(false)
|
|
|
|
{
|
|
|
|
// We allocate ourselves a regular D3D surface (sourceTexture) and paint the
|
|
|
|
// D2D bitmap into it via a DXGI render target. Then we need to copy
|
|
|
|
// sourceTexture into a staging texture (mTexture), which we will lazily map
|
|
|
|
// to get the data.
|
|
|
|
|
|
|
|
CD3D10_TEXTURE2D_DESC desc(DXGIFormat(mFormat), mSize.width, mSize.height);
|
|
|
|
desc.MipLevels = 1;
|
|
|
|
desc.Usage = D3D10_USAGE_DEFAULT;
|
|
|
|
desc.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE;
|
|
|
|
RefPtr<ID3D10Texture2D> sourceTexture;
|
|
|
|
HRESULT hr = aSourceSurface->mDevice->CreateTexture2D(&desc, nullptr,
|
|
|
|
byRef(sourceTexture));
|
|
|
|
if (FAILED(hr)) {
|
2014-09-23 15:35:39 +00:00
|
|
|
gfxWarning() << "Failed to create texture. Code: " << hexa(hr);
|
2012-10-30 23:30:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<IDXGISurface> dxgiSurface;
|
|
|
|
hr = sourceTexture->QueryInterface((IDXGISurface**)byRef(dxgiSurface));
|
|
|
|
if (FAILED(hr)) {
|
2014-09-23 15:35:39 +00:00
|
|
|
gfxWarning() << "Failed to create DXGI surface. Code: " << hexa(hr);
|
2012-10-30 23:30:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-08 20:33:15 +00:00
|
|
|
D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties(
|
|
|
|
D2D1_RENDER_TARGET_TYPE_DEFAULT,
|
2012-10-30 23:30:25 +00:00
|
|
|
D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED));
|
|
|
|
|
|
|
|
RefPtr<ID2D1RenderTarget> renderTarget;
|
2013-07-08 20:33:15 +00:00
|
|
|
hr = DrawTargetD2D::factory()->CreateDxgiSurfaceRenderTarget(dxgiSurface,
|
|
|
|
&rtProps,
|
2012-10-30 23:30:25 +00:00
|
|
|
byRef(renderTarget));
|
|
|
|
if (FAILED(hr)) {
|
2014-09-23 15:35:39 +00:00
|
|
|
gfxWarning() << "Failed to create render target. Code: " << hexa(hr);
|
2012-10-30 23:30:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTarget->BeginDraw();
|
2014-03-26 02:03:55 +00:00
|
|
|
renderTarget->Clear(D2D1::ColorF(0, 0.0f));
|
2014-07-01 05:52:49 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-03-26 02:03:55 +00:00
|
|
|
hr = renderTarget->EndDraw();
|
|
|
|
if (FAILED(hr)) {
|
2014-09-23 15:35:39 +00:00
|
|
|
gfxWarning() << "Failed to draw bitmap. Code: " << hexa(hr);
|
2014-03-26 02:03:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-10-30 23:30:25 +00:00
|
|
|
|
2014-01-16 12:17:24 +00:00
|
|
|
desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ | D3D10_CPU_ACCESS_WRITE;
|
2012-10-30 23:30:25 +00:00
|
|
|
desc.Usage = D3D10_USAGE_STAGING;
|
|
|
|
desc.BindFlags = 0;
|
|
|
|
hr = aSourceSurface->mDevice->CreateTexture2D(&desc, nullptr, byRef(mTexture));
|
|
|
|
if (FAILED(hr)) {
|
2014-09-23 15:35:39 +00:00
|
|
|
gfxWarning() << "Failed to create staging texture. Code: " << hexa(hr);
|
2012-10-30 23:30:25 +00:00
|
|
|
mTexture = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aSourceSurface->mDevice->CopyResource(mTexture, sourceTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurfaceD2D::~DataSourceSurfaceD2D()
|
|
|
|
{
|
|
|
|
if (mMapped) {
|
|
|
|
mTexture->Unmap(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char*
|
|
|
|
DataSourceSurfaceD2D::GetData()
|
|
|
|
{
|
|
|
|
EnsureMappedTexture();
|
|
|
|
if (!mMapped) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<unsigned char*>(mData.pData);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
DataSourceSurfaceD2D::Stride()
|
|
|
|
{
|
|
|
|
EnsureMappedTexture();
|
|
|
|
if (!mMapped) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mData.RowPitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize
|
|
|
|
DataSourceSurfaceD2D::GetSize() const
|
|
|
|
{
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceFormat
|
|
|
|
DataSourceSurfaceD2D::GetFormat() const
|
|
|
|
{
|
|
|
|
return mFormat;
|
|
|
|
}
|
|
|
|
|
2014-01-16 12:17:24 +00:00
|
|
|
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)) {
|
2014-09-23 15:35:39 +00:00
|
|
|
gfxWarning() << "Texture map failed with code: " << hexa(hr);
|
2014-01-16 12:17:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMappedSurface->mData = (uint8_t*)map.pData;
|
|
|
|
aMappedSurface->mStride = map.RowPitch;
|
2014-03-06 23:29:57 +00:00
|
|
|
mIsMapped = true;
|
2014-01-16 12:17:24 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataSourceSurfaceD2D::Unmap()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIsMapped);
|
|
|
|
|
2014-03-06 23:31:22 +00:00
|
|
|
mIsMapped = false;
|
2014-01-16 12:17:24 +00:00
|
|
|
mTexture->Unmap(0);
|
|
|
|
}
|
|
|
|
|
2012-10-30 23:30:25 +00:00
|
|
|
void
|
|
|
|
DataSourceSurfaceD2D::EnsureMappedTexture()
|
|
|
|
{
|
2014-01-16 12:17:24 +00:00
|
|
|
// Do not use GetData() after having used Map!
|
|
|
|
MOZ_ASSERT(!mIsMapped);
|
|
|
|
|
2012-10-30 23:30:25 +00:00
|
|
|
if (mMapped ||
|
|
|
|
!mTexture) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT hr = mTexture->Map(0, D3D10_MAP_READ, 0, &mData);
|
|
|
|
if (FAILED(hr)) {
|
2014-09-23 15:35:39 +00:00
|
|
|
gfxWarning() << "Failed to map texture. Code: " << hexa(hr);
|
2012-10-30 23:30:25 +00:00
|
|
|
mTexture = nullptr;
|
|
|
|
} else {
|
|
|
|
mMapped = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
}
|
|
|
|
}
|