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 // CLPlatform.h: Defines the cl::Platform class, which provides information about platform-specific
7 // OpenCL features.
8
9 #ifndef LIBANGLE_CLPLATFORM_H_
10 #define LIBANGLE_CLPLATFORM_H_
11
12 #include "libANGLE/CLObject.h"
13 #include "libANGLE/Context.h"
14
15 #include "common/WorkerThread.h"
16
17 #include "libANGLE/renderer/CLPlatformImpl.h"
18
19 #include "anglebase/no_destructor.h"
20
21 namespace cl
22 {
23
24 class Platform final : public _cl_platform_id, public Object
25 {
26 public:
27 // Front end entry functions, only called from OpenCL entry points
28
29 static void Initialize(const cl_icd_dispatch &dispatch,
30 rx::CLPlatformImpl::CreateFuncs &&createFuncs);
31
32 static Platform *GetDefault();
33 static Platform *CastOrDefault(cl_platform_id platform);
34 static bool IsValidOrDefault(const _cl_platform_id *platform);
35
36 static angle::Result GetPlatformIDs(cl_uint numEntries,
37 cl_platform_id *platforms,
38 cl_uint *numPlatforms);
39
40 angle::Result getInfo(PlatformInfo name,
41 size_t valueSize,
42 void *value,
43 size_t *valueSizeRet) const;
44
45 angle::Result getDeviceIDs(DeviceType deviceType,
46 cl_uint numEntries,
47 cl_device_id *devices,
48 cl_uint *numDevices) const;
49
50 bool hasDeviceType(DeviceType) const;
51
52 static cl_context CreateContext(const cl_context_properties *properties,
53 cl_uint numDevices,
54 const cl_device_id *devices,
55 ContextErrorCB notify,
56 void *userData);
57
58 static cl_context CreateContextFromType(const cl_context_properties *properties,
59 DeviceType deviceType,
60 ContextErrorCB notify,
61 void *userData);
62
63 angle::Result unloadCompiler();
64
65 public:
66 ~Platform() override;
67
68 const rx::CLPlatformImpl::Info &getInfo() const;
69 cl_version getVersion() const;
70 bool isVersionOrNewer(cl_uint major, cl_uint minor) const;
71 const DevicePtrs &getDevices() const;
72
73 template <typename T = rx::CLPlatformImpl>
74 T &getImpl() const;
75
76 static const PlatformPtrs &GetPlatforms();
77
78 static constexpr const char *GetVendor();
79
80 const std::shared_ptr<angle::WorkerThreadPool> &getMultiThreadPool() const;
81
82 angle::FrameCaptureShared *getFrameCaptureShared();
83
84 private:
85 explicit Platform(const rx::CLPlatformImpl::CreateFunc &createFunc);
86
87 DevicePtrs createDevices(rx::CLDeviceImpl::CreateDatas &&createDatas);
88
89 static PlatformPtrs &GetPointers();
90
91 const rx::CLPlatformImpl::Ptr mImpl;
92 const rx::CLPlatformImpl::Info mInfo;
93 const DevicePtrs mDevices;
94 std::shared_ptr<angle::WorkerThreadPool> mMultiThreadPool;
95
96 static constexpr char kVendor[] = "ANGLE";
97 static constexpr char kIcdSuffix[] = "ANGLE";
98
99 static angle::FrameCaptureShared *mFrameCaptureShared;
100 };
101
GetDefault()102 inline Platform *Platform::GetDefault()
103 {
104 return GetPlatforms().empty() ? nullptr : GetPlatforms().front().get();
105 }
106
CastOrDefault(cl_platform_id platform)107 inline Platform *Platform::CastOrDefault(cl_platform_id platform)
108 {
109 return platform != nullptr ? &platform->cast<Platform>() : GetDefault();
110 }
111
112 // Our CL implementation defines that a nullptr value chooses the platform that we provide as
113 // default, so this function returns true for a nullptr value if a default platform exists.
IsValidOrDefault(const _cl_platform_id * platform)114 inline bool Platform::IsValidOrDefault(const _cl_platform_id *platform)
115 {
116 return platform != nullptr ? IsValid(platform) : GetDefault() != nullptr;
117 }
118
getInfo()119 inline const rx::CLPlatformImpl::Info &Platform::getInfo() const
120 {
121 return mInfo;
122 }
123
getVersion()124 inline cl_version Platform::getVersion() const
125 {
126 return mInfo.version;
127 }
128
isVersionOrNewer(cl_uint major,cl_uint minor)129 inline bool Platform::isVersionOrNewer(cl_uint major, cl_uint minor) const
130 {
131 return mInfo.version >= CL_MAKE_VERSION(major, minor, 0u);
132 }
133
getDevices()134 inline const DevicePtrs &Platform::getDevices() const
135 {
136 return mDevices;
137 }
138
139 template <typename T>
getImpl()140 inline T &Platform::getImpl() const
141 {
142 return static_cast<T &>(*mImpl);
143 }
144
GetPlatforms()145 inline const PlatformPtrs &Platform::GetPlatforms()
146 {
147 return GetPointers();
148 }
149
GetVendor()150 constexpr const char *Platform::GetVendor()
151 {
152 return kVendor;
153 }
154
getMultiThreadPool()155 inline const std::shared_ptr<angle::WorkerThreadPool> &Platform::getMultiThreadPool() const
156 {
157 return mMultiThreadPool;
158 }
159
GetPointers()160 inline PlatformPtrs &Platform::GetPointers()
161 {
162 static angle::base::NoDestructor<PlatformPtrs> sPointers;
163 return *sPointers;
164 }
165
166 } // namespace cl
167
168 #endif // LIBANGLE_CLPLATFORM_H_
169