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()),
21 aImage
->QueryInterface((ID2D1Bitmap1
**)getter_AddRefs(mRealizedBitmap
));
26 mSnapshotLock
= aDT
->mSnapshotLock
;
30 SourceSurfaceD2D1::~SourceSurfaceD2D1() {}
32 bool SourceSurfaceD2D1::IsValid() const {
33 return mDevice
== Factory::GetD2D1Device();
36 already_AddRefed
<DataSourceSurface
> SourceSurfaceD2D1::GetDataSurface() {
37 Maybe
<MutexAutoLock
> lock
;
39 lock
.emplace(*mSnapshotLock
);
42 if (!EnsureRealizedBitmap()) {
43 gfxCriticalError() << "Failed to realize a bitmap, device "
50 RefPtr
<ID2D1Bitmap1
> softwareBitmap
;
51 D2D1_BITMAP_PROPERTIES1 props
;
54 props
.pixelFormat
= D2DPixelFormat(mFormat
);
55 props
.colorContext
= nullptr;
57 D2D1_BITMAP_OPTIONS_CANNOT_DRAW
| D2D1_BITMAP_OPTIONS_CPU_READ
;
58 hr
= mDC
->CreateBitmap(D2DIntSize(mSize
), nullptr, 0, props
,
59 (ID2D1Bitmap1
**)getter_AddRefs(softwareBitmap
));
62 gfxCriticalError() << "Failed to create software bitmap: " << mSize
63 << " Code: " << hexa(hr
);
67 D2D1_POINT_2U point
= D2D1::Point2U(0, 0);
68 D2D1_RECT_U rect
= D2D1::RectU(0, 0, mSize
.width
, mSize
.height
);
70 hr
= softwareBitmap
->CopyFromBitmap(&point
, mRealizedBitmap
, &rect
);
73 gfxWarning() << "Failed to readback into software bitmap. Code: "
78 return MakeAndAddRef
<DataSourceSurfaceD2D1
>(softwareBitmap
, mFormat
);
81 bool SourceSurfaceD2D1::EnsureRealizedBitmap() {
82 if (mRealizedBitmap
) {
86 // Why aren't we using mDevice here or anywhere else?
87 RefPtr
<ID2D1Device
> device
= Factory::GetD2D1Device();
92 RefPtr
<ID2D1DeviceContext
> dc
;
93 device
->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE
,
96 D2D1_BITMAP_PROPERTIES1 props
;
99 props
.pixelFormat
= D2DPixelFormat(mFormat
);
100 props
.colorContext
= nullptr;
101 props
.bitmapOptions
= D2D1_BITMAP_OPTIONS_TARGET
;
102 dc
->CreateBitmap(D2DIntSize(mSize
), nullptr, 0, props
,
103 (ID2D1Bitmap1
**)getter_AddRefs(mRealizedBitmap
));
105 dc
->SetTarget(mRealizedBitmap
);
108 dc
->DrawImage(mImage
);
114 void SourceSurfaceD2D1::DrawTargetWillChange() {
115 MOZ_ASSERT(mSnapshotLock
);
116 mSnapshotLock
->AssertCurrentThreadOwns();
118 // At this point in time this should always be true here.
119 MOZ_ASSERT(mRealizedBitmap
);
121 RefPtr
<ID2D1Bitmap1
> oldBitmap
= mRealizedBitmap
;
123 D2D1_BITMAP_PROPERTIES1 props
;
126 props
.pixelFormat
= D2DPixelFormat(mFormat
);
127 props
.colorContext
= nullptr;
128 props
.bitmapOptions
= D2D1_BITMAP_OPTIONS_TARGET
;
130 mDC
->CreateBitmap(D2DIntSize(mSize
), nullptr, 0, props
,
131 (ID2D1Bitmap1
**)getter_AddRefs(mRealizedBitmap
));
135 << "Failed to create bitmap to make DrawTarget copy. Size: " << mSize
136 << " Code: " << hexa(hr
);
141 D2D1_POINT_2U point
= D2D1::Point2U(0, 0);
142 D2D1_RECT_U rect
= D2D1::RectU(0, 0, mSize
.width
, mSize
.height
);
143 mRealizedBitmap
->CopyFromBitmap(&point
, oldBitmap
, &rect
);
144 mImage
= mRealizedBitmap
;
146 DrawTargetD2D1::mVRAMUsageSS
+=
147 mSize
.width
* mSize
.height
* BytesPerPixel(mFormat
);
149 // Ensure the object stays alive for the duration of MarkIndependent.
150 RefPtr
<SourceSurfaceD2D1
> deathGrip
= this;
151 // We now no longer depend on the source surface content remaining the same.
155 void SourceSurfaceD2D1::MarkIndependent() {
157 MOZ_ASSERT(mDrawTarget
->mSnapshot
== this);
158 mDrawTarget
->mSnapshot
= nullptr;
159 mDrawTarget
= nullptr;
163 DataSourceSurfaceD2D1::DataSourceSurfaceD2D1(ID2D1Bitmap1
* aMappableBitmap
,
164 SurfaceFormat aFormat
)
165 : mBitmap(aMappableBitmap
),
168 mImplicitMapped(false) {}
170 DataSourceSurfaceD2D1::~DataSourceSurfaceD2D1() {
171 if (mImplicitMapped
) {
176 IntSize
DataSourceSurfaceD2D1::GetSize() const {
177 D2D1_SIZE_F size
= mBitmap
->GetSize();
179 return IntSize(int32_t(size
.width
), int32_t(size
.height
));
182 uint8_t* DataSourceSurfaceD2D1::GetData() {
188 bool DataSourceSurfaceD2D1::Map(MapType aMapType
,
189 MappedSurface
* aMappedSurface
) {
190 // DataSourceSurfaces used with the new Map API should not be used with
192 MOZ_ASSERT(!mImplicitMapped
);
193 MOZ_ASSERT(!mIsMapped
);
195 D2D1_MAP_OPTIONS options
;
196 if (aMapType
== MapType::READ
) {
197 options
= D2D1_MAP_OPTIONS_READ
;
199 gfxWarning() << "Attempt to map D2D1 DrawTarget for writing.";
203 D2D1_MAPPED_RECT map
;
204 if (FAILED(mBitmap
->Map(D2D1_MAP_OPTIONS_READ
, &map
))) {
205 gfxCriticalError() << "Failed to map bitmap (M).";
208 aMappedSurface
->mData
= map
.bits
;
209 aMappedSurface
->mStride
= map
.pitch
;
211 mIsMapped
= !!aMappedSurface
->mData
;
215 void DataSourceSurfaceD2D1::Unmap() {
216 MOZ_ASSERT(mIsMapped
);
222 int32_t DataSourceSurfaceD2D1::Stride() {
228 void DataSourceSurfaceD2D1::EnsureMapped() {
229 // Do not use GetData() after having used Map!
230 MOZ_ASSERT(!mIsMapped
);
231 if (mImplicitMapped
) {
234 if (FAILED(mBitmap
->Map(D2D1_MAP_OPTIONS_READ
, &mMap
))) {
235 gfxCriticalError() << "Failed to map bitmap (EM).";
238 mImplicitMapped
= true;
242 } // namespace mozilla