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