Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [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 | |
| 17 | #ifndef ANDROID_HWUI_BAKED_OP_STATE_H |
| 18 | #define ANDROID_HWUI_BAKED_OP_STATE_H |
| 19 | |
| 20 | #include "Matrix.h" |
| 21 | #include "RecordedOp.h" |
| 22 | #include "Rect.h" |
| 23 | #include "Snapshot.h" |
| 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
| 28 | namespace OpClipSideFlags { |
| 29 | enum { |
| 30 | None = 0x0, |
| 31 | Left = 0x1, |
| 32 | Top = 0x2, |
| 33 | Right = 0x4, |
| 34 | Bottom = 0x8, |
| 35 | Full = 0xF, |
| 36 | // ConservativeFull = 0x1F needed? |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | /** |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 41 | * Holds a list of BakedOpStates of ops that can be drawn together |
| 42 | */ |
| 43 | struct MergedBakedOpList { |
| 44 | const BakedOpState*const* states; |
| 45 | size_t count; |
| 46 | int clipSideFlags; |
| 47 | Rect clip; |
| 48 | }; |
| 49 | |
| 50 | /** |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 51 | * Holds the resolved clip, transform, and bounds of a recordedOp, when replayed with a snapshot |
| 52 | */ |
| 53 | class ResolvedRenderState { |
| 54 | public: |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 55 | ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot, |
| 56 | const RecordedOp& recordedOp, bool expandForStroke); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 57 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 58 | // Constructor for unbounded ops without transform/clip (namely shadows) |
| 59 | ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 60 | |
Chris Craik | 7435eb1 | 2016-01-07 17:41:40 -0800 | [diff] [blame] | 61 | // Constructor for primitive ops provided clip, and no transform |
| 62 | ResolvedRenderState(const ClipRect* viewportRect, const Rect& dstRect); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 63 | |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 64 | Rect computeLocalSpaceClip() const { |
| 65 | Matrix4 inverse; |
| 66 | inverse.loadInverse(transform); |
| 67 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 68 | Rect outClip(clipRect()); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 69 | inverse.mapRect(outClip); |
| 70 | return outClip; |
| 71 | } |
| 72 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 73 | const Rect& clipRect() const { |
| 74 | return clipState->rect; |
| 75 | } |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 76 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 77 | bool requiresClip() const { |
| 78 | return clipSideFlags != OpClipSideFlags::None |
Chris Craik | 7435eb1 | 2016-01-07 17:41:40 -0800 | [diff] [blame] | 79 | || CC_UNLIKELY(clipState->mode != ClipMode::Rectangle); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // returns the clip if it's needed to draw the operation, otherwise nullptr |
| 83 | const ClipBase* getClipIfNeeded() const { |
| 84 | return requiresClip() ? clipState : nullptr; |
| 85 | } |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 86 | |
| 87 | Matrix4 transform; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 88 | const ClipBase* clipState = nullptr; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 89 | Rect clippedBounds; |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 90 | int clipSideFlags = 0; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * Self-contained op wrapper, containing all resolved state required to draw the op. |
| 95 | * |
| 96 | * Stashed pointers within all point to longer lived objects, with no ownership implied. |
| 97 | */ |
| 98 | class BakedOpState { |
| 99 | public: |
| 100 | static BakedOpState* tryConstruct(LinearAllocator& allocator, |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 101 | Snapshot& snapshot, const RecordedOp& recordedOp) { |
Chris Craik | 15f0468 | 2016-01-11 17:50:08 -0800 | [diff] [blame] | 102 | if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr; |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 103 | BakedOpState* bakedState = allocator.create_trivial<BakedOpState>( |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 104 | allocator, snapshot, recordedOp, false); |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 105 | if (bakedState->computedState.clippedBounds.isEmpty()) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 106 | // bounds are empty, so op is rejected |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 107 | allocator.rewindIfLastAlloc(bakedState); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 108 | return nullptr; |
| 109 | } |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 110 | return bakedState; |
| 111 | } |
| 112 | |
| 113 | enum class StrokeBehavior { |
| 114 | // stroking is forced, regardless of style on paint |
| 115 | Forced, |
| 116 | // stroking is defined by style on paint |
| 117 | StyleDefined, |
| 118 | }; |
| 119 | |
| 120 | static BakedOpState* tryStrokeableOpConstruct(LinearAllocator& allocator, |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 121 | Snapshot& snapshot, const RecordedOp& recordedOp, StrokeBehavior strokeBehavior) { |
Chris Craik | 15f0468 | 2016-01-11 17:50:08 -0800 | [diff] [blame] | 122 | if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr; |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 123 | bool expandForStroke = (strokeBehavior == StrokeBehavior::StyleDefined) |
| 124 | ? (recordedOp.paint && recordedOp.paint->getStyle() != SkPaint::kFill_Style) |
| 125 | : true; |
| 126 | |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 127 | BakedOpState* bakedState = allocator.create_trivial<BakedOpState>( |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 128 | allocator, snapshot, recordedOp, expandForStroke); |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 129 | if (bakedState->computedState.clippedBounds.isEmpty()) { |
| 130 | // bounds are empty, so op is rejected |
Chris Craik | 15f0468 | 2016-01-11 17:50:08 -0800 | [diff] [blame] | 131 | // NOTE: this won't succeed if a clip was allocated |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 132 | allocator.rewindIfLastAlloc(bakedState); |
| 133 | return nullptr; |
| 134 | } |
| 135 | return bakedState; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 138 | static BakedOpState* tryShadowOpConstruct(LinearAllocator& allocator, |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 139 | Snapshot& snapshot, const ShadowOp* shadowOpPtr) { |
Chris Craik | 15f0468 | 2016-01-11 17:50:08 -0800 | [diff] [blame] | 140 | if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr; |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 141 | |
| 142 | // clip isn't empty, so construct the op |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 143 | return allocator.create_trivial<BakedOpState>(allocator, snapshot, shadowOpPtr); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 146 | static BakedOpState* directConstruct(LinearAllocator& allocator, |
Chris Craik | 7435eb1 | 2016-01-07 17:41:40 -0800 | [diff] [blame] | 147 | const ClipRect* clip, const Rect& dstRect, const RecordedOp& recordedOp) { |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 148 | return allocator.create_trivial<BakedOpState>(clip, dstRect, recordedOp); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // computed state: |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 152 | ResolvedRenderState computedState; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 153 | |
| 154 | // simple state (straight pointer/value storage): |
| 155 | const float alpha; |
| 156 | const RoundRectClipState* roundRectClipState; |
| 157 | const ProjectionPathMask* projectionPathMask; |
| 158 | const RecordedOp* op; |
| 159 | |
| 160 | private: |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 161 | friend class LinearAllocator; |
| 162 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 163 | BakedOpState(LinearAllocator& allocator, Snapshot& snapshot, |
| 164 | const RecordedOp& recordedOp, bool expandForStroke) |
| 165 | : computedState(allocator, snapshot, recordedOp, expandForStroke) |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 166 | , alpha(snapshot.alpha) |
| 167 | , roundRectClipState(snapshot.roundRectClipState) |
| 168 | , projectionPathMask(snapshot.projectionPathMask) |
| 169 | , op(&recordedOp) {} |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 170 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 171 | BakedOpState(LinearAllocator& allocator, Snapshot& snapshot, const ShadowOp* shadowOpPtr) |
| 172 | : computedState(allocator, snapshot) |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 173 | , alpha(snapshot.alpha) |
| 174 | , roundRectClipState(snapshot.roundRectClipState) |
| 175 | , projectionPathMask(snapshot.projectionPathMask) |
| 176 | , op(shadowOpPtr) {} |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 177 | |
Chris Craik | 7435eb1 | 2016-01-07 17:41:40 -0800 | [diff] [blame] | 178 | BakedOpState(const ClipRect* viewportRect, const Rect& dstRect, const RecordedOp& recordedOp) |
| 179 | : computedState(viewportRect, dstRect) |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 180 | , alpha(1.0f) |
| 181 | , roundRectClipState(nullptr) |
| 182 | , projectionPathMask(nullptr) |
| 183 | , op(&recordedOp) {} |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | }; // namespace uirenderer |
| 187 | }; // namespace android |
| 188 | |
| 189 | #endif // ANDROID_HWUI_BAKED_OP_STATE_H |