• 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 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 num_args,const char ** arg_names,const unsigned char *,const unsigned long long * arg_values,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        case 'C': {
83              for(int i=0; i<num_args ; i++)
84              {
85                  ATRACE_INT(arg_names[i],arg_values[i]);
86              }
87              break;
88         }
89         default:
90             // Could handle other event types here
91             break;
92     }
93     // Return any non-zero handle to avoid assert in ANGLE
94     TraceEventHandle result = 1.0;
95     return result;
96 }
97 
assignAnglePlatformMethods(PlatformMethods * platformMethods)98 static void assignAnglePlatformMethods(PlatformMethods* platformMethods) {
99     platformMethods->addTraceEvent = addTraceEvent;
100     platformMethods->getTraceCategoryEnabledFlag = getTraceCategoryEnabledFlag;
101     platformMethods->monotonicallyIncreasingTime = monotonicallyIncreasingTime;
102     platformMethods->logError = logError;
103     platformMethods->logWarning = logWarning;
104     platformMethods->logInfo = logInfo;
105 }
106 
107 // Initialize function ptrs for ANGLE PlatformMethods struct, used for systrace
initializeAnglePlatform(EGLDisplay dpy,android::egl_connection_t * const cnx)108 bool initializeAnglePlatform(EGLDisplay dpy, android::egl_connection_t* const cnx) {
109     if (cnx->angleGetDisplayPlatformFunc == nullptr) {
110         ALOGE("ANGLEGetDisplayPlatform is not initialized!");
111         return false;
112     }
113 
114     GetDisplayPlatformFunc angleGetDisplayPlatform =
115             reinterpret_cast<GetDisplayPlatformFunc>(cnx->angleGetDisplayPlatformFunc);
116 
117     PlatformMethods* platformMethods = nullptr;
118     if (!((angleGetDisplayPlatform)(dpy, g_PlatformMethodNames, g_NumPlatformMethods, nullptr,
119                                     &platformMethods))) {
120         ALOGE("ANGLEGetDisplayPlatform call failed!");
121         return false;
122     }
123     if (platformMethods) {
124         assignAnglePlatformMethods(platformMethods);
125     } else {
126         ALOGE("In initializeAnglePlatform() platformMethods struct ptr is NULL. Not assigning "
127               "tracing function ptrs!");
128     }
129     return true;
130 }
131 
resetAnglePlatform(EGLDisplay dpy,android::egl_connection_t * const cnx)132 void resetAnglePlatform(EGLDisplay dpy, android::egl_connection_t* const cnx) {
133     if (cnx->angleResetDisplayPlatformFunc) {
134         ResetDisplayPlatformFunc angleResetDisplayPlatform =
135                 reinterpret_cast<ResetDisplayPlatformFunc>(cnx->angleResetDisplayPlatformFunc);
136         angleResetDisplayPlatform(dpy);
137     }
138 }
139 
140 }; // namespace angle
141 
142 #endif // __ANDROID__
143