blob: 4ddacc8a922d45ef618264bef53098a33948d952 [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
Stan Ilievf5638332017-12-08 11:32:12 -050045
46 // Reuse memory if previous outline was the same shape (rect or round rect).
47 if ( mPath.countVerbs() > 10) {
48 mPath.reset();
49 } else {
50 mPath.rewind();
51 }
52
Chris Craik40de9f22015-07-29 12:51:41 -070053 // update mPath to reflect new outline
Chris Craik40de9f22015-07-29 12:51:41 -070054 if (MathUtils::isPositive(radius)) {
John Reck1bcacfd2017-11-03 10:12:19 -070055 mPath.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom), radius, radius);
Chris Craik40de9f22015-07-29 12:51:41 -070056 } 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
John Reck1bcacfd2017-11-03 10:12:19 -070084 bool isEmpty() const { return mType == Type::Empty; }
Chris Craik06451282014-07-21 10:25:54 -070085
John Reck1bcacfd2017-11-03 10:12:19 -070086 float getAlpha() const { return mAlpha; }
Chris Craik77b5cad2014-07-30 18:23:07 -070087
John Reck1bcacfd2017-11-03 10:12:19 -070088 void setShouldClip(bool clip) { mShouldClip = clip; }
Chris Craikb49f4462014-03-20 12:44:20 -070089
John Reck1bcacfd2017-11-03 10:12:19 -070090 bool getShouldClip() const { return mShouldClip; }
Chris Craikdeeda3d2014-05-05 19:09:33 -070091
Chris Craikb49f4462014-03-20 12:44:20 -070092 bool willClip() const {
93 // only round rect outlines can be used for clipping
Chris Craikb9ce116d2015-08-20 15:14:06 -070094 return mShouldClip && (mType == Type::RoundRect);
Chris Craikb49f4462014-03-20 12:44:20 -070095 }
96
Chris Craikb60d3e72015-06-25 17:15:16 -070097 bool willRoundRectClip() const {
98 // only round rect outlines can be used for clipping
99 return willClip() && MathUtils::isPositive(mRadius);
100 }
101
Chris Craikdeeda3d2014-05-05 19:09:33 -0700102 bool getAsRoundRect(Rect* outRect, float* outRadius) const {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700103 if (mType == Type::RoundRect) {
Chris Craikdeeda3d2014-05-05 19:09:33 -0700104 outRect->set(mBounds);
105 *outRadius = mRadius;
106 return true;
107 }
108 return false;
109 }
110
John Reckd0a0b2a2014-03-20 16:28:56 -0700111 const SkPath* getPath() const {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700112 if (mType == Type::None || mType == Type::Empty) return nullptr;
Chris Craikb49f4462014-03-20 12:44:20 -0700113
114 return &mPath;
115 }
116
John Reck1bcacfd2017-11-03 10:12:19 -0700117 Type getType() const { return mType; }
Chris Craikb49f4462014-03-20 12:44:20 -0700118
John Reck1bcacfd2017-11-03 10:12:19 -0700119 const Rect& getBounds() const { return mBounds; }
John Recke248bd12015-08-05 13:53:53 -0700120
John Reck1bcacfd2017-11-03 10:12:19 -0700121 float getRadius() const { return mRadius; }
John Recke248bd12015-08-05 13:53:53 -0700122
123private:
Chris Craikb49f4462014-03-20 12:44:20 -0700124 bool mShouldClip;
Chris Craikb9ce116d2015-08-20 15:14:06 -0700125 Type mType;
Chris Craikb49f4462014-03-20 12:44:20 -0700126 Rect mBounds;
127 float mRadius;
Chris Craik77b5cad2014-07-30 18:23:07 -0700128 float mAlpha;
Chris Craikb49f4462014-03-20 12:44:20 -0700129 SkPath mPath;
130};
131
132} /* namespace uirenderer */
133} /* namespace android */
134
135#endif /* OUTLINE_H */