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