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 #ifndef RENDER_SERVICE_CORE_RS_RCD_SURFACE_RENDER_NODE_H
17 #define RENDER_SERVICE_CORE_RS_RCD_SURFACE_RENDER_NODE_H
18 
19 #include <atomic>
20 #include <ibuffer_consumer_listener.h>
21 #include <memory>
22 #include <surface.h>
23 
24 #include "common/rs_rect.h"
25 #include "pipeline/rs_render_node.h"
26 #include "pipeline/rs_surface_handler.h"
27 #include "pipeline/round_corner_display/rs_round_corner_config.h"
28 #ifdef NEW_RENDER_CONTEXT
29 #include "rs_render_surface.h"
30 #include "render_context_base.h"
31 #else
32 #include "platform/drawing/rs_surface.h"
33 #include "render_context/render_context.h"
34 #endif
35 #include "sync_fence.h"
36 #include <filesystem>
37 #include "include/core/SkBitmap.h"
38 
39 namespace OHOS {
40 namespace Rosen {
41 
42 
43 enum class RCDSurfaceType : uint32_t {
44     BOTTOM,
45     TOP,
46     INVALID
47 };
48 
49 struct RcdExtInfo {
50     bool surfaceCreated = false;
51     RectI srcRect_;
52     RectI dstRect_;
53 
54     RectF surfaceBounds;
55     RectF frameBounds;
56     RectF frameViewPort;
57 
58     RCDSurfaceType surfaceType = RCDSurfaceType::INVALID;
59 
ClearRcdExtInfo60     void Clear()
61     {
62         surfaceBounds.Clear();
63         frameBounds.Clear();
64         frameViewPort.Clear();
65     }
66 
GetFrameOffsetXRcdExtInfo67     float GetFrameOffsetX() const
68     {
69         return frameViewPort.GetLeft() - surfaceBounds.GetLeft();
70     }
71 
GetFrameOffsetYRcdExtInfo72     float GetFrameOffsetY() const
73     {
74         return frameViewPort.GetTop() - surfaceBounds.GetTop();
75     }
76 };
77 
78 struct RcdSourceInfo {
79     uint32_t bufferWidth = 0;
80     uint32_t bufferHeight = 0;
81     uint32_t bufferSize = 0;
82 };
83 
84 struct HardwareLayerInfo {
85     std::filesystem::path pathBin;
86     int bufferSize = 0;
87     int cldWidth = 0;
88     int cldHeight = 0;
89 };
90 
91 struct CldInfo {
92     uint32_t cldDataOffset = 0;
93     uint32_t cldSize = 0;
94     uint32_t cldWidth = 0;
95     uint32_t cldHeight = 0;
96     uint32_t cldStride = 0;
97     uint32_t exWidth = 0;
98     uint32_t exHeight = 0;
99     uint32_t baseColor = 0;
100 };
101 
102 class RSRcdSurfaceRenderNode : public RSRenderNode, public RSSurfaceHandler {
103 public:
104     using WeakPtr = std::weak_ptr<RSRcdSurfaceRenderNode>;
105     using SharedPtr = std::shared_ptr<RSRcdSurfaceRenderNode>;
106 
107     RSRcdSurfaceRenderNode(NodeId id, RCDSurfaceType type, const std::weak_ptr<RSContext>& context = {});
108     static SharedPtr Create(NodeId id, RCDSurfaceType type);
109     ~RSRcdSurfaceRenderNode() override;
110 
111     const RectI& GetSrcRect() const;
112     const RectI& GetDstRect() const;
113 
114     bool CreateSurface(sptr<IBufferConsumerListener> listener);
115     bool IsSurfaceCreated() const;
116 #ifdef NEW_RENDER_CONTEXT
117     std::shared_ptr<RSRenderSurface> GetRSSurface() const;
118 #else
119     std::shared_ptr<RSSurface> GetRSSurface() const;
120 #endif
121     sptr<IBufferConsumerListener> GetConsumerListener() const;
122     RcdSourceInfo rcdSourceInfo;
123     void SetRcdBufferWidth(uint32_t width);
124     void SetRcdBufferHeight(uint32_t height);
125     void SetRcdBufferSize(uint32_t bufferSize);
126     void ClearBufferCache();
127     void ResetCurrFrameState();
128     void Reset();
129     bool SetHardwareResourceToBuffer();
130     BufferRequestConfig GetHardenBufferRequestConfig() const;
131     bool PrepareHardwareResourceBuffer(const std::shared_ptr<rs_rcd::RoundCornerLayer>& layerInfo);
132     bool IsBottomSurface() const;
133     bool IsTopSurface() const;
134     bool IsInvalidSurface() const;
135 
136     float GetFrameOffsetX() const;
137     float GetFrameOffsetY() const;
138 
139     const CldInfo& GetCldInfo() const;
140 
141     void SetRenderTargetId(NodeId id);
142 
143 private:
144     float GetSurfaceWidth() const;
145     float GetSurfaceHeight() const;
146     bool FillHardwareResource(HardwareLayerInfo &cldLayerInfo, int height, int width);
147     HardwareLayerInfo cldLayerInfo;
148     Drawing::Bitmap layerBitmap;
149 
150     uint32_t GetRcdBufferWidth() const;
151     uint32_t GetRcdBufferHeight() const;
152     uint32_t GetRcdBufferSize() const;
153 
154 #ifdef NEW_RENDER_CONTEXT
155     std::shared_ptr<RSRenderSurface> surface_ = nullptr;
156 #else
157     std::shared_ptr<RSSurface> surface_ = nullptr;
158 #endif
159     sptr<IBufferConsumerListener> consumerListener_;
160 
161     RcdExtInfo rcdExtInfo_;
162 
163     CldInfo cldInfo_;
164 
165     NodeId renerTargetId_ = 0;
166 };
167 } // namespace Rosen
168 } // namespace OHOS
169 #endif // RENDER_SERVICE_CORE_RS_RCD_SURFACE_RENDER_NODE_H