1 /*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "modules/sksg/include/SkSGRect.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkClipOp.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkPoint.h"
14 #include "include/private/base/SkAssert.h"
15
16 class SkMatrix;
17
18 namespace sksg {
19
Rect(const SkRect & rect)20 Rect::Rect(const SkRect& rect) : fRect(rect) {}
21
onClip(SkCanvas * canvas,bool antiAlias) const22 void Rect::onClip(SkCanvas* canvas, bool antiAlias) const {
23 canvas->clipRect(fRect, SkClipOp::kIntersect, antiAlias);
24 }
25
onDraw(SkCanvas * canvas,const SkPaint & paint) const26 void Rect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
27 canvas->drawRect(fRect, paint);
28 }
29
onContains(const SkPoint & p) const30 bool Rect::onContains(const SkPoint& p) const {
31 return fRect.contains(p.x(), p.y());
32 }
33
onRevalidate(InvalidationController *,const SkMatrix &)34 SkRect Rect::onRevalidate(InvalidationController*, const SkMatrix&) {
35 SkASSERT(this->hasInval());
36
37 return fRect;
38 }
39
onAsPath() const40 SkPath Rect::onAsPath() const {
41 return SkPath::Rect(fRect, this->getDirection(), this->getInitialPointIndex());
42 }
43
RRect(const SkRRect & rr)44 RRect::RRect(const SkRRect& rr) : fRRect(rr) {}
45
onClip(SkCanvas * canvas,bool antiAlias) const46 void RRect::onClip(SkCanvas* canvas, bool antiAlias) const {
47 canvas->clipRRect(fRRect, SkClipOp::kIntersect, antiAlias);
48 }
49
onDraw(SkCanvas * canvas,const SkPaint & paint) const50 void RRect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
51 canvas->drawRRect(fRRect, paint);
52 }
53
onContains(const SkPoint & p) const54 bool RRect::onContains(const SkPoint& p) const {
55 if (!fRRect.rect().contains(p.x(), p.y())) {
56 return false;
57 }
58
59 if (fRRect.isRect()) {
60 return true;
61 }
62
63 // TODO: no SkRRect::contains(x, y)
64 return fRRect.contains(SkRect::MakeLTRB(p.x() - SK_ScalarNearlyZero,
65 p.y() - SK_ScalarNearlyZero,
66 p.x() + SK_ScalarNearlyZero,
67 p.y() + SK_ScalarNearlyZero));
68 }
69
onRevalidate(InvalidationController *,const SkMatrix &)70 SkRect RRect::onRevalidate(InvalidationController*, const SkMatrix&) {
71 SkASSERT(this->hasInval());
72
73 return fRRect.getBounds();
74 }
75
onAsPath() const76 SkPath RRect::onAsPath() const {
77 return SkPath::RRect(fRRect, this->getDirection(), this->getInitialPointIndex());
78 }
79
80 } // namespace sksg
81