Bug 960254: Implement the new Map/Unmap API for Direct2D. r=jrmuizel

This commit is contained in:
Bas Schouten 2014-01-16 13:17:24 +01:00
parent aad0b985e1
commit 5b4524918c
6 changed files with 131 additions and 1 deletions

View File

@ -174,7 +174,7 @@ DataSourceSurfaceD2D::DataSourceSurfaceD2D(SourceSurfaceD2D* aSourceSurface)
Float(mSize.height)));
renderTarget->EndDraw();
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));
@ -228,9 +228,56 @@ 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: " << hr;
return false;
}
aMappedSurface->mData = (uint8_t*)map.pData;
aMappedSurface->mStride = map.RowPitch;
return true;
}
void
DataSourceSurfaceD2D::Unmap()
{
MOZ_ASSERT(mIsMapped);
mTexture->Unmap(0);
}
void
DataSourceSurfaceD2D::EnsureMappedTexture()
{
// Do not use GetData() after having used Map!
MOZ_ASSERT(!mIsMapped);
if (mMapped ||
!mTexture) {
return;

View File

@ -62,6 +62,8 @@ public:
virtual int32_t Stride();
virtual IntSize GetSize() const;
virtual SurfaceFormat GetFormat() const;
virtual bool Map(MapType, MappedSurface *aMappedSurface);
virtual void Unmap();
bool IsValid()
{

View File

@ -141,6 +141,35 @@ DataSourceSurfaceD2D1::GetData()
return mMap.bits;
}
bool
DataSourceSurfaceD2D1::Map(MapType aMapType, MappedSurface *aMappedSurface)
{
// DataSourceSurfaces used with the new Map API should not be used with GetData!!
MOZ_ASSERT(!mMapped);
MOZ_ASSERT(!mIsMapped);
D2D1_MAP_OPTIONS options;
if (aMapType == MapType::READ) {
options = D2D1_MAP_OPTIONS_READ;
} else {
MOZ_CRASH("No support for Write maps on D2D1 DataSourceSurfaces yet!");
}
D2D1_MAPPED_RECT map;
mBitmap->Map(D2D1_MAP_OPTIONS_READ, &map);
aMappedSurface->mData = map.bits;
aMappedSurface->mStride = map.pitch;
mIsMapped = true;
return true;
}
void
DataSourceSurfaceD2D1::Unmap()
{
mBitmap->Unmap();
}
int32_t
DataSourceSurfaceD2D1::Stride()
{
@ -152,6 +181,8 @@ DataSourceSurfaceD2D1::Stride()
void
DataSourceSurfaceD2D1::EnsureMapped()
{
// Do not use GetData() after having used Map!
MOZ_ASSERT(!mIsMapped);
if (mMapped) {
return;
}

View File

@ -70,6 +70,8 @@ public:
virtual SurfaceFormat GetFormat() const { return mFormat; }
virtual uint8_t *GetData();
virtual int32_t Stride();
virtual bool Map(MapType, MappedSurface *aMappedSurface);
virtual void Unmap();
private:
friend class SourceSurfaceD2DTarget;

View File

@ -236,9 +236,55 @@ DataSourceSurfaceD2DTarget::Stride()
return mMap.RowPitch;
}
bool
DataSourceSurfaceD2DTarget::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: " << hr;
return false;
}
aMappedSurface->mData = (uint8_t*)map.pData;
aMappedSurface->mStride = map.RowPitch;
return true;
}
void
DataSourceSurfaceD2DTarget::Unmap()
{
MOZ_ASSERT(mIsMapped);
mTexture->Unmap(0);
}
void
DataSourceSurfaceD2DTarget::EnsureMapped()
{
// Do not use GetData() after having used Map!
MOZ_ASSERT(!mIsMapped);
if (!mMapped) {
HRESULT hr = mTexture->Map(0, D3D10_MAP_READ, 0, &mMap);
if (FAILED(hr)) {

View File

@ -68,6 +68,8 @@ public:
virtual SurfaceFormat GetFormat() const;
virtual uint8_t *GetData();
virtual int32_t Stride();
virtual bool Map(MapType, MappedSurface *aMappedSurface);
virtual void Unmap();
private:
friend class SourceSurfaceD2DTarget;