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: |
| 29 | Outline() |
| 30 | : mShouldClip(false) |
| 31 | , mType(kOutlineType_None) |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 32 | , mRadius(0) |
| 33 | , mAlpha(0.0f) {} |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 34 | |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 35 | void setRoundRect(int left, int top, int right, int bottom, float radius, float alpha) { |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 36 | mType = kOutlineType_RoundRect; |
| 37 | mBounds.set(left, top, right, bottom); |
| 38 | mRadius = radius; |
| 39 | mPath.reset(); |
| 40 | mPath.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom), |
| 41 | radius, radius); |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 42 | mAlpha = alpha; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 45 | void setConvexPath(const SkPath* outline, float alpha) { |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 46 | if (!outline) { |
| 47 | setEmpty(); |
| 48 | return; |
| 49 | } |
| 50 | mType = kOutlineType_ConvexPath; |
| 51 | mPath = *outline; |
| 52 | mBounds.set(outline->getBounds()); |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 53 | mAlpha = alpha; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 56 | void setEmpty() { |
| 57 | mType = kOutlineType_Empty; |
| 58 | mPath.reset(); |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 59 | mAlpha = 0.0f; |
Chris Craik | 6131732 | 2014-05-21 13:03:52 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 62 | void setNone() { |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 63 | mType = kOutlineType_None; |
| 64 | mPath.reset(); |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 65 | mAlpha = 0.0f; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 68 | bool isEmpty() const { |
| 69 | return mType == kOutlineType_Empty; |
| 70 | } |
| 71 | |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 72 | float getAlpha() const { |
| 73 | return mAlpha; |
| 74 | } |
| 75 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 76 | void setShouldClip(bool clip) { |
| 77 | mShouldClip = clip; |
| 78 | } |
| 79 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 80 | bool getShouldClip() const { |
| 81 | return mShouldClip; |
| 82 | } |
| 83 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 84 | bool willClip() const { |
| 85 | // only round rect outlines can be used for clipping |
| 86 | return mShouldClip && (mType == kOutlineType_RoundRect); |
| 87 | } |
| 88 | |
Chris Craik | b60d3e7 | 2015-06-25 17:15:16 -0700 | [diff] [blame^] | 89 | bool willRoundRectClip() const { |
| 90 | // only round rect outlines can be used for clipping |
| 91 | return willClip() && MathUtils::isPositive(mRadius); |
| 92 | } |
| 93 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 94 | bool getAsRoundRect(Rect* outRect, float* outRadius) const { |
| 95 | if (mType == kOutlineType_RoundRect) { |
| 96 | outRect->set(mBounds); |
| 97 | *outRadius = mRadius; |
| 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 103 | const SkPath* getPath() const { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 104 | if (mType == kOutlineType_None || mType == kOutlineType_Empty) return nullptr; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 105 | |
| 106 | return &mPath; |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | enum OutlineType { |
| 111 | kOutlineType_None = 0, |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 112 | kOutlineType_Empty = 1, |
| 113 | kOutlineType_ConvexPath = 2, |
| 114 | kOutlineType_RoundRect = 3 |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | bool mShouldClip; |
| 118 | OutlineType mType; |
| 119 | Rect mBounds; |
| 120 | float mRadius; |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 121 | float mAlpha; |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 122 | SkPath mPath; |
| 123 | }; |
| 124 | |
| 125 | } /* namespace uirenderer */ |
| 126 | } /* namespace android */ |
| 127 | |
| 128 | #endif /* OUTLINE_H */ |