egl_angle_platform.cpp 5.0 KB

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