Hash :
d33a2222
Author :
Date :
2021-04-26T16:56:15
Upstream Apple's direct-to-Metal backend: compile libANGLE. This change is meant to merge the metal backend changes from Apple's direct-to-Metal backend. Taken from Kyle Piddington's CL: https://chromium-review.googlesource.com/c/angle/angle/+/2857366/ The goal of this CL is to merge the metal backend code in a state that compiles, but not to switch the Metal backend over to using the direct-to-metal backend yet. Bug: angleproject:5505 Bug: angleproject:6127 Change-Id: If6783e06e0086b3a1dd25c6f53caca5cfc96cb86 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2950067 Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Reviewed-by: Kenneth Russell <kbr@chromium.org> Commit-Queue: Jonah Ryan-Davis <jonahr@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
//
// Copyright 2020 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// DisplayEAGL.cpp: EAGL implementation of egl::Display
#import "common/platform.h"
#if defined(ANGLE_ENABLE_EAGL)
# import "libANGLE/renderer/gl/eagl/DisplayEAGL.h"
# import "common/debug.h"
# import "gpu_info_util/SystemInfo.h"
# import "libANGLE/Display.h"
# import "libANGLE/renderer/gl/eagl/ContextEAGL.h"
# import "libANGLE/renderer/gl/eagl/DeviceEAGL.h"
# import "libANGLE/renderer/gl/eagl/FunctionsEAGL.h"
# import "libANGLE/renderer/gl/eagl/IOSurfaceSurfaceEAGL.h"
# import "libANGLE/renderer/gl/eagl/PbufferSurfaceEAGL.h"
# import "libANGLE/renderer/gl/eagl/RendererEAGL.h"
# import "libANGLE/renderer/gl/eagl/WindowSurfaceEAGL.h"
# import <Foundation/Foundation.h>
# import <QuartzCore/QuartzCore.h>
# import <dlfcn.h>
namespace
{
const char *kOpenGLESDylibName = "/System/Library/Frameworks/OpenGLES.framework/OpenGLES";
}
namespace rx
{
class FunctionsGLEAGL : public FunctionsGL
{
public:
FunctionsGLEAGL(void *dylibHandle) : mDylibHandle(dylibHandle) {}
~FunctionsGLEAGL() override { dlclose(mDylibHandle); }
private:
void *loadProcAddress(const std::string &function) const override
{
return dlsym(mDylibHandle, function.c_str());
}
void *mDylibHandle;
};
DisplayEAGL::DisplayEAGL(const egl::DisplayState &state)
: DisplayGL(state), mEGLDisplay(nullptr), mContext(nullptr)
{}
DisplayEAGL::~DisplayEAGL() {}
egl::Error DisplayEAGL::initialize(egl::Display *display)
{
mEGLDisplay = display;
angle::SystemInfo info;
// It's legal for GetSystemInfo to return false and thereby
// contain incomplete information.
(void)angle::GetSystemInfo(&info);
mContext = [allocEAGLContextInstance() initWithAPI:kEAGLRenderingAPIOpenGLES3];
if (mContext == nullptr)
{
return egl::EglNotInitialized() << "Could not create the EAGL context.";
}
if (![getEAGLContextClass() setCurrentContext:mContext])
{
return egl::EglNotInitialized() << "Could set the EAGL context current.";
}
mThreadsWithContextCurrent.insert(std::this_thread::get_id());
// There is no equivalent getProcAddress in EAGL so we open the dylib directly
void *handle = dlopen(kOpenGLESDylibName, RTLD_NOW);
if (!handle)
{
return egl::EglNotInitialized() << "Could not open the OpenGLES Framework.";
}
std::unique_ptr<FunctionsGL> functionsGL(new FunctionsGLEAGL(handle));
functionsGL->initialize(display->getAttributeMap());
mRenderer.reset(new RendererEAGL(std::move(functionsGL), display->getAttributeMap(), this));
const gl::Version &maxVersion = mRenderer->getMaxSupportedESVersion();
if (maxVersion < gl::Version(2, 0))
{
return egl::EglNotInitialized() << "OpenGL ES 2.0 is not supportable.";
}
auto &attributes = display->getAttributeMap();
mDeviceContextIsVolatile =
attributes.get(EGL_PLATFORM_ANGLE_DEVICE_CONTEXT_VOLATILE_EAGL_ANGLE, GL_FALSE);
return DisplayGL::initialize(display);
}
void DisplayEAGL::terminate()
{
DisplayGL::terminate();
mRenderer.reset();
if (mContext != nullptr)
{
[getEAGLContextClass() setCurrentContext:nil];
[mContext release];
mContext = nullptr;
mThreadsWithContextCurrent.clear();
}
}
egl::Error DisplayEAGL::prepareForCall()
{
auto threadId = std::this_thread::get_id();
if (mDeviceContextIsVolatile ||
mThreadsWithContextCurrent.find(threadId) == mThreadsWithContextCurrent.end())
{
if (![getEAGLContextClass() setCurrentContext:mContext])
{
return egl::EglBadAlloc() << "Could not make device EAGL context current.";
}
mThreadsWithContextCurrent.insert(threadId);
}
return egl::NoError();
}
egl::Error DisplayEAGL::releaseThread()
{
auto threadId = std::this_thread::get_id();
if (mThreadsWithContextCurrent.find(threadId) != mThreadsWithContextCurrent.end())
{
if (![getEAGLContextClass() setCurrentContext:nil])
{
return egl::EglBadAlloc() << "Could not release device EAGL context.";
}
mThreadsWithContextCurrent.erase(threadId);
}
return egl::NoError();
}
SurfaceImpl *DisplayEAGL::createWindowSurface(const egl::SurfaceState &state,
EGLNativeWindowType window,
const egl::AttributeMap &attribs)
{
return new WindowSurfaceEAGL(state, mRenderer.get(), window, mContext);
}
SurfaceImpl *DisplayEAGL::createPbufferSurface(const egl::SurfaceState &state,
const egl::AttributeMap &attribs)
{
EGLint width = static_cast<EGLint>(attribs.get(EGL_WIDTH, 0));
EGLint height = static_cast<EGLint>(attribs.get(EGL_HEIGHT, 0));
return new PbufferSurfaceEAGL(state, mRenderer.get(), width, height);
}
SurfaceImpl *DisplayEAGL::createPbufferFromClientBuffer(const egl::SurfaceState &state,
EGLenum buftype,
EGLClientBuffer clientBuffer,
const egl::AttributeMap &attribs)
{
ASSERT(buftype == EGL_IOSURFACE_ANGLE);
return new IOSurfaceSurfaceEAGL(state, mContext, clientBuffer, attribs);
}
SurfaceImpl *DisplayEAGL::createPixmapSurface(const egl::SurfaceState &state,
NativePixmapType nativePixmap,
const egl::AttributeMap &attribs)
{
UNIMPLEMENTED();
return nullptr;
}
ContextImpl *DisplayEAGL::createContext(const gl::State &state,
gl::ErrorSet *errorSet,
const egl::Config *configuration,
const gl::Context *shareContext,
const egl::AttributeMap &attribs)
{
return new ContextEAGL(state, errorSet, mRenderer);
}
DeviceImpl *DisplayEAGL::createDevice()
{
return new DeviceEAGL();
}
egl::ConfigSet DisplayEAGL::generateConfigs()
{
// TODO(cwallez): generate more config permutations
egl::ConfigSet configs;
const gl::Version &maxVersion = getMaxSupportedESVersion();
ASSERT(maxVersion >= gl::Version(2, 0));
bool supportsES3 = maxVersion >= gl::Version(3, 0);
egl::Config config;
// Native stuff
config.nativeVisualID = 0;
config.nativeVisualType = 0;
config.nativeRenderable = EGL_TRUE;
// Buffer sizes
config.redSize = 8;
config.greenSize = 8;
config.blueSize = 8;
config.alphaSize = 8;
config.depthSize = 24;
config.stencilSize = 8;
config.colorBufferType = EGL_RGB_BUFFER;
config.luminanceSize = 0;
config.alphaMaskSize = 0;
config.bufferSize = config.redSize + config.greenSize + config.blueSize + config.alphaSize;
config.transparentType = EGL_NONE;
// Pbuffer
config.maxPBufferWidth = 4096;
config.maxPBufferHeight = 4096;
config.maxPBufferPixels = 4096 * 4096;
// Caveat
config.configCaveat = EGL_NONE;
// Misc
config.sampleBuffers = 0;
config.samples = 0;
config.level = 0;
config.bindToTextureRGB = EGL_FALSE;
config.bindToTextureRGBA = EGL_FALSE;
config.bindToTextureTarget = EGL_TEXTURE_2D;
config.surfaceType = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
config.minSwapInterval = 1;
config.maxSwapInterval = 1;
config.renderTargetFormat = GL_RGBA8;
config.depthStencilFormat = GL_DEPTH24_STENCIL8;
config.conformant = EGL_OPENGL_ES2_BIT | (supportsES3 ? EGL_OPENGL_ES3_BIT_KHR : 0);
config.renderableType = config.conformant;
config.matchNativePixmap = EGL_NONE;
config.colorComponentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
configs.add(config);
return configs;
}
bool DisplayEAGL::testDeviceLost()
{
// TODO(cwallez) investigate implementing this
return false;
}
egl::Error DisplayEAGL::restoreLostDevice(const egl::Display *display)
{
UNIMPLEMENTED();
return egl::EglBadDisplay();
}
bool DisplayEAGL::isValidNativeWindow(EGLNativeWindowType window) const
{
NSObject *layer = reinterpret_cast<NSObject *>(window);
return [layer isKindOfClass:[CALayer class]];
}
egl::Error DisplayEAGL::validateClientBuffer(const egl::Config *configuration,
EGLenum buftype,
EGLClientBuffer clientBuffer,
const egl::AttributeMap &attribs) const
{
ASSERT(buftype == EGL_IOSURFACE_ANGLE);
if (!IOSurfaceSurfaceEAGL::validateAttributes(clientBuffer, attribs))
{
return egl::EglBadAttribute();
}
return egl::NoError();
}
EAGLContextObj DisplayEAGL::getEAGLContext() const
{
return mContext;
}
void DisplayEAGL::generateExtensions(egl::DisplayExtensions *outExtensions) const
{
outExtensions->iosurfaceClientBuffer = true;
outExtensions->surfacelessContext = true;
// Contexts are virtualized so textures and semaphores can be shared globally
outExtensions->displayTextureShareGroup = true;
outExtensions->displaySemaphoreShareGroup = true;
outExtensions->powerPreference = false;
DisplayGL::generateExtensions(outExtensions);
}
void DisplayEAGL::generateCaps(egl::Caps *outCaps) const
{
outCaps->textureNPOT = true;
}
egl::Error DisplayEAGL::waitClient(const gl::Context *context)
{
// TODO(cwallez) UNIMPLEMENTED()
return egl::NoError();
}
egl::Error DisplayEAGL::waitNative(const gl::Context *context, EGLint engine)
{
// TODO(cwallez) UNIMPLEMENTED()
return egl::NoError();
}
gl::Version DisplayEAGL::getMaxSupportedESVersion() const
{
return mRenderer->getMaxSupportedESVersion();
}
egl::Error DisplayEAGL::makeCurrentSurfaceless(gl::Context *context)
{
// We have nothing to do as mContext is always current, and that EAGL is surfaceless by
// default.
return egl::NoError();
}
class WorkerContextEAGL final : public WorkerContext
{
public:
WorkerContextEAGL(EAGLContextObj context);
~WorkerContextEAGL() override;
bool makeCurrent() override;
void unmakeCurrent() override;
private:
EAGLContextObj mContext;
};
WorkerContextEAGL::WorkerContextEAGL(EAGLContextObj context) : mContext(context) {}
WorkerContextEAGL::~WorkerContextEAGL()
{
[getEAGLContextClass() setCurrentContext:nil];
mContext = nullptr;
}
bool WorkerContextEAGL::makeCurrent()
{
if (![getEAGLContextClass() setCurrentContext:static_cast<EAGLContext *>(mContext)])
{
ERR() << "Unable to make gl context current.";
return false;
}
return true;
}
void WorkerContextEAGL::unmakeCurrent()
{
[getEAGLContextClass() setCurrentContext:nil];
}
WorkerContext *DisplayEAGL::createWorkerContext(std::string *infoLog)
{
EAGLContextObj context = nullptr;
context = [allocEAGLContextInstance() initWithAPI:kEAGLRenderingAPIOpenGLES3];
if (!context)
{
*infoLog += "Could not create the EAGL context.";
return nullptr;
}
return new WorkerContextEAGL(context);
}
void DisplayEAGL::initializeFrontendFeatures(angle::FrontendFeatures *features) const
{
mRenderer->initializeFrontendFeatures(features);
}
void DisplayEAGL::populateFeatureList(angle::FeatureList *features)
{
mRenderer->getFeatures().populateFeatureList(features);
}
RendererGL *DisplayEAGL::getRenderer() const
{
return mRenderer.get();
}
}
#endif // defined(ANGLE_ENABLE_EAGL)