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