• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
21 namespace android::uirenderer {
22 
CanvasStateHelper(int width,int height)23 CanvasStateHelper::CanvasStateHelper(int width, int height) {
24     resetState(width, height);
25 }
26 
resetState(int width,int height)27 void CanvasStateHelper::resetState(int width, int height) {
28     mInitialBounds = SkIRect::MakeWH(width, height);
29     mSaveStack.clear();
30     mClipStack.clear();
31     mTransformStack.clear();
32     mSaveStack.emplace_back();
33     mClipStack.emplace_back().setRect(mInitialBounds);
34     mTransformStack.emplace_back();
35     mCurrentClipIndex = 0;
36     mCurrentTransformIndex = 0;
37 }
38 
internalSave(SaveEntry saveEntry)39 bool 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
58 static_assert(static_cast<int>(SkClipOp::kDifference) == SkRegion::Op::kDifference_Op);
59 static_assert(static_cast<int>(SkClipOp::kIntersect) == SkRegion::Op::kIntersect_Op);
60 static_assert(static_cast<int>(SkClipOp::kUnion_deprecated) == SkRegion::Op::kUnion_Op);
61 static_assert(static_cast<int>(SkClipOp::kXOR_deprecated) == SkRegion::Op::kXOR_Op);
62 static_assert(static_cast<int>(SkClipOp::kReverseDifference_deprecated) == SkRegion::Op::kReverseDifference_Op);
63 static_assert(static_cast<int>(SkClipOp::kReplace_deprecated) == SkRegion::Op::kReplace_Op);
64 
internalClipRect(const SkRect & rect,SkClipOp op)65 void CanvasStateHelper::internalClipRect(const SkRect& rect, SkClipOp op) {
66     clip().opRect(rect, transform(), mInitialBounds, (SkRegion::Op)op, false);
67 }
68 
internalClipPath(const SkPath & path,SkClipOp op)69 void CanvasStateHelper::internalClipPath(const SkPath& path, SkClipOp op) {
70     clip().opPath(path, transform(), mInitialBounds, (SkRegion::Op)op, true);
71 }
72 
internalRestore()73 bool 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 
getClipBounds() const96 SkRect 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 
quickRejectRect(float left,float top,float right,float bottom) const114 bool CanvasStateHelper::quickRejectRect(float left, float top, float right, float bottom) const {
115     // TODO: Implement
116     return false;
117 }
118 
quickRejectPath(const SkPath & path) const119 bool CanvasStateHelper::quickRejectPath(const SkPath& path) const {
120     // TODO: Implement
121     return false;
122 }
123 
124 } // namespace android::uirenderer
125