blob: 4bd4ae1d48c9c3e21c2dfecf8b6b1a7f23e9c1e4 [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:
29 Outline()
30 : mShouldClip(false)
Chris Craikb9ce116d2015-08-20 15:14:06 -070031 , mType(Type::None)
Chris Craik77b5cad2014-07-30 18:23:07 -070032 , mRadius(0)
33 , mAlpha(0.0f) {}
Chris Craikb49f4462014-03-20 12:44:20 -070034
Chris Craik77b5cad2014-07-30 18:23:07 -070035 void setRoundRect(int left, int top, int right, int bottom, float radius, float alpha) {
Chris Craik40de9f22015-07-29 12:51:41 -070036 mAlpha = alpha;
Chris Craikb9ce116d2015-08-20 15:14:06 -070037 if (mType == Type::RoundRect
Chris Craik40de9f22015-07-29 12:51:41 -070038 && left == mBounds.left
39 && right == mBounds.right
40 && top == mBounds.top
41 && bottom == mBounds.bottom
42 && radius == mRadius) {
43 // nothing to change, don't do any work
44 return;
45 }
46
Chris Craikb9ce116d2015-08-20 15:14:06 -070047 mType = Type::RoundRect;
Chris Craikb49f4462014-03-20 12:44:20 -070048 mBounds.set(left, top, right, bottom);
49 mRadius = radius;
Chris Craik40de9f22015-07-29 12:51:41 -070050
51 // update mPath to reflect new outline
Chris Craikb49f4462014-03-20 12:44:20 -070052 mPath.reset();
Chris Craik40de9f22015-07-29 12:51:41 -070053 if (MathUtils::isPositive(radius)) {
54 mPath.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom),
55 radius, radius);
56 } else {
57 mPath.addRect(left, top, right, bottom);
58 }
Chris Craikb49f4462014-03-20 12:44:20 -070059 }
60
Chris Craik77b5cad2014-07-30 18:23:07 -070061 void setConvexPath(const SkPath* outline, float alpha) {
Chris Craikb49f4462014-03-20 12:44:20 -070062 if (!outline) {
63 setEmpty();
64 return;
65 }
Chris Craikb9ce116d2015-08-20 15:14:06 -070066 mType = Type::ConvexPath;
Chris Craikb49f4462014-03-20 12:44:20 -070067 mPath = *outline;
68 mBounds.set(outline->getBounds());
Chris Craik77b5cad2014-07-30 18:23:07 -070069 mAlpha = alpha;
Chris Craikb49f4462014-03-20 12:44:20 -070070 }
71
Chris Craik06451282014-07-21 10:25:54 -070072 void setEmpty() {
Chris Craikb9ce116d2015-08-20 15:14:06 -070073 mType = Type::Empty;
Chris Craik06451282014-07-21 10:25:54 -070074 mPath.reset();
Chris Craik77b5cad2014-07-30 18:23:07 -070075 mAlpha = 0.0f;
Chris Craik61317322014-05-21 13:03:52 -070076 }
77
Chris Craik06451282014-07-21 10:25:54 -070078 void setNone() {
Chris Craikb9ce116d2015-08-20 15:14:06 -070079 mType = Type::None;
Chris Craikb49f4462014-03-20 12:44:20 -070080 mPath.reset();
Chris Craik77b5cad2014-07-30 18:23:07 -070081 mAlpha = 0.0f;
Chris Craikb49f4462014-03-20 12:44:20 -070082 }
83
Chris Craik06451282014-07-21 10:25:54 -070084 bool isEmpty() const {
Chris Craikb9ce116d2015-08-20 15:14:06 -070085 return mType == Type::Empty;
Chris Craik06451282014-07-21 10:25:54 -070086 }
87
Chris Craik77b5cad2014-07-30 18:23:07 -070088 float getAlpha() const {
89 return mAlpha;
90 }
91
Chris Craikb49f4462014-03-20 12:44:20 -070092 void setShouldClip(bool clip) {
93 mShouldClip = clip;
94 }
95
Chris Craikdeeda3d2014-05-05 19:09:33 -070096 bool getShouldClip() const {
97 return mShouldClip;
98 }
99
Chris Craikb49f4462014-03-20 12:44:20 -0700100 bool willClip() const {
101 // only round rect outlines can be used for clipping
Chris Craikb9ce116d2015-08-20 15:14:06 -0700102 return mShouldClip && (mType == Type::RoundRect);
Chris Craikb49f4462014-03-20 12:44:20 -0700103 }
104
Chris Craikb60d3e72015-06-25 17:15:16 -0700105 bool willRoundRectClip() const {
106 // only round rect outlines can be used for clipping
107 return willClip() && MathUtils::isPositive(mRadius);
108 }
109
Chris Craikdeeda3d2014-05-05 19:09:33 -0700110 bool getAsRoundRect(Rect* outRect, float* outRadius) const {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700111 if (mType == Type::RoundRect) {
Chris Craikdeeda3d2014-05-05 19:09:33 -0700112 outRect->set(mBounds);
113 *outRadius = mRadius;
114 return true;
115 }
116 return false;
117 }
118
John Reckd0a0b2a2014-03-20 16:28:56 -0700119 const SkPath* getPath() const {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700120 if (mType == Type::None || mType == Type::Empty) return nullptr;
Chris Craikb49f4462014-03-20 12:44:20 -0700121
122 return &mPath;
123 }
124
125private:
Chris Craikb9ce116d2015-08-20 15:14:06 -0700126 enum class Type {
127 None = 0,
128 Empty = 1,
129 ConvexPath = 2,
130 RoundRect = 3
Chris Craikb49f4462014-03-20 12:44:20 -0700131 };
132
133 bool mShouldClip;
Chris Craikb9ce116d2015-08-20 15:14:06 -0700134 Type mType;
Chris Craikb49f4462014-03-20 12:44:20 -0700135 Rect mBounds;
136 float mRadius;
Chris Craik77b5cad2014-07-30 18:23:07 -0700137 float mAlpha;
Chris Craikb49f4462014-03-20 12:44:20 -0700138 SkPath mPath;
139};
140
141} /* namespace uirenderer */
142} /* namespace android */
143
144#endif /* OUTLINE_H */