Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 CLIPAREA_H |
| 17 | #define CLIPAREA_H |
| 18 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 19 | #include "Matrix.h" |
| 20 | #include "Rect.h" |
| 21 | #include "utils/Pair.h" |
| 22 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 23 | #include <SkRegion.h> |
| 24 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 28 | class LinearAllocator; |
| 29 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 30 | Rect transformAndCalculateBounds(const Rect& r, const Matrix4& transform); |
| 31 | |
| 32 | class TransformedRectangle { |
| 33 | public: |
| 34 | TransformedRectangle(); |
| 35 | TransformedRectangle(const Rect& bounds, const Matrix4& transform); |
| 36 | |
| 37 | bool canSimplyIntersectWith(const TransformedRectangle& other) const; |
Chris Craik | ac02eb9 | 2015-10-05 12:23:46 -0700 | [diff] [blame] | 38 | void intersectWith(const TransformedRectangle& other); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 39 | |
| 40 | bool isEmpty() const; |
| 41 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 42 | const Rect& getBounds() const { return mBounds; } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 43 | |
| 44 | Rect transformedBounds() const { |
| 45 | Rect transformedBounds(transformAndCalculateBounds(mBounds, mTransform)); |
| 46 | return transformedBounds; |
| 47 | } |
| 48 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 49 | const Matrix4& getTransform() const { return mTransform; } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 50 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 51 | void transform(const Matrix4& transform) { |
| 52 | Matrix4 t; |
| 53 | t.loadMultiply(transform, mTransform); |
| 54 | mTransform = t; |
| 55 | } |
| 56 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 57 | private: |
| 58 | Rect mBounds; |
| 59 | Matrix4 mTransform; |
| 60 | }; |
| 61 | |
| 62 | class RectangleList { |
| 63 | public: |
| 64 | RectangleList(); |
| 65 | |
| 66 | bool isEmpty() const; |
| 67 | int getTransformedRectanglesCount() const; |
| 68 | const TransformedRectangle& getTransformedRectangle(int i) const; |
| 69 | |
| 70 | void setEmpty(); |
| 71 | void set(const Rect& bounds, const Matrix4& transform); |
| 72 | bool intersectWith(const Rect& bounds, const Matrix4& transform); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 73 | void transform(const Matrix4& transform); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 74 | |
| 75 | SkRegion convertToRegion(const SkRegion& clip) const; |
| 76 | Rect calculateBounds() const; |
| 77 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 78 | enum { kMaxTransformedRectangles = 5 }; |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 79 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 80 | private: |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 81 | int mTransformedRectanglesCount; |
| 82 | TransformedRectangle mTransformedRectangles[kMaxTransformedRectangles]; |
| 83 | }; |
| 84 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 85 | enum class ClipMode { |
| 86 | Rectangle, |
| 87 | RectangleList, |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 88 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 89 | // region and path - intersected. if either is empty, don't use |
| 90 | Region |
| 91 | }; |
| 92 | |
| 93 | struct ClipBase { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 94 | explicit ClipBase(ClipMode mode) : mode(mode) {} |
| 95 | explicit ClipBase(const Rect& rect) : mode(ClipMode::Rectangle), rect(rect) {} |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 96 | const ClipMode mode; |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 97 | bool intersectWithRoot = false; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 98 | // Bounds of the clipping area, used to define the scissor, and define which |
| 99 | // portion of the stencil is updated/used |
| 100 | Rect rect; |
Chris Craik | 0280628 | 2016-03-11 19:16:21 -0800 | [diff] [blame] | 101 | |
| 102 | void dump() const; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | struct ClipRect : ClipBase { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 106 | explicit ClipRect(const Rect& rect) : ClipBase(rect) {} |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | struct ClipRectList : ClipBase { |
Chih-Hung Hsieh | a619ec7 | 2016-08-29 14:52:43 -0700 | [diff] [blame] | 110 | explicit ClipRectList(const RectangleList& rectList) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 111 | : ClipBase(ClipMode::RectangleList), rectList(rectList) {} |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 112 | RectangleList rectList; |
| 113 | }; |
| 114 | |
| 115 | struct ClipRegion : ClipBase { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 116 | explicit ClipRegion(const SkRegion& region) : ClipBase(ClipMode::Region), region(region) {} |
| 117 | ClipRegion() : ClipBase(ClipMode::Region) {} |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 118 | SkRegion region; |
| 119 | }; |
| 120 | |
| 121 | class ClipArea { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 122 | public: |
| 123 | ClipArea(); |
| 124 | |
| 125 | void setViewportDimensions(int width, int height); |
| 126 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 127 | bool isEmpty() const { return mClipRect.isEmpty(); } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 128 | |
| 129 | void setEmpty(); |
| 130 | void setClip(float left, float top, float right, float bottom); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 131 | void clipRectWithTransform(const Rect& r, const mat4* transform, SkRegion::Op op); |
| 132 | void clipPathWithTransform(const SkPath& path, const mat4* transform, SkRegion::Op op); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 133 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 134 | const Rect& getClipRect() const { return mClipRect; } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 135 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 136 | const SkRegion& getClipRegion() const { return mClipRegion; } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 137 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 138 | const RectangleList& getRectangleList() const { return mRectangleList; } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 139 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 140 | bool isRegion() const { return ClipMode::Region == mMode; } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 141 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 142 | bool isSimple() const { return mMode == ClipMode::Rectangle; } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 143 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 144 | bool isRectangleList() const { return mMode == ClipMode::RectangleList; } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 145 | |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 146 | WARN_UNUSED_RESULT const ClipBase* serializeClip(LinearAllocator& allocator); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 147 | WARN_UNUSED_RESULT const ClipBase* serializeIntersectedClip( |
| 148 | LinearAllocator& allocator, const ClipBase* recordedClip, |
| 149 | const Matrix4& recordedClipTransform); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 150 | void applyClip(const ClipBase* recordedClip, const Matrix4& recordedClipTransform); |
| 151 | |
Chris Craik | 82457c5 | 2016-06-29 16:22:27 -0700 | [diff] [blame] | 152 | static void applyTransformToRegion(const Matrix4& transform, SkRegion* region); |
| 153 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 154 | private: |
| 155 | void enterRectangleMode(); |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 156 | void rectangleModeClipRectWithTransform(const Rect& r, const mat4* transform, SkRegion::Op op); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 157 | |
| 158 | void enterRectangleListMode(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 159 | void rectangleListModeClipRectWithTransform(const Rect& r, const mat4* transform, |
| 160 | SkRegion::Op op); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 161 | |
| 162 | void enterRegionModeFromRectangleMode(); |
| 163 | void enterRegionModeFromRectangleListMode(); |
| 164 | void enterRegionMode(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 165 | void regionModeClipRectWithTransform(const Rect& r, const mat4* transform, SkRegion::Op op); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 166 | |
Derek Sollenberger | b29b16e | 2017-01-04 14:57:43 -0500 | [diff] [blame] | 167 | void clipRegion(const SkRegion& region, SkRegion::Op op); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 168 | void ensureClipRegion(); |
Tom Hudson | e30b53c | 2015-03-30 15:59:02 -0400 | [diff] [blame] | 169 | void onClipRegionUpdated(); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 170 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 171 | // Called by every state modifying public method. |
| 172 | void onClipUpdated() { |
| 173 | mPostViewportClipObserved = true; |
| 174 | mLastSerialization = nullptr; |
| 175 | mLastResolutionResult = nullptr; |
| 176 | } |
| 177 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 178 | SkRegion createViewportRegion() { return SkRegion(mViewportBounds.toSkIRect()); } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 179 | |
| 180 | void regionFromPath(const SkPath& path, SkRegion& pathAsRegion) { |
Chris Craik | 8ce8f3f | 2015-07-16 13:07:45 -0700 | [diff] [blame] | 181 | // TODO: this should not mask every path to the viewport - this makes it impossible to use |
| 182 | // paths to clip to larger areas (which is valid e.g. with SkRegion::kReplace_Op) |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 183 | pathAsRegion.setPath(path, createViewportRegion()); |
| 184 | } |
| 185 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 186 | ClipMode mMode; |
| 187 | bool mPostViewportClipObserved = false; |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 188 | bool mReplaceOpObserved = false; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 189 | |
| 190 | /** |
| 191 | * If mLastSerialization is non-null, it represents an already serialized copy |
| 192 | * of the current clip state. If null, it has not been computed. |
| 193 | */ |
| 194 | const ClipBase* mLastSerialization = nullptr; |
| 195 | |
| 196 | /** |
| 197 | * This pair of pointers is a single entry cache of most recently seen |
| 198 | */ |
| 199 | const ClipBase* mLastResolutionResult = nullptr; |
| 200 | const ClipBase* mLastResolutionClip = nullptr; |
| 201 | Matrix4 mLastResolutionTransform; |
| 202 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 203 | Rect mViewportBounds; |
| 204 | Rect mClipRect; |
| 205 | SkRegion mClipRegion; |
| 206 | RectangleList mRectangleList; |
| 207 | }; |
| 208 | |
| 209 | } /* namespace uirenderer */ |
| 210 | } /* namespace android */ |
| 211 | |
| 212 | #endif /* CLIPAREA_H_ */ |