1 /*
2  * Copyright (c) 2024 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 "params/rs_render_params.h"
17 
18 #include <string>
19 
20 #include "params/rs_surface_render_params.h"
21 #include "pipeline/rs_render_node.h"
22 #include "property/rs_properties.h"
23 #include "property/rs_properties_painter.h"
24 #include "screen_manager/rs_screen_info.h"
25 namespace OHOS::Rosen {
26 namespace {
27 thread_local Drawing::Matrix parentSurfaceMatrix_;
28 }
SetDirtyType(RSRenderParamsDirtyType dirtyType)29 void RSRenderParams::SetDirtyType(RSRenderParamsDirtyType dirtyType)
30 {
31     dirtyType_.set(dirtyType);
32 }
33 
SetAlpha(float alpha)34 void RSRenderParams::SetAlpha(float alpha)
35 {
36     if (alpha_ == alpha) {
37         return;
38     }
39     alpha_ = alpha;
40     needSync_ = true;
41 }
GetAlpha() const42 float RSRenderParams::GetAlpha() const
43 {
44     return alpha_;
45 }
46 
SetAlphaOffScreen(bool alphaOffScreen)47 void RSRenderParams::SetAlphaOffScreen(bool alphaOffScreen)
48 {
49     if (alphaOffScreen_ == alphaOffScreen) {
50         return;
51     }
52     alphaOffScreen_ = alphaOffScreen;
53     needSync_ = true;
54 }
55 
GetAlphaOffScreen() const56 bool RSRenderParams::GetAlphaOffScreen() const
57 {
58     return alphaOffScreen_;
59 }
60 
SetMatrix(const Drawing::Matrix & matrix)61 void RSRenderParams::SetMatrix(const Drawing::Matrix& matrix)
62 {
63     matrix_ = matrix;
64     needSync_ = true;
65     dirtyType_.set(RSRenderParamsDirtyType::MATRIX_DIRTY);
66 }
GetMatrix() const67 const Drawing::Matrix& RSRenderParams::GetMatrix() const
68 {
69     return matrix_;
70 }
71 
ApplyAlphaAndMatrixToCanvas(RSPaintFilterCanvas & canvas,bool applyMatrix) const72 void RSRenderParams::ApplyAlphaAndMatrixToCanvas(RSPaintFilterCanvas& canvas, bool applyMatrix) const
73 {
74     if (UNLIKELY(HasSandBox())) {
75         if (applyMatrix) {
76             canvas.SetMatrix(parentSurfaceMatrix_);
77             canvas.ConcatMatrix(matrix_);
78         }
79         canvas.SetAlpha(alpha_);
80     } else {
81         if (applyMatrix) {
82             canvas.ConcatMatrix(matrix_);
83         }
84         if (alpha_ < 1.0f && (drawingCacheType_ == RSDrawingCacheType::FORCED_CACHE || alphaOffScreen_)) {
85             auto rect = RSPropertiesPainter::Rect2DrawingRect(GetLocalDrawRect());
86             Drawing::Brush brush;
87             brush.SetAlpha(static_cast<uint32_t>(std::clamp(alpha_, 0.f, 1.f) * UINT8_MAX));
88             Drawing::SaveLayerOps slr(&rect, &brush);
89             canvas.SaveLayer(slr);
90         } else {
91             canvas.MultiplyAlpha(alpha_);
92         }
93     }
94 }
95 
SetBoundsRect(const Drawing::RectF & boundsRect)96 void RSRenderParams::SetBoundsRect(const Drawing::RectF& boundsRect)
97 {
98     if (boundsRect_ == boundsRect) {
99         return;
100     }
101     boundsRect_ = boundsRect;
102     needSync_ = true;
103 }
104 
GetBounds() const105 const Drawing::Rect& RSRenderParams::GetBounds() const
106 {
107     return boundsRect_;
108 }
109 
SetFrameRect(const Drawing::RectF & frameRect)110 void RSRenderParams::SetFrameRect(const Drawing::RectF& frameRect)
111 {
112     if (frameRect_ == frameRect) {
113         return;
114     }
115 
116     frameRect_ = frameRect;
117     needSync_ = true;
118 }
119 
GetFrameRect() const120 const Drawing::Rect& RSRenderParams::GetFrameRect() const
121 {
122     return frameRect_;
123 }
124 
SetLocalDrawRect(const RectF & localDrawRect)125 bool RSRenderParams::SetLocalDrawRect(const RectF& localDrawRect)
126 {
127     if (localDrawRect_.IsNearEqual(localDrawRect)) {
128         return false;
129     }
130     localDrawRect_ = localDrawRect;
131     needSync_ = true;
132     return true;
133 }
134 
GetLocalDrawRect() const135 const RectF& RSRenderParams::GetLocalDrawRect() const
136 {
137     return localDrawRect_;
138 }
139 
SetHasSandBox(bool hasSandbox)140 void RSRenderParams::SetHasSandBox(bool hasSandbox)
141 {
142     if (hasSandBox_ == hasSandbox) {
143         return;
144     }
145     hasSandBox_ = hasSandbox;
146     needSync_ = true;
147 }
148 
HasSandBox() const149 bool RSRenderParams::HasSandBox() const
150 {
151     return hasSandBox_;
152 }
153 
SetShouldPaint(bool shouldPaint)154 void RSRenderParams::SetShouldPaint(bool shouldPaint)
155 {
156     if (shouldPaint_ == shouldPaint) {
157         return;
158     }
159     shouldPaint_ = shouldPaint;
160     needSync_ = true;
161 }
162 
SetContentEmpty(bool empty)163 void RSRenderParams::SetContentEmpty(bool empty)
164 {
165     if (contentEmpty_ == empty) {
166         return;
167     }
168     contentEmpty_ = empty;
169     needSync_ = true;
170 }
171 
GetShouldPaint() const172 bool RSRenderParams::GetShouldPaint() const
173 {
174     return shouldPaint_ && !contentEmpty_;
175 }
176 
SetChildHasVisibleFilter(bool val)177 void RSRenderParams::SetChildHasVisibleFilter(bool val)
178 {
179     if (childHasVisibleFilter_ == val) {
180         return;
181     }
182     childHasVisibleFilter_ = val;
183     needSync_ = true;
184 }
185 
ChildHasVisibleFilter() const186 bool RSRenderParams::ChildHasVisibleFilter() const
187 {
188     return childHasVisibleFilter_;
189 }
190 
SetChildHasVisibleEffect(bool val)191 void RSRenderParams::SetChildHasVisibleEffect(bool val)
192 {
193     if (childHasVisibleEffect_ == val) {
194         return;
195     }
196     childHasVisibleEffect_ = val;
197     needSync_ = true;
198 }
199 
ChildHasVisibleEffect() const200 bool RSRenderParams::ChildHasVisibleEffect() const
201 {
202     return childHasVisibleEffect_;
203 }
204 
SetCacheSize(Vector2f size)205 void RSRenderParams::SetCacheSize(Vector2f size)
206 {
207     if (cacheSize_ == size) {
208         return;
209     }
210     cacheSize_ = size;
211     needSync_ = true;
212 }
213 
SetDrawingCacheChanged(bool isChanged,bool lastFrameSynced)214 void RSRenderParams::SetDrawingCacheChanged(bool isChanged, bool lastFrameSynced)
215 {
216     if (lastFrameSynced) {
217         if (isDrawingCacheChanged_) { // force sync if cache changed
218             needSync_ = true;
219         }
220         if (isDrawingCacheChanged_ == isChanged) {
221             return;
222         }
223         isDrawingCacheChanged_ = isChanged;
224         needSync_ = true;
225     } else {
226         needSync_ = needSync_ || (isDrawingCacheChanged_ || (isDrawingCacheChanged_ != isChanged));
227         isDrawingCacheChanged_ = isDrawingCacheChanged_ || isChanged;
228     }
229 }
230 
GetDrawingCacheChanged() const231 bool RSRenderParams::GetDrawingCacheChanged() const
232 {
233     return isDrawingCacheChanged_;
234 }
235 
SetDrawingCacheType(RSDrawingCacheType cacheType)236 void RSRenderParams::SetDrawingCacheType(RSDrawingCacheType cacheType)
237 {
238     if (drawingCacheType_ == cacheType) {
239         return;
240     }
241     dirtyType_.set(RSRenderParamsDirtyType::DRAWING_CACHE_TYPE_DIRTY);
242     drawingCacheType_ = cacheType;
243     needSync_ = true;
244 }
245 
GetDrawingCacheType() const246 RSDrawingCacheType RSRenderParams::GetDrawingCacheType() const
247 {
248     return drawingCacheType_;
249 }
250 
SetDrawingCacheIncludeProperty(bool includeProperty)251 void RSRenderParams::SetDrawingCacheIncludeProperty(bool includeProperty)
252 {
253     if (drawingCacheIncludeProperty_ == includeProperty) {
254         return;
255     }
256     drawingCacheIncludeProperty_ = includeProperty;
257     needSync_ = true;
258 }
259 
GetDrawingCacheIncludeProperty() const260 bool RSRenderParams::GetDrawingCacheIncludeProperty() const
261 {
262     return drawingCacheIncludeProperty_;
263 }
264 
SetRSFreezeFlag(bool freezeFlag)265 void RSRenderParams::SetRSFreezeFlag(bool freezeFlag)
266 {
267     if (freezeFlag_ == freezeFlag) {
268         return;
269     }
270     freezeFlag_ = freezeFlag;
271     needSync_ = true;
272 }
273 
GetRSFreezeFlag() const274 bool RSRenderParams::GetRSFreezeFlag() const
275 {
276     return freezeFlag_;
277 }
278 
OpincUpdateRootFlag(bool suggestFlag)279 void RSRenderParams::OpincUpdateRootFlag(bool suggestFlag)
280 {
281     if (isOpincRootFlag_ == suggestFlag) {
282         return;
283     }
284     isOpincRootFlag_ = suggestFlag;
285     needSync_ = true;
286 }
287 
OpincGetRootFlag() const288 bool RSRenderParams::OpincGetRootFlag() const
289 {
290     return isOpincRootFlag_;
291 }
292 
OpincSetCacheChangeFlag(bool state,bool lastFrameSynced)293 void RSRenderParams::OpincSetCacheChangeFlag(bool state,  bool lastFrameSynced)
294 {
295     if (lastFrameSynced) {
296         isOpincStateChanged_ = state;
297         needSync_ = true;
298     } else {
299         needSync_ = needSync_ || (isOpincStateChanged_ || (isOpincStateChanged_ != state));
300         isOpincStateChanged_ = isOpincStateChanged_ || state;
301     }
302 }
303 
OpincGetCacheChangeState()304 bool RSRenderParams::OpincGetCacheChangeState()
305 {
306     bool state = isOpincStateChanged_;
307     isOpincStateChanged_ = false;
308     return state;
309 }
310 
SetShadowRect(Drawing::Rect rect)311 void RSRenderParams::SetShadowRect(Drawing::Rect rect)
312 {
313     if (shadowRect_ == rect) {
314         return;
315     }
316     shadowRect_ = rect;
317     needSync_ = true;
318 }
319 
GetShadowRect() const320 Drawing::Rect RSRenderParams::GetShadowRect() const
321 {
322     return shadowRect_;
323 }
324 
SetDirtyRegionInfoForDFX(DirtyRegionInfoForDFX dirtyRegionInfo)325 void RSRenderParams::SetDirtyRegionInfoForDFX(DirtyRegionInfoForDFX dirtyRegionInfo)
326 {
327     if (dirtyRegionInfoForDFX_ == dirtyRegionInfo) {
328         return;
329     }
330     dirtyRegionInfoForDFX_ = dirtyRegionInfo;
331     needSync_ = true;
332 }
333 
GetDirtyRegionInfoForDFX() const334 DirtyRegionInfoForDFX RSRenderParams::GetDirtyRegionInfoForDFX() const
335 {
336     return dirtyRegionInfoForDFX_;
337 }
338 
SetNeedSync(bool needSync)339 void RSRenderParams::SetNeedSync(bool needSync)
340 {
341     needSync_ = needSync;
342 }
343 
SetFrameGravity(Gravity gravity)344 void RSRenderParams::SetFrameGravity(Gravity gravity)
345 {
346     if (frameGravity_ == gravity) {
347         return;
348     }
349     frameGravity_ = gravity;
350     needSync_ = true;
351 }
352 
SetNeedFilter(bool needFilter)353 void RSRenderParams::SetNeedFilter(bool needFilter)
354 {
355     if (needFilter_ == needFilter) {
356         return;
357     }
358     needFilter_ = needFilter;
359     needSync_ = true;
360 }
361 
SetNodeType(RSRenderNodeType type)362 void RSRenderParams::SetNodeType(RSRenderNodeType type)
363 {
364     if (renderNodeType_ == type) {
365         return;
366     }
367     renderNodeType_ = type;
368     needSync_ = true;
369 }
370 
SetEffectNodeShouldPaint(bool effectNodeShouldPaint)371 void RSRenderParams::SetEffectNodeShouldPaint(bool effectNodeShouldPaint)
372 {
373     if (effectNodeShouldPaint_ == effectNodeShouldPaint) {
374         return;
375     }
376     effectNodeShouldPaint_ = effectNodeShouldPaint;
377     needSync_ = true;
378 }
379 
SetHasGlobalCorner(bool hasGlobalCorner)380 void RSRenderParams::SetHasGlobalCorner(bool hasGlobalCorner)
381 {
382     if (hasGlobalCorner_ == hasGlobalCorner) {
383         return;
384     }
385     hasGlobalCorner_ = hasGlobalCorner;
386     needSync_ = true;
387 }
388 
SetHasBlurFilter(bool hasBlurFilter)389 void RSRenderParams::SetHasBlurFilter(bool hasBlurFilter)
390 {
391     if (hasBlurFilter_ == hasBlurFilter) {
392         return;
393     }
394     hasBlurFilter_ = hasBlurFilter;
395     needSync_ = true;
396 }
397 
SetGlobalAlpha(float alpha)398 void RSRenderParams::SetGlobalAlpha(float alpha)
399 {
400     if (ROSEN_EQ(globalAlpha_, alpha)) {
401         return;
402     }
403     globalAlpha_ = alpha;
404     needSync_ = true;
405 }
406 
NeedSync() const407 bool RSRenderParams::NeedSync() const
408 {
409     return needSync_;
410 }
411 
OnCanvasDrawingSurfaceChange(const std::unique_ptr<RSRenderParams> & target)412 void RSRenderParams::OnCanvasDrawingSurfaceChange(const std::unique_ptr<RSRenderParams>& target)
413 {
414     if (!canvasDrawingNodeSurfaceChanged_) {
415         return;
416     }
417     target->canvasDrawingNodeSurfaceChanged_ = true;
418     target->surfaceParams_.width = surfaceParams_.width;
419     target->surfaceParams_.height = surfaceParams_.height;
420     if (GetParamsType() == RSRenderParamsType::RS_PARAM_OWNED_BY_DRAWABLE) {
421         return;
422     }
423     canvasDrawingNodeSurfaceChanged_ = false;
424 }
425 
GetCanvasDrawingSurfaceChanged() const426 bool RSRenderParams::GetCanvasDrawingSurfaceChanged() const
427 {
428     return canvasDrawingNodeSurfaceChanged_;
429 }
430 
SetCanvasDrawingSurfaceChanged(bool changeFlag)431 void RSRenderParams::SetCanvasDrawingSurfaceChanged(bool changeFlag)
432 {
433     if (changeFlag) {
434         needSync_ = true;
435     }
436     canvasDrawingNodeSurfaceChanged_ = changeFlag;
437 }
438 
GetForegroundFilterCache() const439 const std::shared_ptr<RSFilter>& RSRenderParams::GetForegroundFilterCache() const
440 {
441     return foregroundFilterCache_;
442 }
443 
SetForegroundFilterCache(const std::shared_ptr<RSFilter> & foregroundFilterCache)444 void RSRenderParams::SetForegroundFilterCache(const std::shared_ptr<RSFilter>& foregroundFilterCache)
445 {
446     if (foregroundFilterCache_ == foregroundFilterCache) {
447         return;
448     }
449     foregroundFilterCache_ = foregroundFilterCache;
450     needSync_ = true;
451 }
452 
GetCanvasDrawingSurfaceParams()453 RSRenderParams::SurfaceParam RSRenderParams::GetCanvasDrawingSurfaceParams()
454 {
455     return surfaceParams_;
456 }
457 
SetCanvasDrawingSurfaceParams(int width,int height)458 void RSRenderParams::SetCanvasDrawingSurfaceParams(int width, int height)
459 {
460     surfaceParams_.width = width;
461     surfaceParams_.height = height;
462 }
463 
OnSync(const std::unique_ptr<RSRenderParams> & target)464 void RSRenderParams::OnSync(const std::unique_ptr<RSRenderParams>& target)
465 {
466     if (dirtyType_.test(RSRenderParamsDirtyType::MATRIX_DIRTY)) {
467         target->matrix_.Swap(matrix_);
468         dirtyType_.reset(RSRenderParamsDirtyType::MATRIX_DIRTY);
469     }
470     if (dirtyType_.test(RSRenderParamsDirtyType::DRAWING_CACHE_TYPE_DIRTY)) {
471         target->drawingCacheType_ = drawingCacheType_;
472         dirtyType_.reset(RSRenderParamsDirtyType::DRAWING_CACHE_TYPE_DIRTY);
473     }
474     target->alpha_ = alpha_;
475     target->boundsRect_ = boundsRect_;
476     target->frameRect_ = frameRect_;
477     target->shouldPaint_ = shouldPaint_;
478     target->contentEmpty_ = contentEmpty_;
479     target->hasSandBox_ = hasSandBox_;
480     target->localDrawRect_ = localDrawRect_;
481     target->id_ = id_;
482     target->cacheSize_ = cacheSize_;
483     target->frameGravity_ = frameGravity_;
484     target->childHasVisibleFilter_ = childHasVisibleFilter_;
485     target->childHasVisibleEffect_ = childHasVisibleEffect_;
486     // use flag in render param and staging render param to determine if cache should be updated
487     // (flag in render param may be not used because of occlusion skip, so we need to update cache in next frame)
488     target->isDrawingCacheChanged_ = target->isDrawingCacheChanged_ || isDrawingCacheChanged_;
489     target->shadowRect_ = shadowRect_;
490     target->drawingCacheIncludeProperty_ = drawingCacheIncludeProperty_;
491     target->dirtyRegionInfoForDFX_ = dirtyRegionInfoForDFX_;
492     target->alphaOffScreen_ = alphaOffScreen_;
493     target->needFilter_ = needFilter_;
494     target->renderNodeType_ = renderNodeType_;
495     target->globalAlpha_ = globalAlpha_;
496     target->effectNodeShouldPaint_ = effectNodeShouldPaint_;
497     target->hasGlobalCorner_ = hasGlobalCorner_;
498     target->hasBlurFilter_ = hasBlurFilter_;
499     target->foregroundFilterCache_ = foregroundFilterCache_;
500     OnCanvasDrawingSurfaceChange(target);
501     target->isOpincRootFlag_ = isOpincRootFlag_;
502     target->isOpincStateChanged_ = target->isOpincStateChanged_ || isOpincStateChanged_;
503     target->startingWindowFlag_ = startingWindowFlag_;
504     target->freezeFlag_ = freezeFlag_;
505     target->absDrawRect_ = absDrawRect_;
506     target->firstLevelNodeId_ = firstLevelNodeId_;
507     target->uifirstRootNodeId_ = uifirstRootNodeId_;
508     target->absRotation_ = absRotation_;
509     needSync_ = false;
510 }
511 
ToString() const512 std::string RSRenderParams::ToString() const
513 {
514     std::string ret = "RSRenderParams:";
515     ret += RENDER_BASIC_PARAM_TO_STRING(id_);
516     if (alpha_ != 1.0f) {
517         ret += RENDER_BASIC_PARAM_TO_STRING(alpha_);
518     }
519     if (hasSandBox_) {
520         ret += RENDER_BASIC_PARAM_TO_STRING(hasSandBox_);
521     }
522     ret += RENDER_RECT_PARAM_TO_STRING(localDrawRect_);
523     ret += RENDER_BASIC_PARAM_TO_STRING(shouldPaint_);
524     ret += RENDER_BASIC_PARAM_TO_STRING(int(frameGravity_));
525     if (foregroundFilterCache_ != nullptr) {
526         ret += foregroundFilterCache_->GetDescription();
527     }
528     return ret;
529 }
530 
SetParentSurfaceMatrix(const Drawing::Matrix & parentSurfaceMatrix)531 void RSRenderParams::SetParentSurfaceMatrix(const Drawing::Matrix& parentSurfaceMatrix)
532 {
533     parentSurfaceMatrix_ = parentSurfaceMatrix;
534 }
GetParentSurfaceMatrix()535 const Drawing::Matrix& RSRenderParams::GetParentSurfaceMatrix()
536 {
537     return parentSurfaceMatrix_;
538 }
539 
SetFirstLevelNode(NodeId firstLevelNodeId)540 bool RSRenderParams::SetFirstLevelNode(NodeId firstLevelNodeId)
541 {
542     if (firstLevelNodeId_ == firstLevelNodeId) {
543         return false;
544     }
545     firstLevelNodeId_ = firstLevelNodeId;
546     needSync_ = true;
547     return true;
548 }
549 
GetFirstLevelNodeId() const550 NodeId RSRenderParams::GetFirstLevelNodeId() const
551 {
552     return firstLevelNodeId_;
553 }
554 
SetUiFirstRootNode(NodeId uifirstRootNodeId)555 bool RSRenderParams::SetUiFirstRootNode(NodeId uifirstRootNodeId)
556 {
557     if (uifirstRootNodeId_ == uifirstRootNodeId) {
558         return false;
559     }
560     uifirstRootNodeId_ = uifirstRootNodeId;
561     needSync_ = true;
562     return true;
563 }
564 
GetUifirstRootNodeId() const565 NodeId RSRenderParams::GetUifirstRootNodeId() const
566 {
567     return uifirstRootNodeId_;
568 }
569 
570 // overrided surface params
GetLayerInfo() const571 const RSLayerInfo& RSRenderParams::GetLayerInfo() const
572 {
573     static const RSLayerInfo defaultLayerInfo = {};
574     return defaultLayerInfo;
575 }
576 
577 // overrided by displayNode
GetScreenInfo() const578 const ScreenInfo& RSRenderParams::GetScreenInfo() const
579 {
580     static ScreenInfo defalutScreenInfo;
581     return defalutScreenInfo;
582 }
583 
GetTotalMatrix()584 const Drawing::Matrix& RSRenderParams::GetTotalMatrix()
585 {
586     static const Drawing::Matrix defaultMatrix;
587     return defaultMatrix;
588 }
589 // virtual display params
GetMirrorSourceDrawable()590 DrawableV2::RSRenderNodeDrawableAdapter::WeakPtr RSRenderParams::GetMirrorSourceDrawable()
591 {
592     static DrawableV2::RSRenderNodeDrawableAdapter::WeakPtr defaultPtr;
593     return defaultPtr;
594 }
595 } // namespace OHOS::Rosen
596