• 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 num_args,const char ** arg_names,const unsigned char *,const unsigned long long * arg_values,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        case 'C': {
88              for(int i=0; i<num_args ; i++)
89              {
90                  ATRACE_INT(arg_names[i],arg_values[i]);
91              }
92              break;
93         }
94         default:
95             // Could handle other event types here
96             break;
97     }
98     // Return any non-zero handle to avoid assert in ANGLE
99     TraceEventHandle result = 1.0;
100     return result;
101 }
102 
assignAnglePlatformMethods(PlatformMethods * platformMethods)103 static void assignAnglePlatformMethods(PlatformMethods* platformMethods) {
104     platformMethods->addTraceEvent = addTraceEvent;
105     platformMethods->getTraceCategoryEnabledFlag = getTraceCategoryEnabledFlag;
106     platformMethods->monotonicallyIncreasingTime = monotonicallyIncreasingTime;
107     platformMethods->logError = logError;
108     platformMethods->logWarning = logWarning;
109     platformMethods->logInfo = logInfo;
110 }
111 
112 // Initialize function ptrs for ANGLE PlatformMethods struct, used for systrace
initializeAnglePlatform(EGLDisplay dpy)113 bool initializeAnglePlatform(EGLDisplay dpy) {
114     // Since we're inside libEGL, use dlsym to lookup fptr for ANGLEGetDisplayPlatform
115     android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
116     void* so = nullptr;
117     if (ns) {
118         // Loading from an APK, so hard-code the suffix to "_angle".
119         constexpr char kAngleEs2Lib[] = "libGLESv2_angle.so";
120         const android_dlextinfo dlextinfo = {
121                 .flags = ANDROID_DLEXT_USE_NAMESPACE,
122                 .library_namespace = ns,
123         };
124         so = android_dlopen_ext(kAngleEs2Lib, kAngleDlFlags, &dlextinfo);
125         if (so) {
126             ALOGD("dlopen_ext from APK (%s) success at %p", kAngleEs2Lib, so);
127         } else {
128             ALOGE("dlopen_ext(\"%s\") failed: %s", kAngleEs2Lib, dlerror());
129             return false;
130         }
131     } else {
132         // If we are here, ANGLE is loaded as built-in gl driver in the sphal.
133         // Get the specified ANGLE library filename suffix.
134         std::string angleEs2LibSuffix = android::base::GetProperty("ro.hardware.egl", "");
135         if (angleEs2LibSuffix.empty()) {
136             ALOGE("%s failed to get valid ANGLE library filename suffix!", __FUNCTION__);
137             return false;
138         }
139 
140         std::string angleEs2LibName = "libGLESv2_" + angleEs2LibSuffix + ".so";
141         so = android_load_sphal_library(angleEs2LibName.c_str(), kAngleDlFlags);
142         if (so) {
143             ALOGD("dlopen (%s) success at %p", angleEs2LibName.c_str(), so);
144         } else {
145             ALOGE("%s failed to dlopen %s!", __FUNCTION__, angleEs2LibName.c_str());
146             return false;
147         }
148     }
149 
150     angleGetDisplayPlatform =
151             reinterpret_cast<GetDisplayPlatformFunc>(dlsym(so, "ANGLEGetDisplayPlatform"));
152 
153     if (!angleGetDisplayPlatform) {
154         ALOGE("dlsym lookup of ANGLEGetDisplayPlatform in libEGL_angle failed!");
155         return false;
156     }
157 
158     angleResetDisplayPlatform =
159             reinterpret_cast<ResetDisplayPlatformFunc>(dlsym(so, "ANGLEResetDisplayPlatform"));
160 
161     PlatformMethods* platformMethods = nullptr;
162     if (!((angleGetDisplayPlatform)(dpy, g_PlatformMethodNames, g_NumPlatformMethods, nullptr,
163                                     &platformMethods))) {
164         ALOGE("ANGLEGetDisplayPlatform call failed!");
165         return false;
166     }
167     if (platformMethods) {
168         assignAnglePlatformMethods(platformMethods);
169     } else {
170         ALOGE("In initializeAnglePlatform() platformMethods struct ptr is NULL. Not assigning "
171               "tracing function ptrs!");
172     }
173     return true;
174 }
175 
resetAnglePlatform(EGLDisplay dpy)176 void resetAnglePlatform(EGLDisplay dpy) {
177     if (angleResetDisplayPlatform) {
178         angleResetDisplayPlatform(dpy);
179     }
180 }
181 
182 }; // namespace angle
183 
184 #endif // __ANDROID__
185