2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-17 16:12:22 +04:00
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#include "SourceSurfaceD2D1.h"
|
|
|
|
#include "DrawTargetD2D1.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
SourceSurfaceD2D1::SourceSurfaceD2D1(ID2D1Image* aImage,
|
|
|
|
ID2D1DeviceContext* aDC,
|
2013-07-17 16:12:22 +04:00
|
|
|
SurfaceFormat aFormat,
|
2019-05-01 11:47:10 +03:00
|
|
|
const IntSize& aSize, DrawTargetD2D1* aDT)
|
2013-07-17 16:12:22 +04:00
|
|
|
: mImage(aImage),
|
|
|
|
mDC(aDC),
|
2014-10-10 19:04:26 +04:00
|
|
|
mDevice(Factory::GetD2D1Device()),
|
2015-11-05 23:22:08 +03:00
|
|
|
mDrawTarget(aDT) {
|
2019-05-01 11:47:10 +03:00
|
|
|
aImage->QueryInterface((ID2D1Bitmap1**)getter_AddRefs(mRealizedBitmap));
|
2013-07-17 16:12:22 +04:00
|
|
|
|
|
|
|
mFormat = aFormat;
|
|
|
|
mSize = aSize;
|
2017-11-21 18:52:38 +03:00
|
|
|
if (aDT) {
|
|
|
|
mSnapshotLock = aDT->mSnapshotLock;
|
|
|
|
}
|
2013-07-17 16:12:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SourceSurfaceD2D1::~SourceSurfaceD2D1() {}
|
|
|
|
|
2014-10-10 19:04:26 +04:00
|
|
|
bool SourceSurfaceD2D1::IsValid() const {
|
|
|
|
return mDevice == Factory::GetD2D1Device();
|
|
|
|
}
|
|
|
|
|
2013-07-17 16:12:22 +04:00
|
|
|
already_AddRefed<DataSourceSurface> SourceSurfaceD2D1::GetDataSurface() {
|
2017-12-12 23:52:54 +03:00
|
|
|
Maybe<MutexAutoLock> lock;
|
|
|
|
if (mSnapshotLock) {
|
|
|
|
lock.emplace(*mSnapshotLock);
|
|
|
|
}
|
2014-10-10 19:04:26 +04:00
|
|
|
|
2015-07-30 14:56:00 +03:00
|
|
|
if (!EnsureRealizedBitmap()) {
|
|
|
|
gfxCriticalError() << "Failed to realize a bitmap, device "
|
|
|
|
<< hexa(mDevice);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-07-17 16:12:22 +04:00
|
|
|
|
2017-12-12 23:52:54 +03:00
|
|
|
HRESULT hr;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ID2D1Bitmap1> softwareBitmap;
|
2013-07-17 16:12:22 +04:00
|
|
|
D2D1_BITMAP_PROPERTIES1 props;
|
|
|
|
props.dpiX = 96;
|
|
|
|
props.dpiY = 96;
|
|
|
|
props.pixelFormat = D2DPixelFormat(mFormat);
|
|
|
|
props.colorContext = nullptr;
|
|
|
|
props.bitmapOptions =
|
|
|
|
D2D1_BITMAP_OPTIONS_CANNOT_DRAW | D2D1_BITMAP_OPTIONS_CPU_READ;
|
2015-10-18 07:40:10 +03:00
|
|
|
hr = mDC->CreateBitmap(D2DIntSize(mSize), nullptr, 0, props,
|
2019-05-01 11:47:10 +03:00
|
|
|
(ID2D1Bitmap1**)getter_AddRefs(softwareBitmap));
|
2014-10-10 19:04:26 +04:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
gfxCriticalError() << "Failed to create software bitmap: " << mSize
|
|
|
|
<< " Code: " << hexa(hr);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-07-17 16:12:22 +04:00
|
|
|
|
|
|
|
D2D1_POINT_2U point = D2D1::Point2U(0, 0);
|
|
|
|
D2D1_RECT_U rect = D2D1::RectU(0, 0, mSize.width, mSize.height);
|
2018-10-09 15:56:25 +03:00
|
|
|
|
2014-10-10 19:04:26 +04:00
|
|
|
hr = softwareBitmap->CopyFromBitmap(&point, mRealizedBitmap, &rect);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
gfxWarning() << "Failed to readback into software bitmap. Code: "
|
|
|
|
<< hexa(hr);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-04-30 22:20:30 +03:00
|
|
|
return MakeAndAddRef<DataSourceSurfaceD2D1>(softwareBitmap, mFormat);
|
2013-07-17 16:12:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SourceSurfaceD2D1::EnsureRealizedBitmap() {
|
|
|
|
if (mRealizedBitmap) {
|
2015-07-30 14:56:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Why aren't we using mDevice here or anywhere else?
|
2017-07-18 22:15:39 +03:00
|
|
|
RefPtr<ID2D1Device> device = Factory::GetD2D1Device();
|
2015-07-30 14:56:00 +03:00
|
|
|
if (!device) {
|
|
|
|
return false;
|
2013-07-17 16:12:22 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ID2D1DeviceContext> dc;
|
2015-10-18 07:40:10 +03:00
|
|
|
device->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
|
|
|
|
getter_AddRefs(dc));
|
2013-07-17 16:12:22 +04:00
|
|
|
|
|
|
|
D2D1_BITMAP_PROPERTIES1 props;
|
|
|
|
props.dpiX = 96;
|
|
|
|
props.dpiY = 96;
|
|
|
|
props.pixelFormat = D2DPixelFormat(mFormat);
|
|
|
|
props.colorContext = nullptr;
|
|
|
|
props.bitmapOptions = D2D1_BITMAP_OPTIONS_TARGET;
|
2015-10-18 07:40:10 +03:00
|
|
|
dc->CreateBitmap(D2DIntSize(mSize), nullptr, 0, props,
|
2019-05-01 11:47:10 +03:00
|
|
|
(ID2D1Bitmap1**)getter_AddRefs(mRealizedBitmap));
|
2013-07-17 16:12:22 +04:00
|
|
|
|
|
|
|
dc->SetTarget(mRealizedBitmap);
|
|
|
|
|
|
|
|
dc->BeginDraw();
|
|
|
|
dc->DrawImage(mImage);
|
|
|
|
dc->EndDraw();
|
2015-07-30 14:56:00 +03:00
|
|
|
|
|
|
|
return true;
|
2013-07-17 16:12:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SourceSurfaceD2D1::DrawTargetWillChange() {
|
2017-11-21 18:52:38 +03:00
|
|
|
MOZ_ASSERT(mSnapshotLock);
|
|
|
|
mSnapshotLock->AssertCurrentThreadOwns();
|
|
|
|
|
2013-07-17 16:12:22 +04:00
|
|
|
// At this point in time this should always be true here.
|
|
|
|
MOZ_ASSERT(mRealizedBitmap);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ID2D1Bitmap1> oldBitmap = mRealizedBitmap;
|
2013-07-17 16:12:22 +04:00
|
|
|
|
|
|
|
D2D1_BITMAP_PROPERTIES1 props;
|
|
|
|
props.dpiX = 96;
|
|
|
|
props.dpiY = 96;
|
|
|
|
props.pixelFormat = D2DPixelFormat(mFormat);
|
|
|
|
props.colorContext = nullptr;
|
|
|
|
props.bitmapOptions = D2D1_BITMAP_OPTIONS_TARGET;
|
2015-10-18 07:40:10 +03:00
|
|
|
HRESULT hr =
|
|
|
|
mDC->CreateBitmap(D2DIntSize(mSize), nullptr, 0, props,
|
2019-05-01 11:47:10 +03:00
|
|
|
(ID2D1Bitmap1**)getter_AddRefs(mRealizedBitmap));
|
2015-01-08 03:10:48 +03:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
gfxCriticalError()
|
|
|
|
<< "Failed to create bitmap to make DrawTarget copy. Size: " << mSize
|
|
|
|
<< " Code: " << hexa(hr);
|
|
|
|
MarkIndependent();
|
|
|
|
return;
|
|
|
|
}
|
2013-07-17 16:12:22 +04:00
|
|
|
|
|
|
|
D2D1_POINT_2U point = D2D1::Point2U(0, 0);
|
|
|
|
D2D1_RECT_U rect = D2D1::RectU(0, 0, mSize.width, mSize.height);
|
|
|
|
mRealizedBitmap->CopyFromBitmap(&point, oldBitmap, &rect);
|
|
|
|
mImage = mRealizedBitmap;
|
|
|
|
|
|
|
|
DrawTargetD2D1::mVRAMUsageSS +=
|
|
|
|
mSize.width * mSize.height * BytesPerPixel(mFormat);
|
|
|
|
|
2018-09-03 16:16:28 +03:00
|
|
|
// Ensure the object stays alive for the duration of MarkIndependent.
|
|
|
|
RefPtr<SourceSurfaceD2D1> deathGrip = this;
|
2013-07-17 16:12:22 +04:00
|
|
|
// We now no longer depend on the source surface content remaining the same.
|
|
|
|
MarkIndependent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceSurfaceD2D1::MarkIndependent() {
|
|
|
|
if (mDrawTarget) {
|
|
|
|
MOZ_ASSERT(mDrawTarget->mSnapshot == this);
|
|
|
|
mDrawTarget->mSnapshot = nullptr;
|
|
|
|
mDrawTarget = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
DataSourceSurfaceD2D1::DataSourceSurfaceD2D1(ID2D1Bitmap1* aMappableBitmap,
|
2013-07-17 16:12:22 +04:00
|
|
|
SurfaceFormat aFormat)
|
|
|
|
: mBitmap(aMappableBitmap),
|
|
|
|
mFormat(aFormat),
|
2018-11-07 00:04:02 +03:00
|
|
|
mIsMapped(false),
|
|
|
|
mImplicitMapped(false) {}
|
2013-07-17 16:12:22 +04:00
|
|
|
|
|
|
|
DataSourceSurfaceD2D1::~DataSourceSurfaceD2D1() {
|
2018-11-07 00:04:02 +03:00
|
|
|
if (mImplicitMapped) {
|
2013-07-17 16:12:22 +04:00
|
|
|
mBitmap->Unmap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize DataSourceSurfaceD2D1::GetSize() const {
|
|
|
|
D2D1_SIZE_F size = mBitmap->GetSize();
|
|
|
|
|
|
|
|
return IntSize(int32_t(size.width), int32_t(size.height));
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
uint8_t* DataSourceSurfaceD2D1::GetData() {
|
2013-07-17 16:12:22 +04:00
|
|
|
EnsureMapped();
|
|
|
|
|
|
|
|
return mMap.bits;
|
|
|
|
}
|
|
|
|
|
2014-01-16 16:17:24 +04:00
|
|
|
bool DataSourceSurfaceD2D1::Map(MapType aMapType,
|
2019-05-01 11:47:10 +03:00
|
|
|
MappedSurface* aMappedSurface) {
|
2014-01-16 16:17:24 +04:00
|
|
|
// DataSourceSurfaces used with the new Map API should not be used with
|
|
|
|
// GetData!!
|
2018-11-07 00:04:02 +03:00
|
|
|
MOZ_ASSERT(!mImplicitMapped);
|
2014-01-16 16:17:24 +04:00
|
|
|
MOZ_ASSERT(!mIsMapped);
|
|
|
|
|
|
|
|
D2D1_MAP_OPTIONS options;
|
|
|
|
if (aMapType == MapType::READ) {
|
|
|
|
options = D2D1_MAP_OPTIONS_READ;
|
|
|
|
} else {
|
2016-04-28 16:22:32 +03:00
|
|
|
gfxWarning() << "Attempt to map D2D1 DrawTarget for writing.";
|
2015-11-26 10:38:00 +03:00
|
|
|
return false;
|
2014-01-16 16:17:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
D2D1_MAPPED_RECT map;
|
2015-06-11 20:06:23 +03:00
|
|
|
if (FAILED(mBitmap->Map(D2D1_MAP_OPTIONS_READ, &map))) {
|
2015-12-29 22:03:58 +03:00
|
|
|
gfxCriticalError() << "Failed to map bitmap (M).";
|
2015-06-11 20:06:23 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-16 16:17:24 +04:00
|
|
|
aMappedSurface->mData = map.bits;
|
|
|
|
aMappedSurface->mStride = map.pitch;
|
|
|
|
|
2015-03-03 19:17:55 +03:00
|
|
|
mIsMapped = !!aMappedSurface->mData;
|
|
|
|
return mIsMapped;
|
2014-01-16 16:17:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceSurfaceD2D1::Unmap() {
|
2014-03-07 18:55:16 +04:00
|
|
|
MOZ_ASSERT(mIsMapped);
|
|
|
|
|
|
|
|
mIsMapped = false;
|
2014-01-16 16:17:24 +04:00
|
|
|
mBitmap->Unmap();
|
|
|
|
}
|
|
|
|
|
2013-07-17 16:12:22 +04:00
|
|
|
int32_t DataSourceSurfaceD2D1::Stride() {
|
|
|
|
EnsureMapped();
|
|
|
|
|
|
|
|
return mMap.pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceSurfaceD2D1::EnsureMapped() {
|
2014-01-16 16:17:24 +04:00
|
|
|
// Do not use GetData() after having used Map!
|
|
|
|
MOZ_ASSERT(!mIsMapped);
|
2018-11-07 00:04:02 +03:00
|
|
|
if (mImplicitMapped) {
|
2013-07-17 16:12:22 +04:00
|
|
|
return;
|
|
|
|
}
|
2015-06-11 20:06:23 +03:00
|
|
|
if (FAILED(mBitmap->Map(D2D1_MAP_OPTIONS_READ, &mMap))) {
|
2015-12-29 22:03:58 +03:00
|
|
|
gfxCriticalError() << "Failed to map bitmap (EM).";
|
2015-06-11 20:06:23 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-11-07 00:04:02 +03:00
|
|
|
mImplicitMapped = true;
|
2013-07-17 16:12:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|