blob: 529546d5c49c9c6b6fec5b798ceb7bde4c64ca05 [file] [log] [blame]
John Reck013127b2020-10-29 20:53:51 -04001/*
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#pragma once
18
19#include <SkMatrix.h>
20
21#include "CanvasOpTypes.h"
John Reck064650b2021-01-19 21:29:24 -050022#include "CanvasTransform.h"
John Reck013127b2020-10-29 20:53:51 -040023#include "OpBuffer.h"
John Reck064650b2021-01-19 21:29:24 -050024#include "TreeInfo.h"
25#include "private/hwui/WebViewFunctor.h"
26
27#include <functional>
John Reck013127b2020-10-29 20:53:51 -040028
29namespace android::uirenderer {
30
John Reck064650b2021-01-19 21:29:24 -050031class RenderNode;
32
John Reck013127b2020-10-29 20:53:51 -040033template <CanvasOpType T>
34struct CanvasOp;
35
36template <CanvasOpType T>
37class CanvasOpContainer {
38private:
39 BE_OPBUFFERS_FRIEND();
40
41 OpBufferItemHeader<CanvasOpType> header;
42 // TODO: Figure out some magic to make this not be here when it's identity (or not used)
43 SkMatrix mTransform;
44 CanvasOp<T> mImpl;
45
46public:
47 CanvasOpContainer(CanvasOp<T>&& impl, const SkMatrix& transform = SkMatrix::I())
48 : mTransform(transform), mImpl(std::move(impl)) {}
49
50 uint32_t size() const { return header.size; }
51 CanvasOpType type() const { return header.type; }
52
53 const SkMatrix& transform() const { return mTransform; }
54
55 CanvasOp<T>* operator->() noexcept { return &mImpl; }
John Reck90b0a1c2020-11-12 12:37:30 -050056 const CanvasOp<T>* operator->() const noexcept { return &mImpl; }
57
58 CanvasOp<T>& op() noexcept { return mImpl; }
59 const CanvasOp<T>& op() const noexcept { return mImpl; }
John Reck013127b2020-10-29 20:53:51 -040060};
61
62extern template class OpBuffer<CanvasOpType, CanvasOpContainer>;
John Reck064650b2021-01-19 21:29:24 -050063class CanvasOpBuffer final : private OpBuffer<CanvasOpType, CanvasOpContainer> {
64private:
65 using SUPER = OpBuffer<CanvasOpType, CanvasOpContainer>;
66
John Reck013127b2020-10-29 20:53:51 -040067public:
John Reck064650b2021-01-19 21:29:24 -050068 // Expose select superclass methods publicly
69 using SUPER::for_each;
70 using SUPER::size;
71 using SUPER::resize;
72
John Reck013127b2020-10-29 20:53:51 -040073 template <CanvasOpType T>
74 void push(CanvasOp<T>&& op) {
75 push_container(CanvasOpContainer<T>(std::move(op)));
76 }
John Reck064650b2021-01-19 21:29:24 -050077
78 template <CanvasOpType T>
79 void push_container(CanvasOpContainer<T>&& op) {
80 if constexpr (IsDrawOp(T)) {
81 mHas.content = true;
82 }
83 if constexpr (T == CanvasOpType::DrawRenderNode) {
84 mHas.children = true;
85 // use staging property, since recording on UI thread
86 if (op->renderNode->stagingProperties().isProjectionReceiver()) {
87 mHas.projectionReceiver = true;
88 }
89 }
90 SUPER::push_container(std::move(op));
91 }
92
93 void clear() {
94 mHas = Contains{};
95 SUPER::clear();
96 }
97
98 void updateChildren(std::function<void(RenderNode*)> updateFn);
99 bool prepareListAndChildren(
100 TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer,
101 std::function<void(RenderNode*, TreeObserver&, TreeInfo&, bool)> childFn);
102 void syncContents(const WebViewSyncData& data);
Huihong Luoec68b7c2021-06-25 21:54:16 -0700103 void onRemovedFromTree();
John Reck064650b2021-01-19 21:29:24 -0500104 void applyColorTransform(ColorTransform transform);
105
106 [[nodiscard]] bool isEmpty() const { return !mHas.content; }
107 [[nodiscard]] bool hasText() const { return mHas.text; }
108 [[nodiscard]] bool hasVectorDrawables() const { return mHas.vectorDrawable; }
109 [[nodiscard]] bool containsProjectionReceiver() const { return mHas.projectionReceiver; }
110 [[nodiscard]] bool hasFunctor() const { return mHas.functor; }
111
112 [[nodiscard]] size_t getUsedSize() const {
113 return size();
114 }
115
116 [[nodiscard]] size_t getAllocatedSize() const {
117 return capacity();
118 }
119
120 void output(std::ostream& output, uint32_t level) const;
121
122private:
123 struct Contains {
124 bool content : 1 = false;
125 bool children : 1 = false;
126 bool projectionReceiver : 1 = false;
127 bool text : 1 = false;
128 bool vectorDrawable : 1 = false;
129 bool functor : 1 = false;
130 };
131 Contains mHas;
John Reck013127b2020-10-29 20:53:51 -0400132};
133
134} // namespace android::uirenderer