blob: dd01a5b15da5038886952ea7e6782a7b81e89cbd [file] [log] [blame]
John Reckdc95f102020-11-16 12:35:02 -05001/*
2 * Copyright (C) 2020 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 "CanvasFrontend.h"
18#include "CanvasOps.h"
19#include "CanvasOpBuffer.h"
20
21namespace android::uirenderer {
22
23CanvasStateHelper::CanvasStateHelper(int width, int height) {
John Reckb5eeb182020-12-09 13:45:39 -050024 resetState(width, height);
25}
26
27void CanvasStateHelper::resetState(int width, int height) {
John Reckdc95f102020-11-16 12:35:02 -050028 mInitialBounds = SkIRect::MakeWH(width, height);
John Reckb5eeb182020-12-09 13:45:39 -050029 mSaveStack.clear();
30 mClipStack.clear();
31 mTransformStack.clear();
John Reckdc95f102020-11-16 12:35:02 -050032 mSaveStack.emplace_back();
33 mClipStack.emplace_back().setRect(mInitialBounds);
34 mTransformStack.emplace_back();
35 mCurrentClipIndex = 0;
36 mCurrentTransformIndex = 0;
37}
38
39bool CanvasStateHelper::internalSave(SaveEntry saveEntry) {
40 mSaveStack.push_back(saveEntry);
41 if (saveEntry.matrix) {
42 // We need to push before accessing transform() to ensure the reference doesn't move
43 // across vector resizes
44 mTransformStack.emplace_back() = transform();
45 mCurrentTransformIndex += 1;
46 }
47 if (saveEntry.clip) {
48 // We need to push before accessing clip() to ensure the reference doesn't move
49 // across vector resizes
50 mClipStack.emplace_back() = clip();
51 mCurrentClipIndex += 1;
52 return true;
53 }
54 return false;
55}
56
Michael Ludwig70cf50c22021-07-21 17:02:39 +000057// Assert that the cast from SkClipOp to SkRegion::Op is valid.
58// SkClipOp is a subset of SkRegion::Op and only supports difference and intersect.
John Reckdc95f102020-11-16 12:35:02 -050059static_assert(static_cast<int>(SkClipOp::kDifference) == SkRegion::Op::kDifference_Op);
60static_assert(static_cast<int>(SkClipOp::kIntersect) == SkRegion::Op::kIntersect_Op);
John Reckdc95f102020-11-16 12:35:02 -050061
62void CanvasStateHelper::internalClipRect(const SkRect& rect, SkClipOp op) {
63 clip().opRect(rect, transform(), mInitialBounds, (SkRegion::Op)op, false);
64}
65
66void CanvasStateHelper::internalClipPath(const SkPath& path, SkClipOp op) {
67 clip().opPath(path, transform(), mInitialBounds, (SkRegion::Op)op, true);
68}
69
70bool CanvasStateHelper::internalRestore() {
71 // Prevent underflows
72 if (saveCount() <= 1) {
73 return false;
74 }
75
76 SaveEntry entry = mSaveStack[mSaveStack.size() - 1];
77 mSaveStack.pop_back();
78 bool needsRestorePropagation = entry.layer;
79 if (entry.matrix) {
80 mTransformStack.pop_back();
81 mCurrentTransformIndex -= 1;
82 }
83 if (entry.clip) {
84 // We need to push before accessing clip() to ensure the reference doesn't move
85 // across vector resizes
86 mClipStack.pop_back();
87 mCurrentClipIndex -= 1;
88 needsRestorePropagation = true;
89 }
90 return needsRestorePropagation;
91}
92
93SkRect CanvasStateHelper::getClipBounds() const {
94 SkIRect ibounds = clip().getBounds();
95
96 if (ibounds.isEmpty()) {
97 return SkRect::MakeEmpty();
98 }
99
100 SkMatrix inverse;
101 // if we can't invert the CTM, we can't return local clip bounds
102 if (!transform().invert(&inverse)) {
103 return SkRect::MakeEmpty();
104 }
105
106 SkRect ret = SkRect::MakeEmpty();
107 inverse.mapRect(&ret, SkRect::Make(ibounds));
108 return ret;
109}
110
111bool CanvasStateHelper::quickRejectRect(float left, float top, float right, float bottom) const {
112 // TODO: Implement
113 return false;
114}
115
116bool CanvasStateHelper::quickRejectPath(const SkPath& path) const {
117 // TODO: Implement
118 return false;
119}
120
121} // namespace android::uirenderer