Chris Craik | e4db79d | 2015-12-22 16:32:23 -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 | |
| 17 | #include "BakedOpState.h" |
| 18 | |
| 19 | #include "ClipArea.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 24 | static int computeClipSideFlags(const Rect& clip, const Rect& bounds) { |
| 25 | int clipSideFlags = 0; |
| 26 | if (clip.left > bounds.left) clipSideFlags |= OpClipSideFlags::Left; |
| 27 | if (clip.top > bounds.top) clipSideFlags |= OpClipSideFlags::Top; |
| 28 | if (clip.right < bounds.right) clipSideFlags |= OpClipSideFlags::Right; |
| 29 | if (clip.bottom < bounds.bottom) clipSideFlags |= OpClipSideFlags::Bottom; |
| 30 | return clipSideFlags; |
| 31 | } |
| 32 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 33 | ResolvedRenderState::ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot, |
| 34 | const RecordedOp& recordedOp, bool expandForStroke) { |
| 35 | // resolvedMatrix = parentMatrix * localMatrix |
| 36 | transform.loadMultiply(*snapshot.transform, recordedOp.localMatrix); |
| 37 | |
| 38 | // resolvedClippedBounds = intersect(resolvedMatrix * opBounds, resolvedClipRect) |
| 39 | clippedBounds = recordedOp.unmappedBounds; |
| 40 | if (CC_UNLIKELY(expandForStroke)) { |
| 41 | // account for non-hairline stroke |
| 42 | clippedBounds.outset(recordedOp.paint->getStrokeWidth() * 0.5f); |
| 43 | } |
| 44 | transform.mapRect(clippedBounds); |
| 45 | if (CC_UNLIKELY(expandForStroke |
| 46 | && (!transform.isPureTranslate() || recordedOp.paint->getStrokeWidth() < 1.0f))) { |
| 47 | // account for hairline stroke when stroke may be < 1 scaled pixel |
| 48 | // Non translate || strokeWidth < 1 is conservative, but will cover all cases |
| 49 | clippedBounds.outset(0.5f); |
| 50 | } |
| 51 | |
| 52 | // resolvedClipRect = intersect(parentMatrix * localClip, parentClip) |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 53 | clipState = snapshot.serializeIntersectedClip(allocator, |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 54 | recordedOp.localClip, *(snapshot.transform)); |
| 55 | LOG_ALWAYS_FATAL_IF(!clipState, "must clip!"); |
| 56 | |
| 57 | const Rect& clipRect = clipState->rect; |
| 58 | if (CC_UNLIKELY(clipRect.isEmpty() || !clippedBounds.intersects(clipRect))) { |
| 59 | // Rejected based on either empty clip, or bounds not intersecting with clip |
Chris Craik | 15f0468 | 2016-01-11 17:50:08 -0800 | [diff] [blame] | 60 | |
| 61 | // Note: we could rewind the clipState object in situations where the clipRect is empty, |
| 62 | // but *only* if the caching logic within ClipArea was aware of the rewind. |
| 63 | clipState = nullptr; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 64 | clippedBounds.setEmpty(); |
| 65 | } else { |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 66 | // Not rejected! compute true clippedBounds, clipSideFlags, and path mask |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 67 | clipSideFlags = computeClipSideFlags(clipRect, clippedBounds); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 68 | clippedBounds.doIntersect(clipRect); |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 69 | |
| 70 | if (CC_UNLIKELY(snapshot.projectionPathMask)) { |
| 71 | // map projection path mask from render target space into op space, |
| 72 | // so intersection with op geometry is possible |
| 73 | Matrix4 inverseTransform; |
| 74 | inverseTransform.loadInverse(transform); |
| 75 | SkMatrix skInverseTransform; |
| 76 | inverseTransform.copyTo(skInverseTransform); |
| 77 | |
| 78 | auto localMask = allocator.create<SkPath>(); |
| 79 | snapshot.projectionPathMask->transform(skInverseTransform, localMask); |
| 80 | localProjectionPathMask = localMask; |
| 81 | } |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Chris Craik | 4c3980b | 2016-03-15 14:20:18 -0700 | [diff] [blame] | 85 | ResolvedRenderState::ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot, |
| 86 | const Matrix4& localTransform, const ClipBase* localClip) { |
| 87 | transform.loadMultiply(*snapshot.transform, localTransform); |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 88 | clipState = snapshot.serializeIntersectedClip(allocator, localClip, *(snapshot.transform)); |
Chris Craik | 4c3980b | 2016-03-15 14:20:18 -0700 | [diff] [blame] | 89 | clippedBounds = clipState->rect; |
| 90 | clipSideFlags = OpClipSideFlags::Full; |
| 91 | localProjectionPathMask = nullptr; |
| 92 | } |
| 93 | |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 94 | ResolvedRenderState::ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot) |
| 95 | : transform(*snapshot.transform) |
| 96 | , clipState(snapshot.mutateClipArea().serializeClip(allocator)) |
| 97 | , clippedBounds(clipState->rect) |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 98 | , clipSideFlags(OpClipSideFlags::Full) |
| 99 | , localProjectionPathMask(nullptr) {} |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 100 | |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 101 | ResolvedRenderState::ResolvedRenderState(const ClipRect* clipRect, const Rect& dstRect) |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 102 | : transform(Matrix4::identity()) |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 103 | , clipState(clipRect) |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 104 | , clippedBounds(dstRect) |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 105 | , clipSideFlags(computeClipSideFlags(clipRect->rect, dstRect)) |
| 106 | , localProjectionPathMask(nullptr) { |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 107 | clippedBounds.doIntersect(clipRect->rect); |
| 108 | } |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 109 | |
Chris Craik | 80d2ade | 2016-03-28 12:54:07 -0700 | [diff] [blame] | 110 | BakedOpState* BakedOpState::tryConstruct(LinearAllocator& allocator, |
| 111 | Snapshot& snapshot, const RecordedOp& recordedOp) { |
| 112 | if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr; |
| 113 | BakedOpState* bakedState = allocator.create_trivial<BakedOpState>( |
| 114 | allocator, snapshot, recordedOp, false); |
| 115 | if (bakedState->computedState.clippedBounds.isEmpty()) { |
| 116 | // bounds are empty, so op is rejected |
| 117 | allocator.rewindIfLastAlloc(bakedState); |
| 118 | return nullptr; |
| 119 | } |
| 120 | return bakedState; |
| 121 | } |
| 122 | |
| 123 | BakedOpState* BakedOpState::tryConstructUnbounded(LinearAllocator& allocator, |
| 124 | Snapshot& snapshot, const RecordedOp& recordedOp) { |
| 125 | if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr; |
| 126 | return allocator.create_trivial<BakedOpState>(allocator, snapshot, recordedOp); |
| 127 | } |
| 128 | |
| 129 | BakedOpState* BakedOpState::tryStrokeableOpConstruct(LinearAllocator& allocator, |
| 130 | Snapshot& snapshot, const RecordedOp& recordedOp, StrokeBehavior strokeBehavior) { |
| 131 | if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr; |
| 132 | bool expandForStroke = (strokeBehavior == StrokeBehavior::StyleDefined) |
| 133 | ? (recordedOp.paint && recordedOp.paint->getStyle() != SkPaint::kFill_Style) |
| 134 | : true; |
| 135 | |
| 136 | BakedOpState* bakedState = allocator.create_trivial<BakedOpState>( |
| 137 | allocator, snapshot, recordedOp, expandForStroke); |
| 138 | if (bakedState->computedState.clippedBounds.isEmpty()) { |
| 139 | // bounds are empty, so op is rejected |
| 140 | // NOTE: this won't succeed if a clip was allocated |
| 141 | allocator.rewindIfLastAlloc(bakedState); |
| 142 | return nullptr; |
| 143 | } |
| 144 | return bakedState; |
| 145 | } |
| 146 | |
| 147 | BakedOpState* BakedOpState::tryShadowOpConstruct(LinearAllocator& allocator, |
| 148 | Snapshot& snapshot, const ShadowOp* shadowOpPtr) { |
| 149 | if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr; |
| 150 | |
| 151 | // clip isn't empty, so construct the op |
| 152 | return allocator.create_trivial<BakedOpState>(allocator, snapshot, shadowOpPtr); |
| 153 | } |
| 154 | |
| 155 | BakedOpState* BakedOpState::directConstruct(LinearAllocator& allocator, |
| 156 | const ClipRect* clip, const Rect& dstRect, const RecordedOp& recordedOp) { |
| 157 | return allocator.create_trivial<BakedOpState>(clip, dstRect, recordedOp); |
| 158 | } |
| 159 | |
| 160 | void BakedOpState::setupOpacity(const SkPaint* paint) { |
| 161 | computedState.opaqueOverClippedBounds = computedState.transform.isSimple() |
| 162 | && computedState.clipState->mode == ClipMode::Rectangle |
| 163 | && MathUtils::areEqual(alpha, 1.0f) |
| 164 | && !roundRectClipState |
| 165 | && PaintUtils::isOpaquePaint(paint); |
| 166 | } |
| 167 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 168 | } // namespace uirenderer |
| 169 | } // namespace android |