/
d2dgraphicscontext.cpp
993 lines (895 loc) · 32.4 KB
/
d2dgraphicscontext.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
// This file is part of VSTGUI. It is subject to the license terms
// in the LICENSE file found in the top-level directory of this
// distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
#include "d2dgraphicscontext.h"
#include "d2dgradient.h"
#include "d2dgraphicspath.h"
#include "d2dbitmap.h"
#include "d2dbitmapcache.h"
#include "d2dgradient.h"
#include "d2d.h"
#include "../comptr.h"
#include "../../../crect.h"
#include "../../../cgraphicstransform.h"
#include "../../../ccolor.h"
#include "../../../cdrawdefs.h"
#include "../../../clinestyle.h"
#include <stack>
#include <d2d1_1.h>
#include <dwrite.h>
//------------------------------------------------------------------------
namespace VSTGUI {
namespace {
//------------------------------------------------------------------------
struct TransformGuard
{
TransformGuard (ID2D1DeviceContext* context) : context (context)
{
context->GetTransform (&matrix);
}
~TransformGuard () noexcept { context->SetTransform (matrix); }
D2D1_MATRIX_3X2_F matrix;
ID2D1DeviceContext* context;
};
//------------------------------------------------------------------------
struct D2DBitmapDeviceContext : D2DGraphicsDeviceContext
{
static PlatformGraphicsDeviceContextPtr make (const D2DGraphicsDevice& device,
ID2D1DeviceContext* deviceContext,
const SharedPointer<D2DBitmap>& bitmap)
{
D2D1_BITMAP_PROPERTIES1 props = D2D1::BitmapProperties1 (
D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
D2D1::PixelFormat (DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED));
COM::Ptr<ID2D1Bitmap1> bitmapTarget;
deviceContext->CreateBitmapFromWicBitmap (bitmap->getSource (), &props,
bitmapTarget.adoptPtr ());
if (!bitmapTarget)
return nullptr;
deviceContext->SetTarget (bitmapTarget.get ());
TransformMatrix tm;
tm.scale (bitmap->getScaleFactor (), bitmap->getScaleFactor ());
deviceContext->SetTransform (convert (tm));
return std::make_shared<D2DBitmapDeviceContext> (device, bitmap, deviceContext,
TransformMatrix {});
}
D2DBitmapDeviceContext (const D2DGraphicsDevice& device, const SharedPointer<D2DBitmap>& bitmap,
ID2D1DeviceContext* deviceContext, const TransformMatrix& tm)
: D2DGraphicsDeviceContext (device, deviceContext, tm), bitmap (bitmap)
{
}
bool endDraw () const
{
if (D2DGraphicsDeviceContext::endDraw ())
{
COM::Ptr<ID2D1Image> image;
getID2D1DeviceContext ()->GetTarget (image.adoptPtr ());
if (!image)
return false;
COM::Ptr<ID2D1Bitmap1> bitmapTarget;
if (FAILED (image->QueryInterface<ID2D1Bitmap1> (bitmapTarget.adoptPtr ())))
return false;
auto pa = bitmap->lockPixels (true);
if (!pa)
return false;
auto size = bitmap->getSize ();
D2D1_BITMAP_PROPERTIES1 props = D2D1::BitmapProperties1 (
D2D1_BITMAP_OPTIONS_CPU_READ | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
D2D1::PixelFormat (DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED));
COM::Ptr<ID2D1Bitmap1> cpuBitmap;
auto hr = getID2D1DeviceContext ()->CreateBitmap (
D2D1_SIZE_U {static_cast<UINT32> (size.x), static_cast<UINT32> (size.y)}, nullptr,
pa->getBytesPerRow (), props, cpuBitmap.adoptPtr ());
if (FAILED (hr))
return false;
D2D1_POINT_2U dp {};
D2D1_RECT_U dstRect {0, 0, static_cast<UINT32> (size.x), static_cast<UINT32> (size.y)};
hr = cpuBitmap->CopyFromBitmap (&dp, bitmapTarget.get (), &dstRect);
if (FAILED (hr))
return false;
D2D1_MAPPED_RECT mappedRect {};
hr = cpuBitmap->Map (D2D1_MAP_OPTIONS_READ, &mappedRect);
if (FAILED (hr))
return false;
auto height = static_cast<uint32_t> (size.y);
if (pa->getBytesPerRow () == mappedRect.pitch)
{
memcpy (pa->getAddress (), mappedRect.bits, mappedRect.pitch * height);
}
else
{
auto src = mappedRect.bits;
for (auto row = 0u; row < height; ++row, src += mappedRect.pitch)
{
auto dst = pa->getAddress () + pa->getBytesPerRow () * row;
memcpy (dst, src, pa->getBytesPerRow ());
}
}
cpuBitmap->Unmap ();
return true;
}
return false;
}
SharedPointer<D2DBitmap> bitmap;
};
//------------------------------------------------------------------------
COM::Ptr<ID2D1ImageBrush> createBrushFromBitmap (ID2D1DeviceContext* deviceContext,
ID2D1Bitmap* d2d1Bitmap, const CRect& srcRect,
const CRect& dstRect,
BitmapInterpolationQuality quality)
{
D2D1_IMAGE_BRUSH_PROPERTIES imageBrushProp = {};
imageBrushProp.sourceRectangle = convert (srcRect);
imageBrushProp.extendModeX = imageBrushProp.extendModeY = D2D1_EXTEND_MODE_WRAP;
switch (quality)
{
case BitmapInterpolationQuality::kLow:
imageBrushProp.interpolationMode = D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
break;
case BitmapInterpolationQuality::kMedium:
case BitmapInterpolationQuality::kHigh:
default:
imageBrushProp.interpolationMode = D2D1_INTERPOLATION_MODE_LINEAR;
break;
}
CGraphicsTransform brushTransform;
brushTransform.translate (dstRect.getTopLeft ());
D2D1_BRUSH_PROPERTIES brushProp = {};
brushProp.opacity = 1.f;
brushProp.transform = convert (brushTransform);
COM::Ptr<ID2D1ImageBrush> brush;
auto hr =
deviceContext->CreateImageBrush (d2d1Bitmap, imageBrushProp, brushProp, brush.adoptPtr ());
if (FAILED (hr))
return {};
return brush;
}
//------------------------------------------------------------------------
} // anonymous namespace
//------------------------------------------------------------------------
struct D2DGraphicsDeviceFactory::Impl
{
std::vector<std::shared_ptr<D2DGraphicsDevice>> devices;
};
//------------------------------------------------------------------------
D2DGraphicsDeviceFactory::D2DGraphicsDeviceFactory () { impl = std::make_unique<Impl> (); }
//------------------------------------------------------------------------
D2DGraphicsDeviceFactory::~D2DGraphicsDeviceFactory () noexcept {}
//------------------------------------------------------------------------
PlatformGraphicsDevicePtr
D2DGraphicsDeviceFactory::getDeviceForScreen (ScreenInfo::Identifier screen) const
{
if (impl->devices.empty ())
return nullptr;
return impl->devices.front ();
}
//------------------------------------------------------------------------
PlatformGraphicsDevicePtr D2DGraphicsDeviceFactory::find (ID2D1Device* dev) const
{
auto it = std::find_if (impl->devices.begin (), impl->devices.end (),
[dev] (const auto& el) { return el->get () == dev; });
if (it != impl->devices.end ())
return *it;
return nullptr;
}
//------------------------------------------------------------------------
void D2DGraphicsDeviceFactory::addDevice (const std::shared_ptr<D2DGraphicsDevice>& device) const
{
impl->devices.push_back (device);
}
//------------------------------------------------------------------------
void D2DGraphicsDeviceFactory::removeDevice (const std::shared_ptr<D2DGraphicsDevice>& device) const
{
auto it = std::find (impl->devices.begin (), impl->devices.end (), device);
if (it != impl->devices.end ())
impl->devices.erase (it);
}
//------------------------------------------------------------------------
struct D2DGraphicsDevice::Impl
{
COM::Ptr<ID2D1Device> device;
};
//------------------------------------------------------------------------
D2DGraphicsDevice::D2DGraphicsDevice (ID2D1Device* device)
{
impl = std::make_unique<Impl> ();
impl->device = COM::share<ID2D1Device> (device);
}
//------------------------------------------------------------------------
D2DGraphicsDevice::~D2DGraphicsDevice () noexcept {}
//------------------------------------------------------------------------
PlatformGraphicsDeviceContextPtr
D2DGraphicsDevice::createBitmapContext (const PlatformBitmapPtr& bitmap) const
{
auto d2dBitmap = bitmap.cast<D2DBitmap> ();
if (d2dBitmap)
{
COM::Ptr<ID2D1DeviceContext> deviceContext;
auto hr =
impl->device->CreateDeviceContext (D2D1_DEVICE_CONTEXT_OPTIONS_NONE, deviceContext.adoptPtr ());
if (FAILED (hr))
return nullptr;
D2DBitmapCache::removeBitmap (d2dBitmap);
return D2DBitmapDeviceContext::make (*this, deviceContext.get (), d2dBitmap);
}
return nullptr;
}
//------------------------------------------------------------------------
ID2D1Device* D2DGraphicsDevice::get () const { return impl->device.get (); }
//------------------------------------------------------------------------
struct D2DGraphicsDeviceContext::Impl
{
Impl (const D2DGraphicsDevice& device, ID2D1DeviceContext* deviceContext,
const TransformMatrix& tm)
: device (device), deviceContext (COM::share (deviceContext)), globalTM (tm)
{
#if DEBUG
COM::Ptr<ID2D1Device> d2dDevice;
deviceContext->GetDevice (d2dDevice.adoptPtr ());
vstgui_assert (d2dDevice.get () == device.get ());
#endif
}
template<typename Proc>
void doInContext (Proc p, CPoint transformOffset = {})
{
if (state.clip.isEmpty ())
return;
TransformGuard tmGuard (deviceContext.get ());
auto transform = convert (tmGuard.matrix) * globalTM * state.tm;
transform.scale (scaleFactor, scaleFactor);
transform.translate (transformOffset);
auto newClip = state.clip;
globalTM.transform (newClip);
if (applyClip != newClip)
{
if (applyClip.isEmpty () == false)
deviceContext->PopAxisAlignedClip ();
if (newClip.isEmpty () == false)
deviceContext->PushAxisAlignedClip (convert (newClip), D2D1_ANTIALIAS_MODE_ALIASED);
applyClip = newClip;
}
deviceContext->SetTransform (convert (transform));
p (deviceContext.get ());
}
//-----------------------------------------------------------------------------
void applyFrameColor ()
{
if (state.frameBrush)
return;
deviceContext->CreateSolidColorBrush (convert (state.frameColor, state.globalAlpha),
state.frameBrush.adoptPtr ());
}
//-----------------------------------------------------------------------------
void applyFillColor ()
{
if (state.fillBrush)
return;
deviceContext->CreateSolidColorBrush (convert (state.fillColor, state.globalAlpha),
state.fillBrush.adoptPtr ());
}
//-----------------------------------------------------------------------------
void applyFontColor (CColor color)
{
if (state.fontColor != color)
state.fontBrush.reset ();
if (state.fontBrush)
return;
state.fontColor = color;
deviceContext->CreateSolidColorBrush (convert (state.fontColor, state.globalAlpha),
state.fontBrush.adoptPtr ());
}
//-----------------------------------------------------------------------------
CPoint lineWidthTransformMatrixOffset () const
{
int32_t lineWidthInt = static_cast<int32_t> (state.lineWidth);
if (static_cast<CCoord> (lineWidthInt) == state.lineWidth && lineWidthInt % 2)
{
return {0.5, 0.5};
}
return {};
}
//-----------------------------------------------------------------------------
void applyLineStyle ()
{
if (state.strokeStyle)
return;
COM::Ptr<ID2D1Factory> factory {};
deviceContext->GetFactory (factory.adoptPtr ());
D2D1_STROKE_STYLE_PROPERTIES properties;
switch (state.lineStyle.getLineCap ())
{
case CLineStyle::kLineCapButt:
properties.startCap = properties.endCap = properties.dashCap = D2D1_CAP_STYLE_FLAT;
break;
case CLineStyle::kLineCapRound:
properties.startCap = properties.endCap = properties.dashCap = D2D1_CAP_STYLE_ROUND;
break;
case CLineStyle::kLineCapSquare:
properties.startCap = properties.endCap = properties.dashCap =
D2D1_CAP_STYLE_SQUARE;
break;
}
switch (state.lineStyle.getLineJoin ())
{
case CLineStyle::kLineJoinMiter:
properties.lineJoin = D2D1_LINE_JOIN_MITER;
break;
case CLineStyle::kLineJoinRound:
properties.lineJoin = D2D1_LINE_JOIN_ROUND;
break;
case CLineStyle::kLineJoinBevel:
properties.lineJoin = D2D1_LINE_JOIN_BEVEL;
break;
}
properties.dashOffset = static_cast<FLOAT> (state.lineStyle.getDashPhase ());
properties.miterLimit = 10.f;
if (state.lineStyle.getDashCount ())
{
properties.dashStyle = D2D1_DASH_STYLE_CUSTOM;
FLOAT* lengths = new FLOAT[state.lineStyle.getDashCount ()];
for (uint32_t i = 0; i < state.lineStyle.getDashCount (); i++)
lengths[i] = static_cast<FLOAT> (state.lineStyle.getDashLengths ()[i]);
factory->CreateStrokeStyle (properties, lengths, state.lineStyle.getDashCount (),
state.strokeStyle.adoptPtr ());
delete[] lengths;
}
else
{
properties.dashStyle = D2D1_DASH_STYLE_SOLID;
factory->CreateStrokeStyle (properties, nullptr, 0, state.strokeStyle.adoptPtr ());
}
}
struct State
{
CRect clip {};
CLineStyle lineStyle {kLineSolid};
CDrawMode drawMode {};
COM::Ptr<ID2D1StrokeStyle> strokeStyle;
COM::Ptr<ID2D1SolidColorBrush> fillBrush;
COM::Ptr<ID2D1SolidColorBrush> frameBrush;
COM::Ptr<ID2D1SolidColorBrush> fontBrush;
CColor fillColor {kTransparentCColor};
CColor frameColor {kTransparentCColor};
CColor fontColor {kTransparentCColor};
CCoord lineWidth {1.};
double globalAlpha {1.};
TransformMatrix tm {};
};
State state;
std::stack<State> stateStack;
double scaleFactor {1.};
CRect applyClip {};
bool beginDrawCalled {false};
TransformMatrix globalTM;
const D2DGraphicsDevice& device;
COM::Ptr<ID2D1DeviceContext> deviceContext;
#if defined(VSTGUI_TEXTRENDERING_LEGACY_INCONSISTENCY) && \
VSTGUI_TEXTRENDERING_LEGACY_INCONSISTENCY == 1
static constexpr auto antialiasMode = D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE;
#else
static constexpr auto antialiasMode = D2D1_TEXT_ANTIALIAS_MODE_DEFAULT;
#endif
};
//------------------------------------------------------------------------
D2DGraphicsDeviceContext::D2DGraphicsDeviceContext (const D2DGraphicsDevice& device,
ID2D1DeviceContext* deviceContext,
const TransformMatrix& tm)
{
impl = std::make_unique<Impl> (device, deviceContext, tm);
}
//------------------------------------------------------------------------
D2DGraphicsDeviceContext::~D2DGraphicsDeviceContext () noexcept { endDraw (); }
//------------------------------------------------------------------------
ID2D1DeviceContext* D2DGraphicsDeviceContext::getID2D1DeviceContext () const
{
return impl->deviceContext.get ();
}
//------------------------------------------------------------------------
const IPlatformGraphicsDevice& D2DGraphicsDeviceContext::getDevice () const { return impl->device; }
//------------------------------------------------------------------------
PlatformGraphicsPathFactoryPtr D2DGraphicsDeviceContext::getGraphicsPathFactory () const
{
return D2DGraphicsPathFactory::instance ();
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::beginDraw () const
{
impl->beginDrawCalled = true;
impl->deviceContext->BeginDraw ();
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::endDraw () const
{
if (impl->applyClip.isEmpty () == false)
impl->deviceContext->PopAxisAlignedClip ();
impl->applyClip = {};
if (impl->beginDrawCalled)
{
auto hr = impl->deviceContext->EndDraw ();
vstgui_assert (SUCCEEDED (hr));
impl->beginDrawCalled = false;
}
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::drawLine (LinePair line) const
{
impl->doInContext (
[&] (auto deviceContext) {
impl->applyFrameColor ();
impl->applyLineStyle ();
CPoint start (line.first);
CPoint end (line.second);
if (impl->state.drawMode.integralMode ())
{
pixelAlign (impl->state.tm, start);
pixelAlign (impl->state.tm, end);
}
deviceContext->DrawLine (convert (start), convert (end), impl->state.frameBrush.get (),
static_cast<FLOAT> (impl->state.lineWidth),
impl->state.strokeStyle.get ());
},
impl->lineWidthTransformMatrixOffset ());
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::drawLines (const LineList& lines) const
{
impl->doInContext (
[&] (auto deviceContext) {
impl->applyFrameColor ();
impl->applyLineStyle ();
if (impl->state.drawMode.integralMode ())
{
for (const auto& line : lines)
{
CPoint start (line.first);
CPoint end (line.second);
pixelAlign (impl->state.tm, start);
pixelAlign (impl->state.tm, end);
deviceContext->DrawLine (
convert (start), convert (end), impl->state.frameBrush.get (),
static_cast<FLOAT> (impl->state.lineWidth), impl->state.strokeStyle.get ());
}
}
else
{
for (const auto& line : lines)
{
CPoint start (line.first);
CPoint end (line.second);
deviceContext->DrawLine (
convert (start), convert (end), impl->state.frameBrush.get (),
static_cast<FLOAT> (impl->state.lineWidth), impl->state.strokeStyle.get ());
}
}
},
impl->lineWidthTransformMatrixOffset ());
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::drawPolygon (const PointList& polygonPointList,
PlatformGraphicsDrawStyle drawStyle) const
{
auto path = getGraphicsPathFactory ()->createPath ();
if (!path)
return false;
path->beginSubpath (polygonPointList[0]);
for (uint32_t i = 1; i < polygonPointList.size (); ++i)
{
path->addLine (polygonPointList[i]);
}
path->finishBuilding ();
if (drawStyle == PlatformGraphicsDrawStyle::Filled ||
drawStyle == PlatformGraphicsDrawStyle::FilledAndStroked)
{
drawGraphicsPath (*path, PlatformGraphicsPathDrawMode::Filled, nullptr);
}
if (drawStyle == PlatformGraphicsDrawStyle::Stroked ||
drawStyle == PlatformGraphicsDrawStyle::FilledAndStroked)
{
drawGraphicsPath (*path, PlatformGraphicsPathDrawMode::Stroked, nullptr);
}
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::drawRect (CRect rect, PlatformGraphicsDrawStyle drawStyle) const
{
impl->doInContext ([&] (auto deviceContext) {
if (drawStyle != PlatformGraphicsDrawStyle::Filled)
{
rect.right -= 1.;
rect.bottom -= 1.;
}
if (drawStyle == PlatformGraphicsDrawStyle::Filled ||
drawStyle == PlatformGraphicsDrawStyle::FilledAndStroked)
{
impl->applyFillColor ();
deviceContext->FillRectangle (convert (rect), impl->state.fillBrush.get ());
}
if (drawStyle == PlatformGraphicsDrawStyle::Stroked ||
drawStyle == PlatformGraphicsDrawStyle::FilledAndStroked)
{
rect.offset (impl->lineWidthTransformMatrixOffset ());
impl->applyFrameColor ();
impl->applyLineStyle ();
deviceContext->DrawRectangle (convert (rect), impl->state.frameBrush.get (),
static_cast<FLOAT> (impl->state.lineWidth),
impl->state.strokeStyle.get ());
}
});
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::drawArc (CRect rect, double startAngle1, double endAngle2,
PlatformGraphicsDrawStyle drawStyle) const
{
auto path = getGraphicsPathFactory ()->createPath ();
if (!path)
return false;
if (impl->state.drawMode.integralMode ())
pixelAlign (impl->state.tm, rect);
path->addArc (rect, startAngle1, endAngle2, true);
path->finishBuilding ();
if (drawStyle == PlatformGraphicsDrawStyle::Filled ||
drawStyle == PlatformGraphicsDrawStyle::FilledAndStroked)
drawGraphicsPath (*path, PlatformGraphicsPathDrawMode::Filled, nullptr);
if (drawStyle == PlatformGraphicsDrawStyle::Stroked ||
drawStyle == PlatformGraphicsDrawStyle::FilledAndStroked)
drawGraphicsPath (*path, PlatformGraphicsPathDrawMode::Stroked, nullptr);
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::drawEllipse (CRect rect, PlatformGraphicsDrawStyle drawStyle) const
{
impl->doInContext ([&] (auto deviceContext) {
if (impl->state.drawMode.integralMode ())
pixelAlign (impl->state.tm, rect);
if (drawStyle == PlatformGraphicsDrawStyle::Stroked)
rect.inset (0.5, 0.5);
CPoint center (rect.getTopLeft ());
center.offset (rect.getWidth () / 2., rect.getHeight () / 2.);
D2D1_ELLIPSE ellipse;
ellipse.point = convert (center);
ellipse.radiusX = (FLOAT)(rect.getWidth () / 2.);
ellipse.radiusY = (FLOAT)(rect.getHeight () / 2.);
if (drawStyle == PlatformGraphicsDrawStyle::Filled ||
drawStyle == PlatformGraphicsDrawStyle::FilledAndStroked)
{
impl->applyFillColor ();
deviceContext->FillEllipse (ellipse, impl->state.fillBrush.get ());
}
if (drawStyle == PlatformGraphicsDrawStyle::Stroked ||
drawStyle == PlatformGraphicsDrawStyle::FilledAndStroked)
{
impl->applyFrameColor ();
impl->applyLineStyle ();
deviceContext->DrawEllipse (ellipse, impl->state.frameBrush.get (),
static_cast<FLOAT> (impl->state.lineWidth),
impl->state.strokeStyle.get ());
}
});
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::drawPoint (CPoint point, CColor color) const { return false; }
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::drawBitmap (IPlatformBitmap& bitmap, CRect dest, CPoint offset,
double alpha, BitmapInterpolationQuality quality) const
{
D2DBitmap* d2dBitmap = dynamic_cast<D2DBitmap*> (&bitmap);
if (!d2dBitmap || !d2dBitmap->getSource ())
return false;
auto d2d1Bitmap =
D2DBitmapCache::getBitmap (d2dBitmap, impl->deviceContext.get (), impl->device.get ());
if (!d2d1Bitmap)
return false;
auto originalClip = impl->state.clip;
auto cr = dest;
impl->state.tm.transform (cr);
cr.bound (originalClip);
impl->state.clip = cr;
double bitmapScaleFactor = d2dBitmap->getScaleFactor ();
CGraphicsTransform bitmapTransform;
bitmapTransform.scale (1. / bitmapScaleFactor, 1. / bitmapScaleFactor);
auto originalTransformMatrix = impl->state.tm;
TransformMatrix tm = originalTransformMatrix * bitmapTransform;
setTransformMatrix (tm);
auto invBitmapTransform = bitmapTransform.inverse ();
invBitmapTransform.transform (dest);
invBitmapTransform.transform (offset);
impl->doInContext ([&] (auto deviceContext) {
auto bitmapSize = bitmap.getSize ();
CRect d (dest);
d.setWidth (bitmapSize.x);
d.setHeight (bitmapSize.y);
d.offset (-offset.x, -offset.y);
d.makeIntegral ();
CRect source;
source.setWidth (d2d1Bitmap->GetSize ().width);
source.setHeight (d2d1Bitmap->GetSize ().height);
D2D1_RECT_F sourceRect = convert (source);
deviceContext->DrawBitmap (d2d1Bitmap, convert (d),
static_cast<FLOAT> (alpha * impl->state.globalAlpha),
convert (quality), &sourceRect);
});
setTransformMatrix (originalTransformMatrix);
impl->state.clip = originalClip;
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::clearRect (CRect rect) const
{
#if 1
TransformGuard tmGuard (impl->deviceContext.get ());
impl->deviceContext->SetTransform (convert (impl->globalTM * impl->state.tm));
impl->deviceContext->PushAxisAlignedClip (convert (rect), D2D1_ANTIALIAS_MODE_ALIASED);
impl->deviceContext->Clear (D2D1::ColorF (1.f, 1.f, 1.f, 0.f));
impl->deviceContext->PopAxisAlignedClip ();
#else
CRect oldClip = impl->state.clip;
rect.bound (oldClip);
setClipRect (rect);
impl->doInContext (
[] (auto deviceContext) { deviceContext->Clear (D2D1::ColorF (1.f, 1.f, 1.f, 0.f)); });
setClipRect (oldClip);
#endif
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::drawGraphicsPath (IPlatformGraphicsPath& path,
PlatformGraphicsPathDrawMode mode,
TransformMatrix* transformation) const
{
auto d2dPath = dynamic_cast<D2DGraphicsPath*> (&path);
if (d2dPath == nullptr)
return false;
impl->doInContext ([&] (auto deviceContext) {
COM::Ptr<ID2D1Geometry> path;
if (transformation)
{
COM::Ptr<ID2D1Factory> factory {};
deviceContext->GetFactory (factory.adoptPtr ());
path = COM::adopt<ID2D1Geometry> (
d2dPath->createTransformedGeometry (factory.get (), *transformation));
}
else
{
path = COM::share<ID2D1Geometry> (d2dPath->getPathGeometry ());
}
if (path)
{
if (mode == PlatformGraphicsPathDrawMode::Filled ||
mode == PlatformGraphicsPathDrawMode::FilledEvenOdd)
{
impl->applyFillColor ();
deviceContext->FillGeometry (path.get (), impl->state.fillBrush.get ());
}
else if (mode == PlatformGraphicsPathDrawMode::Stroked)
{
impl->applyFrameColor ();
impl->applyLineStyle ();
deviceContext->DrawGeometry (path.get (), impl->state.frameBrush.get (),
(FLOAT)impl->state.lineWidth,
impl->state.strokeStyle.get ());
}
}
});
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::fillLinearGradient (IPlatformGraphicsPath& path,
const IPlatformGradient& gradient,
CPoint startPoint, CPoint endPoint, bool evenOdd,
TransformMatrix* transformation) const
{
auto d2dPath = dynamic_cast<D2DGraphicsPath*> (&path);
const auto d2dGradient = dynamic_cast<const D2DGradient*> (&gradient);
if (d2dPath == nullptr || d2dGradient == nullptr)
return false;
impl->doInContext ([&] (auto deviceContext) {
auto stopCollection = COM::adopt<ID2D1GradientStopCollection> (
d2dGradient->create (deviceContext, static_cast<FLOAT> (impl->state.globalAlpha)));
if (!stopCollection)
return;
COM::Ptr<ID2D1Geometry> path;
if (transformation)
{
COM::Ptr<ID2D1Factory> factory {};
deviceContext->GetFactory (factory.adoptPtr ());
path = COM::adopt<ID2D1Geometry> (
d2dPath->createTransformedGeometry (factory.get (), *transformation));
}
else
{
path = COM::share<ID2D1Geometry> (d2dPath->getPathGeometry ());
}
if (path)
{
COM::Ptr<ID2D1LinearGradientBrush> brush;
D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES properties;
properties.startPoint = convert (startPoint);
properties.endPoint = convert (endPoint);
if (SUCCEEDED (deviceContext->CreateLinearGradientBrush (
properties, stopCollection.get (), brush.adoptPtr ())))
{
deviceContext->FillGeometry (path.get (), brush.get ());
}
}
});
return true;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::fillRadialGradient (IPlatformGraphicsPath& path,
const IPlatformGradient& gradient, CPoint center,
CCoord radius, CPoint originOffset, bool evenOdd,
TransformMatrix* transformation) const
{
auto d2dPath = dynamic_cast<D2DGraphicsPath*> (&path);
const auto d2dGradient = dynamic_cast<const D2DGradient*> (&gradient);
if (d2dPath == nullptr || d2dGradient == nullptr)
return false;
impl->doInContext ([&] (auto deviceContext) {
auto stopCollection = COM::adopt<ID2D1GradientStopCollection> (
d2dGradient->create (deviceContext, static_cast<FLOAT> (impl->state.globalAlpha)));
if (!stopCollection)
return;
COM::Ptr<ID2D1Geometry> path;
if (transformation)
{
COM::Ptr<ID2D1Factory> factory {};
deviceContext->GetFactory (factory.adoptPtr ());
path = COM::adopt<ID2D1Geometry> (
d2dPath->createTransformedGeometry (factory.get (), *transformation));
}
else
{
path = COM::share<ID2D1Geometry> (d2dPath->getPathGeometry ());
}
if (path)
{
COM::Ptr<ID2D1RadialGradientBrush> brush;
D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES properties;
properties.center = convert (center);
properties.gradientOriginOffset = convert (originOffset);
properties.radiusX = (FLOAT)radius;
properties.radiusY = (FLOAT)radius;
if (SUCCEEDED (deviceContext->CreateRadialGradientBrush (
properties, stopCollection.get (), brush.adoptPtr ())))
{
deviceContext->FillGeometry (path.get (), brush.get ());
}
}
});
return true;
}
//------------------------------------------------------------------------
void D2DGraphicsDeviceContext::saveGlobalState () const { impl->stateStack.push (impl->state); }
//------------------------------------------------------------------------
void D2DGraphicsDeviceContext::restoreGlobalState () const
{
vstgui_assert (impl->stateStack.empty () == false,
"Unbalanced calls to saveGlobalState and restoreGlobalState");
#if NDEBUG
if (impl->stateStack.empty ())
return;
#endif
impl->state = impl->stateStack.top ();
impl->stateStack.pop ();
}
//------------------------------------------------------------------------
void D2DGraphicsDeviceContext::setLineStyle (const CLineStyle& style) const
{
if (impl->state.lineStyle != style)
{
impl->state.lineStyle = style;
impl->state.strokeStyle.reset ();
}
}
//------------------------------------------------------------------------
void D2DGraphicsDeviceContext::setLineWidth (CCoord width) const { impl->state.lineWidth = width; }
//------------------------------------------------------------------------
void D2DGraphicsDeviceContext::setDrawMode (CDrawMode mode) const { impl->state.drawMode = mode; }
//------------------------------------------------------------------------
void D2DGraphicsDeviceContext::setClipRect (CRect clip) const { impl->state.clip = clip; }
//------------------------------------------------------------------------
void D2DGraphicsDeviceContext::setFillColor (CColor color) const
{
if (impl->state.fillColor != color)
{
impl->state.fillColor = color;
impl->state.fillBrush.reset ();
}
}
//------------------------------------------------------------------------
void D2DGraphicsDeviceContext::setFrameColor (CColor color) const
{
if (impl->state.frameColor != color)
{
impl->state.frameColor = color;
impl->state.frameBrush.reset ();
}
}
//------------------------------------------------------------------------
void D2DGraphicsDeviceContext::setGlobalAlpha (double newAlpha) const
{
if (impl->state.globalAlpha != newAlpha)
{
impl->state.globalAlpha = newAlpha;
impl->state.fillBrush.reset ();
impl->state.frameBrush.reset ();
impl->state.fontBrush.reset ();
}
}
//------------------------------------------------------------------------
void D2DGraphicsDeviceContext::setTransformMatrix (const TransformMatrix& tm) const
{
impl->state.tm = tm;
}
//------------------------------------------------------------------------
const IPlatformGraphicsDeviceContextBitmapExt* D2DGraphicsDeviceContext::asBitmapExt () const
{
return this;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::drawBitmapNinePartTiled (IPlatformBitmap& bitmap, CRect dest,
const CNinePartTiledDescription& desc,
double alpha,
BitmapInterpolationQuality quality) const
{
return false;
}
//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::fillRectWithBitmap (IPlatformBitmap& bitmap, CRect srcRect,
CRect dstRect, double alpha,
BitmapInterpolationQuality quality) const
{
D2DBitmap* d2dBitmap = dynamic_cast<D2DBitmap*> (&bitmap);
if (!d2dBitmap || !d2dBitmap->getSource ())
return false;
auto d2d1Bitmap =
D2DBitmapCache::getBitmap (d2dBitmap, impl->deviceContext.get (), impl->device.get ());
if (!d2d1Bitmap)
return false;
auto originalClip = impl->state.clip;
auto cr = dstRect;
impl->state.tm.transform (cr);
cr.bound (originalClip);
double bitmapScaleFactor = d2dBitmap->getScaleFactor ();
CGraphicsTransform bitmapTransform;
bitmapTransform.scale (1. / bitmapScaleFactor, 1. / bitmapScaleFactor);
auto invBitmapTransform = bitmapTransform.inverse ();
invBitmapTransform.transform (dstRect);
invBitmapTransform.transform (srcRect);
auto brush =
createBrushFromBitmap (impl->deviceContext.get (), d2d1Bitmap, srcRect, dstRect, quality);
if (!brush)
return false;
impl->state.clip = cr;
auto originalTransformMatrix = impl->state.tm;
TransformMatrix tm = originalTransformMatrix * bitmapTransform;
setTransformMatrix (tm);
impl->doInContext ([&] (auto deviceContext) {
deviceContext->FillRectangle (convert (dstRect), brush.get ());
});
setTransformMatrix (originalTransformMatrix);
impl->state.clip = originalClip;
return true;
}
//------------------------------------------------------------------------
void D2DGraphicsDeviceContext::drawTextLayout (IDWriteTextLayout* textLayout, CPoint pos,
CColor color, bool antialias)
{
impl->doInContext ([&] (auto deviceContext) {
deviceContext->SetTextAntialiasMode (antialias ? impl->antialiasMode
: D2D1_TEXT_ANTIALIAS_MODE_ALIASED);
if (impl->state.drawMode.integralMode ())
pos.makeIntegral ();
pos.y += 0.5;
impl->applyFontColor (color);
deviceContext->DrawTextLayout (convert (pos), textLayout, impl->state.fontBrush.get ());
});
}
//------------------------------------------------------------------------
} // VSTGUI