Merge m-c to inbound.
[gecko.git] / gfx / 2d / SourceSurfaceD2D1.cpp
bloba3054048a1c30629690c36d7d61116c71e4dfc60
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"
8 #include "Tools.h"
10 namespace mozilla {
11 namespace gfx {
13 SourceSurfaceD2D1::SourceSurfaceD2D1(ID2D1Image *aImage, ID2D1DeviceContext *aDC,
14 SurfaceFormat aFormat, const IntSize &aSize,
15 DrawTargetD2D1 *aDT)
16 : mImage(aImage)
17 , mDC(aDC)
18 , mDrawTarget(aDT)
20 aImage->QueryInterface((ID2D1Bitmap1**)byRef(mRealizedBitmap));
22 mFormat = aFormat;
23 mSize = aSize;
26 SourceSurfaceD2D1::~SourceSurfaceD2D1()
30 TemporaryRef<DataSourceSurface>
31 SourceSurfaceD2D1::GetDataSurface()
33 EnsureRealizedBitmap();
35 RefPtr<ID2D1Bitmap1> softwareBitmap;
36 D2D1_BITMAP_PROPERTIES1 props;
37 props.dpiX = 96;
38 props.dpiY = 96;
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);
51 void
52 SourceSurfaceD2D1::EnsureRealizedBitmap()
54 if (mRealizedBitmap) {
55 return;
58 RefPtr<ID2D1DeviceContext> dc;
59 Factory::GetD2D1Device()->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, byRef(dc));
61 D2D1_BITMAP_PROPERTIES1 props;
62 props.dpiX = 96;
63 props.dpiY = 96;
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);
71 dc->BeginDraw();
72 dc->DrawImage(mImage);
73 dc->EndDraw();
76 void
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;
85 props.dpiX = 96;
86 props.dpiY = 96;
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.
101 MarkIndependent();
104 void
105 SourceSurfaceD2D1::MarkIndependent()
107 if (mDrawTarget) {
108 MOZ_ASSERT(mDrawTarget->mSnapshot == this);
109 mDrawTarget->mSnapshot = nullptr;
110 mDrawTarget = nullptr;
114 DataSourceSurfaceD2D1::DataSourceSurfaceD2D1(ID2D1Bitmap1 *aMappableBitmap, SurfaceFormat aFormat)
115 : mBitmap(aMappableBitmap)
116 , mFormat(aFormat)
117 , mMapped(false)
121 DataSourceSurfaceD2D1::~DataSourceSurfaceD2D1()
123 if (mMapped) {
124 mBitmap->Unmap();
128 IntSize
129 DataSourceSurfaceD2D1::GetSize() const
131 D2D1_SIZE_F size = mBitmap->GetSize();
133 return IntSize(int32_t(size.width), int32_t(size.height));
136 uint8_t*
137 DataSourceSurfaceD2D1::GetData()
139 EnsureMapped();
141 return mMap.bits;
144 int32_t
145 DataSourceSurfaceD2D1::Stride()
147 EnsureMapped();
149 return mMap.pitch;
152 void
153 DataSourceSurfaceD2D1::EnsureMapped()
155 if (mMapped) {
156 return;
158 mBitmap->Map(D2D1_MAP_OPTIONS_READ, &mMap);
159 mMapped = true;