blob: 8f261c83b8d3caa701903be4acb24788c16657d2 [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
57// Assert that the cast from SkClipOp to SkRegion::Op is valid
58static_assert(static_cast<int>(SkClipOp::kDifference) == SkRegion::Op::kDifference_Op);
59static_assert(static_cast<int>(SkClipOp::kIntersect) == SkRegion::Op::kIntersect_Op);
60static_assert(static_cast<int>(SkClipOp::kUnion_deprecated) == SkRegion::Op::kUnion_Op);
61static_assert(static_cast<int>(SkClipOp::kXOR_deprecated) == SkRegion::Op::kXOR_Op);
62static_assert(static_cast<int>(SkClipOp::kReverseDifference_deprecated) == SkRegion::Op::kReverseDifference_Op);
63static_assert(static_cast<int>(SkClipOp::kReplace_deprecated) == SkRegion::Op::kReplace_Op);
64
65void CanvasStateHelper::internalClipRect(const SkRect& rect, SkClipOp op) {
66 clip().opRect(rect, transform(), mInitialBounds, (SkRegion::Op)op, false);
67}
68
69void CanvasStateHelper::internalClipPath(const SkPath& path, SkClipOp op) {
70 clip().opPath(path, transform(), mInitialBounds, (SkRegion::Op)op, true);
71}
72
73bool CanvasStateHelper::internalRestore() {
74 // Prevent underflows
75 if (saveCount() <= 1) {
76 return false;
77 }
78
79 SaveEntry entry = mSaveStack[mSaveStack.size() - 1];
80 mSaveStack.pop_back();
81 bool needsRestorePropagation = entry.layer;
82 if (entry.matrix) {
83 mTransformStack.pop_back();
84 mCurrentTransformIndex -= 1;
85 }
86 if (entry.clip) {
87 // We need to push before accessing clip() to ensure the reference doesn't move
88 // across vector resizes
89 mClipStack.pop_back();
90 mCurrentClipIndex -= 1;
91 needsRestorePropagation = true;
92 }
93 return needsRestorePropagation;
94}
95
96SkRect CanvasStateHelper::getClipBounds() const {
97 SkIRect ibounds = clip().getBounds();
98
99 if (ibounds.isEmpty()) {
100 return SkRect::MakeEmpty();
101 }
102
103 SkMatrix inverse;
104 // if we can't invert the CTM, we can't return local clip bounds
105 if (!transform().invert(&inverse)) {
106 return SkRect::MakeEmpty();
107 }
108
109 SkRect ret = SkRect::MakeEmpty();
110 inverse.mapRect(&ret, SkRect::Make(ibounds));
111 return ret;
112}
113
114bool CanvasStateHelper::quickRejectRect(float left, float top, float right, float bottom) const {
115 // TODO: Implement
116 return false;
117}
118
119bool CanvasStateHelper::quickRejectPath(const SkPath& path) const {
120 // TODO: Implement
121 return false;
122}
123
124} // namespace android::uirenderer