-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
DrawingEngine.cpp
1626 lines (1260 loc) · 37.3 KB
/
DrawingEngine.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
/*
* Copyright 2001-2009, Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "DrawingEngine.h"
#include <Bitmap.h>
#include <StackOrHeapArray.h>
#include <stdio.h>
#include <algorithm>
#include <stack>
#include "DrawState.h"
#include "GlyphLayoutEngine.h"
#include "Painter.h"
#include "ServerBitmap.h"
#include "ServerCursor.h"
#include "RenderingBuffer.h"
#include "drawing_support.h"
#if DEBUG
# define ASSERT_PARALLEL_LOCKED() \
{ if (!IsParallelAccessLocked()) debugger("not parallel locked!"); }
# define ASSERT_EXCLUSIVE_LOCKED() \
{ if (!IsExclusiveAccessLocked()) debugger("not exclusive locked!"); }
#else
# define ASSERT_PARALLEL_LOCKED()
# define ASSERT_EXCLUSIVE_LOCKED()
#endif
static inline void
make_rect_valid(BRect& rect)
{
if (rect.left > rect.right) {
float temp = rect.left;
rect.left = rect.right;
rect.right = temp;
}
if (rect.top > rect.bottom) {
float temp = rect.top;
rect.top = rect.bottom;
rect.bottom = temp;
}
}
static inline void
extend_by_stroke_width(BRect& rect, float penSize)
{
// "- 0.5" because if stroke width == 1, we don't need to extend
float inset = -ceilf(penSize / 2.0 - 0.5);
rect.InsetBy(inset, inset);
}
class AutoFloatingOverlaysHider {
public:
AutoFloatingOverlaysHider(HWInterface* interface, const BRect& area)
:
fInterface(interface),
fHidden(interface->HideFloatingOverlays(area))
{
}
AutoFloatingOverlaysHider(HWInterface* interface)
:
fInterface(interface),
fHidden(fInterface->HideFloatingOverlays())
{
}
~AutoFloatingOverlaysHider()
{
if (fHidden)
fInterface->ShowFloatingOverlays();
}
bool WasHidden() const
{
return fHidden;
}
private:
HWInterface* fInterface;
bool fHidden;
};
// #pragma mark -
DrawingEngine::DrawingEngine(HWInterface* interface)
:
fPainter(new Painter()),
fGraphicsCard(NULL),
fAvailableHWAccleration(0),
fSuspendSyncLevel(0),
fCopyToFront(true)
{
SetHWInterface(interface);
}
DrawingEngine::~DrawingEngine()
{
SetHWInterface(NULL);
delete fPainter;
}
// #pragma mark - locking
bool
DrawingEngine::LockParallelAccess()
{
return fGraphicsCard->LockParallelAccess();
}
#if DEBUG
bool
DrawingEngine::IsParallelAccessLocked() const
{
return fGraphicsCard->IsParallelAccessLocked();
}
#endif
void
DrawingEngine::UnlockParallelAccess()
{
fGraphicsCard->UnlockParallelAccess();
}
bool
DrawingEngine::LockExclusiveAccess()
{
return fGraphicsCard->LockExclusiveAccess();
}
bool
DrawingEngine::IsExclusiveAccessLocked() const
{
return fGraphicsCard->IsExclusiveAccessLocked();
}
void
DrawingEngine::UnlockExclusiveAccess()
{
fGraphicsCard->UnlockExclusiveAccess();
}
// #pragma mark -
void
DrawingEngine::FrameBufferChanged()
{
if (!fGraphicsCard) {
fPainter->DetachFromBuffer();
fAvailableHWAccleration = 0;
return;
}
// NOTE: locking is probably bogus, since we are called
// in the thread that changed the frame buffer...
if (LockExclusiveAccess()) {
fPainter->AttachToBuffer(fGraphicsCard->DrawingBuffer());
// available HW acceleration might have changed
fAvailableHWAccleration = fGraphicsCard->AvailableHWAcceleration();
UnlockExclusiveAccess();
}
}
void
DrawingEngine::SetHWInterface(HWInterface* interface)
{
if (fGraphicsCard == interface)
return;
if (fGraphicsCard)
fGraphicsCard->RemoveListener(this);
fGraphicsCard = interface;
if (fGraphicsCard)
fGraphicsCard->AddListener(this);
FrameBufferChanged();
}
void
DrawingEngine::SetCopyToFrontEnabled(bool enable)
{
fCopyToFront = enable;
}
void
DrawingEngine::CopyToFront(/*const*/ BRegion& region)
{
fGraphicsCard->InvalidateRegion(region);
}
// #pragma mark -
//! the DrawingEngine needs to be locked!
void
DrawingEngine::ConstrainClippingRegion(const BRegion* region)
{
ASSERT_PARALLEL_LOCKED();
fPainter->ConstrainClipping(region);
}
void
DrawingEngine::SetDrawState(const DrawState* state, int32 xOffset,
int32 yOffset)
{
fPainter->SetDrawState(state, xOffset, yOffset);
}
void
DrawingEngine::SetHighColor(const rgb_color& color)
{
fPainter->SetHighColor(color);
}
void
DrawingEngine::SetLowColor(const rgb_color& color)
{
fPainter->SetLowColor(color);
}
void
DrawingEngine::SetPenSize(float size)
{
fPainter->SetPenSize(size);
}
void
DrawingEngine::SetStrokeMode(cap_mode lineCap, join_mode joinMode,
float miterLimit)
{
fPainter->SetStrokeMode(lineCap, joinMode, miterLimit);
}
void
DrawingEngine::SetFillRule(int32 fillRule)
{
fPainter->SetFillRule(fillRule);
}
void
DrawingEngine::SetBlendingMode(source_alpha srcAlpha, alpha_function alphaFunc)
{
fPainter->SetBlendingMode(srcAlpha, alphaFunc);
}
void
DrawingEngine::SetPattern(const struct pattern& pattern)
{
fPainter->SetPattern(pattern, false);
}
void
DrawingEngine::SetDrawingMode(drawing_mode mode)
{
fPainter->SetDrawingMode(mode);
}
void
DrawingEngine::SetDrawingMode(drawing_mode mode, drawing_mode& oldMode)
{
oldMode = fPainter->DrawingMode();
fPainter->SetDrawingMode(mode);
}
void
DrawingEngine::SetFont(const ServerFont& font)
{
fPainter->SetFont(font);
}
void
DrawingEngine::SetFont(const DrawState* state)
{
fPainter->SetFont(state);
}
// #pragma mark -
void
DrawingEngine::SuspendAutoSync()
{
ASSERT_PARALLEL_LOCKED();
fSuspendSyncLevel++;
}
void
DrawingEngine::Sync()
{
ASSERT_PARALLEL_LOCKED();
fSuspendSyncLevel--;
if (fSuspendSyncLevel == 0)
fGraphicsCard->Sync();
}
// #pragma mark -
// CopyRegion() does a topological sort of the rects in the
// region. The algorithm was suggested by Ingo Weinhold.
// It compares each rect with each rect and builds a tree
// of successors so we know the order in which they can be copied.
// For example, let's suppose these rects are in a BRegion:
// ************
// * B *
// ************
// *************
// * *
// * A ****************
// * ** *
// ************** *
// * C *
// * *
// * *
// ***************
// When copying stuff from LEFT TO RIGHT, TOP TO BOTTOM, the
// result of the sort will be C, A, B. For this direction, we search
// for the rects that have no neighbors to their right and to their
// bottom, These can be copied without drawing into the area of
// rects yet to be copied. If you move from RIGHT TO LEFT, BOTTOM TO TOP,
// you go look for the ones that have no neighbors to their top and left.
//
// Here I draw some rays to illustrate LEFT TO RIGHT, TOP TO BOTTOM:
// ************
// * B *
// ************
// *************
// * *
// * A ****************-----------------
// * ** *
// ************** *
// * C *
// * *
// * *
// ***************
// |
// |
// |
// |
// There are no rects in the area defined by the rays to the right
// and bottom of rect C, so that's the one we want to copy first
// (for positive x and y offsets).
// Since A is to the left of C and B is to the top of C, The "node"
// for C will point to the nodes of A and B as its "successors". Therefor,
// A and B will have an "indegree" of 1 for C pointing to them. C will
// have an "indegree" of 0, because there was no rect to which C
// was to the left or top of. When comparing A and B, neither is left
// or top from the other and in the sense that the algorithm cares about.
// NOTE: comparison of coordinates assumes that rects don't overlap
// and don't share the actual edge either (as is the case in BRegions).
struct node {
node()
{
pointers = NULL;
}
node(const BRect& r, int32 maxPointers)
{
init(r, maxPointers);
}
~node()
{
delete [] pointers;
}
void init(const BRect& r, int32 maxPointers)
{
rect = r;
pointers = new node*[maxPointers];
in_degree = 0;
next_pointer = 0;
}
void push(node* node)
{
pointers[next_pointer] = node;
next_pointer++;
}
node* top()
{
return pointers[next_pointer];
}
node* pop()
{
node* ret = top();
next_pointer--;
return ret;
}
BRect rect;
int32 in_degree;
node** pointers;
int32 next_pointer;
};
static bool
is_left_of(const BRect& a, const BRect& b)
{
return (a.right < b.left);
}
static bool
is_above(const BRect& a, const BRect& b)
{
return (a.bottom < b.top);
}
void
DrawingEngine::CopyRegion(/*const*/ BRegion* region, int32 xOffset,
int32 yOffset)
{
ASSERT_PARALLEL_LOCKED();
BRect frame = region->Frame();
frame = frame | frame.OffsetByCopy(xOffset, yOffset);
AutoFloatingOverlaysHider _(fGraphicsCard, frame);
int32 count = region->CountRects();
// TODO: make this step unnecessary
// (by using different stack impl inside node)
BStackOrHeapArray<node, 64> nodes(count);
for (int32 i= 0; i < count; i++) {
nodes[i].init(region->RectAt(i), count);
}
for (int32 i = 0; i < count; i++) {
BRect a = region->RectAt(i);
for (int32 k = i + 1; k < count; k++) {
BRect b = region->RectAt(k);
int cmp = 0;
// compare horizontally
if (xOffset > 0) {
if (is_left_of(a, b)) {
cmp -= 1;
} else if (is_left_of(b, a)) {
cmp += 1;
}
} else if (xOffset < 0) {
if (is_left_of(a, b)) {
cmp += 1;
} else if (is_left_of(b, a)) {
cmp -= 1;
}
}
// compare vertically
if (yOffset > 0) {
if (is_above(a, b)) {
cmp -= 1;
} else if (is_above(b, a)) {
cmp += 1;
}
} else if (yOffset < 0) {
if (is_above(a, b)) {
cmp += 1;
} else if (is_above(b, a)) {
cmp -= 1;
}
}
// add appropriate node as successor
if (cmp > 0) {
nodes[i].push(&nodes[k]);
nodes[k].in_degree++;
} else if (cmp < 0) {
nodes[k].push(&nodes[i]);
nodes[i].in_degree++;
}
}
}
// put all nodes onto a stack that have an "indegree" count of zero
std::stack<node*> inDegreeZeroNodes;
for (int32 i = 0; i < count; i++) {
if (nodes[i].in_degree == 0) {
inDegreeZeroNodes.push(&nodes[i]);
}
}
// pop the rects from the stack, do the actual copy operation
// and decrease the "indegree" count of the other rects not
// currently on the stack and to which the current rect pointed
// to. If their "indegree" count reaches zero, put them onto the
// stack as well.
clipping_rect* sortedRectList = NULL;
int32 nextSortedIndex = 0;
if (fAvailableHWAccleration & HW_ACC_COPY_REGION)
sortedRectList = new clipping_rect[count];
while (!inDegreeZeroNodes.empty()) {
node* n = inDegreeZeroNodes.top();
inDegreeZeroNodes.pop();
// do the software implementation or add to sorted
// rect list for using the HW accelerated version
// later
if (sortedRectList) {
sortedRectList[nextSortedIndex].left = (int32)n->rect.left;
sortedRectList[nextSortedIndex].top = (int32)n->rect.top;
sortedRectList[nextSortedIndex].right = (int32)n->rect.right;
sortedRectList[nextSortedIndex].bottom = (int32)n->rect.bottom;
nextSortedIndex++;
} else {
BRect touched = CopyRect(n->rect, xOffset, yOffset);
fGraphicsCard->Invalidate(touched);
}
for (int32 k = 0; k < n->next_pointer; k++) {
n->pointers[k]->in_degree--;
if (n->pointers[k]->in_degree == 0)
inDegreeZeroNodes.push(n->pointers[k]);
}
}
// trigger the HW accelerated version if it was available
if (sortedRectList) {
fGraphicsCard->CopyRegion(sortedRectList, count, xOffset, yOffset);
if (fGraphicsCard->IsDoubleBuffered()) {
fGraphicsCard->Invalidate(
region->Frame().OffsetByCopy(xOffset, yOffset));
}
}
delete[] sortedRectList;
}
void
DrawingEngine::InvertRect(BRect r)
{
ASSERT_PARALLEL_LOCKED();
make_rect_valid(r);
// NOTE: Currently ignores view transformation, so no TransformAndClipRect()
r = fPainter->ClipRect(r);
if (!r.IsValid())
return;
AutoFloatingOverlaysHider _(fGraphicsCard, r);
// try hardware optimized version first
if (fAvailableHWAccleration & HW_ACC_INVERT_REGION) {
BRegion region(r);
region.IntersectWith(fPainter->ClippingRegion());
fGraphicsCard->InvertRegion(region);
} else {
fPainter->InvertRect(r);
}
_CopyToFront(r);
}
void
DrawingEngine::DrawBitmap(ServerBitmap* bitmap, const BRect& bitmapRect,
const BRect& viewRect, uint32 options)
{
ASSERT_PARALLEL_LOCKED();
BRect clipped = fPainter->TransformAndClipRect(viewRect);
if (clipped.IsValid()) {
AutoFloatingOverlaysHider _(fGraphicsCard, clipped);
fPainter->DrawBitmap(bitmap, bitmapRect, viewRect, options);
_CopyToFront(clipped);
}
}
void
DrawingEngine::DrawArc(BRect r, const float& angle, const float& span,
bool filled)
{
ASSERT_PARALLEL_LOCKED();
make_rect_valid(r);
fPainter->AlignEllipseRect(&r, filled);
BRect clipped(r);
if (!filled)
extend_by_stroke_width(clipped, fPainter->PenSize());
clipped = fPainter->TransformAndClipRect(r);
if (clipped.IsValid()) {
AutoFloatingOverlaysHider _(fGraphicsCard, clipped);
float xRadius = r.Width() / 2.0;
float yRadius = r.Height() / 2.0;
BPoint center(r.left + xRadius,
r.top + yRadius);
if (filled)
fPainter->FillArc(center, xRadius, yRadius, angle, span);
else
fPainter->StrokeArc(center, xRadius, yRadius, angle, span);
_CopyToFront(clipped);
}
}
void
DrawingEngine::FillArc(BRect r, const float& angle, const float& span,
const BGradient& gradient)
{
ASSERT_PARALLEL_LOCKED();
make_rect_valid(r);
fPainter->AlignEllipseRect(&r, true);
BRect clipped(r);
clipped = fPainter->TransformAndClipRect(r);
if (clipped.IsValid()) {
AutoFloatingOverlaysHider _(fGraphicsCard, clipped);
float xRadius = r.Width() / 2.0;
float yRadius = r.Height() / 2.0;
BPoint center(r.left + xRadius,
r.top + yRadius);
fPainter->FillArc(center, xRadius, yRadius, angle, span, gradient);
_CopyToFront(clipped);
}
}
void
DrawingEngine::DrawBezier(BPoint* pts, bool filled)
{
ASSERT_PARALLEL_LOCKED();
// TODO: figure out bounds and hide cursor depending on that
AutoFloatingOverlaysHider _(fGraphicsCard);
BRect touched = fPainter->DrawBezier(pts, filled);
_CopyToFront(touched);
}
void
DrawingEngine::FillBezier(BPoint* pts, const BGradient& gradient)
{
ASSERT_PARALLEL_LOCKED();
// TODO: figure out bounds and hide cursor depending on that
AutoFloatingOverlaysHider _(fGraphicsCard);
BRect touched = fPainter->FillBezier(pts, gradient);
_CopyToFront(touched);
}
void
DrawingEngine::DrawEllipse(BRect r, bool filled)
{
ASSERT_PARALLEL_LOCKED();
make_rect_valid(r);
BRect clipped = r;
fPainter->AlignEllipseRect(&clipped, filled);
if (!filled)
extend_by_stroke_width(clipped, fPainter->PenSize());
clipped.left = floorf(clipped.left);
clipped.top = floorf(clipped.top);
clipped.right = ceilf(clipped.right);
clipped.bottom = ceilf(clipped.bottom);
clipped = fPainter->TransformAndClipRect(clipped);
if (clipped.IsValid()) {
AutoFloatingOverlaysHider _(fGraphicsCard, clipped);
fPainter->DrawEllipse(r, filled);
_CopyToFront(clipped);
}
}
void
DrawingEngine::FillEllipse(BRect r, const BGradient& gradient)
{
ASSERT_PARALLEL_LOCKED();
make_rect_valid(r);
BRect clipped = r;
fPainter->AlignEllipseRect(&clipped, true);
clipped.left = floorf(clipped.left);
clipped.top = floorf(clipped.top);
clipped.right = ceilf(clipped.right);
clipped.bottom = ceilf(clipped.bottom);
clipped = fPainter->TransformAndClipRect(clipped);
if (clipped.IsValid()) {
AutoFloatingOverlaysHider _(fGraphicsCard, clipped);
fPainter->FillEllipse(r, gradient);
_CopyToFront(clipped);
}
}
void
DrawingEngine::DrawPolygon(BPoint* ptlist, int32 numpts, BRect bounds,
bool filled, bool closed)
{
ASSERT_PARALLEL_LOCKED();
make_rect_valid(bounds);
if (!filled)
extend_by_stroke_width(bounds, fPainter->PenSize());
bounds = fPainter->TransformAndClipRect(bounds);
if (bounds.IsValid()) {
AutoFloatingOverlaysHider _(fGraphicsCard, bounds);
fPainter->DrawPolygon(ptlist, numpts, filled, closed);
_CopyToFront(bounds);
}
}
void
DrawingEngine::FillPolygon(BPoint* ptlist, int32 numpts, BRect bounds,
const BGradient& gradient, bool closed)
{
ASSERT_PARALLEL_LOCKED();
make_rect_valid(bounds);
bounds = fPainter->TransformAndClipRect(bounds);
if (bounds.IsValid()) {
AutoFloatingOverlaysHider _(fGraphicsCard, bounds);
fPainter->FillPolygon(ptlist, numpts, gradient, closed);
_CopyToFront(bounds);
}
}
// #pragma mark - rgb_color
void
DrawingEngine::StrokePoint(const BPoint& pt, const rgb_color& color)
{
StrokeLine(pt, pt, color);
}
/*! This function is only used by Decorators,
it assumes a one pixel wide line
*/
void
DrawingEngine::StrokeLine(const BPoint& start, const BPoint& end,
const rgb_color& color)
{
ASSERT_PARALLEL_LOCKED();
BRect touched(start, end);
make_rect_valid(touched);
touched = fPainter->ClipRect(touched);
AutoFloatingOverlaysHider _(fGraphicsCard, touched);
if (!fPainter->StraightLine(start, end, color)) {
rgb_color previousColor = fPainter->HighColor();
drawing_mode previousMode = fPainter->DrawingMode();
fPainter->SetHighColor(color);
fPainter->SetDrawingMode(B_OP_OVER);
fPainter->StrokeLine(start, end);
fPainter->SetDrawingMode(previousMode);
fPainter->SetHighColor(previousColor);
}
_CopyToFront(touched);
}
//! This function is used to draw a one pixel wide rect
void
DrawingEngine::StrokeRect(BRect r, const rgb_color& color)
{
ASSERT_PARALLEL_LOCKED();
make_rect_valid(r);
BRect clipped = fPainter->ClipRect(r);
if (clipped.IsValid()) {
AutoFloatingOverlaysHider _(fGraphicsCard, clipped);
fPainter->StrokeRect(r, color);
_CopyToFront(clipped);
}
}
void
DrawingEngine::FillRect(BRect r, const rgb_color& color)
{
ASSERT_PARALLEL_LOCKED();
// NOTE: Write locking because we might use HW acceleration.
// This needs to be investigated, I'm doing this because of
// gut feeling.
make_rect_valid(r);
r = fPainter->ClipRect(r);
if (!r.IsValid())
return;
AutoFloatingOverlaysHider overlaysHider(fGraphicsCard, r);
// try hardware optimized version first
if (fAvailableHWAccleration & HW_ACC_FILL_REGION) {
BRegion region(r);
region.IntersectWith(fPainter->ClippingRegion());
fGraphicsCard->FillRegion(region, color,
fSuspendSyncLevel == 0 || overlaysHider.WasHidden());
} else {
fPainter->FillRect(r, color);
}
_CopyToFront(r);
}
void
DrawingEngine::FillRegion(BRegion& r, const rgb_color& color)
{
ASSERT_PARALLEL_LOCKED();
// NOTE: region expected to be already clipped correctly!!
BRect frame = r.Frame();
if (!fPainter->Bounds().Contains(frame)) {
// NOTE: I am not quite sure yet how this can happen, but apparently it
// can (see bug #634).
// This function is used for internal app_server painting, in the case of
// bug #634, the background of views is painted. But the view region
// should never be outside the frame buffer bounds.
// char message[1024];
// BRect bounds = fPainter->Bounds();
// sprintf(message, "FillRegion() - painter: (%d, %d)->(%d, %d), region: (%d, %d)->(%d, %d)",
// (int)bounds.left, (int)bounds.top, (int)bounds.right, (int)bounds.bottom,
// (int)frame.left, (int)frame.top, (int)frame.right, (int)frame.bottom);
// debugger(message);
return;
}
AutoFloatingOverlaysHider overlaysHider(fGraphicsCard, frame);
// try hardware optimized version first
if ((fAvailableHWAccleration & HW_ACC_FILL_REGION) != 0
&& frame.Width() * frame.Height() > 100) {
fGraphicsCard->FillRegion(r, color, fSuspendSyncLevel == 0
|| overlaysHider.WasHidden());
} else {
int32 count = r.CountRects();
for (int32 i = 0; i < count; i++)
fPainter->FillRectNoClipping(r.RectAtInt(i), color);
}
_CopyToFront(frame);
}
// #pragma mark - DrawState
void
DrawingEngine::StrokeRect(BRect r)
{
ASSERT_PARALLEL_LOCKED();
// support invalid rects
make_rect_valid(r);
BRect clipped(r);
extend_by_stroke_width(clipped, fPainter->PenSize());
clipped = fPainter->TransformAndClipRect(clipped);
if (clipped.IsValid()) {
AutoFloatingOverlaysHider _(fGraphicsCard, clipped);
fPainter->StrokeRect(r);
_CopyToFront(clipped);
}
}
void
DrawingEngine::FillRect(BRect r)
{
ASSERT_PARALLEL_LOCKED();
make_rect_valid(r);
r = fPainter->AlignRect(r);
BRect dirty = fPainter->TransformAndClipRect(r);
if (!dirty.IsValid())
return;
AutoFloatingOverlaysHider overlaysHider(fGraphicsCard, dirty);
bool doInSoftware = true;
if (fPainter->IsIdentityTransform()) {
// TODO the accelerated code path may also be used for transforms that
// only scale and translate (but don't shear or rotate).
if ((r.Width() + 1) * (r.Height() + 1) > 100.0) {
// try hardware optimized version first
// if the rect is large enough
if ((fAvailableHWAccleration & HW_ACC_FILL_REGION) != 0) {
if (fPainter->Pattern() == B_SOLID_HIGH
&& (fPainter->DrawingMode() == B_OP_COPY
|| fPainter->DrawingMode() == B_OP_OVER)) {
BRegion region(r);
region.IntersectWith(fPainter->ClippingRegion());
fGraphicsCard->FillRegion(region, fPainter->HighColor(),
fSuspendSyncLevel == 0 || overlaysHider.WasHidden());
doInSoftware = false;
} else if (fPainter->Pattern() == B_SOLID_LOW
&& fPainter->DrawingMode() == B_OP_COPY) {
BRegion region(r);
region.IntersectWith(fPainter->ClippingRegion());
fGraphicsCard->FillRegion(region, fPainter->LowColor(),
fSuspendSyncLevel == 0 || overlaysHider.WasHidden());
doInSoftware = false;
}