1 //
2 // Copyright 2016 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
7 // FunctionsEGLDL.cpp: Implements the FunctionsEGLDL class.
8
9 #include "libANGLE/renderer/gl/egl/FunctionsEGLDL.h"
10
11 #include <dlfcn.h>
12
13 namespace rx
14 {
15 namespace
16 {
17 // In ideal world, we would want this to be a member of FunctionsEGLDL,
18 // and call dlclose() on it in ~FunctionsEGLDL().
19 // However, some GL implementations are broken and don't allow multiple
20 // load/unload cycles, but only static linking with them.
21 // That's why we dlopen() this handle once and never dlclose() it.
22 // This is consistent with Chromium's CleanupNativeLibraries() code,
23 // referencing crbug.com/250813 and http://www.xfree86.org/4.3.0/DRI11.html
24 void *nativeEGLHandle;
25 } // anonymous namespace
26
FunctionsEGLDL()27 FunctionsEGLDL::FunctionsEGLDL() : mGetProcAddressPtr(nullptr) {}
28
~FunctionsEGLDL()29 FunctionsEGLDL::~FunctionsEGLDL() {}
30
initialize(EGLAttrib platformType,EGLNativeDisplayType nativeDisplay,const char * libName,void * eglHandle)31 egl::Error FunctionsEGLDL::initialize(EGLAttrib platformType,
32 EGLNativeDisplayType nativeDisplay,
33 const char *libName,
34 void *eglHandle)
35 {
36
37 if (eglHandle)
38 {
39 // If the handle is provided, use it.
40 // Caller has already dlopened the vendor library.
41 nativeEGLHandle = eglHandle;
42 }
43
44 if (!nativeEGLHandle)
45 {
46 nativeEGLHandle = dlopen(libName, RTLD_NOW);
47 if (!nativeEGLHandle)
48 {
49 std::ostringstream err;
50 err << "Could not dlopen native EGL: " << dlerror();
51 return egl::Error(EGL_NOT_INITIALIZED, err.str());
52 }
53 }
54
55 mGetProcAddressPtr =
56 reinterpret_cast<PFNEGLGETPROCADDRESSPROC>(dlsym(nativeEGLHandle, "eglGetProcAddress"));
57 if (!mGetProcAddressPtr)
58 {
59 return egl::Error(EGL_NOT_INITIALIZED, "Could not find eglGetProcAddress");
60 }
61
62 return FunctionsEGL::initialize(platformType, nativeDisplay);
63 }
64
getProcAddress(const char * name) const65 void *FunctionsEGLDL::getProcAddress(const char *name) const
66 {
67 void *f = reinterpret_cast<void *>(mGetProcAddressPtr(name));
68 if (f)
69 {
70 return f;
71 }
72 return dlsym(nativeEGLHandle, name);
73 }
74
75 } // namespace rx
76