1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "SourceSurfaceD2D1.h"
8 #include "DrawTargetD2D1.h"
13 SourceSurfaceD2D1::SourceSurfaceD2D1(ID2D1Image
* aImage
,
14 ID2D1DeviceContext
* aDC
,
15 SurfaceFormat aFormat
,
16 const IntSize
& aSize
, DrawTargetD2D1
* aDT
)
19 mDevice(Factory::GetD2D1Device()),
24 aImage
->QueryInterface((ID2D1Bitmap1
**)getter_AddRefs(mRealizedBitmap
));
26 mSnapshotLock
= aDT
->mSnapshotLock
;
30 SourceSurfaceD2D1::~SourceSurfaceD2D1() {
32 DrawTargetD2D1::mVRAMUsageSS
-=
33 mSize
.width
* mSize
.height
* BytesPerPixel(mFormat
);
37 bool SourceSurfaceD2D1::IsValid() const {
38 return mDevice
== Factory::GetD2D1Device();
41 already_AddRefed
<DataSourceSurface
> SourceSurfaceD2D1::GetDataSurface() {
42 Maybe
<MutexAutoLock
> lock
;
44 lock
.emplace(*mSnapshotLock
);
47 if (!EnsureRealizedBitmap()) {
48 gfxCriticalError() << "Failed to realize a bitmap, device "
55 RefPtr
<ID2D1Bitmap1
> softwareBitmap
;
56 D2D1_BITMAP_PROPERTIES1 props
;
59 props
.pixelFormat
= D2DPixelFormat(mFormat
);
60 props
.colorContext
= nullptr;
62 D2D1_BITMAP_OPTIONS_CANNOT_DRAW
| D2D1_BITMAP_OPTIONS_CPU_READ
;
63 hr
= mDC
->CreateBitmap(D2DIntSize(mSize
), nullptr, 0, props
,
64 (ID2D1Bitmap1
**)getter_AddRefs(softwareBitmap
));
67 gfxCriticalError() << "Failed to create software bitmap: " << mSize
68 << " Code: " << hexa(hr
);
72 D2D1_POINT_2U point
= D2D1::Point2U(0, 0);
73 D2D1_RECT_U rect
= D2D1::RectU(0, 0, mSize
.width
, mSize
.height
);
75 hr
= softwareBitmap
->CopyFromBitmap(&point
, mRealizedBitmap
, &rect
);
78 gfxWarning() << "Failed to readback into software bitmap. Code: "
83 return MakeAndAddRef
<DataSourceSurfaceD2D1
>(softwareBitmap
, mFormat
);
86 bool SourceSurfaceD2D1::EnsureRealizedBitmap() {
87 if (mRealizedBitmap
) {
91 // Why aren't we using mDevice here or anywhere else?
92 RefPtr
<ID2D1Device
> device
= Factory::GetD2D1Device();
97 RefPtr
<ID2D1DeviceContext
> dc
;
98 device
->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE
,
101 D2D1_BITMAP_PROPERTIES1 props
;
104 props
.pixelFormat
= D2DPixelFormat(mFormat
);
105 props
.colorContext
= nullptr;
106 props
.bitmapOptions
= D2D1_BITMAP_OPTIONS_TARGET
;
107 dc
->CreateBitmap(D2DIntSize(mSize
), nullptr, 0, props
,
108 (ID2D1Bitmap1
**)getter_AddRefs(mRealizedBitmap
));
110 dc
->SetTarget(mRealizedBitmap
);
113 dc
->DrawImage(mImage
);
119 void SourceSurfaceD2D1::DrawTargetWillChange() {
120 MOZ_ASSERT(mSnapshotLock
);
121 mSnapshotLock
->AssertCurrentThreadOwns();
123 // At this point in time this should always be true here.
124 MOZ_ASSERT(mRealizedBitmap
);
126 RefPtr
<ID2D1Bitmap1
> oldBitmap
= mRealizedBitmap
;
128 D2D1_BITMAP_PROPERTIES1 props
;
131 props
.pixelFormat
= D2DPixelFormat(mFormat
);
132 props
.colorContext
= nullptr;
133 props
.bitmapOptions
= D2D1_BITMAP_OPTIONS_TARGET
;
135 mDC
->CreateBitmap(D2DIntSize(mSize
), nullptr, 0, props
,
136 (ID2D1Bitmap1
**)getter_AddRefs(mRealizedBitmap
));
140 << "Failed to create bitmap to make DrawTarget copy. Size: " << mSize
141 << " Code: " << hexa(hr
);
146 D2D1_POINT_2U point
= D2D1::Point2U(0, 0);
147 D2D1_RECT_U rect
= D2D1::RectU(0, 0, mSize
.width
, mSize
.height
);
148 mRealizedBitmap
->CopyFromBitmap(&point
, oldBitmap
, &rect
);
149 mImage
= mRealizedBitmap
;
151 DrawTargetD2D1::mVRAMUsageSS
+=
152 mSize
.width
* mSize
.height
* BytesPerPixel(mFormat
);
155 // Ensure the object stays alive for the duration of MarkIndependent.
156 RefPtr
<SourceSurfaceD2D1
> deathGrip
= this;
157 // We now no longer depend on the source surface content remaining the same.
161 void SourceSurfaceD2D1::MarkIndependent() {
163 MOZ_ASSERT(mDrawTarget
->mSnapshot
== this);
164 mDrawTarget
->mSnapshot
= nullptr;
165 mDrawTarget
= nullptr;
169 DataSourceSurfaceD2D1::DataSourceSurfaceD2D1(ID2D1Bitmap1
* aMappableBitmap
,
170 SurfaceFormat aFormat
)
171 : mBitmap(aMappableBitmap
),
174 mImplicitMapped(false) {}
176 DataSourceSurfaceD2D1::~DataSourceSurfaceD2D1() {
177 if (mImplicitMapped
) {
182 IntSize
DataSourceSurfaceD2D1::GetSize() const {
183 D2D1_SIZE_F size
= mBitmap
->GetSize();
185 return IntSize(int32_t(size
.width
), int32_t(size
.height
));
188 uint8_t* DataSourceSurfaceD2D1::GetData() {
194 bool DataSourceSurfaceD2D1::Map(MapType aMapType
,
195 MappedSurface
* aMappedSurface
) {
196 // DataSourceSurfaces used with the new Map API should not be used with
198 MOZ_ASSERT(!mImplicitMapped
);
199 MOZ_ASSERT(!mIsMapped
);
201 if (aMapType
!= MapType::READ
) {
202 gfxWarning() << "Attempt to map D2D1 DrawTarget for writing.";
206 D2D1_MAPPED_RECT map
;
207 if (FAILED(mBitmap
->Map(D2D1_MAP_OPTIONS_READ
, &map
))) {
208 gfxCriticalError() << "Failed to map bitmap (M).";
211 aMappedSurface
->mData
= map
.bits
;
212 aMappedSurface
->mStride
= map
.pitch
;
214 mIsMapped
= !!aMappedSurface
->mData
;
218 void DataSourceSurfaceD2D1::Unmap() {
219 MOZ_ASSERT(mIsMapped
);
225 int32_t DataSourceSurfaceD2D1::Stride() {
231 void DataSourceSurfaceD2D1::EnsureMapped() {
232 // Do not use GetData() after having used Map!
233 MOZ_ASSERT(!mIsMapped
);
234 if (mImplicitMapped
) {
237 if (FAILED(mBitmap
->Map(D2D1_MAP_OPTIONS_READ
, &mMap
))) {
238 gfxCriticalError() << "Failed to map bitmap (EM).";
241 mImplicitMapped
= true;
245 } // namespace mozilla