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) |
| 31 | , mRadius(0) {} |
| 32 | |
| 33 | void setRoundRect(int left, int top, int right, int bottom, int radius) { |
| 34 | 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 Craik | 6131732 | 2014-05-21 13:03:52 -0700 | [diff] [blame^] | 52 | bool isEmpty() const { |
| 53 | return mType == kOutlineType_None; |
| 54 | } |
| 55 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 56 | void setEmpty() { |
| 57 | mType = kOutlineType_None; |
| 58 | mPath.reset(); |
| 59 | } |
| 60 | |
| 61 | void setShouldClip(bool clip) { |
| 62 | mShouldClip = clip; |
| 63 | } |
| 64 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 65 | bool getShouldClip() const { |
| 66 | return mShouldClip; |
| 67 | } |
| 68 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 69 | bool willClip() const { |
| 70 | // only round rect outlines can be used for clipping |
| 71 | return mShouldClip && (mType == kOutlineType_RoundRect); |
| 72 | } |
| 73 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 74 | bool getAsRoundRect(Rect* outRect, float* outRadius) const { |
| 75 | if (mType == kOutlineType_RoundRect) { |
| 76 | outRect->set(mBounds); |
| 77 | *outRadius = mRadius; |
| 78 | return true; |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 83 | const SkPath* getPath() const { |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 84 | if (mType == kOutlineType_None) return NULL; |
| 85 | |
| 86 | return &mPath; |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | enum OutlineType { |
| 91 | kOutlineType_None = 0, |
| 92 | kOutlineType_ConvexPath = 1, |
| 93 | kOutlineType_RoundRect = 2 |
| 94 | }; |
| 95 | |
| 96 | bool mShouldClip; |
| 97 | OutlineType mType; |
| 98 | Rect mBounds; |
| 99 | float mRadius; |
| 100 | SkPath mPath; |
| 101 | }; |
| 102 | |
| 103 | } /* namespace uirenderer */ |
| 104 | } /* namespace android */ |
| 105 | |
| 106 | #endif /* OUTLINE_H */ |