Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(565)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/GraphicsContext.cpp

Issue 2841153002: Relocate GC::DrawLineForDocumentMarker() (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/platform/graphics/GraphicsContext.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2009 Apple Inc. All rights reserved. 2 * Copyright (C) 2003, 2004, 2005, 2006, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 29 matching lines...) Expand all
40 #include "platform/instrumentation/tracing/TraceEvent.h" 40 #include "platform/instrumentation/tracing/TraceEvent.h"
41 #include "platform/weborigin/KURL.h" 41 #include "platform/weborigin/KURL.h"
42 #include "platform/wtf/Assertions.h" 42 #include "platform/wtf/Assertions.h"
43 #include "platform/wtf/MathExtras.h" 43 #include "platform/wtf/MathExtras.h"
44 #include "skia/ext/platform_canvas.h" 44 #include "skia/ext/platform_canvas.h"
45 #include "third_party/skia/include/core/SkAnnotation.h" 45 #include "third_party/skia/include/core/SkAnnotation.h"
46 #include "third_party/skia/include/core/SkColorFilter.h" 46 #include "third_party/skia/include/core/SkColorFilter.h"
47 #include "third_party/skia/include/core/SkData.h" 47 #include "third_party/skia/include/core/SkData.h"
48 #include "third_party/skia/include/core/SkRRect.h" 48 #include "third_party/skia/include/core/SkRRect.h"
49 #include "third_party/skia/include/core/SkRefCnt.h" 49 #include "third_party/skia/include/core/SkRefCnt.h"
50 #include "third_party/skia/include/effects/SkGradientShader.h"
51 #include "third_party/skia/include/effects/SkLumaColorFilter.h" 50 #include "third_party/skia/include/effects/SkLumaColorFilter.h"
52 #include "third_party/skia/include/effects/SkPictureImageFilter.h" 51 #include "third_party/skia/include/effects/SkPictureImageFilter.h"
53 #include "third_party/skia/include/pathops/SkPathOps.h" 52 #include "third_party/skia/include/pathops/SkPathOps.h"
54 #include "third_party/skia/include/utils/SkNullCanvas.h" 53 #include "third_party/skia/include/utils/SkNullCanvas.h"
55 54
56 namespace blink { 55 namespace blink {
57 56
58 GraphicsContext::GraphicsContext(PaintController& paint_controller, 57 GraphicsContext::GraphicsContext(PaintController& paint_controller,
59 DisabledMode disable_context_or_painting, 58 DisabledMode disable_context_or_painting,
60 SkMetaData* meta_data) 59 SkMetaData* meta_data)
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 } else { 533 } else {
535 p1.SetX(p1.X() + width / 2.f); 534 p1.SetX(p1.X() + width / 2.f);
536 p2.SetX(p2.X() - width / 2.f); 535 p2.SetX(p2.X() - width / 2.f);
537 } 536 }
538 } 537 }
539 538
540 AdjustLineToPixelBoundaries(p1, p2, width, pen_style); 539 AdjustLineToPixelBoundaries(p1, p2, width, pen_style);
541 canvas_->drawLine(p1.X(), p1.Y(), p2.X(), p2.Y(), flags); 540 canvas_->drawLine(p1.X(), p1.Y(), p2.X(), p2.Y(), flags);
542 } 541 }
543 542
544 namespace {
545
546 #if !OS(MACOSX)
547
548 sk_sp<PaintRecord> RecordMarker(
549 GraphicsContext::DocumentMarkerLineStyle style) {
550 SkColor color = (style == GraphicsContext::kDocumentMarkerGrammarLineStyle)
551 ? SkColorSetRGB(0xC0, 0xC0, 0xC0)
552 : SK_ColorRED;
553
554 // Record the path equivalent to this legacy pattern:
555 // X o o X o o X
556 // o X o o X o
557
558 static const float kW = 4;
559 static const float kH = 2;
560
561 // Adjust the phase such that f' == 0 is "pixel"-centered
562 // (for optimal rasterization at native rez).
563 SkPath path;
564 path.moveTo(kW * -3 / 8, kH * 3 / 4);
565 path.cubicTo(kW * -1 / 8, kH * 3 / 4,
566 kW * -1 / 8, kH * 1 / 4,
567 kW * 1 / 8, kH * 1 / 4);
568 path.cubicTo(kW * 3 / 8, kH * 1 / 4,
569 kW * 3 / 8, kH * 3 / 4,
570 kW * 5 / 8, kH * 3 / 4);
571 path.cubicTo(kW * 7 / 8, kH * 3 / 4,
572 kW * 7 / 8, kH * 1 / 4,
573 kW * 9 / 8, kH * 1 / 4);
574
575 PaintFlags flags;
576 flags.setAntiAlias(true);
577 flags.setColor(color);
578 flags.setStyle(PaintFlags::kStroke_Style);
579 flags.setStrokeWidth(kH * 1 / 2);
580
581 PaintRecorder recorder;
582 recorder.beginRecording(kW, kH);
583 recorder.getRecordingCanvas()->drawPath(path, flags);
584
585 return recorder.finishRecordingAsPicture();
586 }
587
588 #else // OS(MACOSX)
589
590 sk_sp<PaintRecord> RecordMarker(
591 GraphicsContext::DocumentMarkerLineStyle style) {
592 SkColor color = (style == GraphicsContext::kDocumentMarkerGrammarLineStyle)
593 ? SkColorSetRGB(0x6B, 0x6B, 0x6B)
594 : SkColorSetRGB(0xFB, 0x2D, 0x1D);
595
596 // Match the artwork used by the Mac.
597 static const float kW = 4;
598 static const float kH = 3;
599 static const float kR = 1.5f;
600
601 // top->bottom translucent gradient.
602 const SkColor colors[2] = {
603 SkColorSetARGB(0x48,
604 SkColorGetR(color),
605 SkColorGetG(color),
606 SkColorGetB(color)),
607 color
608 };
609 const SkPoint pts[2] = {
610 SkPoint::Make(0, 0),
611 SkPoint::Make(0, 2 * kR)
612 };
613
614 PaintFlags flags;
615 flags.setAntiAlias(true);
616 flags.setColor(color);
617 flags.setShader(SkGradientShader::MakeLinear(
618 pts, colors, nullptr, ARRAY_SIZE(colors), SkShader::kClamp_TileMode));
619 PaintRecorder recorder;
620 recorder.beginRecording(kW, kH);
621 recorder.getRecordingCanvas()->drawCircle(kR, kR, kR, flags);
622
623 return recorder.finishRecordingAsPicture();
624 }
625
626 #endif // OS(MACOSX)
627
628 } // anonymous ns
629
630 void GraphicsContext::DrawLineForDocumentMarker(const FloatPoint& pt,
631 float width,
632 DocumentMarkerLineStyle style,
633 float zoom) {
634 if (ContextDisabled())
635 return;
636
637 DEFINE_STATIC_LOCAL(
638 PaintRecord*, spelling_marker,
639 (RecordMarker(kDocumentMarkerSpellingLineStyle).release()));
640 DEFINE_STATIC_LOCAL(
641 PaintRecord*, grammar_marker,
642 (RecordMarker(kDocumentMarkerGrammarLineStyle).release()));
643 const auto& marker = style == kDocumentMarkerSpellingLineStyle
644 ? spelling_marker
645 : grammar_marker;
646
647 // Position already includes zoom and device scale factor.
648 SkScalar origin_x = WebCoreFloatToSkScalar(pt.X());
649 SkScalar origin_y = WebCoreFloatToSkScalar(pt.Y());
650
651 #if OS(MACOSX)
652 // Make sure to draw only complete dots, and finish inside the marked text.
653 width -= fmodf(width, marker->cullRect().width() * zoom);
654 #else
655 // Offset it vertically by 1 so that there's some space under the text.
656 origin_y += 1;
657 #endif
658
659 const auto rect = SkRect::MakeWH(width, marker->cullRect().height() * zoom);
660 const auto local_matrix = SkMatrix::MakeScale(zoom, zoom);
661
662 PaintFlags flags;
663 flags.setAntiAlias(true);
664 flags.setShader(WrapSkShader(MakePaintShaderRecord(
665 sk_ref_sp(marker), SkShader::kRepeat_TileMode, SkShader::kClamp_TileMode,
666 &local_matrix, nullptr)));
667
668 // Apply the origin translation as a global transform. This ensures that the
669 // shader local matrix depends solely on zoom => Skia can reuse the same
670 // cached tile for all markers at a given zoom level.
671 PaintCanvasAutoRestore acr(canvas_, true);
672 canvas_->translate(origin_x, origin_y);
673 canvas_->drawRect(rect, flags);
674 }
675
676 void GraphicsContext::DrawLineForText(const FloatPoint& pt, float width) { 543 void GraphicsContext::DrawLineForText(const FloatPoint& pt, float width) {
677 if (ContextDisabled()) 544 if (ContextDisabled())
678 return; 545 return;
679 546
680 if (width <= 0) 547 if (width <= 0)
681 return; 548 return;
682 549
683 PaintFlags flags; 550 PaintFlags flags;
684 switch (GetStrokeStyle()) { 551 switch (GetStrokeStyle()) {
685 case kNoStroke: 552 case kNoStroke:
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after
1361 break; 1228 break;
1362 default: 1229 default:
1363 NOTREACHED(); 1230 NOTREACHED();
1364 break; 1231 break;
1365 } 1232 }
1366 1233
1367 return nullptr; 1234 return nullptr;
1368 } 1235 }
1369 1236
1370 } // namespace blink 1237 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/graphics/GraphicsContext.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698