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 IM_RENDER_TASK_ITF_H
17 #define IM_RENDER_TASK_ITF_H
18 
19 #include <memory>
20 #include <future>
21 
22 template <typename RETURNTYPE, typename... ARGSTYPE> class RenderTaskItf {
23 public:
24     typedef RETURNTYPE ReturnType;
25 
26     RenderTaskItf() = default;
27     virtual ~RenderTaskItf() = default;
28 
29     virtual void Run(ARGSTYPE...) = 0;
30 
31     virtual bool operator < (const RenderTaskItf &other)
32     {
33         return this->m_id < other.m_id;
34     };
35 
SetTag(uint64_t tag)36     void SetTag(uint64_t tag)
37     {
38         m_tag = tag;
39     }
40 
GetTag()41     uint64_t GetTag()
42     {
43         return m_tag;
44     }
45 
SetId(uint64_t id)46     void SetId(uint64_t id)
47     {
48         m_id = id;
49     }
50 
GetId()51     uint64_t GetId()
52     {
53         return m_id;
54     }
55 
SetSequenceId(uint64_t id)56     void SetSequenceId(uint64_t id)
57     {
58         m_sequenceId = id;
59     }
60 
GetSequenceId()61     uint64_t GetSequenceId()
62     {
63         return m_sequenceId;
64     }
65 
66     virtual void Wait() = 0;
67 
68     virtual RETURNTYPE GetReturn() = 0;
69 
70     virtual std::shared_future<RETURNTYPE> GetFuture() = 0;
71 
72 protected:
73     uint64_t m_id;
74     uint64_t m_tag;
75     uint64_t m_sequenceId;
76 };
77 
78 template <typename RETURNTYPE, typename... ARGSTYPE>
79 using RenderTaskPtr = std::shared_ptr<RenderTaskItf<RETURNTYPE, ARGSTYPE...>>;
80 
81 template <typename RETURNTYPE, typename... ARGSTYPE> class TaskCompare {
82 public:
operator()83     bool operator () (const RenderTaskPtr<RETURNTYPE, ARGSTYPE...> &a, const RenderTaskPtr<RETURNTYPE, ARGSTYPE...> &b)
84     {
85         return !((*(a.get())) < (*(b.get())));
86     }
87 };
88 
GetTag(const RenderTaskPtr<RETURNTYPE,ARGSTYPE...> & a)89 template <typename RETURNTYPE, typename... ARGSTYPE> uint64_t GetTag(const RenderTaskPtr<RETURNTYPE, ARGSTYPE...> &a)
90 {
91     return (*(a.get())).GetTag();
92 }
93 
94 using RenderCommonTaskPtr = RenderTaskPtr<void>;
95 using RenderTaskWithIdPtr = RenderTaskPtr<void, uint64_t>;
96 #endif