blob: c98932cf095ebfd73a51e216ffe8f837713ca2a9 [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)
31 , mType(kOutlineType_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 Craikb49f4462014-03-20 12:44:20 -070036 mType = kOutlineType_RoundRect;
37 mBounds.set(left, top, right, bottom);
38 mRadius = radius;
39 mPath.reset();
40 mPath.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom),
41 radius, radius);
Chris Craik77b5cad2014-07-30 18:23:07 -070042 mAlpha = alpha;
Chris Craikb49f4462014-03-20 12:44:20 -070043 }
44
Chris Craik77b5cad2014-07-30 18:23:07 -070045 void setConvexPath(const SkPath* outline, float alpha) {
Chris Craikb49f4462014-03-20 12:44:20 -070046 if (!outline) {
47 setEmpty();
48 return;
49 }
50 mType = kOutlineType_ConvexPath;
51 mPath = *outline;
52 mBounds.set(outline->getBounds());
Chris Craik77b5cad2014-07-30 18:23:07 -070053 mAlpha = alpha;
Chris Craikb49f4462014-03-20 12:44:20 -070054 }
55
Chris Craik06451282014-07-21 10:25:54 -070056 void setEmpty() {
57 mType = kOutlineType_Empty;
58 mPath.reset();
Chris Craik77b5cad2014-07-30 18:23:07 -070059 mAlpha = 0.0f;
Chris Craik61317322014-05-21 13:03:52 -070060 }
61
Chris Craik06451282014-07-21 10:25:54 -070062 void setNone() {
Chris Craikb49f4462014-03-20 12:44:20 -070063 mType = kOutlineType_None;
64 mPath.reset();
Chris Craik77b5cad2014-07-30 18:23:07 -070065 mAlpha = 0.0f;
Chris Craikb49f4462014-03-20 12:44:20 -070066 }
67
Chris Craik06451282014-07-21 10:25:54 -070068 bool isEmpty() const {
69 return mType == kOutlineType_Empty;
70 }
71
Chris Craik77b5cad2014-07-30 18:23:07 -070072 float getAlpha() const {
73 return mAlpha;
74 }
75
Chris Craikb49f4462014-03-20 12:44:20 -070076 void setShouldClip(bool clip) {
77 mShouldClip = clip;
78 }
79
Chris Craikdeeda3d2014-05-05 19:09:33 -070080 bool getShouldClip() const {
81 return mShouldClip;
82 }
83
Chris Craikb49f4462014-03-20 12:44:20 -070084 bool willClip() const {
85 // only round rect outlines can be used for clipping
86 return mShouldClip && (mType == kOutlineType_RoundRect);
87 }
88
Chris Craikb60d3e72015-06-25 17:15:16 -070089 bool willRoundRectClip() const {
90 // only round rect outlines can be used for clipping
91 return willClip() && MathUtils::isPositive(mRadius);
92 }
93
Chris Craikdeeda3d2014-05-05 19:09:33 -070094 bool getAsRoundRect(Rect* outRect, float* outRadius) const {
95 if (mType == kOutlineType_RoundRect) {
96 outRect->set(mBounds);
97 *outRadius = mRadius;
98 return true;
99 }
100 return false;
101 }
102
John Reckd0a0b2a2014-03-20 16:28:56 -0700103 const SkPath* getPath() const {
Chris Craike84a2082014-12-22 14:28:49 -0800104 if (mType == kOutlineType_None || mType == kOutlineType_Empty) return nullptr;
Chris Craikb49f4462014-03-20 12:44:20 -0700105
106 return &mPath;
107 }
108
109private:
110 enum OutlineType {
111 kOutlineType_None = 0,
Chris Craik06451282014-07-21 10:25:54 -0700112 kOutlineType_Empty = 1,
113 kOutlineType_ConvexPath = 2,
114 kOutlineType_RoundRect = 3
Chris Craikb49f4462014-03-20 12:44:20 -0700115 };
116
117 bool mShouldClip;
118 OutlineType mType;
119 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 */