1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "DrawTargetCapture.h"
#include "DrawCommand.h"
#include "DrawCommands.h"
#include "gfxPlatform.h"
#include "SourceSurfaceCapture.h"
#include "FilterNodeCapture.h"
#include "PathCapture.h"
namespace mozilla {
namespace gfx {
DrawTargetCaptureImpl::~DrawTargetCaptureImpl() {
if (mSnapshot && !mSnapshot->hasOneRef()) {
mSnapshot->DrawTargetWillDestroy();
mSnapshot = nullptr;
}
}
DrawTargetCaptureImpl::DrawTargetCaptureImpl(gfx::DrawTarget* aTarget,
size_t aFlushBytes)
: mSnapshot(nullptr),
mStride(0),
mSurfaceAllocationSize(0),
mFlushBytes(aFlushBytes) {
mSize = aTarget->GetSize();
mCurrentClipBounds.push(IntRect(IntPoint(0, 0), mSize));
mFormat = aTarget->GetFormat();
SetPermitSubpixelAA(aTarget->GetPermitSubpixelAA());
mRefDT = aTarget;
}
DrawTargetCaptureImpl::DrawTargetCaptureImpl(BackendType aBackend,
const IntSize& aSize,
SurfaceFormat aFormat)
: mSize(aSize),
mSnapshot(nullptr),
mStride(0),
mSurfaceAllocationSize(0),
mFlushBytes(0) {
RefPtr<DrawTarget> screenRefDT =
gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
mCurrentClipBounds.push(IntRect(IntPoint(0, 0), aSize));
mFormat = aFormat;
SetPermitSubpixelAA(IsOpaque(mFormat));
if (aBackend == screenRefDT->GetBackendType()) {
mRefDT = screenRefDT;
} else {
// This situation can happen if a blur operation decides to
// use an unaccelerated path even if the system backend is
// Direct2D.
//
// We don't really want to encounter the reverse scenario:
// we shouldn't pick an accelerated backend if the system
// backend is skia.
if (aBackend == BackendType::DIRECT2D1_1) {
gfxWarning() << "Creating a RefDT in DrawTargetCapture.";
}
// Create a 1x1 size ref dt to create assets
// If we have to snapshot, we'll just create the real DT
IntSize size(1, 1);
mRefDT = Factory::CreateDrawTarget(aBackend, size, mFormat);
}
}
bool DrawTargetCaptureImpl::Init(const IntSize& aSize, DrawTarget* aRefDT) {
if (!aRefDT) {
return false;
}
mRefDT = aRefDT;
mSize = aSize;
mCurrentClipBounds.push(IntRect(IntPoint(0, 0), aSize));
mFormat = aRefDT->GetFormat();
SetPermitSubpixelAA(IsOpaque(mFormat));
return true;
}
void DrawTargetCaptureImpl::InitForData(int32_t aStride,
size_t aSurfaceAllocationSize) {
MOZ_ASSERT(!mFlushBytes);
mStride = aStride;
mSurfaceAllocationSize = aSurfaceAllocationSize;
}
already_AddRefed<SourceSurface> DrawTargetCaptureImpl::Snapshot() {
if (!mSnapshot) {
mSnapshot = new SourceSurfaceCapture(this);
}
RefPtr<SourceSurface> surface = mSnapshot;
return surface.forget();
}
already_AddRefed<SourceSurface> DrawTargetCaptureImpl::IntoLuminanceSource(
LuminanceType aLuminanceType, float aOpacity) {
RefPtr<SourceSurface> surface =
new SourceSurfaceCapture(this, aLuminanceType, aOpacity);
return surface.forget();
}
already_AddRefed<SourceSurface> DrawTargetCaptureImpl::OptimizeSourceSurface(
SourceSurface* aSurface) const {
// If the surface is a recording, make sure it gets resolved on the paint
// thread.
if (aSurface->GetType() == SurfaceType::CAPTURE) {
RefPtr<SourceSurface> surface = aSurface;
return surface.forget();
}
RefPtr<SourceSurfaceCapture> surface = new SourceSurfaceCapture(
const_cast<DrawTargetCaptureImpl*>(this), aSurface);
return surface.forget();
}
void DrawTargetCaptureImpl::DetachAllSnapshots() { MarkChanged(); }
#define AppendCommand(arg) new (AppendToCommandList<arg>()) arg
#define ReuseOrAppendCommand(arg) new (ReuseOrAppendToCommandList<arg>()) arg
void DrawTargetCaptureImpl::SetPermitSubpixelAA(bool aPermitSubpixelAA) {
// Save memory by eliminating state changes with no effect
if (mPermitSubpixelAA == aPermitSubpixelAA) {
return;
}
ReuseOrAppendCommand(SetPermitSubpixelAACommand)(aPermitSubpixelAA);
// Have to update mPermitSubpixelAA for this DT
// because some code paths query the current setting
// to determine subpixel AA eligibility.
DrawTarget::SetPermitSubpixelAA(aPermitSubpixelAA);
}
void DrawTargetCaptureImpl::DrawSurface(SourceSurface* aSurface,
const Rect& aDest, const Rect& aSource,
const DrawSurfaceOptions& aSurfOptions,
const DrawOptions& aOptions) {
aSurface->GuaranteePersistance();
AppendCommand(DrawSurfaceCommand)(aSurface, aDest, aSource, aSurfOptions,
aOptions);
}
void DrawTargetCaptureImpl::DrawSurfaceWithShadow(
SourceSurface* aSurface, const Point& aDest, const DeviceColor& aColor,
const Point& aOffset, Float aSigma, CompositionOp aOperator) {
aSurface->GuaranteePersistance();
AppendCommand(DrawSurfaceWithShadowCommand)(aSurface, aDest, aColor, aOffset,
aSigma, aOperator);
}
void DrawTargetCaptureImpl::DrawFilter(FilterNode* aNode,
const Rect& aSourceRect,
const Point& aDestPoint,
const DrawOptions& aOptions) {
// @todo XXX - this won't work properly long term yet due to filternodes not
// being immutable.
AppendCommand(DrawFilterCommand)(aNode, aSourceRect, aDestPoint, aOptions);
}
void DrawTargetCaptureImpl::ClearRect(const Rect& aRect) {
AppendCommand(ClearRectCommand)(aRect);
}
void DrawTargetCaptureImpl::MaskSurface(const Pattern& aSource,
SourceSurface* aMask, Point aOffset,
const DrawOptions& aOptions) {
aMask->GuaranteePersistance();
AppendCommand(MaskSurfaceCommand)(aSource, aMask, aOffset, aOptions);
}
void DrawTargetCaptureImpl::CopySurface(SourceSurface* aSurface,
const IntRect& aSourceRect,
const IntPoint& aDestination) {
aSurface->GuaranteePersistance();
AppendCommand(CopySurfaceCommand)(aSurface, aSourceRect, aDestination);
}
void DrawTargetCaptureImpl::CopyRect(const IntRect& aSourceRect,
const IntPoint& aDestination) {
AppendCommand(CopyRectCommand)(aSourceRect, aDestination);
}
void DrawTargetCaptureImpl::FillRect(const Rect& aRect, const Pattern& aPattern,
const DrawOptions& aOptions) {
AppendCommand(FillRectCommand)(aRect, aPattern, aOptions);
}
void DrawTargetCaptureImpl::FillRoundedRect(const RoundedRect& aRect,
const Pattern& aPattern,
const DrawOptions& aOptions) {
AppendCommand(FillRoundedRectCommand)(aRect, aPattern, aOptions);
}
void DrawTargetCaptureImpl::StrokeRect(const Rect& aRect,
const Pattern& aPattern,
const StrokeOptions& aStrokeOptions,
const DrawOptions& aOptions) {
AppendCommand(StrokeRectCommand)(aRect, aPattern, aStrokeOptions, aOptions);
}
void DrawTargetCaptureImpl::StrokeLine(const Point& aStart, const Point& aEnd,
const Pattern& aPattern,
const StrokeOptions& aStrokeOptions,
const DrawOptions& aOptions) {
AppendCommand(StrokeLineCommand)(aStart, aEnd, aPattern, aStrokeOptions,
aOptions);
}
void DrawTargetCaptureImpl::Stroke(const Path* aPath, const Pattern& aPattern,
const StrokeOptions& aStrokeOptions,
const DrawOptions& aOptions) {
AppendCommand(StrokeCommand)(aPath, aPattern, aStrokeOptions, aOptions);
}
void DrawTargetCaptureImpl::Fill(const Path* aPath, const Pattern& aPattern,
const DrawOptions& aOptions) {
AppendCommand(FillCommand)(aPath, aPattern, aOptions);
}
void DrawTargetCaptureImpl::FillGlyphs(ScaledFont* aFont,
const GlyphBuffer& aBuffer,
const Pattern& aPattern,
const DrawOptions& aOptions) {
AppendCommand(FillGlyphsCommand)(aFont, aBuffer, aPattern, aOptions);
}
void DrawTargetCaptureImpl::StrokeGlyphs(ScaledFont* aFont,
const GlyphBuffer& aBuffer,
const Pattern& aPattern,
const StrokeOptions& aStrokeOptions,
const DrawOptions& aOptions) {
AppendCommand(StrokeGlyphsCommand)(aFont, aBuffer, aPattern, aStrokeOptions,
aOptions);
}
void DrawTargetCaptureImpl::Mask(const Pattern& aSource, const Pattern& aMask,
const DrawOptions& aOptions) {
AppendCommand(MaskCommand)(aSource, aMask, aOptions);
}
void DrawTargetCaptureImpl::PushClip(const Path* aPath) {
// We need Pushes and Pops to match so instead of trying
// to compute the bounds of the path just repush the current
// bounds.
mCurrentClipBounds.push(mCurrentClipBounds.top());
AppendCommand(PushClipCommand)(aPath);
}
void DrawTargetCaptureImpl::PushClipRect(const Rect& aRect) {
IntRect deviceRect = RoundedOut(mTransform.TransformBounds(aRect));
mCurrentClipBounds.push(mCurrentClipBounds.top().Intersect(deviceRect));
AppendCommand(PushClipRectCommand)(aRect);
}
void DrawTargetCaptureImpl::PushLayer(bool aOpaque, Float aOpacity,
SourceSurface* aMask,
const Matrix& aMaskTransform,
const IntRect& aBounds,
bool aCopyBackground) {
// Have to update mPermitSubpixelAA for this DT
// because some code paths query the current setting
// to determine subpixel AA eligibility.
PushedLayer layer(GetPermitSubpixelAA());
mPushedLayers.push_back(layer);
DrawTarget::SetPermitSubpixelAA(aOpaque);
if (aMask) {
aMask->GuaranteePersistance();
}
AppendCommand(PushLayerCommand)(aOpaque, aOpacity, aMask, aMaskTransform,
aBounds, aCopyBackground);
}
void DrawTargetCaptureImpl::PopLayer() {
MOZ_ASSERT(mPushedLayers.size());
DrawTarget::SetPermitSubpixelAA(mPushedLayers.back().mOldPermitSubpixelAA);
mPushedLayers.pop_back();
AppendCommand(PopLayerCommand)();
}
void DrawTargetCaptureImpl::PopClip() {
mCurrentClipBounds.pop();
AppendCommand(PopClipCommand)();
}
void DrawTargetCaptureImpl::SetTransform(const Matrix& aTransform) {
// Save memory by eliminating state changes with no effect
if (mTransform.ExactlyEquals(aTransform)) {
return;
}
ReuseOrAppendCommand(SetTransformCommand)(aTransform);
// Have to update the transform for this DT
// because some code paths query the current transform
// to render specific things.
DrawTarget::SetTransform(aTransform);
}
void DrawTargetCaptureImpl::Blur(const AlphaBoxBlur& aBlur) {
// gfxAlphaBoxBlur should not use this if it takes the accelerated path.
MOZ_ASSERT(GetBackendType() == BackendType::SKIA);
AppendCommand(BlurCommand)(aBlur);
}
void DrawTargetCaptureImpl::PadEdges(const IntRegion& aRegion) {
AppendCommand(PadEdgesCommand)(aRegion);
}
void DrawTargetCaptureImpl::ReplayToDrawTarget(DrawTarget* aDT,
const Matrix& aTransform) {
for (CaptureCommandList::iterator iter(mCommands); !iter.Done();
iter.Next()) {
DrawingCommand* cmd = iter.Get();
cmd->ExecuteOnDT(aDT, &aTransform);
}
}
void DrawTargetCaptureImpl::MarkChanged() {
if (!mSnapshot) {
return;
}
if (mSnapshot->hasOneRef()) {
mSnapshot = nullptr;
return;
}
mSnapshot->DrawTargetWillChange();
mSnapshot = nullptr;
}
already_AddRefed<DrawTarget> DrawTargetCaptureImpl::CreateSimilarDrawTarget(
const IntSize& aSize, SurfaceFormat aFormat) const {
return MakeAndAddRef<DrawTargetCaptureImpl>(GetBackendType(), aSize, aFormat);
}
RefPtr<DrawTarget> DrawTargetCaptureImpl::CreateClippedDrawTarget(
const Rect& aBounds, SurfaceFormat aFormat) {
IntRect& bounds = mCurrentClipBounds.top();
auto dt = MakeRefPtr<DrawTargetCaptureImpl>(GetBackendType(), bounds.Size(),
aFormat);
RefPtr<DrawTarget> result =
gfx::Factory::CreateOffsetDrawTarget(dt, bounds.TopLeft());
result->SetTransform(mTransform);
return result;
}
RefPtr<DrawTarget> DrawTargetCaptureImpl::CreateSimilarRasterTarget(
const IntSize& aSize, SurfaceFormat aFormat) const {
MOZ_ASSERT(!mRefDT->IsCaptureDT());
return mRefDT->CreateSimilarDrawTarget(aSize, aFormat);
}
already_AddRefed<PathBuilder> DrawTargetCaptureImpl::CreatePathBuilder(
FillRule aFillRule) const {
if (mRefDT->GetBackendType() == BackendType::DIRECT2D1_1) {
return MakeRefPtr<PathBuilderCapture>(aFillRule, mRefDT).forget();
}
return mRefDT->CreatePathBuilder(aFillRule);
}
already_AddRefed<FilterNode> DrawTargetCaptureImpl::CreateFilter(
FilterType aType) {
if (mRefDT->GetBackendType() == BackendType::DIRECT2D1_1) {
return MakeRefPtr<FilterNodeCapture>(aType).forget();
} else {
return mRefDT->CreateFilter(aType);
}
}
bool DrawTargetCaptureImpl::IsEmpty() const { return mCommands.IsEmpty(); }
void DrawTargetCaptureImpl::Dump() {
TreeLog<> output;
output << "DrawTargetCapture(" << (void*)(this) << ")\n";
TreeAutoIndent<> indent(output);
mCommands.Log(output);
output << "\n";
}
} // namespace gfx
} // namespace mozilla
|