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_RS_COLD_START_THREAD_H 17 #define RS_CORE_PIPELINE_RS_COLD_START_THREAD_H 18 19 #include <condition_variable> 20 #include <functional> 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <queue> 25 #include <thread> 26 #include <vector> 27 #include <event_handler.h> 28 29 #include "include/core/SkImage.h" 30 #include "include/core/SkRefCnt.h" 31 #include "include/core/SkSurface.h" 32 #ifdef RS_ENABLE_GL 33 #include "common/rs_shared_context.h" 34 #endif 35 #include "pipeline/rs_render_node_map.h" 36 #include "pipeline/rs_surface_render_node.h" 37 38 class GrContext; 39 namespace OHOS { 40 namespace Rosen { 41 class DrawCmdList; 42 class RSColdStartThread final { 43 public: 44 RSColdStartThread(std::weak_ptr<RSSurfaceRenderNode> surfaceRenderNode, NodeId surfaceNodeId); 45 ~RSColdStartThread(); 46 #ifndef USE_ROSEN_DRAWING 47 void PostPlayBackTask(std::shared_ptr<DrawCmdList> drawCmdList, float width, float height); 48 #else 49 void PostPlayBackTask(std::shared_ptr<Drawing::DrawCmdList> drawCmdList, float width, float height); 50 #endif 51 void Stop(); 52 void PostTask(std::function<void()> task); 53 bool IsIdle(); 54 55 private: 56 #ifdef RS_ENABLE_GL 57 void Run(EGLContext context); 58 #else 59 void Run(); 60 #endif 61 62 std::weak_ptr<RSSurfaceRenderNode> surfaceNode_; 63 NodeId surfaceNodeId_; 64 std::unique_ptr<std::thread> thread_ = nullptr; 65 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 66 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 67 std::atomic_bool isRunning_ = false; 68 #ifdef RS_ENABLE_GL 69 std::shared_ptr<RSSharedContext> context_ = nullptr; 70 #endif 71 #ifndef USE_ROSEN_DRAWING 72 #ifdef NEW_SKIA 73 sk_sp<GrDirectContext> grContext_ = nullptr; 74 #else 75 sk_sp<GrContext> grContext_ = nullptr; 76 #endif 77 sk_sp<SkSurface> skSurface_; 78 #else 79 std::shared_ptr<Drawing::GPUContext> gpuContext_; 80 std::shared_ptr<Drawing::Surface> drSurface_; 81 #endif 82 std::mutex mutex_; 83 std::mutex imageMutex_; 84 #ifndef USE_ROSEN_DRAWING 85 std::queue<sk_sp<SkImage>> images_; 86 #else 87 std::queue<std::shared_ptr<Drawing::Image>> images_; 88 #endif 89 std::condition_variable cv_; 90 }; 91 92 class RSColdStartManager { 93 public: 94 static RSColdStartManager& Instance(); 95 #ifndef USE_ROSEN_DRAWING 96 void PostPlayBackTask(NodeId id, std::shared_ptr<DrawCmdList> drawCmdList, float width, float height); 97 #else 98 void PostPlayBackTask(NodeId id, std::shared_ptr<Drawing::DrawCmdList> drawCmdList, float width, float height); 99 #endif 100 bool IsColdStartThreadRunning(NodeId id); 101 bool IsColdStartThreadIdle(NodeId id); 102 void StartColdStartThreadIfNeed(std::shared_ptr<RSSurfaceRenderNode> surfaceNode); 103 void StopColdStartThread(NodeId id); 104 void DestroyColdStartThread(NodeId id); 105 void CheckColdStartMap(const RSRenderNodeMap& nodeMap); 106 107 private: 108 RSColdStartManager() = default; 109 ~RSColdStartManager() = default; 110 RSColdStartManager(const RSColdStartManager& manager); 111 RSColdStartManager(const RSColdStartManager&& manager); 112 RSColdStartManager& operator=(const RSColdStartManager& manager); 113 RSColdStartManager& operator=(const RSColdStartManager&& manager); 114 115 std::map<NodeId, std::unique_ptr<RSColdStartThread>> coldStartThreadMap_; 116 }; 117 } // namespace Rosen 118 } // namespace OHOS 119 #endif // RS_CORE_PIPELINE_RS_COLD_START_THREAD_H 120