1 /* 2 * Copyright (c) 2022-2025 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_screen_render_node.h" 24 #include "pipeline/rs_surface_render_node.h" 25 #include "sync_fence.h" 26 #ifdef RS_ENABLE_VK 27 #include "platform/ohos/backend/rs_surface_frame_ohos_vulkan.h" 28 #endif // RS_ENABLE_VK 29 #ifdef USE_M133_SKIA 30 #include "include/gpu/ganesh/GrDirectContext.h" 31 #else 32 #include "include/gpu/GrDirectContext.h" 33 #endif 34 #include "rs_base_render_util.h" 35 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 #if (defined(RS_ENABLE_EGLIMAGE) && defined(RS_ENABLE_GPU)) || defined(RS_ENABLE_VK) 42 #include "feature/gpuComposition/rs_image_manager.h" 43 #endif // RS_ENABLE_EGLIMAGE 44 #ifdef USE_VIDEO_PROCESSING_ENGINE 45 #include "colorspace_converter_display.h" 46 #endif 47 48 namespace OHOS { 49 namespace Rosen { 50 namespace DrawableV2 { 51 class RSSurfaceRenderNodeDrawable; 52 } 53 struct FrameContextConfig { 54 public: FrameContextConfigFrameContextConfig55 explicit FrameContextConfig(bool isProtected) 56 { 57 this->isProtected = isProtected; 58 } 59 bool isProtected = false; 60 bool isVirtual = false; 61 int32_t timeOut = 3000; // ms 62 }; 63 // The RenderFrame can do auto flush 64 class RSRenderFrame { 65 public: 66 // we guarantee when constructing this object, all parameters are valid. RSRenderFrame(const std::shared_ptr<RSSurfaceOhos> & target,std::unique_ptr<RSSurfaceFrame> && frame)67 RSRenderFrame(const std::shared_ptr<RSSurfaceOhos>& target, std::unique_ptr<RSSurfaceFrame>&& frame) 68 : targetSurface_(target), surfaceFrame_(std::move(frame)) 69 { 70 } ~RSRenderFrame()71 ~RSRenderFrame() noexcept 72 { 73 Flush(); 74 } 75 76 // noncopyable 77 RSRenderFrame(const RSRenderFrame&) = delete; 78 void operator=(const RSRenderFrame&) = delete; Flush()79 void Flush() noexcept 80 { 81 if (targetSurface_ != nullptr && surfaceFrame_ != nullptr) { 82 targetSurface_->FlushFrame(surfaceFrame_); 83 #ifdef RS_ENABLE_VK 84 auto frameOhosVulkan = static_cast<RSSurfaceFrameOhosVulkan*>(surfaceFrame_.get()); 85 if (frameOhosVulkan) { 86 acquireFence_ = frameOhosVulkan->GetAcquireFence(); 87 } 88 #endif // RS_ENABLE_VK 89 targetSurface_ = nullptr; 90 surfaceFrame_ = nullptr; 91 } 92 } GetSurface()93 const std::shared_ptr<RSSurfaceOhos>& GetSurface() const 94 { 95 return targetSurface_; 96 } GetFrame()97 const std::unique_ptr<RSSurfaceFrame>& GetFrame() const 98 { 99 return surfaceFrame_; 100 } GetAcquireFence()101 const sptr<SyncFence>& GetAcquireFence() const 102 { 103 return acquireFence_; 104 } GetCanvas()105 std::unique_ptr<RSPaintFilterCanvas> GetCanvas() 106 { 107 return std::make_unique<RSPaintFilterCanvas>(surfaceFrame_->GetSurface().get()); 108 } 109 GetBufferAge()110 int32_t GetBufferAge() 111 { 112 return surfaceFrame_ != nullptr ? surfaceFrame_->GetBufferAge() : 0; 113 } 114 SetDamageRegion(const std::vector<RectI> & rects)115 void SetDamageRegion(const std::vector<RectI> &rects) 116 { 117 if (surfaceFrame_ == nullptr) { 118 return; 119 } 120 const auto surface = surfaceFrame_->GetSurface(); 121 if (surface != nullptr) { 122 surfaceFrame_->SetDamageRegion( 123 CheckAndVerifyDamageRegion(rects, RectI(0, 0, surface->Width(), surface->Height()))); 124 } 125 } 126 // some frame maynot need to call FlushFrame Reset()127 void Reset() 128 { 129 targetSurface_ = nullptr; 130 surfaceFrame_ = nullptr; 131 } 132 133 protected: 134 std::vector<RectI> CheckAndVerifyDamageRegion(const std::vector<RectI>& rects, 135 const RectI& surfaceRect) const; 136 137 private: 138 std::shared_ptr<RSSurfaceOhos> targetSurface_; 139 std::unique_ptr<RSSurfaceFrame> surfaceFrame_; 140 sptr<SyncFence> acquireFence_ = SyncFence::InvalidFence(); 141 }; 142 143 // function that will be called before drawing Buffer / Image. 144 using PreProcessFunc = std::function<void(RSPaintFilterCanvas&, BufferDrawParam&)>; 145 // function that will be called after drawing Buffer / Image. 146 using PostProcessFunc = std::function<void(RSPaintFilterCanvas&, BufferDrawParam&)>; 147 148 struct VideoInfo { 149 std::shared_ptr<Drawing::ColorSpace> drawingColorSpace_ = nullptr; 150 #ifdef USE_VIDEO_PROCESSING_ENGINE 151 GSError retGetColorSpaceInfo_ = GSERROR_OK; 152 Media::VideoProcessingEngine::ColorSpaceConverterDisplayParameter parameter_ = {}; 153 #endif 154 }; 155 156 // This render engine aims to do the client composition for all surfaces that hardware can't handle. 157 class RSBaseRenderEngine { 158 public: 159 RSBaseRenderEngine(); 160 virtual ~RSBaseRenderEngine() noexcept; 161 void Init(); 162 RSBaseRenderEngine(const RSBaseRenderEngine&) = delete; 163 void operator=(const RSBaseRenderEngine&) = delete; 164 165 // [PLANNING]: this is a work-around for the lack of colorgamut conversion and yuv support in GPU. 166 // We should remove this function in someday. 167 static bool NeedForceCPU(const std::vector<LayerInfoPtr>& layers); 168 169 // There would only one user(thread) to renderFrame(request frame) at one time. 170 // for framebuffer surface 171 std::unique_ptr<RSRenderFrame> RequestFrame(const sptr<Surface>& targetSurface, 172 const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true, 173 const FrameContextConfig& frameContextConfig = FrameContextConfig(false)); 174 175 // There would only one user(thread) to renderFrame(request frame) at one time. 176 std::unique_ptr<RSRenderFrame> RequestFrame(const std::shared_ptr<RSSurfaceOhos>& rsSurface, 177 const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true, 178 const FrameContextConfig& frameContextConfig = FrameContextConfig(false)); 179 std::shared_ptr<RSSurfaceOhos> MakeRSSurface(const sptr<Surface>& targetSurface, bool forceCPU); 180 static void SetUiTimeStamp(const std::unique_ptr<RSRenderFrame>& renderFrame, 181 std::shared_ptr<RSSurfaceOhos> surfaceOhos); 182 183 virtual void DrawSurfaceNodeWithParams(RSPaintFilterCanvas& canvas, RSSurfaceRenderNode& node, 184 BufferDrawParam& params, PreProcessFunc preProcess = nullptr, PostProcessFunc postProcess = nullptr) = 0; 185 virtual void DrawSurfaceNodeWithParams(RSPaintFilterCanvas& canvas, 186 DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable, BufferDrawParam& params, 187 PreProcessFunc preProcess = nullptr, PostProcessFunc postProcess = nullptr) {} 188 189 virtual void DrawHDRCacheWithParams(RSPaintFilterCanvas& canvas, BufferDrawParam& params) = 0; 190 void DrawScreenNodeWithParams(RSPaintFilterCanvas& canvas, RSScreenRenderNode& node, 191 BufferDrawParam& params); 192 void DrawScreenNodeWithParams(RSPaintFilterCanvas& canvas, RSSurfaceHandler& surfaceHandler, 193 BufferDrawParam& params); 194 void RegisterDeleteBufferListener(const sptr<IConsumerSurface>& consumer, bool isForUniRedraw = false); 195 void RegisterDeleteBufferListener(RSSurfaceHandler& handler); 196 std::shared_ptr<Drawing::Image> CreateImageFromBuffer(RSPaintFilterCanvas& canvas, 197 BufferDrawParam& params, VideoInfo& videoInfo); 198 #ifdef USE_VIDEO_PROCESSING_ENGINE 199 virtual void DrawLayers(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers, bool forceCPU = false, 200 const ScreenInfo& screenInfo = {}, GraphicColorGamut colorGamut = GRAPHIC_COLOR_GAMUT_SRGB) = 0; 201 #else 202 virtual void DrawLayers(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers, bool forceCPU = false, 203 const ScreenInfo& screenInfo = {}) = 0; 204 #endif 205 206 static void DrawBuffer(RSPaintFilterCanvas& canvas, BufferDrawParam& params); 207 208 void ShrinkCachesIfNeeded(bool isForUniRedraw = false); 209 void ClearCacheSet(const std::set<uint32_t>& unmappedCache); 210 static void SetColorFilterMode(ColorFilterMode mode); 211 static ColorFilterMode GetColorFilterMode(); 212 static void SetHighContrast(bool enabled); 213 static bool IsHighContrastEnabled(); 214 215 #if (defined RS_ENABLE_GL) || (defined RS_ENABLE_VK) GetRenderContext()216 const std::shared_ptr<RenderContext>& GetRenderContext() 217 { 218 return renderContext_; 219 } 220 #endif // RS_ENABLE_GL || RS_ENABLE_VK 221 void ResetCurrentContext(); 222 223 #if (defined(RS_ENABLE_EGLIMAGE) && defined(RS_ENABLE_GPU)) || defined(RS_ENABLE_VK) GetImageManager()224 const std::shared_ptr<RSImageManager>& GetImageManager() 225 { 226 return imageManager_; 227 } 228 #endif // RS_ENABLE_EGLIMAGE 229 #ifdef USE_VIDEO_PROCESSING_ENGINE 230 void ColorSpaceConvertor(std::shared_ptr<Drawing::ShaderEffect>& inputShader, BufferDrawParam& params, 231 Media::VideoProcessingEngine::ColorSpaceConverterDisplayParameter& parameter, 232 const RSPaintFilterCanvas::HDRProperties& hdrProperties = RSPaintFilterCanvas::HDRProperties{}); 233 #endif 234 static std::shared_ptr<Drawing::ColorSpace> ConvertColorGamutToDrawingColorSpace(GraphicColorGamut colorGamut); 235 static std::shared_ptr<Drawing::ColorSpace> ConvertColorSpaceNameToDrawingColorSpace( 236 OHOS::ColorManager::ColorSpaceName colorSpaceName); 237 #ifdef RS_ENABLE_VK GetSkContext()238 const std::shared_ptr<Drawing::GPUContext> GetSkContext() const 239 { 240 return skContext_; 241 } GetCaptureSkContext()242 const std::shared_ptr<Drawing::GPUContext> GetCaptureSkContext() const 243 { 244 return captureSkContext_; 245 } 246 #endif 247 void DumpVkImageInfo(std::string &dumpString); 248 protected: 249 void DrawImage(RSPaintFilterCanvas& canvas, BufferDrawParam& params); 250 251 static inline std::mutex colorFilterMutex_; 252 static inline ColorFilterMode colorFilterMode_ = ColorFilterMode::COLOR_FILTER_END; 253 static inline std::atomic_bool isHighContrastEnabled_ = false; 254 255 private: 256 static void DrawImageRect(RSPaintFilterCanvas& canvas, std::shared_ptr<Drawing::Image> image, 257 BufferDrawParam& params, Drawing::SamplingOptions& samplingOptions); 258 259 static bool NeedBilinearInterpolation(const BufferDrawParam& params, const Drawing::Matrix& matrix); 260 261 static std::shared_ptr<Drawing::ColorSpace> GetCanvasColorSpace(const RSPaintFilterCanvas& canvas); 262 263 #if (defined RS_ENABLE_GL) || (defined RS_ENABLE_VK) 264 std::shared_ptr<RenderContext> renderContext_ = nullptr; 265 #endif // RS_ENABLE_GL || RS_ENABLE_VK 266 #ifdef RS_ENABLE_VK 267 std::shared_ptr<Drawing::GPUContext> skContext_ = nullptr; 268 std::shared_ptr<Drawing::GPUContext> captureSkContext_ = nullptr; 269 #endif 270 std::shared_ptr<RSImageManager> imageManager_ = nullptr; 271 using SurfaceId = uint64_t; 272 #ifdef USE_VIDEO_PROCESSING_ENGINE 273 static bool SetColorSpaceConverterDisplayParameter( 274 const BufferDrawParam& params, Media::VideoProcessingEngine::ColorSpaceConverterDisplayParameter& parameter); 275 static bool ConvertDrawingColorSpaceToSpaceInfo(const std::shared_ptr<Drawing::ColorSpace>& colorSpace, 276 HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceInfo& colorSpaceInfo); 277 std::shared_ptr<Media::VideoProcessingEngine::ColorSpaceConverterDisplay> colorSpaceConverterDisplay_ = nullptr; 278 #endif 279 }; 280 } // namespace Rosen 281 } // namespace OHOS 282 #endif // RS_CORE_PIPELINE_BASE_RENDER_ENGINE_H 283