Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 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" |
Chris Craik | b60d3e7 | 2015-06-25 17:15:16 -0700 | [diff] [blame] | 22 | #include "utils/MathUtils.h" |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
| 27 | class Outline { |
| 28 | public: |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 29 | enum class Type { |
| 30 | None = 0, |
| 31 | Empty = 1, |
| 32 | ConvexPath = 2, |
| 33 | RoundRect = 3 |
| 34 | }; |
| 35 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 36 | Outline() |
| 37 | : mShouldClip(false) |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 38 | , mType(Type::None) |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 39 | , mRadius(0) |
| 40 | , mAlpha(0.0f) {} |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 41 | |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 42 | void setRoundRect(int left, int top, int right, int bottom, float radius, float alpha) { |
Chris Craik | 40de9f2 | 2015-07-29 12:51:41 -0700 | [diff] [blame] | 43 | mAlpha = alpha; |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 44 | if (mType == Type::RoundRect |
Chris Craik | 40de9f2 | 2015-07-29 12:51:41 -0700 | [diff] [blame] | 45 | && left == mBounds.left |
| 46 | && right == mBounds.right |
| 47 | && top == mBounds.top |
| 48 | && bottom == mBounds.bottom |
| 49 | && radius == mRadius) { |
| 50 | // nothing to change, don't do any work |
| 51 | return; |
| 52 | } |
| 53 | |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 54 | mType = Type::RoundRect; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 55 | mBounds.set(left, top, right, bottom); |
| 56 | mRadius = radius; |
Chris Craik | 40de9f2 | 2015-07-29 12:51:41 -0700 | [diff] [blame] | 57 | |
| 58 | // update mPath to reflect new outline |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 59 | mPath.reset(); |
Chris Craik | 40de9f2 | 2015-07-29 12:51:41 -0700 | [diff] [blame] | 60 | if (MathUtils::isPositive(radius)) { |
| 61 | mPath.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom), |
| 62 | radius, radius); |
| 63 | } else { |
| 64 | mPath.addRect(left, top, right, bottom); |
| 65 | } |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 68 | void setConvexPath(const SkPath* outline, float alpha) { |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 69 | if (!outline) { |
| 70 | setEmpty(); |
| 71 | return; |
| 72 | } |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 73 | mType = Type::ConvexPath; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 74 | mPath = *outline; |
| 75 | mBounds.set(outline->getBounds()); |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 76 | mAlpha = alpha; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 79 | void setEmpty() { |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 80 | mType = Type::Empty; |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 81 | mPath.reset(); |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 82 | mAlpha = 0.0f; |
Chris Craik | 6131732 | 2014-05-21 13:03:52 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 85 | void setNone() { |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 86 | mType = Type::None; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 87 | mPath.reset(); |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 88 | mAlpha = 0.0f; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 91 | bool isEmpty() const { |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 92 | return mType == Type::Empty; |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 95 | float getAlpha() const { |
| 96 | return mAlpha; |
| 97 | } |
| 98 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 99 | void setShouldClip(bool clip) { |
| 100 | mShouldClip = clip; |
| 101 | } |
| 102 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 103 | bool getShouldClip() const { |
| 104 | return mShouldClip; |
| 105 | } |
| 106 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 107 | bool willClip() const { |
| 108 | // only round rect outlines can be used for clipping |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 109 | return mShouldClip && (mType == Type::RoundRect); |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Chris Craik | b60d3e7 | 2015-06-25 17:15:16 -0700 | [diff] [blame] | 112 | bool willRoundRectClip() const { |
| 113 | // only round rect outlines can be used for clipping |
| 114 | return willClip() && MathUtils::isPositive(mRadius); |
| 115 | } |
| 116 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 117 | bool getAsRoundRect(Rect* outRect, float* outRadius) const { |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 118 | if (mType == Type::RoundRect) { |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 119 | outRect->set(mBounds); |
| 120 | *outRadius = mRadius; |
| 121 | return true; |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 126 | const SkPath* getPath() const { |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 127 | if (mType == Type::None || mType == Type::Empty) return nullptr; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 128 | |
| 129 | return &mPath; |
| 130 | } |
| 131 | |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 132 | Type getType() const { |
| 133 | return mType; |
| 134 | } |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 135 | |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 136 | const Rect& getBounds() const { |
| 137 | return mBounds; |
| 138 | } |
| 139 | |
| 140 | float getRadius() const { |
| 141 | return mRadius; |
| 142 | } |
| 143 | |
| 144 | private: |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 145 | bool mShouldClip; |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 146 | Type mType; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 147 | Rect mBounds; |
| 148 | float mRadius; |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 149 | float mAlpha; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 150 | SkPath mPath; |
| 151 | }; |
| 152 | |
| 153 | } /* namespace uirenderer */ |
| 154 | } /* namespace android */ |
| 155 | |
| 156 | #endif /* OUTLINE_H */ |