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
isUserEvent()41 bool isUserEvent() const { return mCommandType == CL_COMMAND_USER; }
42
43 public:
44 ~Event() override;
45
46 Context &getContext();
47 const Context &getContext() const;
48 const CommandQueuePtr &getCommandQueue() const;
49 cl_command_type getCommandType() const;
50 bool wasStatusChanged() const;
51
52 template <typename T = rx::CLEventImpl>
53 T &getImpl() const;
54
55 void callback(cl_int commandStatus);
56
57 static EventPtrs Cast(cl_uint numEvents, const cl_event *eventList);
58
59 private:
60 using CallbackData = std::pair<EventCB, void *>;
61 using Callbacks = std::vector<CallbackData>;
62
63 Event(Context &context);
64
65 Event(CommandQueue &queue,
66 cl_command_type commandType,
67 const rx::CLEventImpl::CreateFunc &createFunc);
68
69 const ContextPtr mContext;
70 const CommandQueuePtr mCommandQueue;
71 const cl_command_type mCommandType;
72 rx::CLEventImpl::Ptr mImpl;
73
74 bool mStatusWasChanged = false;
75
76 // Create separate storage for each possible callback type.
77 static_assert(CL_COMPLETE == 0 && CL_RUNNING == 1 && CL_SUBMITTED == 2,
78 "OpenCL command execution status values are not as assumed");
79 angle::SynchronizedValue<std::array<Callbacks, 3u>> mCallbacks;
80
81 friend class Object;
82 };
83
getContext()84 inline Context &Event::getContext()
85 {
86 return *mContext;
87 }
88
getContext()89 inline const Context &Event::getContext() const
90 {
91 return *mContext;
92 }
93
getCommandQueue()94 inline const CommandQueuePtr &Event::getCommandQueue() const
95 {
96 return mCommandQueue;
97 }
98
getCommandType()99 inline cl_command_type Event::getCommandType() const
100 {
101 return mCommandType;
102 }
103
wasStatusChanged()104 inline bool Event::wasStatusChanged() const
105 {
106 return mStatusWasChanged;
107 }
108
109 template <typename T>
getImpl()110 inline T &Event::getImpl() const
111 {
112 return static_cast<T &>(*mImpl);
113 }
114
115 } // namespace cl
116
117 #endif // LIBANGLE_CLEVENT_H_
118