blob: 2c839b0ffc1560f56a6df7caf1d0749a4048deeb [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) {
24 mInitialBounds = SkIRect::MakeWH(width, height);
25 mSaveStack.emplace_back();
26 mClipStack.emplace_back().setRect(mInitialBounds);
27 mTransformStack.emplace_back();
28 mCurrentClipIndex = 0;
29 mCurrentTransformIndex = 0;
30}
31
32bool CanvasStateHelper::internalSave(SaveEntry saveEntry) {
33 mSaveStack.push_back(saveEntry);
34 if (saveEntry.matrix) {
35 // We need to push before accessing transform() to ensure the reference doesn't move
36 // across vector resizes
37 mTransformStack.emplace_back() = transform();
38 mCurrentTransformIndex += 1;
39 }
40 if (saveEntry.clip) {
41 // We need to push before accessing clip() to ensure the reference doesn't move
42 // across vector resizes
43 mClipStack.emplace_back() = clip();
44 mCurrentClipIndex += 1;
45 return true;
46 }
47 return false;
48}
49
50// Assert that the cast from SkClipOp to SkRegion::Op is valid
51static_assert(static_cast<int>(SkClipOp::kDifference) == SkRegion::Op::kDifference_Op);
52static_assert(static_cast<int>(SkClipOp::kIntersect) == SkRegion::Op::kIntersect_Op);
53static_assert(static_cast<int>(SkClipOp::kUnion_deprecated) == SkRegion::Op::kUnion_Op);
54static_assert(static_cast<int>(SkClipOp::kXOR_deprecated) == SkRegion::Op::kXOR_Op);
55static_assert(static_cast<int>(SkClipOp::kReverseDifference_deprecated) == SkRegion::Op::kReverseDifference_Op);
56static_assert(static_cast<int>(SkClipOp::kReplace_deprecated) == SkRegion::Op::kReplace_Op);
57
58void CanvasStateHelper::internalClipRect(const SkRect& rect, SkClipOp op) {
59 clip().opRect(rect, transform(), mInitialBounds, (SkRegion::Op)op, false);
60}
61
62void CanvasStateHelper::internalClipPath(const SkPath& path, SkClipOp op) {
63 clip().opPath(path, transform(), mInitialBounds, (SkRegion::Op)op, true);
64}
65
66bool CanvasStateHelper::internalRestore() {
67 // Prevent underflows
68 if (saveCount() <= 1) {
69 return false;
70 }
71
72 SaveEntry entry = mSaveStack[mSaveStack.size() - 1];
73 mSaveStack.pop_back();
74 bool needsRestorePropagation = entry.layer;
75 if (entry.matrix) {
76 mTransformStack.pop_back();
77 mCurrentTransformIndex -= 1;
78 }
79 if (entry.clip) {
80 // We need to push before accessing clip() to ensure the reference doesn't move
81 // across vector resizes
82 mClipStack.pop_back();
83 mCurrentClipIndex -= 1;
84 needsRestorePropagation = true;
85 }
86 return needsRestorePropagation;
87}
88
89SkRect CanvasStateHelper::getClipBounds() const {
90 SkIRect ibounds = clip().getBounds();
91
92 if (ibounds.isEmpty()) {
93 return SkRect::MakeEmpty();
94 }
95
96 SkMatrix inverse;
97 // if we can't invert the CTM, we can't return local clip bounds
98 if (!transform().invert(&inverse)) {
99 return SkRect::MakeEmpty();
100 }
101
102 SkRect ret = SkRect::MakeEmpty();
103 inverse.mapRect(&ret, SkRect::Make(ibounds));
104 return ret;
105}
106
107bool CanvasStateHelper::quickRejectRect(float left, float top, float right, float bottom) const {
108 // TODO: Implement
109 return false;
110}
111
112bool CanvasStateHelper::quickRejectPath(const SkPath& path) const {
113 // TODO: Implement
114 return false;
115}
116
117} // namespace android::uirenderer