1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // CLEvent.h: Defines the cl::Event class, which can be used to track the execution status of an
7 // OpenCL command.
8
9 #ifndef LIBANGLE_CLEVENT_H_
10 #define LIBANGLE_CLEVENT_H_
11
12 #include "libANGLE/CLObject.h"
13 #include "libANGLE/renderer/CLEventImpl.h"
14
15 #include "common/SynchronizedValue.h"
16
17 #include <array>
18
19 namespace cl
20 {
21
22 class Event final : public _cl_event, public Object
23 {
24 public:
25 // Front end entry functions, only called from OpenCL entry points
26
27 angle::Result setUserEventStatus(cl_int executionStatus);
28
29 angle::Result getInfo(EventInfo name,
30 size_t valueSize,
31 void *value,
32 size_t *valueSizeRet) const;
33
34 angle::Result setCallback(cl_int commandExecCallbackType, EventCB pfnNotify, void *userData);
35
36 angle::Result getProfilingInfo(ProfilingInfo name,
37 size_t valueSize,
38 void *value,
39 size_t *valueSizeRet);
40
41 public:
42 ~Event() override;
43
44 Context &getContext();
45 const Context &getContext() const;
46 const CommandQueuePtr &getCommandQueue() const;
47 cl_command_type getCommandType() const;
48 bool wasStatusChanged() const;
49
50 template <typename T = rx::CLEventImpl>
51 T &getImpl() const;
52
53 void callback(cl_int commandStatus);
54
55 angle::Result initBackend(const rx::CLEventImpl::CreateFunc &createFunc);
isUserEvent()56 bool isUserEvent() const { return mCommandType == CL_COMMAND_USER; }
57
58 static EventPtrs Cast(cl_uint numEvents, const cl_event *eventList);
59
60 private:
61 using CallbackData = std::pair<EventCB, void *>;
62 using Callbacks = std::vector<CallbackData>;
63
64 Event(Context &context);
65
66 Event(CommandQueue &queue, cl_command_type commandType);
67
68 const ContextPtr mContext;
69 const CommandQueuePtr mCommandQueue;
70 const cl_command_type mCommandType;
71 rx::CLEventImpl::Ptr mImpl;
72
73 bool mStatusWasChanged = false;
74
75 // Create separate storage for each possible callback type.
76 static_assert(CL_COMPLETE == 0 && CL_RUNNING == 1 && CL_SUBMITTED == 2,
77 "OpenCL command execution status values are not as assumed");
78 angle::SynchronizedValue<std::array<Callbacks, 3u>> mCallbacks;
79
80 friend class Object;
81 };
82
getContext()83 inline Context &Event::getContext()
84 {
85 return *mContext;
86 }
87
getContext()88 inline const Context &Event::getContext() const
89 {
90 return *mContext;
91 }
92
getCommandQueue()93 inline const CommandQueuePtr &Event::getCommandQueue() const
94 {
95 return mCommandQueue;
96 }
97
getCommandType()98 inline cl_command_type Event::getCommandType() const
99 {
100 return mCommandType;
101 }
102
wasStatusChanged()103 inline bool Event::wasStatusChanged() const
104 {
105 return mStatusWasChanged;
106 }
107
108 template <typename T>
getImpl()109 inline T &Event::getImpl() const
110 {
111 return static_cast<T &>(*mImpl);
112 }
113
114 } // namespace cl
115
116 #endif // LIBANGLE_CLEVENT_H_
117