1 /* 2 * Copyright (C) 2024 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 OH_VEF_RENDER_TASK_H 17 #define OH_VEF_RENDER_TASK_H 18 19 #include <functional> 20 #include <future> 21 #include "render_task_interface.h" 22 23 namespace OHOS { 24 namespace Media { 25 template <typename RETURNTYPE = void, typename... ARGSTYPE> 26 class RenderTask : public RenderTaskItf<RETURNTYPE, ARGSTYPE...> { 27 public: 28 RenderTask(std::function<RETURNTYPE(ARGSTYPE...)> run, uint64_t tag = 0, uint64_t id = 0) barrier_()29 : barrier_(), barrierFuture_(barrier_.get_future()) 30 { 31 RenderTaskItf<RETURNTYPE, ARGSTYPE...>::SetTag(tag); 32 RenderTaskItf<RETURNTYPE, ARGSTYPE...>::SetId(id); 33 RenderTaskItf<RETURNTYPE, ARGSTYPE...>::SetSequenceId(0); 34 RenderTaskItf<RETURNTYPE, ARGSTYPE...>::SetRunFunc(run); 35 } 36 ~RenderTask() = default; 37 Run(ARGSTYPE...args)38 void Run(ARGSTYPE... args) override 39 { 40 RunImpl<decltype(this), RETURNTYPE, ARGSTYPE...> impl(this); 41 impl(args...); 42 }; 43 Wait()44 void Wait() override 45 { 46 barrierFuture_.wait(); 47 }; 48 GetReturn()49 RETURNTYPE GetReturn() override 50 { 51 return barrierFuture_.get(); 52 }; 53 GetFuture()54 std::shared_future<RETURNTYPE> GetFuture() override 55 { 56 return barrierFuture_; 57 }; 58 59 private: 60 template <typename THISPTYTPE, typename RUNRETURNTYPE, typename... RUNARGSTYPE> class RunImpl { 61 public: RunImpl(THISPTYTPE p)62 explicit RunImpl(THISPTYTPE p) 63 { 64 pType_ = p; 65 } operator()66 void operator () (RUNARGSTYPE... args) 67 { 68 pType_->barrier_.set_value(pType_->runFunc_(args...)); 69 } 70 THISPTYTPE pType_; 71 }; 72 73 template <typename THISPTYTPE, typename... RUNARGSTYPE> struct RunImpl<THISPTYTPE, void, RUNARGSTYPE...> { 74 explicit RunImpl(THISPTYTPE p) 75 { 76 pType_ = p; 77 } 78 void operator () (RUNARGSTYPE... args) 79 { 80 pType_->runFunc_(args...); 81 pType_->barrier_.set_value(); 82 } 83 THISPTYTPE pType_; 84 }; 85 86 private: 87 std::promise<RETURNTYPE> barrier_; 88 std::shared_future<RETURNTYPE> barrierFuture_; 89 }; 90 } 91 } 92 93 #endif