John Reck | dc95f10 | 2020-11-16 12:35:02 -0500 | [diff] [blame] | 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 | |
| 23 | CanvasStateHelper::CanvasStateHelper(int width, int height) { |
John Reck | b5eeb18 | 2020-12-09 13:45:39 -0500 | [diff] [blame] | 24 | resetState(width, height); |
| 25 | } |
| 26 | |
| 27 | void CanvasStateHelper::resetState(int width, int height) { |
John Reck | dc95f10 | 2020-11-16 12:35:02 -0500 | [diff] [blame] | 28 | mInitialBounds = SkIRect::MakeWH(width, height); |
John Reck | b5eeb18 | 2020-12-09 13:45:39 -0500 | [diff] [blame] | 29 | mSaveStack.clear(); |
| 30 | mClipStack.clear(); |
| 31 | mTransformStack.clear(); |
John Reck | dc95f10 | 2020-11-16 12:35:02 -0500 | [diff] [blame] | 32 | mSaveStack.emplace_back(); |
| 33 | mClipStack.emplace_back().setRect(mInitialBounds); |
| 34 | mTransformStack.emplace_back(); |
| 35 | mCurrentClipIndex = 0; |
| 36 | mCurrentTransformIndex = 0; |
| 37 | } |
| 38 | |
| 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 | |
Michael Ludwig | 70cf50c2 | 2021-07-21 17:02:39 +0000 | [diff] [blame^] | 57 | // 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 Reck | dc95f10 | 2020-11-16 12:35:02 -0500 | [diff] [blame] | 59 | static_assert(static_cast<int>(SkClipOp::kDifference) == SkRegion::Op::kDifference_Op); |
| 60 | static_assert(static_cast<int>(SkClipOp::kIntersect) == SkRegion::Op::kIntersect_Op); |
John Reck | dc95f10 | 2020-11-16 12:35:02 -0500 | [diff] [blame] | 61 | |
| 62 | void CanvasStateHelper::internalClipRect(const SkRect& rect, SkClipOp op) { |
| 63 | clip().opRect(rect, transform(), mInitialBounds, (SkRegion::Op)op, false); |
| 64 | } |
| 65 | |
| 66 | void CanvasStateHelper::internalClipPath(const SkPath& path, SkClipOp op) { |
| 67 | clip().opPath(path, transform(), mInitialBounds, (SkRegion::Op)op, true); |
| 68 | } |
| 69 | |
| 70 | bool 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 | |
| 93 | SkRect 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 | |
| 111 | bool CanvasStateHelper::quickRejectRect(float left, float top, float right, float bottom) const { |
| 112 | // TODO: Implement |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | bool CanvasStateHelper::quickRejectPath(const SkPath& path) const { |
| 117 | // TODO: Implement |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | } // namespace android::uirenderer |