1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "pipeline/rs_effect_render_node.h"
17
18 #include "common/rs_obj_abs_geometry.h"
19 #include "common/rs_optional_trace.h"
20 #include "memory/rs_memory_track.h"
21 #include "params/rs_effect_render_params.h"
22 #include "platform/common/rs_log.h"
23 #include "platform/common/rs_system_properties.h"
24 #include "property/rs_properties_painter.h"
25 #include "visitor/rs_node_visitor.h"
26
27 namespace OHOS {
28 namespace Rosen {
29
RSEffectRenderNode(NodeId id,const std::weak_ptr<RSContext> & context,bool isTextureExportNode)30 RSEffectRenderNode::RSEffectRenderNode(NodeId id, const std::weak_ptr<RSContext>& context, bool isTextureExportNode)
31 : RSRenderNode(id, context, isTextureExportNode)
32 {
33 #ifndef ROSEN_ARKUI_X
34 MemoryInfo info = { sizeof(*this), ExtractPid(id), id, MEMORY_TYPE::MEM_RENDER_NODE };
35 MemoryTrack::Instance().AddNodeRecord(id, info);
36 #endif
37 MemorySnapshot::Instance().AddCpuMemory(ExtractPid(id), sizeof(*this));
38 }
39
~RSEffectRenderNode()40 RSEffectRenderNode::~RSEffectRenderNode()
41 {
42 #ifndef ROSEN_ARKUI_X
43 MemoryTrack::Instance().RemoveNodeRecord(GetId());
44 #endif
45 MemorySnapshot::Instance().RemoveCpuMemory(ExtractPid(GetId()), sizeof(*this));
46 }
47
Prepare(const std::shared_ptr<RSNodeVisitor> & visitor)48 void RSEffectRenderNode::Prepare(const std::shared_ptr<RSNodeVisitor>& visitor)
49 {
50 if (!visitor) {
51 return;
52 }
53 ApplyModifiers();
54 visitor->PrepareEffectRenderNode(*this);
55 }
56
QuickPrepare(const std::shared_ptr<RSNodeVisitor> & visitor)57 void RSEffectRenderNode::QuickPrepare(const std::shared_ptr<RSNodeVisitor>& visitor)
58 {
59 if (!visitor) {
60 return;
61 }
62 ApplyModifiers();
63 visitor->QuickPrepareEffectRenderNode(*this);
64 }
65
Process(const std::shared_ptr<RSNodeVisitor> & visitor)66 void RSEffectRenderNode::Process(const std::shared_ptr<RSNodeVisitor>& visitor)
67 {
68 if (!visitor) {
69 return;
70 }
71 RSRenderNode::RenderTraceDebug();
72 visitor->ProcessEffectRenderNode(*this);
73 }
74
ProcessRenderBeforeChildren(RSPaintFilterCanvas & canvas)75 void RSEffectRenderNode::ProcessRenderBeforeChildren(RSPaintFilterCanvas& canvas)
76 {
77 RSRenderNode::ProcessTransitionBeforeChildren(canvas);
78 auto& properties = GetRenderProperties();
79 // Disable effect region if either of the following conditions is met:
80 // 1. No child with useEffect(true) (needFilter_ == false)
81 // 2. Background filter is null
82 // 3. Canvas is offscreen
83 if (!properties.GetHaveEffectRegion() || properties.GetBackgroundFilter() == nullptr ||
84 !(RSSystemProperties::GetEffectMergeEnabled() && RSFilterCacheManager::isCCMEffectMergeEnable_) ||
85 canvas.GetCacheType() == RSPaintFilterCanvas::CacheType::OFFSCREEN) {
86 canvas.SetEffectData(nullptr);
87 return;
88 }
89
90 if (properties.GetBgImage() == nullptr) {
91 // EffectRenderNode w/o background image, use snapshot as underlay image
92 RSPropertiesPainter::DrawBackgroundEffect(properties, canvas);
93 } else {
94 // EffectRenderNode w/ background image, use bg image as underlay image
95 RSPropertiesPainter::DrawBackgroundImageAsEffect(properties, canvas);
96 }
97 }
98
GetFilterRect() const99 RectI RSEffectRenderNode::GetFilterRect() const
100 {
101 if (!ChildHasVisibleEffect()) {
102 return {};
103 }
104 return RSRenderNode::GetFilterRect();
105 }
106
SetEffectRegion(const std::optional<Drawing::RectI> & effectRegion)107 void RSEffectRenderNode::SetEffectRegion(const std::optional<Drawing::RectI>& effectRegion)
108 {
109 if (!effectRegion.has_value() || !effectRegion->IsValid()) {
110 ROSEN_LOGD("RSEffectRenderNode::SetEffectRegion: no effect region.");
111 GetMutableRenderProperties().SetHaveEffectRegion(false);
112 return;
113 }
114
115 const auto& properties = GetRenderProperties();
116 const auto& absRect = properties.GetBoundsGeometry()->GetAbsRect();
117 Drawing::RectI effectRect = effectRegion.value();
118 if (!effectRect.Intersect(
119 Drawing::RectI(absRect.GetLeft(), absRect.GetTop(), absRect.GetRight(), absRect.GetBottom()))) {
120 ROSEN_LOGD("RSEffectRenderNode::SetEffectRegion: no valid effect region.");
121 GetMutableRenderProperties().SetHaveEffectRegion(false);
122 return;
123 }
124 GetMutableRenderProperties().SetHaveEffectRegion(true);
125 }
126
CheckBlurFilterCacheNeedForceClearOrSave(bool rotationChanged,bool rotationStatusChanged)127 void RSEffectRenderNode::CheckBlurFilterCacheNeedForceClearOrSave(bool rotationChanged, bool rotationStatusChanged)
128 {
129 #ifdef RS_ENABLE_GPU
130 if (GetRenderProperties().GetBackgroundFilter() == nullptr) {
131 return;
132 }
133 auto filterDrawable = GetFilterDrawable(false);
134 if (filterDrawable == nullptr) {
135 return;
136 }
137 filterDrawable->MarkEffectNode();
138 RSRenderNode::CheckBlurFilterCacheNeedForceClearOrSave(rotationChanged, rotationStatusChanged);
139 if (IsForceClearOrUseFilterCache(filterDrawable)) {
140 return;
141 }
142 if (CheckFilterCacheNeedForceClear()) {
143 filterDrawable->MarkFilterForceClearCache();
144 } else if (CheckFilterCacheNeedForceSave()) {
145 filterDrawable->MarkFilterForceUseCache();
146 }
147 #endif
148 }
149
UpdateFilterCacheWithSelfDirty()150 void RSEffectRenderNode::UpdateFilterCacheWithSelfDirty()
151 {
152 #ifdef RS_ENABLE_GPU
153 #if defined(NEW_SKIA) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
154 if (!RSProperties::filterCacheEnabled_) {
155 ROSEN_LOGE("RSEffectRenderNode::UpdateFilterCacheManagerWithCacheRegion filter cache is disabled.");
156 return;
157 }
158 #endif
159 auto filterDrawable = GetFilterDrawable(false);
160 if (filterDrawable == nullptr || IsForceClearOrUseFilterCache(filterDrawable)) {
161 return;
162 }
163 RS_OPTIONAL_TRACE_NAME_FMT("RSEffectRenderNode[%llu]::UpdateFilterCacheWithSelfDirty lastRect:%s, currRegion:%s",
164 GetId(), GetFilterCachedRegion().ToString().c_str(), filterRegion_.ToString().c_str());
165 if (filterRegion_ == GetFilterCachedRegion()) {
166 return;
167 }
168 // effect render node only support background filter
169 MarkFilterStatusChanged(false, true);
170 #endif
171 }
172
173 #ifdef RS_ENABLE_GPU
MarkFilterCacheFlags(std::shared_ptr<DrawableV2::RSFilterDrawable> & filterDrawable,RSDirtyRegionManager & dirtyManager,bool needRequestNextVsync)174 void RSEffectRenderNode::MarkFilterCacheFlags(std::shared_ptr<DrawableV2::RSFilterDrawable>& filterDrawable,
175 RSDirtyRegionManager& dirtyManager, bool needRequestNextVsync)
176 {
177 lastFrameHasVisibleEffect_ = ChildHasVisibleEffect();
178 if (IsForceClearOrUseFilterCache(filterDrawable)) {
179 return;
180 }
181 // use for skip-frame when screen rotation
182 if (isRotationChanged_) {
183 filterDrawable->MarkRotationChanged();
184 }
185 RSRenderNode::MarkFilterCacheFlags(filterDrawable, dirtyManager, needRequestNextVsync);
186 }
187 #endif
188
CheckFilterCacheNeedForceSave()189 bool RSEffectRenderNode::CheckFilterCacheNeedForceSave()
190 {
191 RS_OPTIONAL_TRACE_NAME_FMT("RSEffectRenderNode[%llu]::CheckFilterCacheNeedForceSave"
192 " isBackgroundImage:%d, isRotationChanged_:%d, IsStaticCached():%d",
193 GetId(), GetRenderProperties().GetBgImage() != nullptr, isRotationChanged_, IsStaticCached());
194 return GetRenderProperties().GetBgImage() != nullptr || (IsStaticCached() && !isRotationChanged_);
195 }
196
CheckFilterCacheNeedForceClear()197 bool RSEffectRenderNode::CheckFilterCacheNeedForceClear()
198 {
199 RS_OPTIONAL_TRACE_NAME_FMT("RSEffectRenderNode[%llu]::CheckFilterCacheNeedForceClear foldStatusChanged_:%d,"
200 " preRotationStatus_:%d, isRotationChanged_:%d, preStaticStatus_:%d, isStaticCached:%d",
201 GetId(), foldStatusChanged_, preRotationStatus_, isRotationChanged_, preStaticStatus_, IsStaticCached());
202 RS_LOGD("RSEffectRenderNode[%{public}lld]::CheckFilterCacheNeedForceClear foldStatusChanged_:%{public}d,"
203 " preRotationStatus_:%{public}d, isRotationChanged_:%{public}d,"
204 " preStaticStatus_:%{public}d, isStaticCached:%{public}d", static_cast<long long>(GetId()),
205 foldStatusChanged_, preRotationStatus_, isRotationChanged_, preStaticStatus_, IsStaticCached());
206 // case 3: the state of freeze changed to false, and the last cache maybe wrong.
207 bool res = foldStatusChanged_ || (preRotationStatus_ != isRotationChanged_) ||
208 (preStaticStatus_ != isStaticCached_ && isStaticCached_ == false);
209 preStaticStatus_ = IsStaticCached();
210 return res;
211 }
212
SetRotationChanged(bool isRotationChanged)213 void RSEffectRenderNode::SetRotationChanged(bool isRotationChanged)
214 {
215 preRotationStatus_ = isRotationChanged_;
216 isRotationChanged_ = isRotationChanged;
217 }
218
GetRotationChanged() const219 bool RSEffectRenderNode::GetRotationChanged() const
220 {
221 return isRotationChanged_;
222 }
223
SetCurrentAttachedScreenId(uint64_t screenId)224 void RSEffectRenderNode::SetCurrentAttachedScreenId(uint64_t screenId)
225 {
226 currentAttachedScreenId_ = screenId;
227 }
228
GetCurrentAttachedScreenId() const229 uint64_t RSEffectRenderNode::GetCurrentAttachedScreenId() const
230 {
231 return currentAttachedScreenId_;
232 }
233
SetFoldStatusChanged(bool foldStatusChanged)234 void RSEffectRenderNode::SetFoldStatusChanged(bool foldStatusChanged)
235 {
236 foldStatusChanged_ = foldStatusChanged;
237 }
238
InitRenderParams()239 void RSEffectRenderNode::InitRenderParams()
240 {
241 #ifdef RS_ENABLE_GPU
242 stagingRenderParams_ = std::make_unique<RSEffectRenderParams>(GetId());
243 DrawableV2::RSRenderNodeDrawableAdapter::OnGenerate(shared_from_this());
244 if (renderDrawable_ == nullptr) {
245 RS_LOGE("RSEffectRenderNode::InitRenderParams failed");
246 return;
247 }
248 #endif
249 }
250
MarkFilterHasEffectChildren()251 void RSEffectRenderNode::MarkFilterHasEffectChildren()
252 {
253 #ifdef RS_ENABLE_GPU
254 if (GetRenderProperties().GetBackgroundFilter() == nullptr) {
255 return;
256 }
257 auto effectParams = static_cast<RSEffectRenderParams*>(stagingRenderParams_.get());
258 if (effectParams == nullptr) {
259 return;
260 }
261 effectParams->SetHasEffectChildren(ChildHasVisibleEffect());
262 #if defined(NEW_SKIA) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
263 if (!RSProperties::filterCacheEnabled_) {
264 UpdateDirtySlotsAndPendingNodes(RSDrawableSlot::BACKGROUND_FILTER);
265 }
266 #endif
267 #endif
268 }
269
OnFilterCacheStateChanged()270 void RSEffectRenderNode::OnFilterCacheStateChanged()
271 {
272 #ifdef RS_ENABLE_GPU
273 auto filterDrawable = GetFilterDrawable(false);
274 auto effectParams = static_cast<RSEffectRenderParams*>(stagingRenderParams_.get());
275 if (filterDrawable == nullptr || effectParams == nullptr) {
276 return;
277 }
278 effectParams->SetCacheValid(filterDrawable->IsFilterCacheValid());
279 #endif
280 }
281
EffectNodeShouldPaint() const282 bool RSEffectRenderNode::EffectNodeShouldPaint() const
283 {
284 return ChildHasVisibleEffect();
285 }
286
FirstFrameHasEffectChildren() const287 bool RSEffectRenderNode::FirstFrameHasEffectChildren() const
288 {
289 RS_OPTIONAL_TRACE_NAME_FMT("RSEffectRenderNode[%llu]::FirstFrameHasEffectChildren lastHasVisibleEffect:%d,"
290 "hasVisibleEffect:%d", GetId(), lastFrameHasVisibleEffect_, ChildHasVisibleEffect());
291 return GetRenderProperties().GetBackgroundFilter() != nullptr &&
292 !lastFrameHasVisibleEffect_ && ChildHasVisibleEffect();
293 }
294
FirstFrameHasNoEffectChildren() const295 bool RSEffectRenderNode::FirstFrameHasNoEffectChildren() const
296 {
297 return GetRenderProperties().GetBackgroundFilter() != nullptr &&
298 !ChildHasVisibleEffect() && lastFrameHasVisibleEffect_;
299 }
300
MarkClearFilterCacheIfEffectChildrenChanged()301 void RSEffectRenderNode::MarkClearFilterCacheIfEffectChildrenChanged()
302 {
303 #ifdef RS_ENABLE_GPU
304 // the first frame node has no effect child, it should not be painted and filter cache need to be cleared.
305 auto filterDrawable = GetFilterDrawable(false);
306 if (filterDrawable == nullptr || filterDrawable->IsForceClearFilterCache()) {
307 return;
308 }
309 if (!FirstFrameHasEffectChildren() && !FirstFrameHasNoEffectChildren()) {
310 return;
311 }
312 filterDrawable->MarkFilterForceClearCache();
313 // the first frame node has no effect child, force use cache should be cancled.
314 if (filterDrawable->IsForceUseFilterCache()) {
315 filterDrawable->MarkFilterForceUseCache(false);
316 }
317 #endif
318 }
319 } // namespace Rosen
320 } // namespace OHOS
321