• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include "utils/MathUtils.h"
23 
24 namespace android {
25 namespace uirenderer {
26 
27 class Outline {
28 public:
29     enum class Type { None = 0, Empty = 1, Path = 2, RoundRect = 3 };
30 
Outline()31     Outline() : mShouldClip(false), mType(Type::None), mRadius(0), mAlpha(0.0f) {}
32 
setRoundRect(int left,int top,int right,int bottom,float radius,float alpha)33     void setRoundRect(int left, int top, int right, int bottom, float radius, float alpha) {
34         mAlpha = alpha;
35         if (mType == Type::RoundRect && left == mBounds.left && right == mBounds.right &&
36             top == mBounds.top && bottom == mBounds.bottom && radius == mRadius) {
37             // nothing to change, don't do any work
38             return;
39         }
40 
41         mType = Type::RoundRect;
42         mBounds.set(left, top, right, bottom);
43         mRadius = radius;
44 
45         // Reuse memory if previous outline was the same shape (rect or round rect).
46         if (mPath.countVerbs() > 10) {
47             mPath.reset();
48         } else {
49             mPath.rewind();
50         }
51 
52         // update mPath to reflect new outline
53         if (MathUtils::isPositive(radius)) {
54             mPath.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom), radius, radius);
55         } else {
56             mPath.addRect(left, top, right, bottom);
57         }
58     }
59 
setPath(const SkPath * outline,float alpha)60     void setPath(const SkPath* outline, float alpha) {
61         if (!outline) {
62             setEmpty();
63             return;
64         }
65         mType = Type::Path;
66         mPath = *outline;
67         mBounds.set(outline->getBounds());
68         mAlpha = alpha;
69     }
70 
setEmpty()71     void setEmpty() {
72         mType = Type::Empty;
73         mPath.reset();
74         mAlpha = 0.0f;
75     }
76 
setNone()77     void setNone() {
78         mType = Type::None;
79         mPath.reset();
80         mAlpha = 0.0f;
81     }
82 
isEmpty()83     bool isEmpty() const { return mType == Type::Empty; }
84 
getAlpha()85     float getAlpha() const { return mAlpha; }
86 
setShouldClip(bool clip)87     void setShouldClip(bool clip) { mShouldClip = clip; }
88 
getShouldClip()89     bool getShouldClip() const { return mShouldClip; }
90 
willClip()91     bool willClip() const { return mShouldClip; }
92 
willComplexClip()93     bool willComplexClip() const {
94         return mShouldClip && (mType != Type::RoundRect || MathUtils::isPositive(mRadius));
95     }
96 
getAsRoundRect(Rect * outRect,float * outRadius)97     bool getAsRoundRect(Rect* outRect, float* outRadius) const {
98         if (mType == Type::RoundRect) {
99             outRect->set(mBounds);
100             *outRadius = mRadius;
101             return true;
102         }
103         return false;
104     }
105 
getPath()106     const SkPath* getPath() const {
107         if (mType == Type::None || mType == Type::Empty) return nullptr;
108 
109         return &mPath;
110     }
111 
getType()112     Type getType() const { return mType; }
113 
getBounds()114     const Rect& getBounds() const { return mBounds; }
115 
getRadius()116     float getRadius() const { return mRadius; }
117 
118 private:
119     bool mShouldClip;
120     Type mType;
121     Rect mBounds;
122     float mRadius;
123     float mAlpha;
124     SkPath mPath;
125 };
126 
127 } /* namespace uirenderer */
128 } /* namespace android */
129 
130 #endif /* OUTLINE_H */
131