• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #if defined(__ANDROID__)
18 
19 #include "egl_angle_platform.h"
20 
21 #pragma GCC diagnostic push
22 #pragma GCC diagnostic ignored "-Wunused-parameter"
23 #include <EGL/Platform.h>
24 #pragma GCC diagnostic pop
25 
26 #include <android-base/properties.h>
27 #include <android/dlext.h>
28 #include <dlfcn.h>
29 #include <graphicsenv/GraphicsEnv.h>
30 #include <log/log.h>
31 #include <time.h>
32 #include <vndksupport/linker.h>
33 
34 #include "Loader.h"
35 
36 namespace angle {
37 
38 constexpr int kAngleDlFlags = RTLD_LOCAL | RTLD_NOW;
39 
40 static GetDisplayPlatformFunc angleGetDisplayPlatform = nullptr;
41 static ResetDisplayPlatformFunc angleResetDisplayPlatform = nullptr;
42 
43 static time_t startTime = time(nullptr);
44 
getTraceCategoryEnabledFlag(PlatformMethods *,const char *)45 static const unsigned char* getTraceCategoryEnabledFlag(PlatformMethods* /*platform*/,
46                                                         const char* /*categoryName*/) {
47     // Returning ptr to 'g' (non-zero) to ALWAYS enable tracing initially.
48     // This ptr is what will be passed into "category_group_enabled" of addTraceEvent
49     static const unsigned char traceEnabled = 'g';
50     return &traceEnabled;
51 }
52 
monotonicallyIncreasingTime(PlatformMethods *)53 static double monotonicallyIncreasingTime(PlatformMethods* /*platform*/) {
54     return difftime(time(nullptr), startTime);
55 }
56 
logError(PlatformMethods *,const char * errorMessage)57 static void logError(PlatformMethods* /*platform*/, const char* errorMessage) {
58     ALOGE("ANGLE Error:%s", errorMessage);
59 }
60 
logWarning(PlatformMethods *,const char * warningMessage)61 static void logWarning(PlatformMethods* /*platform*/, const char* warningMessage) {
62     ALOGW("ANGLE Warn:%s", warningMessage);
63 }
64 
logInfo(PlatformMethods *,const char * infoMessage)65 static void logInfo(PlatformMethods* /*platform*/, const char* infoMessage) {
66     ALOGD("ANGLE Info:%s", infoMessage);
67 }
68 
addTraceEvent(PlatformMethods *,char phase,const unsigned char *,const char * name,unsigned long long,double,int,const char **,const unsigned char *,const unsigned long long *,unsigned char)69 static TraceEventHandle addTraceEvent(
70         PlatformMethods* /**platform*/, char phase, const unsigned char* /*category_group_enabled*/,
71         const char* name, unsigned long long /*id*/, double /*timestamp*/, int /*num_args*/,
72         const char** /*arg_names*/, const unsigned char* /*arg_types*/,
73         const unsigned long long* /*arg_values*/, unsigned char /*flags*/) {
74     switch (phase) {
75         case 'B': {
76             ATRACE_BEGIN(name);
77             break;
78         }
79         case 'E': {
80             ATRACE_END();
81             break;
82         }
83         case 'I': {
84             ATRACE_NAME(name);
85             break;
86         }
87         default:
88             // Could handle other event types here
89             break;
90     }
91     // Return any non-zero handle to avoid assert in ANGLE
92     TraceEventHandle result = 1.0;
93     return result;
94 }
95 
assignAnglePlatformMethods(PlatformMethods * platformMethods)96 static void assignAnglePlatformMethods(PlatformMethods* platformMethods) {
97     platformMethods->addTraceEvent = addTraceEvent;
98     platformMethods->getTraceCategoryEnabledFlag = getTraceCategoryEnabledFlag;
99     platformMethods->monotonicallyIncreasingTime = monotonicallyIncreasingTime;
100     platformMethods->logError = logError;
101     platformMethods->logWarning = logWarning;
102     platformMethods->logInfo = logInfo;
103 }
104 
105 // Initialize function ptrs for ANGLE PlatformMethods struct, used for systrace
initializeAnglePlatform(EGLDisplay dpy)106 bool initializeAnglePlatform(EGLDisplay dpy) {
107     // Since we're inside libEGL, use dlsym to lookup fptr for ANGLEGetDisplayPlatform
108     android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
109     void* so = nullptr;
110     if (ns) {
111         // Loading from an APK, so hard-code the suffix to "_angle".
112         constexpr char kAngleEs2Lib[] = "libGLESv2_angle.so";
113         const android_dlextinfo dlextinfo = {
114                 .flags = ANDROID_DLEXT_USE_NAMESPACE,
115                 .library_namespace = ns,
116         };
117         so = android_dlopen_ext(kAngleEs2Lib, kAngleDlFlags, &dlextinfo);
118         if (so) {
119             ALOGD("dlopen_ext from APK (%s) success at %p", kAngleEs2Lib, so);
120         } else {
121             ALOGE("dlopen_ext(\"%s\") failed: %s", kAngleEs2Lib, dlerror());
122             return false;
123         }
124     } else {
125         // If we are here, ANGLE is loaded as built-in gl driver in the sphal.
126         // Get the specified ANGLE library filename suffix.
127         std::string angleEs2LibSuffix = android::base::GetProperty("ro.hardware.egl", "");
128         if (angleEs2LibSuffix.empty()) {
129             ALOGE("%s failed to get valid ANGLE library filename suffix!", __FUNCTION__);
130             return false;
131         }
132 
133         std::string angleEs2LibName = "libGLESv2_" + angleEs2LibSuffix + ".so";
134         so = android_load_sphal_library(angleEs2LibName.c_str(), kAngleDlFlags);
135         if (so) {
136             ALOGD("dlopen (%s) success at %p", angleEs2LibName.c_str(), so);
137         } else {
138             ALOGE("%s failed to dlopen %s!", __FUNCTION__, angleEs2LibName.c_str());
139             return false;
140         }
141     }
142 
143     angleGetDisplayPlatform =
144             reinterpret_cast<GetDisplayPlatformFunc>(dlsym(so, "ANGLEGetDisplayPlatform"));
145 
146     if (!angleGetDisplayPlatform) {
147         ALOGE("dlsym lookup of ANGLEGetDisplayPlatform in libEGL_angle failed!");
148         return false;
149     }
150 
151     angleResetDisplayPlatform =
152             reinterpret_cast<ResetDisplayPlatformFunc>(dlsym(so, "ANGLEResetDisplayPlatform"));
153 
154     PlatformMethods* platformMethods = nullptr;
155     if (!((angleGetDisplayPlatform)(dpy, g_PlatformMethodNames, g_NumPlatformMethods, nullptr,
156                                     &platformMethods))) {
157         ALOGE("ANGLEGetDisplayPlatform call failed!");
158         return false;
159     }
160     if (platformMethods) {
161         assignAnglePlatformMethods(platformMethods);
162     } else {
163         ALOGE("In initializeAnglePlatform() platformMethods struct ptr is NULL. Not assigning "
164               "tracing function ptrs!");
165     }
166     return true;
167 }
168 
resetAnglePlatform(EGLDisplay dpy)169 void resetAnglePlatform(EGLDisplay dpy) {
170     if (angleResetDisplayPlatform) {
171         angleResetDisplayPlatform(dpy);
172     }
173 }
174 
175 }; // namespace angle
176 
177 #endif // __ANDROID__
178