blob: 63edf77279e336c740016a5a32f02de0e966e84c [file] [log] [blame]
Chris Craike4db79d2015-12-22 16:32:23 -08001/*
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
Chris Craik4876de12016-02-25 16:54:08 -080024static 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 Craike4db79d2015-12-22 16:32:23 -080033ResolvedRenderState::ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot,
John Reck1bcacfd2017-11-03 10:12:19 -070034 const RecordedOp& recordedOp, bool expandForStroke,
35 bool expandForPathTexture) {
Chris Craike4db79d2015-12-22 16:32:23 -080036 // 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);
Chris Craik49b403d2017-03-06 13:51:43 -080044 } else if (CC_UNLIKELY(expandForPathTexture)) {
45 clippedBounds.outset(1);
Chris Craike4db79d2015-12-22 16:32:23 -080046 }
47 transform.mapRect(clippedBounds);
John Reck1bcacfd2017-11-03 10:12:19 -070048 if (CC_UNLIKELY(expandForStroke &&
49 (!transform.isPureTranslate() || recordedOp.paint->getStrokeWidth() < 1.0f))) {
Chris Craike4db79d2015-12-22 16:32:23 -080050 // 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)
John Reck1bcacfd2017-11-03 10:12:19 -070056 clipState = snapshot.serializeIntersectedClip(allocator, recordedOp.localClip,
57 *(snapshot.transform));
Chris Craike4db79d2015-12-22 16:32:23 -080058 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
Chris Craik15f04682016-01-11 17:50:08 -080063
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;
Chris Craike4db79d2015-12-22 16:32:23 -080067 clippedBounds.setEmpty();
68 } else {
Chris Craik678ff812016-03-01 13:27:54 -080069 // Not rejected! compute true clippedBounds, clipSideFlags, and path mask
Chris Craik4876de12016-02-25 16:54:08 -080070 clipSideFlags = computeClipSideFlags(clipRect, clippedBounds);
Chris Craike4db79d2015-12-22 16:32:23 -080071 clippedBounds.doIntersect(clipRect);
Chris Craik678ff812016-03-01 13:27:54 -080072
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 }
Chris Craike4db79d2015-12-22 16:32:23 -080085 }
86}
87
Chris Craik4c3980b2016-03-15 14:20:18 -070088ResolvedRenderState::ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot,
John Reck1bcacfd2017-11-03 10:12:19 -070089 const Matrix4& localTransform, const ClipBase* localClip) {
Chris Craik4c3980b2016-03-15 14:20:18 -070090 transform.loadMultiply(*snapshot.transform, localTransform);
Chris Craik04d46eb2016-04-07 13:51:07 -070091 clipState = snapshot.serializeIntersectedClip(allocator, localClip, *(snapshot.transform));
Chris Craik4c3980b2016-03-15 14:20:18 -070092 clippedBounds = clipState->rect;
93 clipSideFlags = OpClipSideFlags::Full;
94 localProjectionPathMask = nullptr;
95}
96
Chris Craik6e068c012016-01-15 16:15:30 -080097ResolvedRenderState::ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot)
98 : transform(*snapshot.transform)
99 , clipState(snapshot.mutateClipArea().serializeClip(allocator))
100 , clippedBounds(clipState->rect)
Chris Craik678ff812016-03-01 13:27:54 -0800101 , clipSideFlags(OpClipSideFlags::Full)
102 , localProjectionPathMask(nullptr) {}
Chris Craike4db79d2015-12-22 16:32:23 -0800103
Chris Craik4876de12016-02-25 16:54:08 -0800104ResolvedRenderState::ResolvedRenderState(const ClipRect* clipRect, const Rect& dstRect)
Chris Craikb87eadd2016-01-06 09:16:05 -0800105 : transform(Matrix4::identity())
Chris Craik4876de12016-02-25 16:54:08 -0800106 , clipState(clipRect)
Chris Craikb87eadd2016-01-06 09:16:05 -0800107 , clippedBounds(dstRect)
Chris Craik678ff812016-03-01 13:27:54 -0800108 , clipSideFlags(computeClipSideFlags(clipRect->rect, dstRect))
109 , localProjectionPathMask(nullptr) {
Chris Craik4876de12016-02-25 16:54:08 -0800110 clippedBounds.doIntersect(clipRect->rect);
111}
Chris Craikb87eadd2016-01-06 09:16:05 -0800112
John Reck1bcacfd2017-11-03 10:12:19 -0700113BakedOpState* BakedOpState::tryConstruct(LinearAllocator& allocator, Snapshot& snapshot,
114 const RecordedOp& recordedOp) {
Chris Craik80d2ade2016-03-28 12:54:07 -0700115 if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr;
John Reck1bcacfd2017-11-03 10:12:19 -0700116 BakedOpState* bakedState =
117 allocator.create_trivial<BakedOpState>(allocator, snapshot, recordedOp, false, false);
Chris Craik80d2ade2016-03-28 12:54:07 -0700118 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
John Reck1bcacfd2017-11-03 10:12:19 -0700126BakedOpState* BakedOpState::tryConstructUnbounded(LinearAllocator& allocator, Snapshot& snapshot,
127 const RecordedOp& recordedOp) {
Chris Craik80d2ade2016-03-28 12:54:07 -0700128 if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr;
129 return allocator.create_trivial<BakedOpState>(allocator, snapshot, recordedOp);
130}
131
John Reck1bcacfd2017-11-03 10:12:19 -0700132BakedOpState* BakedOpState::tryStrokeableOpConstruct(LinearAllocator& allocator, Snapshot& snapshot,
133 const RecordedOp& recordedOp,
134 StrokeBehavior strokeBehavior,
135 bool expandForPathTexture) {
Chris Craik80d2ade2016-03-28 12:54:07 -0700136 if (CC_UNLIKELY(snapshot.getRenderTargetClip().isEmpty())) return nullptr;
John Reck1bcacfd2017-11-03 10:12:19 -0700137 bool expandForStroke =
138 (strokeBehavior == StrokeBehavior::Forced ||
139 (recordedOp.paint && recordedOp.paint->getStyle() != SkPaint::kFill_Style));
Chris Craik80d2ade2016-03-28 12:54:07 -0700140
141 BakedOpState* bakedState = allocator.create_trivial<BakedOpState>(
John Reck1bcacfd2017-11-03 10:12:19 -0700142 allocator, snapshot, recordedOp, expandForStroke, expandForPathTexture);
Chris Craik80d2ade2016-03-28 12:54:07 -0700143 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
John Reck1bcacfd2017-11-03 10:12:19 -0700152BakedOpState* BakedOpState::tryShadowOpConstruct(LinearAllocator& allocator, Snapshot& snapshot,
153 const ShadowOp* shadowOpPtr) {
Chris Craik80d2ade2016-03-28 12:54:07 -0700154 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
John Reck1bcacfd2017-11-03 10:12:19 -0700160BakedOpState* BakedOpState::directConstruct(LinearAllocator& allocator, const ClipRect* clip,
161 const Rect& dstRect, const RecordedOp& recordedOp) {
Chris Craik80d2ade2016-03-28 12:54:07 -0700162 return allocator.create_trivial<BakedOpState>(clip, dstRect, recordedOp);
163}
164
165void BakedOpState::setupOpacity(const SkPaint* paint) {
John Reck1bcacfd2017-11-03 10:12:19 -0700166 computedState.opaqueOverClippedBounds = computedState.transform.isSimple() &&
167 computedState.clipState->mode == ClipMode::Rectangle &&
168 MathUtils::areEqual(alpha, 1.0f) &&
169 !roundRectClipState && PaintUtils::isOpaquePaint(paint);
Chris Craik80d2ade2016-03-28 12:54:07 -0700170}
171
John Reck1bcacfd2017-11-03 10:12:19 -0700172} // namespace uirenderer
173} // namespace android