SkATrace.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "src/core/SkATrace.h"
  8. #include "src/core/SkTraceEvent.h"
  9. #include "src/core/SkTraceEventCommon.h"
  10. #ifdef SK_BUILD_FOR_ANDROID
  11. #include <dlfcn.h>
  12. #endif
  13. SkATrace::SkATrace() : fBeginSection(nullptr), fEndSection(nullptr), fIsEnabled(nullptr) {
  14. #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
  15. fIsEnabled = []{ return static_cast<bool>(CC_UNLIKELY(ATRACE_ENABLED())); };
  16. fBeginSection = [](const char* name){ ATRACE_BEGIN(name); };
  17. fEndSection = []{ ATRACE_END(); };
  18. #elif defined(SK_BUILD_FOR_ANDROID)
  19. if (void* lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL)) {
  20. fBeginSection = (decltype(fBeginSection))dlsym(lib, "ATrace_beginSection");
  21. fEndSection = (decltype(fEndSection))dlsym(lib, "ATrace_endSection");
  22. fIsEnabled = (decltype(fIsEnabled))dlsym(lib, "ATrace_isEnabled");
  23. }
  24. #endif
  25. if (!fIsEnabled) {
  26. fIsEnabled = []{ return false; };
  27. }
  28. }
  29. SkEventTracer::Handle SkATrace::addTraceEvent(char phase,
  30. const uint8_t* categoryEnabledFlag,
  31. const char* name,
  32. uint64_t id,
  33. int numArgs,
  34. const char** argNames,
  35. const uint8_t* argTypes,
  36. const uint64_t* argValues,
  37. uint8_t flags) {
  38. if (fIsEnabled()) {
  39. if (TRACE_EVENT_PHASE_COMPLETE == phase ||
  40. TRACE_EVENT_PHASE_INSTANT == phase) {
  41. fBeginSection(name);
  42. }
  43. if (TRACE_EVENT_PHASE_INSTANT == phase) {
  44. fEndSection();
  45. }
  46. }
  47. return 0;
  48. }
  49. void SkATrace::updateTraceEventDuration(const uint8_t* categoryEnabledFlag,
  50. const char* name,
  51. SkEventTracer::Handle handle) {
  52. // This is only ever called from a scoped trace event so we will just end the ATrace section.
  53. if (fIsEnabled()) {
  54. fEndSection();
  55. }
  56. }
  57. const uint8_t* SkATrace::getCategoryGroupEnabled(const char* name) {
  58. // Chrome tracing is setup to not repeatly call this function once it has been initialized. So
  59. // we can't use this to do a check for ATrace isEnabled(). Thus we will always return yes here
  60. // and then check to see if ATrace is enabled when beginning and ending a section.
  61. static uint8_t yes = SkEventTracer::kEnabledForRecording_CategoryGroupEnabledFlags;
  62. return &yes;
  63. }
  64. #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
  65. bool SkAndroidFrameworkTraceUtil::gEnableAndroidTracing = false;
  66. #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK