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