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 #ifndef RENDER_SERVICE_BASE_DRAWABLE_RS_PROPERTY_DRAWABLE_H
17 #define RENDER_SERVICE_BASE_DRAWABLE_RS_PROPERTY_DRAWABLE_H
18 
19 #include <memory>
20 
21 #include "common/rs_common_def.h"
22 #include "common/rs_rect.h"
23 #include "render/rs_filter.h"
24 #include "recording/draw_cmd_list.h"
25 
26 #include "drawable/rs_drawable.h"
27 
28 namespace OHOS::Rosen {
29 class RSRenderNode;
30 class RSFilter;
31 class RSFilterCacheManager;
32 class ExtendRecordingCanvas;
33 
34 namespace DrawableV2 {
35 class RSPropertyDrawable : public RSDrawable {
36 public:
RSPropertyDrawable(std::shared_ptr<Drawing::DrawCmdList> && drawCmdList)37     RSPropertyDrawable(std::shared_ptr<Drawing::DrawCmdList>&& drawCmdList) : drawCmdList_(std::move(drawCmdList)) {}
38     RSPropertyDrawable() = default;
39     ~RSPropertyDrawable() override = default;
40 
41     void OnSync() override;
42     void OnPurge() override;
43     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
44 
45 protected:
46     bool needSync_ = false;
47     std::shared_ptr<Drawing::DrawCmdList> drawCmdList_;
48     std::shared_ptr<Drawing::DrawCmdList> stagingDrawCmdList_;
49     std::string propertyDescription_;
50     std::string stagingPropertyDescription_;
51 
52     friend class RSPropertyDrawCmdListUpdater;
53 };
54 
55 class RSPropertyDrawCmdListUpdater {
56 public:
57     // not copyable and moveable
58     RSPropertyDrawCmdListUpdater(const RSPropertyDrawCmdListUpdater&) = delete;
59     RSPropertyDrawCmdListUpdater(const RSPropertyDrawCmdListUpdater&&) = delete;
60     RSPropertyDrawCmdListUpdater& operator=(const RSPropertyDrawCmdListUpdater&) = delete;
61     RSPropertyDrawCmdListUpdater& operator=(const RSPropertyDrawCmdListUpdater&&) = delete;
62 
63     explicit RSPropertyDrawCmdListUpdater(int width, int height, RSPropertyDrawable* target);
64     virtual ~RSPropertyDrawCmdListUpdater();
65     const std::unique_ptr<ExtendRecordingCanvas>& GetRecordingCanvas() const;
66 
67 protected:
68     std::unique_ptr<ExtendRecordingCanvas> recordingCanvas_;
69     RSPropertyDrawable* target_;
70 };
71 
72 // ============================================================================
73 // Frame offset
74 class RSFrameOffsetDrawable : public RSPropertyDrawable {
75 public:
RSFrameOffsetDrawable(std::shared_ptr<Drawing::DrawCmdList> && drawCmdList)76     RSFrameOffsetDrawable(std::shared_ptr<Drawing::DrawCmdList>&& drawCmdList)
77         : RSPropertyDrawable(std::move(drawCmdList))
78     {}
79     RSFrameOffsetDrawable() = default;
80     static RSDrawable::Ptr OnGenerate(const RSRenderNode& node);
81     bool OnUpdate(const RSRenderNode& node) override;
82 
83 private:
84 };
85 
86 // ============================================================================
87 // Clip To Bounds and Clip To Frame
88 class RSClipToBoundsDrawable : public RSPropertyDrawable {
89 public:
RSClipToBoundsDrawable(std::shared_ptr<Drawing::DrawCmdList> && drawCmdList)90     RSClipToBoundsDrawable(std::shared_ptr<Drawing::DrawCmdList>&& drawCmdList)
91         : RSPropertyDrawable(std::move(drawCmdList))
92     {}
93     RSClipToBoundsDrawable() = default;
94     static RSDrawable::Ptr OnGenerate(const RSRenderNode& node);
95     bool OnUpdate(const RSRenderNode& node) override;
96 
97 private:
98 };
99 
100 class RSClipToFrameDrawable : public RSPropertyDrawable {
101 public:
RSClipToFrameDrawable(std::shared_ptr<Drawing::DrawCmdList> && drawCmdList)102     RSClipToFrameDrawable(std::shared_ptr<Drawing::DrawCmdList>&& drawCmdList)
103         : RSPropertyDrawable(std::move(drawCmdList))
104     {}
105     RSClipToFrameDrawable() = default;
106     static RSDrawable::Ptr OnGenerate(const RSRenderNode& node);
107     bool OnUpdate(const RSRenderNode& node) override;
108 
109 private:
110 };
111 
112 class RSFilterDrawable : public RSDrawable {
113 public:
114     RSFilterDrawable();
115     ~RSFilterDrawable() override = default;
116 
IsForeground()117     virtual bool IsForeground() const
118     {
119         return false;
120     }
121 
122     // set flags for clearing filter cache
123     // All MarkXXX function should be called from render_service thread
124     void MarkFilterRegionChanged();
125     void MarkFilterRegionInteractWithDirty();
126     void MarkFilterRegionIsLargeArea();
127     void MarkFilterForceUseCache(bool forceUseCache = true);
128     void MarkFilterForceClearCache();
129     void MarkEffectNode();
130     void MarkForceClearCacheWithLastFrame();
131     void MarkRotationChanged();
132     void MarkNodeIsOccluded(bool isOccluded);
133     void MarkNeedClearFilterCache();
134     void MarkBlurIntersectWithDRM(bool IsIntersectWithDRM, bool isDark);
135 
136     bool IsFilterCacheValid() const;
137     bool IsForceClearFilterCache() const;
138     bool IsForceUseFilterCache() const;
139     bool NeedPendingPurge() const;
140     bool IsSkippingFrame() const;
141     bool IsAIBarCacheValid();
142 
143     void OnSync() override;
144     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
145     const RectI GetFilterCachedRegion() const;
146 
147     bool IsFilterCacheValidForOcclusion();
148 
149 private:
150     void ClearFilterCache();
151     void UpdateFlags(FilterCacheType type, bool cacheValid);
152 
153 protected:
154     void RecordFilterInfos(const std::shared_ptr<RSFilter>& rsFilter);
155 
156     bool needSync_ = false;
157     std::shared_ptr<RSFilter> filter_;
158     std::shared_ptr<RSFilter> stagingFilter_;
159 
160     bool needDrawBehindWindow_ = false;
161     bool stagingNeedDrawBehindWindow_ = false;
162 
163     // flags for clearing filter cache
164     // All stagingXXX variables should be read & written by render_service thread
165     bool stagingForceUseCache_ = false;
166     bool stagingForceClearCache_ = false;
167     uint32_t stagingCachedFilterHash_ = 0;
168     bool stagingFilterHashChanged_ = false;
169     bool stagingFilterRegionChanged_ = false;
170     bool stagingFilterInteractWithDirty_ = false;
171     bool stagingRotationChanged_ = false;
172     bool stagingForceClearCacheForLastFrame_ = false;
173     bool stagingIsAIBarInteractWithHWC_ = false;
174     bool stagingIsEffectNode_ = false;
175     bool stagingIntersectWithDRM_ = false;
176     bool stagingIsDarkColorMode_ = false;
177 
178     // clear one of snapshot cache and filtered cache after drawing
179     // All renderXXX variables should be read & written by render_thread or OnSync() function
180     bool renderClearFilteredCacheAfterDrawing_ = false;
181     bool renderFilterHashChanged_ = false;
182     bool renderForceClearCacheForLastFrame_ = false;
183     bool renderIsEffectNode_ = false;
184     bool renderIsSkipFrame_ = false;
185     bool renderIntersectWithDRM_  = false;
186     bool renderIsDarkColorMode_  = false;
187 
188     // the type cache needed clear before drawing
189     FilterCacheType stagingClearType_ = FilterCacheType::NONE;
190     FilterCacheType renderClearType_ = FilterCacheType::NONE;
191     FilterCacheType lastCacheType_ = FilterCacheType::NONE;
192     bool stagingIsOccluded_ = false;
193 
194     // force cache with cacheUpdateInterval_
195     bool stagingIsLargeArea_ = false;
196     bool canSkipFrame_ = false;
197     bool stagingIsSkipFrame_  = false;
198     RSFilter::FilterType filterType_ = RSFilter::NONE;
199     int cacheUpdateInterval_ = 0;
200     bool isFilterCacheValid_ = false; // catch status in current frame
201     bool pendingPurge_ = false;
202 
203     std::unique_ptr<RSFilterCacheManager> cacheManager_;
204     NodeId stagingNodeId_ = INVALID_NODEID;
205     NodeId renderNodeId_ = INVALID_NODEID;
206 };
207 } // namespace DrawableV2
208 } // namespace OHOS::Rosen
209 #endif // RENDER_SERVICE_BASE_DRAWABLE_RS_PROPERTY_DRAWABLE_H
210