Bug 1686855 [wpt PR 27197] - PlzDedicatedWorker: WPT for clients.matchAll() with...
[gecko.git] / gfx / 2d / SourceSurfaceD2D1.cpp
blob659b500c59bb1867e8d3f1ca5d5fd525b54f923b
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 mDrawTarget(aDT) {
21 aImage->QueryInterface((ID2D1Bitmap1**)getter_AddRefs(mRealizedBitmap));
23 mFormat = aFormat;
24 mSize = aSize;
25 if (aDT) {
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;
38 if (mSnapshotLock) {
39 lock.emplace(*mSnapshotLock);
42 if (!EnsureRealizedBitmap()) {
43 gfxCriticalError() << "Failed to realize a bitmap, device "
44 << hexa(mDevice);
45 return nullptr;
48 HRESULT hr;
50 RefPtr<ID2D1Bitmap1> softwareBitmap;
51 D2D1_BITMAP_PROPERTIES1 props;
52 props.dpiX = 96;
53 props.dpiY = 96;
54 props.pixelFormat = D2DPixelFormat(mFormat);
55 props.colorContext = nullptr;
56 props.bitmapOptions =
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));
61 if (FAILED(hr)) {
62 gfxCriticalError() << "Failed to create software bitmap: " << mSize
63 << " Code: " << hexa(hr);
64 return nullptr;
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);
72 if (FAILED(hr)) {
73 gfxWarning() << "Failed to readback into software bitmap. Code: "
74 << hexa(hr);
75 return nullptr;
78 return MakeAndAddRef<DataSourceSurfaceD2D1>(softwareBitmap, mFormat);
81 bool SourceSurfaceD2D1::EnsureRealizedBitmap() {
82 if (mRealizedBitmap) {
83 return true;
86 // Why aren't we using mDevice here or anywhere else?
87 RefPtr<ID2D1Device> device = Factory::GetD2D1Device();
88 if (!device) {
89 return false;
92 RefPtr<ID2D1DeviceContext> dc;
93 device->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
94 getter_AddRefs(dc));
96 D2D1_BITMAP_PROPERTIES1 props;
97 props.dpiX = 96;
98 props.dpiY = 96;
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);
107 dc->BeginDraw();
108 dc->DrawImage(mImage);
109 dc->EndDraw();
111 return true;
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;
124 props.dpiX = 96;
125 props.dpiY = 96;
126 props.pixelFormat = D2DPixelFormat(mFormat);
127 props.colorContext = nullptr;
128 props.bitmapOptions = D2D1_BITMAP_OPTIONS_TARGET;
129 HRESULT hr =
130 mDC->CreateBitmap(D2DIntSize(mSize), nullptr, 0, props,
131 (ID2D1Bitmap1**)getter_AddRefs(mRealizedBitmap));
133 if (FAILED(hr)) {
134 gfxCriticalError()
135 << "Failed to create bitmap to make DrawTarget copy. Size: " << mSize
136 << " Code: " << hexa(hr);
137 MarkIndependent();
138 return;
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.
152 MarkIndependent();
155 void SourceSurfaceD2D1::MarkIndependent() {
156 if (mDrawTarget) {
157 MOZ_ASSERT(mDrawTarget->mSnapshot == this);
158 mDrawTarget->mSnapshot = nullptr;
159 mDrawTarget = nullptr;
163 DataSourceSurfaceD2D1::DataSourceSurfaceD2D1(ID2D1Bitmap1* aMappableBitmap,
164 SurfaceFormat aFormat)
165 : mBitmap(aMappableBitmap),
166 mFormat(aFormat),
167 mIsMapped(false),
168 mImplicitMapped(false) {}
170 DataSourceSurfaceD2D1::~DataSourceSurfaceD2D1() {
171 if (mImplicitMapped) {
172 mBitmap->Unmap();
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() {
183 EnsureMapped();
185 return mMap.bits;
188 bool DataSourceSurfaceD2D1::Map(MapType aMapType,
189 MappedSurface* aMappedSurface) {
190 // DataSourceSurfaces used with the new Map API should not be used with
191 // GetData!!
192 MOZ_ASSERT(!mImplicitMapped);
193 MOZ_ASSERT(!mIsMapped);
195 D2D1_MAP_OPTIONS options;
196 if (aMapType == MapType::READ) {
197 options = D2D1_MAP_OPTIONS_READ;
198 } else {
199 gfxWarning() << "Attempt to map D2D1 DrawTarget for writing.";
200 return false;
203 D2D1_MAPPED_RECT map;
204 if (FAILED(mBitmap->Map(D2D1_MAP_OPTIONS_READ, &map))) {
205 gfxCriticalError() << "Failed to map bitmap (M).";
206 return false;
208 aMappedSurface->mData = map.bits;
209 aMappedSurface->mStride = map.pitch;
211 mIsMapped = !!aMappedSurface->mData;
212 return mIsMapped;
215 void DataSourceSurfaceD2D1::Unmap() {
216 MOZ_ASSERT(mIsMapped);
218 mIsMapped = false;
219 mBitmap->Unmap();
222 int32_t DataSourceSurfaceD2D1::Stride() {
223 EnsureMapped();
225 return mMap.pitch;
228 void DataSourceSurfaceD2D1::EnsureMapped() {
229 // Do not use GetData() after having used Map!
230 MOZ_ASSERT(!mIsMapped);
231 if (mImplicitMapped) {
232 return;
234 if (FAILED(mBitmap->Map(D2D1_MAP_OPTIONS_READ, &mMap))) {
235 gfxCriticalError() << "Failed to map bitmap (EM).";
236 return;
238 mImplicitMapped = true;
241 } // namespace gfx
242 } // namespace mozilla