1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "SourceSurfaceD2D1.h"
7 #include "DrawTargetD2D1.h"
13 SourceSurfaceD2D1::SourceSurfaceD2D1(ID2D1Image
*aImage
, ID2D1DeviceContext
*aDC
,
14 SurfaceFormat aFormat
, const IntSize
&aSize
,
20 aImage
->QueryInterface((ID2D1Bitmap1
**)byRef(mRealizedBitmap
));
26 SourceSurfaceD2D1::~SourceSurfaceD2D1()
30 TemporaryRef
<DataSourceSurface
>
31 SourceSurfaceD2D1::GetDataSurface()
33 EnsureRealizedBitmap();
35 RefPtr
<ID2D1Bitmap1
> softwareBitmap
;
36 D2D1_BITMAP_PROPERTIES1 props
;
39 props
.pixelFormat
= D2DPixelFormat(mFormat
);
40 props
.colorContext
= nullptr;
41 props
.bitmapOptions
= D2D1_BITMAP_OPTIONS_CANNOT_DRAW
|
42 D2D1_BITMAP_OPTIONS_CPU_READ
;
43 mDC
->CreateBitmap(D2DIntSize(mSize
), nullptr, 0, props
, (ID2D1Bitmap1
**)byRef(softwareBitmap
));
45 D2D1_POINT_2U point
= D2D1::Point2U(0, 0);
46 D2D1_RECT_U rect
= D2D1::RectU(0, 0, mSize
.width
, mSize
.height
);
47 softwareBitmap
->CopyFromBitmap(&point
, mRealizedBitmap
, &rect
);
48 return new DataSourceSurfaceD2D1(softwareBitmap
, mFormat
);
52 SourceSurfaceD2D1::EnsureRealizedBitmap()
54 if (mRealizedBitmap
) {
58 RefPtr
<ID2D1DeviceContext
> dc
;
59 Factory::GetD2D1Device()->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE
, byRef(dc
));
61 D2D1_BITMAP_PROPERTIES1 props
;
64 props
.pixelFormat
= D2DPixelFormat(mFormat
);
65 props
.colorContext
= nullptr;
66 props
.bitmapOptions
= D2D1_BITMAP_OPTIONS_TARGET
;
67 dc
->CreateBitmap(D2DIntSize(mSize
), nullptr, 0, props
, (ID2D1Bitmap1
**)byRef(mRealizedBitmap
));
69 dc
->SetTarget(mRealizedBitmap
);
72 dc
->DrawImage(mImage
);
77 SourceSurfaceD2D1::DrawTargetWillChange()
79 // At this point in time this should always be true here.
80 MOZ_ASSERT(mRealizedBitmap
);
82 RefPtr
<ID2D1Bitmap1
> oldBitmap
= mRealizedBitmap
;
84 D2D1_BITMAP_PROPERTIES1 props
;
87 props
.pixelFormat
= D2DPixelFormat(mFormat
);
88 props
.colorContext
= nullptr;
89 props
.bitmapOptions
= D2D1_BITMAP_OPTIONS_TARGET
;
90 mDC
->CreateBitmap(D2DIntSize(mSize
), nullptr, 0, props
, (ID2D1Bitmap1
**)byRef(mRealizedBitmap
));
92 D2D1_POINT_2U point
= D2D1::Point2U(0, 0);
93 D2D1_RECT_U rect
= D2D1::RectU(0, 0, mSize
.width
, mSize
.height
);
94 mRealizedBitmap
->CopyFromBitmap(&point
, oldBitmap
, &rect
);
95 mImage
= mRealizedBitmap
;
97 DrawTargetD2D1::mVRAMUsageSS
+= mSize
.width
* mSize
.height
* BytesPerPixel(mFormat
);
98 mDrawTarget
= nullptr;
100 // We now no longer depend on the source surface content remaining the same.
105 SourceSurfaceD2D1::MarkIndependent()
108 MOZ_ASSERT(mDrawTarget
->mSnapshot
== this);
109 mDrawTarget
->mSnapshot
= nullptr;
110 mDrawTarget
= nullptr;
114 DataSourceSurfaceD2D1::DataSourceSurfaceD2D1(ID2D1Bitmap1
*aMappableBitmap
, SurfaceFormat aFormat
)
115 : mBitmap(aMappableBitmap
)
121 DataSourceSurfaceD2D1::~DataSourceSurfaceD2D1()
129 DataSourceSurfaceD2D1::GetSize() const
131 D2D1_SIZE_F size
= mBitmap
->GetSize();
133 return IntSize(int32_t(size
.width
), int32_t(size
.height
));
137 DataSourceSurfaceD2D1::GetData()
145 DataSourceSurfaceD2D1::Stride()
153 DataSourceSurfaceD2D1::EnsureMapped()
158 mBitmap
->Map(D2D1_MAP_OPTIONS_READ
, &mMap
);