• 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 "Loader.h"
20 #include "egl_angle_platform.h"
21 
22 #pragma GCC diagnostic push
23 #pragma GCC diagnostic ignored "-Wunused-parameter"
24 #include <EGL/Platform.h>
25 #pragma GCC diagnostic pop
26 
27 #include <android/dlext.h>
28 #include <dlfcn.h>
29 #include <graphicsenv/GraphicsEnv.h>
30 #include <time.h>
31 #include <log/log.h>
32 
33 namespace angle {
34 
35 static GetDisplayPlatformFunc angleGetDisplayPlatform = nullptr;
36 static ResetDisplayPlatformFunc angleResetDisplayPlatform = nullptr;
37 
38 static time_t startTime = time(nullptr);
39 
getTraceCategoryEnabledFlag(PlatformMethods *,const char *)40 static const unsigned char* getTraceCategoryEnabledFlag(PlatformMethods* /*platform*/,
41                                                         const char* /*categoryName*/) {
42     // Returning ptr to 'g' (non-zero) to ALWAYS enable tracing initially.
43     // This ptr is what will be passed into "category_group_enabled" of addTraceEvent
44     static const unsigned char traceEnabled = 'g';
45     return &traceEnabled;
46 }
47 
monotonicallyIncreasingTime(PlatformMethods *)48 static double monotonicallyIncreasingTime(PlatformMethods* /*platform*/) {
49     return difftime(time(nullptr), startTime);
50 }
51 
logError(PlatformMethods *,const char * errorMessage)52 static void logError(PlatformMethods* /*platform*/, const char* errorMessage) {
53     ALOGE("ANGLE Error:%s", errorMessage);
54 }
55 
logWarning(PlatformMethods *,const char * warningMessage)56 static void logWarning(PlatformMethods* /*platform*/, const char* warningMessage) {
57     ALOGW("ANGLE Warn:%s", warningMessage);
58 }
59 
logInfo(PlatformMethods *,const char * infoMessage)60 static void logInfo(PlatformMethods* /*platform*/, const char* infoMessage) {
61     ALOGD("ANGLE Info:%s", infoMessage);
62 }
63 
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)64 static TraceEventHandle addTraceEvent(
65         PlatformMethods* /**platform*/, char phase, const unsigned char* /*category_group_enabled*/,
66         const char* name, unsigned long long /*id*/, double /*timestamp*/, int /*num_args*/,
67         const char** /*arg_names*/, const unsigned char* /*arg_types*/,
68         const unsigned long long* /*arg_values*/, unsigned char /*flags*/) {
69     switch (phase) {
70         case 'B': {
71             ATRACE_BEGIN(name);
72             break;
73         }
74         case 'E': {
75             ATRACE_END();
76             break;
77         }
78         case 'I': {
79             ATRACE_NAME(name);
80             break;
81         }
82         default:
83             // Could handle other event types here
84             break;
85     }
86     // Return any non-zero handle to avoid assert in ANGLE
87     TraceEventHandle result = 1.0;
88     return result;
89 }
90 
assignAnglePlatformMethods(PlatformMethods * platformMethods)91 static void assignAnglePlatformMethods(PlatformMethods* platformMethods) {
92     platformMethods->addTraceEvent = addTraceEvent;
93     platformMethods->getTraceCategoryEnabledFlag = getTraceCategoryEnabledFlag;
94     platformMethods->monotonicallyIncreasingTime = monotonicallyIncreasingTime;
95     platformMethods->logError = logError;
96     platformMethods->logWarning = logWarning;
97     platformMethods->logInfo = logInfo;
98 }
99 
100 // Initialize function ptrs for ANGLE PlatformMethods struct, used for systrace
initializeAnglePlatform(EGLDisplay dpy)101 bool initializeAnglePlatform(EGLDisplay dpy) {
102     // Since we're inside libEGL, use dlsym to lookup fptr for ANGLEGetDisplayPlatform
103     android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
104     const android_dlextinfo dlextinfo = {
105             .flags = ANDROID_DLEXT_USE_NAMESPACE,
106             .library_namespace = ns,
107     };
108     void* so = android_dlopen_ext("libGLESv2_angle.so", RTLD_LOCAL | RTLD_NOW, &dlextinfo);
109     angleGetDisplayPlatform =
110             reinterpret_cast<GetDisplayPlatformFunc>(dlsym(so, "ANGLEGetDisplayPlatform"));
111 
112     if (!angleGetDisplayPlatform) {
113         ALOGE("dlsym lookup of ANGLEGetDisplayPlatform in libEGL_angle failed!");
114         return false;
115     }
116 
117     angleResetDisplayPlatform =
118             reinterpret_cast<ResetDisplayPlatformFunc>(
119                     eglGetProcAddress("ANGLEResetDisplayPlatform"));
120 
121     PlatformMethods* platformMethods = nullptr;
122     if (!((angleGetDisplayPlatform)(dpy, g_PlatformMethodNames,
123                                                               g_NumPlatformMethods, nullptr,
124                                                               &platformMethods))) {
125         ALOGE("ANGLEGetDisplayPlatform call failed!");
126         return false;
127     }
128     if (platformMethods) {
129         assignAnglePlatformMethods(platformMethods);
130     } else {
131         ALOGE("In initializeAnglePlatform() platformMethods struct ptr is NULL. Not assigning "
132               "tracing function ptrs!");
133     }
134     return true;
135 }
136 
resetAnglePlatform(EGLDisplay dpy)137 void resetAnglePlatform(EGLDisplay dpy) {
138     if (angleResetDisplayPlatform) {
139         angleResetDisplayPlatform(dpy);
140     }
141 }
142 
143 }; // namespace angle
144 
145 #endif // __ANDROID__
146