blob: 77d7bef9c9e186ddbed3fa8e19f762388ef161ac [file] [log] [blame]
Chris Craikb49f4462014-03-20 12:44:20 -07001/*
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 Craikb60d3e72015-06-25 17:15:16 -070022#include "utils/MathUtils.h"
Chris Craikb49f4462014-03-20 12:44:20 -070023
24namespace android {
25namespace uirenderer {
26
27class Outline {
28public:
John Reck1bcacfd2017-11-03 10:12:19 -070029 enum class Type { None = 0, Empty = 1, ConvexPath = 2, RoundRect = 3 };
John Recke248bd12015-08-05 13:53:53 -070030
John Reck1bcacfd2017-11-03 10:12:19 -070031 Outline() : mShouldClip(false), mType(Type::None), mRadius(0), mAlpha(0.0f) {}
Chris Craikb49f4462014-03-20 12:44:20 -070032
Chris Craik77b5cad2014-07-30 18:23:07 -070033 void setRoundRect(int left, int top, int right, int bottom, float radius, float alpha) {
Chris Craik40de9f22015-07-29 12:51:41 -070034 mAlpha = alpha;
John Reck1bcacfd2017-11-03 10:12:19 -070035 if (mType == Type::RoundRect && left == mBounds.left && right == mBounds.right &&
36 top == mBounds.top && bottom == mBounds.bottom && radius == mRadius) {
Chris Craik40de9f22015-07-29 12:51:41 -070037 // nothing to change, don't do any work
38 return;
39 }
40
Chris Craikb9ce116d2015-08-20 15:14:06 -070041 mType = Type::RoundRect;
Chris Craikb49f4462014-03-20 12:44:20 -070042 mBounds.set(left, top, right, bottom);
43 mRadius = radius;
Chris Craik40de9f22015-07-29 12:51:41 -070044
45 // update mPath to reflect new outline
Chris Craikb49f4462014-03-20 12:44:20 -070046 mPath.reset();
Chris Craik40de9f22015-07-29 12:51:41 -070047 if (MathUtils::isPositive(radius)) {
John Reck1bcacfd2017-11-03 10:12:19 -070048 mPath.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom), radius, radius);
Chris Craik40de9f22015-07-29 12:51:41 -070049 } else {
50 mPath.addRect(left, top, right, bottom);
51 }
Chris Craikb49f4462014-03-20 12:44:20 -070052 }
53
Chris Craik77b5cad2014-07-30 18:23:07 -070054 void setConvexPath(const SkPath* outline, float alpha) {
Chris Craikb49f4462014-03-20 12:44:20 -070055 if (!outline) {
56 setEmpty();
57 return;
58 }
Chris Craikb9ce116d2015-08-20 15:14:06 -070059 mType = Type::ConvexPath;
Chris Craikb49f4462014-03-20 12:44:20 -070060 mPath = *outline;
61 mBounds.set(outline->getBounds());
Chris Craik77b5cad2014-07-30 18:23:07 -070062 mAlpha = alpha;
Chris Craikb49f4462014-03-20 12:44:20 -070063 }
64
Chris Craik06451282014-07-21 10:25:54 -070065 void setEmpty() {
Chris Craikb9ce116d2015-08-20 15:14:06 -070066 mType = Type::Empty;
Chris Craik06451282014-07-21 10:25:54 -070067 mPath.reset();
Chris Craik77b5cad2014-07-30 18:23:07 -070068 mAlpha = 0.0f;
Chris Craik61317322014-05-21 13:03:52 -070069 }
70
Chris Craik06451282014-07-21 10:25:54 -070071 void setNone() {
Chris Craikb9ce116d2015-08-20 15:14:06 -070072 mType = Type::None;
Chris Craikb49f4462014-03-20 12:44:20 -070073 mPath.reset();
Chris Craik77b5cad2014-07-30 18:23:07 -070074 mAlpha = 0.0f;
Chris Craikb49f4462014-03-20 12:44:20 -070075 }
76
John Reck1bcacfd2017-11-03 10:12:19 -070077 bool isEmpty() const { return mType == Type::Empty; }
Chris Craik06451282014-07-21 10:25:54 -070078
John Reck1bcacfd2017-11-03 10:12:19 -070079 float getAlpha() const { return mAlpha; }
Chris Craik77b5cad2014-07-30 18:23:07 -070080
John Reck1bcacfd2017-11-03 10:12:19 -070081 void setShouldClip(bool clip) { mShouldClip = clip; }
Chris Craikb49f4462014-03-20 12:44:20 -070082
John Reck1bcacfd2017-11-03 10:12:19 -070083 bool getShouldClip() const { return mShouldClip; }
Chris Craikdeeda3d2014-05-05 19:09:33 -070084
Chris Craikb49f4462014-03-20 12:44:20 -070085 bool willClip() const {
86 // only round rect outlines can be used for clipping
Chris Craikb9ce116d2015-08-20 15:14:06 -070087 return mShouldClip && (mType == Type::RoundRect);
Chris Craikb49f4462014-03-20 12:44:20 -070088 }
89
Chris Craikb60d3e72015-06-25 17:15:16 -070090 bool willRoundRectClip() const {
91 // only round rect outlines can be used for clipping
92 return willClip() && MathUtils::isPositive(mRadius);
93 }
94
Chris Craikdeeda3d2014-05-05 19:09:33 -070095 bool getAsRoundRect(Rect* outRect, float* outRadius) const {
Chris Craikb9ce116d2015-08-20 15:14:06 -070096 if (mType == Type::RoundRect) {
Chris Craikdeeda3d2014-05-05 19:09:33 -070097 outRect->set(mBounds);
98 *outRadius = mRadius;
99 return true;
100 }
101 return false;
102 }
103
John Reckd0a0b2a2014-03-20 16:28:56 -0700104 const SkPath* getPath() const {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700105 if (mType == Type::None || mType == Type::Empty) return nullptr;
Chris Craikb49f4462014-03-20 12:44:20 -0700106
107 return &mPath;
108 }
109
John Reck1bcacfd2017-11-03 10:12:19 -0700110 Type getType() const { return mType; }
Chris Craikb49f4462014-03-20 12:44:20 -0700111
John Reck1bcacfd2017-11-03 10:12:19 -0700112 const Rect& getBounds() const { return mBounds; }
John Recke248bd12015-08-05 13:53:53 -0700113
John Reck1bcacfd2017-11-03 10:12:19 -0700114 float getRadius() const { return mRadius; }
John Recke248bd12015-08-05 13:53:53 -0700115
116private:
Chris Craikb49f4462014-03-20 12:44:20 -0700117 bool mShouldClip;
Chris Craikb9ce116d2015-08-20 15:14:06 -0700118 Type mType;
Chris Craikb49f4462014-03-20 12:44:20 -0700119 Rect mBounds;
120 float mRadius;
Chris Craik77b5cad2014-07-30 18:23:07 -0700121 float mAlpha;
Chris Craikb49f4462014-03-20 12:44:20 -0700122 SkPath mPath;
123};
124
125} /* namespace uirenderer */
126} /* namespace android */
127
128#endif /* OUTLINE_H */