BakedOpState.cpp revision 80d2ade939153da87b3cd3b0a69a713bf68b64ba
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 21namespace android { 22namespace uirenderer { 23 24static 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 33ResolvedRenderState::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) 53 clipState = snapshot.mutateClipArea().serializeIntersectedClip(allocator, 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 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; 64 clippedBounds.setEmpty(); 65 } else { 66 // Not rejected! compute true clippedBounds, clipSideFlags, and path mask 67 clipSideFlags = computeClipSideFlags(clipRect, clippedBounds); 68 clippedBounds.doIntersect(clipRect); 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 } 82 } 83} 84 85ResolvedRenderState::ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot, 86 const Matrix4& localTransform, const ClipBase* localClip) { 87 transform.loadMultiply(*snapshot.transform, localTransform); 88 clipState = snapshot.mutateClipArea().serializeIntersectedClip(allocator, 89 localClip, *(snapshot.transform)); 90 clippedBounds = clipState->rect; 91 clipSideFlags = OpClipSideFlags::Full; 92 localProjectionPathMask = nullptr; 93} 94 95ResolvedRenderState::ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot) 96 : transform(*snapshot.transform) 97 , clipState(snapshot.mutateClipArea().serializeClip(allocator)) 98 , clippedBounds(clipState->rect) 99 , clipSideFlags(OpClipSideFlags::Full) 100 , localProjectionPathMask(nullptr) {} 101 102ResolvedRenderState::ResolvedRenderState(const ClipRect* clipRect, const Rect& dstRect) 103 : transform(Matrix4::identity()) 104 , clipState(clipRect) 105 , clippedBounds(dstRect) 106 , clipSideFlags(computeClipSideFlags(clipRect->rect, dstRect)) 107 , localProjectionPathMask(nullptr) { 108 clippedBounds.doIntersect(clipRect->rect); 109} 110 111BakedOpState* BakedOpState::tryConstruct(LinearAllocator& allocator, 112 Snapshot& snapshot, const RecordedOp& recordedOp) { 113 if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr; 114 BakedOpState* bakedState = allocator.create_trivial<BakedOpState>( 115 allocator, snapshot, recordedOp, false); 116 if (bakedState->computedState.clippedBounds.isEmpty()) { 117 // bounds are empty, so op is rejected 118 allocator.rewindIfLastAlloc(bakedState); 119 return nullptr; 120 } 121 return bakedState; 122} 123 124BakedOpState* BakedOpState::tryConstructUnbounded(LinearAllocator& allocator, 125 Snapshot& snapshot, const RecordedOp& recordedOp) { 126 if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr; 127 return allocator.create_trivial<BakedOpState>(allocator, snapshot, recordedOp); 128} 129 130BakedOpState* BakedOpState::tryStrokeableOpConstruct(LinearAllocator& allocator, 131 Snapshot& snapshot, const RecordedOp& recordedOp, StrokeBehavior strokeBehavior) { 132 if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr; 133 bool expandForStroke = (strokeBehavior == StrokeBehavior::StyleDefined) 134 ? (recordedOp.paint && recordedOp.paint->getStyle() != SkPaint::kFill_Style) 135 : true; 136 137 BakedOpState* bakedState = allocator.create_trivial<BakedOpState>( 138 allocator, snapshot, recordedOp, expandForStroke); 139 if (bakedState->computedState.clippedBounds.isEmpty()) { 140 // bounds are empty, so op is rejected 141 // NOTE: this won't succeed if a clip was allocated 142 allocator.rewindIfLastAlloc(bakedState); 143 return nullptr; 144 } 145 return bakedState; 146} 147 148BakedOpState* BakedOpState::tryShadowOpConstruct(LinearAllocator& allocator, 149 Snapshot& snapshot, const ShadowOp* shadowOpPtr) { 150 if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr; 151 152 // clip isn't empty, so construct the op 153 return allocator.create_trivial<BakedOpState>(allocator, snapshot, shadowOpPtr); 154} 155 156BakedOpState* BakedOpState::directConstruct(LinearAllocator& allocator, 157 const ClipRect* clip, const Rect& dstRect, const RecordedOp& recordedOp) { 158 return allocator.create_trivial<BakedOpState>(clip, dstRect, recordedOp); 159} 160 161void BakedOpState::setupOpacity(const SkPaint* paint) { 162 computedState.opaqueOverClippedBounds = computedState.transform.isSimple() 163 && computedState.clipState->mode == ClipMode::Rectangle 164 && MathUtils::areEqual(alpha, 1.0f) 165 && !roundRectClipState 166 && PaintUtils::isOpaquePaint(paint); 167} 168 169} // namespace uirenderer 170} // namespace android 171