OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrAAStrokeRectBatch.h" | 8 #include "GrAAStrokeRectBatch.h" |
9 | 9 |
10 #include "GrBatchFlushState.h" | 10 #include "GrBatchFlushState.h" |
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
513 for (int i = 0; i < innerVertexNum; ++i) { | 513 for (int i = 0; i < innerVertexNum; ++i) { |
514 if (tweakAlphaForCoverage) { | 514 if (tweakAlphaForCoverage) { |
515 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; | 515 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; |
516 } else { | 516 } else { |
517 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; | 517 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
518 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor) ) = innerCoverage; | 518 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor) ) = innerCoverage; |
519 } | 519 } |
520 } | 520 } |
521 } | 521 } |
522 | 522 |
523 inline static bool is_miter(const SkStrokeRec& stroke) { | |
524 // For hairlines, make bevel and round joins appear the same as mitered ones . | |
525 // small miter limit means right angles show bevel... | |
526 if ((stroke.getWidth() > 0) && (stroke.getJoin() != SkPaint::kMiter_Join || | |
527 stroke.getMiter() < SK_ScalarSqrt2)) { | |
528 return false; | |
529 } | |
530 return true; | |
531 } | |
532 | |
533 static void compute_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside, | |
534 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect, | |
535 SkScalar strokeWidth, bool miterStroke) { | |
536 SkRect devRect; | |
537 viewMatrix.mapRect(&devRect, rect); | |
538 | |
539 SkVector devStrokeSize; | |
540 if (strokeWidth > 0) { | |
541 devStrokeSize.set(strokeWidth, strokeWidth); | |
542 viewMatrix.mapVectors(&devStrokeSize, 1); | |
543 devStrokeSize.setAbs(devStrokeSize); | |
544 } else { | |
545 devStrokeSize.set(SK_Scalar1, SK_Scalar1); | |
546 } | |
547 | |
548 const SkScalar dx = devStrokeSize.fX; | |
549 const SkScalar dy = devStrokeSize.fY; | |
550 const SkScalar rx = SkScalarMul(dx, SK_ScalarHalf); | |
551 const SkScalar ry = SkScalarMul(dy, SK_ScalarHalf); | |
552 | |
553 *devOutside = devRect; | |
554 *devOutsideAssist = devRect; | |
555 *devInside = devRect; | |
556 | |
557 devOutside->outset(rx, ry); | |
558 devInside->inset(rx, ry); | |
559 | |
560 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we | |
561 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points | |
562 // together when we render these rects. | |
563 SkScalar spare; | |
564 { | |
565 SkScalar w = devRect.width() - dx; | |
566 SkScalar h = devRect.height() - dy; | |
567 spare = SkTMin(w, h); | |
568 } | |
569 | |
570 *isDegenerate = spare <= 0; | |
571 if (*isDegenerate) { | |
572 devInside->fLeft = devInside->fRight = devRect.centerX(); | |
573 devInside->fTop = devInside->fBottom = devRect.centerY(); | |
574 } | |
575 | |
576 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist) | |
577 // to draw the outside of the octagon. Because there are 8 vertices on the o uter | |
578 // edge, while vertex number of inner edge is 4, the same as miter-stroke. | |
579 if (!miterStroke) { | |
580 devOutside->inset(0, ry); | |
581 devOutsideAssist->outset(0, ry); | |
582 } | |
583 } | |
584 | |
523 namespace GrAAStrokeRectBatch { | 585 namespace GrAAStrokeRectBatch { |
524 | 586 |
525 GrDrawBatch* Create(GrColor color, | 587 GrDrawBatch* Create(GrColor color, |
526 const SkMatrix& viewMatrix, | 588 const SkMatrix& viewMatrix, |
527 const SkRect& devOutside, | 589 const SkRect& devOutside, |
528 const SkRect& devOutsideAssist, | 590 const SkRect& devOutsideAssist, |
529 const SkRect& devInside, | 591 const SkRect& devInside, |
530 bool miterStroke, | 592 bool miterStroke, |
531 bool degenerate) { | 593 bool degenerate) { |
532 AAStrokeRectBatch* batch = AAStrokeRectBatch::Create(viewMatrix, miterStroke ); | 594 AAStrokeRectBatch* batch = AAStrokeRectBatch::Create(viewMatrix, miterStroke ); |
533 batch->append(color, devOutside, devOutsideAssist, devInside, degenerate); | 595 batch->append(color, devOutside, devOutsideAssist, devInside, degenerate); |
534 batch->init(); | 596 batch->init(); |
535 return batch; | 597 return batch; |
536 } | 598 } |
537 | 599 |
600 GrDrawBatch* Create(GrColor color, | |
601 const SkMatrix& viewMatrix, | |
602 const SkRect& rect, | |
603 const SkStrokeRec& stroke) { | |
604 bool isMiterStroke = is_miter(stroke); | |
605 AAStrokeRectBatch* batch = AAStrokeRectBatch::Create(viewMatrix, isMiterStro ke); | |
606 | |
607 SkRect devOutside, devOutsideAssist, devInside; | |
608 bool isDegenerate; | |
609 compute_rects(&devOutside, &devOutsideAssist, &devInside, &isDegenerate, vie wMatrix, | |
610 rect, stroke.getWidth(), isMiterStroke); | |
611 | |
612 batch->append(color, devOutside, devOutsideAssist, devInside, isDegenerate); | |
613 batch->init(); | |
614 return batch; | |
615 } | |
616 | |
538 bool Append(GrBatch* origBatch, | 617 bool Append(GrBatch* origBatch, |
539 GrColor color, | 618 GrColor color, |
540 const SkMatrix& viewMatrix, | 619 const SkMatrix& viewMatrix, |
541 const SkRect& devOutside, | 620 const SkRect& rect, |
542 const SkRect& devOutsideAssist, | 621 const SkStrokeRec& stroke) { |
543 const SkRect& devInside, | |
544 bool miterStroke, | |
545 bool degenerate) { | |
546 AAStrokeRectBatch* batch = origBatch->cast<AAStrokeRectBatch>(); | 622 AAStrokeRectBatch* batch = origBatch->cast<AAStrokeRectBatch>(); |
547 | 623 |
548 // we can't batch across vm changes | 624 // we can't batch across vm changes |
549 if (!batch->canAppend(viewMatrix, miterStroke)) { | 625 bool isMiterStroke = is_miter(stroke); |
626 if (!batch->canAppend(viewMatrix, isMiterStroke)) { | |
550 return false; | 627 return false; |
551 } | 628 } |
552 | 629 |
robertphillips
2015/09/22 17:55:51
It is too bad we can't compute all the SkRects in
| |
553 batch->appendAndUpdateBounds(color, devOutside, devOutsideAssist, devInside, degenerate); | 630 SkRect devOutside, devOutsideAssist, devInside; |
631 bool isDegenerate; | |
632 compute_rects(&devOutside, &devOutsideAssist, &devInside, &isDegenerate, vie wMatrix, | |
633 rect, stroke.getWidth(), isMiterStroke); | |
634 | |
635 batch->appendAndUpdateBounds(color, devOutside, devOutsideAssist, devInside, isDegenerate); | |
554 return true; | 636 return true; |
555 } | 637 } |
556 | 638 |
557 }; | 639 }; |
558 | 640 |
559 //////////////////////////////////////////////////////////////////////////////// /////////////////// | 641 //////////////////////////////////////////////////////////////////////////////// /////////////////// |
560 | 642 |
561 #ifdef GR_TEST_UTILS | 643 #ifdef GR_TEST_UTILS |
562 | 644 |
563 #include "GrBatchTest.h" | 645 #include "GrBatchTest.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
574 SkRect inside = outside; | 656 SkRect inside = outside; |
575 inside.inset(strokeWidth, strokeWidth); | 657 inside.inset(strokeWidth, strokeWidth); |
576 | 658 |
577 GrColor color = GrRandomColor(random); | 659 GrColor color = GrRandomColor(random); |
578 | 660 |
579 return GrAAStrokeRectBatch::Create(color, GrTest::TestMatrix(random), outsid e, outsideAssist, | 661 return GrAAStrokeRectBatch::Create(color, GrTest::TestMatrix(random), outsid e, outsideAssist, |
580 inside, miterStroke, inside.isFinite() && inside.isEmpty()); | 662 inside, miterStroke, inside.isFinite() && inside.isEmpty()); |
581 } | 663 } |
582 | 664 |
583 #endif | 665 #endif |
OLD | NEW |