blob: 83426e8e8971b0c370c8f57aed1f84ad072f58fd [file] [log] [blame]
Chris Craikb49f4462014-03-20 12:44:20 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef OUTLINE_H
17#define OUTLINE_H
18
19#include <SkPath.h>
20
21#include "Rect.h"
22
23namespace android {
24namespace uirenderer {
25
26class Outline {
27public:
28 Outline()
29 : mShouldClip(false)
30 , mType(kOutlineType_None)
31 , mRadius(0) {}
32
Chris Craik0f632042014-07-07 19:52:58 -070033 void setRoundRect(int left, int top, int right, int bottom, float radius) {
Chris Craikb49f4462014-03-20 12:44:20 -070034 mType = kOutlineType_RoundRect;
35 mBounds.set(left, top, right, bottom);
36 mRadius = radius;
37 mPath.reset();
38 mPath.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom),
39 radius, radius);
40 }
41
42 void setConvexPath(const SkPath* outline) {
43 if (!outline) {
44 setEmpty();
45 return;
46 }
47 mType = kOutlineType_ConvexPath;
48 mPath = *outline;
49 mBounds.set(outline->getBounds());
50 }
51
Chris Craik06451282014-07-21 10:25:54 -070052 void setEmpty() {
53 mType = kOutlineType_Empty;
54 mPath.reset();
Chris Craik61317322014-05-21 13:03:52 -070055 }
56
Chris Craik06451282014-07-21 10:25:54 -070057 void setNone() {
Chris Craikb49f4462014-03-20 12:44:20 -070058 mType = kOutlineType_None;
59 mPath.reset();
60 }
61
Chris Craik06451282014-07-21 10:25:54 -070062 bool isEmpty() const {
63 return mType == kOutlineType_Empty;
64 }
65
Chris Craikb49f4462014-03-20 12:44:20 -070066 void setShouldClip(bool clip) {
67 mShouldClip = clip;
68 }
69
Chris Craikdeeda3d2014-05-05 19:09:33 -070070 bool getShouldClip() const {
71 return mShouldClip;
72 }
73
Chris Craikb49f4462014-03-20 12:44:20 -070074 bool willClip() const {
75 // only round rect outlines can be used for clipping
76 return mShouldClip && (mType == kOutlineType_RoundRect);
77 }
78
Chris Craikdeeda3d2014-05-05 19:09:33 -070079 bool getAsRoundRect(Rect* outRect, float* outRadius) const {
80 if (mType == kOutlineType_RoundRect) {
81 outRect->set(mBounds);
82 *outRadius = mRadius;
83 return true;
84 }
85 return false;
86 }
87
John Reckd0a0b2a2014-03-20 16:28:56 -070088 const SkPath* getPath() const {
Chris Craik06451282014-07-21 10:25:54 -070089 if (mType == kOutlineType_None || mType == kOutlineType_Empty) return NULL;
Chris Craikb49f4462014-03-20 12:44:20 -070090
91 return &mPath;
92 }
93
94private:
95 enum OutlineType {
96 kOutlineType_None = 0,
Chris Craik06451282014-07-21 10:25:54 -070097 kOutlineType_Empty = 1,
98 kOutlineType_ConvexPath = 2,
99 kOutlineType_RoundRect = 3
Chris Craikb49f4462014-03-20 12:44:20 -0700100 };
101
102 bool mShouldClip;
103 OutlineType mType;
104 Rect mBounds;
105 float mRadius;
106 SkPath mPath;
107};
108
109} /* namespace uirenderer */
110} /* namespace android */
111
112#endif /* OUTLINE_H */