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_INTERFACE_H
17 #define OH_VEF_RENDER_TASK_INTERFACE_H
18
19 #include <memory>
20 #include <future>
21
22 namespace OHOS {
23 namespace Media {
24 template <typename RETURNTYPE, typename... ARGSTYPE> class RenderTaskItf {
25 public:
26 typedef RETURNTYPE ReturnType;
27
28 RenderTaskItf() = default;
29 virtual ~RenderTaskItf() = default;
30
31 virtual void Run(ARGSTYPE...) = 0;
32
33 virtual bool operator < (const RenderTaskItf& other)
34 {
35 return this->id_ < other.id_;
36 };
37
SetTag(uint64_t tag)38 void SetTag(uint64_t tag)
39 {
40 tag_ = tag;
41 }
42
GetTag()43 uint64_t GetTag()
44 {
45 return tag_;
46 }
47
SetId(uint64_t id)48 void SetId(uint64_t id)
49 {
50 id_ = id;
51 }
52
GetId()53 uint64_t GetId()
54 {
55 return id_;
56 }
57
SetSequenceId(uint64_t id)58 void SetSequenceId(uint64_t id)
59 {
60 sequenceId_ = id;
61 }
62
GetSequenceId()63 uint64_t GetSequenceId()
64 {
65 return sequenceId_;
66 }
67
SetRunFunc(std::function<RETURNTYPE (ARGSTYPE...)> run)68 void SetRunFunc(std::function<RETURNTYPE(ARGSTYPE...)> run)
69 {
70 runFunc_ = run;
71 }
72
73 virtual void Wait() = 0;
74
75 virtual RETURNTYPE GetReturn() = 0;
76
77 virtual std::shared_future<RETURNTYPE> GetFuture() = 0;
78
79 protected:
80 uint64_t id_;
81 uint64_t tag_;
82 uint64_t sequenceId_;
83 std::function<RETURNTYPE(ARGSTYPE...)> runFunc_;
84 };
85
86 template <typename RETURNTYPE, typename... ARGSTYPE>
87 using RenderTaskPtr = std::shared_ptr<RenderTaskItf<RETURNTYPE, ARGSTYPE...>>;
88
89 template <typename RETURNTYPE, typename... ARGSTYPE> class TaskCompare {
90 public:
operator()91 bool operator () (const RenderTaskPtr<RETURNTYPE, ARGSTYPE...>& a, const RenderTaskPtr<RETURNTYPE, ARGSTYPE...>& b)
92 {
93 return !((*(a.get())) < (*(b.get())));
94 }
95 };
96
97 template <typename RETURNTYPE, typename... ARGSTYPE>
SetTag(const RenderTaskPtr<RETURNTYPE,ARGSTYPE...> & a,uint64_t tag)98 void SetTag(const RenderTaskPtr<RETURNTYPE, ARGSTYPE...>& a, uint64_t tag)
99 {
100 return (*(a.get())).SetTag(tag);
101 }
102
103 template <typename RETURNTYPE, typename... ARGSTYPE>
SetFunc(const RenderTaskPtr<RETURNTYPE,ARGSTYPE...> & a,std::function<RETURNTYPE (ARGSTYPE...)> run)104 void SetFunc(const RenderTaskPtr<RETURNTYPE, ARGSTYPE...>& a, std::function<RETURNTYPE(ARGSTYPE...)> run)
105 {
106 return (*(a.get())).SetRunFunc(run);
107 }
108
GetTag(const RenderTaskPtr<RETURNTYPE,ARGSTYPE...> & a)109 template <typename RETURNTYPE, typename... ARGSTYPE> uint64_t GetTag(const RenderTaskPtr<RETURNTYPE, ARGSTYPE...>& a)
110 {
111 return (*(a.get())).GetTag();
112 }
113
114 using RenderCommonTaskPtr = RenderTaskPtr<void>;
115 using RenderTaskWithIdPtr = RenderTaskPtr<void, uint64_t>;
116 }
117 }
118
119 #endif