Bug 1866777 - Disable test_race_cache_with_network.js on windows opt for frequent...
[gecko.git] / gfx / 2d / SourceSurfaceD2D1.cpp
blob9aef7ab54e99c7c511d5a7fcba4566e870b6bc88
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"
10 namespace mozilla {
11 namespace gfx {
13 SourceSurfaceD2D1::SourceSurfaceD2D1(ID2D1Image* aImage,
14 ID2D1DeviceContext* aDC,
15 SurfaceFormat aFormat,
16 const IntSize& aSize, DrawTargetD2D1* aDT)
17 : mImage(aImage),
18 mDC(aDC),
19 mDevice(Factory::GetD2D1Device()),
20 mFormat(aFormat),
21 mSize(aSize),
22 mDrawTarget(aDT),
23 mOwnsCopy(false) {
24 aImage->QueryInterface((ID2D1Bitmap1**)getter_AddRefs(mRealizedBitmap));
25 if (aDT) {
26 mSnapshotLock = aDT->mSnapshotLock;
30 SourceSurfaceD2D1::~SourceSurfaceD2D1() {
31 if (mOwnsCopy) {
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;
43 if (mSnapshotLock) {
44 lock.emplace(*mSnapshotLock);
47 if (!EnsureRealizedBitmap()) {
48 gfxCriticalError() << "Failed to realize a bitmap, device "
49 << hexa(mDevice);
50 return nullptr;
53 HRESULT hr;
55 RefPtr<ID2D1Bitmap1> softwareBitmap;
56 D2D1_BITMAP_PROPERTIES1 props;
57 props.dpiX = 96;
58 props.dpiY = 96;
59 props.pixelFormat = D2DPixelFormat(mFormat);
60 props.colorContext = nullptr;
61 props.bitmapOptions =
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));
66 if (FAILED(hr)) {
67 gfxCriticalError() << "Failed to create software bitmap: " << mSize
68 << " Code: " << hexa(hr);
69 return nullptr;
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);
77 if (FAILED(hr)) {
78 gfxWarning() << "Failed to readback into software bitmap. Code: "
79 << hexa(hr);
80 return nullptr;
83 return MakeAndAddRef<DataSourceSurfaceD2D1>(softwareBitmap, mFormat);
86 bool SourceSurfaceD2D1::EnsureRealizedBitmap() {
87 if (mRealizedBitmap) {
88 return true;
91 // Why aren't we using mDevice here or anywhere else?
92 RefPtr<ID2D1Device> device = Factory::GetD2D1Device();
93 if (!device) {
94 return false;
97 RefPtr<ID2D1DeviceContext> dc;
98 device->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
99 getter_AddRefs(dc));
101 D2D1_BITMAP_PROPERTIES1 props;
102 props.dpiX = 96;
103 props.dpiY = 96;
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);
112 dc->BeginDraw();
113 dc->DrawImage(mImage);
114 dc->EndDraw();
116 return true;
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;
129 props.dpiX = 96;
130 props.dpiY = 96;
131 props.pixelFormat = D2DPixelFormat(mFormat);
132 props.colorContext = nullptr;
133 props.bitmapOptions = D2D1_BITMAP_OPTIONS_TARGET;
134 HRESULT hr =
135 mDC->CreateBitmap(D2DIntSize(mSize), nullptr, 0, props,
136 (ID2D1Bitmap1**)getter_AddRefs(mRealizedBitmap));
138 if (FAILED(hr)) {
139 gfxCriticalError()
140 << "Failed to create bitmap to make DrawTarget copy. Size: " << mSize
141 << " Code: " << hexa(hr);
142 MarkIndependent();
143 return;
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);
153 mOwnsCopy = true;
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.
158 MarkIndependent();
161 void SourceSurfaceD2D1::MarkIndependent() {
162 if (mDrawTarget) {
163 MOZ_ASSERT(mDrawTarget->mSnapshot == this);
164 mDrawTarget->mSnapshot = nullptr;
165 mDrawTarget = nullptr;
169 DataSourceSurfaceD2D1::DataSourceSurfaceD2D1(ID2D1Bitmap1* aMappableBitmap,
170 SurfaceFormat aFormat)
171 : mBitmap(aMappableBitmap),
172 mFormat(aFormat),
173 mIsMapped(false),
174 mImplicitMapped(false) {}
176 DataSourceSurfaceD2D1::~DataSourceSurfaceD2D1() {
177 if (mImplicitMapped) {
178 mBitmap->Unmap();
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() {
189 EnsureMapped();
191 return mMap.bits;
194 bool DataSourceSurfaceD2D1::Map(MapType aMapType,
195 MappedSurface* aMappedSurface) {
196 // DataSourceSurfaces used with the new Map API should not be used with
197 // GetData!!
198 MOZ_ASSERT(!mImplicitMapped);
199 MOZ_ASSERT(!mIsMapped);
201 if (aMapType != MapType::READ) {
202 gfxWarning() << "Attempt to map D2D1 DrawTarget for writing.";
203 return false;
206 D2D1_MAPPED_RECT map;
207 if (FAILED(mBitmap->Map(D2D1_MAP_OPTIONS_READ, &map))) {
208 gfxCriticalError() << "Failed to map bitmap (M).";
209 return false;
211 aMappedSurface->mData = map.bits;
212 aMappedSurface->mStride = map.pitch;
214 mIsMapped = !!aMappedSurface->mData;
215 return mIsMapped;
218 void DataSourceSurfaceD2D1::Unmap() {
219 MOZ_ASSERT(mIsMapped);
221 mIsMapped = false;
222 mBitmap->Unmap();
225 int32_t DataSourceSurfaceD2D1::Stride() {
226 EnsureMapped();
228 return mMap.pitch;
231 void DataSourceSurfaceD2D1::EnsureMapped() {
232 // Do not use GetData() after having used Map!
233 MOZ_ASSERT(!mIsMapped);
234 if (mImplicitMapped) {
235 return;
237 if (FAILED(mBitmap->Map(D2D1_MAP_OPTIONS_READ, &mMap))) {
238 gfxCriticalError() << "Failed to map bitmap (EM).";
239 return;
241 mImplicitMapped = true;
244 } // namespace gfx
245 } // namespace mozilla