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 void PostPlayBackTask(std::shared_ptr<DrawCmdList> drawCmdList, float width, float height); 47 void Stop(); 48 void PostTask(std::function<void()> task); 49 50 private: 51 #ifdef RS_ENABLE_GL 52 void Run(EGLContext context); 53 #else 54 void Run(); 55 #endif 56 57 std::weak_ptr<RSSurfaceRenderNode> surfaceNode_; 58 NodeId surfaceNodeId_; 59 std::unique_ptr<std::thread> thread_ = nullptr; 60 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 61 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 62 std::atomic_bool isRunning_ = false; 63 #ifdef RS_ENABLE_GL 64 std::shared_ptr<RSSharedContext> context_ = nullptr; 65 #endif 66 sk_sp<GrContext> grContext_; 67 sk_sp<SkSurface> skSurface_; 68 std::mutex mutex_; 69 std::mutex imageMutex_; 70 std::queue<sk_sp<SkImage>> images_; 71 std::condition_variable cv_; 72 }; 73 74 class RSColdStartManager { 75 public: 76 static RSColdStartManager& Instance(); 77 void PostPlayBackTask(NodeId id, std::shared_ptr<DrawCmdList> drawCmdList, float width, float height); 78 bool IsColdStartThreadRunning(NodeId id); 79 void StartColdStartThreadIfNeed(std::shared_ptr<RSSurfaceRenderNode> surfaceNode); 80 void StopColdStartThread(NodeId id); 81 void DestroyColdStartThread(NodeId id); 82 void CheckColdStartMap(const RSRenderNodeMap& nodeMap); 83 84 private: 85 RSColdStartManager() = default; 86 ~RSColdStartManager() = default; 87 RSColdStartManager(const RSColdStartManager& manager); 88 RSColdStartManager(const RSColdStartManager&& manager); 89 RSColdStartManager& operator=(const RSColdStartManager& manager); 90 RSColdStartManager& operator=(const RSColdStartManager&& manager); 91 92 std::map<NodeId, std::unique_ptr<RSColdStartThread>> coldStartThreadMap_; 93 }; 94 } // namespace Rosen 95 } // namespace OHOS 96 #endif // RS_CORE_PIPELINE_RS_COLD_START_THREAD_H 97