• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(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     if (preStaticStatus_ != isStaticCached_) {
203         RS_TRACE_NAME_FMT("RSEffectRenderNode[%llu]::CheckFilterCacheNeedForceClear"
204             " preStaticStatus_:%d, isStaticCached:%d", GetId(), preStaticStatus_, IsStaticCached());
205         RS_LOGI("RSEffectRenderNode[%{public}lld]::CheckFilterCacheNeedForceClear foldStatusChanged_:%{public}d,"
206             " preRotationStatus_:%{public}d, isRotationChanged_:%{public}d,"
207             " preStaticStatus_:%{public}d, isStaticCached:%{public}d", static_cast<long long>(GetId()),
208             foldStatusChanged_, preRotationStatus_, isRotationChanged_, preStaticStatus_, IsStaticCached());
209     }
210     // case 3: the state of freeze changed to false, and the last cache maybe wrong.
211     bool res = foldStatusChanged_ || (preRotationStatus_ != isRotationChanged_) ||
212         (preStaticStatus_ != isStaticCached_ && isStaticCached_ == false);
213     preStaticStatus_ = IsStaticCached();
214     return res;
215 }
216 
SetRotationChanged(bool isRotationChanged)217 void RSEffectRenderNode::SetRotationChanged(bool isRotationChanged)
218 {
219     preRotationStatus_ = isRotationChanged_;
220     isRotationChanged_ = isRotationChanged;
221 }
222 
GetRotationChanged() const223 bool RSEffectRenderNode::GetRotationChanged() const
224 {
225     return isRotationChanged_;
226 }
227 
SetCurrentAttachedScreenId(uint64_t screenId)228 void RSEffectRenderNode::SetCurrentAttachedScreenId(uint64_t screenId)
229 {
230     currentAttachedScreenId_ = screenId;
231 }
232 
GetCurrentAttachedScreenId() const233 uint64_t RSEffectRenderNode::GetCurrentAttachedScreenId() const
234 {
235     return currentAttachedScreenId_;
236 }
237 
SetFoldStatusChanged(bool foldStatusChanged)238 void RSEffectRenderNode::SetFoldStatusChanged(bool foldStatusChanged)
239 {
240     foldStatusChanged_ = foldStatusChanged;
241 }
242 
InitRenderParams()243 void RSEffectRenderNode::InitRenderParams()
244 {
245 #ifdef RS_ENABLE_GPU
246     stagingRenderParams_ = std::make_unique<RSEffectRenderParams>(GetId());
247     DrawableV2::RSRenderNodeDrawableAdapter::OnGenerate(shared_from_this());
248     if (renderDrawable_ == nullptr) {
249         RS_LOGE("RSEffectRenderNode::InitRenderParams failed");
250         return;
251     }
252 #endif
253 }
254 
MarkFilterHasEffectChildren()255 void RSEffectRenderNode::MarkFilterHasEffectChildren()
256 {
257 #ifdef RS_ENABLE_GPU
258     if (GetRenderProperties().GetBackgroundFilter() == nullptr) {
259         return;
260     }
261     auto effectParams = static_cast<RSEffectRenderParams*>(stagingRenderParams_.get());
262     if (effectParams == nullptr) {
263         return;
264     }
265     effectParams->SetHasEffectChildren(ChildHasVisibleEffect());
266 #if (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
267     if (!RSProperties::filterCacheEnabled_) {
268         UpdateDirtySlotsAndPendingNodes(RSDrawableSlot::BACKGROUND_FILTER);
269     }
270 #endif
271 #endif
272 }
273 
OnFilterCacheStateChanged()274 void RSEffectRenderNode::OnFilterCacheStateChanged()
275 {
276 #ifdef RS_ENABLE_GPU
277     auto filterDrawable = GetFilterDrawable(false);
278     auto effectParams = static_cast<RSEffectRenderParams*>(stagingRenderParams_.get());
279     if (filterDrawable == nullptr || effectParams == nullptr) {
280         return;
281     }
282     effectParams->SetCacheValid(filterDrawable->IsFilterCacheValid());
283 #endif
284 }
285 
EffectNodeShouldPaint() const286 bool RSEffectRenderNode::EffectNodeShouldPaint() const
287 {
288     return ChildHasVisibleEffect();
289 }
290 
FirstFrameHasEffectChildren() const291 bool RSEffectRenderNode::FirstFrameHasEffectChildren() const
292 {
293     RS_OPTIONAL_TRACE_NAME_FMT("RSEffectRenderNode[%llu]::FirstFrameHasEffectChildren lastHasVisibleEffect:%d,"
294         "hasVisibleEffect:%d", GetId(), lastFrameHasVisibleEffect_, ChildHasVisibleEffect());
295     return GetRenderProperties().GetBackgroundFilter() != nullptr &&
296         !lastFrameHasVisibleEffect_ && ChildHasVisibleEffect();
297 }
298 
FirstFrameHasNoEffectChildren() const299 bool RSEffectRenderNode::FirstFrameHasNoEffectChildren() const
300 {
301     return GetRenderProperties().GetBackgroundFilter() != nullptr &&
302         !ChildHasVisibleEffect() && lastFrameHasVisibleEffect_;
303 }
304 
MarkClearFilterCacheIfEffectChildrenChanged()305 void RSEffectRenderNode::MarkClearFilterCacheIfEffectChildrenChanged()
306 {
307 #ifdef RS_ENABLE_GPU
308     // the first frame node has no effect child, it should not be painted and filter cache need to be cleared.
309     auto filterDrawable = GetFilterDrawable(false);
310     if (filterDrawable == nullptr || filterDrawable->IsForceClearFilterCache()) {
311         return;
312     }
313     if (!FirstFrameHasEffectChildren() && !FirstFrameHasNoEffectChildren()) {
314         return;
315     }
316     filterDrawable->MarkFilterForceClearCache();
317     // the first frame node has no effect child, force use cache should be cancled.
318     if (filterDrawable->IsForceUseFilterCache()) {
319         filterDrawable->MarkFilterForceUseCache(false);
320     }
321 #endif
322 }
323 
SetEffectIntersectWithDRM(bool intersect)324 void RSEffectRenderNode::SetEffectIntersectWithDRM(bool intersect)
325 {
326     auto effectParams = static_cast<RSEffectRenderParams*>(stagingRenderParams_.get());
327     if (effectParams == nullptr) {
328         return;
329     }
330     effectParams->SetEffectIntersectWithDRM(intersect);
331 }
332 
SetDarkColorMode(bool isDark)333 void RSEffectRenderNode::SetDarkColorMode(bool isDark)
334 {
335     auto effectParams = static_cast<RSEffectRenderParams*>(stagingRenderParams_.get());
336     if (effectParams == nullptr) {
337         return;
338     }
339     effectParams->SetDarkColorMode(isDark);
340 }
341 } // namespace Rosen
342 } // namespace OHOS
343