blob: 6dacd5edd3878a79ddf52eeb8edcca82e0e15863 [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"
22
23namespace android {
24namespace uirenderer {
25
26class Outline {
27public:
28 Outline()
29 : mShouldClip(false)
30 , mType(kOutlineType_None)
Chris Craik77b5cad2014-07-30 18:23:07 -070031 , mRadius(0)
32 , mAlpha(0.0f) {}
Chris Craikb49f4462014-03-20 12:44:20 -070033
Chris Craik77b5cad2014-07-30 18:23:07 -070034 void setRoundRect(int left, int top, int right, int bottom, float radius, float alpha) {
Chris Craikb49f4462014-03-20 12:44:20 -070035 mType = kOutlineType_RoundRect;
36 mBounds.set(left, top, right, bottom);
37 mRadius = radius;
38 mPath.reset();
39 mPath.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom),
40 radius, radius);
Chris Craik77b5cad2014-07-30 18:23:07 -070041 mAlpha = alpha;
Chris Craikb49f4462014-03-20 12:44:20 -070042 }
43
Chris Craik77b5cad2014-07-30 18:23:07 -070044 void setConvexPath(const SkPath* outline, float alpha) {
Chris Craikb49f4462014-03-20 12:44:20 -070045 if (!outline) {
46 setEmpty();
47 return;
48 }
49 mType = kOutlineType_ConvexPath;
50 mPath = *outline;
51 mBounds.set(outline->getBounds());
Chris Craik77b5cad2014-07-30 18:23:07 -070052 mAlpha = alpha;
Chris Craikb49f4462014-03-20 12:44:20 -070053 }
54
Chris Craik06451282014-07-21 10:25:54 -070055 void setEmpty() {
56 mType = kOutlineType_Empty;
57 mPath.reset();
Chris Craik77b5cad2014-07-30 18:23:07 -070058 mAlpha = 0.0f;
Chris Craik61317322014-05-21 13:03:52 -070059 }
60
Chris Craik06451282014-07-21 10:25:54 -070061 void setNone() {
Chris Craikb49f4462014-03-20 12:44:20 -070062 mType = kOutlineType_None;
63 mPath.reset();
Chris Craik77b5cad2014-07-30 18:23:07 -070064 mAlpha = 0.0f;
Chris Craikb49f4462014-03-20 12:44:20 -070065 }
66
Chris Craik06451282014-07-21 10:25:54 -070067 bool isEmpty() const {
68 return mType == kOutlineType_Empty;
69 }
70
Chris Craik77b5cad2014-07-30 18:23:07 -070071 float getAlpha() const {
72 return mAlpha;
73 }
74
Chris Craikb49f4462014-03-20 12:44:20 -070075 void setShouldClip(bool clip) {
76 mShouldClip = clip;
77 }
78
Chris Craikdeeda3d2014-05-05 19:09:33 -070079 bool getShouldClip() const {
80 return mShouldClip;
81 }
82
Chris Craikb49f4462014-03-20 12:44:20 -070083 bool willClip() const {
84 // only round rect outlines can be used for clipping
85 return mShouldClip && (mType == kOutlineType_RoundRect);
86 }
87
Chris Craikdeeda3d2014-05-05 19:09:33 -070088 bool getAsRoundRect(Rect* outRect, float* outRadius) const {
89 if (mType == kOutlineType_RoundRect) {
90 outRect->set(mBounds);
91 *outRadius = mRadius;
92 return true;
93 }
94 return false;
95 }
96
John Reckd0a0b2a2014-03-20 16:28:56 -070097 const SkPath* getPath() const {
Chris Craik06451282014-07-21 10:25:54 -070098 if (mType == kOutlineType_None || mType == kOutlineType_Empty) return NULL;
Chris Craikb49f4462014-03-20 12:44:20 -070099
100 return &mPath;
101 }
102
103private:
104 enum OutlineType {
105 kOutlineType_None = 0,
Chris Craik06451282014-07-21 10:25:54 -0700106 kOutlineType_Empty = 1,
107 kOutlineType_ConvexPath = 2,
108 kOutlineType_RoundRect = 3
Chris Craikb49f4462014-03-20 12:44:20 -0700109 };
110
111 bool mShouldClip;
112 OutlineType mType;
113 Rect mBounds;
114 float mRadius;
Chris Craik77b5cad2014-07-30 18:23:07 -0700115 float mAlpha;
Chris Craikb49f4462014-03-20 12:44:20 -0700116 SkPath mPath;
117};
118
119} /* namespace uirenderer */
120} /* namespace android */
121
122#endif /* OUTLINE_H */