1 /* 2 * Copyright (c) 2021-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 ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECT_HUB_H 17 #define ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECT_HUB_H 18 19 #include <mutex> 20 #include <platform/ohos/rs_irender_service.h> 21 #include "platform/common/rs_log.h" 22 23 namespace OHOS { 24 namespace Rosen { 25 using OnConnectCallback = std::function<void(sptr<RSIRenderServiceConnection>&)>; 26 class RSRenderServiceConnectHub : public RefBase { 27 public: 28 static sptr<RSIRenderServiceConnection> GetRenderService(); SetOnConnectCallback(OnConnectCallback cb)29 static void SetOnConnectCallback(OnConnectCallback cb) 30 { 31 onConnectCallback_ = cb; 32 // if already connected, call the callback immediately 33 if (instance_ && instance_->conn_ && onConnectCallback_) { 34 onConnectCallback_(instance_->conn_); 35 } 36 } 37 38 private: 39 static sptr<RSRenderServiceConnectHub> GetInstance(); 40 static void Init(); 41 static void Destroy(); 42 43 RSRenderServiceConnectHub(); 44 ~RSRenderServiceConnectHub() noexcept; 45 46 class RenderServiceDeathRecipient final : public IRemoteObject::DeathRecipient { 47 public: RenderServiceDeathRecipient(wptr<RSRenderServiceConnectHub> rsConnHub)48 explicit RenderServiceDeathRecipient(wptr<RSRenderServiceConnectHub> rsConnHub) 49 : rsConnHub_(rsConnHub) {} 50 ~RenderServiceDeathRecipient() noexcept final = default; 51 52 DISALLOW_COPY_AND_MOVE(RenderServiceDeathRecipient); 53 54 void OnRemoteDied(const wptr<IRemoteObject> &remote) final override; 55 56 private: 57 wptr<RSRenderServiceConnectHub> rsConnHub_; 58 }; 59 60 sptr<RSIRenderServiceConnection> GetRenderServiceConnection(); 61 bool Connect(); 62 void ConnectDied(); 63 64 mutable std::mutex mutex_; 65 sptr<RSIRenderService> renderService_; 66 sptr<RSIConnectionToken> token_; 67 sptr<RSIRenderServiceConnection> conn_; 68 sptr<IRemoteObject::DeathRecipient> deathRecipient_; 69 70 static std::once_flag flag_; 71 static sptr<RSRenderServiceConnectHub> instance_; 72 static OnConnectCallback onConnectCallback_; 73 }; 74 } // namespace Rosen 75 } // namespace OHOS 76 77 #endif // ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CONNECT_HUB_H 78