blob: 682bd045098de0d6e69af062c57a5059858086aa [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,
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
Chris Craik15f04682016-01-11 17:50:08 -080060
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 Craike4db79d2015-12-22 16:32:23 -080064 clippedBounds.setEmpty();
65 } else {
66 // Not rejected! compute true clippedBounds and clipSideFlags
Chris Craik4876de12016-02-25 16:54:08 -080067 clipSideFlags = computeClipSideFlags(clipRect, clippedBounds);
Chris Craike4db79d2015-12-22 16:32:23 -080068 clippedBounds.doIntersect(clipRect);
69 }
70}
71
Chris Craik6e068c012016-01-15 16:15:30 -080072ResolvedRenderState::ResolvedRenderState(LinearAllocator& allocator, Snapshot& snapshot)
73 : transform(*snapshot.transform)
74 , clipState(snapshot.mutateClipArea().serializeClip(allocator))
75 , clippedBounds(clipState->rect)
76 , clipSideFlags(OpClipSideFlags::Full) {}
Chris Craike4db79d2015-12-22 16:32:23 -080077
Chris Craik4876de12016-02-25 16:54:08 -080078ResolvedRenderState::ResolvedRenderState(const ClipRect* clipRect, const Rect& dstRect)
Chris Craikb87eadd2016-01-06 09:16:05 -080079 : transform(Matrix4::identity())
Chris Craik4876de12016-02-25 16:54:08 -080080 , clipState(clipRect)
Chris Craikb87eadd2016-01-06 09:16:05 -080081 , clippedBounds(dstRect)
Chris Craik4876de12016-02-25 16:54:08 -080082 , clipSideFlags(computeClipSideFlags(clipRect->rect, dstRect)) {
83 clippedBounds.doIntersect(clipRect->rect);
84}
Chris Craikb87eadd2016-01-06 09:16:05 -080085
Chris Craike4db79d2015-12-22 16:32:23 -080086} // namespace uirenderer
87} // namespace android