1 /* 2 * Copyright (c) 2022 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 RS_CORE_PIPELINE_BASE_RENDER_ENGINE_H 17 #define RS_CORE_PIPELINE_BASE_RENDER_ENGINE_H 18 19 #include <memory> 20 21 #include "hdi_layer_info.h" 22 #include "pipeline/rs_paint_filter_canvas.h" 23 #include "pipeline/rs_display_render_node.h" 24 #include "pipeline/rs_surface_render_node.h" 25 #include "rs_base_render_util.h" 26 27 #ifdef NEW_RENDER_CONTEXT 28 #include "render_backend/rs_render_surface_frame.h" 29 #include "ohos/rs_render_surface_ohos.h" 30 #include "render_context_base.h" 31 #else 32 #include "platform/drawing/rs_surface_frame.h" 33 #include "platform/ohos/rs_surface_ohos.h" 34 #if (defined RS_ENABLE_GL) || (defined RS_ENABLE_VK) 35 #include "render_context/render_context.h" 36 #endif // RS_ENABLE_GL || RS_ENABLE_VK 37 #endif 38 #ifdef RS_ENABLE_EGLIMAGE 39 #include "rs_egl_image_manager.h" 40 #endif // RS_ENABLE_EGLIMAGE 41 42 namespace OHOS { 43 namespace Rosen { 44 // The RenderFrame can do auto flush 45 class RSRenderFrame { 46 public: 47 // we guarantee when constructing this object, all parameters are valid. 48 #ifdef NEW_RENDER_CONTEXT RSRenderFrame(const std::shared_ptr<RSRenderSurfaceOhos> & target)49 explicit RSRenderFrame(const std::shared_ptr<RSRenderSurfaceOhos>& target) 50 : targetSurface_(target) 51 { 52 } 53 #else 54 RSRenderFrame(const std::shared_ptr<RSSurfaceOhos>& target, std::unique_ptr<RSSurfaceFrame>&& frame) 55 : targetSurface_(target), surfaceFrame_(std::move(frame)) 56 { 57 } 58 #endif ~RSRenderFrame()59 ~RSRenderFrame() noexcept 60 { 61 Flush(); 62 } 63 64 // noncopyable 65 RSRenderFrame(const RSRenderFrame&) = delete; 66 void operator=(const RSRenderFrame&) = delete; 67 #ifdef NEW_RENDER_CONTEXT Flush()68 void Flush() noexcept 69 { 70 if (targetSurface_ != nullptr) { 71 targetSurface_->FlushFrame(); 72 targetSurface_ = nullptr; 73 } 74 } 75 #else Flush()76 void Flush() noexcept 77 { 78 if (targetSurface_ != nullptr && surfaceFrame_ != nullptr) { 79 targetSurface_->FlushFrame(surfaceFrame_); 80 targetSurface_ = nullptr; 81 surfaceFrame_ = nullptr; 82 } 83 } 84 #endif 85 #ifdef NEW_RENDER_CONTEXT GetSurface()86 const std::shared_ptr<RSRenderSurfaceOhos>& GetSurface() const 87 #else 88 const std::shared_ptr<RSSurfaceOhos>& GetSurface() const 89 #endif 90 { 91 return targetSurface_; 92 } 93 #ifndef NEW_RENDER_CONTEXT GetFrame()94 const std::unique_ptr<RSSurfaceFrame>& GetFrame() const 95 { 96 return surfaceFrame_; 97 } 98 #endif GetCanvas()99 std::unique_ptr<RSPaintFilterCanvas> GetCanvas() 100 { 101 #ifdef NEW_RENDER_CONTEXT 102 return std::make_unique<RSPaintFilterCanvas>(targetSurface_->GetSurface().get()); 103 #else 104 return std::make_unique<RSPaintFilterCanvas>(surfaceFrame_->GetSurface().get()); 105 #endif 106 } 107 GetBufferAge()108 int32_t GetBufferAge() 109 { 110 #ifdef NEW_RENDER_CONTEXT 111 return targetSurface_ != nullptr ? targetSurface_->GetBufferAge() : 0; 112 #else 113 return surfaceFrame_ != nullptr ? surfaceFrame_->GetBufferAge() : 0; 114 #endif 115 } 116 SetDamageRegion(const std::vector<RectI> & rects)117 void SetDamageRegion(const std::vector<RectI> &rects) 118 { 119 #ifdef NEW_RENDER_CONTEXT 120 if (targetSurface_ != nullptr) { 121 targetSurface_ ->SetDamageRegion(rects); 122 } 123 #else 124 if (surfaceFrame_ != nullptr) { 125 surfaceFrame_->SetDamageRegion(rects); 126 } 127 #endif 128 } 129 private: 130 #ifdef NEW_RENDER_CONTEXT 131 std::shared_ptr<RSRenderSurfaceOhos> targetSurface_; 132 #else 133 std::shared_ptr<RSSurfaceOhos> targetSurface_; 134 std::unique_ptr<RSSurfaceFrame> surfaceFrame_; 135 #endif 136 }; 137 138 // function that will be called before drawing Buffer / Image. 139 using PreProcessFunc = std::function<void(RSPaintFilterCanvas&, BufferDrawParam&)>; 140 // function that will be called after drawing Buffer / Image. 141 using PostProcessFunc = std::function<void(RSPaintFilterCanvas&, BufferDrawParam&)>; 142 143 // This render engine aims to do the client composition for all surfaces that hardware can't handle. 144 class RSBaseRenderEngine { 145 public: 146 RSBaseRenderEngine(); 147 virtual ~RSBaseRenderEngine() noexcept; 148 void Init(); 149 RSBaseRenderEngine(const RSBaseRenderEngine&) = delete; 150 void operator=(const RSBaseRenderEngine&) = delete; 151 152 // [PLANNING]: this is a work-around for the lack of colorgamut conversion and yuv support in GPU. 153 // We should remove this function in someday. 154 static bool NeedForceCPU(const std::vector<LayerInfoPtr>& layers); 155 156 // There would only one user(thread) to renderFrame(request frame) at one time. 157 // for framebuffer surface 158 std::unique_ptr<RSRenderFrame> RequestFrame(const sptr<Surface>& targetSurface, 159 const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true); 160 161 // There would only one user(thread) to renderFrame(request frame) at one time. 162 #ifdef NEW_RENDER_CONTEXT 163 std::unique_ptr<RSRenderFrame> RequestFrame(const std::shared_ptr<RSRenderSurfaceOhos>& rsSurface, 164 const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true); 165 #else 166 std::unique_ptr<RSRenderFrame> RequestFrame(const std::shared_ptr<RSSurfaceOhos>& rsSurface, 167 const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true); 168 #endif 169 void SetUiTimeStamp(const std::unique_ptr<RSRenderFrame>& renderFrame, const uint64_t surfaceId); 170 171 virtual void DrawSurfaceNodeWithParams(RSPaintFilterCanvas& canvas, RSSurfaceRenderNode& node, 172 BufferDrawParam& params, PreProcessFunc preProcess = nullptr, PostProcessFunc postProcess = nullptr) = 0; 173 174 void DrawDisplayNodeWithParams(RSPaintFilterCanvas& canvas, RSDisplayRenderNode& node, 175 BufferDrawParam& params); 176 177 virtual void DrawLayers(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers, bool forceCPU = false, 178 float mirrorAdaptiveCoefficient = 1.0f) = 0; 179 180 void DrawBuffer(RSPaintFilterCanvas& canvas, BufferDrawParam& params); 181 182 void ShrinkCachesIfNeeded(bool isForUniRedraw = false); 183 static void SetColorFilterMode(ColorFilterMode mode); GetColorFilterMode()184 static ColorFilterMode GetColorFilterMode() 185 { 186 return colorFilterMode_; 187 } SetHighContrast(bool enabled)188 static void SetHighContrast(bool enabled) 189 { 190 isHighContrastEnabled_ = enabled; 191 } IsHighContrastEnabled()192 static bool IsHighContrastEnabled() 193 { 194 return isHighContrastEnabled_; 195 } 196 #if defined(NEW_RENDER_CONTEXT) GetRenderContext()197 const std::shared_ptr<RenderContextBase>& GetRenderContext() const 198 { 199 return renderContext_; 200 } GetDrawingContext()201 const std::shared_ptr<DrawingContext>& GetDrawingContext() const 202 { 203 return drawingContext_; 204 } 205 #else 206 #if (defined RS_ENABLE_GL) || (defined RS_ENABLE_VK) GetRenderContext()207 const std::shared_ptr<RenderContext>& GetRenderContext() 208 { 209 return renderContext_; 210 } 211 #endif // RS_ENABLE_GL || RS_ENABLE_VK 212 #endif 213 #ifdef RS_ENABLE_EGLIMAGE GetEglImageManager()214 const std::shared_ptr<RSEglImageManager>& GetEglImageManager() 215 { 216 return eglImageManager_; 217 } 218 #endif // RS_ENABLE_EGLIMAGE 219 void UnMapRsSurface(uint64_t id); 220 protected: 221 void RegisterDeleteBufferListener(const sptr<IConsumerSurface>& consumer, bool isForUniRedraw = false); 222 void RegisterDeleteBufferListener(RSSurfaceHandler& handler); 223 void DrawImage(RSPaintFilterCanvas& canvas, BufferDrawParam& params); 224 225 static inline ColorFilterMode colorFilterMode_ = ColorFilterMode::COLOR_FILTER_END; 226 227 private: 228 #ifndef USE_ROSEN_DRAWING 229 sk_sp<SkImage> CreateEglImageFromBuffer(RSPaintFilterCanvas& canvas, 230 const sptr<SurfaceBuffer>& buffer, const sptr<SyncFence>& acquireFence, 231 const uint32_t threadIndex = UNI_MAIN_THREAD_INDEX); 232 #else 233 std::shared_ptr<Drawing::Image> CreateEglImageFromBuffer(RSPaintFilterCanvas& canvas, 234 const sptr<SurfaceBuffer>& buffer, const sptr<SyncFence>& acquireFence, 235 const uint32_t threadIndex = UNI_MAIN_THREAD_INDEX); 236 #endif 237 238 static inline std::atomic_bool isHighContrastEnabled_ = false; 239 #if defined(NEW_RENDER_CONTEXT) 240 std::shared_ptr<RenderContextBase> renderContext_ = nullptr; 241 std::shared_ptr<DrawingContext> drawingContext_ = nullptr; 242 #else 243 #if (defined RS_ENABLE_GL) || (defined RS_ENABLE_VK) 244 std::shared_ptr<RenderContext> renderContext_ = nullptr; 245 #endif // RS_ENABLE_GL || RS_ENABLE_VK 246 #endif 247 #ifdef RS_ENABLE_EGLIMAGE 248 std::shared_ptr<RSEglImageManager> eglImageManager_ = nullptr; 249 #endif // RS_ENABLE_EGLIMAGE 250 251 // RSSurfaces for framebuffer surfaces. 252 static constexpr size_t MAX_RS_SURFACE_SIZE = 32; // used for rsSurfaces_. 253 using SurfaceId = uint64_t; 254 #ifdef NEW_RENDER_CONTEXT 255 std::unordered_map<SurfaceId, std::shared_ptr<RSRenderSurfaceOhos>> rsSurfaces_; 256 #else 257 std::unordered_map<SurfaceId, std::shared_ptr<RSSurfaceOhos>> rsSurfaces_; 258 #endif 259 }; 260 } // namespace Rosen 261 } // namespace OHOS 262 #endif // RS_CORE_PIPELINE_BASE_RENDER_ENGINE_H 263