-
Notifications
You must be signed in to change notification settings - Fork 1
/
DrawTargetD2D.cpp
2793 lines (2227 loc) · 85.2 KB
/
DrawTargetD2D.cpp
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* 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 <initguid.h>
#include "DrawTargetD2D.h"
#include "SourceSurfaceD2D.h"
#ifdef USE_D2D1_1
#include "SourceSurfaceD2D1.h"
#endif
#include "SourceSurfaceD2DTarget.h"
#include "ShadersD2D.h"
#include "PathD2D.h"
#include "GradientStopsD2D.h"
#include "ScaledFontDWrite.h"
#include "ImageScaling.h"
#include "Logging.h"
#include "Tools.h"
#include <algorithm>
#include "mozilla/Constants.h"
#include "FilterNodeSoftware.h"
#ifdef USE_D2D1_1
#include "FilterNodeD2D1.h"
#endif
#include <dwrite.h>
// decltype is not usable for overloaded functions.
typedef HRESULT (WINAPI*D2D1CreateFactoryFunc)(
D2D1_FACTORY_TYPE factoryType,
REFIID iid,
CONST D2D1_FACTORY_OPTIONS *pFactoryOptions,
void **factory
);
using namespace std;
namespace mozilla {
namespace gfx {
struct Vertex {
float x;
float y;
};
ID2D1Factory *DrawTargetD2D::mFactory;
IDWriteFactory *DrawTargetD2D::mDWriteFactory;
uint64_t DrawTargetD2D::mVRAMUsageDT;
uint64_t DrawTargetD2D::mVRAMUsageSS;
// Helper class to restore surface contents that was clipped out but may have
// been altered by a drawing call.
class AutoSaveRestoreClippedOut
{
public:
AutoSaveRestoreClippedOut(DrawTargetD2D *aDT)
: mDT(aDT)
{}
void Save() {
if (!mDT->mPushedClips.size()) {
return;
}
mDT->Flush();
RefPtr<ID3D10Texture2D> tmpTexture;
IntSize size = mDT->mSize;
SurfaceFormat format = mDT->mFormat;
CD3D10_TEXTURE2D_DESC desc(DXGIFormat(format), size.width, size.height,
1, 1);
desc.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE;
HRESULT hr = mDT->mDevice->CreateTexture2D(&desc, nullptr, byRef(tmpTexture));
if (FAILED(hr)) {
gfxCriticalError() << "[D2D] CreateTexture2D failure " << size << " Code: " << hr;
// Crash debug builds but try to recover in release builds.
MOZ_ASSERT(false);
return;
}
mDT->mDevice->CopyResource(tmpTexture, mDT->mTexture);
D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(D2DPixelFormat(format));
RefPtr<IDXGISurface> surf;
tmpTexture->QueryInterface((IDXGISurface**)byRef(surf));
hr = mDT->mRT->CreateSharedBitmap(IID_IDXGISurface, surf,
&props, byRef(mOldSurfBitmap));
if (FAILED(hr)) {
gfxCriticalError() << "[D2D] CreateSharedBitmap failure " << size << " Code: " << hr;
// Crash debug builds but try to recover in release builds.
MOZ_ASSERT(false);
return;
}
IntRect clipBounds;
mClippedArea = mDT->GetClippedGeometry(&clipBounds);
if (!clipBounds.IsEqualEdges(IntRect(IntPoint(0, 0), mDT->mSize))) {
// We still need to take into account clipBounds if it contains additional
// clipping information.
RefPtr<ID2D1RectangleGeometry> rectGeom;
factory()->CreateRectangleGeometry(D2D1::Rect(Float(clipBounds.x),
Float(clipBounds.y),
Float(clipBounds.XMost()),
Float(clipBounds.YMost())),
byRef(rectGeom));
mClippedArea = IntersectGeometry(mClippedArea, rectGeom);
}
}
ID2D1Factory *factory() { return mDT->factory(); }
~AutoSaveRestoreClippedOut()
{
if (!mOldSurfBitmap) {
return;
}
ID2D1RenderTarget *rt = mDT->mRT;
// Write the area that was clipped out back to the surface. This all
// happens in device space.
rt->SetTransform(D2D1::IdentityMatrix());
mDT->mTransformDirty = true;
RefPtr<ID2D1RectangleGeometry> rectGeom;
factory()->CreateRectangleGeometry(
D2D1::RectF(0, 0, float(mDT->mSize.width), float(mDT->mSize.height)),
byRef(rectGeom));
RefPtr<ID2D1PathGeometry> invClippedArea;
factory()->CreatePathGeometry(byRef(invClippedArea));
RefPtr<ID2D1GeometrySink> sink;
invClippedArea->Open(byRef(sink));
rectGeom->CombineWithGeometry(mClippedArea, D2D1_COMBINE_MODE_EXCLUDE, nullptr, sink);
sink->Close();
RefPtr<ID2D1BitmapBrush> brush;
rt->CreateBitmapBrush(mOldSurfBitmap, D2D1::BitmapBrushProperties(), D2D1::BrushProperties(), byRef(brush));
rt->FillGeometry(invClippedArea, brush);
}
private:
DrawTargetD2D *mDT;
// If we have an operator unbound by the source, this will contain a bitmap
// with the old dest surface data.
RefPtr<ID2D1Bitmap> mOldSurfBitmap;
// This contains the area drawing is clipped to.
RefPtr<ID2D1Geometry> mClippedArea;
};
ID2D1Factory *D2DFactory()
{
return DrawTargetD2D::factory();
}
template<int L>
inline static Log<L>&
operator <<(Log<L>& aStream, const DrawTargetD2D& aDrawTarget)
{
aStream << "DrawTargetD2D(" << &aDrawTarget << ")";
return aStream;
}
DrawTargetD2D::DrawTargetD2D()
: mCurrentCachedLayer(0)
, mClipsArePushed(false)
, mPrivateData(nullptr)
{
}
DrawTargetD2D::~DrawTargetD2D()
{
if (mRT) {
PopAllClips();
mRT->EndDraw();
mVRAMUsageDT -= GetByteSize();
}
if (mTempRT) {
mTempRT->EndDraw();
mVRAMUsageDT -= GetByteSize();
}
if (mSnapshot) {
// We may hold the only reference. MarkIndependent will clear mSnapshot;
// keep the snapshot object alive so it doesn't get destroyed while
// MarkIndependent is running.
RefPtr<SourceSurfaceD2DTarget> deathGrip = mSnapshot;
// mSnapshot can be treated as independent of this DrawTarget since we know
// this DrawTarget won't change again.
deathGrip->MarkIndependent();
// mSnapshot will be cleared now.
}
for (int i = 0; i < kLayerCacheSize; i++) {
if (mCachedLayers[i]) {
mCachedLayers[i] = nullptr;
mVRAMUsageDT -= GetByteSize();
}
}
// Targets depending on us can break that dependency, since we're obviously not going to
// be modified in the future.
for (TargetSet::iterator iter = mDependentTargets.begin();
iter != mDependentTargets.end(); iter++) {
(*iter)->mDependingOnTargets.erase(this);
}
// Our dependencies on other targets no longer matter.
for (TargetSet::iterator iter = mDependingOnTargets.begin();
iter != mDependingOnTargets.end(); iter++) {
(*iter)->mDependentTargets.erase(this);
}
}
/*
* DrawTarget Implementation
*/
TemporaryRef<SourceSurface>
DrawTargetD2D::Snapshot()
{
if (!mSnapshot) {
mSnapshot = new SourceSurfaceD2DTarget(this, mTexture, mFormat);
Flush();
}
return mSnapshot;
}
void
DrawTargetD2D::Flush()
{
PopAllClips();
HRESULT hr = mRT->Flush();
if (FAILED(hr)) {
gfxWarning() << "Error reported when trying to flush D2D rendertarget. Code: " << hr;
}
// We no longer depend on any target.
for (TargetSet::iterator iter = mDependingOnTargets.begin();
iter != mDependingOnTargets.end(); iter++) {
(*iter)->mDependentTargets.erase(this);
}
mDependingOnTargets.clear();
}
void
DrawTargetD2D::AddDependencyOnSource(SourceSurfaceD2DTarget* aSource)
{
if (aSource->mDrawTarget && !mDependingOnTargets.count(aSource->mDrawTarget)) {
aSource->mDrawTarget->mDependentTargets.insert(this);
mDependingOnTargets.insert(aSource->mDrawTarget);
}
}
TemporaryRef<ID2D1Bitmap>
DrawTargetD2D::GetBitmapForSurface(SourceSurface *aSurface,
Rect &aSource)
{
RefPtr<ID2D1Bitmap> bitmap;
switch (aSurface->GetType()) {
case SurfaceType::D2D1_BITMAP:
{
SourceSurfaceD2D *srcSurf = static_cast<SourceSurfaceD2D*>(aSurface);
bitmap = srcSurf->GetBitmap();
}
break;
case SurfaceType::D2D1_DRAWTARGET:
{
SourceSurfaceD2DTarget *srcSurf = static_cast<SourceSurfaceD2DTarget*>(aSurface);
bitmap = srcSurf->GetBitmap(mRT);
AddDependencyOnSource(srcSurf);
}
break;
default:
{
RefPtr<DataSourceSurface> srcSurf = aSurface->GetDataSurface();
if (!srcSurf) {
gfxDebug() << "Not able to deal with non-data source surface.";
return nullptr;
}
// We need to include any pixels that are overlapped by aSource
Rect sourceRect(aSource);
sourceRect.RoundOut();
if (sourceRect.IsEmpty()) {
gfxDebug() << "Bitmap source is empty. DrawBitmap will silently fail.";
return nullptr;
}
if (sourceRect.width > mRT->GetMaximumBitmapSize() ||
sourceRect.height > mRT->GetMaximumBitmapSize()) {
gfxDebug() << "Bitmap source larger than texture size specified. DrawBitmap will silently fail.";
// Don't know how to deal with this yet.
return nullptr;
}
int stride = srcSurf->Stride();
unsigned char *data = srcSurf->GetData() +
(uint32_t)sourceRect.y * stride +
(uint32_t)sourceRect.x * BytesPerPixel(srcSurf->GetFormat());
D2D1_BITMAP_PROPERTIES props =
D2D1::BitmapProperties(D2DPixelFormat(srcSurf->GetFormat()));
mRT->CreateBitmap(D2D1::SizeU(UINT32(sourceRect.width), UINT32(sourceRect.height)), data, stride, props, byRef(bitmap));
// subtract the integer part leaving the fractional part
aSource.x -= (uint32_t)aSource.x;
aSource.y -= (uint32_t)aSource.y;
}
break;
}
return bitmap;
}
#ifdef USE_D2D1_1
TemporaryRef<ID2D1Image>
DrawTargetD2D::GetImageForSurface(SourceSurface *aSurface)
{
RefPtr<ID2D1Image> image;
Rect r(Point(), Size(aSurface->GetSize()));
image = GetBitmapForSurface(aSurface, r);
return image;
}
#endif
void
DrawTargetD2D::DrawSurface(SourceSurface *aSurface,
const Rect &aDest,
const Rect &aSource,
const DrawSurfaceOptions &aSurfOptions,
const DrawOptions &aOptions)
{
RefPtr<ID2D1Bitmap> bitmap;
ID2D1RenderTarget *rt = GetRTForOperation(aOptions.mCompositionOp, ColorPattern(Color()));
PrepareForDrawing(rt);
rt->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));
Rect srcRect = aSource;
bitmap = GetBitmapForSurface(aSurface, srcRect);
if (!bitmap) {
return;
}
rt->DrawBitmap(bitmap, D2DRect(aDest), aOptions.mAlpha, D2DFilter(aSurfOptions.mFilter), D2DRect(srcRect));
FinalizeRTForOperation(aOptions.mCompositionOp, ColorPattern(Color()), aDest);
}
void
DrawTargetD2D::DrawFilter(FilterNode *aNode,
const Rect &aSourceRect,
const Point &aDestPoint,
const DrawOptions &aOptions)
{
#ifdef USE_D2D1_1
RefPtr<ID2D1DeviceContext> dc;
HRESULT hr;
hr = mRT->QueryInterface((ID2D1DeviceContext**)byRef(dc));
if (SUCCEEDED(hr) && aNode->GetBackendType() == FILTER_BACKEND_DIRECT2D1_1) {
ID2D1RenderTarget *rt = GetRTForOperation(aOptions.mCompositionOp, ColorPattern(Color()));
PrepareForDrawing(rt);
rt->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));
hr = rt->QueryInterface((ID2D1DeviceContext**)byRef(dc));
if (SUCCEEDED(hr)) {
dc->DrawImage(static_cast<FilterNodeD2D1*>(aNode)->OutputEffect(), D2DPoint(aDestPoint), D2DRect(aSourceRect));
Rect destRect = aSourceRect;
destRect.MoveBy(aDestPoint);
FinalizeRTForOperation(aOptions.mCompositionOp, ColorPattern(Color()), destRect);
return;
}
}
#endif
if (aNode->GetBackendType() != FILTER_BACKEND_SOFTWARE) {
gfxWarning() << "Invalid filter backend passed to DrawTargetD2D!";
return;
}
FilterNodeSoftware* filter = static_cast<FilterNodeSoftware*>(aNode);
filter->Draw(this, aSourceRect, aDestPoint, aOptions);
}
void
DrawTargetD2D::MaskSurface(const Pattern &aSource,
SourceSurface *aMask,
Point aOffset,
const DrawOptions &aOptions)
{
RefPtr<ID2D1Bitmap> bitmap;
ID2D1RenderTarget *rt = GetRTForOperation(aOptions.mCompositionOp, ColorPattern(Color()));
PrepareForDrawing(rt);
// FillOpacityMask only works if the antialias mode is MODE_ALIASED
rt->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
IntSize size = aMask->GetSize();
Rect maskRect = Rect(0.f, 0.f, size.width, size.height);
bitmap = GetBitmapForSurface(aMask, maskRect);
if (!bitmap) {
return;
}
Rect dest = Rect(aOffset.x, aOffset.y, size.width, size.height);
RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aSource, aOptions.mAlpha);
rt->FillOpacityMask(bitmap, brush, D2D1_OPACITY_MASK_CONTENT_GRAPHICS, D2DRect(dest), D2DRect(maskRect));
FinalizeRTForOperation(aOptions.mCompositionOp, ColorPattern(Color()), dest);
}
void
DrawTargetD2D::DrawSurfaceWithShadow(SourceSurface *aSurface,
const Point &aDest,
const Color &aColor,
const Point &aOffset,
Float aSigma,
CompositionOp aOperator)
{
RefPtr<ID3D10ShaderResourceView> srView = nullptr;
if (aSurface->GetType() != SurfaceType::D2D1_DRAWTARGET) {
return;
}
SetScissorToRect(nullptr);
// XXX - This function is way too long, it should be split up soon to make
// it more graspable!
Flush();
AutoSaveRestoreClippedOut restoreClippedOut(this);
if (!IsOperatorBoundByMask(aOperator)) {
restoreClippedOut.Save();
}
srView = static_cast<SourceSurfaceD2DTarget*>(aSurface)->GetSRView();
EnsureViews();
if (!mTempRTView) {
// This view is only needed in this path.
HRESULT hr = mDevice->CreateRenderTargetView(mTempTexture, nullptr, byRef(mTempRTView));
if (FAILED(hr)) {
gfxWarning() << "Failure to create RenderTargetView. Code: " << hr;
return;
}
}
RefPtr<ID3D10RenderTargetView> destRTView = mRTView;
RefPtr<ID3D10Texture2D> destTexture;
HRESULT hr;
RefPtr<ID3D10Texture2D> maskTexture;
RefPtr<ID3D10ShaderResourceView> maskSRView;
IntRect clipBounds;
if (mPushedClips.size()) {
EnsureClipMaskTexture(&clipBounds);
mDevice->CreateShaderResourceView(mCurrentClipMaskTexture, nullptr, byRef(maskSRView));
}
IntSize srcSurfSize;
ID3D10RenderTargetView *rtViews;
D3D10_VIEWPORT viewport;
UINT stride = sizeof(Vertex);
UINT offset = 0;
ID3D10Buffer *buff = mPrivateData->mVB;
mDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
mDevice->IASetVertexBuffers(0, 1, &buff, &stride, &offset);
mDevice->IASetInputLayout(mPrivateData->mInputLayout);
mPrivateData->mEffect->GetVariableByName("QuadDesc")->AsVector()->
SetFloatVector(ShaderConstantRectD3D10(-1.0f, 1.0f, 2.0f, -2.0f));
mPrivateData->mEffect->GetVariableByName("TexCoords")->AsVector()->
SetFloatVector(ShaderConstantRectD3D10(0, 0, 1.0f, 1.0f));
// If we create a downsampled source surface we need to correct aOffset for that.
Point correctedOffset = aOffset + aDest;
// The 'practical' scaling factors.
Float dsFactorX = 1.0f;
Float dsFactorY = 1.0f;
if (aSigma > 1.7f) {
// In this case 9 samples of our original will not cover it. Generate the
// mip levels for the original and create a downsampled version from
// them. We generate a version downsampled so that a kernel for a sigma
// of 1.7 will produce the right results.
float blurWeights[9] = { 0.234671f, 0.197389f, 0.197389f, 0.117465f, 0.117465f, 0.049456f, 0.049456f, 0.014732f, 0.014732f };
mPrivateData->mEffect->GetVariableByName("BlurWeights")->SetRawValue(blurWeights, 0, sizeof(blurWeights));
CD3D10_TEXTURE2D_DESC desc(DXGI_FORMAT_B8G8R8A8_UNORM,
aSurface->GetSize().width,
aSurface->GetSize().height);
desc.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE;
desc.MiscFlags = D3D10_RESOURCE_MISC_GENERATE_MIPS;
RefPtr<ID3D10Texture2D> mipTexture;
hr = mDevice->CreateTexture2D(&desc, nullptr, byRef(mipTexture));
if (FAILED(hr)) {
gfxCriticalError() << "[D2D] CreateTexture2D failure " << aSurface->GetSize() << " Code: " << hr;
return;
}
IntSize dsSize = IntSize(int32_t(aSurface->GetSize().width * (1.7f / aSigma)),
int32_t(aSurface->GetSize().height * (1.7f / aSigma)));
if (dsSize.width < 1) {
dsSize.width = 1;
}
if (dsSize.height < 1) {
dsSize.height = 1;
}
dsFactorX = dsSize.width / Float(aSurface->GetSize().width);
dsFactorY = dsSize.height / Float(aSurface->GetSize().height);
correctedOffset.x *= dsFactorX;
correctedOffset.y *= dsFactorY;
desc = CD3D10_TEXTURE2D_DESC(DXGI_FORMAT_B8G8R8A8_UNORM,
dsSize.width,
dsSize.height, 1, 1);
desc.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE;
RefPtr<ID3D10Texture2D> tmpDSTexture;
hr = mDevice->CreateTexture2D(&desc, nullptr, byRef(tmpDSTexture));
if (FAILED(hr)) {
gfxCriticalError() << "[D2D] CreateTexture2D failure " << dsSize << " Code: " << hr;
return;
}
D3D10_BOX box;
box.left = box.top = box.front = 0;
box.back = 1;
box.right = aSurface->GetSize().width;
box.bottom = aSurface->GetSize().height;
mDevice->CopySubresourceRegion(mipTexture, 0, 0, 0, 0, static_cast<SourceSurfaceD2DTarget*>(aSurface)->mTexture, 0, &box);
mDevice->CreateShaderResourceView(mipTexture, nullptr, byRef(srView));
mDevice->GenerateMips(srView);
RefPtr<ID3D10RenderTargetView> dsRTView;
RefPtr<ID3D10ShaderResourceView> dsSRView;
mDevice->CreateRenderTargetView(tmpDSTexture, nullptr, byRef(dsRTView));
mDevice->CreateShaderResourceView(tmpDSTexture, nullptr, byRef(dsSRView));
// We're not guaranteed the texture we created will be empty, we've
// seen old content at least on NVidia drivers.
float color[4] = { 0, 0, 0, 0 };
mDevice->ClearRenderTargetView(dsRTView, color);
rtViews = dsRTView;
mDevice->OMSetRenderTargets(1, &rtViews, nullptr);
viewport.MaxDepth = 1;
viewport.MinDepth = 0;
viewport.Height = dsSize.height;
viewport.Width = dsSize.width;
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
mDevice->RSSetViewports(1, &viewport);
mPrivateData->mEffect->GetVariableByName("tex")->AsShaderResource()->SetResource(srView);
mPrivateData->mEffect->GetTechniqueByName("SampleTexture")->
GetPassByIndex(0)->Apply(0);
mDevice->OMSetBlendState(GetBlendStateForOperator(CompositionOp::OP_OVER), nullptr, 0xffffffff);
mDevice->Draw(4, 0);
srcSurfSize = dsSize;
srView = dsSRView;
} else {
// In this case generate a kernel to draw the blur directly to the temp
// surf in one direction and to final in the other.
float blurWeights[9];
float normalizeFactor = 1.0f;
if (aSigma != 0) {
normalizeFactor = 1.0f / Float(sqrt(2 * M_PI * pow(aSigma, 2)));
}
blurWeights[0] = normalizeFactor;
// XXX - We should actually optimize for Sigma = 0 here. We could use a
// much simpler shader and save a lot of texture lookups.
for (int i = 1; i < 9; i += 2) {
if (aSigma != 0) {
blurWeights[i] = blurWeights[i + 1] = normalizeFactor *
exp(-pow(float((i + 1) / 2), 2) / (2 * pow(aSigma, 2)));
} else {
blurWeights[i] = blurWeights[i + 1] = 0;
}
}
mPrivateData->mEffect->GetVariableByName("BlurWeights")->SetRawValue(blurWeights, 0, sizeof(blurWeights));
viewport.MaxDepth = 1;
viewport.MinDepth = 0;
viewport.Height = aSurface->GetSize().height;
viewport.Width = aSurface->GetSize().width;
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
mDevice->RSSetViewports(1, &viewport);
srcSurfSize = aSurface->GetSize();
}
// We may need to draw to a different intermediate surface if our temp
// texture isn't big enough.
bool needBiggerTemp = srcSurfSize.width > mSize.width ||
srcSurfSize.height > mSize.height;
RefPtr<ID3D10RenderTargetView> tmpRTView;
RefPtr<ID3D10ShaderResourceView> tmpSRView;
RefPtr<ID3D10Texture2D> tmpTexture;
IntSize tmpSurfSize = mSize;
if (!needBiggerTemp) {
tmpRTView = mTempRTView;
tmpSRView = mSRView;
// There could still be content here!
float color[4] = { 0, 0, 0, 0 };
mDevice->ClearRenderTargetView(tmpRTView, color);
} else {
CD3D10_TEXTURE2D_DESC desc(DXGI_FORMAT_B8G8R8A8_UNORM,
srcSurfSize.width,
srcSurfSize.height,
1, 1);
desc.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE;
mDevice->CreateTexture2D(&desc, nullptr, byRef(tmpTexture));
mDevice->CreateRenderTargetView(tmpTexture, nullptr, byRef(tmpRTView));
mDevice->CreateShaderResourceView(tmpTexture, nullptr, byRef(tmpSRView));
tmpSurfSize = srcSurfSize;
}
rtViews = tmpRTView;
mDevice->OMSetRenderTargets(1, &rtViews, nullptr);
mPrivateData->mEffect->GetVariableByName("tex")->AsShaderResource()->SetResource(srView);
// Premultiplied!
float shadowColor[4] = { aColor.r * aColor.a, aColor.g * aColor.a,
aColor.b * aColor.a, aColor.a };
mPrivateData->mEffect->GetVariableByName("ShadowColor")->AsVector()->
SetFloatVector(shadowColor);
float pixelOffset = 1.0f / float(srcSurfSize.width);
float blurOffsetsH[9] = { 0, pixelOffset, -pixelOffset,
2.0f * pixelOffset, -2.0f * pixelOffset,
3.0f * pixelOffset, -3.0f * pixelOffset,
4.0f * pixelOffset, - 4.0f * pixelOffset };
pixelOffset = 1.0f / float(tmpSurfSize.height);
float blurOffsetsV[9] = { 0, pixelOffset, -pixelOffset,
2.0f * pixelOffset, -2.0f * pixelOffset,
3.0f * pixelOffset, -3.0f * pixelOffset,
4.0f * pixelOffset, - 4.0f * pixelOffset };
mPrivateData->mEffect->GetVariableByName("BlurOffsetsH")->
SetRawValue(blurOffsetsH, 0, sizeof(blurOffsetsH));
mPrivateData->mEffect->GetVariableByName("BlurOffsetsV")->
SetRawValue(blurOffsetsV, 0, sizeof(blurOffsetsV));
mPrivateData->mEffect->GetTechniqueByName("SampleTextureWithShadow")->
GetPassByIndex(0)->Apply(0);
mDevice->Draw(4, 0);
viewport.MaxDepth = 1;
viewport.MinDepth = 0;
viewport.Height = mSize.height;
viewport.Width = mSize.width;
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
mDevice->RSSetViewports(1, &viewport);
mPrivateData->mEffect->GetVariableByName("tex")->AsShaderResource()->SetResource(tmpSRView);
rtViews = destRTView;
mDevice->OMSetRenderTargets(1, &rtViews, nullptr);
Point shadowDest = aDest + aOffset;
mPrivateData->mEffect->GetVariableByName("QuadDesc")->AsVector()->
SetFloatVector(ShaderConstantRectD3D10(-1.0f + ((shadowDest.x / mSize.width) * 2.0f),
1.0f - (shadowDest.y / mSize.height * 2.0f),
(Float(aSurface->GetSize().width) / mSize.width) * 2.0f,
(-Float(aSurface->GetSize().height) / mSize.height) * 2.0f));
mPrivateData->mEffect->GetVariableByName("TexCoords")->AsVector()->
SetFloatVector(ShaderConstantRectD3D10(0, 0, Float(srcSurfSize.width) / tmpSurfSize.width,
Float(srcSurfSize.height) / tmpSurfSize.height));
if (mPushedClips.size()) {
mPrivateData->mEffect->GetVariableByName("mask")->AsShaderResource()->SetResource(maskSRView);
mPrivateData->mEffect->GetVariableByName("MaskTexCoords")->AsVector()->
SetFloatVector(ShaderConstantRectD3D10(shadowDest.x / mSize.width, shadowDest.y / mSize.height,
Float(aSurface->GetSize().width) / mSize.width,
Float(aSurface->GetSize().height) / mSize.height));
mPrivateData->mEffect->GetTechniqueByName("SampleTextureWithShadow")->
GetPassByIndex(2)->Apply(0);
SetScissorToRect(&clipBounds);
} else {
mPrivateData->mEffect->GetTechniqueByName("SampleTextureWithShadow")->
GetPassByIndex(1)->Apply(0);
}
mDevice->OMSetBlendState(GetBlendStateForOperator(aOperator), nullptr, 0xffffffff);
mDevice->Draw(4, 0);
mPrivateData->mEffect->GetVariableByName("QuadDesc")->AsVector()->
SetFloatVector(ShaderConstantRectD3D10(-1.0f + ((aDest.x / mSize.width) * 2.0f),
1.0f - (aDest.y / mSize.height * 2.0f),
(Float(aSurface->GetSize().width) / mSize.width) * 2.0f,
(-Float(aSurface->GetSize().height) / mSize.height) * 2.0f));
mPrivateData->mEffect->GetVariableByName("tex")->AsShaderResource()->SetResource(static_cast<SourceSurfaceD2DTarget*>(aSurface)->GetSRView());
mPrivateData->mEffect->GetVariableByName("TexCoords")->AsVector()->
SetFloatVector(ShaderConstantRectD3D10(0, 0, 1.0f, 1.0f));
if (mPushedClips.size()) {
mPrivateData->mEffect->GetVariableByName("MaskTexCoords")->AsVector()->
SetFloatVector(ShaderConstantRectD3D10(aDest.x / mSize.width, aDest.y / mSize.height,
Float(aSurface->GetSize().width) / mSize.width,
Float(aSurface->GetSize().height) / mSize.height));
mPrivateData->mEffect->GetTechniqueByName("SampleMaskedTexture")->
GetPassByIndex(0)->Apply(0);
// We've set the scissor rect here for the previous draw call.
} else {
mPrivateData->mEffect->GetTechniqueByName("SampleTexture")->
GetPassByIndex(0)->Apply(0);
}
mDevice->OMSetBlendState(GetBlendStateForOperator(aOperator), nullptr, 0xffffffff);
mDevice->Draw(4, 0);
}
void
DrawTargetD2D::ClearRect(const Rect &aRect)
{
MarkChanged();
PushClipRect(aRect);
PopAllClips();
AutoSaveRestoreClippedOut restoreClippedOut(this);
D2D1_RECT_F clipRect;
bool isPixelAligned;
bool pushedClip = false;
if (mTransform.IsRectilinear() &&
GetDeviceSpaceClipRect(clipRect, isPixelAligned)) {
if (mTransformDirty ||
!mTransform.IsIdentity()) {
mRT->SetTransform(D2D1::IdentityMatrix());
mTransformDirty = true;
}
mRT->PushAxisAlignedClip(clipRect, isPixelAligned ? D2D1_ANTIALIAS_MODE_ALIASED : D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
pushedClip = true;
} else {
FlushTransformToRT();
restoreClippedOut.Save();
}
mRT->Clear(D2D1::ColorF(0, 0.0f));
if (pushedClip) {
mRT->PopAxisAlignedClip();
}
PopClip();
return;
}
void
DrawTargetD2D::CopySurface(SourceSurface *aSurface,
const IntRect &aSourceRect,
const IntPoint &aDestination)
{
MarkChanged();
Rect srcRect(Float(aSourceRect.x), Float(aSourceRect.y),
Float(aSourceRect.width), Float(aSourceRect.height));
Rect dstRect(Float(aDestination.x), Float(aDestination.y),
Float(aSourceRect.width), Float(aSourceRect.height));
mRT->SetTransform(D2D1::IdentityMatrix());
mTransformDirty = true;
mRT->PushAxisAlignedClip(D2DRect(dstRect), D2D1_ANTIALIAS_MODE_ALIASED);
mRT->Clear(D2D1::ColorF(0, 0.0f));
mRT->PopAxisAlignedClip();
RefPtr<ID2D1Bitmap> bitmap = GetBitmapForSurface(aSurface, srcRect);
if (!bitmap) {
return;
}
if (aSurface->GetFormat() == SurfaceFormat::A8) {
RefPtr<ID2D1SolidColorBrush> brush;
mRT->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White),
D2D1::BrushProperties(), byRef(brush));
mRT->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
mRT->FillOpacityMask(bitmap, brush, D2D1_OPACITY_MASK_CONTENT_GRAPHICS);
} else {
mRT->DrawBitmap(bitmap, D2DRect(dstRect), 1.0f,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
D2DRect(srcRect));
}
}
void
DrawTargetD2D::FillRect(const Rect &aRect,
const Pattern &aPattern,
const DrawOptions &aOptions)
{
ID2D1RenderTarget *rt = GetRTForOperation(aOptions.mCompositionOp, aPattern);
PrepareForDrawing(rt);
rt->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));
RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);
if (brush) {
rt->FillRectangle(D2DRect(aRect), brush);
}
FinalizeRTForOperation(aOptions.mCompositionOp, aPattern, aRect);
}
void
DrawTargetD2D::StrokeRect(const Rect &aRect,
const Pattern &aPattern,
const StrokeOptions &aStrokeOptions,
const DrawOptions &aOptions)
{
ID2D1RenderTarget *rt = GetRTForOperation(aOptions.mCompositionOp, aPattern);
PrepareForDrawing(rt);
rt->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));
RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);
RefPtr<ID2D1StrokeStyle> strokeStyle = CreateStrokeStyleForOptions(aStrokeOptions);
if (brush && strokeStyle) {
rt->DrawRectangle(D2DRect(aRect), brush, aStrokeOptions.mLineWidth, strokeStyle);
}
FinalizeRTForOperation(aOptions.mCompositionOp, aPattern, aRect);
}
void
DrawTargetD2D::StrokeLine(const Point &aStart,
const Point &aEnd,
const Pattern &aPattern,
const StrokeOptions &aStrokeOptions,
const DrawOptions &aOptions)
{
ID2D1RenderTarget *rt = GetRTForOperation(aOptions.mCompositionOp, aPattern);
PrepareForDrawing(rt);
rt->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));
RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);
RefPtr<ID2D1StrokeStyle> strokeStyle = CreateStrokeStyleForOptions(aStrokeOptions);
if (brush && strokeStyle) {
rt->DrawLine(D2DPoint(aStart), D2DPoint(aEnd), brush, aStrokeOptions.mLineWidth, strokeStyle);
}
FinalizeRTForOperation(aOptions.mCompositionOp, aPattern, Rect(0, 0, Float(mSize.width), Float(mSize.height)));
}
void
DrawTargetD2D::Stroke(const Path *aPath,
const Pattern &aPattern,
const StrokeOptions &aStrokeOptions,
const DrawOptions &aOptions)
{
if (aPath->GetBackendType() != BackendType::DIRECT2D) {
gfxDebug() << *this << ": Ignoring drawing call for incompatible path.";
return;
}
const PathD2D *d2dPath = static_cast<const PathD2D*>(aPath);
ID2D1RenderTarget *rt = GetRTForOperation(aOptions.mCompositionOp, aPattern);
PrepareForDrawing(rt);
rt->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));
RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);
RefPtr<ID2D1StrokeStyle> strokeStyle = CreateStrokeStyleForOptions(aStrokeOptions);
if (brush && strokeStyle) {
rt->DrawGeometry(d2dPath->mGeometry, brush, aStrokeOptions.mLineWidth, strokeStyle);
}
FinalizeRTForOperation(aOptions.mCompositionOp, aPattern, Rect(0, 0, Float(mSize.width), Float(mSize.height)));
}
void
DrawTargetD2D::Fill(const Path *aPath,
const Pattern &aPattern,
const DrawOptions &aOptions)
{
if (aPath->GetBackendType() != BackendType::DIRECT2D) {
gfxDebug() << *this << ": Ignoring drawing call for incompatible path.";
return;
}
const PathD2D *d2dPath = static_cast<const PathD2D*>(aPath);
ID2D1RenderTarget *rt = GetRTForOperation(aOptions.mCompositionOp, aPattern);
PrepareForDrawing(rt);
rt->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));
RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);
if (brush) {
rt->FillGeometry(d2dPath->mGeometry, brush);
}
Rect bounds;
if (aOptions.mCompositionOp != CompositionOp::OP_OVER) {
D2D1_RECT_F d2dbounds;
d2dPath->mGeometry->GetBounds(D2D1::IdentityMatrix(), &d2dbounds);
bounds = ToRect(d2dbounds);
}
FinalizeRTForOperation(aOptions.mCompositionOp, aPattern, bounds);
}
void
DrawTargetD2D::FillGlyphs(ScaledFont *aFont,
const GlyphBuffer &aBuffer,
const Pattern &aPattern,
const DrawOptions &aOptions,
const GlyphRenderingOptions* aRenderOptions)
{
if (aFont->GetType() != FontType::DWRITE) {
gfxDebug() << *this << ": Ignoring drawing call for incompatible font.";